Skip to content

Commit f7381b4

Browse files
authored
BoundingSphereDebug: add read/write methods
Optimize code for geometry data generation
1 parent b4d8c2b commit f7381b4

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

jme3-core/src/main/java/com/jme3/environment/util/BoundingSphereDebug.java

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
package com.jme3.environment.util;
3333

3434
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;
3539
import com.jme3.material.Material;
3640
import com.jme3.math.ColorRGBA;
3741
import com.jme3.math.FastMath;
@@ -40,6 +44,7 @@
4044
import com.jme3.scene.VertexBuffer.Type;
4145
import com.jme3.util.BufferUtils;
4246

47+
import java.io.IOException;
4348
import java.nio.FloatBuffer;
4449
import java.nio.ShortBuffer;
4550

@@ -69,48 +74,48 @@ public BoundingSphereDebug() {
6974
private void setGeometryData() {
7075
setMode(Mode.Lines);
7176

77+
int numVertices = radialSamples + 1;
78+
7279
// Each circle has radialSamples + 1 vertices (to close the loop)
7380
// 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);
7983

80-
// generate geometry
81-
float fInvRS = 1.0f / radialSamples;
84+
// --- Generate Geometry Data ---
85+
float angleStep = FastMath.TWO_PI / radialSamples;
8286

8387
// 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);
9095
}
91-
// Close the loop by repeating the first point
92-
sin[radialSamples] = sin[0];
93-
cos[radialSamples] = cos[0];
9496

9597
// XY Plane Circle (Blue)
96-
for (int i = 0; i <= radialSamples; i++) {
98+
for (int i = 0; i < numVertices; i++) {
9799
addCircleData(posBuf, colBuf, cos[i], sin[i], 0, ColorRGBA.Blue);
98100
}
99101
// XZ Plane Circle (Green)
100-
for (int i = 0; i <= radialSamples; i++) {
102+
for (int i = 0; i < numVertices; i++) {
101103
addCircleData(posBuf, colBuf, cos[i], 0, sin[i], ColorRGBA.Green);
102104
}
103105
// YZ Plane Circle (Yellow)
104-
for (int i = 0; i <= radialSamples; i++) {
106+
for (int i = 0; i < numVertices; i++) {
105107
addCircleData(posBuf, colBuf, 0, cos[i], sin[i], ColorRGBA.Yellow);
106108
}
107109

110+
setBuffer(Type.Position, 3, posBuf);
111+
setBuffer(Type.Color, 4, colBuf);
112+
108113
updateBound();
109114
setStatic();
110115
}
111116

112117
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) {
114119
posBuf.put(x).put(y).put(z);
115120
colBuf.put(c.r).put(c.g).put(c.b).put(c.a);
116121
}
@@ -152,4 +157,18 @@ public static Geometry createDebugSphere(AssetManager assetManager) {
152157
return geom;
153158
}
154159

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+
155174
}

0 commit comments

Comments
 (0)