glDrawArrays and "waste"

Hi there. Short question:

When drawing a series of quads, each with one solid color,
then with glDrawArrays I need a color array which “unneccesarily” holds each color 4 times in order to map properly to the 4 vertices of each quad.

Is this ok and a good solution, or should it be done differently?


It’s the way it is usually done. There’s not much you can do about it, some data always needs to be duplicated.

thanks, Jan.

I’m paralyzed by pointers right now, maybe you can help me out. :stuck_out_tongue:

my basepointer, bp is of type (GLfloat ).
now I want to fill one color in the array like this (d is just a loop var for repeating 4 times):
(bp+d3sizeof(GLfloat)+0sizeof(GLfloat)) = r;
sizeof(GLfloat)+1sizeof(GLfloat)) = g;
sizeof(GLfloat)+2*sizeof(GLfloat)) = b;

is it right to have all the sizeof()'s there? Because when I have the memory displayed, there are too much zeros in between.
as if the sizeof is too much there and multiplies where it shouldn’t.

I haven’t done so much C programming but actually it’s quite a long time since I started with C. I hope one day I’ll overcome this pointer problems. :frowning:

kurono, Jan is correct of course, but if you want to save some space wrt the colours then look into GL_UNSIGNED_BYTE. You can then effectively squeeze your 4 colour components into the same space as one float.

As you learn more about OpenGL you’ll find other ways to save also, perhaps using Geometry shaders and / or other clever / novel ways of storing your vertex data.

To answer your pointer problem…
I am not sure what you are doing above but a pointer will advance through memory each time you increment it by exactly the size of the unit it is a pointer for… So what you are doing will leave huge spaces between each unit of data as adding the sizeof(GLfloat) to the pointer will actually add sizeof(GLfloat) * sizeof(GLfloat) to your pointer because each integer you add to it is worth one GLFloat already!!

So if the start of your colour array is pointed to by bp and it is a GLfloat* then this should work fine…

*bp++ = r;
*bp++ = g;
*bp++ = b;

Alternatively you could do this…

*bp = r;
*(bp+1) = g;
*(bp+2) = b;

The brackets around bp tell it that it should add 1 unit (a GLfloat) to it’s address, and then the * infront of the brackets says that we should store our colour value into the address pointed to by the expression inside the bracket.

Hope that helps.

One other pitfall to be aware of is that OpenGL pointer commands, such as glVertexPointer etc. use byte size offsets for all their stride sizes etc. That may or may not be something that affects you at the moment, but do keep it at the back of your mind if you start looking at using interleaved vertex, colour, texcoord data…

thanks a lot, scratt,
for clarifying the use of sizeof().