PDA

View Full Version : problem with passing matrix to glsl



billconan
02-17-2011, 07:05 PM
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()

{
gl_Position=ftransform();
}




now if i set the modelview and projection matrix with gluPerspective and gluLookAt, everything renders fine.
i can also change the ftransform to gl_ProjectionMatrix*gl_ModelViewMatrix*gl_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()
{
gl_Position=projection*modelview*gl_Vertex;
}




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:



glUseProgram(secondpass);



glGetFloatv(GL_MODELVIEW_MATRIX,tmodelview);
glGetFloatv(GL_PROJECTION_MATRIX,tprojection);

glUniformMatrix4fv(modelviewUniform,1,GL_FALSE,tmo delview);
glUniformMatrix4fv(projectionUniform,1,GL_FALSE,tp rojection);






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.

Alfonse Reinheart
02-17-2011, 07:18 PM
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 "projection*modelview*gl_Vertex". If you need invariance, you need to use user-defined matrices in both shaders.

Dark Photon
02-18-2011, 12:09 PM
...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.
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?