-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIPrimitiveContext.cpp
More file actions
1439 lines (1178 loc) · 40 KB
/
IPrimitiveContext.cpp
File metadata and controls
1439 lines (1178 loc) · 40 KB
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 "EngineCommon.h"
#include "IPrimitiveContext.h"
#include "Math\VertexFormats.h"
X_NAMESPACE_BEGIN(engine)
namespace
{
const core::XHalf2 uv_00(0.f, 0.f);
const core::XHalf2 uv_10(1.f, 0.f);
const core::XHalf2 uv_01(0.f, 1.f);
const core::XHalf2 uv_11(1.f, 1.f);
} // namespace
// so the problem with TRIANGLESTRIP
// is that when we pass a material to draw with it may have a state that is not tristrip.
// and if not doing tristrip we can have less draw calls.
IPrimitiveContext::IPrimitiveContext()
{
}
IPrimitiveContext::~IPrimitiveContext()
{
}
void IPrimitiveContext::drawQuad(float x, float y, float z, float width, float height, Color8u col)
{
const float fx = x;
const float fy = y;
const float fz = z;
const float fw = width;
const float fh = height;
PrimVertex* pQuad = addPrimitive(6, PrimitiveType::TRIANGLELIST);
// TL
pQuad[0].pos.x = fx;
pQuad[0].pos.y = fy;
pQuad[0].pos.z = fz;
// TR
pQuad[1].pos.x = fx + fw;
pQuad[1].pos.y = fy;
pQuad[1].pos.z = fz;
// BL
pQuad[2].pos.x = fx;
pQuad[2].pos.y = fy + fh;
pQuad[2].pos.z = fz;
// BR
pQuad[3].pos.x = fx + fw;
pQuad[3].pos.y = fy + fh;
pQuad[3].pos.z = fz;
for (int32_t i = 0; i < 4; ++i) {
pQuad[i].color = col;
pQuad[i].st = core::XHalf2::zero();
}
// BL
pQuad[4] = pQuad[2];
// TR
pQuad[5] = pQuad[1];
}
void IPrimitiveContext::drawQuad(const Vec3f& tl, const Vec3f& tr, const Vec3f& bl, const Vec3f& br, Color8u col)
{
PrimVertex* pQuad = addPrimitive(6, PrimitiveType::TRIANGLELIST);
pQuad[0].pos = tl;
pQuad[1].pos = tr;
pQuad[2].pos = bl;
pQuad[3].pos = br;
pQuad[0].st = uv_00;
pQuad[1].st = uv_10;
pQuad[2].st = uv_01;
pQuad[3].st = uv_11;
for (int32_t i = 0; i < 4; ++i) {
pQuad[i].color = col;
}
// BL
pQuad[4] = pQuad[2];
// TR
pQuad[5] = pQuad[1];
}
void IPrimitiveContext::drawQuad(const Vec3f& tl, const Vec3f& tr, const Vec3f& bl, const Vec3f& br, Material* pMaterial, Color8u col)
{
PrimVertex* pQuad = addPrimitive(6, PrimitiveType::TRIANGLELIST, pMaterial);
pQuad[0].pos = tl;
pQuad[1].pos = tr;
pQuad[2].pos = br;
pQuad[3].pos = br;
pQuad[4].pos = bl;
pQuad[5].pos = tl;
pQuad[0].st = uv_00;
pQuad[1].st = uv_10;
pQuad[2].st = uv_11;
pQuad[3].st = uv_11;
pQuad[4].st = uv_01;
pQuad[5].st = uv_00;
for (int32_t i = 0; i < 6; ++i) {
pQuad[i].color = col;
}
}
void IPrimitiveContext::drawQuad(const Vec3f& tl, const Vec3f& tr, const Vec3f& bl, const Vec3f& br,
Material* pMaterial, Color8u col, const Rectf& r)
{
PrimVertex* pQuad = addPrimitive(6, PrimitiveType::TRIANGLELIST, pMaterial);
pQuad[0].pos = tl;
pQuad[1].pos = tr;
pQuad[2].pos = bl;
pQuad[3].pos = br;
auto x1 = core::XHalfCompressor::compress(r.x1);
auto y1 = core::XHalfCompressor::compress(r.y1);
auto x2 = core::XHalfCompressor::compress(r.x2);
auto y2 = core::XHalfCompressor::compress(r.y2);
pQuad[0].st = core::XHalf2(x1, y1);
pQuad[1].st = core::XHalf2(x2, y1);
pQuad[2].st = core::XHalf2(x1, y2);
pQuad[3].st = core::XHalf2(x2, y2);
for (int32_t i = 0; i < 4; ++i) {
pQuad[i].color = col;
}
// BL
pQuad[4] = pQuad[2];
// TR
pQuad[5] = pQuad[1];
}
void IPrimitiveContext::drawLines(core::span<const Vec3f> points, Color8u col)
{
int32_t num = safe_static_cast<int32_t>(points.length());
X_ASSERT((num % 2) == 0, "num points must be a multiple of 2")(num);
if (num < 2) { // 2 points needed to make a line.
return;
}
PrimVertex* pLine = addPrimitive(num, PrimitiveType::LINELIST);
for (int32_t i = 0; i < num; i += 2) {
pLine[i].pos = points[i];
pLine[i].color = col;
pLine[i].st = core::XHalf2::zero();
pLine[i + 1].pos = points[i + 1];
pLine[i + 1].color = col;
pLine[i + 1].st = core::XHalf2::zero();
}
}
void IPrimitiveContext::drawLines(core::span<const Vec3f> points, core::span<const Color8u> col)
{
int32_t num = safe_static_cast<int32_t>(points.length());
int32_t numCol = safe_static_cast<int32_t>(col.length());
X_ASSERT((num % 2) == 0, "num points must be a multiple of 2")(num);
X_ASSERT(num == numCol, "Span length mismatch")(num, numCol);
if (num < 2) { // 2 points needed to make a line.
return;
}
PrimVertex* pLine = addPrimitive(num, PrimitiveType::LINELIST);
for (int32_t i = 0; i < num; i += 2) {
pLine[i].pos = points[i];
pLine[i].color = col[i];
pLine[i].st = core::XHalf2::zero();
pLine[i + 1].pos = points[i + 1];
pLine[i + 1].color = col[i + 1];
pLine[i + 1].st = core::XHalf2::zero();
}
}
void IPrimitiveContext::drawRect(float x, float y, float width, float height, Color8u col)
{
const float x1 = x;
const float y1 = y;
const float x2 = x + width;
const float y2 = y + height;
const Vec3f tl(x1, y1, 0);
const Vec3f tr(x2, y1, 0);
const Vec3f bl(x1, y2, 0);
const Vec3f br(x2, y2, 0);
drawRect(tl, tr, bl, br, col);
}
void IPrimitiveContext::drawRect(const Vec3f& tl, const Vec3f& tr, const Vec3f& bl, const Vec3f& br, Color8u col)
{
const Vec3f points[8] = {
tl, tr,
bl, br,
tl, bl,
tr, br};
drawLines(points, col);
}
void IPrimitiveContext::drawRect(const Vec3f& tl, const Vec3f& tr, const Vec3f& bl, const Vec3f& br,
Color8u tlCol, Color8u trCol, Color8u blCol, Color8u brCol)
{
const Vec3f points[8] = {
tl, tr,
bl, br,
tl, bl,
tr, br};
const Color8u col[8] = {
tlCol, trCol,
blCol, brCol,
tlCol, blCol,
trCol, brCol};
drawLines(points, col);
}
void IPrimitiveContext::drawBarChart(const Rectf& rect, uint32_t num, const float* pHeights,
float padding, uint32_t max, Color8u col)
{
X_ASSERT_NOT_NULL(pHeights);
X_ASSERT(num <= max, "Darw Chart has more items than max")(num, max);
if (num < 1) {
return;
}
// calculate the bar width.
const float bar_width = ((rect.getWidth() / max) - padding) + padding / max;
const float bottom = rect.getY2();
const float height = rect.getHeight();
float right = rect.getX2();
const Color8u col8(col);
PrimVertex* pQuads = addPrimitive(num * 6, PrimitiveType::TRIANGLELIST);
// TL - TR - BR
// BR - BL - TL
for (uint32_t i = 0; i < num; i++) {
PrimVertex* pQuad = &pQuads[i * 6];
float cur_bar = pHeights[i];
// TL
pQuad[0].pos.x = right - bar_width;
pQuad[0].pos.y = bottom - (height * cur_bar);
pQuad[0].pos.z = 0.f;
pQuad[0].color = col8;
// TR
pQuad[1].pos.x = right;
pQuad[1].pos.y = bottom - (height * cur_bar);
pQuad[1].pos.z = 0.f;
pQuad[1].color = col8;
// BR
pQuad[2].pos.x = right;
pQuad[2].pos.y = bottom;
pQuad[2].pos.z = 0.f;
pQuad[2].color = col8;
// BR
pQuad[3].pos.x = right;
pQuad[3].pos.y = bottom;
pQuad[3].pos.z = 0.f;
pQuad[3].color = col8;
// BL
pQuad[4].pos.x = right - bar_width;
pQuad[4].pos.y = bottom;
pQuad[4].pos.z = 0.f;
pQuad[4].color = col8;
// TL
pQuad[5].pos.x = right - bar_width;
pQuad[5].pos.y = bottom - (height * cur_bar);
pQuad[5].pos.z = 0.f;
pQuad[5].color = col8;
right -= (bar_width + padding);
}
}
void IPrimitiveContext::drawGraph(const Rectf& rect, float* pBegin, float* pEnd, Color8u col, Color8u colBck,
float scaleMin, float scaleMax)
{
auto arrayGetter = [](const void* pData, int32_t idx) -> float {
return reinterpret_cast<const float*>(pData)[idx];
};
int32_t num = safe_static_cast<int32_t>(pEnd - pBegin);
if (num < 2) {
return;
}
drawGraph(rect, pBegin, num, arrayGetter, col, colBck, scaleMin, scaleMax);
}
void IPrimitiveContext::drawGraph(const Rectf& rect, const void* pUserData, size_t numValues,
DataCallBack::Pointer dataFunc, Color8u col, Color8u colBck, float scaleMin, float scaleMax)
{
int32_t num = safe_static_cast<int32_t>(numValues);
if (num < 2) {
return;
}
// find min / max.
if(scaleMin == std::numeric_limits<float>::max() || scaleMax == std::numeric_limits<float>::lowest())
{
float min = std::numeric_limits<float>::max();
float max = std::numeric_limits<float>::lowest();
for (int32_t i = 0; i < num; i++)
{
const float v = dataFunc(pUserData, i);
min = core::Min(min, v);
max = core::Max(max, v);
}
if (scaleMin == std::numeric_limits<float>::max()) {
scaleMin = min;
}
if (scaleMax == std::numeric_limits<float>::lowest()) {
scaleMax = max;
}
}
const float range = scaleMax - scaleMin;
// we draw a max of width.
const int32_t res_w = core::Min((int)rect.getWidth(), num - 1);
const int32_t numItems = num - 1;
const float t_step = 1.0f / (float)res_w;
const float inv_scale = (scaleMin == scaleMax) ? 0.0f : (1.0f / (range));
const auto tl = rect.getUpperLeft();
const auto br = rect.getLowerRight();
float v0 = dataFunc(pUserData, 0);
float t0 = 0.0f;
Vec2f tp0 = Vec2f(t0, 1.0f - math<float>::saturate((v0 - scaleMin) * inv_scale));
drawQuad(rect, colBck);
drawRect(rect, Color8u(50, 50, 50, 128));
PrimVertex* pLine = addPrimitive(res_w * 2, PrimitiveType::LINELIST);
for (int32_t i = 0; i < res_w; i++)
{
const float t1 = t0 + t_step;
const int v1_idx = (int)(t0 * numItems + 0.5f);
X_ASSERT(v1_idx >= 0 && v1_idx < num, "Index out of range")(v1_idx, num);
const float v1 = dataFunc(pUserData, (v1_idx + 1) % num);
const auto tp1 = Vec2f(t1, 1.0f - math<float>::saturate((v1 - scaleMin) * inv_scale));
const auto pos0 = tl.lerp(tp0, br);
const auto pos1 = tl.lerp(tp1, br);
auto& l0 = pLine[(i * 2)];
auto& l1 = pLine[(i * 2) + 1];
l0.color = col;
l0.pos.x = pos0.x;
l0.pos.y = pos0.y;
l1.color = col;
l1.pos.x = pos1.x;
l1.pos.y = pos1.y;
// connect
t0 = t1;
tp0 = tp1;
}
}
void IPrimitiveContext::drawTriangle(const Vec3f* pPoints, size_t numPoints, Color8u c0)
{
if (numPoints == 0) {
return;
}
X_ASSERT(numPoints % 3 == 0, "Num points must be a multiple of 3")(numPoints);
// we do the case to uint32_t here just to make the interface nicer to use.
PrimVertex* pTri = addPrimitive(static_cast<uint32_t>(numPoints), PrimitiveType::TRIANGLELIST);
for (size_t i = 0; i < numPoints; i++) {
pTri[i].pos = pPoints[i];
pTri[i].color = c0;
}
}
void IPrimitiveContext::drawTriangle(const Vec3f* pPoints, size_t numPoints, const Color8u* pCol)
{
if (numPoints == 0) {
return;
}
X_ASSERT(numPoints % 3 == 0, "Num points must be a multiple of 3")(numPoints);
PrimVertex* pTri = addPrimitive(static_cast<uint32_t>(numPoints), PrimitiveType::TRIANGLELIST);
for (size_t i = 0; i < numPoints; i++) {
pTri[i].pos = pPoints[i];
pTri[i].color = pCol[i];
}
}
void IPrimitiveContext::drawAABB(const AABB& aabb, bool solid, Color8u col)
{
// for now we do this none indexed
if (!solid) {
const uint32_t numPoints = 24;
// so we need 4 * 3 lines.
PrimVertex* pLines = addPrimitive(numPoints, PrimitiveType::LINELIST);
// create the 8 points.
const Vec3f& xyz = aabb.min;
const Vec3f xyZ(aabb.min.x, aabb.min.y, aabb.max.z);
const Vec3f xYz(aabb.min.x, aabb.max.y, aabb.min.z);
const Vec3f xYZ(aabb.min.x, aabb.max.y, aabb.max.z);
const Vec3f Xyz(aabb.max.x, aabb.min.y, aabb.min.z);
const Vec3f XyZ(aabb.max.x, aabb.min.y, aabb.max.z);
const Vec3f XYz(aabb.max.x, aabb.max.y, aabb.min.z);
const Vec3f& XYZ = aabb.max;
// bottom row (all lower case z's)
pLines[0].pos = xyz;
pLines[1].pos = xYz;
pLines[2].pos = xYz;
pLines[3].pos = XYz;
pLines[4].pos = XYz;
pLines[5].pos = Xyz;
pLines[6].pos = Xyz;
pLines[7].pos = xyz;
// middle (all lower case z's pointing to upper Z's :D)
pLines[8].pos = xyz;
pLines[9].pos = xyZ;
pLines[10].pos = xYz;
pLines[11].pos = xYZ;
pLines[12].pos = XYz;
pLines[13].pos = XYZ;
pLines[14].pos = Xyz;
pLines[15].pos = XyZ;
// top row (all upper case z's)
pLines[16].pos = xyZ;
pLines[17].pos = xYZ;
pLines[18].pos = xYZ;
pLines[19].pos = XYZ;
pLines[20].pos = XYZ;
pLines[21].pos = XyZ;
pLines[22].pos = XyZ;
pLines[23].pos = xyZ;
// colors, maybe mix this into pos assignment, since all these verts don't fit in single cache lane ;(
for (uint32_t i = 0; i < numPoints; i++) {
pLines[i].color = col;
}
}
else {
// we do this as triangles.
PrimVertex* pVerts = addPrimitive(36, PrimitiveType::TRIANGLELIST);
const Vec3f& xyz = aabb.min;
const Vec3f xyZ(aabb.min.x, aabb.min.y, aabb.max.z);
const Vec3f xYz(aabb.min.x, aabb.max.y, aabb.min.z);
const Vec3f xYZ(aabb.min.x, aabb.max.y, aabb.max.z);
const Vec3f Xyz(aabb.max.x, aabb.min.y, aabb.min.z);
const Vec3f XyZ(aabb.max.x, aabb.min.y, aabb.max.z);
const Vec3f XYz(aabb.max.x, aabb.max.y, aabb.min.z);
const Vec3f& XYZ = aabb.max;
Color8u colBot(col * 0.5f);
Color8u colTop(col);
Color8u colBack(col * 0.5f);
Color8u colFront(col * 0.9f);
Color8u colLeft(col * 0.7f);
Color8u colRight(col * 0.8f);
// bottom
pVerts[0].pos = xyz;
pVerts[0].color = colBot;
pVerts[1].pos = xYz;
pVerts[1].color = colBot;
pVerts[2].pos = XYz;
pVerts[2].color = colBot;
pVerts[3].pos = xyz;
pVerts[3].color = colBot;
pVerts[4].pos = XYz;
pVerts[4].color = colBot;
pVerts[5].pos = Xyz;
pVerts[5].color = colBot;
// top
pVerts[6].pos = xyZ;
pVerts[6].color = colTop;
pVerts[7].pos = xYZ;
pVerts[7].color = colTop;
pVerts[8].pos = XYZ;
pVerts[8].color = colTop;
pVerts[9].pos = xyZ;
pVerts[9].color = colTop;
pVerts[10].pos = XYZ;
pVerts[10].color = colTop;
pVerts[11].pos = XyZ;
pVerts[11].color = colTop;
// back.
pVerts[12].pos = xyz;
pVerts[12].color = colBack;
pVerts[13].pos = Xyz;
pVerts[13].color = colBack;
pVerts[14].pos = XyZ;
pVerts[14].color = colBack;
pVerts[15].pos = xyz;
pVerts[15].color = colBack;
pVerts[16].pos = XyZ;
pVerts[16].color = colBack;
pVerts[17].pos = xyZ;
pVerts[17].color = colBack;
// front
pVerts[18].pos = xYz;
pVerts[18].color = colFront;
pVerts[19].pos = xYZ;
pVerts[19].color = colFront;
pVerts[20].pos = XYZ;
pVerts[20].color = colFront;
pVerts[21].pos = xYz;
pVerts[21].color = colFront;
pVerts[22].pos = XYZ;
pVerts[22].color = colFront;
pVerts[23].pos = XYz;
pVerts[23].color = colFront;
// left
pVerts[24].pos = xyz;
pVerts[24].color = colLeft;
pVerts[25].pos = xyZ;
pVerts[25].color = colLeft;
pVerts[26].pos = xYZ;
pVerts[26].color = colLeft;
pVerts[27].pos = xyz;
pVerts[27].color = colLeft;
pVerts[28].pos = xYZ;
pVerts[28].color = colLeft;
pVerts[29].pos = xYz;
pVerts[29].color = colLeft;
// right
pVerts[30].pos = Xyz;
pVerts[30].color = colRight;
pVerts[31].pos = XYz;
pVerts[31].color = colRight;
pVerts[32].pos = XYZ;
pVerts[32].color = colRight;
pVerts[33].pos = Xyz;
pVerts[33].color = colRight;
pVerts[34].pos = XYZ;
pVerts[34].color = colRight;
pVerts[35].pos = XyZ;
pVerts[35].color = colRight;
}
}
void IPrimitiveContext::drawOBB(const OBB& obb, bool solid, Color8u col)
{
const auto& orien = obb.orientation();
const auto& pos = obb.center();
const auto& max = obb.halfVec();
auto min = -obb.halfVec();
Vec3f xyz((orien * Vec3f(min.x, min.y, min.z)) + pos);
Vec3f xyZ((orien * Vec3f(min.x, min.y, max.z)) + pos);
Vec3f xYz((orien * Vec3f(min.x, max.y, min.z)) + pos);
Vec3f xYZ((orien * Vec3f(min.x, max.y, max.z)) + pos);
Vec3f Xyz((orien * Vec3f(max.x, min.y, min.z)) + pos);
Vec3f XyZ((orien * Vec3f(max.x, min.y, max.z)) + pos);
Vec3f XYz((orien * Vec3f(max.x, max.y, min.z)) + pos);
Vec3f XYZ((orien * Vec3f(max.x, max.y, max.z)) + pos);
if (!solid) {
const uint32_t numPoints = 24;
// so we need 4 * 3 lines.
PrimVertex* pLines = addPrimitive(numPoints, PrimitiveType::LINELIST);
// bottom row (all lower case z's)
pLines[0].pos = xyz;
pLines[1].pos = xYz;
pLines[2].pos = xYz;
pLines[3].pos = XYz;
pLines[4].pos = XYz;
pLines[5].pos = Xyz;
pLines[6].pos = Xyz;
pLines[7].pos = xyz;
// middle (all lower case z's pointing to upper Z's :D)
pLines[8].pos = xyz;
pLines[9].pos = xyZ;
pLines[10].pos = xYz;
pLines[11].pos = xYZ;
pLines[12].pos = XYz;
pLines[13].pos = XYZ;
pLines[14].pos = Xyz;
pLines[15].pos = XyZ;
// top row (all upper case z's)
pLines[16].pos = xyZ;
pLines[17].pos = xYZ;
pLines[18].pos = xYZ;
pLines[19].pos = XYZ;
pLines[20].pos = XYZ;
pLines[21].pos = XyZ;
pLines[22].pos = XyZ;
pLines[23].pos = xyZ;
for (uint32_t i = 0; i < numPoints; i++) {
pLines[i].color = col;
}
}
else {
PrimVertex* pVerts = addPrimitive(36, PrimitiveType::TRIANGLELIST);
Color8u colBot(col * 0.5f);
Color8u colTop(col);
Color8u colBack(col * 0.5f);
Color8u colFront(col * 0.9f);
Color8u colLeft(col * 0.7f);
Color8u colRight(col * 0.8f);
// bottom
pVerts[0].pos = xyz;
pVerts[0].color = colBot;
pVerts[1].pos = xYz;
pVerts[1].color = colBot;
pVerts[2].pos = XYz;
pVerts[2].color = colBot;
pVerts[3].pos = xyz;
pVerts[3].color = colBot;
pVerts[4].pos = XYz;
pVerts[4].color = colBot;
pVerts[5].pos = Xyz;
pVerts[5].color = colBot;
// top
pVerts[6].pos = xyZ;
pVerts[6].color = colTop;
pVerts[7].pos = xYZ;
pVerts[7].color = colTop;
pVerts[8].pos = XYZ;
pVerts[8].color = colTop;
pVerts[9].pos = xyZ;
pVerts[9].color = colTop;
pVerts[10].pos = XYZ;
pVerts[10].color = colTop;
pVerts[11].pos = XyZ;
pVerts[11].color = colTop;
// back.
pVerts[12].pos = xyz;
pVerts[12].color = colBack;
pVerts[13].pos = Xyz;
pVerts[13].color = colBack;
pVerts[14].pos = XyZ;
pVerts[14].color = colBack;
pVerts[15].pos = xyz;
pVerts[15].color = colBack;
pVerts[16].pos = XyZ;
pVerts[16].color = colBack;
pVerts[17].pos = xyZ;
pVerts[17].color = colBack;
// front
pVerts[18].pos = xYz;
pVerts[18].color = colFront;
pVerts[19].pos = xYZ;
pVerts[19].color = colFront;
pVerts[20].pos = XYZ;
pVerts[20].color = colFront;
pVerts[21].pos = xYz;
pVerts[21].color = colFront;
pVerts[22].pos = XYZ;
pVerts[22].color = colFront;
pVerts[23].pos = XYz;
pVerts[23].color = colFront;
// left
pVerts[24].pos = xyz;
pVerts[24].color = colLeft;
pVerts[25].pos = xyZ;
pVerts[25].color = colLeft;
pVerts[26].pos = xYZ;
pVerts[26].color = colLeft;
pVerts[27].pos = xyz;
pVerts[27].color = colLeft;
pVerts[28].pos = xYZ;
pVerts[28].color = colLeft;
pVerts[29].pos = xYz;
pVerts[29].color = colLeft;
// right
pVerts[30].pos = Xyz;
pVerts[30].color = colRight;
pVerts[31].pos = XYz;
pVerts[31].color = colRight;
pVerts[32].pos = XYZ;
pVerts[32].color = colRight;
pVerts[33].pos = Xyz;
pVerts[33].color = colRight;
pVerts[34].pos = XYZ;
pVerts[34].color = colRight;
pVerts[35].pos = XyZ;
pVerts[35].color = colRight;
}
}
void IPrimitiveContext::drawOBB(const OBB& obb, const Vec3f& offset, bool solid, Color8u col)
{
const auto& orien = obb.orientation();
const auto& max = obb.halfVec();
auto min = -obb.halfVec();
auto pos = obb.center() + offset;
Vec3f xyz((orien * Vec3f(min.x, min.y, min.z)) + pos);
Vec3f xyZ((orien * Vec3f(min.x, min.y, max.z)) + pos);
Vec3f xYz((orien * Vec3f(min.x, max.y, min.z)) + pos);
Vec3f xYZ((orien * Vec3f(min.x, max.y, max.z)) + pos);
Vec3f Xyz((orien * Vec3f(max.x, min.y, min.z)) + pos);
Vec3f XyZ((orien * Vec3f(max.x, min.y, max.z)) + pos);
Vec3f XYz((orien * Vec3f(max.x, max.y, min.z)) + pos);
Vec3f XYZ((orien * Vec3f(max.x, max.y, max.z)) + pos);
if (!solid) {
const uint32_t numPoints = 24;
// so we need 4 * 3 lines.
PrimVertex* pLines = addPrimitive(numPoints, PrimitiveType::LINELIST);
// bottom row (all lower case z's)
pLines[0].pos = xyz;
pLines[1].pos = xYz;
pLines[2].pos = xYz;
pLines[3].pos = XYz;
pLines[4].pos = XYz;
pLines[5].pos = Xyz;
pLines[6].pos = Xyz;
pLines[7].pos = xyz;
// middle (all lower case z's pointing to upper Z's :D)
pLines[8].pos = xyz;
pLines[9].pos = xyZ;
pLines[10].pos = xYz;
pLines[11].pos = xYZ;
pLines[12].pos = XYz;
pLines[13].pos = XYZ;
pLines[14].pos = Xyz;
pLines[15].pos = XyZ;
// top row (all upper case z's)
pLines[16].pos = xyZ;
pLines[17].pos = xYZ;
pLines[18].pos = xYZ;
pLines[19].pos = XYZ;
pLines[20].pos = XYZ;
pLines[21].pos = XyZ;
pLines[22].pos = XyZ;
pLines[23].pos = xyZ;
for (uint32_t i = 0; i < numPoints; i++) {
pLines[i].color = col;
}
}
else {
PrimVertex* pVerts = addPrimitive(36, PrimitiveType::TRIANGLELIST);
Color8u colBot(col * 0.5f);
Color8u colTop(col);
Color8u colBack(col * 0.5f);
Color8u colFront(col * 0.9f);
Color8u colLeft(col * 0.7f);
Color8u colRight(col * 0.8f);
// bottom
pVerts[0].pos = xyz;
pVerts[0].color = colBot;
pVerts[1].pos = xYz;
pVerts[1].color = colBot;
pVerts[2].pos = XYz;
pVerts[2].color = colBot;
pVerts[3].pos = xyz;
pVerts[3].color = colBot;
pVerts[4].pos = XYz;
pVerts[4].color = colBot;
pVerts[5].pos = Xyz;
pVerts[5].color = colBot;
// top
pVerts[6].pos = xyZ;
pVerts[6].color = colTop;
pVerts[7].pos = xYZ;
pVerts[7].color = colTop;
pVerts[8].pos = XYZ;
pVerts[8].color = colTop;
pVerts[9].pos = xyZ;
pVerts[9].color = colTop;
pVerts[10].pos = XYZ;
pVerts[10].color = colTop;
pVerts[11].pos = XyZ;
pVerts[11].color = colTop;
// back.
pVerts[12].pos = xyz;
pVerts[12].color = colBack;
pVerts[13].pos = Xyz;
pVerts[13].color = colBack;
pVerts[14].pos = XyZ;
pVerts[14].color = colBack;
pVerts[15].pos = xyz;
pVerts[15].color = colBack;
pVerts[16].pos = XyZ;
pVerts[16].color = colBack;
pVerts[17].pos = xyZ;
pVerts[17].color = colBack;
// front
pVerts[18].pos = xYz;
pVerts[18].color = colFront;
pVerts[19].pos = xYZ;
pVerts[19].color = colFront;
pVerts[20].pos = XYZ;
pVerts[20].color = colFront;
pVerts[21].pos = xYz;
pVerts[21].color = colFront;
pVerts[22].pos = XYZ;
pVerts[22].color = colFront;
pVerts[23].pos = XYz;
pVerts[23].color = colFront;
// left
pVerts[24].pos = xyz;
pVerts[24].color = colLeft;
pVerts[25].pos = xyZ;
pVerts[25].color = colLeft;
pVerts[26].pos = xYZ;
pVerts[26].color = colLeft;
pVerts[27].pos = xyz;
pVerts[27].color = colLeft;
pVerts[28].pos = xYZ;
pVerts[28].color = colLeft;
pVerts[29].pos = xYz;
pVerts[29].color = colLeft;
// right
pVerts[30].pos = Xyz;
pVerts[30].color = colRight;
pVerts[31].pos = XYz;
pVerts[31].color = colRight;
pVerts[32].pos = XYZ;
pVerts[32].color = colRight;
pVerts[33].pos = Xyz;
pVerts[33].color = colRight;
pVerts[34].pos = XYZ;
pVerts[34].color = colRight;
pVerts[35].pos = XyZ;
pVerts[35].color = colRight;
}
}
// Sphere
void IPrimitiveContext::drawSphere(const Sphere& sphere, Color8u col, bool solid, int32_t lodIdx)
{
if (sphere.radius() > 0.0f) {
Matrix44f mat = Matrix44f::createScale(Vec3f(sphere.radius()));
mat.setTranslate(sphere.center());
ShapeInstanceData* pObj = addShape(ShapeType::Sphere, solid, lodIdx);
pObj->mat = mat;
pObj->color = col;
}
}
void IPrimitiveContext::drawSphere(const Sphere& sphere, const Matrix34f& mat, Color8u col, bool solid, int32_t lodIdx)
{
if (sphere.radius() > 0.0f) {
Matrix44f scale = Matrix44f::createScale(sphere.radius());
Matrix44f trans = Matrix44f::createTranslation(mat * sphere.center());
Matrix44f transMat = trans * scale;
ShapeInstanceData* pObj = addShape(ShapeType::Sphere, solid, lodIdx);
pObj->mat = transMat;
pObj->color = col;
}
}
// Cone
void IPrimitiveContext::drawCone(const Vec3f& pos, const Vec3f& dir, float radius, float height,
Color8u col, bool solid, int32_t lodIdx)
{
if (radius > 0.0f && height > 0.0f && dir.lengthSquared() > 0.0f) {
Vec3f direction(dir.normalized());
Vec3f orthogonal(direction.getOrthogonal().normalized());
Matrix44f matRot;
matRot.setToIdentity();
matRot.setColumn(0, orthogonal);
matRot.setColumn(1, direction);
matRot.setColumn(2, orthogonal.cross(direction));
Matrix44f scale = Matrix44f::createScale(Vec3f(radius, height, radius));
Matrix44f trans = Matrix44f::createTranslation(pos);
Matrix44f transMat = trans * matRot * scale;
ShapeInstanceData* pObj = addShape(ShapeType::Cone, solid, lodIdx);
pObj->mat = transMat;
pObj->color = col;
}
}
// Cylinder
void IPrimitiveContext::drawCylinder(const Vec3f& pos, const Vec3f& dir, float radius, float height,
Color8u col, bool solid, int32_t lodIdx)
{
if (radius > 0.0f && height > 0.0f && dir.lengthSquared() > 0.0f) {
Vec3f direction(dir.normalized());
Vec3f orthogonal(direction.getOrthogonal().normalized());
Matrix44f matRot;
matRot.setToIdentity();
matRot.setColumn(0, orthogonal);
matRot.setColumn(1, direction);
matRot.setColumn(2, orthogonal.cross(direction));
Matrix44f scale = Matrix44f::createScale(Vec3f(radius, height, radius));