pioneers/src/Render/Render.hs

96 lines
3.6 KiB
Haskell
Raw Normal View History

{-# LANGUAGE BangPatterns #-}
module Render.Render where
import qualified Data.ByteString as B
import Foreign.Marshal.Array (withArray)
import Foreign.Storable (sizeOf)
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)
import Graphics.Rendering.OpenGL.GL.VertexSpec
2014-01-04 22:47:07 +00:00
import Graphics.Rendering.OpenGL.Raw.Core31
import Render.Misc
vertexShaderFile :: String
vertexShaderFile = "shaders/vertex.shader"
fragmentShaderFile :: String
fragmentShaderFile = "shaders/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-01-06 20:13:58 +00:00
initShader :: IO (
AttribLocation -- ^ color
, AttribLocation -- ^ normal
, AttribLocation -- ^ vertex
, UniformLocation -- ^ ProjectionMat
, UniformLocation -- ^ ViewMat
, UniformLocation -- ^ ModelMat
, UniformLocation -- ^ NormalMat
)
initShader = do
! vertexSource <- B.readFile vertexShaderFile
! fragmentSource <- B.readFile fragmentShaderFile
vertexShader <- compileShaderSource VertexShader vertexSource
checkError "compile Vertex"
fragmentShader <- compileShaderSource FragmentShader fragmentSource
checkError "compile Frag"
program <- createProgramUsing [vertexShader, fragmentShader]
checkError "compile Program"
currentProgram $= Just program
projectionMatrixIndex <- get (uniformLocation program "fg_ProjectionMatrix")
checkError "projMat"
2014-01-05 18:09:01 +00:00
viewMatrixIndex <- get (uniformLocation program "fg_ViewMatrix")
checkError "viewMat"
2014-01-04 13:09:42 +00:00
modelMatrixIndex <- get (uniformLocation program "fg_ModelMatrix")
checkError "modelMat"
2014-01-06 20:13:58 +00:00
normalMatrixIndex <- get (uniformLocation program "fg_NormalMatrix")
checkError "normalMat"
vertexIndex <- get (attribLocation program "fg_VertexIn")
vertexAttribArray vertexIndex $= Enabled
checkError "vertexInd"
2014-01-04 15:55:59 +00:00
normalIndex <- get (attribLocation program "fg_NormalIn")
vertexAttribArray normalIndex $= Enabled
checkError "normalInd"
2014-01-04 13:09:42 +00:00
colorIndex <- get (attribLocation program "fg_Color")
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-01-06 20:13:58 +00:00
return (colorIndex, normalIndex, vertexIndex, projectionMatrixIndex, viewMatrixIndex, modelMatrixIndex, normalMatrixIndex)
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"