From bb693756b9aace889f806a768b54655023d163ab Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 9 Jan 2024 20:17:55 +0000 Subject: Add the Illuminator (type) and Light (instance) classes --- game/scenary/illuminator.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ game/scenary/illuminator.h | 26 ++++++++++++++++++++++++++ game/scenary/light.cpp | 7 +++++++ game/scenary/light.h | 19 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 game/scenary/illuminator.cpp create mode 100644 game/scenary/illuminator.h create mode 100644 game/scenary/light.cpp create mode 100644 game/scenary/light.h diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp new file mode 100644 index 0000000..4989933 --- /dev/null +++ b/game/scenary/illuminator.cpp @@ -0,0 +1,40 @@ +#include "illuminator.h" +#include "gfx/gl/sceneShader.h" +#include "gfx/gl/vertexArrayObject.h" +#include "gfx/models/texture.h" +#include "location.h" + +bool +Illuminator::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct) && Asset::persist(store); +} + +void +Illuminator::postLoad() +{ + texture = getTexture(); + bodyMesh->configureVAO(instanceVAO) + .addAttribs(instances.bufferName(), 1); +} + +void +Illuminator::render(const SceneShader & shader) const +{ + if (const auto count = instances.size()) { + shader.basicInst.use(); + if (texture) { + texture->bind(); + } + bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); + } +} + +void +Illuminator::lights(const SceneShader &) const +{ + if (const auto count = instances.size()) { + // shader.pointLight.use(); + // bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); + } +} diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h new file mode 100644 index 0000000..db9acfb --- /dev/null +++ b/game/scenary/illuminator.h @@ -0,0 +1,26 @@ +#pragma once + +#include "assetFactory/asset.h" +#include "gfx/gl/instanceVertices.h" +#include "gfx/renderable.h" + +class SceneShader; +class Location; +class Texture; + +class Illuminator : public Asset, public Renderable, public StdTypeDefs { + Mesh::Ptr bodyMesh; + std::shared_ptr texture; + glVertexArray instanceVAO; + +public: + using LocationVertex = std::pair; + mutable InstanceVertices instances; + void render(const SceneShader &) const override; + void lights(const SceneShader &) const override; + +protected: + friend Persistence::SelectionPtrBase>; + bool persist(Persistence::PersistenceStore & store) override; + void postLoad() override; +}; diff --git a/game/scenary/light.cpp b/game/scenary/light.cpp new file mode 100644 index 0000000..6207497 --- /dev/null +++ b/game/scenary/light.cpp @@ -0,0 +1,7 @@ +#include "light.h" +#include "location.h" + +Light::Light(std::shared_ptr type, const Location & position) : + type {std::move(type)}, location {this->type->instances.acquire(position.getRotationTransform(), position.pos)} +{ +} diff --git a/game/scenary/light.h b/game/scenary/light.h new file mode 100644 index 0000000..0b19535 --- /dev/null +++ b/game/scenary/light.h @@ -0,0 +1,19 @@ +#pragma once + +#include "game/worldobject.h" +#include "illuminator.h" + +class Location; + +class Light : public WorldObject { + std::shared_ptr type; + InstanceVertices::InstanceProxy location; + + void + tick(TickDuration) override + { + } + +public: + Light(std::shared_ptr type, const Location & position); +}; -- cgit v1.2.3 From 00f25bcce58789ad0f8082406f233de461d7ccf2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 9 Jan 2024 20:18:17 +0000 Subject: Add lights resource set and test scene --- res/lights.xml | 16 ++++++++++++++++ test/test-assetFactory.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 res/lights.xml diff --git a/res/lights.xml b/res/lights.xml new file mode 100644 index 0000000..137137d --- /dev/null +++ b/res/lights.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp index c8183df..e7ba19c 100644 --- a/test/test-assetFactory.cpp +++ b/test/test-assetFactory.cpp @@ -9,6 +9,8 @@ #include "assetFactory/object.h" #include "assetFactory/texturePacker.h" #include "game/scenary/foliage.h" +#include "game/scenary/illuminator.h" +#include "game/scenary/light.h" #include "game/scenary/plant.h" #include "game/vehicles/railVehicle.h" #include "game/vehicles/railVehicleClass.h" @@ -125,6 +127,29 @@ BOOST_AUTO_TEST_CASE(foliage, *boost::unit_test::timeout(5)) render(6000); } +BOOST_AUTO_TEST_CASE(lights, *boost::unit_test::timeout(5)) +{ + auto mf = AssetFactory::loadXML(RESDIR "/lights.xml"); + BOOST_REQUIRE(mf); + auto rlight = mf->assets.at("r-light"); + BOOST_REQUIRE(rlight); + auto rlight_f = std::dynamic_pointer_cast(rlight); + BOOST_REQUIRE(rlight_f); + + auto light1 = std::make_shared(rlight_f, Location {{0, 0, 0}, {0, 0, 0}}); + auto light2 = std::make_shared(rlight_f, Location {{-4000, 0, 0}, {0, -1, 0}}); + auto light3 = std::make_shared(rlight_f, Location {{-4000, -4000, 0}, {0, -3, 0}}); + auto light4 = std::make_shared(rlight_f, Location {{3000, 4600, 0}, {0, 1, 0}}); + objects.objects.push_back(rlight_f); + + // yes I'm hacking some floor to light up as though its a bush + auto floorf = std::dynamic_pointer_cast(mf->assets.at("floor")); + auto floor = std::make_shared(floorf, Location {}); + objects.objects.push_back(floorf); + + render(6000); +} + BOOST_AUTO_TEST_SUITE_END(); BOOST_AUTO_TEST_CASE(loadall) -- cgit v1.2.3 From 8740d30b01f6c12e2fcea0450df73736cd17dc87 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 10 Jan 2024 01:10:44 +0000 Subject: Add spot light definition, loader, and rendering Rendering is untested, data is passed to whatever GL program is currently active. --- game/scenary/illuminator.cpp | 27 ++++++++++++++++++++++++--- game/scenary/illuminator.h | 21 ++++++++++++++++++++- res/lights.xml | 1 + test/test-assetFactory.cpp | 1 + 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 4989933..43b6e4e 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -4,10 +4,18 @@ #include "gfx/models/texture.h" #include "location.h" +bool +Illuminator::SpotLight::persist(Persistence::PersistenceStore & store) +{ + return STORE_TYPE && STORE_MEMBER(position) && STORE_MEMBER(direction) && STORE_MEMBER(colour) && STORE_MEMBER(kq) + && STORE_MEMBER(arc); +} + bool Illuminator::persist(Persistence::PersistenceStore & store) { - return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct) && Asset::persist(store); + return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct) + && STORE_HELPER(spotLight, Persistence::Appender) && Asset::persist(store); } void @@ -16,6 +24,14 @@ Illuminator::postLoad() texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); + VertexArrayObject {instancesSpotLightVAO} + .addAttribs(instances.bufferName(), 0) + .addAttribs( + instancesSpotLight.bufferName(), 1); + std::transform(spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { + return instancesSpotLight.acquire(*s); + }); } void @@ -34,7 +50,12 @@ void Illuminator::lights(const SceneShader &) const { if (const auto count = instances.size()) { - // shader.pointLight.use(); - // bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); + if (const auto scount = instancesSpotLight.size()) { + // shader.pointLight.use(); + glBindVertexArray(instancesSpotLightVAO); + glDrawArraysInstanced(GL_POINTS, 0, static_cast(count), static_cast(scount)); + } + + glBindVertexArray(0); } } diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index db9acfb..99b87eb 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -11,11 +11,27 @@ class Texture; class Illuminator : public Asset, public Renderable, public StdTypeDefs { Mesh::Ptr bodyMesh; std::shared_ptr texture; - glVertexArray instanceVAO; + glVertexArray instanceVAO, instancesSpotLightVAO; + +public: + struct SpotLightVertex { + RelativePosition3D position; + Direction3D direction; + RGB colour; + RelativeDistance kq; + Angle arc; + }; + + struct SpotLight : Persistence::Persistable, SpotLightVertex, StdTypeDefs { + private: + friend Persistence::SelectionPtrBase>; + bool persist(Persistence::PersistenceStore & store) override; + }; public: using LocationVertex = std::pair; mutable InstanceVertices instances; + mutable InstanceVertices instancesSpotLight; void render(const SceneShader &) const override; void lights(const SceneShader &) const override; @@ -23,4 +39,7 @@ protected: friend Persistence::SelectionPtrBase>; bool persist(Persistence::PersistenceStore & store) override; void postLoad() override; + + std::vector spotLight; + std::vector::InstanceProxy> spotLightInstances; }; diff --git a/res/lights.xml b/res/lights.xml index 137137d..5c11fdf 100644 --- a/res/lights.xml +++ b/res/lights.xml @@ -6,6 +6,7 @@ + diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp index e7ba19c..91a5321 100644 --- a/test/test-assetFactory.cpp +++ b/test/test-assetFactory.cpp @@ -49,6 +49,7 @@ public: lights(const SceneShader & shader) const override { shader.pointLight.add({-3, 1, 5}, {1, 1, 1}, .1F); + objects.apply(&Renderable::lights, shader); } void -- cgit v1.2.3 From b8401062e1d3f5e6554ab7fd9b983ea63cfb05c5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 10 Jan 2024 19:04:30 +0000 Subject: Initial commit with working light instancing --- game/scenary/illuminator.cpp | 10 +++++----- gfx/gl/sceneShader.cpp | 34 +++++----------------------------- gfx/gl/sceneShader.h | 20 +------------------- gfx/gl/shaders/spotLight.fs | 6 ++++-- gfx/gl/shaders/spotLight.gs | 6 +++--- gfx/gl/shaders/spotLight.vs | 20 +++++++++++++------- res/lights.xml | 4 ++-- test/test-assetFactory.cpp | 6 +++--- test/test-render.cpp | 29 ----------------------------- 9 files changed, 36 insertions(+), 99 deletions(-) diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 43b6e4e..6f51506 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -25,10 +25,10 @@ Illuminator::postLoad() bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); VertexArrayObject {instancesSpotLightVAO} - .addAttribs(instances.bufferName(), 0) .addAttribs( - instancesSpotLight.bufferName(), 1); + instancesSpotLight.bufferName(), 0) + .addAttribs(instances.bufferName(), 1); std::transform(spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { return instancesSpotLight.acquire(*s); }); @@ -47,13 +47,13 @@ Illuminator::render(const SceneShader & shader) const } void -Illuminator::lights(const SceneShader &) const +Illuminator::lights(const SceneShader & shader) const { if (const auto count = instances.size()) { if (const auto scount = instancesSpotLight.size()) { - // shader.pointLight.use(); + shader.spotLightInst.use(); glBindVertexArray(instancesSpotLightVAO); - glDrawArraysInstanced(GL_POINTS, 0, static_cast(count), static_cast(scount)); + glDrawArraysInstanced(GL_POINTS, 0, static_cast(scount), static_cast(count)); } glBindVertexArray(0); diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp index ccc1a1d..cdb8d2f 100644 --- a/gfx/gl/sceneShader.cpp +++ b/gfx/gl/sceneShader.cpp @@ -21,15 +21,15 @@ SceneShader::SceneShader() : basicInst {dynamicPointInst_vs, material_fs}, landmass {fixedPoint_vs, landmass_fs}, - absolute {fixedPoint_vs, material_fs} + absolute {fixedPoint_vs, material_fs}, spotLightInst {spotLight_vs, spotLight_gs, spotLight_fs} { } void SceneShader::setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const { - for (const auto & prog : std::array { - &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLight}) { + for (const auto & prog : std::initializer_list { + &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLightInst}) { prog->setViewProjection(viewPoint, viewProjection); } } @@ -37,8 +37,8 @@ SceneShader::setViewProjection(const GlobalPosition3D & viewPoint, const glm::ma void SceneShader::setViewPort(const ViewPort & viewPort) const { - for (const auto & prog : std::array { - &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLight}) { + for (const auto & prog : std::initializer_list { + &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLightInst}) { prog->setViewPort(viewPort); } } @@ -106,27 +106,3 @@ SceneShader::PointLightShader::add(const Position3D & position, const RGB & colo glBufferData(GL_ARRAY_BUFFER, sizeof(Position3D), glm::value_ptr(position), GL_DYNAMIC_DRAW); glDrawArrays(GL_POINTS, 0, 1); } - -SceneShader::SpotLightShader::SpotLightShader() : - SceneProgram {spotLight_vs, spotLight_gs, spotLight_fs}, directionLoc {*this, "v_direction"}, - colourLoc {*this, "colour"}, kqLoc {*this, "kq"}, arcLoc {*this, "arc"}, viewPointLoc {*this, "viewPoint"} - -{ - using v3pair = std::pair; - VertexArrayObject {va}.addAttribs(b); -} - -void -SceneShader::SpotLightShader::add(const Position3D & position, const Direction3D & direction, const RGB & colour, - const float kq, const float arc) const -{ - Program::use(); - glBindVertexArray(va); - glBindBuffer(GL_ARRAY_BUFFER, b); - glUniform3fv(colourLoc, 1, glm::value_ptr(colour)); - glUniform3fv(directionLoc, 1, glm::value_ptr(direction)); - glUniform1f(kqLoc, kq); - glUniform1f(arcLoc, arc); - 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 7ffaacd..7e31cb8 100644 --- a/gfx/gl/sceneShader.h +++ b/gfx/gl/sceneShader.h @@ -65,31 +65,13 @@ class SceneShader { glBuffer b; }; - class SpotLightShader : public SceneProgram { - public: - SpotLightShader(); - - void add(const Position3D & position, const Direction3D & direction, const RGB & colour, const float kq, - const float arc) const; - - private: - UniformLocation directionLoc; - UniformLocation colourLoc; - UniformLocation kqLoc; - UniformLocation arcLoc; - UniformLocation viewPointLoc; - glVertexArray va; - glBuffer b; - }; - public: SceneShader(); BasicProgram basic; WaterProgram water; - AbsolutePosProgram basicInst, landmass, absolute; + AbsolutePosProgram basicInst, landmass, absolute, spotLightInst; PointLightShader pointLight; - SpotLightShader spotLight; void setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const; void setViewPort(const ViewPort & viewPort) const; diff --git a/gfx/gl/shaders/spotLight.fs b/gfx/gl/shaders/spotLight.fs index 937f922..6d5d332 100644 --- a/gfx/gl/shaders/spotLight.fs +++ b/gfx/gl/shaders/spotLight.fs @@ -6,8 +6,10 @@ out vec3 FragColor; layout(binding = 0) uniform isampler2D gPosition; layout(binding = 1) uniform sampler2D gNormal; uniform ivec4 viewPort; -uniform vec3 colour; -uniform float kq; +// uniform vec3 colour; +// uniform float kq; +const vec3 colour = vec3(1); +const float kq = 0.01; in vec4 geo_centre; in vec4 geo_direction; diff --git a/gfx/gl/shaders/spotLight.gs b/gfx/gl/shaders/spotLight.gs index b58c169..a2805fe 100644 --- a/gfx/gl/shaders/spotLight.gs +++ b/gfx/gl/shaders/spotLight.gs @@ -11,9 +11,9 @@ const vec3[] pyramid = vec3[]( // four-sided ); uniform mat4 viewProjection; uniform ivec3 viewPoint; -uniform float arc; - -in vec3 position[]; +// uniform float arc; +const float arc = 1; +in ivec3 position[]; in vec3 direction[]; in float size[]; in float cosarc[]; diff --git a/gfx/gl/shaders/spotLight.vs b/gfx/gl/shaders/spotLight.vs index ac1d1db..a2b755d 100644 --- a/gfx/gl/shaders/spotLight.vs +++ b/gfx/gl/shaders/spotLight.vs @@ -1,14 +1,20 @@ #version 330 core layout(location = 0) in vec3 v_position; +layout(location = 1) in vec3 v_direction; +// layout(location = 2) in vec3 colour; +// layout(location = 3) in float kq; +// layout(location = 4) in float arc; +layout(location = 5) in mat4 model; +layout(location = 9) in ivec3 modelPos; + +const vec3 colour = vec3(1); +const float kq = 0.01; +const float arc = 1; -uniform vec3 v_direction; -uniform vec3 colour; -uniform float kq; -uniform float arc; uniform ivec3 viewPoint; -out vec3 position; +out ivec3 position; out vec3 direction; out float size; out float cosarc; @@ -16,8 +22,8 @@ out float cosarc; void main() { - position = v_position; - direction = normalize(v_direction); + position = modelPos + ivec3(v_position * mat3(model)); + direction = normalize(v_direction * mat3(model)); size = (8000 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); cosarc = cos(arc / 2); gl_Position = vec4(position - viewPoint, 0); diff --git a/res/lights.xml b/res/lights.xml index 5c11fdf..536afd2 100644 --- a/res/lights.xml +++ b/res/lights.xml @@ -4,9 +4,9 @@ - + - + diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp index 91a5321..bc93729 100644 --- a/test/test-assetFactory.cpp +++ b/test/test-assetFactory.cpp @@ -138,9 +138,9 @@ BOOST_AUTO_TEST_CASE(lights, *boost::unit_test::timeout(5)) BOOST_REQUIRE(rlight_f); auto light1 = std::make_shared(rlight_f, Location {{0, 0, 0}, {0, 0, 0}}); - auto light2 = std::make_shared(rlight_f, Location {{-4000, 0, 0}, {0, -1, 0}}); - auto light3 = std::make_shared(rlight_f, Location {{-4000, -4000, 0}, {0, -3, 0}}); - auto light4 = std::make_shared(rlight_f, Location {{3000, 4600, 0}, {0, 1, 0}}); + auto light2 = std::make_shared(rlight_f, Location {{-4000, 0, 0}, {0, 2, 0}}); + auto light3 = std::make_shared(rlight_f, Location {{-4000, -4000, 0}, {0, 1, 0}}); + auto light4 = std::make_shared(rlight_f, Location {{3000, 4600, 0}, {0, 3, 0}}); objects.objects.push_back(rlight_f); // yes I'm hacking some floor to light up as though its a bush diff --git a/test/test-render.cpp b/test/test-render.cpp index 66b4d46..47d146c 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -188,33 +188,4 @@ BOOST_AUTO_TEST_CASE(pointlight) Texture::save(outImage, "/tmp/pointlight.tga"); } -BOOST_AUTO_TEST_CASE(spotlight) -{ - 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 - { - shader.spotLight.add({50000, 50000, 15000}, down, {1.0, 1.0, 1.0}, 0.01F, 1); - shader.spotLight.add({51000, 59500, 1000}, north, {1.0, 1.0, 1.0}, 0.001F, .5); - shader.spotLight.add({53000, 59500, 1000}, north, {1.0, 1.0, 1.0}, 0.001F, .5); - shader.spotLight.add({60000, 50000, 3000}, north + east, {1.0, 1.0, 1.0}, 0.0001F, .7F); - } - }; - - const PointLightScene scene; - ss.render(scene); - Texture::save(outImage, "/tmp/spotlight.tga"); -} - BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3 From b6fbe0bf35bf58447f2f83f6fc519b4d3eaea107 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 11 Jan 2024 18:57:57 +0000 Subject: Full implementation passing through light defs from vertex data Note: there is a bug where the light position/direction are rotated backwards to the model even though its using the model's rotation matrix... no idea why/how. --- gfx/gl/shaders/spotLight.fs | 12 +++++------- gfx/gl/shaders/spotLight.gs | 24 ++++++++++++++---------- gfx/gl/shaders/spotLight.vs | 24 ++++++++++++------------ res/lights.xml | 3 ++- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/gfx/gl/shaders/spotLight.fs b/gfx/gl/shaders/spotLight.fs index 6d5d332..ad33458 100644 --- a/gfx/gl/shaders/spotLight.fs +++ b/gfx/gl/shaders/spotLight.fs @@ -6,12 +6,10 @@ out vec3 FragColor; layout(binding = 0) uniform isampler2D gPosition; layout(binding = 1) uniform sampler2D gNormal; uniform ivec4 viewPort; -// uniform vec3 colour; -// uniform float kq; -const vec3 colour = vec3(1); -const float kq = 0.01; -in vec4 geo_centre; -in vec4 geo_direction; +flat in vec4 geo_centre; +flat in vec4 geo_direction; +flat in vec3 geo_colour; +flat in float geo_kq; void main() @@ -32,5 +30,5 @@ main() if (normalDot < 0) { discard; } - FragColor = (colour * normalDot) / (1 + (kq * pow(lightDist / 1000.0, 2))); + FragColor = (geo_colour * normalDot) / (1 + (geo_kq * pow(lightDist / 1000.0, 2))); } diff --git a/gfx/gl/shaders/spotLight.gs b/gfx/gl/shaders/spotLight.gs index a2805fe..194812a 100644 --- a/gfx/gl/shaders/spotLight.gs +++ b/gfx/gl/shaders/spotLight.gs @@ -11,17 +11,19 @@ const vec3[] pyramid = vec3[]( // four-sided ); uniform mat4 viewProjection; uniform ivec3 viewPoint; -// uniform float arc; -const float arc = 1; -in ivec3 position[]; -in vec3 direction[]; -in float size[]; -in float cosarc[]; +flat in ivec3 position[]; +flat in vec3 direction[]; +flat in vec3 colour[]; +flat in float size[]; +flat in float kq[]; +flat in vec2 arc[]; layout(points) in; layout(triangle_strip, max_vertices = 8) out; -out vec4 geo_centre; -out vec4 geo_direction; +flat out vec4 geo_centre; +flat out vec4 geo_direction; +flat out vec3 geo_colour; +flat out float geo_kq; vec3 perp(vec3 a) @@ -36,14 +38,16 @@ doVertex(vec4 ndcpos) { gl_Position = ndcpos; geo_centre = vec4(position[0], size[0]); - geo_direction = vec4(direction[0], cosarc[0]); + geo_direction = vec4(direction[0], arc[0].x); + geo_colour = colour[0]; + geo_kq = kq[0]; EmitVertex(); } void main() { - const float base = size[0] * tan(arc / 2); + const float base = size[0] * arc[0].y; const vec3 offx = perp(direction[0]); const vec3 offy = cross(direction[0], offx); vec4 out_py[pyramid.length()]; diff --git a/gfx/gl/shaders/spotLight.vs b/gfx/gl/shaders/spotLight.vs index a2b755d..8023f18 100644 --- a/gfx/gl/shaders/spotLight.vs +++ b/gfx/gl/shaders/spotLight.vs @@ -2,29 +2,29 @@ layout(location = 0) in vec3 v_position; layout(location = 1) in vec3 v_direction; -// layout(location = 2) in vec3 colour; -// layout(location = 3) in float kq; -// layout(location = 4) in float arc; +layout(location = 2) in vec3 v_colour; +layout(location = 3) in float v_kq; +layout(location = 4) in float v_arc; layout(location = 5) in mat4 model; layout(location = 9) in ivec3 modelPos; -const vec3 colour = vec3(1); -const float kq = 0.01; -const float arc = 1; - uniform ivec3 viewPoint; -out ivec3 position; -out vec3 direction; -out float size; -out float cosarc; +flat out ivec3 position; +flat out vec3 direction; +flat out float size; +flat out vec2 arc; // cos,tan (v_arc/2) +flat out vec3 colour; +flat out float kq; void main() { position = modelPos + ivec3(v_position * mat3(model)); direction = normalize(v_direction * mat3(model)); + colour = v_colour; + kq = v_kq; size = (8000 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); - cosarc = cos(arc / 2); + arc = vec2(cos(v_arc / 2), tan(v_arc / 2)); gl_Position = vec4(position - viewPoint, 0); } diff --git a/res/lights.xml b/res/lights.xml index 536afd2..a395306 100644 --- a/res/lights.xml +++ b/res/lights.xml @@ -5,8 +5,9 @@ + - + -- cgit v1.2.3 From c922efb8514fde12131d147e892cde31dc408929 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 12 Jan 2024 19:21:09 +0000 Subject: Tweak street light definition --- res/lights.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/res/lights.xml b/res/lights.xml index a395306..6c051c3 100644 --- a/res/lights.xml +++ b/res/lights.xml @@ -7,7 +7,7 @@ - + -- cgit v1.2.3 From 972d7260affe7f0d8eebaef609f5576a18f66f73 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 12 Jan 2024 19:21:37 +0000 Subject: Fix order or multiple to address reversed rotation --- gfx/gl/shaders/spotLight.vs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gfx/gl/shaders/spotLight.vs b/gfx/gl/shaders/spotLight.vs index 8023f18..eed8778 100644 --- a/gfx/gl/shaders/spotLight.vs +++ b/gfx/gl/shaders/spotLight.vs @@ -20,8 +20,8 @@ flat out float kq; void main() { - position = modelPos + ivec3(v_position * mat3(model)); - direction = normalize(v_direction * mat3(model)); + position = modelPos + ivec3(mat3(model) * v_position); + direction = normalize(mat3(model) * v_direction); colour = v_colour; kq = v_kq; size = (8000 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); -- cgit v1.2.3 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 From eb2094a1641c333a6e6f953e3b37aa962937cd59 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 13 Jan 2024 10:05:44 +0000 Subject: Update point light shaders for instancing --- gfx/gl/shaders/pointLight.fs | 8 ++++---- gfx/gl/shaders/pointLight.gs | 15 ++++++++++----- gfx/gl/shaders/pointLight.vs | 22 ++++++++++++++-------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/gfx/gl/shaders/pointLight.fs b/gfx/gl/shaders/pointLight.fs index ba327b6..7531d3e 100644 --- a/gfx/gl/shaders/pointLight.fs +++ b/gfx/gl/shaders/pointLight.fs @@ -6,9 +6,9 @@ out vec3 FragColor; layout(binding = 0) uniform isampler2D gPosition; layout(binding = 1) uniform sampler2D gNormal; uniform ivec4 viewPort; -uniform vec3 colour; -uniform float kq; -in vec4 geo_centre; +flat in vec4 geo_centre; +flat in vec3 geo_colour; +flat in float geo_kq; void main() @@ -26,5 +26,5 @@ main() if (normalDot < 0) { discard; } - FragColor = (colour * normalDot) / (1 + (kq * pow(lightDist / 1000.0, 2))); + FragColor = (geo_colour * normalDot) / (1 + (geo_kq * pow(lightDist / 1000.0, 2))); } diff --git a/gfx/gl/shaders/pointLight.gs b/gfx/gl/shaders/pointLight.gs index 9c41ed4..fc1d7c3 100644 --- a/gfx/gl/shaders/pointLight.gs +++ b/gfx/gl/shaders/pointLight.gs @@ -20,19 +20,24 @@ const vec3[] cube = vec3[]( // http://www.cs.umd.edu/gvil/papers/av_ts.pdf ); uniform mat4 viewProjection; uniform ivec3 viewPoint; - -in vec3 centre[]; -in float size[]; +flat in vec3 position[]; +flat in vec3 colour[]; +flat in float size[]; +flat in float kq[]; layout(points) in; layout(triangle_strip, max_vertices = cube.length()) out; -out vec4 geo_centre; +flat out vec4 geo_centre; +flat out vec3 geo_colour; +flat out float geo_kq; void doVertex(int idx) { gl_Position = viewProjection * (gl_in[0].gl_Position + vec4(cube[idx] * size[0], 1)); - geo_centre = vec4(centre[0], size[0]); + geo_centre = vec4(position[0], size[0]); + geo_colour = colour[0]; + geo_kq = kq[0]; EmitVertex(); } diff --git a/gfx/gl/shaders/pointLight.vs b/gfx/gl/shaders/pointLight.vs index 00a34a3..c538207 100644 --- a/gfx/gl/shaders/pointLight.vs +++ b/gfx/gl/shaders/pointLight.vs @@ -1,18 +1,24 @@ #version 330 core -layout(location = 0) in vec3 position; +layout(location = 0) in vec3 v_position; +layout(location = 1) in vec3 v_colour; +layout(location = 2) in float v_kq; +layout(location = 3) in mat4 model; +layout(location = 7) in ivec3 modelPos; -uniform vec3 colour; -uniform float kq; uniform ivec3 viewPoint; -out vec3 centre; -out float size; +flat out vec3 position; +flat out vec3 colour; +flat out float size; +flat out float kq; void main() { - centre = position; - size = (8000 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); - gl_Position = vec4(centre - viewPoint, 0); + position = modelPos + ivec3(mat3(model) * v_position); + kq = v_kq; + size = (8000 * sqrt(max(max(v_colour.r, v_colour.g), v_colour.b))) / sqrt(v_kq); + colour = v_colour; + gl_Position = vec4(position - viewPoint, 0); } -- cgit v1.2.3 From 48a5de8943bda62ba5caefe3b64c66a0d9024dff Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 13 Jan 2024 10:05:58 +0000 Subject: Add old street lamp model --- res/lights.xml | 10 ++++++++++ test/test-assetFactory.cpp | 9 +++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/res/lights.xml b/res/lights.xml index 6c051c3..8b25599 100644 --- a/res/lights.xml +++ b/res/lights.xml @@ -9,6 +9,16 @@ + + + + + + + + + + diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp index 9e278c4..8341fbf 100644 --- a/test/test-assetFactory.cpp +++ b/test/test-assetFactory.cpp @@ -133,14 +133,19 @@ BOOST_AUTO_TEST_CASE(lights, *boost::unit_test::timeout(5)) BOOST_REQUIRE(mf); auto rlight = mf->assets.at("r-light"); BOOST_REQUIRE(rlight); + auto oldlamp = mf->assets.at("old-lamp"); + BOOST_REQUIRE(oldlamp); auto rlight_f = std::dynamic_pointer_cast(rlight); BOOST_REQUIRE(rlight_f); + auto oldlamp_f = std::dynamic_pointer_cast(oldlamp); + BOOST_REQUIRE(oldlamp_f); - auto light1 = std::make_shared(rlight_f, Location {{0, 0, 0}, {0, 0, 0}}); + auto light1 = std::make_shared(oldlamp_f, Location {{0, 0, 0}, {0, 0, 0}}); auto light2 = std::make_shared(rlight_f, Location {{-4000, 0, 0}, {0, 2, 0}}); auto light3 = std::make_shared(rlight_f, Location {{-4000, -4000, 0}, {0, 1, 0}}); - auto light4 = std::make_shared(rlight_f, Location {{3000, 4600, 0}, {0, 3, 0}}); + auto light4 = std::make_shared(oldlamp_f, Location {{3000, 4600, 0}, {0, 2, 0}}); objects.objects.push_back(rlight_f); + objects.objects.push_back(oldlamp_f); // yes I'm hacking some floor to light up as though its a bush auto floorf = std::dynamic_pointer_cast(mf->assets.at("floor")); -- cgit v1.2.3 From 9a3ab8a6ae24e6e70ed7d69587c44a6fabb0a558 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 13 Jan 2024 10:14:26 +0000 Subject: Minor tidy up --- game/scenary/illuminator.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 3de4e52..7d072f9 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -1,8 +1,7 @@ #include "illuminator.h" #include "gfx/gl/sceneShader.h" #include "gfx/gl/vertexArrayObject.h" -#include "gfx/models/texture.h" -#include "location.h" +#include "gfx/models/texture.h" // IWYU pragma: keep bool Illuminator::SpotLight::persist(Persistence::PersistenceStore & store) @@ -28,6 +27,9 @@ Illuminator::persist(Persistence::PersistenceStore & store) void Illuminator::postLoad() { + if (spotLight.empty() && pointLight.empty()) { + throw std::logic_error {"Illuminator has no lights"}; + } texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); -- cgit v1.2.3 From e942bec77c77201ed50a2f6ecc77f3279a6bb4df Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 13 Jan 2024 10:32:47 +0000 Subject: Merge common parts of light type vertices --- game/scenary/illuminator.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index a2b287d..1c493b2 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -14,20 +14,19 @@ class Illuminator : public Asset, public Renderable, public StdTypeDefs { private: friend Persistence::SelectionPtrBase>; -- cgit v1.2.3 From e31f69ec26f5c128ae421eb418628ebcdfafb8f3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 13 Jan 2024 11:23:12 +0000 Subject: Only create VAOs for the light type(s) in use --- game/scenary/illuminator.cpp | 43 +++++++++++++++++++++++++------------------ game/scenary/illuminator.h | 3 ++- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/game/scenary/illuminator.cpp b/game/scenary/illuminator.cpp index 7d072f9..e3810ec 100644 --- a/game/scenary/illuminator.cpp +++ b/game/scenary/illuminator.cpp @@ -33,22 +33,29 @@ Illuminator::postLoad() texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); - VertexArrayObject {instancesSpotLightVAO} - .addAttribs( - instancesSpotLight.bufferName(), 0) - .addAttribs(instances.bufferName(), 1); - 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); - }); + if (!spotLight.empty()) { + instancesSpotLightVAO.emplace(); + VertexArrayObject {*instancesSpotLightVAO} + .addAttribs( + instancesSpotLight.bufferName(), 0) + .addAttribs(instances.bufferName(), 1); + std::transform( + spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { + return instancesSpotLight.acquire(*s); + }); + } + if (!pointLight.empty()) { + instancesPointLightVAO.emplace(); + 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 @@ -69,12 +76,12 @@ Illuminator::lights(const SceneShader & shader) const if (const auto count = instances.size()) { if (const auto scount = instancesSpotLight.size()) { shader.spotLightInst.use(); - glBindVertexArray(instancesSpotLightVAO); + glBindVertexArray(*instancesSpotLightVAO); glDrawArraysInstanced(GL_POINTS, 0, static_cast(scount), static_cast(count)); } if (const auto pcount = instancesPointLight.size()) { shader.pointLightInst.use(); - glBindVertexArray(instancesPointLightVAO); + glBindVertexArray(*instancesPointLightVAO); glDrawArraysInstanced(GL_POINTS, 0, static_cast(pcount), static_cast(count)); } diff --git a/game/scenary/illuminator.h b/game/scenary/illuminator.h index 1c493b2..893e5c7 100644 --- a/game/scenary/illuminator.h +++ b/game/scenary/illuminator.h @@ -11,7 +11,8 @@ class Texture; class Illuminator : public Asset, public Renderable, public StdTypeDefs { Mesh::Ptr bodyMesh; std::shared_ptr texture; - glVertexArray instanceVAO, instancesSpotLightVAO, instancesPointLightVAO; + glVertexArray instanceVAO; + std::optional instancesSpotLightVAO, instancesPointLightVAO; public: struct LightCommonVertex { -- cgit v1.2.3