Search code examples
csdl-2glewopengl-3

OpenGL 3.0 glVertexAttribPointer: legacy works, forward compatible (core) doesn’t


This code works fine in the legacy OpenGL 3.0, but fails in the forward compatible mode (4.1 and 3.3 on my setup). I’ve tested it on the hardware and the sofware implementations. I use SDL to get the OpenGL context, and GLEW to find some functions. Adding the line below to my code make the problem.

SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);

The problematic part of code is call to glVertexAttribPointer

glBindBuffer(GL_ARRAY_BUFFER, glb.vbo);
glBufferData(
    GL_ARRAY_BUFFER,
    size * sizeof(GLfloat),
    input,
    GL_DYNAMIC_DRAW
);
test_gl_errors(); /* GL_NO_ERROR */

glUseProgram(glb.program);
glEnableVertexAttribArray(glb.vert_array);
glEnableVertexAttribArray(glb.colour_array); /* I use this later. */

test_gl_errors(); /* GL_NO_ERROR */
glVertexAttribPointer(
    glb.vert_array,
    2,
    GL_FLOAT,
    GL_FALSE,
    5 * sizeof(GLfloat),
    0
);
test_gl_errors(); /* GL_INVALID_OPERATION */

I would like to add more code, but there is a lot of stuff. Reading information from the link below didn’t help me. Any suggestions?
https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml

My OpenGL setup:

  • Gallium 0.4 on AMD CAPE VERDE (DRM 2.45.0 / 4.7.2-1-ck, LLVM 3.8.1)
  • Gallium 0.4 on llvmpipe (LLVM 3.8, 128 bits)
  • Mesa3D: 12.0.1

Solution

  • The working code, based on comments from Reto Koradi.

    glBindVertexArray(GL_ARRAY_BUFFER, glb.vao); /* <-- NEW !!! */
    glBindBuffer(GL_ARRAY_BUFFER, glb.vbo);
    glBufferData(
        GL_ARRAY_BUFFER,
        size * sizeof(GLfloat),
        input,
        GL_DYNAMIC_DRAW
    );
    
    glUseProgram(glb.program);
    glEnableVertexAttribArray(glb.vert_array);
    
    glVertexAttribPointer(
        glb.vert_array,
        2,
        GL_FLOAT,
        GL_FALSE,
        5 * sizeof(GLfloat),
        0
    );
    test_gl_errors(); /* GL_NO_ERROR */