我正在尝试使用 VBO 绘制四边形,这是完整的代码,但我将逐步发布代码:这是我初始化缓冲区的方式:
data= vector<GLfloat> { // Global variable vector<GLfloat> data;
// Viewport: glViewport(0,0,width,height);
width/2+20,height/2+20,0.0,
width/2+20,height/2-20,0.0,
width/2-20, height/2-20,0.0,
width/2-20, height/2+20,0.0
};
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), data.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
然后在显示功能中我尝试绘制四边形(我使用双缓冲):
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, data.data());
glDrawArrays(GL_QUADS, 0, 4); // Here I get segmentation fault
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glutSwapBuffers();
我在调用的行出现分段错误glDrawArrays
,我还尝试使用 获取 OpenGL 错误glGetError()
,但没有错误(它返回零)。