I am building a 3d renderer in C++ using Vulkan.hpp. The renderer uses deferred shading. I have a shadow cube map that I am generating using the depth output of a version of the geometry shader that renders each direction to a face of the cube map. I then want to feed this cube map back into the geometry shader of the main render pass to generate a shadow map. I have tried taking a similar approach to the point shadow mapping I am using, but I'm running into issues converting the depth buffer cube map input to world coordinates to do the comparison to generate the shadows. For the time-being I am hard coding the position of this cube map and the far plane in the shader. The issue I am facing is that the shadow texture input is between 0 and 1, but the values are very high and much closer to 1. The world coordinates are correct. When I try to convert the depth values they don't come out correctly. I don't believe I am linearizing the values properly. Can you look at the portion of the shader code I am using below and work out what's going wrong?
Thanks for sharing your shadow sampling code snippet!
Your approach looks very similar to standard point light shadow cube maps sampling, which is good, but you're hitting a common difficulty: properly interpreting the depth values stored in the cube map texture and comparing them to the current fragment's depth with respect to the light.
Let's break down what's likely going wrong and how to fix it.
What your code currently does:
You calculate currentDepth as the distance from the fragment to the light position.
You sample the cube map using direction vector fragToLight + sampleOffsetDirections[i] * diskRadius.
You get closestDepth from the cube map sample's red channel.
You multiply closestDepth by far_plane to get the linear depth.
Then compare the depth value with a bias for shadow.
Why the values might seem wrong:
Depth range in cube maps uses normalized distance, but how?
For point light shadow mapping with cube maps, the usual approach is:
When rendering the depth to the cube map from the light, you output the distance from the light to the fragment divided byfar_plane so that your depth value is between 0 and 1.
Then when sampling, you multiply by far_plane to get back linear depth.
This works only if the cube map is storing linear depth in [0,1], i.e., distance / far_plane.
Are you actually writing linear depth during shadow cube map generation?
If your cube map stores raw depth buffer values (like standard non-linear depth values after perspective projection), or depth values that are not linearized or normalized, your sampling will produce wrong results.
For shadow cube maps from point lights, you should render depth as linear distance, i.e., output the actual distance divided by the far_plane (thus in [0,1]) when rendering the shadow map—simplest done in the fragment shader that outputs to the cube map.
Are you sampling at the exact direction vector?
You should sample the cube map with the vector from light to fragment (normalized or unnormalized?), typically just the vector from light to fragment normalized.
But since your depth is storing distance, sampling with a normalized direction returns the depth value at that direction, which corresponds to distance.
Your offsets might skew the vector so that the direction is no longer normalized, which might cause incorrect lookups.
Cube map sampler with linear filtering
If your sampleOffsetDirections produce a (non-integer texcoord) offset, and filtering is enabled, it can smooth over edges or cause imprecise results.
Things to verify / fix:
1. Verify you output linear depth in [0,1] during shadow cube map generation.
Make sure your shadow map generation shader calculates:
glsl
float lightDepth = length(worldPos - lightPos) / far_plane;
gl_FragDepth = lightDepth; // or output to a color attachment
and stores that in the cube map.
2. Normalize the sampling vector for texture lookup
Ensure your depth cube map stores linear distance divided by far_plane (0..1).
Use normalized direction vector in cube map sampling.
Multiply sampled depth by far_plane to get real distance.
Compare with actual distance, minus a bias to avoid self-shadow.
If after this your shadows are still off, I'd recommend outputting intermediate values (e.g., sampled depth and currentDepth) to debug visually or via push constants uniform, just to see ranges and confirm your math.
Let me know if that helps or if you want me to look at your shadow map generation shader code too!