From 51eb25ea0f1373ca0442b02049406af38eae3b33 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 12 Jan 2024 19:35:58 +0000 Subject: Add model support for point lights Still invokes non-instanced point light shader --- game/scenary/illuminator.cpp | 20 ++++++++++++++++++++ game/scenary/illuminator.h | 17 ++++++++++++++++- gfx/gl/sceneShader.cpp | 27 ++++----------------------- gfx/gl/sceneShader.h | 17 +---------------- test/test-assetFactory.cpp | 1 - test/test-render.cpp | 30 ------------------------------ 6 files changed, 41 insertions(+), 71 deletions(-) diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 6f51506..3de4e52 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -11,10 +11,17 @@ Illuminator::SpotLight::persist(Persistence::PersistenceStore & store) && STORE_MEMBER(arc); } +bool +Illuminator::PointLight::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(position) && STORE_MEMBER(colour) && STORE_MEMBER(kq); +} + bool Illuminator::persist(Persistence::PersistenceStore & store) { return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct) + && STORE_HELPER(pointLight, Persistence::Appender) && STORE_HELPER(spotLight, Persistence::Appender) && Asset::persist(store); } @@ -32,6 +39,14 @@ Illuminator::postLoad() std::transform(spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { return instancesSpotLight.acquire(*s); }); + VertexArrayObject {instancesPointLightVAO} + .addAttribs(instancesPointLight.bufferName(), 0) + .addAttribs(instances.bufferName(), 1); + std::transform( + pointLight.begin(), pointLight.end(), std::back_inserter(pointLightInstances), [this](const auto & s) { + return instancesPointLight.acquire(*s); + }); } void @@ -55,6 +70,11 @@ Illuminator::lights(const SceneShader & shader) const glBindVertexArray(instancesSpotLightVAO); glDrawArraysInstanced(GL_POINTS, 0, static_cast(scount), static_cast(count)); } + if (const auto pcount = instancesPointLight.size()) { + shader.pointLightInst.use(); + glBindVertexArray(instancesPointLightVAO); + glDrawArraysInstanced(GL_POINTS, 0, static_cast(pcount), static_cast(count)); + } glBindVertexArray(0); } diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index 99b87eb..a2b287d 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -11,7 +11,7 @@ class Texture; class Illuminator : public Asset, public Renderable, public StdTypeDefs { Mesh::Ptr bodyMesh; std::shared_ptr texture; - glVertexArray instanceVAO, instancesSpotLightVAO; + glVertexArray instanceVAO, instancesSpotLightVAO, instancesPointLightVAO; public: struct SpotLightVertex { @@ -22,16 +22,29 @@ public: Angle arc; }; + struct PointLightVertex { + RelativePosition3D position; + RGB colour; + RelativeDistance kq; + }; + struct SpotLight : Persistence::Persistable, SpotLightVertex, StdTypeDefs { private: friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; }; + struct PointLight : Persistence::Persistable, PointLightVertex, StdTypeDefs { + private: + friend Persistence::SelectionPtrBase>; + bool persist(Persistence::PersistenceStore & store) override; + }; + public: using LocationVertex = std::pair; mutable InstanceVertices instances; mutable InstanceVertices instancesSpotLight; + mutable InstanceVertices instancesPointLight; void render(const SceneShader &) const override; void lights(const SceneShader &) const override; @@ -41,5 +54,7 @@ protected: void postLoad() override; std::vector spotLight; + std::vector pointLight; std::vector::InstanceProxy> spotLightInstances; + std::vector::InstanceProxy> pointLightInstances; }; diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp index cdb8d2f..64bf171 100644 --- a/gfx/gl/sceneShader.cpp +++ b/gfx/gl/sceneShader.cpp @@ -1,5 +1,4 @@ #include "sceneShader.h" -#include #include #include #include @@ -21,7 +20,8 @@ SceneShader::SceneShader() : basicInst {dynamicPointInst_vs, material_fs}, landmass {fixedPoint_vs, landmass_fs}, - absolute {fixedPoint_vs, material_fs}, spotLightInst {spotLight_vs, spotLight_gs, spotLight_fs} + absolute {fixedPoint_vs, material_fs}, spotLightInst {spotLight_vs, spotLight_gs, spotLight_fs}, + pointLightInst {pointLight_vs, pointLight_gs, pointLight_fs} { } @@ -29,7 +29,7 @@ void SceneShader::setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const { for (const auto & prog : std::initializer_list { - &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLightInst}) { + &basic, &basicInst, &water, &landmass, &absolute, &pointLightInst, &spotLightInst}) { prog->setViewProjection(viewPoint, viewProjection); } } @@ -38,7 +38,7 @@ void SceneShader::setViewPort(const ViewPort & viewPort) const { for (const auto & prog : std::initializer_list { - &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLightInst}) { + &basic, &basicInst, &water, &landmass, &absolute, &pointLightInst, &spotLightInst}) { prog->setViewPort(viewPort); } } @@ -87,22 +87,3 @@ SceneShader::WaterProgram::use(float waveCycle) const Program::use(); glUniform1f(waveLoc, waveCycle); } - -SceneShader::PointLightShader::PointLightShader() : - SceneProgram {pointLight_vs, pointLight_gs, pointLight_fs}, colourLoc {*this, "colour"}, kqLoc {*this, "kq"}, - viewPointLoc {*this, "viewPoint"} -{ - VertexArrayObject {va}.addAttribs(b); -} - -void -SceneShader::PointLightShader::add(const Position3D & position, const RGB & colour, const float kq) const -{ - Program::use(); - glBindVertexArray(va); - glBindBuffer(GL_ARRAY_BUFFER, b); - glUniform3fv(colourLoc, 1, glm::value_ptr(colour)); - glUniform1f(kqLoc, kq); - glBufferData(GL_ARRAY_BUFFER, sizeof(Position3D), glm::value_ptr(position), GL_DYNAMIC_DRAW); - glDrawArrays(GL_POINTS, 0, 1); -} diff --git a/gfx/gl/sceneShader.h b/gfx/gl/sceneShader.h index 7e31cb8..813c1bf 100644 --- a/gfx/gl/sceneShader.h +++ b/gfx/gl/sceneShader.h @@ -51,27 +51,12 @@ class SceneShader { RequiredUniformLocation waveLoc; }; - class PointLightShader : public SceneProgram { - public: - PointLightShader(); - - void add(const Position3D & position, const RGB & colour, const float kq) const; - - private: - UniformLocation colourLoc; - UniformLocation kqLoc; - UniformLocation viewPointLoc; - glVertexArray va; - glBuffer b; - }; - public: SceneShader(); BasicProgram basic; WaterProgram water; - AbsolutePosProgram basicInst, landmass, absolute, spotLightInst; - PointLightShader pointLight; + AbsolutePosProgram basicInst, landmass, absolute, spotLightInst, pointLightInst; void setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const; void setViewPort(const ViewPort & viewPort) const; diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp index bc93729..9e278c4 100644 --- a/test/test-assetFactory.cpp +++ b/test/test-assetFactory.cpp @@ -48,7 +48,6 @@ public: void lights(const SceneShader & shader) const override { - shader.pointLight.add({-3, 1, 5}, {1, 1, 1}, .1F); objects.apply(&Renderable::lights, shader); } diff --git a/test/test-render.cpp b/test/test-render.cpp index 47d146c..41731dd 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -158,34 +158,4 @@ BOOST_AUTO_TEST_CASE(terrain) Texture::save(outImage, "/tmp/terrain.tga"); } -BOOST_AUTO_TEST_CASE(pointlight) -{ - SceneRenderer ss {size, output}; - ss.camera.setView({-10000, -10000, 60000}, glm::normalize(glm::vec3 {1, 1, -0.5F})); - - class PointLightScene : public TestScene { - public: - void - environment(const SceneShader &, const SceneRenderer & r) const override - { - r.setAmbientLight({0.2F, 0.2F, 0.2F}); - r.setDirectionalLight({0.2F, 0.2F, 0.2F}, west + down, *this); - } - - void - lights(const SceneShader & shader) const override - { - for (int x = 50000; x < 100000; x += 20000) { - for (int y = 50000; y < 2000000; y += 20000) { - shader.pointLight.add({x, y, 4000}, {1.0, 1.0, 1.0}, 0.1F); - } - } - } - }; - - const PointLightScene scene; - ss.render(scene); - Texture::save(outImage, "/tmp/pointlight.tga"); -} - BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3