Vertices.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package min3d.core;
  2. import min3d.vos.Color4;
  3. import min3d.vos.Number3d;
  4. import min3d.vos.Uv;
  5. public class Vertices
  6. {
  7. private Number3dBufferList _points;
  8. private UvBufferList _uvs;
  9. private Number3dBufferList _normals;
  10. private Color4BufferList _colors;
  11. private boolean _hasUvs;
  12. private boolean _hasNormals;
  13. private boolean _hasColors;
  14. /**
  15. * Used by Object3d to hold the lists of vertex points, texture coordinates (UV), normals, and vertex colors.
  16. * Use "addVertex()" to build the vertex data for the Object3d instance associated with this instance.
  17. *
  18. * Direct manipulation of position, UV, normal, or color data can be done directly through the associated
  19. * 'buffer list' instances contained herein.
  20. */
  21. public Vertices(int $maxElements)
  22. {
  23. _points = new Number3dBufferList($maxElements);
  24. _hasUvs = true;
  25. _hasNormals = true;
  26. _hasColors = true;
  27. if (_hasUvs) _uvs = new UvBufferList($maxElements);
  28. if (_hasNormals) _normals = new Number3dBufferList($maxElements);
  29. if (_hasColors) _colors = new Color4BufferList($maxElements);
  30. }
  31. /**
  32. * This version of the constructor adds 3 boolean arguments determine whether
  33. * uv, normal, and color lists will be used by this instance.
  34. * Set to false when appropriate to save memory, increase performance.
  35. */
  36. public Vertices(int $maxElements, Boolean $useUvs, Boolean $useNormals, Boolean $useColors)
  37. {
  38. _points = new Number3dBufferList($maxElements);
  39. _hasUvs = $useUvs;
  40. _hasNormals = $useNormals;
  41. _hasColors = $useColors;
  42. if (_hasUvs) _uvs = new UvBufferList($maxElements);
  43. if (_hasNormals) _normals = new Number3dBufferList($maxElements);
  44. if (_hasColors) _colors = new Color4BufferList($maxElements);
  45. }
  46. public Vertices(Number3dBufferList $points, UvBufferList $uvs, Number3dBufferList $normals,
  47. Color4BufferList $colors)
  48. {
  49. _points = $points;
  50. _uvs = $uvs;
  51. _normals = $normals;
  52. _colors = $colors;
  53. _hasUvs = _uvs != null && _uvs.size() > 0;
  54. _hasNormals = _normals != null && _normals.size() > 0;
  55. _hasColors = _colors != null && _colors.size() > 0;
  56. }
  57. public int size()
  58. {
  59. return _points.size();
  60. }
  61. public int capacity()
  62. {
  63. return _points.capacity();
  64. }
  65. public boolean hasUvs()
  66. {
  67. return _hasUvs;
  68. }
  69. public boolean hasNormals()
  70. {
  71. return _hasNormals;
  72. }
  73. public boolean hasColors()
  74. {
  75. return _hasColors;
  76. }
  77. /**
  78. * Use this to populate an Object3d's vertex data.
  79. * Return's newly added vertex's index
  80. *
  81. * If hasUvs, hasNormals, or hasColors was set to false,
  82. * their corresponding arguments are just ignored.
  83. */
  84. public short addVertex(
  85. float $pointX, float $pointY, float $pointZ,
  86. float $textureU, float $textureV,
  87. float $normalX, float $normalY, float $normalZ,
  88. short $colorR, short $colorG, short $colorB, short $colorA)
  89. {
  90. _points.add($pointX, $pointY, $pointZ);
  91. if (_hasUvs) _uvs.add($textureU, $textureV);
  92. if (_hasNormals) _normals.add($normalX, $normalY, $normalZ);
  93. if (_hasColors) _colors.add($colorR, $colorG, $colorB, $colorA);
  94. return (short)(_points.size()-1);
  95. }
  96. /**
  97. * More structured-looking way of adding a vertex (but potentially wasteful).
  98. * The VO's taken in as arguments are only used to read the values they hold
  99. * (no references are kept to them).
  100. * Return's newly added vertex's index
  101. *
  102. * If hasUvs, hasNormals, or hasColors was set to false,
  103. * their corresponding arguments are just ignored.
  104. */
  105. public short addVertex(Number3d $point, Uv $textureUv, Number3d $normal, Color4 $color)
  106. {
  107. _points.add($point);
  108. if (_hasUvs) _uvs.add($textureUv);
  109. if (_hasNormals) _normals.add($normal);
  110. if (_hasColors) _colors.add($color);
  111. return (short)(_points.size()-1);
  112. }
  113. public void overwriteVerts(float[] $newVerts)
  114. {
  115. _points.overwrite($newVerts);
  116. }
  117. public void overwriteNormals(float[] $newNormals)
  118. {
  119. _normals.overwrite($newNormals);
  120. }
  121. Number3dBufferList points() /*package-private*/
  122. {
  123. return _points;
  124. }
  125. /**
  126. * List of texture coordinates
  127. */
  128. UvBufferList uvs() /*package-private*/
  129. {
  130. return _uvs;
  131. }
  132. /**
  133. * List of normal values
  134. */
  135. Number3dBufferList normals() /*package-private*/
  136. {
  137. return _normals;
  138. }
  139. /**
  140. * List of color values
  141. */
  142. Color4BufferList colors() /*package-private*/
  143. {
  144. return _colors;
  145. }
  146. public Vertices clone()
  147. {
  148. Vertices v = new Vertices(_points.clone(), _uvs.clone(), _normals.clone(), _colors.clone());
  149. return v;
  150. }
  151. }