Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Issue with GLSL - Trying to use vertex colors

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    3

    Issue with GLSL - Trying to use vertex colors

    I have been working on some functions to try and get to understand OpenGL (I have started to learn it a short time ago). I am was trying to make the typical "spinning cube" application. I wanted to use vertex colors for the sides of the cube (I know that they aren't normally used, but I don't know any alternative yet). I am using the following shaders:

    Vertex Shader
    Code :
    #version 400
    layout(location = 1) in vec3 vcolor;
    in vec3 vp;
    out vec3 fragmentColor;
    void main () {
      gl_Position = vec4 (vp, 1.0);
      fragmentColor = vcolor;
    }

    Fragment Shader
    Code :
    #version 400
    in vec3 fragmentColor;
    out vec3 color;
    void main () {
      color = fragmentColor;
    }

    The shader compiles correctly, and all, but I keep getting a black screen. If I change the line color = fragmentColor; to color = vec4( 1.0, 0.0, 0.0, 1.0); it works, which has led me to believe the issue may have been with the glVertexAttribPointer function:

    Code :
    void SimpleMesh::BindVBO ( int index, int attribInd )
    {
        glBindBuffer ( GL_ARRAY_BUFFER, vboVec_[ index ]);
        glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, attribInd, NULL );
    }

    But I see nothing wrong with it. I know the location is 1, I know the color vector is not null, and in a vec3 format, I checked any possible cause I could think of, but I really pinpoint the cause of the issue. If anyone could help I'd be grateful.

    Related to this, I would like to ask: since vertex colors are rarely used, how would one color the faces of a square?

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    894
    Your order of arguments to glVertexAttribPointer is wrong, you are always setting attribute 0.

    Code :
    void SimpleMesh::BindVBO ( int index, int attribInd )
    {
        glBindBuffer ( GL_ARRAY_BUFFER, vboVec_[ index ]);
        glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, attribInd, NULL );
                                ^                             ^
                                attribInd here             stride (probably 0) here
    }

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    3
    Even then, the screen is still black. I have tried to do some other changes in my program, but it still hasn't done much.

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    894
    Sorry to hear that, but with the available information I'm not really sure what else to do. You could try an OpenGL Debugger e.g. gDEBugger or apitrace to see if there is perhaps an OpenGL error that gets overlooked.

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    3
    Thanks for the recommendations, the second one in particular seems very useful. I managed to fix the issue, not completely sure how... But thanks for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •