Implement something like deferred lighting

This commit is contained in:
2021-09-01 00:37:07 +01:00
parent 2d139adc8f
commit 1c587d4b9a
13 changed files with 90 additions and 71 deletions
+6 -6
View File
@@ -1,13 +1,13 @@
#version 430 core #version 430 core
in vec4 vColor; in vec4 vCol;
in vec3 vPosition; in vec3 vPos;
in vec3 vparams; in vec3 vparams;
layout (location=0) out vec4 fColor; layout (location=0) out vec4 fCol;
layout (location=1) out vec3 fPosition; layout (location=1) out vec4 fPos;
void main() void main()
{ {
float d = (dot(vec2(vparams.xy),vec2(vparams.xy))); float d = (dot(vec2(vparams.xy),vec2(vparams.xy)));
if ( d > 1 || d < vparams.z ) {discard;} if ( d > 1 || d < vparams.z ) {discard;}
fColor = vColor; fCol = vCol;
fPosition = vPosition; fPos = vec4(vPos,1);
} }
+7 -7
View File
@@ -1,14 +1,14 @@
#version 430 core #version 430 core
layout (location = 0) in vec3 position; layout (location = 0) in vec3 pos;
layout (location = 1) in vec4 color; layout (location = 1) in vec4 col;
layout (location = 2) in vec3 boxXboxYwidth; layout (location = 2) in vec3 boxXboxYwidth;
layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ; layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ;
out vec4 vColor; out vec4 vCol;
out vec3 vPosition; out vec3 vPos;
out vec3 vparams; out vec3 vparams;
void main() { void main() {
gl_Position = theMat * vec4(position,1); gl_Position = theMat * vec4(pos,1);
vColor = color; vCol = col;
vparams = boxXboxYwidth; vparams = boxXboxYwidth;
vPosition = position; vPos = pos;
} }
+2 -2
View File
@@ -2,9 +2,9 @@
in vec4 vCol; in vec4 vCol;
in vec3 vPos; in vec3 vPos;
layout (location=0) out vec4 fCol; layout (location=0) out vec4 fCol;
layout (location=1) out vec3 fPos; layout (location=1) out vec4 fPos;
void main() void main()
{ {
fCol = vCol; fCol = vCol;
fPos = vPos; fPos = vec4(vPos,1);
} }
+7 -5
View File
@@ -1,7 +1,9 @@
#version 430 core #version 430 core
in vec4 vColor; layout (location=0) out vec4 fCol;
out vec4 fColor; layout (location=1) out vec4 fPos;
void main() in vec4 vCol;
{ in vec3 vPos;
fColor = vColor; void main() {
fCol = vCol;
fPos = vec4(vPos,1) ;
} }
+4 -2
View File
@@ -2,11 +2,13 @@
layout (location = 0) in vec4 position; layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color; layout (location = 1) in vec4 color;
layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ; layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ;
out vec4 vColor; out vec4 vCol;
out vec3 vPos;
void main() void main()
{ {
vec4 posxy = theMat * vec4(position.xyz,1); vec4 posxy = theMat * vec4(position.xyz,1);
vec4 posz = theMat * vec4(position.xyw,1); vec4 posz = theMat * vec4(position.xyw,1);
gl_Position = vec4(posxy.xy,posz.z,1); gl_Position = vec4(posxy.xy,posz.z,1);
vColor = color; vCol = color;
vPos = position.xyz;
} }
+4 -2
View File
@@ -3,7 +3,8 @@ in vec4 vColor;
in vec2 boxOut; in vec2 boxOut;
in vec2 boxIn; in vec2 boxIn;
out vec4 fColor; layout (location=0) out vec4 fCol;
layout (location=1) out vec4 fPos;
void main() void main()
{ {
@@ -12,5 +13,6 @@ void main()
// float e = sqrt(f(x,wStart)) + sqrt(f(y,wEnd)) - 1.0; // float e = sqrt(f(x,wStart)) + sqrt(f(y,wEnd)) - 1.0;
float e = sqrt(boxIn.x) + sqrt(boxIn.y) - 1.0; float e = sqrt(boxIn.x) + sqrt(boxIn.y) - 1.0;
if ( d < 0 || e > 0) { discard; } if ( d < 0 || e > 0) { discard; }
fColor = vColor; fCol = vColor;
fPos = vec4(0,0,0,0) ;
} }
+5 -9
View File
@@ -1,15 +1,11 @@
#version 430 core #version 430 core
out vec4 FragColor; layout (location=0) out vec4 fCol;
layout (location=1) out vec4 fPos;
uniform sampler2D aTexture;
in vec4 gColor; in vec4 gColor;
in vec2 gTexCoord; in vec2 gTexCoord;
uniform sampler2D aTexture;
void main() void main()
{ {
FragColor = texture(aTexture, vec2 (gTexCoord.x * 0.0078125, gTexCoord.y)) * gColor; fCol = texture(aTexture, vec2 (gTexCoord.x * 0.0078125, gTexCoord.y)) * gColor;
// FragColor = texture(aTexture, vTexCoord); fPos = vec4(0,0,0,0);
// FragColor = gColor;
// FragColor = vec4 (1,1,1,1);
} }
+4 -7
View File
@@ -1,16 +1,13 @@
#version 430 core #version 430 core
layout (location=0) out vec4 fCol;
layout (location=1) out vec4 fPos;
in vec4 gColC; in vec4 gColC;
in vec4 gColE; in vec4 gColE;
in vec3 gPos; in vec3 gPos;
in vec2 gBoundingBox; in vec2 gBoundingBox;
void main() {
layout (location=0) out vec4 fCol;
layout (location=1) out vec3 fPos;
void main()
{
float d = dot(gBoundingBox,gBoundingBox); float d = dot(gBoundingBox,gBoundingBox);
if ( d > 1) { discard; } if ( d > 1) { discard; }
fCol = mix (gColE , gColC, d); fCol = mix (gColE , gColC, d);
fPos = vPos; fPos = vec4(gPos,1);
} }
+1
View File
@@ -21,6 +21,7 @@ data RenderData = RenderData
, _wallTextureShader :: FullShader , _wallTextureShader :: FullShader
, _textureArrayShader :: FullShader , _textureArrayShader :: FullShader
, _fullscreenShader :: FullShader , _fullscreenShader :: FullShader
, _fullLightingShader :: FullShader
, _fullscreenAlphaHalveShader :: FullShader , _fullscreenAlphaHalveShader :: FullShader
--, _boxBlurShader :: FullShader --, _boxBlurShader :: FullShader
, _bloomBlurShader :: FullShader , _bloomBlurShader :: FullShader
+2 -8
View File
@@ -8,7 +8,7 @@ import Dodge.Data
import Dodge.Config.Data import Dodge.Config.Data
import Dodge.Base.Window import Dodge.Base.Window
import Dodge.Render.Picture import Dodge.Render.Picture
import Dodge.Creature.ShadowBox --import Dodge.Creature.ShadowBox
import Geometry import Geometry
import Render import Render
import Data.Preload.Render import Data.Preload.Render
@@ -19,7 +19,7 @@ import Shader.Data
import MatrixHelper import MatrixHelper
--import Polyhedra.Data --import Polyhedra.Data
import Polyhedra import Polyhedra
import Tuple --import Tuple
import Foreign import Foreign
--import Control.Applicative --import Control.Applicative
@@ -220,12 +220,6 @@ doDrawing pdata w = do
eTicks <- SDL.ticks eTicks <- SDL.ticks
return (eTicks - sTicks) return (eTicks - sTicks)
bindTO :: TextureObject -> IO ()
bindTO t = textureBinding Texture2D $= Just t
bindFBO :: FramebufferObject -> IO ()
bindFBO fb = bindFramebuffer Framebuffer $= fb
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- note: currently assume there is only one UBO, we only bind it once at setup -- note: currently assume there is only one UBO, we only bind it once at setup
bufferUBO :: [Float] -> IO () bufferUBO :: [Float] -> IO ()
+2 -1
View File
@@ -23,7 +23,7 @@ sizeFBOs
-> IO RenderData -> IO RenderData
sizeFBOs xsize ysize xfull yfull rdata = do sizeFBOs xsize ysize xfull yfull rdata = do
resizeRBO (_rboBaseBloom rdata) xsize ysize resizeRBO (_rboBaseBloom rdata) xsize ysize
rdata' <- updateFBOTO2 xsize ysize minMagFilter GL_RGBA8 GL_RGBA16F rdata fboBase rdata' <- updateFBOTO2 xsize ysize linMinMagFilter GL_RGBA8 GL_RGBA16F rdata fboBase
>>= flip (foldM (updateFBOTO xsize ysize minMagFilter GL_RGBA8)) >>= flip (foldM (updateFBOTO xsize ysize minMagFilter GL_RGBA8))
[fboColor,fboLighting,fboLightingHigh] [fboColor,fboLighting,fboLightingHigh]
rdata'' <- foldM (updateFBOTO xsize ysize linMinMagFilter GL_RGBA16F) rdata' rdata'' <- foldM (updateFBOTO xsize ysize linMinMagFilter GL_RGBA16F) rdata'
@@ -94,6 +94,7 @@ resizeFBOTO2 (fboName,(toOld1,toOld2)) xsize ysize mmfilt inFormat1 inFormat2 =
glTexStorage2D GL_TEXTURE_2D 1 inFormat2 xsize' ysize' glTexStorage2D GL_TEXTURE_2D 1 inFormat2 xsize' ysize'
textureFilter Texture2D $= mmfilt textureFilter Texture2D $= mmfilt
framebufferTexture2D Framebuffer (ColorAttachment 1) Texture2D toName2 0 framebufferTexture2D Framebuffer (ColorAttachment 1) Texture2D toName2 0
drawBuffers $= [FBOColorAttachment 0,FBOColorAttachment 1]
fboStatus <- framebufferStatus Framebuffer fboStatus <- framebufferStatus Framebuffer
putStrLn $ "after resize, framebuffer status:" ++ show fboStatus putStrLn $ "after resize, framebuffer status:" ++ show fboStatus
return (fboName, (toName1,toName2)) return (fboName, (toName1,toName2))
+10 -6
View File
@@ -69,13 +69,13 @@ preloadRender = do
>>= addUniforms ["lightPos"] >>= addUniforms ["lightPos"]
-- 2D draw shaders -- 2D draw shaders
bslist <- makeShader "dualTwoD/basic" [vert,frag] [3,4] ETriangles bslist <- makeShader "dualTwoD/basic" [vert,frag] [3,4] ETriangles
aslist <- makeShader "twoD/arc" [vert,frag] [3,4,3] ETriangles aslist <- makeShader "dualTwoD/arc" [vert,frag] [3,4,3] ETriangles
eslist <- makeShader "twoD/ellipse" [vert,geom,frag] [3,4] ETriangles eslist <- makeShader "dualTwoD/ellipse" [vert,geom,frag] [3,4] ETriangles
bezierQuadShader <- makeShader "twoD/bezierQuad" [vert,frag] [3,4,4] ETriangleStrip bezierQuadShader <- makeShader "dualTwoD/bezierQuad" [vert,frag] [3,4,4] ETriangleStrip
cslist <- makeShader "twoD/character" [vert,frag] [3,4,2] ETriangles cslist <- makeShader "dualTwoD/character" [vert,frag] [3,4,2] ETriangles
>>= vaddTextureNoFilter "data/texture/charMap.png" >>= vaddTextureNoFilter "data/texture/charMap.png"
-- this should really be a 2d texture array -- this should really be a 2d texture array
basicTweakZShad <- makeShader "twoD/basicTweakZ" [vert,frag] [4,4] ETriangles basicTweakZShad <- makeShader "dualTwoD/basicTweakZ" [vert,frag] [4,4] ETriangles
-- fullscreen shaders -- fullscreen shaders
fullscreenAlphaHalveShad <- makeShaderSized "fullscreen/alphaHalve" [vert,frag] [2] 4 ETriangleStrip fullscreenAlphaHalveShad <- makeShaderSized "fullscreen/alphaHalve" [vert,frag] [2] 4 ETriangleStrip
pokeArray (shadVBOptr fullscreenAlphaHalveShad) $ concat cornerListNoCoord pokeArray (shadVBOptr fullscreenAlphaHalveShad) $ concat cornerListNoCoord
@@ -83,9 +83,12 @@ preloadRender = do
fsShad <- makeShaderSized "texture/simple" [vert,frag] [2,2] 4 ETriangleStrip fsShad <- makeShaderSized "texture/simple" [vert,frag] [2,2] 4 ETriangleStrip
-- note we directly poke the shader vertex data here -- note we directly poke the shader vertex data here
pokeArray (shadVBOptr fsShad) $ concat cornerList pokeArray (shadVBOptr fsShad) $ concat cornerList
bloomBlurShad <- makeShaderUsingShaderVAO "texture/bloomBlur" [vert,frag] ETriangleStrip fsShad bloomBlurShad <- makeShaderUsingShaderVAO "texture/bloomBlur" [vert,frag] ETriangleStrip fsShad
colorBlurShad <- makeShaderUsingShaderVAO "texture/colorBlur" [vert,frag] ETriangleStrip fsShad colorBlurShad <- makeShaderUsingShaderVAO "texture/colorBlur" [vert,frag] ETriangleStrip fsShad
grayscaleShad <- makeShaderUsingShaderVAO "texture/grayscale" [vert,frag] ETriangleStrip fsShad grayscaleShad <- makeShaderUsingShaderVAO "texture/grayscale" [vert,frag] ETriangleStrip fsShad
fullLightingShad <- makeShaderUsingShaderVAO "lighting/texture" [vert,frag] ETriangleStrip fsShad
>>= addUniforms ["lightPos","lumRad"]
barrelShad <- makeShader "texture/barrel" [vert,geom,frag] [2,2,2,1] EPoints barrelShad <- makeShader "texture/barrel" [vert,geom,frag] [2,2,2,1] EPoints
-- blank wallShader -- blank wallShader
wlBlank <- makeShader "wall/blank" [vert,geom,frag] [4,4] EPoints wlBlank <- makeShader "wall/blank" [vert,geom,frag] [4,4] EPoints
@@ -93,7 +96,7 @@ preloadRender = do
wlTexture <- makeShader "wall/texture" [vert,geom,frag] [4,4] EPoints wlTexture <- makeShader "wall/texture" [vert,geom,frag] [4,4] EPoints
>>= addTexture "data/texture/grayscaleDirt.png" >>= addTexture "data/texture/grayscaleDirt.png"
---- texture array shader ---- texture array shader
textArrayShad <- makeShader "texture/array" [vert,frag] [3,3] ETriangles textArrayShad <- makeShader "texture/arrayPos" [vert,frag] [3,3] ETriangles
>>= addTextureArray "data/texture/ayene_wooden_floor_transformed.png" >>= addTextureArray "data/texture/ayene_wooden_floor_transformed.png"
-- >>= addTextureArray "data/texture/ayene_wooden_floor.png" -- >>= addTextureArray "data/texture/ayene_wooden_floor.png"
-- bind fixed vertex data -- bind fixed vertex data
@@ -147,6 +150,7 @@ preloadRender = do
, _wallTextureShader = wlTexture { _shaderVAO = wpColVAO } , _wallTextureShader = wlTexture { _shaderVAO = wpColVAO }
, _textureArrayShader = textArrayShad , _textureArrayShader = textArrayShad
, _fullscreenShader = fsShad , _fullscreenShader = fsShad
, _fullLightingShader = fullLightingShad
, _fullscreenAlphaHalveShader = fullscreenAlphaHalveShad , _fullscreenAlphaHalveShader = fullscreenAlphaHalveShad
, _bloomBlurShader = bloomBlurShad , _bloomBlurShader = bloomBlurShad
, _colorBlurShader = colorBlurShad , _colorBlurShader = colorBlurShad
+36 -16
View File
@@ -95,28 +95,41 @@ createLightMap pdata lightPoints nWalls nSils nsurfVs = do
drawShader (_lightingCapShader pdata) (nSils - 6) drawShader (_lightingCapShader pdata) (nSils - 6)
-- depthClamp $= Disabled -- depthClamp $= Disabled
-- use Lequal to redraw surfaces above already drawn surfaces depthFunc $= Just Always
depthFunc $= Just Lequal
-- draw geometry surfaces
--cullFace $= Just Back
cullFace $= Nothing cullFace $= Nothing
--colorMask $= Color4 Disabled Disabled Disabled Enabled bindTO $ snd $ snd $ _fboBase pdata
colorMask $= Color4 Enabled Enabled Enabled Enabled colorMask $= Color4 Enabled Enabled Enabled Enabled
stencilOp $= (OpKeep,OpKeep,OpKeep) stencilOp $= (OpKeep,OpKeep,OpKeep)
stencilFunc $= (Equal, 0, 255) stencilFunc $= (Equal, 0, 255)
currentProgram $= Just (_shaderProgram $ _lightingSurfaceShader pdata) currentProgram $= Just (_shaderProgram $ _fullLightingShader pdata)
uniform (head $ _shaderCustomUnis $ _lightingSurfaceShader pdata) uniform (head $ _shaderCustomUnis $ _fullLightingShader pdata)
$= Vector3 x y z $= Vector3 x y z
uniform (_shaderCustomUnis (_lightingSurfaceShader pdata) !! 1) uniform (_shaderCustomUnis (_fullLightingShader pdata) !! 1)
$= Vector4 r g b rad $= Vector4 r g b rad
drawShader (_lightingSurfaceShader pdata) nsurfVs drawShader (_fullLightingShader pdata) 4
-- draw wall light "circles"
currentProgram $= Just (_shaderProgram $ _lightingWallShader pdata) -- -- use Lequal to redraw surfaces above already drawn surfaces
uniform (head $ _shaderCustomUnis $ _lightingWallShader pdata) -- depthFunc $= Just Lequal
$= Vector3 x y z -- -- draw geometry surfaces
uniform (_shaderCustomUnis (_lightingWallShader pdata) !! 1) -- --cullFace $= Just Back
$= Vector4 r g b rad -- cullFace $= Nothing
drawShader (_lightingWallShader pdata) nWalls -- --colorMask $= Color4 Disabled Disabled Disabled Enabled
-- colorMask $= Color4 Enabled Enabled Enabled Enabled
-- stencilOp $= (OpKeep,OpKeep,OpKeep)
-- stencilFunc $= (Equal, 0, 255)
-- currentProgram $= Just (_shaderProgram $ _lightingSurfaceShader pdata)
-- uniform (head $ _shaderCustomUnis $ _lightingSurfaceShader pdata)
-- $= Vector3 x y z
-- uniform (_shaderCustomUnis (_lightingSurfaceShader pdata) !! 1)
-- $= Vector4 r g b rad
-- drawShader (_lightingSurfaceShader pdata) nsurfVs
-- -- draw wall light "circles"
-- currentProgram $= Just (_shaderProgram $ _lightingWallShader pdata)
-- uniform (head $ _shaderCustomUnis $ _lightingWallShader pdata)
-- $= Vector3 x y z
-- uniform (_shaderCustomUnis (_lightingWallShader pdata) !! 1)
-- $= Vector4 r g b rad
-- drawShader (_lightingWallShader pdata) nWalls
cullFace $= Nothing cullFace $= Nothing
stencilTest $= Disabled stencilTest $= Disabled
{- | Blur between two framebuffers. {- | Blur between two framebuffers.
@@ -211,3 +224,10 @@ pokeFourOff ptr n (x,y,z,w) = do
pokeElemOff ptr (4*n+1) y pokeElemOff ptr (4*n+1) y
pokeElemOff ptr (4*n+2) z pokeElemOff ptr (4*n+2) z
pokeElemOff ptr (4*n+3) w pokeElemOff ptr (4*n+3) w
bindTO :: TextureObject -> IO ()
bindTO t = textureBinding Texture2D $= Just t
bindFBO :: FramebufferObject -> IO ()
bindFBO fb = bindFramebuffer Framebuffer $= fb