OSDN Git Service

d092a89be23d35966f74579b7217ee6c7a2545eb
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / scene / shape / StripBox.java
1 /*
2  * Copyright (c) 2003-2009 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 // $Id$
33 package com.jme.scene.shape;
34
35 import java.nio.FloatBuffer;
36
37 import com.jme.math.Vector3f;
38 import com.jme.scene.TexCoords;
39 import com.jme.scene.TriMesh;
40 import com.jme.util.geom.BufferUtils;
41
42 /**
43  * A box made from a strip mode tri-mesh.
44  * 
45  * @author Mark Powell
46  * @author Joshua Slack (conversion from Box to StripBox)
47  * @version $Revision$, $Date$
48  */
49 public class StripBox extends AbstractBox {
50
51     private static final long serialVersionUID = 1L;
52
53     /** <strong>NOT API:</strong> for internal use, do not call from user code. */
54         public StripBox() {
55                 super("temp");
56         }
57
58         /**
59          * Constructor instantiates a new <code>StripBox</code> object. Center and
60          * vertice information must be supplied later.
61          * 
62          * @param name
63          *            the name of the scene element. This is required for
64          *            identification and comparision purposes.
65          */
66         public StripBox(String name) {
67                 super(name);
68         }
69
70         /**
71          * Constructor instantiates a new <code>StripBox</code> object. The minimum and
72          * maximum point are provided. These two points define the shape and size of
73          * the box, but not it's orientation or position. You should use the
74          * <code>setLocalTranslation</code> and <code>setLocalRotation</code>
75          * for those attributes.
76          * 
77          * @param name
78          *            the name of the scene element. This is required for
79          *            identification and comparision purposes.
80          * @param min
81          *            the minimum point that defines the box.
82          * @param max
83          *            the maximum point that defines the box.
84          */
85         public StripBox(String name, Vector3f min, Vector3f max) {
86                 super(name);
87                 updateGeometry(min, max);
88         }
89
90         /**
91          * Constructs a new box. The box has the given center and extends in the x,
92          * y, and z out from the center (+ and -) by the given amounts. So, for
93          * example, a box with extent of .5 would be the unit cube.
94          * 
95          * @param name
96          *            Name of the box.
97          * @param center
98          *            Center of the box.
99          * @param xExtent
100          *            x extent of the box, in both directions.
101          * @param yExtent
102          *            y extent of the box, in both directions.
103          * @param zExtent
104          *            z extent of the box, in both directions.
105          */
106         public StripBox(String name, Vector3f center, float xExtent, float yExtent,
107                         float zExtent) {
108                 super(name);
109                 updateGeometry(center, xExtent, yExtent, zExtent);
110         }
111
112         protected void duUpdateGeometryVertices() {
113         setVertexBuffer(BufferUtils.createVector3Buffer(getVertexBuffer(), 8));
114                 Vector3f[] vert = computeVertices(); // returns 8
115         getVertexBuffer().clear();
116                 getVertexBuffer().put(vert[0].x).put(vert[0].y).put(vert[0].z);
117                 getVertexBuffer().put(vert[1].x).put(vert[1].y).put(vert[1].z);
118                 getVertexBuffer().put(vert[2].x).put(vert[2].y).put(vert[2].z);
119                 getVertexBuffer().put(vert[3].x).put(vert[3].y).put(vert[3].z);
120         getVertexBuffer().put(vert[4].x).put(vert[4].y).put(vert[4].z);
121         getVertexBuffer().put(vert[5].x).put(vert[5].y).put(vert[5].z);
122         getVertexBuffer().put(vert[6].x).put(vert[6].y).put(vert[6].z);
123         getVertexBuffer().put(vert[7].x).put(vert[7].y).put(vert[7].z);
124         }
125
126         protected void duUpdateGeometryNormals() {
127         Vector3f[] vert = computeVertices(); // returns 8
128         setNormalBuffer(BufferUtils.createVector3Buffer(getNormalBuffer(), 8));
129         Vector3f norm = new Vector3f();
130
131         getNormalBuffer().clear();
132         for (int i = 0; i < 8; i++) {
133             norm.set(vert[i]).normalizeLocal();
134             getNormalBuffer().put(norm.x).put(norm.y).put(norm.z);
135         }
136         }
137
138         protected void duUpdateGeometryTextures() {
139             if (getTextureCoords().get(0) == null) {
140                     getTextureCoords().set(0,new TexCoords(BufferUtils.createVector2Buffer(24)));
141                     FloatBuffer tex = getTextureCoords().get(0).coords;
142                     tex.put(1).put(0); //0
143                         tex.put(0).put(0); //1
144                         tex.put(0).put(1); //2
145                         tex.put(1).put(1); //3
146             tex.put(1).put(0); //4
147             tex.put(0).put(0); //5
148             tex.put(1).put(1); //6
149             tex.put(0).put(1); //7
150             }
151         }
152
153         protected void duUpdateGeometryIndices() {
154         setMode(TriMesh.Mode.Strip);
155             if (getIndexBuffer() == null) {
156                         int[] indices = { 1, 0, 4, 5, 7, 0, 3, 1, 2, 4, 6, 7, 2, 3 };
157                         setIndexBuffer(BufferUtils.createIntBuffer(indices));
158             }
159         }
160
161         /**
162          * Creates a new StripBox object containing the same data as this one.
163          * 
164          * @return the new StripBox
165          */
166         public StripBox clone() {
167                 return new StripBox(getName() + "_clone",
168                         (Vector3f) center.clone(),
169                         xExtent, yExtent, zExtent);
170         }
171
172 }