1 introduction to java 3d. 2 1. java 3d overview v api for 3d applications v portable

Post on 25-Dec-2015

234 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Introduction to Java 3D Introduction to Java 3D

2

1. Java 3D Overview1. Java 3D Overview API for 3D applicationsAPI for 3D applications PortablePortable

3

Some Application AreasSome Application Areas

Scientific/Medical/Data visualizationScientific/Medical/Data visualization Geographical information systems (GIS)Geographical information systems (GIS) Computer-aided design (CAD)Computer-aided design (CAD) SimulationSimulation Computer-aided education (CAE)Computer-aided education (CAE) GamesGames

4

The Java 3D APIThe Java 3D API

Packages related to the Java 3D API:Packages related to the Java 3D API:– Core classes: Core classes: javax.media.j3djavax.media.j3d

– Utility classes:Utility classes: com.sun.j3d.utilscom.sun.j3d.utils

– Math classes:Math classes: javax.vecmathjavax.vecmath

– AWT classes:AWT classes: javax.swingjavax.swing

5

IntroductionIntroduction

Elements of the renderingElements of the rendering– ObjectsObjects– Camera/ViewCamera/View– Canvas/ScreenCanvas/Screen

6

IntroductionIntroduction

RenderingRendering

Camera / objects may move in time…Camera / objects may move in time…

7

Scene RepresentationScene Representation

SceneScene– Representation of elements in the virtual worldRepresentation of elements in the virtual world– How to represent?How to represent?– What to represent?What to represent?

8

The Virtual UniverseThe Virtual Universe

BasicsBasics Scene Graph Scene Graph LocalesLocales Content BranchContent Branch View BranchView Branch

9

2. What is a Scene Graph?2. What is a Scene Graph?

scene graphscene graph = tree-like = tree-like stores,3D scenestores,3D scene easier than OpenGL or DirectX.easier than OpenGL or DirectX.

10

Scene Graph SymbolsScene 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

Java3D’s SceneGraphJava3D’s SceneGraph

S

BG

TG

TG

TG

S

S

S

TG

Locale

VitualUniverse

viewspherebase

bottom arm

up arm

robot arm environmentBG

Root group

TG

Group =erase head

12

The Virtual UniverseThe Virtual Universe

ObjectivesObjectives

Know what a virtual universe and a scene Know what a virtual universe and a scene graph is.graph is.

Understand the basic parts and elements of Understand the basic parts and elements of a scene graph.a scene graph.

Know the basic recipe for a Java 3D Know the basic recipe for a Java 3D program.program.

13

A Typical Scene Graph

Content Branch View Branch

Leaf=cone head

14

The Virtual UniverseThe Virtual Universe

BasicsBasics

A virtual universe can be seen as a 1:1 A virtual universe can be seen as a 1:1 representation of our own universe.representation of our own universe.

Represent structures from astronomical to Represent structures from astronomical to subatomic. subatomic. – Floating point 3D space: 2^256 (!!) for each Floating point 3D space: 2^256 (!!) for each

x,y,z.x,y,z.– Decimal point for 1 meter at 2^128 Decimal point for 1 meter at 2^128 – There is one VU-instance only (Singleton).There is one VU-instance only (Singleton).

15

The Virtual UniverseThe Virtual Universe

Locales (1)Locales (1)

How to handle the huge extend of a virtual How to handle the huge extend of a virtual universe efficiently?universe efficiently?

Our virtual universe contains at least one Our virtual universe contains at least one Locale. Locale.

The locale is a 3D-reference point inside the The locale is a 3D-reference point inside the virtual universe.virtual universe.

16

The Virtual UniverseThe Virtual Universe

Locales (2)Locales (2)

Standard Locale resides at (0,0,0) in the VU.Standard Locale resides at (0,0,0) in the VU. We can have several Locales, eg:We can have several Locales, eg:

– One as reference point of the swiss coordinate system One as reference point of the swiss coordinate system (located in Bordeaux, France).(located in Bordeaux, France).

– A second as the architects reference point of a building A second as the architects reference point of a building plan.plan.

– The both are related, but depending on the point of The both are related, but depending on the point of view it is more convenient (and precise) to work with view it is more convenient (and precise) to work with one or the other.one or the other.

17

The Virtual UniverseThe Virtual Universe

Content Branch (1)Content Branch (1)

Contains all „visible“ objects of our scene. Contains all „visible“ objects of our scene. Contains all transformations for those Contains all transformations for those

objects (displacement, animation, ...).objects (displacement, animation, ...). We distinguish between group nodes and We distinguish between group nodes and

leaf nodes (see following slide).leaf nodes (see following slide).

18

The Virtual UniverseThe Virtual Universe

Content Branch (2)Content Branch (2)

The most common The most common object types object types

BranchGroup

Shape3DAppearance

TransformGroup

...

...

19

The Virtual UniverseThe Virtual Universe

Scene Graph (1)Scene Graph (1)

Scene Graph is the graphical Scene Graph is the graphical representation of the objects in representation of the objects in our VU.our VU.

Directed Acyclic Graph (Tree)Directed Acyclic Graph (Tree)– Nodes, arcs, parents, childs, Nodes, arcs, parents, childs,

leaves, references and other leaves, references and other objects.objects.

VirtualUniverse

Locale

Group

Leaf

NodeComponent Other objects

Reference

Link

20

The Virtual UniverseThe Virtual Universe

Scene Graph (2)Scene Graph (2)

A Java 3D program may have more objects A Java 3D program may have more objects than those in the scene graph.than those in the scene graph.

A scene graph drawing is the correct A scene graph drawing is the correct documentation for the 3D part of a documentation for the 3D part of a Java 3D program.Java 3D program.

21

The Virtual UniverseThe Virtual Universe

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

22

The Virtual UniverseThe Virtual Universe

Recipe for a simple ProgramRecipe for a simple Program

1.1. Create a Frame & a Create a Frame & a Canvas3D instanceCanvas3D instance

2.2. Create a SimpleUniverse Create a SimpleUniverse

3.3. Construct the content branch.Construct the content branch.

4.4. Insert the content branch into the Locale Insert the content branch into the Locale of the SimpleUniverse.of the SimpleUniverse.

23

Hello WorldHello World

24

Hello WorldHello World

Source CodeSource Codepublic class HelloJava3Da extends Applet {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

Hello WorldHello World

Class Diagram of HelloJava3DClass Diagram of HelloJava3D

26

Hello WorldHello World

What happens when running?What happens when running?

while (true) {while (true) {

Process inputProcess input

Perform Behaviours //none at the momentPerform Behaviours //none at the moment

Traverse scene graph and render visual objectsTraverse scene graph and render visual objects

if (request to exit) breakif (request to exit) break

}}

Cleanup and exitCleanup and exit

27

Scene GraphScene Graph

28

Java3D Java3D (cont.)(cont.)

From: “The Java 3D Tutorial” by Dennis J Bouvier at java.sun.com/products/java-media/3D/collateral and http://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

Virtual Universe (V) Virtual Universe (V)

Locale (L) Locale (L)

Group (B or T) Group (B or T)

Leaf (Lf) Leaf (Lf)

Node Component (N) Node Component (N)

Other Objects Other Objects

Symbols representing Scene Graph Objects:

30From: “The Java 3D Tutorial” by Dennis J Bouvier at java.sun.com/products/java-media/3D/collateral and http://www.itk.ilstu.edu/faculty/javila/ITk356/Java3D/scene_graph_basics.htm

Canvas3D, Screen3D, View, PhysicalBody, PhysicalEnvironment.

Scene Graph Viewing ObjectsScene Graph Viewing Objects

31

Java3D Java3D (cont.)(cont.)

Terminology The renderer

32

Code (create robot arm)Code (create robot arm) public void init() // applet’s init() functionpublic void init() // applet’s init() function {{ setLayout(new BorderLayout());setLayout(new BorderLayout()); GraphicsConfiguration config =GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();SimpleUniverse.getPreferredConfiguration();

Canvas3D c = new Canvas3D(config);Canvas3D c = new Canvas3D(config); add("Center", c);add("Center", c); // create simple universe( camera, lights, ...)// create simple universe( camera, lights, ...) u = new SimpleUniverse(c);u = new SimpleUniverse(c);

// This will move the ViewPlatform back a bit so the// This will move the ViewPlatform back a bit so the // objects in the scene can be viewed.// objects in the scene can be viewed. u.getViewingPlatform().setNominalViewingTransform();u.getViewingPlatform().setNominalViewingTransform();

viewTrans =viewTrans = u.getViewingPlatform().getViewPlatformTransform();u.getViewingPlatform().getViewPlatformTransform(); // // Create a robot armCreate a robot arm createSceneGraph();createSceneGraph(); // // add root groupadd root group u.addBranchGraph(rootGroup);u.addBranchGraph(rootGroup); }}

33

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

34

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

35

3. A Java 3D Skeleton3. A Java 3D Skeleton

All our Java 3D programs embed a scene inAll our Java 3D programs embed a scene inside a side a JPanel, which is part of a , which is part of a JFrame– this allows Swing GUI controls to be utilized althis allows Swing GUI controls to be utilized al

ong side the Java 3D panel (if necessary)ong side the Java 3D panel (if necessary)

We will develop an example called We will develop an example called Checkers3D during the course of these slides. during the course of these slides.

36

Checkers3DCheckers3D

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

Checkers3D.javaCheckers3D.javapublic 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

Scene Graph for Checkers3DScene Graph for Checkers3D

view branchnot shown

39

Building the Scene GraphBuilding 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

WrapChecker3D ConstructorWrapChecker3D 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

createSceneGraph(); initUserPosition(); // set user's viewpoint

orbitControls(canvas3D); // controls for moving the viewpoint su.addBranchGraph( sceneBG );

} // end of WrapCheckers3D()

42

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

1. Checkers3D Again1. 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

44

Scene Graph for Checkers3DScene Graph for Checkers3D

view branchnot shown

45

WrapChecker3D ConstructorWrapChecker3D 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

createSceneGraph(); initUserPosition(); // set user's viewpoint

orbitControls(canvas3D); // controls for moving the viewpoint su.addBranchGraph( sceneBG );

} // end of WrapCheckers3D()

47

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

2. The Floor 2. The Floor

The floor is made of tiles created with the oThe floor is made of tiles created with the our ur ColouredTiles class, and axis labels mad class, and axis labels made with the e with the Text2D utility class. utility class.

49

Floor Branch of the Scene GraphFloor Branch of the Scene Graph

floorBG

axisTG

50

CheckerFloor ConstructorCheckerFloor 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

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

floorBG.addChild( new ColouredTiles(blueCoords, blue) ); floorBG.addChild( new ColouredTiles(greenCoords, green) );

addOriginMarker(); labelAxes(); } // end of CheckerFloor()

public BranchGroup getBG() { return floorBG; }

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

p3p4

54

ColouredTiles ClassColouredTiles Class

The The ColouredTiles class extends class extends Shape3D, a, and defines the geometry and appearance of tnd defines the geometry and appearance of tileiless with the same colour. with the same colour.

The geometry uses a The geometry uses a QuadArray to represent to represent the tiles as a series of quadlilaterals. the tiles as a series of quadlilaterals.

v1

v4 v3

v2 v5

v8 v7

v6

55

QuadArray CreationQuadArray Creation

The constructor is:The constructor is:QuadArray(int vertexCount, int vertexFormat);

In In ColouredTiles, the , the QuadArray plane is created plane is created using:using:

plane = new QuadArray( coords.size(), GeometryArray.COORDINATES | GeometryArray.COLOR_3 );

56

Filling the QuadArrayFilling 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

IssuesIssues

Counter-clockwise specification of the vertiCounter-clockwise specification of the vertices for each quadces for each quad– makes the top of the quad its 'front'makes the top of the quad its 'front'

Ensure that each quad is a convex, planar pEnsure that each quad is a convex, planar polygon.olygon.

Normals or no normals?Normals or no normals?

58

Unreflecting ColourUnreflecting Colour

You can specify a shape's colour in three ways: – in the shape's material

when the scene is illuminated

– in the shape's colouring attributes used when the shape is unreflecting

– in the vertices of the shape's geometry also for unreflecting shapes (used here)

59

The AxesThe Axes

Each axis value is Each axis value is a a Text2D object, which specifies object, which specifies the string, colour, font, point size, and font style:the string, colour, font, point size, and font style:

Text2D message = new Text2D("...", white, "SansSerif", 36, Font.BOLD ); // 36 point bold Sans Serif

60

Positioning an Axis ValuePositioning an Axis Value

TransformGroup axisTG = new TransformGroup();Transform3D t3d = new Transform3D();t3d.setTranslation( vertex ); // vertex is the position for the labelaxisTG.setTransform(t3d);

axisTG.addChild( message );

61

Create a color cubeCreate a color cube

Prompt the user for an image, list unique Prompt the user for an image, list unique colors in the image and thencolors in the image and then

Create a sphere for each color such that:Create a sphere for each color such that:– The sphere has that colorThe sphere has that color– The sphere is positioned in 3space according to The sphere is positioned in 3space according to

its colorits color– The color space is an RGB space.The color space is an RGB space.

top related