(not?) using preservingMatrix

View: New views
11 Messages — Rating Filter:   Alert me  

(not?) using preservingMatrix

by jamin1001 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In C OpenGL, you can do something like this to use matrices for computation without affecting context:

glMatrixMode (GL_MODELVIEW) ;
glPushMatrix () ;
glLoadMatrix (your_matrix) ;
// do whatever you want with your your matrix
// for exampl glMultMatrix (your_matrix2) ;
...
// at the end of your calculations use
glGetFloatv (GL_MODELVIEW_MATRIX, your_matrix_result) ;
glPopMatrix () ; // nothing has changed !!!

I'm not exactly sure how this is done with HOpenGL. i.e., how can I get the result of a matrix transformation not effecting the MODELVIEW, etc. matrices?  Could someone give an example?


Thanks,

Jamin


Re: (not?) using preservingMatrix

by Sebastian Sylvan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 1/5/07, jamin1001 <jamin1001@...> wrote:

>
> In C OpenGL, you can do something like this to use matrices for computation
> without affecting context:
>
> glMatrixMode (GL_MODELVIEW) ;
> glPushMatrix () ;
> glLoadMatrix (your_matrix) ;
> // do whatever you want with your your matrix
> // for exampl glMultMatrix (your_matrix2) ;
> ...
> // at the end of your calculations use
> glGetFloatv (GL_MODELVIEW_MATRIX, your_matrix_result) ;
> glPopMatrix () ; // nothing has changed !!!
>
> I'm not exactly sure how this is done with HOpenGL. i.e., how can I get the
> result of a matrix transformation not effecting the MODELVIEW, etc.
> matrices?  Could someone give an example?
>
>
> Thanks,
>
> Jamin

glPushMatrix() ; foo ; glPopMatrix();

Is written like so:

preservingMatrix foo

Often you would write things like
do foo
    preservingMatrix $
        do bar
            baz
    foo2

Here bar and baz may modify the matrices, but as soon as you leave the
scope of the action passed to preservingMatrix, the matrix stack is
popped (glPopMatrix()).

The Haskell way is better, since there is no way of ever forgetting to
pop the matrix - preservingMatrix is like a user defined control
structure. That's what first class actions buys you.

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
HOpenGL mailing list
HOpenGL@...
http://www.haskell.org/mailman/listinfo/hopengl

Re: (not?) using preservingMatrix

by jamin1001 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Right, I buy that.  But I don't see how the C line

glGetFloatv (GL_MODELVIEW_MATRIX, your_matrix_result) ;

can be done, since it writes a result into your_matrix_result, so I would need something like:

preservingMatrix $ do
    loadIdentity
    -- do whatever you want with your your matrix
    rs <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLdouble)) -- this does not compile, but I need rs
    return ()

I want the matrix so that I can then transform a vector.  

Speaking of which, are there any ready-made ways to transform a vector with a matrix, or do I need to do that by hand (eg., see the post http://groups.google.com/group/comp.graphics.api.opengl/browse_thread/thread/831ee85b6f97eb15/2de3e6d9c6a1a9e5)

 
Thanks again,

Jamin


Sebastian Sylvan wrote:
On 1/5/07, jamin1001 <jamin1001@yahoo.com> wrote:
>
> In C OpenGL, you can do something like this to use matrices for computation
> without affecting context:
>
> glMatrixMode (GL_MODELVIEW) ;
> glPushMatrix () ;
> glLoadMatrix (your_matrix) ;
> // do whatever you want with your your matrix
> // for exampl glMultMatrix (your_matrix2) ;
> ...
> // at the end of your calculations use
> glGetFloatv (GL_MODELVIEW_MATRIX, your_matrix_result) ;
> glPopMatrix () ; // nothing has changed !!!
>
> I'm not exactly sure how this is done with HOpenGL. i.e., how can I get the
> result of a matrix transformation not effecting the MODELVIEW, etc.
> matrices?  Could someone give an example?
>
>
> Thanks,
>
> Jamin

glPushMatrix() ; foo ; glPopMatrix();

Is written like so:

preservingMatrix foo

Often you would write things like
do foo
    preservingMatrix $
        do bar
            baz
    foo2

Here bar and baz may modify the matrices, but as soon as you leave the
scope of the action passed to preservingMatrix, the matrix stack is
popped (glPopMatrix()).

The Haskell way is better, since there is no way of ever forgetting to
pop the matrix - preservingMatrix is like a user defined control
structure. That's what first class actions buys you.

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
HOpenGL mailing list
HOpenGL@haskell.org
http://www.haskell.org/mailman/listinfo/hopengl

Re: (not?) using preservingMatrix

by Sebastian Sylvan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 1/5/07, jamin1001 <jamin1001@...> wrote:

>
> Right, I buy that.  But I don't see how the C line
>
> glGetFloatv (GL_MODELVIEW_MATRIX, your_matrix_result) ;
>
> can be done, since it writes a result into your_matrix_result, so I would
> need something like:
>
> preservingMatrix $ do
>     loadIdentity
>     -- do whatever you want with your your matrix
>     rs <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLdouble))

This does indeed compile for me. GHC 6.6.

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
HOpenGL mailing list
HOpenGL@...
http://www.haskell.org/mailman/listinfo/hopengl

Re: (not?) using preservingMatrix

by Sebastian Sylvan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 1/5/07, Sebastian Sylvan <sebastian.sylvan@...> wrote:

> On 1/5/07, jamin1001 <jamin1001@...> wrote:
> >
> > Right, I buy that.  But I don't see how the C line
> >
> > glGetFloatv (GL_MODELVIEW_MATRIX, your_matrix_result) ;
> >
> > can be done, since it writes a result into your_matrix_result, so I would
> > need something like:
> >
> > preservingMatrix $ do
> >     loadIdentity
> >     -- do whatever you want with your your matrix
> >     rs <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLdouble))
>
> This does indeed compile for me. GHC 6.6.
>

Though I would've probably avoidd all those ugly type signatures and
either put one on the action which does this, or (probably) just not
specify the type at all since whatever you do with rs will likely
narrow the type down to GLmatrix GLdouble anyway. Or if it still
doesn't work you could write:

s <- get $ matrix $ Just $ Modelview 0 :: IO (GLmatrix GLdouble)

And get rid of some of the clutter. The reason you need a type is due
to the monomorphism restriction, which you can get rid of using
-fno-monomorphis-restriction.

Also you don't need to use "matrix", you can use "currentMatrix"
(analogous to your C code), which will retrieve whatever matrix is set
using the matrixMode state variable.


--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
HOpenGL mailing list
HOpenGL@...
http://www.haskell.org/mailman/listinfo/hopengl

Re: (not?) using preservingMatrix

by Sebastian Sylvan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 1/5/07, Sebastian Sylvan <sebastian.sylvan@...> wrote:

> Also you don't need to use "matrix", you can use "currentMatrix"
> (analogous to your C code), which will retrieve whatever matrix is set
> using the matrixMode state variable.
>

Eek! Sorry, I see that "currentMatrix" is deprecated, so you should
probably use "matrix" instead :-)

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
HOpenGL mailing list
HOpenGL@...
http://www.haskell.org/mailman/listinfo/hopengl

Re: (not?) using preservingMatrix

by jamin1001 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Sebastian Sylvan wrote:
On 1/5/07, jamin1001 <jamin1001@yahoo.com> wrote:
>
> Right, I buy that.  But I don't see how the C line
>
> glGetFloatv (GL_MODELVIEW_MATRIX, your_matrix_result) ;
>
> can be done, since it writes a result into your_matrix_result, so I would
> need something like:
>
> preservingMatrix $ do
>     loadIdentity
>     -- do whatever you want with your your matrix
>     rs <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLdouble))

This does indeed compile for me. GHC 6.6.

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
HOpenGL mailing list
HOpenGL@haskell.org
http://www.haskell.org/mailman/listinfo/hopengl

Re: (not?) using preservingMatrix

by jamin1001 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

My last post was eaten by this "Nabble Forums" web page!

...

Ah, you're right.  What didn't compile for me was this additional line after the preservingMatrix call:
putStr $ "Result matrix: " ++ (show rs) ++ "\n"


Instead, I want to return that value to use outside the call.  This is what I was after, I think:

rs2 <- preservingMatrix $ do
      loadIdentity
      -- do whatever you want with your your matrix
      rs <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLdouble))
      return (rs)
putStr $ "Result matrix: " ++ (show rs2) ++ "\n"

Now, as for question #2, are there any pre-made ways to transform a vector with a matrix, or do I have to do it by hand (same goes for cross product!) (see the post http://groups.google.com/group/comp.graphics.api.opengl/browse_thread/thread/831ee85b6f97eb15/2de3e6d9c6a1a9e5)




Sebastian Sylvan wrote:
On 1/5/07, jamin1001 <jamin1001@yahoo.com> wrote:
>
> Right, I buy that.  But I don't see how the C line
>
> glGetFloatv (GL_MODELVIEW_MATRIX, your_matrix_result) ;
>
> can be done, since it writes a result into your_matrix_result, so I would
> need something like:
>
> preservingMatrix $ do
>     loadIdentity
>     -- do whatever you want with your your matrix
>     rs <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLdouble))

This does indeed compile for me. GHC 6.6.

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
HOpenGL mailing list
HOpenGL@haskell.org
http://www.haskell.org/mailman/listinfo/hopengl


Re: (not?) using preservingMatrix

by Sebastian Sylvan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 1/5/07, jamin1001 <jamin1001@...> wrote:

>
> My last post was eaten by this "Nabble Forums" web page!
>
> ...
>
> Ah, you're right.  What didn't compile for me was this additional line after
> the preservingMatrix call:
> putStr $ "Result matrix: " ++ (show rs) ++ "\n"
>
>
> Instead, I want to return that value to use outside the call.  This is what
> I was after, I think:
>
> rs2 <- preservingMatrix $ do
>       loadIdentity
>       -- do whatever you want with your your matrix
>       rs <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLdouble))
>       return (rs)
> putStr $ "Result matrix: " ++ (show rs2) ++ "\n"
>
> Now, as for question #2, are there any pre-made ways to transform a vector
> with a matrix, or do I have to do it by hand (same goes for cross product!)

Not with OpenGL. If you go to the haskell.org website and check out
the libraries page I'm sure you'll find plenty of linear algebra
libraries. This is a problem that you have to face in any language,
btw, at least any language which doesn't include linear algebra
operations.

Oh, and also

do bar
     xs <- foo
     return xs

is equal to

do bar
     foo

I'e you can loose the arrow and the "return".


/S
--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
HOpenGL mailing list
HOpenGL@...
http://www.haskell.org/mailman/listinfo/hopengl

Re: (not?) using preservingMatrix

by jamin1001 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you very much.  I appreciate the detailed response and extra tips!

Jamin


Sebastian Sylvan wrote:
On 1/5/07, jamin1001 <jamin1001@yahoo.com> wrote:
>
> My last post was eaten by this "Nabble Forums" web page!
>
> ...
>
> Ah, you're right.  What didn't compile for me was this additional line after
> the preservingMatrix call:
> putStr $ "Result matrix: " ++ (show rs) ++ "\n"
>
>
> Instead, I want to return that value to use outside the call.  This is what
> I was after, I think:
>
> rs2 <- preservingMatrix $ do
>       loadIdentity
>       -- do whatever you want with your your matrix
>       rs <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLdouble))
>       return (rs)
> putStr $ "Result matrix: " ++ (show rs2) ++ "\n"
>
> Now, as for question #2, are there any pre-made ways to transform a vector
> with a matrix, or do I have to do it by hand (same goes for cross product!)

Not with OpenGL. If you go to the haskell.org website and check out
the libraries page I'm sure you'll find plenty of linear algebra
libraries. This is a problem that you have to face in any language,
btw, at least any language which doesn't include linear algebra
operations.

Oh, and also

do bar
     xs <- foo
     return xs

is equal to

do bar
     foo

I'e you can loose the arrow and the "return".


/S
--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
_______________________________________________
HOpenGL mailing list
HOpenGL@haskell.org
http://www.haskell.org/mailman/listinfo/hopengl

Re: (not?) using preservingMatrix

by Sven Panne :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Am Freitag, 5. Januar 2007 22:19 schrieb jamin1001:
> Thank you very much.  I appreciate the detailed response and extra tips!

Thanks to Sebastian, too. Just a few remarks from my side:

   * preservingMatrix actually does a little bit more: It uses the current
matrix mode in effect at the time it was called when popping the matrix, and
does this in an exception-safe way. There is a variant for performance freaks
and/or people who can guarantee that an exception will never be thrown by the
action passed (unsafePreservingMatrix). In the same spirit, there is an
unsafe variant of renderPrimitive, too.

   * "currentMatrix" is equivalent to "matrix Nothing", using whichever matrix
mode is currently set. The example given states an explicit matrix to be
accessed.

   * Sometimes explicit type signatures are needed when the OpenGL package is
used, although this rarely happens for non-toy programs. This is a
consequence of a design decision to be quite flexible regarding the
represenation of some data structures (matrices, in our example). Although
there is already a ready-to-use GLmatrix type, one could use custom data
structures for matrices, as long as they are an instance of the class Matrix
(define withNewMatrix or newMatrix, plus withMatrix or getMatrixComponents).
Similar examples are the Map1/Map2/PolygonStipple classes.

   * A hopefully good source of examples is the "examples" subdirectory of the
GLUT package (http://darcs.haskell.org/packages/GLUT/examples/), which I
intend to extend constantly. The style follows the books in question very
closely, so it should be easy to see how the OpenGL package can be used. An
example which retrieves a calculated matrix is in

   http://darcs.haskell.org/packages/GLUT/examples/RedBook/ShadowMap.hs

(see generateTextureMatrix).

Cheers,
   S.
_______________________________________________
HOpenGL mailing list
HOpenGL@...
http://www.haskell.org/mailman/listinfo/hopengl