Skip to main content
4 of 4
edited tags
joojaa
  • 8.5k
  • 1
  • 25
  • 48

OpenGL Vertex - Texture Mapping

I have a mesh loaded by an obj file (wavefront), complete with normals and texture coordinates per face, and its relative texture available. In my C++ / OpenGL code (assignment for a computer graphics class) I bind correctly the texture, but fail at correctly mapping texture coordinates with face vertexes. Below I show the code for texture binding and the chain of calls to rasterize the object. I also show images for the expected result and the actual results (2 of them, one for each render setting). To be specific, it seems that the first render "tessellates" the plain image on the rasterized shape of the whole object, while the second maps the image once on the shape of the object.

What is going on here?

Expected result Result #1 Result #2 (don't mind the background)

Bind procedure:

SDL_Surface *s = IMG_Load(filename.c_str());

glBindTexture(GL_TEXTURE_2D, textureIndex);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB,
        s->w, s->h, GL_RGB, GL_UNSIGNED_BYTE, s->pixels);
glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_MAG_FILTER,
        GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D,
        GL_TEXTURE_MIN_FILTER,
        GL_LINEAR_MIPMAP_LINEAR );

Raster procedure (pseudo code intended):

// gl pushMatrix, Traslation and rotations;
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureIndex);
glColor3f(1,1,1);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glDisable(GL_LIGHTING);
glBegin(GL_TRIANGLES);
for (face f in faces) {
  glNormal3d(faceNormal.x, faceNormal.y, faceNormal.z);
  for (i = 0, i < 3, ++i) {
    glTexCoord3d(texturePoint[i].x, texturePoint[i].y, 0);
    glVertex3d(vertex[i].x, vertex[i].y, vertex[i].z);
  }
}
glEnd();
glPopMatrix();
// gl PopMatrix to exit from traslation / rotation context.