• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2012 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 
33 // $Id: PQTorus.java 4131 2009-03-19 20:15:28Z blaine.dev $
34 package com.jme3.scene.shape;
35 
36 import com.jme3.export.InputCapsule;
37 import com.jme3.export.JmeExporter;
38 import com.jme3.export.JmeImporter;
39 import com.jme3.export.OutputCapsule;
40 import com.jme3.math.FastMath;
41 import com.jme3.math.Vector3f;
42 import com.jme3.scene.Mesh;
43 import com.jme3.scene.VertexBuffer.Type;
44 import static com.jme3.util.BufferUtils.*;
45 import java.io.IOException;
46 import java.nio.FloatBuffer;
47 import java.nio.ShortBuffer;
48 
49 /**
50  * A parameterized torus, also known as a <em>pq</em> torus.
51  *
52  * @author Joshua Slack, Eric Woroshow
53  * @version $Revision: 4131 $, $Date: 2009-03-19 16:15:28 -0400 (Thu, 19 Mar 2009) $
54  */
55 public class PQTorus extends Mesh {
56 
57     private float p, q;
58 
59     private float radius, width;
60 
61     private int steps, radialSamples;
62 
PQTorus()63     public PQTorus() {
64     }
65 
66     /**
67      * Creates a parameterized torus.
68      * <p>
69      * Steps and radialSamples are both degree of accuracy values.
70      *
71      * @param p the x/z oscillation.
72      * @param q the y oscillation.
73      * @param radius the radius of the PQTorus.
74      * @param width the width of the torus.
75      * @param steps the steps along the torus.
76      * @param radialSamples radial samples for the torus.
77      */
PQTorus(float p, float q, float radius, float width, int steps, int radialSamples)78     public PQTorus(float p, float q, float radius, float width,
79             int steps, int radialSamples) {
80         super();
81         updateGeometry(p, q, radius, width, steps, radialSamples);
82     }
83 
getP()84     public float getP() {
85         return p;
86     }
87 
getQ()88     public float getQ() {
89         return q;
90     }
91 
getRadialSamples()92     public int getRadialSamples() {
93         return radialSamples;
94     }
95 
getRadius()96     public float getRadius() {
97         return radius;
98     }
99 
getSteps()100     public int getSteps() {
101         return steps;
102     }
103 
getWidth()104     public float getWidth() {
105         return width;
106     }
107 
read(JmeImporter e)108     public void read(JmeImporter e) throws IOException {
109         super.read(e);
110         InputCapsule capsule = e.getCapsule(this);
111         p = capsule.readFloat("p", 0);
112         q = capsule.readFloat("q", 0);
113         radius = capsule.readFloat("radius", 0);
114         width = capsule.readFloat("width", 0);
115         steps = capsule.readInt("steps", 0);
116         radialSamples = capsule.readInt("radialSamples", 0);
117     }
118 
119     /**
120      * Rebuilds this torus based on a new set of parameters.
121      *
122      * @param p the x/z oscillation.
123      * @param q the y oscillation.
124      * @param radius the radius of the PQTorus.
125      * @param width the width of the torus.
126      * @param steps the steps along the torus.
127      * @param radialSamples radial samples for the torus.
128      */
updateGeometry(float p, float q, float radius, float width, int steps, int radialSamples)129     public void updateGeometry(float p, float q, float radius, float width, int steps, int radialSamples) {
130         this.p = p;
131         this.q = q;
132         this.radius = radius;
133         this.width = width;
134         this.steps = steps;
135         this.radialSamples = radialSamples;
136 
137         final float thetaStep = (FastMath.TWO_PI / steps);
138         final float betaStep = (FastMath.TWO_PI / radialSamples);
139         Vector3f[] torusPoints = new Vector3f[steps];
140 
141         // Allocate all of the required buffers
142         int vertCount = radialSamples * steps;
143 
144         FloatBuffer fpb = createVector3Buffer(vertCount);
145         FloatBuffer fnb = createVector3Buffer(vertCount);
146         FloatBuffer ftb = createVector2Buffer(vertCount);
147 
148         Vector3f pointB = new Vector3f(), T = new Vector3f(), N = new Vector3f(), B = new Vector3f();
149         Vector3f tempNorm = new Vector3f();
150         float r, x, y, z, theta = 0.0f, beta = 0.0f;
151         int nvertex = 0;
152 
153         // Move along the length of the pq torus
154         for (int i = 0; i < steps; i++) {
155             theta += thetaStep;
156             float circleFraction = ((float) i) / (float) steps;
157 
158             // Find the point on the torus
159             r = (0.5f * (2.0f + FastMath.sin(q * theta)) * radius);
160             x = (r * FastMath.cos(p * theta) * radius);
161             y = (r * FastMath.sin(p * theta) * radius);
162             z = (r * FastMath.cos(q * theta) * radius);
163             torusPoints[i] = new Vector3f(x, y, z);
164 
165             // Now find a point slightly farther along the torus
166             r = (0.5f * (2.0f + FastMath.sin(q * (theta + 0.01f))) * radius);
167             x = (r * FastMath.cos(p * (theta + 0.01f)) * radius);
168             y = (r * FastMath.sin(p * (theta + 0.01f)) * radius);
169             z = (r * FastMath.cos(q * (theta + 0.01f)) * radius);
170             pointB = new Vector3f(x, y, z);
171 
172             // Approximate the Frenet Frame
173             T = pointB.subtract(torusPoints[i]);
174             N = torusPoints[i].add(pointB);
175             B = T.cross(N);
176             N = B.cross(T);
177 
178             // Normalise the two vectors and then use them to create an oriented circle
179             N = N.normalize();
180             B = B.normalize();
181             beta = 0.0f;
182             for (int j = 0; j < radialSamples; j++, nvertex++) {
183                 beta += betaStep;
184                 float cx = FastMath.cos(beta) * width;
185                 float cy = FastMath.sin(beta) * width;
186                 float radialFraction = ((float) j) / radialSamples;
187                 tempNorm.x = (cx * N.x + cy * B.x);
188                 tempNorm.y = (cx * N.y + cy * B.y);
189                 tempNorm.z = (cx * N.z + cy * B.z);
190                 fnb.put(tempNorm.x).put(tempNorm.y).put(tempNorm.z);
191                 tempNorm.addLocal(torusPoints[i]);
192                 fpb.put(tempNorm.x).put(tempNorm.y).put(tempNorm.z);
193                 ftb.put(radialFraction).put(circleFraction);
194             }
195         }
196 
197         // Update the indices data
198         ShortBuffer sib = createShortBuffer(6 * vertCount);
199         for (int i = 0; i < vertCount; i++) {
200             sib.put(new short[] {
201                     (short)(i),
202                     (short)(i - radialSamples),
203                     (short)(i + 1),
204                     (short)(i + 1),
205                     (short)(i - radialSamples),
206                     (short)(i - radialSamples + 1)
207             });
208         }
209         for (int i = 0, len = sib.capacity(); i < len; i++) {
210             int ind = sib.get(i);
211             if (ind < 0) {
212                 ind += vertCount;
213                 sib.put(i, (short) ind);
214             } else if (ind >= vertCount) {
215                 ind -= vertCount;
216                 sib.put(i, (short) ind);
217             }
218         }
219         sib.rewind();
220 
221         setBuffer(Type.Position, 3, fpb);
222         setBuffer(Type.Normal,   3, fnb);
223         setBuffer(Type.TexCoord, 2, ftb);
224         setBuffer(Type.Index,    3, sib);
225     }
226 
227     @Override
write(JmeExporter e)228     public void write(JmeExporter e) throws IOException {
229         super.write(e);
230         OutputCapsule capsule = e.getCapsule(this);
231         capsule.write(p, "p", 0);
232         capsule.write(q, "q", 0);
233         capsule.write(radius, "radius", 0);
234         capsule.write(width, "width", 0);
235         capsule.write(steps, "steps", 0);
236         capsule.write(radialSamples, "radialSamples", 0);
237     }
238 
239 }