Texture Mapping : Texture « 3D « Java






Texture Mapping

Texture Mapping
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.net.MalformedURLException;
import java.net.URL;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Texture2D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;

/**
 * !Diese Klasse wurde fur das Laden uber ein JAR-Archiv und 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 Textur
 * im Tutorial zuruckgreifen!
 */
public class TextureMapping 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);

    objDreh.addChild(new Box(0.5f, 0.5f, 0.5f, Box.GENERATE_TEXTURE_COORDS,
        makeAppearance()));

    objWurzel.addChild(objDreh);

    return objWurzel;
  }

  /**
   * Wurfeldarstellung
   * 
   * @return Appearance
   */
  private Appearance makeAppearance() {
    Appearance a = new Appearance();
    TextureLoader loader = null;
    if (getCodeBase() != null)
      try {
        // Laden der Obj Datei als Applet mit jar
        loader = new TextureLoader(new URL("jar:" + getCodeBase()
            + "TextureMapping.jar!/burnstone.jpg"), null);
      } catch (MalformedURLException e) {
        e.printStackTrace();
      }
    else
      // Laden der Obj Datei mittels jar
      loader = new TextureLoader(ClassLoader
          .getSystemResource("burnstone.jpg"), null);

    Texture2D texture = (Texture2D) loader.getTexture();
    a.setTexture(texture);
    return a;
  }

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

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

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

  private Canvas3D canvas3D;

  private static Frame frame;
}

           
       








Related examples in the same category

1.LocalEyeApp creates a single plane with texture mappingLocalEyeApp creates a single plane with texture mapping
2.TexCoordGeneration class to automatically define the texture coordinates
3.The simple application of textures
4.ExTexture - illustrate use of textures
5.Illustrates how a texture image can be dynamically rotated at runtime
6.Illustrates dynamic texture coordinate generation using the TexCoordGeneration class
7.Create geometry to display the texture image mapped onto a triangulated polygon
8.Texture: picture ball
9.Image Component By ReferenceImage Component By Reference
10.Interleaved TestInterleaved Test
11.Texture ImageTexture Image
12.Multi TextureMulti Texture
13.Texture By Reference