这是我第一次尝试让纹理映射起作用。
这是我的实施:
全球字段:
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。
的的问题: 的