Package org.pollux3d.menu

Source Code of org.pollux3d.menu.BubbleInv

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 BubbleInv 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 BubbleInv(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;
   
    //left center
    vertices[0] = ref.clone();
    texCoord[0] = new Vector2f(0.5f, rw);
   
    // 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((x * 0.5f) + 0.5f, rw-(z * rw));
      System.out.println(texCoord[verticesBase]);
      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();
   
    float rbw = (width-radius)/width;
    texCoord[base2] = new Vector2f(0.5f, rbw);
    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((x * 0.5f) + 0.5f, 1 - (z * rw));
      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();
  }

}
TOP

Related Classes of org.pollux3d.menu.BubbleInv

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.