Jump to content
  • Advertisement
Sign in to follow this  
leonardteo

Passing matrices to GLSL

This topic is 2749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!