hadronized / quaazar

Realtime 3D engine
BSD 3-Clause "New" or "Revised" License
6 stars 2 forks source link

Scene hierarchy and instances #72

Closed hadronized closed 9 years ago

hadronized commented 9 years ago

Transforms

We are going to rename Entity to Transform. See #71.

Instances

Instances are used to associate an object to a Transform as in:

data Instance a = Instance a Transform deriving (Eq,Read,Show)

Hierarchy

A Hierarchy is used to perform a hierarchical computation. We can create a hierarchy perform computations in there. From a given hierarchy, we can go down one level by introducing another hierarchy. Each hierarchy is just a computation that has access to a Transform. When introducing a new hierarchy, we have to provide the Transform to use in the hierarchy. Transforms get combined as we traverse down the whole hierarchy.

newtype Hierarchy a = Hierarchy { unHierarchy :: Reader Transform a } deriving (Applicative,Functor,Monad)

We can add a new level of hierarchy with the function hierarchy, which takes a Transform and the Hierarchy, and outputs the resulting Hierarchy.

We can access the outer hierarchy’s transform with parent.

We can instantiate any value in a Hierarchy with the instantiate function. For instance, we can instantiate a Light at the origin with the following code:

myLight :: Hierarchy (Instance Light)
myLight = instantiate (Omni (color 1 0 0) 10 1) origin

Then, we can run a hierarchy to get the resulting object with:

runHierarchy :: Hierarchy a -> Transform -> a

The Transform needed is the master transform. It’s used to affect the whole hierarchy tree. If the root of the hierarchy represents the total scene space, it’s often set to origin then.