Exercise 10: Use of texture
Using Geometry with pre-generated texture coordinates (eg Sphere).
Load a jpg image as a texture, associate it with an Appearance, then create
a Sphere with that appearance.
|
|
|
Texture
|
Sphere
|
Textured Sphere
|
Tips
Appearance app = new Appearance();
app.setTexture( ??? );
Sphere sphere = new Sphere(1.0f, Sphere.GENERATE_TEXTURE_COORDS,
app);
To load the texture use the TextureLoader class in the com.sun.j3d.utils.image
package.
Creating your own Geometry and texture coordinates (eg IndexedTriangleArray).
Same as the previous exercise but here we create our own geometry and texture
coordinates.
|
|
|
Texture
|
Indexed Triangle Array
|
Textured Indexed Traingle Array
|
Tips
private Geometry createGeometry()
{
IndexedTriangleArray ita = new IndexedTriangleArray(4,
IndexedTriangleArray.COORDINATES | IndexedTriangleArray.TEXTURE_COORDINATE_2,
6);
ita.setCoordinate(0, new Point3d(0, 0, 0));
ita.setCoordinate(1, new Point3d(1, 0, 0));
ita.setCoordinate(2, new Point3d(0, 1, 0));
ita.setCoordinate(3, new Point3d(1, 1, 0));
ita.setCoordinateIndex(0, 0);
ita.setCoordinateIndex(1, 1);
ita.setCoordinateIndex(2, 2);
ita.setCoordinateIndex(3, 1);
ita.setCoordinateIndex(4, 3);
ita.setCoordinateIndex(5, 2);
ita.setTextureCoordinate( ??? ); // repeat 4
times
ita.setTextureCoordinateIndex( ??? ); // repeat
6 times
return ita;
}