File: DList.hs

package info (click to toggle)
haskell-glut 2.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,624 kB
  • ctags: 28
  • sloc: haskell: 10,610; ansic: 121; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 2,855 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
{-
   DList.hs (adapted from list.c which is (c) Silicon Graphics, Inc)
   Copyright (c) Sven Panne 2002-2005 <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 demonstrates how to make and execute a
   display list.  Note that attributes, such as current
   color and matrix, are changed.
-}

import System.Exit ( exitWith, ExitCode(ExitSuccess) )
import Graphics.UI.GLUT

myInit :: IO DisplayList
myInit = do
   [listName] <- genObjectNames 1
   -- resolve overloading, not needed in "real" programs
   let color3f = color :: Color3 GLfloat -> IO ()
       vertex2f = vertex :: Vertex2 GLfloat -> IO ()
       translatef = translate :: Vector3 GLfloat -> IO ()
   defineList listName Compile $ do
      color3f (Color3 1 0 0) -- current color red
      renderPrimitive Triangles $ do
         vertex2f (Vertex2 0 0)
         vertex2f (Vertex2 1 0)
         vertex2f (Vertex2 0 1)
      translatef (Vector3 1.5 0.0 0.0) -- move position
   shadeModel $= Flat
   return listName

drawLine :: IO ()
drawLine = do
   -- resolve overloading, not needed in "real" programs
   let vertex2f = vertex :: Vertex2 GLfloat -> IO ()
   renderPrimitive Lines $ do
      vertex2f (Vertex2  0.0 0.5)
      vertex2f (Vertex2 15.0 0.5)

display :: DisplayList -> DisplayCallback
display listName = do
   -- NOTE: The following 'loadIdentity' is missing in the original
   -- example, but without it the translatef calls accumulate and
   -- the graphics wander out of the window after a few redraws...
   loadIdentity

   clear [ ColorBuffer ]
   -- resolve overloading, not needed in "real" programs
   let color3f = color :: Color3 GLfloat -> IO ()
   color3f (Color3 0 1 0) -- current color green
   sequence_ (replicate 10 (callList listName)) -- draw 10 triangles
   drawLine -- is this line green? NO!
            -- where is the line drawn?
   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 0.0 2.0 (-0.5*hf/wf) (1.5*hf/wf)
      else ortho2D 0.0 (2.0*wf/hf) (-0.5) 1.5
   matrixMode $= Modelview 0
   loadIdentity

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

-- Open window with initial window size, title bar,
-- RGBA display mode, and handle input events.
main :: IO ()
main = do
   (progName, _args) <- getArgsAndInitialize
   initialDisplayMode $= [ SingleBuffered, RGBMode ]
   initialWindowSize $= Size 650 50
   createWindow progName
   listName <- myInit
   reshapeCallback $= Just reshape
   displayCallback $= display listName
   keyboardMouseCallback $= Just keyboard
   mainLoop