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 Bubble extends Mesh {
/**
* The center.
*/
private Vector3f ref = Vector3f.ZERO;
/**
* The radius.
*/
private float radius;
/**
* The width.
*/
private float width;
/**
* The samples.
*/
private int samples = 12;
public Bubble(float height, float width) {
super();
this.radius = height/2f;
this.width = width;
ref.z = width/2f-radius;
//center.x = 1f;
//setMode(Mode.Lines);
updateGeometry();
}
protected void updateGeometry() {
Vector3f[] vertices = new Vector3f[(samples +2)*2];
Vector2f[] texCoord = new Vector2f[(samples +2)*2];
int[] indices = new int[(samples * 6)+9];
float rate = FastMath.PI / ((float) (samples));
float angle = 0;
float rw = radius/width;
float rbw = (width-radius)/width;
//left center
vertices[0] = ref.clone();
texCoord[0] = new Vector2f(rbw, 0.5f);
// draw left half circle
int lastPoint = 0;
for (int i = 0; i <= samples; i++) {
float x = FastMath.cos(angle);
float z = FastMath.sin(angle);
int verticesBase = i+1;
int indicesBase = (i-1)*3;
vertices[verticesBase] = new Vector3f((x * radius) + ref.x, ref.y, (z * radius) + ref.z);
texCoord[verticesBase] = new Vector2f(1-rw+(z * rw), (x * 0.5f) + 0.5f);
if (lastPoint > 0) {
indices[indicesBase] = 0;
indices[indicesBase+1] = verticesBase;
indices[indicesBase+2] = lastPoint;
}
lastPoint = verticesBase;
angle += rate;
}
// right center
ref.z = radius-width/2f;
int base2 = samples+2;
vertices[base2] = ref.clone();
texCoord[base2] = new Vector2f(rw, 0.5f);
angle = FastMath.PI;
lastPoint = 0;
// draw right half circle
for (int i = 0; i <= samples; i++) {
float x = FastMath.cos(angle);
float z = FastMath.sin(angle);
int verticesBase = i+samples+3;
int indicesBase = (i+samples)*3;
vertices[verticesBase] = new Vector3f((x * radius) + ref.x, ref.y, (z * radius) + ref.z);
texCoord[verticesBase] = new Vector2f(rw + (z * rw), (x * 0.5f) + 0.5f);
System.out.println(texCoord[verticesBase]);
if (lastPoint > 0) {
indices[indicesBase] = base2;
indices[indicesBase+1] = verticesBase;
indices[indicesBase+2] = lastPoint;
}
lastPoint = verticesBase;
angle += rate;
}
// draw Rectangle
indices[samples*6+3] = 1;
indices[samples*6+4] = 3+samples;
indices[samples*6+5] = 1+samples;
indices[samples*6+6] = 3+samples;
indices[samples*6+7] = 1;
indices[samples*6+8] = 3+samples*2;
setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indices));
updateBound();
}
}