-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.cpp
1577 lines (1313 loc) · 51.3 KB
/
client.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 <string>
#include <vector>
#include "client.h"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wfloat-equal"
#include "imgui/imgui.h"
#pragma clang diagnostic pop
#else
#include "imgui/imgui.h"
#endif
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
#pragma clang diagnostic ignored "-Wcast-align"
#pragma clang diagnostic ignored "-Wconversion"
#pragma clang diagnostic ignored "-Wimplicit-fallthrough"
#include "stb_image.h"
#include "stb_image_write.h"
#pragma clang diagnostic pop
#else
#pragma warning(push)
#pragma warning(disable: 4242)
#pragma warning(disable: 4244)
#pragma warning(disable: 4365)
#include "stb_image.h"
#pragma warning(disable: 4996)
#include "stb_image_write.h"
#pragma warning(pop)
#endif
kernel_services *g_services = nullptr;
kernel_services::TexID g_font_texture_id;
std::vector<kernel_services::MeshID> g_ui_meshes;
struct ImageDesc
{
kernel_services::TexID texid;
u32 width;
u32 height;
};
// TODO: next two vectors have same size
// image ids in art are indexes in those vectors
std::vector<ImageDesc> g_loaded_images;
std::vector<std::string> g_loaded_image_names;
RenderRecipe g_recipe;
kernel_services::MeshID g_lines_mesh;
std::vector<kernel_services::MeshID> g_bake_meshes;
std::vector<u32> g_bake_mesh_sizes;
size_t g_max_bake_mesh_idx;
kernel_services::MeshID g_curr_mesh;
std::vector<ImDrawVert> g_lines_vb;
struct CurrentImage
{
std::string name;
float aspect_ratio;
u32 imageid;
CurrentImage() : aspect_ratio(1.0f), imageid(0) {}
} g_fit_img;
enum CapturingStage
{
INACTIVE,
SELECTION,
CAPTURE
};
CapturingStage g_image_capturing;
void clear_loaded_images()
{
for (size_t i = 0; i < g_loaded_images.size(); ++i)
{
g_services->delete_texture(g_loaded_images[i].texid);
}
g_loaded_images.clear();
g_loaded_image_names.clear();
}
size_t image_name_idx(const char *name)
{
size_t result = 0;
for (; result < g_loaded_image_names.size(); ++result)
{
if (g_loaded_image_names[result] == name)
break;
}
return result;
}
// TODO: ImDrawIdx vs u16 in proto.cpp
void RenderImGuiDrawLists(ImDrawData *drawData)
{
for (size_t li = 0; li < static_cast<size_t>(drawData->CmdListsCount); ++li)
{
const ImDrawList *drawList = drawData->CmdLists[li];
const ImDrawIdx *indexBuffer = &drawList->IdxBuffer.front();
const ImDrawVert *vertexBuffer = &drawList->VtxBuffer.front();
u32 vtxCount = static_cast<u32>(drawList->VtxBuffer.size());
u32 idxCount = static_cast<u32>(drawList->IdxBuffer.size());
if (g_ui_meshes.size() <= li)
{
kernel_services::MeshID mid =
g_services->create_mesh(*g_services->ui_vertex_layout,
vtxCount, idxCount);
g_ui_meshes.push_back(mid);
}
g_services->update_mesh(g_ui_meshes[li],
vertexBuffer, vtxCount,
indexBuffer, idxCount);
i32 cmdCnt = drawList->CmdBuffer.size();
u32 idxOffs = 0;
for (i32 ci = 0; ci < cmdCnt; ++ci)
{
const ImDrawCmd &cmd = drawList->CmdBuffer[ci];
u32 idxCnt = cmd.ElemCount;
if (cmd.UserCallback)
{
cmd.UserCallback(drawList, &cmd);
}
else
{
GuiDrawcall dc;
size_t texId = reinterpret_cast<size_t>(cmd.TextureId);
dc.texture = static_cast<kernel_services::TexID>(texId);
dc.mesh = g_ui_meshes[li];
dc.offset = idxOffs;
dc.count = idxCnt;
g_recipe.guiDCs.push_back(dc);
/*
float xmin = cmd.ClipRect.x;
float ymin = cmd.ClipRect.y;
float xmax = cmd.ClipRect.z;
float ymax = cmd.ClipRect.w;
*/
}
idxOffs += idxCnt;
}
}
}
#include "vandalism.cpp"
const char *g_roman_numerals[11] =
{
"0", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"
};
Vandalism *g_ism = nullptr;
i32 gui_tool;
i32 gui_fit_tool;
color4f gui_brush_color;
i32 gui_brush_diameter_units;
i32 gui_brush_spread_units;
i32 gui_brush_angle;
bool gui_mouse_occupied;
bool gui_mouse_hover;
bool gui_fake_pressure;
bool gui_debug_smoothing;
bool gui_debug_draw_rects;
bool gui_debug_draw_disks;
bool gui_draw_simplify;
i32 gui_draw_smooth;
i32 gui_present_smooth;
color3f gui_background_color;
color3f gui_grid_bg_color;
color3f gui_grid_fg_color;
bool gui_grid_enabled;
float gui_eraser_alpha;
float gui_smooth_error_order;
i32 gui_goto_idx;
bool gui_layer_active[255];
i32 gui_layer_cnt;
i32 gui_current_layer;
bool gui_current_layer_changed;
bool gui_layer_active_changed[255];
struct stringlog
{
static const u32 SIZE = 10;
std::string strings[SIZE];
u32 times[SIZE];
u32 counter;
stringlog() : counter(0)
{
::memset(times, 0, SIZE * sizeof(u32));
}
void append(const std::string &str)
{
if (strings[counter % SIZE] == str)
{
++times[counter % SIZE];
}
else
{
++counter;
strings[counter % SIZE] = str;
times[counter % SIZE] = 1;
}
}
const char *get_text(u32 relIdx)
{
return strings[(counter + 1 + relIdx) % SIZE].c_str();
}
u32 get_times(u32 relIdx)
{
return times[(counter + 1 + relIdx) % SIZE];
}
};
stringlog g_dclog;
stringlog g_protolog;
const i32 cfg_min_brush_diameter_units = 1;
const i32 cfg_max_brush_diameter_units = 64;
const i32 cfg_def_brush_diameter_units = 4;
const float cfg_brush_diameter_inches_per_unit = 1.0f / 64.0f;
const i32 cfg_max_strokes_per_buffer = 8192;
const float cfg_depth_step = 1.0f / cfg_max_strokes_per_buffer;
const char* cfg_font_path = "Roboto_Condensed/RobotoCondensed-Regular.ttf";
const char* cfg_default_image_file = "default_image.jpg";
const char* cfg_default_capture_file = "default_capture.png";
const char* cfg_default_file = "default.ism";
const float cfg_capture_width_in = 4.0f;
const float cfg_capture_height_in = 4.0f;
ImGuiTextBuffer *g_viewsBuf;
std::vector<output_data::Vertex> g_bake_quads;
std::vector<output_data::Vertex> g_curr_quads;
const u32 VTX_PER_MESH = 65536;
void setup(kernel_services *services)
{
ImGuiIO& io = ImGui::GetIO();
io.RenderDrawListsFn = RenderImGuiDrawLists;
g_viewsBuf = new ImGuiTextBuffer;
u8 *pixels;
i32 width, height;
if (services->check_file(cfg_font_path))
{
ImFontConfig config;
config.OversampleH = 3;
config.OversampleV = 3;
io.Fonts->AddFontFromFileTTF(cfg_font_path, 16.0f, &config);
}
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
g_font_texture_id = services->create_texture(static_cast<u32>(width),
static_cast<u32>(height),
4);
services->update_texture(g_font_texture_id, pixels);
io.Fonts->TexID =
reinterpret_cast<void *>(static_cast<size_t>(g_font_texture_id));
g_services = services;
const u32 BAKE_MESHES_CNT = 5;
const u32 BAKE_QUADS_CNT = 16384;
const u32 CURR_QUADS_CNT = 16384;
g_bake_quads.reserve(BAKE_QUADS_CNT * 4);
g_curr_quads.reserve(CURR_QUADS_CNT * 4);
for (u32 i = 0; i < BAKE_MESHES_CNT; ++i)
{
g_bake_meshes.push_back(g_services->
create_quad_mesh(*g_services->stroke_vertex_layout,
BAKE_QUADS_CNT * 4));
}
g_bake_mesh_sizes.resize(BAKE_MESHES_CNT);
g_curr_mesh =
g_services->create_quad_mesh(*g_services->stroke_vertex_layout,
CURR_QUADS_CNT * 4);
g_lines_mesh =
g_services->create_quad_mesh(*g_services->ui_vertex_layout, 40);
io.Fonts->ClearInputData();
io.Fonts->ClearTexData();
g_ism = new Vandalism();
g_ism->setup();
gui_tool = Vandalism::DRAW;
gui_fit_tool = Vandalism::FT_START;
gui_brush_color = { 1.0, 1.0f, 1.0f, 1.0f };
gui_background_color = { 0.3f, 0.3f, 0.3f };
gui_grid_bg_color = { 0.0f, 0.0f, 0.5f };
gui_grid_fg_color = { 0.5f, 0.5f, 1.0f };
gui_grid_enabled = false;
gui_brush_diameter_units = cfg_def_brush_diameter_units;
gui_brush_spread_units = 0;
gui_eraser_alpha = 1.0f;
gui_smooth_error_order = -3.0f;
gui_mouse_occupied = false;
gui_mouse_hover = false;
gui_fake_pressure = false;
gui_debug_smoothing = false;
gui_debug_draw_disks = true;
gui_debug_draw_rects = true;
gui_draw_simplify = false;
gui_draw_smooth = static_cast<i32>(Vandalism::NONE);
gui_present_smooth = static_cast<i32>(Vandalism::NONE);
gui_goto_idx = 0;
for (u8 i = 0; i < 255; ++i)
{
gui_layer_active[i] = true;
gui_layer_active_changed[i] = true;
}
gui_current_layer = 0;
gui_current_layer_changed = true;
gui_layer_cnt = static_cast<i32>(5);
g_image_capturing = INACTIVE;
}
void cleanup()
{
g_ism->clear();
clear_loaded_images();
delete g_ism;
for (size_t i = 0; i < g_ui_meshes.size(); ++i)
{
g_services->delete_mesh(g_ui_meshes[i]);
}
for (size_t i = 0; i < g_bake_meshes.size(); ++i)
{
g_services->delete_mesh(g_bake_meshes[i]);
}
g_services->delete_mesh(g_curr_mesh);
g_services->delete_mesh(g_lines_mesh);
g_ui_meshes.clear();
g_bake_meshes.clear();
g_recipe.clear();
g_services->delete_texture(g_font_texture_id);
delete g_viewsBuf;
ImGui::Shutdown();
}
// TODO: optimize with triangle fans/strips maybe?
void add_quad(std::vector<output_data::Vertex> &quads,
float2 a, float2 b, float2 c, float2 d,
float zindex, float ec,
float rc, float gc, float bc, float ac,
float u0, float u1)
{
output_data::Vertex v;
v.z = zindex;
v.e = ec;
v.r = rc; v.g = gc; v.b = bc; v.a = ac;
v.x = a.x; v.y = a.y; v.u = u0; v.v = 0.0f; quads.push_back(v);
v.x = b.x; v.y = b.y; v.u = u0; v.v = 1.0f; quads.push_back(v);
v.x = c.x; v.y = c.y; v.u = u1; v.v = 1.0f; quads.push_back(v);
v.x = d.x; v.y = d.y; v.u = u1; v.v = 0.0f; quads.push_back(v);
}
void add_rect(std::vector<output_data::Vertex>& quads,
float2 p0, float2 p1, float2 side0, float2 side1,
const basis2s& tform,
const Masterpiece::Brush &brush,
float zindex)
{
float2 A = point_in_basis(tform, p0 + side0);
float2 B = point_in_basis(tform, p0 - side0);
float2 C = point_in_basis(tform, p1 - side1);
float2 D = point_in_basis(tform, p1 + side1);
add_quad(quads,
A, B, C, D, zindex, (brush.type == 1 ? brush.color.a : 0.0f),
brush.color.r, brush.color.g, brush.color.b, (brush.type == 1 ? 0.0f : brush.color.a),
0.5f, 0.5f);
}
void add_disk(std::vector<output_data::Vertex>& quads,
float2 center, float2 right, float2 up,
const basis2s& tform,
const Masterpiece::Brush &brush,
float zindex)
{
float2 bl = center - right - up;
float2 br = center + right - up;
float2 tl = center - right + up;
float2 tr = center + right + up;
float2 A = point_in_basis(tform, bl);
float2 B = point_in_basis(tform, tl);
float2 C = point_in_basis(tform, tr);
float2 D = point_in_basis(tform, br);
add_quad(quads,
A, B, C, D, zindex, (brush.type == 1 ? brush.color.a : 0.0f),
brush.color.r, brush.color.g, brush.color.b, (brush.type == 1 ? 0.0f : brush.color.a),
0.0f, 1.0f);
}
void modify_brush(Masterpiece::Brush b, float w)
{
b.color.a *= w;
b.diameter *= w;
b.spread *= w;
}
void fill_quads(std::vector<output_data::Vertex>& quads,
const test_point *points, size_t N,
size_t si,
const basis2s& tform,
const Masterpiece::Brush& brush,
float depthStep)
{
if (N == 0)
{
return;
}
float zindex = depthStep * (si + 1);
if (gui_debug_draw_rects)
{
Masterpiece::Brush b1 = brush;
modify_brush(b1, points[0].w);
if (b1.spread > 0.0)
{
float2 spr1n = {si_cosf(b1.angle), si_sinf(b1.angle)};
float2 spr1 = b1.spread * spr1n;
float2 side = 0.5f * b1.diameter * perp(spr1n);
float2 p1 = {points[0].x, points[0].y};
add_rect(quads, p1 - spr1, p1 + spr1,
side, side,
tform, b1, zindex);
}
// rectangles between points
for (size_t i = 1; i < N; ++i)
{
float2 prevC = {points[i-1].x, points[i-1].y};
float2 currC = {points[i].x, points[i].y};
float2 dirC = currC - prevC;
if (len(dirC) > 0.001f)
{
Masterpiece::Brush cb0 = brush;
modify_brush(cb0, points[i-1].w);
Masterpiece::Brush cb1 = brush;
modify_brush(cb1, points[i].w);
if (cb0.spread > 0.0 || cb1.spread > 0.0)
{
float2 spr0n = {si_cosf(cb0.angle), si_sinf(cb0.angle)};
float2 spr0 = cb0.spread * spr0n;
float2 spr1n = {si_cosf(cb1.angle), si_sinf(cb1.angle)};
float2 spr1 = cb1.spread * spr1n;
float2 prevL = prevC - spr0;
float2 currL = currC - spr1;
float2 dirL = currL - prevL;
// TODO: this is inaccurate,
// multiplying this with prev and curr sizes
// will not produce real tangents to circles
float2 sideL = norm(perp(dirL));
// TODO: interp color/alpha across rect
// left edge
add_rect(quads, prevL, currL,
sideL * cb0.diameter * 0.5f,
sideL * cb1.diameter * 0.5f,
tform, cb0, zindex);
float2 prevR = prevC + spr0;
float2 currR = currC + spr1;
float2 dirR = currR - prevR;
// TODO: this is inaccurate,
// multiplying this with prev and curr sizes
// will not produce real tangents to circles
float2 sideR = norm(perp(dirR));
// TODO: interp color/alpha across rect
// right edge
add_rect(quads, prevR, currR,
sideR * cb0.diameter * 0.5f,
sideR * cb1.diameter * 0.5f,
tform, cb0, zindex);
// TODO: interp color/alpha across rect
// fill
add_rect(quads, prevC, currC,
spr0, spr1,
tform, cb0, zindex);
// across edge
float2 side = 0.5f * cb1.diameter * perp(spr1n);
add_rect(quads, currC - spr1, currC + spr1,
side, side,
tform, cb1, zindex);
}
else
{
float2 side = norm(perp(dirC));
add_rect(quads, prevC, currC,
side * cb0.diameter * 0.5f,
side * cb1.diameter * 0.5f,
tform, brush, zindex);
}
}
}
}
if (gui_debug_draw_disks)
{
// disks at points
for (size_t i = 0; i < N; ++i)
{
Masterpiece::Brush cb = brush;
modify_brush(cb, points[i].w);
float2 curr = {points[i].x, points[i].y};
float2 right = {0.5f * cb.diameter, 0.0f};
float2 up = {0.0, 0.5f * cb.diameter};
if (cb.spread > 0.0)
{
float2 side = {cb.spread * si_cosf(cb.angle),
cb.spread * si_sinf(cb.angle)};
add_disk(quads, curr + side, right, up,
tform, cb, zindex);
add_disk(quads, curr - side, right, up,
tform, cb, zindex);
}
else
{
add_disk(quads, curr, right, up,
tform, cb, zindex);
}
}
}
}
void stroke_to_quads(const test_point* begin, const test_point* end,
std::vector<output_data::Vertex>& quads,
size_t stroke_id, const basis2s& tform,
const Masterpiece::Brush& brush)
{
static Masterpiece::Brush s_debug_brush =
{
0.02f, 0.0f, 0.0f,
{1.0f, 0.0f, 0.0f, 1.0f},
false
};
static std::vector<test_point> s_sampled_points;
if (gui_present_smooth == Vandalism::FITBEZIER ||
gui_present_smooth == Vandalism::HERMITE)
{
s_sampled_points.clear();
smooth_stroke(begin, static_cast<size_t>(end - begin),
s_sampled_points,
si_powf(10.0f, gui_smooth_error_order),
gui_present_smooth == Vandalism::FITBEZIER);
if (gui_debug_smoothing)
{
fill_quads(quads, begin, static_cast<size_t>(end - begin),
stroke_id, tform, s_debug_brush,
cfg_depth_step);
}
fill_quads(quads,
s_sampled_points.data(), s_sampled_points.size(),
stroke_id, tform, brush,
cfg_depth_step);
}
else
{
fill_quads(quads, begin, static_cast<size_t>(end - begin),
stroke_id, tform, brush,
cfg_depth_step);
}
}
void append_bake_drawcalls(std::vector<StrokesDrawcall> &strokesDCs,
LayerRecipe &recipe,
u32 &vertexCount,
const std::vector<kernel_services::MeshID> &meshes,
std::vector<u32> &meshSizes,
size_t &meshesUsed)
{
if (vertexCount > 0)
{
if (recipe.items.empty() ||
recipe.items.back().type != LayerRecipe::ItemSpec::STROKEBATCH ||
meshesUsed == 0 ||
meshSizes[meshesUsed - 1] == VTX_PER_MESH)
{
bool nextMesh = (meshesUsed == 0 || meshSizes[meshesUsed - 1] == VTX_PER_MESH);
u32 offset = nextMesh ? 0 : meshSizes[meshesUsed - 1] / 4 * 6;
if (nextMesh) ++meshesUsed;
StrokesDrawcall dc;
dc.mesh = meshes[meshesUsed - 1];
dc.offset = offset;
dc.count = 0;
recipe.items.push_back({ LayerRecipe::ItemSpec::STROKEBATCH, static_cast<u32>(strokesDCs.size()) });
strokesDCs.push_back(dc);
}
u32 room = VTX_PER_MESH - meshSizes[meshesUsed - 1];
u32 portion = (vertexCount >= room ? room : vertexCount);
strokesDCs[recipe.items.back().index].count += portion / 4 * 6;
vertexCount -= portion;
meshSizes[meshesUsed - 1] += portion;
}
}
void collect_bake_data(const test_data& bake_data,
float width_in, float height_in,
float pixel_height_in,
u8 layer_id, u8 current_id)
{
static std::vector<test_visible> s_visibles;
static std::vector<basis2s> s_transforms;
s_visibles.clear();
s_transforms.clear();
box2 viewbox = {-0.5f * width_in,
+0.5f * width_in,
-0.5f * height_in,
+0.5f * height_in};
query(layer_id, bake_data, g_ism->currentView,
viewbox, s_visibles, s_transforms,
pixel_height_in);
LayerRecipe recipe;
for (u32 visIdx = 0; visIdx < s_visibles.size(); ++visIdx)
{
const test_visible& vis = s_visibles[visIdx];
const basis2s& tform = s_transforms[vis.tform_id];
if (vis.ty == test_visible::STROKE)
{
const test_stroke& s = bake_data.strokes[vis.obj_id];
const Masterpiece::Brush& brush = g_ism->art.get_brush(s.brush_id);
u32 vtxCountBefore = static_cast<u32>(g_bake_quads.size());
stroke_to_quads(bake_data.points + s.pi0,
bake_data.points + s.pi1,
g_bake_quads, vis.obj_id, tform, brush);
u32 vtxCountAfter = static_cast<u32>(g_bake_quads.size());
u32 remaining = vtxCountAfter - vtxCountBefore;
while (remaining > 0)
{
append_bake_drawcalls(g_recipe.strokeDCs, recipe, remaining,
g_bake_meshes, g_bake_mesh_sizes, g_max_bake_mesh_idx);
}
}
else if (vis.ty == test_visible::IMAGE)
{
const test_image &img = bake_data.images[vis.obj_id];
float2 pos = point_in_basis(tform, img.basis.o);
float2 ox = point_in_basis(tform, img.basis.o + img.basis.x);
float2 oy = point_in_basis(tform, img.basis.o + img.basis.y);
ImageDrawcall dc;
dc.basis.o = pos;
dc.basis.x = ox - pos;
dc.basis.y = oy - pos;
dc.texture = g_loaded_images[img.nameidx].texid;
recipe.items.push_back({ LayerRecipe::ItemSpec::IMAGE, static_cast<u32>(g_recipe.imageDCs.size()) });
g_recipe.imageDCs.push_back(dc);
}
}
if (layer_id > current_id)
g_recipe.aboveBakery.push_back(recipe);
else if (layer_id < current_id)
g_recipe.belowBakery.push_back(recipe);
else
g_recipe.currentBakery = recipe;
std::cout << "layer #" << static_cast<u32>(layer_id) << " update mesh: " << s_visibles.size() << " visibles" << std::endl;
}
bool load_image(const char *filename, ImageDesc &desc)
{
if (g_services->check_file(filename))
{
i32 image_w, image_h, image_comp;
float *image_data = stbi_loadf(filename, &image_w, &image_h, &image_comp, 4);
if (image_comp > 0 && image_w > 0 && image_h > 0 && image_data != nullptr)
{
desc.width = static_cast<u32>(image_w);
desc.height = static_cast<u32>(image_h);
size_t pixel_cnt = desc.width * desc.height * 4;
std::vector<u8> udata(pixel_cnt);
const float *pIn = image_data;
u8 *pOut = udata.data();
for (size_t i = 0; i < pixel_cnt; ++i)
{
*(pOut++) = static_cast<u8>(*(pIn++) * 255.0f);
}
stbi_image_free(image_data);
desc.texid = g_services->create_texture(desc.width, desc.height, 4);
g_services->update_texture(desc.texid, udata.data());
return true;
}
}
return false;
}
void build_view_dbg_buffer(ImGuiTextBuffer *buffer, const test_data &bake_data)
{
buffer->clear();
std::stringstream ss;
for (u32 vi = 0; vi < bake_data.nviews; ++vi)
{
const auto &view = bake_data.views[vi];
ss << '#' << vi << " L:" << view.li;
float2 t = view.tr.o;
float s = len(view.tr.x);
float a = si_atan2(view.tr.x.y, view.tr.x.x);
if (s == 1.0f && t.x == 0.0f && t.y == 0.0f && a == 0.0f)
ss << " ID";
else if (s == 1.0f && a == 0.0f)
ss << " PAN " << t.x << ',' << t.y;
else if (t.x == 0.0f && t.y == 0.0f && a == 0.0f)
ss << " ZOOM " << s;
else if (s == 1.0f && t.x == 0.0f && t.y == 0.0f)
ss << " ROTATE " << 180.0f * a / 3.1415926535f;
else ss << " COMPLEX " << t.x << ',' << t.y
<< " /" << 180.0f * a / 3.1415926535f
<< " x" << s;
if (view.has_strokes())
ss << " [" << view.si0 << ".." << view.si1 << ')';
if (view.has_image())
ss << " i:" << view.ii;
if (view.ll)
ss << " local";
ss << "\n";
}
buffer->append("%s", ss.str().c_str());
}
float2 g_prevMousePos;
void update_and_render(input_data *input, output_data *output)
{
output->quit_flag = false;
g_recipe.clear();
output->recipe = &g_recipe;
float mxnorm = input->vpMouseXPt / input->vpWidthPt - 0.5f;
float mynorm = input->vpMouseYPt / input->vpHeightPt - 0.5f;
float mxin = input->vpWidthIn * mxnorm;
float myin = input->vpHeightIn * mynorm;
bool mouse_in_ui = gui_mouse_occupied || gui_mouse_hover;
float pixel_height_in = input->vpHeightIn / input->vpHeightPx;
Vandalism::Input ism_input;
ism_input.tool = (input->shiftkey ? Vandalism::PAN : static_cast<Vandalism::Tool>(gui_tool));
ism_input.mousePos.x = mxin;
ism_input.mousePos.y = -myin;
ism_input.negligibledistance = pixel_height_in;
ism_input.mousedown = input->mouseleft && !mouse_in_ui;
ism_input.fakepressure = gui_fake_pressure;
ism_input.brushcolor = gui_brush_color;
ism_input.eraseralpha = gui_eraser_alpha;
ism_input.brushdiameter = gui_brush_diameter_units * cfg_brush_diameter_inches_per_unit;
ism_input.brushangle = 3.1415926535f * gui_brush_angle / 180.0f;
ism_input.brushspread = gui_brush_spread_units * cfg_brush_diameter_inches_per_unit;
ism_input.scrolly = input->scrollY;
ism_input.scrolling = input->scrolling;
ism_input.simplify = gui_draw_simplify;
ism_input.smooth = static_cast<Vandalism::Smooth>(gui_draw_smooth);
ism_input.currentlayer = static_cast<u8>(gui_current_layer);
box2 oBox;
oBox.add(g_ism->fitBasis.o);
oBox.grow(0.05f);
box2 xBox;
xBox.add(g_ism->fitBasis.o + g_ism->fitBasis.x);
xBox.grow(0.05f);
if (oBox.contains(g_prevMousePos)) ism_input.fitgizmo = Vandalism::FG_0POINT;
else if (xBox.contains(g_prevMousePos)) ism_input.fitgizmo = Vandalism::FG_XPOINT;
else ism_input.fitgizmo = Vandalism::FG_NONE;
g_prevMousePos = ism_input.mousePos; // TODO: figure out better way to do this
ism_input.fittool = static_cast<Vandalism::FitTool>(gui_fit_tool);
ism_input.fitimageid = g_fit_img.imageid;
ism_input.fitimageaspectratio = g_fit_img.aspect_ratio;
bool currentChanged, abovesChanged, belowsChanged;
if (gui_current_layer_changed)
{
currentChanged = abovesChanged = belowsChanged = true;
}
else
{
const bool *beg = gui_layer_active_changed;
const bool *mid = beg + gui_current_layer;
belowsChanged = (std::find(beg, mid, true) != mid);
currentChanged = *mid;
const bool *end = beg + gui_layer_cnt;
abovesChanged = (std::find(mid + 1, end, true) != end);
}
// TODO: move all input to this point
g_ism->update(&ism_input);
bool scrollViewsDown = false;
const test_data &bake_data = g_ism->get_bake_data();
abovesChanged |= g_ism->visiblesChanged;
currentChanged |= g_ism->visiblesChanged;
belowsChanged |= g_ism->visiblesChanged;
// split into
// a)all bakes changed -- movement
// b)current bake changed -- end of curr stroke)
// flag processed
// TODO: make this better
g_ism->visiblesChanged = false;
abovesChanged |= input->forceUpdate;
currentChanged |= input->forceUpdate;
belowsChanged |= input->forceUpdate;
if (abovesChanged || belowsChanged || currentChanged)
{
// TODO: this should not happen once per every stroke
// implement some intermediate 'uncommited' strokes
g_bake_quads.clear();
g_max_bake_mesh_idx = 0;
std::fill(g_bake_mesh_sizes.begin(), g_bake_mesh_sizes.end(), 0);
u8 from = static_cast<u8>(gui_current_layer);
u8 to = static_cast<u8>(gui_current_layer + 1);
if (abovesChanged) to = static_cast<u8>(gui_layer_cnt);
if (belowsChanged) from = 0;
::printf("-----\n");
for (u8 layerIdx = from; layerIdx < to; ++layerIdx)
{
bool cur = (layerIdx == gui_current_layer);
if (((cur && currentChanged) || !cur) && gui_layer_active[layerIdx])
{
collect_bake_data(bake_data,
input->rtWidthIn, input->rtHeightIn,
pixel_height_in,
layerIdx, static_cast<u8>(gui_current_layer));
}
}
size_t remaining = g_bake_quads.size();
size_t mesh_idx = 0;
while (remaining > 0)
{
size_t portion = (remaining >= VTX_PER_MESH ? VTX_PER_MESH : remaining);
g_services->update_mesh_vb(g_bake_meshes[mesh_idx],
g_bake_quads.data() + mesh_idx * VTX_PER_MESH, static_cast<u32>(portion));
remaining -= portion;
++mesh_idx;
}
build_view_dbg_buffer(g_viewsBuf, bake_data);
scrollViewsDown = true;
}
g_recipe.bakeBelow = belowsChanged;
g_recipe.bakeCurrent = currentChanged;
g_recipe.bakeAbove = abovesChanged;
{
const bool *beg = gui_layer_active;
const bool *mid = beg + gui_current_layer;
g_recipe.blitBelow = (std::find(beg, mid, true) != mid);
g_recipe.blitCurrent = *mid;
const bool *end = beg + gui_layer_cnt;
g_recipe.blitAbove = (std::find(mid + 1, end, true) != end);
}
output->preTranslate = g_ism->preShift;
output->postTranslate = g_ism->postShift;
output->scale = g_ism->zoomCoeff;
output->rotate = g_ism->rotateAngle;
output->bg_color = gui_background_color;
output->grid_bg_color = gui_grid_bg_color;
output->grid_fg_color = gui_grid_fg_color;
output->zbandwidth = cfg_depth_step;
if (input->forceUpdate || g_ism->currentChanged)
{
// TODO: this overwrites whole stroke on each update
// maybe it can be only appended?
// TODO: flag processed, improve this
g_ism->currentChanged = false;
const test_data& current_data = g_ism->get_current_data();
const Masterpiece::Brush& currBrush = g_ism->get_current_brush();
size_t currStrokeId = g_ism->get_current_stroke_id();
g_curr_quads.clear();
const Masterpiece::Stroke& stroke = current_data.strokes[0];
stroke_to_quads(current_data.points + stroke.pi0,
current_data.points + stroke.pi1,
g_curr_quads,
currStrokeId, default_basis(), currBrush);