|
32 | 32 | package com.jme3.environment.util; |
33 | 33 |
|
34 | 34 | import com.jme3.asset.AssetManager; |
| 35 | + import com.jme3.export.InputCapsule; |
| 36 | + import com.jme3.export.JmeExporter; |
| 37 | + import com.jme3.export.JmeImporter; |
| 38 | + import com.jme3.export.OutputCapsule; |
35 | 39 | import com.jme3.material.Material; |
36 | 40 | import com.jme3.math.ColorRGBA; |
37 | 41 | import com.jme3.math.FastMath; |
|
40 | 44 | import com.jme3.scene.VertexBuffer.Type; |
41 | 45 | import com.jme3.util.BufferUtils; |
42 | 46 |
|
| 47 | + import java.io.IOException; |
43 | 48 | import java.nio.FloatBuffer; |
44 | 49 | import java.nio.ShortBuffer; |
45 | 50 |
|
@@ -69,48 +74,48 @@ public BoundingSphereDebug() { |
69 | 74 | private void setGeometryData() { |
70 | 75 | setMode(Mode.Lines); |
71 | 76 |
|
| 77 | + int numVertices = radialSamples + 1; |
| 78 | + |
72 | 79 | // Each circle has radialSamples + 1 vertices (to close the loop) |
73 | 80 | // 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); |
| 81 | + FloatBuffer posBuf = BufferUtils.createVector3Buffer(numVertices * 3); |
| 82 | + FloatBuffer colBuf = BufferUtils.createVector3Buffer(numVertices * 4); |
79 | 83 |
|
80 | | - // generate geometry |
81 | | - float fInvRS = 1.0f / radialSamples; |
| 84 | + // --- Generate Geometry Data --- |
| 85 | + float angleStep = FastMath.TWO_PI / radialSamples; |
82 | 86 |
|
83 | 87 | // 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); |
| 88 | + float[] sin = new float[numVertices]; |
| 89 | + float[] cos = new float[numVertices]; |
| 90 | + |
| 91 | + for (int i = 0; i <= radialSamples; i++) { |
| 92 | + float angle = angleStep * i; |
| 93 | + cos[i] = FastMath.cos(angle); |
| 94 | + sin[i] = FastMath.sin(angle); |
90 | 95 | } |
91 | | - // Close the loop by repeating the first point |
92 | | - sin[radialSamples] = sin[0]; |
93 | | - cos[radialSamples] = cos[0]; |
94 | 96 |
|
95 | 97 | // XY Plane Circle (Blue) |
96 | | - for (int i = 0; i <= radialSamples; i++) { |
| 98 | + for (int i = 0; i < numVertices; i++) { |
97 | 99 | addCircleData(posBuf, colBuf, cos[i], sin[i], 0, ColorRGBA.Blue); |
98 | 100 | } |
99 | 101 | // XZ Plane Circle (Green) |
100 | | - for (int i = 0; i <= radialSamples; i++) { |
| 102 | + for (int i = 0; i < numVertices; i++) { |
101 | 103 | addCircleData(posBuf, colBuf, cos[i], 0, sin[i], ColorRGBA.Green); |
102 | 104 | } |
103 | 105 | // YZ Plane Circle (Yellow) |
104 | | - for (int i = 0; i <= radialSamples; i++) { |
| 106 | + for (int i = 0; i < numVertices; i++) { |
105 | 107 | addCircleData(posBuf, colBuf, 0, cos[i], sin[i], ColorRGBA.Yellow); |
106 | 108 | } |
107 | 109 |
|
| 110 | + setBuffer(Type.Position, 3, posBuf); |
| 111 | + setBuffer(Type.Color, 4, colBuf); |
| 112 | + |
108 | 113 | updateBound(); |
109 | 114 | setStatic(); |
110 | 115 | } |
111 | 116 |
|
112 | 117 | private void addCircleData(FloatBuffer posBuf, FloatBuffer colBuf, |
113 | | - float x, float y, float z, ColorRGBA c) { |
| 118 | + float x, float y, float z, ColorRGBA c) { |
114 | 119 | posBuf.put(x).put(y).put(z); |
115 | 120 | colBuf.put(c.r).put(c.g).put(c.b).put(c.a); |
116 | 121 | } |
@@ -152,4 +157,18 @@ public static Geometry createDebugSphere(AssetManager assetManager) { |
152 | 157 | return geom; |
153 | 158 | } |
154 | 159 |
|
| 160 | + @Override |
| 161 | + public void write(JmeExporter ex) throws IOException { |
| 162 | + super.write(ex); |
| 163 | + OutputCapsule oc = ex.getCapsule(this); |
| 164 | + oc.write(radialSamples, "radialSamples", 32); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void read(JmeImporter im) throws IOException { |
| 169 | + super.read(im); |
| 170 | + InputCapsule ic = im.getCapsule(this); |
| 171 | + radialSamples = ic.readInt("radialSamples", 32); |
| 172 | + } |
| 173 | + |
155 | 174 | } |
0 commit comments