From 425add81fc8718f1ac4fde48a71344cd332d5a58 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Jul 2024 20:10:24 +0100 Subject: Add shadow shader which takes into account texture transparency --- gfx/gl/shaders/commonShadowPoint.glsl | 3 +++ gfx/gl/shaders/commonShadowPoint.gs | 3 +++ gfx/gl/shaders/shadowDynamicPointInstWithTextures.fs | 15 +++++++++++++++ gfx/gl/shaders/shadowDynamicPointInstWithTextures.gs | 3 +++ gfx/gl/shaders/shadowDynamicPointInstWithTextures.vs | 3 +++ gfx/gl/shadowMapper.cpp | 18 +++++++++++++++--- gfx/gl/shadowMapper.h | 2 ++ 7 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 gfx/gl/shaders/shadowDynamicPointInstWithTextures.fs create mode 100644 gfx/gl/shaders/shadowDynamicPointInstWithTextures.gs create mode 100644 gfx/gl/shaders/shadowDynamicPointInstWithTextures.vs (limited to 'gfx/gl') diff --git a/gfx/gl/shaders/commonShadowPoint.glsl b/gfx/gl/shaders/commonShadowPoint.glsl index ba4611a..c4ea827 100644 --- a/gfx/gl/shaders/commonShadowPoint.glsl +++ b/gfx/gl/shaders/commonShadowPoint.glsl @@ -1,8 +1,11 @@ out vec4 vworldPos; +ifdef(`TEXTURES', out vec2 vtexCoord;); + void main() { vec3 worldPos = model * position; vworldPos = vec4(worldPos - viewPoint + modelPos, 1); + ifdef(`TEXTURES', vtexCoord = texCoord;); } diff --git a/gfx/gl/shaders/commonShadowPoint.gs b/gfx/gl/shaders/commonShadowPoint.gs index b008f29..b99bd20 100644 --- a/gfx/gl/shaders/commonShadowPoint.gs +++ b/gfx/gl/shaders/commonShadowPoint.gs @@ -7,6 +7,8 @@ in vec4 vworldPos[]; layout(triangles) in; layout(triangle_strip, max_vertices = 12) out; +ifdef(`TEXTURES', in vec2 vtexCoord[]; out vec2 texCoord;); + void main() { @@ -15,6 +17,7 @@ main() gl_Position = viewProjection[vp] * vworldPos[v]; gl_Position.z = max(gl_Position.z, -1); gl_Layer = vp; + ifdef(`TEXTURES', texCoord = vtexCoord[v];); EmitVertex(); } EndPrimitive(); diff --git a/gfx/gl/shaders/shadowDynamicPointInstWithTextures.fs b/gfx/gl/shaders/shadowDynamicPointInstWithTextures.fs new file mode 100644 index 0000000..90519e3 --- /dev/null +++ b/gfx/gl/shaders/shadowDynamicPointInstWithTextures.fs @@ -0,0 +1,15 @@ +#version 330 core +#extension GL_ARB_shading_language_420pack : enable + +layout(binding = 3) uniform sampler2D texture0; + +in vec2 texCoord; + +void +main() +{ + if (texture(texture0, texCoord).a < 0.5) { + discard; + } + gl_FragDepth = gl_FragCoord.z; +} diff --git a/gfx/gl/shaders/shadowDynamicPointInstWithTextures.gs b/gfx/gl/shaders/shadowDynamicPointInstWithTextures.gs new file mode 100644 index 0000000..e6e213e --- /dev/null +++ b/gfx/gl/shaders/shadowDynamicPointInstWithTextures.gs @@ -0,0 +1,3 @@ +define(`TEXTURES', 1) + +include(`commonShadowPoint.gs') diff --git a/gfx/gl/shaders/shadowDynamicPointInstWithTextures.vs b/gfx/gl/shaders/shadowDynamicPointInstWithTextures.vs new file mode 100644 index 0000000..27ad9d7 --- /dev/null +++ b/gfx/gl/shaders/shadowDynamicPointInstWithTextures.vs @@ -0,0 +1,3 @@ +define(`TEXTURES', 1) + +include(`shadowDynamicPointInst.vs') diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 206e4a7..c537314 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -1,9 +1,12 @@ #include "shadowMapper.h" #include "camera.h" #include "collections.h" +#include "gfx/gl/shaders/fs-shadowDynamicPointInstWithTextures.h" #include "gfx/gl/shaders/gs-commonShadowPoint.h" +#include "gfx/gl/shaders/gs-shadowDynamicPointInstWithTextures.h" #include "gfx/gl/shaders/vs-shadowDynamicPoint.h" #include "gfx/gl/shaders/vs-shadowDynamicPointInst.h" +#include "gfx/gl/shaders/vs-shadowDynamicPointInstWithTextures.h" #include "gfx/gl/shaders/vs-shadowLandmass.h" #include "gl_traits.h" #include "location.h" @@ -14,11 +17,13 @@ #include #include #include -#include #include ShadowMapper::ShadowMapper(const TextureAbsCoord & s) : - landmess {shadowLandmass_vs}, dynamicPointInst {shadowDynamicPointInst_vs}, size {s} + landmess {shadowLandmass_vs}, dynamicPointInst {shadowDynamicPointInst_vs}, + dynamicPointInstWithTextures {shadowDynamicPointInstWithTextures_vs, shadowDynamicPointInstWithTextures_gs, + shadowDynamicPointInstWithTextures_fs}, + size {s} { glBindTexture(GL_TEXTURE_2D_ARRAY, depthMap); glTexImage3D( @@ -92,7 +97,8 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const return lightProjection * lightViewDir; }); - for (const auto p : std::initializer_list {&landmess, &dynamicPoint, &dynamicPointInst}) { + for (const auto p : std::initializer_list { + &landmess, &dynamicPoint, &dynamicPointInst, &dynamicPointInstWithTextures}) { p->setView(out, lightViewPoint); } scene.shadows(*this); @@ -108,6 +114,12 @@ ShadowMapper::ShadowProgram::ShadowProgram(const Shader & vs) : { } +ShadowMapper::ShadowProgram::ShadowProgram(const Shader & vs, const Shader & gs, const Shader & fs) : + Program {vs, gs, fs}, viewProjectionLoc {*this, "viewProjection"}, viewProjectionsLoc {*this, "viewProjections"}, + viewPointLoc {*this, "viewPoint"} +{ +} + void ShadowMapper::ShadowProgram::setView( const std::span viewProjection, const GlobalPosition3D viewPoint) const diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index dcf7e3f..967d93b 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -24,6 +24,7 @@ public: class ShadowProgram : public Program { public: explicit ShadowProgram(const Shader & vs); + explicit ShadowProgram(const Shader & vs, const Shader & gs, const Shader & fs); void setView(const std::span, const GlobalPosition3D) const; void use() const; @@ -51,6 +52,7 @@ public: }; FixedPoint landmess, dynamicPointInst; + ShadowProgram dynamicPointInstWithTextures; DynamicPoint dynamicPoint; // NOLINTNEXTLINE(hicpp-explicit-conversions) -- cgit v1.2.3