610 likes | 834 Views
Introduction to Java 3D . 1. Java 3D Overview. API for 3D applications Portable. Some Application Areas. Scientific/Medical/Data visualization Geographical information systems (GIS) Computer-aided design (CAD) Simulation Computer-aided education (CAE) Games. The Java 3D API.
E N D
1. Java 3D Overview • API for 3D applications • Portable
Some Application Areas • Scientific/Medical/Data visualization • Geographical information systems (GIS) • Computer-aided design (CAD) • Simulation • Computer-aided education (CAE) • Games
The Java 3D API • Packages related to the Java 3D API: • Core classes: javax.media.j3d • Utility classes: com.sun.j3d.utils • Math classes: javax.vecmath • AWT classes: javax.swing
Introduction • Elements of the rendering • Objects • Camera/View • Canvas/Screen
Introduction • Rendering • Camera / objects may move in time…
Scene Representation • Scene • Representation of elements in the virtual world • How to represent? • What to represent?
The Virtual Universe • Basics • Scene Graph • Locales • Content Branch • View Branch
2. What is a Scene Graph? • scene graph = tree-like • stores,3D scene • easier than OpenGL or DirectX.
Scene Graph Symbols Arcs (object relationships) Nodes and Node Components (objects) Group =erase head Parent-child link Leaf=cone head Node Component Reference Other objects=block head
Java3D’s SceneGraph VitualUniverse Locale Root group BG BG TG robot arm environment TG TG Group = erase head view sphere TG base S S TG bottom arm S up arm S
The Virtual UniverseObjectives • Know what a virtual universe and a scene graph is. • Understand the basic parts and elements of a scene graph. • Know the basic recipe for a Java 3D program.
A Typical Scene Graph Leaf=cone head Content Branch View Branch
The Virtual UniverseBasics • A virtual universe can be seen as a 1:1 representation of our own universe. • Represent structures from astronomical to subatomic. • Floating point 3D space: 2^256 (!!) for each x,y,z. • Decimal point for 1 meter at 2^128 • There is one VU-instance only (Singleton).
The Virtual UniverseLocales (1) • How to handle the huge extend of a virtual universe efficiently? • Our virtual universe contains at least one Locale. • The locale is a 3D-reference point inside the virtual universe.
The Virtual UniverseLocales (2) • Standard Locale resides at (0,0,0) in the VU. • We can have several Locales, eg: • One as reference point of the swiss coordinate system (located in Bordeaux, France). • A second as the architects reference point of a building plan. • The both are related, but depending on the point of view it is more convenient (and precise) to work with one or the other.
The Virtual UniverseContent Branch (1) • Contains all „visible“ objects of our scene. • Contains all transformations for those objects (displacement, animation, ...). • We distinguish between group nodes and leaf nodes (see following slide).
The Virtual UniverseContent Branch (2) • The most common object types Branch Group ... Transform Group ... Appearance Shape3D
VirtualUniverse The Virtual UniverseScene Graph (1) Locale • Scene Graph is the graphical representation of the objects in our VU. • Directed Acyclic Graph (Tree) • Nodes, arcs, parents, childs, leaves, references and other objects. Group Link Leaf Reference NodeComponent Other objects
The Virtual UniverseScene Graph (2) • A Java 3D program may have more objects than those in the scene graph. • A scene graph drawing is the correct documentation for the 3D part of a Java 3D program.
The Virtual UniverseView Branch The Canvas3D will be inserted in our application or applet -> most important!
The Virtual UniverseRecipe for a simple Program • Create a Frame & a Canvas3D instance • Create a SimpleUniverse • Construct the content branch. • Insert the content branch into the Locale of the SimpleUniverse.
Hello WorldSource Code public class HelloJava3Da extends Applet { public HelloJava3Da() { setLayout(new BorderLayout()); Canvas3D canvas3D = new Canvas3D(null); add("Center", canvas3D); SimpleUniverse simpleU = new SimpleUniverse(canvas3D); simpleU.getViewingPlatform().setNominalViewingTransform(); BranchGroup scene = createSceneGraph(); simpleU.addBranchGraph(scene); } public BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); objRoot.addChild(new ColorCube(0.4)); return objRoot; } public static void main(String[] args) { Frame frame = new MainFrame(new HelloJava3Da(), 256, 256); } }
Hello WorldWhat happens when running? while (true) { Process input Perform Behaviours //none at the moment Traverse scene graph and render visual objects if (request to exit) break } Cleanup and exit
Java3D (cont.) • The scene graph is a tree • Elements of the scene that have some data component, such asviewable objects, light sources, camera locations and so forth are contained in the graph nodes. From:“The Java 3D Tutorial” by Dennis J Bouvier atjava.sun.com/products/java-media/3D/collateral and http://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/scene_graph_basics.htm
Scene Graph Viewing Objects Canvas3D, Screen3D, View, PhysicalBody, PhysicalEnvironment. From:“The Java 3D Tutorial” by Dennis J Bouvier atjava.sun.com/products/java-media/3D/collateral and http://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/scene_graph_basics.htm
Java3D (cont.) Terminology The renderer
Code (create robot arm) • public void init() // applet’s init() function • { • setLayout(new BorderLayout()); • GraphicsConfiguration config = • SimpleUniverse.getPreferredConfiguration(); • Canvas3D c = new Canvas3D(config); • add("Center", c); • // create simple universe( camera, lights, ...) • u = new SimpleUniverse(c); • // This will move the ViewPlatform back a bit so the • // objects in the scene can be viewed. • u.getViewingPlatform().setNominalViewingTransform(); • viewTrans = • u.getViewingPlatform().getViewPlatformTransform(); • // Create a robot arm • createSceneGraph(); • // add root group • u.addBranchGraph(rootGroup); • }
public void createSceneGraph() • { • // create root • rootGroup = new BranchGroup(); • rootTrans = new TransformGroup(); • rootTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); • rootTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); • // create bounds • BoundingSphere bounds = • new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); • // create Appearance • Appearance greenLook = new Appearance(); • ColoringAttributes greenColorAttr = new ColoringAttributes(); • greenColorAttr.setColor(new Color3f(0.0f,1.0f,0.0f)); • greenLook.setColoringAttributes(greenColorAttr); • // create Sphere • sphereTrans = new TransformGroup(); • sphere = new Sphere(0.1f,redLook);
// transform sphere • Transform3D sphereT3d = new Transform3D(); • sphereT3d.setTranslation(new Vector3f(0.5f,0.0f,0.0f)); • sphereTrans.setTransform(sphereT3d); • // add sphere into scenegraph • sphereTrans.addChild(sphere); • rootTrans.addChild(sphereTrans); • // create arm • ….. • } // end of createSceneGraph()
3. A Java 3D Skeleton • All our Java 3D programs embed a scene inside a JPanel, which is part of a JFrame • this allows Swing GUI controls to be utilized along side the Java 3D panel (if necessary) • We will develop an example called Checkers3D during the course of these slides.
Checkers3D • The scene consists of • a dark green and blue tiled surface (and red center) • labels along the X and Z axes • a blue background • a floating sphere lit from two different directions • the user (viewer) can move through the scene by moving the mouse
Checkers3D.java public class Checkers3D extends JFrame{ public Checkers3D() { super("Checkers3D"); Container c = getContentPane(); c.setLayout( new BorderLayout() );WrapCheckers3D w3d = new WrapCheckers3D(); // panel holding the 3D scene c.add(w3d, BorderLayout.CENTER); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); pack(); setResizable(false); // fixed size display show(); } // end of Checkers3D() public static void main(String[] args) { new Checkers3D(); }} // end of Checkers3D class
Scene Graph for Checkers3D view branch not shown
Building the Scene Graph • The VirtualUniverse, Locale, and view branch graph often have the same structure across different applications • programmers usually create them with the SimpleUniverse utility class
WrapChecker3D Constructor private SimpleUniverse su; private BranchGroup sceneBG; // for content branch private BoundingSphere bounds; : public WrapCheckers3D() { setLayout( new BorderLayout() ); setOpaque( false ); setPreferredSize( new Dimension(PWIDTH, PHEIGHT));GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas3D = new Canvas3D(config); add("Center", canvas3D); canvas3D.setFocusable(true); canvas3D.requestFocus();su = new SimpleUniverse(canvas3D); :
createSceneGraph(); initUserPosition(); // set user's viewpoint orbitControls(canvas3D); // controls for moving the viewpoint su.addBranchGraph( sceneBG ); } // end of WrapCheckers3D()
createSceneGraph() private void createSceneGraph() // initilise the scene below sceneBG { sceneBG = new BranchGroup(); bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE); lightScene(); // add the light addBackground(); // add the sky sceneBG.addChild( new CheckerFloor().getBG() ); // add the floor floatingSphere(); // add the floating sphere sceneBG.compile(); // fix the scene } // end of createSceneGraph()
1. Checkers3D Again • The scene consists of • a dark green and blue tiled surface (and red center) • labels along the X and Z axes • a blue background • a floating sphere lit from two different directions • the user (viewer) can move through the scene by moving the mouse
Scene Graph for Checkers3D view branch not shown
WrapChecker3D Constructor private SimpleUniverse su; private BranchGroup sceneBG; // for content branch private BoundingSphere bounds; : public WrapCheckers3D() { setLayout( new BorderLayout() ); setOpaque( false ); setPreferredSize( new Dimension(PWIDTH, PHEIGHT)); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas3D = new Canvas3D(config); add("Center", canvas3D); canvas3D.setFocusable(true); canvas3D.requestFocus(); su = new SimpleUniverse(canvas3D); :
createSceneGraph(); initUserPosition(); // set user's viewpoint orbitControls(canvas3D); // controls for moving the viewpoint su.addBranchGraph( sceneBG ); } // end of WrapCheckers3D()
createSceneGraph() private void createSceneGraph() // initilise the scene below sceneBG { sceneBG = new BranchGroup(); bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE); lightScene(); // add the light addBackground(); // add the sky sceneBG.addChild( new CheckerFloor().getBG() ); // add the BranchGroup for the floor floatingSphere(); // add the floating sphere sceneBG.compile(); // fix the scene } // end of createSceneGraph()
2. The Floor • The floor is made of tiles created with the our ColouredTiles class, and axis labels made with the Text2D utility class.
Floor Branch of the Scene Graph floorBG axisTG
CheckerFloor Constructor // constants for various colours : private BranchGroup floorBG; public CheckerFloor() // create tiles, add origin marker, // then the axes labels { ArrayList blueCoords = new ArrayList() ArrayList greenCoords = new ArrayList(); floorBG = new BranchGroup(); :