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.io.IOException;
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;
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.
50 * @author Joel Schuster
51 * @version $Revision$, $Date$
53 public class Hexagon extends RegularPolyhedron {
55 private static final long serialVersionUID = 1L;
57 private static final int NUM_POINTS = 7;
59 private static final int NUM_TRIS = 6;
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
67 * the name of the scene element. This is required for
68 * identification and comparision purposes.
70 * The length of all the sides of the tiangles
72 public Hexagon(String name, float sideLength) {
74 updateGeometry(sideLength);
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()));
91 public void read(JMEImporter e) throws IOException {
93 InputCapsule capsule = e.getCapsule(this);
94 sideLength = capsule.readInt("sideLength", 0);
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
103 private void setIndexData() {
104 getIndexBuffer().rewind();
106 getIndexBuffer().put(0);
107 getIndexBuffer().put(6);
108 getIndexBuffer().put(1);
110 getIndexBuffer().put(1);
111 getIndexBuffer().put(6);
112 getIndexBuffer().put(2);
114 getIndexBuffer().put(2);
115 getIndexBuffer().put(6);
116 getIndexBuffer().put(3);
118 getIndexBuffer().put(3);
119 getIndexBuffer().put(6);
120 getIndexBuffer().put(4);
122 getIndexBuffer().put(4);
123 getIndexBuffer().put(6);
124 getIndexBuffer().put(5);
126 getIndexBuffer().put(5);
127 getIndexBuffer().put(6);
128 getIndexBuffer().put(0);
132 * Sets all the default vertex normals to 'up', +1 in the Z direction.
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);
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);
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
156 private void setVertexData() {
157 getVertexBuffer().put(-(sideLength / 2)).put(sideLength * 0.866f).put(
159 getVertexBuffer().put(sideLength / 2).put(sideLength * 0.866f)
161 getVertexBuffer().put(sideLength).put(0.0f).put(0.0f);
162 getVertexBuffer().put(sideLength / 2).put(-sideLength * 0.866f).put(
164 getVertexBuffer().put(-(sideLength / 2)).put(-sideLength * 0.866f).put(
166 getVertexBuffer().put(-sideLength).put(0.0f).put(0.0f);
167 getVertexBuffer().put(0.0f).put(0.0f).put(0.0f);
170 public void write(JMEExporter e) throws IOException {
172 OutputCapsule capsule = e.getCapsule(this);
173 capsule.write(sideLength, "sideLength", 0);