glDrawElements+ Interleaved arrays ?

Started by
3 comments, last by Sakuku 12 years, 10 months ago
Hi,

I have been working on my game project and got my self to a point where I cannot go further on the development before solving this.

I have noticed serious performances drop when too many elements are being rendered at the same time... The only thing left for me to solve this would be reducing the number of elements to draw at the same time, which will seriously affect the gameplay. :(

I don't want to do this before trying the last rendering optimization left to be implemented, which is the interleaved array.

I have spent too many hours trying to get it done, without any success, which almost turns me crazy. :eek:


i am originally using glDrawElements for rendering elements, and below is the code responsible for this:



Quad 2D:

typedef struct _Quad2 {
float tl_x, tl_y;
float tr_x, tr_y;
float bl_x, bl_y;
float br_x, br_y;
} Quad2;


Color Filter:

typedef struct _CFilter {
GLubyte r;
GLubyte g;
GLubyte b;
GLubyte a;
} CFilter;


Rendering arrays:

CFilter *Filters;
Quad2 *Vertices;
Quad2 *TexCoords;
GLushort *indices;

int DataNbr; //how many quads to draw
int ColorsNbr; //how many color data


Setting the rendering arrays

int MAX_QUADS=400;
Filters= malloc(sizeof(CFilter)*MAX_QUADS*4);
TexCoords = malloc( sizeof(TexCoords[0]) * MAX_QUADS);
Vertices = malloc( sizeof(Vertices[0]) * MAX_QUADS);

indices = malloc( sizeof(indices[0]) * MAX_QUADS * 6);
//VOODOO MAGIC!!!!
for( NSUInteger i=0;i<400;i++) {
indices[i*6+0] = i*4+0;
indices[i*6+1] = i*4+1;
indices[i*6+2] = i*4+2;
indices[i*6+5] = i*4+1;
indices[i*6+4] = i*4+2;
indices[i*6+3] = i*4+3;
}


This is the method responsible for sending calculated vertices and colors to the rendering arrays:


-(void)AddToVertices:(Quad2)ver AndTexCoord:(Quad2)cord Filter:(CFilter)fil{

Vertices[DataNbr]=ver;
TexCoords[DataNbr]=cord;
DataNbr++;

//Storing the same color data 4 times, so each vertex on each quad
// would have the same value

for (int i=0;i<4;i++)
Filters[ColorsNbr+i]=fil;

ColorsNbr=ColorsNbr+4;

}


Actual rendering code:

if (DataNbr>0){
glBindTexture(GL_TEXTURE_2D,[MyIMG TextureName]);

glColorPointer(4,GL_UNSIGNED_BYTE, sizeof(Filters[0]), Filters);
glVertexPointer(2, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, TexCoords);
glDrawElements(GL_TRIANGLES, DataNbr*6, GL_UNSIGNED_SHORT, indices);

DataNbr=0;
ColorsNbr=0;
}



I would really appreciate your guidance on steps necessary to change this rendering system in order to use interleaved arrays, and that's to get some performances improvements.

Thank you very much.
Advertisement
You would make a structure for your vertex. Examples are here
http://www.opengl.org/wiki/Vertex_Arrays

You won't necessarily see any performance improvement. That depends on where your bottleneck is.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

You would make a structure for your vertex. Examples are here
http://www.opengl.or...i/Vertex_Arrays

You won't necessarily see any performance improvement. That depends on where your bottleneck is.


Thank you for your reply.
I have checked out the link you provided, and am still confused, the reason of the confusion is that the Quad2D structure I am using for rendering is composed of a total of eight (8) float elements, while the structure in the link got only 3 float values (x,y and z) for the vertex, and 6 float values for the texture coordinates.

How to make my Quad2D values fit in the structure on the link you provided???
Would really appreciate your help.


The link shows a vertex structure. It is a vertex, not a quad. If you want a quad, then you need 4 vertices


typedef struct _Quad2 {
MyVertex Vertex[4];
} Quad2;
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

The link shows a vertex structure. It is a vertex, not a quad. If you want a quad, then you need 4 vertices


typedef struct _Quad2 {
MyVertex Vertex[4];
} Quad2;



Silly me! I will try to implement this asap.
Thank you.


This topic is closed to new replies.

Advertisement