summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-01-10 19:04:30 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2024-01-10 19:04:30 +0000
commitb8401062e1d3f5e6554ab7fd9b983ea63cfb05c5 (patch)
treeba5c4f944cee1fa04b4aba8b018c3f1f7864490e
parentAdd spot light definition, loader, and rendering (diff)
downloadilt-b8401062e1d3f5e6554ab7fd9b983ea63cfb05c5.tar.bz2
ilt-b8401062e1d3f5e6554ab7fd9b983ea63cfb05c5.tar.xz
ilt-b8401062e1d3f5e6554ab7fd9b983ea63cfb05c5.zip
Initial commit with working light instancing
-rw-r--r--game/scenary/illuminator.cpp10
-rw-r--r--gfx/gl/sceneShader.cpp34
-rw-r--r--gfx/gl/sceneShader.h20
-rw-r--r--gfx/gl/shaders/spotLight.fs6
-rw-r--r--gfx/gl/shaders/spotLight.gs6
-rw-r--r--gfx/gl/shaders/spotLight.vs20
-rw-r--r--res/lights.xml4
-rw-r--r--test/test-assetFactory.cpp6
-rw-r--r--test/test-render.cpp29
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<LocationVertex, &LocationVertex::first, &LocationVertex::second>(instances.bufferName(), 1);
VertexArrayObject {instancesSpotLightVAO}
- .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(instances.bufferName(), 0)
.addAttribs<SpotLightVertex, &SpotLightVertex::position, &SpotLightVertex::direction,
&SpotLightVertex::colour, &SpotLightVertex::kq, &SpotLightVertex::arc>(
- instancesSpotLight.bufferName(), 1);
+ instancesSpotLight.bufferName(), 0)
+ .addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(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<GLsizei>(count), static_cast<GLsizei>(scount));
+ glDrawArraysInstanced(GL_POINTS, 0, static_cast<GLsizei>(scount), static_cast<GLsizei>(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<const SceneProgram *, 7> {
- &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLight}) {
+ for (const auto & prog : std::initializer_list<const SceneProgram *> {
+ &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<const SceneProgram *, 7> {
- &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLight}) {
+ for (const auto & prog : std::initializer_list<const SceneProgram *> {
+ &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<Position3D, Direction3D>;
- VertexArrayObject {va}.addAttribs<v3pair, &v3pair::first, &v3pair::second>(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 @@
<bodyMesh id="body">
<use type="cylinder" scale="0.12,0.12,1" colour="lightslategray"/>
<use type="cylinder" scale="0.07,0.07,3" position="0,0,1" colour="lightslategray"/>
- <use type="cylinder" scale="0.05,0.05,1.5" position="0,0.1,4" rotation="1.5,0,0" colour="lightslategray"/>
+ <use type="cylinder" scale="0.05,0.05,1.5" position="0,-0.1,4" rotation="-1.5,0,0" colour="lightslategray"/>
</bodyMesh>
- <spotLight position="0,1.4,4.1" direction="0,0,-1" colour=".9,.9,.9" kq="8000" arc="1.5"/>
+ <spotLight position="0,1400,4100" direction="0,0,-1" colour=".9,.9,.9" kq="0.01" arc="1.5"/>
</asset>
<!-- yes I'm hacking some floor to light up as though its a bush -->
<asset p.typeid="Foliage" id="floor" name="floor">
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<Light>(rlight_f, Location {{0, 0, 0}, {0, 0, 0}});
- auto light2 = std::make_shared<Light>(rlight_f, Location {{-4000, 0, 0}, {0, -1, 0}});
- auto light3 = std::make_shared<Light>(rlight_f, Location {{-4000, -4000, 0}, {0, -3, 0}});
- auto light4 = std::make_shared<Light>(rlight_f, Location {{3000, 4600, 0}, {0, 1, 0}});
+ auto light2 = std::make_shared<Light>(rlight_f, Location {{-4000, 0, 0}, {0, 2, 0}});
+ auto light3 = std::make_shared<Light>(rlight_f, Location {{-4000, -4000, 0}, {0, 1, 0}});
+ auto light4 = std::make_shared<Light>(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();