{- Hey Emacs, this is -*- haskell -*- !
HOpenGL - a binding of OpenGL and GLUT for Haskell.
Copyright (c) 2002, Sven Panne
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. Neither the name of the author nor the names of its contributors may be
   used to endorse or promote products derived from this software without
   specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

This module corresponds to chapter 4 (Matrix manipulation) of the GLU specs.
-}

module GLU_Matrix (
   ortho2D, perspective, lookAt, pickMatrix,
   project, unProject
) where

import Foreign.Marshal.Alloc ( alloca )
import Foreign.Marshal.Utils ( with )
import Foreign.Ptr           ( Ptr )
import Foreign.Storable      ( Storable(peek) )
import GL_BasicTypes         ( boolToGLint, GLint, GLsizei, GLdouble )
import GL_VertexSpec         ( Vertex4(..) )
import GL_CoordTrans         ( GLmatrix )

---------------------------------------------------------------------------
-- matrix setup

foreign import CALLCONV unsafe "gluOrtho2D" ortho2D ::
   GLdouble -> GLdouble -> GLdouble -> GLdouble -> IO ()

foreign import CALLCONV unsafe "gluPerspective" perspective ::
   GLdouble -> GLdouble -> GLdouble -> GLdouble -> IO ()

lookAt :: (GLdouble, GLdouble, GLdouble)
       -> (GLdouble, GLdouble, GLdouble)
       -> (GLdouble, GLdouble, GLdouble) -> IO ()
lookAt (eyeX, eyeY, eyeZ) (centerX, centerY, centerZ) (upX, upY, upZ) =
   gluLookAt eyeX eyeY eyeZ centerX centerY centerZ upX upY upZ

foreign import CALLCONV unsafe "gluLookAt" gluLookAt ::
      GLdouble -> GLdouble -> GLdouble
   -> GLdouble -> GLdouble -> GLdouble
   -> GLdouble -> GLdouble -> GLdouble -> IO ()
pickMatrix :: (GLdouble, GLdouble) -> (GLdouble, GLdouble)
           -> (GLint, GLint) -> (GLsizei, GLsizei) -> IO ()
pickMatrix (x, y) (w, h) (vx, vy) (vw, vh) =
   with (Vertex4 vx vy (fromIntegral vw) (fromIntegral vh))
        (pickMatrixAux x y w h)

foreign import CALLCONV unsafe "gluPickMatrix" pickMatrixAux ::
   GLdouble -> GLdouble -> GLdouble -> GLdouble -> Ptr (Vertex4 GLint) -> IO ()

---------------------------------------------------------------------------
-- coordinate projection
-- TODO: Missing for GLU 1.3: gluUnProject4

type UnProjFunc =
      (GLdouble, GLdouble, GLdouble)
   -> GLmatrix Double -> GLmatrix Double -> (GLint, GLint) -> (GLsizei, GLsizei)
   -> IO (Maybe (GLdouble, GLdouble, GLdouble))

type UnProjFuncInternal =
      GLdouble -> GLdouble -> GLdouble
   -> Ptr (GLmatrix GLdouble) -> Ptr (GLmatrix GLdouble) -> Ptr (Vertex4 GLint)
   -> Ptr GLdouble -> Ptr GLdouble -> Ptr GLdouble -> IO GLint

project :: UnProjFunc
project = projHelper gluProject

foreign import CALLCONV unsafe "gluProject" gluProject :: UnProjFuncInternal

unProject :: UnProjFunc
unProject = projHelper gluUnProject

foreign import CALLCONV unsafe "gluUnProject" gluUnProject :: UnProjFuncInternal

projHelper :: UnProjFuncInternal -> UnProjFunc
projHelper upf (objX, objY, objZ) model proj (x, y) (w, h) =
   with model $ \modelBuf ->
   with proj  $ \projBuf  ->
   with (Vertex4 (fromIntegral x) (fromIntegral y) (fromIntegral w) (fromIntegral h)) $ \viewBuf  ->
   alloca           $ \winXBuf  ->
   alloca           $ \winYBuf  ->
   alloca           $ \winZBuf  -> do
   success  <- upf objX objY objZ modelBuf projBuf viewBuf winXBuf winYBuf winZBuf
   if success == boolToGLint True
      then do winX <- peek winXBuf
              winY <- peek winYBuf
              winZ <- peek winZBuf
              return $ Just (winX, winY, winZ)
      else return Nothing
