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!
==Using the drawing canvas== So, we have a window, we have a display callback that clears the canvas. Don't we want more out of it? Sure we do. So let's draw some things. <haskell> import Graphics.UI.GLUT myPoints :: [(GLfloat,GLfloat,GLfloat)] myPoints = [ (sin (2*pi*k/12), cos (2*pi*k/12), 0) | k <- [1..12] ] main :: IO () main = do (_progName, _args) <- getArgsAndInitialize _window <- createWindow "Hello World" displayCallback $= display mainLoop display :: DisplayCallback display = do clear [ColorBuffer] renderPrimitive Points $ mapM_ (\(x, y, z) -> vertex $ Vertex3 x y z) myPoints flush </haskell> Now, the important thing to notice in this code extract is that <hask>renderPrimitive</hask> line. It starts a rendering definition, gives the kind of things to be rendered, and then a sequence of function calls, each of which adds a vertex to the rendering canvas. The statement is basically equivalent to something along the lines of <haskell> renderPrimitive Points do vertex (Vertex3 ...) vertex (Vertex3 ...) ... </haskell> for appropriate triples of coordinate values at the appropriate places. This results in the rendition here: [[image:OG-Points.png]] We can replace <code>Points</code> with other primitives, leading to the rendering of: ===<code>Triangles</code>=== [[image:OG-Triangles.png]] Each three coordinates following each other define a triangle. The last n mod 3 coordinates are ignored. ===Triangle strips=== [[image:OG-Trianglestrip.png]] When using <code>TriangleStrip</code>, triangles are drawn according to a “moving window” of size three, so the two last coordinates in the previous triangle become the two first in the next triangle. ===Triangle fans=== [[image:OG-Trianglesfan.png]] When using a <code>TriangleFan</code>, the first given coordinate is used as a base point, and takes each pair of subsequent coordinates to define a triangle together with the first coordinate. ===Lines=== [[image:OG-Lines.png]] Each pair of coordinates define a line. ===Line loops=== [[image:OG-Lineloop.png]] With <code>LineLoop</code>, each further coordinate defines a line together with the last coordinate used. Once all coordinates are used up, an additional line is drawn back to the beginning. ===Line strips=== [[image:OG-Linestrip.png]] A <code>LineStrip</code> is like a <code>LineLoop</code>, only without the last link added. ===Quadrangles=== [[image:OG-Quad.png]] For the <code>Quads</code> keyword, each four coordinates given define a quadrangle. ===Quadrangle strips=== [[image:OG-Quadstrip.png]] And a <code>QuadStrip</code> works as the <code>TriangleStrip</code>, only the window is 4 coordinates wide and steps 2 steps each time, so each new pair of coordinates attaches a new quadrangle to the last edge of the last quadrangle. It is easier to understand what is going on when you see how the window is formed. Giving each coordinate a number, the QuadStrip is rendered as follows: Coordinates 1, 2 and 4 are rendered as a triangle followed by coordinates 1, 3 and 4. Next coordinates 3, 4 and 6 are rendered as a triangle followed by coordinates 3, 5 and 6. Rendering continues for as many coordinates that can be formed by that pattern. ===Polygon=== [[image:OG-Polygon.png]] A <code>Polygon</code> is a filled line loop. Simple as that! ===Using colors=== There are more things we can do on our canvas than just spreading out coordinates. Within the command list constructed after a renderPrimitive, we can give several different commands that control what things are supposed to look like, so for instance we could use the following: <haskell> display = do let color3f r g b = color $ Color3 r g (b :: GLfloat) vertex3f x y z = vertex $ Vertex3 x y (z :: GLfloat) clear [ColorBuffer] renderPrimitive Quads $ do color3f 1 0 0 vertex3f 0 0 0 vertex3f 0 0.2 0 vertex3f 0.2 0.2 0 vertex3f 0.2 0 0 color3f 0 1 0 vertex3f 0 0 0 vertex3f 0 (-0.2) 0 vertex3f 0.2 (-0.2) 0 vertex3f 0.2 0 0 color3f 0 0 1 vertex3f 0 0 0 vertex3f 0 (-0.2) 0 vertex3f (-0.2) (-0.2) 0 vertex3f (-0.2) 0 0 color3f 1 0 1 vertex3f 0 0 0 vertex3f 0 0.2 0 vertex3f (-0.2) 0.2 0 vertex3f (-0.2) 0 0 flush </haskell> in order to produce these four coloured squares: [[image:OG-Colorsquares.png]] where each color command sets the color for the next items drawn, and the vertex commands give vertices for the four squares.
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