bvhoptimisation.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * BVH world optimiser implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <worldoptimiser.h>
  10. #include <cube.h>
  11. #include <transformation.h>
  12. void BVHOptimisation::makeTree(Group *leaf, int depth)
  13. {
  14. /* Let's take the bounding box of the root */
  15. BoundingBox rootBB = leaf->getBounds();
  16. double dx = (rootBB.max.x - rootBB.min.x);
  17. double dy = (rootBB.max.y - rootBB.min.y);
  18. double dz = (rootBB.max.z - rootBB.min.z);
  19. /* Take the mid value for each axes */
  20. Tuple midMin = rootBB.min;
  21. Tuple midMax = rootBB.max;
  22. BoundingBox SlicesBB[2];
  23. int sliceIdx;
  24. Group *Slices[2];
  25. double largestSide = max3(dx, dy, dz);
  26. int i;
  27. if (largestSide == dx)
  28. {
  29. midMin.x = rootBB.min.x + dx / 2.0;
  30. midMax.x = rootBB.min.x + dx / 2.0;
  31. }
  32. else if (largestSide == dy)
  33. {
  34. midMin.y = rootBB.min.y + dy / 2.0;
  35. midMax.y = rootBB.min.y + dy / 2.0;
  36. }
  37. else
  38. {
  39. midMin.z = rootBB.min.z + dx / 2.0;
  40. midMax.z = rootBB.min.z + dx / 2.0;
  41. }
  42. /* Split the main bounding box into 8 boxes */
  43. SlicesBB[0] | rootBB.min;
  44. SlicesBB[0] | midMax;
  45. SlicesBB[1] | rootBB.max;
  46. SlicesBB[1] | midMin;
  47. for (sliceIdx = 0 ; sliceIdx < 2 ; sliceIdx++)
  48. {
  49. Slices[sliceIdx] = nullptr;
  50. }
  51. for (i = 0 ; i < leaf->getObjectCount(); i++)
  52. {
  53. Shape *shp = leaf->getObject(i);
  54. if ((shp->getType() != Shape::GROUP) && (shp->getType() != Shape::OBJFILE))
  55. {
  56. BoundingBox objBB = shp->getBounds();
  57. for (sliceIdx = 0 ; sliceIdx < 2 ; sliceIdx++)
  58. {
  59. if (SlicesBB[sliceIdx].fitsIn(objBB))
  60. {
  61. if (Slices[sliceIdx] == nullptr)
  62. {
  63. char name[32];
  64. snprintf(name, 32, "%d_Slice %d", depth, sliceIdx);
  65. //for(int j=0; j < depth; j++) { printf(" "); }
  66. //printf("%s\n", name);
  67. Slices[sliceIdx] = new Group(name);
  68. Slices[sliceIdx]->setBounds(SlicesBB[sliceIdx]);
  69. }
  70. Slices[sliceIdx]->addObject(shp);
  71. leaf->removeObject(shp);
  72. i -= 1;
  73. break;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. leaf->removeObject(shp);
  80. i -= 1;
  81. }
  82. }
  83. /* Now add the quadrant to the root and recurse in it */
  84. for (sliceIdx = 0 ; sliceIdx < 2 ; sliceIdx++)
  85. {
  86. if (Slices[sliceIdx] != nullptr)
  87. {
  88. this->makeTree(Slices[sliceIdx], depth + 1);
  89. Slices[sliceIdx]->updateBoundingBox();
  90. leaf->addObject(Slices[sliceIdx]);
  91. #if 0
  92. Cube *cb = new Cube();
  93. double sx = SlicesBB[sliceIdx].max.x - SlicesBB[sliceIdx].min.x;
  94. double sy = SlicesBB[sliceIdx].max.y - SlicesBB[sliceIdx].min.y;
  95. double sz = SlicesBB[sliceIdx].max.z - SlicesBB[sliceIdx].min.z;
  96. cb->setTransform(translation(SlicesBB[sliceIdx].min.x, SlicesBB[sliceIdx].min.y,
  97. SlicesBB[sliceIdx].min.z) * scaling(sx, sy, sz));
  98. cb->material.colour = Colour(0.01, 0.01, 0);
  99. cb->materialSet = true;
  100. cb->dropShadow = false;
  101. cb->material.ambient = 0.1;
  102. cb->material.reflective = 0;
  103. cb->material.transparency = 0.95;
  104. cb->material.refractiveIndex = 1;
  105. cb->material.specular = 0;
  106. leaf->addObject(cb);
  107. printf("%s: %d objs\n", Slices[sliceIdx]->getName(),
  108. Slices[sliceIdx]->getObjectCount());
  109. #endif
  110. }
  111. }
  112. }
  113. void BVHOptimisation::run()
  114. {
  115. /* First let's clear our hands */
  116. this->moveInfiniteObjects();
  117. /* Then let's have some fun! */
  118. this->moveAllObjects();
  119. /* Now.. The fun start ! */
  120. makeTree(this->root, 0);
  121. }