Pass depth range to depth map visualization shader

Fixes #895
This commit is contained in:
Sascha Willems 2022-05-08 11:43:06 +02:00
parent 8e98d10f74
commit 8c376121c3
16 changed files with 46 additions and 16 deletions

View file

@ -6,10 +6,21 @@ layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outFragColor;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 view;
mat4 model;
mat4 lightSpace;
vec4 lightPos;
float zNear;
float zFar;
} ubo;
float LinearizeDepth(float depth)
{
float n = 1.0; // camera z near
float f = 128.0; // camera z far
float n = ubo.zNear;
float f = ubo.zFar;
float z = depth;
return (2.0 * n) / (f + n - z * (f - n));
}

View file

@ -11,7 +11,9 @@ layout (binding = 0) uniform UBO
mat4 view;
mat4 model;
mat4 lightSpace;
vec3 lightPos;
vec4 lightPos;
float zNear;
float zFar;
} ubo;
layout (location = 0) out vec3 outNormal;
@ -20,11 +22,6 @@ layout (location = 2) out vec3 outViewVec;
layout (location = 3) out vec3 outLightVec;
layout (location = 4) out vec4 outShadowCoord;
out gl_PerVertex
{
vec4 gl_Position;
};
const mat4 biasMat = mat4(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
@ -40,7 +37,7 @@ void main()
vec4 pos = ubo.model * vec4(inPos, 1.0);
outNormal = mat3(ubo.model) * inNormal;
outLightVec = normalize(ubo.lightPos - inPos);
outLightVec = normalize(ubo.lightPos.xyz - inPos);
outViewVec = -pos.xyz;
outShadowCoord = ( biasMat * ubo.lightSpace * ubo.model ) * vec4(inPos, 1.0);

View file

@ -3,10 +3,23 @@
Texture2D textureColor : register(t1);
SamplerState samplerColor : register(s1);
struct UBO
{
float4x4 projection;
float4x4 view;
float4x4 model;
float4x4 lightSpace;
float4 lightPos;
float zNear;
float zFar;
};
cbuffer ubo : register(b0) { UBO ubo; }
float LinearizeDepth(float depth)
{
float n = 1.0; // camera z near
float f = 128.0; // camera z far
float n = ubo.zNear;
float f = ubo.zFar;
float z = depth;
return (2.0 * n) / (f + n - z * (f - n));
}

View file

@ -14,7 +14,9 @@ struct UBO
float4x4 view;
float4x4 model;
float4x4 lightSpace;
float3 lightPos;
float4 lightPos;
float zNear;
float zFar;
};
cbuffer ubo : register(b0) { UBO ubo; }
@ -45,7 +47,7 @@ VSOutput main(VSInput input)
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
output.Normal = mul((float3x3)ubo.model, input.Normal);
output.LightVec = normalize(ubo.lightPos - input.Pos);
output.LightVec = normalize(ubo.lightPos.xyz - input.Pos);
output.ViewVec = -pos.xyz;
output.ShadowCoord = mul(biasMat, mul(ubo.lightSpace, mul(ubo.model, float4(input.Pos, 1.0))));