From c7af64b7061c59c987958d0830838f1c05caeb29 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 19 Jan 2024 00:23:56 +0000 Subject: Render rail network using new shaders Non-functional, totally unimplemented at this stage --- gfx/gl/sceneShader.cpp | 17 ++++++++++++----- gfx/gl/sceneShader.h | 1 + gfx/gl/shaders/network.fs | 6 ++++++ gfx/gl/shaders/networkCurve.gs | 9 +++++++++ gfx/gl/shaders/networkCurve.vs | 20 ++++++++++++++++++++ gfx/gl/shaders/networkStraight.gs | 15 +++++++++++++++ gfx/gl/shaders/networkStraight.vs | 20 ++++++++++++++++++++ 7 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 gfx/gl/shaders/network.fs create mode 100644 gfx/gl/shaders/networkCurve.gs create mode 100644 gfx/gl/shaders/networkCurve.vs create mode 100644 gfx/gl/shaders/networkStraight.gs create mode 100644 gfx/gl/shaders/networkStraight.vs (limited to 'gfx/gl') diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp index 1b3b27c..146e642 100644 --- a/gfx/gl/sceneShader.cpp +++ b/gfx/gl/sceneShader.cpp @@ -1,14 +1,19 @@ #include "sceneShader.h" #include #include +#include #include #include #include +#include +#include #include #include #include #include #include +#include +#include #include #include #include @@ -21,15 +26,17 @@ SceneShader::SceneShader() : basicInst {dynamicPointInst_vs, material_fs}, landmass {fixedPoint_vs, landmass_fs}, absolute {fixedPoint_vs, material_fs}, spotLightInst {spotLight_vs, spotLight_gs, spotLight_fs}, - pointLightInst {pointLight_vs, pointLight_gs, pointLight_fs} + pointLightInst {pointLight_vs, pointLight_gs, pointLight_fs}, + networkStraight {networkStraight_vs, networkStraight_gs, network_fs}, + networkCurve {networkCurve_vs, networkCurve_gs, network_fs} { } void SceneShader::setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const { - for (const auto & prog : std::initializer_list { - &basic, &basicInst, &water, &landmass, &absolute, &pointLightInst, &spotLightInst}) { + for (const auto & prog : std::initializer_list {&basic, &basicInst, &water, &landmass, + &absolute, &pointLightInst, &spotLightInst, &networkStraight, &networkCurve}) { prog->setViewProjection(viewPoint, viewProjection); } } @@ -37,8 +44,8 @@ SceneShader::setViewProjection(const GlobalPosition3D & viewPoint, const glm::ma void SceneShader::setViewPort(const ViewPort & viewPort) const { - for (const auto & prog : std::initializer_list { - &basic, &basicInst, &water, &landmass, &absolute, &pointLightInst, &spotLightInst}) { + for (const auto & prog : std::initializer_list {&basic, &basicInst, &water, &landmass, + &absolute, &pointLightInst, &spotLightInst, &networkStraight, &networkCurve}) { prog->setViewPort(viewPort); } } diff --git a/gfx/gl/sceneShader.h b/gfx/gl/sceneShader.h index 813c1bf..c86d157 100644 --- a/gfx/gl/sceneShader.h +++ b/gfx/gl/sceneShader.h @@ -57,6 +57,7 @@ public: BasicProgram basic; WaterProgram water; AbsolutePosProgram basicInst, landmass, absolute, spotLightInst, pointLightInst; + AbsolutePosProgram networkStraight, networkCurve; void setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const; void setViewPort(const ViewPort & viewPort) const; diff --git a/gfx/gl/shaders/network.fs b/gfx/gl/shaders/network.fs new file mode 100644 index 0000000..d427da2 --- /dev/null +++ b/gfx/gl/shaders/network.fs @@ -0,0 +1,6 @@ +#version 330 core + +void +main() +{ +} diff --git a/gfx/gl/shaders/networkCurve.gs b/gfx/gl/shaders/networkCurve.gs new file mode 100644 index 0000000..cf70b6e --- /dev/null +++ b/gfx/gl/shaders/networkCurve.gs @@ -0,0 +1,9 @@ +#version 330 core + +layout(points) in; +layout(triangle_strip, max_vertices = 255) out; + +void +main() +{ +} diff --git a/gfx/gl/shaders/networkCurve.vs b/gfx/gl/shaders/networkCurve.vs new file mode 100644 index 0000000..b4324d6 --- /dev/null +++ b/gfx/gl/shaders/networkCurve.vs @@ -0,0 +1,20 @@ +#version 330 core + +layout(location = 0) in ivec3 v_apos; +layout(location = 1) in ivec3 v_bpos; +layout(location = 2) in ivec3 v_centre; +layout(location = 3) in float v_reps; + +uniform mat4 viewProjection; + +flat out ivec3 apos; +flat out ivec3 bpos; +flat out mat2 rot; +flat out float reps; +flat out float dist; + +void +main() +{ + dist = viewProjection[0][0]; +} diff --git a/gfx/gl/shaders/networkStraight.gs b/gfx/gl/shaders/networkStraight.gs new file mode 100644 index 0000000..c2bfe3f --- /dev/null +++ b/gfx/gl/shaders/networkStraight.gs @@ -0,0 +1,15 @@ +#version 330 core + +flat in ivec3 apos[]; +flat in ivec3 bpos[]; +flat in mat2 rot[]; +flat in float reps[]; +flat in float dist[]; + +layout(points) in; +layout(triangle_strip, max_vertices = 20) out; + +void +main() +{ +} diff --git a/gfx/gl/shaders/networkStraight.vs b/gfx/gl/shaders/networkStraight.vs new file mode 100644 index 0000000..025ae8f --- /dev/null +++ b/gfx/gl/shaders/networkStraight.vs @@ -0,0 +1,20 @@ +#version 330 core + +layout(location = 0) in ivec3 v_apos; +layout(location = 1) in ivec3 v_bpos; +layout(location = 2) in mat2 v_rot; +layout(location = 4) in float v_reps; + +uniform mat4 viewProjection; + +flat out ivec3 apos; +flat out ivec3 bpos; +flat out mat2 rot; +flat out float reps; +flat out float dist; + +void +main() +{ + dist = viewProjection[0][0]; +} -- cgit v1.2.3 From 7f852de673bb687c5721c6a74367278f8ba62f56 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jan 2024 12:05:53 +0000 Subject: Implement complete network straight part shader --- gfx/gl/shaders/network.fs | 11 +++++++++++ gfx/gl/shaders/networkStraight.gs | 38 +++++++++++++++++++++++++++++++++++++- gfx/gl/shaders/networkStraight.vs | 8 ++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shaders/network.fs b/gfx/gl/shaders/network.fs index d427da2..e7d15b3 100644 --- a/gfx/gl/shaders/network.fs +++ b/gfx/gl/shaders/network.fs @@ -1,6 +1,17 @@ #version 330 core +#extension GL_ARB_shading_language_420pack : enable + +include(`materialOut.glsl') +in vec3 rposition; +in vec2 texCoord; + +layout(binding = 0) uniform sampler2D texture0; +uniform ivec3 viewPoint; void main() { + gPosition = ivec4(viewPoint + rposition, 0); + gNormal = vec4(0, 0, 1, 1); + gAlbedoSpec = texture(texture0, texCoord); } diff --git a/gfx/gl/shaders/networkStraight.gs b/gfx/gl/shaders/networkStraight.gs index c2bfe3f..ccd456a 100644 --- a/gfx/gl/shaders/networkStraight.gs +++ b/gfx/gl/shaders/networkStraight.gs @@ -1,5 +1,14 @@ #version 330 core +const float RAIL_HEIGHT = 250; +const vec3[] profile = vec3[]( // + vec3(-1900.F, 0.F, 0.F), // + vec3(-608.F, 0.F, RAIL_HEIGHT), // + vec3(0, 0.F, RAIL_HEIGHT * .7F), // + vec3(608.F, 0.F, RAIL_HEIGHT), // + vec3(1900.F, 0.F, 0.F)); +const float[profile.length()] texturePos = float[](0, 0.34, 0.5, 0.65, 1); + flat in ivec3 apos[]; flat in ivec3 bpos[]; flat in mat2 rot[]; @@ -7,9 +16,36 @@ flat in float reps[]; flat in float dist[]; layout(points) in; -layout(triangle_strip, max_vertices = 20) out; +layout(triangle_strip, max_vertices = 10) out; + +uniform mat4 viewProjection; +uniform ivec3 viewPoint; + +uniform int clipDistance = 5000000; +uniform int flatDistance = 1000000; + +out vec2 texCoord; +out vec3 rposition; + +void +doEnd(const ivec3 end, int v, float texY) +{ + rposition = vec3(rot[0] * profile[v].xy, profile[v].z); + ivec3 vpos = end + ivec3(rposition); + gl_Position = viewProjection * vec4(vpos - viewPoint, 1); + texCoord = vec2(texturePos[v], texY); + EmitVertex(); +} void main() { + if (dist[0] < clipDistance) { + int vstep = (dist[0] < flatDistance) ? 1 : profile.length() - 1; + for (int v = 0; v < profile.length(); v += vstep) { + doEnd(bpos[0], v, reps[0]); + doEnd(apos[0], v, 0); + } + EndPrimitive(); + } } diff --git a/gfx/gl/shaders/networkStraight.vs b/gfx/gl/shaders/networkStraight.vs index 025ae8f..a0fa437 100644 --- a/gfx/gl/shaders/networkStraight.vs +++ b/gfx/gl/shaders/networkStraight.vs @@ -5,7 +5,7 @@ layout(location = 1) in ivec3 v_bpos; layout(location = 2) in mat2 v_rot; layout(location = 4) in float v_reps; -uniform mat4 viewProjection; +uniform ivec3 viewPoint; flat out ivec3 apos; flat out ivec3 bpos; @@ -16,5 +16,9 @@ flat out float dist; void main() { - dist = viewProjection[0][0]; + apos = v_apos; + bpos = v_bpos; + rot = v_rot; + reps = v_reps; + dist = min(distance(viewPoint, v_apos), distance(viewPoint, v_bpos)); } -- cgit v1.2.3 From d1d1a4e349f18cd36406f75170dad87fbad2722a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jan 2024 13:00:49 +0000 Subject: Common code between straight/curve network geometry shaders --- gfx/gl/shaders/networkCommon.glsl | 41 +++++++++++++++++++++++++++++++++++++++ gfx/gl/shaders/networkCurve.gs | 10 ++++++++++ gfx/gl/shaders/networkCurve.vs | 10 +++++----- gfx/gl/shaders/networkStraight.gs | 38 ++---------------------------------- 4 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 gfx/gl/shaders/networkCommon.glsl (limited to 'gfx/gl') diff --git a/gfx/gl/shaders/networkCommon.glsl b/gfx/gl/shaders/networkCommon.glsl new file mode 100644 index 0000000..2639eb1 --- /dev/null +++ b/gfx/gl/shaders/networkCommon.glsl @@ -0,0 +1,41 @@ +const float RAIL_HEIGHT = 250; +const vec3[] profile = vec3[]( // + vec3(-1900.F, 0.F, 0.F), // + vec3(-608.F, 0.F, RAIL_HEIGHT), // + vec3(0, 0.F, RAIL_HEIGHT * .7F), // + vec3(608.F, 0.F, RAIL_HEIGHT), // + vec3(1900.F, 0.F, 0.F)); +const float[profile.length()] texturePos = float[](0, 0.34, 0.5, 0.65, 1); + +uniform mat4 viewProjection; +uniform ivec3 viewPoint; + +uniform float clipDistance = 5000000; +uniform float flatDistance = 1000000; + +out vec2 texCoord; +out vec3 rposition; + +void +doVertex(const ivec3 end, const int v, const float texY, const mat2 rot) +{ + rposition = vec3(rot * profile[v].xy, profile[v].z); + ivec3 vpos = end + ivec3(rposition); + gl_Position = viewProjection * vec4(vpos - viewPoint, 1); + texCoord = vec2(texturePos[v], texY); + EmitVertex(); +} + +void +doSeg(const float dist, const ivec3 apos, const ivec3 bpos, const float atexY, const float btexY, const mat2 arot, + const mat2 brot) +{ + if (dist < clipDistance) { + int vstep = (dist < flatDistance) ? 1 : profile.length() - 1; + for (int v = 0; v < profile.length(); v += vstep) { + doVertex(bpos, v, btexY, brot); + doVertex(apos, v, atexY, arot); + } + EndPrimitive(); + } +} diff --git a/gfx/gl/shaders/networkCurve.gs b/gfx/gl/shaders/networkCurve.gs index cf70b6e..c504df3 100644 --- a/gfx/gl/shaders/networkCurve.gs +++ b/gfx/gl/shaders/networkCurve.gs @@ -1,9 +1,19 @@ #version 330 core +flat in ivec3 apos[]; +flat in ivec3 bpos[]; +flat in ivec3 cpos[]; +flat in float reps[]; + layout(points) in; layout(triangle_strip, max_vertices = 255) out; +const mat2 rot = mat2(1); + +include(`networkCommon.glsl') + void main() { + doSeg(0, apos[0], bpos[0], 0.f, reps[0], rot, rot); } diff --git a/gfx/gl/shaders/networkCurve.vs b/gfx/gl/shaders/networkCurve.vs index b4324d6..d083765 100644 --- a/gfx/gl/shaders/networkCurve.vs +++ b/gfx/gl/shaders/networkCurve.vs @@ -5,16 +5,16 @@ layout(location = 1) in ivec3 v_bpos; layout(location = 2) in ivec3 v_centre; layout(location = 3) in float v_reps; -uniform mat4 viewProjection; - flat out ivec3 apos; flat out ivec3 bpos; -flat out mat2 rot; +flat out ivec3 cpos; flat out float reps; -flat out float dist; void main() { - dist = viewProjection[0][0]; + apos = v_apos; + bpos = v_bpos; + cpos = v_centre; + reps = v_reps; } diff --git a/gfx/gl/shaders/networkStraight.gs b/gfx/gl/shaders/networkStraight.gs index ccd456a..51df5fb 100644 --- a/gfx/gl/shaders/networkStraight.gs +++ b/gfx/gl/shaders/networkStraight.gs @@ -1,14 +1,5 @@ #version 330 core -const float RAIL_HEIGHT = 250; -const vec3[] profile = vec3[]( // - vec3(-1900.F, 0.F, 0.F), // - vec3(-608.F, 0.F, RAIL_HEIGHT), // - vec3(0, 0.F, RAIL_HEIGHT * .7F), // - vec3(608.F, 0.F, RAIL_HEIGHT), // - vec3(1900.F, 0.F, 0.F)); -const float[profile.length()] texturePos = float[](0, 0.34, 0.5, 0.65, 1); - flat in ivec3 apos[]; flat in ivec3 bpos[]; flat in mat2 rot[]; @@ -17,35 +8,10 @@ flat in float dist[]; layout(points) in; layout(triangle_strip, max_vertices = 10) out; - -uniform mat4 viewProjection; -uniform ivec3 viewPoint; - -uniform int clipDistance = 5000000; -uniform int flatDistance = 1000000; - -out vec2 texCoord; -out vec3 rposition; - -void -doEnd(const ivec3 end, int v, float texY) -{ - rposition = vec3(rot[0] * profile[v].xy, profile[v].z); - ivec3 vpos = end + ivec3(rposition); - gl_Position = viewProjection * vec4(vpos - viewPoint, 1); - texCoord = vec2(texturePos[v], texY); - EmitVertex(); -} +include(`networkCommon.glsl') void main() { - if (dist[0] < clipDistance) { - int vstep = (dist[0] < flatDistance) ? 1 : profile.length() - 1; - for (int v = 0; v < profile.length(); v += vstep) { - doEnd(bpos[0], v, reps[0]); - doEnd(apos[0], v, 0); - } - EndPrimitive(); - } + doSeg(dist[0], apos[0], bpos[0], 0.f, reps[0], rot[0], rot[0]); } -- cgit v1.2.3 From a19011b730ac6d770e3b42cde3a10961495a417d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jan 2024 20:10:12 +0000 Subject: Implement basic network curve part shader --- gfx/gl/shaders/networkCommon.glsl | 45 ++++++++++++++++++++++----------------- gfx/gl/shaders/networkCurve.gs | 29 ++++++++++++++++++++++++- gfx/gl/shaders/networkCurve.vs | 6 ++++++ gfx/gl/shaders/networkStraight.vs | 6 +++--- 4 files changed, 63 insertions(+), 23 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shaders/networkCommon.glsl b/gfx/gl/shaders/networkCommon.glsl index 2639eb1..e1811bc 100644 --- a/gfx/gl/shaders/networkCommon.glsl +++ b/gfx/gl/shaders/networkCommon.glsl @@ -16,26 +16,33 @@ uniform float flatDistance = 1000000; out vec2 texCoord; out vec3 rposition; -void -doVertex(const ivec3 end, const int v, const float texY, const mat2 rot) +float +segDist(const ivec3 a, const ivec3 b) { - rposition = vec3(rot * profile[v].xy, profile[v].z); - ivec3 vpos = end + ivec3(rposition); - gl_Position = viewProjection * vec4(vpos - viewPoint, 1); - texCoord = vec2(texturePos[v], texY); - EmitVertex(); + return min(distance(viewPoint, a), distance(viewPoint, b)); } -void -doSeg(const float dist, const ivec3 apos, const ivec3 bpos, const float atexY, const float btexY, const mat2 arot, - const mat2 brot) -{ - if (dist < clipDistance) { - int vstep = (dist < flatDistance) ? 1 : profile.length() - 1; - for (int v = 0; v < profile.length(); v += vstep) { - doVertex(bpos, v, btexY, brot); - doVertex(apos, v, atexY, arot); +ifelse( + TYPE, .gs, + // Begin: Geometry shader only function + void doVertex(const ivec3 end, const int v, const float texY, const mat2 rot) { + rposition = vec3(rot * profile[v].xy, profile[v].z); + ivec3 vpos = end + ivec3(rposition); + gl_Position = viewProjection * vec4(vpos - viewPoint, 1); + texCoord = vec2(texturePos[v], texY); + EmitVertex(); } - EndPrimitive(); - } -} + + void doSeg(const float dist, const ivec3 apos, const ivec3 bpos, const float atexY, const float btexY, + const mat2 arot, const mat2 brot) { + if (dist < clipDistance) { + int vstep = (dist < flatDistance) ? 1 : profile.length() - 1; + for (int v = 0; v < profile.length(); v += vstep) { + doVertex(bpos, v, btexY, brot); + doVertex(apos, v, atexY, arot); + } + EndPrimitive(); + } + } + // End: Geometry shader only function +) diff --git a/gfx/gl/shaders/networkCurve.gs b/gfx/gl/shaders/networkCurve.gs index c504df3..82d8c59 100644 --- a/gfx/gl/shaders/networkCurve.gs +++ b/gfx/gl/shaders/networkCurve.gs @@ -4,6 +4,8 @@ flat in ivec3 apos[]; flat in ivec3 bpos[]; flat in ivec3 cpos[]; flat in float reps[]; +flat in float aangle[]; +flat in float bangle[]; layout(points) in; layout(triangle_strip, max_vertices = 255) out; @@ -12,8 +14,33 @@ const mat2 rot = mat2(1); include(`networkCommon.glsl') +mat2 +getRot(float angle) +{ + return mat2(cos(angle), sin(angle), -sin(angle), cos(angle)); +} + void main() { - doSeg(0, apos[0], bpos[0], 0.f, reps[0], rot, rot); + float segs = floor(255 / (profile.length() * 2)); + float radius = distance(cpos[0], apos[0]); + vec3 arcstep = vec3((bangle[0] - aangle[0]), // angle + reps[0], // texture + (bpos[0].z - apos[0].z)) // height + / segs; + + ivec3 prevPos = apos[0]; + mat2 prevRot = getRot(aangle[0]); + float prevTex = 0; + for (vec3 arc = arcstep; arc.y < reps[0] - 0.01; arc += arcstep) { + mat2 rot = getRot(arc.x + aangle[0]); + ivec3 pos = cpos[0] + ivec3(rot * vec2(radius, 0), arc.z); + float tex = arc.y; + doSeg(segDist(prevPos, pos), pos, prevPos, tex, prevTex, rot, prevRot); + prevPos = pos; + prevRot = rot; + prevTex = tex; + } + doSeg(segDist(prevPos, bpos[0]), bpos[0], prevPos, reps[0], prevTex, getRot(bangle[0]), prevRot); } diff --git a/gfx/gl/shaders/networkCurve.vs b/gfx/gl/shaders/networkCurve.vs index d083765..6c56e93 100644 --- a/gfx/gl/shaders/networkCurve.vs +++ b/gfx/gl/shaders/networkCurve.vs @@ -4,11 +4,15 @@ layout(location = 0) in ivec3 v_apos; layout(location = 1) in ivec3 v_bpos; layout(location = 2) in ivec3 v_centre; layout(location = 3) in float v_reps; +layout(location = 4) in float v_aangle; +layout(location = 5) in float v_bangle; flat out ivec3 apos; flat out ivec3 bpos; flat out ivec3 cpos; flat out float reps; +flat out float aangle; +flat out float bangle; void main() @@ -17,4 +21,6 @@ main() bpos = v_bpos; cpos = v_centre; reps = v_reps; + aangle = v_aangle; + bangle = v_bangle; } diff --git a/gfx/gl/shaders/networkStraight.vs b/gfx/gl/shaders/networkStraight.vs index a0fa437..55f9c4f 100644 --- a/gfx/gl/shaders/networkStraight.vs +++ b/gfx/gl/shaders/networkStraight.vs @@ -5,14 +5,14 @@ layout(location = 1) in ivec3 v_bpos; layout(location = 2) in mat2 v_rot; layout(location = 4) in float v_reps; -uniform ivec3 viewPoint; - flat out ivec3 apos; flat out ivec3 bpos; flat out mat2 rot; flat out float reps; flat out float dist; +include(`networkCommon.glsl') + void main() { @@ -20,5 +20,5 @@ main() bpos = v_bpos; rot = v_rot; reps = v_reps; - dist = min(distance(viewPoint, v_apos), distance(viewPoint, v_bpos)); + dist = segDist(v_apos, v_bpos); } -- cgit v1.2.3 From 35a9f035963458156c719dc16f4073b8244d66eb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jan 2024 23:27:01 +0000 Subject: Pass curve link radius, no recalculate, its constant --- gfx/gl/shaders/networkCurve.gs | 4 ++-- gfx/gl/shaders/networkCurve.vs | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shaders/networkCurve.gs b/gfx/gl/shaders/networkCurve.gs index 82d8c59..d373c0c 100644 --- a/gfx/gl/shaders/networkCurve.gs +++ b/gfx/gl/shaders/networkCurve.gs @@ -6,6 +6,7 @@ flat in ivec3 cpos[]; flat in float reps[]; flat in float aangle[]; flat in float bangle[]; +flat in float radius[]; layout(points) in; layout(triangle_strip, max_vertices = 255) out; @@ -24,7 +25,6 @@ void main() { float segs = floor(255 / (profile.length() * 2)); - float radius = distance(cpos[0], apos[0]); vec3 arcstep = vec3((bangle[0] - aangle[0]), // angle reps[0], // texture (bpos[0].z - apos[0].z)) // height @@ -35,7 +35,7 @@ main() float prevTex = 0; for (vec3 arc = arcstep; arc.y < reps[0] - 0.01; arc += arcstep) { mat2 rot = getRot(arc.x + aangle[0]); - ivec3 pos = cpos[0] + ivec3(rot * vec2(radius, 0), arc.z); + ivec3 pos = cpos[0] + ivec3(rot * vec2(radius[0], 0), arc.z); float tex = arc.y; doSeg(segDist(prevPos, pos), pos, prevPos, tex, prevTex, rot, prevRot); prevPos = pos; diff --git a/gfx/gl/shaders/networkCurve.vs b/gfx/gl/shaders/networkCurve.vs index 6c56e93..f51bb87 100644 --- a/gfx/gl/shaders/networkCurve.vs +++ b/gfx/gl/shaders/networkCurve.vs @@ -6,6 +6,7 @@ layout(location = 2) in ivec3 v_centre; layout(location = 3) in float v_reps; layout(location = 4) in float v_aangle; layout(location = 5) in float v_bangle; +layout(location = 6) in float v_radius; flat out ivec3 apos; flat out ivec3 bpos; @@ -13,6 +14,7 @@ flat out ivec3 cpos; flat out float reps; flat out float aangle; flat out float bangle; +flat out float radius; void main() @@ -23,4 +25,5 @@ main() reps = v_reps; aangle = v_aangle; bangle = v_bangle; + radius = v_radius; } -- cgit v1.2.3 From d4ae68b6ba5211c7bc11a52951c466c5050ae377 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jan 2024 01:34:10 +0000 Subject: String view/constexpr Shader instances Gonna need more constexpr stuff and strstr/strlen aren't that --- gfx/gl/shader.cpp | 5 ++++- gfx/gl/shader.h | 6 ++---- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index 5f83b83..285c2c3 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -7,7 +7,10 @@ Shader::ShaderRef Shader::compile() const { ShaderRef shader {type}; - glShaderSource(shader, 1, &text, &len); + auto source = [&shader](auto text, GLint len) { + glShaderSource(shader, 1, &text, &len); + }; + source(text.data(), static_cast(text.length())); glCompileShader(shader); CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!"); diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index cff2281..cc1bbc0 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -2,20 +2,18 @@ #include #include -#include #include class Shader { public: using ShaderRef = glRef; - constexpr Shader(const GLchar * text, GLint len, GLuint type) : text {text}, len {len}, type {type} { } + constexpr Shader(const GLchar * text, GLuint type) : text {text}, type {type} { } [[nodiscard]] ShaderRef compile() const; static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage); private: - const GLchar * text; - GLint len; + const std::basic_string_view text; GLuint type; }; -- cgit v1.2.3 From 979c226bdcd7904db5bd23dfbf526de4c040f436 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jan 2024 02:39:30 +0000 Subject: Look for and replace GL_XXX with fixed string --- gfx/gl/shader.cpp | 11 ++++++++++- gfx/gl/shader.h | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index 285c2c3..b57dcd4 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -10,7 +10,16 @@ Shader::compile() const auto source = [&shader](auto text, GLint len) { glShaderSource(shader, 1, &text, &len); }; - source(text.data(), static_cast(text.length())); + if (lookups) { + std::basic_string textMod {text}; + for (const auto & match : ctre::range(textMod)) { + textMod.replace(match.begin(), match.end(), "255"); + } + source(textMod.c_str(), static_cast(textMod.length())); + } + else { + source(text.data(), static_cast(text.length())); + } glCompileShader(shader); CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!"); diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index cc1bbc0..c6b45af 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -3,12 +3,16 @@ #include #include #include +#include class Shader { public: using ShaderRef = glRef; - constexpr Shader(const GLchar * text, GLuint type) : text {text}, type {type} { } + constexpr Shader(const GLchar * text, GLuint type) : + text {text}, type {type}, lookups {ctre::search(this->text)} + { + } [[nodiscard]] ShaderRef compile() const; static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage); @@ -16,4 +20,5 @@ public: private: const std::basic_string_view text; GLuint type; + bool lookups; }; -- cgit v1.2.3 From 2368cde54675f39e9b5a99880124503b3ae391e1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jan 2024 03:01:44 +0000 Subject: Replace tokens found with values from getIntegerv --- gfx/gl/shader.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index b57dcd4..0f75817 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -1,8 +1,24 @@ #include "shader.h" +#include #include #include #include +namespace { + auto + getInt(GLenum e) + { + GLint i {}; + glGetIntegerv(e, &i); + return std::to_string(i); + } + + using LookUpFunction = std::string (*)(GLenum); + constexpr std::array, 1> LOOKUPS {{ + {"GL_MAX_GEOMETRY_OUTPUT_VERTICES", GL_MAX_GEOMETRY_OUTPUT_VERTICES, getInt}, + }}; +} + Shader::ShaderRef Shader::compile() const { @@ -13,7 +29,14 @@ Shader::compile() const if (lookups) { std::basic_string textMod {text}; for (const auto & match : ctre::range(textMod)) { - textMod.replace(match.begin(), match.end(), "255"); + if (const auto lookup = std::find_if(LOOKUPS.begin(), LOOKUPS.end(), + [&match](const auto & lookup) { + return std::get(lookup) == match; + }); + lookup != LOOKUPS.end()) { + const auto & [name, pname, getFunction] = *lookup; + textMod.replace(match.begin(), match.end(), getFunction(pname)); + } } source(textMod.c_str(), static_cast(textMod.length())); } -- cgit v1.2.3 From 7ce3368e918b085ef8894fb7aa68e36b07984b1d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jan 2024 03:04:57 +0000 Subject: Use symbol GL_MAX_GEOMETRY_OUTPUT_VERTICES to lookup the implementation limit at runtime --- gfx/gl/shaders/networkCurve.gs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shaders/networkCurve.gs b/gfx/gl/shaders/networkCurve.gs index d373c0c..326e8b6 100644 --- a/gfx/gl/shaders/networkCurve.gs +++ b/gfx/gl/shaders/networkCurve.gs @@ -9,7 +9,7 @@ flat in float bangle[]; flat in float radius[]; layout(points) in; -layout(triangle_strip, max_vertices = 255) out; +layout(triangle_strip, max_vertices = GL_MAX_GEOMETRY_OUTPUT_VERTICES) out; const mat2 rot = mat2(1); @@ -24,7 +24,7 @@ getRot(float angle) void main() { - float segs = floor(255 / (profile.length() * 2)); + float segs = floor(GL_MAX_GEOMETRY_OUTPUT_VERTICES / (profile.length() * 2)); vec3 arcstep = vec3((bangle[0] - aangle[0]), // angle reps[0], // texture (bpos[0].z - apos[0].z)) // height -- cgit v1.2.3 From fb3e42e2d13138865d5eb40a0f65ef14e67f995b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jan 2024 03:27:01 +0000 Subject: Dynamic number of segments into curve render Simpler formula than the old one, neither scientific, needs to tests to demonstrate a decent result for whatever formula we use. --- gfx/gl/shaders/networkCurve.gs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shaders/networkCurve.gs b/gfx/gl/shaders/networkCurve.gs index 326e8b6..619625f 100644 --- a/gfx/gl/shaders/networkCurve.gs +++ b/gfx/gl/shaders/networkCurve.gs @@ -24,7 +24,8 @@ getRot(float angle) void main() { - float segs = floor(GL_MAX_GEOMETRY_OUTPUT_VERTICES / (profile.length() * 2)); + float segs = clamp( + round(reps[0] * radius[0] / 1000), 4, floor(GL_MAX_GEOMETRY_OUTPUT_VERTICES / (profile.length() * 2))); vec3 arcstep = vec3((bangle[0] - aangle[0]), // angle reps[0], // texture (bpos[0].z - apos[0].z)) // height -- cgit v1.2.3 From 961f69d8cda0364c04ec98efc70a6f21e0a091e6 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 22 Jan 2024 02:25:47 +0000 Subject: Bind the network profile in as uniforms Makes the network shaders generic to network type --- gfx/gl/sceneShader.cpp | 17 +++++++++++++++++ gfx/gl/sceneShader.h | 14 ++++++++++++-- gfx/gl/shaders/networkCommon.glsl | 17 ++++++----------- gfx/gl/shaders/networkCurve.gs | 2 +- 4 files changed, 36 insertions(+), 14 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp index 146e642..bc64a88 100644 --- a/gfx/gl/sceneShader.cpp +++ b/gfx/gl/sceneShader.cpp @@ -86,6 +86,23 @@ SceneShader::BasicProgram::use(Location const & location) const setModel(location); } +template +SceneShader::NetworkProgram::NetworkProgram(S &&... s) : + AbsolutePosProgram {std::forward(s)...}, profileLoc {*this, "profile"}, texturePosLoc {*this, "texturePos"}, + profileLengthLoc {*this, "profileLength"} +{ +} + +void +SceneShader::NetworkProgram::use( + const std::span profile, const std::span texturePos) const +{ + Program::use(); + glUniform(profileLoc, profile); + glUniform(texturePosLoc, texturePos); + glUniform(profileLengthLoc, static_cast(profile.size())); +} + SceneShader::WaterProgram::WaterProgram() : SceneProgram {water_vs, water_fs}, waveLoc {*this, "waves"} { } void diff --git a/gfx/gl/sceneShader.h b/gfx/gl/sceneShader.h index c86d157..8621442 100644 --- a/gfx/gl/sceneShader.h +++ b/gfx/gl/sceneShader.h @@ -3,6 +3,7 @@ #include "config/types.h" #include "program.h" #include +#include class Location; @@ -41,8 +42,17 @@ class SceneShader { using SceneProgram::SceneProgram; }; - class WaterProgram : public SceneProgram { + class NetworkProgram : public AbsolutePosProgram { public: + template explicit NetworkProgram(S &&...); + + void use(const std::span, const std::span) const; + + private: + RequiredUniformLocation profileLoc, texturePosLoc, profileLengthLoc; + }; + + class WaterProgram : public SceneProgram { public: WaterProgram(); void use(float waveCycle) const; @@ -57,7 +67,7 @@ public: BasicProgram basic; WaterProgram water; AbsolutePosProgram basicInst, landmass, absolute, spotLightInst, pointLightInst; - AbsolutePosProgram networkStraight, networkCurve; + NetworkProgram networkStraight, networkCurve; void setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const; void setViewPort(const ViewPort & viewPort) const; diff --git a/gfx/gl/shaders/networkCommon.glsl b/gfx/gl/shaders/networkCommon.glsl index e1811bc..7b55cad 100644 --- a/gfx/gl/shaders/networkCommon.glsl +++ b/gfx/gl/shaders/networkCommon.glsl @@ -1,11 +1,6 @@ -const float RAIL_HEIGHT = 250; -const vec3[] profile = vec3[]( // - vec3(-1900.F, 0.F, 0.F), // - vec3(-608.F, 0.F, RAIL_HEIGHT), // - vec3(0, 0.F, RAIL_HEIGHT * .7F), // - vec3(608.F, 0.F, RAIL_HEIGHT), // - vec3(1900.F, 0.F, 0.F)); -const float[profile.length()] texturePos = float[](0, 0.34, 0.5, 0.65, 1); +uniform vec3[10] profile; +uniform float[10] texturePos; +uniform uint profileLength; uniform mat4 viewProjection; uniform ivec3 viewPoint; @@ -25,7 +20,7 @@ segDist(const ivec3 a, const ivec3 b) ifelse( TYPE, .gs, // Begin: Geometry shader only function - void doVertex(const ivec3 end, const int v, const float texY, const mat2 rot) { + void doVertex(const ivec3 end, const uint v, const float texY, const mat2 rot) { rposition = vec3(rot * profile[v].xy, profile[v].z); ivec3 vpos = end + ivec3(rposition); gl_Position = viewProjection * vec4(vpos - viewPoint, 1); @@ -36,8 +31,8 @@ ifelse( void doSeg(const float dist, const ivec3 apos, const ivec3 bpos, const float atexY, const float btexY, const mat2 arot, const mat2 brot) { if (dist < clipDistance) { - int vstep = (dist < flatDistance) ? 1 : profile.length() - 1; - for (int v = 0; v < profile.length(); v += vstep) { + uint vstep = (dist < flatDistance) ? 1u : profileLength - 1u; + for (uint v = 0u; v < profileLength; v += vstep) { doVertex(bpos, v, btexY, brot); doVertex(apos, v, atexY, arot); } diff --git a/gfx/gl/shaders/networkCurve.gs b/gfx/gl/shaders/networkCurve.gs index 619625f..7cb6c42 100644 --- a/gfx/gl/shaders/networkCurve.gs +++ b/gfx/gl/shaders/networkCurve.gs @@ -25,7 +25,7 @@ void main() { float segs = clamp( - round(reps[0] * radius[0] / 1000), 4, floor(GL_MAX_GEOMETRY_OUTPUT_VERTICES / (profile.length() * 2))); + round(reps[0] * radius[0] / 1000), 4, floor(uint(GL_MAX_GEOMETRY_OUTPUT_VERTICES) / (profileLength * 2u))); vec3 arcstep = vec3((bangle[0] - aangle[0]), // angle reps[0], // texture (bpos[0].z - apos[0].z)) // height -- cgit v1.2.3 From 8391c63472641c67f59723d0a6706efff6fb17d4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 23 Jan 2024 23:50:46 +0000 Subject: Fix network population of position in gBuffer Adds some rails to the basic test highlighting broken shadows --- gfx/gl/shaders/network.fs | 2 +- gfx/gl/shaders/networkCommon.glsl | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'gfx/gl') diff --git a/gfx/gl/shaders/network.fs b/gfx/gl/shaders/network.fs index e7d15b3..4e347b4 100644 --- a/gfx/gl/shaders/network.fs +++ b/gfx/gl/shaders/network.fs @@ -11,7 +11,7 @@ uniform ivec3 viewPoint; void main() { - gPosition = ivec4(viewPoint + rposition, 0); + gPosition = ivec4(viewPoint + rposition, 1); gNormal = vec4(0, 0, 1, 1); gAlbedoSpec = texture(texture0, texCoord); } diff --git a/gfx/gl/shaders/networkCommon.glsl b/gfx/gl/shaders/networkCommon.glsl index 7b55cad..0bc3c1c 100644 --- a/gfx/gl/shaders/networkCommon.glsl +++ b/gfx/gl/shaders/networkCommon.glsl @@ -21,9 +21,9 @@ ifelse( TYPE, .gs, // Begin: Geometry shader only function void doVertex(const ivec3 end, const uint v, const float texY, const mat2 rot) { - rposition = vec3(rot * profile[v].xy, profile[v].z); - ivec3 vpos = end + ivec3(rposition); - gl_Position = viewProjection * vec4(vpos - viewPoint, 1); + ivec3 vpos = end + ivec3(rot * profile[v].xy, profile[v].z); + rposition = vpos - viewPoint; + gl_Position = viewProjection * vec4(rposition, 1); texCoord = vec2(texturePos[v], texY); EmitVertex(); } -- cgit v1.2.3