package org.pollux3d.menu;
import com.jme3.math.FastMath;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.util.BufferUtils;
public class Ring extends Mesh {
private int samples = 32;
private Vector3f center = Vector3f.ZERO;
private float radius = 3;
private float width = 0.5f;
public Ring(float radius, float width) {
super();
this.radius = radius;
this.width = width;
/*
if (radius > samples*1.5f)
samples = (int) (radius * 0.75f);
*/
updateGeometry();
}
public void updateGeometry() {
Vector3f[] vertices = new Vector3f[samples * 2];
Vector2f[] texCoord = new Vector2f[samples * 2];
//FloatBuffer vertices = BufferUtils.createFloatBuffer(samples * 2);
//FloatBuffer texCoord = BufferUtils.createFloatBuffer(samples * 2);
int[] indices = new int[samples * 6];
float rate = FastMath.TWO_PI / (float) samples;
float angle = 0;
for (int i = 0; i < samples; i++) {
float x = FastMath.cos(angle) + center.x;
float y = FastMath.sin(angle) + center.y;
int base = i*2;
int indicesBase = i*6;
vertices[base] = new Vector3f(x * radius, y * radius, center.z);
vertices[base+1] = new Vector3f(x * (radius-width), y * (radius-width), center.z);
texCoord[base] = new Vector2f(x * radius, y * radius);
texCoord[base+1] = new Vector2f(x * (radius-width), y * (radius-width));
indices[indicesBase] = base;
indices[indicesBase+1] = calc(base+1);
indices[indicesBase+2] = calc(base-1);
indices[indicesBase+3] = base;
indices[indicesBase+4] = calc(base+2);
indices[indicesBase+5] = calc(base+1);
/*
System.out.println("angle: " + angle);
System.out.println("\tpoly1: " + indices[indicesBase] + " " + indices[indicesBase+1] + " " +indices[indicesBase+2]);
System.out.println("\tpoly1: " + indices[indicesBase+3] + " " + indices[indicesBase+4] + " " +indices[indicesBase+5]);
System.out.println("\tvertices "+base+": "+vertices[base] );
System.out.println("\tvertices "+(base+1)+": "+vertices[base+1] );
*/
angle += rate;
}
setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indices));
updateBound();
}
private int calc(int value) {
int max = samples * 2;
if(value >= max) return value - max;
if(value < 0) return max + value;
return value;
}
}