Problem with passing matrix to glsl

hello guys,

i’m trying to debug a problem i met while implementing the shadow map method.

i first render the scene from the light source. and save the depth map onto a texture.
and then, i write a second pass, use the modelview matrix and the projection matrix from the first pass to find the texture coordinate of a vertex on the shadow map.

the problem is that i cannot pass the matrices from the first pass to the glsl shader of the second pass properly.

i striped down my program and did some simple tests:

here is the vertex shader:

void main()


now if i set the modelview and projection matrix with gluPerspective and gluLookAt, everything renders fine.
i can also change the ftransform to gl_ProjectionMatrixgl_ModelViewMatrixgl_Vertex; , everything is still working.

however, if i pass the modelview matrix and projection matrix to two uniform mat4 variables i defined in the shader, then the rendering becomes wrong, i can’t see anything.

uniform mat4 modelview;
uniform mat4 projection;

void main()

i’m sure that the uniform location ids are all valid. they are 1 and 0 in my program.

and this is how i get the two matrices inside my c++ program and pass them to the shader:




my self-defined matrices should have the same values as those in the gl_ProjectionMatrix and the gl_ModelViewMatrix, because i just acquired them from the opengl context before passing them to the shader.

I am working under ubuntu 10.10 with nvidia 8800GTX. I have stuck on this for two days, can’t figure out why.

thank you for your help.

the problem is that i cannot pass the matrices from the first pass to the glsl shader of the second pass properly.

It’s time to talk a bit about invariance and GLSL.

If you have two separate programs, and you want them both to be able to generate the exact same position, you must do the following:

1: Ensure that they are given the same inputs/attributes. They don’t need to use the same attribute indices, but any input values that factor into the position output needs to be binary-identical.

2: They must do the exact same math to compute the position output.

3: Any uniforms that are used to compute the position output must be binary-identical between the two. If textures are involved in computing the position, then the texture filtering can’t change between executions.

4: You must redeclare gl_Position with the “invariant” qualifier.

You’re likely running afoul of #3 and #4. You hit #3 because you’re using ftransform() in one instance, which is not invariant with “projectionmodelviewgl_Vertex”. If you need invariance, you need to use user-defined matrices in both shaders.

Let’s see a stand-alone GLUT test program we can actually look at and try. Sounds to me like your matrix settings or wrong somehow, or you’re setting them on the uniforms wrong. Maybe you’ve got your modelview and projection uniform handles swapped?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.