From dd12f7b136ffb94360082702801a6ef1fbe141be Mon Sep 17 00:00:00 2001 From: Stefan Dresselhaus Date: Tue, 13 May 2014 11:25:06 +0200 Subject: [PATCH] seperated shaders - shadowmap now uses own shader - not used only defined - changed a bit in tyes. needs refinement - new indices need to be read out and used. refs #495 @3h --- src/Render/Misc.hs | 13 ++++++++++-- src/Render/Render.hs | 49 ++++++++++++++++++++++++++++---------------- src/Types.hs | 23 +++++++++++++-------- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/src/Render/Misc.hs b/src/Render/Misc.hs index 48e84f9..0fbf58c 100644 --- a/src/Render/Misc.hs +++ b/src/Render/Misc.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE ForeignFunctionInterface #-} module Render.Misc where import Control.Monad @@ -75,6 +74,16 @@ createFrustum fov n' f' rat = (V4 0 0 (-((f+n)/(f-n))) (-((2*f*n)/(f-n)))) (V4 0 0 (-1) 0) +-- | Creates an orthogonal frustum with given width, height, near and far-plane +createFrustumOrtho :: Float -> Float -> Float -> Float -> M44 CFloat +createFrustumOrtho w' h' n' f' = + let [w,h,n,f] = map realToFrac [w',h',n',f'] + in + V4 (V4 (0.5/w) 0 0 0) + (V4 0 (0.5/h) 0 0) + (V4 0 0 (-2/(f-n)) ((-f+n)/(f-n))) + (V4 0 0 0 1) + -- from vmath.h lookAt :: V3 CFloat -> V3 CFloat -> V3 CFloat -> M44 CFloat lookAt eye center up' = @@ -128,5 +137,5 @@ tryWithTexture t f fail' = genColorData :: Int -- ^ Amount -> [Int8] -- ^ [r,g,b,a], [r,g,b] - whatever should be repeatet. -> [Int8] -genColorData n c = take ((length c)*n) (cycle c) +genColorData n c = take (length c*n) (cycle c) diff --git a/src/Render/Render.hs b/src/Render/Render.hs index 39fde96..c51bbd9 100644 --- a/src/Render/Render.hs +++ b/src/Render/Render.hs @@ -33,6 +33,8 @@ mapTessEvalShaderFile :: String mapTessEvalShaderFile = "shaders/map/tessEval.shader" mapFragmentShaderFile :: String mapFragmentShaderFile = "shaders/map/fragment.shader" +mapFragmentShaderShadowMapFile :: String +mapFragmentShaderShadowMapFile = "shaders/map/fragmentShadow.shader" objectVertexShaderFile :: String objectVertexShaderFile = "shaders/mapobjects/vertex.shader" @@ -66,6 +68,7 @@ initMapShader tessFac (buf, vertDes) = do ! tessControlSource <- B.readFile mapTessControlShaderFile ! tessEvalSource <- B.readFile mapTessEvalShaderFile ! fragmentSource <- B.readFile mapFragmentShaderFile + ! fragmentShadowSource <- B.readFile mapFragmentShaderShadowMapFile vertexShader <- compileShaderSource VertexShader vertexSource checkError "compile Vertex" tessControlShader <- compileShaderSource TessControlShader tessControlSource @@ -74,7 +77,10 @@ initMapShader tessFac (buf, vertDes) = do checkError "compile TessEval" fragmentShader <- compileShaderSource FragmentShader fragmentSource checkError "compile Frag" + fragmentShadowShader <- compileShaderSource FragmentShader fragmentShadowSource + checkError "compile Frag" program <- createProgramUsing [vertexShader, tessControlShader, tessEvalShader, fragmentShader] + shadowProgram <- createProgramUsing [vertexShader, tessControlShader, tessEvalShader, fragmentShadowShader] checkError "compile Program" currentProgram $= Just program @@ -139,17 +145,21 @@ initMapShader tessFac (buf, vertDes) = do currentProgram $= Just objProgram checkError "initShader" + let sdata = MapShaderData + { shdrVertexIndex = vertexIndex + , shdrColorIndex = colorIndex + , shdrNormalIndex = normalIndex + , shdrProjMatIndex = projectionMatrixIndex + , shdrViewMatIndex = viewMatrixIndex + , shdrModelMatIndex = modelMatrixIndex + , shdrNormalMatIndex = normalMatrixIndex + , shdrTessInnerIndex = tessLevelInner + , shdrTessOuterIndex = tessLevelOuter + } + return GLMapState { _mapProgram = program - , _shdrColorIndex = colorIndex - , _shdrNormalIndex = normalIndex - , _shdrVertexIndex = vertexIndex - , _shdrProjMatIndex = projectionMatrixIndex - , _shdrViewMatIndex = viewMatrixIndex - , _shdrModelMatIndex = modelMatrixIndex - , _shdrNormalMatIndex = normalMatrixIndex - , _shdrTessInnerIndex = tessLevelInner - , _shdrTessOuterIndex = tessLevelOuter + , _mapShaderData = sdata , _renderedMapTexture = tex , _stateTessellationFactor = tessFac , _stateMap = buf @@ -159,6 +169,7 @@ initMapShader tessFac (buf, vertDes) = do , _shadowMapTexture = smap , _mapObjects = objs , _objectProgram = objProgram + , _shadowMapProgram = shadowProgram } initHud :: IO GLHud @@ -306,14 +317,15 @@ drawMap :: Pioneers () drawMap = do state <- RWS.get let - vi = state ^. gl.glMap.shdrVertexIndex - ni = state ^. gl.glMap.shdrNormalIndex - ci = state ^. gl.glMap.shdrColorIndex + d = state ^. gl.glMap.mapShaderData + vi = shdrVertexIndex d + ni = shdrNormalIndex d + ci = shdrColorIndex d numVert = state ^. gl.glMap.mapVert map' = state ^. gl.glMap.stateMap tessFac = state ^. gl.glMap.stateTessellationFactor - (UniformLocation tli) = state ^. gl.glMap.shdrTessInnerIndex - (UniformLocation tlo) = state ^. gl.glMap.shdrTessOuterIndex + (UniformLocation tli) = shdrTessInnerIndex d + (UniformLocation tlo) = shdrTessOuterIndex d liftIO $ do glUniform1f tli (fromIntegral tessFac) glUniform1f tlo (fromIntegral tessFac) @@ -356,9 +368,10 @@ render = do frust = state ^. camera.Types.frustum camPos = state ^. camera.camObject zDist' = state ^. camera.zDist - (UniformLocation proj) = state ^. gl.glMap.shdrProjMatIndex - (UniformLocation nmat) = state ^. gl.glMap.shdrNormalMatIndex - (UniformLocation vmat) = state ^. gl.glMap.shdrViewMatIndex + d = state ^. gl.glMap.mapShaderData + (UniformLocation proj) = shdrProjMatIndex d + (UniformLocation nmat) = shdrNormalMatIndex d + (UniformLocation vmat) = shdrViewMatIndex d liftIO $ do ---- RENDER MAP IN TEXTURE ------------------------------------------ @@ -391,7 +404,7 @@ render = do --set up projection (= copy from state) --TODO: Fix - with (distribute frust) $ \ptr -> + with (distribute (createFrustumOrtho 20 20 0 100)) $ \ptr -> glUniformMatrix4fv proj 1 0 (castPtr (ptr :: Ptr (L.M44 CFloat))) checkError "copy shadowmap-projection" diff --git a/src/Types.hs b/src/Types.hs index 95192bd..f16333c 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -101,15 +101,7 @@ data KeyboardState = KeyboardState data GLMapState = GLMapState - { _shdrVertexIndex :: !GL.AttribLocation - , _shdrColorIndex :: !GL.AttribLocation - , _shdrNormalIndex :: !GL.AttribLocation - , _shdrProjMatIndex :: !GL.UniformLocation - , _shdrViewMatIndex :: !GL.UniformLocation - , _shdrModelMatIndex :: !GL.UniformLocation - , _shdrNormalMatIndex :: !GL.UniformLocation - , _shdrTessInnerIndex :: !GL.UniformLocation - , _shdrTessOuterIndex :: !GL.UniformLocation + { _mapShaderData :: !MapShaderData , _stateTessellationFactor :: !Int , _stateMap :: !GL.BufferObject , _mapVert :: !GL.NumArrayIndices @@ -120,6 +112,19 @@ data GLMapState = GLMapState , _mapTextures :: ![TextureObject] --TODO: Fix size on list? , _objectProgram :: !GL.Program , _mapObjects :: ![MapObject] + , _shadowMapProgram :: !GL.Program + } + +data MapShaderData = MapShaderData + { shdrVertexIndex :: !GL.AttribLocation + , shdrColorIndex :: !GL.AttribLocation + , shdrNormalIndex :: !GL.AttribLocation + , shdrProjMatIndex :: !GL.UniformLocation + , shdrViewMatIndex :: !GL.UniformLocation + , shdrModelMatIndex :: !GL.UniformLocation + , shdrNormalMatIndex :: !GL.UniformLocation + , shdrTessInnerIndex :: !GL.UniformLocation + , shdrTessOuterIndex :: !GL.UniformLocation } data MapObject = MapObject !IQM !MapCoordinates !MapObjectState