Question

I have an interleaved array format and I want to render it in open GL. It is an array of triangles. The vertices are 2D floating points and the colors are RGBA floating points. That is, a single triangle is layed out this this:

{vertex.x, vertex.y, color.red, color.blue, color.green, color.alpha, ...}

Where all number are single precision floats. I'm having trouble figuring out what the format parameter should be. It seems like it needs V2F and C4F, but not such symbolic constant exists. Can I OR them together like (GL_V2F | GL_C4F)?

UPDATE: I'm using python and pyopengl. tibur's answer is very clear and if I were programming in C I'd be done. The python code is pretty similar, but I have to have to offset the pointer into the color array by 8 bytes. I don't know how to do this in python or if it even can be done:

strideInBytes = 24
interleavedBytes = array.tostring()
glVertexPointer(2, GL_FLOAT, 24, interleavedBytes) 
glColorPointer(4, GL_FLOAT, 24, interleavedBytes) #The first color actually starts on the 9th byte

I need to avoid copying the entire interleaved array. Otherwise I could just make a copy and chop off the first 8 bytes.

Was it helpful?

Solution

No, you can't OR them. You need to use the following code:

glVertexPointer(2, GL_FLOAT, 6*sizeof(float), ptr);
glColorPointer(4, GL_FLOAT, 6*sizeof(float), ((unsigned char*)ptr) + 2 * sizeof(float));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top