I am building a 3d renderer in C++ using Vulkan.hpp. The renderer uses deferred shading.…
- User
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?
vec3 sampleOffsetDirections[20] = vec3[] ( vec3( 1, 1, 1), vec3(1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1), vec3( 1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1), vec3( 1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0), vec3( 1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1), vec3( 0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1) );
float RadiantShadowCalculation(vec3 fragPos) { vec3 lightPos = vec3(0.0, -8.0, 0.0); float far_plane = 100.0; vec3 fragToLight = fragPos - lightPos; float currentDepth = length(fragToLight); float shadow = 0.0; float bias = 0.15; int samples = 10; float diskRadius = 0.05;
for(int i = 0; i < samples; ++i) { float closestDepth = texture(radiantShadowSampler, fragToLight + sampleOffsetDirections[i] * diskRadius).r; closestDepth *= far_plane; // undo mapping [0;1] if(currentDepth - bias > closestDepth) shadow += 1.0; } shadow /= float(samples);codereturn 1-shadow;}
- Assistant
- User
To give context to the shadow generation component, I am using a D32 format for the image as it is only the depth buffer being stored. The cube map is being rendered with a 90 degree FoV, to align with the face projections, and a near plane of 0.1 and a far plane of 100. The position of the cube map being generated is vec3(0,-8,0). Beyond this the cubemap is being generated in a fairly standard way.
- Assistant
- User
This looks better, but the values have the bounds of 0 to 2. The frag position that I am comparing to is in world space. Therefore there needs to be a way to convert to world space.
- Assistant
- User
Sorry I should have mentioned that the values are actually between 0.02 and 0.2. The range of values in the frag position are -16 to 1.4. The frag positions are clearly based on the world positions. However, the light positions don't seem to align to the world position.
- Assistant