2014-04-04 09:15:00 +00:00
|
|
|
{-# LANGUAGE BangPatterns, InstanceSigs, ExistentialQuantification #-}
|
2014-01-02 02:35:38 +00:00
|
|
|
module Render.Render where
|
|
|
|
|
2014-01-02 12:02:01 +00:00
|
|
|
import qualified Data.ByteString as B
|
2014-04-04 09:15:00 +00:00
|
|
|
import Data.Array.Storable
|
|
|
|
import qualified Data.Vector.Storable as V
|
2014-01-02 12:02:01 +00:00
|
|
|
import Foreign.Marshal.Array (withArray)
|
2014-04-04 09:15:00 +00:00
|
|
|
import Foreign.Storable
|
2014-01-02 12:02:01 +00:00
|
|
|
import Graphics.Rendering.OpenGL.GL.BufferObjects
|
|
|
|
import Graphics.Rendering.OpenGL.GL.Framebuffer (clearColor)
|
|
|
|
import Graphics.Rendering.OpenGL.GL.ObjectName
|
2014-01-04 22:47:07 +00:00
|
|
|
import Graphics.Rendering.OpenGL.GL.PerFragment
|
2014-01-02 12:02:01 +00:00
|
|
|
import Graphics.Rendering.OpenGL.GL.Shaders
|
|
|
|
import Graphics.Rendering.OpenGL.GL.StateVar
|
2014-04-05 13:53:49 +00:00
|
|
|
import Graphics.Rendering.OpenGL.GL.Texturing.Objects (TextureObject)
|
2014-01-02 12:02:01 +00:00
|
|
|
import Graphics.Rendering.OpenGL.GL.VertexArrays (Capability (..),
|
2014-04-04 09:15:00 +00:00
|
|
|
vertexAttribArray,
|
|
|
|
VertexArrayDescriptor,
|
|
|
|
DataType(Float))
|
2014-01-02 12:02:01 +00:00
|
|
|
import Graphics.Rendering.OpenGL.GL.VertexSpec
|
2014-01-04 22:47:07 +00:00
|
|
|
import Graphics.Rendering.OpenGL.Raw.Core31
|
2014-01-02 12:02:01 +00:00
|
|
|
import Render.Misc
|
2014-04-04 09:15:00 +00:00
|
|
|
import Foreign.Ptr (Ptr, wordPtrToPtr)
|
|
|
|
|
|
|
|
import Types
|
|
|
|
import Graphics.GLUtil.BufferObjects (makeBuffer)
|
2014-01-02 02:35:38 +00:00
|
|
|
|
2014-03-24 07:21:30 +00:00
|
|
|
mapVertexShaderFile :: String
|
|
|
|
mapVertexShaderFile = "shaders/map/vertex.shader"
|
|
|
|
mapTessControlShaderFile :: String
|
|
|
|
mapTessControlShaderFile = "shaders/map/tessControl.shader"
|
|
|
|
mapTessEvalShaderFile :: String
|
|
|
|
mapTessEvalShaderFile = "shaders/map/tessEval.shader"
|
|
|
|
mapFragmentShaderFile :: String
|
|
|
|
mapFragmentShaderFile = "shaders/map/fragment.shader"
|
|
|
|
|
|
|
|
uiVertexShaderFile :: String
|
|
|
|
uiVertexShaderFile = "shaders/ui/vertex.shader"
|
|
|
|
uiFragmentShaderFile :: String
|
|
|
|
uiFragmentShaderFile = "shaders/ui/fragment.shader"
|
|
|
|
|
2014-01-02 02:35:38 +00:00
|
|
|
initBuffer :: [GLfloat] -> IO BufferObject
|
2014-01-02 12:02:01 +00:00
|
|
|
initBuffer varray =
|
2014-01-02 02:35:38 +00:00
|
|
|
let
|
|
|
|
sizeOfVarray = length varray * sizeOfComponent
|
|
|
|
sizeOfComponent = sizeOf (head varray)
|
|
|
|
in do
|
|
|
|
bufferObject <- genObjectName
|
|
|
|
bindBuffer ArrayBuffer $= Just bufferObject
|
|
|
|
withArray varray $ \buffer ->
|
|
|
|
bufferData ArrayBuffer $= (fromIntegral sizeOfVarray, buffer, StaticDraw)
|
|
|
|
checkError "initBuffer"
|
|
|
|
return bufferObject
|
|
|
|
|
2014-03-24 07:21:30 +00:00
|
|
|
initMapShader :: IO (
|
|
|
|
Program -- ^ the GLSL-Program
|
|
|
|
, AttribLocation -- ^ color
|
2014-01-06 20:13:58 +00:00
|
|
|
, AttribLocation -- ^ normal
|
|
|
|
, AttribLocation -- ^ vertex
|
|
|
|
, UniformLocation -- ^ ProjectionMat
|
|
|
|
, UniformLocation -- ^ ViewMat
|
|
|
|
, UniformLocation -- ^ ModelMat
|
|
|
|
, UniformLocation -- ^ NormalMat
|
2014-01-21 15:44:42 +00:00
|
|
|
, UniformLocation -- ^ TessLevelInner
|
|
|
|
, UniformLocation -- ^ TessLevelOuter
|
2014-04-05 13:53:49 +00:00
|
|
|
, TextureObject -- ^ Texture where to draw into
|
2014-01-06 20:13:58 +00:00
|
|
|
)
|
2014-03-24 07:21:30 +00:00
|
|
|
initMapShader = do
|
|
|
|
! vertexSource <- B.readFile mapVertexShaderFile
|
|
|
|
! tessControlSource <- B.readFile mapTessControlShaderFile
|
|
|
|
! tessEvalSource <- B.readFile mapTessEvalShaderFile
|
|
|
|
! fragmentSource <- B.readFile mapFragmentShaderFile
|
2014-01-02 02:35:38 +00:00
|
|
|
vertexShader <- compileShaderSource VertexShader vertexSource
|
2014-01-03 16:46:41 +00:00
|
|
|
checkError "compile Vertex"
|
2014-01-21 15:18:48 +00:00
|
|
|
tessControlShader <- compileShaderSource TessControlShader tessControlSource
|
2014-03-24 07:21:30 +00:00
|
|
|
checkError "compile TessControl"
|
2014-01-21 15:18:48 +00:00
|
|
|
tessEvalShader <- compileShaderSource TessEvaluationShader tessEvalSource
|
2014-03-24 07:21:30 +00:00
|
|
|
checkError "compile TessEval"
|
2014-01-02 02:35:38 +00:00
|
|
|
fragmentShader <- compileShaderSource FragmentShader fragmentSource
|
2014-01-03 16:46:41 +00:00
|
|
|
checkError "compile Frag"
|
2014-01-21 15:18:48 +00:00
|
|
|
program <- createProgramUsing [vertexShader, tessControlShader, tessEvalShader, fragmentShader]
|
2014-01-03 16:46:41 +00:00
|
|
|
checkError "compile Program"
|
|
|
|
|
2014-01-02 02:35:38 +00:00
|
|
|
currentProgram $= Just program
|
|
|
|
|
2014-01-21 15:18:48 +00:00
|
|
|
projectionMatrixIndex <- get (uniformLocation program "ProjectionMatrix")
|
2014-01-03 16:46:41 +00:00
|
|
|
checkError "projMat"
|
2014-01-02 02:35:38 +00:00
|
|
|
|
2014-01-21 15:18:48 +00:00
|
|
|
viewMatrixIndex <- get (uniformLocation program "ViewMatrix")
|
2014-01-05 18:09:01 +00:00
|
|
|
checkError "viewMat"
|
2014-01-02 02:35:38 +00:00
|
|
|
|
2014-01-21 15:18:48 +00:00
|
|
|
modelMatrixIndex <- get (uniformLocation program "ModelMatrix")
|
2014-01-04 13:09:42 +00:00
|
|
|
checkError "modelMat"
|
2014-01-03 02:01:54 +00:00
|
|
|
|
2014-01-21 15:18:48 +00:00
|
|
|
normalMatrixIndex <- get (uniformLocation program "NormalMatrix")
|
2014-01-06 20:13:58 +00:00
|
|
|
checkError "normalMat"
|
|
|
|
|
2014-01-21 15:44:42 +00:00
|
|
|
tessLevelInner <- get (uniformLocation program "TessLevelInner")
|
|
|
|
checkError "TessLevelInner"
|
|
|
|
|
|
|
|
tessLevelOuter <- get (uniformLocation program "TessLevelOuter")
|
|
|
|
checkError "TessLevelOuter"
|
|
|
|
|
|
|
|
|
2014-01-21 15:18:48 +00:00
|
|
|
vertexIndex <- get (attribLocation program "Position")
|
2014-01-03 16:46:41 +00:00
|
|
|
vertexAttribArray vertexIndex $= Enabled
|
|
|
|
checkError "vertexInd"
|
2014-01-03 02:01:54 +00:00
|
|
|
|
2014-01-21 15:18:48 +00:00
|
|
|
normalIndex <- get (attribLocation program "Normal")
|
2014-01-04 15:55:59 +00:00
|
|
|
vertexAttribArray normalIndex $= Enabled
|
|
|
|
checkError "normalInd"
|
|
|
|
|
2014-01-21 15:18:48 +00:00
|
|
|
colorIndex <- get (attribLocation program "Color")
|
2014-01-04 13:09:42 +00:00
|
|
|
vertexAttribArray colorIndex $= Enabled
|
|
|
|
checkError "colorInd"
|
|
|
|
|
2014-01-04 15:55:59 +00:00
|
|
|
att <- get (activeAttribs program)
|
|
|
|
|
2014-01-04 13:09:42 +00:00
|
|
|
putStrLn $ unlines $ "Attributes: ":map show att
|
2014-01-04 15:55:59 +00:00
|
|
|
putStrLn $ unlines $ ["Indices: ", show (colorIndex, normalIndex, vertexIndex)]
|
2014-01-04 13:09:42 +00:00
|
|
|
|
2014-04-05 13:53:49 +00:00
|
|
|
tex <- genObjectName
|
|
|
|
|
2014-01-02 02:35:38 +00:00
|
|
|
checkError "initShader"
|
2014-04-05 13:53:49 +00:00
|
|
|
return (program, colorIndex, normalIndex, vertexIndex, projectionMatrixIndex, viewMatrixIndex, modelMatrixIndex, normalMatrixIndex, tessLevelInner, tessLevelOuter, tex)
|
2014-03-24 07:21:30 +00:00
|
|
|
|
2014-04-04 09:15:00 +00:00
|
|
|
initHud :: IO GLHud
|
|
|
|
initHud = do
|
|
|
|
! vertexSource <- B.readFile "shaders/ui/vertex.shader"
|
|
|
|
! fragmentSource <- B.readFile "shaders/ui/fragment.shader"
|
2014-03-24 07:21:30 +00:00
|
|
|
vertexShader <- compileShaderSource VertexShader vertexSource
|
2014-04-04 09:15:00 +00:00
|
|
|
checkError "compile UI-Vertex"
|
2014-03-24 07:21:30 +00:00
|
|
|
fragmentShader <- compileShaderSource FragmentShader fragmentSource
|
2014-04-04 09:15:00 +00:00
|
|
|
checkError "compile UI-Fragment"
|
2014-03-24 07:21:30 +00:00
|
|
|
program <- createProgramUsing [vertexShader, fragmentShader]
|
|
|
|
checkError "compile Program"
|
2014-04-04 09:15:00 +00:00
|
|
|
|
|
|
|
tex <- genObjectName
|
|
|
|
|
|
|
|
currentProgram $= Just program
|
|
|
|
|
2014-04-05 13:53:49 +00:00
|
|
|
backIndex <- get (uniformLocation program "tex[0]")
|
|
|
|
texIndex <- get (uniformLocation program "tex[1]")
|
2014-04-04 09:15:00 +00:00
|
|
|
checkError "ui-tex"
|
|
|
|
|
|
|
|
-- | simple triangle over the whole screen.
|
|
|
|
let vertexBufferData = reverse [-1, -1, 1, -1, -1, 1, 1, 1] :: [GLfloat]
|
|
|
|
|
|
|
|
vertexIndex <- get (attribLocation program "position")
|
|
|
|
vertexAttribArray vertexIndex $= Enabled
|
|
|
|
checkError "vertexInd"
|
|
|
|
|
|
|
|
ebo <- makeBuffer ElementArrayBuffer ([0..3] :: [GLuint])
|
|
|
|
vbo <- makeBuffer ArrayBuffer vertexBufferData
|
|
|
|
|
2014-03-24 07:21:30 +00:00
|
|
|
att <- get (activeAttribs program)
|
|
|
|
|
|
|
|
putStrLn $ unlines $ "Attributes: ":map show att
|
2014-04-04 09:15:00 +00:00
|
|
|
putStrLn $ unlines $ ["Indices: ", show (texIndex)]
|
|
|
|
|
|
|
|
checkError "initHud"
|
|
|
|
return GLHud
|
|
|
|
{ _hudTexture = tex
|
|
|
|
, _hudTexIndex = texIndex
|
2014-04-05 13:53:49 +00:00
|
|
|
, _hudBackIndex = backIndex
|
2014-04-04 09:15:00 +00:00
|
|
|
, _hudVertexIndex = vertexIndex
|
|
|
|
, _hudVert = 4
|
|
|
|
, _hudVBO = vbo
|
|
|
|
, _hudEBO = ebo
|
|
|
|
, _hudProgram = program
|
|
|
|
}
|
|
|
|
|
2014-03-24 07:21:30 +00:00
|
|
|
|
|
|
|
|
2014-01-02 12:02:01 +00:00
|
|
|
|
|
|
|
initRendering :: IO ()
|
|
|
|
initRendering = do
|
|
|
|
clearColor $= Color4 0 0 0 0
|
2014-01-04 22:47:07 +00:00
|
|
|
depthFunc $= Just Less
|
|
|
|
glCullFace gl_BACK
|
2014-01-02 12:02:01 +00:00
|
|
|
checkError "initRendering"
|