001: /*
002: * $RCSfile: Primitive.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.7 $
041: * $Date: 2007/04/24 18:50:59 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.geometry;
046:
047: import com.sun.j3d.utils.geometry.*;
048: import java.io.*;
049: import java.util.*;
050: import javax.media.j3d.*;
051: import javax.vecmath.*;
052: import java.math.*;
053:
054: /**
055: * Base class for all Java 3D primitives. By default all primitives
056: * with the same parameters share their geometry (e.g., you can have 50
057: * shperes in your scene, but the geometry is stored only once). A
058: * change to one primitive will effect all shared nodes. Another
059: * implication of this implementation is that the capabilities of the
060: * geometry are shared, and once one of the shared nodes is live, the
061: * capabilities cannot be set. Use the GEOMETRY_NOT_SHARED flag if
062: * you do not wish to share geometry among primitives with the same
063: * parameters.
064: */
065:
066: public abstract class Primitive extends Group {
067: /**
068: * Specifies that normals are generated along with the positions.
069: */
070: public static final int GENERATE_NORMALS = 0x01;
071:
072: /**
073: * Specifies that texture coordinates are generated along with the
074: * positions.
075: */
076: public static final int GENERATE_TEXTURE_COORDS = 0x02;
077:
078: /**
079: * Specifies that normals are to be flipped along the surface.
080: */
081: public static final int GENERATE_NORMALS_INWARD = 0x04;
082:
083: /**
084: * Specifies that texture coordinates are to be Y up.
085: *
086: * @since Java 3D 1.5.1
087: */
088: // Fix to Issue 411. Java 3D prefers images used for texture mapping to be Y-up
089: public static final int GENERATE_TEXTURE_COORDS_Y_UP = 0x08;
090:
091: /**
092: * Specifies that the geometry being created will not be shared by
093: * another scene graph node. By default all primitives created with
094: * the same parameters share their geometry (e.g., you can have 50
095: * spheres in your scene, but the geometry is stored only once). A
096: * change to one primitive will effect all shared nodes. You
097: * specify this flag if you do not wish to share any geometry among
098: * primitives of the same parameters. */
099: public static final int GEOMETRY_NOT_SHARED = 0x10;
100:
101: /**
102: * Specifies that the ALLOW_INTERSECT
103: * capability bit should be set on the generated geometry.
104: * This allows the object
105: * to be picked using Geometry based picking.
106: */
107: public static final int ENABLE_GEOMETRY_PICKING = 0x20;
108:
109: /**
110: * Specifies that the ALLOW_APPEARANCE_READ and
111: * ALLOW_APPEARANCE_WRITE bits are to be set on the generated
112: * geometry's Shape3D nodes.
113: */
114: public static final int ENABLE_APPEARANCE_MODIFY = 0x40;
115:
116: static final int SPHERE = 0x01;
117: static final int CYLINDER = 0x02;
118: static final int CONE = 0x04;
119: static final int BOX = 0x08;
120:
121: // used for cached geometries of Cone and Cylinder
122: static final int TOP_DISK = 0x10;
123: static final int BOTTOM_DISK = 0x20;
124: static final int CONE_DIVISIONS = 0x40;
125:
126: int numTris = 0;
127: int numVerts = 0;
128:
129: /**
130: * Primitive flags.
131: */
132: int flags;
133:
134: /**
135: * Constructs a default primitive.
136: */
137: public Primitive() {
138: flags = 0;
139: setCapability(ENABLE_PICK_REPORTING);
140: setCapability(ALLOW_CHILDREN_READ);
141: }
142:
143: /**
144: * Returns the total number of triangles in this primitive.
145: * @return the total number of triangles in this primitive
146: */
147: public int getNumTriangles() {
148: return numTris;
149: }
150:
151: /**
152: * @deprecated The number of triangles is an immutable attribute.
153: */
154: public void setNumTriangles(int num) {
155: System.err.println("Warning: setNumTriangles has no effect");
156: }
157:
158: /**
159: * Returns the total number of vertices in this primitive.
160: * @return the total number of vertices in this primitive
161: */
162: public int getNumVertices() {
163: return numVerts;
164: }
165:
166: /**
167: * @deprecated The number of vertices is an immutable attribute.
168: */
169: public void setNumVertices(int num) {
170: System.err.println("Warning: setNumVertices has no effect");
171: }
172:
173: /** Returns the flags of primitive (generate normal, textures, caching, etc).
174: */
175: public int getPrimitiveFlags() {
176: return flags;
177: }
178:
179: /**
180: * @deprecated The primitive flags must be set at construction time
181: * via one of the subclass constructors.
182: */
183: public void setPrimitiveFlags(int fl) {
184: System.err.println("Warning: setPrimitiveFlags has no effect");
185: }
186:
187: /** Obtains a shape node of a subpart of the primitive.
188: * @param partid identifier for a given subpart of the primitive.
189: */
190: public abstract Shape3D getShape(int partid);
191:
192: /** Gets the appearance of the primitive (defaults to first subpart).
193: */
194: public Appearance getAppearance() {
195: return getShape(0).getAppearance();
196: }
197:
198: /**
199: * Gets the appearance of the specified part of the primitive.
200: *
201: * @param partId identifier for a given subpart of the primitive
202: *
203: * @return The appearance object associated with the partID. If an
204: * invalid partId is passed in, null is returned.
205: *
206: * @since Java 3D 1.2.1
207: */
208: public abstract Appearance getAppearance(int partId);
209:
210: /** Sets the appearance of a subpart given a partid.
211: */
212:
213: public void setAppearance(int partid, Appearance ap) {
214: getShape(partid).setAppearance(ap);
215: }
216:
217: /** Sets the main appearance of the primitive (all subparts) to
218: * same appearance.
219: */
220: public abstract void setAppearance(Appearance ap);
221:
222: /** Sets the main appearance of the primitive (all subparts) to
223: * a default white appearance.
224: */
225: public void setAppearance() {
226:
227: Color3f aColor = new Color3f(0.1f, 0.1f, 0.1f);
228: Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
229: Color3f dColor = new Color3f(0.6f, 0.6f, 0.6f);
230: Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
231:
232: Material m = new Material(aColor, eColor, dColor, sColor,
233: 100.0f);
234: Appearance a = new Appearance();
235: m.setLightingEnable(true);
236: a.setMaterial(m);
237: setAppearance(a);
238: }
239:
240: static Hashtable geomCache = new Hashtable();
241:
242: String strfloat(float x) {
243: return (new Float(x)).toString();
244: }
245:
246: protected void cacheGeometry(int kind, float a, float b, float c,
247: int d, int e, int flags, GeomBuffer geo) {
248: String key = new String(kind + strfloat(a) + strfloat(b)
249: + strfloat(c) + d + e + flags);
250: geomCache.put(key, geo);
251: }
252:
253: protected GeomBuffer getCachedGeometry(int kind, float a, float b,
254: float c, int d, int e, int flags) {
255: String key = new String(kind + strfloat(a) + strfloat(b)
256: + strfloat(c) + d + e + flags);
257: Object cache = geomCache.get(key);
258:
259: return ((GeomBuffer) cache);
260: }
261:
262: /**
263: * Clear the shared geometry cache for all Primitive types.
264: * Existing Shapes with shared geometry will continue to share
265: * the geometry. New Primitives will create new shared geometry.
266: *
267: * @since Java 3D 1.3.2
268: */
269: public static void clearGeometryCache() {
270: geomCache.clear();
271: }
272: }
|