From 3596723d3d335447ddda0edfb121cc999cceb2dc Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Sat, 28 Sep 2024 13:24:28 +0800 Subject: [PATCH] chore: refactoring and cleanup for Gizmo --- .../native/include/APIBoundaryTypes.h | 2 +- .../native/include/CustomGeometry.hpp | 2 +- .../native/include/FilamentViewer.hpp | 4 +- thermion_dart/native/include/Gizmo.hpp | 67 ++++++++++--------- thermion_dart/native/include/GridOverlay.hpp | 2 +- .../native/include/ResourceBuffer.hpp | 2 +- thermion_dart/native/include/SceneManager.hpp | 18 +++-- .../native/include/StreamBufferAdapter.hpp | 2 +- thermion_dart/native/include/TGizmo.h | 8 ++- thermion_dart/native/include/TView.h | 1 + .../native/include/ThermionDartApi.h | 7 +- thermion_dart/native/include/ThreadPool.hpp | 2 +- .../native/include/UnprojectTexture.hpp | 2 +- .../components/AnimationComponentManager.hpp | 2 +- .../components/CollisionComponentManager.hpp | 2 +- .../include/material/FileMaterialProvider.hpp | 2 +- .../material/UnlitMaterialProvider.hpp | 4 +- thermion_dart/native/src/CustomGeometry.cpp | 2 +- thermion_dart/native/src/FilamentViewer.cpp | 11 ++- thermion_dart/native/src/Gizmo.cpp | 58 ++++++++-------- thermion_dart/native/src/GridOverlay.cpp | 4 +- thermion_dart/native/src/HighlightOverlay.cpp | 2 +- thermion_dart/native/src/SceneManager.cpp | 11 +-- .../native/src/StreamBufferAdapter.cpp | 2 +- thermion_dart/native/src/TGizmo.cpp | 53 +++++++++------ thermion_dart/native/src/TView.cpp | 5 ++ thermion_dart/native/src/ThermionDartApi.cpp | 22 +----- .../src/ThermionDartRenderThreadApi.cpp | 2 +- thermion_dart/native/src/UnprojectTexture.cpp | 4 +- 29 files changed, 155 insertions(+), 150 deletions(-) diff --git a/thermion_dart/native/include/APIBoundaryTypes.h b/thermion_dart/native/include/APIBoundaryTypes.h index a2a87b9a..2eac56fe 100644 --- a/thermion_dart/native/include/APIBoundaryTypes.h +++ b/thermion_dart/native/include/APIBoundaryTypes.h @@ -8,7 +8,6 @@ extern "C" #include typedef int32_t EntityId; - typedef int32_t _ManipulatorMode; typedef struct TCamera TCamera; typedef struct TMaterialInstance TMaterialInstance; typedef struct TEngine TEngine; @@ -19,6 +18,7 @@ extern "C" typedef struct TSwapChain TSwapChain; typedef struct TView TView; typedef struct TGizmo TGizmo; + typedef struct TScene TScene; struct TMaterialKey { bool doubleSided = 1; diff --git a/thermion_dart/native/include/CustomGeometry.hpp b/thermion_dart/native/include/CustomGeometry.hpp index 5ecc568d..a3348b43 100644 --- a/thermion_dart/native/include/CustomGeometry.hpp +++ b/thermion_dart/native/include/CustomGeometry.hpp @@ -11,7 +11,7 @@ #include #include -namespace thermion_filament +namespace thermion { using namespace filament; diff --git a/thermion_dart/native/include/FilamentViewer.hpp b/thermion_dart/native/include/FilamentViewer.hpp index 23ef388d..d998bffd 100644 --- a/thermion_dart/native/include/FilamentViewer.hpp +++ b/thermion_dart/native/include/FilamentViewer.hpp @@ -36,7 +36,7 @@ #include "SceneManager.hpp" #include "ThreadPool.hpp" -namespace thermion_filament +namespace thermion { typedef std::chrono::time_point time_point_t; @@ -139,7 +139,7 @@ namespace thermion_filament void* _context = nullptr; Scene *_scene = nullptr; Engine *_engine = nullptr; - thermion_filament::ThreadPool *_tp = nullptr; + thermion::ThreadPool *_tp = nullptr; Renderer *_renderer = nullptr; SceneManager *_sceneManager = nullptr; std::vector _renderTargets; diff --git a/thermion_dart/native/include/Gizmo.hpp b/thermion_dart/native/include/Gizmo.hpp index 9f244cc4..cc8e0dde 100644 --- a/thermion_dart/native/include/Gizmo.hpp +++ b/thermion_dart/native/include/Gizmo.hpp @@ -21,7 +21,7 @@ #include "ThermionDartApi.h" -namespace thermion_filament { +namespace thermion { using namespace filament; using namespace utils; @@ -29,38 +29,13 @@ using namespace utils; class Gizmo { enum Axis { X, Y, Z}; - - class PickCallbackHandler { - public: - PickCallbackHandler(Gizmo* gizmo, void (*callback)(EntityId entityId, int x, int y)) : _gizmo(gizmo), _callback(callback) {}; - void handle(filament::View::PickingQueryResult const &result) { - auto x = static_cast(result.fragCoords.x); - auto y= static_cast(result.fragCoords.y); - for(int i = 0; i < 7; i++) { - if(_gizmo->_entities[i] == result.renderable) { - if(i < 4) { - return; - } - _gizmo->highlight(_gizmo->_entities[i - 4]); - _callback(Entity::smuggle(_gizmo->_entities[i - 4]), x, y); - return; - } - } - _gizmo->unhighlight(); - _callback(0, x, y); - delete(this); - } - - private: - Gizmo* _gizmo; - void (*_callback)(EntityId entityId, int x, int y); - - }; - + public: - Gizmo(Engine& engine, Scene *scene); + Gizmo(Engine *engine, View *view, Scene *scene); ~Gizmo(); + typedef void (*PickCallback)(EntityId entityId, uint32_t x, uint32_t y, View *view); + Entity x() { return _entities[0]; }; @@ -78,16 +53,44 @@ class Gizmo { return _isActive; } - void pick(View* view, uint32_t x, uint32_t y, void (*callback)(EntityId entityId, int x, int y)); + void pick(uint32_t x, uint32_t y, PickCallback callback); bool isGizmoEntity(Entity entity); void setVisibility(bool visible); private: + + class PickCallbackHandler { + public: + PickCallbackHandler(Gizmo* gizmo, PickCallback callback) : _gizmo(gizmo), _callback(callback) {}; + void handle(filament::View::PickingQueryResult const &result) { + auto x = static_cast(result.fragCoords.x); + auto y= static_cast(result.fragCoords.y); + for(int i = 0; i < 7; i++) { + if(_gizmo->_entities[i] == result.renderable) { + if(i < 4) { + return; + } + _gizmo->highlight(_gizmo->_entities[i - 4]); + _callback(Entity::smuggle(_gizmo->_entities[i - 4]), x, y, _gizmo->_view); + return; + } + } + _gizmo->unhighlight(); + _callback(0, x, y, _gizmo->_view); + delete(this); + } + + private: + Gizmo* _gizmo; + PickCallback _callback; + + }; void createTransparentRectangles(); void highlight(Entity entity); void unhighlight(); - Engine &_engine; + Engine *_engine; Scene *_scene; + View *_view; utils::Entity _entities[7] = { utils::Entity(), utils::Entity(), utils::Entity(), utils::Entity(), utils::Entity(), utils::Entity(), utils::Entity() }; Material* _material; MaterialInstance* _materialInstances[7]; diff --git a/thermion_dart/native/include/GridOverlay.hpp b/thermion_dart/native/include/GridOverlay.hpp index f4009bcd..09832200 100644 --- a/thermion_dart/native/include/GridOverlay.hpp +++ b/thermion_dart/native/include/GridOverlay.hpp @@ -22,7 +22,7 @@ #include "material/gizmo.h" -namespace thermion_filament { +namespace thermion { using namespace filament; using namespace utils; diff --git a/thermion_dart/native/include/ResourceBuffer.hpp b/thermion_dart/native/include/ResourceBuffer.hpp index 94f64c05..0a1d9c57 100644 --- a/thermion_dart/native/include/ResourceBuffer.hpp +++ b/thermion_dart/native/include/ResourceBuffer.hpp @@ -8,7 +8,7 @@ using namespace std::chrono_literals; #endif -namespace thermion_filament +namespace thermion { struct ResourceLoaderWrapperImpl : public ResourceLoaderWrapper diff --git a/thermion_dart/native/include/SceneManager.hpp b/thermion_dart/native/include/SceneManager.hpp index 1dce4c3a..08ff82ad 100644 --- a/thermion_dart/native/include/SceneManager.hpp +++ b/thermion_dart/native/include/SceneManager.hpp @@ -19,10 +19,8 @@ #include #include -#include "material/gizmo.h" - #include "CustomGeometry.hpp" -#include "Gizmo.hpp" + #include "APIBoundaryTypes.h" #include "GridOverlay.hpp" #include "ResourceBuffer.hpp" @@ -32,7 +30,7 @@ #include "tsl/robin_map.h" -namespace thermion_filament +namespace thermion { typedef int32_t EntityId; @@ -253,8 +251,6 @@ namespace thermion_filament friend class FilamentViewer; - Gizmo* gizmo = nullptr; - gltfio::MaterialProvider * const unlitMaterialProvider() { return _unlitMaterialProvider; } @@ -267,10 +263,6 @@ namespace thermion_filament return _geometry[entityId].get(); } - Scene* const getScene() { - return _scene; - } - bool isGltfAsset(EntityId entity) { return getAssetByEntityId(entity) != nullptr; } @@ -307,6 +299,12 @@ namespace thermion_filament Camera* getCameraAt(size_t index); + bool isGizmoEntity(utils::Entity entity); + + Scene* getScene() { + return _scene; + } + private: gltfio::AssetLoader *_assetLoader = nullptr; const ResourceLoaderWrapperImpl *const _resourceLoaderWrapper; diff --git a/thermion_dart/native/include/StreamBufferAdapter.hpp b/thermion_dart/native/include/StreamBufferAdapter.hpp index e76a9fda..abf8e3e0 100644 --- a/thermion_dart/native/include/StreamBufferAdapter.hpp +++ b/thermion_dart/native/include/StreamBufferAdapter.hpp @@ -5,7 +5,7 @@ #include #include -namespace thermion_filament { +namespace thermion { // diff --git a/thermion_dart/native/include/TGizmo.h b/thermion_dart/native/include/TGizmo.h index c7cac630..df42119d 100644 --- a/thermion_dart/native/include/TGizmo.h +++ b/thermion_dart/native/include/TGizmo.h @@ -6,10 +6,14 @@ extern "C" #endif #include "ThermionDartApi.h" +#include "TGizmo.h" -EMSCRIPTEN_KEEPALIVE void Gizmo_pick(TGizmo *tGizmo, TView *tView, int x, int y, void (*callback)(EntityId entityId, int x, int y)); +typedef void (*GizmoPickCallback)(EntityId entityId, uint32_t x, uint32_t y, TView* view); + +EMSCRIPTEN_KEEPALIVE TGizmo* Gizmo_new(TEngine *tEngine, TView *tView, TScene *tScene); +EMSCRIPTEN_KEEPALIVE void Gizmo_pick(TGizmo *tGizmo, uint32_t x, uint32_t y, GizmoPickCallback callback); +EMSCRIPTEN_KEEPALIVE void Gizmo_setVisibility(TGizmo *tGizmo, bool visible); #ifdef __cplusplus } #endif -#endif diff --git a/thermion_dart/native/include/TView.h b/thermion_dart/native/include/TView.h index 17c9a823..563d6fce 100644 --- a/thermion_dart/native/include/TView.h +++ b/thermion_dart/native/include/TView.h @@ -40,6 +40,7 @@ EMSCRIPTEN_KEEPALIVE void View_setToneMapping(TView* tView, TEngine* tEngine, To EMSCRIPTEN_KEEPALIVE void View_setAntiAliasing(TView *tView, bool msaa, bool fxaa, bool taa); EMSCRIPTEN_KEEPALIVE void View_setLayerEnabled(TView *tView, int layer, bool visible); EMSCRIPTEN_KEEPALIVE void View_setCamera(TView *tView, TCamera *tCamera); +EMSCRIPTEN_KEEPALIVE TScene* View_getScene(TView *tView); EMSCRIPTEN_KEEPALIVE TCamera* View_getCamera(TView *tView); #ifdef __cplusplus diff --git a/thermion_dart/native/include/ThermionDartApi.h b/thermion_dart/native/include/ThermionDartApi.h index 5c3ad0bb..45296c15 100644 --- a/thermion_dart/native/include/ThermionDartApi.h +++ b/thermion_dart/native/include/ThermionDartApi.h @@ -211,7 +211,8 @@ extern "C" EMSCRIPTEN_KEEPALIVE void SceneManager_queueTransformUpdates(TSceneManager *sceneManager, EntityId* entities, const double* const transforms, int numEntities); EMSCRIPTEN_KEEPALIVE TCamera* SceneManager_findCameraByName(TSceneManager* tSceneManager, EntityId entity, const char* name); EMSCRIPTEN_KEEPALIVE void SceneManager_setVisibilityLayer(TSceneManager *tSceneManager, EntityId entity, int layer); - EMSCRIPTEN_KEEPALIVE TGizmo* SceneManager_getGizmo(TSceneManager *tSceneManager); + EMSCRIPTEN_KEEPALIVE TScene* SceneManager_getScene(TSceneManager *tSceneManager); + EMSCRIPTEN_KEEPALIVE bool update_bone_matrices(TSceneManager *sceneManager, EntityId entityId); EMSCRIPTEN_KEEPALIVE void get_morph_target_name(TSceneManager *sceneManager, EntityId assetEntity, EntityId childEntity, char *const outPtr, int index); @@ -302,11 +303,11 @@ extern "C" EMSCRIPTEN_KEEPALIVE void set_parent(TSceneManager *sceneManager, EntityId child, EntityId parent, bool preserveScaling); EMSCRIPTEN_KEEPALIVE void test_collisions(TSceneManager *sceneManager, EntityId entity); EMSCRIPTEN_KEEPALIVE void set_priority(TSceneManager *sceneManager, EntityId entityId, int priority); - EMSCRIPTEN_KEEPALIVE void get_gizmo(TSceneManager *sceneManager, EntityId *out); + EMSCRIPTEN_KEEPALIVE Aabb2 get_bounding_box(TSceneManager *sceneManager, TView *view, EntityId entity); EMSCRIPTEN_KEEPALIVE void get_bounding_box_to_out(TSceneManager *sceneManager, TView *view, EntityId entity, float *minX, float *minY, float *maxX, float *maxY); - EMSCRIPTEN_KEEPALIVE void set_gizmo_visibility(TSceneManager *sceneManager, bool visible); + EMSCRIPTEN_KEEPALIVE void set_stencil_highlight(TSceneManager *sceneManager, EntityId entity, float r, float g, float b); EMSCRIPTEN_KEEPALIVE void remove_stencil_highlight(TSceneManager *sceneManager, EntityId entity); EMSCRIPTEN_KEEPALIVE void set_material_property_float(TSceneManager *sceneManager, EntityId entity, int materialIndex, const char *property, float value); diff --git a/thermion_dart/native/include/ThreadPool.hpp b/thermion_dart/native/include/ThreadPool.hpp index 1f1e27fb..eb4bc07b 100644 --- a/thermion_dart/native/include/ThreadPool.hpp +++ b/thermion_dart/native/include/ThreadPool.hpp @@ -15,7 +15,7 @@ #ifndef _THREADPOOL_HPP #define _THREADPOOL_HPP -namespace thermion_filament { +namespace thermion { class ThreadPool { std::vector pool; diff --git a/thermion_dart/native/include/UnprojectTexture.hpp b/thermion_dart/native/include/UnprojectTexture.hpp index 0f04dba0..86bcecdf 100644 --- a/thermion_dart/native/include/UnprojectTexture.hpp +++ b/thermion_dart/native/include/UnprojectTexture.hpp @@ -17,7 +17,7 @@ #include "CustomGeometry.hpp" -namespace thermion_filament { +namespace thermion { class UnprojectTexture { public: diff --git a/thermion_dart/native/include/components/AnimationComponentManager.hpp b/thermion_dart/native/include/components/AnimationComponentManager.hpp index 40b18219..7153c6ae 100644 --- a/thermion_dart/native/include/components/AnimationComponentManager.hpp +++ b/thermion_dart/native/include/components/AnimationComponentManager.hpp @@ -24,7 +24,7 @@ #include template class std::vector; -namespace thermion_filament +namespace thermion { using namespace filament; using namespace filament::gltfio; diff --git a/thermion_dart/native/include/components/CollisionComponentManager.hpp b/thermion_dart/native/include/components/CollisionComponentManager.hpp index b7e62ab1..279f6207 100644 --- a/thermion_dart/native/include/components/CollisionComponentManager.hpp +++ b/thermion_dart/native/include/components/CollisionComponentManager.hpp @@ -8,7 +8,7 @@ #include "gltfio/FilamentInstance.h" #include "Log.hpp" -namespace thermion_filament +namespace thermion { typedef void(*CollisionCallback)(int32_t entityId1, int32_t entityId2) ; diff --git a/thermion_dart/native/include/material/FileMaterialProvider.hpp b/thermion_dart/native/include/material/FileMaterialProvider.hpp index 00b76859..519c5fc9 100644 --- a/thermion_dart/native/include/material/FileMaterialProvider.hpp +++ b/thermion_dart/native/include/material/FileMaterialProvider.hpp @@ -11,7 +11,7 @@ #include #include "Log.hpp" -namespace thermion_filament { +namespace thermion { class FileMaterialProvider : public filament::gltfio::MaterialProvider { diff --git a/thermion_dart/native/include/material/UnlitMaterialProvider.hpp b/thermion_dart/native/include/material/UnlitMaterialProvider.hpp index 524d2841..81f86dd1 100644 --- a/thermion_dart/native/include/material/UnlitMaterialProvider.hpp +++ b/thermion_dart/native/include/material/UnlitMaterialProvider.hpp @@ -10,7 +10,7 @@ #include #include -namespace thermion_filament { +namespace thermion { class UnlitMaterialProvider : public filament::gltfio::MaterialProvider { private: @@ -63,6 +63,6 @@ public: } }; -} // namespace thermion_filament +} // namespace thermion #endif // UNLIT_MATERIAL_PROVIDER_HPP \ No newline at end of file diff --git a/thermion_dart/native/src/CustomGeometry.cpp b/thermion_dart/native/src/CustomGeometry.cpp index 0736bdaf..5689508f 100644 --- a/thermion_dart/native/src/CustomGeometry.cpp +++ b/thermion_dart/native/src/CustomGeometry.cpp @@ -11,7 +11,7 @@ #include "Log.hpp" #include "CustomGeometry.hpp" -namespace thermion_filament { +namespace thermion { using namespace filament; diff --git a/thermion_dart/native/src/FilamentViewer.cpp b/thermion_dart/native/src/FilamentViewer.cpp index 65e2ef5d..bba4145a 100644 --- a/thermion_dart/native/src/FilamentViewer.cpp +++ b/thermion_dart/native/src/FilamentViewer.cpp @@ -105,7 +105,7 @@ namespace filament class LightManager; } // namespace filament -namespace thermion_filament +namespace thermion { using namespace filament; @@ -349,7 +349,7 @@ namespace thermion_filament void FilamentViewer::loadPngTexture(string path, ResourceBuffer rb) { - thermion_filament::StreamBufferAdapter sb((char *)rb.data, (char *)rb.data + rb.size); + thermion::StreamBufferAdapter sb((char *)rb.data, (char *)rb.data + rb.size); std::istream inputStream(&sb); @@ -1209,10 +1209,9 @@ namespace thermion_filament void FilamentViewer::pick(View *view, uint32_t x, uint32_t y, void (*callback)(EntityId entityId, int x, int y)) { - view->pick(x, y, [=](filament::View::PickingQueryResult const &result) - { + view->pick(x, y, [=](filament::View::PickingQueryResult const &result) { - if(_sceneManager->gizmo->isGizmoEntity(result.renderable)) { + if(_sceneManager->isGizmoEntity(result.renderable)) { Log("Gizmo entity, ignoring"); return; } @@ -1243,4 +1242,4 @@ namespace thermion_filament // unproject.unproject(utils::Entity::import(entityId), input, out, inputWidth, inputHeight, outWidth, outHeight); } -} // namespace thermion_filament +} // namespace thermion diff --git a/thermion_dart/native/src/Gizmo.cpp b/thermion_dart/native/src/Gizmo.cpp index d136498a..df92c332 100644 --- a/thermion_dart/native/src/Gizmo.cpp +++ b/thermion_dart/native/src/Gizmo.cpp @@ -10,23 +10,21 @@ #include "material/gizmo.h" #include "Log.hpp" -namespace thermion_filament { +namespace thermion { using namespace filament::gltfio; -Gizmo::Gizmo(Engine &engine, Scene* scene) : _engine(engine) +Gizmo::Gizmo(Engine *engine, View *view, Scene* scene) : _engine(engine), _view(view), _scene(scene) { - - _scene = scene; - + auto &entityManager = EntityManager::get(); - auto &transformManager = engine.getTransformManager(); + auto &transformManager = _engine->getTransformManager(); _material = Material::Builder() .package(GIZMO_GIZMO_DATA, GIZMO_GIZMO_SIZE) - .build(engine); + .build(*_engine); // First, create the black cube at the center // The axes widgets will be parented to this entity @@ -60,13 +58,13 @@ Gizmo::Gizmo(Engine &engine, Scene* scene) : _engine(engine) .vertexCount(8) .bufferCount(1) .attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3) - .build(engine); + .build(*engine); - centerCubeVb->setBufferAt(engine, 0, VertexBuffer::BufferDescriptor(centerCubeVertices, 8 * sizeof(filament::math::float3), [](void *buffer, size_t size, void *) + centerCubeVb->setBufferAt(*engine, 0, VertexBuffer::BufferDescriptor(centerCubeVertices, 8 * sizeof(filament::math::float3), [](void *buffer, size_t size, void *) { delete[] static_cast(buffer); })); - auto centerCubeIb = IndexBuffer::Builder().indexCount(36).bufferType(IndexBuffer::IndexType::USHORT).build(engine); - centerCubeIb->setBuffer(engine, IndexBuffer::BufferDescriptor( + auto centerCubeIb = IndexBuffer::Builder().indexCount(36).bufferType(IndexBuffer::IndexType::USHORT).build(*engine); + centerCubeIb->setBuffer(*engine, IndexBuffer::BufferDescriptor( centerCubeIndices, 36 * sizeof(uint16_t), [](void *buffer, size_t size, void *) { delete[] static_cast(buffer); })); @@ -79,7 +77,7 @@ Gizmo::Gizmo(Engine &engine, Scene* scene) : _engine(engine) .priority(7) .geometry(0, RenderableManager::PrimitiveType::TRIANGLES, centerCubeVb, centerCubeIb, 0, 36) .culling(false) - .build(engine, _entities[3]); + .build(*engine, _entities[3]); auto cubeTransformInstance = transformManager.getInstance(_entities[3]); math::mat4f cubeTransform; @@ -126,13 +124,13 @@ Gizmo::Gizmo(Engine &engine, Scene* scene) : _engine(engine) .vertexCount(13) .bufferCount(1) .attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3) - .build(engine); + .build(*engine); - vb->setBufferAt(engine, 0, VertexBuffer::BufferDescriptor(vertices, 13 * sizeof(filament::math::float3), [](void *buffer, size_t size, void *) + vb->setBufferAt(*engine, 0, VertexBuffer::BufferDescriptor(vertices, 13 * sizeof(filament::math::float3), [](void *buffer, size_t size, void *) { delete[] static_cast(buffer); })); - auto ib = IndexBuffer::Builder().indexCount(54).bufferType(IndexBuffer::IndexType::USHORT).build(engine); - ib->setBuffer(engine, IndexBuffer::BufferDescriptor( + auto ib = IndexBuffer::Builder().indexCount(54).bufferType(IndexBuffer::IndexType::USHORT).build(*engine); + ib->setBuffer(*engine, IndexBuffer::BufferDescriptor( indices, 54 * sizeof(uint16_t), [](void *buffer, size_t size, void *) { delete[] static_cast(buffer); })); @@ -174,7 +172,7 @@ Gizmo::Gizmo(Engine &engine, Scene* scene) : _engine(engine) .culling(false) .receiveShadows(false) .castShadows(false) - .build(engine, _entities[i]); + .build(*engine, _entities[i]); auto instance = transformManager.getInstance(_entities[i]); @@ -192,21 +190,21 @@ Gizmo::~Gizmo() { _scene->removeEntities(_entities, 7); for(int i = 0; i < 7; i++) { - _engine.destroy(_entities[i]); + _engine->destroy(_entities[i]); } for(int i = 0; i < 7; i++) { - _engine.destroy(_materialInstances[i]); + _engine->destroy(_materialInstances[i]); } - _engine.destroy(_material); + _engine->destroy(_material); } void Gizmo::createTransparentRectangles() { auto &entityManager = EntityManager::get(); - auto &transformManager = _engine.getTransformManager(); + auto &transformManager = _engine->getTransformManager(); float volumeWidth = 0.2f; float volumeLength = 1.2f; @@ -236,9 +234,9 @@ void Gizmo::createTransparentRectangles() .vertexCount(8) .bufferCount(1) .attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3) - .build(_engine); + .build(*_engine); - volumeVb->setBufferAt(_engine, 0, VertexBuffer::BufferDescriptor( + volumeVb->setBufferAt(*_engine, 0, VertexBuffer::BufferDescriptor( volumeVertices, 8 * sizeof(filament::math::float3), [](void *buffer, size_t size, void *) { delete[] static_cast(buffer); } )); @@ -246,9 +244,9 @@ void Gizmo::createTransparentRectangles() auto volumeIb = IndexBuffer::Builder() .indexCount(36) .bufferType(IndexBuffer::IndexType::USHORT) - .build(_engine); + .build(*_engine); - volumeIb->setBuffer(_engine, IndexBuffer::BufferDescriptor( + volumeIb->setBuffer(*_engine, IndexBuffer::BufferDescriptor( volumeIndices, 36 * sizeof(uint16_t), [](void *buffer, size_t size, void *) { delete[] static_cast(buffer); } )); @@ -282,7 +280,7 @@ void Gizmo::createTransparentRectangles() .culling(false) .receiveShadows(false) .castShadows(false) - .build(_engine, _entities[i]); + .build(*_engine, _entities[i]); auto instance = transformManager.getInstance(_entities[i]); transformManager.setTransform(instance, transform); @@ -293,7 +291,7 @@ void Gizmo::createTransparentRectangles() } void Gizmo::highlight(Entity entity) { - auto &rm = _engine.getRenderableManager(); + auto &rm = _engine->getRenderableManager(); auto renderableInstance = rm.getInstance(entity); auto materialInstance = rm.getMaterialInstanceAt(renderableInstance, 0); @@ -313,7 +311,7 @@ void Gizmo::highlight(Entity entity) { } void Gizmo::unhighlight() { - auto &rm = _engine.getRenderableManager(); + auto &rm = _engine->getRenderableManager(); for(int i = 0; i < 3; i++) { auto renderableInstance = rm.getInstance(_entities[i]); @@ -324,10 +322,10 @@ void Gizmo::unhighlight() { } } -void Gizmo::pick(View *view, uint32_t x, uint32_t y, void (*callback)(EntityId entityId, int x, int y)) +void Gizmo::pick(uint32_t x, uint32_t y, PickCallback callback) { auto handler = new Gizmo::PickCallbackHandler(this, callback); - view->pick(x, y, [=](filament::View::PickingQueryResult const &result) { + _view->pick(x, y, [=](filament::View::PickingQueryResult const &result) { handler->handle(result); }); } diff --git a/thermion_dart/native/src/GridOverlay.cpp b/thermion_dart/native/src/GridOverlay.cpp index 6d3a59a6..05fdf616 100644 --- a/thermion_dart/native/src/GridOverlay.cpp +++ b/thermion_dart/native/src/GridOverlay.cpp @@ -11,7 +11,7 @@ #include "SceneManager.hpp" #include "Log.hpp" -namespace thermion_filament { +namespace thermion { using namespace filament::gltfio; @@ -194,4 +194,4 @@ void GridOverlay::destroy() _engine.destroy(_gridEntity); } -} // namespace thermion_filament \ No newline at end of file +} // namespace thermion \ No newline at end of file diff --git a/thermion_dart/native/src/HighlightOverlay.cpp b/thermion_dart/native/src/HighlightOverlay.cpp index 0b0f0f0d..95c6498f 100644 --- a/thermion_dart/native/src/HighlightOverlay.cpp +++ b/thermion_dart/native/src/HighlightOverlay.cpp @@ -4,7 +4,7 @@ #include "SceneManager.hpp" -namespace thermion_filament +namespace thermion { SceneManager::HighlightOverlay::HighlightOverlay( diff --git a/thermion_dart/native/src/SceneManager.cpp b/thermion_dart/native/src/SceneManager.cpp index bd50a89a..9ee00394 100644 --- a/thermion_dart/native/src/SceneManager.cpp +++ b/thermion_dart/native/src/SceneManager.cpp @@ -38,7 +38,7 @@ extern "C" #include "material/image.h" } -namespace thermion_filament +namespace thermion { using namespace std::chrono; @@ -98,7 +98,6 @@ namespace thermion_filament _collisionComponentManager = new CollisionComponentManager(tm); _animationComponentManager = new AnimationComponentManager(tm, _engine->getRenderableManager()); - gizmo = new Gizmo(*_engine, _scene); _gridOverlay = new GridOverlay(*_engine); _scene->addEntity(_gridOverlay->sphere()); @@ -114,7 +113,7 @@ namespace thermion_filament _engine->getEntityManager().destroy(entity); } _cameras.clear(); - delete gizmo; + _gridOverlay->destroy(); destroyAll(); @@ -132,6 +131,10 @@ namespace thermion_filament AssetLoader::destroy(&_assetLoader); } + bool SceneManager::isGizmoEntity(Entity entity) { + return false; // TODO + } + int SceneManager::getInstanceCount(EntityId entityId) { auto *asset = getAssetByEntityId(entityId); @@ -2502,4 +2505,4 @@ EntityId SceneManager::createGeometry( } -} // namespace thermion_filament +} // namespace thermion diff --git a/thermion_dart/native/src/StreamBufferAdapter.cpp b/thermion_dart/native/src/StreamBufferAdapter.cpp index 8d51faaa..c3d54b9c 100644 --- a/thermion_dart/native/src/StreamBufferAdapter.cpp +++ b/thermion_dart/native/src/StreamBufferAdapter.cpp @@ -3,7 +3,7 @@ #include #include -namespace thermion_filament { +namespace thermion { class StreamBufferAdapter : public std::streambuf { diff --git a/thermion_dart/native/src/TGizmo.cpp b/thermion_dart/native/src/TGizmo.cpp index 8142caf5..d1c080b6 100644 --- a/thermion_dart/native/src/TGizmo.cpp +++ b/thermion_dart/native/src/TGizmo.cpp @@ -1,29 +1,40 @@ -#ifdef _WIN32 -#pragma comment(lib, "Shlwapi.lib") -#pragma comment(lib, "opengl32.lib") -#endif +#include +#include +#include -#include "ResourceBuffer.hpp" -#include "FilamentViewer.hpp" -#include "filament/LightManager.h" +#include "ThermionDartApi.h" +#include "TGizmo.h" +#include "Gizmo.hpp" #include "Log.hpp" -#include "ThreadPool.hpp" - -#include -#include - -#ifdef __EMSCRIPTEN__ -#include -#endif - -using namespace thermion_filament; +#ifdef __cplusplus +namespace thermion { extern "C" { - EMSCRIPTEN_KEEPALIVE void Gizmo_pick(TGizmo *tGizmo, TView *tView, int x, int y, void (*callback)(EntityId entityId, int x, int y)) +using namespace filament; +#endif + + EMSCRIPTEN_KEEPALIVE TGizmo* Gizmo_new(TEngine *tEngine, TView *tView, TScene *tScene) + { + auto *view = reinterpret_cast(tView); + auto *engine = reinterpret_cast(tEngine); + auto *scene = reinterpret_cast(tScene); + auto gizmo = new Gizmo(engine, view, scene); + return reinterpret_cast(gizmo); + } + + EMSCRIPTEN_KEEPALIVE void Gizmo_pick(TGizmo *tGizmo, uint32_t x, uint32_t y, GizmoPickCallback callback) { auto *gizmo = reinterpret_cast(tGizmo); - auto *view = reinterpret_cast(tView); - gizmo->pick(view, x, y, callback); + gizmo->pick(x, y, reinterpret_cast(callback)); } -} \ No newline at end of file + + EMSCRIPTEN_KEEPALIVE void Gizmo_setVisibility(TGizmo *tGizmo, bool visible) { + auto *gizmo = reinterpret_cast(tGizmo); + gizmo->setVisibility(visible); + } + +#ifdef __cplusplus +} +} +#endif diff --git a/thermion_dart/native/src/TView.cpp b/thermion_dart/native/src/TView.cpp index eb200403..acd4f116 100644 --- a/thermion_dart/native/src/TView.cpp +++ b/thermion_dart/native/src/TView.cpp @@ -145,6 +145,11 @@ using namespace filament; view->setCamera(camera); } + EMSCRIPTEN_KEEPALIVE TScene* View_getScene(TView* tView) { + auto view = reinterpret_cast(tView); + return reinterpret_cast(view->getScene()); + } + #ifdef __cplusplus } diff --git a/thermion_dart/native/src/ThermionDartApi.cpp b/thermion_dart/native/src/ThermionDartApi.cpp index 716d3078..5ab4f09a 100644 --- a/thermion_dart/native/src/ThermionDartApi.cpp +++ b/thermion_dart/native/src/ThermionDartApi.cpp @@ -16,7 +16,7 @@ #include #endif -using namespace thermion_filament; +using namespace thermion; extern "C" { @@ -681,11 +681,6 @@ extern "C" return nullptr; } - EMSCRIPTEN_KEEPALIVE TGizmo* SceneManager_getGizmo(TSceneManager *tSceneManager) { - auto *sceneManager = reinterpret_cast(tSceneManager); - auto *gizmo = sceneManager->gizmo; - return reinterpret_cast(gizmo); - } EMSCRIPTEN_KEEPALIVE bool SceneManager_setTransform(TSceneManager *sceneManager, EntityId entityId, const double *const transform) { @@ -911,15 +906,7 @@ extern "C" ((SceneManager *)sceneManager)->setPriority(entity, priority); } - EMSCRIPTEN_KEEPALIVE void get_gizmo(TSceneManager *sceneManager, EntityId *out) - { - auto gizmo = ((SceneManager *)sceneManager)->gizmo; - out[0] = Entity::smuggle(gizmo->x()); - out[1] = Entity::smuggle(gizmo->y()); - out[2] = Entity::smuggle(gizmo->z()); - out[3] = Entity::smuggle(gizmo->center()); - } - + EMSCRIPTEN_KEEPALIVE Aabb2 get_bounding_box(TSceneManager *sceneManager, TView *tView, EntityId entity) { auto view = reinterpret_cast(tView); @@ -947,11 +934,6 @@ extern "C" free(ptr); } - EMSCRIPTEN_KEEPALIVE void set_gizmo_visibility(TSceneManager *sceneManager, bool visible) - { - ((SceneManager *)sceneManager)->gizmo->setVisibility(visible); - } - EMSCRIPTEN_KEEPALIVE void set_stencil_highlight(TSceneManager *sceneManager, EntityId entityId, float r, float g, float b) { ((SceneManager *)sceneManager)->setStencilHighlight(entityId, r, g, b); diff --git a/thermion_dart/native/src/ThermionDartRenderThreadApi.cpp b/thermion_dart/native/src/ThermionDartRenderThreadApi.cpp index aca06e59..03c4272f 100644 --- a/thermion_dart/native/src/ThermionDartRenderThreadApi.cpp +++ b/thermion_dart/native/src/ThermionDartRenderThreadApi.cpp @@ -26,7 +26,7 @@ extern "C" #include #include -using namespace thermion_filament; +using namespace thermion; using namespace std::chrono_literals; #include diff --git a/thermion_dart/native/src/UnprojectTexture.cpp b/thermion_dart/native/src/UnprojectTexture.cpp index 0f576c70..8af0e664 100644 --- a/thermion_dart/native/src/UnprojectTexture.cpp +++ b/thermion_dart/native/src/UnprojectTexture.cpp @@ -20,7 +20,7 @@ #include "CustomGeometry.hpp" #include "UnprojectTexture.hpp" -namespace thermion_filament +namespace thermion { bool UnprojectTexture::isInsideTriangle(const math::float2 &p, const math::float2 &a, const math::float2 &b, const math::float2 &c) @@ -207,5 +207,5 @@ namespace thermion_filament } } -} // namespace thermion_filament +} // namespace thermion