Teapot : Object Model « 3D « Java






Teapot

Teapot

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;

/**
 * !Diese Klasse wurde fur das Laden uber ein JAR-Archiv oder Applet welches ein
 * JAR - Archiv nutzt angepasst Um das Programm als einfache Applikation uber
 * einen class-File laufen zu lassen bitte auf den Code zum Einladen der OBJ
 * Datei im Tutorial zuruckgreifen!
 */
public class Phong extends Applet {

  /**
   * init Methoden fur die Darstellung als Applet
   */
  public void init() {
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();
    canvas3D = new Canvas3D(config);
    add("Center", canvas3D);
    BranchGroup szene = macheSzene();
    szene.compile();
    universe = new SimpleUniverse(canvas3D);
    universe.getViewingPlatform().setNominalViewingTransform();
    universe.addBranchGraph(szene);
  }

  /**
   * Erstellt den Szenegraphen
   * 
   * @return BranchGroup
   */
  public BranchGroup macheSzene() {
    BranchGroup objWurzel = new BranchGroup();
    // Transformation, 2 Rotationen:
    Transform3D drehung = new Transform3D();
    Transform3D drehung2 = new Transform3D();
    drehung.rotX(Math.PI / 4.0d);
    drehung2.rotY(Math.PI / 5.0d);
    drehung.mul(drehung2);
    TransformGroup objDreh = new TransformGroup(drehung);
    //Loader
    ObjectFile file = new ObjectFile(ObjectFile.RESIZE);
    Scene scene = null;
    try {
        scene = file.load(ClassLoader.getSystemResource("teapot.obj"));

    } catch (Exception e) {
      System.err.println(e);
      System.exit(1);
    }
    objDreh.addChild(scene.getSceneGroup());

    DirectionalLight d_Licht = new DirectionalLight(new Color3f(1.0f, 1.0f,
        1.0f), new Vector3f(-1.0f, -1.0f, -1.0f));
    d_Licht.setInfluencingBounds(new BoundingSphere(new Point3d(0.0d, 0.0d,
        0.0d), 100.0d));
    objDreh.addChild(d_Licht);
    objWurzel.addChild(objDreh);
    return objWurzel;
  }

  /**
   * gibt speicher frei
   */
  public void destroy() {
    universe.removeAllLocales();
  }

  public static void main(String[] args) {
    frame = new MainFrame(new Phong(), 500, 500);
    frame.setTitle("Phong");
  }

  //---- Attribute -----------------------
  private SimpleUniverse universe;

  private Canvas3D canvas3D;

  private static Frame frame;
}



           
       








Phong.zip( 18 k)

Related examples in the same category

1.Line TypesLine Types
2.Shape: Point outlineShape: Point outline
3.Color YoyoColor Yoyo
4.Yoyo LineYoyo Line
5.The use of the GeometryInfo class and related classesThe use of the GeometryInfo class and related classes
6.Example SwitchExample Switch
7.A Morph object to animate a shape between two key shapesA Morph object to animate a shape between two key shapes
8.ExHenge - create a stone-henge like (vaguely) mysterious temple thing
9.Appearance Is Everything
10.Geometry By ReferenceGeometry By Reference
11.Stereo Girl
12.Red Green GirlRed Green Girl
13.Red Green GriffinRed Green Griffin
14.cg viewer
15.A basic hierarchical model of the top part of a human torsoA basic hierarchical model of the top part of a human torso
16.A large hollow box
17.Java 3D Box and a custom Cuboid implementationJava 3D Box and a custom Cuboid implementation
18.A simple class using the an indexed quadrilateral arrayA simple class using the an indexed quadrilateral array
19.Simple Indexed Quad NormalsSimple Indexed Quad Normals
20.Simple Indexed QuadSimple Indexed Quad
21.Twist Strip visual objectTwist Strip visual object
22.Door AppDoor App
23.ShadowApp creates a single planeShadowApp creates a single plane
24.MorphingMorphing
25.VolRend
26.GouraudGouraud
27.An Object An Object