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

#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

#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:

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?

Your order of arguments to glVertexAttribPointer is wrong, you are always setting attribute 0.


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
}

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.

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.

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!