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