Einführung in Java3D

Transcription

Einführung in Java3D
Einführung in Java3D
SS05
Ashraf Abu Baker
[email protected]
Varrentrappstr. 40-42
60486 Frankfurt am Main
Raum 218
Java 3D ist eine high level Interaktive 3D Graphik-API
(Application Programming Interface) für die
Erstellung von 3D-Applikationen und -Applets in Java
-Visualisieren von wissenschaftlichen Daten
- Entwicklung medizinischer Anwendungen
- Technische Modelierung
- Animationen
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
- 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_Overview
http://www.micg.et.fh-stralsund.de/Java3D/java3D.htm
Literatur:
Java3D Programming von Daniel Selman
- 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
- In der Regel besteht eine virtuelle Welt aus einem einzigen
Szenegraphen, kann jedoch aus mehreren bestehen
- Jeder Knote entspricht genau einer Instanz einer
Java3D-Klasse
- Ein Szenegraph weist eine Baumstruktur und besteht aus 2
Arten von Knoten (Szenegraphobjekte)
- 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.
- Eine dritte Art von Knoten, die im Szenegraphen dargestellt
werden, jedoch nicht zum ihm gehören sind:
NodeComponents: legen die Struktur (Geometrie) und das
Erscheinungsbild (Appearance) der visuellen Objekte
des Szenegraphen fest
Beispiele :
Geometry, Appearance, Attributes, Texture, Material
- GroupeNodes und LeafNodes stehen in einer Vater-KindBeziehung (parent child relationship) zueinander
- NodeComponents werden von den anderen
Szenegraphknoten referenziert und stehen somit in einer
Referenzbeziehung zu denen
- 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
- 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)
Rezept für die Erstellung von Java3DProgrammen
1. Erzeuge ein Canvas3D-Objekt
2. Erstelle ein VirtualUniverse-Objekt
3. Erzeuge ein Locale-Objekt
4. Erstelle einen View Branch Graph, der das
Canvas3d referenziert
5. Erstelle einen oder mehrere Content Graphs
6. Kompiliere den Content Branch Graph
7. 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
Erstes Beispiel HelloJava3D1
import javax.swing.JApplet;
import javax.swing.JFrame;
//Java3D Core Package
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.vecmath.*;
//Java3D Utility Package
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.ColorCube;
public class HelloJava3D1 extends JApplet {
public HelloJava3D1() {
//1. Create a canvas3d object
Canvas3D canvas3D=
new Canvas3D(SimpleUniverse.getPreferredConfiguration());
getContentPane().add(canvas3D);
//2. Create SimpleUniverse object which references the canvas3d object
SimpleUniverse 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 graph
contentGraph.compile();
//
5. Insert the content graph into the Locale of the
SimpleUniverse and make it live
simpleU.addBranchGraph(contentGraph);
} // end of HelloJava3D1 (constructor)
public BranchGroup createContentGraph() {
// Create the root of the content branch graph
BranchGroup 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
//2. Create SimpleUniverse object which references the
canvas3d object
SimpleUniverse 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();
- Das SimpleUniverse-Objekt besteht aus :
dreidimensionaler virtuellen Universe (Locale)
Image Plage der Dimension 2x2 und Mittelpunkt (0,0,0) und einer
Kamera, die im Punkt (0,0,2.41) positioniert ist
2. Beispiel
- Das Einfügen eines ColorCube-Objekts
root.addChild(new Pyramid());
root.addChild(new ColorCubes(.2));
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)
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
public BranchGroup createContentGraph() {
// Create the root of the content branch graph
BranchGroup root = new BranchGroup();
// create a transformation matrix to translate the pyramid
Transform3D translatePyramid=new Transform3D();
// a translation of .5 unit towards the positive x-axis
translatePyramid.set(new Vector3f(0.5f,0,0));
// create a transformation matrix to translate the cube
Transform3D translateCube=new Transform3D();
// a translation of .5 unit towards the negative x-axis
translateCube.set(new Vector3f(-0.5f,0,0));
// create a transform group for the pyramid which refrences the
translation matrix translatePyramid
TransformGroup tgPyramid=new TransformGroup(translatePyramid);
// create a transform group for the cube which refrences the
translation matrix translateCube
TransformGroup tgCube=new TransformGroup(translateCube);
root.addChild(tgPyramid);
tgPyramid.addChild(new Pyramid());
root.addChild(tgCube);
tgCube.addChild(new ColorCube(0.2));
return root;
Der Szenegraph nach den Tranlationen
3. Beispiel
Verschiebung, Rotation des ColorCubes und Verschiebung der Pyramide
public BranchGroup createContentGraph() {
// Create the root of the content branch graph
BranchGroup root = new BranchGroup();
// create a transformation matrix to rotate and translate the
pyramid
Transform3D rotateTranslatePyramid=new Transform3D();
// a translation of .5 unit towards the positive x-Axis
rotateTranslatePyramid.set(new Vector3f(0.5f,0,0));
Transform3D rotatePyramidXY=new Transform3D();
// a rotation of 90/60 degrees clockweise around the y/X-Axis
rotatePyramidY.rotY(Math.PI/2);
rotatePyramidY.rotX(Math.PI/3);
// set the rotations together
rotateTranslatePyramid.mul(rotatePyramidXY);
//
create a transformation matrix to rotate and translate
the cube
Transform3D rotateTranslateCube=new Transform3D();
//
Axis
//
//
a roation of 45 degrees counter clockwise around the xrotateTranslateCube.rotX(Math.PI/4);
Transform3D rotateCubeY=new Transform3D();
a rotation 36 degrees clockweise around the y-Axis
rotateCubeY.rotY(-Math.PI/5);
set the rotations together
rotateTranslateCube.mul(rotateCubeY);
//
a translation of .5 unit toward the negative x-Axis
rotateTranslateCube.setTranslation(new Vector3f(-0.5f,0,0));
// create a transform group for the pyramid which refrences the
translation matrix translatePyramid
TransformGroup tgPyramid=new TransformGroup(translatePyramid);
// create a transform group for the cube which refrences the
translation matrix translateCube
TransformGroup 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
Neupositionierung der Kamera in einer Szene
public HelloJava3D3() {
//
1. Create a canvas3d object
Canvas3D canvas3D = new
Canvas3D(SimpleUniverse.getPreferredConfiguration());
getContentPane().add(canvas3D);
//
2. Create SimpleUniverse object which references
the canvas3d object
SimpleUniverse 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
positive z-axis
the
Transform3D vT = new Transform3D();
vT.setTranslation(new Vector3f(0,0,5));
simpleU.getViewingPlatform().getViewPlatformTransform().setTrans
form(vT);