Simplify wall lighting shader

This commit is contained in:
2025-10-30 11:17:06 +00:00
parent c2e5aaa7a0
commit 71006e24fd
2 changed files with 23 additions and 15 deletions
+21 -14
View File
@@ -1,6 +1,7 @@
#version 450 core #version 450 core
layout(points) in; layout(points) in;
layout(triangle_strip, max_vertices = 18) out; //layout(triangle_strip, max_vertices = 18) out;
layout(triangle_strip, max_vertices = 9) out;
layout(std140, binding = 0) uniform TheMat { mat4 theMat; }; layout(std140, binding = 0) uniform TheMat { mat4 theMat; };
layout(location=0) uniform vec3 lightPos; layout(location=0) uniform vec3 lightPos;
layout(location=1) uniform float rad; layout(location=1) uniform float rad;
@@ -24,13 +25,17 @@ vec2 shift(vec2 p) {
float isLHS(vec2 startV, vec2 testV) { float isLHS(vec2 startV, vec2 testV) {
return sign(-startV.x * testV.y + startV.y * testV.x); return sign(-startV.x * testV.y + startV.y * testV.x);
} }
// construct a box with openings on bottom face and face away from wall // constructs a "bridge" shape
void main() { void main() {
vec2 p1 = gl_in[0].gl_Position.xy; vec2 p1 = gl_in[0].gl_Position.xy;
vec2 p2 = gl_in[0].gl_Position.zw; vec2 p2 = gl_in[0].gl_Position.zw;
vec2 closepoint = closestPointOnSeg(p1,p2,lightPos.xy); vec2 closepoint = closestPointOnSeg(p1,p2,lightPos.xy);
if (isLHS(p1 - lightPos.xy, p2 - lightPos.xy) < 0 && distance(closepoint,lightPos.xy) < rad) { if (isLHS(p1 - lightPos.xy, p2 - lightPos.xy) < 0 && distance(closepoint,lightPos.xy) < rad) {
// this is a point far along the ray from the light source to the midpoint
// of the wall. Necessary in case the light source is very close to the
// wall
vec2 closeshift = (rad * normalize(closepoint - lightPos.xy)) + lightPos.xy - closepoint; vec2 closeshift = (rad * normalize(closepoint - lightPos.xy)) + lightPos.xy - closepoint;
vec2 shifto = (rad * normalize(closepoint - lightPos.xy)) + closepoint;
vec2 p1cs = p1 + closeshift; vec2 p1cs = p1 + closeshift;
vec2 p1o = vec2( distance(p1,lightPos.xy) > rad ? p1cs : shift(p1) ); vec2 p1o = vec2( distance(p1,lightPos.xy) > rad ? p1cs : shift(p1) );
vec2 p2cs = p2 + closeshift; vec2 p2cs = p2 + closeshift;
@@ -40,27 +45,29 @@ void main() {
gl_Position = theMat * vec4(p1,-500,1); EmitVertex(); gl_Position = theMat * vec4(p1,-500,1); EmitVertex();
gl_Position = theMat * vec4(p1o,100,1); EmitVertex(); gl_Position = theMat * vec4(p1o,100,1); EmitVertex();
gl_Position = theMat * vec4(p1,100,1); EmitVertex(); gl_Position = theMat * vec4(p1,100,1); EmitVertex();
gl_Position = theMat * vec4(p1cs,100,1); EmitVertex(); gl_Position = theMat * vec4(shifto,100,1); EmitVertex();
gl_Position = theMat * vec4(p2,100,1); EmitVertex(); gl_Position = theMat * vec4(p2,100,1); EmitVertex();
gl_Position = theMat * vec4(p2cs,100,1); EmitVertex();
gl_Position = theMat * vec4(p2o,100,1); EmitVertex(); gl_Position = theMat * vec4(p2o,100,1); EmitVertex();
gl_Position = theMat * vec4(p2,-500,1); EmitVertex();
gl_Position = theMat * vec4(p2o,-500,1); EmitVertex();
EndPrimitive(); EndPrimitive();
// be carefull with the winding...
// gl_Position = theMat * vec4(p2o,100,1); EmitVertex(); // gl_Position = theMat * vec4(p2o,100,1); EmitVertex();
// gl_Position = theMat * vec4(p2,100,1); EmitVertex(); // gl_Position = theMat * vec4(p2,100,1); EmitVertex();
// gl_Position = theMat * vec4(p2o,0,1); EmitVertex(); // gl_Position = theMat * vec4(p2o,0,1); EmitVertex();
// gl_Position = theMat * vec4(p2,0,1); EmitVertex(); // gl_Position = theMat * vec4(p2,0,1); EmitVertex();
gl_Position = theMat * vec4(p2,100,1); EmitVertex(); /// gl_Position = theMat * vec4(p2,100,1); EmitVertex();
gl_Position = theMat * vec4(p2,-500,1); EmitVertex(); /// gl_Position = theMat * vec4(p2,-500,1); EmitVertex();
gl_Position = theMat * vec4(p2o,100,1); EmitVertex(); /// gl_Position = theMat * vec4(p2o,100,1); EmitVertex();
gl_Position = theMat * vec4(p2o,-500,1); EmitVertex(); /// gl_Position = theMat * vec4(p2o,-500,1); EmitVertex();
gl_Position = theMat * vec4(p2cs,100,1); EmitVertex(); /// gl_Position = theMat * vec4(p2cs,100,1); EmitVertex();
gl_Position = theMat * vec4(p2cs,-500,1); EmitVertex(); /// gl_Position = theMat * vec4(p2cs,-500,1); EmitVertex();
gl_Position = theMat * vec4(p1cs,100,1); EmitVertex(); /// gl_Position = theMat * vec4(p1cs,100,1); EmitVertex();
gl_Position = theMat * vec4(p1cs,-500,1); EmitVertex(); /// gl_Position = theMat * vec4(p1cs,-500,1); EmitVertex();
gl_Position = theMat * vec4(p1o,100,1); EmitVertex(); /// gl_Position = theMat * vec4(p1o,100,1); EmitVertex();
gl_Position = theMat * vec4(p1o,-500,1); EmitVertex(); /// gl_Position = theMat * vec4(p1o,-500,1); EmitVertex();
EndPrimitive(); EndPrimitive();
} else { } else {
+2 -1
View File
@@ -128,6 +128,7 @@ doDrawing' win pdata u = do
GL_DEPTH GL_DEPTH
0 0
-- draw wall occlusions from the camera's point of view -- draw wall occlusions from the camera's point of view
glDisable GL_CULL_FACE
unless (debugOn Remove_LOS cfig) $ do unless (debugOn Remove_LOS cfig) $ do
glUseProgram (pdata ^. lightingWallShadShader . shaderUINT) glUseProgram (pdata ^. lightingWallShadShader . shaderUINT)
glProgramUniform3f glProgramUniform3f
@@ -139,7 +140,7 @@ doDrawing' win pdata u = do
glProgramUniform1f glProgramUniform1f
(pdata ^. lightingWallShadShader . shaderUINT) (pdata ^. lightingWallShadShader . shaderUINT)
1 1
1000 1000 -- this should be the furthest on screen distance from the viewpoint
glBindVertexArray $ pdata ^. lightingWallShadShader . shaderVAO . vaoName glBindVertexArray $ pdata ^. lightingWallShadShader . shaderVAO . vaoName
glDrawArrays glDrawArrays
(_unPrimitiveMode $ pdata ^. lightingWallShadShader . shaderPrimitive) (_unPrimitiveMode $ pdata ^. lightingWallShadShader . shaderPrimitive)