Vertex Buffer Object Implementation Help

Started by
5 comments, last by Ademan555 19 years, 2 months ago
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.
Advertisement
Hello,

You might want to take a look at the glInterleavedArrays function. It may be a shortcut to what you want to do.

Does your code work without VBO:s?
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.
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).
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...
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 );
...
"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
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."

This topic is closed to new replies.

Advertisement