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;
36 import java.nio.IntBuffer;
38 import com.jme.math.FastMath;
39 import com.jme.math.Vector2f;
40 import com.jme.math.Vector3f;
41 import com.jme.scene.TexCoords;
42 import com.jme.util.export.InputCapsule;
43 import com.jme.util.export.JMEExporter;
44 import com.jme.util.export.JMEImporter;
45 import com.jme.util.export.OutputCapsule;
46 import com.jme.util.geom.BufferUtils;
49 * A regular polyhedron with 8 faces.
51 * It looks somewhat like two pyramids placed bottom to bottom (or a 8-sided
52 * die for all you D&D fans!).
55 * @version $Revision$, $Date$
57 public class Octahedron extends RegularPolyhedron {
59 private static final long serialVersionUID = 1L;
61 private static final int NUM_POINTS = 6;
63 private static final int NUM_TRIS = 8;
69 * Creates an octahedron with center at the origin. The lenght sides are
73 * The name of the octahedron.
75 * The length of each side of the octahedron.
77 public Octahedron(String name, float sideLength) {
79 updateGeometry(sideLength);
82 protected void doUpdateGeometry() {
83 setVertexCount(NUM_POINTS);
84 setVertexBuffer(BufferUtils.createVector3Buffer(NUM_POINTS));
85 setNormalBuffer(BufferUtils.createVector3Buffer(NUM_POINTS));
86 setTextureCoords(new TexCoords(BufferUtils.createVector2Buffer(NUM_POINTS)), 0);
87 setTriangleQuantity(NUM_TRIS);
88 setIndexBuffer(BufferUtils.createIntBuffer(3 * getTriangleCount()));
95 public void read(JMEImporter e) throws IOException {
97 InputCapsule capsule = e.getCapsule(this);
98 sideLength = capsule.readInt("sideLength", 0);
101 private void setIndexData() {
102 IntBuffer indices = getIndexBuffer();
104 indices.put(4).put(0).put(2);
105 indices.put(4).put(2).put(1);
106 indices.put(4).put(1).put(3);
107 indices.put(4).put(3).put(0);
108 indices.put(5).put(2).put(0);
109 indices.put(5).put(1).put(2);
110 indices.put(5).put(3).put(1);
111 indices.put(5).put(0).put(3);
114 for (int i = 0; i < getTriangleCount(); i++) {
115 int iSave = getIndexBuffer().get(3 * i + 1);
117 .put(3 * i + 1, getIndexBuffer().get(3 * i + 2));
118 getIndexBuffer().put(3 * i + 2, iSave);
123 private void setNormalData() {
124 Vector3f norm = new Vector3f();
125 for (int i = 0; i < NUM_POINTS; i++) {
126 BufferUtils.populateFromBuffer(norm, getVertexBuffer(), i);
127 norm.normalizeLocal();
128 BufferUtils.setInBuffer(norm, getNormalBuffer(), i);
132 private void setTextureData() {
133 Vector2f tex = new Vector2f();
134 Vector3f vert = new Vector3f();
135 for (int i = 0; i < NUM_POINTS; i++) {
136 BufferUtils.populateFromBuffer(vert, getVertexBuffer(), i);
137 if (FastMath.abs(vert.z) < sideLength) {
138 tex.x = 0.5f * (1.0f + FastMath.atan2(vert.y, vert.x)
143 tex.y = FastMath.acos(vert.z) * FastMath.INV_PI;
144 getTextureCoords().get(0).coords.put(tex.x).put(tex.y);
148 private void setVertexData() {
149 getVertexBuffer().put(sideLength).put(0.0f).put(0.0f);
150 getVertexBuffer().put(-sideLength).put(0.0f).put(0.0f);
151 getVertexBuffer().put(0.0f).put(sideLength).put(0.0f);
152 getVertexBuffer().put(0.0f).put(-sideLength).put(0.0f);
153 getVertexBuffer().put(0.0f).put(0.0f).put(sideLength);
154 getVertexBuffer().put(0.0f).put(0.0f).put(-sideLength);
157 public void write(JMEExporter e) throws IOException {
159 OutputCapsule capsule = e.getCapsule(this);
160 capsule.write(sideLength, "sideLength", 0);