From 9a861d6d710e1365936b7bc982d75d6d53865fc5 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 9 Feb 2025 17:10:30 +0100 Subject: [PATCH 1/2] Add mouse controll to provide orbiting --- CMakeLists.txt | 1 - src/ModelLoading.cpp | 87 +++++++++++++++++++++++++++++++++++++------ src/ModelLoadingApp.h | 21 +++++------ src/SceneData.h | 2 +- src/main.cpp | 26 ++++++++++++- 5 files changed, 110 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c1c364b..5083286 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,6 @@ project(assimp_view) find_package(SDL2 CONFIG REQUIRED) - set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/lib ) set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/lib ) set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/bin ) diff --git a/src/ModelLoading.cpp b/src/ModelLoading.cpp index 851da65..29eaf7a 100644 --- a/src/ModelLoading.cpp +++ b/src/ModelLoading.cpp @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2015-2024 OSRE ( Open Source Render Engine ) by Kim Kulling +Copyright (c) 2024-2025 Assimp-Viewer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -20,11 +20,12 @@ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -----------------------------------------------------------------------------------------------*/ -#include "ModelLoadingApp.h" #include "App/App.h" +#include "App/MouseEventListener.h" #include "App/Scene.h" -#include "IO/Uri.h" #include "Common/BaseMath.h" +#include "IO/Uri.h" +#include "ModelLoadingApp.h" #include "Platform/AbstractWindow.h" #include "Platform/PlatformOperations.h" #include "Properties/Settings.h" @@ -46,17 +47,15 @@ static constexpr c8 Tag[] = "Assimp-Viewer"; ModelLoadingApp::ModelLoadingApp(int argc, char *argv[]) : AppBase(argc, (const char **)argv, "api", "The render API"), - mAssetFolder(), mCamera(nullptr), - mTransformMatrix(), - mModelNode(), mIntention(0), mAssimpWrapper(nullptr) { - // empty + mLastMousePos.x = 0; + mLastMousePos.y = 0; } ModelLoadingApp::~ModelLoadingApp() { - delete mAssimpWrapper; + clear(); } bool ModelLoadingApp::hasModel() const { @@ -104,7 +103,68 @@ void ModelLoadingApp::showStatistics(const aiScene &scene) { std::cout << "Number of materials : " << scene.mNumMaterials << "\n"; } +void ModelLoadingApp::onMouseUpdate() { + auto *mouseListener = AppBase::getMouseEventListener(); + if (mouseListener == nullptr) { + return; + } + mMousePos.x = mouseListener->getAbsoluteX(); + mMousePos.y = mouseListener->getAbsoluteY(); + + glm::mat4 localModel = glm::mat4(1.0); + if (mCamera != nullptr) { + if (mouseListener->leftButttonPressed()) { + const int diffX = (mMousePos.x - mLastMousePos.x); + const int diffY = (mMousePos.y - mLastMousePos.y); + if (diffX != 0) { + const f32 angle = glm::radians(((f32)diffX)/6); + localModel = rotate(localModel, angle, mCamera->getRight()); + } + + if (diffY != 0) { + const f32 angle = glm::radians(((f32)diffY)/6); + localModel = rotate(localModel, angle, mCamera->getUp()); + } + mTransformMatrix.mModel *= localModel; + } + + if (mouseListener->middleButttonPressed()) { + zoom(); + } + } + mLastMousePos.x = mMousePos.x; + mLastMousePos.y = mMousePos.y; +} + +void ModelLoadingApp::zoom() { + glm::mat4 localModel = glm::mat4(1.0); + const int diff = (mMousePos.y - mLastMousePos.y); + if (diff != 0) { + const f32 scaleFactor = 1.0 + (diff * 0.01); + + glm::vec3 s(scaleFactor, scaleFactor, scaleFactor); + localModel = scale(localModel, s); + mTransformMatrix.mModel *= localModel; + } +} + +void ModelLoadingApp::zoomAll() { + Scene *scene = getActiveScene(); + if (scene == nullptr || mCamera == nullptr) { + return; + } + + Entity *entity = mAssimpWrapper->getEntity(); + mCamera->observeBoundingBox(entity->getAABB()); +} + +void ModelLoadingApp::clear() { + delete mAssimpWrapper; + mAssimpWrapper = nullptr; +} + void ModelLoadingApp::importAsset(const IO::Uri &modelLoc) { + clear(); mAssimpWrapper = new AssimpWrapper(*getIdContainer(), getActiveScene()); if (!mAssimpWrapper->importAsset(modelLoc, 0)) { return; @@ -124,12 +184,12 @@ void ModelLoadingApp::importAsset(const IO::Uri &modelLoc) { Scene *scene = getActiveScene(); Entity *entity = mAssimpWrapper->getEntity(); Entity *camEntity = new Entity("camera", *getIdContainer(), scene); - mCamera = (CameraComponent*)camEntity->createComponent(ComponentType::CameraComponentType); + mCamera = (CameraComponent*) camEntity->createComponent(ComponentType::CameraComponentType); mCamera->setProjectionParameters(60.f, (f32)windowsRect.width, (f32)windowsRect.height, 0.01f, 1000.f); scene->setActiveCamera(mCamera); scene->addEntity(entity); - mCamera->observeBoundingBox(entity->getAABB()); + zoomAll(); mModelNode = entity->getNode(); showStatistics(*mAssimpWrapper->getScene()); @@ -145,7 +205,10 @@ void ModelLoadingApp::exportAsset(const IO::Uri &modelLoc) { } Assimp::Exporter exporter; - exporter.Export(mAssimpWrapper->getScene(), "obj", modelLoc.getAbsPath().c_str()); + aiReturn res = exporter.Export(mAssimpWrapper->getScene(), "obj", modelLoc.getAbsPath().c_str()); + if (res != AI_SUCCESS) { + osre_error(Tag, "Error while exporting asset"); + } } void ModelLoadingApp::onUpdate() { @@ -164,7 +227,6 @@ void ModelLoadingApp::onUpdate() { exportAsset(modelLoc); } } - glm::mat4 rot(1.0); if (AppBase::isKeyPressed(Platform::KEY_A) || AppBase::isKeyPressed(Platform::KEY_a)) { mTransformMatrix.mModel *= glm::rotate(rot, 0.01f, glm::vec3(1, 0, 0)); @@ -181,6 +243,7 @@ void ModelLoadingApp::onUpdate() { if (AppBase::isKeyPressed(Platform::KEY_D) || AppBase::isKeyPressed(Platform::KEY_d)) { mTransformMatrix.mModel *= glm::rotate(rot, -0.01f, glm::vec3(0, 1, 0)); } + onMouseUpdate(); RenderBackendService *rbSrv = ServiceProvider::getService(ServiceType::RenderService); rbSrv->beginPass(RenderPass::getPassNameById(RenderPassId)); diff --git a/src/ModelLoadingApp.h b/src/ModelLoadingApp.h index 3dfcd69..c171fe8 100644 --- a/src/ModelLoadingApp.h +++ b/src/ModelLoadingApp.h @@ -1,9 +1,7 @@ -#pragma once - /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2015-2024 OSRE ( Open Source Render Engine ) by Kim Kulling +Copyright (c) 2024-2025 Assimp-Viewer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in @@ -24,14 +22,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -----------------------------------------------------------------------------------------------*/ #include "App/App.h" -#include "App/Scene.h" -#include "IO/Uri.h" -#include "Common/BaseMath.h" -#include "Platform/AbstractWindow.h" -#include "Platform/PlatformOperations.h" -#include "Properties/Settings.h" #include "RenderBackend/RenderBackendService.h" -#include "RenderBackend/RenderCommon.h" #include "RenderBackend/TransformMatrixBlock.h" using namespace ::OSRE; @@ -57,6 +48,10 @@ class ModelLoadingApp final : public App::AppBase { void checkName(String &name); void dumpNode(aiNode &node); void showStatistics(const aiScene &scene); + void onMouseUpdate(); + void zoom(); + void zoomAll(); + void clear(); protected: void importAsset(const IO::Uri &modelLoc); @@ -65,9 +60,11 @@ class ModelLoadingApp final : public App::AppBase { private: String mAssetFolder; ///< The asset folder, here we will locate our assets. - App::CameraComponent *mCamera; ///< The camera component. + CameraComponent *mCamera; ///< The camera component. TransformMatrixBlock mTransformMatrix; ///< The tansform block. TransformComponent::NodePtr mModelNode; ///< The mode node. int mIntention; - OSRE::App::AssimpWrapper *mAssimpWrapper; + AssimpWrapper *mAssimpWrapper; + glm::i32vec2 mMousePos; + glm::i32vec2 mLastMousePos; }; diff --git a/src/SceneData.h b/src/SceneData.h index bcbf126..46b69d6 100644 --- a/src/SceneData.h +++ b/src/SceneData.h @@ -1,7 +1,7 @@ /*----------------------------------------------------------------------------------------------- The MIT License (MIT) -Copyright (c) 2015-2023 OSRE ( Open Source Render Engine ) by Kim Kulling +Copyright (c) 2015-2025 OSRE ( Open Source Render Engine ) by Kim Kulling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/src/main.cpp b/src/main.cpp index a14ce30..a70f18f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,25 @@ +/*----------------------------------------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2024-2025 Assimp-Viewer + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-----------------------------------------------------------------------------------------------*/ #include "ModelLoadingApp.h" using namespace ::OSRE; @@ -7,7 +29,9 @@ using namespace ::OSRE::RenderBackend; int main(int argc, char *argv[]) { ModelLoadingApp myApp(argc, argv); - if (!myApp.initWindow(10, 10, 1024, 768, "Assimp-Vewer! Press i to import, e to export", WindowMode::Windowed, WindowType::Root, App::RenderBackendType::OpenGLRenderBackend)) { + if (!myApp.initWindow(10, 10, 1024, 768, "Assimp-Vewer! Press i to import, e to export", + WindowMode::Windowed, WindowType::Root, + RenderBackendType::OpenGLRenderBackend)) { return 1; } From 0c15f809b1d04b1595979992e770379380b2b33e Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 9 Feb 2025 20:16:11 +0100 Subject: [PATCH 2/2] osre: latest greatest --- contrib/osre | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/osre b/contrib/osre index 57a70ad..1412929 160000 --- a/contrib/osre +++ b/contrib/osre @@ -1 +1 @@ -Subproject commit 57a70ad2422445ab3a669fe7c2f66dcf252c07c5 +Subproject commit 14129296edf923d7a2dddda95b05f03222e7d69c