iDevGames Forums
Mapping 2D texture to multiple GL_TRIANGLES - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Mapping 2D texture to multiple GL_TRIANGLES (/thread-10912.html)



Mapping 2D texture to multiple GL_TRIANGLES - ardowz - Feb 26, 2014 03:46 PM

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;
                float spreadIncrement = spread / dimensionW;
                
                for (int wCtr = 0; dimensionW > wCtr; wCtr++) {
                    
                    float explodeSpread = time * spreadIncrement;
                    
                    glTranslatef(explodeSpread, explodeSpread, explodeSpread);
                    glRotatef(explodeSpread, 1, 1, 1);
                    
                    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();
                    
                    glTranslatef(explodeSpread, explodeSpread, explodeSpread);
                    glRotatef(explodeSpread, 1, 1, 1);
                    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();
                    
                    glTranslatef(explodeSpread, explodeSpread, explodeSpread);
                    glRotatef(explodeSpread, 1, 1, 1);
                    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();
                    
                    glTranslatef(-explodeSpread, -explodeSpread, -explodeSpread);
                    
                    spreadIncrement = spreadIncrement + spreadIncrement;
                }
                cH = cH + blockH;
                
            }



RE: Mapping 2D texture to multiple GL_TRIANGLES - SethWillits - Feb 26, 2014 05:13 PM

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.


RE: Mapping 2D texture to multiple GL_TRIANGLES - ardowz - Mar 12, 2014 08:56 AM

Yes that is exactly what I was going for. I got to to separate along with the texture, thanks!


RE: Mapping 2D texture to multiple GL_TRIANGLES - Mason_Baumgar - Nov 21, 2014 10:51 AM

nice info tnx

nice