1:import java.awt.*;
2:import javax.media.j3d.*;
3:import javax.vecmath.*;
4:import com.sun.j3d.utils.image.TextureLoader;
5:
6:
7:public class TextureTestCube extends Shape3D {
8:
9: private Geometry TestGeometry;
10: private Appearance TestAppearance;
11: private Component comp; // needed for the TextureLoader method. Doesn't really matter what it is
12:
13:
14: public TextureTestCube(Component comp) {
15:
16: //to construct a Shape3D object we want to set the geometry and the appearance
17: //first we create Geometry and Appearance objects using the methods
18: //createGeometry and createAppearance below
19: TestGeometry = createGeometry();
20: TestAppearance = createAppearance();
21:
22: this.comp = comp;
23:
24: //then we set the geometry and appeance
25: this.setGeometry(TestGeometry);
26: this.setAppearance(TestAppearance);
27:
28: }
29:
30:
31:
32: private Geometry createGeometry() {
33:
34: //we will return the QuadArray subclass of Geometry
35: //this is a series of quadrilateral faces
36: //we will use a constructor that accepts an array of floats
37: //these are intepreted as x,y,z co-ordinates in groups of four
38: float[] verts = {
39: // Front Face
40: 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
41: -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
42: // Back Face
43: -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
44: 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
45: // Right Face
46: 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f,
47: 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f,
48: // Left Face
49: -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
50: -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
51: // Top Face
52: 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
53: -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
54: // Bottom Face
55: -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f,
56: 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f,
57: };
58:
59: //the normal vectors, used for calculating lighting
60: //we don't actually need these if there are no directional lights
61: //somewhat strangely, one for each vertex (rather than each face)
62: //also, it is up to you to check they have unit length and actually are normal!
63: float[] normals = {
64: // Front Face
65: 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
66: 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
67: // Back Face
68: 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,
69: 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,
70: // Right Face
71: 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
72: 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
73: // Left Face
74: -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
75: -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
76: // Top Face
77: 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
78: 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
79: // Bottom Face
80: 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
81: 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
82: };
83:
84: //the texture co-ordinates are used to map a 2d image onto
85: //the polygons. These 2d co-ordinages, x,y, between 0 and 1
86: //refer to the 2d image (with 0.0f, 0.0f the bottom right
87: //corner and 1.0f, 1.0f the upper right).
88: //there is one x,y pair for each vertex in the cube
89: //and specifies what part of the image will be mapped
90: //to that vertex.
91: float[] textCoords = {
92: // Front Face
93: 1.0f, 0.0f, 1.0f, 1.0f,
94: 0.0f, 1.0f, 0.0f, 0.0f,
95: // Back Face
96: 1.0f, 0.0f, 1.0f, 1.0f,
97: 0.0f, 1.0f, 0.0f, 0.0f,
98: // Right Face
99: 1.0f, 0.0f, 1.0f, 1.0f,
100: 0.0f, 1.0f, 0.0f, 0.0f,
101: // Left Face
102: 1.0f, 0.0f, 1.0f, 1.0f,
103: 0.0f, 1.0f, 0.0f, 0.0f,
104: // Top Face
105: 1.0f, 0.0f, 1.0f, 1.0f,
106: 0.0f, 1.0f, 0.0f, 0.0f,
107: // Bottom Face
108: 1.0f, 0.0f, 1.0f, 1.0f,
109: 0.0f, 1.0f, 0.0f, 0.0f
110: };
111:
112: //construct the quadArray, indicating the number of vertices (24)
113: //and the fact that we will be specifying the vertices, normals and
114: //texture co-ordinates.
115: QuadArray quadArray = new QuadArray(24, QuadArray.COORDINATES |
116: QuadArray.NORMALS |
117: QuadArray.TEXTURE_COORDINATE_2);
118:
119: //now we set them, using the values above
120: quadArray.setCoordinates(0, verts);
121: quadArray.setNormals(0, normals);
122: quadArray.setTextureCoordinates(0, 0, textCoords);
123:
124: return quadArray;
125: }
126:
127:
128:
129: private Appearance createAppearance() {
130:
131: //this is an easy way to convert a jpg file to the texture object we need.
132: TextureLoader loader = new TextureLoader("/home/rfroese/java/reedtalk/applets/bixle_door.jpg", new String("RGB"), comp);
133: Texture texture = loader.getTexture();
134:
135: //we will just set the texture
136: Appearance appearance = new Appearance();
137: appearance.setTexture(texture);
138:
139: return appearance;
140: }
141:
142:}
143:
previous
start
toc
next