Sign in to follow this  
Sakuku

glDrawElements+ Interleaved arrays ?

Recommended Posts

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:
[code]
typedef struct _Quad2 {
float tl_x, tl_y;
float tr_x, tr_y;
float bl_x, bl_y;
float br_x, br_y;
} Quad2;
[/code]

Color Filter:
[code]
typedef struct _CFilter {
GLubyte r;
GLubyte g;
GLubyte b;
GLubyte a;
} CFilter;
[/code]

Rendering arrays:
[code]
CFilter *Filters;
Quad2 *Vertices;
Quad2 *TexCoords;
GLushort *indices;

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

Setting the rendering arrays
[code]
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;
}
[/code]

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

[code]
-(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;

}
[/code]

Actual rendering code:
[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;
}
[/code]


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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='V-man' timestamp='1307736897' post='4821842']
You would make a structure for your vertex. Examples are here
[url="http://www.opengl.org/wiki/Vertex_Arrays"]http://www.opengl.or...i/Vertex_Arrays[/url]

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

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.


Share this post


Link to post
Share on other sites
The link shows a vertex structure. It is a vertex, not a quad. If you want a quad, then you need 4 vertices

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

Share this post


Link to post
Share on other sites
[quote name='V-man' timestamp='1307744325' post='4821889']
The link shows a vertex structure. It is a vertex, not a quad. If you want a quad, then you need 4 vertices

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

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


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this