From e751ab465bbbf1365ec6e108c64dafd03ce42f1d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 23 Apr 2023 21:46:21 +0100 Subject: Pop and complete instanced shadow support --- game/scenary/foliage.cpp | 9 ++++++++- game/vehicles/railVehicleClass.cpp | 15 ++++++++++++++- gfx/gl/shaders/shadowDynamicPointInst.vs | 13 +++++++++++++ gfx/gl/shadowMapper.cpp | 7 +++++-- gfx/gl/shadowMapper.h | 4 ++-- 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 gfx/gl/shaders/shadowDynamicPointInst.vs diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 35be051..e54dfbd 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -34,6 +34,13 @@ Foliage::render(const SceneShader & shader) const } void -Foliage::shadows(const ShadowMapper &) const +Foliage::shadows(const ShadowMapper & mapper) const { + if (const auto count = instances.count()) { + mapper.dynamicPointInst.use(); + glBindVertexArray(instanceVAO); + glDrawElementsInstanced( + bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); + glBindVertexArray(0); + } } diff --git a/game/vehicles/railVehicleClass.cpp b/game/vehicles/railVehicleClass.cpp index 3e62bf1..66d7ba3 100644 --- a/game/vehicles/railVehicleClass.cpp +++ b/game/vehicles/railVehicleClass.cpp @@ -65,6 +65,19 @@ RailVehicleClass::render(const SceneShader & shader) const } void -RailVehicleClass::shadows(const ShadowMapper &) const +RailVehicleClass::shadows(const ShadowMapper & mapper) const { + if (const auto count = instancesBody.count()) { + mapper.dynamicPointInst.use(); + glBindVertexArray(instanceVAO); + glDrawElementsInstanced( + bodyMesh->type(), bodyMesh->count(), GL_UNSIGNED_INT, nullptr, static_cast(count)); + glBindVertexArray(instancesBogiesVAO.front()); + glDrawElementsInstanced(bogies.front()->type(), bogies.front()->count(), GL_UNSIGNED_INT, nullptr, + static_cast(instancesBogies.front().count())); + glBindVertexArray(instancesBogiesVAO.back()); + glDrawElementsInstanced(bogies.back()->type(), bogies.back()->count(), GL_UNSIGNED_INT, nullptr, + static_cast(instancesBogies.back().count())); + glBindVertexArray(0); + } } diff --git a/gfx/gl/shaders/shadowDynamicPointInst.vs b/gfx/gl/shaders/shadowDynamicPointInst.vs new file mode 100644 index 0000000..eb7313a --- /dev/null +++ b/gfx/gl/shaders/shadowDynamicPointInst.vs @@ -0,0 +1,13 @@ +#version 330 core + +include(`meshIn.glsl') +layout(location = 5) in mat4 model; + +uniform mat4 viewProjection; + +void +main() +{ + gl_Position = viewProjection * model * vec4(position, 1.0); + gl_Position.z = max(gl_Position.z, -1); +} diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 7649a54..9f1e420 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -2,6 +2,7 @@ #include "camera.h" #include "collections.hpp" #include "gfx/gl/shaders/vs-shadowDynamicPoint.h" +#include "gfx/gl/shaders/vs-shadowDynamicPointInst.h" #include "gfx/gl/shaders/vs-shadowFixedPoint.h" #include "location.hpp" #include "maths.h" @@ -14,7 +15,8 @@ #include #include -ShadowMapper::ShadowMapper(const glm::ivec2 & s) : size {s} +ShadowMapper::ShadowMapper(const glm::ivec2 & s) : + fixedPoint {shadowFixedPoint_vs}, dynamicPointInst {shadowDynamicPointInst_vs}, size {s} { glBindTexture(GL_TEXTURE_2D, depthMap); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); @@ -144,6 +146,7 @@ ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const C const auto lightViewProjection = lightProjection * lightView; fixedPoint.setViewProjection(lightViewProjection); dynamicPoint.setViewProjection(lightViewProjection); + dynamicPointInst.setViewProjection(lightViewProjection); const auto & viewport = viewports[bands][out.maps]; glViewport(size.x >> viewport.x, size.y >> viewport.y, size.x >> viewport.z, size.y >> viewport.w); @@ -157,7 +160,7 @@ ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const C return out; } -ShadowMapper::FixedPoint::FixedPoint() : Program {shadowFixedPoint_vs}, viewProjectionLoc {*this, "viewProjection"} { } +ShadowMapper::FixedPoint::FixedPoint(const Shader & vs) : Program {vs}, viewProjectionLoc {*this, "viewProjection"} { } void ShadowMapper::FixedPoint::setViewProjection(const glm::mat4 & viewProjection) const { diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index a5c2c7b..36371eb 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -22,7 +22,7 @@ public: class FixedPoint : public Program { public: - FixedPoint(); + FixedPoint(const Shader & vs); void setViewProjection(const glm::mat4 &) const; void use() const; @@ -40,7 +40,7 @@ public: RequiredUniformLocation viewProjectionLoc; RequiredUniformLocation modelLoc; }; - FixedPoint fixedPoint; + FixedPoint fixedPoint, dynamicPointInst; DynamicPoint dynamicPoint; // NOLINTNEXTLINE(hicpp-explicit-conversions) -- cgit v1.2.3