I have been using glColor4f() to set each of my sprites alpha value as well as RGB values... I am now trying to get rid of the glColor4f() calls and make each sprite (Quad) set it's own colors values using glColorPointer...
I am drawing sprites using glDrawElements which is using some sort of indices initialized as follows:
Code:
//COMMENT: 100 is the max quads number
indices = malloc( sizeof(indices[0]) * 100 * 6);
bzero( indices, sizeof(indices[0]) * 100 * 6);
for( NSUInteger i=0;i<100;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;
}
The drawing routine is as follows:
Code:
glEnableClientState(GL_COLOR_ARRAY);
glBindTexture(GL_TEXTURE_2D,TextureName);
glColorPointer(4, GL_FLOAT,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; //How Many quads to be draw
Now when I use glColorf() everything works like a charm, but as soon as I enable the COLOR_ARRAY, I get a strange results...
Could anybody please help me and let me know what's wrong with my code... I've been struggling in order to make this work but I only made myself half crazy...
I have been using glColor4f() to set each of my sprites alpha value as well as RGB values... I am now trying to get rid of the glColor4f() calls and make each sprite (Quad) set it's own colors values using glColorPointer...
I am drawing sprites using glDrawElements which is using some sort of indices initialized as follows:
Code:
//COMMENT: 100 is the max quads number
indices = malloc( sizeof(indices[0]) * 100 * 6);
bzero( indices, sizeof(indices[0]) * 100 * 6);
for( NSUInteger i=0;i<100;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;
}
The drawing routine is as follows:
Code:
glEnableClientState(GL_COLOR_ARRAY);
glBindTexture(GL_TEXTURE_2D,TextureName);
glColorPointer(4, GL_FLOAT,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; //How Many quads to be draw
Now when I use glColorf() everything works like a charm, but as soon as I enable the COLOR_ARRAY, I get a strange results...
Could anybody please help me and let me know what's wrong with my code... I've been struggling in order to make this work but I only made myself half crazy...
I would really apreciate any help. thank you
What sort of strange results are you getting ?
Well, looking at your code this is generally how are you supposed to do it.
Now, you may want to verify that your Filters array which you pass to glColorPointer(4, GL_FLOAT,sizeof(Filters[0]), Filters); actually contains color values you specified ( just walk it and display floats).
Unrelated to your problem but as a general advice ... .don't use GL_FLOAT to specify colors .. it is a waste since it consumes 16 bytes per vertex while you get the same results with going with an unsigned int (GL_UNSIGNED_BYTE - 4 bytes per vertex).
This is even mentioned in Apple's own GLES guidelines.
Thank you very much for your answer... The strange result is that it seems that the sprite (which vertices are represented as Quad2) have R,G,B and Alpha value all mixed together (e.g: the upper half is redish and opaque while the bottom half is partialy transparent...)
I guess the problem is that I should specify one RGBA for each vertex in the Quad to be rendered... But I am not sure how to do this... the sprite is represented as four 2D vertices, but glDrawElements is actually rendering 6 of them (Something related to the Indices[ ] array initialization):
Thank you very much for your answer... The strange result is that it seems that the sprite (which vertices are represented as Quad2) have R,G,B and Alpha value all mixed together (e.g: the upper half is redish and opaque while the bottom half is partialy transparent...)
I guess the problem is that I should specify one RGBA for each vertex in the Quad to be rendered... But I am not sure how to do this... the sprite is represented as four 2D vertices, but glDrawElements is actually rendering 6 of them (Something related to the Indices[ ] array initialization):
As for the issue of replacing GL_FLOAT with GL_UNSIGNED_BYTE... How I am supposed to specify an alpha value of 0.5 (float) using unisgned byte?
I am really sorry about my ignorance, and I really apreciate your kind help.
glDrawElements only renders 4 unique vertices, you have six values there because you are rendering triangles(some vertices are shared) but if you examine your index values for this quad you will see that it refers only to 4 unique vertex offsets
As far as colors, you should simply replicate your color values for each vertex that is part of the quad.
In other words, if your quad definition contains 4 vertices - there should 4 colors with the same value replicated 4 times in the color array ( you can make them different if you want your quad to have some sort of gradient effect)
To convert a float into 4 fixed point values you can use code like this ( this is C++, get rid of static_cast<> to compile as C code):
Thanks a lot for your help. Your answers are always a guide for me to improve my engine further more.
The results of ColorPointer are correct, now that I set the color data for each 4 vertices of my Quad... I have also changed the colors data to unsigned bytes instead of floats.
Next thing for me to do would be working on the interleaved array as you suggested.
Thank you very much for you much appreciated help.