Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Tags in this Discussion

Using COLOR semantic with OpenGL 3
  • I've recently started porting an old demo I wrote using OpenGL 2 and CG to OpenGL 3 to expand on it. In the OpenGL 2 version I was using vertex arrays like this:


    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, triVerts);
    glColorPointer(3, GL_FLOAT, 0, triColors);
    glDrawArrays(GL_TRIANGLES, 0, 9);
    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);


    This worked fine but I noticed when I changed my code to use vertex buffer objects for OpenGL 3 that only the position worked. The colour data wasn't being applied. This is how I'm using the VBOs now:


    GLuint vao, vbo[2];
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glGenBuffers(2, vbo);

    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), triVerts, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), triColors, GL_STATIC_DRAW);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    glDeleteBuffers(2, vbo);
    glDeleteVertexArrays(1, &vao);


    I tried changing the semantic for the colour paramter in my vertex shader from COLOR to ATTR1 and now the colours work but I'm wondering if that's the correct way to be doing it.

    Is there any way to use the COLOR semantic with OpenGL 3.0 VBOs?
  • 1 Comment sorted by
  • I'm not sure if this is the correct way to be doing things but for now I'm adopting multiple entry points, one with proper semantics and one with generic semantics. Then I'm calling the proper entry point from the generic one like this:


    TestShaderOutput TestShader(float3 position : POSITION,
    float3 colour : COLOR)
    {
    ...
    }

    TestShaderOutput TestShaderATTR(float3 position : ATTR0,
    float3 colour : ATTR1)
    {
    return TestShader(position, colour);
    }


    That way, I can use the proper entry point from my old fixed function code and the generic entry point from OpenGL 3. It's probably not ideal but it'll do until I find the correct solution.