/Users/lyon/j4p/src/j3d/cr325/Shapes.java
|
1 /**
2 * DocJava, Inc.
3 * http://www.docjava.com
4 * Programmer: dlyon
5 * Date: Mar 3, 2004
6 * Time: 2:32:59 PM
7 *
8 */
9 package j3d.cr325;
10
11 import com.sun.j3d.utils.geometry.Box;
12 import com.sun.j3d.utils.geometry.Cylinder;
13 import com.sun.j3d.utils.geometry.Primitive;
14 import com.sun.j3d.utils.geometry.Sphere;
15 import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
16 import com.sun.j3d.utils.universe.PlatformGeometry;
17 import j3d.Utils;
18
19 import javax.media.j3d.*;
20 import javax.vecmath.*;
21 import java.awt.*;
22
23 public class Shapes {
24 public static Shape3D getLand() {
25 int numberOfLines = 888;
26 LineArray la = new LineArray(numberOfLines,
27 GeometryArray.COORDINATES
28 | GeometryArray.COLOR_3);
29 float l = -50.0f;
30 for (int c = 0; c < numberOfLines; c += 4) {
31 int r = math.MathUtils.rand(1, 10);
32 la.setCoordinate(c + 0, new Point3f(-50.0f, 0f, l));
33 la.setCoordinate(c + 1, new Point3f(50.0f, 0.0f, l));
34 la.setCoordinate(c + 2, new Point3f(l, 0.0f, -50.0f));
35 la.setCoordinate(c + 3, new Point3f(l, 0.0f, 50.0f));
36 l += 1.0f;
37 }
38
39 Color3f c = new Color3f(0.8f, 0.1f, 0.1f);
40 for (int i = 0; i < numberOfLines; i++) la.setColor(i, c);
41
42 return new Shape3D(la);
43 }
44
45 public static QuadArray getHouse() {
46 QuadArray qa = new QuadArray(28, GeometryArray.COORDINATES | GeometryArray.COLOR_3);
47
48 qa.setCoordinate(0, new Point3f(0f, 0f, 0f));
49 qa.setCoordinate(1, new Point3f(1f, 0f, 0f));
50 qa.setCoordinate(2, new Point3f(1f, 1f, 0f));
51 qa.setCoordinate(3, new Point3f(0f, 1f, 0f));
52
53 qa.setCoordinate(4, new Point3f(0.4f, 0f, 0.01f));
54 qa.setCoordinate(5, new Point3f(0.6f, 0f, 0.01f));
55 qa.setCoordinate(6, new Point3f(0.6f, .5f, 0.01f));
56 qa.setCoordinate(7, new Point3f(0.4f, .5f, 0.01f));
57
58 qa.setCoordinate(8, new Point3f(0.2f, .60f, 0.01f));
59 qa.setCoordinate(9, new Point3f(0.4f, 0.6f, 0.01f));
60 qa.setCoordinate(10, new Point3f(0.4f, 0.8f, 0.01f));
61 qa.setCoordinate(11, new Point3f(0.2f, 0.8f, 0.01f));
62
63 qa.setCoordinate(12, new Point3f(0.6f, .60f, 0.01f));
64 qa.setCoordinate(13, new Point3f(0.8f, 0.6f, 0.01f));
65 qa.setCoordinate(14, new Point3f(0.8f, 0.8f, 0.01f));
66 qa.setCoordinate(15, new Point3f(0.6f, 0.8f, 0.01f));
67
68 qa.setCoordinate(16, new Point3f(0f, 1f, 0f));
69 qa.setCoordinate(17, new Point3f(0.5f, 1f, 0f));
70 qa.setCoordinate(18, new Point3f(0.5f, 1.3f, 0f));
71 qa.setCoordinate(19, new Point3f(0f, 1f, 0f));
72
73 qa.setCoordinate(20, new Point3f(1f, 1f, 0f));
74 qa.setCoordinate(21, new Point3f(0.5f, 1.3f, 0f));
75 qa.setCoordinate(22, new Point3f(0.5f, 1f, 0f));
76 qa.setCoordinate(23, new Point3f(1f, 1f, 0f));
77
78 qa.setCoordinate(24, new Point3f(0.1f, 1f, -0.01f));
79 qa.setCoordinate(25, new Point3f(0.2f, 1f, -0.01f));
80 qa.setCoordinate(26, new Point3f(0.2f, 1.2f, -0.01f));
81 qa.setCoordinate(27, new Point3f(0.1f, 1.2f, -0.01f));
82
83 return qa;
84 }
85
86 public static TriangleFanArray getTree() {
87 int counts[] = {19};
88
89
90 TriangleFanArray tfa = new TriangleFanArray(19,
91 GeometryArray.COORDINATES
92 | GeometryArray.COLOR_3,
93 counts);
94
95 tfa.setCoordinate(0, new Point3f(0.00f, 0.60f, 0.0f));
96 tfa.setCoordinate(1, new Point3f(-0.05f, 0.00f, 0.0f));
97 tfa.setCoordinate(2, new Point3f(0.05f, 0.00f, 0.0f));
98 tfa.setCoordinate(3, new Point3f(0.05f, 0.25f, 0.0f));
99 tfa.setCoordinate(4, new Point3f(0.15f, 0.30f, 0.0f));
100 tfa.setCoordinate(5, new Point3f(0.22f, 0.25f, 0.0f));
101 tfa.setCoordinate(6, new Point3f(0.18f, 0.40f, 0.0f));
102 tfa.setCoordinate(7, new Point3f(0.20f, 0.55f, 0.0f));
103 tfa.setCoordinate(8, new Point3f(0.15f, 0.65f, 0.0f));
104 tfa.setCoordinate(9, new Point3f(0.14f, 0.80f, 0.0f));
105 tfa.setCoordinate(10, new Point3f(0.08f, 0.95f, 0.0f));
106 tfa.setCoordinate(11, new Point3f(0.00f, 1.00f, 0.0f));
107 tfa.setCoordinate(12, new Point3f(-0.20f, 0.85f, 0.0f));
108 tfa.setCoordinate(13, new Point3f(-0.22f, 0.70f, 0.0f));
109 tfa.setCoordinate(14, new Point3f(-0.30f, 0.60f, 0.0f));
110 tfa.setCoordinate(15, new Point3f(-0.35f, 0.45f, 0.0f));
111 tfa.setCoordinate(16, new Point3f(-0.25f, 0.43f, 0.0f));
112 tfa.setCoordinate(17, new Point3f(-0.30f, 0.25f, 0.0f));
113 tfa.setCoordinate(18, new Point3f(-0.02f, 0.24f, 0.0f));
114 return tfa;
115 }
116
117 public static Shape3D getTrees() {
118
119 TriangleFanArray tfa = getTree();
120
121 Color3f c = new Color3f(0.1f, 0.9f, 0.0f);
122 for (int i = 0; i < 19; i++)
123 tfa.setColor(i, c);
124 c.set(0.5f, 0.5f, 0.3f);
125 tfa.setColor(1, c);
126 tfa.setColor(2, c);
127 tfa.setColor(18, c);
128 return new Shape3D(tfa);
129 }
130
131 public static Shape3D getHouses() {
132
133 QuadArray qa = getHouse();
134
135 Color3f c = new Color3f(1f, 1f, 1f);
136 Color3f d = new Color3f(0f, 0f, 0f);
137 Color3f e = new Color3f(1f, 0f, 0f);
138 for (int i = 0; i < 4; i++)
139 qa.setColor(i, c);
140 for (int i = 4; i < 24; i++)
141 qa.setColor(i, d);
142 for (int i = 24; i < 28; i++)
143 qa.setColor(i, e);
144
145 Shape3D house = new Shape3D(qa);
146 return house;
147 }
148
149 public static BranchGroup getTreeBranchGroup() {
150 BranchGroup bg = getReadWriteBranchGroup();
151 BoundingSphere bs = new BoundingSphere();
152 bs.setRadius(10);
153 SharedGroup share = new SharedGroup();
154 share.addChild(getTrees());
155
156 float position[][] = {{0.0f, 0f, -3.0f},
157 {6.0f, 0f, 0.0f},
158 {6.0f, 0f, 6.0f},
159 {3.0f, 0f, -10.0f},
160 {13.0f, 0f, -30.0f},
161 {-13.0f, 0f, 30.0f},
162 {-13.0f, 0f, 23.0f},
163 {13.0f, 0f, 3.0f}};
164
165 Vector3f translate = new Vector3f();
166 Transform3D t3D = new Transform3D();
167 TransformGroup TGT;
168 TransformGroup TGR;
169 Billboard billboard;
170 for (int i = 0; i < position.length; i++) {
171 translate.set(position[i]);
172 t3D.setTranslation(translate);
173 TGT = new TransformGroup(t3D);
174 TGR = new TransformGroup();
175 TGR.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
176 billboard = new Billboard(TGR);
177 billboard.setSchedulingBounds(bs);
178 billboard.setAlignmentMode(Billboard.ROTATE_ABOUT_POINT);
179 bg.addChild(TGT);
180 bg.addChild(billboard);
181 TGT.addChild(TGR);
182 TGR.addChild(new Link(share));
183 }
184 return bg;
185 }
186
187 public static BranchGroup getReadWriteBranchGroup() {
188 BranchGroup bg = new BranchGroup();
189 bg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
190 bg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
191 return bg;
192 }
193
194 public static BranchGroup getHousesBranchGroup() {
195 BranchGroup bg = getReadWriteBranchGroup();
196 BoundingSphere bs = new BoundingSphere();
197 bs.setRadius(10);
198 Vector3f translate = new Vector3f();
199 Transform3D t3d = new Transform3D();
200 SharedGroup sg = new SharedGroup();
201
202 sg.addChild(getHouses());
203
204 float position[][] = {{0.0f, 0f, -2.0f},
205 {3.0f, 0f, 0.0f},
206 {3.0f, 0f, 4.0f},
207 {4.0f, 0f, -8.0f},
208 {6.0f, 0f, -25.0f},
209 {-6.0f, 0f, 25.0f},
210 {-6.0f, 0f, 15.0f},
211 {6.0f, 0f, 1.0f}};
212
213 TransformGroup TGT;
214 TransformGroup TGR;
215 Billboard billboard;
216 for (int i = 0; i < position.length; i++) {
217 translate.set(position[i]);
218 t3d.setTranslation(translate);
219 TGT = new TransformGroup(t3d);
220 TGR = new TransformGroup();
221 TGR.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
222 billboard = new Billboard(TGR);
223 billboard.setSchedulingBounds(bs);
224 billboard.setAlignmentMode(Billboard.ROTATE_ABOUT_POINT);
225 bg.addChild(TGT);
226 bg.addChild(billboard);
227 TGT.addChild(TGR);
228 TGR.addChild(new Link(sg));
229 }
230 return bg;
231 }
232
233 public static Locale getLocale(View myView) {
234
235 //First, we create the necessary coordinate systems
236 //(VirtualUniverse, Locale), container nodes
237 //(BranchGroup, TransformGroup), and platform which
238 //determines our viewing position and direction (ViewPlatform).
239 VirtualUniverse myUniverse = new VirtualUniverse();
240 Locale myLocale = new Locale(myUniverse);
241 BranchGroup myBranchGroup = new BranchGroup();
242 TransformGroup myTransformGroup = new TransformGroup();
243 ViewPlatform myViewPlatform = new ViewPlatform();
244
245 //Next, we insert the platform into the transform group,
246 //the transform group into the branch group, and the branch
247 //group into the locale's branch graph portion of the
248 //scene graph.
249 myTransformGroup.addChild(myViewPlatform);
250 myBranchGroup.addChild(myTransformGroup);
251 myLocale.addBranchGraph(myBranchGroup);
252
253 //Finally, we attach our view to the view platform and we
254 //return a reference to our new universe. We are ready to
255 //render 3D content!
256 myView.attachViewPlatform(myViewPlatform);
257 return (myLocale);
258 }
259
260 public static View constructView(Canvas3D myCanvas3D) {
261 View myView = new View();
262 myView.addCanvas3D(myCanvas3D);
263 myView.setPhysicalBody(new PhysicalBody());
264 myView.setPhysicalEnvironment(new PhysicalEnvironment());
265 return (myView);
266 }
267
268 public static void addTextToLocale(Locale myLocale) {
269 //We first create a regular 2D font, then from that a
270 //3D font, 3D text, and 3D shape, in succession. We use
271 //the default constructors for FontExtrusion and Appearance.
272 Font myFont = new Font("TimesRoman", Font.PLAIN, 10);
273 Font3D myFont3D = new Font3D(myFont, new FontExtrusion());
274 Text3D myText3D = new Text3D(myFont3D, "The Midterm begins");
275 Shape3D myShape3D = new Shape3D(myText3D, new Appearance());
276
277 //We created a new branch group and transform group to hold
278 //our content. This time when we create the transform group,
279 //however, we pass in a Transform3D to the transform group's
280 //constructor. This 3D transform has been manipulated
281 //to perform the transformations we desire, which results
282 //in those manipulations being carried out on all children
283 //of the given transform group, in this case, our content.
284 BranchGroup contentBranchGroup = new BranchGroup();
285 Transform3D myTransform3D = new Transform3D();
286 myTransform3D.setTranslation(new Vector3f(-2.0f, 0.0f, -4.0f));
287 myTransform3D.setScale(0.1);
288 Transform3D tempTransform3D = new Transform3D();
289 tempTransform3D.rotY(Math.PI / 4.0d);
290 myTransform3D.mul(tempTransform3D);
291 TransformGroup contentTransformGroup = new TransformGroup(myTransform3D);
292
293 //We add our child nodes and insert the branch group into
294 //the live scene graph. This results in Java 3D rendering
295 //the content.
296 contentTransformGroup.addChild(myShape3D);
297 contentBranchGroup.addChild(contentTransformGroup);
298 myLocale.addBranchGraph(contentBranchGroup);
299 }
300
301 public static BranchGroup getBackgroundBranchGroup() {
302 // create a parent BranchGroup for the Background
303 BranchGroup backgroundGroup = new BranchGroup();
304
305 // create a new Background node
306 Background back = new Background();
307
308 // set the range of influence of the background
309 back.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 2000.0));
310
311 // create a BranchGroup that will hold
312 // our Sphere geometry
313 BranchGroup bgGeometry = new BranchGroup();
314
315 // create the Sphere geometry with radius 1.0
316 // we tell the Sphere to generate texture coordinates
317 // to enable the texture image to be rendered
318 // and because we are *inside* the Sphere we have to generate
319 // Normal coordinates inwards or the Sphere will not be visible.
320 Appearance appearance = new Appearance();
321 appearance.setTexture(Utils.getMandleTexture(640, 480));
322 Sphere sphere = new Sphere(.50f,
323 Primitive.GENERATE_TEXTURE_COORDS |
324 Primitive.GENERATE_NORMALS_INWARD,
325 appearance);
326
327 // start wiring everything together
328 // add the Sphere to its parent BranchGroup
329 bgGeometry.addChild(sphere);
330
331
332 // assign the BranchGroup to the Background as geometry.
333 back.setGeometry(bgGeometry);
334
335 // add the Background node to its parent BranchGroup
336 backgroundGroup.addChild(back);
337
338 return backgroundGroup;
339 }
340
341 public static TransformGroup getReadWriteTransformGroup() {
342 TransformGroup tg = new TransformGroup();
343 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
344 tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
345 return tg;
346 }
347
348 public static BranchGroup getGunBranchGroup(TransformGroup gunTransformGroup) {
349 BranchGroup theGun = new BranchGroup();
350
351 Appearance gunApp = getGunAppearance();
352 TransformGroup init = new TransformGroup();
353
354 Transform3D gunXfm = new Transform3D();
355
356 Matrix3d gunXfmMat = new Matrix3d();
357 gunXfmMat.rotX(Math.PI / 2);
358 gunXfm.set(gunXfmMat, new Vector3d(0.0, 0.0, 0.0), 1.0);
359 init.setTransform(gunXfm);
360 gunTransformGroup.addChild(new Box(1.0f, 1.0f, 0.5f, gunApp));
361 gunTransformGroup.addChild(getBarrel(gunApp));
362 theGun.addChild(init);
363 init.addChild(gunTransformGroup);
364 return theGun;
365 }
366
367 public static Appearance getGunAppearance() {
368 Color3f ambientColour = new Color3f(0.5f, 0.5f, 0.5f);
369 Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
370 Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
371 Color3f diffuseColour = new Color3f(0.5f, 0.5f, 0.5f);
372 float shininess = 20.0f;
373 Appearance gunApp = new Appearance();
374 gunApp.setMaterial(new Material(ambientColour, emissiveColour,
375 diffuseColour, specularColour, shininess));
376 return gunApp;
377 }
378 public static PlatformGeometry getBarrelPlatformGeometry() {
379 PlatformGeometry pg = new PlatformGeometry();
380 pg.addChild(Target.getTargetGroup());
381 return pg;
382 }
383 public static TransformGroup getBarrel(){
384 return getBarrel(getGunAppearance());
385 }
386 public static TransformGroup getBarrel(Appearance gunApp) {
387 TransformGroup barrelTransformGroup = new TransformGroup();
388 Transform3D barrelXfm = new Transform3D();
389 barrelXfm.set(new Vector3d(0.0, -2.0, 0.0));
390 barrelTransformGroup.setTransform(barrelXfm);
391 barrelTransformGroup.addChild(new Cylinder(0.3f, 4.0f, gunApp));
392 return barrelTransformGroup;
393 }
394
395 /**
396 * This builds the view branch of the scene graph.
397 *
398 * @return BranchGroup with viewing objects attached.
399 */
400 public static BranchGroup buildViewBranch(Canvas3D c) {
401 Matrix3d viewTiltMatrix3D = new Matrix3d();
402 viewTiltMatrix3D.rotX(Math.PI / -6);
403
404 Transform3D viewTransform3D = new Transform3D();
405
406 viewTransform3D.set(viewTiltMatrix3D, new Vector3d(0.0, 10.0, 10.0), 1.0);
407 TransformGroup viewTransformGroup
408 = new TransformGroup(viewTransform3D);
409 viewTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
410 viewTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
411
412 ViewPlatform myViewPlatform = new ViewPlatform();
413 PhysicalBody myPhysicalBody = new PhysicalBody();
414 PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
415 viewTransformGroup.addChild(myViewPlatform);
416 BranchGroup viewBranchGroup = new BranchGroup();
417 viewBranchGroup.addChild(getKeyNavigatorBehavior(viewTransformGroup));
418 viewBranchGroup.addChild(viewTransformGroup);
419 View myView = new View();
420 myView.addCanvas3D(c);
421 myView.attachViewPlatform(myViewPlatform);
422 myView.setPhysicalBody(myPhysicalBody);
423 myView.setPhysicalEnvironment(myEnvironment);
424 return viewBranchGroup;
425 }
426
427 private static KeyNavigatorBehavior getKeyNavigatorBehavior(TransformGroup viewXfmGroup) {
428 KeyNavigatorBehavior knb = new KeyNavigatorBehavior(viewXfmGroup);
429 knb.setSchedulingBounds(new BoundingSphere());
430 return knb;
431 }
432 }
433