group.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Group implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <tuple.h>
  10. #include <ray.h>
  11. #include <group.h>
  12. #include <math_helper.h>
  13. #include <renderstat.h>
  14. #define MIN_ALLOC (2)
  15. Group::Group() : Shape(SHAPE_GROUP)
  16. {
  17. stats.addGroup();
  18. this->allocatedObjectCount = MIN_ALLOC;
  19. this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  20. this->objectCount = 0;
  21. this->allocatedUnboxableObjectCount = MIN_ALLOC;
  22. this->unboxableObjectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
  23. this->unboxableObjectCount = 0;
  24. }
  25. Intersect Group::intersect(Ray r)
  26. {
  27. Intersect ret;
  28. int i, j;
  29. if (this->objectCount > 0)
  30. {
  31. if (this->bounds.intesectMe(r))
  32. {
  33. for (i = 0 ; i < this->objectCount ; i++)
  34. {
  35. Intersect xs = this->objectList[i]->intersect(r);
  36. if (xs.count() > 0)
  37. {
  38. for (j = 0 ; j < xs.count() ; j++)
  39. {
  40. ret.add(xs[j]);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. /* We are force to do them all the time */
  47. if (this->unboxableObjectCount > 0)
  48. {
  49. for(i = 0; i < this->unboxableObjectCount; i++)
  50. {
  51. Intersect xs = this->unboxableObjectList[i]->intersect(r);
  52. if (xs.count() > 0)
  53. {
  54. for(j = 0; j < xs.count(); j++)
  55. {
  56. ret.add(xs[j]);
  57. }
  58. }
  59. }
  60. }
  61. return ret;
  62. }
  63. Intersect Group::localIntersect(Ray r)
  64. {
  65. return Intersect();
  66. }
  67. Tuple Group::localNormalAt(Tuple point)
  68. {
  69. return Vector(1, 0, 0);
  70. }
  71. /* ONLY INSERT SHAPES THAT ARE NOT GOING TO CHANGE ELSE..! */
  72. void Group::addObject(Shape *s)
  73. {
  74. if (s->haveFiniteBounds())
  75. {
  76. if ((this->objectCount + 1) > this->allocatedObjectCount)
  77. {
  78. this->allocatedObjectCount *= 2;
  79. this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
  80. }
  81. s->parent = this;
  82. s->updateTransform();
  83. this->objectList[this->objectCount++] = s;
  84. this->bounds | s->getBounds();
  85. }
  86. else
  87. {
  88. if ((this->unboxableObjectCount + 1) > this->allocatedUnboxableObjectCount)
  89. {
  90. this->allocatedUnboxableObjectCount *= 2;
  91. this->unboxableObjectList = (Shape **)realloc(this->unboxableObjectList, sizeof(Shape **) * this->allocatedUnboxableObjectCount);
  92. }
  93. s->parent = this;
  94. s->updateTransform();
  95. this->unboxableObjectList[this->unboxableObjectCount++] = s;
  96. }
  97. }
  98. bool Group::isEmpty()
  99. {
  100. return (this->objectCount == 0) && (this->unboxableObjectCount == 0);
  101. }
  102. BoundingBox Group::getBounds()
  103. {
  104. if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
  105. return this->bounds;
  106. }
  107. void Group::updateBoundingBox()
  108. {
  109. this->bounds.reset();
  110. if (this->objectCount > 0)
  111. {
  112. int i;
  113. for(i = 0; i < this->objectCount; i++)
  114. {
  115. if (!this->objectList[i]->haveFiniteBounds())
  116. {
  117. BoundingBox objB = this->objectList[i]->getBounds();
  118. this->bounds | objB;
  119. }
  120. }
  121. }
  122. }
  123. void Group::updateTransform()
  124. {
  125. int i;
  126. Shape::updateTransform();
  127. if (this->objectCount > 0)
  128. {
  129. for (i = 0 ; i < this->objectCount ; i++)
  130. {
  131. this->objectList[i]->updateTransform();
  132. }
  133. }
  134. /* We are force to do them all the time */
  135. if (this->unboxableObjectCount > 0)
  136. {
  137. for(i = 0; i < this->unboxableObjectCount; i++)
  138. {
  139. this->unboxableObjectList[i]->updateTransform();
  140. }
  141. }
  142. /* Once the full stack being notified of the changes, let's update the
  143. * bounding box
  144. */
  145. this->updateBoundingBox();
  146. }