1
votes

I am making this program that needs to render colored quads. I am using VBOs for this.

This is how I store the data for the vertices and the colors:

render quad:

 buff.put(3 floats color)
 render the quad (3 floats per vertex * 4 per face * 6 per quad) 

Now as you see my aim is to use interleaved VBOs but I cant figure out the stride and offset.

Here are my calls when rendering:

(Note im using mapped vbos thats why there is no actual data binding):

        glBindBufferARB(GL_ARRAY_BUFFER_ARB, etn.getVboHandel());
        glBufferDataARB(GL_ARRAY_BUFFER_ARB, etn.getVboData().capacity() << 2, GL_STATIC_DRAW_ARB);

        glColorPointer(3, GL_FLOAT, /* stride **/6 << 2, /* offset **/0); 
        glVertexPointer(3, GL_FLOAT, /* stride **/6 << 2, /* offset **/0);

        glDrawArrays(GL_QUADS, 0,  etn.getVboData().capacity());    

I cant get the offset and stride to be correct so when I render I see this weird artifacts..

2
There's too much missing information here to be able to answer the question. It's not clear what it is that you're trying to do. For example, what does "3 floats color" mean? Also, stop using the ARB suffixed stuff. Buffer objects are GL 1.5 functionality; they've been core OpenGL for over a decade. - Nicol Bolas
I removed the ARB stuff - Amit Assaraf

2 Answers

3
votes

The stride is how much space is between two tuples and the offset is how much space is between 0 and the first appearance of the first tuple

eg for triangles with 3 floats per position and then 3 floats for normal it looks like this

glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 24, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1,3, GL_FLOAT, GL_FALSE, 24, (GLvoid*) (sizeof(float)*3));
glEnableVertexAttribArray(1);

So your vertex pointer has an offset of 0 (assuming that the first three entries are positions) and a stride of 24 (assuming that your vertex color comes directly after the vertex positon)

for the color pointer the offset will be (GLvoid*) (sizeof(float)*3) as three GLfloats before the first color triple

This all assumes that your interleaved array is built like this

VVVCCCVVVCCCVVVCCC and so on

For a very simple example in C++ using a model having only vertex and normal information as well as faces, you can check this code out: http://www.incentivelabs.de/Sourcecode/OpenGL10.zip

3
votes

Your description is not really consistent: 3 floats per vertex are not enough to describe a 3D position and a RGB color. YOur code suggests that you actually have 6 values per vertex, so the buffer might look like

    +0    +4    +8    +12   +16   +20
 0: pos.x pos.y pos.z col.r col.g col.b
24: pos.x pos.y pos.z col.r col.g col.b
...

The stride is the "distance" of two consecutive values of the same attribute, so in this case, it is - as you correctly set - 6*sizeof(GLfloat). The offset is the byte offset of the attribute relative to the start of the buffer. In my example, it is 0 for pos and 3*sizeof(GLfloat) for col. You are setting both to the same offset, so you use the same data for different attributes, which is seldom a good idea.

Also, your buffer setup seems unclear. The line

glDrawArrays(GL_QUADS, 0,  etn.getVboData().capacity());

suggests that etn.getVboData().capacity() is the number of vertices in the buffer. If so,

glBufferDataARB(GL_ARRAY_BUFFER_ARB, etn.getVboData().capacity() << 2, GL_STATIC_DRAW_ARB);

is clearly wrong, since you need 6*sizeof(GLfloat) bytes per vertex, so you copy only parts of the data, and you are accessing out-ouf-buffer data, which is undefined behavior and could result in all kinds of strange effects.