0

I noticed that in some vertex shaders they invert the Z coordinate of the light vector, i.e.

// Compute denormalized light vector in world space
float3 vLightWS = g_LightPosition.xyz - vPositionWS.xyz;
// Need to invert Z for correct lighting
vLightWS.z = -vLightWS.z;

Shouldn't be that the light vector starts at the light's position and ends at the vertex position, but in order to use it in the diffuse lighting calculation it is reverted to start at the vertex position and end at the light's position as float3 vLightWS = g_LightPosition.xyz - vPostionWS.xyz suggests?

In this case what is the meaning of the Z coordinate inversion?

Pekov
  • 157
  • 6
  • 2
    When doing lighting calculations you sometimes have to reverse the entire light direction due to needing the vector to the light instead of from the light. It looks like that might be what's trying to happen here, but if so it only works when the light vector only has a z component, and 0 for x and y. It also could be a coordinate fix up operation. Like some data assumes z positive is forward, but in reality its z negative. In either case it looks like invalid BS honestly. – Alan Wolfe Jun 27 '15 at 02:13
  • 1
    I think it is more likely to be coordinate fix up operation where the world coordinate system is right-handed with +z points toward us but the clip space is left-handed with +z points away from us, though I am not sure how to prove that for the code I study. – Pekov Jun 27 '15 at 06:42

2 Answers2

1

Z inversion simply means that the data somewhere is wrong. It is completely unnecessary in lighting calculations and can only do good when all vertex/light positions or normals happen to be scaled by the vector (1,1,-1) somehow.

snake5
  • 3,598
  • 1
  • 15
  • 28
  • 1
    Pekov mentioned that it could be changing handedness. That seems fairly reasonable :p – Alan Wolfe Jun 27 '15 at 14:10
  • 2
    @AlanWolfe Only if you do it incorrectly. Handedness change would have to be applied to both positions and normals, in which case they preserve their relationships. Same thing goes for every other rotation/flip transform. – snake5 Jun 27 '15 at 14:59
  • Unfortunately it is not the handedness change of the coordinate system since the Z coordinate of all other world space quantities remains unchanged, unless if it is that only the light position is specified in right-handed CS (less likely). This code I study is actually that of the DirectX sample DetailTessellation11. – Pekov Jun 27 '15 at 18:39
-4

It simply inverts the color rendered by your shader

Inverting Colors

Fatty Mieo
  • 77
  • 7