/
ViewerExample.cpp
296 lines (234 loc) · 10.4 KB
/
ViewerExample.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include <memory>
#include <unordered_map>
#include "PluginManager/PluginManager.h"
#include "Scene.h"
#include "Camera.h"
#include "Trade/AbstractImporter.h"
#include "Trade/MeshData.h"
#include "Trade/MeshObjectData.h"
#include "Trade/SceneData.h"
#include "MeshTools/Tipsify.h"
#include "MeshTools/Interleave.h"
#include "MeshTools/CompressIndices.h"
#include "Shaders/PhongShader.h"
#include "FpsCounterExample.h"
#include "ViewedObject.h"
#include "configure.h"
using namespace std;
using namespace Corrade::PluginManager;
using namespace Corrade::Utility;
using namespace Magnum;
using namespace Magnum::Shaders;
using namespace Magnum::Trade;
using namespace Magnum::Examples;
namespace Magnum { namespace Examples {
class ViewerExample: public FpsCounterExample {
public:
ViewerExample(int& argc, char** argv);
~ViewerExample() {
for(auto i: meshes) delete i.second;
}
protected:
inline void viewportEvent(const Math::Vector2<GLsizei>& size) {
camera->setViewport(size);
FpsCounterExample::viewportEvent(size);
}
void drawEvent() {
camera->draw();
swapBuffers();
if(fpsCounterEnabled()) redraw();
}
void keyEvent(Key key, const Math::Vector2<int>& position) {
switch(key) {
case Key::Up:
o->rotate(deg(10.0f), Vector3::xAxis(-1));
break;
case Key::Down:
o->rotate(deg(10.0f), Vector3::xAxis(1));
break;
case Key::Left:
o->rotate(deg(10.0f), Vector3::yAxis(-1), false);
break;
case Key::Right:
o->rotate(deg(10.0f), Vector3::yAxis(1), false);
break;
case Key::PageUp:
camera->translate(Vector3::zAxis(-0.5), false);
break;
case Key::PageDown:
camera->translate(Vector3::zAxis(0.5), false);
break;
case Key::Home:
glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_FILL : GL_LINE);
wireframe = !wireframe;
break;
case Key::End:
if(fpsCounterEnabled()) printCounterStatistics();
else resetCounter();
setFpsCounterEnabled(!fpsCounterEnabled());
break;
default: break;
}
redraw();
}
void mouseEvent(MouseButton button, MouseState state, const Math::Vector2<int>& position) {
switch(button) {
case MouseButton::Left:
if(state == MouseState::Down) previousPosition = positionOnSphere(position);
else previousPosition = Vector3();
break;
case MouseButton::WheelUp:
case MouseButton::WheelDown: {
if(state == MouseState::Up) return;
/* Distance between origin and near camera clipping plane */
GLfloat distance = camera->transformation()[3].z()-0-camera->near();
/* Move 15% of the distance back or forward */
if(button == MouseButton::WheelUp)
distance *= 1 - 1/0.85f;
else
distance *= 1 - 0.85f;
camera->translate(Vector3::zAxis(distance), false);
redraw();
break;
}
default: ;
}
}
void mouseMoveEvent(const Math::Vector2<int>& position) {
Vector3 currentPosition = positionOnSphere(position);
Vector3 axis = Vector3::cross(previousPosition, currentPosition);
if(previousPosition.length() < 0.001f || axis.length() < 0.001f) return;
GLfloat angle = acos(Vector3::dot(previousPosition, currentPosition));
o->rotate(angle, axis);
previousPosition = currentPosition;
redraw();
}
private:
Vector3 positionOnSphere(const Math::Vector2<int>& _position) const {
Math::Vector2<GLsizei> viewport = camera->viewport();
Vector2 position(_position.x()*2.0f/viewport.x() - 1.0f,
_position.y()*2.0f/viewport.y() - 1.0f);
GLfloat length = position.length();
Vector3 result(length > 1.0f ? position : Vector3(position, 1.0f - length));
result.setY(-result.y());
return result.normalized();
}
void addObject(AbstractImporter* colladaImporter, Object* parent, unordered_map<size_t, PhongMaterialData*>& materials, size_t objectId);
Scene scene;
Camera* camera;
PhongShader shader;
Object* o;
unordered_map<size_t, IndexedMesh*> meshes;
size_t vertexCount, triangleCount, objectCount, meshCount, materialCount;
bool wireframe;
Vector3 previousPosition;
};
ViewerExample::ViewerExample(int& argc, char** argv): FpsCounterExample(argc, argv, "Magnum Viewer"), vertexCount(0), triangleCount(0), objectCount(0), meshCount(0), materialCount(0), wireframe(false) {
if(argc != 2) {
Debug() << "Usage:" << argv[0] << "file.dae";
exit(0);
}
/* Instance ColladaImporter plugin */
PluginManager<AbstractImporter> manager(PLUGIN_IMPORTER_DIR);
if(manager.load("ColladaImporter") != AbstractPluginManager::LoadOk) {
Error() << "Could not load ColladaImporter plugin";
exit(1);
}
unique_ptr<AbstractImporter> colladaImporter(manager.instance("ColladaImporter"));
if(!colladaImporter) {
Error() << "Could not instance ColladaImporter plugin";
exit(2);
}
if(!(colladaImporter->features() & AbstractImporter::OpenFile)) {
Error() << "ColladaImporter cannot open files";
exit(3);
}
scene.setFeature(Scene::DepthTest, true);
/* Every scene needs a camera */
camera = new Camera(&scene);
camera->setPerspective(deg(35.0f), 0.001f, 100);
camera->translate(Vector3::zAxis(5));
Debug() << "Opening file" << argv[1];
/* Load file */
if(!colladaImporter->open(argv[1]))
exit(4);
if(colladaImporter->sceneCount() == 0)
exit(5);
/* Map with materials */
unordered_map<size_t, PhongMaterialData*> materials;
/* Default object, parent of all (for manipulation) */
o = new Object(&scene);
Debug() << "Adding default scene...";
/* Load the scene */
SceneData* scene = colladaImporter->scene(colladaImporter->defaultScene());
/* Add all children */
for(size_t objectId: scene->children())
addObject(colladaImporter.get(), o, materials, objectId);
Debug() << "Imported" << objectCount << "objects with" << meshCount << "meshes and" << materialCount << "materials,";
Debug() << " " << vertexCount << "vertices and" << triangleCount << "triangles total.";
/* Delete materials, as they are now unused */
for(auto i: materials) delete i.second;
colladaImporter->close();
delete colladaImporter.release();
}
void ViewerExample::addObject(AbstractImporter* colladaImporter, Object* parent, unordered_map<size_t, PhongMaterialData*>& materials, size_t objectId) {
ObjectData* object = colladaImporter->object(objectId);
/* Only meshes for now */
if(object->instanceType() == ObjectData::InstanceType::Mesh) {
++objectCount;
/* Use already processed mesh, if exists */
IndexedMesh* mesh;
auto found = meshes.find(object->instanceId());
if(found != meshes.end()) mesh = found->second;
/* Or create a new one */
else {
++meshCount;
mesh = new IndexedMesh;
meshes.insert(make_pair(object->instanceId(), mesh));
MeshData* data = colladaImporter->mesh(object->instanceId());
if(!data || !data->indices() || !data->vertexArrayCount() || !data->normalArrayCount())
exit(6);
vertexCount += data->vertices(0)->size();
triangleCount += data->indices()->size()/3;
/* Optimize vertices */
Debug() << "Optimizing vertices of mesh" << object->instanceId() << "using Tipsify algorithm (cache size 24)...";
MeshTools::tipsify(*data->indices(), data->vertices(0)->size(), 24);
/* Interleave mesh data */
Buffer* buffer = mesh->addBuffer(true);
mesh->bindAttribute<PhongShader::Vertex>(buffer);
mesh->bindAttribute<PhongShader::Normal>(buffer);
MeshTools::interleave(mesh, buffer, Buffer::Usage::StaticDraw, *data->vertices(0), *data->normals(0));
/* Compress indices */
MeshTools::compressIndices(mesh, Buffer::Usage::StaticDraw, *data->indices());
}
/* Use already processed material, if exists */
PhongMaterialData* material;
auto materialFound = materials.find(static_cast<MeshObjectData*>(object)->material());
if(materialFound != materials.end()) material = materialFound->second;
/* Else get material or create default one */
else {
++materialCount;
material = static_cast<PhongMaterialData*>(colladaImporter->material(static_cast<MeshObjectData*>(object)->material()));
if(!material) material = new PhongMaterialData({0.0f, 0.0f, 0.0f}, {0.9f, 0.9f, 0.9f}, {1.0f, 1.0f, 1.0f}, 50.0f);
}
/* Add object */
Object* o = new ViewedObject(mesh, material, &shader, parent);
o->setTransformation(object->transformation());
}
/* Recursively add children */
for(size_t id: object->children())
addObject(colladaImporter, o, materials, id);
}
}}
MAGNUM_EXAMPLE_MAIN(Magnum::Examples::ViewerExample)