001: /*
002: * $RCSfile: Cube.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 java.applet.Applet;
048: import java.awt.event.*;
049: import javax.media.j3d.*;
050: import javax.vecmath.*;
051:
052: public class Cube extends Object {
053:
054: private Shape3D shape3D;
055:
056: private static final float[] verts = {
057: // Front Face
058: 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
059: 1.0f,
060: 1.0f,
061: -1.0f,
062: -1.0f,
063: 1.0f,
064: // Back Face
065: -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
066: -1.0f,
067: 1.0f,
068: -1.0f,
069: -1.0f,
070: // Right Face
071: 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
072: 1.0f,
073: -1.0f,
074: 1.0f,
075: // Left Face
076: -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f,
077: -1.0f, -1.0f,
078: -1.0f,
079: // Top Face
080: 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
081: -1.0f, 1.0f, 1.0f,
082: // Bottom Face
083: -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
084: -1.0f, 1.0f, -1.0f, 1.0f, };
085:
086: private static final float[] normals = {
087: // Front Face
088: 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
089: 0.0f,
090: 1.0f,
091: 0.0f,
092: 0.0f,
093: 1.0f,
094: // Back Face
095: 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
096: -1.0f,
097: 0.0f,
098: 0.0f,
099: -1.0f,
100: // Right Face
101: 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
102: 1.0f,
103: 0.0f,
104: 0.0f,
105: // Left Face
106: -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
107: -1.0f, 0.0f,
108: 0.0f,
109: // Top Face
110: 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
111: 1.0f, 0.0f,
112: // Bottom Face
113: 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
114: 0.0f, -1.0f, 0.0f, };
115:
116: public Cube(Appearance appearance) {
117:
118: QuadArray quadArray = new QuadArray(24, QuadArray.COORDINATES
119: | QuadArray.NORMALS | QuadArray.TEXTURE_COORDINATE_2);
120: quadArray.setCoordinates(0, verts);
121: quadArray.setNormals(0, normals);
122:
123: shape3D = new Shape3D(quadArray, appearance);
124: shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
125: shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
126: shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
127: shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
128: }
129:
130: public Cube(Appearance appearance, float size) {
131:
132: QuadArray quadArray = new QuadArray(24, QuadArray.COORDINATES
133: | QuadArray.NORMALS);
134: for (int i = 0; i < 72; i++)
135: verts[i] *= size;
136:
137: quadArray.setCoordinates(0, verts);
138: quadArray.setNormals(0, normals);
139:
140: shape3D = new Shape3D(quadArray, appearance);
141: shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
142: shape3D.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
143: shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
144: shape3D.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
145: }
146:
147: public Shape3D getChild() {
148: return shape3D;
149: }
150: }
|