From f9018c987884055e62aca1184fe0d78843b3156d Mon Sep 17 00:00:00 2001 From: j3oss Date: Wed, 21 Jul 2021 18:49:26 +0200 Subject: [PATCH] added shadowmap renderer --- assets/shaders/shadowmap.vert | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 assets/shaders/shadowmap.vert diff --git a/assets/shaders/shadowmap.vert b/assets/shaders/shadowmap.vert new file mode 100644 index 0000000..294f46c --- /dev/null +++ b/assets/shaders/shadowmap.vert @@ -0,0 +1,55 @@ +#version 450 +#extension GL_EXT_nonuniform_qualifier : require + +struct Vertex { + vec4 position; + vec4 normal; + vec2 uv[2]; + vec4 tangent; + vec4 bitangent; // No longer needed. // TODO remove +}; + +struct Light { + mat4 tranform; + vec4 color; + float intensity; +}; + +layout( push_constant ) uniform constants +{ + mat4 transform; + uint indexBufferIndex; + uint vertexBufferIndex; +} PushConstants; + +layout(set = 0, binding = 1) buffer VertexBuffer { + layout(align = 16) Vertex vertices[]; +} VertexBuffers[]; + +layout(set = 0, binding = 2) buffer IndexBuffer { + uint indices[]; +} IndexBuffers[]; + +const int MAX_LIGHT_COUNT = 128; + +layout(set = 1, binding = 0) uniform LightBuffer { + layout(align = 16) Light lights[MAX_LIGHT_COUNT]; +} LightsBuffer; + +float near = 10; +float far = 0.1; + +void main() +{ + uint index = IndexBuffers[ PushConstants.indexBufferIndex ].indices[gl_VertexIndex]; + Vertex vertex = VertexBuffers[ PushConstants.vertexBufferIndex ].vertices[ index ]; + + mat4 lightView = transpose(inverse( LightsBuffer.lights[0].tranform)); + mat4 lightProjection = mat4(1.0); + lightProjection[3][2] = -(far+near)/(far-near); + lightProjection[2][2] = -2/(far-near); + + mat4 lightSpaceMatrix = lightProjection * lightView; + + gl_Position = lightSpaceMatrix * PushConstants.transform * vec4(vertex.position.xyz, 1.0); +}