OSDN Git Service

797d0167f9537780805734e5fc10695ec98d706b
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / scene / shape / Pyramid.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.FloatBuffer;
37 import java.nio.IntBuffer;
38
39 import com.jme.scene.TexCoords;
40 import com.jme.scene.TriMesh;
41 import com.jme.util.export.InputCapsule;
42 import com.jme.util.export.JMEExporter;
43 import com.jme.util.export.JMEImporter;
44 import com.jme.util.export.OutputCapsule;
45 import com.jme.util.geom.BufferUtils;
46
47 /**
48  * A four sided pyramid.
49  * <p>
50  * A pyramid is defined by a width at the base and a height. The pyramid is a
51  * 4-sided pyramid with the center at (0,0), it will be axis aligned with the
52  * peak being on the positive y axis and the base being in the x-z plane.
53  * <p>
54  * The texture that defines the look of the pyramid has the top point of the
55  * pyramid as the top center of the texture, with the remaining texture
56  * wrapping around it.
57  * 
58  * @author Mark Powell
59  * @version $Revision$, $Date$
60  */
61 public class Pyramid extends TriMesh {
62
63     private static final long serialVersionUID = 1L;
64
65     private float height;
66
67     private float width;
68
69     public Pyramid() {
70     }
71
72     /**
73      * Constructor instantiates a new <code>Pyramid</code> object. The base
74      * width and the height are provided.
75      * 
76      * @param name
77      *            the name of the scene element. This is required for
78      *            identification and comparision purposes.
79      * @param width
80      *            the base width of the pyramid.
81      * @param height
82      *            the height of the pyramid from the base to the peak.
83      */
84     public Pyramid(String name, float width, float height) {
85         super(name);
86         updateGeometry(width, height);
87     }
88
89     public float getHeight() {
90         return height;
91     }
92
93     public float getWidth() {
94         return width;
95     }
96
97     public void read(JMEImporter e) throws IOException {
98         super.read(e);
99         InputCapsule capsule = e.getCapsule(this);
100         height = capsule.readFloat("height", 0);
101         width = capsule.readFloat("width", 0);
102     }
103
104     public void updateGeometry(float width, float height) {
105         this.width = width;
106         this.height = height;
107
108         // Update the vertex buffer
109         float pkx = 0, pky = height / 2, pkz = 0;
110         float vx0 = -width / 2, vy0 = -height / 2, vz0 = -width / 2;
111         float vx1 =  width / 2, vy1 = -height / 2, vz1 = -width / 2;
112         float vx2 =  width / 2, vy2 = -height / 2, vz2 =  width / 2;
113         float vx3 = -width / 2, vy3 = -height / 2, vz3 =  width / 2;
114         FloatBuffer verts = BufferUtils.createVector3Buffer(16);
115         verts.put(new float[] {
116                 vx3, vy3, vz3, vx2, vy2, vz2, vx1, vy1, vz1, vx0, vy0, vz0, // base
117                 vx0, vy0, vz0, vx1, vy1, vz1, pkx, pky, pkz, // side 1
118                 vx1, vy1, vz1, vx2, vy2, vz2, pkx, pky, pkz, // side 2
119                 vx2, vy2, vz2, vx3, vy3, vz3, pkx, pky, pkz, // side 3
120                 vx3, vy3, vz3, vx0, vy0, vz0, pkx, pky, pkz  // side 4
121         }); 
122         verts.rewind();
123         setVertexBuffer(verts);
124         
125         // Update the normals buffer
126         FloatBuffer norms = BufferUtils.createVector3Buffer(16);
127         float pn = 0.70710677f, nn = -0.70710677f;
128         norms.put(new float[] {
129                 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, // top
130                 0, pn, nn, 0, pn, nn, 0, pn, nn, // back
131                 pn, pn, 0, pn, pn, 0, pn, pn, 0, // right
132                 0, pn, pn, 0, pn, pn, 0, pn, pn, // front
133                 nn, pn, 0, nn, pn, 0, nn, pn, 0  // left
134         });
135         norms.rewind();
136         setNormalBuffer(norms);
137
138         // Update the texture buffer
139         FloatBuffer texCoords = BufferUtils.createVector2Buffer(16);
140         texCoords.put(new float[] {
141                 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0.75f, 0, 0.5f, 1, 0.75f, 0, 0.5f,
142                 0, 0.5f, 1, 0.5f, 0, 0.25f, 0, 0.5f, 1, 0.25f, 0, 0, 0, 0.5f, 1                
143         });
144         texCoords.rewind();
145         setTextureCoords(new TexCoords(texCoords), 0);
146
147         // Update the indices buffer
148         IntBuffer indices = BufferUtils.createIntBuffer(18);
149         indices.put(new int[] {
150                 3, 2, 1, 3, 1, 0, 6, 5, 4, 9, 8, 7, 12, 11, 10, 15, 14, 13
151         });
152         indices.rewind();
153         setIndexBuffer(indices);
154     }
155
156     public void write(JMEExporter e) throws IOException {
157         super.write(e);
158         OutputCapsule capsule = e.getCapsule(this);
159         capsule.write(height, "height", 0);
160         capsule.write(width, "width", 0);
161     }
162
163 }