From 50ea125e8e7f66bc5d44eb84ad911cf65d9aa80b Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 1 Dec 2024 18:25:56 +0100 Subject: [PATCH] TransformCtrl: Add new commands --- samples/01_ModelLoading/ModelLoading.cpp | 36 +++++---- src/Engine/Animation/AnimatorBase.h | 7 +- src/Engine/App/CameraComponent.cpp | 4 +- src/Engine/App/TransformController.cpp | 74 ++++++++++--------- src/Engine/App/TransformController.h | 49 ++++++++---- .../RenderBackend/2D/CanvasRenderer.cpp | 1 - 6 files changed, 102 insertions(+), 69 deletions(-) diff --git a/samples/01_ModelLoading/ModelLoading.cpp b/samples/01_ModelLoading/ModelLoading.cpp index a857b68ae..fa97306ea 100644 --- a/samples/01_ModelLoading/ModelLoading.cpp +++ b/samples/01_ModelLoading/ModelLoading.cpp @@ -41,7 +41,7 @@ using namespace ::OSRE::RenderBackend; static constexpr c8 Tag[] = "ModelLoadingApp"; //------------------------------------------------------------------------------------------------- -/// @ingroup Editor +/// @ingroup Samples /// /// @brief //------------------------------------------------------------------------------------------------- @@ -53,6 +53,7 @@ class ModelLoadingApp : public App::AppBase { TransformMatrixBlock mTransformMatrix; ///< The transform block. TransformComponent::NodePtr mModelNode; ///< The mode node. int mIntention; + Animation::AnimationControllerBase *mKeyboardTransCtrl; public: ModelLoadingApp(int argc, char *argv[]) : @@ -61,7 +62,8 @@ class ModelLoadingApp : public App::AppBase { mCamera(nullptr), mTransformMatrix(), mModelNode(), - mIntention(0) { + mIntention(0), + mKeyboardTransCtrl(nullptr) { // empty } @@ -113,6 +115,15 @@ class ModelLoadingApp : public App::AppBase { } protected: + bool onCreate() override { + if (!AppBase::onCreate()) { + return false; + } + mKeyboardTransCtrl = AppBase::getTransformController(mTransformMatrix); + + return true; + } + void loadAsset(const IO::Uri &modelLoc) { AssimpWrapper assimpWrapper(*getIdContainer(), getStage()->getActiveWorld(0)); if (!assimpWrapper.importAsset(modelLoc, 0)) { @@ -155,23 +166,16 @@ class ModelLoadingApp : public App::AppBase { } } - glm::mat4 rot(1.0); - if (AppBase::isKeyPressed(Platform::KEY_A)) { - mTransformMatrix.mModel *= glm::rotate(rot, 0.01f, glm::vec3(1, 0, 0)); - - } - if (AppBase::isKeyPressed(Platform::KEY_S)) { - mTransformMatrix.mModel *= glm::rotate(rot, -0.01f, glm::vec3(1, 0, 0)); + Platform::Key key = AppBase::getKeyboardEventListener()->getLastKey(); + if (key != Platform::KEY_UNKNOWN) { + mKeyboardTransCtrl->update(mKeyboardTransCtrl->getKeyBinding(key)); } - if (AppBase::isKeyPressed(Platform::KEY_W)) { - mTransformMatrix.mModel *= glm::rotate(rot, 0.01f, glm::vec3(0, 1, 0)); - } - - if (AppBase::isKeyPressed(Platform::KEY_D)) { - mTransformMatrix.mModel *= glm::rotate(rot, -0.01f, glm::vec3(0, 1, 0)); - } RenderBackendService *rbSrv = ServiceProvider::getService(ServiceType::RenderService); + if (nullptr == rbSrv) { + osre_error(Tag, "RenderBackendService not available."); + return; + } rbSrv->beginPass(RenderPass::getPassNameById(RenderPassId)); rbSrv->beginRenderBatch("b1"); diff --git a/src/Engine/Animation/AnimatorBase.h b/src/Engine/Animation/AnimatorBase.h index f00b072f1..0071f7187 100644 --- a/src/Engine/Animation/AnimatorBase.h +++ b/src/Engine/Animation/AnimatorBase.h @@ -132,7 +132,12 @@ enum class TransformCommandType { RotateZCommandNegative, ScaleInCommand, ScaleOutCommand, - TransformCommand, + TransformCommandXPositive, + TransformCommandXNegative, + TransformCommandYPositive, + TransformCommandYNegative, + TransformCommandZPositive, + TransformCommandZNegative, Count }; diff --git a/src/Engine/App/CameraComponent.cpp b/src/Engine/App/CameraComponent.cpp index cde3e33c4..f6a117406 100644 --- a/src/Engine/App/CameraComponent.cpp +++ b/src/Engine/App/CameraComponent.cpp @@ -160,7 +160,9 @@ bool CameraComponent::onUpdate(Time) { } bool CameraComponent::onRender(RenderBackendService *rbSrv) { - osre_assert(nullptr != rbSrv); + if (nullptr == rbSrv) { + return false; + } rbSrv->setMatrix(MatrixType::View, mView); rbSrv->setMatrix(MatrixType::Projection, mProjection); diff --git a/src/Engine/App/TransformController.cpp b/src/Engine/App/TransformController.cpp index 31e0b7a2c..a2f796d2c 100644 --- a/src/Engine/App/TransformController.cpp +++ b/src/Engine/App/TransformController.cpp @@ -35,13 +35,13 @@ using namespace ::OSRE::RenderBackend; using namespace ::OSRE::Platform; TransformController::TransformController(TransformMatrixBlock &tmb) : - mKeyboardMap(), + mInputMap(), mTransform(tmb) { - mKeyboardMap.setDefault(); + mInputMap.setDefault(); } TransformCommandType TransformController::getKeyBinding(Key key) const { - return mKeyboardMap.get(key); + return mInputMap.get(key); } void mapToSphere(const glm::vec2 &newPt, glm::vec3 *newVector, f32 w, f32 h, f32 r) { @@ -100,37 +100,43 @@ void TransformController::getMouseUpdate(const MouseInputState &mis) { void TransformController::update(TransformCommandType cmdType) { glm::mat4 rot(1.0); - if (cmdType == TransformCommandType::RotateXCommandPositive) { - mTransform.mModel *= glm::rotate(rot, 0.01f, glm::vec3(1, 0, 0)); - } - - if (cmdType == TransformCommandType::RotateXCommandNegative) { - mTransform.mModel *= glm::rotate(rot, -0.01f, glm::vec3(1, 0, 0)); - } - - if (cmdType == TransformCommandType::RotateYCommandPositive) { - mTransform.mModel *= glm::rotate(rot, 0.01f, glm::vec3(0, 1, 0)); - } - - if (cmdType == TransformCommandType::RotateYCommandNegative) { - mTransform.mModel *= glm::rotate(rot, -0.01f, glm::vec3(0, 1, 0)); - } - - if (cmdType == TransformCommandType::RotateZCommandNegative) { - mTransform.mModel *= glm::rotate(rot, -0.01f, glm::vec3(0, 0, 1)); - } - - if (cmdType == TransformCommandType::RotateZCommandPositive) { - mTransform.mModel *= glm::rotate(rot, 0.01f, glm::vec3(0, 0, 1)); - } - - glm::mat4 scale(1.0); - if (cmdType == TransformCommandType::ScaleInCommand) { - mTransform.mModel *= glm::scale(scale, glm::vec3(1.01, 1.01, 1.01)); - } - - if (cmdType == TransformCommandType::ScaleOutCommand) { - mTransform.mModel *= glm::scale(scale, glm::vec3(0.99, 0.99, 0.99)); + switch (cmdType) { + case TransformCommandType::RotateXCommandPositive: + mTransform.mModel *= glm::rotate(rot, mTransformConfig.mRotateFactor, glm::vec3(1, 0, 0)); + break; + case TransformCommandType::RotateXCommandNegative: + mTransform.mModel *= glm::rotate(rot, -mTransformConfig.mRotateFactor, glm::vec3(1, 0, 0)); + break; + case TransformCommandType::RotateYCommandPositive: + mTransform.mModel *= glm::rotate(rot, mTransformConfig.mRotateFactor, glm::vec3(0, 1, 0)); + break; + case TransformCommandType::RotateYCommandNegative: + mTransform.mModel *= glm::rotate(rot, -mTransformConfig.mRotateFactor, glm::vec3(0, 1, 0)); + break; + case TransformCommandType::RotateZCommandNegative: + mTransform.mModel *= glm::rotate(rot, -mTransformConfig.mRotateFactor, glm::vec3(0, 0, 1)); + break; + case TransformCommandType::RotateZCommandPositive: + mTransform.mModel *= glm::rotate(rot, mTransformConfig.mRotateFactor, glm::vec3(0, 0, 1)); + break; + case TransformCommandType::ScaleInCommand: + mTransform.mModel *= glm::scale(rot, glm::vec3(mTransformConfig.mScaleFactorPlus, mTransformConfig.mScaleFactorPlus, mTransformConfig.mScaleFactorPlus)); + break; + case TransformCommandType::ScaleOutCommand: + mTransform.mModel *= glm::scale(rot, glm::vec3(mTransformConfig.mScaleFactorMinus, mTransformConfig.mScaleFactorMinus, mTransformConfig.mScaleFactorMinus)); + break; + case TransformCommandType::TransformCommandXPositive: + mTransform.mModel = glm::translate(mTransform.mModel, glm::vec3(mTransformConfig.mTranslateFactor, 0, 0)); + break; + case TransformCommandType::TransformCommandXNegative: + mTransform.mModel = glm::translate(mTransform.mModel, glm::vec3(-mTransformConfig.mTranslateFactor, 0, 0)); + break; + case TransformCommandType::TransformCommandYPositive: + mTransform.mModel = glm::translate(mTransform.mModel, glm::vec3(0, mTransformConfig.mTranslateFactor, 0)); + break; + case TransformCommandType::TransformCommandYNegative: + mTransform.mModel = glm::translate(mTransform.mModel, glm::vec3(0, -mTransformConfig.mTranslateFactor, 0)); + break; } } diff --git a/src/Engine/App/TransformController.h b/src/Engine/App/TransformController.h index 49b4c9237..91e432971 100644 --- a/src/Engine/App/TransformController.h +++ b/src/Engine/App/TransformController.h @@ -37,16 +37,16 @@ namespace RenderBackend { namespace App { /// @brief The class to manage the keyboard mappings for transform commands. -struct KeyboardMap { +struct InputMap { cppcore::TStaticArray KeyArray; /// @brief The class constructor. - KeyboardMap() { + InputMap() { init(); } /// @brief The class destructor. - ~KeyboardMap() = default; + ~InputMap() = default; /// @brief Will initialize the array, no mapping is stored. void init() { @@ -57,20 +57,31 @@ struct KeyboardMap { /// @brief Enables the default mapping. void setDefault() { - KeyArray[Platform::KEY_A] = Animation::TransformCommandType::RotateXCommandPositive; - KeyArray[Platform::KEY_a] = Animation::TransformCommandType::RotateXCommandPositive; - KeyArray[Platform::KEY_D] = Animation::TransformCommandType::RotateXCommandNegative; - KeyArray[Platform::KEY_d] = Animation::TransformCommandType::RotateXCommandNegative; - KeyArray[Platform::KEY_W] = Animation::TransformCommandType::RotateYCommandPositive; - KeyArray[Platform::KEY_w] = Animation::TransformCommandType::RotateYCommandPositive; - KeyArray[Platform::KEY_S] = Animation::TransformCommandType::RotateYCommandNegative; - KeyArray[Platform::KEY_s] = Animation::TransformCommandType::RotateYCommandNegative; - KeyArray[Platform::KEY_Q] = Animation::TransformCommandType::RotateZCommandPositive; - KeyArray[Platform::KEY_q] = Animation::TransformCommandType::RotateZCommandPositive; - KeyArray[Platform::KEY_E] = Animation::TransformCommandType::RotateZCommandNegative; - KeyArray[Platform::KEY_e] = Animation::TransformCommandType::RotateZCommandNegative; + KeyArray[Platform::KEY_Q] = Animation::TransformCommandType::RotateXCommandPositive; + KeyArray[Platform::KEY_q] = Animation::TransformCommandType::RotateXCommandPositive; + KeyArray[Platform::KEY_E] = Animation::TransformCommandType::RotateXCommandNegative; + KeyArray[Platform::KEY_e] = Animation::TransformCommandType::RotateXCommandNegative; + KeyArray[Platform::KEY_y] = Animation::TransformCommandType::RotateYCommandPositive; + KeyArray[Platform::KEY_Y] = Animation::TransformCommandType::RotateYCommandPositive; + KeyArray[Platform::KEY_c] = Animation::TransformCommandType::RotateYCommandNegative; + KeyArray[Platform::KEY_C] = Animation::TransformCommandType::RotateYCommandNegative; + KeyArray[Platform::KEY_s] = Animation::TransformCommandType::RotateZCommandPositive; + KeyArray[Platform::KEY_S] = Animation::TransformCommandType::RotateZCommandPositive; + KeyArray[Platform::KEY_f] = Animation::TransformCommandType::RotateZCommandNegative; + KeyArray[Platform::KEY_F] = Animation::TransformCommandType::RotateZCommandNegative; KeyArray[Platform::KEY_PLUS] = Animation::TransformCommandType::ScaleInCommand; KeyArray[Platform::KEY_MINUS] = Animation::TransformCommandType::ScaleOutCommand; + + KeyArray[Platform::KEY_w] = Animation::TransformCommandType::TransformCommandXPositive; + KeyArray[Platform::KEY_W] = Animation::TransformCommandType::TransformCommandXPositive; + KeyArray[Platform::KEY_s] = Animation::TransformCommandType::TransformCommandXNegative; + KeyArray[Platform::KEY_S] = Animation::TransformCommandType::TransformCommandXNegative; + + KeyArray[Platform::KEY_a] = Animation::TransformCommandType::TransformCommandYNegative; + KeyArray[Platform::KEY_A] = Animation::TransformCommandType::TransformCommandYNegative; + + KeyArray[Platform::KEY_d] = Animation::TransformCommandType::TransformCommandYPositive; + KeyArray[Platform::KEY_D] = Animation::TransformCommandType::TransformCommandYPositive; } /// @brief Set a new mapping. @@ -122,7 +133,13 @@ class OSRE_EXPORT TransformController : public Animation::AnimationControllerBas void update(Animation::TransformCommandType cmdType) override; private: - KeyboardMap mKeyboardMap; + struct TransformConfig { + f32 mScaleFactorPlus = 1.01f; + f32 mScaleFactorMinus = 0.99f; + f32 mRotateFactor = 0.01f; + f32 mTranslateFactor = 0.15f; + } mTransformConfig; + InputMap mInputMap; RenderBackend::TransformMatrixBlock &mTransform; }; diff --git a/src/Engine/RenderBackend/2D/CanvasRenderer.cpp b/src/Engine/RenderBackend/2D/CanvasRenderer.cpp index 0e1646257..704ee11e1 100644 --- a/src/Engine/RenderBackend/2D/CanvasRenderer.cpp +++ b/src/Engine/RenderBackend/2D/CanvasRenderer.cpp @@ -147,7 +147,6 @@ void dealloc(DrawCmd *cmd) { delete cmd; } - CanvasRenderer::CanvasRenderer(i32 numLayers, i32 x, i32 y, i32 w, i32 h) : IRenderPath(), mDirty(true),