-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewer.cpp
382 lines (319 loc) · 11.8 KB
/
viewer.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* Copyright (C) 2015, IGG Group, ICube, University of Strasbourg, France *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library 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 *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.unistra.fr/ *
* Contact information: [email protected] *
* *
*******************************************************************************/
#include <QApplication>
#include <QMatrix4x4>
#include <QKeyEvent>
#include <QOGLViewer/qoglviewer.h>
#include <cgogn/core/cmap/cmap2.h>
//#include <cgogn/core/cmap/cmap2_tri.h>
//#include <cgogn/core/cmap/cmap2_quad.h>
#include <cgogn/io/map_import.h>
#include <cgogn/io/map_export.h>
#include <cgogn/geometry/algos/bounding_box.h>
#include <cgogn/geometry/algos/normal.h>
#include <cgogn/rendering/map_render.h>
#include <cgogn/rendering/shaders/shader_simple_color.h>
#include <cgogn/rendering/shaders/shader_flat.h>
#include <cgogn/rendering/shaders/shader_phong.h>
#include <cgogn/rendering/shaders/shader_vector_per_vertex.h>
#include <cgogn/rendering/shaders/vbo.h>
#include <cgogn/rendering/shaders/shader_bold_line.h>
#include <cgogn/rendering/shaders/shader_point_sprite.h>
#include <cgogn/rendering/shaders/shader_round_point.h>
#include <cgogn/geometry/algos/ear_triangulation.h>
#include <cgogn/rendering/drawer.h>
#include <mnt_io.h>
#include <color_maps.h>
#define DEFAULT_MESH_PATH CGOGN_STR(CGOGN_TEST_MESHES_PATH)
using Map2 = cgogn::CMap2;
//using Map2 = cgogn::CMap2Tri;
//using Map2 = cgogn::CMap2Quad;
using Vec3 = Eigen::Vector3d;
//using Vec3 = cgogn::geometry::Vec_T<std::array<double,3>>;
template <typename T>
using VertexAttribute = Map2::VertexAttribute<T>;
class Viewer : public QOGLViewer
{
public:
Viewer();
CGOGN_NOT_COPYABLE_NOR_MOVABLE(Viewer);
virtual void draw();
virtual void init();
virtual void keyPressEvent(QKeyEvent *);
void import(const std::string& surface_mesh);
virtual ~Viewer();
virtual void closeEvent(QCloseEvent *e);
private:
Map2 map_;
VertexAttribute<Vec3> vertex_position_;
VertexAttribute<Vec3> vertex_normal_;
cgogn::geometry::AABB<Vec3> bb_;
std::unique_ptr<cgogn::rendering::MapRender> render_;
std::unique_ptr<cgogn::rendering::VBO> vbo_pos_;
std::unique_ptr<cgogn::rendering::VBO> vbo_norm_;
std::unique_ptr<cgogn::rendering::VBO> vbo_color_;
std::unique_ptr<cgogn::rendering::VBO> vbo_sphere_sz_;
std::unique_ptr<cgogn::rendering::ShaderBoldLine::Param> param_edge_;
std::unique_ptr<cgogn::rendering::ShaderFlat::Param> param_flat_;
std::unique_ptr<cgogn::rendering::ShaderVectorPerVertex::Param> param_normal_;
std::unique_ptr<cgogn::rendering::ShaderPhongColor::Param> param_phong_;
std::unique_ptr<cgogn::rendering::ShaderPointSpriteColorSize::Param> param_point_sprite_;
std::unique_ptr<cgogn::rendering::DisplayListDrawer> drawer_;
std::unique_ptr<cgogn::rendering::DisplayListDrawer::Renderer> drawer_rend_;
bool phong_rendering_;
bool flat_rendering_;
bool vertices_rendering_;
bool edge_rendering_;
bool normal_rendering_;
bool bb_rendering_;
};
//
// IMPLEMENTATION
//
void Viewer::import(const std::string& surface_mesh)
{
cgogn::io::import_height_map<Vec3>(map_, surface_mesh);
vertex_position_ = map_.template get_attribute<Vec3, Map2::Vertex>("position");
if (!vertex_position_.is_valid())
{
cgogn_log_error("Viewer::import") << "Missing attribute position. Aborting.";
std::exit(EXIT_FAILURE);
}
vertex_normal_ = map_.template add_attribute<Vec3, Map2::Vertex>("normal");
cgogn::geometry::compute_normal<Vec3>(map_, vertex_position_, vertex_normal_);
cgogn::geometry::compute_AABB(vertex_position_, bb_);
setSceneRadius(bb_.diag_size()/2.0);
Vec3 center = bb_.center();
setSceneCenter(qoglviewer::Vec(center[0], center[1], center[2]));
showEntireScene();
}
Viewer::~Viewer()
{}
void Viewer::closeEvent(QCloseEvent*)
{
render_.reset();
vbo_pos_.reset();
vbo_norm_.reset();
vbo_color_.reset();
vbo_sphere_sz_.reset();
drawer_.reset();
drawer_rend_.reset();
}
Viewer::Viewer() :
map_(),
vertex_position_(),
vertex_normal_(),
bb_(),
render_(nullptr),
vbo_pos_(nullptr),
vbo_norm_(nullptr),
vbo_color_(nullptr),
vbo_sphere_sz_(nullptr),
drawer_(nullptr),
drawer_rend_(nullptr),
phong_rendering_(true),
flat_rendering_(false),
vertices_rendering_(false),
edge_rendering_(false),
normal_rendering_(false),
bb_rendering_(true)
{}
void Viewer::keyPressEvent(QKeyEvent *ev)
{
switch (ev->key())
{
case Qt::Key_P:
phong_rendering_ = true;
flat_rendering_ = false;
break;
case Qt::Key_F:
flat_rendering_ = true;
phong_rendering_ = false;
break;
case Qt::Key_N:
normal_rendering_ = !normal_rendering_;
break;
case Qt::Key_E:
edge_rendering_ = !edge_rendering_;
break;
case Qt::Key_V:
vertices_rendering_ = !vertices_rendering_;
break;
case Qt::Key_B:
bb_rendering_ = !bb_rendering_;
break;
case Qt::Key_X:
{
const cgogn::Orbit vertorb = Map2::Vertex::ORBIT;
cgogn::io::export_surface(map_, cgogn::io::ExportOptions("moderbach.off", {vertorb, "position"}, {}, false, false));
break;
}
default:
break;
}
// enable QGLViewer keys
QOGLViewer::keyPressEvent(ev);
//update drawing
update();
}
void Viewer::draw()
{
QMatrix4x4 proj;
QMatrix4x4 view;
camera()->getProjectionMatrix(proj);
camera()->getModelViewMatrix(view);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f, 2.0f);
if (flat_rendering_)
{
param_flat_->bind(proj,view);
render_->draw(cgogn::rendering::TRIANGLES);
param_flat_->release();
}
if (phong_rendering_)
{
param_phong_->bind(proj,view);
render_->draw(cgogn::rendering::TRIANGLES);
param_phong_->release();
}
glDisable(GL_POLYGON_OFFSET_FILL);
if (vertices_rendering_)
{
param_point_sprite_->bind(proj,view);
render_->draw(cgogn::rendering::POINTS);
param_point_sprite_->release();
}
if (edge_rendering_)
{
param_edge_->bind(proj,view);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
render_->draw(cgogn::rendering::LINES);
glDisable(GL_BLEND);
param_edge_->release();
}
if (normal_rendering_)
{
param_normal_->bind(proj,view);
render_->draw(cgogn::rendering::POINTS);
param_normal_->release();
}
if (bb_rendering_)
drawer_rend_->draw(proj,view,this);
}
void Viewer::init()
{
glClearColor(0.1f,0.1f,0.3f,0.0f);
// create and fill VBO for positions
vbo_pos_ = cgogn::make_unique<cgogn::rendering::VBO>(3);
cgogn::rendering::update_vbo(vertex_position_, vbo_pos_.get());
// create and fill VBO for normals
vbo_norm_ = cgogn::make_unique<cgogn::rendering::VBO>(3);
cgogn::rendering::update_vbo(vertex_normal_, vbo_norm_.get());
// fill a color vbo with abs of normals
vbo_color_ = cgogn::make_unique<cgogn::rendering::VBO>(3);
cgogn::rendering::update_vbo(vertex_position_, vbo_color_.get(), [] (const Vec3& n) -> std::array<float,3>
{
return cgogn::color_map_BCGYR(n[2]);
});
// fill a sphere size vbo
vbo_sphere_sz_ = cgogn::make_unique<cgogn::rendering::VBO>(1);
cgogn::rendering::update_vbo(vertex_position_, vbo_sphere_sz_.get(), [&] (const Vec3& n) -> float
{
return bb_.diag_size()/1000.0 * (1.0 + 2.0*std::abs(n[2]));
});
// map rendering object (primitive creation & sending to GPU)
render_ = cgogn::make_unique<cgogn::rendering::MapRender>();
render_->init_primitives(map_, cgogn::rendering::POINTS);
render_->init_primitives(map_, cgogn::rendering::LINES);
render_->init_primitives<Vec3>(map_, cgogn::rendering::TRIANGLES, &vertex_position_);
// generation of one parameter set (for this shader) : vbo + uniforms
param_point_sprite_ = cgogn::rendering::ShaderPointSpriteColorSize::generate_param();
// set vbo param (see param::set_vbo signature)
param_point_sprite_->set_all_vbos(vbo_pos_.get(), vbo_color_.get(), vbo_sphere_sz_.get());
// set uniforms data
param_edge_ = cgogn::rendering::ShaderBoldLine::generate_param();
param_edge_->set_position_vbo(vbo_pos_.get());
param_edge_->color_ = QColor(255,255,0);
param_edge_->width_= 2.5f;
param_flat_ = cgogn::rendering::ShaderFlat::generate_param();
param_flat_->set_position_vbo(vbo_pos_.get());
param_flat_->front_color_ = QColor(0,200,0);
param_flat_->back_color_ = QColor(0,0,200);
param_flat_->ambiant_color_ = QColor(5,5,5);
param_normal_ = cgogn::rendering::ShaderVectorPerVertex::generate_param();
param_normal_->set_all_vbos(vbo_pos_.get(), vbo_norm_.get());
param_normal_->color_ = QColor(200,0,200);
param_normal_->length_ = bb_.diag_size()/50;
param_phong_ = cgogn::rendering::ShaderPhongColor::generate_param();
param_phong_->set_all_vbos(vbo_pos_.get(), vbo_norm_.get(), vbo_color_.get());
// drawer for simple old-school g1 rendering
drawer_ = cgogn::make_unique<cgogn::rendering::DisplayListDrawer>();
drawer_rend_= drawer_->generate_renderer();
drawer_->new_list();
drawer_->line_width_aa(2.0);
drawer_->begin(GL_LINE_LOOP);
drawer_->color3f(1.0,1.0,1.0);
drawer_->vertex3f(bb_.min()[0],bb_.min()[1],bb_.min()[2]);
drawer_->vertex3f(bb_.max()[0],bb_.min()[1],bb_.min()[2]);
drawer_->vertex3f(bb_.max()[0],bb_.max()[1],bb_.min()[2]);
drawer_->vertex3f(bb_.min()[0],bb_.max()[1],bb_.min()[2]);
drawer_->vertex3f(bb_.min()[0],bb_.max()[1],bb_.max()[2]);
drawer_->vertex3f(bb_.max()[0],bb_.max()[1],bb_.max()[2]);
drawer_->vertex3f(bb_.max()[0],bb_.min()[1],bb_.max()[2]);
drawer_->vertex3f(bb_.min()[0],bb_.min()[1],bb_.max()[2]);
drawer_->end();
drawer_->begin(GL_LINES);
drawer_->color3f(1.0,1.0,1.0);
drawer_->vertex3f(bb_.min()[0],bb_.min()[1],bb_.min()[2]);
drawer_->vertex3f(bb_.min()[0],bb_.max()[1],bb_.min()[2]);
drawer_->vertex3f(bb_.min()[0],bb_.min()[1],bb_.max()[2]);
drawer_->vertex3f(bb_.min()[0],bb_.max()[1],bb_.max()[2]);
drawer_->vertex3f(bb_.max()[0],bb_.min()[1],bb_.min()[2]);
drawer_->vertex3f(bb_.max()[0],bb_.min()[1],bb_.max()[2]);
drawer_->vertex3f(bb_.max()[0],bb_.max()[1],bb_.min()[2]);
drawer_->vertex3f(bb_.max()[0],bb_.max()[1],bb_.max()[2]);
drawer_->end();
drawer_->end_list();
}
int main(int argc, char** argv)
{
std::string surface_mesh;
if (argc < 2)
{
cgogn_log_info("simple_viewer") << "USAGE: " << argv[0] << " [filename]";
surface_mesh = std::string(DEFAULT_MESH_PATH) + std::string("off/aneurysm_3D.off");
cgogn_log_info("simple_viewer") << "Using default mesh \"" << surface_mesh << "\".";
}
else
surface_mesh = std::string(argv[1]);
QApplication application(argc, argv);
qoglviewer::init_ogl_context();
// Instantiate the viewer.
Viewer viewer;
viewer.setWindowTitle("simple_viewer");
viewer.import(surface_mesh);
viewer.show();
// Run main loop.
return application.exec();
}