2
votes

I have the following shaders: My fragment shader:

#version 110
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;


void main() {
    gl_FragColor = vec4(1, 0, 0, 1);
}

And my vertex shader:

#version 110

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

attribute vec3 vertex;

void main() {
    vec4 world =  modelMatrix * vec4(vertex, 1);
    vec4 camera = world * viewMatrix;
    gl_Position = projectionMatrix * world;
}

They both compile and link just fine. When I print out my active uniforms I get

projectionMatrix
modelMatrix

but no viewMatrix. When I try and get the Uniform with glGetUniformLocation, I can get projectionMatrix, modelMatrix, and my vertex attribute just fine, so why is viewMatrix inactive?

1

1 Answers

6
votes

The problem lies in the last line of your vertex shader:

gl_Position = projectionMatrix * world;

You probably meant projectionMatrix * camera. Otherwise, the GLSL compiler sees that camera isn't being used and optimizes it away, which means that viewMatrix is no longer being used either. Unused uniforms are not considered active, which leads to your predicament.

Note: Your viewing transform is also backwards. You probably want vec4 camera = viewMatrix * world.