-
Notifications
You must be signed in to change notification settings - Fork 29
/
Interface.cpp
2060 lines (1675 loc) · 65.8 KB
/
Interface.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
#include "Interface.hpp"
#include "Stitch.hpp"
#include <kit/GLProgram.hpp>
#include <kit/GLTexture.hpp>
#include <kit/Load.hpp>
#include <kit/gl_errors.hpp>
#include <kit/check_fb.hpp>
#include <glm/gtx/norm.hpp>
#include <glm/gtx/hash.hpp>
#include <SDL.h>
#include <algorithm>
#include <unordered_set>
//given normalized 0..1 time:
glm::vec3 time_color(float time) {
const constexpr size_t Size = 3;
static glm::vec3 grad[Size] = {
glm::vec3(0.2f, 0.2f, 1.0f),
glm::vec3(0.8f, 0.8f, 0.8f),
glm::vec3(0.8f, 0.2f, 0.2f)
};
time *= (Size-1);
int32_t i = std::max(0, std::min(int32_t(Size)-2, int32_t(std::floor(time))));
float f = std::max(0.0f, std::min(1.0f, time - i));
return glm::mix(grad[i], grad[i+1], f);
}
//-------------------------------------------------------------
constexpr const uint32_t TimeTexSize = 16;
kit::Load< GLTexture > time_tex(kit::LoadTagDefault, [](){
GLTexture *ret = new GLTexture();
std::vector< glm::vec3 > data(TimeTexSize);
for (uint32_t i = 0; i < data.size(); ++i) {
data[i] = time_color(i / float(TimeTexSize-1));
}
glBindTexture(GL_TEXTURE_2D, ret->texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, data.size(), 1, 0, GL_RGB, GL_FLOAT, data.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
return ret;
});
//-------------------------------------------------------------
GLuint model_draw_p2c = -1U;
GLuint model_draw_p2l = -1U;
GLuint model_draw_n2l = -1U;
kit::Load< GLProgram > model_draw(kit::LoadTagDefault, [](){
GLProgram *ret = new GLProgram(
"#version 330\n"
"#line " STR(__LINE__) "\n"
"uniform mat4 p2c;\n" //position to clip space
"uniform mat4x3 p2l;\n" //position to light space
"uniform mat3 n2l;\n" //normal to light space
"in vec4 Position;\n"
"in vec3 Normal;\n"
"in vec4 ID;\n"
"out vec3 normal;\n"
"out vec3 position;\n"
"out vec4 id;\n"
"void main() {\n"
" gl_Position = p2c * Position;\n"
" position = p2l * Position;\n"
" normal = n2l * Normal;\n"
" id = ID;\n"
"}\n"
,
"#version 330\n"
"#line " STR(__LINE__) "\n"
"in vec3 normal;\n"
"in vec3 position;\n"
"in vec4 id;\n"
"layout(location = 0) out vec4 fragColor;\n"
"layout(location = 1) out vec4 fragID;\n"
"void main() {\n"
" vec3 n = normalize(normal);\n"
" vec3 l = vec3(0.0, 0.0, 1.0);\n"
" float nl = dot(n, l) * 0.5 + 0.5;\n"
" fragColor = vec4(nl * vec3(1.0), 1.0);\n"
" fragID = id;\n"
"}\n"
);
model_draw_p2c = ret->getUniformLocation("p2c", GLProgram::MissingIsError);
model_draw_p2l = ret->getUniformLocation("p2l", GLProgram::MissingIsWarning);
model_draw_n2l = ret->getUniformLocation("n2l", GLProgram::MissingIsWarning);
return ret;
});
//-------------------------------------------------------------
GLuint textured_draw_p2c = -1U;
GLuint textured_draw_p2l = -1U;
GLuint textured_draw_n2l = -1U;
kit::Load< GLProgram > textured_draw(kit::LoadTagDefault, [](){
GLProgram *ret = new GLProgram(
"#version 330\n"
"#line " STR(__LINE__) "\n"
"uniform mat4 p2c;\n" //position to clip space
"uniform mat4x3 p2l;\n" //position to light space
"uniform mat3 n2l;\n" //normal to light space
"in vec4 Position;\n"
"in vec3 Normal;\n"
"in vec4 ID;\n"
"in vec2 TexCoord;\n"
"out vec3 normal;\n"
"out vec3 position;\n"
"out vec4 id;\n"
"out vec2 texCoord;\n"
"void main() {\n"
" gl_Position = p2c * Position;\n"
" position = p2l * Position;\n"
" normal = n2l * Normal;\n"
" id = ID;\n"
" texCoord = TexCoord;\n"
"}\n"
,
"#version 330\n"
"#line " STR(__LINE__) "\n"
"uniform sampler2D tex;\n"
"in vec3 normal;\n"
"in vec3 position;\n"
"in vec4 id;\n"
"in vec2 texCoord;\n"
"layout(location = 0) out vec4 fragColor;\n"
"layout(location = 1) out vec4 fragID;\n"
"void main() {\n"
" vec3 n = normalize(normal);\n"
" vec3 l = vec3(0.0, 0.0, 1.0);\n"
" float nl = dot(n, l) * 0.5 + 0.5;\n"
" fragColor = vec4(nl * texture(tex, texCoord).rgb, 1.0);\n"
" fragID = id;\n"
"}\n"
);
textured_draw_p2c = ret->getUniformLocation("p2c", GLProgram::MissingIsError);
textured_draw_p2l = ret->getUniformLocation("p2l", GLProgram::MissingIsWarning);
textured_draw_n2l = ret->getUniformLocation("n2l", GLProgram::MissingIsWarning);
glUseProgram(ret->program);
glUniform1i(ret->getUniformLocation("tex", GLProgram::MissingIsWarning), 0);
glUseProgram(0);
return ret;
});
//------------------------------------
GLuint marker_draw_p2c = -1U;
GLuint marker_draw_p2l = -1U;
GLuint marker_draw_n2l = -1U;
GLuint marker_draw_id = -1U;
GLuint marker_draw_color = -1U;
kit::Load< GLProgram > marker_draw(kit::LoadTagDefault, [](){
GLProgram *ret = new GLProgram(
"#version 330\n"
"#line " STR(__LINE__) "\n"
"uniform mat4 p2c;\n" //position to clip space
"uniform mat4x3 p2l;\n" //position to light space
"uniform mat3 n2l;\n" //normal to light space
"in vec4 Position;\n"
"in vec3 Normal;\n"
"out vec3 normal;\n"
"out vec3 position;\n"
"void main() {\n"
" gl_Position = p2c * Position;\n"
" position = p2l * Position;\n"
" normal = n2l * Normal;\n"
"}\n"
,
"#version 330\n"
"#line " STR(__LINE__) "\n"
"uniform vec3 color;\n"
"uniform vec4 id;\n"
"in vec3 normal;\n"
"in vec3 position;\n"
"layout(location = 0) out vec4 fragColor;\n"
"layout(location = 1) out vec4 fragID;\n"
"void main() {\n"
" vec3 n = normalize(normal);\n"
" vec3 l = vec3(0.0, 0.0, 1.0);\n"
" float nl = dot(n, l) * 0.5 + 0.5;\n"
" fragColor = vec4(nl * color, 1.0);\n"
" fragID = id;\n"
"}\n"
);
marker_draw_p2c = ret->getUniformLocation("p2c", GLProgram::MissingIsError);
marker_draw_p2l = ret->getUniformLocation("p2l", GLProgram::MissingIsWarning);
marker_draw_n2l = ret->getUniformLocation("n2l", GLProgram::MissingIsWarning);
marker_draw_color = ret->getUniformLocation("color", GLProgram::MissingIsWarning);
marker_draw_id = ret->getUniformLocation("id", GLProgram::MissingIsWarning);
return ret;
});
//------------------------------------
GLuint path_draw_p2c = -1U;
GLuint path_draw_p2l = -1U;
GLuint path_draw_n2l = -1U;
GLuint path_draw_id = -1U;
kit::Load< GLProgram > path_draw(kit::LoadTagDefault, [](){
GLProgram *ret = new GLProgram(
"#version 330\n"
"#line " STR(__LINE__) "\n"
"uniform mat4 p2c;\n" //position to clip space
"uniform mat4x3 p2l;\n" //position to light space
"uniform mat3 n2l;\n" //normal to light space
"in vec4 Position;\n"
"in vec3 Normal;\n"
"in vec3 Color;\n"
"out vec3 normal;\n"
"out vec3 position;\n"
"out vec3 color;\n"
"void main() {\n"
" gl_Position = p2c * Position;\n"
" position = p2l * Position;\n"
" normal = n2l * Normal;\n"
" color = Color;\n"
"}\n"
,
"#version 330\n"
"#line " STR(__LINE__) "\n"
"uniform vec4 id;\n"
"in vec3 normal;\n"
"in vec3 position;\n"
"in vec3 color;\n"
"layout(location = 0) out vec4 fragColor;\n"
"layout(location = 1) out vec4 fragID;\n"
"void main() {\n"
" vec3 n = normalize(normal);\n"
" vec3 l = vec3(0.0, 0.0, 1.0);\n"
" float nl = dot(n, l) * 0.5 + 0.5;\n"
" fragColor = vec4(nl * color, 1.0);\n"
" fragID = id;\n"
"}\n"
);
path_draw_p2c = ret->getUniformLocation("p2c", GLProgram::MissingIsError);
path_draw_p2l = ret->getUniformLocation("p2l", GLProgram::MissingIsWarning);
path_draw_n2l = ret->getUniformLocation("n2l", GLProgram::MissingIsWarning);
path_draw_id = ret->getUniformLocation("id", GLProgram::MissingIsWarning);
return ret;
});
//------------------------------------
kit::Load< GLProgram > copy_fb(kit::LoadTagDefault, [](){
GLProgram *ret = new GLProgram(
//ye olde "fullscreen triangle":
"#version 330\n"
"#line " STR(__LINE__) "\n"
"void main() {\n"
" gl_Position = vec4(4 * (gl_VertexID & 1) - 1, 2 * (gl_VertexID & 2) - 1, 0.0, 1.0);\n"
"}\n"
,
//infinite perspective matrix takes z to (-z -2n) / -z = 1 + 2n/z (so z ranges from -n to -inf go to -1 to 1
//depth-component texture takes [-1,1] to [0,1]
"#version 330\n"
"#line " STR(__LINE__) "\n"
"uniform sampler2D color_tex;\n"
"uniform sampler2D depth_tex;\n"
"layout(location = 0) out vec4 fragColor;\n"
"void main() {\n"
" ivec2 px = ivec2(gl_FragCoord.xy);\n"
" float NEAR = 0.1;\n"
" float ref = texelFetchOffset(depth_tex, px, 0, ivec2(0,0)).r;\n"
" float z = min(0.9999, ref);\n"
//over from depth component space -> projected z value:
" z = 2.0 * (z - 0.5);\n"
//from projected z value to world z value:
" z = (2.0 * NEAR) / (z - 1.0);\n"
//z is now in range [-inf, -NEAR]
" z += 0.5;\n"
" z = 1.0 + (2.0 * NEAR) / z;\n"
" z = 0.5 * z + 0.5;\n"
" float amt = 1.0 / (z - ref);\n"
" #define DO(X,Y) { \\\n"
" float val = texelFetchOffset(depth_tex, px, 0, ivec2(X,Y)).r; \\\n"
" tint += clamp((val - ref) * amt, 0.0, 1.0);\\\n"
" }\n"
" float tint = 0.0;\n"
" DO(-1, 2) DO( 0, 2) DO( 1, 2) \n"
" DO(-2, 1) DO(-1, 1) DO( 0, 1) DO( 1, 1) DO(2, 1)\n"
" DO(-2, 0) DO(-1, 0) DO( 1, 0) DO(2, 0)\n"
" DO(-2, -1) DO(-1, -1) DO( 0, -1) DO( 1, -1) DO(2, -1)\n"
" DO(-1, -2) DO( 0, -2) DO( 1, -2) \n"
" tint = 1.0 - (tint / 20.0);\n"
" fragColor = vec4(tint, tint, tint, 1.0) * texelFetch(color_tex, px, 0);\n"
"}\n"
);
glUseProgram(ret->program);
glUniform1i(ret->getUniformLocation("color_tex", GLProgram::MissingIsWarning), 0);
glUniform1i(ret->getUniformLocation("depth_tex", GLProgram::MissingIsWarning), 1);
glUseProgram(0);
return ret;
});
//------------------------------------
//layout: (position)
kit::Load< GLAttribBuffer< glm::vec3 > > sphere_tristrip(kit::LoadTagInit, [](){
GLAttribBuffer< glm::vec3 > *ret = new GLAttribBuffer< glm::vec3 >();
const uint32_t Rings = 10;
const uint32_t Slices = 16;
std::vector< glm::vec2 > circle;
circle.reserve(Slices + 1);
for (uint32_t a = 0; a < Slices; ++a) {
float ang = a / float(Slices) * M_PI * 2.0f;
circle.emplace_back(std::cos(ang), std::sin(ang));
}
circle.emplace_back(circle[0]);
std::vector< GLAttribBuffer< glm::vec3 >::Vertex > attribs;
attribs.reserve((Rings) * 2 * (Slices+1));
glm::vec2 prev(0.0f, -1.0f);
for (uint32_t a = 1; a <= Rings; ++a) {
glm::vec2 next;
if (a == Rings) {
next = glm::vec2(0.0f, 1.0f);
} else {
float ang = (a / float(Rings) - 0.5f) * M_PI;
next = glm::vec2(std::cos(ang), std::sin(ang));
}
for (auto const &c : circle) {
attribs.emplace_back(glm::vec3(prev.x * c.x, prev.x * c.y, prev.y));
attribs.emplace_back(glm::vec3(next.x * c.x, next.x * c.y, next.y));
}
prev = next;
}
ret->set(attribs, GL_STATIC_DRAW);
return ret;
});
kit::Load< GLVertexArray > sphere_tristrip_for_marker_draw(kit::LoadTagDefault, [](){
return new GLVertexArray(GLVertexArray::make_binding(marker_draw->program, {
{marker_draw->getAttribLocation("Position", GLProgram::MissingIsError), (*sphere_tristrip)[0]},
{marker_draw->getAttribLocation("Normal", GLProgram::MissingIsError), (*sphere_tristrip)[0]}
}));
});
kit::Load< GLVertexArray > empty_vertex_array(kit::LoadTagDefault);
Interface::Interface() {
std::cout << "Setting up various buffer bindings." << std::endl; //DEBUG
model_triangles_for_model_draw = GLVertexArray::make_binding(model_draw->program, {
{model_draw->getAttribLocation("Position", GLProgram::MissingIsError), model_triangles[0]},
{model_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), model_triangles[1]},
{model_draw->getAttribLocation("ID", GLProgram::MissingIsWarning), model_triangles[2]}
});
constraints_tristrip_for_path_draw = GLVertexArray::make_binding(path_draw->program, {
{path_draw->getAttribLocation("Position", GLProgram::MissingIsError), constraints_tristrip[0]},
{path_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), constraints_tristrip[1]},
{path_draw->getAttribLocation("Color", GLProgram::MissingIsWarning), constraints_tristrip[2]}
});
times_model_triangles_for_textured_draw = GLVertexArray::make_binding(textured_draw->program, {
{textured_draw->getAttribLocation("Position", GLProgram::MissingIsError), times_model_triangles[0]},
{textured_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), times_model_triangles[1]},
{textured_draw->getAttribLocation("ID", GLProgram::MissingIsWarning), times_model_triangles[2]},
{textured_draw->getAttribLocation("TexCoord", GLProgram::MissingIsWarning), times_model_triangles[3]}
});
rowcol_graph_tristrip_for_path_draw = GLVertexArray::make_binding(path_draw->program, {
{path_draw->getAttribLocation("Position", GLProgram::MissingIsError), rowcol_graph_tristrip[0]},
{path_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), rowcol_graph_tristrip[1]},
{path_draw->getAttribLocation("Color", GLProgram::MissingIsWarning), rowcol_graph_tristrip[2]}
});
active_chains_tristrip_for_path_draw = GLVertexArray::make_binding(path_draw->program, {
{path_draw->getAttribLocation("Position", GLProgram::MissingIsError), active_chains_tristrip[0]},
{path_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), active_chains_tristrip[1]},
{path_draw->getAttribLocation("Color", GLProgram::MissingIsWarning), active_chains_tristrip[2]}
});
slice_triangles_for_path_draw = GLVertexArray::make_binding(path_draw->program, {
{path_draw->getAttribLocation("Position", GLProgram::MissingIsError), slice_triangles[0]},
{path_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), slice_triangles[1]},
{path_draw->getAttribLocation("Color", GLProgram::MissingIsWarning), slice_triangles[2]},
});
slice_chains_tristrip_for_path_draw = GLVertexArray::make_binding(path_draw->program, {
{path_draw->getAttribLocation("Position", GLProgram::MissingIsError), slice_chains_tristrip[0]},
{path_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), slice_chains_tristrip[1]},
{path_draw->getAttribLocation("Color", GLProgram::MissingIsWarning), slice_chains_tristrip[2]}
});
links_tristrip_for_path_draw = GLVertexArray::make_binding(path_draw->program, {
{path_draw->getAttribLocation("Position", GLProgram::MissingIsError), links_tristrip[0]},
{path_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), links_tristrip[1]},
{path_draw->getAttribLocation("Color", GLProgram::MissingIsWarning), links_tristrip[2]}
});
next_active_chains_tristrip_for_path_draw = GLVertexArray::make_binding(path_draw->program, {
{path_draw->getAttribLocation("Position", GLProgram::MissingIsError), next_active_chains_tristrip[0]},
{path_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), next_active_chains_tristrip[1]},
{path_draw->getAttribLocation("Color", GLProgram::MissingIsWarning), next_active_chains_tristrip[2]}
});
traced_tristrip_for_path_draw = GLVertexArray::make_binding(path_draw->program, {
{path_draw->getAttribLocation("Position", GLProgram::MissingIsError), traced_tristrip[0]},
{path_draw->getAttribLocation("Normal", GLProgram::MissingIsWarning), traced_tristrip[1]},
{path_draw->getAttribLocation("Color", GLProgram::MissingIsWarning), traced_tristrip[2]}
});
GL_ERRORS();
}
Interface::~Interface() {
if (color_id_fb) {
glDeleteFramebuffers(1, &color_id_fb);
color_id_fb = 0;
}
if (color_tex) {
glDeleteTextures(1, &color_tex);
}
if (id_tex) {
glDeleteTextures(1, &id_tex);
}
if (depth_tex) {
glDeleteTextures(1, &depth_tex);
}
}
void Interface::update(float elapsed) {
if (mouse.moved) {
update_hovered();
mouse.moved = false;
}
if (constraints_dirty) {
update_constraints();
}
}
void Interface::draw() {
if (fb_size != kit::display.size) alloc_fbs();
float scale_factor = model.avg_edge() * 2.f;
glViewport(0, 0, kit::display.size.x, kit::display.size.y);
glBindFramebuffer(GL_FRAMEBUFFER, color_id_fb);
{
glClearColor(0.9f, 0.9f, 0.9f, 0.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
GLfloat zeros[4] = {0.0f, 0.0f, 0.0f, 0.0f};
glClearBufferfv(GL_COLOR, 1, zeros);
}
glEnable(GL_DEPTH_TEST);
if (show & ShowModel) { //draw the model:
if (model_triangles_dirty) update_model_triangles();
glUseProgram(model_draw->program);
//Position-to-clip matrix:
glm::mat4 p2c = camera.mvp();
//Position-to-light matrix:
glm::mat4x3 p2l = camera.mv(); //glm::mat4(1.0f);
//Normal-to-light matrix:
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUniformMatrix4fv(model_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(model_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(model_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glBindVertexArray(model_triangles_for_model_draw.array);
glDrawArrays(GL_TRIANGLES, 0, model_triangles.count);
glBindVertexArray(0);
glUseProgram(0);
}
if (show & ShowTimesModel) { //draw the constrained model:
if (times_model_triangles_dirty) update_times_model_triangles();
glUseProgram(textured_draw->program);
//Position-to-clip matrix:
glm::mat4 p2c = camera.mvp();
//Position-to-light matrix:
glm::mat4x3 p2l = camera.mv(); //glm::mat4(1.0f);
//Normal-to-light matrix:
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUniformMatrix4fv(textured_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(textured_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(textured_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glBindVertexArray(times_model_triangles_for_textured_draw.array);
glBindTexture(GL_TEXTURE_2D, time_tex->texture);
glDrawArrays(GL_TRIANGLES, 0, times_model_triangles.count);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
glUseProgram(0);
}
if (show & ShowSlice) { //draw the clipped model debug out:
if (slice_triangles_dirty) update_slice_triangles();
glUseProgram(path_draw->program);
//Position-to-clip matrix:
glm::mat4 p2c = camera.mvp();
//Position-to-light matrix:
glm::mat4x3 p2l = camera.mv(); //glm::mat4(1.0f);
//Normal-to-light matrix:
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUniformMatrix4fv(path_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(path_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(path_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glBindVertexArray(slice_triangles_for_path_draw.array);
glDrawArrays(GL_TRIANGLES, 0, slice_triangles.count);
glBindVertexArray(0);
glUseProgram(0);
}
if (show & ShowModel) { //draw marker spheres:
glUseProgram(marker_draw->program);
glBindVertexArray(sphere_tristrip_for_marker_draw->array);
auto sphere = [&](glm::vec3 const &at, float r, glm::vec3 const &color, glm::u8vec4 const &id) {
r = r * scale_factor;
//Position-to-light matrix:
glm::mat4x3 p2l = glm::mat4x3(
glm::vec3(r, 0.0f, 0.0f),
glm::vec3(0.0f, r, 0.0f),
glm::vec3(0.0f, 0.0f, r),
glm::vec3(at)
);
//Normal-to-light matrix:
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
//Position-to-clip matrix:
glm::mat4 p2c = camera.mvp() * glm::mat4(p2l);
glUniformMatrix4fv(marker_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(marker_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(marker_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glUniform4f(marker_draw_id, id.r / 255.0f, id.g / 255.0f, id.b / 255.0f, id.a / 255.0f);
glUniform3fv(marker_draw_color, 1, glm::value_ptr(color));
glDrawArrays(GL_TRIANGLE_STRIP, 0, sphere_tristrip->count);
};
//constrained vertices (do draw into ID buffer):
float min_time = -1.0f;
float max_time = 1.0f;
for (auto const &c : constraints) {
min_time = glm::min(min_time, c.value);
max_time = glm::max(max_time, c.value);
}
for (auto const &c : constraints) {
uint32_t idx = &c - &constraints[0];
glm::vec3 color = time_color((c.value - min_time) / (max_time - min_time));
for (uint32_t i = 0; i < c.chain.size(); ++i) {
glm::u8vec4 id(2, i, (idx >> 8) & 0xff, idx & 0xff);
sphere(model.vertices[c.chain[i]], 0.04f, color, id);
}
}
//mouse cursor sphere (don't draw into ID buffer):
glColorMaski(1, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
if (hovered.tri < model.triangles.size()) {
glm::vec3 const &a = model.vertices[model.triangles[hovered.tri].x];
glm::vec3 const &b = model.vertices[model.triangles[hovered.tri].y];
glm::vec3 const &c = model.vertices[model.triangles[hovered.tri].z];
sphere(a, 0.02f, glm::vec3(1.0f, 0.0f, 0.0f), glm::u8vec4(0x00));
sphere(b, 0.02f, glm::vec3(0.0f, 1.0f, 0.0f), glm::u8vec4(0x00));
sphere(c, 0.02f, glm::vec3(0.0f, 0.0f, 1.0f), glm::u8vec4(0x00));
sphere(hovered.point, 0.02f, glm::vec3(0.4f, 0.4f, 0.4f), glm::u8vec4(0x00));
}
glColorMaski(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBindVertexArray(0);
glUseProgram(0);
}
//draw constraint paths:
if (show & ShowConstraints) {
if (constraints_tristrip_dirty) update_constraints_tristrip();
glm::mat4 p2c = camera.mvp();
glm::mat4x3 p2l = camera.mv();
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUseProgram(path_draw->program);
glBindVertexArray(constraints_tristrip_for_path_draw.array);
glUniformMatrix4fv(path_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(path_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(path_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glUniform4f(path_draw_id, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f);
//don't draw into ID array:
glColorMaski(1, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, constraints_tristrip.count);
glColorMaski(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBindVertexArray(0);
glUseProgram(0);
}
//draw current row-column graph (peeling):
if (show & ShowRowColGraph) {
if (rowcol_graph_tristrip_dirty) update_rowcol_graph_tristrip();
glm::mat4 p2c = camera.mvp();
glm::mat4x3 p2l = camera.mv();
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUseProgram(path_draw->program);
glBindVertexArray(rowcol_graph_tristrip_for_path_draw.array);
glUniformMatrix4fv(path_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(path_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(path_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glUniform4f(path_draw_id, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f);
//don't draw into ID array:
glColorMaski(1, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, rowcol_graph_tristrip.count);
glColorMaski(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBindVertexArray(0);
glUseProgram(0);
}
//draw active chains (peeling):
if (show & ShowActiveChains) {
if (active_chains_tristrip_dirty) update_active_chains_tristrip();
glm::mat4 p2c = camera.mvp();
glm::mat4x3 p2l = camera.mv();
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUseProgram(path_draw->program);
glBindVertexArray(active_chains_tristrip_for_path_draw.array);
glUniformMatrix4fv(path_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(path_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(path_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glUniform4f(path_draw_id, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f);
//don't draw into ID array:
glColorMaski(1, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, active_chains_tristrip.count);
glColorMaski(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBindVertexArray(0);
glUseProgram(0);
}
//draw slice-relative chains (peeling):
if (show & ShowSliceChains) {
if (slice_chains_tristrip_dirty) update_slice_chains_tristrip();
glm::mat4 p2c = camera.mvp();
glm::mat4x3 p2l = camera.mv();
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUseProgram(path_draw->program);
glBindVertexArray(slice_chains_tristrip_for_path_draw.array);
glUniformMatrix4fv(path_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(path_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(path_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glUniform4f(path_draw_id, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f);
//don't draw into ID array:
glColorMaski(1, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, slice_chains_tristrip.count);
glColorMaski(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBindVertexArray(0);
glUseProgram(0);
}
//draw links + next stitches:
if (show & ShowLinks) {
if (links_tristrip_dirty) update_links_tristrip();
glm::mat4 p2c = camera.mvp();
glm::mat4x3 p2l = camera.mv();
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUseProgram(path_draw->program);
glBindVertexArray(links_tristrip_for_path_draw.array);
glUniformMatrix4fv(path_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(path_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(path_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glUniform4f(path_draw_id, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f);
//don't draw into ID array:
glColorMaski(1, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, links_tristrip.count);
glColorMaski(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBindVertexArray(0);
glUseProgram(0);
}
//draw next active chains:
if (show & ShowNextActiveChains) {
if (next_active_chains_tristrip_dirty) update_next_active_chains_tristrip();
glm::mat4 p2c = camera.mvp();
glm::mat4x3 p2l = camera.mv();
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUseProgram(path_draw->program);
glBindVertexArray(next_active_chains_tristrip_for_path_draw.array);
glUniformMatrix4fv(path_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(path_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(path_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glUniform4f(path_draw_id, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f);
//don't draw into ID array:
glColorMaski(1, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, next_active_chains_tristrip.count);
glColorMaski(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBindVertexArray(0);
glUseProgram(0);
}
//draw current traced stitches (tracing):
if (show & ShowTraced) {
if (traced_tristrip_dirty) update_traced_tristrip();
glm::mat4 p2c = camera.mvp();
glm::mat4x3 p2l = camera.mv();
glm::mat3 n2l = glm::inverse(glm::transpose(glm::mat3(p2l)));
glUseProgram(path_draw->program);
glBindVertexArray(traced_tristrip_for_path_draw.array);
glUniformMatrix4fv(path_draw_p2c, 1, GL_FALSE, glm::value_ptr(p2c));
glUniformMatrix4x3fv(path_draw_p2l, 1, GL_FALSE, glm::value_ptr(p2l));
glUniformMatrix3fv(path_draw_n2l, 1, GL_FALSE, glm::value_ptr(n2l));
glUniform4f(path_draw_id, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f, 0 / 255.0f);
//don't draw into ID array:
glColorMaski(1, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, traced_tristrip.count);
glColorMaski(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBindVertexArray(0);
glUseProgram(0);
}
GL_ERRORS();
//---------------------------------------------------
//copy rendered color to screen:
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glClearColor(1.0f, 0.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(copy_fb->program);
glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, color_tex);
glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, depth_tex);
glBindVertexArray(empty_vertex_array->array);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
GL_ERRORS();
}
void Interface::handle_event(SDL_Event const &evt) {
#define MAPX( X ) ((((X) + 0.5f) / kit::display.window_size.x) * 2.0f - 1.0f)
#define MAPY( Y ) ((((Y) + 0.5f) / kit::display.window_size.y) *-2.0f + 1.0f)
if (evt.type == SDL_MOUSEMOTION) {
glm::vec2 old_at = mouse.at;
mouse.at = glm::vec2( MAPX(evt.motion.x), MAPY(evt.motion.y) );
mouse.moved = true;
if (drag == DragCamera || drag == DragCameraFlipX) {
glm::vec2 d = mouse.at - old_at;
if (drag == DragCameraFlipX) d.x = -d.x;
camera.azimuth -= d.x;
camera.elevation -= d.y;
} else if (drag == DragCameraPan) {
glm::vec2 d = mouse.at - old_at;
glm::mat3 frame = glm::transpose(glm::mat3(camera.mv()));
camera.center -= 0.5f * (d.x * frame[0] + d.y * frame[1]) * camera.radius;
} else if (drag == DragConsPt) {
if (dragging.cons < constraints.size() && dragging.cons_pt < constraints[dragging.cons].chain.size()) {
if (hovered.vert < model.vertices.size()) {
constraints[dragging.cons].chain[dragging.cons_pt] = hovered.vert;
constraints_dirty = true; //TODO: only set when vert has changed.
}
} else {
drag = DragNone;
dragging.clear();
}
} else if (drag == DragConsRadius) {
if (dragging.cons < constraints.size() && dragging.cons_pt < constraints[dragging.cons].chain.size()) {
if (hovered.tri < model.triangles.size()) {
constraints[dragging.cons].radius = glm::length(hovered.point - model.vertices[constraints[dragging.cons].chain[dragging.cons_pt]]);
constraints_dirty = true;
}
} else {
drag = DragNone;
dragging.clear();
}
}
} else if (evt.type == SDL_MOUSEBUTTONDOWN) {
mouse.at = glm::vec2( MAPX(evt.button.x), MAPY(evt.button.y) );
mouse.moved = true;
if (evt.button.button == SDL_BUTTON_LEFT) {
if (drag == DragNone) {
if (hovered.cons < constraints.size() && hovered.cons_pt < constraints[hovered.cons].chain.size()) {
drag = DragConsPt;
dragging.cons = hovered.cons;
dragging.cons_pt = hovered.cons_pt;
}
}
} else if (evt.button.button == SDL_BUTTON_RIGHT) {
if (drag == DragNone) {
if (SDL_GetModState() & KMOD_SHIFT) {
drag = DragCameraPan;
} else {
if (std::cos(camera.elevation) > 0.0f) {
drag = DragCamera;
} else {
drag = DragCameraFlipX;
}
}
}
}
} else if (evt.type == SDL_MOUSEBUTTONUP) {
mouse.at = glm::vec2( MAPX(evt.button.x), MAPY(evt.button.y) );
mouse.moved = true;
if (evt.button.button == SDL_BUTTON_LEFT) {
if (drag == DragConsPt || drag == DragConsRadius) {
drag = DragNone;
dragging.clear();
}
} else if (evt.button.button == SDL_BUTTON_RIGHT) {
if (drag == DragCamera || drag == DragCameraFlipX || drag == DragCameraPan) {
drag = DragNone;
}
}
} else if (evt.type == SDL_MOUSEWHEEL) {
camera.radius *= std::pow(0.5f, evt.wheel.y / 10.0f);
if (camera.radius < 0.1f) camera.radius = 0.1f;
} else if (evt.type == SDL_KEYDOWN) {
if (evt.key.keysym.scancode == SDL_SCANCODE_S) {
if (show & ShowModel) show = (show & ~ShowModelBits) | ShowTimesModel;
else if (show & ShowTimesModel) show = (show & ~ShowModelBits) | ShowSlice;
else /*if (show & ShowSlice)*/ show = (show & ~ShowModelBits) | ShowModel;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_G) {
show = show ^ ShowRowColGraph;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_L) {
//show = ShowConstrainedModel;
DEBUG_test_linking();
} else if (evt.key.keysym.scancode == SDL_SCANCODE_P) {
if (evt.key.keysym.mod & KMOD_SHIFT) {
clear_peeling();
} else {
step_peeling();
}
} else if (evt.key.keysym.scancode == SDL_SCANCODE_T) {
update_traced();
} else if (evt.key.keysym.scancode == SDL_SCANCODE_C) {
if (hovered.cons < constraints.size()) {
if (drag == DragNone) {
auto &cons = constraints[hovered.cons];
cons.chain.insert(cons.chain.begin() + hovered.cons_pt, cons.chain[hovered.cons_pt]);
drag = DragConsPt;
dragging.cons = hovered.cons;
dragging.cons_pt = hovered.cons_pt + 1;
}
} else if (hovered.vert < model.vertices.size()) {
constraints.emplace_back();
constraints.back().chain.emplace_back(hovered.vert);
constraints_dirty = true;
}
} else if (evt.key.keysym.scancode == SDL_SCANCODE_R) {
if (hovered.cons < constraints.size()) {
if (drag == DragNone) {
if (constraints[hovered.cons].radius == 0.0f) {
drag = DragConsRadius;
dragging.cons = hovered.cons;
dragging.cons_pt = hovered.cons_pt;
} else {
constraints[hovered.cons].radius = 0.0f;
constraints_dirty = true;
}
}
}
} else if (evt.key.keysym.scancode == SDL_SCANCODE_X) {
if (hovered.cons < constraints.size() && hovered.cons_pt < constraints[hovered.cons].chain.size()) {
auto &cons = constraints[hovered.cons];
cons.chain.erase(
cons.chain.begin() + hovered.cons_pt
);
if (cons.chain.empty()) {
constraints.erase(constraints.begin() + hovered.cons);
}
constraints_dirty = true;
}
} else if (evt.key.keysym.scancode == SDL_SCANCODE_EQUALS || evt.key.keysym.scancode == SDL_SCANCODE_KP_PLUS) {
if (hovered.cons < constraints.size()) {
constraints[hovered.cons].value += 0.1f;
constraints_dirty = true;
}
} else if (evt.key.keysym.scancode == SDL_SCANCODE_MINUS || evt.key.keysym.scancode == SDL_SCANCODE_KP_MINUS) {
if (hovered.cons < constraints.size()) {