Advertisement
Guest User

Untitled

a guest
May 27th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. /**
  2.  
  3. Circle.
  4.  
  5. *
  6. @author Martin Simons
  7. @version $Id$
  8.  
  9. */
  10.  
  11. public class Circle
  12.  
  13. extends Mesh
  14.  
  15. {
  16.  
  17. /**
  18. The center.
  19.  
  20. */
  21.  
  22. private Vector3f center;
  23.  
  24.  
  25.  
  26. /**
  27. The radius.
  28.  
  29. */
  30.  
  31. private float radius;
  32.  
  33.  
  34.  
  35. /**
  36. The samples.
  37.  
  38. */
  39.  
  40. private int samples;
  41.  
  42.  
  43.  
  44. /**
  45. Constructs a new instance of this class.
  46.  
  47. *
  48. @param radius
  49.  
  50. */
  51.  
  52. public Circle(float radius)
  53.  
  54. {
  55.  
  56. this(Vector3f.ZERO, radius, 16);
  57.  
  58. }
  59.  
  60.  
  61.  
  62. /**
  63. Constructs a new instance of this class.
  64.  
  65. *
  66. @param radius
  67. @param samples
  68.  
  69. */
  70.  
  71. public Circle(float radius, int samples)
  72.  
  73. {
  74.  
  75. this(Vector3f.ZERO, radius, samples);
  76.  
  77. }
  78.  
  79.  
  80.  
  81. /**
  82. Constructs a new instance of this class.
  83.  
  84. *
  85. @param center
  86. @param radius
  87. @param samples
  88.  
  89. */
  90.  
  91. public Circle(Vector3f center, float radius, int samples)
  92.  
  93. {
  94.  
  95. super();
  96.  
  97. this.center = center;
  98.  
  99. this.radius = radius;
  100.  
  101. this.samples = samples;
  102.  
  103.  
  104.  
  105. setMode(Mode.Lines);
  106.  
  107. updateGeometry();
  108.  
  109. }
  110.  
  111.  
  112.  
  113. protected void updateGeometry()
  114.  
  115. {
  116.  
  117. FloatBuffer positions = BufferUtils.createFloatBuffer(samples * 3);
  118.  
  119. FloatBuffer normals = BufferUtils.createFloatBuffer(samples * 3);
  120.  
  121. short[] indices = new short[samples * 2];
  122.  
  123.  
  124.  
  125. float rate = FastMath.TWO_PI / (float) samples;
  126.  
  127. float angle = 0;
  128.  
  129. for (int i = 0; i < samples; i++)
  130.  
  131. {
  132.  
  133. float x = FastMath.cos(angle) + center.x;
  134.  
  135. float z = FastMath.sin(angle) + center.z;
  136.  
  137.  
  138.  
  139. positions.put(x * radius).put(center.y).put(z * radius);
  140.  
  141. normals.put(new float[] { 0, 1, 0 });
  142.  
  143. indices = (short) i;
  144.  
  145. if (i < samples - 1)
  146.  
  147. indices = (short) (i + 1);
  148.  
  149. else
  150.  
  151. indices = 0;
  152.  
  153.  
  154.  
  155. angle += rate;
  156.  
  157. }
  158.  
  159.  
  160.  
  161. setBuffer(Type.Position, 3, positions);
  162.  
  163. setBuffer(Type.Normal, 3, normals);
  164.  
  165. setBuffer(Type.Index, 2, indices);
  166.  
  167.  
  168.  
  169. setBuffer(Type.TexCoord, 2, new float[] { 0, 0, 1, 1 });
  170.  
  171.  
  172.  
  173. updateBound();
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement