I'm rather embarrassed to ask for help on this but I can't figure out what the problem is. I want to simulate basic phong shading in GLSL. However, the resulting shading is influenced by the camera's orientation. Can you spot my mistake? I've tried switching spaces, to no avail.

Not necessary since you're not projecting with the modelview matrix. Furthermore, I'd translate the worldspace light direction to eye coordinates. What you do here is compute the difference of an eye and world space vector:

Also, normalizing in the vertex shader is wasted since for correct results you'll have to re-normalize the interpolated vector in the fragement shader anyway. You can use the interpolated, normalized vector but in general it doesn't have unit length.

This should do it.

tksuoran

12-09-2011, 04:00 AM

Also, normalizing in the vertex shader is wasted since for correct results you'll have to re-normalize the interpolated vector in the fragement shader anyway. You can use the interpolated, normalized vector but in general it doesn't have unit length.
Just to be clear - it is not only wasted, it is plain wrong, as interpolated directions will be wrong.

Vexator

12-09-2011, 04:33 AM

Thank you for your quick answer!
I adopted your suggestions, however, the problem remains.

Model and camera are positioned at the origin here, and no rotation is applied to the model:
http://img850.imageshack.us/img850/6142/shading.jpg

This is redundant. Since it's probably the case that mat3(transform.modelView) is orthogonal it is always true that (M^-1)^T = (M^-1)^-1 = M.

Vexator

12-09-2011, 05:03 AM

if the input values are correct the light vector shouldn't be any different.

this does indeed result in different shading!

This is redundant. Since it's probably the case that mat3(transform.modelView) is orthogonal it is always true that (M^-1)^T = (M^-1)^-1 = M.

but isn't that how one is supposed to calculate the normal matrix? also, shouldn't this result in the same shading compared with when I'm using the matrix computed on the CPU?

tksuoran

12-09-2011, 05:52 AM

You rarely need to use a separate normal matrix. If you only have translation, rotation and uniform scale, you can use the modelview matrix also for normal transform. Inverse transpose of these kind of matrices is the matrix itself. Only if you add non-uniform scaling or something more interesting, you would need separate inverse transpose.

Yeah, I'm aware of that. But I might apply non-uniform scaling to my model matrices at a later point, so that's why I'm doing it like that. Either way, I would like to understand why the normal matrix I compute on the CPU using GLM does not work but the matrix computed in the vertex shader does.
Also, I still haven't figured out why the shading "flips around" when I move the camera close to the origin.

Vexator

12-11-2011, 03:31 AM

Well turns out I had a bug in my uniform buffer update code.. the 3x3 normal matrix was uploaded as if it was a 4x4 matrix, which caused the other matrices to be corrupted during the update.
Thank you for helping me fixing the shading, though :)