Texture coordinates Second example code


/** 
 * 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.0f, 1.0f, 1.0f),
						new Point3f(-1.0f, 1.0f, 1.0f),
						new Point3f(-1.0f,-1.0f, 1.0f),
						new Point3f( 1.0f,-1.0f, 1.0f),
						new Point3f( 1.0f, 1.0f,-1.0f),
						new Point3f(-1.0f, 1.0f,-1.0f),
						new Point3f(-1.0f,-1.0f,-1.0f),
						new Point3f( 1.0f,-1.0f,-1.0f)};
	Vector3f[] normals= {new Vector3f( 0.0f, 0.0f, 1.0f),
				   new Vector3f( 0.0f, 0.0f,-1.0f),
				   new Vector3f( 1.0f, 0.0f, 0.0f),
				   new Vector3f(-1.0f, 0.0f, 0.0f),
				   new Vector3f( 0.0f, 1.0f, 0.0f),
				   new Vector3f( 0.0f,-1.0f, 0.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.0f,1.0f),
						new TexCoord2f(0.0f,1.0f),
						new TexCoord2f(0.0f,0.0f),
						new TexCoord2f(1.0f,0.0f)};
	int coordIndices[] =  {0,1,2,3,7,6,5,4,0,3,7,4,5,6,2,1,0,4,5,1,6,7,3,2};
	int normalIndices[] = {0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5};
	int textIndices[] =   {0,1,2,3,3,0,1,2,1,2,3,0,1,2,3,0,3,0,1,2,1,2,3,0};
	indexedCube.setCoordinates(0, cubeCoordinates);
	indexedCube.setCoordinateIndices(0, coordIndices);
	indexedCube.setNormals(0,normals);
	indexedCube.setNormalIndices(0, normalIndices);
	indexedCube.setTextureCoordinates(0,0,textCoord);
	indexedCube.setTextureCoordinateIndices(0,0,textIndices);
	return new Shape3D(indexedCube, DefineAppearance());
}