{-
   Checker.hs (adapted from checker.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 texture maps a checkerboard image onto two rectangles.
-}

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

import GL
import GLU
import Graphics.UI.GLUT

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

myInit :: IO TextureName
myInit = do
   clearColor (Color4 0.0 0.0 0.0 0.0)
   shadeModel GL.Flat
   enable DepthTest
   pixelStore Unpack (Alignment 1)
   texName <- liftM head (genTextures 1)
   bindTexture Texture2d texName
   mapM_ (texParameter Texture2d) [
      TextureWrap S Repeat,
      TextureWrap T Repeat,
      TextureFilters Nearest Nearest ]
   withArray [ Color4 c c c 255 |
               i <- [ 0 .. checkImageHeight - 1 ],
               j <- [ 0 .. checkImageWidth  - 1 ],
               let c | (i .&. 0x8) == (j .&. 0x8) = 0 :: GLubyte
                     | otherwise                  = 255 ] $
      texImage2D Texture2d 0 GL.Rgba' checkImageWidth checkImageHeight 0 .
      PixelDescriptor GL.Rgba UnsignedByte . castPtr
   return texName

display :: TextureName -> DisplayCallback
display texName = do
   clear [ ColorBufferBit, DepthBufferBit ]
   enable Texture2d'
   texEnv (TextureEnvMode Decal)
   bindTexture Texture2d texName

   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)
   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
		      
      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
   disable Texture2d'

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
   texName <- myInit
   setDisplayCallback (display texName)
   setReshapeCallback (Just reshape)
   setKeyboardMouseCallback (Just keyboard)
   mainLoop
