Skip to content

Commit

Permalink
Merge pull request #37 from assimp/feature/mouse_control
Browse files Browse the repository at this point in the history
[Draft] Add mouse controll to provide orbiting
  • Loading branch information
kimkulling authored Feb 9, 2025
2 parents 79b299c + 0c15f80 commit c9022b1
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 28 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
87 changes: 75 additions & 12 deletions src/ModelLoading.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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() {
Expand All @@ -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));
Expand All @@ -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<RenderBackendService>(ServiceType::RenderService);

rbSrv->beginPass(RenderPass::getPassNameById(RenderPassId));
Expand Down
21 changes: 9 additions & 12 deletions src/ModelLoadingApp.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
};
2 changes: 1 addition & 1 deletion src/SceneData.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 25 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit c9022b1

Please sign in to comment.