Add scissor test when creating lightmap

This commit is contained in:
2025-11-14 02:06:51 +00:00
parent c79e5df683
commit 292a1c3f42
5 changed files with 66 additions and 35 deletions
+13 -1
View File
@@ -2,8 +2,12 @@ module Dodge.Base.Coordinate (
worldPosToScreen,
screenToWorldPos,
mouseWorldPos,
worldPosToResOffset,
) where
import Dodge.WindowSize
import Linear
import Dodge.Data.Config
import Control.Lens
import Dodge.Data.Camera
import Dodge.Data.Input
@@ -18,9 +22,17 @@ import Geometry
worldPosToScreen :: Camera -> Point2 -> Point2
worldPosToScreen cam =
rotateV (negate $ cam ^. camRot)
. ((cam ^. camZoom) *.*)
. ((cam ^. camZoom) *^)
. (-.- (cam ^. camCenter))
-- gets the position from the bottom left in "world res" "pixels"
worldPosToResOffset :: Config -> Camera -> Point2 -> Point2
worldPosToResOffset cfig cam p = (worldPosToScreen cam p & each %~ f)
+ 0.5 *^ (uncurry V2 (getWindowSize _gr_world_res cfig::(Int,Int)) & each %~ fromIntegral)
where
f x = applyResFactorF (cfig ^. gr_world_res) x
-- | The mouse position in world coordinates.
mouseWorldPos :: Input -> Camera -> Point2
mouseWorldPos inp cam = screenToWorldPos cam (inp ^. mousePos)
+9
View File
@@ -129,6 +129,15 @@ applyResFactor rf = case rf of
EighthRes -> (`div` 8)
SixteenthRes -> (`div` 16)
applyResFactorF :: ResFactor -> Float -> Float
applyResFactorF rf = case rf of
DoubleRes -> (2*)
FullRes -> id
HalfRes -> (/ 2)
QuarterRes -> (/ 4)
EighthRes -> (/ 8)
SixteenthRes -> (/ 16)
--resFactorNum :: ResFactor -> Int
--resFactorNum rf = case rf of
-- FullRes -> 1
+2
View File
@@ -161,6 +161,7 @@ doDrawing' win pdata u = do
glEnable GL_BLEND
--draw lightmap into its own buffer
createLightMap
(w ^. wCam)
cfig
(fromIntegral trueNWalls)
nSilIndices
@@ -284,6 +285,7 @@ doDrawing' win pdata u = do
glEnable GL_BLEND
glBindFramebuffer GL_FRAMEBUFFER (_unFBO (fst (_fboLighting pdata)))
createLightMap
(w ^. wCam)
cfig
(fromIntegral trueNWalls)
nSilIndices
+13 -7
View File
@@ -5,6 +5,8 @@ module Render (
bindFBO,
) where
import Dodge.Base.Coordinate
import Dodge.Data.Camera
import Control.Lens
import Control.Monad.Primitive
import Data.Preload.Render
@@ -24,6 +26,7 @@ import Shader.Data
away" from the shape colors.
-}
createLightMap ::
Camera ->
Config ->
-- | number of walls
GLsizei ->
@@ -38,10 +41,10 @@ createLightMap ::
[(Point3, Float, Point3)] -> -- Lights
RenderData ->
IO ()
createLightMap cfig = case shadrendertype of
createLightMap cam cfig = case shadrendertype of
NoShadows -> const . const . const renderLightingNoShadows
NoLighting -> const . const . const . const . const . const renderFlatLighting
_ -> renderShadows shadrendertype
_ -> renderShadows cam cfig shadrendertype
where
shadrendertype = cfig ^. gr_shadow_rendering
@@ -114,6 +117,8 @@ renderFlatLighting pdata = do
glStencilOp GL_KEEP GL_KEEP GL_KEEP
renderShadows ::
Camera ->
Config ->
ShadowRendering ->
GLsizei ->
Int ->
@@ -123,7 +128,7 @@ renderShadows ::
[(Point3, Float, Point3)] ->
RenderData ->
IO ()
renderShadows shadrendertype nWalls nSils nCaps positiontexture normaltexture lightPoints pdata = do
renderShadows cam cfig shadrendertype nWalls nSils nCaps positiontexture normaltexture lightPoints pdata = do
glBindFramebuffer GL_FRAMEBUFFER $ pdata ^. fboLighting . _1 . unFBO
let llinesShad = _lightingLineShadowShader pdata
lcapShad = _lightingCapShader pdata
@@ -150,12 +155,14 @@ renderShadows shadrendertype nWalls nSils nCaps positiontexture normaltexture li
-- 1. stencil out the shadows from this light's point of view
-- 2. calculate lighting based on each fragment's position and normal
-- TODO stencil out square around light!
-- glEnable GL_SCISSOR_TEST
glEnable GL_SCISSOR_TEST
glDisable GL_CULL_FACE
flip VFSM.mapM_ (VFSM.fromList lightPoints) $ \(V3 x y z, rad, V3 r g b) -> do
withArray [x,y,z,1,r,g,b,rad] $ glNamedBufferSubData (pdata ^. lightUBO) 0 32
-- note that the stencil shadows rely on the depth buffer
-- glScissor 0 0 200 200
let d = rad * cam ^. camZoom
V2 x' y' = worldPosToResOffset cfig cam (V2 x y)
glScissor (round (x'-d)) (round (y'-d)) (round $ 2 *d) (round $ 2* d)
glDepthFunc GL_LESS
glColorMask GL_FALSE GL_FALSE GL_FALSE GL_FALSE
-- setup stencil
@@ -187,8 +194,7 @@ renderShadows shadrendertype nWalls nSils nCaps positiontexture normaltexture li
glUseProgram ltextShad
glDrawArrays GL_TRIANGLES 0 6
--cleanup: may not be necessary, depending on what comes after...
-- glDisable GL_STENCIL_TEST
-- glDisable GL_SCISSOR_TEST
glDisable GL_SCISSOR_TEST
glStencilFunc GL_NOTEQUAL 128 255
glStencilOp GL_KEEP GL_KEEP GL_KEEP
glColorMask GL_TRUE GL_TRUE GL_TRUE GL_TRUE