Jump to content
  • Advertisement
Sign in to follow this  
SuperVGA

VBO Color Problems

This topic is 2858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!