From 900d19a41bc1886e7a809d99d6119b12235a4f0a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jul 2024 10:57:17 +0100 Subject: Initial commit of basic shadow depth map creation --- game/scenary/foliage.cpp | 3 +++ game/scenary/foliage.h | 2 ++ 2 files changed, 5 insertions(+) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 73d285f..870adb0 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -17,6 +17,9 @@ Foliage::postLoad() texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); + ShadowStenciller ss; + ss.renderStencil(shadowStencil, *bodyMesh, {-4000, -4000, 0}, {4000, 4000, 8000}); + Texture::saveDepth(shadowStencil, std::format("/tmp/stencil-{}.tga", id).c_str()); } void diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h index 0a4261c..5367d44 100644 --- a/game/scenary/foliage.h +++ b/game/scenary/foliage.h @@ -2,6 +2,7 @@ #include "assetFactory/asset.h" #include "gfx/gl/instanceVertices.h" +#include "gfx/gl/shadowStenciller.h" #include "gfx/models/texture.h" #include "gfx/renderable.h" @@ -19,6 +20,7 @@ public: mutable InstanceVertices instances; void render(const SceneShader &) const override; void shadows(const ShadowMapper &) const override; + glTexture shadowStencil = ShadowStenciller::createStencilTexture(256, 256); protected: friend Persistence::SelectionPtrBase>; -- cgit v1.2.3 From 4ba67f34eac848e43bad2ccc4b5c09ac65dd7952 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jul 2024 12:55:15 +0100 Subject: Use mesh extents for shadow stencil extents --- game/scenary/foliage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 870adb0..24caeb7 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -18,7 +18,7 @@ Foliage::postLoad() bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); ShadowStenciller ss; - ss.renderStencil(shadowStencil, *bodyMesh, {-4000, -4000, 0}, {4000, 4000, 8000}); + ss.renderStencil(shadowStencil, *bodyMesh); Texture::saveDepth(shadowStencil, std::format("/tmp/stencil-{}.tga", id).c_str()); } -- cgit v1.2.3 From f737aada2b7164683303beb3bb490c30a4949fb0 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jul 2024 02:33:24 +0100 Subject: Use texture alpha in shadow stencil --- game/scenary/foliage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 24caeb7..ba1b37e 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -18,7 +18,7 @@ Foliage::postLoad() bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); ShadowStenciller ss; - ss.renderStencil(shadowStencil, *bodyMesh); + ss.renderStencil(shadowStencil, *bodyMesh, texture); Texture::saveDepth(shadowStencil, std::format("/tmp/stencil-{}.tga", id).c_str()); } -- cgit v1.2.3 From 172beac34e82c86f8c16b8a1be5fca9d7ccfc0d1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 18 Aug 2024 16:14:29 +0100 Subject: Update asset stencils from shadow mapper --- game/scenary/foliage.cpp | 13 +++++++++---- game/scenary/foliage.h | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index ba1b37e..13d2f3b 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -3,7 +3,6 @@ #include "gfx/gl/shadowMapper.h" #include "gfx/gl/vertexArrayObject.h" #include "gfx/models/texture.h" -#include "location.h" bool Foliage::persist(Persistence::PersistenceStore & store) @@ -17,9 +16,15 @@ Foliage::postLoad() texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); - ShadowStenciller ss; - ss.renderStencil(shadowStencil, *bodyMesh, texture); - Texture::saveDepth(shadowStencil, std::format("/tmp/stencil-{}.tga", id).c_str()); +} + +void +Foliage::updateStencil(const ShadowStenciller & ss) const +{ + if (instances.size() > 0) { + ss.renderStencil(shadowStencil, *bodyMesh, texture); + Texture::saveDepth(shadowStencil, std::format("/tmp/stencil-{}.tga", id).c_str()); + } } void diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h index 5367d44..fa6c63b 100644 --- a/game/scenary/foliage.h +++ b/game/scenary/foliage.h @@ -20,6 +20,7 @@ public: mutable InstanceVertices instances; void render(const SceneShader &) const override; void shadows(const ShadowMapper &) const override; + void updateStencil(const ShadowStenciller &) const override; glTexture shadowStencil = ShadowStenciller::createStencilTexture(256, 256); protected: -- cgit v1.2.3 From 362c6952742eb01e32fdeb2ec26a2fe463e5001c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 26 Aug 2024 13:47:22 +0100 Subject: Add a foliage vertex array for just the location data --- game/scenary/foliage.cpp | 2 ++ game/scenary/foliage.h | 1 + 2 files changed, 3 insertions(+) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 13d2f3b..1c03217 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -16,6 +16,8 @@ Foliage::postLoad() texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); + VertexArrayObject {instancePointVAO}.addAttribs( + instances.bufferName()); } void diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h index fa6c63b..824a644 100644 --- a/game/scenary/foliage.h +++ b/game/scenary/foliage.h @@ -14,6 +14,7 @@ class Foliage : public Asset, public Renderable, public StdTypeDefs { Mesh::Ptr bodyMesh; Texture::Ptr texture; glVertexArray instanceVAO; + glVertexArray instancePointVAO; public: using LocationVertex = std::pair; -- cgit v1.2.3 From 75905c01b13fd7a1d58494b8aaacb3b4b3cccdaa Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 28 Aug 2024 03:48:43 +0100 Subject: Use billboard shadows for foliage --- game/scenary/foliage.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 1c03217..af67ea6 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -45,10 +45,12 @@ void Foliage::shadows(const ShadowMapper & mapper) const { if (const auto count = instances.size()) { - mapper.dynamicPointInstWithTextures.use(); - if (texture) { - texture->bind(GL_TEXTURE3); - } - bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); + const auto dimensions = bodyMesh->getDimensions(); + mapper.stencilShadowProgram.use(dimensions.centre, dimensions.size); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, shadowStencil); + glBindVertexArray(instancePointVAO); + glDrawArrays(GL_POINTS, 0, static_cast(count)); + glBindVertexArray(0); } } -- cgit v1.2.3 From 052b2b49d9bfdaaa7f7c5603fa7c47b881df93c2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 2 Sep 2024 21:00:41 +0100 Subject: Update stencil texture to 2d array --- game/scenary/foliage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index af67ea6..1e7424e 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -48,7 +48,7 @@ Foliage::shadows(const ShadowMapper & mapper) const const auto dimensions = bodyMesh->getDimensions(); mapper.stencilShadowProgram.use(dimensions.centre, dimensions.size); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, shadowStencil); + glBindTexture(GL_TEXTURE_2D_ARRAY, shadowStencil); glBindVertexArray(instancePointVAO); glDrawArrays(GL_POINTS, 0, static_cast(count)); glBindVertexArray(0); -- cgit v1.2.3 From 10998a8302b3d7651b4afc046311961eb2dea2c8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 6 Oct 2024 12:48:40 +0100 Subject: Use LightDirection for calculating/passing all light dir components --- game/environment.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'game') diff --git a/game/environment.cpp b/game/environment.cpp index 19aad84..5aa1f09 100644 --- a/game/environment.cpp +++ b/game/environment.cpp @@ -1,4 +1,5 @@ #include "environment.h" +#include "gfx/lightDirection.h" #include #include @@ -16,14 +17,12 @@ Environment::render(const SceneRenderer & renderer, const SceneProvider & scene) constexpr RGB baseAmbient {0.1F}, baseDirectional {0.0F}; constexpr RGB relativeAmbient {0.3F, 0.3F, 0.4F}, relativeDirectional {0.6F, 0.6F, 0.5F}; - const auto sunPos = getSunPos({}, worldTime); - const auto sunDir = (glm::mat3 {rotate_yp({sunPos.y + pi, sunPos.x})} * north); - const auto vertical = -std::min(0.F, sunDir.z - 0.1F); - const auto ambient = baseAmbient + relativeAmbient * vertical; - const auto directional = baseDirectional + relativeDirectional * vertical; + const LightDirection sunPos = getSunPos({}, worldTime); + const auto ambient = baseAmbient + relativeAmbient * sunPos.vertical(); + const auto directional = baseDirectional + relativeDirectional * sunPos.vertical(); renderer.setAmbientLight(ambient); - renderer.setDirectionalLight(directional, sunDir, scene); + renderer.setDirectionalLight(directional, sunPos, scene); } // Based on the C++ code published at https://www.psa.es/sdg/sunpos.htm -- cgit v1.2.3 From 439630ae86b01b714574335d4dd2e583f724dba3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 10 Oct 2024 00:59:41 +0100 Subject: Include foliage yaw rotation in vertex, pass through instead of whole model rotation matrix --- game/scenary/foliage.cpp | 5 +++-- game/scenary/foliage.h | 7 ++++++- game/scenary/plant.cpp | 3 ++- 3 files changed, 11 insertions(+), 4 deletions(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index 1e7424e..c90d5be 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -15,8 +15,9 @@ Foliage::postLoad() { texture = getTexture(); bodyMesh->configureVAO(instanceVAO) - .addAttribs(instances.bufferName(), 1); - VertexArrayObject {instancePointVAO}.addAttribs( + .addAttribs( + instances.bufferName(), 1); + VertexArrayObject {instancePointVAO}.addAttribs( instances.bufferName()); } diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h index 824a644..5da63f0 100644 --- a/game/scenary/foliage.h +++ b/game/scenary/foliage.h @@ -17,7 +17,12 @@ class Foliage : public Asset, public Renderable, public StdTypeDefs { glVertexArray instancePointVAO; public: - using LocationVertex = std::pair; + struct LocationVertex { + glm::mat3 rotation; + float yaw; + GlobalPosition3D position; + }; + mutable InstanceVertices instances; void render(const SceneShader &) const override; void shadows(const ShadowMapper &) const override; diff --git a/game/scenary/plant.cpp b/game/scenary/plant.cpp index b39c28b..2006225 100644 --- a/game/scenary/plant.cpp +++ b/game/scenary/plant.cpp @@ -2,6 +2,7 @@ #include "location.h" Plant::Plant(std::shared_ptr type, const Location & position) : - type {std::move(type)}, location {this->type->instances.acquire(position.getRotationTransform(), position.pos)} + type {std::move(type)}, + location {this->type->instances.acquire(position.getRotationTransform(), position.rot.y, position.pos)} { } -- cgit v1.2.3 From a5903c351d9219524cf6440d299e57ac66e7ae35 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 10 Oct 2024 01:13:22 +0100 Subject: Don't dump stencil texture --- game/scenary/foliage.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'game') diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp index c90d5be..a0ec576 100644 --- a/game/scenary/foliage.cpp +++ b/game/scenary/foliage.cpp @@ -2,7 +2,6 @@ #include "gfx/gl/sceneShader.h" #include "gfx/gl/shadowMapper.h" #include "gfx/gl/vertexArrayObject.h" -#include "gfx/models/texture.h" bool Foliage::persist(Persistence::PersistenceStore & store) @@ -26,7 +25,6 @@ Foliage::updateStencil(const ShadowStenciller & ss) const { if (instances.size() > 0) { ss.renderStencil(shadowStencil, *bodyMesh, texture); - Texture::saveDepth(shadowStencil, std::format("/tmp/stencil-{}.tga", id).c_str()); } } -- cgit v1.2.3 From 5b6a6f3b241fea6d19521ddbb705e27d5e4c0268 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 20 Oct 2024 16:24:14 +0100 Subject: Move lots of maths helpers to inline, constexpr, templates Always for working with different dimensions/types --- game/network/link.cpp | 2 +- game/network/network.cpp | 2 +- game/network/rail.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'game') diff --git a/game/network/link.cpp b/game/network/link.cpp index 248fe7d..745896c 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -51,7 +51,7 @@ LinkCurve::positionAt(float dist, unsigned char start) const + RelativePosition3D {0, 0, static_cast(es.first->pos.z - centreBase.z) + (static_cast(es.second->pos.z - es.first->pos.z) * frac)}}; - const auto pitch {vector_pitch({0, 0, static_cast(es.second->pos.z - es.first->pos.z) / length})}; + const auto pitch {vector_pitch(difference(es.second->pos, es.first->pos) / length)}; return Location {GlobalPosition3D(relPos + relClimb) + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}}; } diff --git a/game/network/network.cpp b/game/network/network.cpp index 65b2a62..fa86cb5 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -95,7 +95,7 @@ Network::routeFromTo(const Link::End & end, const Node::Ptr & dest) const GenCurveDef Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir) { - const auto diff {end - start}; + const auto diff = difference(end, start); const auto vy {vector_yaw(diff)}; const auto dir = pi + startDir; const auto flatStart {start.xy()}, flatEnd {end.xy()}; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index fd07ace..c29217a 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -32,7 +32,7 @@ RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end) } // Find start link/end - opposite entry dir to existing link; so pi +... const Angle dir = pi + findNodeDirection(node1ins.first); - if (dir == vector_yaw(end - start)) { + if (dir == vector_yaw(difference(end, start))) { return addLink(start, end); } const auto flatStart {start.xy()}, flatEnd {end.xy()}; @@ -62,7 +62,7 @@ RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end) return addLink(midh, end, c2); } } - const auto diff {end - start}; + const auto diff = difference(end, start); const auto vy {vector_yaw(diff)}; const auto n2ed {(vy * 2) - dir - pi}; const auto centre {find_arc_centre(flatStart, dir, flatEnd, n2ed)}; -- cgit v1.2.3 From 56100b1c4cb02db7608763dddd77f8052a533dae Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 21 Oct 2024 18:30:16 +0100 Subject: Further template maths functions --- game/geoData.cpp | 2 +- game/network/link.cpp | 4 ++-- game/network/network.cpp | 8 ++++---- game/network/rail.cpp | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) (limited to 'game') diff --git a/game/geoData.cpp b/game/geoData.cpp index 72aa056..d212651 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -559,7 +559,7 @@ GeoData::setHeights(const std::span triangleStrip, const const auto limit = std::ceil(arc.length() * 5.F / pi); const auto inc = arc.length() / limit; for (float step = 0; step <= limit; step += 1.F) { - addExtrusionFor(sincosf(arc.first + (step * inc))); + addExtrusionFor(sincos(arc.first + (step * inc))); } } else { diff --git a/game/network/link.cpp b/game/network/link.cpp index 745896c..79af92a 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -46,7 +46,7 @@ LinkCurve::positionAt(float dist, unsigned char start) const const auto es {std::make_pair(ends[start].node.get(), ends[1 - start].node.get())}; const auto as {std::make_pair(arc[start], arc[1 - start])}; const auto ang {as.first + ((as.second - as.first) * frac)}; - const auto relPos {(sincosf(ang) || 0.F) * radius}; + const auto relPos {(sincos(ang) || 0.F) * radius}; const auto relClimb {vehiclePositionOffset() + RelativePosition3D {0, 0, static_cast(es.first->pos.z - centreBase.z) @@ -69,7 +69,7 @@ LinkCurve::intersectRay(const Ray & ray) const points.reserve(segCount); for (std::remove_const_t swing = {arc.first, centreBase.z - e0p.z}; segCount; swing += step, --segCount) { - points.emplace_back(centreBase + ((sincosf(swing.x) * radius) || swing.y)); + points.emplace_back(centreBase + ((sincos(swing.x) * radius) || swing.y)); } return ray.passesCloseToEdges(points, 1.F); } diff --git a/game/network/network.cpp b/game/network/network.cpp index fa86cb5..6ba3ed6 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -121,16 +121,16 @@ Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & en }; if (const auto radii = find_arcs_radius(flatStart, startDir, flatEnd, endDir); radii.first < radii.second) { const auto radius {radii.first}; - const auto c1 = flatStart + (sincosf(startDir + half_pi) * radius); - const auto c2 = flatEnd + (sincosf(endDir + half_pi) * radius); + const auto c1 = flatStart + (sincos(startDir + half_pi) * radius); + const auto c2 = flatEnd + (sincos(endDir + half_pi) * radius); const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); return {{start, midh, c1}, {end, midh, c2}}; } else { const auto radius {radii.second}; - const auto c1 = flatStart + (sincosf(startDir - half_pi) * radius); - const auto c2 = flatEnd + (sincosf(endDir - half_pi) * radius); + const auto c1 = flatStart + (sincos(startDir - half_pi) * radius); + const auto c2 = flatEnd + (sincos(endDir - half_pi) * radius); const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); return {{midh, start, c1}, {midh, end, c2}}; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index c29217a..6f04070 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -45,8 +45,8 @@ RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end) const float dir2 = pi + findNodeDirection(node2ins.first); if (const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); radii.first < radii.second) { const auto radius {radii.first}; - const auto c1 = flatStart + (sincosf(dir + half_pi) * radius); - const auto c2 = flatEnd + (sincosf(dir2 + half_pi) * radius); + const auto c1 = flatStart + (sincos(dir + half_pi) * radius); + const auto c2 = flatEnd + (sincos(dir2 + half_pi) * radius); const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); addLink(start, midh, c1); @@ -54,8 +54,8 @@ RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end) } else { const auto radius {radii.second}; - const auto c1 = flatStart + (sincosf(dir - half_pi) * radius); - const auto c2 = flatEnd + (sincosf(dir2 - half_pi) * radius); + const auto c1 = flatStart + (sincos(dir - half_pi) * radius); + const auto c2 = flatEnd + (sincos(dir2 - half_pi) * radius); const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); addLink(midh, start, c1); -- cgit v1.2.3