From 41432a07ed6e026576e269a928f9299d58d2afe8 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 26 Sep 2024 18:49:32 +0800 Subject: [PATCH] chore: getter for native camera view matrix --- .../lib/src/viewer/src/ffi/src/camera_ffi.dart | 5 +++++ .../src/viewer/src/ffi/src/thermion_dart.g.dart | 15 ++++++++++----- .../lib/src/viewer/src/shared_types/camera.dart | 1 + thermion_dart/native/include/ThermionDartApi.h | 5 ++++- thermion_dart/native/src/ThermionDartApi.cpp | 5 +++++ thermion_dart/test/camera_tests.dart | 4 ++++ 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/camera_ffi.dart b/thermion_dart/lib/src/viewer/src/ffi/src/camera_ffi.dart index 5b797cbc..f741aa1e 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/camera_ffi.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/camera_ffi.dart @@ -75,4 +75,9 @@ class ThermionFFICamera extends Camera { Future getFocalLength() async { return Camera_getFocalLength(camera); } + + @override + Future getViewMatrix() async { + return double4x4ToMatrix4(Camera_getViewMatrix(camera)); + } } diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart b/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart index 3436ea07..6b5b9ac4 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/thermion_dart.g.dart @@ -887,6 +887,16 @@ external double Camera_getCullingFar( ffi.Pointer camera, ); +@ffi.Native)>(isLeaf: true) +external double4x4 Camera_getViewMatrix( + ffi.Pointer camera, +); + +@ffi.Native)>(isLeaf: true) +external double4x4 Camera_getModelMatrix( + ffi.Pointer camera, +); + @ffi.Native)>(isLeaf: true) external double get_camera_near( ffi.Pointer camera, @@ -947,11 +957,6 @@ external void Camera_setLensProjection( double focalLength, ); -@ffi.Native)>(isLeaf: true) -external double4x4 Camera_getModelMatrix( - ffi.Pointer camera, -); - @ffi.Native)>(isLeaf: true) external int Camera_getEntity( ffi.Pointer camera, diff --git a/thermion_dart/lib/src/viewer/src/shared_types/camera.dart b/thermion_dart/lib/src/viewer/src/shared_types/camera.dart index 3b306036..6e1f4d6e 100644 --- a/thermion_dart/lib/src/viewer/src/shared_types/camera.dart +++ b/thermion_dart/lib/src/viewer/src/shared_types/camera.dart @@ -12,6 +12,7 @@ abstract class Camera { double aspect = 1.0, double focalLength = kFocalLength}); + Future getViewMatrix(); Future getModelMatrix(); Future setModelMatrix(Matrix4 matrix); diff --git a/thermion_dart/native/include/ThermionDartApi.h b/thermion_dart/native/include/ThermionDartApi.h index e378d533..3c9ba4ea 100644 --- a/thermion_dart/native/include/ThermionDartApi.h +++ b/thermion_dart/native/include/ThermionDartApi.h @@ -228,6 +228,9 @@ extern "C" EMSCRIPTEN_KEEPALIVE double Camera_getFocalLength(TCamera *const camera); EMSCRIPTEN_KEEPALIVE double Camera_getNear(TCamera *const camera); EMSCRIPTEN_KEEPALIVE double Camera_getCullingFar(TCamera *const camera); + EMSCRIPTEN_KEEPALIVE double4x4 Camera_getViewMatrix(TCamera *const camera); + EMSCRIPTEN_KEEPALIVE double4x4 Camera_getModelMatrix(TCamera* camera); + EMSCRIPTEN_KEEPALIVE double get_camera_near(TCamera *camera); EMSCRIPTEN_KEEPALIVE double get_camera_culling_far(TCamera *camera); EMSCRIPTEN_KEEPALIVE float get_camera_fov(TCamera *camera, bool horizontal); @@ -237,7 +240,7 @@ extern "C" EMSCRIPTEN_KEEPALIVE void Camera_setCustomProjectionWithCulling(TCamera* camera, double4x4 projectionMatrix, double near, double far); EMSCRIPTEN_KEEPALIVE void Camera_setModelMatrix(TCamera* camera, double4x4 modelMatrix); EMSCRIPTEN_KEEPALIVE void Camera_setLensProjection(TCamera *camera, double near, double far, double aspect, double focalLength); - EMSCRIPTEN_KEEPALIVE double4x4 Camera_getModelMatrix(TCamera* camera); + EMSCRIPTEN_KEEPALIVE EntityId Camera_getEntity(TCamera* camera); EMSCRIPTEN_KEEPALIVE TCamera *Engine_getCameraComponent(TEngine *engine, EntityId entity); diff --git a/thermion_dart/native/src/ThermionDartApi.cpp b/thermion_dart/native/src/ThermionDartApi.cpp index 78ed51ce..298db338 100644 --- a/thermion_dart/native/src/ThermionDartApi.cpp +++ b/thermion_dart/native/src/ThermionDartApi.cpp @@ -1081,6 +1081,11 @@ EMSCRIPTEN_KEEPALIVE double4x4 Camera_getModelMatrix(TCamera* tCamera) { return convert_mat4_to_double4x4(camera->getModelMatrix()); } +EMSCRIPTEN_KEEPALIVE double4x4 Camera_getViewMatrix(TCamera *const tCamera) { + auto * camera = reinterpret_cast(tCamera); + return convert_mat4_to_double4x4(camera->getViewMatrix()); +} + EMSCRIPTEN_KEEPALIVE EntityId Camera_getEntity(TCamera* tCamera) { auto * camera = reinterpret_cast(tCamera); return Entity::smuggle(camera->getEntity()); diff --git a/thermion_dart/test/camera_tests.dart b/thermion_dart/test/camera_tests.dart index 1b64362e..1ed61534 100644 --- a/thermion_dart/test/camera_tests.dart +++ b/thermion_dart/test/camera_tests.dart @@ -11,6 +11,7 @@ void main() async { var viewer = await createViewer(); var matrix = await viewer.getCameraModelMatrix(); expect(matrix.trace(), 4); + await viewer.setCameraPosition(2.0, 2.0, 2.0); matrix = await viewer.getCameraModelMatrix(); var position = matrix.getColumn(3).xyz; @@ -33,6 +34,9 @@ void main() async { // The view matrix should be the inverse of the model matrix var identity = modelMatrix * viewMatrix; expect(identity.isIdentity(), isTrue); + var camera = await viewer.getMainCamera(); + identity = modelMatrix * (await camera.getViewMatrix()); + expect(identity.isIdentity(), isTrue); // Check that moving the camera affects the view matrix await viewer.setCameraPosition(3.0, 4.0, 5.0);