Reputation: 2221
I am trying to get simple shaders to work. To be more specific, I am trying to set a uniform value of the shader. The shaders compile successfully and work correctly if I eliminate the uniform value. Here is the (vertex) shader code:
#version 440 core
layout(location = 0) in vec3 vertex;
uniform mat4 mvp;
void main()
{
gl_Position = mvp * vec4(vertex, 1);
}
And the code I use to set the value (glGetUniformLocation
returns 0 as expected):
mvp = glm::mat4(
glm::vec4(3.0f, 0.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 1.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 1.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 0.0f, 2.0f)
);
GLuint matrix = glGetUniformLocation(program, "mvp");
glUniformMatrix4fv(matrix, 1, GL_FALSE, glm::value_ptr(mvp));
The problem is that it doesn't seem to set the value at all. If I query the uniform's value using glGetUniformfv
, it returns the default value (not the one I just set), and the geometry doesn't show up at all (but that is just a consequence, that's not the problem here).
If I hardcode the uniform's value in the shader, the application displays the geometry correctly, and when asked for the value (again, using glGetUniformfv
), it returns the correct value.
#version 440 core
layout(location = 0) in vec3 vertex;
uniform mat4 mvp = mat4(
vec4(1.0f, 0.0f, 0.0f, 0.0f),
vec4(0.0f, 1.0f, 0.0f, 0.0f),
vec4(0.0f, 0.0f, 1.0f, 0.0f),
vec4(0.0f, 0.0f, 0.0f, 2.0f)
);
void main()
{
gl_Position = mvp * vec4(vertex, 1);
}
Any idea why does this happen? I am using OpenGL 4.4, but I have tried many different versions with the same result.
Upvotes: 6
Views: 17007
Reputation: 3504
Are you calling glUseProgram
before setting the uniforms? A full working listing can be found in many examples online, including what I had written sometime back (Github: sgxperf/sgxperf_gles20_vg.cpp
) that shows the sequence to be:
glCreateProgram() glAttachShader() glLinkProgram() glUseProgram() glGetUniformLocation() glUniformMatrix4fv()
Upvotes: 15