2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 package com.jme.scene.shape;
35 import java.nio.FloatBuffer;
37 import com.jme.math.Vector3f;
38 import com.jme.scene.TexCoords;
39 import com.jme.util.geom.BufferUtils;
42 * A box with solid (filled) faces.
45 * @version $Revision$, $Date$
47 public class Box extends AbstractBox {
49 private static final int[] GEOMETRY_INDICES_DATA = {
50 2, 1, 0, 3, 2, 0, // back
51 6, 5, 4, 7, 6, 4, // right
52 10, 9, 8, 11, 10, 8, // front
53 14, 13, 12, 15, 14, 12, // left
54 18, 17, 16, 19, 18, 16, // top
55 22, 21, 20, 23, 22, 20 // bottom
58 private static final float[] GEOMETRY_NORMALS_DATA = {
59 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, // back
60 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // right
61 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // front
62 -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // left
63 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // top
64 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 // bottom
67 private static final float[] GEOMETRY_TEXTURE_DATA = {
68 1, 0, 0, 0, 0, 1, 1, 1, // back
69 1, 0, 0, 0, 0, 1, 1, 1, // right
70 1, 0, 0, 0, 0, 1, 1, 1, // front
71 1, 0, 0, 0, 0, 1, 1, 1, // left
72 1, 0, 0, 0, 0, 1, 1, 1, // top
73 1, 0, 0, 0, 0, 1, 1, 1 // bottom
76 private static final long serialVersionUID = 1L;
85 * Center and vertices information must be supplied later.
87 * @param name the name of the box.
89 public Box(String name) {
96 * The box has the given center and extends in the out from the center by
97 * the given amount in <em>each</em> direction. So, for example, a box
98 * with extent of 0.5 would be the unit cube.
100 * @param name the name of the box.
101 * @param center the center of the box.
102 * @param x the size of the box along the x axis, in both directions.
103 * @param y the size of the box along the y axis, in both directions.
104 * @param z the size of the box along the z axis, in both directions.
106 public Box(String name, Vector3f center, float x, float y, float z) {
108 updateGeometry(center, x, y, z);
112 * Constructor instantiates a new <code>Box</code> object.
114 * The minimum and maximum point are provided, these two points define the
115 * shape and size of the box but not it’s orientation or position. You should
116 * use the {@link #setLocalTranslation()} and {@link #setLocalRotation()}
117 * methods to define those properties.
119 * @param name the name of the box.
120 * @param min the minimum point that defines the box.
121 * @param max the maximum point that defines the box.
123 public Box(String name, Vector3f min, Vector3f max) {
125 updateGeometry(min, max);
129 * Creates a clone of this box.
131 * The cloned box will have ‘_clone’ appended to it’s name, but all other
132 * properties will be the same as this box.
135 return new Box(getName() + "_clone", center.clone(), xExtent, yExtent, zExtent);
138 protected void duUpdateGeometryIndices() {
139 if (getIndexBuffer() == null) {
140 setIndexBuffer(BufferUtils.createIntBuffer(GEOMETRY_INDICES_DATA));
144 protected void duUpdateGeometryNormals() {
145 if (getNormalBuffer() == null) {
146 setNormalBuffer(BufferUtils.createVector3Buffer(24));
147 getNormalBuffer().put(GEOMETRY_NORMALS_DATA);
151 protected void duUpdateGeometryTextures() {
152 if (getTextureCoords().get(0) == null) {
153 getTextureCoords().set(0, new TexCoords(BufferUtils.createVector2Buffer(24)));
154 FloatBuffer tex = getTextureCoords().get(0).coords;
155 tex.put(GEOMETRY_TEXTURE_DATA);
159 protected void duUpdateGeometryVertices() {
160 setVertexBuffer(BufferUtils.createVector3Buffer(getVertexBuffer(), 24));
162 Vector3f[] v = computeVertices();
163 getVertexBuffer().put(new float[] {
164 v[0].x, v[0].y, v[0].z, v[1].x, v[1].y, v[1].z, v[2].x, v[2].y, v[2].z, v[3].x, v[3].y, v[3].z, // back
165 v[1].x, v[1].y, v[1].z, v[4].x, v[4].y, v[4].z, v[6].x, v[6].y, v[6].z, v[2].x, v[2].y, v[2].z, // right
166 v[4].x, v[4].y, v[4].z, v[5].x, v[5].y, v[5].z, v[7].x, v[7].y, v[7].z, v[6].x, v[6].y, v[6].z, // front
167 v[5].x, v[5].y, v[5].z, v[0].x, v[0].y, v[0].z, v[3].x, v[3].y, v[3].z, v[7].x, v[7].y, v[7].z, // left
168 v[2].x, v[2].y, v[2].z, v[6].x, v[6].y, v[6].z, v[7].x, v[7].y, v[7].z, v[3].x, v[3].y, v[3].z, // top
169 v[0].x, v[0].y, v[0].z, v[5].x, v[5].y, v[5].z, v[4].x, v[4].y, v[4].z, v[1].x, v[1].y, v[1].z // bottom