bvhoptimisation.cpp 3.9 KB

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