OpenGL很顽固,不允许纹理映射

时间:2011-12-17 00:57:30

标签: c++ opengl

我正在研究一种在MD3模型文件上使用BMP纹理文件的游戏引擎。不幸的是,它根本不起作用。我已经尝试了一切,但无济于事。自从我自己编写了加载程序后,我查看了它们,并修复了它们中的一些错误,但仍然,我只是得到一个灰色的点亮模型。

这是我的渲染代码:

void drawmd3(md3& model, GLuint tex)
{
    int i = 0;
    glLoadIdentity();
    glTranslatef(-1.5,0,-24);
    glRotatef(270,0,0,1);
    glRotatef(-90,0,1,0);
    glRotatef(a,0,0,1);
    while(i<model.surfnum)
    {
        glBindTexture(GL_TEXTURE_2D, tex);
        int j = 0;
        while(j<model.surfs.at(i).trinum)
        {
                int tmp = model.surfs.at(i).tris.at(j).indexes[0];
                int tmp1 = model.surfs.at(i).tris.at(j).indexes[1];
                int tmp2 = model.surfs.at(i).tris.at(j).indexes[2];

            float norms[3];
            norms[0] = convert_normals0(model.surfs.at(i).verts.at(tmp).normal);
            norms[1] = convert_normals1(model.surfs.at(i).verts.at(tmp).normal);
            norms[2] = convert_normals2(model.surfs.at(i).verts.at(tmp).normal);
            float norms1[3];
            norms1[0] = convert_normals0(model.surfs.at(i).verts.at(tmp1).normal);
            norms1[1] = convert_normals1(model.surfs.at(i).verts.at(tmp1).normal);
            norms1[2] = convert_normals2(model.surfs.at(i).verts.at(tmp1).normal);
            float norms2[3];
            norms2[0] = convert_normals0(model.surfs.at(i).verts.at(tmp2).normal);
            norms2[1] = convert_normals1(model.surfs.at(i).verts.at(tmp2).normal);
            norms2[2] = convert_normals2(model.surfs.at(i).verts.at(tmp2).normal);

            glBegin(GL_TRIANGLES);

            glNormal3f(norms[0],norms[1],norms[2]);
            glTexCoord2f(model.surfs.at(i).st.at(tmp).st[0],1-model.surfs.at(i).st.at(tmp).st[1]);
            glVertex3f(model.surfs.at(i).verts.at(tmp).coord[0]/64, model.surfs.at(i).verts.at(tmp).coord[1]/64, model.surfs.at(i).verts.at(tmp).coord[2]/64);
            glNormal3f(norms1[0],norms1[1],norms1[2]);
            glTexCoord2f(model.surfs.at(i).st.at(tmp1).st[0],1-model.surfs.at(i).st.at(tmp1).st[1]);
            glVertex3f(model.surfs.at(i).verts.at(tmp1).coord[0]/64, model.surfs.at(i).verts.at(tmp1).coord[1]/64, model.surfs.at(i).verts.at(tmp1).coord[2]/64);
            glNormal3f(norms2[0],norms2[1],norms2[2]);
            glTexCoord2f(model.surfs.at(i).st.at(tmp2).st[0],1-model.surfs.at(i).st.at(tmp2).st[1]);
            glVertex3f(model.surfs.at(i).verts.at(tmp2).coord[0]/64, model.surfs.at(i).verts.at(tmp2).coord[1]/64, model.surfs.at(i).verts.at(tmp2).coord[2]/64);

            glEnd();
            j++;
        }
        glBindTexture(GL_TEXTURE_2D, 0);
        i++;
    }
}

这是我的初始代码:

glEnable(GL_TEXTURE_2D);

glGenTextures(1,&tex[0]);

glBindTexture(GL_TEXTURE_2D, tex[0]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img.img.imgwidth, img.img.imgheight, 0, GL_RGBA8, GL_UNSIGNED_BYTE, img.data);

1 个答案:

答案 0 :(得分:3)

那就是你的问题:

glTexImage2D(GL_TEXTURE_2D, 
             0, 
             GL_RGBA8, 
             img.img.imgwidth, 
             img.img.imgheight, 
             0, 
             GL_RGBA8,    <<<<<<<<<<----------
             GL_UNSIGNED_BYTE, 
             img.data);

不允许使用您用于格式参数的令牌。来自参考文献:

  

格式

     

指定像素数据的格式。接受以下符号值:GL_COLOR_INDEX,GL_RED,GL_GREEN,GL_BLUE,GL_ALPHA,GL_RGB,GL_BGR,GL_RGBA,GL_BGRA,GL_LUMINANCE和GL_LUMINANCE_ALPHA。

因此,不会加载任何纹理,最终会得到默认的非设置纹理白色。