{- 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 3 (Mipmapping) of the GLU specs.
-}

module GLU_Mipmap (
   scaleImage, build1DMipmaps, build2DMipmaps
) where

import Foreign.Ptr    ( Ptr )
import GL_BasicTypes  ( GLsizei, GLint, GLenum )
import GL_VertexArray ( marshall_Type )
import GL_PixelRect   ( PixelDescriptor(..), marshall_PixelFormat )
import GL_Texturing   ( TextureTarget, marshall_TextureTarget,
                        InternalFormat, marshall_InternalFormat )

---------------------------------------------------------------------------
-- Section 3.1 (Image Scaling)

scaleImage :: GLsizei -> GLsizei -> PixelDescriptor
           -> GLsizei -> GLsizei -> PixelDescriptor -> IO ()
scaleImage widthIn  heightIn  (PixelDescriptor formatIn  typeIn  addrIn )
           widthOut heightOut (PixelDescriptor formatOut typeOut addrOut)
   | formatIn == formatOut = scaleImageAux (marshall_PixelFormat formatIn) widthIn  heightIn  (marshall_Type typeIn ) addrIn
                                                                           widthOut heightOut (marshall_Type typeOut) addrOut
   | otherwise = error "scaleImage: pixel formats differ"

foreign import CALLCONV unsafe "gluScaleImage" scaleImageAux ::
   GLenum -> GLsizei -> GLsizei -> GLenum -> Ptr ()
          -> GLsizei -> GLsizei -> GLenum -> Ptr () -> IO ()

---------------------------------------------------------------------------
-- Section 3.2 (Automatic Mipmapping)
-- Missing for GLU 1.3: gluBuild3DMipmaps, gluBuild{1,2,3}DMipmapLevels

build1DMipmaps :: TextureTarget -> InternalFormat -> GLsizei
               -> PixelDescriptor -> IO ()
build1DMipmaps target internalFormat height (PixelDescriptor f t a) = do
   build1DMipmapsAux (marshall_TextureTarget target) (fromIntegral (marshall_InternalFormat internalFormat)) height (marshall_PixelFormat f) (marshall_Type t) a
   return ()   -- TODO: Should we use the return value?

foreign import CALLCONV unsafe "gluBuild1DMipmaps" build1DMipmapsAux ::
      GLenum -> GLint -> GLsizei -> GLenum -> GLenum -> Ptr () -> IO GLint

build2DMipmaps :: TextureTarget -> InternalFormat -> GLsizei -> GLsizei
               -> PixelDescriptor -> IO ()
build2DMipmaps target internalFormat width height (PixelDescriptor f t a) = do
   build2DMipmapsAux (marshall_TextureTarget target) (fromIntegral (marshall_InternalFormat internalFormat)) width height (marshall_PixelFormat f) (marshall_Type t) a
   return ()   -- TODO: Should we use the return value?

foreign import CALLCONV unsafe "gluBuild2DMipmaps" build2DMipmapsAux ::
      GLenum -> GLint -> GLsizei -> GLsizei -> GLenum -> GLenum -> Ptr () -> IO GLint
