summaryrefslogtreecommitdiff
path: root/gfx/gl
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 /gfx/gl
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
Diffstat (limited to 'gfx/gl')
-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
5 files changed, 133 insertions, 2 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);
+}