Sunday, 21 November 2010

glBegin to vertex arrays

So even though i already did some conversions from opengl to opengles, i cannot remember what and how i did! here is a little reminder

glBegin( GL_QUADS );
glTexCoord2i(0, 0);
glVertex3f( 0-(ButtonSprites[index].Width/2), 0-(ButtonSprites[index].Height/2), 0 );

glTexCoord2i(1, 0);
glVertex3f( 0+(ButtonSprites[index].Width/2), 0-(ButtonSprites[index].Height/2), 0 );

glTexCoord2i(1, 1);
glVertex3f( 0+(ButtonSprites[index].Width/2), 0+(ButtonSprites[index].Height/2), 0 );

glTexCoord2i(0, 1);
glVertex3f( 0-(ButtonSprites[index].Width/2), 0+(ButtonSprites[index].Height/2), 0 );
glEnd();


get converted into

int numvertices = 4;
glEnableClientState( GL_VERTEX_ARRAY ); // Enable Vertex Arrays
glEnableClientState( GL_TEXTURE_COORD_ARRAY ); // Enable Texture Coord Arrays

float* vertices= new float[3*numvertices]; //3 coordiantes per vertex
float* textcoord= new float[2*numvertices]; //2 texture coordiantes per vertex

//fill in your array of vertices and texture coordinates with data
textcoord[0] = 0;
textcoord[1] = 0;
textcoord[2] = 1;
textcoord[3] = 0;
textcoord[4] = 1;
textcoord[5] = 1;
textcoord[6] = 0;
textcoord[7] = 1;

vertices[0] = -(ButtonSprites[index].Width/2);
vertices[1] = -(ButtonSprites[index].Height/2);
vertices[2] = 0;
vertices[3] = (ButtonSprites[index].Width/2);
vertices[4] = -(ButtonSprites[index].Height/2);
vertices[5] = 0;
vertices[6] = (ButtonSprites[index].Width/2);
vertices[7] = (ButtonSprites[index].Height/2);
vertices[8] = 0;
vertices[9] = -(ButtonSprites[index].Width/2);
vertices[10] = (ButtonSprites[index].Height/2);
vertices[11] = 0;

glVertexPointer( 3, GL_FLOAT, 0, vertices ); // Set The Vertex Pointer To Vertex Data
glTexCoordPointer( 2, GL_FLOAT, 0, textcoord ); // Set The Vertex Pointer To TexCoord Data

glDrawArrays( GL_TRIANGLE_FAN, 0, numvertices ); //Draw the vertices



All the projects here are under a Creative Commons 3.0 licence! You can use and distribute them as you like (just quote the author so he knows his work is not useless)!

If you wish to get in touch with me write at projectsymphony@gmail.com