OSDN Git Service

3e801cd6bd94947d5933dc07b5ca960d9183c992
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / scene / shape / Octahedron.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 import java.nio.IntBuffer;
37
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;
47
48 /**
49  * A regular polyhedron with 8 faces.
50  * <p>
51  * It looks somewhat like two pyramids placed bottom to bottom (or a 8-sided
52  * die for all you D&D fans!).
53  * 
54  * @author Mark Powell
55  * @version $Revision$, $Date$
56  */
57 public class Octahedron extends RegularPolyhedron {
58
59     private static final long serialVersionUID = 1L;
60
61     private static final int NUM_POINTS = 6;
62
63     private static final int NUM_TRIS = 8;
64
65     public Octahedron() {
66     }
67
68     /**
69      * Creates an octahedron with center at the origin. The lenght sides are
70      * given.
71      * 
72      * @param name
73      *            The name of the octahedron.
74      * @param sideLength
75      *            The length of each side of the octahedron.
76      */
77     public Octahedron(String name, float sideLength) {
78         super(name);
79         updateGeometry(sideLength);
80     }
81
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()));
89         setVertexData();
90         setNormalData();
91         setTextureData();
92         setIndexData();
93     }
94
95     public void read(JMEImporter e) throws IOException {
96         super.read(e);
97         InputCapsule capsule = e.getCapsule(this);
98         sideLength = capsule.readInt("sideLength", 0);
99     }
100
101     private void setIndexData() {
102         IntBuffer indices = getIndexBuffer();
103         indices.rewind();
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);
112
113         if (!true) {
114             for (int i = 0; i < getTriangleCount(); i++) {
115                 int iSave = getIndexBuffer().get(3 * i + 1);
116                 getIndexBuffer()
117                         .put(3 * i + 1, getIndexBuffer().get(3 * i + 2));
118                 getIndexBuffer().put(3 * i + 2, iSave);
119             }
120         }
121     }
122
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);
129         }
130     }
131
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)
139                         * FastMath.INV_PI);
140             } else {
141                 tex.x = 0.5f;
142             }
143             tex.y = FastMath.acos(vert.z) * FastMath.INV_PI;
144             getTextureCoords().get(0).coords.put(tex.x).put(tex.y);
145         }
146     }
147
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);
155     }
156
157     public void write(JMEExporter e) throws IOException {
158         super.write(e);
159         OutputCapsule capsule = e.getCapsule(this);
160         capsule.write(sideLength, "sideLength", 0);
161     }
162
163 }