-
Notifications
You must be signed in to change notification settings - Fork 0
/
Viewer.cpp
1076 lines (954 loc) · 33.6 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2011 CNRS and LIRIS' Establishments (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); 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 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
// SPDX-License-Identifier: LGPL-3.0+
//
// Author(s) : Guillaume Damiand <[email protected]>
// Contributor(s): Kumar Snehasish <[email protected]>
//
#include "Viewer.h"
#include <CGAL/Linear_cell_complex_operations.h>
#include <CGAL/bounding_box.h>
#include <CGAL/Qt/CreateOpenGLContext.h>
#include <CGAL/Qt/viewer_actions.h>
#include <QMessageBox>
#include <CGAL/Qt/vec.h>
#include <QDebug>
//Vertex source code
const char vertex_source[] =
{
"#version 120 \n"
"attribute highp vec4 vertex;\n"
"attribute highp vec3 normal;\n"
"attribute highp vec3 color;\n"
"uniform highp mat4 mvp_matrix;\n"
"uniform highp mat4 mv_matrix; \n"
"uniform highp float point_size; \n"
"varying highp vec4 fP; \n"
"varying highp vec3 fN; \n"
"varying highp vec4 fColor; \n"
"void main(void)\n"
"{\n"
" gl_PointSize = point_size; \n"
" fP = mv_matrix * vertex; \n"
" fN = mat3(mv_matrix)* normal; \n"
" fColor = vec4(color, 1.0); \n"
" gl_Position = mvp_matrix * vertex;\n"
"}"
};
//Vertex source code
const char fragment_source[] =
{
"#version 120 \n"
"varying highp vec4 fP; \n"
"varying highp vec3 fN; \n"
"varying highp vec4 fColor; \n"
"uniform vec4 light_pos; \n"
"uniform vec4 light_diff; \n"
"uniform vec4 light_spec; \n"
"uniform vec4 light_amb; \n"
"uniform float spec_power ; \n"
"void main(void) { \n"
" vec3 L = light_pos.xyz - fP.xyz; \n"
" vec3 V = -fP.xyz; \n"
" vec3 N = normalize(fN); \n"
" L = normalize(L); \n"
" V = normalize(V); \n"
" vec3 R = reflect(-L, N); \n"
" vec4 diffuse = max(dot(N,L), 0.0) * light_diff * fColor; \n"
" vec4 specular = pow(max(dot(R,V), 0.0), spec_power) * light_spec; \n"
"gl_FragColor = light_amb*fColor + diffuse ; \n"
"} \n"
"\n"
};
//Vertex source code
const char vertex_source_p_l[] =
{
"#version 120 \n"
"attribute highp vec4 vertex;\n"
"uniform highp mat4 mvp_matrix;\n"
"uniform highp float point_size; \n"
"void main(void)\n"
"{\n"
" gl_PointSize = point_size; \n"
" gl_Position = mvp_matrix * vertex;\n"
"}"
};
//Vertex source code
const char fragment_source_p_l[] =
{
"#version 120 \n"
"uniform highp vec4 color; \n"
"void main(void) { \n"
"gl_FragColor = color; \n"
"} \n"
"\n"
};
Viewer::Viewer(QWidget* parent)
: CGAL::QGLViewer(parent),
wireframe(false),
flatShading(true),
edges(true),
vertices(true),
inverse_normal(false),
size_points(7.),
size_edges(3.1),
ambient(0.6f, 0.5f, 0.5f, 0.5f),
m_previous_scene_empty(true),
are_buffers_initialized(false),
m_face_formatter(new FaceFormatter())
{
}
Viewer::~Viewer()
{
for (int i=0; i<NB_VBO_BUFFERS; ++i)
buffers[i].destroy();
for (int i=0; i<NB_VAO_BUFFERS; ++i)
vao[i].destroy();
}
void Viewer::compile_shaders()
{
rendering_program.removeAllShaders();
rendering_program_p_l.removeAllShaders();
// Create the buffers
for (int i=0; i<NB_VBO_BUFFERS; ++i)
if(!buffers[i].isCreated() && !buffers[i].create())
{
std::cerr<<"VBO Creation number "<<i<<" FAILED"<<std::endl;
}
for (int i=0; i<NB_VAO_BUFFERS; ++i)
if(!vao[i].isCreated() && !vao[i].create())
{
std::cerr<<"VAO Creation number "<<i<<" FAILED"<<std::endl;
}
//The Facets
QOpenGLShader *vertex_shader = new QOpenGLShader(QOpenGLShader::Vertex);
if(!vertex_shader->compileSourceCode(vertex_source))
{
std::cerr<<"Compiling vertex source FAILED"<<std::endl;
}
QOpenGLShader *fragment_shader= new QOpenGLShader(QOpenGLShader::Fragment);
if(!fragment_shader->compileSourceCode(fragment_source))
{
std::cerr<<"Compiling fragmentsource FAILED"<<std::endl;
}
if(!rendering_program.addShader(vertex_shader))
{
std::cerr<<"adding vertex shader FAILED"<<std::endl;
}
if(!rendering_program.addShader(fragment_shader))
{
std::cerr<<"adding fragment shader FAILED"<<std::endl;
}
if(!rendering_program.link())
{
std::cerr<<"linking Program FAILED"<<std::endl;
}
rendering_program.bind();
vertex_shader = new QOpenGLShader(QOpenGLShader::Vertex);
if(!vertex_shader->compileSourceCode(vertex_source_p_l))
{
std::cerr<<"Compiling vertex source FAILED"<<std::endl;
}
fragment_shader= new QOpenGLShader(QOpenGLShader::Fragment);
if(!fragment_shader->compileSourceCode(fragment_source_p_l))
{
std::cerr<<"Compiling fragmentsource FAILED"<<std::endl;
}
if(!rendering_program_p_l.addShader(vertex_shader))
{
std::cerr<<"adding vertex shader FAILED"<<std::endl;
}
if(!rendering_program_p_l.addShader(fragment_shader))
{
std::cerr<<"adding fragment shader FAILED"<<std::endl;
}
if(!rendering_program_p_l.link())
{
std::cerr<<"linking Program FAILED"<<std::endl;
}
rendering_program_p_l.bind();
}
void Viewer::postSelection(const QPoint &point) {
CGAL::qglviewer::Vec orig, dir;
// Compute orig and dir, used to draw a representation of the intersecting
// line
camera()->convertClickToLine(point, orig, dir);
float bestHit = -1.0f;
Volume_info *selected;
Face_cache *selected_face;
for (auto dh = scene->lcc->one_dart_per_cell<2>().begin(); dh != scene->lcc->one_dart_per_cell<2>().end(); dh++)
{
if (!scene->lcc->info<3>(dh).is_visible())
continue;
Face_cache cache = scene->lcc->info<2>(dh);
float newHit = hit(cache, orig, dir);
if (bestHit < 0 || (newHit < bestHit && newHit > 0))
{
bestHit = newHit;
selected = &scene->lcc->info<3>(dh);
selected_face = &scene->lcc->info<2>(dh);
}
}
if (bestHit > 0)
{
displayMessage("Hit on " + QString::fromStdString(selected_face->get_guid()) + " (" + QString::fromStdString(selected_face->get_semantic_surface()) + ")", 15000);
selected->set_selected(!selected->is_selected());
}
sceneChanged();
}
float Viewer::hit(Face_cache object, CGAL::qglviewer::Vec origin, CGAL::qglviewer::Vec direction) {
qreal bestHit = -1.0;
qreal epsilon = 0.000001;
// Moller-Trumbore algorithm for triangle-ray intersection (non-culling)
// u,v are the barycentric coordinates of the intersection point
// t is the distance from rayOrigin to the intersection point
for (auto triangle: *object.triangle_cache()) {
CGAL::qglviewer::Vec vertex[3];
for (int point = 0; point < 3; ++point) {
vertex[point] = CGAL::qglviewer::Vec((triangle.points[point].x),
(triangle.points[point].y),
(triangle.points[point].z));
}
CGAL::qglviewer::Vec edge1 = vertex[1] - vertex[0];
CGAL::qglviewer::Vec edge2 = vertex[2] - vertex[0];
CGAL::qglviewer::Vec pvec = direction ^ edge2;
qreal determinant = edge1 * pvec;
if (determinant > -epsilon && determinant < epsilon) continue; // if determinant is near zero ray lies in plane of triangle
qreal inverseDeterminant = 1.0 / determinant;
CGAL::qglviewer::Vec tvec = origin - vertex[0]; // distance from vertex0 to rayOrigin
qreal u = (tvec * pvec) * inverseDeterminant;
if (u < 0.0 || u > 1.0) continue;
CGAL::qglviewer::Vec qvec = tvec ^ edge1;
qreal v = direction * qvec * inverseDeterminant;
if (v < 0.0 || u + v > 1.0) continue;
qreal t = (edge2 * qvec) * inverseDeterminant;
if (t > epsilon) {
if (t > bestHit) bestHit = t;
}
}
return bestHit;
}
void Viewer::initialize_buffers()
{
//points of the facets
vao[0].bind();
buffers[0].bind();
buffers[0].allocate(pos_facets.data(),
static_cast<int>(pos_facets.size()*sizeof(float)));
vertexLocation[0] = rendering_program.attributeLocation("vertex");
rendering_program.bind();
rendering_program.enableAttributeArray(vertexLocation[0]);
rendering_program.setAttributeBuffer(vertexLocation[0],GL_FLOAT,0,3);
rendering_program.release();
buffers[0].release();
//normals of the facets
buffers[1].bind();
buffers[1].allocate(flat_normals.data(),
static_cast<int>(flat_normals.size()*sizeof(float)));
normalsLocation = rendering_program.attributeLocation("normal");
rendering_program.bind();
rendering_program.enableAttributeArray(normalsLocation);
rendering_program.setAttributeBuffer(normalsLocation,GL_FLOAT,0,3);
buffers[1].release();
//colors of the facets
buffers[2].bind();
buffers[2].allocate(colors.data(),
static_cast<int>(colors.size()*sizeof(float)));
colorsLocation = rendering_program.attributeLocation("color");
rendering_program.bind();
rendering_program.enableAttributeArray(colorsLocation);
rendering_program.setAttributeBuffer(colorsLocation,GL_FLOAT,0,3);
buffers[2].release();
rendering_program.release();
vao[0].release();
vao[1].bind();
//points of the facets
buffers[3].bind();
buffers[3].allocate(pos_facets.data(), static_cast<int>(pos_facets.size()*sizeof(float)));
vertexLocation[0] = rendering_program.attributeLocation("vertex");
rendering_program.bind();
rendering_program.enableAttributeArray(vertexLocation[0]);
rendering_program.setAttributeBuffer(vertexLocation[0],GL_FLOAT,0,3);
rendering_program.release();
buffers[3].release();
//normals of the facets
buffers[4].bind();
buffers[4].allocate(smooth_normals.data(),
static_cast<int>(smooth_normals.size()*sizeof(float)));
normalsLocation = rendering_program.attributeLocation("normal");
rendering_program.bind();
rendering_program.enableAttributeArray(normalsLocation);
rendering_program.setAttributeBuffer(normalsLocation,GL_FLOAT,0,3);
buffers[4].release();
//colors of the facets
buffers[5].bind();
buffers[5].allocate(colors.data(), static_cast<int>(colors.size()*sizeof(float)));
colorsLocation = rendering_program.attributeLocation("color");
rendering_program.bind();
rendering_program.enableAttributeArray(colorsLocation);
rendering_program.setAttributeBuffer(colorsLocation,GL_FLOAT,0,3);
buffers[5].release();
rendering_program.release();
vao[1].release();
//The lines
vao[2].bind();
buffers[6].bind();
buffers[6].allocate(pos_lines.data(), static_cast<int>(pos_lines.size()*sizeof(float)));
vertexLocation[2] = rendering_program_p_l.attributeLocation("vertex");
rendering_program_p_l.bind();
rendering_program_p_l.enableAttributeArray(vertexLocation[2]);
rendering_program_p_l.setAttributeBuffer(vertexLocation[2],GL_FLOAT,0,3);
buffers[6].release();
rendering_program_p_l.release();
vao[2].release();
//The points
vao[3].bind();
buffers[7].bind();
buffers[7].allocate(pos_points.data(), static_cast<int>(pos_points.size()*sizeof(float)));
vertexLocation[2] = rendering_program_p_l.attributeLocation("vertex");
rendering_program_p_l.bind();
rendering_program_p_l.enableAttributeArray(vertexLocation[2]);
rendering_program_p_l.setAttributeBuffer(vertexLocation[2],GL_FLOAT,0,3);
buffers[7].release();
rendering_program_p_l.release();
vao[3].release();
are_buffers_initialized = true;
}
void Viewer::compute_face(Dart_handle dh, LCC::size_type markface)
{
LCC &lcc = *scene->lcc;
CGAL::mark_cell<LCC, 2>(lcc, dh, markface);
auto *volume_info = &lcc.info<3>(dh);
if (lcc.attribute<2>(dh)==NULL )
scene->lcc->set_attribute<2>(dh, scene->lcc->create_attribute<2>());
auto *face_info = &lcc.info<2>(dh);
double r = (double)volume_info->color().r()/255.0;
double g = (double)volume_info->color().g()/255.0;
double b = (double)volume_info->color().b()/255.0;
if ( !lcc.is_free(dh, 3) )
{
r += (double)lcc.info<3>(lcc.beta(dh,3)).color().r()/255.0;
g += (double)lcc.info<3>(lcc.beta(dh,3)).color().g()/255.0;
b += (double)lcc.info<3>(lcc.beta(dh,3)).color().b()/255.0;
r /= 2; g /= 2; b /= 2;
}
m_face_formatter->getColor(face_info, r, g, b);
if (volume_info->is_selected())
{
r = 1;
g = 1;
b = 0;
}
//compute flat normals
LCC::Vector normal = CGAL::compute_normal_of_cell_2(lcc,dh);
normal = normal/(CGAL::sqrt(normal*normal));
if (inverse_normal)
normal=normal*-1;
if (isnan(normal.x()))
return;
if (!face_info->has_triangle_cache())
{
if (lcc.beta<1,1,1>(dh)!=dh)
{
try // Try catch to avoir crash of triangulation
{
P_traits cdt_traits(normal);
CDT* cdt = new CDT(cdt_traits);
// Iterates on the vector of facet handles
CDT::Vertex_handle previous = NULL, first = NULL;
for (LCC::Dart_of_orbit_range<1>::const_iterator
he_circ = lcc.darts_of_orbit<1>(dh).begin(),
he_circ_end = lcc.darts_of_orbit<1>(dh).end();
he_circ!=he_circ_end; ++he_circ)
{
CDT::Vertex_handle vh = cdt->insert(lcc.point(he_circ));
if(first == NULL)
{ first = vh; }
vh->info().v = CGAL::compute_normal_of_cell_0<LCC>(lcc, he_circ);
if (inverse_normal) vh->info().v=vh->info().v*-1;
if(previous!=NULL && previous != vh)
{ cdt->insert_constraint(previous, vh); }
previous = vh;
}
if (previous!=NULL)
cdt->insert_constraint(previous, first);
// sets mark is_external
for(CDT::All_faces_iterator fit = cdt->all_faces_begin(),
fitend = cdt->all_faces_end(); fit!=fitend; ++fit)
{
fit->info().is_external = true;
fit->info().is_process = false;
}
//check if the facet is external or internal
std::queue<CDT::Face_handle> face_queue;
CDT::Face_handle face_internal = NULL;
face_queue.push(cdt->infinite_vertex()->face());
while(! face_queue.empty() )
{
CDT::Face_handle fh = face_queue.front();
face_queue.pop();
if(!fh->info().is_process)
{
fh->info().is_process = true;
for(int i = 0; i <3; ++i)
{
if(!cdt->is_constrained(std::make_pair(fh, i)))
{
face_queue.push(fh->neighbor(i));
}
else if (face_internal==NULL)
{
face_internal = fh->neighbor(i);
}
}
}
}
if ( face_internal!=NULL )
face_queue.push(face_internal);
while(! face_queue.empty() )
{
CDT::Face_handle fh = face_queue.front();
face_queue.pop();
if(!fh->info().is_process)
{
fh->info().is_process = true;
fh->info().is_external = false;
for(int i = 0; i <3; ++i)
{
if(!cdt->is_constrained(std::make_pair(fh, i)))
{
face_queue.push(fh->neighbor(i));
}
}
}
}
for(CDT::Finite_faces_iterator ffit = cdt->finite_faces_begin(),
ffitend = cdt->finite_faces_end(); ffit != ffitend; ++ffit)
{
if(!ffit->info().is_external)
{
CacheTriangle triangle;
for (int i =0; i<3; i++)
{
triangle.normals[i].x = ffit->vertex(i)->info().v.x();
triangle.normals[i].y = ffit->vertex(i)->info().v.y();
triangle.normals[i].z = ffit->vertex(i)->info().v.z();
triangle.points[i].x = ffit->vertex(i)->point().x();
triangle.points[i].y = ffit->vertex(i)->point().y();
triangle.points[i].z = ffit->vertex(i)->point().z();
}
face_info->triangle_cache()->push_back(triangle);
}
}
}
catch(...)
{ // Triangulation crash: the face is not filled
}
}
else
{ // The face is a triangle
colors.push_back(r);colors.push_back(g);colors.push_back(b);
colors.push_back(r);colors.push_back(g);colors.push_back(b);
colors.push_back(r);colors.push_back(g);colors.push_back(b);
flat_normals.push_back(normal.x());
flat_normals.push_back(normal.y());
flat_normals.push_back(normal.z());
flat_normals.push_back(normal.x());
flat_normals.push_back(normal.y());
flat_normals.push_back(normal.z());
flat_normals.push_back(normal.x());
flat_normals.push_back(normal.y());
flat_normals.push_back(normal.z());
CacheTriangle triangle;
int i = 0;
for (LCC::Dart_of_orbit_range<1>::const_iterator
orbitIter = lcc.darts_of_orbit<1>(dh).begin();
orbitIter.cont(); ++orbitIter)
{
//compute Smooth normals
LCC::Vector normal = CGAL::compute_normal_of_cell_0(lcc,orbitIter);
normal = normal/(CGAL::sqrt(normal*normal));
if (inverse_normal) normal=normal*-1;
smooth_normals.push_back(normal.x());
smooth_normals.push_back(normal.y());
smooth_normals.push_back(normal.z());
const LCC::Point& p = lcc.point(orbitIter);
pos_facets.push_back(p.x());
pos_facets.push_back(p.y());
pos_facets.push_back(p.z());
triangle.normals[i].x = normal.x();
triangle.normals[i].y = normal.y();
triangle.normals[i].z = normal.z();
triangle.points[i].x = p.x();
triangle.points[i].y = p.y();
triangle.points[i].z = p.z();
i++;
}
face_info->triangle_cache()->push_back(triangle);
}
}
//iterates on the internal faces to add the vertices to the positions
//and the normals to the appropriate vectors
for(auto ffit = face_info->triangle_cache()->begin(),
ffitend = face_info->triangle_cache()->end(); ffit != ffitend; ++ffit)
{
flat_normals.push_back(normal.x());
flat_normals.push_back(normal.y());
flat_normals.push_back(normal.z());
flat_normals.push_back(normal.x());
flat_normals.push_back(normal.y());
flat_normals.push_back(normal.z());
flat_normals.push_back(normal.x());
flat_normals.push_back(normal.y());
flat_normals.push_back(normal.z());
smooth_normals.push_back(ffit->normals[0].x);
smooth_normals.push_back(ffit->normals[0].y);
smooth_normals.push_back(ffit->normals[0].z);
smooth_normals.push_back(ffit->normals[1].x);
smooth_normals.push_back(ffit->normals[1].y);
smooth_normals.push_back(ffit->normals[1].z);
smooth_normals.push_back(ffit->normals[2].x);
smooth_normals.push_back(ffit->normals[2].y);
smooth_normals.push_back(ffit->normals[2].z);
pos_facets.push_back(ffit->points[0].x);
pos_facets.push_back(ffit->points[0].y);
pos_facets.push_back(ffit->points[0].z);
pos_facets.push_back(ffit->points[1].x);
pos_facets.push_back(ffit->points[1].y);
pos_facets.push_back(ffit->points[1].z);
pos_facets.push_back(ffit->points[2].x);
pos_facets.push_back(ffit->points[2].y);
pos_facets.push_back(ffit->points[2].z);
colors.push_back(r);colors.push_back(g);colors.push_back(b);
colors.push_back(r);colors.push_back(g);colors.push_back(b);
colors.push_back(r);colors.push_back(g);colors.push_back(b);
}
}
void Viewer::compute_edge(Dart_handle dh, LCC::size_type markedge)
{
LCC &lcc = *scene->lcc;
CGAL::mark_cell<LCC, 1>(lcc, dh, markedge);
const LCC::Point& p = lcc.point(dh);
Dart_handle d2 = lcc.other_extremity(dh);
if ( d2!=NULL )
{
const LCC::Point& p2 = lcc.point(d2);
pos_lines.push_back(p.x());
pos_lines.push_back(p.y());
pos_lines.push_back(p.z());
pos_lines.push_back(p2.x());
pos_lines.push_back(p2.y());
pos_lines.push_back(p2.z());
}
}
void Viewer::compute_vertex(Dart_handle dh, LCC::size_type markvertex, bool& empty)
{
LCC &lcc = *scene->lcc;
CGAL::mark_cell<LCC, 0>(lcc, dh, markvertex);
const LCC::Point& p = lcc.point(dh);
pos_points.push_back(p.x());
pos_points.push_back(p.y());
pos_points.push_back(p.z());
if ( empty )
{
bb = p.bbox();
empty = false;
}
else
bb = bb + p.bbox();
}
void Viewer::compute_elements()
{
LCC &lcc = *scene->lcc;
pos_facets.clear();
flat_normals.clear();
smooth_normals.clear();
colors.clear();
pos_lines.clear();
pos_points.clear();
if ( lcc.is_empty() )
{
bb = LCC::Point(CGAL::ORIGIN).bbox();
bb = bb + LCC::Point(1,1,1).bbox(); // To avoid a warning from Qglviewer
return;
}
LCC::size_type markvertex = lcc.get_new_mark();
LCC::size_type markedge = lcc.get_new_mark();
LCC::size_type markface = lcc.get_new_mark();
bool empty = true;
for (LCC::Attribute_range<3>::type::iterator it=lcc.attributes<3>().begin(),
itend=lcc.attributes<3>().end(); it!=itend; ++it )
{
if ( it->info().is_visible() )
{
for(LCC::Dart_of_cell_range<3>::iterator
dartIter=lcc.darts_of_cell<3>(lcc.dart_of_attribute<3>(it)).begin();
dartIter.cont(); ++dartIter)
{
if ( it->info().is_filled() && !lcc.is_marked(dartIter, markface) )
compute_face(dartIter, markface);
if ( !lcc.is_marked(dartIter, markedge) )
compute_edge(dartIter, markedge);
if ( !lcc.is_marked(dartIter, markvertex) )
compute_vertex(dartIter, markvertex, empty);
}
}
}
if ( empty )
{
bb = LCC::Point(CGAL::ORIGIN).bbox();
bb = bb + LCC::Point(1,1,1).bbox(); // To avoid a warning from Qglviewer
}
for (LCC::Dart_range::iterator it=lcc.darts().begin(),
itend=lcc.darts().end(); it!=itend; ++it )
{
lcc.unmark(it, markvertex);
lcc.unmark(it, markedge);
lcc.unmark(it, markface);
}
lcc.free_mark(markvertex);
lcc.free_mark(markedge);
lcc.free_mark(markface);
}
void Viewer::attrib_buffers(CGAL::QGLViewer* viewer)
{
QMatrix4x4 mvpMatrix;
QMatrix4x4 mvMatrix;
double mat[16];
viewer->camera()->getModelViewProjectionMatrix(mat);
for(int i=0; i < 16; i++)
{
mvpMatrix.data()[i] = (float)mat[i];
}
viewer->camera()->getModelViewMatrix(mat);
for(int i=0; i < 16; i++)
{
mvMatrix.data()[i] = (float)mat[i];
}
// define material
QVector4D diffuse( 0.9f,
0.9f,
0.9f,
0.9f );
QVector4D specular( 0.0f,
0.0f,
0.0f,
1.0f );
QVector4D position((bb.xmax()-bb.xmin())/2, (bb.ymax()-bb.ymin())/2,bb.zmax(), 0.0 );
GLfloat shininess = 1.0f;
rendering_program.bind();
mvpLocation[0] = rendering_program.uniformLocation("mvp_matrix");
mvLocation = rendering_program.uniformLocation("mv_matrix");
lightLocation[0] = rendering_program.uniformLocation("light_pos");
lightLocation[1] = rendering_program.uniformLocation("light_diff");
lightLocation[2] = rendering_program.uniformLocation("light_spec");
lightLocation[3] = rendering_program.uniformLocation("light_amb");
lightLocation[4] = rendering_program.uniformLocation("spec_power");
rendering_program.setUniformValue(lightLocation[0], position);
rendering_program.setUniformValue(lightLocation[1], diffuse);
rendering_program.setUniformValue(lightLocation[2], specular);
rendering_program.setUniformValue(lightLocation[3], ambient);
rendering_program.setUniformValue(lightLocation[4], shininess);
rendering_program.setUniformValue(mvpLocation[0], mvpMatrix);
rendering_program.setUniformValue(mvLocation, mvMatrix);
rendering_program.release();
rendering_program_p_l.bind();
mvpLocation[1] = rendering_program_p_l.uniformLocation("mvp_matrix");
colorLocation = rendering_program_p_l.uniformLocation("color");
rendering_program.setUniformValue(mvpLocation[1], mvpMatrix);
rendering_program_p_l.release();
}
void Viewer::sceneChanged()
{
compute_elements();
this->camera()->setSceneBoundingBox(CGAL::qglviewer::Vec(bb.xmin(),
bb.ymin(),
bb.zmin()),
CGAL::qglviewer::Vec(bb.xmax(),
bb.ymax(),
bb.zmax()));
are_buffers_initialized = false;
if (m_previous_scene_empty)
this->showEntireScene();
else
this->update();
m_previous_scene_empty = scene->lcc->is_empty(); // for the next call to sceneChanged
}
void Viewer::draw()
{
if(scene)
{
glEnable(GL_DEPTH_TEST);
if(!are_buffers_initialized)
initialize_buffers();
QColor color;
if ( !wireframe )
{
if(flatShading)
{
vao[0].bind();
attrib_buffers(this);
rendering_program.bind();
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(pos_facets.size()/3));
rendering_program.release();
vao[0].release();
}
else
{
vao[1].bind();
attrib_buffers(this);
rendering_program.bind();
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(pos_facets.size()/3));
rendering_program.release();
vao[1].release();
}
}
if(edges)
{
vao[2].bind();
attrib_buffers(this);
color.setRgbF(0.2f, 0.2f, 0.7f);
rendering_program_p_l.bind();
rendering_program_p_l.setAttributeValue(colorLocation,color);
glLineWidth(size_edges);
glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(pos_lines.size()/3));
rendering_program_p_l.release();
vao[2].release();
}
if(vertices)
{
vao[3].bind();
attrib_buffers(this);
color.setRgbF(.2f,.2f,.6f);
rendering_program_p_l.bind();
rendering_program_p_l.setAttributeValue(colorLocation,color);
rendering_program_p_l.setUniformValue("point_size", GLfloat(size_points));
glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(pos_points.size()/3));
rendering_program_p_l.release();
vao[3].release();
}
}
}
void Viewer::set_semantic_formatting(FaceFormatter* new_formatter)
{
delete m_face_formatter;
m_face_formatter = new_formatter;
}
void Viewer::init()
{
// Restore previous viewer state.
restoreStateFromFile();
initializeOpenGLFunctions();
// Define 'Control+Q' as the new exit shortcut (default was 'Escape')
setShortcut(CGAL::qglviewer::EXIT_VIEWER, Qt::CTRL+Qt::Key_Q);
// Add custom key description (see keyPressEvent).
setKeyDescription(Qt::Key_W, "Toggles wire frame display");
setKeyDescription(Qt::Key_F, "Toggles flat shading display");
setKeyDescription(Qt::Key_E, "Toggles edges display");
setKeyDescription(Qt::Key_V, "Toggles vertices display");
setKeyDescription(Qt::Key_N, "Inverse direction of normals");
setKeyDescription(Qt::Key_Plus, "Increase size of edges");
setKeyDescription(Qt::Key_Minus, "Decrease size of edges");
setKeyDescription(Qt::Key_Plus+Qt::ShiftModifier, "Increase size of vertices");
setKeyDescription(Qt::Key_Minus+Qt::ShiftModifier, "Decrease size of vertices");
setKeyDescription(Qt::Key_PageDown, "Increase light (all colors, use shift/alt/ctrl for one rgb component)");
setKeyDescription(Qt::Key_PageUp, "Decrease light (all colors, use shift/alt/ctrl for one rgb component)");
// Light default parameters
glLineWidth(size_edges);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f,1.0f);
glClearColor(1.0f,1.0f,1.0f,0.0f);
glDisable(GL_BLEND);
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POLYGON_SMOOTH_HINT);
glBlendFunc(GL_ONE, GL_ZERO);
glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST);
compile_shaders();
}
void Viewer::keyPressEvent(QKeyEvent *e)
{
const Qt::KeyboardModifiers modifiers = e->modifiers();
if ((e->key()==Qt::Key_W) && (modifiers==Qt::NoButton))
{
wireframe = !wireframe;
if (wireframe)
{
displayMessage("Wireframe.");
}
else
{
displayMessage("Filled faces.");
}
update();
}
else if ((e->key()==Qt::Key_F) && (modifiers==Qt::NoButton))
{
flatShading = !flatShading;
if (flatShading)
displayMessage("Flat shading.");
else
displayMessage("Gouraud shading.");
update();
}
else if ((e->key()==Qt::Key_E) && (modifiers==Qt::NoButton))
{
edges = !edges;
displayMessage(QString("Draw edges=%1.").arg(edges?"true":"false"));
update();
}
else if ((e->key()==Qt::Key_V) && (modifiers==Qt::NoButton))
{
vertices = !vertices;
displayMessage(QString("Draw vertices=%1.").arg(vertices?"true":"false"));
update();
}
else if ((e->key()==Qt::Key_N) && (modifiers==Qt::NoButton))
{
inverse_normal = !inverse_normal;
displayMessage(QString("Inverse normal=%1.").arg(inverse_normal?"true":"false"));
sceneChanged();
}
else if ((e->key()==Qt::Key_Plus) && (modifiers==Qt::KeypadModifier))
{
size_edges+=.5;
displayMessage(QString("Size of edges=%1.").arg(size_edges));
update();
}
else if ((e->key()==Qt::Key_Minus) && (modifiers==Qt::KeypadModifier))
{
if (size_edges>.5) size_edges-=.5;
displayMessage(QString("Size of edges=%1.").arg(size_edges));
update();
}
else if ((e->key()==Qt::Key_Plus) && (modifiers==(Qt::ShiftModifier|Qt::KeypadModifier)))
{
size_points+=.5;
displayMessage(QString("Size of points=%1.").arg(size_points));
update();
}
else if ((e->key()==Qt::Key_Minus) && (modifiers==(Qt::ShiftModifier|Qt::KeypadModifier)))
{
if (size_points>.5) size_points-=.5;
displayMessage(QString("Size of points=%1.").arg(size_points));
update();
}
else if ((e->key()==Qt::Key_PageUp) && (modifiers==Qt::NoButton))
{
ambient.setX(ambient.x()+.1);
if (ambient.x()>1.) ambient.setX(1.);
ambient.setY(ambient.x()+.1);
if (ambient.y()>1.) ambient.setY(1.);
ambient.setZ(ambient.x()+.1);
if (ambient.z()>1.) ambient.setZ(1.);
displayMessage(QString("Light color=(%1 %2 %3).").
arg(ambient.x()).arg(ambient.y()).arg(ambient.z()));
update();
}
else if ((e->key()==Qt::Key_PageDown) && (modifiers==Qt::NoButton))
{
ambient.setX(ambient.x()-.1);
if (ambient.x()<0.) ambient.setX(0.);
ambient.setY(ambient.y()-.1);
if (ambient.y()<0.) ambient.setY(0.);
ambient.setZ(ambient.z()-.1);
if (ambient.z()<0.) ambient.setZ(0.);
displayMessage(QString("Light color=(%1 %2 %3).").
arg(ambient.x()).arg(ambient.y()).arg(ambient.z()));
update();
}
else if ((e->key()==Qt::Key_PageUp) && (modifiers==Qt::ShiftModifier))
{
ambient.setX(ambient.x()+.1);
if (ambient.x()>1.) ambient.setX(1.);
displayMessage(QString("Light color=(%1 %2 %3).").
arg(ambient.x()).arg(ambient.y()).arg(ambient.z()));
update();
}