aboutsummaryrefslogtreecommitdiff
path: root/Spear/GL.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Spear/GL.hs')
-rw-r--r--Spear/GL.hs104
1 files changed, 59 insertions, 45 deletions
diff --git a/Spear/GL.hs b/Spear/GL.hs
index aa3e930..6792d35 100644
--- a/Spear/GL.hs
+++ b/Spear/GL.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE FlexibleInstances #-}
1module Spear.GL 2module Spear.GL
2( 3(
3 -- * Programs 4 -- * Programs
@@ -12,12 +13,7 @@ module Spear.GL
12, fragLocation 13, fragLocation
13, uniformLocation 14, uniformLocation
14 -- ** Uniforms 15 -- ** Uniforms
15, uniformVec2 16, Uniform(..)
16, uniformVec3
17, uniformVec4
18, uniformMat3
19, uniformMat4
20, LoadUniforms(..)
21 -- * Shaders 17 -- * Shaders
22, GLSLShader 18, GLSLShader
23, ShaderType(..) 19, ShaderType(..)
@@ -327,53 +323,71 @@ readSource' file = do
327 323
328 return code 324 return code
329 325
330-- | Load a 2D vector. 326class Uniform a where
331uniformVec2 :: GLint -> Vector2 -> IO ()
332uniformVec2 loc v = glUniform2f loc x' y'
333 where x' = unsafeCoerce $ x v
334 y' = unsafeCoerce $ y v
335
336-- | Load a 3D vector.
337uniformVec3 :: GLint -> Vector3 -> IO ()
338uniformVec3 loc v = glUniform3f loc x' y' z'
339 where x' = unsafeCoerce $ x v
340 y' = unsafeCoerce $ y v
341 z' = unsafeCoerce $ z v
342
343-- | Load a 4D vector.
344uniformVec4 :: GLint -> Vector4 -> IO ()
345uniformVec4 loc v = glUniform4f loc x' y' z' w'
346 where x' = unsafeCoerce $ x v
347 y' = unsafeCoerce $ y v
348 z' = unsafeCoerce $ z v
349 w' = unsafeCoerce $ w v
350
351-- | Load a 3x3 matrix.
352uniformMat3 :: GLint -> Matrix3 -> IO ()
353uniformMat3 loc mat =
354 with mat $ \ptrMat ->
355 glUniformMatrix3fv loc 1 (toEnum 0) (unsafeCoerce ptrMat)
356
357-- | Load a 4x4 matrix.
358uniformMat4 :: GLint -> Matrix4 -> IO ()
359uniformMat4 loc mat =
360 with mat $ \ptrMat ->
361 glUniformMatrix4fv loc 1 (toEnum 0) (unsafeCoerce ptrMat)
362
363class LoadUniforms a where
364 -- | Load a list of uniform values. 327 -- | Load a list of uniform values.
365 uniforml :: GLint -> [a] -> IO () 328 uniform :: GLint -> a -> IO ()
366 329
367instance LoadUniforms Float where 330instance Uniform Int where uniform loc a = glUniform1i loc (fromIntegral a)
368 uniforml loc vals = withArray (map unsafeCoerce vals) $ \ptr -> 331instance Uniform Float where uniform loc a = glUniform1f loc (unsafeCoerce a)
332
333instance Uniform (Int,Int) where
334 uniform loc (x,y) = glUniform2i loc (fromIntegral x) (fromIntegral y)
335
336instance Uniform (Float,Float) where
337 uniform loc (x,y) = glUniform2f loc (unsafeCoerce x) (unsafeCoerce y)
338
339instance Uniform (Int,Int,Int) where
340 uniform loc (x,y,z) = glUniform3i loc (fromIntegral x) (fromIntegral y) (fromIntegral z)
341
342instance Uniform (Float,Float,Float) where
343 uniform loc (x,y,z) = glUniform3f loc (unsafeCoerce x) (unsafeCoerce y) (unsafeCoerce z)
344
345instance Uniform (Int,Int,Int,Int) where
346 uniform loc (x,y,z,w) = glUniform4i loc
347 (fromIntegral x) (fromIntegral y) (fromIntegral z) (fromIntegral w)
348
349instance Uniform (Float,Float,Float,Float) where
350 uniform loc (x,y,z,w) = glUniform4f loc
351 (unsafeCoerce x) (unsafeCoerce y) (unsafeCoerce z) (unsafeCoerce w)
352
353instance Uniform Vector2 where
354 uniform loc v = glUniform2f loc x' y'
355 where x' = unsafeCoerce $ x v
356 y' = unsafeCoerce $ y v
357
358instance Uniform Vector3 where
359 uniform loc v = glUniform3f loc x' y' z'
360 where x' = unsafeCoerce $ x v
361 y' = unsafeCoerce $ y v
362 z' = unsafeCoerce $ z v
363
364instance Uniform Vector4 where
365 uniform loc v = glUniform4f loc x' y' z' w'
366 where x' = unsafeCoerce $ x v
367 y' = unsafeCoerce $ y v
368 z' = unsafeCoerce $ z v
369 w' = unsafeCoerce $ w v
370
371instance Uniform Matrix3 where
372 uniform loc mat =
373 with mat $ \ptrMat ->
374 glUniformMatrix3fv loc 1 (toEnum 0) (unsafeCoerce ptrMat)
375
376instance Uniform Matrix4 where
377 uniform loc mat =
378 with mat $ \ptrMat ->
379 glUniformMatrix4fv loc 1 (toEnum 0) (unsafeCoerce ptrMat)
380
381instance Uniform [Float] where
382 uniform loc vals = withArray (map unsafeCoerce vals) $ \ptr ->
369 case length vals of 383 case length vals of
370 1 -> glUniform1fv loc 1 ptr 384 1 -> glUniform1fv loc 1 ptr
371 2 -> glUniform2fv loc 1 ptr 385 2 -> glUniform2fv loc 1 ptr
372 3 -> glUniform3fv loc 1 ptr 386 3 -> glUniform3fv loc 1 ptr
373 4 -> glUniform4fv loc 1 ptr 387 4 -> glUniform4fv loc 1 ptr
374 388
375instance LoadUniforms Int where 389instance Uniform [Int] where
376 uniforml loc vals = withArray (map fromIntegral vals) $ \ptr -> 390 uniform loc vals = withArray (map fromIntegral vals) $ \ptr ->
377 case length vals of 391 case length vals of
378 1 -> glUniform1iv loc 1 ptr 392 1 -> glUniform1iv loc 1 ptr
379 2 -> glUniform2iv loc 1 ptr 393 2 -> glUniform2iv loc 1 ptr