Square textures on triangles?

I have just managed to get my first JOGL textures to render - a PNG stretched over a quad. :slight_smile: In my code I use gl.glTexCoord2f() to tie each corner of the texture to the corners of the quad, and this works well. But how does this work for three-cornered triangles?

Also, how do I integrate textures with my VBOs? There doesn’t seem to be a drawArrays() style command for switching on textures.

The concept is the same.

Assuming we’re using power-of-two textures, here’s how you’d render a rectangular sprite using 2 triangles:

//first triangle...
glTexCoord2f(0, 0); //top left
glVertex2f(0, 0);

glTexCoord2f(1f, 0); //top right
glVertex2f(width, 0);

glTexCoord2f(0, 1f); //bottom left
glVertex2f(0, height);

//second triangle...
glTexCoord2f(1f, 0); //top right
glVertex2f(width, 0);

glTexCoord2f(1f, 1f); //bottom right
glVertex2f(width, height);

glTexCoord2f(0f, 1f); //bottom left
glVertex2f(0, height);

Or do you mean tiling a texture onto an arbitrary triangle/polygon? For each vert you could calculate it like so:

//x, y -> the vertex location
//min/max x/y -> the min/max locations of your polygon/triangle
//scale -> how much to scale the texture; i.e. for tiling with GL_REPEAT
float u = (x - minX) / (maxX - minX);
float v = (y - minY) / (maxY - minY);
glTexCoord2f( u * scale, v * scale );

Using VBOs/vertex arrays/etc. you would use glTexCoordPointer. If you’re doing an all shader approach (OpenGL 3+) then you would use glVertexAttribPointer.

Thanks, I will give tiling a go next. Then I will try 3D textures - looks like a good way to draw a path through grass.