StripBox.java revision 59b2e6871c65f58fdad78cd7229c292f6a177578
159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine 359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved. 459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without 659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are 759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met: 859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright 1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer. 1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright 1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * notice, this list of conditions and the following disclaimer in the 1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * documentation and/or other materials provided with the distribution. 1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * may be used to endorse or promote products derived from this software 1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * without specific prior written permission. 1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta// $Id: Box.java 4131 2009-03-19 20:15:28Z blaine.dev $ 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.scene.shape; 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f; 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.VertexBuffer.Type; 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.util.BufferUtils; 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.FloatBuffer; 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * A box with solid (filled) faces. 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Mark Powell 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @version $Revision: 4131 $, $Date: 2009-03-19 16:15:28 -0400 (Thu, 19 Mar 2009) $ 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class StripBox extends AbstractBox { 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private static final short[] GEOMETRY_INDICES_DATA = 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta { 1, 0, 4, 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5, 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7, 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 0, 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3, 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 1, 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 2, 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4, 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6, 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7, 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 2, 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3 }; 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private static final float[] GEOMETRY_TEXTURE_DATA = { 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 1, 0, 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 0, 0, 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 0, 1, 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 1, 1, 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 1, 0, 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 0, 0, 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 1, 1, 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 0, 1 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta }; 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Creates a new box. 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * <p> 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * The box has a center of 0,0,0 and extends in the out from the center by 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the given amount in <em>each</em> direction. So, for example, a box 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * with extent of 0.5 would be the unit cube. 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param x the size of the box along the x axis, in both directions. 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param y the size of the box along the y axis, in both directions. 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param z the size of the box along the z axis, in both directions. 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public StripBox(float x, float y, float z) { 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super(); 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta updateGeometry(Vector3f.ZERO, x, y, z); 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Creates a new box. 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * <p> 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * The box has the given center and extends in the out from the center by 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the given amount in <em>each</em> direction. So, for example, a box 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * with extent of 0.5 would be the unit cube. 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param center the center of the box. 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param x the size of the box along the x axis, in both directions. 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param y the size of the box along the y axis, in both directions. 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param z the size of the box along the z axis, in both directions. 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public StripBox(Vector3f center, float x, float y, float z) { 10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super(); 10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta updateGeometry(center, x, y, z); 10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Constructor instantiates a new <code>Box</code> object. 11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * <p> 11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * The minimum and maximum point are provided, these two points define the 11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * shape and size of the box but not it’s orientation or position. You should 11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * use the {@link #setLocalTranslation()} and {@link #setLocalRotation()} 11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * methods to define those properties. 11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param min the minimum point that defines the box. 11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @param max the maximum point that defines the box. 11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public StripBox(Vector3f min, Vector3f max) { 12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super(); 12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta updateGeometry(min, max); 12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Empty constructor for serialization only. Do not use. 12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public StripBox(){ 12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta super(); 12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Creates a clone of this box. 13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * <p> 13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * The cloned box will have ‘_clone’ appended to it’s name, but all other 13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * properties will be the same as this box. 13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public StripBox clone() { 13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return new StripBox(center.clone(), xExtent, yExtent, zExtent); 14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected void duUpdateGeometryIndices() { 14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (getBuffer(Type.Index) == null){ 14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta setBuffer(Type.Index, 3, BufferUtils.createShortBuffer(GEOMETRY_INDICES_DATA)); 14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected void duUpdateGeometryNormals() { 14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (getBuffer(Type.Normal) == null){ 15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float[] normals = new float[8 * 3]; 15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Vector3f[] vert = computeVertices(); 15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Vector3f norm = new Vector3f(); 15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int i = 0; i < 8; i++) { 15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta norm.set(vert[i]).normalizeLocal(); 15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta normals[i * 3 + 0] = norm.x; 15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta normals[i * 3 + 1] = norm.x; 16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta normals[i * 3 + 2] = norm.x; 16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals)); 16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected void duUpdateGeometryTextures() { 16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (getBuffer(Type.TexCoord) == null){ 16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(GEOMETRY_TEXTURE_DATA)); 17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta protected void duUpdateGeometryVertices() { 17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta FloatBuffer fpb = BufferUtils.createVector3Buffer(8 * 3); 17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Vector3f[] v = computeVertices(); 17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta fpb.put(new float[] { 17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta v[0].x, v[0].y, v[0].z, 17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta v[1].x, v[1].y, v[1].z, 17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta v[2].x, v[2].y, v[2].z, 18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta v[3].x, v[3].y, v[3].z, 18159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta v[4].x, v[4].y, v[4].z, 18259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta v[5].x, v[5].y, v[5].z, 18359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta v[6].x, v[6].y, v[6].z, 18459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta v[7].x, v[7].y, v[7].z, 18559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta }); 18659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta setBuffer(Type.Position, 3, fpb); 18759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta setMode(Mode.TriangleStrip); 18859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta updateBound(); 18959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 19059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 19159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}