Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import Graphics.UI.GLUT
  2. import Data.IORef
  3.  
  4. cubeVertex :: [Vertex3 GLdouble]
  5. cubeVertex = [ Vertex3 0 0 0
  6. , Vertex3 1 0 0
  7. , Vertex3 1 1 0
  8. , Vertex3 0 1 0
  9. , Vertex3 0 0 1
  10. , Vertex3 1 0 1
  11. , Vertex3 1 1 1
  12. , Vertex3 0 1 1
  13. ]
  14.  
  15. cubeFace :: [(Int, Int, Int, Int)]
  16. cubeFace = [ (0, 1, 2, 3)
  17. , (1, 5, 6, 2)
  18. , (5, 4, 7, 6)
  19. , (4, 0, 3, 7)
  20. , (4, 5, 1, 0)
  21. , (3, 2, 6, 7)
  22. ]
  23.  
  24. cubeColor :: [Color3 GLdouble]
  25. cubeColor = [ Color3 1 0 0
  26. , Color3 0 1 0
  27. , Color3 0 0 1
  28. , Color3 1 1 0
  29. , Color3 1 0 1
  30. , Color3 0 1 1
  31. ]
  32.  
  33. display :: IORef GLdouble -> IORef GLdouble -> DisplayCallback
  34. display rot1 rot2 = do
  35. clear [ColorBuffer, DepthBuffer]
  36. r1 <- get rot1
  37. r2 <- get rot2
  38. loadIdentity
  39. rotate r1 $ Vector3 1 0 0
  40. rotate r2 $ Vector3 0 1 0
  41. renderPrimitive Quads $ do
  42. mapM_ (draw cubeVertex) $ zip cubeFace cubeColor
  43. swapBuffers
  44.  
  45. draw :: [Vertex3 GLdouble] -> ((Int, Int, Int, Int), Color3 GLdouble)
  46. -> IO ()
  47. draw xs ((n,m,t,s), cl) = do
  48. color3d cl
  49. vertex3d (xs !! n)
  50. vertex3d (xs !! m)
  51. vertex3d (xs !! t)
  52. vertex3d (xs !! s)
  53. where
  54. color3d = color :: Color3 GLdouble -> IO ()
  55. vertex3d = vertex :: Vertex3 GLdouble -> IO ()
  56.  
  57. resize :: Size -> IO ()
  58. resize s@(Size w h) = do
  59. viewport $= (Position 0 0,s)
  60. matrixMode $= Projection
  61. loadIdentity
  62. perspective 45.0 (w'/h') 1.0 100.0
  63. lookAt (Vertex3 0.5 0.5 (-5)) (Vertex3 0.5 0.5 0.5) (Vector3 0 1 0)
  64. matrixMode $= Modelview 0
  65. where
  66. w' = realToFrac w
  67. h' = realToFrac h
  68.  
  69. keyboard :: IORef GLdouble -> IORef GLdouble -> KeyboardCallback
  70. keyboard rot1 rot2 c _ = do
  71. case c of
  72. 'j' -> rot1 $~! (subtract 1)
  73. 'k' -> rot1 $~! (+1)
  74. 'h' -> rot2 $~! (subtract 1)
  75. 'l' -> rot2 $~! (+1)
  76. 'q' -> leaveMainLoop
  77. _ -> return ()
  78.  
  79. idle :: IdleCallback
  80. idle = postRedisplay Nothing
  81.  
  82. main :: IO ()
  83. main = do
  84. getArgsAndInitialize
  85. initialDisplayMode $= [RGBAMode, DoubleBuffered, WithDepthBuffer]
  86. createWindow "Sample"
  87. clearColor $= Color4 1 1 1 1
  88. depthFunc $= Just Less
  89. rot1 <- newIORef 0
  90. rot2 <- newIORef 0
  91. displayCallback $= display rot1 rot2
  92. reshapeCallback $= Just resize
  93. keyboardCallback $= Just (keyboard rot1 rot2)
  94. idleCallback $= Just idle
  95. mainLoop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement