VBO Color Problems

Started by
1 comment, last by SuperVGA 13 years, 7 months ago
Hello everyone,

I'm trying to pack my homemade vertex struct into a VBO and send it off to be rendered.
Currently, it looks like I got the geometry right. The trouble concerns the
colours, that are a part of my struct:

struct opaque_vertex{    float x, y, z;                  // Vertex (3*4 bytes)    float r, g, b;                  // Color (3*4 bytes)    float padding[2];               // Padding to reach 32 bytes (2*4 bytes)};

The colours are set properly after the vertex coords have been set.
They are basically just the x,y,z -components scaled according to some
maximum on each axis.

I recently changed to this from using two seperate vectors of vertices,
one for position and one for color.
I thoght I liked this the most. Keeps things simple, and should be quite fast.

Now, when I have created my VBO, I use this for rendering:

glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_id);glVertexPointer(3, GL_FLOAT, sizeof(float)*8, NULL);glColorPointer(3, GL_FLOAT, sizeof(float)*8, NULL);glEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_COLOR_ARRAY);glDrawArrays(GL_QUADS, 0, vertex_count);glDisableClientState(GL_COLOR_ARRAY);glDisableClientState(GL_VERTEX_ARRAY);

Only I'm a little worried about all those arrays there, I should have just one,
and a single vertex buffer with all the required information.

I'm also passing NULL as the offset for color data, but if I want to forget my
static vertex data, my vertex list is gone at this point my VBO has been created,
so it's not like I can retrieve a proper address for the colour part.

EDIT (But what does it look like?):
But the colours shown clearly represents the positions of the vertices.
Ex, a vertex ocurring in 0,0,0 (before translations, naturally) will be black,
and vertices from >1,0,0 will be red. Most of the vertices thus, are white.

What am I doing wrong?

[Edited by - SuperVGA on August 23, 2010 7:24:00 AM]
Advertisement
The 0 offset for the color pointer makes the color array point to the vertex data. The color array starts at sizeof(float)*3 bytes into the vertex buffer.
glVertexPointer(3, GL_FLOAT, sizeof(float)*8, NULL);glColorPointer(3, GL_FLOAT, sizeof(float)*8, reinterpret_cast<void *>(sizeof(float)*3));

Adjust he cast to make the compiler happy, if necessary.
Oh! Thanks Bob, now it looks great!

So the offset is in fact, as an offset should be, a relative offset.
I thought it was some ugly stuff with a void pointer to beyond
where the vertex data starts in local ram. Well good thing it wasn't.

Thanks again, now my color components are smoothed actross the span of my geometry.
:D

This topic is closed to new replies.

Advertisement