Here I load textures:
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++;
}
}
Then for every mesh i do this:
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: Vertices number=number of texture coordinatess
Every glTexCoord2f contains something like p.TextureCoords().at(numerovertici+2).x where
*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
So,I am using a vertices counter named “numerovertici” to remember how many vertices were drawn,and before drawing each vertex I am calling the correspondent texture coordinate.
Is this a coordinates problem (I’m pretty sure they’re ok) or should it be something else?