File: AAIndex.hs

package info (click to toggle)
haskell-glut 2.1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,936 kB
  • ctags: 25
  • sloc: haskell: 10,092; sh: 2,811; ansic: 53; makefile: 2
file content (104 lines) | stat: -rw-r--r-- 3,227 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{-
   AAIndex.hs (adapted from aaindex.c which is (c) Silicon Graphics, Inc.)
   Copyright (c) Sven Panne 2002-2006 <sven.panne@aedion.de>
   This file is part of HOpenGL and distributed under a BSD-style license
   See the file libraries/GLUT/LICENSE

   This program draws shows how to draw anti-aliased lines in color index
   mode. It draws two diagonal lines to form an X; when 'r' is typed in the
   window, the lines are rotated in opposite directions.
-}

import Data.Char ( toLower )
import Data.IORef ( IORef, newIORef )
import System.Exit ( exitWith, ExitCode(ExitSuccess) )
import Graphics.UI.GLUT

data State = State { rotAngle :: IORef Int }

makeState :: IO State
makeState = do
   r <- newIORef 0
   return $ State { rotAngle = r }

rampSize, ramp1Start, ramp2Start :: GLint
rampSize = 16
ramp1Start = 32
ramp2Start = 48

-- Initialize antialiasing for color index mode, including loading a green color
-- ramp starting at ramp1Start, and a blue color ramp starting at ramp2Start.
-- The ramps must be a multiple of 16.
myInit :: IO ()
myInit = do
   flip mapM_ [ 0 .. rampSize - 1 ] $ \i -> do
      let shade = fromIntegral i / fromIntegral rampSize
      colorMapEntry (Index1 (ramp1Start + i)) $= Color3 0 shade 0
      colorMapEntry (Index1 (ramp2Start + i)) $= Color3 0 0 shade

   lineSmooth $= Enabled
   hint LineSmooth $= DontCare
   lineWidth $= 1.5

   clearIndex $= Index1 (fromIntegral ramp1Start)

-- Draw 2 diagonal lines to form an X
display :: State -> DisplayCallback
display state = do
   r <- get (rotAngle state)
   clear [ ColorBuffer ]

   -- resolve overloading, not needed in "real" programs
   let vertex2f = vertex :: Vertex2 GLfloat -> IO ()

   index (Index1 ramp1Start)
   preservingMatrix $ do
      rotate (-(fromIntegral r :: GLfloat)) (Vector3 0 0 0.1)
      renderPrimitive Lines $ do
         vertex2f (Vertex2 (-0.5) 0.5)
         vertex2f (Vertex2 0.5 (-0.5))

   index (Index1 ramp2Start)
   preservingMatrix $ do
      rotate (fromIntegral r :: GLfloat) (Vector3 0 0 0.1)
      renderPrimitive Lines $ do
         vertex2f (Vertex2 0.5 0.5)
         vertex2f (Vertex2 (-0.5) (-0.5))

   flush

reshape :: ReshapeCallback
reshape size@(Size w h) = do
   viewport $= (Position 0 0, size)
   matrixMode $= Projection
   loadIdentity
   let wf = fromIntegral w
       hf = fromIntegral h
   if w <= h
      then ortho2D (-1) 1 (-1*hf/wf) (1*hf/wf)
      else ortho2D (-1*wf/hf) (1*wf/hf) (-1) 1
   matrixMode $= Modelview 0
   loadIdentity

keyboard :: State -> KeyboardMouseCallback
keyboard state (Char c) Down _ _ = case toLower c of
   'r'   -> do rotAngle state  $~ ((`mod` 360) . (+ 30)); postRedisplay Nothing
   '\27' -> exitWith ExitSuccess
   _     -> return ()
keyboard _ _ _ _ _ = return ()

-- Main Loop
-- Open window with initial window size, title bar,
-- color index display mode, and handle input events.
main :: IO ()
main = do
   (progName, _args) <- getArgsAndInitialize
   initialDisplayMode $= [ SingleBuffered, IndexMode ]
   initialWindowSize $= Size 200 200
   createWindow progName
   state <- makeState
   myInit
   reshapeCallback $= Just reshape
   keyboardMouseCallback $= Just (keyboard state)
   displayCallback $= display state
   mainLoop