001: /*
002: * $RCSfile: Tetrahedron.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:49 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.picking;
046:
047: import javax.media.j3d.*;
048: import javax.vecmath.*;
049:
050: public class Tetrahedron extends Shape3D {
051: private static final float sqrt3 = (float) Math.sqrt(3.0);
052: private static final float sqrt3_3 = sqrt3 / 3.0f;
053: private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
054:
055: private static final float ycenter = 0.5f * sqrt24_3;
056: private static final float zcenter = -sqrt3_3;
057:
058: private static final Point3f p1 = new Point3f(-1.0f, -ycenter,
059: -zcenter);
060: private static final Point3f p2 = new Point3f(1.0f, -ycenter,
061: -zcenter);
062: private static final Point3f p3 = new Point3f(0.0f, -ycenter,
063: -sqrt3 - zcenter);
064: private static final Point3f p4 = new Point3f(0.0f, sqrt24_3
065: - ycenter, 0.0f);
066:
067: private static final Point3f[] verts = { p1, p2, p4, // front face
068: p1, p4, p3, // left, back face
069: p2, p3, p4, // right, back face
070: p1, p3, p2, // bottom face
071: };
072:
073: private TexCoord2f texCoord[] = { new TexCoord2f(0.0f, 0.0f),
074: new TexCoord2f(1.0f, 0.0f),
075: new TexCoord2f(0.5f, sqrt3 / 2.0f), };
076:
077: public Tetrahedron() {
078: int i;
079:
080: TriangleArray tetra = new TriangleArray(12,
081: TriangleArray.COORDINATES | TriangleArray.NORMALS
082: | TriangleArray.TEXTURE_COORDINATE_2);
083:
084: tetra.setCoordinates(0, verts);
085: for (i = 0; i < 12; i++) {
086: tetra.setTextureCoordinate(0, i, texCoord[i % 3]);
087: }
088:
089: int face;
090: Vector3f normal = new Vector3f();
091: Vector3f v1 = new Vector3f();
092: Vector3f v2 = new Vector3f();
093: Point3f[] pts = new Point3f[3];
094: for (i = 0; i < 3; i++)
095: pts[i] = new Point3f();
096:
097: for (face = 0; face < 4; face++) {
098: tetra.getCoordinates(face * 3, pts);
099: v1.sub(pts[1], pts[0]);
100: v2.sub(pts[2], pts[0]);
101: normal.cross(v1, v2);
102: normal.normalize();
103: for (i = 0; i < 3; i++) {
104: tetra.setNormal((face * 3 + i), normal);
105: }
106: }
107:
108: tetra.setCapability(Geometry.ALLOW_INTERSECT);
109:
110: this .setGeometry(tetra);
111: this .setAppearance(new Appearance());
112: }
113: }
|