-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderer.h
871 lines (694 loc) · 25.4 KB
/
Renderer.h
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
#ifndef RR_RENDERER
#define RR_RENDERER
// this is kind of a Platform thing, thats why it was in buffers.h...
struct RenderCommands
{
u32 maxBufferSize;
u32 pushBufferSize;
u8 *pushBufferBase;
u32 width;
u32 height;
f32 aspectRatio;
};
enum RenderGroupEntryType
{
RenderGroup_EntryChangeRenderSetup,
RenderGroup_EntryClear,
RenderGroup_EntryTexturedQuads,
RenderGroup_EntryLines,
RenderGroup_EntryTriangles,
RenderGroup_EntryTriangleMesh,
};
enum OpenGLShaderFlags : u32
{
ShaderFlags_None = 0x0,
ShaderFlags_Textured = 0x1,
ShaderFlags_ShadowMapping = 0x2,
ShaderFlags_Phong = 0x4,
ShaderFlags_Animated = 0x8,
ShaderFlags_ZBias = 0x10,
ShaderFlags_MultiTextured = 0x20,
ShaderArray_Size = 0x40, // right now this does not have to be a hash map I guess..
};
// not sure if we should try to make this as small as possible and calculate more later
struct RenderSetup
{
u32 flags;
b32 isProjective; // todo we could actually join this with the flags, if we just make it real big, bigger then size.
m4x4 projection;
m4x4 cameraTransform;
m4x4 shadowMat;
v3 lightP;
v3 cameraP;
};
struct RenderGroupEntryHeader
{
RenderGroupEntryType type;
};
struct EntryChangeRenderSetup
{
RenderGroupEntryHeader header;
RenderSetup setup;
};
struct EntryColoredVertices
{
RenderGroupEntryHeader header;
VertexFormatPC *data;
u32 vertexCount;
u32 maxAmount;
};
struct EntryClear
{
RenderGroupEntryHeader header;
v4 color;
};
struct EntryTriangleMesh
{
RenderGroupEntryHeader header;
u32 vertexFormatSize;
u32 meshType;
m4x4 objectTransform;
v4 scaleColor;
m4x4Array boneStates;
IndexSetArray indexSets;
u32Array textureIDs;
u32 vertexVBO;
u32 indexVBO;
};
struct EntryTexturedQuads
{
RenderGroupEntryHeader header;
VertexFormatPCUI *vertexBuffer;
u16 *indexBuffer;
u32 count;
u32 maxAmount;
};
struct RenderGroup
{
EntryColoredVertices *currentTriangles;
EntryColoredVertices *currentLines;
EntryTexturedQuads *currentQuads;
RenderSetup setup;
RenderCommands *commands;
AssetHandler *assetHandler;
};
static RenderGroup InitRenderGroup(AssetHandler *assetHandler, RenderCommands *comands)
{
Assert(comands);
Assert(assetHandler);
RenderGroup ret;
ret.assetHandler = assetHandler;
ret.currentLines = NULL;
ret.currentQuads = NULL;
ret.currentTriangles = NULL;
ret.commands = comands;
return ret;
}
#define PushRenderEntryType(type, entryId) (type *)PushRenderEntry_(rg, sizeof(type), RenderGroup_##entryId)
#define PushRenderEntry(type) (type *)PushRenderEntry_(rg, sizeof(type), RenderGroup_##type)
static RenderGroupEntryHeader *PushRenderEntry_(RenderGroup *rg, u32 size, RenderGroupEntryType type)
{
RenderGroupEntryHeader *result = 0;
if (rg->commands)
{
RenderCommands *commands = rg->commands;
if (commands->pushBufferSize + size < commands->maxBufferSize)
{
result = (RenderGroupEntryHeader *)(commands->pushBufferBase + commands->pushBufferSize);
result->type = type;
commands->pushBufferSize += size;
}
else
{
Die;
}
}
else
{
Die;
}
return result;
}
static void PushOrthogonalSetup(RenderGroup *rg, u32 flags)
{
TimedBlock;
if(rg->setup.isProjective == false && rg->setup.flags == flags) return;
EntryChangeRenderSetup *entry = PushRenderEntry(EntryChangeRenderSetup);
RenderSetup *setup = &entry->setup;
{
m4x4 ortho = Orthogonal(1.0f, 1.0f);
setup->projection = Translate(ortho, V3(-1.0f, 1.0f, 0.0f));
setup->cameraTransform = Identity();
setup->isProjective = false;
}
// todo reuse the rest of the memory!
rg->currentLines = NULL;
rg->currentTriangles = NULL;
rg->currentQuads = NULL;
setup->flags = flags;
rg->setup = *setup;
}
static void PushProjectiveSetup(RenderGroup *rg, Camera camera, LightSource lightSource, u32 flags)
{
TimedBlock;
// todo this is fine for now, but if we doit like this we should have frame data, like camera pos and lightsource.... it does not really make sense to pass them every time.
if(rg->setup.isProjective && rg->setup.flags == flags) return;
RenderCommands *commands = rg->commands;
EntryChangeRenderSetup *entry = PushRenderEntry(EntryChangeRenderSetup);
RenderSetup *setup = &entry->setup;
{//projective Codes
setup->projection = Projection(commands->aspectRatio, camera.focalLength);
setup->cameraTransform = CameraTransform(camera.orientation, camera.pos);;
setup->lightP = lightSource.pos;
setup->shadowMat = CameraTransform(lightSource.orientation, lightSource.pos);
setup->cameraP = camera.pos;
setup->isProjective = true;
}
rg->currentLines = NULL;
rg->currentTriangles = NULL;
rg->currentQuads = NULL;
setup->flags = flags;
rg->setup = *setup;
}
static VertexFormatPC *GetVertexMemory(RenderGroup *rg, RenderGroupEntryType type)
{
switch (type)
{
case RenderGroup_EntryLines:
{
if (!rg->currentLines)
{
EntryColoredVertices *lines = PushRenderEntryType(EntryColoredVertices, EntryLines);
lines->maxAmount = 5536;
lines->data = PushData(frameArena, VertexFormatPC, lines->maxAmount);
lines->vertexCount = 0;
rg->currentLines = lines;
}
u32 amount = 2;
auto currentVertices = rg->currentLines;
if (currentVertices->vertexCount + amount < currentVertices->maxAmount)
{
VertexFormatPC *ret = currentVertices->data + currentVertices->vertexCount;
currentVertices->vertexCount += amount;
return ret;
}
else
{
rg->currentLines = NULL;
return GetVertexMemory(rg, type);
}
}break;
case RenderGroup_EntryTriangles:
{
if (!rg->currentTriangles)
{
EntryColoredVertices *triangles = PushRenderEntryType(EntryColoredVertices, EntryTriangles);
triangles->maxAmount = 5536;
triangles->data = PushData(frameArena, VertexFormatPC, triangles->maxAmount);
triangles->vertexCount = 0;
rg->currentTriangles = triangles;
}
u32 amount = 3;
auto currentVertices = rg->currentTriangles;
if (currentVertices->vertexCount + amount < currentVertices->maxAmount)
{
VertexFormatPC *ret = currentVertices->data + currentVertices->vertexCount;
currentVertices->vertexCount += amount;
return ret;
}
else
{
rg->currentTriangles = NULL;
return GetVertexMemory(rg, type);
}
}break;
default:
{
Die;
}break;
}
return NULL;
}
static void PushTriangle(RenderGroup *rg, v3 p1, v3 p2, v3 p3, u32 c1, u32 c2, u32 c3)
{
VertexFormatPC *vert = GetVertexMemory(rg, RenderGroup_EntryTriangles);
vert[0].p = p1;
vert[1].p = p2;
vert[2].p = p3;
vert[0].c = c1;
vert[1].c = c2;
vert[2].c = c3;
}
static void PushTriangle(RenderGroup *rg, VertexFormatPC cv1, VertexFormatPC cv2, VertexFormatPC cv3)
{
PushTriangle(rg, cv1.p, cv2.p, cv3.p, cv1.c, cv2.c, cv3.c);
}
static void PushTriangle(RenderGroup *rg, v3 p1, v3 p2, v3 p3, u32 c)
{
PushTriangle(rg, p1, p2, p3, c, c, c);
}
static void PushTriangle(RenderGroup *rg, v3 p1, v3 p2, v3 p3, v4 color = V4(1, 1, 1, 1))
{
PushTriangle(rg, p1, p2, p3, Pack4x8(color));
}
static f32 LayerToZLayer(i32 layer)
{
// only negative exponents, retains sign of layer looses first bit..
#if 0
u32 bias = (1 << 7) - 1;
u32 biasedExp = exp + bias;
u32 whatsThis = (((u32)biasedExp) << 23);
u32 up = whatsThis | (1u << 30);
f32 Zlayer = *(f32 *)&up;
#else
f32 zLayer;
if(layer >= 0)
{
zLayer = 1.0f - 0.5f/((u32) layer + 1); // maps 0... to 0.5, ....
}
else
{
zLayer = 0.5f / ((u32)(-layer) + 1);
}
#endif
return zLayer;
}
static void PushTriangle(RenderGroup *rg, v2 p1, v2 p2, v2 p3, i32 layer, u32 c)
{
f32 zLayer = LayerToZLayer(layer);
PushTriangle(rg, V3(p1, zLayer), V3(p2, zLayer), V3(p3, zLayer), c);
}
static void PushTriangle(RenderGroup *rg, v3 p1, v3 p2, v3 p3, v3 color)
{
PushTriangle(rg, p1, p2, p3, V4(1.0f, color));
}
static void PushTriangleMesh(RenderGroup *rg, TriangleMesh *mesh, Quaternion orientation, v3 pos, f32 scale, v4 scaleColor)
{
if (!mesh) return; // todo think about this, we want this if we redo our mesh write
EntryTriangleMesh *meshHeader = PushRenderEntry(EntryTriangleMesh);
u32Array arr = PushArray(frameArena, u32, mesh->indexSets.amount);
// todo this still feels pretty stupid
for (u32 i = 0; i < arr.amount; i++)
{
arr[i] = GetTexture(rg->assetHandler, mesh->indexSets[i].mat.bitmapID).index;
}
// todo most of these could be stored on the asset entry. But maybe this should just be on there for cache
// this depeds whether I usually access these per id or per mesh, per id we might be able to only get the
// asset entry. We might be able to get rid of meshes that way.
meshHeader->vertexFormatSize = mesh->vertexFormatSize;
meshHeader->vertexVBO = mesh->vertexVBO;
meshHeader->indexVBO = mesh->indexVBO;
meshHeader->indexSets = mesh->indexSets;
meshHeader->textureIDs = arr;
meshHeader->objectTransform = InterpolationDataToMatrix(pos, orientation, scale);
meshHeader->scaleColor = scaleColor;
meshHeader->meshType = mesh->type;
meshHeader->boneStates = {};
}
static void PushTriangleMesh(RenderGroup *rg, u32 meshId, Quaternion orientation, v3 pos, f32 scale, v4 scaleColor)
{
TriangleMesh *mesh = GetMesh(rg->assetHandler, meshId);
PushTriangleMesh(rg, mesh, orientation, pos, scale, scaleColor);
}
// todo unify this with pushTriangle mesh.
static void PushAnimatedMesh(RenderGroup *rg, TriangleMesh *mesh, Quaternion orientation, v3 pos, f32 scale, v4 scaleColor, m4x4Array boneStates)
{
if (!mesh) return; // todo think about this, we want this if we redo our mesh write
EntryTriangleMesh *meshHeader = PushRenderEntry(EntryTriangleMesh);
u32Array arr = PushArray(frameArena, u32, mesh->indexSets.amount);
for (u32 i = 0; i < arr.amount; i++)
{
arr[i] = GetTexture(rg->assetHandler, mesh->indexSets[i].mat.bitmapID).index;
}
meshHeader->vertexFormatSize = mesh->vertexFormatSize;
meshHeader->vertexVBO = mesh->vertexVBO;
meshHeader->indexVBO = mesh->indexVBO;
meshHeader->indexSets = mesh->indexSets;
meshHeader->textureIDs = arr;
meshHeader->objectTransform =InterpolationDataToMatrix(pos, orientation, scale);
meshHeader->scaleColor = scaleColor;
meshHeader->boneStates = boneStates;
meshHeader->meshType = mesh->type;
}
static void PushAnimatedMesh(RenderGroup *rg, u32 meshId, Quaternion orientation, v3 pos, f32 scale, v4 scaleColor, m4x4Array boneStates)
{
PushAnimatedMesh(rg, GetMesh(rg->assetHandler, meshId), orientation, pos, scale, scaleColor, boneStates);
}
static void PushAnimatedMeshImmidiet(RenderGroup *rg, TriangleMesh *mesh, Quaternion orientation, v3 pos, f32 scale, v4 scaleColor, m4x4Array boneStates)
{
Skeleton *skeleton = &mesh->skeleton;
#if 1
v3Array out = PushArray(frameArena, v3, mesh->positions.amount);
for (u32 i = 0; i < mesh->positions.amount; i++)
{
u32 unflattend = skeleton->vertexMap[i];
WeightDataArray weights = skeleton->vertexToWeightsMap[unflattend];
out[i] = V3();
v4 v = V4(mesh->positions[i], 1.0f);
For(weights)
{
out[i] += it->weight * (boneStates[it->boneIndex] * v).xyz;
}
}
for(u32 i = 0; i < mesh->positions.amount; i++)
{
u32 i0 = mesh->indices[i + 0];
u32 i1 = mesh->indices[i + 1];
u32 i2 = mesh->indices[i + 2];
PushTriangle(rg, out[i0], out[i1], out[i2]);
}
#else
for (u32 i = 0; i < mesh->indices.amount; i += 3)
{
u16 i1 = mesh->indices[i + 0];
u16 i2 = mesh->indices[i + 1];
u16 i3 = mesh->indices[i + 2];
VertexFormatPCUNBD x1 = mesh->dumbDebugPointer[i1];
VertexFormatPCUNBD x2 = mesh->dumbDebugPointer[i2];
VertexFormatPCUNBD x3 = mesh->dumbDebugPointer[i3];
v3 p1 = V3();
{
v4 boneWeights = x1.bw;
v4i boneIndices = x1.bi;
v4 inputVertex = V4(x1.p, 1.0f);
p1 += boneWeights.x * (boneStates[boneIndices.x] * inputVertex).xyz;
p1 += boneWeights.y * (boneStates[boneIndices.y] * inputVertex).xyz;
p1 += boneWeights.z * (boneStates[boneIndices.z] * inputVertex).xyz;
p1 += boneWeights.w * (boneStates[boneIndices.w] * inputVertex).xyz;
}
v3 p2 = V3();
{
v4 boneWeights = x2.bw;
v4i boneIndices = x2.bi;
v4 inputVertex = V4(x2.p, 1.0f);
p2 += boneWeights.x * (boneStates[boneIndices.x] * inputVertex).xyz;
p2 += boneWeights.y * (boneStates[boneIndices.y] * inputVertex).xyz;
p2 += boneWeights.z * (boneStates[boneIndices.z] * inputVertex).xyz;
p2 += boneWeights.w * (boneStates[boneIndices.w] * inputVertex).xyz;
}
v3 p3 = V3();
{
v4 boneWeights = x3.bw;
v4i boneIndices = x3.bi;
v4 inputVertex = V4(x3.p, 1.0f);
p3 += boneWeights.x * (boneStates[boneIndices.x] * inputVertex).xyz;
p3 += boneWeights.y * (boneStates[boneIndices.y] * inputVertex).xyz;
p3 += boneWeights.z * (boneStates[boneIndices.z] * inputVertex).xyz;
p3 += boneWeights.w * (boneStates[boneIndices.w] * inputVertex).xyz;
}
PushTriangle(rg, p1, p2, p3, scaleColor);
}
#endif
}
static void PushLine(RenderGroup *rg, v3 p1, v3 p2, u32 color = 0xFFFFFFFF)
{
VertexFormatPC *vert = GetVertexMemory(rg, RenderGroup_EntryLines);
if (vert)
{
vert[0].p = p1;
vert[1].p = p2;
vert[0].c = color;
vert[1].c = color;
}
}
static void PushLine(RenderGroup *rg, v2 p1, v2 p2)
{
PushLine(rg, i12(p1), i12(p2));
}
static void PushOutlinedTriangle(RenderGroup *rg, v3 p1, v3 p2, v3 p3, v4 color)
{
PushTriangle(rg, p1, p2, p3, color);
PushLine(rg, p1, p2);
PushLine(rg, p1, p3);
PushLine(rg, p2, p3);
}
static void PushOutlinedTriangle(RenderGroup *rg, v3 p1, v3 p2, v3 p3, u32 c1, u32 c2, u32 c3)
{
PushTriangle(rg, p1, p2, p3, c1, c2, c3);
PushLine(rg, p1, p2);
PushLine(rg, p1, p3);
PushLine(rg, p2, p3);
}
static void PushCuboid(RenderGroup *rg, v3 pos, Vector3Basis basis, v4 color = V4(1, 1, 1, 1))
{
v3 d1 = basis.d1;
v3 d2 = basis.d2;
v3 d3 = basis.d3;
v3 p = pos;
v3 p1 = pos + d1;
v3 p2 = pos + d2;
v3 p3 = pos + d3;
v3 p12 = pos + d1 + d2;
v3 p13 = pos + d1 + d3;
v3 p23 = pos + d2 + d3;
v3 p123 = pos + d1 + d2 + d3;
PushTriangle(rg, p, p1, p2, color);
PushTriangle(rg, p12, p1, p2, color);
PushTriangle(rg, p, p1, p3, color);
PushTriangle(rg, p13, p1, p3, color);
PushTriangle(rg, p, p2, p3, color);
PushTriangle(rg, p23, p2, p3, color);
PushTriangle(rg, p3, p13, p23, color);
PushTriangle(rg, p123, p13, p23, color);
PushTriangle(rg, p2, p12, p23, color);
PushTriangle(rg, p123, p12, p23, color);
PushTriangle(rg, p1, p12, p13, color);
PushTriangle(rg, p123, p12, p13, color);
}
static void PushCenteredCuboid(RenderGroup *rg, v3 pos, Vector3Basis basis, v4 color = V4(1, 1, 1, 1))
{
v3 posAd = pos - 0.5f * basis.d1 - 0.5f * basis.d2 - 0.5f * basis.d3;
PushCuboid(rg, posAd, basis, color);
}
static void PushDebugPointCuboid(RenderGroup *rg, v3 pos, v4 color = V4(1, 1, 1, 1))
{
f32 scale = 0.1f;
PushCenteredCuboid(rg, pos, scale * v3StdBasis, color);
}
static void PushAABBOutLine(RenderGroup *rg, v3 minDim, v3 maxDim)
{
v3 d1 = V3(maxDim.x - minDim.x, 0, 0);
v3 d2 = V3(0, maxDim.y - minDim.y, 0);
v3 d3 = V3(0, 0, maxDim.z - minDim.z);
v3 p[8] =
{
minDim, //0
minDim + d1, //1
minDim + d2, //2
minDim + d3, //3
minDim + d1 + d2, //4
minDim + d2 + d3, //5
minDim + d3 + d1, //6
maxDim, //7
};
//_upper_ square
PushLine(rg, p[0], p[1]);
PushLine(rg, p[1], p[4]);
PushLine(rg, p[4], p[2]);
PushLine(rg, p[2], p[0]);
//_lower_ square
PushLine(rg, p[7], p[5]);
PushLine(rg, p[5], p[3]);
PushLine(rg, p[3], p[6]);
PushLine(rg, p[6], p[7]);
//_connecting_ lines
PushLine(rg, p[0], p[3]);
PushLine(rg, p[2], p[5]);
PushLine(rg, p[1], p[6]);
PushLine(rg, p[4], p[7]);
}
static void PushClear(RenderGroup *rg, v4 color)
{
EntryClear *result = PushRenderEntry(EntryClear);
if (result)
{
result->color = color;
}
}
static VertexFormatPCUI *GetTexturedQuadMemory(RenderGroup *rg)
{
if (!rg->currentQuads)
{
EntryTexturedQuads *quads = PushRenderEntry(EntryTexturedQuads);
if(quads)
{
quads->maxAmount = 1000;
quads->vertexBuffer = PushData(frameArena, VertexFormatPCUI, 4u * quads->maxAmount);
quads->indexBuffer = PushData(frameArena, u16, 6u * quads->maxAmount);
quads->count = 0;
rg->currentQuads = quads;
}
else
{
Die;
return NULL;
}
}
if (rg->currentQuads->count < rg->currentQuads->maxAmount)
{
VertexFormatPCUI *verts = rg->currentQuads->vertexBuffer + 4u * rg->currentQuads->count;
u16 vertsAt = (u16)(4u * rg->currentQuads->count);
Assert(vertsAt == (4u * rg->currentQuads->count));
u32 indexAt = 6u * rg->currentQuads->count;
u16 *indices = rg->currentQuads->indexBuffer + indexAt;
*indices++ = (vertsAt + 0);
*indices++ = (vertsAt + 1);
*indices++ = (vertsAt + 2);
*indices++ = (vertsAt + 2);
*indices++ = (vertsAt + 1);
*indices++ = (vertsAt + 3);
rg->currentQuads->count++;
return verts;
}
else
{
rg->currentQuads = NULL;
return GetTexturedQuadMemory(rg);
}
}
static void PushTexturedQuad(RenderGroup *rg, v3 p1, v3 p2, v3 p3, v3 p4, TextureIndex textureIndex, bool inverted, v2 minUV, v2 maxUV, u32 c1, u32 c2, u32 c3, u32 c4)
{
VertexFormatPCUI *verts = GetTexturedQuadMemory(rg);
if(!verts) return;
verts[0].p = p1;
verts[1].p = p2;
verts[2].p = p3;
verts[3].p = p4;
if (inverted)
{
verts[0].uv = V2(minUV.x, minUV.y); // 0 0
verts[1].uv = V2(maxUV.x, minUV.y); // 1 0
verts[2].uv = V2(minUV.x, maxUV.y); // 0 1
verts[3].uv = V2(maxUV.x, maxUV.y); // 1 1
}
else
{
verts[0].uv = V2(minUV.x, maxUV.y); // 0 1
verts[1].uv = V2(maxUV.x, maxUV.y); // 1 1
verts[2].uv = V2(minUV.x, minUV.y); // 0 0
verts[3].uv = V2(maxUV.x, minUV.y); // 1 0
}
verts[0].c = c1;
verts[1].c = c2;
verts[2].c = c3;
verts[3].c = c4;
u16 castTextureIndex = (u16)textureIndex.index;
Assert(castTextureIndex == textureIndex.index);
verts[0].textureIndex = castTextureIndex;
verts[1].textureIndex = castTextureIndex;
verts[2].textureIndex = castTextureIndex;
verts[3].textureIndex = castTextureIndex;
}
static void PushTexturedQuad(RenderGroup *rg, v3 p1, v3 p2, v3 p3, v3 p4, TextureIndex textureIndex, bool inverted = false, v2 minUV = {0, 0}, v2 maxUV = {1, 1}, u32 c = 0xFFFFFFFF)
{
PushTexturedQuad(rg, p1, p2, p3, p4, textureIndex, inverted, minUV, maxUV, c, c, c, c);
}
static void PushTexturedQuad(RenderGroup *rg, v3 p1, v3 p2, v3 p3, v3 p4, u32 textureId, bool inverted = false, v2 minUV = {0, 0}, v2 maxUV = {1, 1}, v4 color = {1, 1, 1, 1})
{
TextureIndex textureIndex = GetTexture(rg->assetHandler, textureId);
PushTexturedQuad(rg, p1, p2, p3, p4, textureIndex, inverted, minUV, maxUV, Pack4x8(color));
}
static void PushTexturedRect(RenderGroup *rg, v2 p1, f32 width, f32 height, TextureIndex textureIndex, i32 layer, bool inverted = false, v2 minUV = {0, 0}, v2 maxUV = {1, 1}, v4 color = {1, 1, 1, 1})
{
f32 zLayer = LayerToZLayer(layer);
v2 p2 = p1 + V2(width, 0);
v2 p3 = p1 + V2(0, height);
v2 p4 = p1 + V2(width, height);
PushTexturedQuad(rg, V3(p1, zLayer), V3(p2, zLayer), V3(p3, zLayer), V3(p4, zLayer), textureIndex, inverted, minUV, maxUV, Pack4x8(color));
}
static void PushTexturedRect(RenderGroup *rg, v2 p1, f32 width, f32 height, u32 textureId, i32 layer, bool inverted = false, v2 minUV = {0, 0}, v2 maxUV = {1, 1}, v4 color = {1, 1, 1, 1})
{
TextureIndex textureIndex = GetTexture(rg->assetHandler, textureId);
f32 zLayer = LayerToZLayer(layer);
v2 p2 = p1 + V2(width, 0);
v2 p3 = p1 + V2(0, height);
v2 p4 = p1 + V2(width, height);
PushTexturedQuad(rg, V3(p1, zLayer), V3(p2, zLayer), V3(p3, zLayer), V3(p4, zLayer), textureIndex, inverted, minUV, maxUV, Pack4x8(color));
}
static void PushQuadrilateral(RenderGroup *rg, v3 p1, v3 p2, v3 p3, v3 p4, u32 c)
{
TextureIndex textureIndex = GetTexture(rg->assetHandler, rg->assetHandler->whiteTextureId);
PushTexturedQuad(rg, p1, p2, p3, p4, textureIndex, false, {0, 0}, {1, 1}, c);
}
static void PushRectangle(RenderGroup *rg, v3 pos, v3 vec1, v3 vec2, v4 color)
{
PushQuadrilateral(rg, pos, pos + vec1, pos + vec2, pos + vec1 + vec2, Pack4x8(color));
}
static void PushRectangle(RenderGroup *rg, v2 min, v2 max, i32 layer, u32 c)
{
v2 p1 = min;
v2 p2 = V2(max.x, min.y);
v2 p3 = V2(min.x, max.y);
v2 p4 = max;
f32 Zlayer = LayerToZLayer(layer);
PushQuadrilateral(rg, V3(p1, Zlayer), V3(p2, Zlayer), V3(p3, Zlayer), V3(p4, Zlayer), c);
}
static void PushRectangle(RenderGroup *rg, v2 min, v2 max, i32 layer, v4 color)
{
PushRectangle(rg, min, max, layer, Pack4x8(color));
}
static void PushRectangle(RenderGroup *rg, v2 pos, f32 width, f32 height, i32 layer, u32 c)
{
PushRectangle(rg, pos, pos + V2(width, height), layer, c);
}
static void PushRectangle(RenderGroup *rg, v2 pos, f32 width, f32 height, i32 layer, v4 color)
{
PushRectangle(rg, pos, pos + V2(width, height), layer, Pack4x8(color));
}
static void PushCenteredRectangle(RenderGroup *rg, v2 pos, float width, float height, i32 layer, v4 color)
{
v2 vec1 = V2(width, 0.0f);
v2 vec2 = V2(0.0f, height);
v2 p1 = (pos - 0.5f * (vec1 + vec2));
v2 p2 = (pos + vec1 - 0.5f * (vec1 + vec2));
v2 p3 = (pos + vec2 - 0.5f * (vec1 + vec2));
v2 p4 = (pos + vec1 + vec2 - 0.5f * (vec1 + vec2));
PushRectangle(rg, pos - 0.5f * V2(width, height), pos + 0.5f * V2(width, height), layer, Pack4x8(color));
}
// exactly used in colorpicker...
static void PushRectangle(RenderGroup *rg, v2 pos, f32 width, f32 height, i32 layer, u32 c1, u32 c2, u32 c3, u32 c4)
{
v2 p1 = pos;
v2 p2 = V2(pos.x + width, pos.y);
v2 p3 = V2(pos.x, pos.y + height);
v2 p4 = pos + V2(width, height);
TextureIndex textureIndex = GetTexture(rg->assetHandler, rg->assetHandler->whiteTextureId);
f32 zLayer = LayerToZLayer(layer);
PushTexturedQuad(rg, V3(p1, zLayer), V3(p2, zLayer), V3(p3, zLayer), V3(p4, zLayer), textureIndex, false, {0, 0}, {1, 1}, c1, c2, c3, c4);
}
static f32 PushString(RenderGroup *rg, v2 pos, i32 layer, Char* string, u32 stringLength, f32 size, v4 color = V4(1, 1, 1, 1))
{
TimedBlock;
f32 x = pos.x;
Font font = rg->assetHandler->currentlyLoadedFont;
float fScale = size / (f32)font.charHeight;
TextureIndex tex = GetTexture(rg->assetHandler, font.textureId);
for(u32 i = 0; i < stringLength; i++)
{
if(string[i] >= font.charData.amount)
{
Assert(!"Not handled font symbol");
}
CharData data = font.charData[string[i]];
f32 offSetX = fScale * data.xOff;
f32 offSetY = fScale * data.yOff;
f32 scaledWidth = fScale * (f32)data.width;
f32 scaledHeight = fScale * (f32)data.height;
f32 y = pos.y + (f32)size;
v2 writePos = V2(x + offSetX, y + offSetY);
PushTexturedRect(rg, writePos, scaledWidth, scaledHeight, tex, layer, true, data.minUV, data.maxUV, color);
float actualFloatWidth = data.xAdvance * fScale;
x += actualFloatWidth;
}
return (x - pos.x);
}
// todo this is kinda in efficient
static f32 PushString(RenderGroup *rg, v2 pos, i32 layer, const char* string, f32 size, v4 color = V4(1, 1, 1, 1))
{
return PushString(rg, pos, layer, (Char *)string, NullTerminatedStringLength(string), size, color);
}
static f32 PushString(RenderGroup *rg, v2 pos, i32 layer, String string, f32 size, v4 color = V4(1, 1, 1, 1))
{
return PushString(rg, pos, layer, string.data, string.length, size, color);
}
#endif