-
Notifications
You must be signed in to change notification settings - Fork 51
/
batch.cpp
1129 lines (953 loc) · 30.5 KB
/
batch.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 <blah/graphics/batch.h>
#include <blah/graphics/texture.h>
#include <blah/graphics/target.h>
#include <blah/graphics/mesh.h>
#include <blah/graphics/shader.h>
#include <blah/graphics/material.h>
#include <blah/numerics/calc.h>
#include <blah/app.h>
#include <cmath>
using namespace Blah;
namespace
{
// TODO:
// This shader needs to be graphics API agnostic
const ShaderData opengl_shader_data = {
// vertex shader
#ifdef __EMSCRIPTEN__
"#version 300 es\n"
#else
"#version 330\n"
#endif
"uniform mat4 u_matrix;\n"
"layout(location=0) in vec2 a_position;\n"
"layout(location=1) in vec2 a_tex;\n"
"layout(location=2) in vec4 a_color;\n"
"layout(location=3) in vec4 a_type;\n"
"out vec2 v_tex;\n"
"out vec4 v_col;\n"
"out vec4 v_type;\n"
"void main(void)\n"
"{\n"
" gl_Position = u_matrix * vec4(a_position.xy, 0, 1);\n"
" v_tex = a_tex;\n"
" v_col = a_color;\n"
" v_type = a_type;\n"
"}",
// fragment shader
#ifdef __EMSCRIPTEN__
"#version 300 es\n"
"precision mediump float;\n"
#else
"#version 330\n"
#endif
"uniform sampler2D u_texture;\n"
"in vec2 v_tex;\n"
"in vec4 v_col;\n"
"in vec4 v_type;\n"
"out vec4 o_color;\n"
"void main(void)\n"
"{\n"
" vec4 color = texture(u_texture, v_tex);\n"
" o_color = \n"
" v_type.x * color * v_col + \n"
" v_type.y * color.a * v_col + \n"
" v_type.z * v_col;\n"
"}"
};
const char* d3d11_shader = ""
"cbuffer constants : register(b0)\n"
"{\n"
" row_major float4x4 u_matrix;\n"
"}\n"
"struct vs_in\n"
"{\n"
" float2 position : POS;\n"
" float2 texcoord : TEX;\n"
" float4 color : COL;\n"
" float4 mask : MASK;\n"
"};\n"
"struct vs_out\n"
"{\n"
" float4 position : SV_POSITION;\n"
" float2 texcoord : TEX;\n"
" float4 color : COL;\n"
" float4 mask : MASK;\n"
"};\n"
"Texture2D u_texture : register(t0);\n"
"SamplerState u_sampler : register(s0);\n"
"vs_out vs_main(vs_in input)\n"
"{\n"
" vs_out output;\n"
" output.position = mul(float4(input.position, 0.0f, 1.0f), u_matrix);\n"
" output.texcoord = input.texcoord;\n"
" output.color = input.color;\n"
" output.mask = input.mask;\n"
" return output;\n"
"}\n"
"float4 ps_main(vs_out input) : SV_TARGET\n"
"{\n"
" float4 color = u_texture.Sample(u_sampler, input.texcoord);\n"
" return\n"
" input.mask.x * color * input.color + \n"
" input.mask.y * color.a * input.color + \n"
" input.mask.z * input.color;\n"
"}\n";
const ShaderData d3d11_shader_data = {
d3d11_shader,
d3d11_shader,
{
{ "POS", 0 },
{ "TEX", 0 },
{ "COL", 0 },
{ "MASK", 0 },
}
};
const VertexFormat format = VertexFormat(
{
{ 0, VertexType::Float2, false },
{ 1, VertexType::Float2, false },
{ 2, VertexType::UByte4, true },
{ 3, VertexType::UByte4, true },
});
}
namespace
{
static Vec2 batch_shape_intersection(const Vec2& p0, const Vec2& p1, const Vec2& q0, const Vec2& q1)
{
const auto aa = p1 - p0;
const auto bb = q0 - q1;
const auto cc = q0 - p0;
const auto t = (bb.x * cc.y - bb.y * cc.x) / (aa.y * bb.x - aa.x * bb.y);
return Vec2(p0.x + t * (p1.x - p0.x), p0.y + t * (p1.y - p0.y));
}
}
#define MAKE_VERTEX(vert, mat, px, py, tx, ty, c, m, w, f) \
(vert)->pos.x = ((px) * (mat).m11) + ((py) * (mat).m21) + (mat).m31; \
(vert)->pos.y = ((px) * (mat).m12) + ((py) * (mat).m22) + (mat).m32; \
(vert)->tex.x = tx; \
if (m_batch.flip_vertically) \
(vert)->tex.y = 1.0f - (ty); \
else \
(vert)->tex.y = ty; \
(vert)->col = c; \
(vert)->mult = m; \
(vert)->wash = w; \
(vert)->fill = f;
#define PUSH_QUAD(px0, py0, px1, py1, px2, py2, px3, py3, tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3, col0, col1, col2, col3, mult, fill, wash) \
{ \
m_batch.elements += 2; \
auto _i = m_indices.expand(6); \
*_i++ = (u32)m_vertices.size() + 0; \
*_i++ = (u32)m_vertices.size() + 1; \
*_i++ = (u32)m_vertices.size() + 2; \
*_i++ = (u32)m_vertices.size() + 0; \
*_i++ = (u32)m_vertices.size() + 2; \
*_i++ = (u32)m_vertices.size() + 3; \
Vertex* _v = m_vertices.expand(4); \
MAKE_VERTEX(_v, m_matrix, px0, py0, tx0, ty0, col0, mult, fill, wash); _v++; \
MAKE_VERTEX(_v, m_matrix, px1, py1, tx1, ty1, col1, mult, fill, wash); _v++; \
MAKE_VERTEX(_v, m_matrix, px2, py2, tx2, ty2, col2, mult, fill, wash); _v++; \
MAKE_VERTEX(_v, m_matrix, px3, py3, tx3, ty3, col3, mult, fill, wash); \
}
#define PUSH_TRIANGLE(px0, py0, px1, py1, px2, py2, tx0, ty0, tx1, ty1, tx2, ty2, col0, col1, col2, mult, fill, wash) \
{ \
m_batch.elements += 1; \
auto* _i = m_indices.expand(3); \
*_i++ = (u32)m_vertices.size() + 0; \
*_i++ = (u32)m_vertices.size() + 1; \
*_i++ = (u32)m_vertices.size() + 2; \
Vertex* _v = m_vertices.expand(3); \
MAKE_VERTEX(_v, m_matrix, px0, py0, tx0, ty0, col0, mult, fill, wash); _v++; \
MAKE_VERTEX(_v, m_matrix, px1, py1, tx1, ty1, col1, mult, fill, wash); _v++; \
MAKE_VERTEX(_v, m_matrix, px2, py2, tx2, ty2, col2, mult, fill, wash); \
}
#define INSERT_BATCH() \
do { \
m_batches.expand(); \
for (auto i = m_batches.size() - 1; i > m_batch_insert; i --) \
m_batches[i] = std::move(m_batches[i - 1]); \
m_batches[m_batch_insert++] = m_batch; \
m_batch.offset += m_batch.elements; \
m_batch.elements = 0; \
} while (0)
// Compares a Batcher variable, and starts a new batch if it has changed
#define SET_BATCH_VAR(variable) \
if (m_batch.elements > 0 && (variable) != m_batch.variable) \
INSERT_BATCH(); \
m_batch.variable = variable;
ShaderRef Batch::m_default_shader;
Batch::Batch()
{
matrix_uniform = "u_matrix";
clear();
}
Batch::~Batch()
{
dispose();
}
void Batch::push_matrix(const Mat3x2& matrix, bool absolute)
{
m_matrix_stack.push_back(m_matrix);
if (absolute)
m_matrix = matrix;
else
m_matrix = matrix * m_matrix;
}
Mat3x2 Batch::pop_matrix()
{
auto was = m_matrix;
m_matrix = m_matrix_stack.pop();
return was;
}
Mat3x2 Batch::peek_matrix() const
{
return m_matrix;
}
void Batch::push_scissor(const Rect& scissor)
{
m_scissor_stack.push_back(m_batch.scissor);
SET_BATCH_VAR(scissor);
}
Rect Batch::pop_scissor()
{
Rect was = m_batch.scissor;
Rect scissor = m_scissor_stack.pop();
SET_BATCH_VAR(scissor);
return was;
}
Rect Batch::peek_scissor() const
{
return m_batch.scissor;
}
void Batch::push_blend(const BlendMode& blend)
{
m_blend_stack.push_back(m_batch.blend);
SET_BATCH_VAR(blend);
}
BlendMode Batch::pop_blend()
{
BlendMode was = m_batch.blend;
BlendMode blend = m_blend_stack.pop();
SET_BATCH_VAR(blend);
return was;
}
BlendMode Batch::peek_blend() const
{
return m_batch.blend;
}
void Batch::push_material(const MaterialRef& material)
{
m_material_stack.push_back(m_batch.material);
SET_BATCH_VAR(material);
}
MaterialRef Batch::pop_material()
{
MaterialRef was = m_batch.material;
MaterialRef material = m_material_stack.pop();
SET_BATCH_VAR(material);
return was;
}
MaterialRef Batch::peek_material() const
{
return m_batch.material;
}
void Batch::push_layer(int layer)
{
m_layer_stack.push_back(m_batch.layer);
if (layer != m_batch.layer)
{
INSERT_BATCH();
// TODO:
// binary search
auto insert = 0;
while (insert < m_batches.size() && m_batches[insert].layer >= layer)
insert++;
m_batch.layer = layer;
m_batch_insert = insert;
}
}
int Batch::pop_layer()
{
int was = m_batch.layer;
int layer = m_layer_stack.pop();
if (layer != was)
{
INSERT_BATCH();
// TODO:
// binary search
auto insert = 0;
while (insert < m_batches.size() && m_batches[insert].layer >= layer)
insert++;
m_batch.layer = layer;
m_batch_insert = insert;
}
return was;
}
int Batch::peek_layer() const
{
return m_batch.layer;
}
void Batch::push_color_mode(ColorMode mode)
{
m_color_mode_stack.push_back(m_color_mode);
m_color_mode = mode;
m_tex_mult = (m_color_mode == ColorMode::Normal ? 255 : 0);
m_tex_wash = (m_color_mode == ColorMode::Wash ? 255 : 0);
}
ColorMode Batch::pop_color_mode()
{
ColorMode was = m_color_mode;
m_color_mode = m_color_mode_stack.pop();
m_tex_mult = (m_color_mode == ColorMode::Normal ? 255 : 0);
m_tex_wash = (m_color_mode == ColorMode::Wash ? 255 : 0);
return was;
}
ColorMode Batch::peek_color_mode() const
{
return m_color_mode;
}
void Batch::set_texture(const TextureRef& texture)
{
if (m_batch.elements > 0 && texture != m_batch.texture && m_batch.texture)
INSERT_BATCH();
if (m_batch.texture != texture)
{
m_batch.texture = texture;
m_batch.flip_vertically = App::renderer_features().origin_bottom_left && texture && texture->is_framebuffer();
}
}
void Batch::set_sampler(const TextureSampler& sampler)
{
if (m_batch.elements > 0 && sampler != m_batch.sampler)
INSERT_BATCH();
m_batch.sampler = sampler;
}
void Batch::render(const TargetRef& target)
{
Point size;
if (!target)
size = Point(App::draw_width(), App::draw_height());
else
size = Point(target->width(), target->height());
render(target, Mat4x4::create_ortho_offcenter(0, (float)size.x, (float)size.y, 0, 0.01f, 1000.0f));
}
void Batch::render(const TargetRef& target, const Mat4x4& matrix)
{
// nothing to draw
if ((m_batches.size() <= 0 && m_batch.elements <= 0) || m_indices.size() <= 0)
return;
// define defaults
{
if (!m_mesh)
m_mesh = Mesh::create();
if (!m_default_shader)
{
if (App::renderer() == Renderer::OpenGL)
m_default_shader = Shader::create(opengl_shader_data);
else if (App::renderer() == Renderer::D3D11)
m_default_shader = Shader::create(d3d11_shader_data);
}
if (!m_default_material)
m_default_material = Material::create(m_default_shader);
}
// upload data
m_mesh->index_data(IndexFormat::UInt32, m_indices.data(), m_indices.size());
m_mesh->vertex_data(format, m_vertices.data(), m_vertices.size());
RenderPass pass;
pass.target = target;
pass.mesh = m_mesh;
pass.has_viewport = false;
pass.viewport = Rect();
pass.instance_count = 0;
pass.depth = Compare::None;
pass.cull = Cull::None;
// render batches
for (int i = 0, n = m_batches.size(); i < n; i++)
{
// remaining elements in the current batch
if (m_batch_insert == i && m_batch.elements > 0)
render_single_batch(pass, m_batch, matrix);
// render the batch
render_single_batch(pass, m_batches[i], matrix);
}
// remaining elements in the current batch
if (m_batch_insert == m_batches.size() && m_batch.elements > 0)
render_single_batch(pass, m_batch, matrix);
}
void Batch::render_single_batch(RenderPass& pass, const DrawBatch& b, const Mat4x4& matrix)
{
pass.material = b.material;
if (!pass.material)
pass.material = m_default_material;
pass.material->set_texture(0, b.texture);
pass.material->set_sampler(0, b.sampler);
pass.material->set_value(matrix_uniform, &matrix.m11, 16);
pass.blend = b.blend;
pass.has_scissor = b.scissor.w >= 0 && b.scissor.h >= 0;
pass.scissor = b.scissor;
pass.index_start = (i64)b.offset * 3;
pass.index_count = (i64)b.elements * 3;
pass.perform();
}
void Batch::clear()
{
m_matrix = Mat3x2::identity;
m_color_mode = ColorMode::Normal;
m_tex_mult = 255;
m_tex_wash = 0;
m_vertices.clear();
m_indices.clear();
m_batch.layer = 0;
m_batch.elements = 0;
m_batch.offset = 0;
m_batch.blend = BlendMode::Normal;
m_batch.material.reset();
m_batch.texture.reset();
m_batch.sampler = default_sampler;
m_batch.scissor.w = m_batch.scissor.h = -1;
m_batch.flip_vertically = false;
m_matrix_stack.clear();
m_scissor_stack.clear();
m_blend_stack.clear();
m_material_stack.clear();
m_color_mode_stack.clear();
m_layer_stack.clear();
m_batches.clear();
m_batch_insert = 0;
}
void Batch::dispose()
{
clear();
m_vertices.dispose();
m_indices.dispose();
m_matrix_stack.dispose();
m_scissor_stack.dispose();
m_blend_stack.dispose();
m_material_stack.dispose();
m_color_mode_stack.dispose();
m_layer_stack.dispose();
m_batches.dispose();
m_default_material.reset();
m_mesh.reset();
}
void Batch::line(const Vec2& from, const Vec2& to, float t, Color color)
{
line(from, to, t, color, color);
}
void Batch::line(const Vec2& from, const Vec2& to, float t, Color fromColor, Color toColor)
{
if (from.x == to.x && from.y == to.y)
return;
Vec2 normal = (to - from).normal();
Vec2 perp = Vec2(normal.y, -normal.x);
Vec2 pos0 = from + perp * t * 0.5f;
Vec2 pos1 = to + perp * t * 0.5f;
Vec2 pos2 = to - perp * t * 0.5f;
Vec2 pos3 = from - perp * t * 0.5f;
PUSH_QUAD(
pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y, pos3.x, pos3.y,
0, 0, 0, 0, 0, 0, 0, 0,
fromColor, toColor, toColor, fromColor,
0, 0, 255);
}
void Batch::bezier_line(const Vec2& from, const Vec2& b, const Vec2& to, int steps, float t, Color color)
{
Vec2 prev = from;
float add = 1.0f / steps;
for (int i = 1; i < steps; i++)
{
Vec2 at = Vec2::lerp_bezier(from, b, to, add * i);
line(prev, at, t, color);
prev = at;
}
line(prev, to, t, color);
}
void Batch::bezier_line(const Vec2& from, const Vec2& b, const Vec2& c, const Vec2& to, int steps, float t, Color color)
{
Vec2 prev = from;
float add = 1.0f / steps;
for (int i = 1; i < steps; i++)
{
Vec2 at = Vec2::lerp_bezier(from, b, c, to, add * i);
line(prev, at, t, color);
prev = at;
}
line(prev, to, t, color);
}
void Batch::tri(const Vec2& pos0, const Vec2& pos1, const Vec2& pos2, Color color)
{
PUSH_TRIANGLE(
pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y,
0, 0, 0, 0, 0, 0,
color, color, color,
0, 0, 255);
}
void Batch::tri(const Vec2& pos0, const Vec2& pos1, const Vec2& pos2, Color col0, Color col1, Color col2)
{
PUSH_TRIANGLE(
pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y,
0, 0, 0, 0, 0, 0,
col0, col1, col2,
0, 0, 255);
}
void Batch::tri(const Vec2& pos0, const Vec2& pos1, const Vec2& pos2, const Vec2& tex0, const Vec2& tex1, const Vec2& tex2, Color color)
{
PUSH_TRIANGLE(
pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y,
tex0.x, tex0.y, tex1.x, tex1.y, tex2.x, tex2.y,
color, color, color,
m_tex_mult, m_tex_wash, 0);
}
void Batch::tri(const Vec2& pos0, const Vec2& pos1, const Vec2& pos2, const Vec2& tex0, const Vec2& tex1, const Vec2& tex2, Color col0, Color col1, Color col2)
{
PUSH_TRIANGLE(
pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y,
tex0.x, tex0.y, tex1.x, tex1.y, tex2.x, tex2.y,
col0, col1, col2,
m_tex_mult, m_tex_wash, 0);
}
void Batch::tri_line(const Vec2& a, const Vec2& b, const Vec2& c, float t, Color color)
{
// TODO:
// Detect if the thickness of the line fills the entire shape
// (in which case, draw a triangle instead)
const float len_ab = (a - b).length();
const float len_bc = (b - c).length();
const float len_ca = (c - a).length();
const auto off_ab = ((b - a) / len_ab).turn_left() * t;
const auto off_bc = ((c - b) / len_bc).turn_left() * t;
const auto off_ca = ((a - c) / len_ca).turn_left() * t;
const auto aa = batch_shape_intersection(a + off_ca, a + off_ca, a + off_ab, b + off_ab);
const auto bb = batch_shape_intersection(a + off_ab, b + off_ab, b + off_bc, c + off_bc);
const auto cc = batch_shape_intersection(b + off_bc, c + off_bc, c + off_ca, a + off_ca);
quad(aa, a, b, bb, color);
quad(bb, b, c, cc, color);
quad(cc, c, a, aa, color);
}
void Batch::rect(const Rect& rect, Color color)
{
PUSH_QUAD(
rect.x, rect.y,
rect.x + rect.w, rect.y,
rect.x + rect.w, rect.y + rect.h,
rect.x, rect.y + rect.h,
0, 0, 0, 0, 0, 0, 0, 0,
color, color, color, color,
0, 0, 255);
}
void Batch::rect_line(const Rect& rect, float t, Color color)
{
if (t >= rect.w || t >= rect.h)
{
this->rect(rect, color);
}
else
{
PUSH_QUAD(
rect.x, rect.y,
rect.x + rect.w - t, rect.y,
rect.x + rect.w - t, rect.y + t,
rect.x, rect.y + t,
0, 0, 0, 0, 0, 0, 0, 0,
color, color, color, color,
0, 0, 255);
PUSH_QUAD(
rect.x + rect.w - t, rect.y,
rect.x + rect.w, rect.y,
rect.x + rect.w, rect.y + rect.h - t,
rect.x + rect.w - t, rect.y + rect.h - t,
0, 0, 0, 0, 0, 0, 0, 0,
color, color, color, color,
0, 0, 255);
PUSH_QUAD(
rect.x + t, rect.y + rect.h - t,
rect.x + rect.w, rect.y + rect.h - t,
rect.x + rect.w, rect.y + rect.h,
rect.x, rect.y + rect.h,
0, 0, 0, 0, 0, 0, 0, 0,
color, color, color, color,
0, 0, 255);
PUSH_QUAD(
rect.x, rect.y + t,
rect.x + t, rect.y + t,
rect.x + t, rect.y + rect.h - t,
rect.x, rect.y + rect.h,
0, 0, 0, 0, 0, 0, 0, 0,
color, color, color, color,
0, 0, 255);
}
}
void Batch::rect_rounded(const Rect& rect, float radius, int steps, Color color)
{
rect_rounded(rect, radius, steps, radius, steps, radius, steps, radius, steps, color);
}
void Batch::rect_rounded(const Rect& rect, float rtl, int rtl_steps, float rtr, int rtr_steps, float rbr, int rbr_steps, float rbl, int rbl_steps, Color color)
{
// clamp
rtl = Calc::min(Calc::min(Calc::max(0.0f, rtl), rect.w / 2.0f), rect.h / 2.0f);
rtr = Calc::min(Calc::min(Calc::max(0.0f, rtr), rect.w / 2.0f), rect.h / 2.0f);
rbr = Calc::min(Calc::min(Calc::max(0.0f, rbr), rect.w / 2.0f), rect.h / 2.0f);
rbl = Calc::min(Calc::min(Calc::max(0.0f, rbl), rect.w / 2.0f), rect.h / 2.0f);
if (rtl <= 0 && rtr <= 0 && rbr <= 0 && rbl <= 0)
{
this->rect(rect, color);
}
else
{
// get corners
Rect tl = Rect(rect.top_left(), Vec2(rtl, rtl));
Rect tr = Rect(rect.top_right() + Vec2(-rtr, 0.0f), Vec2(rtr, rtr));
Rect bl = Rect(rect.bottom_left() + Vec2(0.0f, -rbl), Vec2(rbl, rbl));
Rect br = Rect(rect.bottom_right() + Vec2(-rbr, -rbr), Vec2(rbr, rbr));
// rounded corners
semi_circle(tl.bottom_right(), Calc::UP, Calc::LEFT, rtl, rtl_steps, color);
semi_circle(tr.bottom_left(), Calc::UP, Calc::RIGHT, rtr, rtr_steps, color);
semi_circle(bl.top_right(), Calc::DOWN, Calc::LEFT, rbl, rbl_steps, color);
semi_circle(br.top_left(), Calc::DOWN, Calc::RIGHT, rbr, rbr_steps, color);
// center quads
quad(tl.top_right(), tr.top_left(), tr.bottom_left(), tl.bottom_right(), color);
quad(tr.bottom_left(), tr.bottom_right(), br.top_right(), br.top_left(), color);
quad(bl.top_right(), br.top_left(), br.bottom_left(), bl.bottom_right(), color);
quad(tl.bottom_left(), tl.bottom_right(), bl.top_right(), bl.top_left(), color);
quad(tl.bottom_right(), tr.bottom_left(), br.top_left(), bl.top_right(), color);
}
}
void Batch::rect_rounded_line(const Rect & rect, float radius, int steps, float t, Color color)
{
rect_rounded_line(rect, radius, steps, radius, steps, radius, steps, radius, steps, t, color);
}
void Batch::rect_rounded_line(const Rect& r, float rtl, int rtl_steps, float rtr, int rtr_steps, float rbr, int rbr_steps, float rbl, int rbl_steps, float t, Color color)
{
// clamp
rtl = Calc::min(Calc::min(Calc::max(0.0f, rtl), r.w / 2.0f), r.h / 2.0f);
rtr = Calc::min(Calc::min(Calc::max(0.0f, rtr), r.w / 2.0f), r.h / 2.0f);
rbr = Calc::min(Calc::min(Calc::max(0.0f, rbr), r.w / 2.0f), r.h / 2.0f);
rbl = Calc::min(Calc::min(Calc::max(0.0f, rbl), r.w / 2.0f), r.h / 2.0f);
if (rtl <= 0 && rtr <= 0 && rbr <= 0 && rbl <= 0)
{
rect_line(r, t, color);
}
else
{
// rounded corners
semi_circle_line(Vec2(r.x + rtl, r.y + rtl), Calc::UP, Calc::LEFT, rtl, rtl_steps, t, color);
semi_circle_line(Vec2(r.x + r.w - rtr, r.y + rtr), Calc::UP, Calc::UP + Calc::TAU * 0.25f, rtr, rtr_steps, t, color);
semi_circle_line(Vec2(r.x + rbl, r.y + r.h - rbl), Calc::DOWN, Calc::LEFT, rbl, rbl_steps, t, color);
semi_circle_line(Vec2(r.x + r.w - rbr, r.y + r.h - rbr), Calc::DOWN, Calc::RIGHT, rbr, rbr_steps, t, color);
// connect sides that aren't touching
if (r.h > rtl + rbl)
rect(Rect(r.x, r.y + rtl, t, r.h - rtl - rbl), color);
if (r.h > rtr + rbr)
rect(Rect(r.x + r.w - t, r.y + rtr, t, r.h - rtr - rbr), color);
if (r.w > rtl + rtr)
rect(Rect(r.x + rtl, r.y, r.w - rtl - rtr, t), color);
if (r.w > rbl + rbr)
rect(Rect(r.x + rbl, r.y + r.h - t, r.w - rbl - rbr, t), color);
}
}
void Batch::semi_circle(const Vec2& center, float start_radians, float end_radians, float radius, int steps, Color centerColor, Color edgeColor)
{
Vec2 last = Vec2::from_angle(start_radians, radius);
float add = Calc::angle_diff(start_radians, end_radians);
for (int i = 1; i <= steps; i++)
{
Vec2 next = Vec2::from_angle(start_radians + add * (i / (float)steps), radius);
tri(center + last, center + next, center, edgeColor, edgeColor, centerColor);
last = next;
}
}
void Batch::semi_circle(const Vec2& center, float start_radians, float end_radians, float radius, int steps, Color color)
{
semi_circle(center, start_radians, end_radians, radius, steps, color, color);
}
void Batch::semi_circle_line(const Vec2& center, float start_radians, float end_radians, float radius, int steps, float t, Color color)
{
if (t >= radius)
{
semi_circle(center, start_radians, end_radians, radius, steps, color, color);
}
else
{
const auto add = Calc::angle_diff(start_radians, end_radians);
Vec2 last_inner = Vec2::from_angle(start_radians, radius - t);
Vec2 last_outer = Vec2::from_angle(start_radians, radius);
for (int i = 1; i <= steps; i++)
{
const auto next_inner = Vec2::from_angle(start_radians + add * (i / (float)steps), radius - t);
const auto next_outer = Vec2::from_angle(start_radians + add * (i / (float)steps), radius);
quad(center + last_inner, center + last_outer, center + next_outer, center + next_inner, color);
last_inner = next_inner;
last_outer = next_outer;
}
}
}
void Batch::circle(const Vec2& center, float radius, int steps, Color color)
{
circle(center, radius, steps, color, color);
}
void Batch::circle(const Vec2& center, float radius, int steps, Color center_color, Color outer_color)
{
Vec2 last = Vec2(center.x + radius, center.y);
for (int i = 1; i <= steps; i++)
{
const auto radians = (i / (float)steps) * Calc::TAU;
const auto next = Vec2(center.x + Calc::cos(radians) * radius, center.y + Calc::sin(radians) * radius);
tri(last, next, center, outer_color, outer_color, center_color);
last = next;
}
}
void Batch::circle_line(const Vec2& center, float radius, float t, int steps, Color color)
{
if (t >= radius)
{
circle(center, radius, steps, color);
}
else
{
Vec2 last_inner = Vec2(center.x + radius - t, center.y);
Vec2 last_outer = Vec2(center.x + radius, center.y);
for (int i = 1; i <= steps; i++)
{
const auto radians = (i / (float)steps) * Calc::TAU;
const auto normal = Vec2(Calc::cos(radians), Calc::sin(radians));
const auto next_inner = Vec2(center.x + normal.x * (radius - t), center.y + normal.y * (radius - t));
const auto next_outer = Vec2(center.x + normal.x * radius, center.y + normal.y * radius);
quad(last_inner, last_outer, next_outer, next_inner, color);
last_outer = next_outer;
last_inner = next_inner;
}
}
}
void Batch::quad(const Vec2& pos0, const Vec2& pos1, const Vec2& pos2, const Vec2& pos3, Color color)
{
PUSH_QUAD(
pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y, pos3.x, pos3.y,
0, 0, 0, 0, 0, 0, 0, 0,
color, color, color, color,
0, 0, 255);
}
void Batch::quad(const Vec2& pos0, const Vec2& pos1, const Vec2& pos2, const Vec2& pos3, Color col0, Color col1, Color col2, Color col3)
{
PUSH_QUAD(
pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y, pos3.x, pos3.y,
0, 0, 0, 0, 0, 0, 0, 0,
col0, col1, col2, col3,
0, 0, 255);
}
void Batch::quad(const Vec2& pos0, const Vec2& pos1, const Vec2& pos2, const Vec2& pos3, const Vec2& tex0, const Vec2& tex1, const Vec2& tex2, const Vec2& tex3, Color color)
{
PUSH_QUAD(
pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y, pos3.x, pos3.y,
tex0.x, tex0.y, tex1.x, tex1.y, tex2.x, tex2.y, tex3.x, tex3.y,
color, color, color, color,
m_tex_mult, m_tex_wash, 0);
}
void Batch::quad(const Vec2& pos0, const Vec2& pos1, const Vec2& pos2, const Vec2& pos3, const Vec2& tex0, const Vec2& tex1, const Vec2& tex2, const Vec2& tex3, Color col0, Color col1, Color col2, Color col3)
{
PUSH_QUAD(
pos0.x, pos0.y, pos1.x, pos1.y, pos2.x, pos2.y, pos3.x, pos3.y,
tex0.x, tex0.y, tex1.x, tex1.y, tex2.x, tex2.y, tex3.x, tex3.y,
col0, col1, col2, col3,
m_tex_mult, m_tex_wash, 0);
}
void Batch::quad_line(const Vec2& a, const Vec2& b, const Vec2& c, const Vec2& d, float t, Color color)
{
// TODO:
// Detect if the thickness of the line fills the entire shape
// (in which case, draw a quad instead)
const float len_ab = (a - b).length();
const float len_bc = (b - c).length();
const float len_cd = (c - d).length();
const float len_da = (d - a).length();
const auto off_ab = ((b - a) / len_ab).turn_left() * t;
const auto off_bc = ((c - b) / len_bc).turn_left() * t;
const auto off_cd = ((d - c) / len_cd).turn_left() * t;
const auto off_da = ((a - d) / len_da).turn_left() * t;
const auto aa = batch_shape_intersection(d + off_da, a + off_da, a + off_ab, b + off_ab);
const auto bb = batch_shape_intersection(a + off_ab, b + off_ab, b + off_bc, c + off_bc);
const auto cc = batch_shape_intersection(b + off_bc, c + off_bc, c + off_cd, d + off_cd);
const auto dd = batch_shape_intersection(c + off_cd, d + off_cd, d + off_da, a + off_da);
quad(aa, a, b, bb, color);
quad(bb, b, c, cc, color);
quad(cc, c, d, dd, color);
quad(dd, d, a, aa, color);
}
void Batch::arrow_head(const Vec2& point_pos, float radians, float side_len, Color color)
{
arrow_head(point_pos, point_pos - Vec2::from_angle(radians), side_len, color);
}
void Batch::arrow_head(const Vec2& point_pos, const Vec2& from_pos, float side_len, Color color)
{
float height = sqrtf(side_len * side_len - powf(side_len / 2, 2));
Vec2 dir = (point_pos - from_pos).normal();
Vec2 perp = dir.perpendicular();
Vec2 base = point_pos - dir * height;
tri(point_pos, base + perp * side_len / 2, base - perp * side_len / 2, color);
}
void Batch::tex(const TextureRef& texture, const Vec2& pos, Color color)
{
set_texture(texture);
const auto w = texture->width();
const auto h = texture->height();
PUSH_QUAD(
pos.x, pos.y, pos.x + w, pos.y, pos.x + w, pos.y + h, pos.x, pos.y + h,
0, 0, 1, 0, 1, 1, 0, 1,
color, color, color, color,
m_tex_mult, m_tex_wash, 0);
}
void Batch::tex(const TextureRef& texture, const Vec2& pos, const Vec2& origin, const Vec2& scale, float rotation, Color color)
{
push_matrix(Mat3x2::create_transform(pos, origin, scale, rotation));
set_texture(texture);
const auto w = texture->width();
const auto h = texture->height();
PUSH_QUAD(
0, 0, w, 0, w, h, 0, h,
0, 0, 1, 0, 1, 1, 0, 1,
color, color, color, color,
m_tex_mult, m_tex_wash, 0);
pop_matrix();
}
void Batch::tex(const TextureRef& texture, const Rect& clip, const Vec2& pos, const Vec2& origin, const Vec2& scale, float rotation, Color color)
{
push_matrix(Mat3x2::create_transform(pos, origin, scale, rotation));
set_texture(texture);
const auto tw = texture->width();
const auto th = texture->height();
const auto tx0 = clip.x / tw;
const auto tx1 = (clip.x + clip.w) / tw;
const auto ty0 = clip.y / th;
const auto ty1 = (clip.y + clip.h) / th;
PUSH_QUAD(
0, 0, clip.w, 0, clip.w, clip.h, 0, clip.h,
tx0, ty0, tx1, ty0, tx1, ty1, tx0, ty1,
color, color, color, color,
m_tex_mult, m_tex_wash, 0);
pop_matrix();
}
void Batch::tex(const Subtexture& sub, const Vec2& pos, Color color)
{
if (!sub.texture)
{
PUSH_QUAD(
pos.x + sub.draw_coords[0].x, pos.y + sub.draw_coords[0].y,
pos.x + sub.draw_coords[1].x, pos.y + sub.draw_coords[1].y,
pos.x + sub.draw_coords[2].x, pos.y + sub.draw_coords[2].y,
pos.x + sub.draw_coords[3].x, pos.y + sub.draw_coords[3].y,
0, 0, 0, 0, 0, 0, 0, 0,
color, color, color, color,
0, 0, 255);
}
else
{
set_texture(sub.texture);
PUSH_QUAD(