0

I'm trying to implement shadow maps for point lights using Webgl2. To do so, I use a cubemap of depth buffers to which I render 6 axis aligned views from the light position.

In the fragment shader I use to render lighting, I query this cubemap using these lines:

vec3 lightToPos = lightPos-fragPos;
float closestDepth = texture(shadowMap, lightToPos).r;

Note that lightPos and fragPos are in 'view' referential, that is: viewMat * worldMat * objPos. In the tutorial I'm following this is supposed to be in World Position (http://ogldev.atspace.co.uk/www/tutorial43/tutorial43.html). Now, I don't see a reason why my way of doing it wouldn't work because the difference of two positions should be the same regardless of the point of view their coordinates are expressed in, right? The results I'm getting are not what I'm expecting, everything is in the shadow. I have probably made a mistake elsewhere but I would like to be sure I understood how that works and that the mistake is not from here.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
François Guthmann
  • 461
  • 1
  • 4
  • 15
  • 1
    "*the difference of two positions should be the same regardless of the point of view their coordinates are expressed in, right?*" In what respect should they "be the same"? If you have two world-space positions and you subtract them, you get a direction. But that direction is in *world-space*. Coordinate systems are like units; they don't go away just because you subtract them. 6cm - 2cm is not 4inches. – Nicol Bolas Aug 04 '17 at 00:17
  • It should "be the same" in respect to what is relevant to cubemap querying. From what I understand, cubemap cares only for direction and not vector size. From that, and since translations and rotations are isometric, getting the lightToPos vector in world coordinates or view coordinates doesn't change a thing. (I don't scale, but even homogeneous scaling shouldn't be a problem) – François Guthmann Aug 04 '17 at 01:32
  • Before even thinking about the math aspect, have you: checked that your direction calculation returns the values you expect it to, checked that your shadow map is correctly generated? – LJᛃ Aug 04 '17 at 02:02
  • Direction calculation is used in PBR shading and seems to be ok. Also, I'm having some issue with checking the content of the shadow map but it's definitely on my todo list! (I steel want to understand the math though) – François Guthmann Aug 04 '17 at 09:20

1 Answers1

2

the difference of two positions should be the same regardless of the point of view their coordinates are expressed in, right?

No, they're not.

Consider a world where the light is straight above the origin of the world, and "above" means in the +Y direction. So that's the direction the shadow map was generated from. And it was generated from world-space.

Now, consider the camera at the origin of the world. It's looking down the -Z axis, with +Y in pointing straight up. In this case, there is no orientation difference between camera space and world space. Therefore, in both cases, the light direction is (0, 1, 0).

But what happens if you rotate the camera 90 degrees around its viewing axis? Well, the camera is lying on its side, relative to the world. That means the light direction is either (1, 0, 0) or (-1, 0, 0), depending on which way you rotated. And that direction is different from the world space direction, which is still (0, 1, 0).

Yes, the light did not move, relative to the world. But the camera did. Therefore, the value of the camera-space light direction is different from what its value would be for the world-space light direction.

If both the light position and the fragment position truly are in camera space, then subtracting them will give the direction to the light in camera space. If the shadow map was indeed built in world space, this direction will be utterly useless.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Han, I guess in my head I was picturing objects translating / rotating when it's an entire coordinate system that does. Thank you for your answer, I'll need to think a bit on it before marking the question as solved but I definitely will. – François Guthmann Aug 04 '17 at 09:31