{-
   TexBind.hs (adapted from texbind.c which is (c) Silicon Graphics, Inc)
   This file is part of HOpenGL - a binding of OpenGL and GLUT for Haskell.
   Copyright (C) 2000  Sven Panne <Sven.Panne@informatik.uni-muenchen.de>

   This program demonstrates using bindTexture by creating and managing
   two textures.
-}

import Data.Bits   ( (.&.) )
import System.Exit ( exitWith, ExitCode(ExitSuccess) )

import GL
import GLU
import Graphics.UI.GLUT

checkImageWidth, checkImageHeight :: GLsizei
checkImageWidth  = 64
checkImageHeight = 64

makeCheckImage :: Color4 GLubyte -> GLsizei -> (PixelDescriptor -> IO ()) -> IO ()
makeCheckImage col mask act =
   withArray [ if (i .&. mask) == (j .&. mask) then Color4 0 0 0 255 else col
             | i <- [ 0 .. checkImageHeight - 1 ],
               j <- [ 0 .. checkImageWidth  - 1 ] ] $
   act . PixelDescriptor GL.Rgba UnsignedByte . castPtr
               

myInit :: IO (TextureName, TextureName)
myInit = do
   clearColor (Color4 0.0 0.0 0.0 0.0)
   shadeModel GL.Flat
   enable DepthTest

   pixelStore Unpack (Alignment 1)

   [texName1, texName2] <- genTextures 2

   bindTexture Texture2d texName1
   mapM_ (texParameter Texture2d) [
      TextureWrap S Clamp,
      TextureWrap T Clamp,
      TextureFilters Nearest Nearest ]
   makeCheckImage (Color4 255 255 255 255) 0x08 $
      texImage2D Texture2d 0 GL.Rgba' checkImageWidth checkImageHeight 0

   bindTexture Texture2d texName2
   mapM_ (texParameter Texture2d) [
      TextureWrap S Clamp,
      TextureWrap T Clamp,
      TextureFilters Nearest Nearest ]
   texEnv (TextureEnvMode Decal)
   makeCheckImage (Color4 255 0 0 255) 0x10 $
      texImage2D Texture2d 0 GL.Rgba' checkImageWidth checkImageHeight 0

   enable Texture2d'
   return (texName1, texName2)

display :: TextureName -> TextureName -> DisplayCallback
display texName1 texName2 = do
   clear [ ColorBufferBit, DepthBufferBit ]
   let texCoord2 :: GLfloat -> GLfloat -> IO ()
       texCoord2 x y = texCoord (TexCoord2 x y)
       vertex3 :: GLfloat -> GLfloat -> GLfloat -> IO ()
       vertex3 x y z = vertex (Vertex3 x y z)
   bindTexture Texture2d texName1
   beginEnd Quads $ do
      texCoord2 0.0 0.0;  vertex3 (-2.0)    (-1.0)   0.0
      texCoord2 0.0 1.0;  vertex3 (-2.0)      1.0    0.0
      texCoord2 1.0 1.0;  vertex3   0.0       1.0    0.0
      texCoord2 1.0 0.0;  vertex3   0.0     (-1.0)   0.0
   bindTexture Texture2d texName2
   beginEnd Quads $ do
      texCoord2 0.0 0.0;  vertex3   1.0     (-1.0)   0.0
      texCoord2 0.0 1.0;  vertex3   1.0       1.0    0.0
      texCoord2 1.0 1.0;  vertex3   2.41421   1.0  (-1.41421)
      texCoord2 1.0 0.0;  vertex3   2.41421 (-1.0) (-1.41421)
   flush

reshape :: ReshapeCallback
reshape (WindowSize w h) = do
   viewport (0, 0) (fromIntegral w, fromIntegral h)
   matrixMode Projection
   loadIdentity
   perspective 60.0 (fromIntegral w / fromIntegral h) 1.0 30.0
   matrixMode Modelview
   loadIdentity
   translate (Vector3 0 0 (-3.6 :: GLfloat))

keyboard :: KeyboardMouseCallback
keyboard (Char '\27') Down _ _ = exitWith ExitSuccess
keyboard _            _    _ _ = return ()

main :: IO ()
main = do
   (progName, _args) <- getArgsAndInitialize
   setInitialDisplayMode [ Single, RGB, Graphics.UI.GLUT.Depth ]
   setInitialWindowSize (WindowSize 250 250)
   setInitialWindowPosition (WindowPosition 100 100)
   createWindow progName
   (texName1, texName2) <- myInit
   setReshapeCallback (Just reshape)
   setDisplayCallback (display texName1 texName2)
   setKeyboardMouseCallback (Just keyboard)
   mainLoop
