{-
   Smooth.hs (adapted from smooth.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 smooth shading.
   A smooth shaded polygon is drawn in a 2-D projection.
-}

import System.Exit ( exitWith, ExitCode(ExitSuccess) )

import GL
import GLU
import Graphics.UI.GLUT

myInit :: IO ()
myInit = do
   clearColor (Color4 0.0 0.0 0.0 0.0)
   shadeModel GL.Smooth

triangle :: IO ()
triangle =
   beginEnd Triangles $ do
      color (Color3 1.0 0.0 (0.0 :: GLfloat))
      vertex (Vertex2 5.0 (5.0 :: GLfloat))
      color (Color3 0.0 1.0 (0.0 :: GLfloat))
      vertex (Vertex2 25.0 (5.0 :: GLfloat))
      color (Color3 0.0 0.0 (1.0 :: GLfloat))
      vertex (Vertex2 5.0 (25.0 :: GLfloat))

display :: DisplayCallback
display = do
   clear [ ColorBufferBit ]
   triangle
   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 0.0 30.0 0.0 (30.0 * hf/wf)
      else ortho2D 0.0 (30.0 * wf/hf) 0.0 30.0
   matrixMode Modelview

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

main :: IO ()
main = do
   (progName, _args) <- getArgsAndInitialize
   setInitialDisplayMode  [ Single, RGB ]
   setInitialWindowSize (WindowSize 500 500)
   setInitialWindowPosition (WindowPosition 100 100)
   createWindow progName
   myInit
   setDisplayCallback display
   setReshapeCallback (Just reshape)
   setKeyboardMouseCallback (Just keyboard)
   mainLoop
