Use stencil test to not draw floors where there are chasms

This commit is contained in:
2025-08-05 19:57:49 +01:00
parent 83124703c5
commit df443744db
4 changed files with 44 additions and 38 deletions
-6
View File
@@ -1,6 +0,0 @@
#version 450 core
out vec4 fColor;
void main()
{
fColor = vec4 (0,0,0,1);
}
+7 -7
View File
@@ -36,8 +36,8 @@ void main() {
vec2 p2cs = p2 + closeshift; vec2 p2cs = p2 + closeshift;
vec2 p2o = vec2( distance(p2,lightPos.xy) > rad2 ? p2cs : shift(p2) ); vec2 p2o = vec2( distance(p2,lightPos.xy) > rad2 ? p2cs : shift(p2) );
gl_Position = theMat * vec4(p1o,0,1); EmitVertex(); gl_Position = theMat * vec4(p1o,-500,1); EmitVertex();
gl_Position = theMat * vec4(p1,0,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(p1cs,100,1); EmitVertex();
@@ -52,15 +52,15 @@ void main() {
// 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,0,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,0,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,0,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,0,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,0,1); EmitVertex(); gl_Position = theMat * vec4(p1o,-500,1); EmitVertex();
EndPrimitive(); EndPrimitive();
} else { } else {
+36 -25
View File
@@ -1,24 +1,23 @@
-- | Contains the central drawing functions for the dodge loop. -- | Contains the central drawing functions for the dodge loop.
module Dodge.Render ( module Dodge.Render (
doDrawing, doDrawing,
getWindowSize getWindowSize,
) where ) where
import qualified Data.Vector as V
import Dodge.Shadows
import Dodge.WindowSize
import Control.Lens import Control.Lens
import Control.Monad import Control.Monad
import qualified Control.Monad.Parallel as MP import qualified Control.Monad.Parallel as MP
--import Data.List (sortOn) --import Data.List (sortOn)
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import Data.Preload.Render import Data.Preload.Render
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed.Mutable as UMV import qualified Data.Vector.Unboxed.Mutable as UMV
import Dodge.Data.Universe import Dodge.Data.Universe
import Dodge.Render.Lights import Dodge.Render.Lights
import Dodge.Render.ShapePicture import Dodge.Render.ShapePicture
import Dodge.Render.Walls import Dodge.Render.Walls
import Dodge.Shadows
import Dodge.WindowSize
import Foreign import Foreign
import GLHelp import GLHelp
import Geometry import Geometry
@@ -80,7 +79,8 @@ doDrawing' win pdata u = do
windowPoints windowPoints
wallsToPoke wallsToPoke
) )
( pokeShape (drawShadowsByImportance $ cfig ^. graphics_shadow_size) ( pokeShape
(drawShadowsByImportance $ cfig ^. graphics_shadow_size)
(_vboPtr $ _vboShapes pdata) (_vboPtr $ _vboShapes pdata)
(_eboPtr $ _shapeEBO pdata) (_eboPtr $ _shapeEBO pdata)
(_eboPtr $ _silhouetteEBO pdata) (_eboPtr $ _silhouetteEBO pdata)
@@ -88,19 +88,18 @@ doDrawing' win pdata u = do
ws ws
) )
(nCloudVs', nCloudIs') <- (nCloudVs', nCloudIs') <-
-- Might want to sort the clouds based on z depth, not sure -- Might want to sort the clouds based on z depth, not sure
V.foldM' V.foldM'
(pokeCloud (pdata ^. cloudVBO . vboPtr) (pdata ^. cloudEBO . eboPtr)) (pokeCloud (pdata ^. cloudVBO . vboPtr) (pdata ^. cloudEBO . eboPtr))
(0, 0) (0, 0)
(V.fromList $ w ^. cWorld . lWorld . clouds) (V.fromList $ w ^. cWorld . lWorld . clouds)
--(sortOn (^. clPos . _3) $ w ^. cWorld . lWorld . clouds)
(nCloudVs, nCloudIs) <- (nCloudVs, nCloudIs) <-
-- Might want to sort the clouds based on z depth, not sure
V.foldM' V.foldM'
(pokeDust (pdata ^. cloudVBO . vboPtr) (pdata ^. cloudEBO . eboPtr)) (pokeDust (pdata ^. cloudVBO . vboPtr) (pdata ^. cloudEBO . eboPtr))
(nCloudVs', nCloudIs') (nCloudVs', nCloudIs')
(V.fromList $ w ^. cWorld . lWorld . dusts) (V.fromList $ w ^. cWorld . lWorld . dusts)
--(sortOn (^. clPos . _3) $ w ^. cWorld . lWorld . clouds)
-- bind wall points, silhouette data, surface geometry -- bind wall points, silhouette data, surface geometry
bufferShaderLayers shadV pokeCounts bufferShaderLayers shadV pokeCounts
mapM_ mapM_
@@ -161,6 +160,8 @@ doDrawing' win pdata u = do
GL_COLOR GL_COLOR
2 2
ptr ptr
withArray [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2] $
\ptr -> glDrawBuffers 3 ptr
--draw walls onto base buffer --draw walls onto base buffer
-- maybe cull faces? maybe do this before? -- maybe cull faces? maybe do this before?
glEnable GL_CULL_FACE glEnable GL_CULL_FACE
@@ -172,7 +173,7 @@ doDrawing' win pdata u = do
0 0
(fromIntegral trueNWalls) (fromIntegral trueNWalls)
--draw object pictures onto base buffer (are there any?!) --draw object pictures onto base buffer (are there any?!)
-- renderLayer BottomLayer shadV layerCounts -- renderLayer BottomLayer shadV layerCounts
--draw object shapes onto base buffer --draw object shapes onto base buffer
let fs = _shapeShader pdata let fs = _shapeShader pdata
glUseProgram (_shaderUINT fs) glUseProgram (_shaderUINT fs)
@@ -185,22 +186,33 @@ doDrawing' win pdata u = do
-- draw chasm shader blocking floor -- draw chasm shader blocking floor
glUseProgram (pdata ^. chasmShader . shaderUINT) glUseProgram (pdata ^. chasmShader . shaderUINT)
glBindVertexArray $ pdata ^. chasmShader . shaderVAO . vaoName glBindVertexArray $ pdata ^. chasmShader . shaderVAO . vaoName
-- glColorMask GL_FALSE GL_FALSE GL_FALSE GL_FALSE glDepthMask GL_FALSE
glDrawBuffer GL_NONE glDrawBuffer GL_NONE
glEnable GL_STENCIL_TEST
glStencilFunc GL_ALWAYS 0 255
glStencilOp GL_INCR GL_INCR GL_INCR
with 0 $ \ptr ->
glClearNamedFramebufferiv
(pdata ^. fboBase . _1 . unFBO)
GL_STENCIL
0
ptr
glDrawArrays glDrawArrays
(_unPrimitiveMode $ pdata ^. chasmShader . shaderPrimitive) (_unPrimitiveMode $ pdata ^. chasmShader . shaderPrimitive)
0 0
(fromIntegral nchs) (fromIntegral nchs)
-- glColorMask GL_TRUE GL_TRUE GL_TRUE GL_TRUE withArray [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2] $
withArray [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2] \ptr -> glDrawBuffers 3 ptr
$ \ptr -> glDrawBuffers 3 ptr glDepthMask GL_TRUE
--draw floor onto base buffer --draw floor onto base buffer
glUseProgram (pdata ^. floorShader . shaderUINT) glUseProgram (pdata ^. floorShader . shaderUINT)
glBindVertexArray $ pdata ^. floorShader . shaderVAO . vaoName glBindVertexArray $ pdata ^. floorShader . shaderVAO . vaoName
glStencilFunc GL_EQUAL 0 255
glDrawArrays glDrawArrays
(_unPrimitiveMode $ pdata ^. floorShader . shaderPrimitive) (_unPrimitiveMode $ pdata ^. floorShader . shaderPrimitive)
0 0
(fromIntegral nFls) (fromIntegral nFls)
glDisable GL_STENCIL_TEST
glDisable GL_CULL_FACE glDisable GL_CULL_FACE
glEnable GL_BLEND glEnable GL_BLEND
--draw lightmap into its own buffer --draw lightmap into its own buffer
@@ -245,7 +257,7 @@ doDrawing' win pdata u = do
-- draw clouds, transparency blocks some alpha -- draw clouds, transparency blocks some alpha
glDepthMask GL_FALSE glDepthMask GL_FALSE
glBlendColor 0 0 0 0.1 -- using this and GL_CONSTANT_ALPHA below glBlendColor 0 0 0 0.1 -- using this and GL_CONSTANT_ALPHA below
-- can allow for some underlighting of the clouds -- can allow for some underlighting of the clouds
glBlendFuncSeparate GL_ZERO GL_ONE GL_CONSTANT_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFuncSeparate GL_ZERO GL_ONE GL_CONSTANT_ALPHA GL_ONE_MINUS_SRC_ALPHA
glUseProgram (pdata ^. cloudShader . shaderUINT) glUseProgram (pdata ^. cloudShader . shaderUINT)
glBindVertexArray $ pdata ^. cloudShader . shaderVAO . vaoName glBindVertexArray $ pdata ^. cloudShader . shaderVAO . vaoName
@@ -264,7 +276,7 @@ doDrawing' win pdata u = do
glBindTextureUnit 0 (pdata ^. fboBloom . _2 . unTO) glBindTextureUnit 0 (pdata ^. fboBloom . _2 . unTO)
glDisable GL_BLEND glDisable GL_BLEND
drawShader (_bloomBlurShader pdata) 4 drawShader (_bloomBlurShader pdata) 4
when (cfig ^. graphics_bloom) $ do when (cfig ^. graphics_bloom) $ do
replicateM_ 2 $ pingPongBetween (_fboHalf1 pdata) (_fboHalf2 pdata) (_bloomBlurShader pdata) replicateM_ 2 $ pingPongBetween (_fboHalf1 pdata) (_fboHalf2 pdata) (_bloomBlurShader pdata)
glEnable GL_BLEND glEnable GL_BLEND
setViewport _graphics_world_resolution cfig setViewport _graphics_world_resolution cfig
@@ -287,7 +299,7 @@ doDrawing' win pdata u = do
GL_COLOR GL_COLOR
0 0
ptr ptr
-- renderLayer MidLayer shadV pokeCounts -- renderLayer MidLayer shadV pokeCounts
glUseProgram (pdata ^. cloudShader . shaderUINT) glUseProgram (pdata ^. cloudShader . shaderUINT)
glBindVertexArray $ pdata ^. cloudShader . shaderVAO . vaoName glBindVertexArray $ pdata ^. cloudShader . shaderVAO . vaoName
glDrawElements glDrawElements
@@ -301,8 +313,8 @@ doDrawing' win pdata u = do
glDepthMask GL_TRUE glDepthMask GL_TRUE
glDepthFunc GL_ALWAYS glDepthFunc GL_ALWAYS
glDisable GL_BLEND glDisable GL_BLEND
withArray [GL_NONE, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2] withArray [GL_NONE, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2] $
$ \ptr -> glDrawBuffers 3 ptr \ptr -> glDrawBuffers 3 ptr
withArray [0, 0, 0, 0] $ \ptr -> withArray [0, 0, 0, 0] $ \ptr ->
glClearNamedFramebufferfv glClearNamedFramebufferfv
(pdata ^. fboCloud . _1 . unFBO) (pdata ^. fboCloud . _1 . unFBO)
@@ -379,7 +391,6 @@ doDrawing' win pdata u = do
drawShader (_fullscreenShader pdata) 4 drawShader (_fullscreenShader pdata) 4
glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
--set viewport for radial distortion --set viewport for radial distortion
--setViewportSize (round winx) (round winy) --setViewportSize (round winx) (round winy)
setViewport (const FullRes) cfig setViewport (const FullRes) cfig
@@ -392,7 +403,7 @@ doDrawing' win pdata u = do
glBindTextureUnit 0 $ pdata ^. fboBase . _2 . _1 . unTO glBindTextureUnit 0 $ pdata ^. fboBase . _2 . _1 . unTO
glBindFramebuffer GL_FRAMEBUFFER 0 glBindFramebuffer GL_FRAMEBUFFER 0
drawShader (_fullscreenShader pdata) 4 drawShader (_fullscreenShader pdata) 4
(rds,_) -> do (rds, _) -> do
let bindDrawDist :: Distortion -> IO () let bindDrawDist :: Distortion -> IO ()
bindDrawDist (RadialDistortion (V2 a b) (V2 c d) (V2 e f) g) = do bindDrawDist (RadialDistortion (V2 a b) (V2 c d) (V2 e f) g) = do
pokeArray (shadVBOptr $ _barrelShader pdata) [a, b, c, d, e, f, g] pokeArray (shadVBOptr $ _barrelShader pdata) [a, b, c, d, e, f, g]
@@ -413,8 +424,8 @@ doDrawing' win pdata u = do
glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA
renderLayer DebugLayer shadV pokeCounts renderLayer DebugLayer shadV pokeCounts
glBindBufferBase GL_UNIFORM_BUFFER 0 (pdata ^. matUBO) glBindBufferBase GL_UNIFORM_BUFFER 0 (pdata ^. matUBO)
withArray (scaleMatrix (2 / windowXFloat cfig) (2 / windowYFloat cfig)) withArray (scaleMatrix (2 / windowXFloat cfig) (2 / windowYFloat cfig)) $
$ \ptr -> glNamedBufferSubData (pdata ^. matUBO) 0 64 ptr \ptr -> glNamedBufferSubData (pdata ^. matUBO) 0 64 ptr
glDepthFunc GL_GEQUAL glDepthFunc GL_GEQUAL
glDepthMask GL_TRUE glDepthMask GL_TRUE
with (-1) $ \ptr -> glClearNamedFramebufferfv 0 GL_DEPTH 0 ptr with (-1) $ \ptr -> glClearNamedFramebufferfv 0 GL_DEPTH 0 ptr
@@ -422,7 +433,7 @@ doDrawing' win pdata u = do
SDL.glSwapWindow win SDL.glSwapWindow win
getDistortions :: Configuration -> World -> [Distortion] getDistortions :: Configuration -> World -> [Distortion]
getDistortions cfig w getDistortions cfig w
| cfig ^. graphics_distortions = w ^. cWorld . lWorld . distortions | cfig ^. graphics_distortions = w ^. cWorld . lWorld . distortions
| otherwise = [] | otherwise = []
+1
View File
@@ -153,6 +153,7 @@ renderShadows shadrendertype nWalls nSils nCaps positiontexture normaltexture li
-- 1. stencil out the shadows from this light's point of view -- 1. stencil out the shadows from this light's point of view
-- 2. calculate lighting based on each fragment's position and normal -- 2. calculate lighting based on each fragment's position and normal
flip VFSM.mapM_ (VFSM.fromList lightPoints) $ \(V3 x y z, rad, V3 r g b) -> do flip VFSM.mapM_ (VFSM.fromList lightPoints) $ \(V3 x y z, rad, V3 r g b) -> do
-- note that the stencil shadows rely on the depth buffer
glDepthFunc GL_LESS glDepthFunc GL_LESS
-- setup stencil -- setup stencil
glColorMask GL_FALSE GL_FALSE GL_FALSE GL_FALSE glColorMask GL_FALSE GL_FALSE GL_FALSE GL_FALSE