Subversion Repositories gelsvn

Rev

Rev 667 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 667 Rev 676
Line 3... Line 3...
3
//  GEL
3
//  GEL
4
//
4
//
5
//  Created by J. Andreas Bærentzen on 09/10/13.
5
//  Created by J. Andreas Bærentzen on 09/10/13.
6
//
6
//
7
//
7
//
8
 
-
 
-
 
8
#include <thread>
9
#define _CRT_SECURE_NO_WARNINGS
9
#include <unistd.h>
10
 
-
 
11
#include <GL/glew.h>
10
#include <GL/glew.h>
12
#include <functional>
11
#include <functional>
13
#include "MeshEditor.h"
12
#include "MeshEditor.h"
14
#include <string>
13
#include <string>
15
#include <iostream>
14
#include <iostream>
16
#include <vector>
15
#include <vector>
17
#include <algorithm>
16
#include <algorithm>
18
#include <queue>
17
#include <queue>
19
 
18
 
20
#include <GLGraphics/Console.h>
19
#include <GLGraphics/Console.h>
-
 
20
#include <GLGraphics/glsl_shader.h>
-
 
21
#include <GLGraphics/ShadowBuffer.h>
21
 
22
 
22
#include <CGLA/eigensolution.h>
23
#include <CGLA/eigensolution.h>
23
#include <CGLA/Vec2d.h>
24
#include <CGLA/Vec2d.h>
24
#include <CGLA/Vec3d.h>
25
#include <CGLA/Vec3d.h>
25
#include <CGLA/Mat3x3d.h>
26
#include <CGLA/Mat3x3d.h>
Line 159... Line 160...
159
            VertexAttributeVector<Mat3x3d> curvature_tensors(mani.allocated_vertices());
160
            VertexAttributeVector<Mat3x3d> curvature_tensors(mani.allocated_vertices());
160
            VertexAttributeVector<Vec3d> min_curv_direction(mani.allocated_vertices());
161
            VertexAttributeVector<Vec3d> min_curv_direction(mani.allocated_vertices());
161
            VertexAttributeVector<Vec3d> max_curv_direction(mani.allocated_vertices());
162
            VertexAttributeVector<Vec3d> max_curv_direction(mani.allocated_vertices());
162
            VertexAttributeVector<Vec2d> curvature(mani.allocated_vertices());
163
            VertexAttributeVector<Vec2d> curvature(mani.allocated_vertices());
163
            
164
            
164
            //    curvature_tensors_from_edges(mani, curvature_tensors);
-
 
165
            //    for(int i=0;i<3; ++i)
-
 
166
            //        smooth_curvature_tensors(mani,curvature_tensors);
-
 
167
            //    curvature_from_tensors(mani, curvature_tensors,
-
 
168
            //                        min_curv_direction,
-
 
169
            //                        max_curv_direction,
-
 
170
            //                        curvature);
-
 
171
            
-
 
172
            //    void console_refit_polar(MeshEditor* me, const std::vector<std::string> & args)
-
 
173
            //    {
-
 
174
            //        if(wantshelp(args)) {
-
 
175
            //            me->printf("usage: simplify.polar <mesh 1> <mesh 2> <iter>");
-
 
176
            //            return;
-
 
177
            //        }
-
 
178
            //        int m1=1;
-
 
179
            //        int m2=2;
-
 
180
            //        int iter=1;
-
 
181
            //        int dim = 64;
-
 
182
            //        if(args.size() > 0){
-
 
183
            //            istringstream a0(args[0]);
-
 
184
            //            a0 >> m1;
-
 
185
            //        }
-
 
186
            //        if(args.size() > 1){
-
 
187
            //            istringstream a0(args[1]);
-
 
188
            //            a0 >> m2;
-
 
189
            //        }
-
 
190
            //        if(args.size() > 2){
-
 
191
            //            istringstream a0(args[2]);
-
 
192
            //            a0 >> iter;
-
 
193
            //        }
-
 
194
            //
-
 
195
            //        if(args.size() > 3){
-
 
196
            //            istringstream a0(args[3]);
-
 
197
            //            a0 >> dim;
-
 
198
            //        }
-
 
199
            //
-
 
200
            //
-
 
201
            //
-
 
202
            //        me->save_active_mesh();
-
 
203
            //
-
 
204
            //        smooth_and_refit(get_vis_obj(m1-1).mesh() , get_vis_obj(m2-1).mesh(), iter, dim);
-
 
205
            //    }
-
 
206
            //
-
 
207
            
165
            
208
            
166
            
209
            curvature_paraboloids(mani,
167
            curvature_paraboloids(mani,
210
                                  min_curv_direction,
168
                                  min_curv_direction,
211
                                  max_curv_direction,
169
                                  max_curv_direction,
Line 414... Line 372...
414
            cc_smooth(me->active_mesh());
372
            cc_smooth(me->active_mesh());
415
            
373
            
416
            return;
374
            return;
417
        }
375
        }
418
        
376
        
-
 
377
        void console_root_cc_subdivide(MeshEditor* me, const std::vector<std::string> & args)
-
 
378
        {
-
 
379
            if(wantshelp(args)) {
-
 
380
                me->printf("usage: subdivide.catmull_clark ");
-
 
381
                me->printf("Does one step of Catmull-Clark subdivision");
-
 
382
                
-
 
383
                return;
-
 
384
            }
-
 
385
            me->save_active_mesh();
-
 
386
            
-
 
387
            rootCC_subdivide(me->active_mesh(),me->active_mesh());
-
 
388
            return;
-
 
389
        }
-
 
390
 
-
 
391
        
419
        void console_loop_subdivide(MeshEditor* me, const std::vector<std::string> & args)
392
        void console_loop_subdivide(MeshEditor* me, const std::vector<std::string> & args)
420
        {
393
        {
421
            if(wantshelp(args)) {
394
            if(wantshelp(args)) {
422
                me->printf("usage: subdivide.loop");
395
                me->printf("usage: subdivide.loop");
423
                me->printf("Does one step of Loop subdivision");
396
                me->printf("Does one step of Loop subdivision");
Line 430... Line 403...
430
            loop_smooth(me->active_mesh());
403
            loop_smooth(me->active_mesh());
431
            
404
            
432
            return;
405
            return;
433
        }
406
        }
434
        
407
        
-
 
408
        void console_stitch(MeshEditor* me, const std::vector<std::string> & args)
-
 
409
        {
-
 
410
            if(wantshelp(args)) {
-
 
411
                me->printf("usage: cleanup.stitch <rad>");
-
 
412
                me->printf("Stitches faces");
-
 
413
                
-
 
414
                return;
-
 
415
            }
-
 
416
            double r = 0.001;
-
 
417
            
-
 
418
            if(args.size() > 0){
-
 
419
                istringstream a0(args[0]);
-
 
420
                a0 >> r;
-
 
421
            }
-
 
422
            
-
 
423
            me->save_active_mesh();
-
 
424
            Manifold& m = me->active_mesh();
-
 
425
            Vec3d c;
-
 
426
            float rad;
-
 
427
            bsphere(m, c, rad);
-
 
428
            stitch_mesh(me->active_mesh(), r * rad);
-
 
429
            return;
-
 
430
        }
-
 
431
        
-
 
432
        void console_remove_duplicates(MeshEditor* me, const std::vector<std::string> & args)
-
 
433
        {
-
 
434
            if(wantshelp(args)) {
-
 
435
                me->printf("usage: cleanup.remove_duplicates <rad>");
-
 
436
                me->printf("Removes duplicate vertices and incident faces");
-
 
437
                
-
 
438
                return;
-
 
439
            }
-
 
440
            double r = 0.001;
-
 
441
            
-
 
442
            if(args.size() > 0){
-
 
443
                istringstream a0(args[0]);
-
 
444
                a0 >> r;
-
 
445
            }
-
 
446
            
-
 
447
            me->save_active_mesh();
-
 
448
            Manifold& m = me->active_mesh();
-
 
449
            Vec3d c;
-
 
450
            float rad;
-
 
451
            bsphere(m, c, rad);
-
 
452
            remove_duplicates(me->active_mesh(), r * rad);
-
 
453
            return;
-
 
454
        }
-
 
455
 
-
 
456
        
-
 
457
        void console_remove_val2(MeshEditor* me, const std::vector<std::string> & args)
-
 
458
        {
-
 
459
            if(wantshelp(args)) {
-
 
460
                me->printf("usage: cleanup.remove_val2");
-
 
461
                me->printf("Removes valence 2 vertices");
-
 
462
                
-
 
463
                return;
-
 
464
            }
-
 
465
            me->save_active_mesh();
-
 
466
            Manifold& m = me->active_mesh();
-
 
467
            remove_valence_two_vertices(m);
-
 
468
            return;
-
 
469
        }
-
 
470
 
-
 
471
        
435
        void console_root3_subdivide(MeshEditor* me, const std::vector<std::string> & args)
472
        void console_root3_subdivide(MeshEditor* me, const std::vector<std::string> & args)
436
        {
473
        {
437
            if(wantshelp(args)) {
474
            if(wantshelp(args)) {
438
                me->printf("usage: subdivide.root3");
475
                me->printf("usage: subdivide.root3");
439
                me->printf("Does one step of sqrt(3) subdivision");
476
                me->printf("Does one step of sqrt(3) subdivision");
Line 629... Line 666...
629
                me->harmonics_analyze_mesh();
666
                me->harmonics_analyze_mesh();
630
                return;
667
                return;
631
            }
668
            }
632
        
669
        
633
        
670
        
634
        //    void console_partial_reconstruct(MeshEditor* me, const std::vector<std::string> & args)
671
        void console_partial_reconstruct(MeshEditor* me, const std::vector<std::string> & args)
635
        //    {
672
        {
636
        //        if(args.size() != 3)
673
            if(args.size() != 3)
637
        //            me->printf("usage: haramonics.partial_reconstruct <e0> <e1> <s>");
674
                me->printf("usage: haramonics.partial_reconstruct <e0> <e1> <s>");
638
        //
675
            
639
        //        if(wantshelp(args)) {
676
            if(wantshelp(args)) {
640
        //            me->printf("Reconstruct from projections onto eigenvectors. The two first arguments indicate");
677
                me->printf("Reconstruct from projections onto eigenvectors. The two first arguments indicate");
641
        //            me->printf("the eigenvector interval that we reconstruct from. The last argument is the ");
678
                me->printf("the eigenvector interval that we reconstruct from. The last argument is the ");
642
        //            me->printf("scaling factor. Thus, for a vertex, v, the formula for computing the position, p, is:");
679
                me->printf("scaling factor. Thus, for a vertex, v, the formula for computing the position, p, is:");
643
        //            me->printf("for (i=e0; i<=e1;++i) p += proj[i] * Q[i][v] * s;");
680
                me->printf("for (i=e0; i<=e1;++i) p += proj[i] * Q[i][v] * s;");
644
        //            me->printf("where proj[i] is the 3D vector containing the x, y, and z projections of the mesh onto");
681
                me->printf("where proj[i] is the 3D vector containing the x, y, and z projections of the mesh onto");
645
        //            me->printf("eigenvector i. Q[i][v] is the v'th coordinate of the i'th eigenvector.");
682
                me->printf("eigenvector i. Q[i][v] is the v'th coordinate of the i'th eigenvector.");
646
        //            me->printf("Note that if vertex coordinates are not first reset, the result is probably unexpected.");
683
                me->printf("Note that if vertex coordinates are not first reset, the result is probably unexpected.");
647
        //        }
684
            }
648
        //        me->save_active_mesh();
685
            me->save_active_mesh();
649
        //
686
            
650
        //        if(args.size() != 3)
687
            if(args.size() != 3)
651
        //            return;
688
                return;
652
        //
689
            
653
        //        int E0,E1;
690
            int E0,E1;
654
        //        float scale;
691
            float scale;
655
        //        istringstream a0(args[0]);
692
            istringstream a0(args[0]);
656
        //        a0 >> E0;
693
            a0 >> E0;
657
        //        istringstream a1(args[1]);
694
            istringstream a1(args[1]);
658
        //        a1 >> E1;
695
            a1 >> E1;
659
        //        istringstream a2(args[2]);
696
            istringstream a2(args[2]);
660
        //        a2 >> scale;
697
            a2 >> scale;
661
        //        me->harmonics_partial_reconstruct(E0,E1,scale);
698
            me->harmonics_partial_reconstruct(E0,E1,scale);
662
        //        return;
699
            return;
663
        //    }
700
        }
664
        //
701
        
665
        //    void console_reset_shape(MeshEditor* me, const std::vector<std::string> & args)
702
        void console_reset_shape(MeshEditor* me, const std::vector<std::string> & args)
666
        //    {
703
        {
667
        //        if(wantshelp(args))
704
            if(wantshelp(args))
668
        //        {
705
            {
669
        //            me->printf("usage: harmonics.reset_shape ");
706
                me->printf("usage: harmonics.reset_shape ");
670
        //            me->printf("Simply sets all vertices to 0,0,0. Call this before doing partial_reconstruct");
707
                me->printf("Simply sets all vertices to 0,0,0. Call this before doing partial_reconstruct");
671
        //            me->printf("unless you know what you are doing.");
708
                me->printf("unless you know what you are doing.");
672
        //            return;
709
                return;
673
        //        }
710
            }
674
        //        me->save_active_mesh();
711
            me->save_active_mesh();
675
        //        me->harmonics_reset_shape();
712
            me->harmonics_reset_shape();
676
        //        return;
713
            return;
677
        //    }
714
        }
678
        
715
        
679
        
716
        
680
        void console_close_holes(MeshEditor* me, const std::vector<std::string> & args)
717
        void console_close_holes(MeshEditor* me, const std::vector<std::string> & args)
681
        {
718
        {
682
            if(wantshelp(args))
719
            if(wantshelp(args))
Line 792... Line 829...
792
                Walker w = m.walker(hid);
829
                Walker w = m.walker(hid);
793
                DebugRenderer::edge_colors[hid] = Vec3f(1.0-max(dist[w.vertex()],dist[w.opp().vertex()])/max_dist,0,0);
830
                DebugRenderer::edge_colors[hid] = Vec3f(1.0-max(dist[w.vertex()],dist[w.opp().vertex()])/max_dist,0,0);
794
            }
831
            }
795
            return;
832
            return;
796
        }
833
        }
-
 
834
 
-
 
835
        const Vec3f& get_color(int i)
797
        
836
        {
-
 
837
            static Vec3f ctable[100000];
-
 
838
            static bool was_here;
-
 
839
            gel_srand(0);
-
 
840
            if(!was_here)
-
 
841
            {
-
 
842
                was_here = true;
-
 
843
                ctable[0] = Vec3f(0);
-
 
844
                for(int j=1;j<100000;++j)
-
 
845
                    ctable[j] = Vec3f(0.3)+0.7*normalize(Vec3f(gel_rand(),gel_rand(),gel_rand()));
-
 
846
                ctable[3] = Vec3f(1,0,0);
-
 
847
                ctable[4] = Vec3f(0,1,0);
-
 
848
                ctable[5] = Vec3f(0,0,1);
-
 
849
                ctable[6] = Vec3f(1,0,1);
-
 
850
            }
-
 
851
            return ctable[i%100000];
-
 
852
        }
-
 
853
 
798
        void console_info(MeshEditor* me, const std::vector<std::string> & args)
854
        void console_info(MeshEditor* me, const std::vector<std::string> & args)
799
        {
855
        {
800
            if(wantshelp(args))
856
            if(wantshelp(args))
801
            {
857
            {
802
                me->printf("usage:  info");
858
                me->printf("usage:  info");
Line 808... Line 864...
808
            stringstream bbox_corners;
864
            stringstream bbox_corners;
809
            bbox_corners << p0 << " - " << p7 << endl;
865
            bbox_corners << p0 << " - " << p7 << endl;
810
            me->printf("Bounding box corners : %s", bbox_corners.str().c_str());
866
            me->printf("Bounding box corners : %s", bbox_corners.str().c_str());
811
            map<int,int> val_hist;
867
            map<int,int> val_hist;
812
            
868
            
-
 
869
            Manifold& m = me->active_mesh();
-
 
870
            for(HalfEdgeID h: m.halfedges())
-
 
871
                DebugRenderer::edge_colors[h] = Vec3f(0.3);
813
            for(VertexIDIterator vi = me->active_mesh().vertices_begin(); vi != me->active_mesh().vertices_end(); ++vi)
872
            for(VertexID v: m.vertices())
814
            {
873
            {
815
                int val = valency(me->active_mesh(), *vi);
874
                int val = valency(m,v);
816
                if(val_hist.find(val) == val_hist.end())
875
                DebugRenderer::vertex_colors[v] = get_color(val);
817
                    val_hist[val] = 0;
-
 
818
                ++val_hist[val];
876
                ++val_hist[val];
-
 
877
                
-
 
878
                if(val != 4)
-
 
879
                    circulate_vertex_ccw(m, v, [&](HalfEdgeID h){
-
 
880
                        Walker w = m.walker(h);
-
 
881
                        DebugRenderer::edge_colors[h] = Vec3f(1);
-
 
882
                        DebugRenderer::edge_colors[w.opp().halfedge()] = Vec3f(1);
-
 
883
                        while(valency(m, w.vertex())==4) {
-
 
884
                            w = w.next().opp().next();
-
 
885
                            DebugRenderer::edge_colors[w.halfedge()] = Vec3f(1);
-
 
886
                            DebugRenderer::edge_colors[w.opp().halfedge()] = Vec3f(1);
-
 
887
                        }
-
 
888
                    });
-
 
889
            }
-
 
890
            map<int, int> ngon_hist;
-
 
891
            for(FaceID f: m.faces()) {
-
 
892
                int ne = no_edges(m, f);
-
 
893
                ++ngon_hist[ne];
-
 
894
                DebugRenderer::face_colors[f] = 0.7*get_color(ne);
819
            }
895
            }
820
            
896
            
821
            me->printf("Valency histogam");
897
            me->printf("Valency histogram");
822
            for(map<int,int>::iterator iter = val_hist.begin(); iter != val_hist.end(); ++iter)
898
            for(map<int,int>::iterator iter = val_hist.begin(); iter != val_hist.end(); ++iter)
823
            {
899
            {
824
                stringstream vhl;
-
 
825
                vhl << iter->first << ", " << iter->second;
-
 
826
                me->printf("%d, %d", iter->first, iter->second);
900
                me->printf("%d, %d", iter->first, iter->second);
827
            }
901
            }
828
            
902
            
-
 
903
            me->printf("Ngon histogram");
-
 
904
            for(map<int,int>::iterator iter = ngon_hist.begin(); iter != ngon_hist.end(); ++iter)
-
 
905
            {
-
 
906
                me->printf("%d, %d", iter->first, iter->second);
-
 
907
            }
-
 
908
 
-
 
909
            
829
            me->printf("Mesh contains %d faces", me->active_mesh().no_faces());
910
            me->printf("Mesh contains %d faces", me->active_mesh().no_faces());
830
            me->printf("Mesh contains %d halfedges", me->active_mesh().no_halfedges());
911
            me->printf("Mesh contains %d halfedges", me->active_mesh().no_halfedges());
831
            me->printf("Mesh contains %d vertices", me->active_mesh().no_vertices());
912
            me->printf("Mesh contains %d vertices", me->active_mesh().no_vertices());
832
            return;
913
            return;
833
        }
914
        }
Line 1154... Line 1235...
1154
            me->active_mesh().cleanup();
1235
            me->active_mesh().cleanup();
1155
            
1236
            
1156
            return;
1237
            return;
1157
        }
1238
        }
1158
        
1239
        
-
 
1240
        void console_flip_orientation(MeshEditor* me, const std::vector<std::string> & args)
-
 
1241
        {
-
 
1242
            if(wantshelp(args)){
-
 
1243
                me->printf("usage: cleanup.flip_orientation");
-
 
1244
                me->printf("reorients all faces - flipping normal direction");
-
 
1245
                return;
-
 
1246
            }
-
 
1247
            me->save_active_mesh();
-
 
1248
            flip_orientation(me->active_mesh());
-
 
1249
            return;
-
 
1250
        }
-
 
1251
 
-
 
1252
        
1159
        void console_undo(MeshEditor* me, const std::vector<std::string> & args)
1253
        void console_undo(MeshEditor* me, const std::vector<std::string> & args)
1160
        {
1254
        {
1161
            if(wantshelp(args)) {
1255
            if(wantshelp(args)) {
1162
                me->printf("usage:  undo");
1256
                me->printf("usage:  undo");
1163
                me->printf("This function undoes one operation. Repeated undo does nothing");
1257
                me->printf("This function undoes one operation. Repeated undo does nothing");
Line 1165... Line 1259...
1165
            }
1259
            }
1166
            me->restore_active_mesh();
1260
            me->restore_active_mesh();
1167
            return;
1261
            return;
1168
        }
1262
        }
1169
        
1263
        
-
 
1264
        void console_save_trackball(MeshEditor* me, const std::vector<std::string> & args)
-
 
1265
        {
-
 
1266
            if(wantshelp(args)) {
-
 
1267
                me->printf("usage:  display.save_trackball");
-
 
1268
                me->printf("This function saves the trackball to disk");
-
 
1269
                return;
-
 
1270
            }
-
 
1271
            me->save_ball();
-
 
1272
            return;
-
 
1273
        }
-
 
1274
 
-
 
1275
        void console_load_trackball(MeshEditor* me, const std::vector<std::string> & args)
-
 
1276
        {
-
 
1277
            if(wantshelp(args)) {
-
 
1278
                me->printf("usage:  display.load_trackball");
-
 
1279
                me->printf("This function loads the trackball from disk");
-
 
1280
                return;
-
 
1281
            }
-
 
1282
            me->load_ball();
-
 
1283
            return;
-
 
1284
        }
-
 
1285
 
1170
        
1286
        
1171
    }
1287
    }
1172
    
1288
    
1173
    
1289
    
1174
    void MeshEditor::register_console_function(const std::string& name,
1290
    void MeshEditor::register_console_function(const std::string& name,
Line 1187... Line 1303...
1187
            return;
1303
            return;
1188
        }
1304
        }
1189
        
1305
        
1190
        if (console_visible)
1306
        if (console_visible)
1191
        {
1307
        {
-
 
1308
            static Timer tim;
-
 
1309
            if(key==13)
-
 
1310
                tim.start();
1192
            theConsole.keyboard(key);
1311
            theConsole.keyboard(key);
1193
            if(key == 13)
1312
            if(key == 13)
1194
            {
1313
            {
1195
                active_visobj().post_create_display_list();
1314
                active_visobj().post_create_display_list();
-
 
1315
                double t = tim.get_secs();
-
 
1316
                printf("%f seconds",t);
1196
            }
1317
            }
1197
            return;
1318
            return;
1198
        }
1319
        }
1199
        else {
1320
        else {
1200
            
1321
            
1201
            switch(key) {
1322
            switch(key) {
-
 
1323
                    case 'R':
-
 
1324
                {
-
 
1325
                    theConsole.key_up();
-
 
1326
                    theConsole.keyboard(13);
-
 
1327
                    active_visobj().post_create_display_list();
-
 
1328
                }
-
 
1329
                    break;
1202
                case 'q': exit(0);
1330
                case 'q': exit(0);
1203
                case '\033':
1331
                case '\033':
1204
                    console_visible = false;
1332
                    console_visible = false;
1205
                    break;
1333
                    break;
1206
                case '1':
1334
                case '1':
Line 1271... Line 1399...
1271
    void MeshEditor::key_left(){theConsole.key_left();}
1399
    void MeshEditor::key_left(){theConsole.key_left();}
1272
    void MeshEditor::key_right(){theConsole.key_right();}
1400
    void MeshEditor::key_right(){theConsole.key_right();}
1273
    void MeshEditor::key_home(){theConsole.key_home();}
1401
    void MeshEditor::key_home(){theConsole.key_home();}
1274
    void MeshEditor::key_end(){theConsole.key_end();}
1402
    void MeshEditor::key_end(){theConsole.key_end();}
1275
    
1403
    
-
 
1404
    
-
 
1405
    
1276
    void MeshEditor::grab_ball(TrackBallAction action, const CGLA::Vec2i& pos){
1406
    void MeshEditor::grab_ball(TrackBallAction action, const CGLA::Vec2i& pos){
1277
        active_view_control().grab_ball(action, pos);
1407
        active_view_control().grab_ball(action, pos);
1278
    }
1408
    }
1279
    void MeshEditor::roll_ball(const CGLA::Vec2i& pos){
1409
    void MeshEditor::roll_ball(const CGLA::Vec2i& pos){
1280
        active_view_control().roll_ball(pos);
1410
        active_view_control().roll_ball(pos);
Line 1284... Line 1414...
1284
    }
1414
    }
1285
    bool MeshEditor::try_spinning_ball(){
1415
    bool MeshEditor::try_spinning_ball(){
1286
        return active_view_control().try_spin();
1416
        return active_view_control().try_spin();
1287
    }
1417
    }
1288
    
1418
    
-
 
1419
    void MeshEditor::save_ball() {
-
 
1420
        ofstream ofs("trackball.bin", ios_base::binary);
-
 
1421
        active_view_control().save(ofs);
-
 
1422
        
-
 
1423
    }
-
 
1424
    void MeshEditor::load_ball() {
-
 
1425
        ifstream ifs("trackball.bin", ios_base::binary);
-
 
1426
        active_view_control().load(ifs);
-
 
1427
    
-
 
1428
    }
-
 
1429
 
-
 
1430
    
1289
    bool MeshEditor::grab_mesh(const CGLA::Vec2i& pos)
1431
    bool MeshEditor::grab_mesh(const CGLA::Vec2i& pos)
1290
    {
1432
    {
1291
        if(depth_pick(pos[0], pos[1], depth))
1433
        if(depth_pick(pos[0], pos[1], depth))
1292
        {
1434
        {
1293
            dragging = true;
1435
            dragging = true;
Line 1327... Line 1469...
1327
    
1469
    
1328
    void MeshEditor::release_mesh()
1470
    void MeshEditor::release_mesh()
1329
    {
1471
    {
1330
        dragging = false;
1472
        dragging = false;
1331
    }
1473
    }
1332
 
-
 
1333
    
1474
    
-
 
1475
    
-
 
1476
    
1334
 
1477
    
-
 
1478
    mutex parallel_work;
1335
    void MeshEditor::display(int scale){
1479
    void MeshEditor::display(int scale){
-
 
1480
 
-
 
1481
        // Clear screen.
1336
        glClearColor(1, 1, 1, 0);
1482
        glClearColor(1, 1, 1, 0);
1337
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1483
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
 
1484
        
-
 
1485
        //        parallel_work.lock();
-
 
1486
        //        active_visobj().post_create_display_list();
-
 
1487
        
-
 
1488
        
-
 
1489
        // Display object.
1338
        active_visobj().display(display_render_mode, theConsole, display_smooth_shading, display_gamma);
1490
        active_visobj().display(display_render_mode, theConsole, display_smooth_shading, display_gamma);
-
 
1491
        
-
 
1492
        // Draw console
1339
        if(console_visible)
1493
        if(console_visible)
1340
        {
1494
        {
1341
            glUseProgram(0);
1495
            glUseProgram(0);
1342
            theConsole.display(scale);
1496
            theConsole.display(scale);
1343
        }
1497
        }
-
 
1498
        
-
 
1499
        // Static variable controlling whether we render shadow at all.
-
 
1500
        static Console::variable<int> shadow_enable(0);
-
 
1501
        shadow_enable.reg(theConsole, "display.shadow.enable", "");
-
 
1502
        if(shadow_enable) {
-
 
1503
            // Static variables that control the shadow display created and linked to console
-
 
1504
            static Console::variable<float> zenith(1.571);
-
 
1505
            static Console::variable<float> azimuth(0);
-
 
1506
            static Console::variable<float> shadow_alpha(0.3);
-
 
1507
            azimuth.reg(theConsole, "display.shadow.azimuth", "");
-
 
1508
            zenith.reg(theConsole, "display.shadow.zenith", "");
-
 
1509
            shadow_alpha.reg(theConsole, "display.shadow.shadow_alpha", "");
1344
 
1510
 
-
 
1511
            
-
 
1512
            // Shadow buffer (really a frame buffer object)
-
 
1513
            static ShadowBuffer sb;
-
 
1514
            
-
 
1515
            // String containing fragment program for shadow rendering
-
 
1516
            static string shadow_shdr_fp = "#version 120\n"
-
 
1517
            "    uniform sampler2DShadow shadow_map;\n"
-
 
1518
            "    uniform mat4 Mat;\n"
-
 
1519
            "   uniform float shadow_alpha;\n"
-
 
1520
            "    varying vec4 ep;\n"
-
 
1521
            "    void main()\n"
-
 
1522
            "    {\n"
-
 
1523
            "        vec4 light_pos =  Mat * ep;\n"
-
 
1524
            "        light_pos.z = max(0.001,min(0.999,light_pos.z-0.003));\n"
-
 
1525
            "        if(light_pos.x <=0 || light_pos.x >=1 || light_pos.y <=0 || light_pos.y>=1) gl_FragColor = vec4(1);"
-
 
1526
            "   else      gl_FragColor= vec4(1.0-shadow_alpha)+shadow_alpha*0.25*(shadow2D(shadow_map, light_pos.xyz+vec3(0.001,-0.001,0))+shadow2D(shadow_map, light_pos.xyz+vec3(0.001,0.001,0))+shadow2D(shadow_map, light_pos.xyz-vec3(0.001,0.001,0))+shadow2D(shadow_map, light_pos.xyz+vec3(-0.001,0.001,0)));\n"
-
 
1527
            "    }\n";
-
 
1528
            
-
 
1529
            // Shader program for shadow rendering is compiled and linked.
-
 
1530
            static GLuint prog = 0;
-
 
1531
            if(!prog)
-
 
1532
            {
-
 
1533
                GLuint vp = create_glsl_shader(GL_VERTEX_SHADER,"#version 120\n varying vec4 ep; void main(){ep = gl_Vertex; gl_Position = ftransform();}");
-
 
1534
                GLuint fp = create_glsl_shader(GL_FRAGMENT_SHADER, shadow_shdr_fp);
-
 
1535
                prog = glCreateProgram();
-
 
1536
                
-
 
1537
                glAttachShader(prog, vp);
-
 
1538
                glAttachShader(prog, fp);
-
 
1539
                glLinkProgram(prog);
-
 
1540
            }
-
 
1541
            
-
 
1542
            
-
 
1543
            // Setup OpenGL state for lighting - used when rendering 3D object casting shadow
-
 
1544
            Vec4f lpos = Vec4f(cos(azimuth)*cos(zenith),sin(zenith),sin(azimuth)*cos(zenith),0);
-
 
1545
            glLightfv(GL_LIGHT0, GL_POSITION, lpos.get());
-
 
1546
            Vec4f mamb(.8,.8,.8,0);
-
 
1547
            Vec4f lamb(.4,.4,.5,0);
-
 
1548
            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mamb.get());
-
 
1549
            glLightfv(GL_LIGHT0, GL_AMBIENT, lamb.get());
-
 
1550
 
-
 
1551
            // Get old viewport dimensions. Setup rendering to FBO and set viewport
-
 
1552
            // dimensions for rendering shadows.
-
 
1553
            GLint viewp[4];
-
 
1554
            glGetIntegerv(GL_VIEWPORT, viewp);
-
 
1555
            sb.enable();
-
 
1556
            glViewport(0, 0, 1024, 1024);
-
 
1557
            
-
 
1558
            // Setup object transformations using old school GL.
-
 
1559
            Mat4x4f m;
-
 
1560
            float r = active_visobj().get_bsphere_radius();
-
 
1561
            glMatrixMode(GL_MODELVIEW);
-
 
1562
            glPushMatrix();
-
 
1563
            glLoadIdentity();
-
 
1564
            glMatrixMode(GL_PROJECTION);
-
 
1565
            glPushMatrix();
-
 
1566
            glLoadIdentity();
-
 
1567
            glOrtho(-2*r, 2*r, -2*r, 2*r, -2*r, 2*r);
-
 
1568
            gluLookAt(0.1*r*cos(azimuth)*cos(zenith),0.1*r*sin(zenith),0.1*r*sin(azimuth)*cos(zenith), 0,0,0, 0,1,0);
-
 
1569
            
-
 
1570
            // Copy the transformation matrix to user code.
-
 
1571
            glGetFloatv(GL_PROJECTION_MATRIX, m.get());
-
 
1572
            
-
 
1573
            // Draw the object in light space.
-
 
1574
            draw(active_visobj().mesh());
-
 
1575
            
-
 
1576
            // Restore transformation matrices.
-
 
1577
            glPopMatrix();
-
 
1578
            glMatrixMode(GL_MODELVIEW);
-
 
1579
            glPopMatrix();
-
 
1580
            
-
 
1581
            // Restore the usual on screen framebuffer.
-
 
1582
            glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
-
 
1583
            glDrawBuffer(GL_BACK);
-
 
1584
            glViewport(0, 0, viewp[2], viewp[3]);
-
 
1585
            
-
 
1586
            // Use shadow: bind the shadow texture to unit 0.
-
 
1587
            sb.bind_textures(0);
-
 
1588
            
-
 
1589
            // Use the shadow rendering shader program.
-
 
1590
            glUseProgram(prog);
-
 
1591
            active_visobj().view_control().set_gl_modelview();
-
 
1592
            glUniform1i(glGetUniformLocation(prog, "shadow_map"), 0);
-
 
1593
            glUniform1f(glGetUniformLocation(prog, "shadow_alpha"), shadow_alpha);
-
 
1594
            m = translation_Mat4x4f(Vec3f(0.5)) * scaling_Mat4x4f(Vec3f(0.5)) * transpose(m);
-
 
1595
            glUniformMatrix4fv(glGetUniformLocation(prog, "Mat"), 1, 1, m.get());
-
 
1596
            
-
 
1597
            
-
 
1598
            // Setup blending such that the shadow is alpha blended with model.
-
 
1599
            glDepthFunc(GL_LEQUAL);
-
 
1600
            glEnable(GL_BLEND);
-
 
1601
            glBlendFunc(GL_ZERO, GL_SRC_COLOR);
-
 
1602
 
-
 
1603
            // Draw ground plane for shadows.
-
 
1604
            Vec3d p0, p7;
-
 
1605
            bbox(active_visobj().mesh(), p0, p7);
-
 
1606
            glBegin(GL_QUADS);
-
 
1607
            glVertex3f(-100*r, p0[1],-100*r);
-
 
1608
            glVertex3f(-100*r, p0[1],100*r);
-
 
1609
            glVertex3f(100*r, p0[1],100*r);
-
 
1610
            glVertex3f(100*r, p0[1],-100*r);
-
 
1611
            glEnd();
-
 
1612
            
-
 
1613
            // Draw model again ... just to add shadow.
-
 
1614
            draw(active_visobj().mesh());
-
 
1615
            
-
 
1616
            // Disable blending and shader program.
-
 
1617
            glDisable(GL_BLEND);
-
 
1618
            glUseProgram(0);
-
 
1619
        }
-
 
1620
        //        parallel_work.unlock();
-
 
1621
        //        std::this_thread::sleep_for(std::chrono::milliseconds(10));
1345
    }
1622
    }
1346
    
1623
    
1347
    void MeshEditor::reshape(int w, int h) {
1624
    void MeshEditor::reshape(int w, int h) {
1348
        for(VisObj& v : vo)
1625
        for(VisObj& v : vo)
1349
            v.view_control().reshape(w, h);
1626
            v.view_control().reshape(w, h);
Line 1387... Line 1664...
1387
        register_console_function("optimize.maximize_min_angle", console_maximize_min_angle,"");
1664
        register_console_function("optimize.maximize_min_angle", console_maximize_min_angle,"");
1388
        register_console_function("cleanup.close_holes", console_close_holes,"");
1665
        register_console_function("cleanup.close_holes", console_close_holes,"");
1389
        register_console_function("load_mesh", console_reload,"");
1666
        register_console_function("load_mesh", console_reload,"");
1390
        register_console_function("add_mesh", console_add_mesh,"");
1667
        register_console_function("add_mesh", console_add_mesh,"");
1391
        
1668
        
-
 
1669
        register_console_function("cleanup.stitch", console_stitch,"");
-
 
1670
        register_console_function("cleanup.remove_duplicates", console_remove_duplicates,"");
-
 
1671
        register_console_function("cleanup.remove_val2", console_remove_val2, "");
-
 
1672
        register_console_function("cleanup.flip_orientation", console_flip_orientation,"");
1392
        register_console_function("cleanup.remove_caps", console_remove_caps,"");
1673
        register_console_function("cleanup.remove_caps", console_remove_caps,"");
1393
        register_console_function("cleanup.remove_needles", console_remove_needles,"");
1674
        register_console_function("cleanup.remove_needles", console_remove_needles,"");
1394
        register_console_function("triangulate", console_triangulate,"");
1675
        register_console_function("triangulate", console_triangulate,"");
1395
        register_console_function("refine.split_edges", console_refine_edges,"");
1676
        register_console_function("refine.split_edges", console_refine_edges,"");
1396
        register_console_function("refine.split_faces", console_refine_faces,"");
1677
        register_console_function("refine.split_faces", console_refine_faces,"");
1397
        register_console_function("subdivide.catmull_clark", console_cc_subdivide,"");
1678
        register_console_function("subdivide.catmull_clark", console_cc_subdivide,"");
-
 
1679
        register_console_function("subdivide.rootcc", console_root_cc_subdivide,"");
1398
        register_console_function("subdivide.loop", console_loop_subdivide,"");
1680
        register_console_function("subdivide.loop", console_loop_subdivide,"");
1399
        register_console_function("subdivide.root3", console_root3_subdivide,"");
1681
        register_console_function("subdivide.root3", console_root3_subdivide,"");
1400
        register_console_function("subdivide.doo_sabin", console_doosabin_subdivide,"");
1682
        register_console_function("subdivide.doo_sabin", console_doosabin_subdivide,"");
1401
        register_console_function("subdivide.butterfly", console_butterfly_subdivide,"");
1683
        register_console_function("subdivide.butterfly", console_butterfly_subdivide,"");
1402
        register_console_function("save_mesh", console_save,"");
1684
        register_console_function("save_mesh", console_save,"");
Line 1411... Line 1693...
1411
        register_console_function("undo", console_undo,"");
1693
        register_console_function("undo", console_undo,"");
1412
        
1694
        
1413
        register_console_function("validity", console_valid,"");
1695
        register_console_function("validity", console_valid,"");
1414
        register_console_function("info", console_info,"");
1696
        register_console_function("info", console_info,"");
1415
        
1697
        
1416
        //        register_console_function("harmonics.reset_shape", console_reset_shape, "");
1698
        register_console_function("harmonics.reset_shape", console_reset_shape, "");
1417
        register_console_function("harmonics.analyze", console_analyze, "");
1699
        register_console_function("harmonics.analyze", console_analyze, "");
1418
        //        register_console_function("harmonics.partial_reconstruct", console_partial_reconstruct,"");
1700
        register_console_function("harmonics.partial_reconstruct", console_partial_reconstruct,"");
1419
 
1701
 
1420
        register_console_function("Dijkstra", console_Dijkstra,"");
1702
        register_console_function("Dijkstra", console_Dijkstra,"");
1421
        
1703
        
-
 
1704
        register_console_function("display.save_trackball", console_save_trackball, "Saves trackball to disk");
-
 
1705
        register_console_function("display.load_trackball", console_load_trackball, "Load trackball to disk");
-
 
1706
        
1422
        register_console_function("transform.scale", console_scale, "Scale mesh");
1707
        register_console_function("transform.scale", console_scale, "Scale mesh");
1423
        register_console_function("test", console_test, "Test some shit");
1708
        register_console_function("test", console_test, "Test some shit");
1424
        active.reg(theConsole, "active_mesh", "The active mesh");
1709
        active.reg(theConsole, "active_mesh", "The active mesh");
1425
        display_render_mode.reg(theConsole, "display.render_mode", "Display render mode");
1710
        display_render_mode.reg(theConsole, "display.render_mode", "Display render mode");
1426
        brush_size.reg(theConsole, "brush_size", "Size of brush used for editing");
1711
        brush_size.reg(theConsole, "brush_size", "Size of brush used for editing");