Texture Mapping in Java3D
An Introduction with Examples |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
Appearance
TextureLoader
Class:
ImageComponent2D
Class:
Texture
Class:
Texture
and Creating an Appearance
/** * Create a textured Appearance * * @param name The name of the file containg the image * @return The Appearance */ private static Appearance createTexturedAppearance(String name) { Appearance appearance; ImageComponent2D image; Texture2D texture; TextureLoader textureLoader; // Load the image textureLoader = new TextureLoader(name, null); image = textureLoader.getImage(); // Create the Texture texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image.getWidth(), image.getHeight()); texture.setImage(0, image); // Level 0 indicates no mip-mapping // Create the Appearance appearance = new Appearance(); appearance.setTexture(texture); return appearance; }
TexCoord2f
TexCoord3f
// Texture Coordinates private static final float texcoords[][] = {{0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f} }; // Textures private static final String textures[] = { "abstract.gif", "brick.gif", "brownpaper.gif", "flagstone.gif", "frostedglass.gif", "oak.gif" };
/** * Create a cube with white faces * * @return The BranchGroup containing the cube */ public static BranchGroup createCube() { Appearance appearance; BranchGroup branchGroup; QuadArray face; Shape3D node; // Create an empty BranchGroup branchGroup = new BranchGroup(); // Create each face for (int f=0; f<faces.length; f++) { // Create a QuadArray to hold each face face = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2); // Add the vertices to the QuadArray for (int v=0; v<faces[f].length; v++) { face.setCoordinates(v, vertices[faces[f][v]]); // Set the texture coordinates for (int i=0; i<4; i++) { face.setTextureCoordinate(i, texcoords[i]); } } // Create a Node to hold the face node = new Shape3D(face); // Add an Appearance appearance = createTexturedAppearance(textures[f]); node.setAppearance(appearance); // Add the Node to the BranchGroup branchGroup.addChild(node); } return branchGroup; }