OSX OpenGL 4.1 glEnableVertexAttribArray glDrawArrays GL_INVALID_OPERATION

时间:2014-10-15 08:51:42

标签: c++ macos opengl

我使用SDL窗口编写了一个OpenGL应用程序。核心配置文件在Mac OSX 10.9上运行OpenGL 4.1。命令glClearColor效果很好,我在屏幕上得到了正确的颜色。但是在我的渲染器中,每次执行命令GL_INVALID_OPERATIONglEnableVertexAttribArray时都会出现glDrawArrays错误。

这就是代码:

顶点着色器:

#version 410

in vec4 position;

void main(void)
{
    gl_Position = position;
}

片段着色器:

#version 410

out vec4 out_Color;

void main(void)
{
    out_Color = vec4(0.0, 1.0, 0.0, 1.0);
}

所有着色器编译并链接到程序。没有链接错误!

使用数据创建缓冲区:

static const float VertexBufferData[] = {
            0.75f, 0.75f, 0.0f, 1.0f,
            0.75f, -0.75f, 0.0f, 1.0f,
            -0.75f, -0.75f, 0.0f, 1.0f,
 };

 glGenBuffers(1, &m_VertexBuffer);
 glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
 glBufferData(GL_ARRAY_BUFFER, sizeof(VertexBufferData), VertexBufferData, GL_STATIC_DRAW);
 glBindBuffer(GL_ARRAY_BUFFER, 0);

渲染:

glUseProgram(m_ShaderProgram);

glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);

glEnableVertexAttribArray(0);

glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 3);

// glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);

glDisableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glUseProgram(0);

输出:

Output of my application with profiler description

1 个答案:

答案 0 :(得分:1)

AS BDL已经说过:解决方案是创建一个VAO。作为简短描述:VAO(顶点数组对象)是VBO的集合,它描述了着色器的顶点布局和顶点。

此处有更多信息:

所以我更改了以下代码块:

<强>缓冲液:

    static const float VertexBufferData[] = {
            0.75f, 0.75f, 0.0f, 1.0f,
            0.75f, -0.75f, 0.0f, 1.0f,
            -0.75f, -0.75f, 0.0f, 1.0f,
        };

    glGenVertexArrays(1, &m_VertexBufferArray);
    glBindVertexArray(m_VertexBufferArray);

    glGenBuffers(1, &m_VertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(VertexBufferData), VertexBufferData, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);

    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

    glBindVertexArray(0);

<强>渲染:

    glUseProgram(m_ShaderProgram);

    glBindVertexArray(m_VertexBufferArray);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glBindVertexArray(0);

    glUseProgram(0);

<强>结果:

Final working result.