Textures work now.

Scene gets rendered and alpha-overlayed with HUD-Texture.

fixes #472 @2h
This commit is contained in:
Nicole Dresselhaus 2014-04-05 15:53:49 +02:00
parent 39341bf0f9
commit 189f9f84fa
4 changed files with 117 additions and 19 deletions

View File

@ -1,10 +1,11 @@
#version 110 #version 110
uniform sampler2D tex; uniform sampler2D tex[2];
varying vec2 texcoord; varying vec2 texcoord;
void main() void main()
{ {
vec4 map = texture2D(tex[0], texcoord);
gl_FragColor = texture2D(tex, texcoord); vec4 hud = texture2D(tex[1], texcoord);
gl_FragColor = vec4(mix(map.rgb,hud.rgb,hud.a),1.0);
} }

View File

@ -87,11 +87,18 @@ main = do
withOpenGL window $ do withOpenGL window $ do
--TTF.withInit $ do --TTF.withInit $ do
--Create Renderbuffer & Framebuffer
-- We will render to this buffer to copy the result into textures
renderBuffer <- GL.genObjectName
frameBuffer <- GL.genObjectName
GL.bindFramebuffer GL.Framebuffer GL.$= frameBuffer
GL.bindRenderbuffer GL.Renderbuffer GL.$= renderBuffer
(Size fbWidth fbHeight) <- glGetDrawableSize window (Size fbWidth fbHeight) <- glGetDrawableSize window
initRendering initRendering
--generate map vertices --generate map vertices
(mapBuffer, vert) <- getMapBufferObject (mapBuffer, vert) <- getMapBufferObject
(mapprog, ci, ni, vi, pri, vii, mi, nmi, tli, tlo) <- initMapShader (mapprog, ci, ni, vi, pri, vii, mi, nmi, tli, tlo, mapTex) <- initMapShader
putStrLn $ show window putStrLn $ show window
eventQueue <- newTQueueIO :: IO (TQueue Event) eventQueue <- newTQueueIO :: IO (TQueue Event)
putStrLn "foo" putStrLn "foo"
@ -130,6 +137,7 @@ main = do
, _stateMap = mapBuffer , _stateMap = mapBuffer
, _mapVert = vert , _mapVert = vert
, _mapProgram = mapprog , _mapProgram = mapprog
, _mapTexture = mapTex
} }
env = Env env = Env
{ _eventsChan = eventQueue { _eventsChan = eventQueue
@ -176,6 +184,8 @@ main = do
, _gl = GLState , _gl = GLState
{ _glMap = glMap { _glMap = glMap
, _glHud = glHud , _glHud = glHud
, _glRenderbuffer = renderBuffer
, _glFramebuffer = frameBuffer
} }
, _game = GameState , _game = GameState
{ {
@ -216,14 +226,52 @@ draw = do
zDist' = state ^. camera.zDist zDist' = state ^. camera.zDist
tessFac = state ^. gl.glMap.stateTessellationFactor tessFac = state ^. gl.glMap.stateTessellationFactor
window = env ^. windowObject window = env ^. windowObject
rb = state ^. gl.glRenderbuffer
prepareGUI prepareGUI
liftIO $ do liftIO $ do
--bind renderbuffer and set sample 0 as target
--GL.bindRenderbuffer GL.Renderbuffer GL.$= rb
--GL.bindFramebuffer GL.Framebuffer GL.$= GL.defaultFramebufferObject
--checkError "bind renderbuffer"
--checkError "clear renderbuffer"
{-GL.framebufferRenderbuffer
GL.Framebuffer --framebuffer
(GL.ColorAttachment 1) --sample 1
GL.Renderbuffer --const
rb --buffer
checkError "setup renderbuffer"-}
-- draw map
--(vi,GL.UniformLocation proj) <- initShader --(vi,GL.UniformLocation proj) <- initShader
GL.bindFramebuffer GL.Framebuffer GL.$= (state ^. gl.glFramebuffer)
GL.bindRenderbuffer GL.Renderbuffer GL.$= (state ^. gl.glRenderbuffer)
GL.framebufferRenderbuffer
GL.Framebuffer
GL.DepthAttachment
GL.Renderbuffer
(state ^. gl.glRenderbuffer)
textureBinding GL.Texture2D GL.$= Just (state ^. gl.glMap.mapTexture)
GL.framebufferTexture2D
GL.Framebuffer
(GL.ColorAttachment 0)
GL.Texture2D
(state ^. gl.glMap.mapTexture)
0
-- Render to FrameBufferObject
GL.drawBuffers GL.$= [GL.FBOColorAttachment 0]
checkError "setup Render-Target"
GL.clear [GL.ColorBuffer, GL.DepthBuffer] GL.clear [GL.ColorBuffer, GL.DepthBuffer]
checkError "clear buffer"
GL.currentProgram GL.$= Just (state ^. gl.glMap.mapProgram) GL.currentProgram GL.$= Just (state ^. gl.glMap.mapProgram)
checkError "clearing buffer" checkError "setting up buffer"
--set up projection (= copy from state) --set up projection (= copy from state)
with (distribute frust) $ \ptr -> with (distribute frust) $ \ptr ->
glUniformMatrix4fv proj 1 0 (castPtr (ptr :: Ptr (M44 CFloat))) glUniformMatrix4fv proj 1 0 (castPtr (ptr :: Ptr (M44 CFloat)))
@ -264,13 +312,32 @@ draw = do
glDrawArrays gl_PATCHES 0 (fromIntegral numVert) glDrawArrays gl_PATCHES 0 (fromIntegral numVert)
checkError "draw map" checkError "draw map"
-- set sample 1 as target in renderbuffer
{-GL.framebufferRenderbuffer
GL.DrawFramebuffer --write-only
(GL.ColorAttachment 1) --sample 1
GL.Renderbuffer --const
rb --buffer-}
-- Render to BackBuffer (=Screen)
GL.bindFramebuffer GL.Framebuffer GL.$= GL.defaultFramebufferObject
GL.drawBuffer GL.$= GL.BackBuffers
-- Drawing HUD -- Drawing HUD
GL.clear [GL.ColorBuffer, GL.DepthBuffer]
checkError "clear buffer"
let hud = state ^. gl.glHud let hud = state ^. gl.glHud
stride = fromIntegral $ sizeOf (undefined::GLfloat) * 2 stride = fromIntegral $ sizeOf (undefined::GLfloat) * 2
vad = GL.VertexArrayDescriptor 2 GL.Float stride offset0 vad = GL.VertexArrayDescriptor 2 GL.Float stride offset0
GL.currentProgram GL.$= Just (hud ^. hudProgram) GL.currentProgram GL.$= Just (hud ^. hudProgram)
GL.activeTexture GL.$= GL.TextureUnit 0 GL.activeTexture GL.$= GL.TextureUnit 0
textureBinding GL.Texture2D GL.$= Just (hud ^. hudTexture) textureBinding GL.Texture2D GL.$= Just (hud ^. hudTexture)
GL.uniform (hud ^. hudTexIndex) GL.$= GL.Index1 (0::GL.GLint)
GL.activeTexture GL.$= GL.TextureUnit 1
textureBinding GL.Texture2D GL.$= Just (state ^. gl.glMap.mapTexture)
GL.uniform (hud ^. hudBackIndex) GL.$= GL.Index1 (1::GL.GLint)
GL.bindBuffer GL.ArrayBuffer GL.$= Just (hud ^. hudVBO) GL.bindBuffer GL.ArrayBuffer GL.$= Just (hud ^. hudVBO)
GL.vertexAttribPointer (hud ^. hudVertexIndex) GL.$= (GL.ToFloat, vad) GL.vertexAttribPointer (hud ^. hudVertexIndex) GL.$= (GL.ToFloat, vad)
@ -331,7 +398,7 @@ run = do
mults = sin $ state ^. camera.yAngle mults = sin $ state ^. camera.yAngle
modx x' = x' - 0.2 * kxrot * multc modx x' = x' - 0.2 * kxrot * multc
- 0.2 * kyrot * mults - 0.2 * kyrot * mults
mody y' = y' - 0.2 * kxrot * mults mody y' = y' + 0.2 * kxrot * mults
- 0.2 * kyrot * multc - 0.2 * kyrot * multc
modify $ (camera.camPosition.x %~ modx) modify $ (camera.camPosition.x %~ modx)
. (camera.camPosition.y %~ mody) . (camera.camPosition.y %~ mody)
@ -383,20 +450,40 @@ adjustWindow = do
frust = createFrustum fov near far ratio frust = createFrustum fov near far ratio
liftIO $ glViewport 0 0 (fromIntegral fbWidth) (fromIntegral fbHeight) liftIO $ glViewport 0 0 (fromIntegral fbWidth) (fromIntegral fbHeight)
modify $ camera.frustum .~ frust modify $ camera.frustum .~ frust
liftIO $ do rb <- liftIO $ do
let texid = state ^. gl.glHud.hudTexture -- bind ints to CInt for lateron.
int = fromInteger . toInteger let fbCWidth = (fromInteger.toInteger) fbWidth
textureBinding Texture2D GL.$= Just texid fbCHeight = (fromInteger.toInteger) fbHeight
checkError "bind HUD-Tex" -- free old renderbuffer & create new (reuse is NOT advised!)
textureFilter Texture2D GL.$= ((Linear', Nothing), Linear') GL.deleteObjectName (state ^. gl.glRenderbuffer)
checkError "filter HUD-Tex" renderBuffer <- GL.genObjectName
GL.bindRenderbuffer GL.Renderbuffer GL.$= renderBuffer
GL.renderbufferStorage
GL.Renderbuffer -- use the only available renderbuffer
-- - must be this constant.
GL.DepthComponent' -- 32-bit float-rgba-color
(GL.RenderbufferSize fbCWidth fbCHeight) -- size of buffer
let hudtexid = state ^. gl.glHud.hudTexture
maptexid = state ^. gl.glMap.mapTexture
allocaBytes (fbWidth*fbHeight*4) $ \ptr -> do allocaBytes (fbWidth*fbHeight*4) $ \ptr -> do
let imData = take (fbWidth*fbHeight*4) (cycle [255,0,0,128] :: [Int8]) let imData = take (fbWidth*fbHeight*4) (cycle [255,0,0,64] :: [Int8])
--putStrLn $ show imData --putStrLn $ show imData
pokeArray ptr imData pokeArray ptr imData
texImage2D Texture2D GL.NoProxy 0 RGBA8 (GL.TextureSize2D (int fbWidth) (int fbHeight)) 0 -- HUD
textureBinding Texture2D GL.$= Just hudtexid
textureFilter Texture2D GL.$= ((Linear', Nothing), Linear')
texImage2D Texture2D GL.NoProxy 0 RGBA8 (GL.TextureSize2D fbCWidth fbCHeight) 0
(GL.PixelData GL.RGBA GL.UnsignedByte ptr)
-- MAP
textureBinding Texture2D GL.$= Just maptexid
textureFilter Texture2D GL.$= ((Linear', Nothing), Linear')
texImage2D Texture2D GL.NoProxy 0 RGBA8 (GL.TextureSize2D fbCWidth fbCHeight) 0
(GL.PixelData GL.RGBA GL.UnsignedByte ptr) (GL.PixelData GL.RGBA GL.UnsignedByte ptr)
checkError "setting up HUD-Tex" checkError "setting up HUD-Tex"
return renderBuffer
modify $ gl.glRenderbuffer .~ rb
processEvents :: Pioneers () processEvents :: Pioneers ()
processEvents = do processEvents = do

View File

@ -12,6 +12,7 @@ import Graphics.Rendering.OpenGL.GL.ObjectName
import Graphics.Rendering.OpenGL.GL.PerFragment import Graphics.Rendering.OpenGL.GL.PerFragment
import Graphics.Rendering.OpenGL.GL.Shaders import Graphics.Rendering.OpenGL.GL.Shaders
import Graphics.Rendering.OpenGL.GL.StateVar import Graphics.Rendering.OpenGL.GL.StateVar
import Graphics.Rendering.OpenGL.GL.Texturing.Objects (TextureObject)
import Graphics.Rendering.OpenGL.GL.VertexArrays (Capability (..), import Graphics.Rendering.OpenGL.GL.VertexArrays (Capability (..),
vertexAttribArray, vertexAttribArray,
VertexArrayDescriptor, VertexArrayDescriptor,
@ -62,6 +63,7 @@ initMapShader :: IO (
, UniformLocation -- ^ NormalMat , UniformLocation -- ^ NormalMat
, UniformLocation -- ^ TessLevelInner , UniformLocation -- ^ TessLevelInner
, UniformLocation -- ^ TessLevelOuter , UniformLocation -- ^ TessLevelOuter
, TextureObject -- ^ Texture where to draw into
) )
initMapShader = do initMapShader = do
! vertexSource <- B.readFile mapVertexShaderFile ! vertexSource <- B.readFile mapVertexShaderFile
@ -117,8 +119,10 @@ initMapShader = do
putStrLn $ unlines $ "Attributes: ":map show att putStrLn $ unlines $ "Attributes: ":map show att
putStrLn $ unlines $ ["Indices: ", show (colorIndex, normalIndex, vertexIndex)] putStrLn $ unlines $ ["Indices: ", show (colorIndex, normalIndex, vertexIndex)]
tex <- genObjectName
checkError "initShader" checkError "initShader"
return (program, colorIndex, normalIndex, vertexIndex, projectionMatrixIndex, viewMatrixIndex, modelMatrixIndex, normalMatrixIndex, tessLevelInner, tessLevelOuter) return (program, colorIndex, normalIndex, vertexIndex, projectionMatrixIndex, viewMatrixIndex, modelMatrixIndex, normalMatrixIndex, tessLevelInner, tessLevelOuter, tex)
initHud :: IO GLHud initHud :: IO GLHud
initHud = do initHud = do
@ -135,7 +139,8 @@ initHud = do
currentProgram $= Just program currentProgram $= Just program
texIndex <- get (uniformLocation program "tex") backIndex <- get (uniformLocation program "tex[0]")
texIndex <- get (uniformLocation program "tex[1]")
checkError "ui-tex" checkError "ui-tex"
-- | simple triangle over the whole screen. -- | simple triangle over the whole screen.
@ -157,6 +162,7 @@ initHud = do
return GLHud return GLHud
{ _hudTexture = tex { _hudTexture = tex
, _hudTexIndex = texIndex , _hudTexIndex = texIndex
, _hudBackIndex = backIndex
, _hudVertexIndex = vertexIndex , _hudVertexIndex = vertexIndex
, _hudVert = 4 , _hudVert = 4
, _hudVBO = vbo , _hudVBO = vbo

View File

@ -87,11 +87,13 @@ data GLMapState = GLMapState
, _stateMap :: !GL.BufferObject , _stateMap :: !GL.BufferObject
, _mapVert :: !GL.NumArrayIndices , _mapVert :: !GL.NumArrayIndices
, _mapProgram :: !GL.Program , _mapProgram :: !GL.Program
, _mapTexture :: !TextureObject
} }
data GLHud = GLHud data GLHud = GLHud
{ _hudTexture :: !TextureObject -- ^ Texture itself { _hudTexture :: !TextureObject -- ^ HUD-Texture itself
, _hudTexIndex :: !GL.UniformLocation -- ^ Position of Texture in Shader , _hudTexIndex :: !GL.UniformLocation -- ^ Position of Overlay-Texture in Shader
, _hudBackIndex :: !GL.UniformLocation -- ^ Position of Background-Texture in Shader
, _hudVertexIndex :: !GL.AttribLocation -- ^ Position of Vertices in Shader , _hudVertexIndex :: !GL.AttribLocation -- ^ Position of Vertices in Shader
, _hudVert :: !GL.NumArrayIndices -- ^ Number of Vertices to draw , _hudVert :: !GL.NumArrayIndices -- ^ Number of Vertices to draw
, _hudVBO :: !GL.BufferObject -- ^ Vertex-Buffer-Object , _hudVBO :: !GL.BufferObject -- ^ Vertex-Buffer-Object
@ -102,6 +104,8 @@ data GLHud = GLHud
data GLState = GLState data GLState = GLState
{ _glMap :: !GLMapState { _glMap :: !GLMapState
, _glHud :: !GLHud , _glHud :: !GLHud
, _glRenderbuffer :: !GL.RenderbufferObject
, _glFramebuffer :: !GL.FramebufferObject
} }
data UIState = UIState data UIState = UIState