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
OpenGLTutorial2
(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!
== Adding Depth == The code we have written so far may not handle depth properly, but the program as-written won't reveal whether or not this is the case! Let's extend the example to add outlines the cubes, and add depth to the animation! The code for the wire frame belongs in Cube.hs. We can draw a wire frame using <code>vertex3f</code> from above: <haskell> cubeFrame :: GLfloat -> IO () cubeFrame w = renderPrimitive Lines $ mapM_ vertex3f [ ( w,-w, w), ( w, w, w), ( w, w, w), (-w, w, w), (-w, w, w), (-w,-w, w), (-w,-w, w), ( w,-w, w), ( w,-w, w), ( w,-w,-w), ( w, w, w), ( w, w,-w), (-w, w, w), (-w, w,-w), (-w,-w, w), (-w,-w,-w), ( w,-w,-w), ( w, w,-w), ( w, w,-w), (-w, w,-w), (-w, w,-w), (-w,-w,-w), (-w,-w,-w), ( w,-w,-w) ] </haskell> If you simply call this with a unique color in your <hask>display</hask> function, you may not get the results you expect. You might see lines which should be occluded, or you might not see new lines at all. Let's take a look at how we can fix some of these problems. The first thing we need to do is ensure that we initialize our window with a depth buffer. The depth buffer indicates the current depth of a pixel on our screen, allowing OpenGL to determine whether or not to draw over the current color. We also need to specify how our depth buffer will do this. We want things with less depth to be rendered above those with more depth, so we use the comparison function <hask>Less</hask>. We again modify the HelloWorld.hs as follows: <haskell> import Graphics.UI.GLUT import Data.IORef import Bindings main :: IO () main = do (_progName, _args) <- getArgsAndInitialize initialDisplayMode $= [WithDepthBuffer, DoubleBuffered] _window <- createWindow "Hello World" reshapeCallback $= Just reshape depthFunc $= Just Less -- the comparison function for depth the buffer angle <- newIORef 0 delta <- newIORef 0.1 pos <- newIORef (0, 0) keyboardMouseCallback $= Just (keyboardMouse delta pos) idleCallback $= Just (idle angle delta) displayCallback $= display angle pos mainLoop </haskell> Lastly, we modify the Display function to have it clear the depth buffer to keep our image in order. We should also call <hask>cubeFrame</hask> to see our spiffy new outlines, and modify our axis of rotation so we can see the corners of the cubes in action! <haskell> module Display (idle, display) where import Graphics.UI.GLUT import Control.Monad import Data.IORef import Cube import Points display :: IORef GLfloat -> IORef (GLfloat, GLfloat) -> DisplayCallback display angle pos = do clear [ColorBuffer, DepthBuffer] -- clear depth buffer, too clear [ColorBuffer] loadIdentity (x',y') <- get pos translate $ Vector3 x' y' 0 preservingMatrix $ do a <- get angle rotate a $ Vector3 0 0 1 rotate a $ Vector3 0 0.1 1 -- changed y-component a bit to show off cube corners scale 0.7 0.7 (0.7::GLfloat) forM_ (points 7) $ \(x,y,z) -> preservingMatrix $ do color $ Color3 ((x+1)/2) ((y+1)/2) ((z+1)/2) translate $ Vector3 x y z cube 0.1 color $ Color3 (0::GLfloat) 0 0 -- set outline color to black cubeFrame 0.1 -- draw the outline swapBuffers idle :: IORef GLfloat -> IORef GLfloat -> IdleCallback idle angle delta = do d <- get delta angle $~! (+ d) postRedisplay Nothing </haskell> The animation starts off with all the cubes facing you, so increase the speed and let it run for a bit to let the corners show up. You should now have cubes revolving around an off-center axis with outlines, showing off their corners! [[Image:Outline-cubes.png|300px]] Note that the code covered here allows us to add some depth to our image, but may not be sufficient to cover transparency and blending.
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