{-
   AARGB.hs  (adapted from aargb.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 draws shows how to draw anti-aliased lines. It draws
   two diagonal lines to form an X; when 'r' is typed in the window, 
   the lines are rotated in opposite directions.
-}

import Control.Monad ( liftM )
import Data.Char     ( toLower )
import Data.IORef    ( IORef, newIORef, readIORef, writeIORef, modifyIORef )
import System.Exit   ( exitWith, ExitCode(ExitSuccess) )

import GL
import GLU
import Graphics.UI.GLUT

-- Initialize antialiasing for RGBA mode, including alpha
-- blending, hint, and line width.  Print out implementation
-- specific info on line width granularity and width.
myInit :: IO ()
myInit = do
   gran <- get VarLineWidthGranularity
   putStrLn ("GL_LINE_WIDTH_GRANULARITY value is " ++ show (gran :: GLfloat))

   (lo,hi) <- get VarLineWidthRange
   putStrLn ("GL_LINE_WIDTH_RANGE values are " ++
             show (lo :: GLfloat) ++ " " ++ show (hi :: GLfloat))

   enable LineSmooth
   enable Blend'
   blendFunc SrcAlpha OneMinusSrcAlpha
   hint LineSmoothHint DontCare
   lineWidth 1.5

   clearColor (Color4 0.0 0.0 0.0 0.0)

-- Draw 2 diagonal lines to form an X
display :: IORef Int -> DisplayCallback
display rotAngle = do
   angle <- liftM (fromIntegral :: Int -> GLfloat) (readIORef rotAngle)
   clear [ ColorBufferBit ]

   color (Color3 0 1 (0 :: GLfloat))
   pushMatrix
   rotate (-angle) (Vector3 0 0 1)
   beginEnd Lines $ do
      vertex (Vertex2 (-0.5) ( 0.5 :: GLfloat))
      vertex (Vertex2   0.5  (-0.5 :: GLfloat))
   popMatrix

   color (Color3 0 0 (1 :: GLfloat))
   pushMatrix
   rotate angle (Vector3 0 0 1)
   beginEnd Lines $ do
      vertex (Vertex2   0.5  ( 0.5 :: GLfloat))
      vertex (Vertex2 (-0.5) (-0.5 :: GLfloat))
   popMatrix

   flush

reshape :: ReshapeCallback
reshape (WindowSize w h) = do
   viewport (0, 0) (fromIntegral w, fromIntegral h)
   matrixMode Projection
   loadIdentity
   let wf = fromIntegral w
       hf = fromIntegral h
   if (w <= h)
      then ortho2D (-1.0) 1.0 (-1.0*hf/wf) (1.0*hf/wf)
      else ortho2D (-1.0*wf/hf) (1.0*wf/hf) (-1.0) 1.0
   matrixMode Modelview
   loadIdentity

keyboard :: IORef Int -> KeyboardMouseCallback
keyboard rotAngle (Char c) Down _ _ = case toLower c of
   'r'   -> do modifyIORef rotAngle (\a -> (a + 20) `mod` 360); postRedisplay
   '\27' -> exitWith ExitSuccess
   _     -> return ()
keyboard _ _ _ _ _ = return ()

--  Main Loop
--  Open window with initial window size, title bar, 
--  RGBA display mode, and handle input events.
main :: IO ()
main = do
   (progName, _args) <- getArgsAndInitialize
   setInitialDisplayMode [ Single, RGB ]
   setInitialWindowSize (WindowSize 200 200)
   createWindow progName
   myInit
   setReshapeCallback (Just reshape)
   rotAngle <- newIORef 0
   setKeyboardMouseCallback (Just (keyboard rotAngle))
   setDisplayCallback (display rotAngle) 
   mainLoop
