canvas/app/Renderer.hs
2020-02-07 14:57:09 +01:00

81 lines
1.8 KiB
Haskell

module Renderer where
import SDL (($=))
import qualified Graphics.Rendering.OpenGL as GL
import qualified Graphics.GLUtil as GLU
import qualified Data.Map.Strict as M
import qualified Data.ByteString as BS
import Codec.Picture
import Linear
import Foreign
-- internal imports
import Types
initSpriteRenderObjects = do
quadVAO <- GL.genObjectName
quadVBO <- GL.genObjectName
GL.bindBuffer GL.ArrayBuffer $= Just quadVBO
withArray rawVertices $ \ptr ->
GL.bufferData GL.ArrayBuffer $=
( fromIntegral $ length rawVertices * sizeOf (0 :: Float)
, ptr
, GL.StaticDraw
)
GL.bindVertexArrayObject $= Just quadVAO
GL.vertexAttribArray (GL.AttribLocation 0) $= GL.Enabled
GL.vertexAttribPointer (GL.AttribLocation 0) $=
( GL.ToFloat
, GL.VertexArrayDescriptor 4 GL.Float 0 (plusPtr nullPtr 0)
)
GL.bindBuffer GL.ArrayBuffer $= Nothing
GL.bindVertexArrayObject $= Nothing
return (SpriteRenderObjects quadVAO quadVBO)
where
rawVertices :: [Float]
rawVertices =
[ 0, 1, 0, 0
, 1, 0, 1, 0
, 0, 0, 0, 0
, 0, 1, 0, 1
, 1, 1, 1, 1
, 1, 0, 1, 0
]
initSpriteShaderObjects = do
vertexShaderSrc <- BS.readFile "shader/vertex.sl"
fragmentShaderSrc <- BS.readFile "shader/fragment.sl"
shaderProgram <- GLU.simpleShaderProgramBS vertexShaderSrc fragmentShaderSrc
return (SpriteShaderObjects shaderProgram)
loadSprites :: IO (AssetDetails SpriteRenderObjects SpriteShaderObjects Sprite)
loadSprites = do
rawImg <- readImage "assets/img/lynx.jpg"
let img = case rawImg of
Left err -> error err
Right dynImg -> convertRGBA8 dynImg
vertObj <- initSpriteRenderObjects
shadObj <- initSpriteShaderObjects
return AssetDetails
{ adVertObj = vertObj
, adShadObj = shadObj
, adDataObj = M.singleton "lynx" (Sprite (V2 0 0) img)
}