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!
==Local transformations== One of the core reasons I started to write this tutorial series was that I wanted to figure out why [http://www.tfh-berlin.de/~panitz/hopengl/skript.html Panitz' tutorial] didn't work for me. The core explanation is simple - the names of some of the functions used has changed since he wrote them. Thus, the matrixExcursion in his tutorial is nowadays named preservingMatrix. This may well change further - though I hope it won't - in which case this tutorial will be painfully out of date as well. The idea of preservingMatrix, however, is to take a small piece of drawing actions, and perform them independent of the transformations going on outside that small piece. For demonstration, let's draw a bunch of cubes, shall we? We'll change the rather boring display subroutine in Display.hs into one using preservingMatrix to modify each cube drawn individually, giving a new Display.hs: <haskell> module Display (display) where import Graphics.UI.GLUT import Control.Monad import Cube points :: [(GLfloat,GLfloat,GLfloat)] points = [ (sin (2*pi*k/12), cos (2*pi*k/12), 0) | k <- [1..12] ] display :: DisplayCallback display = do clear [ColorBuffer] forM_ points $ \(x,y,z) -> preservingMatrix $ do color $ Color3 x y z translate $ Vector3 x y z cube 0.1 flush </haskell> Say... Those points on the unit circle might be something we'll want more of. Let's abstract some again! We'll break them out to a Points.hs: <haskell> module Points where import Graphics.Rendering.OpenGL points :: Int -> [(GLfloat,GLfloat,GLfloat)] points n = [ (sin (2*pi*k/n'), cos (2*pi*k/n'), 0) | k <- [1..n'] ] where n' = fromIntegral n </haskell> and then we get the Display.hs: <haskell> module Display (display) where import Graphics.UI.GLUT import Control.Monad import Cube import Points display :: DisplayCallback display = do clear [ColorBuffer] 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 flush </haskell> where we note that we need to renormalize our colours to get them within the interval [0,1] from the interval [-1,1] in order to get valid colour values. The output looks like: [[image:OG-7circle.png]] The point of this yoga doesn't come apparent until you start adding some global transformations as well. So let's! We add the line <haskell>scale 0.7 0.7 (0.7::GLfloat)</haskell> just after the <hask>clear [ColorBuffer]</hask>, in order to scale the entire picture. As a result, we get [[image:OG-7circlescaled.png]] We can do this with all sorts of transformations - we can rotate the picture, skew it, move the entire picture around. Using preservingMatrix, we make sure that the transformations โoutsideโ apply in the way we'd expect them to.
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