Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Java 3D. 2 1. Java 3D Overview v API for 3D applications v Portable.

Similar presentations


Presentation on theme: "1 Introduction to Java 3D. 2 1. Java 3D Overview v API for 3D applications v Portable."— Presentation transcript:

1 1 Introduction to Java 3D

2 2 1. Java 3D Overview v API for 3D applications v Portable

3 3 Some Application Areas v Scientific/Medical/Data visualization v Geographical information systems (GIS) v Computer-aided design (CAD) v Simulation v Computer-aided education (CAE) v Games

4 4 The Java 3D API v 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

5 5 Introduction v Elements of the rendering –Objects –Camera/View –Canvas/Screen

6 6 Introduction v Rendering v Camera / objects may move in time…

7 7 Scene Representation v Scene –Representation of elements in the virtual world –How to represent? –What to represent?

8 8 The Virtual Universe v Basics v Scene Graph v Locales v Content Branch v View Branch

9 9 2. What is a Scene Graph? v scene graph = tree-like v stores,3D scene v easier than OpenGL or DirectX.

10 10 Scene Graph Symbols Arcs (object relationships) Group =erase head Leaf=cone head Node Component Other objects=block head Parent-child link Reference Nodes and Node Components (objects)

11 11 Java3D’s SceneGraph S BG TG S S S Locale VitualUniverse view sphere base bottom arm up arm robot armenvironment BG Root group TG Group = erase head

12 12 The Virtual Universe Objectives v Know what a virtual universe and a scene graph is. v Understand the basic parts and elements of a scene graph. v Know the basic recipe for a Java 3D program.

13 13 A Typical Scene Graph Content BranchView Branch Leaf=cone head

14 14 The Virtual Universe Basics v A virtual universe can be seen as a 1:1 representation of our own universe. v 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).

15 15 The Virtual Universe Locales (1) v How to handle the huge extend of a virtual universe efficiently? v Our virtual universe contains at least one Locale. v The locale is a 3D-reference point inside the virtual universe.

16 16 The Virtual Universe Locales (2) v Standard Locale resides at (0,0,0) in the VU. v 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.

17 17 The Virtual Universe Content Branch (1) v Contains all „visible“ objects of our scene. v Contains all transformations for those objects (displacement, animation,...). v We distinguish between group nodes and leaf nodes (see following slide).

18 18 The Virtual Universe Content Branch (2) v The most common object types Branch Group Shape3D Appearance Transform Group...

19 19 The Virtual Universe Scene Graph (1) v Scene Graph is the graphical representation of the objects in our VU. v Directed Acyclic Graph (Tree) –Nodes, arcs, parents, childs, leaves, references and other objects. VirtualUniverse Locale Group Leaf NodeComponent Other objects Reference Link

20 20 The Virtual Universe Scene Graph (2) v A Java 3D program may have more objects than those in the scene graph. v A scene graph drawing is the correct documentation for the 3D part of a Java 3D program.

21 21 The Virtual Universe View Branch The Canvas3D will be inserted in our application or applet -> most important!

22 22 The Virtual Universe Recipe for a simple Program 1. Create a Frame & a Canvas3D instance 2. Create a SimpleUniverse 3. Construct the content branch. 4. Insert the content branch into the Locale of the SimpleUniverse.

23 23 Hello World

24 24 Hello World Source Code public class HelloJava3Da extends Applet { public HelloJava3Da() { public HelloJava3Da() { setLayout(new BorderLayout()); setLayout(new BorderLayout()); Canvas3D canvas3D = new Canvas3D(null); Canvas3D canvas3D = new Canvas3D(null); add("Center", canvas3D); add("Center", canvas3D); SimpleUniverse simpleU = new SimpleUniverse(canvas3D); SimpleUniverse simpleU = new SimpleUniverse(canvas3D); simpleU.getViewingPlatform().setNominalViewingTransform(); simpleU.getViewingPlatform().setNominalViewingTransform(); BranchGroup scene = createSceneGraph(); BranchGroup scene = createSceneGraph(); simpleU.addBranchGraph(scene); simpleU.addBranchGraph(scene); } public BranchGroup createSceneGraph() { public BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); BranchGroup objRoot = new BranchGroup(); objRoot.addChild(new ColorCube(0.4)); objRoot.addChild(new ColorCube(0.4)); return objRoot; return objRoot; } public static void main(String[] args) { public static void main(String[] args) { Frame frame = new MainFrame(new HelloJava3Da(), 256, 256); Frame frame = new MainFrame(new HelloJava3Da(), 256, 256); }}

25 25 Hello World Class Diagram of HelloJava3D

26 26 Hello World What 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

27 27 Scene Graph

28 28 Java3D (cont.) From: “The Java 3D Tutorial” by Dennis J Bouvier at java.sun.com/products/java-media/3D/collateraljava.sun.com/products/java-media/3D/collateral and http://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/scene_graph_basics.htmhttp://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/scene_graph_basics.htm The scene graph is a tree Elements of the scene that have some data component, such as viewable objects, light sources, camera locations and so forth are contained in the graph nodes.

29 29 Virtual Universe (V) Locale (L) Group (B or T) Leaf (Lf) Node Component (N) Other Objects Symbols representing Scene Graph Objects:

30 30 From: “The Java 3D Tutorial” by Dennis J Bouvier at java.sun.com/products/java-media/3D/collateraljava.sun.com/products/java-media/3D/collateral and http://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/scene_graph_basics.htmhttp://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/scene_graph_basics.htm Canvas3D, Screen3D, View, PhysicalBody, PhysicalEnvironment. Scene Graph Viewing Objects

31 31 Java3D (cont.) TerminologyThe renderer

32 32 Code (create robot arm) v public void init() // applet’s init() function v { v setLayout(new BorderLayout()); v GraphicsConfiguration config = v SimpleUniverse.getPreferredConfiguration(); v Canvas3D c = new Canvas3D(config); v add("Center", c); v v // create simple universe( camera, lights,...) v u = new SimpleUniverse(c); v // This will move the ViewPlatform back a bit so the v // objects in the scene can be viewed. v u.getViewingPlatform().setNominalViewingTransform(); v viewTrans = v u.getViewingPlatform().getViewPlatformTransform(); v // Create a robot arm v createSceneGraph(); v // add root group v u.addBranchGraph(rootGroup); v }

33 33 v public void createSceneGraph() v { v // create root v rootGroup = new BranchGroup(); v rootTrans = new TransformGroup(); v rootTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); v rootTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); v // create bounds v BoundingSphere bounds = v new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); v // create Appearance v Appearance greenLook = new Appearance(); v ColoringAttributes greenColorAttr = new ColoringAttributes(); v greenColorAttr.setColor(new Color3f(0.0f,1.0f,0.0f)); v greenLook.setColoringAttributes(greenColorAttr); v v // create Sphere v sphereTrans = new TransformGroup(); v sphere = new Sphere(0.1f,redLook); v

34 34 v // transform sphere v Transform3D sphereT3d = new Transform3D(); v sphereT3d.setTranslation(new Vector3f(0.5f,0.0f,0.0f)); v sphereTrans.setTransform(sphereT3d); v v // add sphere into scenegraph v sphereTrans.addChild(sphere); v rootTrans.addChild(sphereTrans); v v // create arm v ….. v } // end of createSceneGraph()

35 35 3. A Java 3D Skeleton  All our Java 3D programs embed a scene inside a, which is part of a  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 during the course of these slides.  We will develop an example called Checkers3D during the course of these slides.

36 36 Checkers3D v v 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

37 37 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

38 38 Scene Graph for Checkers3D view branch not shown

39 39 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

40 40 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); :

41 41 createSceneGraph(); initUserPosition(); // set user's viewpoint orbitControls(canvas3D); // controls for moving the viewpoint su.addBranchGraph( sceneBG ); } // end of WrapCheckers3D()

42 42 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()

43 43 1. Checkers3D Again v v 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

44 44 Scene Graph for Checkers3D view branch not shown

45 45 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); :

46 46 createSceneGraph(); initUserPosition(); // set user's viewpoint orbitControls(canvas3D); // controls for moving the viewpoint su.addBranchGraph( sceneBG ); } // end of WrapCheckers3D()

47 47 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()

48 48 2. The Floor  The floor is made of tiles created with the our class, and axis labels made with the utility class.  The floor is made of tiles created with the our ColouredTiles class, and axis labels made with the Text2D utility class.

49 49 Floor Branch of the Scene Graph floorBG axisTG

50 50 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(); :

51 51 boolean isBlue; for(int z = -FLOOR_LEN/2; z <= (FLOOR_LEN/2)-1; z++) { isBlue = (z%2 == 0)? true : false; // set colour for new row for(int x = -FLOOR_LEN/2; x <= (FLOOR_LEN/2)-1; x++) { if (isBlue) createCoords(x, z, blueCoords); else createCoords(x, z, greenCoords); isBlue = !isBlue; } } :

52 52 floorBG.addChild( new ColouredTiles(blueCoords, blue) ); floorBG.addChild( new ColouredTiles(greenCoords, green) ); addOriginMarker(); labelAxes(); } // end of CheckerFloor() public BranchGroup getBG() { return floorBG; }

53 53 private void createCoords(int x, int z, ArrayList coords) // Coords for a single blue or green square, // its left hand corner at (x,0,z) { // points created in counter-clockwise order Point3f p1 = new Point3f(x, 0.0f, z+1.0f); Point3f p2 = new Point3f(x+1.0f,0.0f,z+1.0f); Point3f p3 = new Point3f(x+1.0f, 0.0f, z); Point3f p4 = new Point3f(x, 0.0f, z); coords.add(p1); coords.add(p2); coords.add(p3); coords.add(p4); } // end of createCoords() x z + + p1 p2 p3 p4

54 54 ColouredTiles Class  The class extends, and defines the geometry and appearance of tiles with the same colour.  The ColouredTiles class extends Shape3D, and defines the geometry and appearance of tiles with the same colour.  The geometry uses a to represent the tiles as a series of quadlilaterals.  The geometry uses a QuadArray to represent the tiles as a series of quadlilaterals. v1 v4v3 v2v5 v8v7 v6

55 55 QuadArray Creation v The constructor is: QuadArray(int vertexCount, int vertexFormat);  In, the plane is created using:  In ColouredTiles, the QuadArray plane is created using: plane = new QuadArray( coords.size(), GeometryArray.COORDINATES | GeometryArray.COLOR_3 );

56 56 Filling the QuadArray // coordinate data int numPoints = coords.size(); Point3f[] points = new Point3f[numPoints]; coords.toArray( points ); // ArrayList-->array plane.setCoordinates(0, points); // colour data Color3f cols[] = new Color3f[numPoints]; for(int i=0; i < numPoints; i++) cols[i] = col; plane.setColors(0, cols);

57 57 Issues v Counter-clockwise specification of the vertices for each quad –makes the top of the quad its 'front' v Ensure that each quad is a convex, planar polygon. v Normals or no normals?

58 58 Unreflecting Colour v v You can specify a shape's colour in three ways: – –in the shape's material u u when the scene is illuminated – –in the shape's colouring attributes u u used when the shape is unreflecting – –in the vertices of the shape's geometry u u also for unreflecting shapes (used here)

59 59 The Axes  Each axis value is a object, which specifies the string, colour, font, point size, and font style:  Each axis value is a Text2D object, which specifies the string, colour, font, point size, and font style: Text2D message = new Text2D("...", white, "SansSerif", 36, Font.BOLD ); // 36 point bold Sans Serif

60 60 Positioning an Axis Value TransformGroup axisTG = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setTranslation( vertex ); // vertex is the position for the label axisTG.setTransform(t3d); axisTG.addChild( message );

61 61 Create a color cube v Prompt the user for an image, list unique colors in the image and then v Create a sphere for each color such that: –The sphere has that color –The sphere is positioned in 3space according to its color –The color space is an RGB space.


Download ppt "1 Introduction to Java 3D. 2 1. Java 3D Overview v API for 3D applications v Portable."

Similar presentations


Ads by Google