OpenGL cannot find uniform with glGetUniformLocation for second uniform

2k views Asked by At

Me and a friend of mine are doing some 3d-programming, and we're trying to get out a pointlight. We're are trying to access a uniform for our light's position in the vertex shader but it doesn't get found.

Here is some code:

GLuint program = LoadShaders(shaders);
glUseProgram(program);

rotationLoc = glGetUniformLocation(program,"vRotationMatrix");
if(rotationLoc != -1)
    glUniformMatrix4fv(rotationLoc,1,GL_TRUE,&world.rotationMatrix[0][0]);

LightLoc = glGetUniformLocation(program, "vLightPos");
if (LightLoc != -1)
    glUniform3fv(LightLoc, 1, LightPos);

As you can see we do the same thing for our rotation matrix first. It works for the rotation matrix, while it doesn't for LightLoc. The error line is: LightLoc = glGetUniformLocation(program, "vLightPos"); LightLoc returns -1, but it should return 2.

Our vertex shader looks (partially) like this:

uniform mat4 vRotationMatrix;
uniform vec3 vLightPos;
void main()
{
    color = vColor;
    oTexCoord = vTexCoord;
    gl_Position = vRotationMatrix * vPosition;
    lightPos = vLightPos;
}
1

There are 1 answers

0
Diversity On

Try to output the result of the shader compilation using the code under the comment check vertex shader:

GLint Result = GL_FALSE;
int InfoLogLength;



// Compile Vertex Shader
printf("Compiling shader : %s\n", vertex_file_path);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);

// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if ( InfoLogLength > 0 ){
    std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
    glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
    printf("%s\n", &VertexShaderErrorMessage[0]);
}

Maybe there are any warnings or errors. The same you can use for the fragment shader compilation