From d78f70ac504af196efd1f1e9834b6369dcbca5bc Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 25 Aug 2022 19:01:04 +1000 Subject: [PATCH] setRotation/setPosition from center --- ios/src/FilamentViewer.cpp | 10 ++++++++++ ios/src/FilamentViewer.hpp | 3 +++ ios/src/PolyvoxFilamentApi.cpp | 8 ++++++++ ios/src/PolyvoxFilamentApi.hpp | 4 ++++ ios/src/SceneAsset.cpp | 23 ++++++++++++++++++----- ios/src/SceneAsset.hpp | 9 ++++++++- 6 files changed, 51 insertions(+), 6 deletions(-) diff --git a/ios/src/FilamentViewer.cpp b/ios/src/FilamentViewer.cpp index a846295a..862c2b0b 100644 --- a/ios/src/FilamentViewer.cpp +++ b/ios/src/FilamentViewer.cpp @@ -597,4 +597,14 @@ void FilamentViewer::updateViewportAndCameraProjection( contentScaleFactor); } +void FilamentViewer::setCameraPosition(float x, float y, float z) { + Camera& cam =_view->getCamera(); + auto &tm = _engine->getTransformManager(); + // tm.setTransform(tm.getInstance(_asset->getRoot()), math::mat4f::translation(math::float3(x,y,z)) * _cameraRotation); +} + +void FilamentViewer::setCameraRotation(float rads, float x, float y, float z) { + Camera& cam =_view->getCamera(); +} + } // namespace polyvox diff --git a/ios/src/FilamentViewer.hpp b/ios/src/FilamentViewer.hpp index 6b726047..1fb2c3bf 100644 --- a/ios/src/FilamentViewer.hpp +++ b/ios/src/FilamentViewer.hpp @@ -74,6 +74,9 @@ namespace polyvox { void setBackgroundImage(const char* resourcePath); + void setCameraPosition(float x, float y, float z); + void setCameraRotation(float rads, float x, float y, float z); + private: void createImageRenderable(); void loadResources(std::string relativeResourcePath); diff --git a/ios/src/PolyvoxFilamentApi.cpp b/ios/src/PolyvoxFilamentApi.cpp index 0b4ca5f9..12aa21a9 100644 --- a/ios/src/PolyvoxFilamentApi.cpp +++ b/ios/src/PolyvoxFilamentApi.cpp @@ -56,6 +56,14 @@ extern "C" { return ((FilamentViewer*)viewer)->setCamera((SceneAsset*)asset, nodeName); } + void set_camera_position(void* viewer, float x, float y, float z) { + return ((FilamentViewer*)viewer)->setCameraPosition(x, y, z); + } + + void set_camera_rotation(void* viewer, float rads, float x, float y, float z) { + return ((FilamentViewer*)viewer)->setCameraRotation(rads, x, y, z); + } + void render( void* viewer ) { diff --git a/ios/src/PolyvoxFilamentApi.hpp b/ios/src/PolyvoxFilamentApi.hpp index a5710f29..37db0cde 100644 --- a/ios/src/PolyvoxFilamentApi.hpp +++ b/ios/src/PolyvoxFilamentApi.hpp @@ -56,5 +56,9 @@ void set_position(void* asset, float x, float y, float z); void set_rotation(void* asset, float rads, float x, float y, float z); void stop_animation(void* asset, int index); + +void set_camera_position(void* viewer, float x, float y, float z); + +void set_camera_rotation(void* viewer, float rads, float x, float y, float z); #endif diff --git a/ios/src/SceneAsset.cpp b/ios/src/SceneAsset.cpp index 4756fcda..95798f89 100644 --- a/ios/src/SceneAsset.cpp +++ b/ios/src/SceneAsset.cpp @@ -27,7 +27,6 @@ using namespace filament; using namespace filament::gltfio; using namespace image; using namespace utils; -using namespace filament::math; SceneAsset::SceneAsset(FilamentAsset *asset, Engine *engine, NameComponentManager *ncm, LoadResource loadResource, FreeResource freeResource) @@ -294,15 +293,29 @@ void SceneAsset::transformToUnitCube() { void SceneAsset::setPosition(float x, float y, float z) { Log("Setting position to %f %f %f", x, y, z); auto &tm = _engine->getTransformManager(); - auto transform = tm.getTransform(tm.getInstance(_asset->getRoot())); - tm.setTransform(tm.getInstance(_asset->getRoot()), transform * math::mat4f::translation(math::float3(x,y,z))); + _position = math::mat4f::translation(math::float3(x,y,z)); + auto aabb = _asset->getBoundingBox(); + auto center = aabb.center(); + auto halfExtent = aabb.extent(); + auto maxExtent = max(halfExtent) * 2; + auto scaleFactor = 2.0f / maxExtent; + auto transform = + math::mat4f::scaling(scaleFactor) * math::mat4f::translation(-center) * _position * _rotation; + tm.setTransform(tm.getInstance(_asset->getRoot()), transform); } void SceneAsset::setRotation(float rads, float x, float y, float z) { Log("Rotating %f radians around axis %f %f %f", x, y, z); auto &tm = _engine->getTransformManager(); - auto transform = tm.getTransform(tm.getInstance(_asset->getRoot())); - tm.setTransform(tm.getInstance(_asset->getRoot()), transform * math::mat4f::rotation(rads, math::float3(x,y,z))); + _rotation = math::mat4f::rotation(rads, math::float3(x,y,z)); + auto aabb = _asset->getBoundingBox(); + auto center = aabb.center(); + auto halfExtent = aabb.extent(); + auto maxExtent = max(halfExtent) * 2; + auto scaleFactor = 2.0f / maxExtent; + auto transform = + math::mat4f::scaling(scaleFactor) * math::mat4f::translation(-center) * _position * _rotation; + tm.setTransform(tm.getInstance(_asset->getRoot()), transform); } diff --git a/ios/src/SceneAsset.hpp b/ios/src/SceneAsset.hpp index a0692662..186e3f50 100644 --- a/ios/src/SceneAsset.hpp +++ b/ios/src/SceneAsset.hpp @@ -6,11 +6,15 @@ #include #include +#include +#include +#include +#include + #include #include #include - #include #include "SceneResources.hpp" @@ -102,5 +106,8 @@ namespace polyvox { // a slot to preload textures filament::Texture* _texture = nullptr; + math::mat4f _position; + math::mat4f _rotation; + }; } \ No newline at end of file