001: /*
002: * $RCSfile: ModelClipTest2.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:44 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.model_clip;
046:
047: import com.sun.j3d.utils.behaviors.mouse.*;
048: import com.sun.j3d.utils.geometry.Cylinder;
049: import com.sun.j3d.utils.geometry.Box;
050: import java.applet.Applet;
051: import java.awt.*;
052: import java.awt.event.*;
053: import com.sun.j3d.utils.applet.MainFrame;
054: import com.sun.j3d.utils.universe.*;
055: import javax.media.j3d.*;
056: import javax.vecmath.*;
057:
058: /**
059: * ModelClipTest2 draws a cylinder and creates two clip planes
060: * to see the interior of the cylinder. It also has a behavior to
061: * move the clip planes.
062: */
063: public class ModelClipTest2 extends Applet {
064:
065: private SimpleUniverse u;
066:
067: public BranchGroup createSceneGraph() {
068: // Create the root of the branch graph
069: BranchGroup objRoot = new BranchGroup();
070:
071: // Create a Transformgroup to scale all objects so they
072: // appear in the scene.
073: TransformGroup objScale = new TransformGroup();
074: Transform3D t3d = new Transform3D();
075: t3d.setScale(0.4);
076: objScale.setTransform(t3d);
077: objRoot.addChild(objScale);
078:
079: // Create lights
080: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
081: 0.0, 0.0), 100.0);
082:
083: //Shine it with two colored lights.
084: Color3f lColor0 = new Color3f(1.0f, 1.0f, 1.0f);
085: Color3f lColor1 = new Color3f(0.5f, 0.0f, 0.5f);
086: Color3f lColor2 = new Color3f(0.7f, 0.7f, 0.0f);
087: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, 1.0f);
088: Vector3f lDir2 = new Vector3f(0.0f, 0.0f, -1.0f);
089:
090: AmbientLight lgt0 = new AmbientLight(true, lColor2);
091: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
092: DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
093: lgt0.setInfluencingBounds(bounds);
094: lgt1.setInfluencingBounds(bounds);
095: lgt2.setInfluencingBounds(bounds);
096: objScale.addChild(lgt0);
097: objScale.addChild(lgt1);
098: objScale.addChild(lgt2);
099:
100: // Create a Transformgroup for the geometry
101: TransformGroup objRot = new TransformGroup();
102: Transform3D t3d1 = new Transform3D();
103: AxisAngle4f rot1 = new AxisAngle4f(0.0f, 1.0f, 0.0f, 45.0f);
104: t3d1.setRotation(rot1);
105: objRot.setTransform(t3d1);
106: objScale.addChild(objRot);
107:
108: //Create a cylinder
109: PolygonAttributes attr = new PolygonAttributes();
110: attr.setCullFace(PolygonAttributes.CULL_NONE);
111: Appearance ap = new Appearance();
112: Material mat = new Material();
113: mat.setLightingEnable(true);
114: ap.setMaterial(mat);
115: ap.setPolygonAttributes(attr);
116:
117: Cylinder CylinderObj = new Cylinder(0.5f, 2.2f, ap);
118: objRot.addChild(CylinderObj);
119:
120: //Create a box
121: Box BoxObj = new Box(0.8f, 0.8f, 0.8f, ap);
122: objRot.addChild(BoxObj);
123:
124: // This Transformgroup is used by the mouse manipulators to
125: // move the model clip planes.
126: TransformGroup objTrans = new TransformGroup();
127: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
128: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
129: objRot.addChild(objTrans);
130:
131: // Create the rotate behavior node
132: MouseRotate behavior = new MouseRotate(objTrans);
133: objTrans.addChild(behavior);
134: behavior.setSchedulingBounds(bounds);
135:
136: // Create the zoom behavior node
137: MouseZoom behavior2 = new MouseZoom(objTrans);
138: objTrans.addChild(behavior2);
139: behavior2.setSchedulingBounds(bounds);
140:
141: //Create Model Clip
142: ModelClip mc = new ModelClip();
143: boolean enables[] = { false, false, false, false, false, false };
144: Vector4d eqn = new Vector4d(0.0, 1.0, 1.0, 0.0);
145: mc.setEnables(enables);
146: mc.setPlane(1, eqn);
147: mc.setEnable(1, true);
148: mc.setInfluencingBounds(bounds);
149: objTrans.addChild(mc);
150:
151: // Let Java 3D perform optimizations on this scene graph.
152: objRoot.compile();
153:
154: return objRoot;
155: }
156:
157: public ModelClipTest2() {
158: }
159:
160: public void init() {
161: setLayout(new BorderLayout());
162: GraphicsConfiguration config = SimpleUniverse
163: .getPreferredConfiguration();
164:
165: Canvas3D c = new Canvas3D(config);
166: add("Center", c);
167:
168: // Create a simple scene and attach it to the virtual universe
169: BranchGroup scene = createSceneGraph();
170: u = new SimpleUniverse(c);
171:
172: // This will move the ViewPlatform back a bit so the
173: // objects in the scene can be viewed.
174: u.getViewingPlatform().setNominalViewingTransform();
175:
176: u.addBranchGraph(scene);
177: }
178:
179: public void destroy() {
180: u.cleanup();
181: }
182:
183: public static void main(String argv[]) {
184:
185: BranchGroup group;
186:
187: new MainFrame(new ModelClipTest2(), 500, 500);
188: }
189: }
|