55#include " common.hpp"
66
77#include " ../3rdparty/portable-file-dialogs/portable-file-dialogs.h"
8+ #include < nbl/builtin/hlsl/math/thin_lens_projection.hlsl>
89
910#ifdef NBL_BUILD_MITSUBA_LOADER
1011#include " nbl/ext/MitsubaLoader/CSerializedLoader.h"
@@ -19,12 +20,18 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
1920 using device_base_t = MonoWindowApplication;
2021 using asset_base_t = BuiltinResourcesApplication;
2122
22- public:
23- inline MeshLoadersApp (const path& _localInputCWD, const path& _localOutputCWD, const path& _sharedInputCWD, const path& _sharedOutputCWD)
24- : IApplicationFramework(_localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD),
25- device_base_t({ 1280 ,720 }, EF_D32_SFLOAT, _localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD)
26- {
27- }
23+ enum DrawBoundingBoxMode
24+ {
25+ DBBM_NONE,
26+ DBBM_AABB,
27+ DBBM_OBB,
28+ DBBM_COUNT
29+ };
30+
31+ public:
32+ inline MeshLoadersApp (const path& _localInputCWD, const path& _localOutputCWD, const path& _sharedInputCWD, const path& _sharedOutputCWD)
33+ : IApplicationFramework(_localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD),
34+ device_base_t({1280 ,720 }, EF_D32_SFLOAT, _localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD) {}
2835
2936 inline bool onAppInitialized (smart_refctd_ptr<ISystem>&& system) override
3037 {
@@ -171,7 +178,9 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
171178 if (event.keyCode == E_KEY_CODE::EKC_R && event.action == SKeyboardEvent::ECA_RELEASED)
172179 reload = true ;
173180 if (event.keyCode == E_KEY_CODE::EKC_B && event.action == SKeyboardEvent::ECA_RELEASED)
174- m_drawBBs = !m_drawBBs;
181+ {
182+ m_drawBBMode = DrawBoundingBoxMode ((m_drawBBMode + 1 ) % DBBM_COUNT);
183+ }
175184 }
176185 camera.keyboardProcess (events);
177186 },
@@ -182,24 +191,17 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
182191 reloadModel ();
183192 }
184193 // draw scene
185- float32_t3x4 viewMatrix;
186- float32_t4x4 viewProjMatrix;
187- {
188- // TODO: get rid of legacy matrices
189- {
190- memcpy (&viewMatrix,camera.getViewMatrix ().pointer (),sizeof (viewMatrix));
191- memcpy (&viewProjMatrix,camera.getConcatenatedMatrix ().pointer (),sizeof (viewProjMatrix));
192- }
193- m_renderer->render (cb,CSimpleDebugRenderer::SViewParams (viewMatrix,viewProjMatrix));
194- }
194+ float32_t3x4 viewMatrix = camera.getViewMatrix ();
195+ float32_t4x4 viewProjMatrix = camera.getConcatenatedMatrix ();
196+ m_renderer->render (cb,CSimpleDebugRenderer::SViewParams (viewMatrix,viewProjMatrix));
195197#ifdef NBL_BUILD_DEBUG_DRAW
196- if (m_drawBBs )
198+ if (m_drawBBMode != DBBM_NONE )
197199 {
198200 const ISemaphore::SWaitInfo drawFinished = { .semaphore = m_semaphore.get (),.value = m_realFrameIx + 1u };
199201 ext::debug_draw::DrawAABB::DrawParameters drawParams;
200202 drawParams.commandBuffer = cb;
201203 drawParams.cameraMat = viewProjMatrix;
202- m_drawAABB->render (drawParams, drawFinished, m_aabbInstances);
204+ m_drawAABB->render (drawParams, drawFinished, m_drawBBMode == DBBM_OBB ? m_obbInstances : m_aabbInstances);
203205 }
204206#endif
205207 cb->endRenderPass ();
@@ -460,6 +462,7 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
460462 core::vector<hlsl::float32_t3x4> worldTforms;
461463 const auto & converted = reservation.getGPUObjects <ICPUPolygonGeometry>();
462464 m_aabbInstances.resize (converted.size ());
465+ m_obbInstances.resize (converted.size ());
463466 for (uint32_t i = 0 ; i < converted.size (); i++)
464467 {
465468 const auto & geom = converted[i];
@@ -472,18 +475,36 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
472475 bound = hlsl::shapes::util::union_ (transformed,bound);
473476
474477#ifdef NBL_BUILD_DEBUG_DRAW
475- auto & inst = m_aabbInstances[i];
478+
479+ auto & aabbInst = m_aabbInstances[i];
476480 const auto tmpAabb = shapes::AABB<3 ,float >(promoted.minVx , promoted.maxVx );
477- hlsl::float32_t3x4 instanceTransform = ext::debug_draw::DrawAABB::getTransformFromAABB (tmpAabb);
481+
482+ hlsl::float32_t3x4 aabbTransform = ext::debug_draw::DrawAABB::getTransformFromAABB (tmpAabb);
478483 const auto tmpWorld = hlsl::float32_t3x4 (promotedWorld);
479- inst.color = { 1 ,1 ,1 ,1 };
480- inst.transform [0 ] = tmpWorld[0 ];
481- inst.transform [1 ] = tmpWorld[1 ];
482- inst.transform [2 ] = tmpWorld[2 ];
483- inst.transform [3 ] = float32_t4 (0 , 0 , 0 , 1 );
484- inst.transform = math::linalg::promoted_mul (inst.transform , instanceTransform);
484+ const auto world4x4 = float32_t4x4{
485+ tmpWorld[0 ],
486+ tmpWorld[1 ],
487+ tmpWorld[2 ],
488+ float32_t4 (0 , 0 , 0 , 1 )
489+ };
490+
491+ aabbInst.color = { 1 ,1 ,1 ,1 };
492+ aabbInst.transform = math::linalg::promoted_mul (world4x4, aabbTransform);
493+
494+ auto & obbInst = m_obbInstances[i];
495+ const auto & cpuGeom = geometries[i].get ();
496+ const auto obb = CPolygonGeometryManipulator::calculateOBB (
497+ cpuGeom->getPositionView ().getElementCount (),
498+ [geo = cpuGeom, &world4x4](size_t vertex_i) {
499+ hlsl::float32_t3 pt;
500+ geo->getPositionView ().decodeElement (vertex_i, pt);
501+ return pt;
502+ });
503+ obbInst.color = { 0 , 0 , 1 , 1 };
504+ obbInst.transform = math::linalg::promoted_mul (world4x4, obb.transform );
485505#endif
486506 }
507+
487508 printAABB (bound," Total" );
488509 if (!m_renderer->addGeometries ({ &converted.front ().get (),converted.size () }))
489510 return false ;
@@ -503,7 +524,7 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
503524 {
504525 const auto measure = hlsl::length (diagonal);
505526 const auto aspectRatio = float (m_window->getWidth ()) / float (m_window->getHeight ());
506- camera.setProjectionMatrix (core::matrix4SIMD::buildProjectionMatrixPerspectiveFovRH (1 .2f , aspectRatio, distance * measure * 0.1 , measure * 4.0 ));
527+ camera.setProjectionMatrix (hlsl::math::thin_lens::rhPerspectiveFovMatrix< float > (1 .2f , aspectRatio, distance * measure * 0.1 , measure * 4.0 ));
507528 camera.setMoveSpeed (measure * 0.04 );
508529 }
509530 const auto pos = bound.maxVx + diagonal * distance;
@@ -539,14 +560,16 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
539560 InputSystem::ChannelReader<IMouseEventChannel> mouse;
540561 InputSystem::ChannelReader<IKeyboardEventChannel> keyboard;
541562 //
542- Camera camera = Camera(core::vectorSIMDf(0 , 0 , 0 ), core::vectorSIMDf(0 , 0 , 0 ), core::matrix4SIMD ());
563+ Camera camera = Camera(core::vectorSIMDf(0 , 0 , 0 ), core::vectorSIMDf(0 , 0 , 0 ), hlsl::float32_t4x4 ());
543564 // mutables
544565 std::string m_modelPath;
545566
546- bool m_drawBBs = true ;
567+ DrawBoundingBoxMode m_drawBBMode ;
547568#ifdef NBL_BUILD_DEBUG_DRAW
548569 smart_refctd_ptr<ext::debug_draw::DrawAABB> m_drawAABB;
549570 std::vector<ext::debug_draw::InstanceData> m_aabbInstances;
571+ std::vector<ext::debug_draw::InstanceData> m_obbInstances;
572+
550573#endif
551574
552575 bool m_saveGeom = false ;
0 commit comments