CS424 Notes, 28 March
- Scene Graphs in world2.html
- A scene graph is a data structure that contains information about objects in a scene. The scene can be rendered by traversing the scene graph.
- The world2 example from Monday's class defines simple scene graphs using basic objects (such as cubes and spheres) and a class, ComplexObject, that represents one or more simpler objects together with some possible transformations.
- The basic objects belong to another class, SimpleObject3D. You can add SimpleObject3Ds to a ComplexObject, and you can add other ComplexObjects to a ComplexObject. Only ComplexObjects can have color and transformations. SimpleObject3Ds are used as terminal nodes in the scene graph. ComplexObjects are interior nodes, which link to other "child" objects.
- Five SimpleObject3Ds are pre-defined as variables in the example:
- cube1 -- a cube with side 2, centered at (0,0,0) and extending from -1 to 1 along all three axes.
- sphere1 -- a sphere of radius 0.5 (diameter 1), centered at (0,0,0)
- cone1 -- a cone whose base has radius 0.5 (diameter 1), and whose height is 1. Its axis is the z-axis, and it extends from -0.5 to 0.5 along the z-axis.
- cylinder1 -- a cylinder of radius 0.5 (diameter 1) and height 1, centered at (0,0,0). The z-axis is the axis of the cylinder.
- torus1 -- a torus of radius 0.5 (diameter 1) and inner radius 0.5/3, centered at (0,0,0), bisected by the xy-plane, with the z-axis going through the doughnut hole.
- The ComplexObject class contains the following methods:
- drawObjects(modelview,material) -- render this ComplexObject, including recursive rendering of its sub-objects, using the specified modelview matrix and material. The second parameter is an object with fields ambientAndDiffuse, specular, and emissive. The value of each field is an array of three numbers giving the ambient and diffuse, specular, and emissive material colors. (Ambient and diffuse material colors are always the same.)
- add(obj1,obj2,...) -- add objects to this ComplexObject. There can be any number of parameters, although one parameter is most common. The parameters should be of type SimpleObject3D or ComplexObject.
- setScaling(sx,sy,sz) -- Set the scale transformation that will be applied to this ComplexObject when it is rendered.
- setRotation(degrees,ax,ay,az) -- Set the rotation transformation that will be applied to this ComplexObject when it is rendered.
- setTranslation(tx,ty,tz) -- Set the translation transformation that will be applied to this ComplexObject when it is rendered. (Note: The scaling, rotation, and translation are applied one after the other, in that order, to the object and, recursively, its sub-objects; they are always applied in this order, not in the order in which the functions setScaling, setRotation, setTranslation are called.)
- setColor(r,g,b) -- Set the ambient and diffuse color of the object to r, g, b. Set the specular color to r/4, g/4, b/4. The parameters are numbers in the range 0.0 to 1.0. Note that the default value for color properties is null. A null value indicates that this object inherits its color from its parent in the scene graph. This means that the color of a ComplexObject is a default color for all the sub-objects, which can be overridden for a sub-object by setting the color property of the sub-object. (There really should be a setMaterial methods for setting all the material properties.)
- The ComplexObject class has a constructor that can take zero or more objects as parameters. The parameters can be of type SimpleObject3D or ComplexObject. For example, new ComplexObject(cube1) creates a new ComplexObject that contains a cube as its child object.
- The ComplexObject methods setScaling, setRotation, setTranslation, and setColor
return this as their value. This allows you to string together calls
to these methods in a single statement. For example
obj.setColor(1,0,0).setScaling(2,2,2).setTranslation(0,0,1);
A constructor, of course, also returns the object that is being constructed as its value, so you can do things like:var topCone = new ComplexObject(cone1).setScaling(10,10,10).setRotation(90,-1,0,0);
orworld.add( new ComplexObject(cube1).setScalilng(5, 0.2, 5) );
- Building Scene Graphs
- We will discuss creating various complex objects and animating them. You will then do some of that yourself in this week's lab.
- We will also start talking about building a more comprehensive and robust scene graph API.