0
votes

I'm working on a 2D project using OpenGL 3.2 and I have the following very simple vertex shader

#version 150

in vec2 pos;

void main() { 
    gl_Position = vec4(pos.x, pos.y, 0, 1); 
}

Then using an array and index buffer I draw a simple quad on the screen and everything works fine. The problem comes when I want to pass in a transformation matrix. This comes from a 2D camera class so that screen rotations and zooming can be achieved. what I am finding is that as soon as I pass in the 4x4 matrix as a uniform everything stops being rendered (or being visible).

Having stepped through the code I can see that where it comes to update the uniform, the matrix being passed in is a 4x4 identity matrix, as expected because the camera is not doing anything special yet. I would expect that applying the identity matrix should just result in the same as if it wasn't multiplied at all.

The updated shader is as follows:

#version 150

in vec2 pos;
uniform mat4 MVP;

void main() { 
    gl_Position = MVP * vec4(pos.x, pos.y, 0, 1); 
}

The uniform is updated using glUniformMatrix4fv(mvpPos, 1, GL_FALSE, &m[0][0]);

I'm at a loss as to why this is happening so would appreciate any idea. I know that there is no code posted but am happy to post any particular parts to be looked at and I didn't want to bog the initial question with a wall of possibly irrelevant code.

Update: I have tested declaring a mat4 directly in the shader with the values of an identity matrix. Replacing the MVP with this matrix means it works which points to the issue being in the update of the uniform via glUniformMatrix4fv.

2

2 Answers

2
votes

The problem is that you're setting the value at the wrong location. You should call glGetUniformLocation instead of passing 0 into the first argument of the glUniform* call.

i.e.

GLint mvpLocation = glGetUniformLocation(program, "MVP");
// ...
glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, &m[0][0]);
1
votes

After performing some further investigation using glGetError() and gluErrorString() I found that it was throwing an GL_INVALID_OPERATION. This is unfortunately a bit vague but I was lucky enough to come across someone else who had the same issue.

It was as simple as I had forgot to bind the program before making the call to update the uniform as previously I had only done it in rendering, not needing to use the shader in any updates.