Sign in to follow this  

GLSL rounding errors?

Recommended Posts

chronozphere    142
Hey guys,

I've got the following weird issue.

I'm trying to implement multipass lighting and I encountered some sort of Z-fighting issue. I perform one ambient/diffuse pass with glDepthFunc(GL_LESS) and then a additive light pass with glDepthFunc(GL_EQUAL) with depth writes disabled. When I pan my camera around, I get huge flickering artifacts.

The vertex shader for the light pass contains:
uniform mat4 uModel;uniform mat4 uView;uniform mat4 uProjection; void main() {aPosition = uView * uModel * vPosition;gl_Position = uProjection * aPosition;
I'd pass aPosition to the fragment shader to do the lighting with. When I change this to:

aPosition = uView * uModel * vPosition;gl_Position = uProjection * uView * uModel * vPosition;
it suddenly works without flickering. What's going on?
Does an assignment inbetween some math expressions create errors?

Thanks a bunch! smile.png Edited by chronozphere

Share this post

Link to post
Share on other sites
Yours3!f    1532



I dont think this would be related to numerical errors, but rather what you are doing.


in the first snippet you essentially wrote:

gl_Position = uProjection * uView * uModel * aPosition;

this should output clip-space positions correctly.


in the second you wrote:

gl_Position = uProjection * uView * uModel * uView * uModel * vPosition;

this is rather incorrect.


What I suspect should be the problem is that your light is in the surface you are trying to light.

like you have a plane at y = 1, and the light is located at (0,1,0). This causes z-fighting. Make sure the light is above the surface.


The other thing is: why do you need to change the depth testing?

Share this post

Link to post
Share on other sites
chronozphere    142

Thanks for the reply.


I made a very stupid mistake by writing aPosition instead of vPosition in the second code snippet.


So the following gives artifacts:

aPosition = uView * uModel * vPosition;
gl_Position = uProjection * aPosition;

The following works:

aPosition = uView * uModel * vPosition;
gl_Position = uProjection * uView * uModel * vPosition;   //vPosition instead of aPosition


It seems strange to me why these different lines of code return different results. 


About the depth testing: Yes, I could have set it to GL_LEQUAL and leave it there. Good point!



Share this post

Link to post
Share on other sites
Ravyne    14300

My math is rusty enough that I wouldn't consider this much more than conjecture, but I'd point out that what you're doing seems not to be equivalent.


If you work the first set of equations by substituting the first line into the second, you get:


gl_position = uProjection * (uView *uModel * vPosition);


whereas your second set of equations is simply:


gl_position = uProjection * uView *uModel * vPosition;



With the parenthesis in place, you're transforming a vector through the View and Model matrices (which are first multiplied together) to get an intermediate result, and then transforming that intermediate result by the Projection matrix to get the final result. Without the parenthesis in place, the Projection, View and Model matrices are all multiplied together first, and then used to transform the vector -- there is no intermediate. These are not the same formulas.


Due to floating-point error, even if these formulas were/are mathematically equivilent, the mere introduction of the intermediate result in a z-buffered position might introduce enough difference to place the point in front/behind the intended plane erroneously.



If you like, you can confirm my conjecture by adding the parenthesis into your second equation, and see if that gives results that are similar to the errors you saw.

Edited by Ravyne

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