• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32 
33 // $Id: Box.java 4131 2009-03-19 20:15:28Z blaine.dev $
34 package com.jme3.scene.shape;
35 
36 import com.jme3.math.Vector3f;
37 import com.jme3.scene.VertexBuffer.Type;
38 import com.jme3.util.BufferUtils;
39 import java.nio.FloatBuffer;
40 
41 /**
42  * A box with solid (filled) faces.
43  *
44  * @author Mark Powell
45  * @version $Revision: 4131 $, $Date: 2009-03-19 16:15:28 -0400 (Thu, 19 Mar 2009) $
46  */
47 public class StripBox extends AbstractBox {
48 
49     private static final short[] GEOMETRY_INDICES_DATA =
50     { 1, 0, 4,
51       5,
52       7,
53       0,
54       3,
55       1,
56       2,
57       4,
58       6,
59       7,
60       2,
61       3 };
62 
63     private static final float[] GEOMETRY_TEXTURE_DATA = {
64         1, 0,
65         0, 0,
66         0, 1,
67         1, 1,
68 
69         1, 0,
70         0, 0,
71         1, 1,
72         0, 1
73     };
74 
75     /**
76      * Creates a new box.
77      * <p>
78      * The box has a center of 0,0,0 and extends in the out from the center by
79      * the given amount in <em>each</em> direction. So, for example, a box
80      * with extent of 0.5 would be the unit cube.
81      *
82      * @param x the size of the box along the x axis, in both directions.
83      * @param y the size of the box along the y axis, in both directions.
84      * @param z the size of the box along the z axis, in both directions.
85      */
StripBox(float x, float y, float z)86     public StripBox(float x, float y, float z) {
87         super();
88         updateGeometry(Vector3f.ZERO, x, y, z);
89     }
90 
91     /**
92      * Creates a new box.
93      * <p>
94      * The box has the given center and extends in the out from the center by
95      * the given amount in <em>each</em> direction. So, for example, a box
96      * with extent of 0.5 would be the unit cube.
97      *
98      * @param center the center of the box.
99      * @param x the size of the box along the x axis, in both directions.
100      * @param y the size of the box along the y axis, in both directions.
101      * @param z the size of the box along the z axis, in both directions.
102      */
StripBox(Vector3f center, float x, float y, float z)103     public StripBox(Vector3f center, float x, float y, float z) {
104         super();
105         updateGeometry(center, x, y, z);
106     }
107 
108     /**
109      * Constructor instantiates a new <code>Box</code> object.
110      * <p>
111      * The minimum and maximum point are provided, these two points define the
112      * shape and size of the box but not it’s orientation or position. You should
113      * use the {@link #setLocalTranslation()} and {@link #setLocalRotation()}
114      * methods to define those properties.
115      *
116      * @param min the minimum point that defines the box.
117      * @param max the maximum point that defines the box.
118      */
StripBox(Vector3f min, Vector3f max)119     public StripBox(Vector3f min, Vector3f max) {
120         super();
121         updateGeometry(min, max);
122     }
123 
124     /**
125      * Empty constructor for serialization only. Do not use.
126      */
StripBox()127     public StripBox(){
128         super();
129     }
130 
131     /**
132      * Creates a clone of this box.
133      * <p>
134      * The cloned box will have ‘_clone’ appended to it’s name, but all other
135      * properties will be the same as this box.
136      */
137     @Override
clone()138     public StripBox clone() {
139         return new StripBox(center.clone(), xExtent, yExtent, zExtent);
140     }
141 
duUpdateGeometryIndices()142     protected void duUpdateGeometryIndices() {
143         if (getBuffer(Type.Index) == null){
144             setBuffer(Type.Index, 3, BufferUtils.createShortBuffer(GEOMETRY_INDICES_DATA));
145         }
146     }
147 
duUpdateGeometryNormals()148     protected void duUpdateGeometryNormals() {
149         if (getBuffer(Type.Normal) == null){
150             float[] normals = new float[8 * 3];
151 
152             Vector3f[] vert = computeVertices();
153             Vector3f norm = new Vector3f();
154 
155             for (int i = 0; i < 8; i++) {
156                 norm.set(vert[i]).normalizeLocal();
157 
158                 normals[i * 3 + 0] = norm.x;
159                 normals[i * 3 + 1] = norm.x;
160                 normals[i * 3 + 2] = norm.x;
161             }
162 
163             setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals));
164         }
165     }
166 
duUpdateGeometryTextures()167     protected void duUpdateGeometryTextures() {
168         if (getBuffer(Type.TexCoord) == null){
169             setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(GEOMETRY_TEXTURE_DATA));
170         }
171     }
172 
duUpdateGeometryVertices()173     protected void duUpdateGeometryVertices() {
174         FloatBuffer fpb = BufferUtils.createVector3Buffer(8 * 3);
175         Vector3f[] v = computeVertices();
176         fpb.put(new float[] {
177                 v[0].x, v[0].y, v[0].z,
178                 v[1].x, v[1].y, v[1].z,
179                 v[2].x, v[2].y, v[2].z,
180                 v[3].x, v[3].y, v[3].z,
181                 v[4].x, v[4].y, v[4].z,
182                 v[5].x, v[5].y, v[5].z,
183                 v[6].x, v[6].y, v[6].z,
184                 v[7].x, v[7].y, v[7].z,
185         });
186         setBuffer(Type.Position, 3, fpb);
187         setMode(Mode.TriangleStrip);
188         updateBound();
189     }
190 
191 }