OpenGL VBO无法正常工作(C ++)

时间:2014-05-19 14:15:38

标签: c++ opengl vbo

当我尝试使用VBO +索引绘制三角形时,它无法正常工作

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

但是用VBO指出它正在运作

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

我已经阅读了一些教程和其他帖子,但没有任何作用。 我是不是正确使用了这些指数?

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

你只将3个花车传递给你想要传递更多的VBO:

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

当你绑定了GL_ELEMENT_ARRAY_BUFFER glDrawElements中的void *是相对于绑定缓冲区的时候:

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

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

并在遇到问题时始终检查glGetError,这将有助于了解您哪些电话开始出错。

答案 1 :(得分:0)

除了问题@ratched_freak已经在他的答案中提到了问题(错误的大小传递给glBufferData顶点,glBufferData没有调用索引缓冲区,并且将最后一个参数更新为glDrawElements因此,你有另一个问题。仔细看看你的坐标:

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

第一个和第三个顶点是相同的。所以你有一个退化的三角形。