Introduction To Transformations
From JReality Wiki
Source file: TransformationFlat
JavaDoc: Transformation
This tutorial demonstrates how the same geometry can be reused in different SceneGraphComponent instances, each time being rendered according to the Transformation and Appearance contained in that instance.
Use flat hierarchy
In this case the scene graph is flat, with 8 children below the root.
... public static void main(String[] args) throws IOException { Color[] faceColors = {new Color(100, 200, 100), new Color(100, 100, 200), new Color(100,200,200), new Color(200,100,100)}; IndexedFaceSet ico = Primitives.sharedIcosahedron; SceneGraphComponent world = SceneGraphUtility.createFullSceneGraphComponent("world"); // set up the colors and sizes for edge and point rendering Appearance ap = world.getAppearance(); setupAppearance(ap); for (int i = 0; i<2; ++i) { for (int j = 0; j<2; ++j) { for (int k = 0; k<2; ++k) { SceneGraphComponent sgc = SceneGraphUtility.createFullSceneGraphComponent("sgc"+i+j+k); // set translation onto corner of a cube MatrixBuilder.euclidean().translate(-2+4*i, -2+4*j, -2+4*k).scale(1.5).assignTo(sgc); // set same geometry sgc.setGeometry(ico); // set appearance individually sgc.getAppearance().setAttribute(DIFFUSE_COLOR, faceColors[2*j+k]); sgc.getAppearance().setAttribute(FACE_DRAW, i == 0); sgc.getAppearance().setAttribute(EDGE_DRAW, j == 0); sgc.getAppearance().setAttribute(VERTEX_DRAW, k == 0); world.addChild(sgc); } } } JRViewer.display(world); } private static void setupAppearance(Appearance ap) { ... } }
Here's what the image should look like:
Use hierarchical scene graph
Source file: TransformationHierarchy
The following example draws the same picture as above, but constructs a genuinely hierarchical scene graph.
- the translation is not concatenated into a single node, but rather split up among the different levels.
- The first level, for example, translates in x, the second in y, and the third in z.
- You can use the navigator in the JRViewer to confirm this structure.
- The rendered image is identical to the above image.
... SceneGraphComponent sgc0, sgc1, sgc2; for (int i = 0; i<2; ++i) { sgc0 = SceneGraphUtility.createFullSceneGraphComponent("sgc"+i); MatrixBuilder.euclidean().translate(-2+4*i, 0, 0).assignTo(sgc0); world.addChild(sgc0); for (int j = 0; j<2; ++j) { sgc1 = SceneGraphUtility.createFullSceneGraphComponent("sgc"+i+j); MatrixBuilder.euclidean().translate(0,-2+4*j, 0).assignTo(sgc1); sgc0.addChild(sgc1); for (int k = 0; k<2; ++k) { sgc2 = SceneGraphUtility.createFullSceneGraphComponent("sgc"+i+j+k); sgc1.addChild(sgc2); // set translation onto corner of a cube MatrixBuilder.euclidean().translate(0, 0, -2+4*k).scale(1.5).assignTo(sgc2); // set same geometry sgc2.setGeometry(ico); // set appearance individually sgc2.getAppearance().setAttribute(DIFFUSE_COLOR, faceColors[2*j+k]); sgc2.getAppearance().setAttribute(FACE_DRAW, i == 0); sgc2.getAppearance().setAttribute(EDGE_DRAW, j == 0); sgc2.getAppearance().setAttribute(VERTEX_DRAW, k == 0); } } } ... }
|