88 lines
2.1 KiB
Haskell
88 lines
2.1 KiB
Haskell
module Renderer where
|
|
|
|
import SDL (($=))
|
|
|
|
import qualified Graphics.Rendering.OpenGL as GL
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
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 (RenderObjects 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"
|
|
|
|
vertexShader <- GL.createShader Gl.VertexShader
|
|
fragmentShader <- GL.createShader Gl.FragmentShader
|
|
|
|
GL.shaderSourceBS vertexShader $= vertexShaderSrc
|
|
GL.ShaderSourceBS fragmentShader $= fragmentShaderSrc
|
|
|
|
GL.compileShader vertexShader
|
|
GL.compileShader fragmentShader
|
|
|
|
shaderProgram <- GL.createProgram
|
|
|
|
GL.attachShader shaderProgram vertexShader
|
|
GL.attachShader shaderProgram fragmentShader
|
|
|
|
GL.linkProgram shaderProgram
|
|
|
|
return (SpriteShaderObjects vertexShader fragmentShader shaderProgram)
|
|
|
|
loadSprites :: IO (AssetDetails SpriteRenderObjects SpriteShaderObjects Sprite)
|
|
loadSprites = do
|
|
rawImg <- readImage "assets/img/lynx.jpg"
|
|
let img = case eRawImg 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)
|
|
}
|