001: /*
002: * $RCSfile: GeometryByReferenceNIOBuffer.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: import java.nio.*;
060:
061: public class GeometryByReferenceNIOBuffer extends JApplet implements
062: ActionListener, GeometryUpdater {
063:
064: RenderingAttributes ra;
065: ColoringAttributes ca;
066: Material mat;
067: Appearance app;
068: JComboBox geomType;
069: JComboBox vertexType;
070: JComboBox colorType;
071: JCheckBox transparency;
072: JComboBox updates;
073: Shape3D shape;
074: TransparencyAttributes transp;
075: int updateIndex = 0;
076: int colorCount = 0, vertexCount = 0;
077: int vertexIndex = 0, colorIndex = 0;
078:
079: GeometryArray tetraRegular, tetraStrip, tetraIndexed,
080: tetraIndexedStrip;
081: GeometryArray[] geoArrays = new GeometryArray[4];
082:
083: private static final float sqrt3 = (float) Math.sqrt(3.0);
084: private static final float sqrt3_3 = sqrt3 / 3.0f;
085: private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
086:
087: private static final float ycenter = 0.5f * sqrt24_3;
088: private static final float zcenter = -sqrt3_3;
089:
090: private static final Point3f p1 = new Point3f(-1.0f, -ycenter,
091: -zcenter);
092: private static final Point3f p2 = new Point3f(1.0f, -ycenter,
093: -zcenter);
094: private static final Point3f p3 = new Point3f(0.0f, -ycenter,
095: -sqrt3 - zcenter);
096: private static final Point3f p4 = new Point3f(0.0f, sqrt24_3
097: - ycenter, 0.0f);
098:
099: private static final float[] floatVerts = { p1.x, p1.y, p1.z, // front face
100: p2.x, p2.y, p2.z, p4.x, p4.y, p4.z,
101:
102: p1.x, p1.y, p1.z,// left, back face
103: p4.x, p4.y, p4.z, p3.x, p3.y, p3.z,
104:
105: p2.x, p2.y, p2.z,// right, back face
106: p3.x, p3.y, p3.z, p4.x, p4.y, p4.z,
107:
108: p1.x, p1.y, p1.z,// bottom face
109: p3.x, p3.y, p3.z, p2.x, p2.y, p2.z, };
110:
111: private static final Color3f c1 = new Color3f(0.6f, 0.0f, 0.0f);
112: private static final Color3f c2 = new Color3f(0.0f, 0.6f, 0.0f);
113: private static final Color3f c3 = new Color3f(0.0f, 0.6f, 0.6f);
114: private static final Color3f c4 = new Color3f(0.6f, 0.6f, 0.0f);
115:
116: private static final float[] floatClrs = { c1.x, c1.y, c1.z, // front face
117: c2.x, c2.y, c2.z, c4.x, c4.y, c4.z,
118:
119: c1.x, c1.y, c1.z,// left, back face
120: c4.x, c4.y, c4.z, c3.x, c3.y, c3.z,
121:
122: c2.x, c2.y, c2.z,// right, back face
123: c3.x, c3.y, c3.z, c4.x, c4.y, c4.z,
124:
125: c1.x, c1.y, c1.z,// bottom face
126: c3.x, c3.y, c3.z, c2.x, c2.y, c2.z, };
127:
128: private static final float[] indexedFloatVerts = { p1.x, p1.y,
129: p1.z, p2.x, p2.y, p2.z, p3.x, p3.y, p3.z, p4.x, p4.y, p4.z,
130:
131: };
132:
133: private static final float[] indexedFloatClrs = { c1.x, c1.y, c1.z,
134: c2.x, c2.y, c2.z, c3.x, c3.y, c3.z, c4.x, c4.y, c4.z, };
135:
136: private static final int[] indices = { 0, 1, 3, 0, 3, 2, 1, 2, 3,
137: 0, 2, 1 };
138: private int[] stripVertexCounts = { 3, 3, 3, 3 };
139:
140: private SimpleUniverse u;
141:
142: private J3DBuffer floatBufferCoord;
143: private J3DBuffer floatBufferColor;
144: private J3DBuffer indexedFloatBufferCoord;
145: private J3DBuffer indexedFloatBufferColor;
146:
147: void createJ3DBuffers() {
148: int i;
149: ByteOrder order = ByteOrder.nativeOrder();
150:
151: FloatBuffer coord = ByteBuffer.allocateDirect(36 * 4).order(
152: order).asFloatBuffer();
153: coord.put(floatVerts, 0, 36);
154: floatBufferCoord = new J3DBuffer(coord);
155:
156: FloatBuffer color = ByteBuffer.allocateDirect(36 * 4).order(
157: order).asFloatBuffer();
158: color.put(floatClrs, 0, 36);
159: floatBufferColor = new J3DBuffer(color);
160:
161: FloatBuffer indexedCoord = ByteBuffer.allocateDirect(12 * 4)
162: .order(order).asFloatBuffer();
163: indexedCoord.put(indexedFloatVerts, 0, 12);
164: indexedFloatBufferCoord = new J3DBuffer(indexedCoord);
165:
166: FloatBuffer indexedColor = ByteBuffer.allocateDirect(12 * 4)
167: .order(order).asFloatBuffer();
168: indexedColor.put(indexedFloatClrs, 0, 12);
169: indexedFloatBufferColor = new J3DBuffer(indexedColor);
170: }
171:
172: BranchGroup createSceneGraph() {
173: BranchGroup objRoot = new BranchGroup();
174:
175: // Set up attributes to render lines
176: app = new Appearance();
177:
178: transp = new TransparencyAttributes();
179: transp.setTransparency(0.5f);
180: transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
181: transp.setTransparencyMode(TransparencyAttributes.NONE);
182: app.setTransparencyAttributes(transp);
183:
184: //create the direct nio buffer
185: createJ3DBuffers();
186:
187: tetraRegular = createGeometry(1);
188: tetraStrip = createGeometry(2);
189: tetraIndexed = createGeometry(3);
190: tetraIndexedStrip = createGeometry(4);
191:
192: geoArrays[0] = tetraRegular;
193: geoArrays[1] = tetraStrip;
194: geoArrays[2] = tetraIndexed;
195: geoArrays[3] = tetraIndexedStrip;
196:
197: shape = new Shape3D(tetraRegular, app);
198: shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
199: shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
200:
201: Transform3D t = new Transform3D();
202: // move the object upwards
203: t.set(new Vector3f(0.0f, 0.3f, 0.0f));
204:
205: // rotate the shape
206: Transform3D temp = new Transform3D();
207: temp.rotX(Math.PI / 4.0d);
208: t.mul(temp);
209: temp.rotY(Math.PI / 4.0d);
210: t.mul(temp);
211:
212: // Shrink the object
213: t.setScale(0.6);
214:
215: TransformGroup trans = new TransformGroup(t);
216: trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
217: trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
218:
219: objRoot.addChild(trans);
220: trans.addChild(shape);
221:
222: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
223: 0.0, 0.0), 100.0);
224:
225: // Set up the global lights
226: Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
227: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
228: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
229:
230: AmbientLight aLgt = new AmbientLight(alColor);
231: aLgt.setInfluencingBounds(bounds);
232: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
233: lgt1.setInfluencingBounds(bounds);
234: objRoot.addChild(aLgt);
235: objRoot.addChild(lgt1);
236:
237: // Let Java 3D perform optimizations on this scene graph.
238: objRoot.compile();
239:
240: return objRoot;
241: }
242:
243: JPanel createGeometryByReferencePanel() {
244: JPanel panel = new JPanel();
245: panel.setBorder(new TitledBorder("Geometry Type"));
246:
247: String values[] = { "Array", "Strip", "Indexed", "IndexedStrip" };
248: geomType = new JComboBox(values);
249: geomType.setLightWeightPopupEnabled(false);
250: geomType.addActionListener(this );
251: geomType.setSelectedIndex(0);
252: panel.add(new JLabel("Geometry Type"));
253: panel.add(geomType);
254:
255: return panel;
256: }
257:
258: JPanel createUpdatePanel() {
259:
260: JPanel panel = new JPanel();
261: panel.setBorder(new TitledBorder("Other Attributes"));
262:
263: String updateComp[] = { "None", "Geometry", "Color" };
264:
265: transparency = new JCheckBox("EnableTransparency", false);
266: transparency.addActionListener(this );
267: panel.add(transparency);
268:
269: updates = new JComboBox(updateComp);
270: updates.setLightWeightPopupEnabled(false);
271: updates.addActionListener(this );
272: updates.setSelectedIndex(0);
273: panel.add(new JLabel("UpdateData"));
274: panel.add(updates);
275:
276: return panel;
277: }
278:
279: public GeometryByReferenceNIOBuffer() {
280: }
281:
282: public void init() {
283: Container contentPane = getContentPane();
284:
285: Canvas3D c = new Canvas3D(SimpleUniverse
286: .getPreferredConfiguration());
287: contentPane.add("Center", c);
288:
289: BranchGroup scene = createSceneGraph();
290: // SimpleUniverse is a Convenience Utility class
291: u = new SimpleUniverse(c);
292:
293: // add mouse behaviors to the viewingPlatform
294: ViewingPlatform viewingPlatform = u.getViewingPlatform();
295:
296: // This will move the ViewPlatform back a bit so the
297: // objects in the scene can be viewed.
298: viewingPlatform.setNominalViewingTransform();
299: u.addBranchGraph(scene);
300:
301: // add Orbit behavior to the ViewingPlatform
302: OrbitBehavior orbit = new OrbitBehavior(c,
303: OrbitBehavior.REVERSE_ALL);
304: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
305: 0.0, 0.0), 100.0);
306: orbit.setSchedulingBounds(bounds);
307: viewingPlatform.setViewPlatformBehavior(orbit);
308:
309: // Create GUI
310: JPanel p = new JPanel();
311: BoxLayout boxlayout = new BoxLayout(p, BoxLayout.Y_AXIS);
312: p.add(createGeometryByReferencePanel());
313: p.add(createUpdatePanel());
314: p.setLayout(boxlayout);
315:
316: contentPane.add("South", p);
317: }
318:
319: public void destroy() {
320: u.cleanup();
321: }
322:
323: public void actionPerformed(ActionEvent e) {
324: Object target = e.getSource();
325: GeometryArray geo;
326: boolean setColor = false, setVertex = false;
327: if (target == geomType) {
328: geo = geoArrays[geomType.getSelectedIndex()];
329: // Set everything to null, and set it later ..
330: geo.setColorRefBuffer(null);
331: geo.setCoordRefBuffer(null);
332: shape.setGeometry(geoArrays[geomType.getSelectedIndex()]);
333:
334: setColor = true;
335: setVertex = true;
336:
337: } else if (target == transparency) {
338: if (transparency.isSelected()) {
339: transp
340: .setTransparencyMode(TransparencyAttributes.BLENDED);
341: } else {
342: transp.setTransparencyMode(TransparencyAttributes.NONE);
343: }
344:
345: } else if (target == updates) {
346: updateIndex = updates.getSelectedIndex();
347: if (updateIndex == 1) {
348: System.out.println("Doing coordinate update");
349: ((GeometryArray) (shape.getGeometry()))
350: .updateData(this );
351: } else if (updateIndex == 2) {
352: System.out.println("Doing color update");
353: ((GeometryArray) (shape.getGeometry()))
354: .updateData(this );
355: }
356:
357: }
358:
359: if (setVertex) {
360: geo = (GeometryArray) shape.getGeometry();
361: if (geo instanceof IndexedGeometryArray)
362: geo.setCoordRefBuffer(indexedFloatBufferCoord);
363: else
364: geo.setCoordRefBuffer(floatBufferCoord);
365:
366: }
367: if (setColor) {
368: geo = (GeometryArray) shape.getGeometry();
369: if (geo instanceof IndexedGeometryArray)
370: geo.setColorRefBuffer(indexedFloatBufferColor);
371: else
372: geo.setColorRefBuffer(floatBufferColor);
373: }
374: }
375:
376: public static void main(String[] args) {
377: Frame frame = new MainFrame(new GeometryByReferenceNIOBuffer(),
378: 800, 800);
379: }
380:
381: public GeometryArray createGeometry(int type) {
382: GeometryArray tetra = null;
383: if (type == 1) {
384: tetra = new TriangleArray(12, TriangleArray.COORDINATES
385: | TriangleArray.COLOR_3
386: | TriangleArray.BY_REFERENCE
387: | TriangleArray.USE_NIO_BUFFER);
388:
389: tetra.setCoordRefBuffer(floatBufferCoord);
390: tetra.setColorRefBuffer(floatBufferColor);
391:
392: } else if (type == 2) {
393: tetra = new TriangleStripArray(12,
394: TriangleStripArray.COORDINATES
395: | TriangleStripArray.COLOR_3
396: | TriangleStripArray.BY_REFERENCE
397: | TriangleStripArray.USE_NIO_BUFFER,
398: stripVertexCounts);
399: tetra.setCoordRefBuffer(floatBufferCoord);
400: tetra.setColorRefBuffer(floatBufferColor);
401:
402: } else if (type == 3) { // Indexed Geometry
403: tetra = new IndexedTriangleArray(4,
404: IndexedTriangleArray.COORDINATES
405: | IndexedTriangleArray.COLOR_3
406: | IndexedTriangleArray.BY_REFERENCE
407: | IndexedTriangleArray.USE_NIO_BUFFER,
408: //IndexedTriangleStripArray.USE_COORD_INDEX_ONLY,
409: 12);
410: tetra.setCoordRefBuffer(indexedFloatBufferCoord);
411: tetra.setColorRefBuffer(indexedFloatBufferColor);
412: ((IndexedTriangleArray) tetra).setCoordinateIndices(0,
413: indices);
414: ((IndexedTriangleArray) tetra).setColorIndices(0, indices);
415: } else if (type == 4) { // Indexed strip geometry
416: tetra = new IndexedTriangleStripArray(
417: 4,
418: IndexedTriangleStripArray.COORDINATES
419: | IndexedTriangleStripArray.COLOR_3
420: | IndexedTriangleStripArray.BY_REFERENCE
421: | IndexedTriangleStripArray.USE_NIO_BUFFER
422: | IndexedTriangleStripArray.USE_COORD_INDEX_ONLY,
423: 12, stripVertexCounts);
424: tetra.setCoordRefBuffer(indexedFloatBufferCoord);
425: tetra.setColorRefBuffer(indexedFloatBufferColor);
426: ((IndexedTriangleStripArray) tetra).setCoordinateIndices(0,
427: indices);
428: /*
429: // Do not set color indices in UCIO mode
430: ((IndexedTriangleStripArray)tetra).setColorIndices(0, indices);
431: */
432: }
433:
434: if (tetra != null)
435: tetra.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
436: return tetra;
437: }
438:
439: public void updateData(Geometry geometry) {
440: int i;
441: float val;
442: float val1;
443: if (updateIndex == 1) { // geometry
444: // Translate the geometry by a small amount in x
445: vertexCount++;
446: if ((vertexCount & 1) == 1)
447: val = 0.2f;
448: else
449: val = -0.2f;
450:
451: FloatBuffer indexedCoord = (FloatBuffer) indexedFloatBufferCoord
452: .getBuffer();
453: indexedCoord.rewind();
454: FloatBuffer coord = (FloatBuffer) floatBufferCoord
455: .getBuffer();
456: coord.rewind();
457:
458: if (vertexIndex == 0) {
459: // Do Indexed geometry
460: for (i = 0; i < indexedCoord.limit(); i += 3) {
461: val1 = indexedCoord.get(i);
462: indexedCoord.put(i, val1 + val);
463: }
464: // Do non-indexed float geometry
465: for (i = 0; i < coord.limit(); i += 3) {
466: val1 = coord.get(i);
467: coord.put(i, val1 + val);
468: }
469: }
470: } else if (updateIndex == 2) { // colors
471: colorCount++;
472: if ((colorCount & 1) == 1)
473: val = 0.4f;
474: else
475: val = -0.4f;
476:
477: FloatBuffer indexedColors = (FloatBuffer) indexedFloatBufferColor
478: .getBuffer();
479: indexedColors.rewind();
480: FloatBuffer colors = (FloatBuffer) floatBufferColor
481: .getBuffer();
482: colors.rewind();
483:
484: if (colorIndex == 0) {
485: // Do Indexed geometry
486: for (i = 0; i < indexedColors.limit(); i += 3) {
487: indexedColors.put(i, indexedColors.get(i) + val);
488: }
489: // Do non-indexed float geometry
490: for (i = 0; i < colors.limit(); i += 3) {
491: colors.put(i, colors.get(i) + val);
492: }
493: }
494:
495: }
496:
497: }
498: }
|