From b6feccd93401275a19b0e0e18760201f650418a8 Mon Sep 17 00:00:00 2001 From: jgk Date: Fri, 25 Jun 2021 01:19:20 +0200 Subject: [PATCH] Make light position uniform 3d --- shader/lighting/occlude.geom | 7 +++--- shader/lighting/wall.geom | 43 ++++----------------------------- src/Dodge/Creature/Inanimate.hs | 3 ++- src/Dodge/Data.hs | 4 +-- src/Dodge/Default/World.hs | 6 +++-- src/Dodge/LevelGen.hs | 4 +-- src/Dodge/LightSources.hs | 20 +++++++-------- src/Dodge/Render/Picture.hs | 6 ++--- src/Dodge/WorldEvent/Flash.hs | 9 ++++--- src/Picture/Render.hs | 9 +++---- 10 files changed, 40 insertions(+), 71 deletions(-) diff --git a/shader/lighting/occlude.geom b/shader/lighting/occlude.geom index 522e28235..f94f1f27c 100644 --- a/shader/lighting/occlude.geom +++ b/shader/lighting/occlude.geom @@ -2,11 +2,10 @@ layout (points) in; layout (triangle_strip, max_vertices = 11) out; layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ; -uniform vec2 lightPos; -vec2 lightPosa = ( theMat * vec4(lightPos,0.1,1) ).xy; +uniform vec3 lightPos; vec4 shift (vec4 p) -{ return vec4 (p.xy + (200 * (p.xy-lightPos)), 0.1 , 1); +{ return vec4 (p.xy + (200 * (p.xy-lightPos.xy)), 0.1 , 1); } float isLHS (vec2 startV, vec2 testV) { @@ -17,7 +16,7 @@ void main() { vec4 p1 = vec4 (gl_in[0].gl_Position.xy,0.1,1); vec4 p2 = vec4 (gl_in[0].gl_Position.zw,0.1,1); -if (isLHS (p1.xy - lightPos, p2.xy - lightPos) < 0) +if (isLHS (p1.xy - lightPos.xy, p2.xy - lightPos.xy) < 0) { vec4 p3 = vec4 (p1.xy,-0.5,1); vec4 p4 = vec4 (p2.xy,-0.5,1); diff --git a/shader/lighting/wall.geom b/shader/lighting/wall.geom index 903eec4d1..01d356dfe 100644 --- a/shader/lighting/wall.geom +++ b/shader/lighting/wall.geom @@ -2,22 +2,21 @@ layout (points) in; layout (triangle_strip, max_vertices = 4) out; layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ; +uniform vec3 lightPos; +uniform vec2 radLum; out vec3 dField; out float lum; -uniform vec2 lightPos; -uniform vec2 radLum; float isLHS (vec2 startV, vec2 testV) { return sign( -startV.x * testV.y + startV.y * testV.x); } vec4 shiftCloser (vec4 v) { return vec4 (v.xy , v.z-0.0001 , v.w) ; } - void main() { vec4 p1 = vec4 (gl_in[0].gl_Position.xy,0.1,1); vec4 p2 = vec4 (gl_in[0].gl_Position.zw,0.1,1); -if (isLHS (p1.xy - lightPos, p2.xy - lightPos) < 0) +if (isLHS (p1.xy - lightPos.xy, p2.xy - lightPos.xy) < 0) { lum = radLum.y; float rad = radLum.x; @@ -25,8 +24,8 @@ if (isLHS (p1.xy - lightPos, p2.xy - lightPos) < 0) vec4 p3 = vec4 (p1.xy,-0.3,1); vec4 p4 = vec4 (p2.xy,-0.3,1); - vec2 d1 = p1.xy - lightPos; - vec2 d2 = p2.xy - lightPos; + vec2 d1 = p1.xy - lightPos.xy; + vec2 d2 = p2.xy - lightPos.xy; vec4 a1 = shiftCloser( theMat * p1 ); vec4 a2 = shiftCloser( theMat * p2 ); @@ -49,35 +48,3 @@ if (isLHS (p1.xy - lightPos, p2.xy - lightPos) < 0) EndPrimitive(); } else {} } - -// // void main() -// // { -// // vec4 frontL4 = worldMat * vec4(gl_in[0].gl_Position.xy, 0 ,1); -// // vec4 frontR4 = worldMat * vec4(gl_in[0].gl_Position.zw, 0 ,1); -// // vec2 frontL = frontL4.xy; -// // vec2 frontR = frontR4.xy; -// // -// // emitLine (frontR); -// // emitLine (frontL); -// // EndPrimitive(); -// // } -// gl_Position = perspective * worldMat * p4; EmitVertex(); -// gl_Position = perspective * worldMat * p3; EmitVertex(); -// gl_Position = perspective * worldMat * p7; EmitVertex(); -// gl_Position = perspective * worldMat * p8; EmitVertex(); -// gl_Position = perspective * worldMat * p5; EmitVertex(); -// gl_Position = perspective * worldMat * p3; EmitVertex(); -// gl_Position = perspective * worldMat * p1; EmitVertex(); -// gl_Position = perspective * worldMat * p4; EmitVertex(); -// gl_Position = perspective * worldMat * p2; EmitVertex(); -// gl_Position = perspective * worldMat * p7; EmitVertex(); -// gl_Position = perspective * worldMat * p6; EmitVertex(); -// gl_Position = perspective * worldMat * p5; EmitVertex(); -//void emitLine (vec2 pa) -//{ -// gl_Position = vec4 (pa, 0, 1); -// EmitVertex(); -// gl_Position = shift (vec4 (pa,0,1)); //vec4 (pa + (200 * (pa - lightPosa)), 0 , 1); -// EmitVertex(); -//} - diff --git a/src/Dodge/Creature/Inanimate.hs b/src/Dodge/Creature/Inanimate.hs index 16fd70f14..eabc19e82 100644 --- a/src/Dodge/Creature/Inanimate.hs +++ b/src/Dodge/Creature/Inanimate.hs @@ -55,9 +55,10 @@ updateLamp i = unrandUpdate handleLS internalUpdate handleLS cr w | _crHP cr < 0 = explosionFlashAt cPos $ mkSoundBreakGlass cPos w & lightSources %~ IM.delete i - | otherwise = w & lightSources . ix i . lsPos .~ cPos + | otherwise = w & lightSources . ix i . lsPos .~ f cPos where cPos = _crPos cr + f (x,y) = (x,y,0) internalUpdate cr | _crHP cr < 0 = Nothing | otherwise = Just $ doDamage cr diff --git a/src/Dodge/Data.hs b/src/Dodge/Data.hs index 1dde79e20..e401c9fe4 100644 --- a/src/Dodge/Data.hs +++ b/src/Dodge/Data.hs @@ -130,13 +130,13 @@ data Cloud = Cloud } data LightSource = LS { _lsID :: !Int - , _lsPos :: !Point2 + , _lsPos :: !Point3 , _lsDir :: !Float , _lsRad :: !Float , _lsIntensity :: !Float } data TempLightSource = TLS - { _tlsPos :: !Point2 + { _tlsPos :: !Point3 , _tlsRad :: !Float , _tlsIntensity :: !Float , _tlsUpdate :: World -> TempLightSource -> (World, Maybe TempLightSource) diff --git a/src/Dodge/Default/World.hs b/src/Dodge/Default/World.hs index 562d3e9c2..821c1d27a 100644 --- a/src/Dodge/Default/World.hs +++ b/src/Dodge/Default/World.hs @@ -86,8 +86,10 @@ defaultDebugFlags = DebugFlags } youLight :: TempLightSource youLight = - TLS { _tlsPos = (0,0) + TLS { _tlsPos = (0,0,0) ,_tlsRad = 300 ,_tlsIntensity = 0.1 - ,_tlsUpdate = \w _ -> (w, Just (youLight {_tlsPos = _crPos (you w)})) + ,_tlsUpdate = \w _ -> (w, Just (youLight {_tlsPos = f $ _crPos (you w)})) } + where + f (x,y) = (x,y,0) diff --git a/src/Dodge/LevelGen.hs b/src/Dodge/LevelGen.hs index 7286e584d..963224a11 100644 --- a/src/Dodge/LevelGen.hs +++ b/src/Dodge/LevelGen.hs @@ -189,11 +189,11 @@ placeCr crF p rot = over creatures addCr crs placeLS :: LightSource -> Picture -> Point2 -> Float -> World -> World -placeLS ls dec p rot w = over lightSources addLS $ over decorations addDec w +placeLS ls dec p@(x,y) rot w = over lightSources addLS $ over decorations addDec w where addLS lss = IM.insert (IM.newKey lss) - (ls {_lsPos = p,_lsDir = rot,_lsID = IM.newKey lss}) + (ls {_lsPos = (x,y,0),_lsDir = rot,_lsID = IM.newKey lss}) lss addDec decs = IM.insert (IM.newKey decs) diff --git a/src/Dodge/LightSources.hs b/src/Dodge/LightSources.hs index ebdd7e97a..9f87b2021 100644 --- a/src/Dodge/LightSources.hs +++ b/src/Dodge/LightSources.hs @@ -9,9 +9,9 @@ import Picture --import qualified Data.IntMap.Strict as IM lightAt :: Point2 -> Int -> LightSource -lightAt p i = +lightAt (x,y) i = LS {_lsID = i - ,_lsPos = p + ,_lsPos = (x,y,0) ,_lsDir = 0 ,_lsRad = 600 ,_lsIntensity = 0.75 @@ -23,14 +23,14 @@ basicLS = PutLS ls dec dec = onLayer PtLayer $ color white $ circleSolid 8 tLightFade :: Int -> Float -> (Int -> Float) -> Point2 -> TempLightSource -tLightFade 0 rmax intensityF p = TLS - { _tlsPos = p +tLightFade 0 rmax intensityF (x,y) = TLS + { _tlsPos = (x,y,0) , _tlsRad = rmax , _tlsIntensity = intensityF 0 , _tlsUpdate = \w _ -> (w, Nothing) } -tLightFade i rmax intensityF p = TLS - { _tlsPos = p +tLightFade i rmax intensityF p@(x,y) = TLS + { _tlsPos = (x,y,0) , _tlsRad = rmax , _tlsIntensity = intensityF i , _tlsUpdate = \w _ -> (w, Just $ tLightFade (i-1) rmax intensityF p) @@ -42,14 +42,14 @@ tLightRad -> Float -- ^ minimal radius (unused) -> Point2 -> TempLightSource -tLightRad 0 rmax _ p = TLS - { _tlsPos = p +tLightRad 0 rmax _ (x,y) = TLS + { _tlsPos = (x,y,0) , _tlsRad = rmax , _tlsIntensity = 0.5 , _tlsUpdate = \w _ -> (w, Nothing) } -tLightRad i rmax rmin p = TLS - { _tlsPos = p +tLightRad i rmax rmin p@(x,y) = TLS + { _tlsPos = (x,y,0) , _tlsRad = rmax , _tlsIntensity = 0.5 , _tlsUpdate = \w _ -> (w, Just $ tLightRad (i-1) rmax rmin p) diff --git a/src/Dodge/Render/Picture.hs b/src/Dodge/Render/Picture.hs index 2c8598f06..8d7911bc6 100644 --- a/src/Dodge/Render/Picture.hs +++ b/src/Dodge/Render/Picture.hs @@ -217,8 +217,8 @@ wallsForShadows w = map _wlLine . filter (not . _wlIsSeeThrough) . IM.elems $ wallsNearZones (zoneOfDoubleScreen w) w -lightsForGloom :: World -> [Point4] +lightsForGloom :: World -> [(Point3,Float,Float)] lightsForGloom w = map getLS (IM.elems $ _lightSources w) ++ map getTLS (_tempLightSources w) where - getLS ls = ( fst $ _lsPos ls, snd $ _lsPos ls, _lsRad ls , _lsIntensity ls) - getTLS ls = ( fst $ _tlsPos ls,snd $ _tlsPos ls, _tlsRad ls, _tlsIntensity ls) + getLS ls = ( _lsPos ls, _lsRad ls , _lsIntensity ls) + getTLS ls = ( _tlsPos ls, _tlsRad ls, _tlsIntensity ls) diff --git a/src/Dodge/WorldEvent/Flash.hs b/src/Dodge/WorldEvent/Flash.hs index d65f2d820..9f0170494 100644 --- a/src/Dodge/WorldEvent/Flash.hs +++ b/src/Dodge/WorldEvent/Flash.hs @@ -86,10 +86,11 @@ flashFlareAt col alphax (x,y) = Particle explosionFlashAt :: Point2 -> World -> World explosionFlashAt p = over tempLightSources ((:) $ tLightFade 20 150 intensityFunc p) - . glareAt 20 10 5 (withAlpha 0.3 white) 75 150 p - where intensityFunc x - | x < 10 = 1 / (10 - fromIntegral x) - | otherwise = 1 + . glareAt 20 10 5 (withAlpha 0.3 white) 75 150 p + where + intensityFunc x + | x < 10 = 1 / (10 - fromIntegral x) + | otherwise = 1 flameGlareAt :: Point2 -> World -> World flameGlareAt = glareAt 1 10 5 (withAlpha 0.05 orange) 8 40 diff --git a/src/Picture/Render.hs b/src/Picture/Render.hs index 6152ae296..911274384 100644 --- a/src/Picture/Render.hs +++ b/src/Picture/Render.hs @@ -55,12 +55,11 @@ createLightMap :: RenderData -> Int -- Resolution division -> [(Point2,Point2)] -- Wall pairs - -> [Point4] -- Lights + -> [(Point3,Float,Float)] -- Lights -> (Float,Float) -- View from position -> Picture -- foreground pictures -> IO () createLightMap pdata resDiv wallPoints lightPoints (viewFromx,viewFromy) _ = do - -- get viewport size so we can reset it later (vppos,vpsize) <- get viewport -- set the viewport size to that of the render buffer @@ -97,7 +96,7 @@ createLightMap pdata resDiv wallPoints lightPoints (viewFromx,viewFromy) _ = do depthMask $= Disabled blendFunc $= (Zero, OneMinusSrcAlpha) stencilTest $= Enabled - forM_ lightPoints $ \(x,y,r,lum) -> do + forM_ lightPoints $ \((x,y,z),r,lum) -> do -- bind buffer for floor light circle let lightPtr = _vboPointer $ _vaoVBO $ _shaderVAO $ _lightingFloorShader pdata pokeFourOff lightPtr 0 (x,y,r,lum) @@ -109,7 +108,7 @@ createLightMap pdata resDiv wallPoints lightPoints (viewFromx,viewFromy) _ = do stencilFunc $= (Always, 0, 255) currentProgram $= Just (_shaderProgram $ _lightingOccludeShader pdata) uniform (head $ _shaderCustomUnis $ _lightingOccludeShader pdata) - $= Vector2 x y + $= Vector3 x y z drawShader (_lightingOccludeShader pdata) nWalls cullFace $= Just Front stencilOp $= (OpKeep,OpKeep,OpDecr) @@ -123,7 +122,7 @@ createLightMap pdata resDiv wallPoints lightPoints (viewFromx,viewFromy) _ = do -- draw wall light "circles" currentProgram $= Just (_shaderProgram $ _lightingWallShader pdata) uniform (head $ _shaderCustomUnis $ _lightingWallShader pdata) - $= Vector2 x y + $= Vector3 x y z uniform (_shaderCustomUnis (_lightingWallShader pdata) !! 1) $= Vector2 r lum drawShader (_lightingWallShader pdata) nWallLights