Opengl texture coordinate problem
-
Hi everyone, this is my first post here. Hopefully this is in the right area.
I downloaded the qt obj viewer a while ago from this "link":http://qt.gitorious.org/qt-labs/modelviewer. I have been modifying it to make my own engine. It is my first time using opengl. Right now I am working on texturing objects. I cannot tell if it is my opengl code that is wrong or if I am writing my u v coordinates incorrectly. As it stands I write the u v coords in the same order as they are in the obj file. It appears that part of my texture is correct. Here is an image and below is my code.
!http://i.imgur.com/jMMqv.png(Partially correct texturing)!
As you can see the u v coords are going into a class Point3d. u_v_coords is a QVector<Point3d>
@...
else if (id == "vt"){
Point3d p;
for(int i = 0; i < 2; ++i){
ts >> p[i];
}
u_v_coords << p;
}
...@The opengl code for drawing a textures is below. I am using glTexCoordPointer with the stride of size Point3d.
@
if(texture != NULL){
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture->texture );
glTexCoordPointer(2, GL_FLOAT, sizeof(Point3d), u_v_coords.data());
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
glMaterialfv(GL_FRONT, GL_AMBIENT, materials[objectMaterialInt[i]].Ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE, materials[objectMaterialInt[i]].Kd);
glMaterialfv(GL_FRONT, GL_SPECULAR, materials[objectMaterialInt[i]].Ks);
glMaterialf(GL_FRONT, GL_SHININESS, materials[objectMaterialInt[i]].Ns);
glShadeModel(GL_SMOOTH);
glPushMatrix();
glTranslatef(position[0], position[1], position[2]);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, (float *)m_points.data());
glNormalPointer(GL_FLOAT, 0, (float *)m_normals.data());
glDrawElements(GL_TRIANGLES, objects[i].size(), GL_UNSIGNED_INT, objects[i].data());
glPopMatrix();
if(texture != NULL){
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
@Up till this point, reading obj files with multiple objects and materials worked just fine. That still works, it is the
-
Looks to me like your u,v are not correct for the exterior front left side face of the box at least.
As a side note, if you're starting up with OpenGL now it is worth going the extra mile and learning the modern programmable pipeline instead of the rigid fixed pipeline. This will enable you to work on OpenGL ES 2.0 for mobile devices one day if you happen to feel like it.
- Matti
-
I had also posted my problem at gamedev.net. I think I am close to figuring it out. I will post the link to the gamedev post as soon as their servers are back up, it looks like they are down.
But as for your comment, since I have been searching everywhere for answers, it appears Opengl ES 2.0 wants people to use glDrawElements. Which is what I am currently doing but part of my solution is to use glDrawArrays. Is glDrawArrays allowed in OpenGL ES 2.0?
Can you elaborate on what else I should focus on for the modern opengl pipeline?
Thanks.
-
You still get to use glDrawArrays ;) What you lose is the matrix stack and the fixed rendering functionality (meaning you have to implement shaders yourself). What you win is a ton more control to what you're doing, which I personally like immensely.
See here for example about the changes: http://www.jeffwofford.com/?p=698