-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrasterize.c
More file actions
699 lines (586 loc) · 23.1 KB
/
Copy pathrasterize.c
File metadata and controls
699 lines (586 loc) · 23.1 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
/**
* Basic rasterizer / affine texture mapper
*/
#include <stdlib.h>
#include <string.h>
#include "rasterize.h"
#define RGBCOMPSCALE(col, shift, mask, s) ((FIXED_INT_ROUND(imul(INT_FIXED(((col) >> (shift)) & (mask)), (s)))) << (shift))
#define RGB322SCALE(col, s) (RGBCOMPSCALE(col, 5, 0x07, s) + RGBCOMPSCALE(col, 2, 0x07, s) + RGBCOMPSCALE(col, 0, 0x03, s))
//#define RGB322SCALE(col, s) (col)
// Storage for post-transform vertices / texcoords / triangles
static int32_t num_vertices_total = 0;
static transformed_vertex_t* transformed_vertices = 0;
static int32_t num_faces_total = 0;
static triangle_t* sorted_triangles = 0;
// Triangle drawer
static inline void rasterize_triangle(uint8_t* image, transformed_triangle_t* tri, uint8_t* shadetex) {
// Local vertex sorting
transformed_vertex_t upperVertex;
transformed_vertex_t centerVertex;
transformed_vertex_t lowerVertex;
if(tri->v[0].p.y < tri->v[1].p.y) {
upperVertex = tri->v[0];
lowerVertex = tri->v[1];
}
else {
upperVertex = tri->v[1];
lowerVertex = tri->v[0];
}
if(tri->v[2].p.y < upperVertex.p.y) {
centerVertex = upperVertex;
upperVertex = tri->v[2];
}
else {
if(tri->v[2].p.y > lowerVertex.p.y) {
centerVertex = lowerVertex;
lowerVertex = tri->v[2];
}
else {
centerVertex = tri->v[2];
}
}
// Scanline counters
int32_t scanline;
int32_t scanlineMax;
// Left / right x and deltas
int32_t leftX;
int32_t leftXd;
int32_t rightX;
int32_t rightXd;
// Left texcoords and texcoord delta
int32_t leftU;
int32_t leftV;
int32_t leftUd;
int32_t leftVd;
// Texcoords and texcoord x deltas
int32_t U;
int32_t V;
int32_t UdX;
int32_t VdX;
// Calculate y differences
int32_t upperDiff = upperVertex.p.y - centerVertex.p.y;
int32_t lowerDiff = upperVertex.p.y - lowerVertex.p.y;
// Deltas
int32_t upperCenter;
int32_t upperLower;
// Guard against special case A: No triangle
if(lowerDiff == 0 && upperDiff == 0) {
return;
}
// Calculate whole-triangle deltas
int32_t temp = idiv(centerVertex.p.y - upperVertex.p.y,lowerVertex.p.y - upperVertex.p.y);
int32_t width = imul(temp, (lowerVertex.p.x - upperVertex.p.x)) + (upperVertex.p.x - centerVertex.p.x);
if(width == 0) {
return;
}
UdX = idiv(imul(temp, lowerVertex.uw - upperVertex.uw) + upperVertex.uw - centerVertex.uw, width);
VdX = idiv(imul(temp, lowerVertex.vw - upperVertex.vw) + upperVertex.vw - centerVertex.vw, width);
// Guard against special case B: Flat upper edge
if(upperDiff == 0 ) {
if(upperVertex.p.x < centerVertex.p.x) {
leftX = upperVertex.p.x;
leftU = upperVertex.uw;
leftV = upperVertex.vw;
rightX = centerVertex.p.x;
leftXd = idiv(upperVertex.p.x - lowerVertex.p.x, lowerDiff);
rightXd = idiv(centerVertex.p.x - lowerVertex.p.x, lowerDiff);
}
else {
leftX = centerVertex.p.x;
leftU = centerVertex.uw;
leftV = centerVertex.vw;
rightX = upperVertex.p.x;
leftXd = idiv(centerVertex.p.x - lowerVertex.p.x, lowerDiff);
rightXd = idiv(upperVertex.p.x - lowerVertex.p.x, lowerDiff);
}
leftUd = idiv(leftU - lowerVertex.uw, lowerDiff);
leftVd = idiv(leftV - lowerVertex.vw, lowerDiff);
goto lower_half_render;
}
// Calculate upper triangle half deltas
upperCenter = idiv(upperVertex.p.x - centerVertex.p.x, upperDiff);
upperLower = idiv(upperVertex.p.x - lowerVertex.p.x, lowerDiff);
// Upper triangle half
leftX = rightX = upperVertex.p.x;
leftU = upperVertex.uw;
leftV = upperVertex.vw;
if(upperCenter < upperLower) {
leftXd = upperCenter;
rightXd = upperLower;
leftUd = idiv(leftU - centerVertex.uw, upperDiff);
leftVd = idiv(leftV - centerVertex.vw, upperDiff);
}
else {
leftXd = upperLower;
rightXd = upperCenter;
leftUd = idiv(leftU - lowerVertex.uw, lowerDiff);
leftVd = idiv(leftV - lowerVertex.vw, lowerDiff);
}
U = leftU;
V = leftV;
scanlineMax = imin(FIXED_INT_ROUND(centerVertex.p.y), SCREEN_HEIGHT - 1);
for(scanline = FIXED_INT_ROUND(upperVertex.p.y); scanline < scanlineMax; scanline++ ) {
if(scanline >= 0) {
int32_t xMax = imin(FIXED_INT_ROUND(rightX), SCREEN_WIDTH - 1);
if(xMax >= 0) {
int32_t offset = scanline * SCREEN_WIDTH;
int32_t x = FIXED_INT_ROUND(leftX);
while(x <= -1) {
U += UdX;
V += VdX;
x++;
}
while(x <= xMax) {
image[x+offset] = RGB322SCALE(shadetex[TEX_TRANSFORM(U, V)], tri->shade);
x++;
U += UdX;
V += VdX;
}
}
}
leftX += leftXd;
rightX += rightXd;
leftU += leftUd;
leftV += leftVd;
U = leftU;
V = leftV;
}
// Guard against special case C: flat lower edge
int32_t centerDiff = centerVertex.p.y - lowerVertex.p.y;
if(centerDiff == 0) {
return;
}
// Calculate lower triangle half deltas
if(upperCenter < upperLower) {
leftX = centerVertex.p.x;
leftXd = idiv(centerVertex.p.x - lowerVertex.p.x, centerDiff);
leftU = centerVertex.uw;
leftV = centerVertex.vw;
leftUd = idiv(leftU - lowerVertex.uw, centerDiff);
leftVd = idiv(leftV - lowerVertex.vw, centerDiff);
}
else {
rightX = centerVertex.p.x;
rightXd = idiv(centerVertex.p.x - lowerVertex.p.x, centerDiff);
}
lower_half_render:
// Lower triangle half
scanlineMax = imin(FIXED_INT_ROUND(lowerVertex.p.y), SCREEN_HEIGHT - 1);
U = leftU;
V = leftV;
for(scanline = FIXED_INT_ROUND(centerVertex.p.y); scanline < scanlineMax; scanline++ ) {
if(scanline >= 0) {
int32_t xMax = imin(FIXED_INT_ROUND(rightX), SCREEN_WIDTH - 1);
if(xMax >= 0) {
int32_t offset = scanline * SCREEN_WIDTH;
int32_t x = FIXED_INT_ROUND(leftX);
while(x <= -1) {
U += UdX;
V += VdX;
x++;
}
while(x <= xMax) {
image[x+offset] = RGB322SCALE(shadetex[TEX_TRANSFORM(U, V)], tri->shade);
x++;
U += UdX;
V += VdX;
}
}
}
leftX += leftXd;
rightX += rightXd;
leftU += leftUd;
U = leftU;
leftV += leftVd;
V = leftV;
}
}
// Depth sorting comparator for comparing by average (sum) depth
static int triAvgDepthCompare(const void *p1, const void *p2) {
triangle_t* t1 = (triangle_t*)p1;
triangle_t* t2 = (triangle_t*)p2;
return(
transformed_vertices[t2->v[0]].p.z +
transformed_vertices[t2->v[1]].p.z +
transformed_vertices[t2->v[2]].p.z -
transformed_vertices[t1->v[0]].p.z -
transformed_vertices[t1->v[1]].p.z -
transformed_vertices[t1->v[2]].p.z
);
}
// Depth sorting comparator for comparing by miminal depth
static int triClosestDepthCompare(const void *p1, const void *p2) {
triangle_t* t1 = (triangle_t*)p1;
triangle_t* t2 = (triangle_t*)p2;
int d1 = imin(imin(
transformed_vertices[t1->v[0]].p.z,
transformed_vertices[t1->v[1]].p.z
),
transformed_vertices[t1->v[2]].p.z
);
int d2 = imin(imin(
transformed_vertices[t2->v[0]].p.z,
transformed_vertices[t2->v[1]].p.z
),
transformed_vertices[t2->v[2]].p.z
);
return(d1 - d2);
}
// Set up storage for geometry and copy face data
void prepare_geometry_storage(model_t* models, int32_t num_models) {
// Count vertices / faces
int32_t vert_count = 0;
int32_t face_count = 0;
for(int32_t m = 0; m < num_models; m++) {
vert_count += models[m].num_vertices;
face_count += models[m].num_faces;
}
// (Re)alloc storage
if(vert_count > num_vertices_total || transformed_vertices == 0) {
num_vertices_total = vert_count;
transformed_vertices = (transformed_vertex_t*)realloc(transformed_vertices, sizeof(transformed_vertex_t) * num_vertices_total);
}
if (face_count > num_faces_total || sorted_triangles == 0) {
num_faces_total = face_count;
sorted_triangles = (triangle_t*)realloc(sorted_triangles, sizeof(triangle_t) * num_faces_total);
}
// Copy face data
int32_t face_offset = 0;
int32_t vert_offset = 0;
for(int32_t m = 0; m < num_models; m++) {
memcpy(&sorted_triangles[face_offset], models[m].faces, sizeof(triangle_t) * models[m].num_faces);
for(int i = face_offset; i < face_offset + models[m].num_faces; i++) {
sorted_triangles[i].model_id = m;
for(int j = 0; j < 3; j++) {
sorted_triangles[i].v[j] += vert_offset;
}
}
vert_offset += models[m].num_vertices;
face_offset += models[m].num_faces;
}
num_faces_total = face_count;
num_vertices_total = vert_count;
}
// Cleanup
void free_geometry_storage() {
free(transformed_vertices);
free(sorted_triangles);
}
// Clip a line against znear
ivec4_t clip_line(ivec4_t a, ivec4_t b) {
int32_t dist = idiv(a.z, (a.z - b.z));
a.x = a.x + imul(dist, b.x - a.x);
a.y = a.y + imul(dist, b.y - a.y);
a.z = a.z + imul(dist, b.z - a.z);
a.w = a.w + imul(dist, b.w - a.w);
return a;
}
// Set up shading information for a normal textured shaded triangle
// from model info and index
void set_shading(uint8_t* framebuffer, model_t* models, int32_t tri_idx, transformed_triangle_t* tri) {
// Set up tex coords
for(int ver = 0; ver < 3; ver++) {
tri->v[ver].uw = models[sorted_triangles[tri_idx].model_id].texcoords[sorted_triangles[tri_idx].v[ver + 4]].u;
tri->v[ver].vw = models[sorted_triangles[tri_idx].model_id].texcoords[sorted_triangles[tri_idx].v[ver + 4]].v;
}
// Shade (Hemi lighting, per face)
ivec3_t light_dir = ivec3norm(ivec3(FLOAT_FIXED(0.5), FLOAT_FIXED(1.0), FLOAT_FIXED(0.5)));
// TODO rotate
ivec3_t norm = models[sorted_triangles[tri_idx].model_id].normals[sorted_triangles[tri_idx].v[3]];
ivec4_t norm_tranformed = imat4x4transform( models[sorted_triangles[tri_idx].model_id].modelview, ivec4(norm.x, norm.y, norm.z, 0));
ivec3_t norm_proper = ivec3norm(ivec3(norm_tranformed.x, norm_tranformed.y, norm_tranformed.z));
tri->shade = imin(FLOAT_FIXED(1.0), FLOAT_FIXED(0.1) + imax(0, ivec3dot(norm_proper, light_dir)));
}
// Draw a single triangle, view clipping against near/far if need be
void clip_rasterize(uint8_t* framebuffer, model_t* models, int32_t tri_idx, transformed_triangle_t tri, uint8_t* texture_override) {
// Check what needs clipping
uint32_t clip = 0;
// Clip A, B, C are the vertices with clipping verts first
int clip_a = -1;
int clip_b = -1;
int clip_c = -1;
for(int ver = 0; ver < 3; ver++) {
clip += tri.v[ver].clip;
// Vertex clips
if(tri.v[ver].clip == 1) {
if(clip_a == -1) {
clip_a = ver;
}
else {
clip_b = ver;
}
}
else {
if(clip_c == -1) {
clip_c = ver;
}
else {
clip_b = ver;
}
}
}
// All vertices clip
if(clip + ((clip & 0xFF) << 8) >= 0x300) {
return;
}
clip = clip & 0xFF;
// One vertex out -> quad, so copy tri
if(clip == 1) {
ivec4_t transform_pos = clip_line(tri.v[clip_a].cp, tri.v[clip_b].cp);
if(transform_pos.w == 0) {
return;
}
tri.v[clip_a].p = ivec3(
VIEWPORT(transform_pos.x, transform_pos.w, SCREEN_WIDTH),
VIEWPORT(transform_pos.y, transform_pos.w, SCREEN_HEIGHT),
transform_pos.z
);
// Additional draw for the bonus triangle
if(texture_override == 0) {
set_shading(framebuffer, models, tri_idx, &tri);
rasterize_triangle(framebuffer, &tri, sorted_triangles[tri_idx].texture);
}
else {
rasterize_triangle(framebuffer, &tri, texture_override);
}
// Set up final triangle
tri.v[clip_b] = tri.v[clip_c];
transform_pos = clip_line(tri.v[clip_a].cp, tri.v[clip_c].cp);
if(transform_pos.w == 0) {
return;
}
tri.v[clip_c].p = ivec3(
VIEWPORT(transform_pos.x, transform_pos.w, SCREEN_WIDTH),
VIEWPORT(transform_pos.y, transform_pos.w, SCREEN_HEIGHT),
transform_pos.z
);
}
// Two vertices out -> tri again
if (clip == 2) {
ivec4_t transform_pos = clip_line(tri.v[clip_a].cp, tri.v[clip_c].cp);
transform_pos = clip_line(tri.v[clip_a].cp, tri.v[clip_c].cp);
if(transform_pos.w == 0) {
return;
}
tri.v[clip_a].p = ivec3(
VIEWPORT(transform_pos.x, transform_pos.w, SCREEN_WIDTH),
VIEWPORT(transform_pos.y, transform_pos.w, SCREEN_HEIGHT),
transform_pos.z
);
transform_pos = clip_line(tri.v[clip_b].cp, tri.v[clip_c].cp);
if(transform_pos.w == 0) {
return;
}
tri.v[clip_b].p = ivec3(
VIEWPORT(transform_pos.x, transform_pos.w, SCREEN_WIDTH),
VIEWPORT(transform_pos.y, transform_pos.w, SCREEN_HEIGHT),
transform_pos.z
);
}
if(texture_override == 0) {
set_shading(framebuffer, models, tri_idx, &tri);
rasterize_triangle(framebuffer, &tri, sorted_triangles[tri_idx].texture);
}
else {
rasterize_triangle(framebuffer, &tri, texture_override);
}
}
// Draw a xz-plane
void draw_floor(uint8_t* framebuffer, imat4x4_t camera, imat4x4_t projection, uint8_t* texture, int32_t height) {
imat4x4_t mvp = imat4x4mul(projection, camera);
// Figure out how far above the plane we are so we can clip agressively
int harsh_clip = 1;
ivec4_t pos = imat4x4transform(camera, ivec4(0, height, 0, INT_FIXED(1)));
if(iabs(pos.y) > INT_FIXED(20)) {
harsh_clip = 3;
}
for(int x = -32; x <= 32; x++) {
for(int z = -32; z <= 32; z++) {
// Skip if outside arena
if(x * x + z * z > 32 * 32) {
continue;
}
// Floor triangle 1
transformed_triangle_t floor_tri;
floor_tri.shade = INT_FIXED(1);
floor_tri.v[0].cp = imat4x4transform(mvp, ivec4(INT_FIXED(8 * x), height, INT_FIXED(8 * z), INT_FIXED(1)));
floor_tri.v[2].cp = imat4x4transform(mvp, ivec4(INT_FIXED(8 * (x + 1)), height, INT_FIXED(8 * z), INT_FIXED(1)));
floor_tri.v[1].cp = imat4x4transform(mvp, ivec4(INT_FIXED(8 * x), height, INT_FIXED(8 * (z + 1)), INT_FIXED(1)));
floor_tri.v[0].uw = INT_FIXED(0);
floor_tri.v[0].vw = INT_FIXED(0);
floor_tri.v[1].uw = INT_FIXED(1);
floor_tri.v[1].vw = INT_FIXED(0);
floor_tri.v[2].uw = INT_FIXED(0);
floor_tri.v[2].vw = INT_FIXED(1);
// Clip?
for(int i = 0; i < 3; i++) {
if(floor_tri.v[i].cp.z <= 0) {
floor_tri.v[i].clip = harsh_clip;
continue;
}
else {
if(floor_tri.v[i].cp.z >= floor_tri.v[i].cp.w) {
floor_tri.v[i].clip = 3;
continue;
}
}
// xy clip?
if(
floor_tri.v[i].cp.x >= floor_tri.v[i].cp.w ||
floor_tri.v[i].cp.y >= floor_tri.v[i].cp.w ||
floor_tri.v[i].cp.x <= -floor_tri.v[i].cp.w ||
floor_tri.v[i].cp.y <= -floor_tri.v[i].cp.w
) {
floor_tri.v[i].clip = 0x100;
}
floor_tri.v[i].p = ivec3(
VIEWPORT(floor_tri.v[i].cp.x, floor_tri.v[i].cp.w, SCREEN_WIDTH),
VIEWPORT(floor_tri.v[i].cp.y, floor_tri.v[i].cp.w, SCREEN_HEIGHT),
floor_tri.v[i].cp.z
);
floor_tri.v[i].clip = 0;
}
// Pass to rasterizer
clip_rasterize(framebuffer, 0, 0, floor_tri, texture);
// Floor triangle 2
floor_tri.shade = INT_FIXED(1);
floor_tri.v[0].cp = imat4x4transform(mvp, ivec4(INT_FIXED(8 * (x + 1)), height, INT_FIXED(8 * (z + 1)), INT_FIXED(1)));
floor_tri.v[2].cp = imat4x4transform(mvp, ivec4(INT_FIXED(8 * (x + 1)), height, INT_FIXED(8 * z), INT_FIXED(1)));
floor_tri.v[1].cp = imat4x4transform(mvp, ivec4(INT_FIXED(8 * x), height, INT_FIXED(8 * (z + 1)), INT_FIXED(1)));
floor_tri.v[0].uw = INT_FIXED(1);
floor_tri.v[0].vw = INT_FIXED(1);
floor_tri.v[1].uw = INT_FIXED(1);
floor_tri.v[1].vw = INT_FIXED(0);
floor_tri.v[2].uw = INT_FIXED(0);
floor_tri.v[2].vw = INT_FIXED(1);
// Clip?
for(int i = 0; i < 3; i++) {
if(floor_tri.v[i].cp.z <= 0) {
floor_tri.v[i].clip = harsh_clip;
continue;
}
else {
if(floor_tri.v[i].cp.z >= floor_tri.v[i].cp.w) {
floor_tri.v[i].clip = 3;
continue;
}
}
floor_tri.v[i].p = ivec3(
VIEWPORT(floor_tri.v[i].cp.x, floor_tri.v[i].cp.w, SCREEN_WIDTH),
VIEWPORT(floor_tri.v[i].cp.y, floor_tri.v[i].cp.w, SCREEN_HEIGHT),
floor_tri.v[i].cp.z
);
floor_tri.v[i].clip = 0;
}
// Pass to rasterizer
clip_rasterize(framebuffer, 0, 0, floor_tri, texture);
}
}
}
// Actual model rasterizer. Prepare model storage before rendering (whenever scene changes)
void rasterize(uint8_t* framebuffer, model_t* models, int32_t num_models, imat4x4_t camera, imat4x4_t projection, uint8_t* floor_tex, uint8_t sky_color) {
int32_t vert_offset = 0;
for(int32_t m = 0; m < num_models; m++) {
// Mvp matrix from camera, mv and p
imat4x4_t mvp = imat4x4mul(camera, models[m].modelview);
mvp = imat4x4mul(projection, mvp);
// Transform all vertices
shade_vertex_t transform_vertex;
for(int32_t i = 0; i < models[m].num_vertices; i++) {
transform_vertex.p = imat4x4transform(mvp, ivec4(models[m].vertices[i].x, models[m].vertices[i].y, models[m].vertices[i].z, INT_FIXED(1)));
transformed_vertices[i + vert_offset].cp = transform_vertex.p;
transformed_vertices[i + vert_offset].clip = 0;
// Near clip?
if(transform_vertex.p.z <= 0) {
transformed_vertices[i + vert_offset].clip = 1;
continue;
}
else {
// Far clip?
if(transform_vertex.p.z >= transform_vertex.p.w) {
transformed_vertices[i + vert_offset].clip = 3; // Far clip is THREE TIMES as bad as near clip
continue;
}
// xy clip?
if(
transform_vertex.p.x >= transform_vertex.p.w ||
transform_vertex.p.y >= transform_vertex.p.w ||
transform_vertex.p.x <= -transform_vertex.p.w ||
transform_vertex.p.y <= -transform_vertex.p.w
) {
transformed_vertices[i + vert_offset].clip = 0x100;
}
// No clipping? Perspective divide and viewport transform
transformed_vertices[i + vert_offset].p = ivec3(
VIEWPORT(transform_vertex.p.x, transform_vertex.p.w, SCREEN_WIDTH),
VIEWPORT(transform_vertex.p.y, transform_vertex.p.w, SCREEN_HEIGHT),
transform_vertex.p.z
);
}
}
vert_offset += models[m].num_vertices;
}
// Depth sort
qsort(sorted_triangles, num_faces_total, sizeof(triangle_t), &triAvgDepthCompare);
// Clear screen
memset(framebuffer, sky_color, SCREEN_HEIGHT * SCREEN_WIDTH);
// Floor / ceiling
if(floor_tex != 0) {
draw_floor(framebuffer, camera, projection, floor_tex, INT_FIXED(0));
draw_floor(framebuffer, camera, projection, floor_tex, INT_FIXED(200));
}
// Draw border
for(int i = 0; i < 20; i++) {
ivec4_t dot;
imat4x4_t mvp = imat4x4mul(projection, camera);
for(int angle = 0; angle < INT_FIXED(1) - FLOAT_FIXED(0.0125 / 2.0); angle += FLOAT_FIXED(0.0125)) {
dot = ivec4(isin(angle) << 8, INT_FIXED(i * 10), icos(angle) << 8, INT_FIXED(1));
dot = imat4x4transform(mvp, dot);
// Near clip
if(dot.z > 0) {
int32_t dot_x = FIXED_INT_ROUND(VIEWPORT(dot.x, dot.w, SCREEN_WIDTH));
int32_t dot_y = FIXED_INT_ROUND(VIEWPORT(dot.y, dot.w, SCREEN_HEIGHT));
if(dot_x >= 0 && dot_x < SCREEN_WIDTH && dot_y >= 0 && dot_y < SCREEN_HEIGHT) {
framebuffer[dot_x + dot_y * SCREEN_WIDTH] = 0xFF;
}
}
}
}
// Rasterize triangle-order
transformed_triangle_t tri;
for(int32_t i = 0; i < num_faces_total; i++ ) {
// Inefficient, but urgh too lazy to rewrite: skip triangle if model inactive
if(models[sorted_triangles[i].model_id].draw == 0) {
continue;
}
// Set up triangle
for(int ver = 0; ver < 3; ver++) {
tri.v[ver] = transformed_vertices[sorted_triangles[i].v[ver]];
}
// Cull backfaces
if(imul(tri.v[1].p.x - tri.v[0].p.x, tri.v[2].p.y - tri.v[0].p.y) -
imul(tri.v[2].p.x - tri.v[0].p.x, tri.v[1].p.y - tri.v[0].p.y) < 0) {
continue;
}
clip_rasterize(framebuffer, models, i, tri, 0);
}
/*
// Draw a little RGB332 swatch
int r = 0;
int g = 0;
int b = 0;
for(int y = 0; y < 16; y++) {
for(int x = 0; x < 16; x++) {
framebuffer[x + y * SCREEN_WIDTH] = (r << 5) | (g << 2) | b;
r += 1;
if(r == 0x8) {
r = 0;
g++;
}
if(g == 0x8) {
g = 0;
b++;
}
}
}*/
}