33
Einführung in Java3D SS05 Ashraf Abu Baker [email protected] Varrentrappstr. 40-42 60486 Frankfurt am Main Raum 218

Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

Einführung in Java3D

SS05

Ashraf Abu Baker

[email protected]

Varrentrappstr. 40-42

60486 Frankfurt am Main

Raum 218

Page 2: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

Java 3D ist eine high level Interaktive 3D Graphik-API(Application Programming Interface) für die Erstellung von 3D-Applikationen und -Applets in Java

- Entwicklung medizinischer Anwendungen

-Visualisieren von wissenschaftlichen Daten

Page 3: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

- Technische Modelierung

- Animationen

Page 4: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

Installation der Java3D-Umgebung

- JDK Java Development Kit

http://java.sun.com/j2se/

- Java3D SDK

OpenGL-SDK java3d-1_3_1-windows-i586-opengl-sdk.exe

oder DirectX-SDK java3d-1_3_1-windows-i586-directx-sdk.exe http://java.sun.com/products/java-media/3D/download.html

- Bei der Installation: das JDK-Verzeichnis als Zielverzeichnis angeben

- Für weitere Installationshinweise siehe http://java.sun.com/products/java-media/3D/java3d-install.html

Page 5: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

- Dokumentation

J3D-Doc:http://java.sun.com/products/java-media/3D/download.html

J3D-Sun-Tutorial:http://java.sun.com/products/java-media/3D/collateral/

Java 3D API Spezifikation

http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_3_API/j3dguide/index.html

Weitere Tutorials:

http://www.sdsc.edu/~nadeau/Courses/Siggraph99/http://www.willamette.edu/~gorr/classes/cs445/lectures/Java_Overviewhttp://www.micg.et.fh-stralsund.de/Java3D/java3D.htm

Literatur:Java3D Programming von Daniel Selman

Page 6: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen
Page 7: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

- Ein Java3D-Programm besteht aus einer Menge von Objekten der Java3D-Klassen

BranchGroup, TransformationsGroup, Shape3D, Geometry, Apperance, Light, Behavior, Texture,Fog, Sound…….

- Die Gesamtheit aller Objekte eines Programms beschreibt eine virtuelle Welt (Virtual Universe)

- Eine Virtuelle Welt wird als ein sog. Szenegraph dargestellt

Page 8: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

- Ein Szenegraph weist eine Baumstruktur und besteht aus 2 Arten von Knoten (Szenegraphobjekte)

- In der Regel besteht eine virtuelle Welt aus einem einzigen Szenegraphen, kann jedoch aus mehreren bestehen

- Jeder Knote entspricht genau einer Instanz einerJava3D-Klasse

Page 9: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

- 2 Arten von Knotentypen:

LeafNodes: Blätter des Baumes

Beispiele:Shape3D, Light, Background, Sound, Behavior, etc.

GroupeNodes: inneren Knoten des Baumes

Beispiele:

BranchGroup, TransfromGroup, Primitive, etc.

Page 10: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

- GroupeNodes und LeafNodes stehen in einer Vater-Kind-Beziehung (parent child relationship) zueinander

- NodeComponents werden von den anderen Szenegraphknoten referenziert und stehen somit in einer Referenzbeziehung zu denen

- Eine dritte Art von Knoten, die im Szenegraphen dargestellt werden, jedoch nicht zum ihm gehören sind:

NodeComponents: legen die Struktur (Geometrie) und dasErscheinungsbild (Appearance) der visuellen Objekte des Szenegraphen fest

Beispiele:Geometry, Appearance, Attributes, Texture, Material

Page 11: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen
Page 12: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen
Page 13: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

- Aufteilung eines Szenegraphen in:

Content Branch Graphs und View Branch Graph

- Szenegraph besteht aus einem oder mehrere Content Branch

Graphs, einem View Branch Graph und einem Locale-Objekt

Page 14: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

- Ein Locale-Objekt bildet die Wurzel des Szenengraphen und fungiert als Referenzpunkt für die Objekte in der virtuellen Welt und wird mit einem virtuellen Koordinatensystem assoziiert

-Der content branch graph spezifiziert die Geomentry, das

Erscheinungsbild, die Lichtquellen, das Verhalten, die

Position der visuellen Objekte in der virtuellen Welt

-Der view branch graph spezifiziert die Kameraparameter

(Position, Orientierung,…etc)

Page 15: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

Rezept für die Erstellung von Java3D-Programmen

1. Erzeuge ein Canvas3D-Objekt2. Erstelle ein VirtualUniverse-Objekt3. Erzeuge ein Locale-Objekt4. Erstelle einen View Branch Graph, der das

Canvas3d referenziert5. Erstelle einen oder mehrere Content Graphs6. Kompiliere den Content Branch Graph7. Füge den Content Graphs und den View Graph in die Locale ein

- Die Schritte 2-4 können auf einmal erledigt werden, wenn man ein Objekt der Klasse SimpleUniverse instanziiert

- Ein Objekt der Klasse SimpleUniverse enthält ein VirtualUniverse, Locale-Objekt und View Brach Graph

Page 16: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

Erstes Beispiel HelloJava3D1

import javax.swing.JApplet;import javax.swing.JFrame;

//Java3D Core Packageimport javax.media.j3d.BranchGroup;import javax.media.j3d.Canvas3D;import javax.vecmath.*;

//Java3D Utility Packageimport com.sun.j3d.utils.universe.SimpleUniverse; import com.sun.j3d.utils.geometry.ColorCube;

public class HelloJava3D1 extends JApplet {

public HelloJava3D1() {

Page 17: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

//1. Create a canvas3d objectCanvas3D canvas3D=

new Canvas3D(SimpleUniverse.getPreferredConfiguration());getContentPane().add(canvas3D);

//2. Create SimpleUniverse object which references the canvas3d objectSimpleUniverse simpleU = new SimpleUniverse(canvas3D);

// This will move the ViewPlatform back a bit so that the// camera is positioned at point (0,0,2.41)

simpleU.getViewingPlatform().setNominalViewingTransform();

// 3. Create Content Graph

BranchGroup contentGraph = createContentGraph();

// 4.Compile the content graphcontentGraph.compile();

// 5. Insert the content graph into the Locale of theSimpleUniverse and make it live

simpleU.addBranchGraph(contentGraph);

} // end of HelloJava3D1 (constructor)

Page 18: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

public BranchGroup createContentGraph() {

// Create the root of the content branch graphBranchGroup root = new BranchGroup();

// add visual objects

root.addChild(new Pyramid());//root.addChild(new ColorCube(0.2));

return root;

} // end of CreateSceneGraph method of HelloJava3D1

// The following allows this to be run as an application// as well as an applet

public static void main(String[] args) {JFrame frame = new JFrame();frame.getContentPane().add(new HelloJava3D1());frame.setSize(400,400);frame.setTitle("HelloJava3D1");frame.show();

} // end of main (method of HelloJava3D1)} // end of class HelloJava3D1

Page 19: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen
Page 20: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

//2. Create SimpleUniverse object which references thecanvas3d object

SimpleUniverse simpleU = newSimpleUniverse(canvas3D);

// This will move the ViewPlatform back a bit so thatthe camera is positioned at point (0,0,2.41)

simpleU.getViewingPlatform().setNominalViewingTransform();

- Das SimpleUniverse-Objekt besteht aus :dreidimensionaler virtuellen Universe (Locale)

Image Plage der Dimension 2x2 und Mittelpunkt (0,0,0) und einerKamera, die im Punkt (0,0,2.41) positioniert ist

Page 21: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen
Page 22: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

2. Beispiel

- Das Einfügen eines ColorCube-Objekts

root.addChild(new Pyramid());root.addChild(new ColorCubes(.2));

Page 23: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen
Page 24: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

3D-Transformationen

- Transform3D ist eine Klasse, die eine Transformationsmatrix speichert (Rotation, Translation, Scaling, Scharing oder eine Kompination von Transformationen)

- Wichtigsten Methoden:void rotX(double angle)void rotY(double angle)void rotZ(double angle)void set(Vector3f translate)

Page 25: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

Beispiel 2

- Verschiebung der Pyramide um 0,5 Einheiten in die positive x-Richtung

- Verschiebung des ColorCubes um 0,5 Einheiten in die negative x-Richtung

- Nur eine Änderung des Content-Graphs erforderlich

Page 26: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

public BranchGroup createContentGraph() { // Create the root of the content branch graph

BranchGroup root = new BranchGroup();

// create a transformation matrix to translate the pyramidTransform3D translatePyramid=new Transform3D();

// a translation of .5 unit towards the positive x-axistranslatePyramid.set(new Vector3f(0.5f,0,0));

// create a transformation matrix to translate the cubeTransform3D translateCube=new Transform3D();

// a translation of .5 unit towards the negative x-axistranslateCube.set(new Vector3f(-0.5f,0,0));

// create a transform group for the pyramid which refrences thetranslation matrix translatePyramidTransformGroup tgPyramid=new TransformGroup(translatePyramid);// create a transform group for the cube which refrences the

translation matrix translateCubeTransformGroup tgCube=new TransformGroup(translateCube);root.addChild(tgPyramid);tgPyramid.addChild(new Pyramid());root.addChild(tgCube);tgCube.addChild(new ColorCube(0.2));return root;

Page 27: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

Der Szenegraph nach den Tranlationen

Page 28: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

3. BeispielVerschiebung, Rotation des ColorCubes und Verschiebung der Pyramidepublic BranchGroup createContentGraph() {

// Create the root of the content branch graphBranchGroup root = new BranchGroup();

// create a transformation matrix to rotate and translate thepyramid

Transform3D rotateTranslatePyramid=new Transform3D();

// a translation of .5 unit towards the positive x-AxisrotateTranslatePyramid.set(new Vector3f(0.5f,0,0));

Transform3D rotatePyramidXY=new Transform3D();// a rotation of 90/60 degrees clockweise around the y/X-AxisrotatePyramidY.rotY(Math.PI/2);rotatePyramidY.rotX(Math.PI/3);

// set the rotations togetherrotateTranslatePyramid.mul(rotatePyramidXY);

Page 29: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

// create a transformation matrix to rotate and translatethe cube

Transform3D rotateTranslateCube=new Transform3D();

// a roation of 45 degrees counter clockwise around the x-Axis

rotateTranslateCube.rotX(Math.PI/4);Transform3D rotateCubeY=new Transform3D();

// a rotation 36 degrees clockweise around the y-AxisrotateCubeY.rotY(-Math.PI/5);

// set the rotations togetherrotateTranslateCube.mul(rotateCubeY);

// a translation of .5 unit toward the negative x-AxisrotateTranslateCube.setTranslation(new Vector3f(-0.5f,0,0));

Page 30: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

// create a transform group for the pyramid which refrences thetranslation matrix translatePyramidTransformGroup tgPyramid=new TransformGroup(translatePyramid);

// create a transform group for the cube which refrences thetranslation matrix translateCubeTransformGroup tgCube=new TransformGroup(rotateTranslateCube);

root.addChild(tgPyramid);tgPyramid.addChild(new Pyramid());

root.addChild(tgCube);tgCube.addChild(new ColorCube(0.2));

return root;} // end of CreateSceneGraph method of HelloJava3D3

Page 31: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen
Page 32: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen

Neupositionierung der Kamera in einer Szenepublic HelloJava3D3() {// 1. Create a canvas3d object

Canvas3D canvas3D = newCanvas3D(SimpleUniverse.getPreferredConfiguration());

getContentPane().add(canvas3D); // 2. Create SimpleUniverse object which references

the canvas3d objectSimpleUniverse simpleU = new SimpleUniverse(canvas3D);

// This will move the ViewPlatform back a bit so that the

// camera is positioned at point (0,0,2.41) // simpleU.getViewingPlatform().setNominalViewingTransform();

// the following lines moves the camera 5 units towards thepositive z-axis

Transform3D vT = new Transform3D();vT.setTranslation(new Vector3f(0,0,5));

simpleU.getViewingPlatform().getViewPlatformTransform().setTransform(vT);

Page 33: Einführung in Java3D - gdv.informatik.uni-frankfurt.de · Java 3D ist eine high level Interaktive 3D Graphik-API (Application Programming Interface) für die Erstellung von 3D-Applikationen