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
Glome tutorial
(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!
===Groups=== Sometimes it's convenient to treat a whole group of object as if they were a single object, and for that purpose we have Group. One reason we might want to do this has already been mentioned: the scene needs to be described in terms of a single object. There are several others. We might want to apply a texture or move or rotate a whole group of objects at once, instead of treating them individually. Using groups is quite simple. For instance: <haskell> group [ (sphere (Vec (-1) 0 0) 2), (sphere (Vec 1 0 0) 2), (sphere (Vec 0 (-1) 0) 2), (sphere (Vec 0 1 0) 2) ] </haskell> This gives us four spheres we can treat as a single object. Group is just an alias for [SolidItem]. It has a special constructor, "group", that does a little bit of extra optimization for us: <haskell> group :: [SolidItem t m] -> SolidItem t m group [] = SolidItem Void group (sld:[]) = sld group slds = SolidItem (flatten_group slds) </haskell> If you try to create an empty group, Glome will replace it with a primitive of type Void, which is a degenerate object that has no appearance. If you try to create a group that contains only one object, Glome throws the group away and just uses that object directly. If you try to create a group that contains other groups, Glome will consolidate those into one big group. (This is what "flatten_group" does.) In general, it's better to use "group" than "Group". Even better than "group", though, is "bih", which behaves similarly to group but performs much better if your group contains more than a couple items. I'll explain why later. Haskell has some convenient syntax for creating lists. For instance, if you want to create a lot of spheres, you can use: <haskell> let lattice = let n = 20 in [sphere (vec x y z) 0.1 | x <- [(-n)..n], y <- [(-n)..n], z <- [(-n)..n]] </haskell> This gives you a list of spheres arranged in a 41x41x41 3-D grid. You can then create a group out of it: <haskell> group lattice </haskell> But it will render very slowly. If you use "bih" instead of "group", it will render much faster. (We'll get into why later on.) We can use cones and spheres together in a useful way by stringing a series of cones together with a sphere to hide each joint. <haskell> spiral = [ ((Vec ((sin (rot n))*n) ((cos (rot n))*n) (n-3)), (n/15)) | n <- [0, 0.05..6]] coil = bih (zipWith (\ (p1,r1) (p2,r2) -> (Solid.group [(cone p1 r1 p2 r2), (sphere p1 r1)] )) spiral (tail spiral)) </haskell> Here, "spiral" is a list of (location,radius) tuples, and we use [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v%3AzipWith zipWith] to create cylinders and cones from pairs of (location,radius) tuples. We save the result into the variable "coil".
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