Skip to content

Commit 336823b

Browse files
authored
Update Circle.java
1 parent b0cf966 commit 336823b

File tree

1 file changed

+30
-29
lines changed
  • jme3-core/src/main/java/com/jme3/environment/util

1 file changed

+30
-29
lines changed

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
* It's defined by a specified number of radial samples, which determine its smoothness.</p>
5151
*
5252
* <p>The circle is centered at (0,0,0) in its local coordinate space and has a radius of 1.0.</p>
53+
*
54+
* @author capdevon
5355
*/
5456
public class Circle extends Mesh {
5557

@@ -81,62 +83,61 @@ public Circle(int radialSamples) {
8183
}
8284

8385
/**
84-
* builds the vertices based on the radius.
86+
* Initializes the vertex buffers for the circle mesh.
8587
*/
8688
private void setGeometryData() {
8789
setMode(Mode.Lines);
8890

89-
FloatBuffer posBuf = BufferUtils.createVector3Buffer((radialSamples + 1));
90-
FloatBuffer colBuf = BufferUtils.createFloatBuffer((radialSamples + 1) * 4);
91-
FloatBuffer texBuf = BufferUtils.createVector2Buffer(radialSamples + 1);
91+
int numVertices = radialSamples + 1;
92+
93+
FloatBuffer posBuf = BufferUtils.createVector3Buffer(numVertices);
94+
FloatBuffer colBuf = BufferUtils.createFloatBuffer(numVertices * 4); // 4 floats per color (RGBA)
95+
FloatBuffer texBuf = BufferUtils.createVector2Buffer(numVertices);
9296

9397
setBuffer(Type.Position, 3, posBuf);
9498
setBuffer(Type.Color, 4, colBuf);
9599
setBuffer(Type.TexCoord, 2, texBuf);
96100

97-
// generate geometry
98-
float fInvRS = 1.0f / radialSamples;
101+
// --- Generate Geometry Data ---
102+
float[] sin = new float[numVertices];
103+
float[] cos = new float[numVertices];
104+
float angleStep = FastMath.TWO_PI / radialSamples;
99105

100-
// Generate points on the unit circle to be used in computing the mesh
101-
// points on a sphere slice.
102-
float[] sin = new float[(radialSamples + 1)];
103-
float[] cos = new float[(radialSamples + 1)];
104-
for (int i = 0; i < radialSamples; i++) {
105-
float angle = FastMath.TWO_PI * fInvRS * i;
106+
// Populate sine and cosine arrays for all sample points.
107+
for (int i = 0; i <= radialSamples; i++) {
108+
float angle = angleStep * i;
106109
cos[i] = FastMath.cos(angle);
107110
sin[i] = FastMath.sin(angle);
108111
}
109-
sin[radialSamples] = sin[0];
110-
cos[radialSamples] = cos[0];
111112

113+
// Define the color for the entire circle.
112114
ColorRGBA color = ColorRGBA.Orange;
113-
for (int iR = 0; iR <= radialSamples; iR++) {
114-
posBuf.put(cos[iR]).put(sin[iR]).put(0);
115+
116+
// Populate the position, color, and texture coordinate buffers.
117+
for (int i = 0; i < numVertices; i++) {
118+
posBuf.put(cos[i]).put(sin[i]).put(0);
115119
colBuf.put(color.r).put(color.g).put(color.b).put(color.a);
116-
texBuf.put(iR % 2f).put(iR % 2f);
120+
texBuf.put(i % 2f).put(i % 2f);
117121
}
118122

119123
updateBound();
120124
setStatic();
121125
}
122126

123127
/**
124-
* sets the indices for rendering the circle.
128+
* Initializes the index buffer for the circle mesh.
125129
*/
126130
private void setIndexData() {
127131
// allocate connectivity
128-
int nbSegments = (radialSamples);
132+
int numIndices = radialSamples * 2;
129133

130-
ShortBuffer idxBuf = BufferUtils.createShortBuffer(2 * nbSegments);
134+
ShortBuffer idxBuf = BufferUtils.createShortBuffer(numIndices);
131135
setBuffer(Type.Index, 2, idxBuf);
132136

133-
int idx = 0;
134-
int segDone = 0;
135-
while (segDone < nbSegments) {
136-
idxBuf.put((short) idx);
137-
idxBuf.put((short) (idx + 1));
138-
idx++;
139-
segDone++;
137+
// --- Generate Index Data ---
138+
for (int i = 0; i < radialSamples; i++) {
139+
idxBuf.put((short) i); // Start of segment
140+
idxBuf.put((short) (i + 1)); // End of segment
140141
}
141142
}
142143

@@ -165,7 +166,7 @@ public static Geometry createShape(AssetManager assetManager, String name, int r
165166
Geometry geom = new Geometry(name, mesh);
166167
geom.setQueueBucket(RenderQueue.Bucket.Transparent);
167168

168-
Material mat = new Material(assetManager, "Common/MatDefs/Dashed/dashed.j3md");
169+
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Dashed.j3md");
169170
mat.getAdditionalRenderState().setWireframe(true);
170171
mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
171172
mat.getAdditionalRenderState().setDepthWrite(false);
@@ -178,4 +179,4 @@ public static Geometry createShape(AssetManager assetManager, String name, int r
178179
return geom;
179180
}
180181

181-
}
182+
}

0 commit comments

Comments
 (0)