OpenGL纹理映射和协调奇怪的问题

时间:2013-09-17 03:12:55

标签: opengl textures texture-mapping

这就是它现在看起来的样子(只绘制了一半的场景让你看到底部纹理)。我上传了一些屏幕,因为它们优于1000字。

d

放大(建筑物似乎有完全随机的坐标) df 应该怎么样 enter image description here

另一个例子 enter image description here

这里我加载纹理:

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(numerovertici + 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

所以,我使用一个名为“numerovertici”的顶点计数器来记住绘制了多少个顶点,在绘制每个顶点之前,我调用了相应的纹理坐标。

这是一个坐标问题(我很确定它们没问题)还是应该是其他问题?

PS:我在glvertex之前尝试过使用glTexCoord2f,但没有区别。

0 个答案:

没有答案