I want to implement a simple SceneGraph in Haskell using Data.Tree
consisting of Transform
and Shape
nodes. In a SceneGraph the spatial transformation is accumulated while traversing and applied to the shape for rendering.
type Transform = Vector2 Double
data Shape = Circle Double | Square Double
data SceneNode = XFormNode Transform | ShapeNode Shape
Say we have a scene with an object which is moved to the right and consisting of a Square at bottom and a Circle on top
^
|
| ()
| []
0----->
I came up with this tree definition:
let tree = Node (XFormNode (vector2 10 0))
[Node (ShapeNode (Square 10)) []
,Node (XFormNode (vector2 0 10))
[Node (ShapeNode (Circle 10)) []]
]
The rendering would be something like this:
render :: Position2 -> Shape -> IO ()
render p (Circle r) = drawCircle p r
render p (Square a) = drawSquare p a
My questions are:
1) How do you define a traverse
function, which accumulates the transformation and calls the render tasks?
2) How do you avoid making the traverse IO?
3) Is there a shorter version to define this tree? All but the first Node definition and all empty subForests are actually superfluous.
Thank you!