-
Notifications
You must be signed in to change notification settings - Fork 26
/
delabella-sdl2.cpp
3839 lines (3215 loc) · 110 KB
/
delabella-sdl2.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
/*
DELABELLA - Delaunay triangulation library
Copyright (C) 2018-2022 GUMIX - Marcin Sokalski
*/
#define _CRT_SECURE_NO_WARNINGS
//#define ANIMATION
//#define BENCH
//#define COMPARE // WITH_CDT must be also set
//#define CULLING
#define VORONOI
//#define VORONOI_POLYS
// otherwise EDGES
// override build define
#undef WITH_DELAUNATOR
//#define WITH_DELAUNATOR
// override build define
#undef WITH_CDT
//#define WITH_CDT
// override build define
#undef WITH_FADE
//#define WITH_FADE
// override build define
#undef WITH_TRIANGLE
//#define WITH_TRIANGLE
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <vector>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <random>
#include "delabella.h"
#undef GL_GLEXT_VERSION // silence MAC vs SDL2 fight
#include <GL/gl.h>
#ifndef _WIN32
#include <GL/glext.h>
#else
#pragma comment(lib,"OpenGL32.lib")
#pragma comment(lib,"SDL2.lib")
#undef main // on windows SDL does this weird thing
#endif
#ifdef WITH_FADE
#ifdef _WIN32
#pragma comment(lib,"libgmp-10.lib")
#ifdef _DEBUG
#pragma comment(lib,"fade2D_x64_v142_Debug.lib")
#else
#pragma comment(lib,"fade2D_x64_v142_Release.lib")
#endif
#endif
#include "fade/include_fade2d/Fade_2D.h"
using namespace GEOM_FADE2D;
#endif
// competitors
#ifdef WITH_DELAUNATOR
#include "delaunator-cpp/include/delaunator-header-only.hpp"
#endif
// competitors
#ifdef WITH_TRIANGLE
// we compile triangle.c as .cpp
//extern "C" {
#include "triangle/triangle.h"
//}
#endif
#ifdef WITH_CDT
#include "CDT/CDT/include/CDT.h"
namespace CDT
{
typedef std::vector<TriInd> Poly;
template <typename T, typename L = LocatorKDTree<T> >
std::vector<Poly> Polygonize(Triangulation<T, L>& cdt)
{
typedef TriInd PolyInd;
// for every tri, here we store index of the poly the tri belongs to
auto map = std::vector<PolyInd>(cdt.triangles.size());
// vector of Polys, each as a vector of tri indices belonging to the Poly
auto polys = std::vector<Poly>();
for (TriInd iT = 0; iT < (TriInd)cdt.triangles.size(); iT++)
{
const auto& tri = cdt.triangles[iT];
auto merged = false;
const auto& v1 = cdt.vertices[tri.vertices[0]];
const auto& v2 = cdt.vertices[tri.vertices[1]];
const auto& v3 = cdt.vertices[tri.vertices[2]];
// compare i'th tri with its adjacent tris
int e_from = 0, e_to = 1;
for (const auto adj : tri.neighbors)
{
// but only if there's adjacent tri and it is processed already ...
if (adj == noNeighbor || adj > iT)
{
e_from = e_to;
e_to = e_to == 2 ? 0 : e_to + 1;
continue;
}
// ... and edge between these 2 faces is not marked as fixed!
auto fix_it = cdt.fixedEdges.find(Edge(tri.vertices[e_from], tri.vertices[e_to]));
e_from = e_to;
e_to = e_to == 2 ? 0 : e_to + 1;
if (fix_it != cdt.fixedEdges.end())
continue;
// locate reflex vert in adj triangle
const auto& vr = cdt.vertices[opposedVertex(cdt.triangles[adj], iT)];
using predicates::adaptive::incircle;
if (!incircle(vr.x, vr.y, v1.x, v1.y, v2.x, v2.y, v3.x, v3.y))
{
if (!merged)
{
// append tri to already existing poly
merged = true;
const auto append_to = map[adj];
map[iT] = append_to;
polys[append_to].push_back(iT);
}
else
{
const auto merge_to = map[iT];
const auto merge_from = map[adj];
if (merge_to == merge_from)
continue;
// funny case, tri is a bridge between 2 polys merge'em all
// together
for (const auto i : polys[merge_from])
{
map[i] = merge_to; // remap
polys[merge_to].push_back(i); // merge
}
if (merge_from != (PolyInd)polys.size() - 1)
{
// replace merge_from poly with last poly in polys
polys[merge_from] = polys.back();
for (const auto i : polys[merge_from])
{
map[i] = merge_from; // remap
}
}
polys.pop_back();
}
}
}
if (!merged)
{
// at the moment, just alone tri
// make a new poly for it
map[iT] = (PolyInd)polys.size();
polys.push_back({ iT });
}
}
// post proc
struct Stepper
{
const Triangulation<T, L>& cdt;
TriInd current;
int around;
Stepper(const Triangulation<T, L>& cdt, TriInd t, int a) : cdt(cdt), current(t), around(a) {}
void StepOver(int a)
{
around = a;
}
TriInd Clockwise()
{
const auto& prev = cdt.triangles[current];
current = prev.neighbors[around];
const auto& next = cdt.triangles[current];
VertInd v = prev.vertices[around];
if (next.vertices[0] == v)
around = 0;
else
if (next.vertices[1] == v)
around = 1;
else
around = 2;
return current;
}
};
const PolyInd mask = (~(PolyInd)0) >> 1;
for (PolyInd p = 0; p < polys.size(); p++)
{
const PolyInd q = p | ~mask; // unmarked
// single triangle polys are ok already
if (polys[p].size() == 1)
continue;
// find good starting triangle,
// one with exeactly 1 inner edge
TriInd first = noNeighbor;
for (const auto t : polys[p])
{
if (first == noNeighbor)
{
const auto& tri = cdt.triangles[t];
int inner_edges =
(tri.neighbors[0] != noNeighbor && (map[tri.neighbors[0]] & mask) == p) +
(tri.neighbors[1] != noNeighbor && (map[tri.neighbors[1]] & mask) == p) +
(tri.neighbors[2] != noNeighbor && (map[tri.neighbors[2]] & mask) == p);
if (inner_edges == 1)
first = t;
}
// mark all tris as not inserted
map[t] = q;
}
// we can clear current poly now,
// as we depend only on map and adjacency
polys[p].clear();
TriInd f = first; // current face
bool step_on = false; // is current vertex inserted
int insert = 2; // first triangle should end with 2
Stepper it(cdt, f,
cdt.triangles[f].neighbors[0] != noNeighbor && map[cdt.triangles[f].neighbors[0]] == q ? 0 :
cdt.triangles[f].neighbors[1] != noNeighbor && map[cdt.triangles[f].neighbors[1]] == q ? 1 : 2);
while (1)
{
if (!step_on && map[f] == q)
{
step_on = true;
map[f] = p; // mark as inserted
if (it.around != insert)
{
auto& tri = cdt.triangles[f];
static const int rot[3][3] = { {0,1,2},{2,0,1},{1,2,0} };
const int r = rot[it.around][insert];
const auto v = tri.vertices[r];
const auto n = tri.neighbors[r];
switch (r)
{
case 1:
tri.vertices[1] = tri.vertices[0];
tri.vertices[0] = tri.vertices[2];
tri.vertices[2] = v;
tri.neighbors[1] = tri.neighbors[0];
tri.neighbors[0] = tri.neighbors[2];
tri.neighbors[2] = n;
break;
case 2:
tri.vertices[2] = tri.vertices[0];
tri.vertices[0] = tri.vertices[1];
tri.vertices[1] = v;
tri.neighbors[2] = tri.neighbors[0];
tri.neighbors[0] = tri.neighbors[1];
tri.neighbors[1] = n;
break;
default:
break;
}
it.StepOver(insert);
}
polys[p].push_back(f);
insert = 0; // everything but first should use 0
}
TriInd probe = cdt.triangles[f].neighbors[it.around];
if (probe == noNeighbor || (map[probe] & mask) != p)
{
// check if we've covered current vertex
// with some face before stepping over
assert(step_on);
// we're on last tri inside poly (marked or unmarked)
// step on other leg:
static const int other_leg[3] = { 1,2,0 };
it.StepOver(other_leg[it.around]);
step_on = false;
continue;
}
f = it.Clockwise();
if (f == first)
break;
}
}
return polys;
}
}
#else
#include "predicates.h" // we use them for panning expansions
#endif
PFNGLBINDBUFFERPROC glBindBuffer = 0;
PFNGLDELETEBUFFERSPROC glDeleteBuffers = 0;
PFNGLGENBUFFERSPROC glGenBuffers = 0;
PFNGLBUFFERDATAPROC glBufferData = 0;
PFNGLBUFFERSUBDATAPROC glBufferSubData = 0;
PFNGLMAPBUFFERPROC glMapBuffer = 0;
PFNGLUNMAPBUFFERPROC glUnmapBuffer = 0;
PFNGLPRIMITIVERESTARTINDEXPROC glPrimitiveRestartIndex = 0;
PFNGLCREATESHADERPROC glCreateShader = 0;
PFNGLDELETESHADERPROC glDeleteShader = 0;
PFNGLCREATEPROGRAMPROC glCreateProgram = 0;
PFNGLDELETEPROGRAMPROC glDeleteProgram = 0;
PFNGLSHADERSOURCEPROC glShaderSource = 0;
PFNGLCOMPILESHADERPROC glCompileShader = 0;
PFNGLATTACHSHADERPROC glAttachShader = 0;
PFNGLDETACHSHADERPROC glDetachShader = 0;
PFNGLLINKPROGRAMPROC glLinkProgram = 0;
PFNGLUSEPROGRAMPROC glUseProgram = 0;
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog = 0;
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog = 0;
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = 0;
PFNGLUNIFORM1IPROC glUniform1i = 0;
PFNGLUNIFORM4FPROC glUniform4f = 0;
//PFNGLUNIFORMMATRIX4DVPROC glUniformMatrix4dv = 0;
PFNGLUNIFORM4DVPROC glUniform4dv = 0;
PFNGLUNIFORM4FVPROC glUniform4fv = 0;
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer = 0;
PFNGLVERTEXATTRIBLPOINTERPROC glVertexAttribLPointer = 0;
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray = 0;
PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray = 0;
PFNGLGENVERTEXARRAYSPROC glGenVertexArrays = 0;
PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays = 0;
PFNGLBINDVERTEXARRAYPROC glBindVertexArray = 0;
PFNGLTEXBUFFERPROC glTexBuffer = 0;
bool BindGL()
{
#define BINDGL(proc) if ((*(void**)&proc = SDL_GL_GetProcAddress(#proc)) == 0) return false;
BINDGL(glBindBuffer);
BINDGL(glDeleteBuffers);
BINDGL(glGenBuffers);
BINDGL(glBufferData);
BINDGL(glBufferSubData);
BINDGL(glMapBuffer);
BINDGL(glUnmapBuffer);
BINDGL(glCreateShader);
BINDGL(glDeleteShader);
BINDGL(glCreateProgram);
BINDGL(glDeleteProgram);
BINDGL(glShaderSource);
BINDGL(glCompileShader);
BINDGL(glAttachShader);
BINDGL(glDetachShader);
BINDGL(glLinkProgram);
BINDGL(glUseProgram);
BINDGL(glGetShaderInfoLog);
BINDGL(glGetProgramInfoLog);
//BINDGL(glUniformMatrix4dv); // packed proj to vec4
BINDGL(glUniform4dv);
BINDGL(glUniform4fv);
BINDGL(glUniform4f);
BINDGL(glUniform1i);
BINDGL(glVertexAttribPointer);
BINDGL(glVertexAttribLPointer);
BINDGL(glEnableVertexAttribArray);
BINDGL(glDisableVertexAttribArray);
BINDGL(glGetUniformLocation);
BINDGL(glGenVertexArrays);
BINDGL(glDeleteVertexArrays);
BINDGL(glBindVertexArray);
BINDGL(glPrimitiveRestartIndex);
BINDGL(glTexBuffer);
#undef BINDGL
return true;
}
int errlog(void* stream, const char* fmt, ...)
{
va_list arg;
va_start(arg,fmt);
int ret = vfprintf((FILE*)stream, fmt, arg);
va_end(arg);
//fflush((FILE*)stream);
return ret;
}
static uint64_t uSec()
{
#ifdef _WIN32
LARGE_INTEGER c;
static LARGE_INTEGER f;
static BOOL bf = QueryPerformanceFrequency(&f);
QueryPerformanceCounter(&c);
uint64_t n = c.QuadPart;
uint64_t d = f.QuadPart;
uint64_t m = 1000000;
// calc microseconds = n*m/d carefully!
// naive mul/div would work only for upto 5h on 1GHz freq
// we exploit fact that m*d fits in uint64 (upto 18THz freq)
// so n%d*m fits as well,
return n / d * m + n % d * m / d;
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
#endif
}
// wrap GL buffer,
// so mapping works even if it doesnt
struct Buf
{
GLuint buf;
GLenum target;
GLsizeiptr size;
void* map;
bool mapped;
Buf() : buf(0),target(0),size(0),map(0),mapped(false) {}
GLuint Gen(GLenum t, GLsizeiptr s)
{
target = t;
size = s;
GLint push = 0;
if (target == GL_ARRAY_BUFFER)
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &push);
else
if (target == GL_ELEMENT_ARRAY_BUFFER)
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &push);
else
if (target == GL_TEXTURE_BUFFER)
glGetIntegerv(GL_TEXTURE_BUFFER_BINDING, &push);
glGenBuffers(1, &buf);
glBindBuffer(target, buf);
glBufferData(target, size, 0, GL_STATIC_DRAW);
glBindBuffer(target, push);
return buf;
}
void Del()
{
if (buf)
{
Unmap();
glDeleteBuffers(1,&buf);
buf = 0;
}
}
void* Map()
{
if (map || !buf)
return 0;
GLint push = 0;
if (target == GL_ARRAY_BUFFER)
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &push);
else
if (target == GL_ELEMENT_ARRAY_BUFFER)
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &push);
else
if (target == GL_TEXTURE_BUFFER)
glGetIntegerv(GL_TEXTURE_BUFFER_BINDING, &push);
glBindBuffer(target, buf);
map = glMapBuffer(target, GL_WRITE_ONLY);
mapped = true;
if (!map)
{
map = malloc(size);
mapped = false;
}
assert(map);
glBindBuffer(target, push);
return map;
}
void Unmap()
{
if (!map || !buf)
return;
GLint push = 0;
if (target == GL_ARRAY_BUFFER)
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &push);
else
if (target == GL_ELEMENT_ARRAY_BUFFER)
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &push);
else
if (target == GL_TEXTURE_BUFFER)
glGetIntegerv(GL_TEXTURE_BUFFER_BINDING, &push);
glBindBuffer(target, buf);
if (mapped)
{
GLboolean buf_unmap_ok = glUnmapBuffer(target);
assert(buf_unmap_ok);
}
else
{
glBufferSubData(target, 0, size, map);
free(map);
}
map = 0;
mapped = false;
glBindBuffer(target, push);
}
void Bind()
{
glBindBuffer(target,buf);
}
};
typedef double MyCoord;
//typedef intptr_t MyIndex;
//#define IDXF "%zd"
typedef int32_t MyIndex;
#define IDXF "%d"
typedef IDelaBella2<MyCoord, MyIndex> IDelaBella;
typedef IDelaBella::Vertex DelaBella_Vertex;
typedef IDelaBella::Simplex DelaBella_Triangle;
struct MyPoint
{
MyPoint() {}
MyPoint(MyCoord x, MyCoord y) : x(x), y(y) {}
MyCoord x;
MyCoord y;
// operators used by result comparator
bool operator == (const MyPoint& p) const
{
return x == p.x && y == p.y;
}
bool operator < (const MyPoint& p) const
{
if (x < p.x)
return true;
if (x == p.x)
return y < p.y;
return false;
}
};
struct MyEdge
{
MyEdge(MyIndex a, MyIndex b) : a(a), b(b) {}
MyIndex a, b;
};
struct GfxStuffer
{
GfxStuffer() { memset(this, 0, sizeof(GfxStuffer)); }
GLenum type;
Buf vbo, ibo_delabella, ibo_constraint;
Buf vbo_voronoi, ibo_voronoi;
GLuint texbuf;
Buf tbo;
#ifdef WITH_CDT
Buf ibo_cdt;
#endif
MyCoord box[4];
#ifdef CULLING
MyCoord* max_tri_len;
MyCoord* max_vor_len;
MyCoord* max_con_len;
MyIndex ConsByScale(MyIndex num, double scale)
{
const double thr = 6;
if (num < 2)
return num;
MyCoord* len = max_con_len;
if (len[num - 1] * scale > thr)
return num;
if (len[0] * scale <= thr)
return 0;
MyIndex lo = 1, hi = num - 2;
while (lo < hi)
{
MyIndex med = (lo + hi) / 2;
if (len[med] * scale > thr)
lo = med + 1;
else
hi = med - 1;
}
return lo + 1;
}
MyIndex VoroByScale(MyIndex num, double scale)
{
const double thr = 6;
if (num < 2)
return num;
MyCoord* len = max_vor_len;
if (len[num - 1] * scale > thr)
return num;
if (len[0] * scale <= thr)
return 0;
MyIndex lo = 1, hi = num - 2;
while (lo < hi)
{
MyIndex med = (lo + hi) / 2;
if (len[med] * scale > thr)
lo = med + 1;
else
hi = med - 1;
}
return lo + 1;
}
MyIndex TrisByScale(MyIndex num, double scale)
{
const double thr = 6;
if (num < 2)
return num;
MyCoord* len = max_tri_len;
if (len[num-1] * scale > thr)
return num;
if (len[0] * scale <= thr)
return 0;
MyIndex lo = 1, hi = num - 2;
while (lo < hi)
{
MyIndex med = (lo + hi) / 2;
if (len[med] * scale > thr)
lo = med + 1;
else
hi = med - 1;
}
return lo + 1;
}
#endif
struct Vao
{
Vao() : vao(0) {}
GLuint Gen()
{
glGenVertexArrays(1, &vao);
return vao;
}
void Bind()
{
glBindVertexArray(vao);
}
void Del()
{
if (vao)
glDeleteVertexArrays(1, &vao);
vao = 0;
}
GLuint vao;
};
Vao vao_main, vao_constraint, vao_voronoi, vao_cdt;
GLuint prg;
GLint tfm;
GLint low;
GLint clr;
GLint tex;
void LoadProj(int vpw, int vph, double cx, double cy, double scale, double lx, double ly)
{
glViewport(0,0,vpw,vph);
if (type == GL_DOUBLE)
{
glUseProgram(prg);
GLdouble mat[4];
mat[0] = scale / vpw;
mat[1] = scale / vph;
mat[2] = -cx;
mat[3] = -cy;
glUniform4dv(tfm, 1, mat);
GLdouble lxy[4] = { -lx, -ly, 0.0, 0.0 }; // zw-spare
glUniform4dv(low, 1, lxy);
}
else
{
glUseProgram(prg);
GLfloat mat[4];
mat[0] = (GLfloat)(scale / vpw);
mat[1] = (GLfloat)(scale / vph);
mat[2] = (GLfloat)(-cx);
mat[3] = (GLfloat)(-cy);
glUniform4fv(tfm, 1, mat);
GLfloat lxy[4] = { -(GLfloat)lx, -(GLfloat)ly, 0.0f, 0.0f }; // zw-spare
glUniform4fv(low, 1, lxy);
}
}
void SetColor(float r, float g, float b, float a)
{
glUniform4f(clr, r,g,b,a);
}
void Destroy()
{
glBindTexture(GL_TEXTURE_BUFFER, 0);
glDeleteTextures(1,&texbuf);
tbo.Del();
#ifdef CULLING
if (max_tri_len)
free(max_tri_len);
#ifdef VORONOI
if (max_vor_len)
free(max_vor_len);
#endif
#endif
vao_main.Del();
vao_constraint.Del();
vao_voronoi.Del();
vao_cdt.Del();
glUseProgram(0);
if (prg)
glDeleteProgram(prg);
vbo.Del();
ibo_delabella.Del();
ibo_constraint.Del();
#ifdef WITH_CDT
ibo_cdt.Del();
#endif
#ifdef VORONOI
vbo_voronoi.Del();
ibo_voronoi.Del();
#endif
}
void Upload(
GLenum gl_e,
const IDelaBella* idb,
MyIndex points,
const MyPoint* cloud,
MyIndex constrain_edges,
const MyEdge* force
#ifdef VORONOI
, MyIndex voronoi_vertices,
const MyPoint* voronoi_vtx_buf,
MyIndex voronoi_indices,
const MyIndex* voronoi_idx_buf
#endif
#ifdef WITH_CDT
, const CDT::Triangulation<MyCoord>& cdt,
const CDT::DuplicatesInfo& dups
#endif
)
{
Destroy();
assert(points >= 0);
assert(constrain_edges >= 0);
#ifdef VORONOI
assert(voronoi_vertices >= 0);
assert(voronoi_indices >= 0);
#endif
vao_main.Gen();
type = gl_e;
static const char* vs_src[1] = { 0 };
static const char* fs_src[1] = { 0 };
#define CODE(...) #__VA_ARGS__
fs_src[0] = CODE(#version 410\n
uniform vec4 clr;
uniform usamplerBuffer tex;
layout (location = 0) out vec4 c;
void main()
{
uint flags = texelFetch(tex, gl_PrimitiveID).r;
if ((flags & 0x40) != 0)
c = clr + vec4(0.25);
else
c = clr;
}
);
if (type == GL_DOUBLE)
{
vs_src[0] = CODE(#version 410\n
uniform dvec4 tfm;
uniform dvec4 low;
layout (location = 0) in dvec3 v;
void main()
{
gl_Position = vec4(dvec4(tfm.xy*(v.xy + tfm.zw * v.z + low.xy * v.z), 0.0lf, v.z));
}
);
}
else
{
vs_src[0] = CODE(#version 330\n
uniform vec4 tfm;
uniform vec4 low;
layout (location = 0) in vec3 v;
void main()
{
gl_Position = vec4(tfm.xy*(v.xy + tfm.zw * v.z + low.xy * v.z), 0.0, v.z);
}
);
}
#undef CODE
/*
char nfolog[1025];
int nfolen;
*/
prg = glCreateProgram();
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, vs_src, 0);
glCompileShader(vs);
glAttachShader(prg,vs);
/*
glGetShaderInfoLog(vs,1024,&nfolen,nfolog);
nfolog[nfolen]=0;
printf("VS:\n%s\n\n",nfolog);
*/
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, fs_src, 0);
glCompileShader(fs);
glAttachShader(prg,fs);
/*
glGetShaderInfoLog(vs,1024,&nfolen,nfolog);
nfolog[nfolen]=0;
printf("FS:\n%s\n\n",nfolog);
*/
glLinkProgram(prg);
glDeleteShader(vs);
glDeleteShader(fs);
tfm = glGetUniformLocation(prg, "tfm");
low = glGetUniformLocation(prg, "low");
clr = glGetUniformLocation(prg, "clr");
tex = glGetUniformLocation(prg, "tex");
size_t gl_s = type == GL_DOUBLE ? sizeof(GLdouble) : sizeof(GLfloat);
MyIndex tris_delabella = idb->GetNumOutputIndices() / 3;
MyIndex contour = idb->GetNumBoundaryVerts();
if (constrain_edges)
{
vao_constraint.Gen();
ibo_constraint.Gen(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint[2]) * (size_t)constrain_edges);
GLuint* map = (GLuint*)ibo_constraint.Map();
#ifdef CULLING
struct ConSort
{
MyIndex e;
MyCoord weight;
bool operator < (const ConSort& b) const
{
return weight > b.weight;
}
};
ConSort* consort = (ConSort*)malloc(sizeof(ConSort) * (size_t)constrain_edges);
assert(consort);
for (MyIndex i = 0; i < constrain_edges; i++)
{
consort[i].e = i;
MyIndex i0 = force[i].a;
MyIndex i1 = force[i].b;
MyCoord v01[2] = { cloud[i1].x - cloud[i0].x, cloud[i1].y - cloud[i0].y };
MyCoord sqr = v01[0] * v01[0] + v01[1] * v01[1];
consort[i].weight = sqrt(sqr);
}
std::sort(consort, consort + constrain_edges);
max_con_len = (MyCoord*)malloc(sizeof(MyCoord) * (size_t)constrain_edges);
assert(max_con_len);
for (MyIndex i = 0; i < constrain_edges; i++)
{
MyIndex e = consort[i].e;
map[2 * i + 0] = (GLuint)force[e].a;
map[2 * i + 1] = (GLuint)force[e].b;
max_con_len[i] = consort[i].weight;
}
free(consort);
#else
for (int i = 0; i < constrain_edges; i++)
{
map[2 * i + 0] = (GLuint)force[i].a;
map[2 * i + 1] = (GLuint)force[i].b;
}
#endif
ibo_constraint.Unmap();
}
vbo.Gen(GL_ARRAY_BUFFER, gl_s * 3 * (size_t)points);
void* vbo_ptr = vbo.Map();