001: /*
002: * $RCSfile: RenderFrame.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:35 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.depth_func;
046:
047: import com.sun.j3d.utils.behaviors.mouse.*;
048: import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
049: import java.awt.GraphicsConfiguration;
050: import javax.media.j3d.*;
051: import com.sun.j3d.utils.universe.*;
052: import com.sun.j3d.utils.geometry.*;
053: import javax.vecmath.*;
054:
055: public class RenderFrame extends javax.swing.JFrame {
056:
057: DepthFuncTest dpt;
058:
059: SimpleUniverse su;
060:
061: RenderingAttributes staticWFBoxRA;
062: RenderingAttributes staticBoxRA;
063:
064: RenderingAttributes rotatingBoxRA;
065:
066: /** Creates new form RenderFrame */
067: public RenderFrame(DepthFuncTest _dpt) {
068: dpt = _dpt;
069: initComponents();
070: initUniverse();
071: }
072:
073: /** This method is called from within the constructor to
074: * initialize the form.
075: * WARNING: Do NOT modify this code. The content of this method is
076: * always regenerated by the Form Editor.
077: */
078: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
079: private void initComponents() {
080:
081: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
082: setTitle("J3D frame");
083: setBounds(400, 0, 640, 480);
084: }
085:
086: // </editor-fold>//GEN-END:initComponents
087:
088: void initUniverse() {
089: GraphicsConfiguration config = SimpleUniverse
090: .getPreferredConfiguration();
091:
092: Canvas3D c = new Canvas3D(config);
093: add("Center", c);
094: su = new SimpleUniverse(c);
095: su.addBranchGraph(createScene());
096: c.getView().setMinimumFrameCycleTime(10);
097: }
098:
099: // Variables declaration - do not modify//GEN-BEGIN:variables
100: // End of variables declaration//GEN-END:variables
101:
102: BranchGroup createScene() {
103: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
104: 0.0, 0.0), 100.0);
105:
106: BranchGroup globalBG = new BranchGroup();
107: BranchGroup rotObjectBG = new BranchGroup();
108: OrderedGroup staticObjectOG = new OrderedGroup();
109: BranchGroup lampsBG = new BranchGroup();
110: OrderedGroup oGroup = new OrderedGroup();
111: TransformGroup staticBoxRotTG = new TransformGroup();
112: staticBoxRotTG.addChild(staticObjectOG);
113: TransformGroup objectsTGRot = new TransformGroup();
114: TransformGroup objectsTGTrans = new TransformGroup();
115: Transform3D objectsTGTransT3d = new Transform3D();
116: objectsTGTransT3d.setTranslation(new Vector3f(0.0f, 0.0f,
117: -10.0f));
118: objectsTGTrans.setTransform(objectsTGTransT3d);
119: objectsTGRot.addChild(oGroup);
120: objectsTGTrans.addChild(objectsTGRot);
121: lampsBG.addChild(objectsTGTrans);
122:
123: //adding a sphere as backgroung so there is something else than flat black, and cut cube removal as an other implication. (seeing through)
124: Appearance globalSphereAppearance = new Appearance();
125: PolygonAttributes globalSpherePA = new PolygonAttributes();
126: globalSpherePA.setCullFace(globalSpherePA.CULL_FRONT);// so that interior of the sphere is visible.
127: Material globalSphereMaterial = new Material();
128: globalSphereMaterial.setEmissiveColor(.25f, .3f, .35f);
129: globalSphereAppearance.setMaterial(globalSphereMaterial);
130: globalSphereAppearance.setPolygonAttributes(globalSpherePA);
131: Sphere globalSphere = new Sphere(6.0f);
132: globalSphere.setAppearance(globalSphereAppearance);
133: globalSphere.setBounds(bounds);
134: oGroup.addChild(globalSphere);
135:
136: globalBG.addChild(lampsBG);
137:
138: // adding lamps.
139: PointLight frontLamp = new PointLight(new Color3f(1.0f, 1.0f,
140: 1.0f), new Point3f(20, 20, 20), new Point3f(0.0f, .0f,
141: 0.f));
142: lampsBG.addChild(frontLamp);
143: frontLamp.setBounds(bounds);
144: frontLamp.setInfluencingBounds(bounds);
145: PointLight backLamp = new PointLight(
146: new Color3f(1.0f, .0f, .0f),
147: new Point3f(-20, -20, -20), new Point3f(0.0f, .0f, 0.f));
148: lampsBG.addChild(backLamp);
149: backLamp.setBounds(bounds);
150: backLamp.setInfluencingBounds(bounds);
151:
152: //adding shapes.
153: {
154: //adding rotating and scaling cube
155: //doing the rotation
156: TransformGroup rotBoxTGRot = new TransformGroup();
157: rotBoxTGRot
158: .setCapability(rotBoxTGRot.ALLOW_TRANSFORM_WRITE);
159: RotationInterpolator rotBoxRotInt = new RotationInterpolator(
160: new Alpha(-1, 20000), rotBoxTGRot);
161: rotBoxRotInt.setSchedulingBounds(bounds);
162: rotBoxRotInt.setBounds(bounds);
163:
164: //doing the scaling
165: Transform3D scaleBoxt3d = new Transform3D();
166: TransformGroup rotBoxTGScale = new TransformGroup();
167: rotBoxTGScale
168: .setCapability(rotBoxTGScale.ALLOW_TRANSFORM_WRITE);
169: ScaleInterpolator rotBoxScaleInt = new ScaleInterpolator(
170: new Alpha(-1, Alpha.INCREASING_ENABLE
171: | Alpha.DECREASING_ENABLE, 0, 0, 3000,
172: 1500, 0, 3000, 1500, 0), rotBoxTGScale,
173: new Transform3D(), 0.7f, 1.6f);
174: rotBoxScaleInt.setSchedulingBounds(bounds);
175: rotBoxScaleInt.setBounds(bounds);
176:
177: Appearance rotBoxApp = new Appearance();
178: Material rotBoxMat = new Material();
179: rotBoxMat.setDiffuseColor(.4f, .4f, .4f);
180: rotBoxApp.setMaterial(rotBoxMat);
181: Box rotBox = new Box(1.1f, 1.1f, 1.1f, rotBoxApp);
182: rotBoxTGScale.addChild(rotBox);
183: rotBoxTGRot.addChild(rotBoxTGScale);
184: TransformGroup rotBoxTG = new TransformGroup();
185: rotBoxTG.addChild(rotBoxTGRot);
186: rotObjectBG.addChild(rotBoxTG);
187: rotObjectBG.addChild(rotBoxScaleInt);
188: rotObjectBG.addChild(rotBoxRotInt);
189: rotBox.setBounds(bounds);
190:
191: rotatingBoxRA = new RenderingAttributes();
192: rotatingBoxRA.setRasterOpEnable(true);
193: rotatingBoxRA
194: .setCapability(staticBoxRA.ALLOW_RASTER_OP_WRITE);
195: // rotatingBoxRA.setRasterOp( rotatingBoxRA.ROP_XOR );
196: rotBoxApp.setRenderingAttributes(rotatingBoxRA);
197:
198: rotBox.setAppearance(rotBoxApp);
199: }
200:
201: //adding static back face wireframe cube
202: {
203: Box staticWFBoxBack = new Box();
204: Appearance staticWFBoxApp = new Appearance();
205: Material staticWFBoxMat = new Material();
206: staticWFBoxMat.setDiffuseColor(0.f, 0.f, 0.f);
207: staticWFBoxMat.setEmissiveColor(0.f, .4f, 0.f);
208: staticWFBoxApp.setMaterial(staticWFBoxMat);
209: PolygonAttributes staticWFBoxPABack = new PolygonAttributes(
210: PolygonAttributes.POLYGON_LINE,
211: PolygonAttributes.CULL_FRONT, 0.0f);
212: staticWFBoxApp.setPolygonAttributes(staticWFBoxPABack);
213: staticWFBoxRA = new RenderingAttributes();
214: staticWFBoxRA
215: .setCapability(staticWFBoxRA.ALLOW_DEPTH_TEST_FUNCTION_WRITE);
216: staticWFBoxRA
217: .setCapability(staticWFBoxRA.ALLOW_DEPTH_ENABLE_WRITE);
218: staticWFBoxRA.setDepthTestFunction(staticWFBoxRA.GREATER);
219: staticWFBoxRA.setDepthBufferWriteEnable(false);
220: staticWFBoxApp.setRenderingAttributes(staticWFBoxRA);
221: staticWFBoxBack.setAppearance(staticWFBoxApp);
222: staticWFBoxBack.setBounds(bounds);
223: staticObjectOG.addChild(staticWFBoxBack);
224: }
225:
226: //adding static front face wireframe cube
227: {
228: Box staticWFBox = new Box();
229: Appearance staticWFBoxApp = new Appearance();
230: Material staticWFBoxMat = new Material();
231: staticWFBoxMat.setDiffuseColor(0.f, 0.f, 0.f);
232: staticWFBoxMat.setEmissiveColor(0.f, 1.f, 0.f);
233: staticWFBoxApp.setMaterial(staticWFBoxMat);
234: PolygonAttributes staticWFBoxPA = new PolygonAttributes(
235: PolygonAttributes.POLYGON_LINE,
236: PolygonAttributes.CULL_BACK, 0.0f);
237: staticWFBoxApp.setPolygonAttributes(staticWFBoxPA);
238: staticWFBoxApp.setRenderingAttributes(staticWFBoxRA);
239: staticWFBox.setAppearance(staticWFBoxApp);
240: staticWFBox.setBounds(bounds);
241: staticObjectOG.addChild(staticWFBox);
242: }
243:
244: {// rotating the static cubes
245: Transform3D boxt3d = new Transform3D();
246: Transform3D tempt3d = new Transform3D();
247: boxt3d.rotZ(Math.PI / 4.0f);
248: tempt3d.rotX(Math.PI / 4.0f);
249: boxt3d.mul(tempt3d);
250: tempt3d.rotY(Math.PI / 4.0f);
251: boxt3d.mul(tempt3d);
252: staticBoxRotTG.setTransform(boxt3d);
253: }
254:
255: // adding static flat cube
256: {
257: Box staticBox = new Box();
258: staticBox.setBounds(bounds);
259: Appearance boxApp = new Appearance();
260: Material boxMat = new Material();
261: boxMat.setDiffuseColor(.7f, .7f, .7f);
262: boxApp.setMaterial(boxMat);
263: staticBoxRA = new RenderingAttributes();
264: staticBoxRA
265: .setCapability(staticBoxRA.ALLOW_DEPTH_TEST_FUNCTION_WRITE);
266: staticBoxRA
267: .setCapability(staticBoxRA.ALLOW_DEPTH_ENABLE_WRITE);
268: staticBoxRA.setDepthTestFunction(staticBoxRA.LESS);
269: staticBoxRA.setDepthBufferWriteEnable(false);
270: boxApp.setRenderingAttributes(staticBoxRA);
271: staticBox.setAppearance(boxApp);
272: staticObjectOG.addChild(staticBox);
273: }
274: oGroup.addChild(rotObjectBG);
275: oGroup.addChild(staticBoxRotTG);
276:
277: //adding the mouse rotate behavior to the group of cubes.
278: MouseRotate behavior = new MouseRotate();
279: behavior.setTransformGroup(objectsTGRot);
280: objectsTGRot.addChild(behavior);
281: objectsTGRot.setCapability(objectsTGRot.ALLOW_TRANSFORM_READ);
282: objectsTGRot.setCapability(objectsTGRot.ALLOW_TRANSFORM_WRITE);
283: behavior.setSchedulingBounds(bounds);
284: return globalBG;
285: }
286:
287: public void setStaticWFObjectTestFunc(int func) {
288: staticWFBoxRA.setDepthTestFunction(func);
289: }
290:
291: public void setStaticObjectTestFunc(int func) {
292: staticBoxRA.setDepthTestFunction(func);
293: }
294:
295: public void setStaticWFObjectDBWriteStatus(boolean status) {
296: staticWFBoxRA.setDepthBufferWriteEnable(status);
297: }
298:
299: public void setStaticObjectDBWriteStatus(boolean status) {
300: staticBoxRA.setDepthBufferWriteEnable(status);
301: }
302:
303: public void setRotatingObjectROPMode(int mode) {
304: rotatingBoxRA.setRasterOp(mode);
305: }
306:
307: }
|