Sign in to follow this  

Vertex Buffer Object Implementation Help

This topic is 4686 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

Sorry if this thing had been asked before. I have tried searching the forums but I get an error message. I am still quite a newb and I am trying to get my head in implementing Vertex Buffer Objects. This is my struct typedef struct _my_vertex { float tu, tv; float r, g, b, a; float nx, ny, nz; float x, y, z; } my_vertex; I fill g_vertices with information about my model. Next, I then create my buffer unsigned int g_VBOvertices; glGenBuffersARB( 1, &g_VBOvertices ); glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_VBOvertices ); glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(g_vertices), g_vertices, GL_STATIC_DRAW_ARB ); Then my rendering loop { glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_VBOvertices ); glTexCoordPointer(2, GL_FLOAT, 12 * sizeof(float), 0); glColorPointer(4, GL_FLOAT, 12 * sizeof(float), (void*)(2 * sizeof(float)) ); glNormalPointer(GL_FLOAT, 12 * sizeof(float), (void*)(6 * sizeof(float)) ); glVertexPointer(3, GL_FLOAT, 12 * sizeof(float), (void*)(9 * sizeof(float)) ); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glDrawArrays(GL_TRIANGLES, 0, numvertices); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_COLOR_ARRAY); } My program crashes and I cannot figure out why. Am I setting the offsets of my gl*Pointer functions correctly? Any help will be very much appreciated.

Share this post


Link to post
Share on other sites
Quote:
Original post by skanatic
Does your code work without VBO:s?


yeah. it works without the VBO and I used the glInterleavedArrays function in that implementation. However, I thought using glInterleavedArrays makes it impossible to do multi-texturing which is essential in lightmaps.

Share this post


Link to post
Share on other sites
Three things spring out at me;

Firstly, with the size parameter replace 12 * sizeof(float) with sizeof(_my_vertex)
Secondly, what is g_vertices ? Because that sizeof(g_vertices) looks a tad suspect.
Finally, the parameters passed to VBOs dont want to be cast to void, casting to a char* is better, alot of people use the macro #define BUFFER_OFFSET(i) ((char *)NULL + (i)) todo VBO offsets. So it would look like BUFFER_OFFSET(2*sizeof(float)) for example.

And yes, glInterleavedArrays is FAR to restrictive to be usefull, I'd forget about it for the most part (it might not even be an optermised route on some drivers).

Share this post


Link to post
Share on other sites
Quote:
Original post by codechallenged
Sorry if this thing had been asked before. I have tried searching the forums but I get an error message. I am still quite a newb and I am trying to get my head in implementing Vertex Buffer Objects.

This is my struct

typedef struct _my_vertex
{
float tu, tv;
float r, g, b, a;
float nx, ny, nz;
float x, y, z;
} my_vertex;


I fill g_vertices with information about my model.

Next, I then create my buffer

unsigned int g_VBOvertices;

glGenBuffersARB( 1, &g_VBOvertices );
glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_VBOvertices );
glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(g_vertices), g_vertices, GL_STATIC_DRAW_ARB );

Then my rendering loop

{
glBindBufferARB( GL_ARRAY_BUFFER_ARB, g_VBOvertices );
glTexCoordPointer(2, GL_FLOAT, 12 * sizeof(float), 0);
glColorPointer(4, GL_FLOAT, 12 * sizeof(float), (void*)(2 * sizeof(float)) );
glNormalPointer(GL_FLOAT, 12 * sizeof(float), (void*)(6 * sizeof(float)) );
glVertexPointer(3, GL_FLOAT, 12 * sizeof(float), (void*)(9 * sizeof(float)) );

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLES, 0, numvertices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}

My program crashes and I cannot figure out why. Am I setting the offsets of my gl*Pointer functions correctly? Any help will be very much appreciated.


Well I can tell you this your call to


glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(g_vertices), g_vertices, GL_STATIC_DRAW_ARB );



is wrong. Your sizeof(g_vertices) is not in machine units...
"<size> set to the size of the data store in basic machine units" from the extension page... And I hope that g_vertices is a pointer or an array? if not you need &g_vertices. These are a few things I see just glancing at your code you posted...

Share this post


Link to post
Share on other sites
g_vertices is a pointer as in:

my_vertex* g_vertices;

... you guys are probably right that that is the problem.

would this be proper then?



int numvertices; // to be filled in at model loading
...
glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(_my_vertex) * numvertices, g_vertices, GL_STATIC_DRAW_ARB );
...

Share this post


Link to post
Share on other sites
"int numvertices; // to be filled in at model loading
...
glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(_my_vertex) * numvertices, g_vertices, GL_STATIC_DRAW_ARB );
..."

yeah, thats how it should be, i didnt really look hard at the rest of your code there may be other problems, but yeah

hope that helps
-Dan

Share this post


Link to post
Share on other sites

This topic is 4686 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this