-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathModelLoading.cpp
247 lines (205 loc) · 8.44 KB
/
ModelLoading.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)
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
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 "App/App.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"
#include "App/MouseEventListener.h"
#include "App/OrbitalMouseControl.h"
#include <assimp/scene.h>
#include <iostream>
using namespace ::OSRE;
using namespace ::OSRE::App;
using namespace ::OSRE::Common;
using namespace ::OSRE::RenderBackend;
DECL_OSRE_LOG_MODULE(ModelLoadingApp)
class SceneDumper {
/// The intention.
int mIntention = 0;
public:
/// @brief The class constructor.
SceneDumper() = default;
/// @brief Will push intention.
void pushIntention() {
mIntention++;
}
/// @brief Will pop intention.
void popIntention() {
mIntention--;
}
/// @brief Evaluates the name of the node.
/// @param[in] name The name to evaluate.
void checkName(String &name) {
if (name.empty()) {
name = "No Name";
}
}
/// @brief Will dump the single name.
/// @param[in] node The node to dump.
void dumpNode(aiNode &node) {
String name = node.mName.C_Str();
checkName(name);
for (int i=0; i<mIntention; ++i) {
std::cout << "+-";
}
std::cout << "Node name: " << name << "\n";
for (unsigned int i=0; i < node.mNumChildren; ++i) {
pushIntention();
dumpNode(*node.mChildren[i]);
popIntention();
}
}
/// @brief Will show the statistic.
/// @param[in] scene The scene to show statistics.
void showStatistics(const aiScene &scene) {
std::cout << "Model name: " << scene.mName.C_Str() << "\n";
std::cout << "=============================================================\n";
if (scene.mRootNode != nullptr) {
dumpNode(*scene.mRootNode);
}
std::cout << "Number of meshes : " << scene.mNumMeshes << "\n";
std::cout << "Number of materials : " << scene.mNumMaterials << "\n";
std::cout << " +-Number of embedded textures : " << scene.mNumTextures << "\n";
std::cout << "Number of animations : " << scene.mNumAnimations << "\n";
}
};
//-------------------------------------------------------------------------------------------------
/// @ingroup Samples
///
/// @brief This sample shows how to load a model using the Assimp wrapper.
///
/// It will also show some statistics about the loaded model. The model will get loaded, the
/// camera will be placed to get an optimal view onto the model. The model will be rendered.
//-------------------------------------------------------------------------------------------------
class ModelLoadingApp : public App::AppBase {
/// The asset folder, here we will locate our assets.
String mAssetFolder;
/// The camera component.
CameraComponent *mCamera = nullptr;
/// The transform block.
TransformMatrixBlock mTransformMatrix;
/// The mode node.
TransformComponent::NodePtr mModelNode;
/// The controller for the keyboard.
Animation::AnimationControllerBase *mKeyboardTransCtrl = nullptr;
/// The mouse controller for rotating the model.
OrbitalMouseControl *mOrbitalMouseControl = nullptr;
public:
/// @brief The class constructor.
/// @param argc The number of arguments.
/// @param argv The argument array.
ModelLoadingApp(int argc, char *argv[]) :
AppBase(argc, (const char **)argv, "api", "The render API") {
// empty
}
/// @brief The class destructor.
~ModelLoadingApp() override {
delete mOrbitalMouseControl;
}
/// @brief Will return true, if a model was loaded.
/// @return true for model loaded.
bool hasModel() const {
return mModelNode != nullptr;
}
protected:
bool onCreate() override {
if (!AppBase::onCreate()) {
return false;
}
mKeyboardTransCtrl = AppBase::getTransformController(mTransformMatrix);
if(mKeyboardTransCtrl == nullptr) {
osre_error(Tag, "Failed to initialize keyboard transform controller.");
return false;
}
mOrbitalMouseControl = new OrbitalMouseControl(&mTransformMatrix);
return true;
}
void loadAsset(const IO::Uri &modelLoc) {
AssimpWrapper assimpWrapper(*getIdContainer(), getActiveScene());
if (!assimpWrapper.importAsset(modelLoc, 0)) {
return;
}
auto *rbSrv = ServiceProvider::getService<RenderBackendService>(ServiceType::RenderService);
if (nullptr == rbSrv) {
osre_error(Tag, "RenderBackendService not available.");
return;
}
Platform::AbstractWindow *rootWindow = getRootWindow();
if (nullptr == rootWindow) {
osre_error(Tag, "Root window not available.");
return;
}
Rect2ui windowsRect;
rootWindow->getWindowsRect(windowsRect);
auto *scene = new Scene("model");
addScene(scene, true);
mCamera = AppBase::setupCamera("camera", scene, windowsRect, *getIdContainer());
auto *entity = assimpWrapper.getEntity();
scene->addEntity(entity);
mCamera->observeBoundingBox(entity->getAABB());
mModelNode = entity->getNode();
SceneDumper sd;
sd.showStatistics(*assimpWrapper.getScene());
}
void onUpdate() override {
if (AppBase::isKeyPressed(Platform::KEY_O) || AppBase::isKeyPressed(Platform::KEY_o)) {
IO::Uri modelLoc;
Platform::PlatformOperations::getFileOpenDialog("Choose asset for import", "*", modelLoc);
if ( modelLoc.isValid()) {
loadAsset(modelLoc);
}
}
if (Platform::Key key = AppBase::getKeyboardEventListener()->getLastKey(); key != Platform::KEY_UNKNOWN) {
mKeyboardTransCtrl->update(mKeyboardTransCtrl->getKeyBinding(key));
}
auto *rbSrv = ServiceProvider::getService<RenderBackendService>(ServiceType::RenderService);
if (nullptr == rbSrv) {
osre_error(Tag, "RenderBackendService not available.");
return;
}
if (mCamera != nullptr) {
mOrbitalMouseControl->update(AppBase::getMouseEventListener(), mCamera->getRight(), mCamera->getUp());
}
rbSrv->beginPass(RenderPass::getPassNameById(RenderPassId));
rbSrv->beginRenderBatch("b1");
rbSrv->setMatrix(MatrixType::Model, mTransformMatrix.mModel);
rbSrv->endRenderBatch();
rbSrv->endPass();
AppBase::onUpdate();
}
};
int main(int argc, char *argv[]) {
ModelLoadingApp myApp(argc, argv);
if (!myApp.initWindow(10, 10, 1024, 768, "ModelLoader sample! Press o to import an Asset",
WindowMode::Windowed, WindowType::Root, RenderBackendType::OpenGLRenderBackend)) {
osre_error(Tag, "Error while creating window.");
return 1;
}
while (myApp.handleEvents()) {
myApp.update();
myApp.requestNextFrame();
}
myApp.destroy();
return 0;
}