001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/environment/geometry/Primitive.java,v 1.1 2005/04/20 21:04:55 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.environment.geometry;
019:
020: import java.io.*;
021: import java.util.*;
022: import javax.media.j3d.*;
023: import javax.vecmath.*;
024: import java.math.*;
025:
026: /**
027: * Base class for all Java 3D primitives.
028: */
029:
030: public abstract class Primitive extends Group {
031: /**
032: * Specifies that normals are generated along with the positions.
033: **/
034: public static final int GENERATE_NORMALS = 0x01;
035:
036: /**
037: * Specifies that texture coordinates are generated along with the
038: * positions.
039: **/
040: public static final int GENERATE_TEXTURE_COORDS = 0x02;
041:
042: /**
043: * Specifies that normals are to be flipped along the surface.
044: **/
045: public static final int GENERATE_NORMALS_INWARD = 0x04;
046:
047: public static final int GENERATE_COLOR_3 = 0x08;
048:
049: /**
050: * Specifies that the geometry being created will not be shared by
051: * another scene graph node. By default all primitives created with
052: * the same parameters share their geometry (you have 50 spheres in
053: * your scene, but the geometry stored only once). A change to one
054: * primitive will effect all shared nodes.
055: * You specify this flag if you do not wish to share any geometry
056: * among primitives of the same parameters.
057: */
058: public static final int GEOMETRY_NOT_SHARED = 0x10;
059:
060: /**
061: * Specifies that the ALLOW_INTERSECT
062: * capability bit should be set on the generated geometry.
063: * This allows the object
064: * to be picked using Geometry based picking.
065: */
066: public static final int ENABLE_GEOMETRY_PICKING = 0x20;
067:
068: /**
069: * Specifies that the ALLOW_APPEARANCE_READ and
070: * ALLOW_APPEARANCE_WRITE bits are to be set on the generated
071: * geometry's Shape3D nodes.
072: */
073: public static final int ENABLE_APPEARANCE_MODIFY = 0x40;
074:
075: static final int SPHERE = 0x01;
076: static final int CYLINDER = 0x02;
077: static final int CONE = 0x04;
078: static final int BOX = 0x08;
079:
080: int numTris = 0;
081: int numVerts = 0;
082:
083: /**
084: * Primitive flags.
085: */
086: int flags;
087:
088: /** Constructs a default primitive.
089: */
090: public Primitive() {
091: flags = 0;
092: setCapability(ENABLE_PICK_REPORTING);
093: setCapability(ALLOW_CHILDREN_READ);
094: }
095:
096: /** Returns total number of triangles in this primitive.
097: */
098: public int getNumTriangles() {
099: return numTris;
100: }
101:
102: /** Sets the total number of triangles in this primitive.
103: */
104: public void setNumTriangles(int num) {
105: numTris = num;
106: }
107:
108: /** Returns total number of vertices in this primitive.
109: */
110: public int getNumVertices() {
111: return numVerts;
112: }
113:
114: /** Sets total number of vertices in this primitive.
115: */
116: public void setNumVertices(int num) {
117: numVerts = num;
118: }
119:
120: /** Returns the flags of primitive (generate normal, textures, caching, etc).
121: */
122: public int getPrimitiveFlags() {
123: return flags;
124: }
125:
126: /**
127: * @deprecated The primitive flags must be set at construction time
128: * via one of the subclass constructors.
129: */
130: public void setPrimitiveFlags(int fl) {
131: System.err.println("Warning: setPrimitiveFlags has no effect");
132: }
133:
134: /** Obtains a shape node of a subpart of the primitive.
135: * @param partid identifier for a given subpart of the primitive.
136: */
137: public abstract Shape3D getShape(int partid);
138:
139: /** Gets the appearance of the primitive (defaults to first subpart).
140: */
141: public Appearance getAppearance() {
142: return getShape(0).getAppearance();
143: }
144:
145: /** Sets the appearance of a subpart given a partid.
146: */
147:
148: public void setAppearance(int partid, Appearance ap) {
149: getShape(partid).setAppearance(ap);
150: }
151:
152: /** Sets the main appearance of the primitive (all subparts) to
153: * same appearance.
154: */
155: public abstract void setAppearance(Appearance ap);
156:
157: /** Sets the main appearance of the primitive (all subparts) to
158: * a default white appearance.
159: */
160: public void setAppearance() {
161:
162: Color3f aColor = new Color3f(0.1f, 0.1f, 0.1f);
163: Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
164: Color3f dColor = new Color3f(0.6f, 0.6f, 0.6f);
165: Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
166:
167: Material m = new Material(aColor, eColor, dColor, sColor,
168: 100.0f);
169: Appearance a = new Appearance();
170: m.setLightingEnable(true);
171: a.setMaterial(m);
172: setAppearance(a);
173: }
174:
175: static Hashtable geomCache = new Hashtable();
176:
177: String strfloat(float x) {
178: return (new Float(x)).toString();
179: }
180:
181: protected void cacheGeometry(int kind, float a, float b, float c,
182: int d, int e, int flags, GeomBuffer geo) {
183: String key = new String(kind + strfloat(a) + strfloat(b)
184: + strfloat(c) + d + e + flags);
185: geomCache.put(key, geo);
186: }
187:
188: protected GeomBuffer getCachedGeometry(int kind, float a, float b,
189: float c, int d, int e, int flags) {
190: String key = new String(kind + strfloat(a) + strfloat(b)
191: + strfloat(c) + d + e + flags);
192: Object cache = geomCache.get(key);
193:
194: return ((GeomBuffer) cache);
195: }
196: }
|