001: /*
002: * $RCSfile: Cylinder.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:37 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.four_by_four;
046:
047: import javax.media.j3d.*;
048: import javax.vecmath.*;
049:
050: public class Cylinder {
051:
052: float verts[];
053: float normals[];
054: QuadArray quad = null;
055: float div = 3.0f;
056: Shape3D shape;
057:
058: public Cylinder(float x, float z, float radius, float length,
059: int quality, Appearance a) {
060:
061: if (quality < 3)
062: quality = 3;
063:
064: div = (float) quality;
065:
066: verts = new float[quality * 12];
067: normals = new float[quality * 12];
068:
069: double inc = 2.0 * Math.PI / (double) div;
070: for (int i = 0; i < quality; i++) {
071: float z1 = radius * (float) Math.sin((double) i * inc) + z;
072: float x1 = radius * (float) Math.cos((double) i * inc) + x;
073: float z2 = radius
074: * (float) Math.sin((double) (i + 1) * inc) + z;
075: float x2 = radius
076: * (float) Math.cos((double) (i + 1) * inc) + x;
077:
078: verts[12 * i] = x1;
079: verts[12 * i + 1] = -length / 2.f;
080: verts[12 * i + 2] = z1;
081: verts[12 * i + 3] = x1;
082: verts[12 * i + 4] = length / 2.f;
083: verts[12 * i + 5] = z1;
084: verts[12 * i + 6] = x2;
085: verts[12 * i + 7] = length / 2.f;
086: verts[12 * i + 8] = z2;
087: verts[12 * i + 9] = x2;
088: verts[12 * i + 10] = -length / 2.f;
089: verts[12 * i + 11] = z2;
090:
091: float nz1 = (float) Math.sin((double) i * inc);
092: float nx1 = (float) Math.cos((double) i * inc);
093: float nz2 = (float) Math.sin((double) (i + 1) * inc);
094: float nx2 = (float) Math.cos((double) (i + 1) * inc);
095:
096: normals[12 * i] = nx1;
097: normals[12 * i + 1] = 0.0f;
098: normals[12 * i + 2] = nz1;
099: normals[12 * i + 3] = nx1;
100: normals[12 * i + 4] = 0.0f;
101: normals[12 * i + 5] = nz1;
102: normals[12 * i + 6] = nx2;
103: normals[12 * i + 7] = 0.0f;
104: normals[12 * i + 8] = nz2;
105: normals[12 * i + 9] = nx2;
106: normals[12 * i + 10] = 0.0f;
107: normals[12 * i + 11] = nz2;
108: }
109:
110: quad = new QuadArray(quality * 4, QuadArray.COORDINATES
111: | QuadArray.NORMALS);
112: quad.setCoordinates(0, verts);
113: quad.setNormals(0, normals);
114: shape = new Shape3D(quad, a);
115: }
116:
117: Shape3D getShape() {
118: return shape;
119: }
120: }
|