1

I programmed an OpenGL application with an SDL window. Core Profile running OpenGL 4.1 on a Mac OSX 10.9. The command glClearColor works great and I get the right color on screen. But inside my renderer I got the GL_INVALID_OPERATION error everytime I execute the commands glEnableVertexAttribArray and glDrawArrays.

That is the code:

Vertex Shader:

#version 410

in vec4 position;

void main(void)
{
    gl_Position = position;
}

Fragment Shader:

#version 410

out vec4 out_Color;

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

All shader compile and linked to an program. No linking error!

Creating buffer with data:

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

Render:

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:

Output of my application with profiler description

Tobias Schwandt
  • 397
  • 2
  • 18
  • 2
    As far as I know, drawing without an active VAO is not allowed in Core profile. Try to generate a vao and bind this before glBindBuffer. – BDL Oct 15 '14 at 11:09
  • Ok. That was the problem. A little bit strange in my opinion. Why did I need an VAO... I thing I have to read the basics again. :-) – Tobias Schwandt Oct 15 '14 at 12:16
  • If you don't explicitly create and bind a VAO, you would just be using the default (0) VAO. Using any kind of default objects is not allowed anymore in the Core Profile. From the spec, under "Deprecated and Removed Features" : "The default vertex array object (the name zero) is also deprecated." – Reto Koradi Oct 15 '14 at 14:47
  • Also see here: http://stackoverflow.com/questions/23566379/how-to-fix-black-screen-with-glfw. – Reto Koradi Oct 15 '14 at 14:53
  • possible duplicate of [OpenGL: INVALID\_OPERATION following glEnableVertexAttribArray](http://stackoverflow.com/questions/24643027/opengl-invalid-operation-following-glenablevertexattribarray) – Reto Koradi Oct 15 '14 at 14:54

1 Answers1

1

AS BDL already said: The solution is to create a VAO. As a short description: A VAO (Vertex Array Object) is a collection of VBOs, which describes the vertex layout and the vertices for the shader.

More informations here:

So I have changed the following code blocks:

Buffer:

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

Render:

    glUseProgram(m_ShaderProgram);

    glBindVertexArray(m_VertexBufferArray);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glBindVertexArray(0);

    glUseProgram(0);

Result:

Final working result.

Community
  • 1
  • 1
Tobias Schwandt
  • 397
  • 2
  • 18