VBO rendering issues

Started by
3 comments, last by Bob Wade 8 years, 6 months ago

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.

Advertisement

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.

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

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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);
...
}

This topic is closed to new replies.

Advertisement