Skip to content

Commit b1b5003

Browse files
committed
Added Uniform instance for GLmatrix. Bumped version to 3.0.1.0.
1 parent ed76729 commit b1b5003

File tree

5 files changed

+94
-45
lines changed

5 files changed

+94
-45
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
3.0.1.0
2+
-------
3+
* Added Uniform instance for `GLmatrix`.
4+
15
3.0.0.2
26
-------
37
* Removed redundant constraints.

OpenGL.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: OpenGL
2-
version: 3.0.0.2
2+
version: 3.0.1.0
33
synopsis: A binding for the OpenGL graphics system
44
description:
55
A Haskell binding for the OpenGL graphics system (GL, version 4.5) and its
@@ -126,6 +126,7 @@ library
126126
Graphics.Rendering.OpenGL.GL.FramebufferObjects.RenderbufferTarget
127127
Graphics.Rendering.OpenGL.GL.GLboolean
128128
Graphics.Rendering.OpenGL.GL.IOState
129+
Graphics.Rendering.OpenGL.GL.MatrixComponent
129130
Graphics.Rendering.OpenGL.GL.PeekPoke
130131
Graphics.Rendering.OpenGL.GL.PixelData
131132
Graphics.Rendering.OpenGL.GL.PixelFormat

src/Graphics/Rendering/OpenGL/GL/CoordTrans.hs

+1-35
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
--
1414
--------------------------------------------------------------------------------
1515

16-
{-# LANGUAGE TypeSynonymInstances #-}
17-
1816
module Graphics.Rendering.OpenGL.GL.CoordTrans (
1917
-- * Controlling the Viewport
2018
depthRange,
@@ -43,9 +41,9 @@ import Foreign.Marshal.Array
4341
import Foreign.Marshal.Utils
4442
import Foreign.Ptr
4543
import Foreign.Storable
46-
import Graphics.Rendering.OpenGL.GL.Tensor
4744
import Graphics.Rendering.OpenGL.GL.Capability
4845
import Graphics.Rendering.OpenGL.GL.Exception
46+
import Graphics.Rendering.OpenGL.GL.MatrixComponent
4947
import Graphics.Rendering.OpenGL.GL.PeekPoke
5048
import Graphics.Rendering.OpenGL.GL.QueryUtils
5149
import Graphics.Rendering.OpenGL.GL.Texturing.TextureUnit
@@ -180,38 +178,6 @@ data MatrixOrder = ColumnMajor | RowMajor
180178

181179
--------------------------------------------------------------------------------
182180

183-
class Storable c => MatrixComponent c where
184-
getMatrix :: GetPNameMatrix p => p -> Ptr c -> IO ()
185-
loadMatrix :: Ptr c -> IO ()
186-
loadTransposeMatrix :: Ptr c -> IO ()
187-
multMatrix_ :: Ptr c -> IO ()
188-
multTransposeMatrix :: Ptr c -> IO ()
189-
rotate :: c -> Vector3 c -> IO ()
190-
translate :: Vector3 c -> IO ()
191-
scale :: c -> c -> c -> IO ()
192-
193-
instance MatrixComponent GLfloat where
194-
getMatrix = getMatrixf
195-
loadMatrix = glLoadMatrixf
196-
loadTransposeMatrix = glLoadTransposeMatrixf
197-
multMatrix_ = glMultMatrixf
198-
multTransposeMatrix = glMultTransposeMatrixf
199-
rotate a (Vector3 x y z) = glRotatef a x y z
200-
translate (Vector3 x y z) = glTranslatef x y z
201-
scale = glScalef
202-
203-
instance MatrixComponent GLdouble where
204-
getMatrix = getMatrixd
205-
loadMatrix = glLoadMatrixd
206-
loadTransposeMatrix = glLoadTransposeMatrixd
207-
multMatrix_ = glMultMatrixd
208-
multTransposeMatrix = glMultTransposeMatrixd
209-
rotate a (Vector3 x y z) = glRotated a x y z
210-
translate (Vector3 x y z) = glTranslated x y z
211-
scale = glScaled
212-
213-
--------------------------------------------------------------------------------
214-
215181
class Matrix m where
216182
-- | Create a new matrix of the given order (containing undefined elements)
217183
-- and call the action to fill it with 4x4 elements.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{-# LANGUAGE TypeSynonymInstances #-}
2+
{-# OPTIONS_HADDOCK hide #-}
3+
--------------------------------------------------------------------------------
4+
-- |
5+
-- Module : Graphics.Rendering.OpenGL.GL.CoordTrans
6+
-- Copyright : (c) Sven Panne 2002-2016
7+
-- License : BSD3
8+
--
9+
-- Maintainer : Sven Panne <[email protected]>
10+
-- Stability : stable
11+
-- Portability : portable
12+
--
13+
-- This is a purely internal module for handling matrix components.
14+
--
15+
--------------------------------------------------------------------------------
16+
17+
module Graphics.Rendering.OpenGL.GL.MatrixComponent where
18+
19+
import Foreign.Ptr
20+
import Foreign.Storable
21+
import Graphics.Rendering.OpenGL.GL.QueryUtils
22+
import Graphics.Rendering.OpenGL.GL.Tensor
23+
import Graphics.GL
24+
25+
--------------------------------------------------------------------------------
26+
27+
class Storable c => MatrixComponent c where
28+
getMatrix :: GetPNameMatrix p => p -> Ptr c -> IO ()
29+
loadMatrix :: Ptr c -> IO ()
30+
loadTransposeMatrix :: Ptr c -> IO ()
31+
multMatrix_ :: Ptr c -> IO ()
32+
multTransposeMatrix :: Ptr c -> IO ()
33+
getUniformv :: GLuint -> GLint -> Ptr c -> IO ()
34+
uniformMatrix4v :: GLint -> GLsizei -> GLboolean -> Ptr c -> IO ()
35+
rotate :: c -> Vector3 c -> IO ()
36+
translate :: Vector3 c -> IO ()
37+
scale :: c -> c -> c -> IO ()
38+
39+
instance MatrixComponent GLfloat where
40+
getMatrix = getMatrixf
41+
loadMatrix = glLoadMatrixf
42+
loadTransposeMatrix = glLoadTransposeMatrixf
43+
multMatrix_ = glMultMatrixf
44+
multTransposeMatrix = glMultTransposeMatrixf
45+
getUniformv = glGetUniformfv
46+
uniformMatrix4v = glUniformMatrix4fv
47+
rotate a (Vector3 x y z) = glRotatef a x y z
48+
translate (Vector3 x y z) = glTranslatef x y z
49+
scale = glScalef
50+
51+
instance MatrixComponent GLdouble where
52+
getMatrix = getMatrixd
53+
loadMatrix = glLoadMatrixd
54+
loadTransposeMatrix = glLoadTransposeMatrixd
55+
multMatrix_ = glMultMatrixd
56+
multTransposeMatrix = glMultTransposeMatrixd
57+
getUniformv = glGetUniformdv
58+
uniformMatrix4v = glUniformMatrix4dv
59+
rotate a (Vector3 x y z) = glRotated a x y z
60+
translate (Vector3 x y z) = glTranslated x y z
61+
scale = glScaled

src/Graphics/Rendering/OpenGL/GL/Shaders/Uniform.hs

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE TypeSynonymInstances #-}
12
-----------------------------------------------------------------------------
23
-- |
34
-- Module : Graphics.Rendering.OpenGL.GL.Shaders.Uniform
@@ -13,8 +14,6 @@
1314
--
1415
-----------------------------------------------------------------------------
1516

16-
{-# LANGUAGE TypeSynonymInstances #-}
17-
1817
module Graphics.Rendering.OpenGL.GL.Shaders.Uniform (
1918
-- * Uniform variables
2019
UniformLocation(..), uniformLocation, activeUniforms, Uniform(..),
@@ -29,6 +28,9 @@ import Foreign.Marshal.Alloc
2928
import Foreign.Ptr
3029
import Foreign.Storable
3130
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
3234
import Graphics.Rendering.OpenGL.GL.Shaders.Program
3335
import Graphics.Rendering.OpenGL.GL.Shaders.ProgramObjects
3436
import Graphics.Rendering.OpenGL.GL.Shaders.Variables
@@ -236,13 +238,28 @@ instance UniformComponent a => Uniform (Index1 a) where
236238
-- getUniform. Even worse is that it requires the `GLint` uniforms while it is an enum or
237239
-- uint.
238240
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)
246246
uniformv location count = uniform1v location count . (castPtr :: Ptr TextureUnit -> Ptr GLint)
247247

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+
248265
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)