Skip to content

Commit

Permalink
TransformCtrl: Add new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kimkulling committed Dec 1, 2024
1 parent 80d890a commit 50ea125
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 69 deletions.
36 changes: 20 additions & 16 deletions samples/01_ModelLoading/ModelLoading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ using namespace ::OSRE::RenderBackend;
static constexpr c8 Tag[] = "ModelLoadingApp";

//-------------------------------------------------------------------------------------------------
/// @ingroup Editor
/// @ingroup Samples
///
/// @brief
//-------------------------------------------------------------------------------------------------
Expand All @@ -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[]) :
Expand All @@ -61,7 +62,8 @@ class ModelLoadingApp : public App::AppBase {
mCamera(nullptr),
mTransformMatrix(),
mModelNode(),
mIntention(0) {
mIntention(0),
mKeyboardTransCtrl(nullptr) {
// empty
}

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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<RenderBackendService>(ServiceType::RenderService);
if (nullptr == rbSrv) {
osre_error(Tag, "RenderBackendService not available.");
return;
}

rbSrv->beginPass(RenderPass::getPassNameById(RenderPassId));
rbSrv->beginRenderBatch("b1");
Expand Down
7 changes: 6 additions & 1 deletion src/Engine/Animation/AnimatorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ enum class TransformCommandType {
RotateZCommandNegative,
ScaleInCommand,
ScaleOutCommand,
TransformCommand,
TransformCommandXPositive,
TransformCommandXNegative,
TransformCommandYPositive,
TransformCommandYNegative,
TransformCommandZPositive,
TransformCommandZNegative,
Count
};

Expand Down
4 changes: 3 additions & 1 deletion src/Engine/App/CameraComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
74 changes: 40 additions & 34 deletions src/Engine/App/TransformController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Check warning

Code scanning / CodeQL

Missing enum case in switch Warning

Switch statement does not have a case for
Invalid
.
Switch statement does not have a case for
TransformCommandZPositive
.
Switch statement does not have a case for
TransformCommandZNegative
.
Switch statement does not have a case for
Count
.
}

Expand Down
49 changes: 33 additions & 16 deletions src/Engine/App/TransformController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Animation::TransformCommandType, Platform::KEY_LAST> 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() {
Expand All @@ -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.
Expand Down Expand Up @@ -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;
};

Expand Down
1 change: 0 additions & 1 deletion src/Engine/RenderBackend/2D/CanvasRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 50ea125

Please sign in to comment.