added depth-buffer to map
This commit is contained in:
		
							
								
								
									
										18
									
								
								src/Main.hs
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								src/Main.hs
									
									
									
									
									
								
							@@ -90,8 +90,9 @@ main = do
 | 
			
		||||
        initRendering
 | 
			
		||||
        --generate map vertices
 | 
			
		||||
        curMap <- exportedMap
 | 
			
		||||
        (glMap', tex) <- initMapShader 4 =<< getMapBufferObject curMap
 | 
			
		||||
        (glMap', tex, dtex) <- initMapShader 4 =<< getMapBufferObject curMap
 | 
			
		||||
        tex' <- newTVarIO tex
 | 
			
		||||
        dtex' <- newTVarIO dtex
 | 
			
		||||
        eventQueue <- newTQueueIO :: IO (TQueue SDL.Event)
 | 
			
		||||
        now <- getCurrentTime
 | 
			
		||||
        --font <- TTF.openFont "fonts/ttf-04B_03B_/04B_03B_.TTF" 10
 | 
			
		||||
@@ -143,6 +144,7 @@ main = do
 | 
			
		||||
                        }
 | 
			
		||||
              , _camera              = cam'
 | 
			
		||||
              , _mapTexture          = tex'
 | 
			
		||||
              , _mapDepthTexture     = dtex'
 | 
			
		||||
              , _camStack            = camStack'
 | 
			
		||||
              , _keyboard            = KeyboardState
 | 
			
		||||
                        { _arrowsPressed       = aks
 | 
			
		||||
@@ -296,7 +298,9 @@ adjustWindow = do
 | 
			
		||||
 | 
			
		||||
                   let hudtexid = state ^. gl.glHud.hudTexture
 | 
			
		||||
                       smaptexid = state ^. gl.glMap.shadowMapTexture
 | 
			
		||||
                   maptexid <- liftIO $ readTVarIO (state ^. mapTexture)
 | 
			
		||||
                   maptexid <- readTVarIO (state ^. mapTexture)
 | 
			
		||||
                   mapdepthtexid <- readTVarIO (state ^. mapDepthTexture)
 | 
			
		||||
                   -- create & clear textures for hud & background (map)
 | 
			
		||||
                   allocaBytes (fbWidth*fbHeight*4) $ \ptr -> do
 | 
			
		||||
                                                               --default to ugly pink to see if
 | 
			
		||||
                                                               --somethings go wrong.
 | 
			
		||||
@@ -315,6 +319,16 @@ adjustWindow = do
 | 
			
		||||
                        texImage2D Texture2D GL.NoProxy 0 RGBA8 (GL.TextureSize2D fbCWidth fbCHeight) 0
 | 
			
		||||
                                                (GL.PixelData GL.RGBA GL.UnsignedByte ptr)
 | 
			
		||||
                        textureBinding Texture2D GL.$= Nothing
 | 
			
		||||
                   -- create & clear map depth texture
 | 
			
		||||
                   allocaBytes (fbWidth*fbHeight) $ \ptr -> do
 | 
			
		||||
                        let smapdata = genColorData (fbWidth*fbHeight) [0]
 | 
			
		||||
                        pokeArray ptr smapdata
 | 
			
		||||
                        textureBinding Texture2D GL.$= Just mapdepthtexid
 | 
			
		||||
                        textureFilter  Texture2D GL.$= ((Linear', Nothing), Linear')
 | 
			
		||||
                        texImage2D Texture2D GL.NoProxy 0 GL.DepthComponent16 (GL.TextureSize2D fbCWidth fbCHeight) 0
 | 
			
		||||
                                                (GL.PixelData GL.DepthComponent GL.UnsignedByte ptr)
 | 
			
		||||
                        textureBinding Texture2D GL.$= Nothing
 | 
			
		||||
                   -- create & clear depth texture for shadows
 | 
			
		||||
                   allocaBytes (2048*2048) $ \ptr -> do
 | 
			
		||||
                        let smapdata = genColorData (2048*2048) [0]
 | 
			
		||||
                        pokeArray ptr smapdata
 | 
			
		||||
 
 | 
			
		||||
@@ -62,7 +62,7 @@ initBuffer varray =
 | 
			
		||||
initMapShader ::
 | 
			
		||||
                Int                                -- ^ initial Tessallation-Factor
 | 
			
		||||
                -> (BufferObject,NumArrayIndices)  -- ^ Buffer with Data and DataDescriptor
 | 
			
		||||
                -> IO (GLMapState, TextureObject)
 | 
			
		||||
                -> IO (GLMapState, TextureObject, TextureObject)
 | 
			
		||||
initMapShader tessFac (buf, vertDes) = do
 | 
			
		||||
   ! vertexSource <- B.readFile mapVertexShaderFile
 | 
			
		||||
   ! tessControlSource <- B.readFile mapTessControlShaderFile
 | 
			
		||||
@@ -122,6 +122,7 @@ initMapShader tessFac (buf, vertDes) = do
 | 
			
		||||
   putStrLn $ unlines ["Map-Indices: ", show (colorIndex, normalIndex, vertexIndex)]
 | 
			
		||||
 | 
			
		||||
   tex <- genObjectName
 | 
			
		||||
   dtex <- genObjectName
 | 
			
		||||
   overTex <- genObjectName
 | 
			
		||||
 | 
			
		||||
   textures <- genObjectNames 6
 | 
			
		||||
@@ -224,7 +225,7 @@ initMapShader tessFac (buf, vertDes) = do
 | 
			
		||||
        , _mapObjects         = objs
 | 
			
		||||
        , _objectProgram      = objProgram
 | 
			
		||||
        , _shadowMapProgram   = shadowProgram
 | 
			
		||||
        }, tex)
 | 
			
		||||
        }, tex, dtex)
 | 
			
		||||
 | 
			
		||||
initHud :: IO GLHud
 | 
			
		||||
initHud = do
 | 
			
		||||
@@ -397,16 +398,16 @@ render = do
 | 
			
		||||
                (state ^. gl.glRenderbuffer)-}
 | 
			
		||||
 | 
			
		||||
        ---- RENDER SHADOWMAP <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 | 
			
		||||
    liftIO $ do
 | 
			
		||||
--        textureBinding Texture2D $= Just (state ^. gl.glMap.shadowMapTexture)
 | 
			
		||||
{-        framebufferTexture2D
 | 
			
		||||
{-    liftIO $ do
 | 
			
		||||
        textureBinding Texture2D $= Just (state ^. gl.glMap.shadowMapTexture)
 | 
			
		||||
        framebufferTexture2D
 | 
			
		||||
                Framebuffer
 | 
			
		||||
                DepthAttachment
 | 
			
		||||
                Texture2D
 | 
			
		||||
                (state ^. gl.glMap.shadowMapTexture)
 | 
			
		||||
                0-}
 | 
			
		||||
                0
 | 
			
		||||
 | 
			
		||||
 --       drawBuffer $= NoBuffers --color-buffer is not needed but must(?) be set up
 | 
			
		||||
        drawBuffer $= NoBuffers --color-buffer is not needed but must(?) be set up
 | 
			
		||||
        checkError "setup Render-Target"
 | 
			
		||||
 | 
			
		||||
        clear [DepthBuffer]
 | 
			
		||||
@@ -426,7 +427,7 @@ render = do
 | 
			
		||||
 | 
			
		||||
--    drawMap
 | 
			
		||||
 | 
			
		||||
{-    liftIO $ do
 | 
			
		||||
    liftIO $ do
 | 
			
		||||
        ---- RENDER MAPOBJECTS --------------------------------------------
 | 
			
		||||
        currentProgram $= Just (state ^. gl.glMap.objectProgram)
 | 
			
		||||
        checkError "setting up shadowmap-program"
 | 
			
		||||
@@ -453,14 +454,22 @@ render = do
 | 
			
		||||
    liftIO $ do
 | 
			
		||||
        {-bindFramebuffer Framebuffer $= defaultFramebufferObject
 | 
			
		||||
        drawBuffer $= BackBuffers-}
 | 
			
		||||
        tex <- liftIO $ readTVarIO (state ^. mapTexture)
 | 
			
		||||
        textureBinding Texture2D $= Just tex
 | 
			
		||||
        tex  <- readTVarIO (state ^. mapTexture)
 | 
			
		||||
        dtex <- readTVarIO (state ^. mapDepthTexture)
 | 
			
		||||
        -- add color to texture target
 | 
			
		||||
        framebufferTexture2D
 | 
			
		||||
                Framebuffer
 | 
			
		||||
                (ColorAttachment 0)
 | 
			
		||||
                Texture2D
 | 
			
		||||
                tex
 | 
			
		||||
                0
 | 
			
		||||
        -- add depth to texture target
 | 
			
		||||
        framebufferTexture2D
 | 
			
		||||
                Framebuffer
 | 
			
		||||
                DepthAttachment
 | 
			
		||||
                Texture2D
 | 
			
		||||
                dtex
 | 
			
		||||
                0
 | 
			
		||||
 | 
			
		||||
        -- Render to FrameBufferObject
 | 
			
		||||
        drawBuffers $= [FBOColorAttachment 0]
 | 
			
		||||
@@ -512,7 +521,7 @@ render = do
 | 
			
		||||
        uniform (hud ^. hudTexIndex) $= Index1 (0::GLint)
 | 
			
		||||
 | 
			
		||||
        activeTexture  $= TextureUnit 1
 | 
			
		||||
        tex <- liftIO $ readTVarIO (state ^. mapTexture)
 | 
			
		||||
        tex <- readTVarIO (state ^. mapTexture)
 | 
			
		||||
        textureBinding Texture2D $= Just tex
 | 
			
		||||
        uniform (hud ^. hudBackIndex) $= Index1 (1::GLint)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -174,6 +174,7 @@ data State = State
 | 
			
		||||
    { _window              :: !WindowState
 | 
			
		||||
    , _camera              :: TVar CameraState
 | 
			
		||||
    , _mapTexture          :: TVar TextureObject
 | 
			
		||||
    , _mapDepthTexture     :: TVar TextureObject
 | 
			
		||||
    , _camStack            :: (Map.HashMap UIId (TVar CameraState, TVar TextureObject))
 | 
			
		||||
    , _io                  :: !IOState
 | 
			
		||||
    , _keyboard            :: !KeyboardState
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user