简体   繁体   中英

OpenGL VBO not working (C++)

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM