Mapping 2D texture to multiple GL_TRIANGLES

Apprentice
Posts: 11
Joined: 2013.10
Post: #1
I'm trying to map a texture to a set of GL_TRIANGLES.

I wrote each triangle creation like this so that the transform and rotation would affect them.

this pretty much creates one quad, made up of GL_TRIANGLES, that would change directions after certain time.

Is it possible for me to map a texture to a set up like this? Or is there a better approach?

Code:
for(int hCtr = 0; dimensionH > hCtr; hCtr++){

cW = 0;

for (int wCtr = 0; dimensionW > wCtr; wCtr++) {

glBegin(GL_TRIANGLES);

glTexCoord2f(cW, cH);
glVertex3f(cW, cH, 0);

glTexCoord2f(cW, cH + blockH);
glVertex3f(cW, cH + blockH, 0);

glTexCoord2f(cW + halfBW, cH);
glVertex3f(cW + halfBW, cH, 0);

glEnd();

cW = cW + halfBW;

glBegin(GL_TRIANGLES);

glTexCoord2f(cW, cH);
glVertex3f(cW, cH, 0);

glTexCoord2f(cW - halfBW, cH + blockH);
glVertex3f(cW - halfBW, cH + blockH, 0);

glTexCoord2f(cW + halfBW, cH + blockH);
glVertex3f(cW + halfBW, cH + blockH, 0);

glEnd();

cW = cW + halfBW;

glBegin(GL_TRIANGLES);

glTexCoord2f(cW, cH);
glVertex3f(cW, cH, 0);

glTexCoord2f(cW - halfBW, cH);
glVertex3f(cW - halfBW, cH, 0);

glTexCoord2f(cW, cH + blockH);
glVertex3f(cW, cH + blockH, 0);

glEnd();

}
cH = cH + blockH;

}
⌘-R in Chief
Posts: 1,277
Joined: 2002.05
Post: #2
You've asked a bit of a strange question, and your code isn't clear but I'm pretty sure I understand what you're trying to do, so here's a quick overview.

Think of it like this: Since you're "exploding" a quad into triangle shards, you need to create individual Shard objects. Each Shard has its own 3 vertices and 3 mapping texture coordinates, and then when you're exploding, each shard moves off in its own direction.

When texture mapping a quad, you're using the tex coord {0, 0} for the vertex in one corner, and {1, 1} for the opposite corner's vertex.

Consider your quad dimensions as W x H where W and H are both 1: Your texture coords are the same as your vertex coords. Easy.

When you "smash" the original quad and slice it up into a bunch of triangles, the vertices of the triangles you create are all are between 0 and 1. Since texture coordinates are also from 0 to 1, mapping a texture onto those triangles is easy: the 0-1 vertex values are also the 0-1 texture coordinates.

When you create each shard, set its vertices and texture coordinates, and then your main loop just moves each shard.
Apprentice
Posts: 11
Joined: 2013.10
Post: #3
Yes that is exactly what I was going for. I got to to separate along with the texture, thanks!
Nibbie
Posts: 2
Joined: 2014.11
Post: #4
nice info tnx

nice
Nibbie
Posts: 4
Joined: 2016.04
Post: #5
I think the accepted answer in the post Mapping a square texture to a triangle nearly covers what im after, if it is indeed possible to index and texture in the way that i am expected to.