|
1 | 1 | /* |
2 | | - * Copyright (c) 2009-2024 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 | | -package com.jme3.environment.util; |
33 | | - |
34 | | -import com.jme3.asset.AssetManager; |
35 | | -import com.jme3.material.Material; |
36 | | -import com.jme3.math.ColorRGBA; |
37 | | -import com.jme3.math.FastMath; |
38 | | -import com.jme3.scene.Geometry; |
39 | | -import com.jme3.scene.Mesh; |
40 | | -import com.jme3.scene.VertexBuffer.Type; |
41 | | -import com.jme3.util.BufferUtils; |
42 | | -import java.nio.FloatBuffer; |
43 | | -import java.nio.ShortBuffer; |
44 | | - |
45 | | -/** |
46 | | - * |
47 | | - * A debugging shape for a BoundingSphere |
48 | | - * Consists of 3 axis aligned circles. |
49 | | - * |
50 | | - * @author nehon |
51 | | - */ |
52 | | -public class BoundingSphereDebug extends Mesh { |
53 | | - |
54 | | - protected int vertCount; |
55 | | - protected int triCount; |
56 | | - protected int radialSamples = 32; |
57 | | - protected boolean useEvenSlices; |
58 | | - protected boolean interior; |
59 | | - |
60 | | - public BoundingSphereDebug() { |
61 | | - setGeometryData(); |
62 | | - setIndexData(); |
63 | | - } |
64 | | - |
65 | | - /** |
66 | | - * builds the vertices based on the radius |
67 | | - */ |
68 | | - private void setGeometryData() { |
69 | | - setMode(Mode.Lines); |
70 | | - |
71 | | - FloatBuffer posBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 3); |
72 | | - FloatBuffer colBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 4); |
73 | | - |
74 | | - setBuffer(Type.Position, 3, posBuf); |
75 | | - setBuffer(Type.Color, 4, colBuf); |
76 | | - |
77 | | - // generate geometry |
78 | | - float fInvRS = 1.0f / radialSamples; |
79 | | - |
80 | | - // Generate points on the unit circle to be used in computing the mesh |
81 | | - // points on a sphere slice. |
82 | | - float[] afSin = new float[(radialSamples + 1)]; |
83 | | - float[] afCos = new float[(radialSamples + 1)]; |
84 | | - for (int iR = 0; iR < radialSamples; iR++) { |
85 | | - float fAngle = FastMath.TWO_PI * fInvRS * iR; |
86 | | - afCos[iR] = FastMath.cos(fAngle); |
87 | | - afSin[iR] = FastMath.sin(fAngle); |
88 | | - } |
89 | | - afSin[radialSamples] = afSin[0]; |
90 | | - afCos[radialSamples] = afCos[0]; |
91 | | - |
92 | | - for (int iR = 0; iR <= radialSamples; iR++) { |
93 | | - posBuf.put(afCos[iR]) |
94 | | - .put(afSin[iR]) |
95 | | - .put(0); |
96 | | - colBuf.put(ColorRGBA.Blue.r) |
97 | | - .put(ColorRGBA.Blue.g) |
98 | | - .put(ColorRGBA.Blue.b) |
99 | | - .put(ColorRGBA.Blue.a); |
100 | | - |
101 | | - } |
102 | | - for (int iR = 0; iR <= radialSamples; iR++) { |
103 | | - posBuf.put(afCos[iR]) |
104 | | - .put(0) |
105 | | - .put(afSin[iR]); |
106 | | - colBuf.put(ColorRGBA.Green.r) |
107 | | - .put(ColorRGBA.Green.g) |
108 | | - .put(ColorRGBA.Green.b) |
109 | | - .put(ColorRGBA.Green.a); |
110 | | - } |
111 | | - for (int iR = 0; iR <= radialSamples; iR++) { |
112 | | - posBuf.put(0) |
113 | | - .put(afCos[iR]) |
114 | | - .put(afSin[iR]); |
115 | | - colBuf.put(ColorRGBA.Yellow.r) |
116 | | - .put(ColorRGBA.Yellow.g) |
117 | | - .put(ColorRGBA.Yellow.b) |
118 | | - .put(ColorRGBA.Yellow.a); |
119 | | - } |
120 | | - |
121 | | - updateBound(); |
122 | | - setStatic(); |
123 | | - } |
124 | | - |
125 | | - /** |
126 | | - * sets the indices for rendering the sphere. |
127 | | - */ |
128 | | - private void setIndexData() { |
129 | | - |
130 | | - // allocate connectivity |
131 | | - int nbSegments = (radialSamples) * 3; |
132 | | - |
133 | | - ShortBuffer idxBuf = BufferUtils.createShortBuffer(2 * nbSegments); |
134 | | - setBuffer(Type.Index, 2, idxBuf); |
135 | | - |
136 | | - int idx = 0; |
137 | | - int segDone = 0; |
138 | | - while (segDone < nbSegments) { |
139 | | - idxBuf.put((short) idx); |
140 | | - idxBuf.put((short) (idx + 1)); |
141 | | - idx++; |
142 | | - segDone++; |
143 | | - if (segDone == radialSamples || segDone == radialSamples * 2) { |
144 | | - idx++; |
145 | | - } |
146 | | - } |
147 | | - } |
148 | | - |
149 | | - /** |
150 | | - * Convenience factory method that creates a debug bounding-sphere geometry |
151 | | - * |
152 | | - * @param assetManager the assetManager |
153 | | - * @return the bounding sphere debug geometry. |
154 | | - */ |
155 | | - public static Geometry createDebugSphere(AssetManager assetManager) { |
156 | | - BoundingSphereDebug mesh = new BoundingSphereDebug(); |
157 | | - Geometry geom = new Geometry("BoundingDebug", mesh); |
158 | | - Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
159 | | - mat.setBoolean("VertexColor", true); |
160 | | - mat.getAdditionalRenderState().setWireframe(true); |
161 | | - geom.setMaterial(mat); |
162 | | - return geom; |
163 | | - } |
164 | | - |
165 | | -} |
| 2 | + * Copyright (c) 2009-2025 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 | + package com.jme3.environment.util; |
| 33 | + |
| 34 | + import com.jme3.asset.AssetManager; |
| 35 | + import com.jme3.material.Material; |
| 36 | + import com.jme3.math.ColorRGBA; |
| 37 | + import com.jme3.math.FastMath; |
| 38 | + import com.jme3.scene.Geometry; |
| 39 | + import com.jme3.scene.Mesh; |
| 40 | + import com.jme3.scene.VertexBuffer.Type; |
| 41 | + import com.jme3.util.BufferUtils; |
| 42 | + |
| 43 | + import java.nio.FloatBuffer; |
| 44 | + import java.nio.ShortBuffer; |
| 45 | + |
| 46 | + /** |
| 47 | + * A debugging shape for a BoundingSphere. |
| 48 | + * This mesh consists of three axis-aligned circles (XY, XZ, YZ planes), |
| 49 | + * providing a visual representation of a bounding sphere. |
| 50 | + * |
| 51 | + * @author nehon |
| 52 | + */ |
| 53 | + public class BoundingSphereDebug extends Mesh { |
| 54 | + |
| 55 | + protected int radialSamples = 32; |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructs a new BoundingSphereDebug mesh. |
| 59 | + */ |
| 60 | + public BoundingSphereDebug() { |
| 61 | + setGeometryData(); |
| 62 | + setIndexData(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Generates and sets the position and color data for the three circles. |
| 67 | + * The circles are drawn in the XY (blue), XZ (green), and YZ (yellow) planes. |
| 68 | + */ |
| 69 | + private void setGeometryData() { |
| 70 | + setMode(Mode.Lines); |
| 71 | + |
| 72 | + // Each circle has radialSamples + 1 vertices (to close the loop) |
| 73 | + // We have 3 circles, so (radialSamples + 1) * 3 vertices in total. |
| 74 | + FloatBuffer posBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 3); |
| 75 | + FloatBuffer colBuf = BufferUtils.createVector3Buffer((radialSamples + 1) * 4); |
| 76 | + |
| 77 | + setBuffer(Type.Position, 3, posBuf); |
| 78 | + setBuffer(Type.Color, 4, colBuf); |
| 79 | + |
| 80 | + // generate geometry |
| 81 | + float fInvRS = 1.0f / radialSamples; |
| 82 | + |
| 83 | + // Generate points for a unit circle |
| 84 | + float[] sin = new float[(radialSamples + 1)]; |
| 85 | + float[] cos = new float[(radialSamples + 1)]; |
| 86 | + for (int i = 0; i < radialSamples; i++) { |
| 87 | + float fAngle = FastMath.TWO_PI * fInvRS * i; |
| 88 | + cos[i] = FastMath.cos(fAngle); |
| 89 | + sin[i] = FastMath.sin(fAngle); |
| 90 | + } |
| 91 | + // Close the loop by repeating the first point |
| 92 | + sin[radialSamples] = sin[0]; |
| 93 | + cos[radialSamples] = cos[0]; |
| 94 | + |
| 95 | + // XY Plane Circle (Blue) |
| 96 | + for (int i = 0; i <= radialSamples; i++) { |
| 97 | + addCircleData(posBuf, colBuf, cos[i], sin[i], 0, ColorRGBA.Blue); |
| 98 | + } |
| 99 | + // XZ Plane Circle (Green) |
| 100 | + for (int i = 0; i <= radialSamples; i++) { |
| 101 | + addCircleData(posBuf, colBuf, cos[i], 0, sin[i], ColorRGBA.Green); |
| 102 | + } |
| 103 | + // YZ Plane Circle (Yellow) |
| 104 | + for (int i = 0; i <= radialSamples; i++) { |
| 105 | + addCircleData(posBuf, colBuf, 0, cos[i], sin[i], ColorRGBA.Yellow); |
| 106 | + } |
| 107 | + |
| 108 | + updateBound(); |
| 109 | + setStatic(); |
| 110 | + } |
| 111 | + |
| 112 | + private void addCircleData(FloatBuffer posBuf, FloatBuffer colBuf, |
| 113 | + float x, float y, float z, ColorRGBA c) { |
| 114 | + posBuf.put(x).put(y).put(z); |
| 115 | + colBuf.put(c.r).put(c.g).put(c.b).put(c.a); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Sets the index data for rendering the circles as lines. |
| 120 | + * Each circle is made of `radialSamples` line segments. |
| 121 | + */ |
| 122 | + private void setIndexData() { |
| 123 | + |
| 124 | + // allocate connectivity |
| 125 | + int nbSegments = (radialSamples) * 3; // 3 circles |
| 126 | + |
| 127 | + ShortBuffer idxBuf = BufferUtils.createShortBuffer(2 * nbSegments); |
| 128 | + setBuffer(Type.Index, 2, idxBuf); |
| 129 | + |
| 130 | + for (int c = 0; c < 3; c++) { // For each of the 3 circles |
| 131 | + int baseIndex = c * (radialSamples + 1); |
| 132 | + for (int i = 0; i < radialSamples; i++) { |
| 133 | + idxBuf.put((short) (baseIndex + i)); |
| 134 | + idxBuf.put((short) (baseIndex + i + 1)); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Convenience factory method that creates a debug bounding-sphere geometry |
| 141 | + * |
| 142 | + * @param assetManager the assetManager |
| 143 | + * @return the bounding sphere debug geometry. |
| 144 | + */ |
| 145 | + public static Geometry createDebugSphere(AssetManager assetManager) { |
| 146 | + BoundingSphereDebug mesh = new BoundingSphereDebug(); |
| 147 | + Geometry geom = new Geometry("BoundingDebug", mesh); |
| 148 | + Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
| 149 | + mat.setBoolean("VertexColor", true); |
| 150 | + mat.getAdditionalRenderState().setWireframe(true); |
| 151 | + geom.setMaterial(mat); |
| 152 | + return geom; |
| 153 | + } |
| 154 | + |
| 155 | + } |
0 commit comments