Textures on Triangles

Started by
8 comments, last by kburkhart84 18 years, 8 months ago
i know how to map a texture onto a quad but how with triangles?
Advertisement
just the same way, you just give it three points instead of 4 and it takes a triangle shape out of the texture..
**EDIT** Phantom beat me, oh well **END EDIT**

One of NEHE's tuts shows you, but I'll tell you to make it quicker. In reality, it is the same way as with a quad, only using three verts. For example, this will draw the triangle, using the upperleft, lowerright, and upperright of the texture.

glBegin(GL_TRIANGLES);    glTexCoord(0, 1);//UV for upper left    glVertex3f(-1, 1, -5);    glTexCoord(1, 0);//UV for lower right    glVertex3f(1, -1, -5);    glTexCoord(1, 1);//UV for upper right    glVertex3f(1, 1, -5);glEnd();


This is assuming you have texturing enabled and the texture binded. It would look like the upper left half of the quad, but cut in half corner to corner.

Like This.
***** *  *  * *   **    *


It would have that portion of the texture as well.


there has to be a better way...... i dont really care how its mapped as long as its on there
Quote:Original post by C plus noob
there has to be a better way...... i dont really care how its mapped as long as its on there

What do you mean "a better way"? What do you perceive as the problems with "this way"?
im not having a right triangle his way would map to a right triangle
Only because that's the coordinates he used. Other sets of vertex coordinates would produce a different triangle, and other texture coordinates would change how the texture was mapped onto the triangle.
The example proposed by kburkhart84 is the right one with those coordinates. Sure.
Remember also that with the texture matrix, in the same way as the modelview, you can rotate, translate and scale your texture coordinates.
I'm not really sure what you want, but I made a demo which may be useful for you:
Link
the demo shows a way how to calculate the texture coordinates to make the texture always standing like on a right triangle. Use t to toggle between precalculated texturemapping and the realtime calculation. The Source is commentated too.

edit: note that this calculation can get slow when using a huge amount of dynamic triangles.
My example should show you how to do it. I think it's up to you to calculate what the actual verts and UV coords are. It depends on the triangle. You can do any coords with any verts just like with quads. You don't have to use the whole texture. Triangles are the same way except that it is only 3 instead of 4 verts. The texture that is between the 3 UV coords you give it will be used and put inbetween the 3 verts you draw.


This topic is closed to new replies.

Advertisement