diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-01-05 19:45:36 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-01-05 19:45:36 +0000 |
commit | 7c1e9b25fa4062a69ea00c20cdbcc745d1b82659 (patch) | |
tree | 03a200a82211016a70420930ee88d2afd01f60a5 /gfx/gl/shaders/spotLight.gs | |
parent | Include uniform name in required uniform does not exist error message (diff) | |
download | ilt-7c1e9b25fa4062a69ea00c20cdbcc745d1b82659.tar.bz2 ilt-7c1e9b25fa4062a69ea00c20cdbcc745d1b82659.tar.xz ilt-7c1e9b25fa4062a69ea00c20cdbcc745d1b82659.zip |
Add rendering support for spot lights
Diffstat (limited to 'gfx/gl/shaders/spotLight.gs')
-rw-r--r-- | gfx/gl/shaders/spotLight.gs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/gfx/gl/shaders/spotLight.gs b/gfx/gl/shaders/spotLight.gs new file mode 100644 index 0000000..ad65675 --- /dev/null +++ b/gfx/gl/shaders/spotLight.gs @@ -0,0 +1,63 @@ +#version 330 core +#extension GL_ARB_enhanced_layouts : enable +#extension GL_ARB_shading_language_420pack : enable + +const vec3[] pyramid = vec3[]( // four-sided + vec3(0, 0, 0), // Apex + vec3(-1, 1, 1), // Back-left + vec3(-1, -1, 1), // Front-left + vec3(1, 1, 1), // Back-right + vec3(1, -1, 1) // Front-right +); +uniform mat4 viewProjection; +uniform float arc; + +in vec3 position[]; +in vec3 direction[]; +in float size[]; +in float cosarc[]; +layout(points) in; + +layout(triangle_strip, max_vertices = 8) out; +out vec4 geo_centre; +out vec4 geo_direction; + +vec3 +perp(vec3 a) +{ + return normalize(a.x != 0 ? vec3((a.y + a.z) / -a.x, 1, 1) + : a.y != 0 ? vec3(1, (a.x + a.z) / -a.y, 1) + : vec3(1, 1, (a.x + a.y) / -a.z)); +} + +void +doVertex(vec4 ndcpos) +{ + gl_Position = ndcpos; + geo_centre = vec4(position[0], size[0]); + geo_direction = vec4(direction[0], cosarc[0]); + EmitVertex(); +} + +void +main() +{ + const float base = size[0] * tan(arc / 2); + const vec3 offx = perp(direction[0]); + const vec3 offy = cross(direction[0], offx); + vec4 out_py[pyramid.length()]; + for (int i = 0; i < pyramid.length(); ++i) { + const vec3 p = pyramid[i]; + const vec3 edge = (offx * base * p.x) + (offy * base * p.y) + (direction[0] * size[0] * p.z); + out_py[i] = viewProjection * (gl_in[0].gl_Position + vec4(edge, 1)); + } + doVertex(out_py[3]); + doVertex(out_py[0]); + doVertex(out_py[1]); + doVertex(out_py[2]); + doVertex(out_py[3]); + doVertex(out_py[4]); + doVertex(out_py[0]); + doVertex(out_py[2]); + EndPrimitive(); +} |