0

When I'm trying to draw a triangle by using VBO + indices it's not working

   vertices.push_back(0.5f);
   vertices.push_back(-0.5f);
   vertices.push_back(-2.0f);
   vertices.push_back(-0.5f);
   vertices.push_back(-0.5f);
   vertices.push_back(-2.0f);
   vertices.push_back(0.5f);
   vertices.push_back(-0.5f);
   vertices.push_back(-2.0f);

   indices.push_back(0);
   indices.push_back(1);
   indices.push_back(2);


   GLuint bufferID,bufferID2;

   glGenBuffers(1,&bufferID);
   glGenBuffers(1,&bufferID2);
   glBindBuffer(GL_ARRAY_BUFFER,bufferID);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,bufferID2);
   glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*3,&vertices[0],GL_STATIC_DRAW);

   glEnableClientState(GL_VERTEX_ARRAY);

   glVertexPointer(3,GL_FLOAT,0,0);
   glDrawElements(GL_TRIANGLES,indices.size(),GL_UNSIGNED_INT,&indices[0]);

   glDisableClientState(GL_VERTEX_ARRAY);

   glDeleteBuffers(1,&bufferID);
   glDeleteBuffers(1,&bufferID2);

but drawing a point with VBO it's working

vertices.push_back(0.5f);
vertices.push_back(-0.5f);
vertices.push_back(-2.0f);

GLuint bufferID;

glGenBuffers(1,&bufferID);
glBindBuffer(GL_ARRAY_BUFFER,bufferID);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*3,&vertices[0],GL_STATIC_DRAW);



glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,0);

glPointSize(100.0f);
glDrawArrays(GL_POINTS,0,1);
glDisableClientState(GL_VERTEX_ARRAY);

glDeleteBuffers(1,&bufferID);

I already read through some tutorials and other posts, but nothing works. mby I'm not using the indices correctly ?

anyone can help me with this problem?

jeromintus
  • 367
  • 1
  • 5
  • 14
  • Maybe this helps? http://stackoverflow.com/questions/7561165/rendering-kinect-point-cloud-with-vertex-buffer-object-vbo – Ian Medeiros May 19 '14 at 14:19

2 Answers2

2

you only pass 3 floats to the VBO you want to pass more:

glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*vertices.size(),&vertices[0],GL_STATIC_DRAW);

and when you have bound the GL_ELEMENT_ARRAY_BUFFER the void* in glDrawElements is relative to the bound buffer:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*indices.size(), &indices[0], GL_STATIC_DRAW);

glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);

and always check glGetError when you have problems, it will help understand at which calls you start going wrong.

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
  • glGenBuffers(1,&bufferID); glGenBuffers(1,&bufferID2); glBindBuffer(GL_ARRAY_BUFFER,bufferID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,bufferID2); glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*vertices.size(),&vertices[0],GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(GLuint)*indices.size(),&indices[0],GL_STATIC_DRAW); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3,GL_FLOAT,0,0); glDrawElements(GL_TRIANGLES,indices.size(),GL_UNSIGNED_INT,0); glDisableClientState(GL_VERTEX_ARRAY); still doesn't work. hmm also got no error from glGetError – jeromintus May 19 '14 at 15:00
  • nvm it works! i had wrong coordinates in my vector -> for the triangle – jeromintus May 20 '14 at 10:35
1

On top of the problems @ratched_freak already covered in his answer (wrong size passed to glBufferData for vertices, glBufferData not called for index buffer, and updating the last argument to glDrawElements accordingly), you have another problem. Take a close look at your coordinates:

vertices.push_back(0.5f);
vertices.push_back(-0.5f);
vertices.push_back(-2.0f);
vertices.push_back(-0.5f);
vertices.push_back(-0.5f);
vertices.push_back(-2.0f);
vertices.push_back(0.5f);
vertices.push_back(-0.5f);
vertices.push_back(-2.0f);

The first and third vertex are the same. So you have a degenerate triangle.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133