From 50d6f63fb28c82a35e68914fe941b685d26f6e89 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 23 Nov 2022 19:07:18 +0000 Subject: Add rendering support for point lights --- gfx/gl/shaders/pointLight.fs | 27 +++++++++++++++++++++++++++ gfx/gl/shaders/pointLight.gs | 44 ++++++++++++++++++++++++++++++++++++++++++++ gfx/gl/shaders/pointLight.vs | 18 ++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 gfx/gl/shaders/pointLight.fs create mode 100644 gfx/gl/shaders/pointLight.gs create mode 100644 gfx/gl/shaders/pointLight.vs (limited to 'gfx/gl/shaders') 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); +} -- cgit v1.2.3