OpenGL纹理映射和坐标奇怪的问题
这就是它现在的样子(只画了半个场景,让你看到底部纹理)。我上传了一些屏幕,因为它们比1000字要好 缩放(建筑物似乎有完全随机的坐标) 应该是什么样子 另一个例子 这里我加载纹理:OpenGL纹理映射和坐标奇怪的问题,opengl,textures,texture-mapping,Opengl,Textures,Texture Mapping,这就是它现在的样子(只画了半个场景,让你看到底部纹理)。我上传了一些屏幕,因为它们比1000字要好 缩放(建筑物似乎有完全随机的坐标) 应该是什么样子 另一个例子 这里我加载纹理: glGenTextures(sceneList.count(), &mTextureID[0]); for (QList<SceneObject*>::ConstIterator i = sceneList.begin();i!=sceneList.end();++i) { Mes
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++;
}
}
glGenTextures(sceneList.count(),&mTextureID[0]);
对于(QList::conditerator i=sceneList.begin();i!=sceneList.end();+i){
网格*p=动态_铸造(*i);
如果(p){
如果(p->HasTexture()){
QImage*a=p->Mat().GetTexture()->GetImage();
*a=QGLWidget::convertToGLFormat(*a);
glEnable(GL_纹理_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_无符号字节,a.位());
glTexParameteri(GL_纹理2D、GL_纹理最小过滤器、GL_线性);
glTexParameteri(GL_纹理2D、GL_纹理MAG_过滤器、GL_线性);
glDisable(GL_纹理_2D);
}
}
计数器++;
}
}
然后对每个网格执行以下操作:
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);
...
glBindTexture(GL_TEXTURE_2D,mTextureID[counter])//其中计数器=当前网格的纹理
...
常量QList顶点=p.顶点();
QList&faccie=p.Faces();
int numerioface=faccie.count();
QList&normals=p.normals();
int numerioverici=0
对于(int t=0;t<数值;++t){
glEnable(GL_纹理_2D);
...
开关(f.numVertici){
案例1:
a[1]++;
面_模式=GL_点;
glBegin(面_模式);
如果(有法线)
glNormal3fv(&normals[(f.normalIndex)[0]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numeriovertici).x,p.TextureCoords().at(numeriovertici).y);
glVertex3fv(&顶点[lista[0]].pos[0]);
numerovertici++;
打破
案例2:
面_模式=GL_线;
glBegin(面_模式);
如果(有法线){
glNormal3fv(&normals[(f.normalIndex)[0]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numeriovertici).x,p.TextureCoords().at(numeriovertici).y);
glVertex3fv(&顶点[lista[0]].pos[0]);
glNormal3fv(&normals[(f.normalIndex)[1]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numeriovertici+1).x,p.TextureCoords().at(numeriovertici+1).y);
glVertex3fv(&顶点[lista[1]].pos[0]);
}
否则{
glTexCoord2f(p.TextureCoords().at(numeriovertici).x,p.TextureCoords().at(numeriovertici).y);
glVertex3fv(&顶点[lista[0]].pos[0]);
glTexCoord2f(p.TextureCoords().at(numeriovertici+1).x,p.TextureCoords().at(numeriovertici+1).y);
glVertex3fv(&顶点[lista[1]].pos[0]);
}
数值积分+=2;
打破
案例3:
面_模式=GL_三角形;
glBegin(面_模式);
如果(NumeroOverticick,不要将glVertex3f(…)
与glTexCoord2f(…)
放在同一行。在我找到对glVertex3f(…)
的任何调用之前,我必须重新格式化代码:-\n我猜这与您使用p.TextureCoords()有关。在(…)
,而所有其他顶点属性都有存储其内容的简单数组。我需要看看这个纹理词(…)的实现
method。尽管所有这些glVertex3f的东西都被弃用了,你真的应该使用顶点和索引缓冲区。@AndonM.Coleman代码和描述更新了,希望现在更清楚。几天前解决了,坐标的顺序取错了。
*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