I am trying to figure out how to correctly texture an object that is comprised of many triangles and each triangle has an assigned texture id. What is the proper way to do it?
If you want to draw a bunch of triangles you can do one begin-end call and encapsulate all of the triangle drawing in it…I think this saves performance.
glBegin(GL_TRIANGLES);
for (triangle = 1 to numTriangles) {
for (vertex = 1 to 3) {
glNormal(normal);
glVertex(vertex);
}
}
glEnd();
I’m trying to figure out how to render .3DS (3D Studio Max) models correctly in JOGL (Java OpenGL) - fyi, this is really no different than pure OpenGL. I had code (originally JOGLUTILS 3DS loader) that I fixed up pretty well and it worked fine until I found out that each object in a 3DS file can actually have more than one texture associated with it. The previous developers thought that each object had 1 texture total, but in reality the textures are not assigned to the object itself but rather to the individual faces/triangles.
So you can have a list of triangles with their material/texture ids such as this, where the same materialId can be assigned to multiple triangles/faces and not necessarily contiguous. So the original assumption in the code that I got was only valid if all of the materialId were referencing just one texture…but that was incorrect.
Tri# face1 face2 face3 materialId
1 1 2 3 1
2 3 2 4 1
3 4 5 6 1
4 6 3 5 2
5 1 15 16 2
6 3 4 8 1
7 15 12 4 3
...
Before, when I thought I had one texture for the entire object, I would just do this (effectively that was the original code)
glEnable(GL_TEXTURE_2D);
texture.enable();
texture.bind();
glBegin(GL_TRIANGLES);
for (triangle = 1 to numTriangles) {
for (vertex = 1 to 3) {
glNormal(normal);
glVertex(vertex);
glTextCoord2f(x,y);
}
}
glEnd();
texture.disable();
glDisable(GL_TEXTURE_2D);
But, as it turns out, each face can have a different material/texture id.
As a result, instead of having one texture that I can just blindly apply to the entire triangle loop above, I have to individually assign the textures based on what each face/triangle should have. So I tried to do this code where I move the GL_TRIANGLES and binding of the texture to inside the for-loop:
glEnable(GL_TEXTURE_2D);
for (i = 1 to numTextures) {
texture[i].enable();
}
for (triangle = 1 to numTriangles) {
glBegin(GL_TRIANGLES); // Even tried GL_TRIANGLE_FAN
texture[triangle.textureId].bind();
for (vertex = 1 to 3) {
glNormal(normal);
glVertex(vertex);
glTextCoord2f(x,y);
}
glEnd();
}
for (i = 1 to numTextures) {
texture[i].disable();
}
glDisable(GL_TEXTURE_2D);
In doing so, I moved the GL_TRIANGLES begin-end block and use to for each separate triangle I draw. I believe this move is is killing my performance but the texturing looks correct. When I move it out of the loop (as in the 2nd code snip) I get better performance but the texture is incorrect. I tried wrapping this in a Display List but it takes a while for the display list to get generated…which is also weird to me because I can make a display list around the original code and it is fast. I’m guessing by changing from one begin-end call for GL_TRIANGLES to multiple begin-end calls increases the size of the display list and hence it is slower.
Can someone tell me if what I’m doing is correct? I’m not asking if the code is exactly correct, I know I’m not showing you actual code but rather pseudo-code. I just want to know if it makes sense to call the texture binding withing the triangle loop and also is that where I put the GL_TRIANGLES begin-end block or is there a better way to do this?
Thanks.