Skip to content

Instantly share code, notes, and snippets.

@zaphod42
Created May 10, 2013 23:52
Show Gist options
  • Select an option

  • Save zaphod42/5558304 to your computer and use it in GitHub Desktop.

Select an option

Save zaphod42/5558304 to your computer and use it in GitHub Desktop.
Learning Haskell and OpenGL at the same time.
import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
import Data.IORef
main :: IO ()
main = do
(progname, _) <- getArgsAndInitialize
createWindow "Hello World"
angle <- newIORef 0.0
displayCallback $= display angle
repeatedly 10 $ do
increase angle
forceDisplay
mainLoop
display angle = do
clear [ ColorBuffer ]
rotationAngle <- get angle
preservingMatrix $ do
rotate rotationAngle zAxis
renderSquare red 0.2
sayHello rotationAngle
flush
repeatedly milliseconds callback = do
addTimerCallback milliseconds $ again milliseconds callback
again milliseconds callback = do
callback
addTimerCallback milliseconds $ again milliseconds callback
increase angle = do
rotationAngle <- get angle
angle $=! (rotationAngle + 0.1)
forceDisplay = postRedisplay Nothing
renderSquare c size = renderPrimitive Quads $ do
color c
square size
sayHello angle = do
color white
currentRasterPosition $= bottomLeftCorner
renderString TimesRoman24 $ "Hello World! (" ++ (show angle) ++ ")"
square size =
let delta = size / 2
in do
vertex (vertexAt (0 + delta) (0 - delta) 0)
vertex (vertexAt (0 + delta) (0 + delta) 0)
vertex (vertexAt (0 - delta) (0 + delta) 0)
vertex (vertexAt (0 - delta) (0 - delta) 0)
bottomLeftCorner = Vertex4 (-1.0) (-1.0) 0.0 1.0
red = rgb 1 0 0
white = rgb 1 1 1
zAxis = vectorTo 0 0 1
rgb :: Float -> Float -> Float -> Color3 Float
rgb = Color3
vertexAt :: Float -> Float -> Float -> Vertex3 Float
vertexAt x y z = Vertex3 x y z
vectorTo :: Float -> Float -> Float -> Vector3 Float
vectorTo x y z = Vector3 x y z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment