Box.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package min3d.objectPrimitives;
  2. import min3d.Utils;
  3. import min3d.core.Object3dContainer;
  4. import min3d.vos.Color4;
  5. /**
  6. * Note how each 'face' (quad) of the box uses its own set of 4 vertices each,
  7. * rather than sharing with adjacent faces. This allows for each face to be
  8. * texture mapped, normal'ed, and colored independently of the others.
  9. *
  10. * Object origin is center of box.
  11. */
  12. public class Box extends Object3dContainer
  13. {
  14. private Color4[] _cols;
  15. private float _width;
  16. private float _height;
  17. private float _depth;
  18. public Box(float $width, float $height, float $depth, Color4[] $sixColor4s, Boolean $useUvs, Boolean $useNormals, Boolean $useVertexColors)
  19. {
  20. super(4*6, 2*6, $useUvs,$useNormals,$useVertexColors);
  21. _width = $width;
  22. _height = $height;
  23. _depth = $depth;
  24. if ($sixColor4s != null)
  25. {
  26. _cols = $sixColor4s;
  27. }
  28. else
  29. {
  30. _cols = new Color4[6];
  31. _cols[0] = new Color4(255,0,0,255);
  32. _cols[1] = new Color4(0,255,0,255);
  33. _cols[2] = new Color4(0,0,255,255);
  34. _cols[3] = new Color4(255,255,0,255);
  35. _cols[4] = new Color4(0,255,255,255);
  36. _cols[5] = new Color4(255,0,255,255);
  37. }
  38. make();
  39. }
  40. public Box(float $width, float $height, float $depth, Color4[] $sixColor4s)
  41. {
  42. this($width,$height,$depth, $sixColor4s, true,true,true);
  43. }
  44. public Box(float $width, float $height, float $depth, Color4 color)
  45. {
  46. this($width,$height,$depth, new Color4[] { color, color, color, color, color, color }, true,true,true);
  47. }
  48. public Box(float $width, float $height, float $depth)
  49. {
  50. this($width,$height,$depth, null, true,true,true);
  51. }
  52. private void make()
  53. {
  54. float w = _width / 2f;
  55. float h = _height / 2f;
  56. float d = _depth / 2f;
  57. short ul, ur, lr, ll;
  58. // front
  59. ul = this.vertices().addVertex(-w,+h,+d, 0f,0f, 0,0,1, _cols[0].r,_cols[0].g,_cols[0].b,_cols[0].a);
  60. ur = this.vertices().addVertex(+w,+h,+d, 1f,0f, 0,0,1, _cols[0].r,_cols[0].g,_cols[0].b,_cols[0].a);
  61. lr = this.vertices().addVertex(+w,-h,+d, 1f,1f, 0,0,1, _cols[0].r,_cols[0].g,_cols[0].b,_cols[0].a);
  62. ll = this.vertices().addVertex(-w,-h,+d, 0f,1f, 0,0,1, _cols[0].r,_cols[0].g,_cols[0].b,_cols[0].a);
  63. Utils.addQuad(this, ul,ur,lr,ll);
  64. // right
  65. ul = this.vertices().addVertex(+w,+h,+d, 0f,0f, 1,0,0, _cols[1].r,_cols[1].g,_cols[1].b,_cols[1].a);
  66. ur = this.vertices().addVertex(+w,+h,-d, 1f,0f, 1,0,0, _cols[1].r,_cols[1].g,_cols[1].b,_cols[1].a);
  67. lr = this.vertices().addVertex(+w,-h,-d, 1f,1f, 1,0,0, _cols[1].r,_cols[1].g,_cols[1].b,_cols[1].a);
  68. ll = this.vertices().addVertex(+w,-h,+d, 0f,1f, 1,0,0, _cols[1].r,_cols[1].g,_cols[1].b,_cols[1].a);
  69. Utils.addQuad(this, ul,ur,lr,ll);
  70. // back
  71. ul = this.vertices().addVertex(+w,+h,-d, 0f,0f, 0,0,-1, _cols[2].r,_cols[2].g,_cols[2].b,_cols[2].a);
  72. ur = this.vertices().addVertex(-w,+h,-d, 1f,0f, 0,0,-1, _cols[2].r,_cols[2].g,_cols[2].b,_cols[2].a);
  73. lr = this.vertices().addVertex(-w,-h,-d, 1f,1f, 0,0,-1, _cols[2].r,_cols[2].g,_cols[2].b,_cols[2].a);
  74. ll = this.vertices().addVertex(+w,-h,-d, 0f,1f, 0,0,-1, _cols[2].r,_cols[2].g,_cols[2].b,_cols[2].a);
  75. Utils.addQuad(this, ul,ur,lr,ll);
  76. // left
  77. ul = this.vertices().addVertex(-w,+h,-d, 0f,0f, -1,0,0, _cols[3].r,_cols[3].g,_cols[3].b,_cols[3].a);
  78. ur = this.vertices().addVertex(-w,+h,+d, 1f,0f, -1,0,0, _cols[3].r,_cols[3].g,_cols[3].b,_cols[3].a);
  79. lr = this.vertices().addVertex(-w,-h,+d, 1f,1f, -1,0,0, _cols[3].r,_cols[3].g,_cols[3].b,_cols[3].a);
  80. ll = this.vertices().addVertex(-w,-h,-d, 0f,1f, -1,0,0, _cols[3].r,_cols[3].g,_cols[3].b,_cols[3].a);
  81. Utils.addQuad(this, ul,ur,lr,ll);
  82. // top
  83. ul = this.vertices().addVertex(-w,+h,-d, 0f,0f, 0,1,0, _cols[4].r,_cols[4].g,_cols[4].b,_cols[4].a);
  84. ur = this.vertices().addVertex(+w,+h,-d, 1f,0f, 0,1,0, _cols[4].r,_cols[4].g,_cols[4].b,_cols[4].a);
  85. lr = this.vertices().addVertex(+w,+h,+d, 1f,1f, 0,1,0, _cols[4].r,_cols[4].g,_cols[4].b,_cols[4].a);
  86. ll = this.vertices().addVertex(-w,+h,+d, 0f,1f, 0,1,0, _cols[4].r,_cols[4].g,_cols[4].b,_cols[4].a);
  87. Utils.addQuad(this, ul,ur,lr,ll);
  88. // bottom
  89. ul = this.vertices().addVertex(-w,-h,+d, 0f,0f, 0,-1,0, _cols[5].r,_cols[5].g,_cols[5].b,_cols[5].a);
  90. ur = this.vertices().addVertex(+w,-h,+d, 1f,0f, 0,-1,0, _cols[5].r,_cols[5].g,_cols[5].b,_cols[5].a);
  91. lr = this.vertices().addVertex(+w,-h,-d, 1f,1f, 0,-1,0, _cols[5].r,_cols[5].g,_cols[5].b,_cols[5].a);
  92. ll = this.vertices().addVertex(-w,-h,-d, 0f,1f, 0,-1,0, _cols[5].r,_cols[5].g,_cols[5].b,_cols[5].a);
  93. Utils.addQuad(this, ul,ur,lr,ll);
  94. }
  95. }