Created
October 17, 2012 19:23
-
-
Save 573/3907562 to your computer and use it in GitHub Desktop.
GLFW-b test file (lesson02.hs)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- This code was created by Jeff Molofee '99 (ported to Haskell GHC 2005) | |
-- | |
-- source: https://github.com/dagit/nehe-tuts/blob/3bfebb167596a47b4a0e2ac36ef86df36ca65f18/lesson02.hs | |
-- | |
module Main where | |
import qualified Graphics.UI.GLFW as GLFW | |
-- everything from here starts with gl or GL | |
import Graphics.Rendering.OpenGL.Raw | |
import Graphics.Rendering.GLU.Raw ( gluPerspective ) | |
import Data.Bits ( (.|.) ) | |
import System.Exit ( exitWith, ExitCode(..) ) | |
import Control.Monad ( forever ) | |
initGL :: IO () | |
initGL = do | |
glShadeModel gl_SMOOTH -- enables smooth color shading | |
glClearColor 0 0 0 0 -- Clear the background color to black | |
glClearDepth 1 -- enables clearing of the depth buffer | |
glEnable gl_DEPTH_TEST | |
glDepthFunc gl_LEQUAL -- type of depth test | |
glHint gl_PERSPECTIVE_CORRECTION_HINT gl_NICEST | |
resizeScene :: GLFW.WindowSizeCallback | |
resizeScene w 0 = resizeScene w 1 -- prevent divide by zero | |
resizeScene width height = do | |
glViewport 0 0 (fromIntegral width) (fromIntegral height) | |
glMatrixMode gl_PROJECTION | |
glLoadIdentity | |
gluPerspective 45 (fromIntegral width/fromIntegral height) 0.1 100 | |
glMatrixMode gl_MODELVIEW | |
glLoadIdentity | |
glFlush | |
drawScene :: IO () | |
drawScene = do | |
-- clear the screen and the depth bufer | |
glClear $ fromIntegral $ gl_COLOR_BUFFER_BIT | |
.|. gl_DEPTH_BUFFER_BIT | |
glLoadIdentity -- reset view | |
glTranslatef (-1.5) 0 (-6.0) --Move left 1.5 Units and into the screen 6.0 | |
-- draw a triangle | |
glBegin gl_TRIANGLES | |
glVertex3f 0 1 0 -- top | |
glVertex3f 1 (-1) 0 -- bottom right | |
glVertex3f (-1) (-1) 0 -- bottom left | |
glEnd | |
glTranslatef 3 0 0 -- move right three units | |
glBegin gl_QUADS | |
glVertex3f (-1) 1 0 -- top left | |
glVertex3f 1 1 0 -- top right | |
glVertex3f 1 (-1) 0 -- bottom right | |
glVertex3f (-1) (-1) 0 -- bottom left | |
glEnd | |
glFlush | |
shutdown :: GLFW.WindowCloseCallback | |
shutdown = do | |
GLFW.closeWindow | |
GLFW.terminate | |
_ <- exitWith ExitSuccess | |
return True | |
keyPressed :: GLFW.KeyCallback | |
keyPressed GLFW.KeyEsc True = shutdown >> return () | |
keyPressed _ _ = return () | |
main :: IO () | |
main = do | |
True <- GLFW.initialize | |
-- select type of display mode: | |
-- Double buffer | |
-- RGBA color | |
-- Alpha components supported | |
-- Depth buffer | |
let dspOpts = GLFW.defaultDisplayOptions | |
-- get a 800 x 600 window | |
{ GLFW.displayOptions_width = 800 | |
, GLFW.displayOptions_height = 600 | |
-- Set depth buffering and RGBA colors | |
, GLFW.displayOptions_numRedBits = 8 | |
, GLFW.displayOptions_numGreenBits = 8 | |
, GLFW.displayOptions_numBlueBits = 8 | |
, GLFW.displayOptions_numAlphaBits = 8 | |
, GLFW.displayOptions_numDepthBits = 1 | |
-- , GLFW.displayOptions_displayMode = GLFW.Fullscreen | |
} | |
-- open a window | |
True <- GLFW.openWindow dspOpts | |
-- window starts at upper left corner of the screen | |
GLFW.setWindowPosition 0 0 | |
GLFW.setWindowTitle "Jeff Molofee's GL Code Tutorial ... NeHe '99" | |
-- register the function to do all our OpenGL drawing | |
GLFW.setWindowRefreshCallback drawScene | |
-- register the funciton called when our window is resized | |
GLFW.setWindowSizeCallback resizeScene | |
-- register the function called when the keyboard is pressed. | |
GLFW.setKeyCallback keyPressed | |
GLFW.setWindowCloseCallback shutdown | |
-- initialize our window. | |
initGL | |
-- start event processing engine | |
forever $ do | |
drawScene | |
GLFW.swapBuffers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment