OSDN Git Service

ec2304c1fcb2342e6e3fc25b0179430fa69d9c9c
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / scene / shape / Hexagon.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.io.IOException;
36
37 import com.jme.math.Vector3f;
38 import com.jme.scene.TexCoords;
39 import com.jme.util.export.InputCapsule;
40 import com.jme.util.export.JMEExporter;
41 import com.jme.util.export.JMEImporter;
42 import com.jme.util.export.OutputCapsule;
43 import com.jme.util.geom.BufferUtils;
44
45 /**
46  * <code>Hexagon</code> provides an extension of <code>TriMesh</code>. A
47  * <code>Hexagon</code> provides a regular hexagon with each triangle having
48  * side length that is given in the constructor.
49  * 
50  * @author Joel Schuster
51  * @version $Revision$, $Date$
52  */
53 public class Hexagon extends RegularPolyhedron {
54
55     private static final long serialVersionUID = 1L;
56
57     private static final int NUM_POINTS = 7;
58
59     private static final int NUM_TRIS = 6;
60
61     /**
62      * Hexagon Constructor instantiates a new Hexagon. This element is center on
63      * 0,0,0 with all normals pointing up. The user must move and rotate for
64      * positioning.
65      * 
66      * @param name
67      *            the name of the scene element. This is required for
68      *            identification and comparision purposes.
69      * @param sideLength
70      *            The length of all the sides of the tiangles
71      */
72     public Hexagon(String name, float sideLength) {
73         super(name);
74         updateGeometry(sideLength);
75     }
76
77     protected void doUpdateGeometry() {
78         setVertexCount(NUM_POINTS);
79         setVertexBuffer(BufferUtils.createVector3Buffer(getVertexCount()));
80         setNormalBuffer(BufferUtils.createVector3Buffer(getVertexCount()));
81         getTextureCoords().set(0,
82                 new TexCoords(BufferUtils.createVector2Buffer(getVertexCount())));
83         setTriangleQuantity(NUM_TRIS);
84         setIndexBuffer(BufferUtils.createIntBuffer(3 * getTriangleCount()));
85         setVertexData();
86         setIndexData();
87         setTextureData();
88         setNormalData();
89     }
90
91     public void read(JMEImporter e) throws IOException {
92         super.read(e);
93         InputCapsule capsule = e.getCapsule(this);
94         sideLength = capsule.readInt("sideLength", 0);
95     }
96
97     /**
98      * Sets up the indexes of the mesh. These go in a clockwise fashion and thus
99      * only the 'up' side of the hex is lit properly. If you wish to have to
100      * either set two sided lighting or create two hexes back-to-back
101      */
102
103     private void setIndexData() {
104         getIndexBuffer().rewind();
105         // tri 1
106         getIndexBuffer().put(0);
107         getIndexBuffer().put(6);
108         getIndexBuffer().put(1);
109         // tri 2
110         getIndexBuffer().put(1);
111         getIndexBuffer().put(6);
112         getIndexBuffer().put(2);
113         // tri 3
114         getIndexBuffer().put(2);
115         getIndexBuffer().put(6);
116         getIndexBuffer().put(3);
117         // tri 4
118         getIndexBuffer().put(3);
119         getIndexBuffer().put(6);
120         getIndexBuffer().put(4);
121         // tri 5
122         getIndexBuffer().put(4);
123         getIndexBuffer().put(6);
124         getIndexBuffer().put(5);
125         // tri 6
126         getIndexBuffer().put(5);
127         getIndexBuffer().put(6);
128         getIndexBuffer().put(0);
129     }
130
131     /**
132      * Sets all the default vertex normals to 'up', +1 in the Z direction.
133      */
134     private void setNormalData() {
135         Vector3f zAxis = new Vector3f(0, 0, 1);
136         for (int i = 0; i < NUM_POINTS; i++)
137             BufferUtils.setInBuffer(zAxis, getNormalBuffer(), i);
138     }
139
140     private void setTextureData() {
141         getTextureCoords().get(0).coords.put(0.25f).put(0);
142         getTextureCoords().get(0).coords.put(0.75f).put(0);
143         getTextureCoords().get(0).coords.put(1.0f).put(0.5f);
144         getTextureCoords().get(0).coords.put(0.75f).put(1.0f);
145         getTextureCoords().get(0).coords.put(0.25f).put(1.0f);
146         getTextureCoords().get(0).coords.put(0.0f).put(0.5f);
147         getTextureCoords().get(0).coords.put(0.5f).put(0.5f);
148     }
149
150     /**
151      * Vertexes are set up like this: 0__1 / \ / \ 5/__\6/__\2 \ / \ / \ /___\ /
152      * 4 3 All lines on this diagram are sideLength long. Therefore, the width
153      * of the hexagon is sideLength * 2, and the height is 2 * the height of one
154      * equalateral triangle with all side = sideLength which is .866
155      */
156     private void setVertexData() {
157         getVertexBuffer().put(-(sideLength / 2)).put(sideLength * 0.866f).put(
158                 0.0f);
159         getVertexBuffer().put(sideLength / 2).put(sideLength * 0.866f)
160                 .put(0.0f);
161         getVertexBuffer().put(sideLength).put(0.0f).put(0.0f);
162         getVertexBuffer().put(sideLength / 2).put(-sideLength * 0.866f).put(
163                 0.0f);
164         getVertexBuffer().put(-(sideLength / 2)).put(-sideLength * 0.866f).put(
165                 0.0f);
166         getVertexBuffer().put(-sideLength).put(0.0f).put(0.0f);
167         getVertexBuffer().put(0.0f).put(0.0f).put(0.0f);
168     }
169
170     public void write(JMEExporter e) throws IOException {
171         super.write(e);
172         OutputCapsule capsule = e.getCapsule(this);
173         capsule.write(sideLength, "sideLength", 0);
174     }
175
176 }