Skip to content

Commit

Permalink
implemented camera manipulator
Browse files Browse the repository at this point in the history
  • Loading branch information
hannojg committed Feb 23, 2024
1 parent 628084c commit 639249b
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 4 deletions.
1 change: 1 addition & 0 deletions package/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_library(

# Filament Utils
../cpp/core/utils/EntityWrapper.cpp
../cpp/core/utils/ManipulatorWrapper.cpp

# Java JNI
src/main/cpp/AndroidFilamentProxy.cpp
Expand Down
8 changes: 7 additions & 1 deletion package/cpp/core/CameraWrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "CameraWrapper.h"

void margelo::CameraWrapper::loadHybridMethods() {
// TODO: implement
registerHybridMethod("lookAt", &CameraWrapper::lookAt, this);
}

void margelo::CameraWrapper::lookAt(std::shared_ptr<ManipulatorWrapper> cameraManipulator) {
math::float3 eye, center, up;
cameraManipulator->getManipulator()->getLookAt(&eye, &center, &up);
_camera->lookAt({eye[0], eye[1], eye[2]}, {center[0], center[1], center[2]}, {up[0], up[1], up[2]});
}
5 changes: 5 additions & 0 deletions package/cpp/core/CameraWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "jsi/HybridObject.h"

#include "utils/ManipulatorWrapper.h"
#include <filament/Camera.h>

namespace margelo {
Expand All @@ -19,5 +20,9 @@ class CameraWrapper : public HybridObject {

private:
std::shared_ptr<Camera> _camera;

private:
// Convenience methods
void lookAt(std::shared_ptr<ManipulatorWrapper> cameraManipulator);
};
} // namespace margelo
14 changes: 13 additions & 1 deletion package/cpp/core/EngineWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace margelo {

EngineWrapper::EngineWrapper() {
// TODO: make the enum for the backend for the engine configurable
_engine = References<Engine>::adoptRef(Engine::create(), [](Engine* engine) { engine->destroy(&engine); });
_materialProvider = filament::gltfio::createUbershaderProvider(_engine.get(), UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
_assetLoader =
Expand All @@ -35,8 +36,11 @@ void EngineWrapper::loadHybridMethods() {
registerHybridMethod("createScene", &EngineWrapper::createScene, this);
registerHybridMethod("createCamera", &EngineWrapper::createCamera, this);
registerHybridMethod("createView", &EngineWrapper::createView, this);
registerHybridMethod("createDefaultLight", &EngineWrapper::createDefaultLight, this);
registerHybridMethod("createSwapChain", &EngineWrapper::createSwapChain, this);

// Custom simplification methods
registerHybridMethod("createDefaultLight", &EngineWrapper::createDefaultLight, this);
registerHybridMethod("createCameraManipulator", &EngineWrapper::createCameraManipulator, this);
}

void EngineWrapper::setSurfaceProvider(std::shared_ptr<SurfaceProvider> surfaceProvider) {
Expand Down Expand Up @@ -115,4 +119,12 @@ std::shared_ptr<EntityWrapper> EngineWrapper::createDefaultLight() {
return std::make_shared<EntityWrapper>(std::move(lightEntity));
}

std::shared_ptr<ManipulatorWrapper> EngineWrapper::createCameraManipulator(int width, int height) {
ManipulatorBuilder* builder = new ManipulatorBuilder();
builder->targetPosition(0.0f, 0.0f, -4.0f); // kDefaultObjectPosition
builder->viewport(width, height);
std::shared_ptr<Manipulator<float>> manipulator = std::shared_ptr<Manipulator<float>>(builder->build(Mode::ORBIT));
return std::make_shared<ManipulatorWrapper>(manipulator);
}

} // namespace margelo
6 changes: 6 additions & 0 deletions package/cpp/core/EngineWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
#include "SwapChainWrapper.h"
#include "ViewWrapper.h"
#include "jsi/HybridObject.h"
#include <camutils/Manipulator.h>
#include <core/utils/ManipulatorWrapper.h>

namespace margelo {

using namespace filament;
using namespace camutils;

using ManipulatorBuilder = Manipulator<float>::Builder;

class EngineWrapper : public HybridObject {
public:
Expand All @@ -44,6 +49,7 @@ class EngineWrapper : public HybridObject {

// Custom simplification methods
std::shared_ptr<EntityWrapper> createDefaultLight();
std::shared_ptr<ManipulatorWrapper> createCameraManipulator(int windowWidth, int windowHeight);

private:
std::shared_ptr<Engine> _engine;
Expand Down
6 changes: 6 additions & 0 deletions package/cpp/core/ViewWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void ViewWrapper::loadHybridMethods() {
registerHybridGetter("scene", &ViewWrapper::getScene, this);
registerHybridSetter("camera", &ViewWrapper::setCamera, this);
registerHybridGetter("camera", &ViewWrapper::getCamera, this);
registerHybridMethod("setViewport", &ViewWrapper::setViewport, this);
}

void ViewWrapper::setScene(std::shared_ptr<SceneWrapper> scene) {
Expand All @@ -26,4 +27,9 @@ void ViewWrapper::setCamera(std::shared_ptr<CameraWrapper> camera) {
std::shared_ptr<CameraWrapper> ViewWrapper::getCamera() {
return _camera;
}

void ViewWrapper::setViewport(int x, int y, int width, int height) {
_view->setViewport({x, y, static_cast<uint32_t>(width), static_cast<uint32_t>(height)});
}

} // namespace margelo
4 changes: 3 additions & 1 deletion package/cpp/core/ViewWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#include "jsi/HybridObject.h"

#include <filament/View.h>
#include <filament/Viewport.h>

namespace margelo {
using namespace filament;

class ViewWrapper : public HybridObject {
public:
explicit ViewWrapper(std::shared_ptr<View> view) : _view(view) {}
explicit ViewWrapper(const std::shared_ptr<View>& view) : _view(std::move(view)) {}

void loadHybridMethods() override;

Expand All @@ -24,6 +25,7 @@ class ViewWrapper : public HybridObject {
std::shared_ptr<SceneWrapper> getScene();
void setCamera(std::shared_ptr<CameraWrapper> camera);
std::shared_ptr<CameraWrapper> getCamera();
void setViewport(int x, int y, int width, int height);

private:
std::shared_ptr<View> _view;
Expand Down
5 changes: 5 additions & 0 deletions package/cpp/core/utils/ManipulatorWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "ManipulatorWrapper.h"

namespace margelo {
void ManipulatorWrapper::loadHybridMethods() {}
} // namespace margelo
24 changes: 24 additions & 0 deletions package/cpp/core/utils/ManipulatorWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "jsi/HybridObject.h"

#include <camutils/Manipulator.h>

namespace margelo {
using namespace filament;
using namespace camutils;

class ManipulatorWrapper : public HybridObject {
public:
explicit ManipulatorWrapper(const std::shared_ptr<Manipulator<float>>& manipulator) : _manipulator(manipulator) {}

void loadHybridMethods() override;

const std::shared_ptr<Manipulator<float>>& getManipulator() {
return _manipulator;
}

private:
std::shared_ptr<Manipulator<float>> _manipulator;
};
} // namespace margelo
6 changes: 6 additions & 0 deletions package/src/FilamentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,18 @@ export class FilamentView extends React.PureComponent<FilamentViewProps> {
const view = engine.createView()
view.scene = scene
view.camera = camera
// TODO: setting the viewport currently crashes the renderer
// view.setViewport(0, 0, surface.width, surface.height)

const defaultLight = engine.createDefaultLight()
scene.addEntity(defaultLight)

const cameraManipulator = engine.createCameraManipulator(surface.width, surface.height)

// Start the rendering loop:
this.choreographer.addOnFrameListener((timestamp) => {
camera.lookAt(cameraManipulator)

// Render the scene, unless the renderer wants to skip the frame.
if (renderer.beginFrame(swapChain, timestamp)) {
renderer.render(view)
Expand Down
10 changes: 9 additions & 1 deletion package/src/native/FilamentTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export interface Scene {
addEntity(entity: Entity): void
}

export interface Manipulator {}

/**
* Camera represents the eye through which the scene is viewed.
*
Expand Down Expand Up @@ -177,7 +179,10 @@ export interface Scene {
*
* @see View
*/
export interface Camera {}
export interface Camera {
// Convenience method. The original method works slightly different, this is a simplification, so we don't have to deal with out params.
lookAt(cameraManipulator: Manipulator): void
}

/**
* Encompasses all the state needed for rendering a {@link Scene}.
Expand All @@ -200,6 +205,7 @@ export interface Camera {}
export interface View {
camera: Camera
scene: Scene
setViewport(x: number, y: number, width: number, height: number): void
}

// TODO: I think entities are at their core just numbers maybe we can expose them just as such to JS
Expand Down Expand Up @@ -252,5 +258,7 @@ export interface Engine {
createView(): View
createSwapChain(surface: TSurface): SwapChain

// Convenience methods:
createDefaultLight(): Entity
createCameraManipulator(screenWidth: number, screenHeight: number): Manipulator
}

0 comments on commit 639249b

Please sign in to comment.