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:55 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.texture_by_ref;
046:
047: import javax.media.j3d.*;
048: import javax.vecmath.*;
049:
050: public class Tetrahedron extends Shape3D {
051:
052: private static final float sqrt3 = (float) Math.sqrt(3.0);
053: private static final float sqrt3_3 = sqrt3 / 3.0f;
054: private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
055:
056: private static final float ycenter = 0.5f * sqrt24_3;
057: private static final float zcenter = -sqrt3_3;
058:
059: private static final Point3f p1 = new Point3f(-1.0f, -ycenter,
060: -zcenter);
061: private static final Point3f p2 = new Point3f(1.0f, -ycenter,
062: -zcenter);
063: private static final Point3f p3 = new Point3f(0.0f, -ycenter,
064: -sqrt3 - zcenter);
065: private static final Point3f p4 = new Point3f(0.0f, sqrt24_3
066: - ycenter, 0.0f);
067:
068: private static final Point3f[] verts = { p1, p2, p4, // front face
069: p1, p4, p3, // left, back face
070: p2, p3, p4, // right, back face
071: p1, p3, p2, // bottom face
072: };
073:
074: private Point2f texCoord[] = { new Point2f(-0.25f, 0.0f),
075: new Point2f(1.25f, 0.0f), new Point2f(0.5f, 2.0f), };
076:
077: private TriangleArray geometryByRef;
078: private TriangleArray geometryByCopy;
079:
080: // for geometry by reference
081: private Point3f[] verticesArray = new Point3f[12];
082: private TexCoord2f[] textureCoordsArray = new TexCoord2f[12];
083: private Vector3f[] normalsArray = new Vector3f[12];
084:
085: // default to geometry by copy
086: public Tetrahedron() {
087: this (false);
088: }
089:
090: // creates a tetrahedron with geometry by reference or by copy depending on
091: // the byRef parameter
092: public Tetrahedron(boolean byRef) {
093: if (byRef) {
094: createGeometryByRef();
095: this .setGeometry(geometryByRef);
096: } else {
097: createGeometryByCopy();
098: this .setGeometry(geometryByCopy);
099: }
100: this .setCapability(Shape3D.ALLOW_GEOMETRY_READ);
101: this .setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
102: setAppearance(new Appearance());
103: }
104:
105: // create the geometry by reference and
106: // store it in the geometryByRef variable
107: public void createGeometryByRef() {
108: // System.out.println("createGeometryByRef");
109: geometryByRef = new TriangleArray(12, TriangleArray.COORDINATES
110: | TriangleArray.NORMALS
111: | TriangleArray.TEXTURE_COORDINATE_2
112: | TriangleArray.BY_REFERENCE);
113:
114: int i;
115:
116: // the coordinates
117: for (i = 0; i < 12; i++) {
118: verticesArray[i] = new Point3f(verts[i]);
119: }
120: geometryByRef.setCoordRef3f(verticesArray);
121: // System.out.println("coordinates set");
122: // Point3f[] temp1 = geometryByRef.getCoordRef3f();
123: // for (i = 0; i < 12; i++) {
124: // System.out.println(temp1[i]);
125: // }
126:
127: // the texture coordinates
128: for (i = 0; i < 12; i++) {
129: textureCoordsArray[i] = new TexCoord2f(texCoord[i % 3]);
130: }
131: geometryByRef.setTexCoordRef2f(0, textureCoordsArray);
132: // System.out.println("texture coords set");
133: // TexCoord2f[] temp2 = geometryByRef.getTexCoordRef2f(0);
134: // for (i = 0; i < 12; i++) {
135: // System.out.println(temp2[i]);
136: // }
137:
138: // the normals
139: Vector3f normal = new Vector3f();
140: Vector3f v1 = new Vector3f();
141: Vector3f v2 = new Vector3f();
142: Point3f[] pts = new Point3f[3];
143: for (int face = 0; face < 4; face++) {
144: pts[0] = new Point3f(verts[face * 3]);
145: pts[1] = new Point3f(verts[face * 3 + 1]);
146: pts[2] = new Point3f(verts[face * 3 + 2]);
147: v1.sub(pts[1], pts[0]);
148: v2.sub(pts[2], pts[0]);
149: normal.cross(v1, v2);
150: normal.normalize();
151: for (i = 0; i < 3; i++) {
152: normalsArray[face * 3 + i] = new Vector3f(normal);
153: }
154: }
155: geometryByRef.setNormalRef3f(normalsArray);
156: // System.out.println("normals set");
157: // Vector3f[] temp3 = geometryByRef.getNormalRef3f();
158: // for (i = 0; i < 12; i++) {
159: // System.out.println(temp3[i]);
160: // }
161: }
162:
163: // create the geometry by copy and store it in the geometryByCopy variable
164: public void createGeometryByCopy() {
165: int i;
166: geometryByCopy = new TriangleArray(12,
167: TriangleArray.COORDINATES | TriangleArray.NORMALS
168: | TriangleArray.TEXTURE_COORDINATE_2);
169:
170: geometryByCopy.setCoordinates(0, verts);
171:
172: for (i = 0; i < 12; i++) {
173: geometryByCopy.setTextureCoordinate(0, i, new TexCoord2f(
174: texCoord[i % 3]));
175: }
176:
177: int face;
178: Vector3f normal = new Vector3f();
179: Vector3f v1 = new Vector3f();
180: Vector3f v2 = new Vector3f();
181: Point3f[] pts = new Point3f[3];
182: for (i = 0; i < 3; i++)
183: pts[i] = new Point3f();
184:
185: for (face = 0; face < 4; face++) {
186: geometryByCopy.getCoordinates(face * 3, pts);
187: v1.sub(pts[1], pts[0]);
188: v2.sub(pts[2], pts[0]);
189: normal.cross(v1, v2);
190: normal.normalize();
191: for (i = 0; i < 3; i++) {
192: geometryByCopy.setNormal((face * 3 + i), normal);
193: }
194: }
195: }
196:
197: // set the geometry to geometryByRef or geometryByCopy depending on the
198: // parameter. Create geometryByRef or geometryByCopy if necessary
199: public void setByReference(boolean b) {
200: // System.out.println("Tetrahedron.setByReference " + b);
201: // by reference is true
202: if (b) {
203: // if there is no geometryByRef, create it
204: if (geometryByRef == null) {
205: createGeometryByRef();
206: }
207: // set the geometry
208: this .setGeometry(geometryByRef);
209: }
210: // by reference is false
211: else {
212: // if there is no geometryByCopy, create it
213: if (geometryByCopy == null) {
214: createGeometryByCopy();
215: }
216: // set the geometry
217: this.setGeometry(geometryByCopy);
218: }
219: }
220: }
|