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