I'm trying to implement shadow mapping with cube maps. I believe I've done the first stage by filling the cube map, but for the final shading pass I am unsure how to exactly calculate the cubemap coordinates. Any pointers?


2 Answers 2


To use a cube map for shadows you need to recreate the world position of the pixel you are rendering and from that get the normal that points at that world position from the light source, now you can calculate the distance of your pixel from the light source as well as check the cube map for the distance to the first shadow casting object.

Vertex shader needs to send along the z and w components of the vertices so you can perform the unprojection in the fragment shader

output.position = mul(input.position, worldViewProjMatrix);
output.depth.xy = output.position.zw;

Then you can combine these values with the inverse projection matrix in the pixel shader to recreate the world position of each pixel:

float4 projectedPos = float3(input.position.xy, input.depth.x / input.depth.y, 1);
float4 worldPos = mul(projectedPos, invProjectionMatrix); 
float4 lightDir = worldPos - lightPos

float4 normal = normalize(light2pixel);
float dist = distance(worldPos, lightPos);

float shadow = texture(shadowCubeMap, normal);

if (shadow > dist)
  // Perform lighting...

I haven't tested this code so there may be bugs and errors in it, but it should be enough to demonstrate the idea I hope (the unprojection of the pixel coordinate is identical to other type of shadow maps, the main difference is using the normal with the cube map instead of calculating the texture coordinate with projection).


Cube maps are using direction as their texture coordinates. A quote from the OpenGL wiki:

The texture coordinates for cubemaps are 3D vector directions. These are conceptually directions from within the cube defined by the cubemap, pointing in a particular direction. The vectors do not have to be normalized.

You have to calculate the direction of the fragment is pointing at to get a cube map direction. You could use the vertex normals for that, or some other mechanism. In your case of shadows, I'm guessing (I've not actually done that yet myself, I am a learner too) you could get the direction from the camera to the fragment to get the correct depth texel, or at least something similar to that.

Read a tutorial about cube maps.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .