PDA

View Full Version : drawing a square texture inside a GL_TRIANGLE



11-03-2005, 02:08 AM
Hi all,

Ive got a square texture created from an NSImage, what I would like to do is draw a tetrahedon(sp?) with four sides, and on each side draw this texture fully within the triangle, as a square entirely within each triangular face.

So Ive gotten the tetrahedron drawn without any problems, but I seem to be getting lost drawing the square texture within it.
...the best Ive managed is to draw a corner of the image inside the triangle...

has anyone seen this done or got any suggestions?

here is my latest (unsuccessful) effort
glBegin( GL_TRIANGLES ); // Draw a triangle
glVertex3f( 1.0f, 1.0f, 1.0f );
glVertex3f( 1.0f, -1.0f, -1.0f );
glVertex3f( -1.0f, 1.0f, -1.0f );
glEnd();

glBegin( GL_QUADS );
glTexCoord2f( 0.0f, 0.0f );
glTexCoord2f( 1.0f, 0.0f );
glTexCoord2f( 1.0f, 1.0f );
glTexCoord2f( 0.0f, 1.0f );
glEnd();

hernan
11-03-2005, 03:53 AM
Hi,

First of all, let me tell you that you're using the glTexCoord* functions in a wrong way. When you use textures, you have to map a tex coord to every specified vertex like this:

glTexCoord*(...); glVertex*(...);
glTexCoord*(...); glVertex*(...);
glTexCoord*(...); glVertex*(...);

Secondly, I think that your trying to draw a textured quad inside a triangle, right? Well, first you have to draw the triangle:

glBegin( GL_TRIANGLES ); // Draw a triangle
glVertex3f( 1.0f, 1.0f, 1.0f );
glVertex3f( 1.0f, -1.0f, -1.0f );
glVertex3f( -1.0f, 1.0f, -1.0f );
glEnd(); Then, you have to bind the texture you want using:

glBindTexture( whatever_texture_id );And then draw the square:


glBegin( GL_QUADS ); // Draw a triangle
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( ... );
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( ... );
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( ... );
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( ... );
glEnd(); (you'll have to do a little math to know where each quad vertex should be).

I hope this will help you.
Bye

ppeterson
11-07-2005, 12:03 AM
If my calculations are correct, the square that fits inside a triangle which has a side of 1 should have a side of: sqrt(3) / (2 + sqrt(3)) = 0.464

So if the vertices of the triangle are at (0,0), (1,0) and (0.5, 0.866) then the vertices of the square are at (0.270, 0), (0.732, 0), (0.732, 0.464) and (0.270, 0.464).

Good luck.
Pete.

11-08-2005, 10:29 AM
create a GL_POLYGON with 7 vertexes.
3 vertexes of the triangle
and betweend those 4 vertexes (wherever you want)
and on those u use glTexCoord2f.
make sure the 4 vertexes of the quad are on the same line between the other 3 vertexes, so it looks like a triangle.

gl