Sign in to follow this  
leonardteo

Passing matrices to GLSL

Recommended Posts

Hey guys,

I'm trying to pass my matrices to GLSL and having a tough time doing it. When using the built in (deprecated) gl_ModelViewProjectionMatrix etc it works, but when defining and sending my own into GLSL, nothing draws....

Here's my code (very simple):

vert shader:


uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;

void main(){
//gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = projectionMatrix * modelViewMatrix * gl_Vertex;
}



Here's my code snippets to send the information


//Projection Matrix
GLfloat projectionMatrix[16];
glGetFloatv(GL_PROJECTION_MATRIX, projectionMatrix);

//Pass to shader
glUniformMatrix4fv(shader->projectionMatrix, 1, false, projectionMatrix);

//MV matrix
GLfloat modelViewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelViewMatrix);
glUniformMatrix4fv(shader->modelViewMatrix, 1, false, modelViewMatrix);




I've debugged and checked that:
- the GLints of the uniforms are present. They are 0, 1;
- Both the projection and Modelview matrices are populating in the glGetFloatv command.

So...I'm just not sure why the above isn't working. When I switch back to using the built-in (deprecated) gl_ModelViewMatrix, etc. everything draws fine.

I'm a bit suspicious of the 'count' argument. I'm not sure if it should be 1 for 1 matrix or 16 for 16 values in the array. Both don't work in this case...

Any help would be most appreciated. Thanks,

Leonard

Share this post


Link to post
Share on other sites
ARGH. I solved it. I had to set the Projection matrix at the time that I was doing the draw call. I was only setting the projection uniform value in another function.

This works. Not exactly pretty or optimized code, but it works.


glUseProgram(this->shader->shaderProgram);

//Pass projection matrix to shader
GLfloat projectionMatrix[16];
glGetFloatv(GL_PROJECTION_MATRIX, projectionMatrix);

//Pass to shader
glUniformMatrix4fv(this->shader->projectionMatrix, 1, false, &projectionMatrix[0]);

//Pass current modelview matrix to shader
GLfloat modelViewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelViewMatrix);
glUniformMatrix4fv(this->shader->modelViewMatrix, 1, false, &modelViewMatrix[0]);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this