# How to transform a shadow map to camera view?

I'm making a rendering engine as a hobby to learn more about 3D. I have a deferred renderer with the G-buffer (color, normal and depth). I also have a lighting controller that uses only spotlights at the moment. For each light, I render the depth to a texture. This works nicely, but I can't wrap my head around how to map this depth map.

Basically what I would want is a view from the camera with the light's depth map mapped in the view. I've read a ton of tutorials but cannot seem to understand this process well enough.

(BTW, I'm aiming OpenGL 3.3, so no deprecated stuff in the answers. The web is full of those already :D)

-

I haven't worked in OpenGL but the process should be the same. Actually it goes the other way around: you map a pixel in the view to the depth map. I try to describe this in small steps:

• Pass the pixel position from the vertex shader to the fragment shader.
• Give the light's camera and projection to the fragment shader.
• Do what you usually do in the vertex shader: multiply the pixel position with the (light) camera and the (light) projection.

Now you have the pixel's position in the light's view. To get the uv coordinates:

• Convert to the light views clipping space coordinates (x=[-1, 1], y=[-1, 1], z=[0, 1]) by dividing xyz components by w. (z isn't really needed though)
• To change x and y to uv coordinates (u=[0, 1], v=[0, 1]), multiply by 0.5 and add 0.5. This will, for example, turn -0.5 into 0.25 which is exactly what you want.

Use these uv coordinates to get the depth from the depth map texture.

-