pioneers/src/Render/Render.hs

176 lines
6.6 KiB
Haskell
Raw Normal View History

{-# LANGUAGE BangPatterns, InstanceSigs, ExistentialQuantification #-}
module Render.Render where
import qualified Data.ByteString as B
import Data.Array.Storable
import qualified Data.Vector.Storable as V
import Foreign.Marshal.Array (withArray)
import Foreign.Storable
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
import Graphics.Rendering.OpenGL.GL.Shaders
import Graphics.Rendering.OpenGL.GL.StateVar
import Graphics.Rendering.OpenGL.GL.VertexArrays (Capability (..),
vertexAttribArray,
VertexArrayDescriptor,
DataType(Float))
import Graphics.Rendering.OpenGL.GL.VertexSpec
2014-01-04 22:47:07 +00:00
import Graphics.Rendering.OpenGL.Raw.Core31
import Render.Misc
import Foreign.Ptr (Ptr, wordPtrToPtr)
import Types
import Graphics.GLUtil.BufferObjects (makeBuffer)
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"
initBuffer :: [GLfloat] -> IO BufferObject
initBuffer varray =
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-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
vertexShader <- compileShaderSource VertexShader vertexSource
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"
fragmentShader <- compileShaderSource FragmentShader fragmentSource
checkError "compile Frag"
2014-01-21 15:18:48 +00:00
program <- createProgramUsing [vertexShader, tessControlShader, tessEvalShader, fragmentShader]
checkError "compile Program"
currentProgram $= Just program
2014-01-21 15:18:48 +00:00
projectionMatrixIndex <- get (uniformLocation program "ProjectionMatrix")
checkError "projMat"
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-21 15:18:48 +00:00
modelMatrixIndex <- get (uniformLocation program "ModelMatrix")
2014-01-04 13:09:42 +00:00
checkError "modelMat"
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")
vertexAttribArray vertexIndex $= Enabled
checkError "vertexInd"
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
checkError "initShader"
2014-03-24 07:21:30 +00:00
return (program, colorIndex, normalIndex, vertexIndex, projectionMatrixIndex, viewMatrixIndex, modelMatrixIndex, normalMatrixIndex, tessLevelInner, tessLevelOuter)
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
checkError "compile UI-Vertex"
2014-03-24 07:21:30 +00:00
fragmentShader <- compileShaderSource FragmentShader fragmentSource
checkError "compile UI-Fragment"
2014-03-24 07:21:30 +00:00
program <- createProgramUsing [vertexShader, fragmentShader]
checkError "compile Program"
tex <- genObjectName
currentProgram $= Just program
texIndex <- get (uniformLocation program "tex")
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
putStrLn $ unlines $ ["Indices: ", show (texIndex)]
checkError "initHud"
return GLHud
{ _hudTexture = tex
, _hudTexIndex = texIndex
, _hudVertexIndex = vertexIndex
, _hudVert = 4
, _hudVBO = vbo
, _hudEBO = ebo
, _hudProgram = program
}
2014-03-24 07:21:30 +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
checkError "initRendering"