尝试使用vbo OpenGL绘制球体时出错

时间:2019-05-21 23:38:12

标签: c++ opengl vbo

我试图绘制一个球体,但我不断得到

  

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x0

此行错误:

<div layout="row" class="option">
    <md-switch class="md-primary var-label" aria-label="graded" ng-model="ctrl.settingsService.generalSettings.graded" ng-change="ctrl.onChangeGraded()">
         <strong>Graded</strong>
    </md-switch>
    <p flex=""><span ng-if="ctrl.settingsService.generalSettings.graded">The quiz has at least one graded question (a question that has a right/wrong answer).</span><span ng-if="!ctrl.settingsService.generalSettings.graded">It's not a graded quiz.</span>
    </p>
</div>

我不确定这是什么意思。

以下是我的代码:

glGenBuffers(1, &ballVbo);

1 个答案:

答案 0 :(得分:0)

GL_VERTEX_ARRAY分别不是glBindBuffer glBufferData的有效目标参数。这将导致GL_INVALID_ENUM错误。您必须使用GL_ARRAY_BUFFER
注意OpenGL错误可以通过glGetErrorEnabling Debug Output得到。

在使用任何OpenGL指令之前,需要有效的当前OpenGL Context

这意味着:

  1. 创建窗口和Curretn OpenGL上下文,例如glutCreateWindow

  2. 通过glewInit初始化GLEW

  3. 初始化OpenGL缓冲区

GLuint ballVbo;

int main (int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(350,350);
    glutCreateWindow("Solid Sphere");

    glewExperimental = true;
    if (glewInit() != GLEW_OK)
      return 0;

    std::vector<GLfloat> ballVerts;

    for(int i = 0; i <= 40; i++)
    {
        // [...]
    }

    glGenBuffers(1, &ballVbo);
    glBindBuffer(GL_ARRAY_BUFFER, ballVbo);

    GLuint sphereSize = 3362*7*4; 
    glBufferData(GL_ARRAY_BUFFER, sphereSize, &ballVerts[0], GL_STATIC_DRAW);

    // [...]

}
{

    // [...]

    glBindBuffer(GL_ARRAY_BUFFER, ballVbo);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 7*4, 0);
    glEnableClientState(GL_COLOR_ARRAY);
    glColorPointer(4, GL_FLOAT, 7*4, (void*)(3*4));

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 3362);

   // [...]
}

旁注:41 * 41 * 2 = 3362而不是3200。for(int j = 0; j <= 40; j++)是41次迭代,而不是40次。