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:
This assumes that the rotational part of modelView is orthogonal.
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.
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?
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.
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