{-
   Wrap.hs (adapted from wrap.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.  This program demonstrates the wrapping
   modes, if the texture coordinates fall outside 0.0 and 1.0.
   Interaction: Pressing the 's' and 'S' keys switch the
   wrapping between clamping and repeating for the s parameter.
   The 't' and 'T' keys control the wrapping for the t parameter.
-}

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

makeCheckImage :: (PixelDescriptor -> IO ()) -> IO ()
makeCheckImage act =
   withArray [ Color4 c c c 255 |
               i <- [ 0 .. checkImageHeight - 1 ],
               j <- [ 0 .. checkImageWidth  - 1 ],
               let c | (i .&. 0x8) == (j .&. 0x8) = 0 :: GLubyte
                     | otherwise                  = 255 ] $
   act . PixelDescriptor GL.Rgba UnsignedByte . castPtr


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 ]
   makeCheckImage $ texImage2D Texture2d 0 GL.Rgba'
                               checkImageWidth checkImageHeight 0
   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 3.0;  vertex3 (-2.0)      1.0    0.0
      texCoord2 3.0 3.0;  vertex3   0.0       1.0    0.0
      texCoord2 3.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 3.0;  vertex3   1.0       1.0    0.0
      texCoord2 3.0 3.0;  vertex3   2.41421   1.0  (-1.41421)
      texCoord2 3.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 's')   Down _   _ = do texParameter Texture2d (TextureWrap S Clamp ); postRedisplay
keyboard (Char 'S')   Down _   _ = do texParameter Texture2d (TextureWrap S Repeat); postRedisplay
keyboard (Char 't')   Down _   _ = do texParameter Texture2d (TextureWrap T Clamp ); postRedisplay
keyboard (Char 'T')   Down _   _ = do texParameter Texture2d (TextureWrap T Repeat); postRedisplay
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
