1 / 27

CSC 308 – Graphics Programming

CSC 308 – Graphics Programming. Java 3D Information from online tutorial at http://java.sun.com/developer/onlineTraining/java3d/. Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC. Java 3D Virtual Universe. Created from a scene graph

wolfe
Download Presentation

CSC 308 – Graphics Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 308 – Graphics Programming Java 3D Information from online tutorial at http://java.sun.com/developer/onlineTraining/java3d/ Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC

  2. Java 3D Virtual Universe • Created from a scene graph • Scene graphs are created from using instances of Java 3D classes • Assembled from objects to define the geometry, sound, lights, location, orientation, and appearance of visual and audio objects • Only one path can exist from the root of the tree to each leaf; if not, the program may compile, but it will not render.

  3. Java 3D Basic Recipe • Create a Canvas3D object • Create a VirtualUniverse object • Create a Locale object, attaching it to the VirtualUniverse object • Construct a view branch graph • Create a View object • Create a ViewPlatform object • Create a PhysicalBody object • Create a PhysicalEnvironment object • Attach ViewPlatform, PhysicalBody, PhysicalEnvironment, and Canvas3D objects to View object • Construct content branch graph(s) • Compile branch graph(s) • Insert subgraphs into the Locale

  4. Java 3D Basic Recipe • Creating each branch graph of the scene is the majority of the programming • Can we simplify the recipe? • Yes! The SimpleUniverse class takes care of steps 2,3, and 4 for us This significantly reduces the time and effort necessary to create the view branch graph allowing more time to concentrate on the content.

  5. Java 3D Simple Recipe • Create a Canvas3D object • Create a SimpleUniverse object which references the earlier Canvas3D object • Customize the SimpleUniverse object • Construct content branch • Compile content branch graph • Insert content branch graph into the Locale of the SimpleUniverse

  6. A Simple Program public class HelloJava3Da extends Applet { public HelloJava3Da() { setLayout(new BorderLayout()); Canvas3D canvas3D = new Canvas3D(null); add("Center", canvas3D); BranchGroup scene = createSceneGraph(); scene.compile(); //Simple Universe is a Convience Utility class SimpleUniverse simpleU = new SimpleUniverse(canvas3D); // This moves the ViewPlatform back a bit so the // objects in the scene can be viewed. simpleU.getViewingPlatform().setNominalViewingTransform(); simpleU.addBranchGraph(scene); } // end of HelloJava3Da (constructor)

  7. Simple Recipe Example All the steps of the Simple Recipe example are inside the constructor of HelloJava3Da.java • Create a Canvas3d object Canvas3D canvas3D = new Canvas3D(null); • Create a Simple Universe Object and customize it SimpleUniverse simpleU = new SimpleUniverse(canvas3D); simpleU.getViewingPlatform().setNominalViewingTransform(); • Construct content branch BranchGroup scene = createSceneGraph(); • Compile content branch graph scene.compile(); • Insert content branch graph into the Locale of the Simple Universe simpleU.addBranchGraph(scene);

  8. Content Branch Graph • This is created with a call to “createSceneGraph()” This method simply creates a color cube object for now and returns it as the child of the objRoot. The code follows on the next slide…

  9. Create Content Branch Graph public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Create a simple shape leaf node, add it to //the scene graph. //ColorCube is a convenience Utility class objRoot.addChild(new ColorCube(0.4)); return objRoot; }

  10. Applet or Application? • Currently, this code is an applet; however, with the addition of a simple main program, we can run it as an application: public static void main (String [] args) { Frame frame = new MainFrame(new HelloJava3Da(), 256, 256); } // end of main method }

  11. Don’t forget to import… • import java.applet.*; • import java.awt.*; • import java.awt.event.*; • import com.sun.j3d.utils.applet.*; • import com.sun.j3d.utils.universe.*; • import com.sun.j3d.utils.geometry.*; • import javax.media.j3d.*; • import javax.vecmath.*;

  12. Scene Graph

  13. Rotating the Cube • A simple rotation can be used to show more than one face of the cube. The first step is to create the desired transformation using a Transform3D object. • First, let’s rotate about the x-axis. Two parameters are necessary – the axis of revolution and the angle of rotation. • Choose axis by choosing proper method • Choose angle specified in radians

  14. Rotating the Cube • After creating the Transform3D object “rotate”, it is used in the creation of a Transformation3D object objRotate which, in turn, is used in the scene graph. Then, the ColorCube becomes it’s child and the objRoot takes objRotate as its child. (See example)

  15. Rotating the Cube public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Rotate object has composite transformation matrix Transform3D rotate = new Transform3D(); Transform3D tempRotate = new Transform3D(); rotate.rotX(Math.PI/4.0d); tempRotate.rotY(Math.PI/5.0d); rotate.mul(tempRotate); TransformGroup objRotate = new TransformGroup(rotate); // Create a simple shape leaf node, add it to the scene graph. //ColorCube is a convenience Utility class objRotate.addChild(new ColorCube(0.4)); objRoot.addChild(objRotate); return objRoot; }

  16. Scene Graph

  17. Aside… • Do any of these names look familiar? • You have implemented methods to do these same procedures. You know how these work and why! (If you don’t, go back and review…)

  18. Adding Animation Behavior • Create a target TransformGroup • Set the ALLOW_TRANSFORM_WRITE capability • Create an Alpha object • Specify the time parameters for the alpha • Create the interpolator object • Have it reference the Alpha and TransformGroup objects • Customize the behavior parameters • Specify a scheduling region • Set the scheduling region for the behavior • Make the behavior a child of the TransformGroup

  19. Adding Animation Behavior public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Create the transform node and initialize it to the // identity. Add it to the root of the subgraph TransformGroup objSpin = new TransformGroup(); objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objSpin); // Create a simple shape leaf node, add it to the scene graph. //ColorCube is a convenience Utility class objSpin.addChild(new ColorCube(0.4)); // create time varying function to drive the animation Alpha rotationAlpha = new Alpha(-1, 4000); // Create a new Behavior object that performs the desired // operation on the specified transform object and add it into // the scene graph RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin); // a bounding sphere specifies a region a behavior is active BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); objSpin.addChild(rotator); return objRoot; }

  20. Scene Graph

  21. Transformation and Behavior Combination Example public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); Transform3D rotate = new Transform3D(); Transform3D tempRotate = new Transform3D(); rotate.rotX(Math.PI/4.0d); tempRotate.rotY(Math.PI/5.0d); rotate.mul(tempRotate); TransformGroup objRotate = new TransformGroup(rotate); // Create the transform node and initialize it to the // identity. Add it to the root of the subgraph TransformGroup objSpin = new TransformGroup(); objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); // Create a simple shape leaf node, add it to the scene graph. //ColorCube is a convenience Utility class objSpin.addChild(new ColorCube(0.4)); objRoot.addChild(objRotate); objRotate.addChild(objSpin); // Create a new Behavior object that performs the desired // operation on the specified transform object and add it into // the scene graph Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, 4000); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin, yAxis,0.0f, (float) Math.PI*2.0f); // a bounding sphere specifies a region a behavior is active BoundingSphere bounds = new BoundingSphere(); rotator.setSchedulingBounds(bounds); objSpin.addChild(rotator); return objRoot; }

  22. Scene Graph

  23. Self Test • In the HelloJava3Db program, which combines two rotations in one TransformGroup, what would be the difference if you reverse the order of the multiplication in the specification of the rotation? Alter the program to see if your answer is correct.

  24. Self Test • In the HelloJava3Dd program, what would be the difference if you reverse the order of the Transform Nodes above the ColorCube in the content branch graph? Alter the program to see if your answer is correct

  25. Self Test • Translate the ColorCube 1 unit in the Y dimension and rotate the cube. You can use HelloJava3Db as a starting point. • Try the transformation in the opposite order. Do you expect to see a difference in the results? Why or Why not?

  26. Self Test • The example programs give sufficient information for assembling a virtual universe with multiple color cubes How do you construct such a scene graph? In what part of the code would this be accomplished?

  27. For Monday… • Just as we went through the first tutorial in class, go through the second tutorial and complete the Self Test at the end (section 2.7) • Dropbox the self test into the csc308lab8 dropbox by midnight, Sunday night. We will begin part 3 of the tutorial Monday in class. You are expected to know and understand the material from part 2 and have any questions about it ready.

More Related