[HOpenGL] multMatrix

Sven Panne Sven.Panne at aedion.de
Sat Oct 15 06:19:53 EDT 2005


Am Samstag, 15. Oktober 2005 10:24 schrieb Bas van Dijk:
> I would like to know how to multiply the current matrix with, for example,
> the following translation matrix in RowMajor order:
>
> [1,0,0,x
> ,0,1,0,y
> ,0,0,1,z
> ,0,0,0,1]
>
> I know I must use:
>
> multMatrix :: (Matrix m, MatrixComponent c) => m c -> IO ()
>
> but I don't know how to call it. Can anybody help me?

Again I fell guilty regarding the OpenGL package implementation... :-] OK, 
let's tear the signature apart a bit, so things might become clear. First the 
Matrix class:

http://haskell.org/ghc/docs/latest/html/libraries/OpenGL/Graphics.Rendering.OpenGL.GL.CoordTrans.html#t%3AMatrix

It has 2 pairs of constructors/accessors for matrices: One pointer-based pair 
(withNewMatrix and withMatrix) and one list-based pair (newMatrix and 
getMatrixComponents). Each pair has default definitions using the other pair, 
so you only have to implement one constructor (withNewMatrix or newMatrix) 
and one accessor (withMatrix or getMatrixComponents). Lists are often more 
handy when you explicitly specify the matrix by hand and the pointer-based 
stuff is often more convenient/efficient when you have the data already 
loaded from some external source.

The OpenGL package comes already with one abstract type (GLmatrix) which is an 
instance of Matrix, so you don't have to write instances for yourself if 
there are no special needs. GLmatrix is based on ForeignPtr internally, BTW, 
but this is really an implementation detail.

The other class involved is MatrixComponent:

http://haskell.org/ghc/docs/latest/html/libraries/OpenGL/Graphics.Rendering.OpenGL.GL.CoordTrans.html#t%3AMatrixComponent

Instances of this class are the possible types of matrix components which are 
supported by OpenGL, i.e. GLfloat and GLdouble.

So if the x, y, z variables in your example above are of type GLdouble and you 
have no special needs regarding the matrix representation, your question 
boils down to: How can I construct "GLmatrix GLdouble" given a list of 
GLdoubles in row-major order? The answer is: Use newMatrix and help Haskell's 
type inference a bit by giving an explicit type signature:

   ...
   m <- newMatrix RowMajor [1,0,0,x
                           ,0,1,0,y
                           ,0,0,1,z
                           ,0,0,0,1] :: IO (GLmatrix GLdouble)
   multMatrix m

The type signature is needed because the type of the matrix itself does not 
uniquely specify its components (and not the other way round, either). By 
using a named helper function with a type signature which wraps newMatrix you 
can get rid of the explicit type signature in the middle of the code above, 
but that's a matter of taste.

Hope that helps,
   S.


More information about the HOpenGL mailing list