diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-01-13 11:31:13 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-01-13 11:31:13 +0000 |
commit | 332355a9f4a4199e0ddb51852546d44ec54e176f (patch) | |
tree | 4998cde56004f53a0218dc5f00d578f939ffea08 /gfx | |
parent | Integer support in persistence (diff) | |
parent | Only create VAOs for the light type(s) in use (diff) | |
download | ilt-332355a9f4a4199e0ddb51852546d44ec54e176f.tar.bz2 ilt-332355a9f4a4199e0ddb51852546d44ec54e176f.tar.xz ilt-332355a9f4a4199e0ddb51852546d44ec54e176f.zip |
Merge branch 'model-lights'
Diffstat (limited to 'gfx')
-rw-r--r-- | gfx/gl/sceneShader.cpp | 55 | ||||
-rw-r--r-- | gfx/gl/sceneShader.h | 35 | ||||
-rw-r--r-- | gfx/gl/shaders/pointLight.fs | 8 | ||||
-rw-r--r-- | gfx/gl/shaders/pointLight.gs | 15 | ||||
-rw-r--r-- | gfx/gl/shaders/pointLight.vs | 22 | ||||
-rw-r--r-- | gfx/gl/shaders/spotLight.fs | 10 | ||||
-rw-r--r-- | gfx/gl/shaders/spotLight.gs | 24 | ||||
-rw-r--r-- | gfx/gl/shaders/spotLight.vs | 28 |
8 files changed, 71 insertions, 126 deletions
diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp index ccc1a1d..64bf171 100644 --- a/gfx/gl/sceneShader.cpp +++ b/gfx/gl/sceneShader.cpp @@ -1,5 +1,4 @@ #include "sceneShader.h" -#include <array> #include <gfx/gl/shaders/fs-landmass.h> #include <gfx/gl/shaders/fs-material.h> #include <gfx/gl/shaders/fs-pointLight.h> @@ -21,15 +20,16 @@ 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}, + pointLightInst {pointLight_vs, pointLight_gs, pointLight_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, &pointLightInst, &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, &pointLightInst, &spotLightInst}) { prog->setViewPort(viewPort); } } @@ -87,46 +87,3 @@ SceneShader::WaterProgram::use(float waveCycle) const Program::use(); glUniform1f(waveLoc, waveCycle); } - -SceneShader::PointLightShader::PointLightShader() : - SceneProgram {pointLight_vs, pointLight_gs, pointLight_fs}, colourLoc {*this, "colour"}, kqLoc {*this, "kq"}, - viewPointLoc {*this, "viewPoint"} -{ - VertexArrayObject {va}.addAttribs<Position3D>(b); -} - -void -SceneShader::PointLightShader::add(const Position3D & position, const RGB & colour, const float kq) const -{ - Program::use(); - glBindVertexArray(va); - glBindBuffer(GL_ARRAY_BUFFER, b); - glUniform3fv(colourLoc, 1, glm::value_ptr(colour)); - glUniform1f(kqLoc, kq); - 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..813c1bf 100644 --- a/gfx/gl/sceneShader.h +++ b/gfx/gl/sceneShader.h @@ -51,45 +51,12 @@ class SceneShader { RequiredUniformLocation waveLoc; }; - class PointLightShader : public SceneProgram { - public: - PointLightShader(); - - void add(const Position3D & position, const RGB & colour, const float kq) const; - - private: - UniformLocation colourLoc; - UniformLocation kqLoc; - UniformLocation viewPointLoc; - glVertexArray va; - 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; - PointLightShader pointLight; - SpotLightShader spotLight; + AbsolutePosProgram basicInst, landmass, absolute, spotLightInst, pointLightInst; void setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const; void setViewPort(const ViewPort & viewPort) const; diff --git a/gfx/gl/shaders/pointLight.fs b/gfx/gl/shaders/pointLight.fs index ba327b6..7531d3e 100644 --- a/gfx/gl/shaders/pointLight.fs +++ b/gfx/gl/shaders/pointLight.fs @@ -6,9 +6,9 @@ out vec3 FragColor; layout(binding = 0) uniform isampler2D gPosition; layout(binding = 1) uniform sampler2D gNormal; uniform ivec4 viewPort; -uniform vec3 colour; -uniform float kq; -in vec4 geo_centre; +flat in vec4 geo_centre; +flat in vec3 geo_colour; +flat in float geo_kq; void main() @@ -26,5 +26,5 @@ main() if (normalDot < 0) { discard; } - FragColor = (colour * normalDot) / (1 + (kq * pow(lightDist / 1000.0, 2))); + FragColor = (geo_colour * normalDot) / (1 + (geo_kq * pow(lightDist / 1000.0, 2))); } diff --git a/gfx/gl/shaders/pointLight.gs b/gfx/gl/shaders/pointLight.gs index 9c41ed4..fc1d7c3 100644 --- a/gfx/gl/shaders/pointLight.gs +++ b/gfx/gl/shaders/pointLight.gs @@ -20,19 +20,24 @@ const vec3[] cube = vec3[]( // http://www.cs.umd.edu/gvil/papers/av_ts.pdf ); uniform mat4 viewProjection; uniform ivec3 viewPoint; - -in vec3 centre[]; -in float size[]; +flat in vec3 position[]; +flat in vec3 colour[]; +flat in float size[]; +flat in float kq[]; layout(points) in; layout(triangle_strip, max_vertices = cube.length()) out; -out vec4 geo_centre; +flat out vec4 geo_centre; +flat out vec3 geo_colour; +flat out float geo_kq; void doVertex(int idx) { gl_Position = viewProjection * (gl_in[0].gl_Position + vec4(cube[idx] * size[0], 1)); - geo_centre = vec4(centre[0], size[0]); + geo_centre = vec4(position[0], size[0]); + geo_colour = colour[0]; + geo_kq = kq[0]; EmitVertex(); } diff --git a/gfx/gl/shaders/pointLight.vs b/gfx/gl/shaders/pointLight.vs index 00a34a3..c538207 100644 --- a/gfx/gl/shaders/pointLight.vs +++ b/gfx/gl/shaders/pointLight.vs @@ -1,18 +1,24 @@ #version 330 core -layout(location = 0) in vec3 position; +layout(location = 0) in vec3 v_position; +layout(location = 1) in vec3 v_colour; +layout(location = 2) in float v_kq; +layout(location = 3) in mat4 model; +layout(location = 7) in ivec3 modelPos; -uniform vec3 colour; -uniform float kq; uniform ivec3 viewPoint; -out vec3 centre; -out float size; +flat out vec3 position; +flat out vec3 colour; +flat out float size; +flat out float kq; void main() { - centre = position; - size = (8000 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); - gl_Position = vec4(centre - viewPoint, 0); + position = modelPos + ivec3(mat3(model) * v_position); + kq = v_kq; + size = (8000 * sqrt(max(max(v_colour.r, v_colour.g), v_colour.b))) / sqrt(v_kq); + colour = v_colour; + gl_Position = vec4(position - viewPoint, 0); } diff --git a/gfx/gl/shaders/spotLight.fs b/gfx/gl/shaders/spotLight.fs index 937f922..ad33458 100644 --- a/gfx/gl/shaders/spotLight.fs +++ b/gfx/gl/shaders/spotLight.fs @@ -6,10 +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; -in vec4 geo_centre; -in vec4 geo_direction; +flat in vec4 geo_centre; +flat in vec4 geo_direction; +flat in vec3 geo_colour; +flat in float geo_kq; void main() @@ -30,5 +30,5 @@ main() if (normalDot < 0) { discard; } - FragColor = (colour * normalDot) / (1 + (kq * pow(lightDist / 1000.0, 2))); + FragColor = (geo_colour * normalDot) / (1 + (geo_kq * pow(lightDist / 1000.0, 2))); } diff --git a/gfx/gl/shaders/spotLight.gs b/gfx/gl/shaders/spotLight.gs index b58c169..194812a 100644 --- a/gfx/gl/shaders/spotLight.gs +++ b/gfx/gl/shaders/spotLight.gs @@ -11,17 +11,19 @@ const vec3[] pyramid = vec3[]( // four-sided ); uniform mat4 viewProjection; uniform ivec3 viewPoint; -uniform float arc; - -in vec3 position[]; -in vec3 direction[]; -in float size[]; -in float cosarc[]; +flat in ivec3 position[]; +flat in vec3 direction[]; +flat in vec3 colour[]; +flat in float size[]; +flat in float kq[]; +flat in vec2 arc[]; layout(points) in; layout(triangle_strip, max_vertices = 8) out; -out vec4 geo_centre; -out vec4 geo_direction; +flat out vec4 geo_centre; +flat out vec4 geo_direction; +flat out vec3 geo_colour; +flat out float geo_kq; vec3 perp(vec3 a) @@ -36,14 +38,16 @@ doVertex(vec4 ndcpos) { gl_Position = ndcpos; geo_centre = vec4(position[0], size[0]); - geo_direction = vec4(direction[0], cosarc[0]); + geo_direction = vec4(direction[0], arc[0].x); + geo_colour = colour[0]; + geo_kq = kq[0]; EmitVertex(); } void main() { - const float base = size[0] * tan(arc / 2); + const float base = size[0] * arc[0].y; const vec3 offx = perp(direction[0]); const vec3 offy = cross(direction[0], offx); vec4 out_py[pyramid.length()]; diff --git a/gfx/gl/shaders/spotLight.vs b/gfx/gl/shaders/spotLight.vs index ac1d1db..eed8778 100644 --- a/gfx/gl/shaders/spotLight.vs +++ b/gfx/gl/shaders/spotLight.vs @@ -1,24 +1,30 @@ #version 330 core layout(location = 0) in vec3 v_position; +layout(location = 1) in vec3 v_direction; +layout(location = 2) in vec3 v_colour; +layout(location = 3) in float v_kq; +layout(location = 4) in float v_arc; +layout(location = 5) in mat4 model; +layout(location = 9) in ivec3 modelPos; -uniform vec3 v_direction; -uniform vec3 colour; -uniform float kq; -uniform float arc; uniform ivec3 viewPoint; -out vec3 position; -out vec3 direction; -out float size; -out float cosarc; +flat out ivec3 position; +flat out vec3 direction; +flat out float size; +flat out vec2 arc; // cos,tan (v_arc/2) +flat out vec3 colour; +flat out float kq; void main() { - position = v_position; - direction = normalize(v_direction); + position = modelPos + ivec3(mat3(model) * v_position); + direction = normalize(mat3(model) * v_direction); + colour = v_colour; + kq = v_kq; size = (8000 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); - cosarc = cos(arc / 2); + arc = vec2(cos(v_arc / 2), tan(v_arc / 2)); gl_Position = vec4(position - viewPoint, 0); } |