1
+ {-# LANGUAGE TypeSynonymInstances #-}
1
2
-----------------------------------------------------------------------------
2
3
-- |
3
4
-- Module : Graphics.Rendering.OpenGL.GL.Shaders.Uniform
13
14
--
14
15
-----------------------------------------------------------------------------
15
16
16
- {-# LANGUAGE TypeSynonymInstances #-}
17
-
18
17
module Graphics.Rendering.OpenGL.GL.Shaders.Uniform (
19
18
-- * Uniform variables
20
19
UniformLocation (.. ), uniformLocation , activeUniforms , Uniform (.. ),
@@ -29,6 +28,9 @@ import Foreign.Marshal.Alloc
29
28
import Foreign.Ptr
30
29
import Foreign.Storable
31
30
import Graphics.Rendering.OpenGL.GL.ByteString
31
+ import Graphics.Rendering.OpenGL.GL.CoordTrans
32
+ import Graphics.Rendering.OpenGL.GL.GLboolean
33
+ import Graphics.Rendering.OpenGL.GL.MatrixComponent
32
34
import Graphics.Rendering.OpenGL.GL.Shaders.Program
33
35
import Graphics.Rendering.OpenGL.GL.Shaders.ProgramObjects
34
36
import Graphics.Rendering.OpenGL.GL.Shaders.Variables
@@ -236,13 +238,28 @@ instance UniformComponent a => Uniform (Index1 a) where
236
238
-- getUniform. Even worse is that it requires the `GLint` uniforms while it is an enum or
237
239
-- uint.
238
240
instance Uniform TextureUnit where
239
- uniform loc@ (UniformLocation ul) = makeStateVar getter setter
240
- where setter (TextureUnit tu) = uniform1 loc (fromIntegral tu :: GLint )
241
- getter = do program <- fmap fromJust $ get currentProgram
242
- allocaBytes (sizeOf (undefined :: GLint )) $ \ buf -> do
243
- glGetUniformiv (programID program) ul buf
244
- tuID <- peek buf
245
- return . TextureUnit $ fromIntegral tuID
241
+ uniform loc = makeStateVar getter setter
242
+ where getter = allocaBytes (sizeOf (undefined :: GLint )) $ \ buf -> do
243
+ getUniformWith glGetUniformiv loc buf
244
+ fmap (TextureUnit . fromIntegral ) $ peek buf
245
+ setter (TextureUnit tu) = uniform1 loc (fromIntegral tu :: GLint )
246
246
uniformv location count = uniform1v location count . (castPtr :: Ptr TextureUnit -> Ptr GLint )
247
247
248
+ -- | Note: 'uniformv' expects all matrices to be in 'ColumnMajor' form.
249
+ instance MatrixComponent a => Uniform (GLmatrix a ) where
250
+ uniform loc@ (UniformLocation ul) = makeStateVar getter setter
251
+ where getter = withNewMatrix ColumnMajor $ getUniformWith getUniformv loc
252
+ setter m = withMatrix m $ uniformMatrix4v ul 1 . isRowMajor
253
+ uniformv (UniformLocation ul) count buf =
254
+ uniformMatrix4v ul count (marshalGLboolean False ) (castPtr buf `asTypeOf` elemType buf)
255
+ where elemType = undefined :: MatrixComponent c => Ptr (GLmatrix c ) -> Ptr c
256
+
257
+ isRowMajor :: MatrixOrder -> GLboolean
258
+ isRowMajor = marshalGLboolean . (RowMajor == )
259
+
260
+ getUniformWith :: (GLuint -> GLint -> Ptr a -> IO () ) -> UniformLocation -> Ptr a -> IO ()
261
+ getUniformWith getter (UniformLocation ul) buf = do
262
+ program <- fmap (programID . fromJust) $ get currentProgram
263
+ getter program ul buf
264
+
248
265
--------------------------------------------------------------------------------
0 commit comments