summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-11-23 19:07:18 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-11-23 19:07:18 +0000
commit50d6f63fb28c82a35e68914fe941b685d26f6e89 (patch)
tree4a86d800d36341aedee7319307675cd1e6e5b784
parentSupport setting a viewPort uniform for those shaders which need it (diff)
downloadilt-50d6f63fb28c82a35e68914fe941b685d26f6e89.tar.bz2
ilt-50d6f63fb28c82a35e68914fe941b685d26f6e89.tar.xz
ilt-50d6f63fb28c82a35e68914fe941b685d26f6e89.zip
Add rendering support for point lights
-rw-r--r--gfx/gl/sceneShader.cpp31
-rw-r--r--gfx/gl/sceneShader.h15
-rw-r--r--gfx/gl/shaders/pointLight.fs27
-rw-r--r--gfx/gl/shaders/pointLight.gs44
-rw-r--r--gfx/gl/shaders/pointLight.vs18
-rw-r--r--test/test-render.cpp25
-rw-r--r--ui/gameMainWindow.cpp4
-rw-r--r--ui/gameMainWindow.h1
-rw-r--r--ui/sceneRenderer.cpp1
-rw-r--r--ui/sceneRenderer.h1
10 files changed, 164 insertions, 3 deletions
diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp
index 0b0c530..a02d955 100644
--- a/gfx/gl/sceneShader.cpp
+++ b/gfx/gl/sceneShader.cpp
@@ -2,9 +2,12 @@
#include <array>
#include <gfx/gl/shaders/fs-basicShader.h>
#include <gfx/gl/shaders/fs-landmassShader.h>
+#include <gfx/gl/shaders/fs-pointLight.h>
#include <gfx/gl/shaders/fs-waterShader.h>
+#include <gfx/gl/shaders/gs-pointLight.h>
#include <gfx/gl/shaders/vs-basicShader.h>
#include <gfx/gl/shaders/vs-landmassShader.h>
+#include <gfx/gl/shaders/vs-pointLight.h>
#include <gfx/gl/shaders/vs-waterShader.h>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/transform.hpp>
@@ -19,7 +22,7 @@ SceneShader::SceneShader() :
void
SceneShader::setViewProjection(const glm::mat4 & viewProjection) const
{
- for (const auto & prog : std::array<const SceneProgram *, 4> {&basic, &water, &landmass, &absolute}) {
+ for (const auto & prog : std::array<const SceneProgram *, 5> {&basic, &water, &landmass, &absolute, &pointLight}) {
prog->setViewProjection(viewProjection);
}
}
@@ -27,7 +30,7 @@ SceneShader::setViewProjection(const glm::mat4 & viewProjection) const
void
SceneShader::setViewPort(const glm::ivec4 & viewPort) const
{
- for (const auto & prog : std::array<const SceneProgram *, 4> {&basic, &water, &landmass, &absolute}) {
+ for (const auto & prog : std::array<const SceneProgram *, 5> {&basic, &water, &landmass, &absolute, &pointLight}) {
prog->setViewPort(viewPort);
}
}
@@ -73,3 +76,27 @@ SceneShader::WaterProgram::use(float waveCycle) const
glm::vec3 waves {waveCycle, 0.F, 0.F};
glUniform3fv(waveLoc, 1, glm::value_ptr(waves));
}
+
+SceneShader::PointLightShader::PointLightShader() :
+ SceneProgram {pointLight_vs, pointLight_gs, pointLight_fs}, colourLoc {*this, "colour"}, kqLoc {*this, "kq"}
+{
+ glBindVertexArray(va);
+ glBindBuffer(GL_ARRAY_BUFFER, b);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3), nullptr, GL_DYNAMIC_DRAW);
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindVertexArray(0);
+}
+
+void
+SceneShader::PointLightShader::add(const glm::vec3 & position, const glm::vec3 & colour, const float kq) const
+{
+ Program::use();
+ glBindVertexArray(va);
+ glBindBuffer(GL_ARRAY_BUFFER, b);
+ glUniform3fv(colourLoc, 1, glm::value_ptr(colour));
+ glUniform1f(kqLoc, kq);
+ glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::vec3), glm::value_ptr(position));
+ glDrawArrays(GL_POINTS, 0, 1);
+}
diff --git a/gfx/gl/sceneShader.h b/gfx/gl/sceneShader.h
index 99431f1..62df9bf 100644
--- a/gfx/gl/sceneShader.h
+++ b/gfx/gl/sceneShader.h
@@ -1,6 +1,7 @@
#pragma once
#include "program.h"
+#include <glArrays.h>
class Location;
@@ -47,12 +48,26 @@ class SceneShader {
RequiredUniformLocation waveLoc;
};
+ class PointLightShader : public SceneProgram {
+ public:
+ PointLightShader();
+
+ void add(const glm::vec3 & position, const glm::vec3 & colour, const float kq) const;
+
+ private:
+ UniformLocation colourLoc;
+ UniformLocation kqLoc;
+ glVertexArray va;
+ glBuffer b;
+ };
+
public:
SceneShader();
BasicProgram basic;
WaterProgram water;
AbsolutePosProgram landmass, absolute;
+ PointLightShader pointLight;
void setViewProjection(const glm::mat4 & viewProjection) const;
void setViewPort(const glm::ivec4 & viewPort) const;
diff --git a/gfx/gl/shaders/pointLight.fs b/gfx/gl/shaders/pointLight.fs
new file mode 100644
index 0000000..18407fe
--- /dev/null
+++ b/gfx/gl/shaders/pointLight.fs
@@ -0,0 +1,27 @@
+#version 330 core
+#extension GL_ARB_shading_language_420pack : enable
+
+out vec3 FragColor;
+
+layout(binding = 0) uniform sampler2D gPosition;
+layout(binding = 1) uniform sampler2D gNormal;
+uniform ivec4 viewPort;
+uniform vec3 colour;
+uniform float kq;
+in vec3 geo_centre;
+
+void
+main()
+{
+ const vec2 texCoord = gl_FragCoord.xy / viewPort.zw;
+ const vec3 position = texture(gPosition, texCoord).xyz;
+ const vec3 normal = texture(gNormal, texCoord).xyz;
+ const vec3 lightv = position - geo_centre;
+ const float lightDist = length(lightv);
+ const vec3 lightDirection = normalize(lightv);
+ const float normalDot = dot(-lightDirection, normal);
+ if (normalDot < 0) {
+ discard;
+ }
+ FragColor = (colour * normalDot) / (1 + (kq * pow(lightDist, 2)));
+}
diff --git a/gfx/gl/shaders/pointLight.gs b/gfx/gl/shaders/pointLight.gs
new file mode 100644
index 0000000..2bd71e9
--- /dev/null
+++ b/gfx/gl/shaders/pointLight.gs
@@ -0,0 +1,44 @@
+#version 330 core
+#extension GL_ARB_enhanced_layouts : enable
+#extension GL_ARB_shading_language_420pack : enable
+
+const int maxv = 128;
+const int maxarcs = (maxv / 2) - 1;
+const int minarcs = 10;
+const float tau = radians(360);
+const float scale = 150;
+uniform ivec4 viewPort;
+
+in vec3 centre[];
+in float size[];
+layout(points) in;
+
+layout(triangle_strip, max_vertices = maxv) out;
+out vec3 geo_centre;
+
+void
+doSeg(float arc, vec2 radius)
+{
+ gl_Position = gl_in[0].gl_Position;
+ geo_centre = centre[0];
+ EmitVertex();
+
+ const vec2 off = vec2(cos(arc), sin(arc)) * radius;
+ gl_Position.xy = gl_in[0].gl_Position.xy + off;
+ geo_centre = centre[0];
+ EmitVertex();
+}
+
+void
+main()
+{
+ const vec2 display = viewPort.zw;
+ const vec2 ratio = vec2(1, display.x / display.y);
+ const vec2 radius = (size[0] * ratio * scale) / gl_in[0].gl_Position.w;
+ const float step = tau / clamp(radius.x, minarcs, maxarcs);
+ for (float arc = 0; arc < tau; arc += step) {
+ doSeg(arc, radius);
+ }
+ doSeg(tau, radius);
+ EndPrimitive();
+}
diff --git a/gfx/gl/shaders/pointLight.vs b/gfx/gl/shaders/pointLight.vs
new file mode 100644
index 0000000..6236c33
--- /dev/null
+++ b/gfx/gl/shaders/pointLight.vs
@@ -0,0 +1,18 @@
+#version 330 core
+
+layout(location = 0) in vec3 position;
+
+uniform vec3 colour;
+uniform float kq;
+uniform mat4 viewProjection;
+
+out vec3 centre;
+out float size;
+
+void
+main()
+{
+ centre = position;
+ size = sqrt(256 * max(max(colour.r, colour.g), colour.b) / kq);
+ gl_Position = viewProjection * vec4(centre, 1);
+}
diff --git a/test/test-render.cpp b/test/test-render.cpp
index d5a79ec..089665f 100644
--- a/test/test-render.cpp
+++ b/test/test-render.cpp
@@ -8,6 +8,7 @@
#include <game/terrain.h>
#include <gfx/models/texture.h>
#include <lib/glArrays.h>
+#include <maths.h>
#include <ui/applicationBase.h>
#include <ui/sceneRenderer.h>
#include <ui/window.h>
@@ -65,6 +66,10 @@ class TestScene : public SceneRenderer::SceneProvider {
{
terrain.render(shader);
}
+ void
+ lights(const SceneShader &) const
+ {
+ }
};
BOOST_GLOBAL_FIXTURE(ApplicationBase);
@@ -87,7 +92,25 @@ BOOST_AUTO_TEST_CASE(pointlight)
SceneRenderer ss {size, output};
ss.camera.pos = {-10, -10, 60};
ss.camera.forward = glm::normalize(glm::vec3 {1, 1, -0.5F});
- TestScene scene;
+ class PointLightScene : public TestScene {
+ public:
+ void
+ environment(const SceneShader &, const SceneRenderer & r) const override
+ {
+ r.setAmbientLight({0.2F, 0.2F, 0.2F});
+ r.setDirectionalLight({}, down);
+ }
+ void
+ lights(const SceneShader & shader) const override
+ {
+ for (int x = 50; x < 100; x += 20) {
+ for (int y = 50; y < 2000; y += 20) {
+ shader.pointLight.add({x, y, 4}, {1.0, 1.0, 1.0}, 0.1);
+ }
+ }
+ }
+ };
+ PointLightScene scene;
ss.render(scene);
Texture::save(outImage, size, "/tmp/pointlight.tga");
}
diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp
index 0e861b4..8e7e711 100644
--- a/ui/gameMainWindow.cpp
+++ b/ui/gameMainWindow.cpp
@@ -60,3 +60,7 @@ GameMainWindow::environment(const SceneShader & s, const SceneRenderer & r) cons
// default for now
SceneRenderer::SceneProvider::environment(s, r);
}
+void
+GameMainWindow::lights(const SceneShader &) const
+{
+}
diff --git a/ui/gameMainWindow.h b/ui/gameMainWindow.h
index 8485266..fbba0cd 100644
--- a/ui/gameMainWindow.h
+++ b/ui/gameMainWindow.h
@@ -16,4 +16,5 @@ public:
private:
void content(const SceneShader &) const override;
void environment(const SceneShader &, const SceneRenderer &) const override;
+ void lights(const SceneShader &) const override;
};
diff --git a/ui/sceneRenderer.cpp b/ui/sceneRenderer.cpp
index dc8e440..a97387e 100644
--- a/ui/sceneRenderer.cpp
+++ b/ui/sceneRenderer.cpp
@@ -79,6 +79,7 @@ SceneRenderer::render(const SceneProvider & scene) const
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, gNormal);
scene.environment(shader, *this);
+ scene.lights(shader);
// Lighting pass
glBindFramebuffer(GL_FRAMEBUFFER, output);
diff --git a/ui/sceneRenderer.h b/ui/sceneRenderer.h
index 8b81d0c..f40b894 100644
--- a/ui/sceneRenderer.h
+++ b/ui/sceneRenderer.h
@@ -14,6 +14,7 @@ public:
virtual ~SceneProvider() = default;
virtual void content(const SceneShader &) const = 0;
virtual void environment(const SceneShader &, const SceneRenderer &) const;
+ virtual void lights(const SceneShader &) const = 0;
};
explicit SceneRenderer(glm::ivec2 size, GLuint output);