11
22#include " threepp/threepp.hpp"
33
4+ #include " threepp/objects/BVH.hpp"
5+
46#include < cmath>
7+ #include < iostream>
58
69using namespace threepp ;
710
@@ -55,9 +58,35 @@ int main() {
5558
5659 OrbitControls controls{*camera, canvas};
5760
58- auto curve = makeTubeMesh ();
59- const auto geometry = curve->geometry ();
60- scene->add (curve);
61+ auto tube = makeTubeMesh ();
62+ const auto geometry = tube->geometry ();
63+ scene->add (tube);
64+
65+ BVH bvh;
66+ bvh.build (*tube->geometry ());
67+
68+ std::vector<Box3> boxes;
69+ bvh.collectBoxes (boxes);
70+
71+ Group boxesGroup;
72+ boxesGroup.visible = false ; // Start with boxes hidden
73+ for (const auto & box : boxes) {
74+ auto helper = Box3Helper::create (box);
75+ box.getCenter (helper->position );
76+ boxesGroup.add (helper);
77+ }
78+ tube->add (boxesGroup);
79+
80+ KeyAdapter keyAdapter (KeyAdapter::Mode::KEY_PRESSED, [&](const KeyEvent& evt) {
81+ static bool show = boxesGroup.visible ;
82+ if (evt.key == Key::B) {
83+ show = !show;
84+ boxesGroup.visible = show;
85+ }
86+ });
87+ std::cout << " Press 'B' to toggle BVH visibility." << std::endl;
88+
89+ canvas.addKeyListener (keyAdapter);
6190
6291 canvas.onWindowResize ([&](WindowSize size) {
6392 camera->aspect = size.aspect ();
@@ -68,7 +97,7 @@ int main() {
6897 Clock clock;
6998 const auto count = static_cast <float >(geometry->getIndex ()->count ());
7099 canvas.animate ([&]() {
71- curve ->rotation .y += 1 * clock.getDelta ();
100+ tube ->rotation .y += 1 * clock.getDelta ();
72101
73102 const auto map = math::mapLinear (std::sin (clock.elapsedTime ), -1 , 1 , 0 , count);
74103 geometry->setDrawRange (0 , static_cast <int >(map));
0 commit comments