OpenGL纹理绑定/渲染

时间:2013-09-02 01:19:24

标签: c++ qt opengl textures texture-mapping

这是我第一次尝试让纹理映射起作用。

这是我的实施:

全球字段:

GLuint texture[big];            // Storage for some Textures
int globalcounter;

网格绘制:

ApplyMaterial(mesh->Materials());  //set material properties,works fine
DrawSingleObject(*mesh);

绘制方法:

DrawSingleObject(Mesh& p){

QList<Face>& faces=p.Faces();
int NumberOfFaces=faces.count();
QList<Vector3D>& normals =p.Normals();

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glGenTextures(1, &texture[globalcounter]);                  // Create The Texture
glBindTexture(GL_TEXTURE_2D, texture[globalcounter]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 256 , 256 , 0, GL_RGB, GL_UNSIGNED_BYTE, p.Mat().GetTexture()->Data() );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

for (int t = 0; t < NumberOfFaces; ++t) {
    Face& f = faces[t];
    GLenum face_mode;
    int* lista=f.arrayVertici;
    const QList<Vector3D> vertices=p.Vertices();

    int* listaNorm=f.normalIndex;
    switch(f.VertexNumber) {
    case 1:
        face_mode = GL_POINTS;
        glBegin(face_mode);
        if(hasNormals)
            glNormal3fv(&((normals[listaNorm[0]]).pos[0]));
        glTexCoord2f(?);    
        glVertex3fv(&vertices[lista[0]].pos[0]);                    
        break;
    case 2:
        face_mode = GL_LINES;
        glBegin(face_mode);
        if(hasNormals){
            glNormal3fv(&((normals[(f.normalIndex)[0]]).pos[0]));
            glTexCoord2f(?);
            glVertex3fv(&vertices[lista[0]].pos[0]);
            glNormal3fv(&((normals[(f.normalIndex)[1]]).pos[0]));
            glTexCoord2f(?);
            glVertex3fv(&vertices[lista[1]].pos[0]);
        }
        else{
            glVertex3fv(&vertices[lista[0]].pos[0]);
            glVertex3fv(&vertices[lista[1]].pos[0]);
        }
        break;
    case 3:
        face_mode = GL_TRIANGLES;
        glBegin(face_mode);
        if(hasNormals){
            glNormal3fv(&normals[(f.normalIndex)[0]].pos[0]);
            glTexCoord2f(?);
            glVertex3fv(&vertices[lista[0]].pos[0]);
            glNormal3fv(&normals[(f.normalIndex)[1]].pos[0]);
            glTexCoord2f(?);
            glVertex3fv(&vertices[lista[1]].pos[0]);
            glNormal3fv(&normals[(f.normalIndex)[2]].pos[0]);
            glTexCoord2f(?);
            glVertex3fv(&vertices[lista[2]].pos[0]);

        }
        else{
            glVertex3fv(&vertices[lista[0]].pos[0]);
            glVertex3fv(&vertices[lista[1]].pos[0]);
            glVertex3fv(&vertices[lista[2]].pos[0]);
        }
        break;
    default: face_mode = GL_POLYGON; break;
    }
    glEnd();

}
glDisable(GL_TEXTURE_2D);
}

Mesh有这些字段:

QList<Vector3D> mTextureCoords;     //contains Textures coordinates
Material mMat;

材料有这个字段:

Texture* mTexture;

纹理有这个字段:

QImage* mImage;                     
a dedicated pointer to mImage->bits()

纹理现在正确加载到mImage。

的问题:

  • 绑定:在顶点绘制之前绑定纹理是否正确?这是正确的实现吗?
  • 你能解释一下如何使用Vector3D纹理坐标在特定网格上加载纹理吗?我认为我的主要问题是我渲染迭代遍历面,而纹理的顶点列表是通用的(绑定到网格而不是面)。

0 个答案:

没有答案