001: /*
002: * $RCSfile: InterleavedNIOBuffer.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.3 $
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.awt.*;
048: import java.awt.event.*;
049: import com.sun.j3d.utils.applet.MainFrame;
050: import com.sun.j3d.utils.universe.*;
051: import com.sun.j3d.utils.image.TextureLoader;
052: import javax.media.j3d.*;
053: import javax.vecmath.*;
054: import javax.swing.*;
055: import javax.swing.border.*;
056: import com.sun.j3d.utils.behaviors.vp.*;
057: import java.nio.*;
058: import org.jdesktop.j3d.examples.Resources;
059:
060: public class InterleavedNIOBuffer extends JApplet implements
061: ActionListener {
062:
063: RenderingAttributes ra;
064: ColoringAttributes ca;
065: Material mat;
066: Appearance app;
067: JComboBox geomType;
068: JCheckBox transparency;
069: JCheckBox textureBox;
070: Shape3D shape;
071: TransparencyAttributes transp;
072:
073: GeometryArray tetraRegular, tetraStrip, tetraIndexed,
074: tetraIndexedStrip;
075: GeometryArray[] geoArrays = new GeometryArray[4];
076:
077: // Globally used colors
078: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
079: Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
080: Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
081: Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
082: Color3f[] colors = { white, red, green, blue };
083:
084: private static final float sqrt3 = (float) Math.sqrt(3.0);
085: private static final float sqrt3_3 = sqrt3 / 3.0f;
086: private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
087:
088: private static final float ycenter = 0.5f * sqrt24_3;
089: private static final float zcenter = -sqrt3_3;
090:
091: private static final Point3f p1 = new Point3f(-1.0f, -ycenter,
092: -zcenter);
093: private static final Point3f p2 = new Point3f(1.0f, -ycenter,
094: -zcenter);
095: private static final Point3f p3 = new Point3f(0.0f, -ycenter,
096: -sqrt3 - zcenter);
097: private static final Point3f p4 = new Point3f(0.0f, sqrt24_3
098: - ycenter, 0.0f);
099:
100: private static final Point2f t1 = new Point2f(0.0f, 0.0f);
101: private static final Point2f t2 = new Point2f(0.5f, 1.0f);
102: private static final Point2f t3 = new Point2f(1.0f, 0.0f);
103: private static final Point2f t4 = new Point2f(1.0f, 1.0f);
104:
105: private static final Color3f c1 = new Color3f(1.0f, 0.0f, 0.0f);
106: private static final Color3f c2 = new Color3f(0.0f, 1.0f, 0.0f);
107: private static final Color3f c3 = new Color3f(0.0f, 1.0f, 1.0f);
108: private static final Color3f c4 = new Color3f(1.0f, 1.0f, 0.0f);
109:
110: private static final float[] interleaved = { t1.x,
111: t1.y,
112: t1.x,
113: t1.y,
114: c1.x,
115: c1.y,
116: c1.z, // front face
117: p1.x,
118: p1.y,
119: p1.z, // front face
120: t2.x, t2.y, t2.x, t2.y, c2.x, c2.y, c2.z, p2.x, p2.y, p2.z,
121: t4.x, t4.y, t4.x, t4.y, c4.x, c4.y, c4.z, p4.x, p4.y, p4.z,
122:
123: t1.x, t1.y, t1.x, t1.y,
124: c1.x,
125: c1.y,
126: c1.z,// left, back face
127: p1.x,
128: p1.y,
129: p1.z,// left, back face
130: t4.x, t4.y, t4.x, t4.y, c4.x, c4.y, c4.z, p4.x, p4.y, p4.z,
131: t3.x, t3.y, t3.x, t3.y, c3.x, c3.y, c3.z, p3.x, p3.y, p3.z,
132:
133: t2.x, t2.y, t2.x, t2.y, c2.x,
134: c2.y,
135: c2.z,// right, back face
136: p2.x,
137: p2.y,
138: p2.z,// right, back face
139: t3.x, t3.y, t3.x, t3.y, c3.x, c3.y, c3.z, p3.x, p3.y, p3.z,
140: t4.x, t4.y, t4.x, t4.y, c4.x, c4.y, c4.z, p4.x, p4.y, p4.z,
141:
142: t1.x, t1.y, t1.x, t1.y, c1.x, c1.y,
143: c1.z,// bottom face
144: p1.x, p1.y,
145: p1.z,// bottom face
146: t3.x, t3.y, t3.x, t3.y, c3.x, c3.y, c3.z, p3.x, p3.y, p3.z,
147: t2.x, t2.y, t2.x, t2.y, c2.x, c2.y, c2.z, p2.x, p2.y, p2.z, };
148:
149: private static final float[] indexedInterleaved = { t1.x, t1.y,
150: t1.x, t1.y, c1.x, c1.y, c1.z, p1.x, p1.y, p1.z, t2.x, t2.y,
151: t2.x, t2.y, c2.x, c2.y, c2.z, p2.x, p2.y, p2.z, t3.x, t3.y,
152: t3.x, t3.y, c3.x, c3.y, c3.z, p3.x, p3.y, p3.z, t4.x, t4.y,
153: t4.x, t4.y, c4.x, c4.y, c4.z, p4.x, p4.y, p4.z, };
154:
155: private static final int[] indices = { 0, 1, 3, 0, 3, 2, 1, 2, 3,
156: 0, 2, 1 };
157: private int[] stripVertexCounts = { 3, 3, 3, 3 };
158:
159: TextureUnitState textureUnitState[] = new TextureUnitState[2];
160: Texture tex1;
161: Texture tex2;
162:
163: private java.net.URL texImage1 = null;
164: private java.net.URL texImage2 = null;
165:
166: private SimpleUniverse u;
167:
168: private J3DBuffer interleavedBuffer;
169: private J3DBuffer indexedInterleavedBuffer;
170:
171: void createInterleavedBuffers() {
172: int size;
173: ByteOrder order = ByteOrder.nativeOrder();
174:
175: size = (2 + 2 + 3 + 3) * 3 * 4;
176: FloatBuffer vertex = ByteBuffer.allocateDirect(size * 4).order(
177: order).asFloatBuffer();
178: vertex.put(interleaved, 0, size);
179: interleavedBuffer = new J3DBuffer(vertex);
180:
181: size = (2 + 2 + 3 + 3) * 4;
182: FloatBuffer indexedVertex = ByteBuffer.allocateDirect(size * 4)
183: .order(order).asFloatBuffer();
184: indexedVertex.put(indexedInterleaved, 0, size);
185: indexedInterleavedBuffer = new J3DBuffer(indexedVertex);
186: }
187:
188: BranchGroup createSceneGraph() {
189: BranchGroup objRoot = new BranchGroup();
190:
191: // Set up attributes to render lines
192: app = new Appearance();
193: app.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);
194:
195: transp = new TransparencyAttributes();
196: transp.setTransparency(0.5f);
197: transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
198: transp.setTransparencyMode(TransparencyAttributes.NONE);
199: app.setTransparencyAttributes(transp);
200:
201: // load textures
202: TextureAttributes texAttr1 = new TextureAttributes();
203: texAttr1.setTextureMode(TextureAttributes.DECAL);
204: TextureAttributes texAttr2 = new TextureAttributes();
205: texAttr2.setTextureMode(TextureAttributes.MODULATE);
206:
207: TextureLoader tex = new TextureLoader(texImage1, new String(
208: "RGB"), this );
209: if (tex == null)
210: return null;
211: tex1 = tex.getTexture();
212:
213: tex = new TextureLoader(texImage2, new String("RGB"), this );
214: if (tex == null)
215: return null;
216: tex2 = tex.getTexture();
217:
218: textureUnitState[0] = new TextureUnitState(tex1, texAttr1, null);
219: textureUnitState[1] = new TextureUnitState(tex2, texAttr2, null);
220:
221: createInterleavedBuffers();
222:
223: tetraRegular = createGeometry(1);
224: tetraStrip = createGeometry(2);
225: tetraIndexed = createGeometry(3);
226: tetraIndexedStrip = createGeometry(4);
227:
228: geoArrays[0] = tetraRegular;
229: geoArrays[1] = tetraStrip;
230: geoArrays[2] = tetraIndexed;
231: geoArrays[3] = tetraIndexedStrip;
232:
233: shape = new Shape3D(tetraRegular, app);
234: shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
235:
236: Transform3D t = new Transform3D();
237: // move the object upwards
238: t.set(new Vector3f(0.0f, 0.3f, 0.0f));
239:
240: // rotate the shape
241: Transform3D temp = new Transform3D();
242: temp.rotX(Math.PI / 4.0d);
243: t.mul(temp);
244: temp.rotY(Math.PI / 4.0d);
245: t.mul(temp);
246:
247: // Shrink the object
248: t.setScale(0.6);
249:
250: TransformGroup trans = new TransformGroup(t);
251: trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
252: trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
253:
254: objRoot.addChild(trans);
255: trans.addChild(shape);
256:
257: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
258: 0.0, 0.0), 100.0);
259:
260: // Set up the global lights
261: Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
262: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
263: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
264:
265: AmbientLight aLgt = new AmbientLight(alColor);
266: aLgt.setInfluencingBounds(bounds);
267: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
268: lgt1.setInfluencingBounds(bounds);
269: objRoot.addChild(aLgt);
270: objRoot.addChild(lgt1);
271:
272: // Let Java 3D perform optimizations on this scene graph.
273: objRoot.compile();
274:
275: return objRoot;
276: }
277:
278: JPanel createGeometryByReferencePanel() {
279: JPanel panel = new JPanel();
280: panel.setBorder(new TitledBorder("Geometry Type"));
281:
282: String values[] = { "Array", "Strip", "Indexed", "IndexedStrip" };
283: geomType = new JComboBox(values);
284: geomType.setLightWeightPopupEnabled(false);
285: geomType.addActionListener(this );
286: geomType.setSelectedIndex(0);
287: panel.add(new JLabel("Geometry Type"));
288: panel.add(geomType);
289:
290: transparency = new JCheckBox("EnableTransparency", false);
291: transparency.addActionListener(this );
292: panel.add(transparency);
293:
294: textureBox = new JCheckBox("EnableTexture", false);
295: textureBox.addActionListener(this );
296: panel.add(textureBox);
297:
298: return panel;
299: }
300:
301: public InterleavedNIOBuffer() {
302: }
303:
304: public InterleavedNIOBuffer(java.net.URL texURL1,
305: java.net.URL texURL2) {
306: texImage1 = texURL1;
307: texImage2 = texURL2;
308: }
309:
310: public void init() {
311:
312: // create textures
313: texImage1 = Resources.getResource("resources/images/bg.jpg");
314: if (texImage1 == null) {
315: System.err.println("resources/images/bg.jpg not found");
316: System.exit(1);
317: }
318:
319: texImage2 = Resources.getResource("resources/images/one.jpg");
320: if (texImage2 == null) {
321: System.err.println("resources/images/one.jpg not found");
322: System.exit(1);
323: }
324:
325: Container contentPane = getContentPane();
326:
327: Canvas3D c = new Canvas3D(SimpleUniverse
328: .getPreferredConfiguration());
329: contentPane.add("Center", c);
330:
331: BranchGroup scene = createSceneGraph();
332: // SimpleUniverse is a Convenience Utility class
333: u = new SimpleUniverse(c);
334:
335: // add mouse behaviors to the viewingPlatform
336: ViewingPlatform viewingPlatform = u.getViewingPlatform();
337:
338: // This will move the ViewPlatform back a bit so the
339: // objects in the scene can be viewed.
340: viewingPlatform.setNominalViewingTransform();
341:
342: // add Orbit behavior to the viewing platform
343: OrbitBehavior orbit = new OrbitBehavior(c,
344: OrbitBehavior.REVERSE_ALL);
345: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
346: 0.0, 0.0), 100.0);
347: orbit.setSchedulingBounds(bounds);
348: viewingPlatform.setViewPlatformBehavior(orbit);
349:
350: u.addBranchGraph(scene);
351:
352: // Create GUI
353: JPanel p = new JPanel();
354: BoxLayout boxlayout = new BoxLayout(p, BoxLayout.Y_AXIS);
355: p.add(createGeometryByReferencePanel());
356: p.setLayout(boxlayout);
357:
358: contentPane.add("South", p);
359: }
360:
361: public void destroy() {
362: u.cleanup();
363: }
364:
365: public void actionPerformed(ActionEvent e) {
366: Object target = e.getSource();
367: if (target == geomType) {
368: shape.setGeometry(geoArrays[geomType.getSelectedIndex()]);
369:
370: } else if (target == transparency) {
371: if (transparency.isSelected()) {
372: transp
373: .setTransparencyMode(TransparencyAttributes.BLENDED);
374: } else {
375: transp.setTransparencyMode(TransparencyAttributes.NONE);
376: }
377: } else if (target == textureBox) {
378: if (textureBox.isSelected()) {
379: app.setTextureUnitState(textureUnitState);
380: } else {
381: app.setTextureUnitState(null);
382: }
383: }
384: }
385:
386: public static void main(String[] args) {
387: java.net.URL texURL1 = null;
388: java.net.URL texURL2 = null;
389: // the path to the image for an application
390: texURL1 = Resources.getResource("resources/images/bg.jpg");
391: if (texURL1 == null) {
392: System.err.println("resources/images/bg.jpg not found");
393: System.exit(1);
394: }
395:
396: texURL2 = Resources.getResource("resources/images/one.jpg");
397: if (texURL2 == null) {
398: System.err.println("resources/images/one.jpg not found");
399: System.exit(1);
400: }
401:
402: Frame frame = new MainFrame(new InterleavedNIOBuffer(texURL1,
403: texURL2), 800, 800);
404: }
405:
406: public GeometryArray createGeometry(int type) {
407: GeometryArray tetra = null;
408: int texCoordSetMap[] = { 0, 0 };
409:
410: if (type == 1) {
411: tetra = new TriangleArray(12, TriangleArray.COORDINATES
412: | TriangleArray.COLOR_3 |
413: /*
414: TriangleArray.NORMAL_3|
415: */
416: TriangleArray.TEXTURE_COORDINATE_2
417: | TriangleArray.INTERLEAVED
418: | TriangleArray.BY_REFERENCE
419: | TriangleArray.USE_NIO_BUFFER, 2, texCoordSetMap);
420:
421: tetra.setInterleavedVertexBuffer(interleavedBuffer);
422:
423: } else if (type == 2) {
424: tetra = new TriangleStripArray(12,
425: TriangleStripArray.COORDINATES
426: | TriangleStripArray.COLOR_3 |
427: /*
428: TriangleArray.NORMAL_3|
429: */
430: TriangleArray.TEXTURE_COORDINATE_2
431: | TriangleStripArray.INTERLEAVED
432: | TriangleStripArray.BY_REFERENCE
433: | TriangleStripArray.USE_NIO_BUFFER, 2,
434: texCoordSetMap, stripVertexCounts);
435: tetra.setInterleavedVertexBuffer(interleavedBuffer);
436:
437: } else if (type == 3) { // Indexed Geometry
438: tetra = new IndexedTriangleArray(
439: 4,
440: IndexedTriangleArray.COORDINATES
441: | IndexedTriangleArray.COLOR_3 |
442: /*
443: IndexedTriangleArray.NORMAL_3|
444: */
445: IndexedTriangleArray.TEXTURE_COORDINATE_2
446: | IndexedTriangleArray.INTERLEAVED
447: | IndexedTriangleArray.BY_REFERENCE
448: | IndexedTriangleArray.USE_NIO_BUFFER
449: | IndexedTriangleArray.USE_COORD_INDEX_ONLY,
450: 2, texCoordSetMap, 12);
451: tetra.setInterleavedVertexBuffer(indexedInterleavedBuffer);
452: ((IndexedTriangleArray) tetra).setCoordinateIndices(0,
453: indices);
454: /*
455: // Do not set color or texcoord indices in UCIO mode
456: ((IndexedTriangleArray)tetra).setColorIndices(0, indices);
457: ((IndexedTriangleArray)tetra).setTextureCoordinateIndices(
458: 0, 0, indices);
459: ((IndexedTriangleArray)tetra).setTextureCoordinateIndices(
460: 1, 0, indices);
461: */
462: } else if (type == 4) { // Indexed strip geometry
463: tetra = new IndexedTriangleStripArray(4,
464: IndexedTriangleStripArray.COORDINATES
465: | IndexedTriangleStripArray.COLOR_3 |
466: /*
467: IndexedTriangleArray.NORMAL_3|
468: */
469: IndexedTriangleArray.TEXTURE_COORDINATE_2
470: | IndexedTriangleStripArray.INTERLEAVED
471: | IndexedTriangleStripArray.BY_REFERENCE
472: | IndexedTriangleStripArray.USE_NIO_BUFFER,
473: //IndexedTriangleStripArray.USE_COORD_INDEX_ONLY,
474: 2, texCoordSetMap, 12, stripVertexCounts);
475: tetra.setInterleavedVertexBuffer(indexedInterleavedBuffer);
476: ((IndexedTriangleStripArray) tetra).setCoordinateIndices(0,
477: indices);
478: ((IndexedTriangleStripArray) tetra).setColorIndices(0,
479: indices);
480: ((IndexedTriangleStripArray) tetra)
481: .setTextureCoordinateIndices(0, 0, indices);
482: ((IndexedTriangleStripArray) tetra)
483: .setTextureCoordinateIndices(1, 0, indices);
484: } else if (type == 5) { // Interleaved array
485: }
486: return tetra;
487: }
488: }
|