--------------------------------------------------------------------------------
-- |
-- Module      :  Graphics.Rendering.OpenGL.GL.CoordTrans
-- Copyright   :  (c) Sven Panne 2002-2019
-- License     :  BSD3
--
-- Maintainer  :  Sven Panne <svenpanne@gmail.com>
-- Stability   :  stable
-- Portability :  portable
--
-- This module corresponds to section 2.11 (Coordinate Transformations) of the
-- OpenGL 2.1 specs.
--
--------------------------------------------------------------------------------

module Graphics.Rendering.OpenGL.GL.CoordTrans (
   -- * Controlling the Viewport
   depthRange,
   Position(..), Size(..), viewport, maxViewportDims,

   -- * Matrices
   MatrixMode(..), matrixMode,
   MatrixOrder(..), MatrixComponent(rotate,translate,scale), Matrix(..),
   matrix, multMatrix, GLmatrix, loadIdentity,
   ortho, frustum, depthClamp,
   activeTexture,
   preservingMatrix, unsafePreservingMatrix,
   stackDepth, maxStackDepth,

   -- * Normal Transformation
   rescaleNormal, normalize,

   -- * Generating Texture Coordinates
   Plane(..), TextureCoordName(..), TextureGenMode(..), textureGenMode
) where

import Data.StateVar
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc
import Foreign.Marshal.Array
import Foreign.Marshal.Utils
import Foreign.Ptr
import Foreign.Storable
import Graphics.Rendering.OpenGL.GL.Capability
import Graphics.Rendering.OpenGL.GL.Exception
import Graphics.Rendering.OpenGL.GL.MatrixComponent
import Graphics.Rendering.OpenGL.GL.PeekPoke
import Graphics.Rendering.OpenGL.GL.QueryUtils
import Graphics.Rendering.OpenGL.GL.Texturing.TextureUnit
import Graphics.Rendering.OpenGL.GLU.ErrorsInternal
import Graphics.GL

--------------------------------------------------------------------------------

-- | After clipping and division by /w/, depth coordinates range from -1 to 1,
-- corresponding to the near and far clipping planes. 'depthRange' specifies a
-- linear mapping of the normalized depth coordinates in this range to window
-- depth coordinates. Regardless of the actual depth buffer implementation,
-- window coordinate depth values are treated as though they range from 0
-- through 1 (like color components). Thus, the values accepted by 'depthRange'
-- are both clamped to this range before they are accepted.
--
-- The initial setting of (0, 1) maps the near plane to 0 and the far plane to
-- 1. With this mapping, the depth buffer range is fully utilized.
--
-- It is not necessary that the near value be less than the far value. Reverse
-- mappings such as (1, 0) are acceptable.

depthRange :: StateVar (GLclampd, GLclampd)
depthRange :: StateVar (GLdouble, GLdouble)
depthRange = forall a. IO a -> (a -> IO ()) -> StateVar a
makeStateVar (forall p a.
GetPName2F p =>
(GLdouble -> GLdouble -> a) -> p -> IO a
getClampd2 (,) PName2F
GetDepthRange) (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall (m :: * -> *). MonadIO m => GLdouble -> GLdouble -> m ()
glDepthRange)

--------------------------------------------------------------------------------

-- | A 2-dimensional position, measured in pixels.
data Position = Position !GLint !GLint
   deriving ( Position -> Position -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Position -> Position -> Bool
$c/= :: Position -> Position -> Bool
== :: Position -> Position -> Bool
$c== :: Position -> Position -> Bool
Eq, Eq Position
Position -> Position -> Bool
Position -> Position -> Ordering
Position -> Position -> Position
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Position -> Position -> Position
$cmin :: Position -> Position -> Position
max :: Position -> Position -> Position
$cmax :: Position -> Position -> Position
>= :: Position -> Position -> Bool
$c>= :: Position -> Position -> Bool
> :: Position -> Position -> Bool
$c> :: Position -> Position -> Bool
<= :: Position -> Position -> Bool
$c<= :: Position -> Position -> Bool
< :: Position -> Position -> Bool
$c< :: Position -> Position -> Bool
compare :: Position -> Position -> Ordering
$ccompare :: Position -> Position -> Ordering
Ord, Int -> Position -> ShowS
[Position] -> ShowS
Position -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Position] -> ShowS
$cshowList :: [Position] -> ShowS
show :: Position -> String
$cshow :: Position -> String
showsPrec :: Int -> Position -> ShowS
$cshowsPrec :: Int -> Position -> ShowS
Show )

-- | A 2-dimensional size, measured in pixels.
data Size = Size !GLsizei !GLsizei
   deriving ( Size -> Size -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Size -> Size -> Bool
$c/= :: Size -> Size -> Bool
== :: Size -> Size -> Bool
$c== :: Size -> Size -> Bool
Eq, Eq Size
Size -> Size -> Bool
Size -> Size -> Ordering
Size -> Size -> Size
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Size -> Size -> Size
$cmin :: Size -> Size -> Size
max :: Size -> Size -> Size
$cmax :: Size -> Size -> Size
>= :: Size -> Size -> Bool
$c>= :: Size -> Size -> Bool
> :: Size -> Size -> Bool
$c> :: Size -> Size -> Bool
<= :: Size -> Size -> Bool
$c<= :: Size -> Size -> Bool
< :: Size -> Size -> Bool
$c< :: Size -> Size -> Bool
compare :: Size -> Size -> Ordering
$ccompare :: Size -> Size -> Ordering
Ord, Int -> Size -> ShowS
[Size] -> ShowS
Size -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Size] -> ShowS
$cshowList :: [Size] -> ShowS
show :: Size -> String
$cshow :: Size -> String
showsPrec :: Int -> Size -> ShowS
$cshowsPrec :: Int -> Size -> ShowS
Show )

-- | Controls the affine transformation from normalized device coordinates to
-- window coordinates. The viewport state variable consists of the coordinates
-- (/x/, /y/) of the lower left corner of the viewport rectangle, (in pixels,
-- initial value (0,0)), and the size (/width/, /height/) of the viewport. When
-- a GL context is first attached to a window, /width/ and /height/ are set to
-- the dimensions of that window.
--
-- Let (/xnd/, /ynd/) be normalized device coordinates. Then the window
-- coordinates (/xw/, /yw/) are computed as follows:
--
-- /xw/ = (/xnd/ + 1) (/width/  \/ 2) + /x/
--
-- /yw/ = (/ynd/ + 1) (/heigth/ \/ 2) + /y/
--
-- Viewport width and height are silently clamped to a range that depends on the
-- implementation, see 'maxViewportDims'.

viewport :: StateVar (Position, Size)
viewport :: StateVar (Position, Size)
viewport = forall a. IO a -> (a -> IO ()) -> StateVar a
makeStateVar (forall p a.
GetPName4I p =>
(GLint -> GLint -> GLint -> GLint -> a) -> p -> IO a
getInteger4 forall {a} {a}.
(Integral a, Integral a) =>
GLint -> GLint -> a -> a -> (Position, Size)
makeVp PName4I
GetViewport)
                        (\(Position GLint
x GLint
y, Size GLint
w GLint
h) -> forall (m :: * -> *).
MonadIO m =>
GLint -> GLint -> GLint -> GLint -> m ()
glViewport GLint
x GLint
y GLint
w GLint
h)
   where makeVp :: GLint -> GLint -> a -> a -> (Position, Size)
makeVp GLint
x GLint
y a
w a
h = (GLint -> GLint -> Position
Position GLint
x GLint
y, GLint -> GLint -> Size
Size (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
w) (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
h))

-- | The implementation-dependent maximum viewport width and height.

maxViewportDims :: GettableStateVar Size
maxViewportDims :: GettableStateVar Size
maxViewportDims = forall a. IO a -> IO a
makeGettableStateVar (forall p a. GetPName2I p => (GLint -> GLint -> a) -> p -> IO a
getSizei2 GLint -> GLint -> Size
Size PName2I
GetMaxViewportDims)

--------------------------------------------------------------------------------

-- | A matrix stack.

data MatrixMode =
     Modelview GLsizei  -- ^ The modelview matrix stack of the specified vertex unit.
   | Projection         -- ^ The projection matrix stack.
   | Texture            -- ^ The texture matrix stack.
   | Color              -- ^ The color matrix stack.
   | MatrixPalette      -- ^ The matrix palette stack.
   deriving ( MatrixMode -> MatrixMode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MatrixMode -> MatrixMode -> Bool
$c/= :: MatrixMode -> MatrixMode -> Bool
== :: MatrixMode -> MatrixMode -> Bool
$c== :: MatrixMode -> MatrixMode -> Bool
Eq, Eq MatrixMode
MatrixMode -> MatrixMode -> Bool
MatrixMode -> MatrixMode -> Ordering
MatrixMode -> MatrixMode -> MatrixMode
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: MatrixMode -> MatrixMode -> MatrixMode
$cmin :: MatrixMode -> MatrixMode -> MatrixMode
max :: MatrixMode -> MatrixMode -> MatrixMode
$cmax :: MatrixMode -> MatrixMode -> MatrixMode
>= :: MatrixMode -> MatrixMode -> Bool
$c>= :: MatrixMode -> MatrixMode -> Bool
> :: MatrixMode -> MatrixMode -> Bool
$c> :: MatrixMode -> MatrixMode -> Bool
<= :: MatrixMode -> MatrixMode -> Bool
$c<= :: MatrixMode -> MatrixMode -> Bool
< :: MatrixMode -> MatrixMode -> Bool
$c< :: MatrixMode -> MatrixMode -> Bool
compare :: MatrixMode -> MatrixMode -> Ordering
$ccompare :: MatrixMode -> MatrixMode -> Ordering
Ord, Int -> MatrixMode -> ShowS
[MatrixMode] -> ShowS
MatrixMode -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MatrixMode] -> ShowS
$cshowList :: [MatrixMode] -> ShowS
show :: MatrixMode -> String
$cshow :: MatrixMode -> String
showsPrec :: Int -> MatrixMode -> ShowS
$cshowsPrec :: Int -> MatrixMode -> ShowS
Show )

marshalMatrixMode :: MatrixMode -> Maybe GLenum
marshalMatrixMode :: MatrixMode -> Maybe GLenum
marshalMatrixMode MatrixMode
x = case MatrixMode
x of
   Modelview GLint
i -> GLint -> Maybe GLenum
modelviewIndexToEnum GLint
i
   MatrixMode
Projection -> forall a. a -> Maybe a
Just GLenum
GL_PROJECTION
   MatrixMode
Texture -> forall a. a -> Maybe a
Just GLenum
GL_TEXTURE
   MatrixMode
Color -> forall a. a -> Maybe a
Just GLenum
GL_COLOR
   MatrixMode
MatrixPalette -> forall a. a -> Maybe a
Just GLenum
GL_MATRIX_PALETTE_ARB

unmarshalMatrixMode :: GLenum -> MatrixMode
unmarshalMatrixMode :: GLenum -> MatrixMode
unmarshalMatrixMode GLenum
x
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_PROJECTION = MatrixMode
Projection
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_TEXTURE = MatrixMode
Texture
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_COLOR = MatrixMode
Color
   | GLenum
x forall a. Eq a => a -> a -> Bool
== GLenum
GL_MATRIX_PALETTE_ARB = MatrixMode
MatrixPalette
   | Bool
otherwise =
        case GLenum -> Maybe GLint
modelviewEnumToIndex GLenum
x of
           Just GLint
i -> GLint -> MatrixMode
Modelview GLint
i
           Maybe GLint
Nothing -> forall a. HasCallStack => String -> a
error (String
"unmarshalMatrixMode: illegal value " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show GLenum
x)

matrixModeToGetMatrix :: MatrixMode -> PNameMatrix
matrixModeToGetMatrix :: MatrixMode -> PNameMatrix
matrixModeToGetMatrix MatrixMode
x = case MatrixMode
x of
   Modelview GLint
_   -> PNameMatrix
GetModelviewMatrix -- ???
   MatrixMode
Projection    -> PNameMatrix
GetProjectionMatrix
   MatrixMode
Texture       -> PNameMatrix
GetTextureMatrix
   MatrixMode
Color         -> PNameMatrix
GetColorMatrix
   MatrixMode
MatrixPalette -> PNameMatrix
GetMatrixPalette

matrixModeToGetStackDepth :: MatrixMode -> PName1I
matrixModeToGetStackDepth :: MatrixMode -> PName1I
matrixModeToGetStackDepth MatrixMode
x =  case MatrixMode
x of
   Modelview GLint
_   -> PName1I
GetModelviewStackDepth
   MatrixMode
Projection    -> PName1I
GetProjectionStackDepth
   MatrixMode
Texture       -> PName1I
GetTextureStackDepth
   MatrixMode
Color         -> PName1I
GetColorMatrixStackDepth
   MatrixMode
MatrixPalette -> forall a. HasCallStack => String -> a
error String
"matrixModeToGetStackDepth: impossible"

matrixModeToGetMaxStackDepth :: MatrixMode -> PName1I
matrixModeToGetMaxStackDepth :: MatrixMode -> PName1I
matrixModeToGetMaxStackDepth MatrixMode
x = case MatrixMode
x of
   Modelview GLint
_    -> PName1I
GetMaxModelviewStackDepth
   MatrixMode
Projection     -> PName1I
GetMaxProjectionStackDepth
   MatrixMode
Texture        -> PName1I
GetMaxTextureStackDepth
   MatrixMode
Color          -> PName1I
GetMaxColorMatrixStackDepth
   MatrixMode
MatrixPalette  -> PName1I
GetMaxMatrixPaletteStackDepth

--------------------------------------------------------------------------------

-- | Controls which matrix stack is the target for subsequent matrix operations.
-- The initial value is ('Modelview' 0).

matrixMode :: StateVar MatrixMode
matrixMode :: StateVar MatrixMode
matrixMode =
   forall a. IO a -> (a -> IO ()) -> StateVar a
makeStateVar (forall p a. GetPName1I p => (GLenum -> a) -> p -> IO a
getEnum1 GLenum -> MatrixMode
unmarshalMatrixMode PName1I
GetMatrixMode)
                (forall b a. b -> (a -> b) -> Maybe a -> b
maybe IO ()
recordInvalidValue forall (m :: * -> *). MonadIO m => GLenum -> m ()
glMatrixMode forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatrixMode -> Maybe GLenum
marshalMatrixMode)

--------------------------------------------------------------------------------

data MatrixOrder = ColumnMajor | RowMajor
   deriving ( MatrixOrder -> MatrixOrder -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MatrixOrder -> MatrixOrder -> Bool
$c/= :: MatrixOrder -> MatrixOrder -> Bool
== :: MatrixOrder -> MatrixOrder -> Bool
$c== :: MatrixOrder -> MatrixOrder -> Bool
Eq, Eq MatrixOrder
MatrixOrder -> MatrixOrder -> Bool
MatrixOrder -> MatrixOrder -> Ordering
MatrixOrder -> MatrixOrder -> MatrixOrder
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: MatrixOrder -> MatrixOrder -> MatrixOrder
$cmin :: MatrixOrder -> MatrixOrder -> MatrixOrder
max :: MatrixOrder -> MatrixOrder -> MatrixOrder
$cmax :: MatrixOrder -> MatrixOrder -> MatrixOrder
>= :: MatrixOrder -> MatrixOrder -> Bool
$c>= :: MatrixOrder -> MatrixOrder -> Bool
> :: MatrixOrder -> MatrixOrder -> Bool
$c> :: MatrixOrder -> MatrixOrder -> Bool
<= :: MatrixOrder -> MatrixOrder -> Bool
$c<= :: MatrixOrder -> MatrixOrder -> Bool
< :: MatrixOrder -> MatrixOrder -> Bool
$c< :: MatrixOrder -> MatrixOrder -> Bool
compare :: MatrixOrder -> MatrixOrder -> Ordering
$ccompare :: MatrixOrder -> MatrixOrder -> Ordering
Ord, Int -> MatrixOrder -> ShowS
[MatrixOrder] -> ShowS
MatrixOrder -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MatrixOrder] -> ShowS
$cshowList :: [MatrixOrder] -> ShowS
show :: MatrixOrder -> String
$cshow :: MatrixOrder -> String
showsPrec :: Int -> MatrixOrder -> ShowS
$cshowsPrec :: Int -> MatrixOrder -> ShowS
Show )

--------------------------------------------------------------------------------

class Matrix m where
   -- | Create a new matrix of the given order (containing undefined elements)
   -- and call the action to fill it with 4x4 elements.
   withNewMatrix ::
      MatrixComponent c => MatrixOrder -> (Ptr c -> IO ()) -> IO (m c)

   -- | Call the action with the given matrix. /Note:/ The action is /not/
   -- allowed to modify the matrix elements!
   withMatrix ::
      MatrixComponent c => m c -> (MatrixOrder -> Ptr c -> IO a) -> IO a

   newMatrix :: MatrixComponent c => MatrixOrder -> [c] -> IO (m c)
   getMatrixComponents :: MatrixComponent c => MatrixOrder -> m c -> IO [c]

   withNewMatrix MatrixOrder
order Ptr c -> IO ()
act =
      forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray Int
16 forall a b. (a -> b) -> a -> b
$ \Ptr c
p -> do
         Ptr c -> IO ()
act Ptr c
p
         [c]
components <- forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray Int
16 Ptr c
p
         forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
MatrixOrder -> [c] -> IO (m c)
newMatrix MatrixOrder
order [c]
components

   withMatrix m c
mat MatrixOrder -> Ptr c -> IO a
act = do
      [c]
components <- forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
MatrixOrder -> m c -> IO [c]
getMatrixComponents MatrixOrder
ColumnMajor m c
mat
      forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray [c]
components forall a b. (a -> b) -> a -> b
$ MatrixOrder -> Ptr c -> IO a
act MatrixOrder
ColumnMajor

   newMatrix MatrixOrder
order [c]
components =
      forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
MatrixOrder -> (Ptr c -> IO ()) -> IO (m c)
withNewMatrix MatrixOrder
order forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Storable a => Ptr a -> [a] -> IO ()
pokeArray (forall a. Int -> [a] -> [a]
take Int
16 [c]
components)

   getMatrixComponents MatrixOrder
desiredOrder m c
mat =
      forall (m :: * -> *) c a.
(Matrix m, MatrixComponent c) =>
m c -> (MatrixOrder -> Ptr c -> IO a) -> IO a
withMatrix m c
mat forall a b. (a -> b) -> a -> b
$ \MatrixOrder
order Ptr c
p ->
        if MatrixOrder
desiredOrder forall a. Eq a => a -> a -> Bool
== MatrixOrder
order
           then forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray Int
16 Ptr c
p
           else forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr c
p) [ Int
0, Int
4,  Int
8, Int
12,
                                       Int
1, Int
5,  Int
9, Int
13,
                                       Int
2, Int
6, Int
10, Int
14,
                                       Int
3, Int
7, Int
11, Int
15 ]

--------------------------------------------------------------------------------

matrix :: (Matrix m, MatrixComponent c) => Maybe MatrixMode -> StateVar (m c)
matrix :: forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
Maybe MatrixMode -> StateVar (m c)
matrix Maybe MatrixMode
maybeMode =
   forall a. IO a -> (a -> IO ()) -> StateVar a
makeStateVar
      (forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall t a (m :: * -> *). (HasGetter t a, MonadIO m) => t -> m a
get StateVar MatrixMode
matrixMode) forall (m :: * -> *) a. Monad m => a -> m a
return Maybe MatrixMode
maybeMode forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
PNameMatrix -> IO (m c)
getMatrix' forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatrixMode -> PNameMatrix
matrixModeToGetMatrix))
      (forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id forall a. MatrixMode -> IO a -> IO a
withMatrixMode Maybe MatrixMode
maybeMode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
m c -> IO ()
setMatrix)

withMatrixMode :: MatrixMode -> IO a -> IO a
withMatrixMode :: forall a. MatrixMode -> IO a -> IO a
withMatrixMode MatrixMode
mode IO a
act =
   forall a. IO a -> IO a
preservingMatrixMode forall a b. (a -> b) -> a -> b
$ do
      StateVar MatrixMode
matrixMode forall t a (m :: * -> *).
(HasSetter t a, MonadIO m) =>
t -> a -> m ()
$= MatrixMode
mode
      IO a
act

getMatrix' :: (Matrix m, MatrixComponent c) => PNameMatrix -> IO (m c)
getMatrix' :: forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
PNameMatrix -> IO (m c)
getMatrix' = forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
MatrixOrder -> (Ptr c -> IO ()) -> IO (m c)
withNewMatrix MatrixOrder
ColumnMajor forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c p.
(MatrixComponent c, GetPNameMatrix p) =>
p -> Ptr c -> IO ()
getMatrix

setMatrix :: (Matrix m, MatrixComponent c) => m c -> IO ()
setMatrix :: forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
m c -> IO ()
setMatrix m c
mat =
   forall (m :: * -> *) c a.
(Matrix m, MatrixComponent c) =>
m c -> (MatrixOrder -> Ptr c -> IO a) -> IO a
withMatrix m c
mat forall a b. (a -> b) -> a -> b
$ \MatrixOrder
order ->
      case MatrixOrder
order of
         MatrixOrder
ColumnMajor -> forall c. MatrixComponent c => Ptr c -> IO ()
loadMatrix
         MatrixOrder
RowMajor    -> forall c. MatrixComponent c => Ptr c -> IO ()
loadTransposeMatrix

multMatrix :: (Matrix m, MatrixComponent c) => m c -> IO ()
multMatrix :: forall (m :: * -> *) c.
(Matrix m, MatrixComponent c) =>
m c -> IO ()
multMatrix m c
mat =
   forall (m :: * -> *) c a.
(Matrix m, MatrixComponent c) =>
m c -> (MatrixOrder -> Ptr c -> IO a) -> IO a
withMatrix m c
mat forall a b. (a -> b) -> a -> b
$ \MatrixOrder
order ->
      case MatrixOrder
order of
         MatrixOrder
ColumnMajor -> forall c. MatrixComponent c => Ptr c -> IO ()
multMatrix_
         MatrixOrder
RowMajor    -> forall c. MatrixComponent c => Ptr c -> IO ()
multTransposeMatrix

--------------------------------------------------------------------------------

data GLmatrix a = GLmatrix MatrixOrder (ForeignPtr a)
   deriving ( GLmatrix a -> GLmatrix a -> Bool
forall a. GLmatrix a -> GLmatrix a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GLmatrix a -> GLmatrix a -> Bool
$c/= :: forall a. GLmatrix a -> GLmatrix a -> Bool
== :: GLmatrix a -> GLmatrix a -> Bool
$c== :: forall a. GLmatrix a -> GLmatrix a -> Bool
Eq, GLmatrix a -> GLmatrix a -> Bool
GLmatrix a -> GLmatrix a -> Ordering
forall a. Eq (GLmatrix a)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. GLmatrix a -> GLmatrix a -> Bool
forall a. GLmatrix a -> GLmatrix a -> Ordering
forall a. GLmatrix a -> GLmatrix a -> GLmatrix a
min :: GLmatrix a -> GLmatrix a -> GLmatrix a
$cmin :: forall a. GLmatrix a -> GLmatrix a -> GLmatrix a
max :: GLmatrix a -> GLmatrix a -> GLmatrix a
$cmax :: forall a. GLmatrix a -> GLmatrix a -> GLmatrix a
>= :: GLmatrix a -> GLmatrix a -> Bool
$c>= :: forall a. GLmatrix a -> GLmatrix a -> Bool
> :: GLmatrix a -> GLmatrix a -> Bool
$c> :: forall a. GLmatrix a -> GLmatrix a -> Bool
<= :: GLmatrix a -> GLmatrix a -> Bool
$c<= :: forall a. GLmatrix a -> GLmatrix a -> Bool
< :: GLmatrix a -> GLmatrix a -> Bool
$c< :: forall a. GLmatrix a -> GLmatrix a -> Bool
compare :: GLmatrix a -> GLmatrix a -> Ordering
$ccompare :: forall a. GLmatrix a -> GLmatrix a -> Ordering
Ord, Int -> GLmatrix a -> ShowS
forall a. Int -> GLmatrix a -> ShowS
forall a. [GLmatrix a] -> ShowS
forall a. GLmatrix a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GLmatrix a] -> ShowS
$cshowList :: forall a. [GLmatrix a] -> ShowS
show :: GLmatrix a -> String
$cshow :: forall a. GLmatrix a -> String
showsPrec :: Int -> GLmatrix a -> ShowS
$cshowsPrec :: forall a. Int -> GLmatrix a -> ShowS
Show )

instance Matrix GLmatrix where
   withNewMatrix :: forall c.
MatrixComponent c =>
MatrixOrder -> (Ptr c -> IO ()) -> IO (GLmatrix c)
withNewMatrix MatrixOrder
order Ptr c -> IO ()
f = do
      ForeignPtr c
fp <- forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray Int
16
      forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr c
fp Ptr c -> IO ()
f
      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. MatrixOrder -> ForeignPtr a -> GLmatrix a
GLmatrix MatrixOrder
order ForeignPtr c
fp

   withMatrix :: forall c a.
MatrixComponent c =>
GLmatrix c -> (MatrixOrder -> Ptr c -> IO a) -> IO a
withMatrix (GLmatrix MatrixOrder
order ForeignPtr c
fp) MatrixOrder -> Ptr c -> IO a
f = forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr c
fp (MatrixOrder -> Ptr c -> IO a
f MatrixOrder
order)

--------------------------------------------------------------------------------

loadIdentity :: IO ()
loadIdentity :: IO ()
loadIdentity = forall (m :: * -> *). MonadIO m => m ()
glLoadIdentity

--------------------------------------------------------------------------------

ortho :: GLdouble -> GLdouble -> GLdouble -> GLdouble -> GLdouble -> GLdouble -> IO ()
ortho :: GLdouble
-> GLdouble
-> GLdouble
-> GLdouble
-> GLdouble
-> GLdouble
-> IO ()
ortho = forall (m :: * -> *).
MonadIO m =>
GLdouble
-> GLdouble -> GLdouble -> GLdouble -> GLdouble -> GLdouble -> m ()
glOrtho

frustum :: GLdouble -> GLdouble -> GLdouble -> GLdouble -> GLdouble -> GLdouble -> IO ()
frustum :: GLdouble
-> GLdouble
-> GLdouble
-> GLdouble
-> GLdouble
-> GLdouble
-> IO ()
frustum = forall (m :: * -> *).
MonadIO m =>
GLdouble
-> GLdouble -> GLdouble -> GLdouble -> GLdouble -> GLdouble -> m ()
glFrustum

--------------------------------------------------------------------------------

depthClamp :: StateVar Capability
depthClamp :: StateVar Capability
depthClamp = EnableCap -> StateVar Capability
makeCapability EnableCap
CapDepthClamp

--------------------------------------------------------------------------------

activeTexture :: StateVar TextureUnit
activeTexture :: StateVar TextureUnit
activeTexture = forall a. IO a -> (a -> IO ()) -> StateVar a
makeStateVar (forall p a. GetPName1I p => (GLenum -> a) -> p -> IO a
getEnum1 GLenum -> TextureUnit
unmarshalTextureUnit PName1I
GetActiveTexture)
                             (forall (m :: * -> *). MonadIO m => GLenum -> m ()
glActiveTexture forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextureUnit -> GLenum
marshalTextureUnit)

--------------------------------------------------------------------------------

-- | Push the current matrix stack down by one, duplicating the current matrix,
-- excute the given action, and pop the current matrix stack, replacing the
-- current matrix with the one below it on the stack (i.e. restoring it to its
-- previous state). The returned value is that of the given action. Note that
-- a round-trip to the server is probably required. For a more efficient
-- version, see 'unsafePreservingMatrix'.

preservingMatrix :: IO a -> IO a
preservingMatrix :: forall a. IO a -> IO a
preservingMatrix = forall a. IO a -> IO a
unsafePreservingMatrix forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> IO a
preservingMatrixMode

-- performance paranoia: No (un-)marshaling by avoiding matrixMode
preservingMatrixMode :: IO a -> IO a
preservingMatrixMode :: forall a. IO a -> IO a
preservingMatrixMode = forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (forall p a. GetPName1I p => (GLenum -> a) -> p -> IO a
getEnum1 forall a. a -> a
id PName1I
GetMatrixMode) forall (m :: * -> *). MonadIO m => GLenum -> m ()
glMatrixMode forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const

-- | A more efficient, but potentially dangerous version of 'preservingMatrix':
-- The given action is not allowed to throw an exception or change the
-- current matrix mode permanently.

unsafePreservingMatrix :: IO a -> IO a
unsafePreservingMatrix :: forall a. IO a -> IO a
unsafePreservingMatrix = forall a b c. IO a -> IO b -> IO c -> IO c
unsafeBracket_ forall (m :: * -> *). MonadIO m => m ()
glPushMatrix forall (m :: * -> *). MonadIO m => m ()
glPopMatrix

--------------------------------------------------------------------------------

stackDepth :: Maybe MatrixMode -> GettableStateVar GLsizei
stackDepth :: Maybe MatrixMode -> GettableStateVar GLint
stackDepth Maybe MatrixMode
maybeMode =
   forall a. IO a -> IO a
makeGettableStateVar forall a b. (a -> b) -> a -> b
$
      case Maybe MatrixMode
maybeMode of
         Maybe MatrixMode
Nothing -> forall p a. GetPName1I p => (GLint -> a) -> p -> IO a
getSizei1 forall a. a -> a
id PName1I
GetCurrentMatrixStackDepth -- only with ARB_fragment_program
         Just MatrixMode
MatrixPalette -> do IO ()
recordInvalidEnum ; forall (m :: * -> *) a. Monad m => a -> m a
return GLint
0
         Just MatrixMode
mode -> forall p a. GetPName1I p => (GLint -> a) -> p -> IO a
getSizei1 forall a. a -> a
id (MatrixMode -> PName1I
matrixModeToGetStackDepth MatrixMode
mode)

maxStackDepth :: MatrixMode -> GettableStateVar GLsizei
maxStackDepth :: MatrixMode -> GettableStateVar GLint
maxStackDepth =
   forall a. IO a -> IO a
makeGettableStateVar forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p a. GetPName1I p => (GLint -> a) -> p -> IO a
getSizei1 forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatrixMode -> PName1I
matrixModeToGetMaxStackDepth

--------------------------------------------------------------------------------

-- | If 'rescaleNormal' contains 'Enabled', normal vectors specified with
-- 'Graphics.Rendering.OpenGL.GL.VertexSpec.normal' are scaled by a scaling
-- factor derived from the modelview matrix. 'rescaleNormal' requires that the
-- originally specified normals were of unit length, and that the modelview
-- matrix contains only uniform scales for proper results. The initial value of
-- 'rescaleNormal' is 'Disabled'.

rescaleNormal :: StateVar Capability
rescaleNormal :: StateVar Capability
rescaleNormal = EnableCap -> StateVar Capability
makeCapability EnableCap
CapRescaleNormal

-- | If 'normalize' contains 'Enabled', normal vectors specified with
-- 'Graphics.Rendering.OpenGL.GL.VertexSpec.normal' are scaled to unit length
-- after transformation. The initial value of 'normalize' is 'Disabled'.

normalize :: StateVar Capability
normalize :: StateVar Capability
normalize = EnableCap -> StateVar Capability
makeCapability EnableCap
CapNormalize

--------------------------------------------------------------------------------

data Plane a = Plane !a !a !a !a
   deriving ( Plane a -> Plane a -> Bool
forall a. Eq a => Plane a -> Plane a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Plane a -> Plane a -> Bool
$c/= :: forall a. Eq a => Plane a -> Plane a -> Bool
== :: Plane a -> Plane a -> Bool
$c== :: forall a. Eq a => Plane a -> Plane a -> Bool
Eq, Plane a -> Plane a -> Bool
Plane a -> Plane a -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (Plane a)
forall a. Ord a => Plane a -> Plane a -> Bool
forall a. Ord a => Plane a -> Plane a -> Ordering
forall a. Ord a => Plane a -> Plane a -> Plane a
min :: Plane a -> Plane a -> Plane a
$cmin :: forall a. Ord a => Plane a -> Plane a -> Plane a
max :: Plane a -> Plane a -> Plane a
$cmax :: forall a. Ord a => Plane a -> Plane a -> Plane a
>= :: Plane a -> Plane a -> Bool
$c>= :: forall a. Ord a => Plane a -> Plane a -> Bool
> :: Plane a -> Plane a -> Bool
$c> :: forall a. Ord a => Plane a -> Plane a -> Bool
<= :: Plane a -> Plane a -> Bool
$c<= :: forall a. Ord a => Plane a -> Plane a -> Bool
< :: Plane a -> Plane a -> Bool
$c< :: forall a. Ord a => Plane a -> Plane a -> Bool
compare :: Plane a -> Plane a -> Ordering
$ccompare :: forall a. Ord a => Plane a -> Plane a -> Ordering
Ord, Int -> Plane a -> ShowS
forall a. Show a => Int -> Plane a -> ShowS
forall a. Show a => [Plane a] -> ShowS
forall a. Show a => Plane a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Plane a] -> ShowS
$cshowList :: forall a. Show a => [Plane a] -> ShowS
show :: Plane a -> String
$cshow :: forall a. Show a => Plane a -> String
showsPrec :: Int -> Plane a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Plane a -> ShowS
Show )

instance Storable a => Storable (Plane a) where
   sizeOf :: Plane a -> Int
sizeOf    ~(Plane a
a a
_ a
_ a
_) = Int
4 forall a. Num a => a -> a -> a
* forall a. Storable a => a -> Int
sizeOf a
a
   alignment :: Plane a -> Int
alignment ~(Plane a
a a
_ a
_ a
_) = forall a. Storable a => a -> Int
alignment a
a
   peek :: Ptr (Plane a) -> IO (Plane a)
peek                       = forall a b. Storable a => (a -> a -> a -> a -> b) -> Ptr a -> IO b
peek4 forall a. a -> a -> a -> a -> Plane a
Plane forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Ptr a -> Ptr b
castPtr
   poke :: Ptr (Plane a) -> Plane a -> IO ()
poke Ptr (Plane a)
ptr   (Plane a
a a
b a
c a
d) = forall a. Storable a => Ptr a -> a -> a -> a -> a -> IO ()
poke4 (forall a b. Ptr a -> Ptr b
castPtr Ptr (Plane a)
ptr) a
a a
b a
c a
d

--------------------------------------------------------------------------------

data TextureCoordName =
     S
   | T
   | R
   | Q
   deriving ( TextureCoordName -> TextureCoordName -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TextureCoordName -> TextureCoordName -> Bool
$c/= :: TextureCoordName -> TextureCoordName -> Bool
== :: TextureCoordName -> TextureCoordName -> Bool
$c== :: TextureCoordName -> TextureCoordName -> Bool
Eq, Eq TextureCoordName
TextureCoordName -> TextureCoordName -> Bool
TextureCoordName -> TextureCoordName -> Ordering
TextureCoordName -> TextureCoordName -> TextureCoordName
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TextureCoordName -> TextureCoordName -> TextureCoordName
$cmin :: TextureCoordName -> TextureCoordName -> TextureCoordName
max :: TextureCoordName -> TextureCoordName -> TextureCoordName
$cmax :: TextureCoordName -> TextureCoordName -> TextureCoordName
>= :: TextureCoordName -> TextureCoordName -> Bool
$c>= :: TextureCoordName -> TextureCoordName -> Bool
> :: TextureCoordName -> TextureCoordName -> Bool
$c> :: TextureCoordName -> TextureCoordName -> Bool
<= :: TextureCoordName -> TextureCoordName -> Bool
$c<= :: TextureCoordName -> TextureCoordName -> Bool
< :: TextureCoordName -> TextureCoordName -> Bool
$c< :: TextureCoordName -> TextureCoordName -> Bool
compare :: TextureCoordName -> TextureCoordName -> Ordering
$ccompare :: TextureCoordName -> TextureCoordName -> Ordering
Ord, Int -> TextureCoordName -> ShowS
[TextureCoordName] -> ShowS
TextureCoordName -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TextureCoordName] -> ShowS
$cshowList :: [TextureCoordName] -> ShowS
show :: TextureCoordName -> String
$cshow :: TextureCoordName -> String
showsPrec :: Int -> TextureCoordName -> ShowS
$cshowsPrec :: Int -> TextureCoordName -> ShowS
Show )

marshalTextureCoordName :: TextureCoordName -> GLenum
marshalTextureCoordName :: TextureCoordName -> GLenum
marshalTextureCoordName TextureCoordName
x = case TextureCoordName
x of
   TextureCoordName
S -> GLenum
GL_S
   TextureCoordName
T -> GLenum
GL_T
   TextureCoordName
R -> GLenum
GL_R
   TextureCoordName
Q -> GLenum
GL_Q

--------------------------------------------------------------------------------

data TextureGenParameter =
     TextureGenMode
   | ObjectPlane
   | EyePlane

marshalTextureGenParameter :: TextureGenParameter -> GLenum
marshalTextureGenParameter :: TextureGenParameter -> GLenum
marshalTextureGenParameter TextureGenParameter
x = case TextureGenParameter
x of
   TextureGenParameter
TextureGenMode -> GLenum
GL_TEXTURE_GEN_MODE
   TextureGenParameter
ObjectPlane -> GLenum
GL_OBJECT_PLANE
   TextureGenParameter
EyePlane -> GLenum
GL_EYE_PLANE

--------------------------------------------------------------------------------

data TextureGenMode' =
     EyeLinear'
   | ObjectLinear'
   | SphereMap'
   | NormalMap'
   | ReflectionMap'

marshalTextureGenMode' :: TextureGenMode' -> GLint
marshalTextureGenMode' :: TextureGenMode' -> GLint
marshalTextureGenMode' TextureGenMode'
x = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ case TextureGenMode'
x of
   TextureGenMode'
EyeLinear' -> GLenum
GL_EYE_LINEAR
   TextureGenMode'
ObjectLinear' -> GLenum
GL_OBJECT_LINEAR
   TextureGenMode'
SphereMap' -> GLenum
GL_SPHERE_MAP
   TextureGenMode'
NormalMap' -> GLenum
GL_NORMAL_MAP
   TextureGenMode'
ReflectionMap' -> GLenum
GL_REFLECTION_MAP

unmarshalTextureGenMode' :: GLint -> TextureGenMode'
unmarshalTextureGenMode' :: GLint -> TextureGenMode'
unmarshalTextureGenMode' GLint
x
   | GLenum
y forall a. Eq a => a -> a -> Bool
== GLenum
GL_EYE_LINEAR = TextureGenMode'
EyeLinear'
   | GLenum
y forall a. Eq a => a -> a -> Bool
== GLenum
GL_OBJECT_LINEAR = TextureGenMode'
ObjectLinear'
   | GLenum
y forall a. Eq a => a -> a -> Bool
== GLenum
GL_SPHERE_MAP = TextureGenMode'
SphereMap'
   | GLenum
y forall a. Eq a => a -> a -> Bool
== GLenum
GL_NORMAL_MAP = TextureGenMode'
NormalMap'
   | GLenum
y forall a. Eq a => a -> a -> Bool
== GLenum
GL_REFLECTION_MAP = TextureGenMode'
ReflectionMap'
   | Bool
otherwise = forall a. HasCallStack => String -> a
error (String
"unmarshalTextureGenMode': illegal value " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show GLint
x)
   where y :: GLenum
y = forall a b. (Integral a, Num b) => a -> b
fromIntegral GLint
x

--------------------------------------------------------------------------------

data TextureGenMode =
     EyeLinear    (Plane GLdouble)
   | ObjectLinear (Plane GLdouble)
   | SphereMap
   | NormalMap
   | ReflectionMap
   deriving ( TextureGenMode -> TextureGenMode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TextureGenMode -> TextureGenMode -> Bool
$c/= :: TextureGenMode -> TextureGenMode -> Bool
== :: TextureGenMode -> TextureGenMode -> Bool
$c== :: TextureGenMode -> TextureGenMode -> Bool
Eq, Eq TextureGenMode
TextureGenMode -> TextureGenMode -> Bool
TextureGenMode -> TextureGenMode -> Ordering
TextureGenMode -> TextureGenMode -> TextureGenMode
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TextureGenMode -> TextureGenMode -> TextureGenMode
$cmin :: TextureGenMode -> TextureGenMode -> TextureGenMode
max :: TextureGenMode -> TextureGenMode -> TextureGenMode
$cmax :: TextureGenMode -> TextureGenMode -> TextureGenMode
>= :: TextureGenMode -> TextureGenMode -> Bool
$c>= :: TextureGenMode -> TextureGenMode -> Bool
> :: TextureGenMode -> TextureGenMode -> Bool
$c> :: TextureGenMode -> TextureGenMode -> Bool
<= :: TextureGenMode -> TextureGenMode -> Bool
$c<= :: TextureGenMode -> TextureGenMode -> Bool
< :: TextureGenMode -> TextureGenMode -> Bool
$c< :: TextureGenMode -> TextureGenMode -> Bool
compare :: TextureGenMode -> TextureGenMode -> Ordering
$ccompare :: TextureGenMode -> TextureGenMode -> Ordering
Ord, Int -> TextureGenMode -> ShowS
[TextureGenMode] -> ShowS
TextureGenMode -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TextureGenMode] -> ShowS
$cshowList :: [TextureGenMode] -> ShowS
show :: TextureGenMode -> String
$cshow :: TextureGenMode -> String
showsPrec :: Int -> TextureGenMode -> ShowS
$cshowsPrec :: Int -> TextureGenMode -> ShowS
Show )

marshalTextureGenMode :: TextureGenMode -> GLint
marshalTextureGenMode :: TextureGenMode -> GLint
marshalTextureGenMode = TextureGenMode' -> GLint
marshalTextureGenMode' forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextureGenMode -> TextureGenMode'
convertMode
   where convertMode :: TextureGenMode -> TextureGenMode'
convertMode (EyeLinear    Plane GLdouble
_) = TextureGenMode'
EyeLinear'
         convertMode (ObjectLinear Plane GLdouble
_) = TextureGenMode'
ObjectLinear'
         convertMode TextureGenMode
SphereMap        = TextureGenMode'
SphereMap'
         convertMode TextureGenMode
NormalMap        = TextureGenMode'
NormalMap'
         convertMode TextureGenMode
ReflectionMap    = TextureGenMode'
ReflectionMap'

--------------------------------------------------------------------------------

textureGenMode :: TextureCoordName -> StateVar (Maybe TextureGenMode)
textureGenMode :: TextureCoordName -> StateVar (Maybe TextureGenMode)
textureGenMode TextureCoordName
coord =
   forall a.
IO EnableCap -> IO a -> (a -> IO ()) -> StateVar (Maybe a)
makeStateVarMaybe
      (forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ TextureCoordName -> EnableCap
textureCoordNameToEnableCap TextureCoordName
coord)
      (do TextureGenMode'
mode <- TextureCoordName -> IO TextureGenMode'
getMode TextureCoordName
coord
          case TextureGenMode'
mode of
             TextureGenMode'
EyeLinear'     -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Plane GLdouble -> TextureGenMode
EyeLinear forall a b. (a -> b) -> a -> b
$ TextureCoordName -> TextureGenParameter -> IO (Plane GLdouble)
getPlane TextureCoordName
coord TextureGenParameter
EyePlane
             TextureGenMode'
ObjectLinear'  -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Plane GLdouble -> TextureGenMode
ObjectLinear forall a b. (a -> b) -> a -> b
$ TextureCoordName -> TextureGenParameter -> IO (Plane GLdouble)
getPlane TextureCoordName
coord TextureGenParameter
ObjectPlane
             TextureGenMode'
SphereMap'     -> forall (m :: * -> *) a. Monad m => a -> m a
return TextureGenMode
SphereMap
             TextureGenMode'
NormalMap'     -> forall (m :: * -> *) a. Monad m => a -> m a
return TextureGenMode
NormalMap
             TextureGenMode'
ReflectionMap' -> forall (m :: * -> *) a. Monad m => a -> m a
return TextureGenMode
ReflectionMap)
      (\TextureGenMode
mode -> do
         TextureCoordName -> TextureGenMode -> IO ()
setMode TextureCoordName
coord TextureGenMode
mode
         case TextureGenMode
mode of
            EyeLinear    Plane GLdouble
plane -> TextureCoordName -> TextureGenParameter -> Plane GLdouble -> IO ()
setPlane TextureCoordName
coord TextureGenParameter
EyePlane    Plane GLdouble
plane
            ObjectLinear Plane GLdouble
plane -> TextureCoordName -> TextureGenParameter -> Plane GLdouble -> IO ()
setPlane TextureCoordName
coord TextureGenParameter
ObjectPlane Plane GLdouble
plane
            TextureGenMode
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ())

--------------------------------------------------------------------------------

textureCoordNameToEnableCap :: TextureCoordName -> EnableCap
textureCoordNameToEnableCap :: TextureCoordName -> EnableCap
textureCoordNameToEnableCap TextureCoordName
coord = case TextureCoordName
coord of
   TextureCoordName
S -> EnableCap
CapTextureGenS
   TextureCoordName
T -> EnableCap
CapTextureGenT
   TextureCoordName
R -> EnableCap
CapTextureGenR
   TextureCoordName
Q -> EnableCap
CapTextureGenQ

--------------------------------------------------------------------------------

getMode :: TextureCoordName -> IO TextureGenMode'
getMode :: TextureCoordName -> IO TextureGenMode'
getMode TextureCoordName
coord = forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr GLint
buf -> do
   forall (m :: * -> *).
MonadIO m =>
GLenum -> GLenum -> Ptr GLint -> m ()
glGetTexGeniv (TextureCoordName -> GLenum
marshalTextureCoordName TextureCoordName
coord)
                 (TextureGenParameter -> GLenum
marshalTextureGenParameter TextureGenParameter
TextureGenMode)
                 Ptr GLint
buf
   forall a b. Storable a => (a -> b) -> Ptr a -> IO b
peek1 GLint -> TextureGenMode'
unmarshalTextureGenMode' Ptr GLint
buf

setMode :: TextureCoordName -> TextureGenMode -> IO ()
setMode :: TextureCoordName -> TextureGenMode -> IO ()
setMode TextureCoordName
coord TextureGenMode
mode =
   forall (m :: * -> *).
MonadIO m =>
GLenum -> GLenum -> GLint -> m ()
glTexGeni (TextureCoordName -> GLenum
marshalTextureCoordName TextureCoordName
coord)
             (TextureGenParameter -> GLenum
marshalTextureGenParameter TextureGenParameter
TextureGenMode)
             (TextureGenMode -> GLint
marshalTextureGenMode TextureGenMode
mode)

--------------------------------------------------------------------------------

getPlane :: TextureCoordName -> TextureGenParameter -> IO (Plane GLdouble)
getPlane :: TextureCoordName -> TextureGenParameter -> IO (Plane GLdouble)
getPlane TextureCoordName
coord TextureGenParameter
param = forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr (Plane GLdouble)
planeBuffer -> do
   forall (m :: * -> *).
MonadIO m =>
GLenum -> GLenum -> Ptr GLdouble -> m ()
glGetTexGendv (TextureCoordName -> GLenum
marshalTextureCoordName TextureCoordName
coord)
                 (TextureGenParameter -> GLenum
marshalTextureGenParameter TextureGenParameter
param)
                 (forall a b. Ptr a -> Ptr b
castPtr Ptr (Plane GLdouble)
planeBuffer)
   forall a. Storable a => Ptr a -> IO a
peek Ptr (Plane GLdouble)
planeBuffer

setPlane :: TextureCoordName -> TextureGenParameter -> Plane GLdouble -> IO ()
setPlane :: TextureCoordName -> TextureGenParameter -> Plane GLdouble -> IO ()
setPlane TextureCoordName
coord TextureGenParameter
param Plane GLdouble
plane =
   forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Plane GLdouble
plane forall a b. (a -> b) -> a -> b
$ \Ptr (Plane GLdouble)
planeBuffer ->
      forall (m :: * -> *).
MonadIO m =>
GLenum -> GLenum -> Ptr GLdouble -> m ()
glTexGendv (TextureCoordName -> GLenum
marshalTextureCoordName TextureCoordName
coord)
                 (TextureGenParameter -> GLenum
marshalTextureGenParameter TextureGenParameter
param)
                 (forall a b. Ptr a -> Ptr b
castPtr Ptr (Plane GLdouble)
planeBuffer)