Sign in to follow this  

VBO rendering issues

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

Hey.

 

I've been trying to get my rendering working but I'm sure I'm missing something and I can't see what I'm doing wrong here..

struct Model_OBJ
{
   vector<GLfloat> vertices;
   vector<GLushort> elements;

   GLuint vbo_vertices;
   GLuint ibo;
};

Init:

glGenBuffers(1,&Model[i].ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,Model[i].ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,Model[i].elements.size() * sizeof(GLushort),Model[i].elements.data(),GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
		
glGenBuffers(1,&Model[i].vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER,Model[i].vbo_vertices);
glBufferData(GL_ARRAY_BUFFER,Model[i].vertices.size() * sizeof(GLfloat),Model[i].vertices.data(),GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);

Render: this doesn't draw the model

void CCore::DrawModel(Model_OBJ model, float x, float y, float z)
{
...
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,model.ibo);

   glBindBuffer(GL_ARRAY_BUFFER,model.vbo_vertices);
   glEnableClientState(GL_VERTEX_ARRAY);
   glVertexPointer(3,GL_FLOAT,0,&model.vertices.front());

   glDrawElements(GL_TRIANGLES,model.elements.size(),GL_UNSIGNED_SHORT,NULL);

   glDisableClientState(GL_VERTEX_ARRAY);
   glBindBuffer(GL_ARRAY_BUFFER,0);

   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
...
}

Render (alternative): this draws the model

void CCore::DrawModel(Model_OBJ model, float x, float y, float z)
{
...
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,model.ibo);

   //glBindBuffer(GL_ARRAY_BUFFER,model.vbo_vertices);
   glEnableClientState(GL_VERTEX_ARRAY);
   glVertexPointer(3,GL_FLOAT,0,&model.vertices.front());

   glDrawElements(GL_TRIANGLES,model.elements.size(),GL_UNSIGNED_SHORT,NULL);

   glDisableClientState(GL_VERTEX_ARRAY);
   //glBindBuffer(GL_ARRAY_BUFFER,0);

   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
...
}

Am I storing the VBO data somehow wrong? I've googled alot but I can't figure out what's the problem.

Edited by Bob Wade

Share this post


Link to post
Share on other sites

Can I ask you why are you using the fixed functionality? (glEnableClientState...)

 

My guess is binding another buffer without doing anything with the previous buffer would just elminate your indicies which you try to use through glDrawElements.

Try moving the first binding before you the call to glDrawElements.

Share this post


Link to post
Share on other sites

Can I ask you why are you using the fixed functionality? (glEnableClientState...)

 

My guess is binding another buffer without doing anything with the previous buffer would just elminate your indicies which you try to use through glDrawElements.

Try moving the first binding before you the call to glDrawElements.

Didn't cross my mind tbh, I copied stuff from my old project.. haven't been programming in a long long time...

 

I'll try that when I get a chance, thanks

Share this post


Link to post
Share on other sites

What do model.vertices.size(), model.vertices.data() and model.vertices.front() do?  And what data type do the last two return?

 

The answer to this could be really simple, but so far your question is semi-obfuscated because you haven't provided enough information.  In particular, using your own classes but not providing information about what those class members do is not really helping us to help you.

 

Anyway, there are a number of things that could be wrong with your code depending on the answers to the above.  Your glBufferData call could be not placing enough data into the VBO.  Your glVertexPointer call could be using an actual memory pointer rather than an offset into the buffer.  Either would cause your model to not draw (or at best draw a jumbled mess of triangles), so where to go next depends on the answers.

Share this post


Link to post
Share on other sites

Thanks for the reply, I double checked .data() and .front() and got it working with:

void CCore::DrawModel(Model_OBJ model, float x, float y, float z)
{
...
   glBindBuffer(GL_ARRAY_BUFFER,model.vbo_vertices);
   glEnableClientState(GL_VERTEX_ARRAY);
   glVertexPointer(3,GL_FLOAT,0,0);
   // instead of
   // glVertexPointer(3,GL_FLOAT,0,&model.vertices.front());

   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,model.ibo);
   glDrawElements(GL_TRIANGLES,model.elements.size(),GL_UNSIGNED_SHORT,NULL);

   glDisableClientState(GL_VERTEX_ARRAY);
   glBindBuffer(GL_ARRAY_BUFFER,0);

   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
...
}

Share this post


Link to post
Share on other sites

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