PDA

View Full Version : Problems Rendering Vertex Buffer Objects



Eimrik
03-29-2012, 07:44 AM
Hi - I have been using opengl for some time now and have decided to use vertex buffer objects within my graphics engine. However I am having problems in that its seems not to be drawing and if it is, it is not texture properly.

Here is the code I have for binding vertex buffer and buffer index lists:

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">void OpenGLGraphicsModule::bindObject(string model)
{
VertexBufferModule *vertex_buffer = new VertexBufferModule();
vertex_buffer->createVertexBuffer(model);

map<string, GLuint>::iterator buffer_list_itr = buffer_list.find(model);
map<string, Vertex_Index>::iterator buffer_index_list_itr = buffer_index_list.find(model);

Vertex_Index vertex_index;
vertex_index.buffer_index = buffer_index_list.size();
vertex_index.buffer_size = vertex_buffer->getVertexIndexBuffer()->size();

buffer_list.insert(pair<string, GLuint>(model, buffer_list.size()));
buffer_index_list.insert(pair<string, Vertex_Index>(model, vertex_index));

buffer_list_itr = buffer_list.find(model);
buffer_index_list_itr = buffer_index_list.find(model);

glGenBuffers(1, &amp;buffer_list_itr->second);
glBindBuffer(GL_ARRAY_BUFFER, buffer_list_itr->second);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertex_buffer->getVertexBuffer()->size(), &amp;vertex_buffer->getVertexBuffer()[0], GL_STATIC_DRAW);

glGenBuffers(1, (GLuint*)&amp;buffer_index_list_itr->second.buffer_index);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer_index_list_itr->second.buffer_index);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * vertex_buffer->getVertexIndexBuffer()->size() , &amp;vertex_buffer->getVertexIndexBuffer()[0], GL_STATIC_DRAW);
}[/QUOTE]</div>

Here is the Vertex_Index and vertex structs:
<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">typedef struct
{
float vertex_x, vertex_y, vertex_z;
float normal_vertex_x, normal_vertex_y, normal_vertex_z;
float texture_vertex_x, texture_vertex_y, texture_vertex_z;

}Vertex;

typedef struct
{
int buffer_index;
int buffer_size;

}Vertex_Index;[/QUOTE]</div>

And finally here is the drawing code (this is where I have a feeling I am going wrong):
<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">[/spoiler]void OpenGLGraphicsModule::drawModel(string model, string texture, GLfloat scale_factor)
{
map<string, GLuint>::iterator buffer_list_itr = buffer_list.find(model);
map<string, Vertex_Index>::iterator buffer_index_list_itr = buffer_index_list.find(model);
map<string, GLuint>::iterator texture_list_itr = texture_list.find(texture);

glClientActiveTexture( GL_TEXTURE0 );
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, buffer_list_itr->second);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));
glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
glTexCoordPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(24));



glBindTexture(GL_TEXTURE_2D, texture_list_itr->second);
glScalef(scale_factor, scale_factor, scale_factor);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer_index_list_itr->second.buffer_index);
glDrawElements(GL_TRIANGLES, buffer_index_list_itr->second.buffer_size, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}[/QUOTE]</div>

Thanks for any help in advance.

EDIT: I appologise if this post is in the wrong spot.

Dan Bartlett
03-29-2012, 09:02 AM
You have:


glGenBuffers(1, (GLuint*)&amp;buffer_index_list_itr->second.buffer_index);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer_index_list_itr->second.buffer_index);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * vertex_buffer->getVertexIndexBuffer()->size() , &amp;vertex_buffer->getVertexIndexBuffer()[0], GL_STATIC_DRAW);

seeing the sizeof(int), then maybe you should be using GL_UNSIGNED_INT instead of GL_UNSIGNED_SHORT for


glDrawElements(GL_TRIANGLES, buffer_index_list_itr->second.buffer_size, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));
or the size you are allocating for the index buffer is 2x bigger than necessary (but won't cause any problems).

Eimrik
03-29-2012, 08:07 PM
Hey

Thanks for the response.

I have changed this line:

glDrawElements(GL_TRIANGLES, buffer_index_list_itr->second.buffer_size, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));


to used GL_UNSIGNED_INT, but it hasn't seemed to make any difference. That said having an index buffer twice the size than necessary may end up producing wierd results later on, so for that I thank you.

Eimrik
03-30-2012, 09:37 PM
Fixed it!

When I was binding the buffer data using glBufferData I wasn't pointing to the right arrays.

Changed:


glGenBuffers(1, &amp;buffer_list_itr->second);
glBindBuffer(GL_ARRAY_BUFFER, buffer_list_itr->second);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertex_buffer->getVertexBuffer()->size(), &amp;vertex_buffer->getVertexBuffer()[0], GL_STATIC_DRAW);

glGenBuffers(1, (GLuint*)&amp;buffer_index_list_itr->second.buffer_index);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer_index_list_itr->second.buffer_index);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * vertex_buffer->getVertexIndexBuffer()->size() , &amp;vertex_buffer->getVertexIndexBuffer()[0], GL_STATIC_DRAW);

To:

glGenBuffers(1, &amp;buffer_list_itr->second);
glBindBuffer(GL_ARRAY_BUFFER, buffer_list_itr->second);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertex_buffer->getVertexBuffer()->size(), &amp;vertex_buffer->getVertexBuffer()[0][0], GL_STATIC_DRAW);

glGenBuffers(1, (GLuint*)&amp;buffer_index_list_itr->second.buffer_index);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer_index_list_itr->second.buffer_index);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * vertex_buffer->getVertexIndexBuffer()->size() , &amp;vertex_buffer->getVertexIndexBuffer()[0][0], GL_STATIC_DRAW);