Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
OpenGLTutorial1
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Hello World== A minimal OpenGL program will need to load the OpenGL libraries and open a window. This is all you need to get an OpenGL program running. This is the skeleton that we'll be building on for the rest of this tutorial: <haskell> import Graphics.UI.GLUT main :: IO () main = do (_progName, _args) <- getArgsAndInitialize _window <- createWindow "Hello World" displayCallback $= display mainLoop display :: DisplayCallback display = do clear [ ColorBuffer ] flush </haskell> Save it to HelloWorld.hs and load it into GHCi or compile it with GHC via <code>ghc --make HelloWorld.hs</code>. When you run the program, a new blank window open with the title "Hello World" will open. This code creates a window and sets the main display function. <hask>getArgsAndInitialize</hask> initializes the OpenGL systems. <hask>createWindow</hask> opens the window; its argument is the title of the window. <hask>displayCallback</hask> controls the main display function for the current window. We use <hask>($=)</hask> to set it to our <hask>display</hask> function. <hask>mainLoop</hask> is where GLUT takes over, using our <hask>displayCallback</hask> to draw the contents of the window. This defines a function <hask>display</hask> that calls a few OpenGL functions. <hask>clear</hask> clears out the graphics color state (so we get a blank canvas). <hask>flush</hask> pushes our OpenGL commands down to the system graphics for actual display. ===<code>displayCallback $= display</code>=== We don't call <hask>display</hask> directly. In fact, we don't call any graphics drawing functions directly. Instead we set a display callback, and then call <hask>mainLoop</hask>. In <hask>mainLoop</hask>, GLUT takes over. It handles all the details of interacting with the OS, refreshing our window, and calling our <hask>displayCallback</hask> to draw graphics. <hask>displayCallback</hask> is a globally defined StateVar (mutable state variable), which we set using a call to <hask>($=)</hask>. In the OpenGL [http://hackage.haskell.org/packages/archive/OpenGL/latest/doc/html/Graphics-Rendering-OpenGL-GL-StateVar.html StateVar] module, there is a HasSetter type class and an StateVar implementation providing functions <hask>($=)</hask> (assignment) and <hask>get</hask> to facilitate interactions with these state variables. IORefs are StateVars, too: <haskell> do height <- newIORef 1.0 currentHeight <- get height height $= 1.5 </haskell>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width