/usr/share/doc/libghc-glut-doc/examples/BOGLGP/Chapter03/TrianglesQuads.hs is in libghc-glut-doc 2.1.2.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | {-
TrianglesQuads.hs (adapted from TrianglesQuads which is (c) 2004 Astle/Hawkins)
Copyright (c) Sven Panne 2004-2005 <sven.panne@aedion.de>
This file is part of HOpenGL and distributed under a BSD-style license
See the file libraries/GLUT/LICENSE
-}
import Control.Monad ( when, unless )
import Data.Maybe ( isJust )
import Graphics.UI.GLUT hiding ( initialize )
import System.Console.GetOpt
import System.Environment ( getProgName )
import System.Exit ( exitWith, ExitCode(..) )
import System.IO ( hPutStr, stderr )
--------------------------------------------------------------------------------
-- Setup GLUT and OpenGL, drop into the event loop.
--------------------------------------------------------------------------------
main :: IO ()
main = do
-- Setup the basic GLUT stuff
(_, args) <- getArgsAndInitialize
opts <- parseOptions args
initialDisplayMode $= [ DoubleBuffered, RGBMode, WithDepthBuffer ]
(if useFullscreen opts then fullscreenMode else windowedMode) opts
initialize
-- Register the event callback functions
displayCallback $= do render; swapBuffers
reshapeCallback $= Just setupProjection
keyboardMouseCallback $= Just keyboardMouseHandler
-- No need for an idle callback here, this would just hog the CPU
-- without any visible effect
-- At this point, control is relinquished to the GLUT event handler.
-- Control is returned as events occur, via the callback functions.
mainLoop
fullscreenMode :: Options -> IO ()
fullscreenMode opts = do
let addCapability c = maybe id (\x -> (Where' c IsEqualTo x :))
gameModeCapabilities $=
(addCapability GameModeWidth (Just (windowWidth opts)) .
addCapability GameModeHeight (Just (windowHeight opts)) .
addCapability GameModeBitsPerPlane (bpp opts) .
addCapability GameModeRefreshRate (refreshRate opts)) []
enterGameMode
maybeWin <- get currentWindow
if isJust maybeWin
then cursor $= None
else do
hPutStr stderr "Could not enter fullscreen mode, using windowed mode\n"
windowedMode (opts { useFullscreen = False } )
windowedMode :: Options -> IO ()
windowedMode opts = do
initialWindowSize $=
Size (fromIntegral (windowWidth opts)) (fromIntegral (windowHeight opts))
createWindow "BOGLGP - Chapter 3 - TrianglesQuads"
return ()
--------------------------------------------------------------------------------
-- Option handling
--------------------------------------------------------------------------------
data Options = Options {
useFullscreen :: Bool,
windowWidth :: Int,
windowHeight :: Int,
bpp :: Maybe Int,
refreshRate :: Maybe Int
}
startOpt :: Options
startOpt = Options {
useFullscreen = False,
windowWidth = 800,
windowHeight = 600,
bpp = Nothing,
refreshRate = Nothing
}
options :: [OptDescr (Options -> IO Options)]
options = [
Option ['f'] ["fullscreen"]
(NoArg (\opt -> return opt { useFullscreen = True }))
"use fullscreen mode if possible",
Option ['w'] ["width"]
(ReqArg (\arg opt -> do w <- readInt "WIDTH" arg
return opt { windowWidth = w })
"WIDTH")
"use window width WIDTH",
Option ['h'] ["height"]
(ReqArg (\arg opt -> do h <- readInt "HEIGHT" arg
return opt { windowHeight = h })
"HEIGHT")
"use window height HEIGHT",
Option ['b'] ["bpp"]
(ReqArg (\arg opt -> do b <- readInt "BPP" arg
return opt { bpp = Just b })
"BPP")
"use BPP bits per plane (ignored in windowed mode)",
Option ['r'] ["refresh-rate"]
(ReqArg (\arg opt -> do r <- readInt "HZ" arg
return opt { refreshRate = Just r })
"HZ")
"use refresh rate HZ (ignored in windowed mode)",
Option ['?'] ["help"]
(NoArg (\_ -> do usage >>= putStr
safeExitWith ExitSuccess))
"show help" ]
readInt :: String -> String -> IO Int
readInt name arg =
case reads arg of
((x,[]) : _) -> return x
_ -> dieWith ["Can't parse " ++ name ++ " argument '" ++ arg ++ "'\n"]
usage :: IO String
usage = do
progName <- getProgName
return $ usageInfo ("Usage: " ++ progName ++ " [OPTION...]") options
parseOptions :: [String] -> IO Options
parseOptions args = do
let (optsActions, nonOptions, errs) = getOpt Permute options args
unless (null nonOptions && null errs) (dieWith errs)
foldl (>>=) (return startOpt) optsActions
dieWith :: [String] -> IO a
dieWith errs = do
u <- usage
mapM_ (hPutStr stderr) (errs ++ [u])
safeExitWith (ExitFailure 1)
--------------------------------------------------------------------------------
-- Handle mouse and keyboard events. For this simple demo, just exit when
-- ESCAPE is pressed.
--------------------------------------------------------------------------------
keyboardMouseHandler :: KeyboardMouseCallback
keyboardMouseHandler (Char '\27') Down _ _ = safeExitWith ExitSuccess
keyboardMouseHandler _ _ _ _ = return ()
safeExitWith :: ExitCode -> IO a
safeExitWith code = do
gma <- get gameModeActive
when gma leaveGameMode
exitWith code
--------------------------------------------------------------------------------
-- Do one time setup, i.e. set the clear color.
--------------------------------------------------------------------------------
initialize :: IO ()
initialize = clearColor $= Color4 0 0 0 0
--------------------------------------------------------------------------------
-- Reset the viewport for window changes.
--------------------------------------------------------------------------------
setupProjection :: ReshapeCallback
setupProjection (Size width height) = do
-- don't want a divide by zero
let h = max 1 height
-- reset the viewport to new dimensions
viewport $= (Position 0 0, Size width h)
-- set projection matrix as the current matrix
matrixMode $= Projection
-- reset projection matrix
loadIdentity
-- calculate aspect ratio of window
perspective 52 (fromIntegral width / fromIntegral h) 1 1000
-- set modelview matrix
matrixMode $= Modelview 0
-- reset modelview matrix
loadIdentity
--------------------------------------------------------------------------------
-- Clear and redraw the scene.
--------------------------------------------------------------------------------
render :: DisplayCallback
render = do
-- clear screen and depth buffer
clear [ ColorBuffer, DepthBuffer ]
loadIdentity
lookAt (Vertex3 0 10 0.1) (Vertex3 0 0 0) (Vector3 0 1 0)
-- resolve overloading, not needed in "real" programs
let translate3f = translate :: Vector3 GLfloat -> IO ()
-- top left
preservingMatrix $ do
translate3f (Vector3 (-6) 0 (-4))
drawPoints
-- top middle
preservingMatrix $ do
polygonMode $= (Fill, Fill)
translate3f (Vector3 (-2) 0 (-4))
drawTriangles
-- top right
preservingMatrix $ do
polygonMode $= (Line, Line)
translate3f (Vector3 2 0 (-4))
drawQuads
-- bottom left
preservingMatrix $ do
polygonMode $= (Line, Line)
translate3f (Vector3 (-6) 0 0.5)
drawTriangleStrip
-- bottom middle
preservingMatrix $ do
polygonMode $= (Line, Line)
translate3f (Vector3 (-2) 0 0.5)
drawTriangleFan
-- bottom right
preservingMatrix $ do
polygonMode $= (Line, Line)
translate3f (Vector3 2 0 0.5)
drawQuadStrip
-- Hello, this is C... :-)
for :: [GLfloat] -> (GLfloat -> IO ()) -> IO ()
for = flip mapM_
-- draw grid of points showing dataset we are working with
drawPoints :: IO ()
drawPoints = do
pointSize $= 4
renderPrimitive Points $
for [ 0 .. 3 ] $ \x ->
for [ 0 .. 3 ] $ \z ->
vertex (Vertex3 x 0 z)
-- draw grid as individual triangles
drawTriangles :: IO ()
drawTriangles =
renderPrimitive Triangles $
for [ 0 .. 2 ] $ \x ->
for [ 0 .. 2 ] $ \z -> do
vertex (Vertex3 x 0 z )
vertex (Vertex3 (x + 1) 0 z )
vertex (Vertex3 x 0 (z + 1))
-- draw grid as triangle fan
drawTriangleFan :: IO ()
drawTriangleFan =
renderPrimitive TriangleFan $ do
-- center vertex of fan
vertex (Vertex3 0 0 (0 :: GLfloat))
-- bottom side
for [ 3, 2 .. 0 ] $ \x ->
vertex (Vertex3 x 0 3)
-- right side
for [ 3, 2 .. 0 ] $ \z ->
vertex (Vertex3 3 0 z)
-- draw grid as triangle strips
drawTriangleStrip :: IO ()
drawTriangleStrip =
-- 3 rows of triangle strips
for [ 0 .. 2 ] $ \x ->
renderPrimitive TriangleStrip $
for [ 0 .. 2 ] $ \z -> do
vertex (Vertex3 x 0 z )
vertex (Vertex3 (x + 1) 0 z )
vertex (Vertex3 x 0 (z + 1))
vertex (Vertex3 (x + 1) 0 (z + 1))
-- draw grid as individual quads
drawQuads :: IO ()
drawQuads =
renderPrimitive Quads $
for [ 0 .. 2 ] $ \x ->
for [ 0 .. 2 ] $ \z -> do
vertex (Vertex3 x 0 z )
vertex (Vertex3 (x + 1) 0 z )
vertex (Vertex3 (x + 1) 0 (z + 1))
vertex (Vertex3 x 0 (z + 1))
-- draw grid as quad strips
drawQuadStrip :: IO ()
drawQuadStrip =
for [ 0 .. 2 ] $ \x ->
renderPrimitive QuadStrip $
for [ 0 .. 3 ] $ \z -> do
vertex (Vertex3 x 0 z)
vertex (Vertex3 (x + 1) 0 z)
|