Sphere.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package min3d.objectPrimitives;
  2. import min3d.Utils;
  3. import min3d.core.Object3dContainer;
  4. import min3d.vos.Color4;
  5. import min3d.vos.Number3d;
  6. /**
  7. * Creates a sphere.
  8. * Vertex colors are assigned randomly across the 'latitudes' of the sphere,
  9. */
  10. public class Sphere extends Object3dContainer
  11. {
  12. private float _radius;
  13. private int _cols;
  14. private int _rows;
  15. public Sphere(float $radius, int $columns, int $rows, Boolean $useUvs, Boolean $useNormals, Boolean $useVertexColors)
  16. {
  17. super(
  18. ($columns+1) * ($rows+1),
  19. $columns * $rows * 2,
  20. $useUvs,
  21. $useNormals,
  22. $useVertexColors
  23. );
  24. _cols = $columns;
  25. _rows = $rows;
  26. _radius = $radius;
  27. build();
  28. }
  29. public Sphere(float $radius, int $columns, int $rows)
  30. {
  31. super(
  32. ($columns+1) * ($rows+1),
  33. $columns * $rows * 2,
  34. true,
  35. true,
  36. true
  37. );
  38. _cols = $columns;
  39. _rows = $rows;
  40. _radius = $radius;
  41. build();
  42. }
  43. public Sphere(float $radius, int $columns, int $rows, Color4 color)
  44. {
  45. super(
  46. ($columns+1) * ($rows+1),
  47. $columns * $rows * 2,
  48. true,
  49. true,
  50. true
  51. );
  52. defaultColor(color);
  53. _cols = $columns;
  54. _rows = $rows;
  55. _radius = $radius;
  56. build();
  57. }
  58. private void build()
  59. {
  60. int r, c;
  61. Number3d n = new Number3d();
  62. Number3d pos = new Number3d();
  63. Number3d posFull = new Number3d();
  64. if( defaultColor() == null ) defaultColor(new Color4());
  65. // Build vertices
  66. for (r = 0; r <= _rows; r++)
  67. {
  68. float v = (float)r / (float)_rows; // [0,1]
  69. float theta1 = v * (float)Math.PI; // [0,PI]
  70. n.setAll(0,1,0);
  71. n.rotateZ(theta1);
  72. // each 'row' assigned random color. for the hell of it.
  73. for (c = 0; c <= _cols; c++)
  74. {
  75. float u = (float)c / (float)_cols; // [0,1]
  76. float theta2 = u * (float)(Math.PI * 2f); // [0,2PI]
  77. pos.setAllFrom(n);
  78. pos.rotateY(theta2);
  79. posFull.setAllFrom(pos);
  80. posFull.multiply(_radius);
  81. this.vertices().addVertex(posFull.x,posFull.y,posFull.z, u,v, pos.x,pos.y,pos.z, defaultColor().r,defaultColor().g,defaultColor().b,defaultColor().a);
  82. }
  83. }
  84. // Add faces
  85. int colLength = _cols + 1;
  86. for (r = 0; r < _rows; r++)
  87. {
  88. int offset = r * colLength;
  89. for (c = 0; c < _cols; c++)
  90. {
  91. int ul = offset + c;
  92. int ur = offset + c+1;
  93. int br = offset + (int)(c + 1 + colLength);
  94. int bl = offset + (int)(c + 0 + colLength);
  95. Utils.addQuad(this, ul,ur,br,bl);
  96. }
  97. }
  98. }
  99. }