001: /*
002: * $RCSfile: Shaft.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:38 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.gears;
046:
047: import java.lang.Math.*;
048: import javax.media.j3d.*;
049: import javax.vecmath.*;
050:
051: public class Shaft extends javax.media.j3d.TransformGroup {
052:
053: /**
054: * Construct a Shaft;
055: * @return a new shaft that with the specified radius centered about
056: * the origin an laying in the XY plane and of a specified length
057: * extending in the Z dimension
058: * @param radius radius of shaft
059: * @param length shaft length shaft extends from -length/2 to length/2 in
060: * the Z dimension
061: * @param segmentCount number of segments for the shaft face
062: * @param look the Appearance to associate with this shaft
063: */
064: public Shaft(float radius, float length, int segmentCount,
065: Appearance look) {
066: // The direction of the ray from the shaft's center
067: float xDirection, yDirection;
068: float xShaft, yShaft;
069:
070: // The z coordinates for the shaft's faces (never change)
071: float frontZ = -0.5f * length;
072: float rearZ = 0.5f * length;
073:
074: int shaftFaceVertexCount; // #(vertices) per shaft face
075: int shaftFaceTotalVertexCount; // total #(vertices) in all teeth
076: int shaftFaceStripCount[] = new int[1]; // per shaft vertex count
077: int shaftVertexCount; // #(vertices) for shaft
078: int shaftStripCount[] = new int[1]; // #(vertices) in strip/strip
079:
080: // Front and rear facing normals for the shaft's faces
081: Vector3f frontNormal = new Vector3f(0.0f, 0.0f, -1.0f);
082: Vector3f rearNormal = new Vector3f(0.0f, 0.0f, 1.0f);
083: // Outward facing normal
084: Vector3f outNormal = new Vector3f(1.0f, 0.0f, 0.0f);
085:
086: // Temporary variables for storing coordinates and vectors
087: Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
088: Shape3D newShape;
089:
090: // The angle subtended by a single segment
091: double segmentAngle = 2.0 * Math.PI / segmentCount;
092: double tempAngle;
093:
094: // Allow this object to spin. etc.
095: this .setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
096:
097: /* for the forward facing fan:
098: * ___3___
099: * - | -
100: * / | \
101: * 4/\ | /\2
102: * / \ | / \
103: * / \ | / \
104: * : \ | / :
105: * |--------------- *----------------|
106: * 5 0 1
107: *
108: * for backward facing fan exchange 1 with 5; 2 with 4, etc.
109: */
110:
111: // Construct the shaft's front and rear face
112: shaftFaceVertexCount = segmentCount + 2;
113: shaftFaceStripCount[0] = shaftFaceVertexCount;
114:
115: TriangleFanArray frontShaftFace = new TriangleFanArray(
116: shaftFaceVertexCount, GeometryArray.COORDINATES
117: | GeometryArray.NORMALS, shaftFaceStripCount);
118:
119: TriangleFanArray rearShaftFace = new TriangleFanArray(
120: shaftFaceVertexCount, GeometryArray.COORDINATES
121: | GeometryArray.NORMALS, shaftFaceStripCount);
122:
123: coordinate.set(0.0f, 0.0f, frontZ);
124: frontShaftFace.setCoordinate(0, coordinate);
125: frontShaftFace.setNormal(0, frontNormal);
126:
127: coordinate.set(0.0f, 0.0f, rearZ);
128: rearShaftFace.setCoordinate(0, coordinate);
129: rearShaftFace.setNormal(0, rearNormal);
130:
131: for (int index = 1; index < segmentCount + 2; index++) {
132:
133: tempAngle = segmentAngle * -(double) index;
134: coordinate.set(radius * (float) Math.cos(tempAngle), radius
135: * (float) Math.sin(tempAngle), frontZ);
136: frontShaftFace.setCoordinate(index, coordinate);
137: frontShaftFace.setNormal(index, frontNormal);
138:
139: tempAngle = -tempAngle;
140: coordinate.set(radius * (float) Math.cos(tempAngle), radius
141: * (float) Math.sin(tempAngle), rearZ);
142: rearShaftFace.setCoordinate(index, coordinate);
143: rearShaftFace.setNormal(index, rearNormal);
144: }
145: newShape = new Shape3D(frontShaftFace, look);
146: this .addChild(newShape);
147: newShape = new Shape3D(rearShaftFace, look);
148: this .addChild(newShape);
149:
150: // Construct shaft's outer skin (the cylinder body)
151: shaftVertexCount = 2 * segmentCount + 2;
152: shaftStripCount[0] = shaftVertexCount;
153:
154: TriangleStripArray shaft = new TriangleStripArray(
155: shaftVertexCount, GeometryArray.COORDINATES
156: | GeometryArray.NORMALS, shaftStripCount);
157:
158: outNormal.set(1.0f, 0.0f, 0.0f);
159:
160: coordinate.set(radius, 0.0f, rearZ);
161: shaft.setCoordinate(0, coordinate);
162: shaft.setNormal(0, outNormal);
163:
164: coordinate.set(radius, 0.0f, frontZ);
165: shaft.setCoordinate(1, coordinate);
166: shaft.setNormal(1, outNormal);
167:
168: for (int count = 0; count < segmentCount; count++) {
169: int index = 2 + count * 2;
170:
171: tempAngle = segmentAngle * (double) (count + 1);
172: xDirection = (float) Math.cos(tempAngle);
173: yDirection = (float) Math.sin(tempAngle);
174: xShaft = radius * xDirection;
175: yShaft = radius * yDirection;
176: outNormal.set(xDirection, yDirection, 0.0f);
177:
178: coordinate.set(xShaft, yShaft, rearZ);
179: shaft.setCoordinate(index, coordinate);
180: shaft.setNormal(index, outNormal);
181:
182: coordinate.set(xShaft, yShaft, frontZ);
183: shaft.setCoordinate(index + 1, coordinate);
184: shaft.setNormal(index + 1, outNormal);
185: }
186: newShape = new Shape3D(shaft, look);
187: this.addChild(newShape);
188: }
189: }
|