The simple application of textures : Texture « 3D « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » 3D » Texture 




The simple application of textures


/*
Essential Java 3D Fast

Ian Palmer

Publisher: Springer-Verlag

ISBN: 1-85233-394-4

*/

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.IndexedQuadArray;
import javax.media.j3d.Locale;
import javax.media.j3d.Material;
import javax.media.j3d.Node;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.TexCoord2f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.image.TextureLoader;

/**
 * This demonstrates the simple application of textures. Each face of a cube has
 * an image mapped onto it exactly once. The image is loaded from an external
 * file.
 
 @author I.J.Palmer
 @version 1.0
 */
public class SimpleTexture extends Frame implements ActionListener {
  protected Canvas3D myCanvas3D = new Canvas3D(null);

  protected Button myButton = new Button("Exit");

  /**
   * This function builds the view branch of the scene graph. It creates a
   * branch group and then creates the necessary view elements to give a
   * useful view of our content.
   
   @param c
   *            Canvas3D that will display the view
   @return BranchGroup that is the root of the view elements
   */
  protected BranchGroup buildViewBranch(Canvas3D c) {
    BranchGroup viewBranch = new BranchGroup();
    Transform3D viewXfm = new Transform3D();
    viewXfm.set(new Vector3f(0.0f0.0f5.0f));
    TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
    ViewPlatform myViewPlatform = new ViewPlatform();
    PhysicalBody myBody = new PhysicalBody();
    PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
    viewXfmGroup.addChild(myViewPlatform);
    viewBranch.addChild(viewXfmGroup);
    View myView = new View();
    myView.addCanvas3D(c);
    myView.attachViewPlatform(myViewPlatform);
    myView.setPhysicalBody(myBody);
    myView.setPhysicalEnvironment(myEnvironment);
    return viewBranch;
  }

  /**
   * Add some lights so that we can illuminate the scene. This adds one
   * ambient light to bring up the overall lighting level and one directional
   * shape to show the shape of the objects in the scene.
   
   @param b
   *            BranchGroup that the lights are to be added to.
   */
  protected void addLights(BranchGroup b) {
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.00.00.0),
        100.0);
    Color3f lightColour1 = new Color3f(1.0f1.0f1.0f);
    Vector3f lightDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f lightColour2 = new Color3f(1.0f1.0f1.0f);
    Vector3f lightDir2 = new Vector3f(0.0f0.0f, -1.0f);
    Color3f ambientColour = new Color3f(0.2f0.2f0.2f);
    AmbientLight ambientLight1 = new AmbientLight(ambientColour);
    ambientLight1.setInfluencingBounds(bounds);
    DirectionalLight directionalLight1 = new DirectionalLight(lightColour1,
        lightDir1);
    directionalLight1.setInfluencingBounds(bounds);
    b.addChild(ambientLight1);
    b.addChild(directionalLight1);
  }

  /**
   * This builds the content branch of our scene graph. The shape supplied as
   * a parameter is slightly tilted to reveal its 3D shape. It also uses the
   * addLights function to add some lights to the scene.
   
   @param shape
   *            Node that represents the geometry for the content
   @return BranchGroup that is the root of the content branch
   */
  protected BranchGroup buildContentBranch(Node shape) {
    BranchGroup contentBranch = new BranchGroup();
    Transform3D rotateCube = new Transform3D();
    rotateCube.set(new AxisAngle4d(1.01.00.0, Math.PI / 4.0));
    TransformGroup rotationGroup = new TransformGroup(rotateCube);
    contentBranch.addChild(rotationGroup);
    rotationGroup.addChild(shape);
    addLights(contentBranch);
    return contentBranch;
  }

  /**
   * This defines the appearance with a texture. The texture is loaded from an
   * external file.
   
   @return Appearance that uses the texture.
   */
  protected Appearance DefineAppearance() {
    //Load the texture from the external image file
    TextureLoader textLoad = new TextureLoader("housebrick.jpg"this);
    //Access the image from the loaded texture
    ImageComponent2D textImage = textLoad.getImage();
    //Create a two dimensional texture
    Texture2D texture = new Texture2D(Texture2D.BASE_LEVEL, Texture.RGB,
        textImage.getWidth(), textImage.getHeight());
    //Set the texture from the image loaded
    texture.setImage(0, textImage);
    //Create the appearance that will use the texture
    Appearance app = new Appearance();
    app.setTexture(texture);
    //Define how the texture will be mapped onto the surface
    //by creating the appropriate texture attributes
    TextureAttributes textAttr = new TextureAttributes();
    textAttr.setTextureMode(TextureAttributes.REPLACE);
    app.setTextureAttributes(textAttr);
    app.setMaterial(new Material());
    return app;
  }

  /**
   * Build a cube from an IndexedQuadArray. This method creates the vertices
   * as a set of eight points and the normals as a set of six vectors (one for
   * each face). The data is then defined such that each vertex has a
   * different normal associated with it when it is being used for a different
   * face. The shape is created with texture coordinates so that when the
   * appearance is set it will use the appearance texture on the surface.
   
   @return Node that is the shape.
   */
  protected Node buildShape() {
    IndexedQuadArray indexedCube = new IndexedQuadArray(8,
        IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS
            | IndexedQuadArray.TEXTURE_COORDINATE_2, 24);
    Point3f[] cubeCoordinates = new Point3f(1.0f1.0f1.0f),
        new Point3f(-1.0f1.0f1.0f),
        new Point3f(-1.0f, -1.0f1.0f),
        new Point3f(1.0f, -1.0f1.0f)new Point3f(1.0f1.0f, -1.0f),
        new Point3f(-1.0f1.0f, -1.0f),
        new Point3f(-1.0f, -1.0f, -1.0f),
        new Point3f(1.0f, -1.0f, -1.0f) };
    Vector3f[] normals = new Vector3f(0.0f0.0f1.0f),
        new Vector3f(0.0f0.0f, -1.0f),
        new Vector3f(1.0f0.0f0.0f),
        new Vector3f(-1.0f0.0f0.0f),
        new Vector3f(0.0f1.0f0.0f)new Vector3f(0.0f, -1.0f0.0f) };
    //Define the texture coordinates. These are defined
    //as floating point pairs of values that are used to
    //map the corners of the texture image onto the vertices
    //of the face. We then define the indices into this
    //array of values in a similar way to that used for
    //the vertices and normals.
    TexCoord2f[] textCoord = new TexCoord2f(1.0f1.0f),
        new TexCoord2f(0.0f1.0f)new TexCoord2f(0.0f0.0f),
        new TexCoord2f(1.0f0.0f) };
    int coordIndices[] 0123765403745621,
        0451673};
    int normalIndices[] 0000111122223333,
        4444555};
    int textIndices[] 0123301212301230,
        3012123};
    indexedCube.setCoordinates(0, cubeCoordinates);
    indexedCube.setCoordinateIndices(0, coordIndices);
    indexedCube.setNormals(0, normals);
    indexedCube.setNormalIndices(0, normalIndices);
    indexedCube.setTextureCoordinates(00, textCoord);
    indexedCube.setTextureCoordinateIndices(00, textIndices);
    return new Shape3D(indexedCube, DefineAppearance());
  }

  /**
   * Handles the exit button action to quit the program.
   */
  public void actionPerformed(ActionEvent e) {
    dispose();
    System.exit(0);
  }

  public SimpleTexture() {
    VirtualUniverse myUniverse = new VirtualUniverse();
    Locale myLocale = new Locale(myUniverse);
    myLocale.addBranchGraph(buildViewBranch(myCanvas3D));
    myLocale.addBranchGraph(buildContentBranch(buildShape()));
    setTitle("SimpleTexture");
    setSize(400400);
    setLayout(new BorderLayout());
    add("Center", myCanvas3D);
    add("South", myButton);
    myButton.addActionListener(this);
    setVisible(true);
  }

  public static void main(String[] args) {
    SimpleTexture st = new SimpleTexture();
  }
}
           
       














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.ExTexture - illustrate use of textures
4.Illustrates how a texture image can be dynamically rotated at runtime
5.Illustrates dynamic texture coordinate generation using the TexCoordGeneration class
6.Create geometry to display the texture image mapped onto a triangulated polygon
7.Texture: picture ball
8.Image Component By ReferenceImage Component By Reference
9.Interleaved TestInterleaved Test
10.Texture ImageTexture Image
11.Multi TextureMulti Texture
12.Texture By Reference
13.Texture MappingTexture Mapping
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.