001: /*
002: * $RCSfile: GeometryByReferenceTest.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:39 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.geometry_by_ref;
046:
047: import java.applet.Applet;
048: import java.awt.*;
049: import java.awt.event.*;
050: import com.sun.j3d.utils.applet.MainFrame;
051: import com.sun.j3d.utils.geometry.*;
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import javax.swing.*;
056: import javax.swing.event.*;
057: import javax.swing.border.*;
058: import com.sun.j3d.utils.behaviors.vp.*;
059:
060: public class GeometryByReferenceTest extends JApplet implements
061: ActionListener, GeometryUpdater {
062:
063: RenderingAttributes ra;
064: ColoringAttributes ca;
065: Material mat;
066: Appearance app;
067: JComboBox geomType;
068: JComboBox vertexType;
069: JComboBox colorType;
070: JCheckBox transparency;
071: JComboBox updates;
072: Shape3D shape;
073: TransparencyAttributes transp;
074: int updateIndex = 0;
075: int colorCount = 0, vertexCount = 0;
076: int vertexIndex = 0, colorIndex = 0;
077:
078: GeometryArray tetraRegular, tetraStrip, tetraIndexed,
079: tetraIndexedStrip;
080: GeometryArray[] geoArrays = new GeometryArray[4];
081:
082: private static final float sqrt3 = (float) Math.sqrt(3.0);
083: private static final float sqrt3_3 = sqrt3 / 3.0f;
084: private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
085:
086: private static final float ycenter = 0.5f * sqrt24_3;
087: private static final float zcenter = -sqrt3_3;
088:
089: private static final Point3f p1 = new Point3f(-1.0f, -ycenter,
090: -zcenter);
091: private static final Point3f p2 = new Point3f(1.0f, -ycenter,
092: -zcenter);
093: private static final Point3f p3 = new Point3f(0.0f, -ycenter,
094: -sqrt3 - zcenter);
095: private static final Point3f p4 = new Point3f(0.0f, sqrt24_3
096: - ycenter, 0.0f);
097:
098: private static final float[] floatVerts = { p1.x, p1.y, p1.z, // front face
099: p2.x, p2.y, p2.z, p4.x, p4.y, p4.z,
100:
101: p1.x, p1.y, p1.z,// left, back face
102: p4.x, p4.y, p4.z, p3.x, p3.y, p3.z,
103:
104: p2.x, p2.y, p2.z,// right, back face
105: p3.x, p3.y, p3.z, p4.x, p4.y, p4.z,
106:
107: p1.x, p1.y, p1.z,// bottom face
108: p3.x, p3.y, p3.z, p2.x, p2.y, p2.z, };
109:
110: private static final Color3f c1 = new Color3f(0.6f, 0.0f, 0.0f);
111: private static final Color3f c2 = new Color3f(0.0f, 0.6f, 0.0f);
112: private static final Color3f c3 = new Color3f(0.0f, 0.6f, 0.6f);
113: private static final Color3f c4 = new Color3f(0.6f, 0.6f, 0.0f);
114:
115: private static final float[] floatClrs = { c1.x, c1.y, c1.z, // front face
116: c2.x, c2.y, c2.z, c4.x, c4.y, c4.z,
117:
118: c1.x, c1.y, c1.z,// left, back face
119: c4.x, c4.y, c4.z, c3.x, c3.y, c3.z,
120:
121: c2.x, c2.y, c2.z,// right, back face
122: c3.x, c3.y, c3.z, c4.x, c4.y, c4.z,
123:
124: c1.x, c1.y, c1.z,// bottom face
125: c3.x, c3.y, c3.z, c2.x, c2.y, c2.z, };
126:
127: private static final float[] indexedFloatVerts = { p1.x, p1.y,
128: p1.z, p2.x, p2.y, p2.z, p3.x, p3.y, p3.z, p4.x, p4.y, p4.z,
129:
130: };
131: private static final float[] indexedFloatClrs = { c1.x, c1.y, c1.z,
132: c2.x, c2.y, c2.z, c3.x, c3.y, c3.z, c4.x, c4.y, c4.z, };
133: private static final Point3f[] p3fVerts = { p1, p2, p4, p1, p4, p3,
134: p2, p3, p4, p1, p3, p2 };
135:
136: private static final Point3f[] indexedP3fVerts = { p1, p2, p3, p4 };
137:
138: private static final Color3f[] c3fClrs = { c1, c2, c4, c1, c4, c3,
139: c2, c3, c4, c1, c3, c2 };
140:
141: private static final Color3f[] indexedC3fClrs = { c1, c2, c3, c4 };
142:
143: private static final int[] indices = { 0, 1, 3, 0, 3, 2, 1, 2, 3,
144: 0, 2, 1 };
145: private int[] stripVertexCounts = { 3, 3, 3, 3 };
146:
147: private SimpleUniverse u;
148:
149: BranchGroup createSceneGraph() {
150: BranchGroup objRoot = new BranchGroup();
151:
152: // Set up attributes to render lines
153: app = new Appearance();
154:
155: transp = new TransparencyAttributes();
156: transp.setTransparency(0.5f);
157: transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
158: transp.setTransparencyMode(TransparencyAttributes.NONE);
159: app.setTransparencyAttributes(transp);
160:
161: tetraRegular = createGeometry(1);
162: tetraStrip = createGeometry(2);
163: tetraIndexed = createGeometry(3);
164: tetraIndexedStrip = createGeometry(4);
165:
166: geoArrays[0] = tetraRegular;
167: geoArrays[1] = tetraStrip;
168: geoArrays[2] = tetraIndexed;
169: geoArrays[3] = tetraIndexedStrip;
170:
171: shape = new Shape3D(tetraRegular, app);
172: shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
173: shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
174:
175: Transform3D t = new Transform3D();
176: // move the object upwards
177: t.set(new Vector3f(0.0f, 0.3f, 0.0f));
178:
179: // rotate the shape
180: Transform3D temp = new Transform3D();
181: temp.rotX(Math.PI / 4.0d);
182: t.mul(temp);
183: temp.rotY(Math.PI / 4.0d);
184: t.mul(temp);
185:
186: // Shrink the object
187: t.setScale(0.6);
188:
189: TransformGroup trans = new TransformGroup(t);
190: trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
191: trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
192:
193: objRoot.addChild(trans);
194: trans.addChild(shape);
195:
196: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
197: 0.0, 0.0), 100.0);
198:
199: // Set up the global lights
200: Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
201: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
202: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
203:
204: AmbientLight aLgt = new AmbientLight(alColor);
205: aLgt.setInfluencingBounds(bounds);
206: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
207: lgt1.setInfluencingBounds(bounds);
208: objRoot.addChild(aLgt);
209: objRoot.addChild(lgt1);
210:
211: // Let Java 3D perform optimizations on this scene graph.
212: objRoot.compile();
213:
214: return objRoot;
215: }
216:
217: JPanel createGeometryByReferencePanel() {
218: JPanel panel = new JPanel();
219: panel.setBorder(new TitledBorder("Geometry Type"));
220:
221: String values[] = { "Array", "Strip", "Indexed", "IndexedStrip" };
222: geomType = new JComboBox(values);
223: geomType.setLightWeightPopupEnabled(false);
224: geomType.addActionListener(this );
225: geomType.setSelectedIndex(0);
226: panel.add(new JLabel("Geometry Type"));
227: panel.add(geomType);
228:
229: String vertex_types[] = { "Float", "P3F" };
230:
231: vertexType = new JComboBox(vertex_types);
232: vertexType.setLightWeightPopupEnabled(false);
233: vertexType.addActionListener(this );
234: vertexType.setSelectedIndex(0);
235: panel.add(new JLabel("VertexType"));
236: panel.add(vertexType);
237:
238: String color_types[] = { "Float", "C3F" };
239:
240: colorType = new JComboBox(color_types);
241: colorType.setLightWeightPopupEnabled(false);
242: colorType.addActionListener(this );
243: colorType.setSelectedIndex(0);
244: panel.add(new JLabel("ColorType"));
245: panel.add(colorType);
246:
247: return panel;
248: }
249:
250: JPanel createUpdatePanel() {
251:
252: JPanel panel = new JPanel();
253: panel.setBorder(new TitledBorder("Other Attributes"));
254:
255: String updateComp[] = { "None", "Geometry", "Color" };
256:
257: transparency = new JCheckBox("EnableTransparency", false);
258: transparency.addActionListener(this );
259: panel.add(transparency);
260:
261: updates = new JComboBox(updateComp);
262: updates.setLightWeightPopupEnabled(false);
263: updates.addActionListener(this );
264: updates.setSelectedIndex(0);
265: panel.add(new JLabel("UpdateData"));
266: panel.add(updates);
267:
268: return panel;
269: }
270:
271: public GeometryByReferenceTest() {
272: }
273:
274: public void init() {
275: Container contentPane = getContentPane();
276:
277: Canvas3D c = new Canvas3D(SimpleUniverse
278: .getPreferredConfiguration());
279: contentPane.add("Center", c);
280:
281: BranchGroup scene = createSceneGraph();
282: // SimpleUniverse is a Convenience Utility class
283: u = new SimpleUniverse(c);
284:
285: // add mouse behaviors to the viewingPlatform
286: ViewingPlatform viewingPlatform = u.getViewingPlatform();
287:
288: // This will move the ViewPlatform back a bit so the
289: // objects in the scene can be viewed.
290: viewingPlatform.setNominalViewingTransform();
291: u.addBranchGraph(scene);
292:
293: // add Orbit behavior to the ViewingPlatform
294: OrbitBehavior orbit = new OrbitBehavior(c,
295: OrbitBehavior.REVERSE_ALL);
296: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
297: 0.0, 0.0), 100.0);
298: orbit.setSchedulingBounds(bounds);
299: viewingPlatform.setViewPlatformBehavior(orbit);
300:
301: // Create GUI
302: JPanel p = new JPanel();
303: BoxLayout boxlayout = new BoxLayout(p, BoxLayout.Y_AXIS);
304: p.add(createGeometryByReferencePanel());
305: p.add(createUpdatePanel());
306: p.setLayout(boxlayout);
307:
308: contentPane.add("South", p);
309: }
310:
311: public void destroy() {
312: u.cleanup();
313: }
314:
315: public void actionPerformed(ActionEvent e) {
316: Object target = e.getSource();
317: GeometryArray geo;
318: boolean setColor = false, setVertex = false;
319: if (target == geomType) {
320: geo = geoArrays[geomType.getSelectedIndex()];
321: // Set everything to null, and set it later ..
322: geo.setColorRefFloat(null);
323: geo.setColorRef3f(null);
324: geo.setCoordRefFloat(null);
325: geo.setCoordRef3f(null);
326: shape.setGeometry(geoArrays[geomType.getSelectedIndex()]);
327:
328: setColor = true;
329: setVertex = true;
330:
331: } else if (target == transparency) {
332: if (transparency.isSelected()) {
333: transp
334: .setTransparencyMode(TransparencyAttributes.BLENDED);
335: } else {
336: transp.setTransparencyMode(TransparencyAttributes.NONE);
337: }
338:
339: } else if (target == updates) {
340: updateIndex = updates.getSelectedIndex();
341: if (updateIndex == 1) {
342: System.out.println("Doing coordinate update");
343: ((GeometryArray) (shape.getGeometry()))
344: .updateData(this );
345: } else if (updateIndex == 2) {
346: System.out.println("Doing color update");
347: ((GeometryArray) (shape.getGeometry()))
348: .updateData(this );
349: }
350:
351: } else if (target == vertexType) {
352: geo = ((GeometryArray) shape.getGeometry());
353: if (vertexIndex == 0) {
354: geo.setCoordRefFloat(null);
355: } else if (vertexIndex == 1) {
356: geo.setCoordRef3f(null);
357: }
358: vertexIndex = vertexType.getSelectedIndex();
359: setVertex = true;
360: } else if (target == colorType) {
361: geo = (GeometryArray) shape.getGeometry();
362: if (colorIndex == 0) {
363: geo.setColorRefFloat(null);
364: } else if (colorIndex == 1) {
365: geo.setColorRef3f(null);
366: }
367: colorIndex = colorType.getSelectedIndex();
368: setColor = true;
369: }
370:
371: if (setVertex) {
372: geo = (GeometryArray) shape.getGeometry();
373: if (vertexIndex == 0) {
374: if (geo instanceof IndexedGeometryArray)
375: geo.setCoordRefFloat(indexedFloatVerts);
376: else
377: geo.setCoordRefFloat(floatVerts);
378: } else if (vertexIndex == 1) {
379: if (geo instanceof IndexedGeometryArray)
380: geo.setCoordRef3f(indexedP3fVerts);
381: else
382: geo.setCoordRef3f(p3fVerts);
383: }
384:
385: }
386: if (setColor) {
387: geo = (GeometryArray) shape.getGeometry();
388: if (colorIndex == 0) {
389: if (geo instanceof IndexedGeometryArray)
390: geo.setColorRefFloat(indexedFloatClrs);
391: else
392: geo.setColorRefFloat(floatClrs);
393: } else if (colorIndex == 1) {
394: if (geo instanceof IndexedGeometryArray)
395: geo.setColorRef3f(indexedC3fClrs);
396: else
397: geo.setColorRef3f(c3fClrs);
398: }
399: }
400:
401: }
402:
403: public static void main(String[] args) {
404: Frame frame = new MainFrame(new GeometryByReferenceTest(), 800,
405: 800);
406: }
407:
408: public GeometryArray createGeometry(int type) {
409: GeometryArray tetra = null;
410: if (type == 1) {
411: tetra = new TriangleArray(12, TriangleArray.COORDINATES
412: | TriangleArray.COLOR_3
413: | TriangleArray.BY_REFERENCE);
414:
415: tetra.setCoordRefFloat(floatVerts);
416: tetra.setColorRefFloat(floatClrs);
417:
418: } else if (type == 2) {
419: tetra = new TriangleStripArray(12,
420: TriangleStripArray.COORDINATES
421: | TriangleStripArray.COLOR_3
422: | TriangleStripArray.BY_REFERENCE,
423: stripVertexCounts);
424: tetra.setCoordRefFloat(floatVerts);
425: tetra.setColorRefFloat(floatClrs);
426:
427: } else if (type == 3) { // Indexed Geometry
428: tetra = new IndexedTriangleArray(4,
429: IndexedTriangleArray.COORDINATES
430: | IndexedTriangleArray.COLOR_3
431: | IndexedTriangleArray.BY_REFERENCE, 12);
432: tetra.setCoordRefFloat(indexedFloatVerts);
433: tetra.setColorRefFloat(indexedFloatClrs);
434: ((IndexedTriangleArray) tetra).setCoordinateIndices(0,
435: indices);
436: ((IndexedTriangleArray) tetra).setColorIndices(0, indices);
437: } else if (type == 4) { // Indexed strip geometry
438: tetra = new IndexedTriangleStripArray(4,
439: IndexedTriangleStripArray.COORDINATES
440: | IndexedTriangleStripArray.COLOR_3
441: | IndexedTriangleStripArray.BY_REFERENCE,
442: 12, stripVertexCounts);
443: tetra.setCoordRefFloat(indexedFloatVerts);
444: tetra.setColorRefFloat(indexedFloatClrs);
445: ((IndexedTriangleStripArray) tetra).setCoordinateIndices(0,
446: indices);
447: ((IndexedTriangleStripArray) tetra).setColorIndices(0,
448: indices);
449: }
450:
451: if (tetra != null)
452: tetra.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
453: return tetra;
454: }
455:
456: public void updateData(Geometry geometry) {
457: int i;
458: float val;
459:
460: if (updateIndex == 1) { // geometry
461: // Translate the geometry by a small amount in x
462: vertexCount++;
463: if ((vertexCount & 1) == 1)
464: val = 0.2f;
465: else
466: val = -0.2f;
467:
468: if (vertexIndex == 0) {
469: // Do Indexed geometry
470: for (i = 0; i < indexedFloatVerts.length; i += 3) {
471: indexedFloatVerts[i] += val;
472: }
473: // Do non-indexed float geometry
474: for (i = 0; i < floatVerts.length; i += 3) {
475: floatVerts[i] += val;
476: }
477: } else {
478: // If p3f do each point only once
479: for (i = 0; i < indexedP3fVerts.length; i++) {
480: indexedP3fVerts[i].x += val;
481: }
482: }
483:
484: } else if (updateIndex == 2) { // colors
485: colorCount++;
486: if ((colorCount & 1) == 1)
487: val = 0.4f;
488: else
489: val = -0.4f;
490: if (colorIndex == 0) {
491: // Do Indexed geometry
492: for (i = 0; i < indexedFloatClrs.length; i += 3) {
493: indexedFloatClrs[i] += val;
494: }
495: // Do non-indexed float geometry
496: for (i = 0; i < floatClrs.length; i += 3) {
497: floatClrs[i] += val;
498: }
499: } else {
500: // If c3f do each point only once
501: for (i = 0; i < indexedC3fClrs.length; i++) {
502: indexedC3fClrs[i].x += val;
503: }
504: }
505:
506: }
507:
508: }
509: }
|