From 8bd8db922e4d6f39a18d603532af4467aae42435 Mon Sep 17 00:00:00 2001 From: Stefan Dresselhaus Date: Tue, 13 May 2014 10:40:35 +0200 Subject: [PATCH] added shadow-map - added shadow-map - compiles and smap gets initialized/generated - generation ist stil incorrect (cam, light-dir, ...) --- src/Main.hs | 8 +++ src/Render/Render.hs | 152 ++++++++++++++++++++++++++++++------------- src/Types.hs | 1 + 3 files changed, 114 insertions(+), 47 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index 0aa092d..7db4c40 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -304,6 +304,7 @@ adjustWindow = do let hudtexid = state ^. gl.glHud.hudTexture maptexid = state ^. gl.glMap.renderedMapTexture + smaptexid = state ^. gl.glMap.shadowMapTexture allocaBytes (fbWidth*fbHeight*4) $ \ptr -> do --default to ugly pink to see if --somethings go wrong. @@ -320,6 +321,13 @@ adjustWindow = do textureFilter Texture2D GL.$= ((Linear', Nothing), Linear') texImage2D Texture2D GL.NoProxy 0 RGBA8 (GL.TextureSize2D fbCWidth fbCHeight) 0 (GL.PixelData GL.RGBA GL.UnsignedByte ptr) + allocaBytes (2048*2048) $ \ptr -> do + let smapdata = genColorData (2048*2048) [0] + pokeArray ptr smapdata + textureBinding Texture2D GL.$= Just smaptexid + textureFilter Texture2D GL.$= ((Nearest,Nothing), Nearest) + texImage2D Texture2D GL.NoProxy 0 GL.DepthComponent16 (GL.TextureSize2D 2048 2048) 0 + (GL.PixelData GL.DepthComponent GL.UnsignedByte ptr) checkError "setting up HUD-Tex" return renderBuffer modify $ gl.glRenderbuffer .~ rb diff --git a/src/Render/Render.hs b/src/Render/Render.hs index c6e4369..39fde96 100644 --- a/src/Render/Render.hs +++ b/src/Render/Render.hs @@ -119,6 +119,8 @@ initMapShader tessFac (buf, vertDes) = do overTex <- genObjectName texts <- genObjectNames 6 + + smap <- genObjectName testobj <- parseIQM "sample.iqm" @@ -154,6 +156,7 @@ initMapShader tessFac (buf, vertDes) = do , _mapVert = vertDes , _overviewTexture = overTex , _mapTextures = texts + , _shadowMapTexture = smap , _mapObjects = objs , _objectProgram = objProgram } @@ -190,7 +193,7 @@ initHud = do att <- get (activeAttribs program) putStrLn $ unlines $ "Attributes: ":map show att - putStrLn $ unlines $ ["Indices: ", show (texIndex)] + putStrLn $ unlines $ ["Indices: ", show texIndex] checkError "initHud" return GLHud @@ -299,38 +302,123 @@ renderObject :: MapObject -> IO () renderObject (MapObject model pos@(L.V3 x y z) _{-state-}) = renderIQM model pos (L.V3 1 1 1) +drawMap :: Pioneers () +drawMap = do + state <- RWS.get + let + vi = state ^. gl.glMap.shdrVertexIndex + ni = state ^. gl.glMap.shdrNormalIndex + ci = state ^. gl.glMap.shdrColorIndex + 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 + liftIO $ do + glUniform1f tli (fromIntegral tessFac) + glUniform1f tlo (fromIntegral tessFac) + + bindBuffer ArrayBuffer $= Just map' + vertexAttribPointer ci $= fgColorIndex + vertexAttribArray ci $= Enabled + vertexAttribPointer ni $= fgNormalIndex + vertexAttribArray ni $= Enabled + vertexAttribPointer vi $= fgVertexIndex + vertexAttribArray vi $= Enabled + checkError "beforeDraw" + + glPatchParameteri gl_PATCH_VERTICES 3 + + cullFace $= Just Front + + glDrawArrays gl_PATCHES 0 (fromIntegral numVert) + + checkError "draw map" + + ---- RENDER MAPOBJECTS -------------------------------------------- + + currentProgram $= Just (state ^. gl.glMap.objectProgram) + + mapM_ renderObject (state ^. gl.glMap.mapObjects) + + -- set sample 1 as target in renderbuffer + {-framebufferRenderbuffer + DrawFramebuffer --write-only + (ColorAttachment 1) --sample 1 + Renderbuffer --const + rb --buffer-} render :: Pioneers () render = do state <- RWS.get let xa = state ^. camera.xAngle ya = state ^. camera.yAngle - (UniformLocation proj) = state ^. gl.glMap.shdrProjMatIndex - (UniformLocation nmat) = state ^. gl.glMap.shdrNormalMatIndex - (UniformLocation vmat) = state ^. gl.glMap.shdrViewMatIndex - (UniformLocation tli) = state ^. gl.glMap.shdrTessInnerIndex - (UniformLocation tlo) = state ^. gl.glMap.shdrTessOuterIndex - vi = state ^. gl.glMap.shdrVertexIndex - ni = state ^. gl.glMap.shdrNormalIndex - ci = state ^. gl.glMap.shdrColorIndex - numVert = state ^. gl.glMap.mapVert - map' = state ^. gl.glMap.stateMap frust = state ^. camera.Types.frustum camPos = state ^. camera.camObject zDist' = state ^. camera.zDist - tessFac = state ^. gl.glMap.stateTessellationFactor + (UniformLocation proj) = state ^. gl.glMap.shdrProjMatIndex + (UniformLocation nmat) = state ^. gl.glMap.shdrNormalMatIndex + (UniformLocation vmat) = state ^. gl.glMap.shdrViewMatIndex liftIO $ do ---- RENDER MAP IN TEXTURE ------------------------------------------ bindFramebuffer Framebuffer $= (state ^. gl.glFramebuffer) - bindRenderbuffer Renderbuffer $= (state ^. gl.glRenderbuffer) + {-bindRenderbuffer Renderbuffer $= (state ^. gl.glRenderbuffer) framebufferRenderbuffer Framebuffer DepthAttachment Renderbuffer - (state ^. gl.glRenderbuffer) - textureBinding Texture2D $= Just (state ^. gl.glMap.renderedMapTexture) + (state ^. gl.glRenderbuffer)-} + + -- SHADOWMAP + textureBinding Texture2D $= Just (state ^. gl.glMap.shadowMapTexture) + framebufferTexture2D + Framebuffer + DepthAttachment + Texture2D + (state ^. gl.glMap.shadowMapTexture) + 0 + drawBuffer $= NoBuffers --color-buffer is not needed but must(?) be set up + checkError "setup Render-Target" + + clear [DepthBuffer] + checkError "clearing shadowmap-buffer" + + --TODO: simplified program for shadows? + currentProgram $= Just (state ^. gl.glMap.mapProgram) + checkError "setting up shadowmap-program" + + --set up projection (= copy from state) + --TODO: Fix + with (distribute frust) $ \ptr -> + glUniformMatrix4fv proj 1 0 (castPtr (ptr :: Ptr (L.M44 CFloat))) + checkError "copy shadowmap-projection" + + --set up camera + --TODO: Fix + let ! cam = getCam camPos zDist' xa ya + with (distribute cam) $ \ptr -> + glUniformMatrix4fv vmat 1 0 (castPtr (ptr :: Ptr (L.M44 CFloat))) + checkError "copy shadowmap-cam" + + --set up normal--Mat transpose((model*camera)^-1) + --needed? + let normal' = (case L.inv33 (fmap (^. L._xyz) cam ^. L._xyz) of + (Just a) -> a + Nothing -> L.eye3) :: L.M33 CFloat + nmap = collect id normal' :: L.M33 CFloat --transpose... + + with (distribute nmap) $ \ptr -> + glUniformMatrix3fv nmat 1 0 (castPtr (ptr :: Ptr (L.M33 CFloat))) + + checkError "nmat" + drawMap + liftIO $ do + checkError "draw ShadowMap" + + -- COLORMAP + textureBinding Texture2D $= Just (state ^. gl.glMap.renderedMapTexture) framebufferTexture2D Framebuffer (ColorAttachment 0) @@ -371,38 +459,8 @@ render = do checkError "nmat" - glUniform1f tli (fromIntegral tessFac) - glUniform1f tlo (fromIntegral tessFac) - - bindBuffer ArrayBuffer $= Just map' - vertexAttribPointer ci $= fgColorIndex - vertexAttribArray ci $= Enabled - vertexAttribPointer ni $= fgNormalIndex - vertexAttribArray ni $= Enabled - vertexAttribPointer vi $= fgVertexIndex - vertexAttribArray vi $= Enabled - checkError "beforeDraw" - - glPatchParameteri gl_PATCH_VERTICES 3 - - cullFace $= Just Front - - glDrawArrays gl_PATCHES 0 (fromIntegral numVert) - - checkError "draw map" - - ---- RENDER MAPOBJECTS -------------------------------------------- - - currentProgram $= Just (state ^. gl.glMap.objectProgram) - - mapM_ renderObject (state ^. gl.glMap.mapObjects) - - -- set sample 1 as target in renderbuffer - {-framebufferRenderbuffer - DrawFramebuffer --write-only - (ColorAttachment 1) --sample 1 - Renderbuffer --const - rb --buffer-} + drawMap --draw map -> put to another function for readability + liftIO $ do ---- COMPOSE RENDERING -------------------------------------------- -- Render to BackBuffer (=Screen) diff --git a/src/Types.hs b/src/Types.hs index 8583f1a..95192bd 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -116,6 +116,7 @@ data GLMapState = GLMapState , _mapProgram :: !GL.Program , _renderedMapTexture :: !TextureObject --TODO: Probably move to UI? , _overviewTexture :: !TextureObject + , _shadowMapTexture :: !TextureObject , _mapTextures :: ![TextureObject] --TODO: Fix size on list? , _objectProgram :: !GL.Program , _mapObjects :: ![MapObject]