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.FloatBuffer;
37 import java.nio.IntBuffer;
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;
48 * A four sided pyramid.
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.
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
59 * @version $Revision$, $Date$
61 public class Pyramid extends TriMesh {
63 private static final long serialVersionUID = 1L;
73 * Constructor instantiates a new <code>Pyramid</code> object. The base
74 * width and the height are provided.
77 * the name of the scene element. This is required for
78 * identification and comparision purposes.
80 * the base width of the pyramid.
82 * the height of the pyramid from the base to the peak.
84 public Pyramid(String name, float width, float height) {
86 updateGeometry(width, height);
89 public float getHeight() {
93 public float getWidth() {
97 public void read(JMEImporter e) throws IOException {
99 InputCapsule capsule = e.getCapsule(this);
100 height = capsule.readFloat("height", 0);
101 width = capsule.readFloat("width", 0);
104 public void updateGeometry(float width, float height) {
106 this.height = height;
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
123 setVertexBuffer(verts);
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
136 setNormalBuffer(norms);
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
145 setTextureCoords(new TexCoords(texCoords), 0);
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
153 setIndexBuffer(indices);
156 public void write(JMEExporter e) throws IOException {
158 OutputCapsule capsule = e.getCapsule(this);
159 capsule.write(height, "height", 0);
160 capsule.write(width, "width", 0);