这就是它现在的样子(只画了一半的场景让你看到底部的纹理)。我上传了一些屏幕,因为它们超过 1000 字。
缩放(建筑物似乎具有完全随机的坐标) 它应该看起来如何
另一个例子
在这里我加载纹理:
glGenTextures(sceneList.count(), &mTextureID[0]);
for (QList<SceneObject*>::ConstIterator i = sceneList.begin();i!=sceneList.end();++i) {
Mesh* p = dynamic_cast<Mesh*>(*i);
if(p){
if (p->HasTexture()){
QImage* a = p->Mat().GetTexture()->GetImage();
*a= QGLWidget::convertToGLFormat(*a);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mTextureID[counter]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p->Mat().GetTexture()->ImageSize().width() , p->Mat().GetTexture()->ImageSize().height() , 0, GL_RGBA, GL_UNSIGNED_BYTE, a.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glDisable(GL_TEXTURE_2D);
}
}
counter++;
}
}
然后对于每个网格我这样做:
glBindTexture(GL_TEXTURE_2D, mTextureID[counter]); //where counter=texture for current mesh
...
const QList<Vector3D> vertices=p.Vertices();
QList<Face>& faccie=p.Faces();
int numerofacce=faccie.count();
QList<Vector3D>& normals =p.Normals();
int numerovertici=0
for (int t = 0; t < numerofacce; ++t) {
glEnable(GL_TEXTURE_2D);
...
switch(f.numVertici) {
case 1:
a[1]++;
face_mode = GL_POINTS;
glBegin(face_mode);
if(hasNormals)
glNormal3fv(&normals[(f.normalIndex)[0]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numerovertici).x, p.TextureCoords().at(numerovertici).y);
glVertex3fv(&vertices[lista[0]].pos[0]);
numerovertici++;
break;
case 2:
face_mode = GL_LINES;
glBegin(face_mode);
if(hasNormals){
glNormal3fv(&normals[(f.normalIndex)[0]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numerovertici).x, p.TextureCoords().at(numerovertici).y);
glVertex3fv(&vertices[lista[0]].pos[0]);
glNormal3fv(&normals[(f.normalIndex)[1]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numerovertici+1).x, p.TextureCoords().at(numerovertici+1).y);
glVertex3fv(&vertices[lista[1]].pos[0]);
}
else{
glTexCoord2f(p.TextureCoords().at(numerovertici).x, p.TextureCoords().at(numerovertici).y);
glVertex3fv(&vertices[lista[0]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numerovertici+1).x, p.TextureCoords().at(numerovertici+1).y);
glVertex3fv(&vertices[lista[1]].pos[0]);
}
numerovertici+=2;
break;
case 3:
face_mode = GL_TRIANGLES;
glBegin(face_mode);
if (numerovertici<p.Vertices().count()-3){
if(hasNormals){
glTexCoord2f(p.TextureCoords().at(numerovertici).x, p.TextureCoords().at(numerovertici).y);
glNormal3fv(&normals[(f.normalIndex)[0]].pos[0]);
glVertex3fv(&vertices[lista[0]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numerovertici+1).x, p.TextureCoords().at(numerovertici+1).y);
glNormal3fv(&normals[(f.normalIndex)[1]].pos[0]);
glVertex3fv(&vertices[lista[1]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numerovertici+2).x, p.TextureCoords().at(numerovertici+2).y);
glNormal3fv(&normals[(f.normalIndex)[2]].pos[0]);
glVertex3fv(&vertices[lista[2]].pos[0]);
}
else{
glTexCoord2f(p.TextureCoords().at(numerovertici).x, p.TextureCoords().at(numerovertici).y);
glVertex3fv(&vertices[lista[0]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numerovertici+1).x, p.TextureCoords().at(numerovertici+1).y);
glVertex3fv(&vertices[lista[1]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numerovertici+2).x, p.TextureCoords().at(numerovertici+2).y);
glVertex3fv(&vertices[lista[2]].pos[0]);
}
numerovertici+=3;
break;
default: face_mode = GL_POLYGON; break;
}
glDisable(GL_TEXTURE_2D);
...
PRE:顶点数=纹理坐标数
每个 glTexCoord2f 都包含类似 p.TextureCoords().at(numervertici+2).x 的东西,其中
*p* is the current mesh
*TextureCoords()* is a QList<Vector3D> that contains all the 2D coords
*at(numerovertici)* : takes from TextureCoords List the "numerovertici" element
*.x* taking x coordinate
所以,我使用一个名为“numervertici”的顶点计数器来记住绘制了多少个顶点,在绘制每个顶点之前,我调用了对应的纹理坐标。
这是坐标问题(我很确定他们没问题)还是应该是其他问题?
PS:我在 glvertex 之前尝试过使用 glTexCoord2f 但没有区别。