VBO's and color

I have a cube rendered using indices on 8 points:

float cubev[120] = { -2.0f,-2.0f,2.0f, 2.0f,-2.0f,2.0f, -2.0f,2.0f,2.0f, 2.0f,2.0f,2.0f, -2.0f,2.0f,-2.0f, 2.0f,2.0f,-2.0f, -2.0f,-2.0f,-2.0f, 2.0f,-2.0f,-2.0f};

GLuint cubei[] = { 0,1,3,2, 2,3,5,4, 4,5,7,6, 6,7,1,0, 6,0,2,4, 1,7,5,2 };

I left space in cubev to assign 24*4 RGBA vertices – in 6 sets of four, one set to each side.

Pretty clearly, tho, only the first two sets count (8 vertices), meaning they are applied to the points in the ARRAY_BUFFER and not according to the points as ordered in the ELEMENT_ARRAY_BUFFER.

I presume that means I cannot duplicate points using an index unless I want the object to be a uniform color?

Or am I missing something?

I’m not absolutely sure I’ve latched onto your question, but yes, there is one “index” in the index array for each vertex to be sent down the pipe. This index is simultaneously used to lookup values from each of the enabled vertex attribute arrays (POSITION/VERTEX, COLOR, NORMAL, TEXCOORD0, etc.).

So yes, if you want to have the same vertex position with different colors, you either need to use:

  1. Non-interleaved vertex arrays, and just swap out the color vertex array binding, or

  2. Separate Interleaved vertex arrays, where you’ve duplicated the vertex attributes that don’t change (e.g. POSITION/VERTEX).

I can now see, doing the interleaving, that the same point used on two different sides (ie, the corner…) requires two separate indices since the normals are different. So the point of the index is sort of lost on me, but nevermind.

Anyway, I can’t get the interleaving to work for some reason. Simple object (two planes perpendicular for a quad strip) with interleaved vertices/normals:

float bgstrip[] = { 
    -5.0f,-5.0f,5.0f, 0.0f,1.0f,0.0f,
    5.0f,-5.0f,5.0f, 0.0f,1.0f,0.0f,
    -5.0f,-5.0f,-5.0f, 0.0f,1.0f,0.0f, 
    5.0f,-5.0f,-5.0f, 0.0f,1.0f,0.0f, 
    -5.0f,0.0f,-5.0f, 0.0f,0.0f,1.0f, 
    5.0f,0.0f,-5.0f, 0.0f,0.0f,1.0f };

I figured the way to render this would be:

GLsizei offset = sizeof(float)*3;
glNormalPointer(GL_FLOAT, offset, BUFFER_OFFSET(offset));
glVertexPointer(3, GL_FLOAT, offset, BUFFER_OFFSET(0));

No luck, all bent out of shape – and what is very weird, if I substitute my previous “bgstrip” vertices – the one WITHOUT interleaved normals, the object appears fine.

In other words, despite setting the “stride” argument to glVertexPointer, it is not skipping any bytes at all! Is there something more I have to do to enable this?

Okay, I got it. The offset is from start to start, not finish to start, so it should be 6*sizeof(float)…