# 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):

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 MatrixGLfloat projectionMatrix[16];glGetFloatv(GL_PROJECTION_MATRIX, projectionMatrix);//Pass to shaderglUniformMatrix4fv(shader->projectionMatrix, 1, false, projectionMatrix);//MV matrixGLfloat 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 on other sites
Hi,

how do you set the bindings for the uniforms?

glGetActiveUniform
glGetUniformLocation
glUniformMatrix*
...

##### 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]);

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628400
• Total Posts
2982449

• 9
• 10
• 9
• 19
• 24