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 {
+28 -17
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)
@@ -93,14 +93,13 @@ doDrawing' win pdata u = do
(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
@@ -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
@@ -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
@@ -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
+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