forked from Vogtinator/nGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl.cpp
926 lines (746 loc) · 23.7 KB
/
gl.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
#include <ctime>
#include <utility>
#include <algorithm>
#ifdef _TINSPIRE
#include <libndls.h>
#else
#include <SDL/SDL.h>
#include <assert.h>
#include <signal.h>
static SDL_Surface *scr;
#endif
#include "gl.h"
#include "fastmath.h"
#define M(m, y, x) (m.data[y][x])
#define P(m, y, x) (m->data[y][x])
MATRIX *transformation;
static COLOR color;
static GLFix u, v;
static COLOR *screen;
static uint16_t *z_buffer;
static GLFix near_plane = 256;
static const TEXTURE *texture;
static unsigned int vertices_count = 0;
static VERTEX vertices[4];
static GLDrawMode draw_mode = GL_TRIANGLES;
static bool is_monochrome;
static COLOR *screen_inverted; //For monochrome calcs
#ifdef FPS_COUNTER
volatile unsigned int fps;
#endif
static int matrix_stack_left = MATRIX_STACK_SIZE;
void nglInit()
{
init_fastmath();
transformation = new MATRIX[MATRIX_STACK_SIZE];
//C++ <3
z_buffer = new std::remove_reference<decltype(*z_buffer)>::type[SCREEN_WIDTH*SCREEN_HEIGHT];
glLoadIdentity();
color = colorRGB(0, 0, 0); //Black
u = v = 0;
texture = nullptr;
vertices_count = 0;
draw_mode = GL_TRIANGLES;
#ifdef _TINSPIRE
is_monochrome = lcd_type() == SCR_320x240_4;
if(is_monochrome)
{
screen_inverted = new COLOR[SCREEN_WIDTH*SCREEN_HEIGHT];
lcd_init(SCR_320x240_16);
}
else
lcd_init(SCR_320x240_565);
#else
SDL_Init(SDL_INIT_VIDEO);
scr = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, SDL_SWSURFACE);
signal(SIGINT, SIG_DFL);
assert(scr);
#endif
matrix_stack_left = MATRIX_STACK_SIZE;
}
void nglUninit()
{
uninit_fastmath();
delete[] transformation;
delete[] z_buffer;
delete[] screen_inverted;
#ifdef _TINSPIRE
lcd_init(SCR_TYPE_INVALID);
#else
//TODO
#endif
}
void nglMultMatMat(MATRIX *mat1, MATRIX *mat2)
{
GLFix a00 = P(mat1, 0, 0), a01 = P(mat1, 0, 1), a02 = P(mat1, 0, 2), a03 = P(mat1, 0, 3);
GLFix a10 = P(mat1, 1, 0), a11 = P(mat1, 1, 1), a12 = P(mat1, 1, 2), a13 = P(mat1, 1, 3);
GLFix a20 = P(mat1, 2, 0), a21 = P(mat1, 2, 1), a22 = P(mat1, 2, 2), a23 = P(mat1, 2, 3);
GLFix a30 = P(mat1, 3, 0), a31 = P(mat1, 3, 1), a32 = P(mat1, 3, 2), a33 = P(mat1, 3, 3);
GLFix b00 = P(mat2, 0, 0), b01 = P(mat2, 0, 1), b02 = P(mat2, 0, 2), b03 = P(mat2, 0, 3);
GLFix b10 = P(mat2, 1, 0), b11 = P(mat2, 1, 1), b12 = P(mat2, 1, 2), b13 = P(mat2, 1, 3);
GLFix b20 = P(mat2, 2, 0), b21 = P(mat2, 2, 1), b22 = P(mat2, 2, 2), b23 = P(mat2, 2, 3);
GLFix b30 = P(mat2, 3, 0), b31 = P(mat2, 3, 1), b32 = P(mat2, 3, 2), b33 = P(mat2, 3, 3);
P(mat1, 0, 0) = a00*b00 + a01*b10 + a02*b20 + a03*b30;
P(mat1, 0, 1) = a00*b01 + a01*b11 + a02*b21 + a03*b31;
P(mat1, 0, 2) = a00*b02 + a01*b12 + a02*b22 + a03*b32;
P(mat1, 0, 3) = a00*b03 + a01*b13 + a02*b23 + a03*b33;
P(mat1, 1, 0) = a10*b00 + a11*b10 + a12*b20 + a13*b30;
P(mat1, 1, 1) = a10*b01 + a11*b11 + a12*b21 + a13*b31;
P(mat1, 1, 2) = a10*b02 + a11*b12 + a12*b22 + a13*b32;
P(mat1, 1, 3) = a10*b03 + a11*b13 + a12*b23 + a13*b33;
P(mat1, 2, 0) = a20*b00 + a21*b10 + a22*b20 + a23*b30;
P(mat1, 2, 1) = a20*b01 + a21*b11 + a22*b21 + a23*b31;
P(mat1, 2, 2) = a20*b02 + a21*b12 + a22*b22 + a23*b32;
P(mat1, 2, 3) = a20*b03 + a21*b13 + a22*b23 + a23*b33;
P(mat1, 3, 0) = a30*b00 + a31*b10 + a32*b20 + a33*b30;
P(mat1, 3, 1) = a30*b01 + a31*b11 + a32*b21 + a33*b31;
P(mat1, 3, 2) = a30*b02 + a31*b12 + a32*b22 + a33*b32;
P(mat1, 3, 3) = a30*b03 + a31*b13 + a32*b23 + a33*b33;
}
void nglMultMatVectRes(const MATRIX *mat1, const VERTEX *vect, VERTEX *res)
{
GLFix x = vect->x, y = vect->y, z = vect->z;
res->x = P(mat1, 0, 0)*x + P(mat1, 0, 1)*y + P(mat1, 0, 2)*z + P(mat1, 0, 3);
res->y = P(mat1, 1, 0)*x + P(mat1, 1, 1)*y + P(mat1, 1, 2)*z + P(mat1, 1, 3);
res->z = P(mat1, 2, 0)*x + P(mat1, 2, 1)*y + P(mat1, 2, 2)*z + P(mat1, 2, 3);
}
void nglMultMatVectRes(const MATRIX *mat1, const VECTOR3 *vect, VECTOR3 *res)
{
GLFix x = vect->x, y = vect->y, z = vect->z;
res->x = P(mat1, 0, 0)*x + P(mat1, 0, 1)*y + P(mat1, 0, 2)*z + P(mat1, 0, 3);
res->y = P(mat1, 1, 0)*x + P(mat1, 1, 1)*y + P(mat1, 1, 2)*z + P(mat1, 1, 3);
res->z = P(mat1, 2, 0)*x + P(mat1, 2, 1)*y + P(mat1, 2, 2)*z + P(mat1, 2, 3);
}
void nglPerspective(VERTEX *v)
{
#ifdef BETTER_PERSPECTIVE
float new_z = v->z;
decltype(new_z) new_x = v->x, new_y = v->y;
decltype(new_z) div = decltype(new_z)(near_plane)/new_z;
new_x *= div;
new_y *= div;
v->x = new_x;
v->y = new_y;
#else
auto div = Fix<12, int32_t>(near_plane)/v->z.toInteger<int>();
//Round to integers, as we don't lose the topmost bits with integer multiplication
v->x = div * v->x.toInteger<int>();
v->y = div * v->y.toInteger<int>();
#endif
// (0/0) is in the center of the screen
v->x += SCREEN_WIDTH/2;
v->y += SCREEN_HEIGHT/2;
v->y = GLFix(SCREEN_HEIGHT - 1) - v->y;
//TODO: Move this somewhere else
if(!texture)
return;
if(v->u > GLFix(texture->width))
{
printf("Warning: Texture coord out of bounds!\n");
v->u = texture->height;
}
else if(v->u < GLFix(0))
{
printf("Warning: Texture coord out of bounds!\n");
v->u = 0;
}
if(v->v > GLFix(texture->height))
{
printf("Warning: Texture coord out of bounds!\n");
v->v = texture->height;
}
else if(v->v < GLFix(0))
{
printf("Warning: Texture coord out of bounds!\n");
v->v = 0;
}
}
void nglPerspective(VECTOR3 *v)
{
#ifdef BETTER_PERSPECTIVE
float new_z = v->z;
decltype(new_z) new_x = v->x, new_y = v->y;
decltype(new_z) div = decltype(new_z)(near_plane)/new_z;
new_x *= div;
new_y *= div;
v->x = new_x;
v->y = new_y;
#else
auto div = Fix<12, int32_t>(near_plane)/v->z.toInteger<int>();
//Round to integers, as we don't lose the topmost bits with integer multiplication
v->x = div * v->x.toInteger<int>();
v->y = div * v->y.toInteger<int>();
#endif
// (0/0) is in the center of the screen
v->x += SCREEN_WIDTH/2;
v->y += SCREEN_HEIGHT/2;
v->y = GLFix(SCREEN_HEIGHT - 1) - v->y;
}
void nglSetBuffer(COLOR *screenBuf)
{
screen = screenBuf;
}
void nglDisplay()
{
#ifdef _TINSPIRE
if(is_monochrome)
{
//Flip everything, as 0xFFFF is white on CX, but black on classic
COLOR *ptr = screen + SCREEN_HEIGHT*SCREEN_WIDTH, *ptr_inv = screen_inverted + SCREEN_HEIGHT*SCREEN_WIDTH;
while(--ptr >= screen)
*--ptr_inv = ~*ptr;
lcd_blit(screen_inverted, SCR_320x240_16);
}
else
lcd_blit(screen, SCR_320x240_565);
#else
SDL_LockSurface(scr);
std::copy(screen, screen + SCREEN_HEIGHT*SCREEN_WIDTH, reinterpret_cast<COLOR*>(scr->pixels));
SDL_UnlockSurface(scr);
SDL_UpdateRect(scr, 0, 0, 0, 0);
#endif
#ifdef FPS_COUNTER
static unsigned int frames = 0;
++frames;
static time_t last = 0;
time_t now = time(nullptr);
if(now != last)
{
fps = frames;
printf("FPS: %u\n", frames);
last = now;
frames = 0;
}
#endif
}
void nglRotateX(const GLFix a)
{
MATRIX rot;
const GLFix sina = fast_sin(a), cosa = fast_cos(a);
M(rot, 0, 0) = 1;
M(rot, 1, 1) = cosa;
M(rot, 1, 2) = -sina;
M(rot, 2, 1) = sina;
M(rot, 2, 2) = cosa;
M(rot, 3, 3) = 1;
nglMultMatMat(transformation, &rot);
}
void nglRotateY(const GLFix a)
{
MATRIX rot;
const GLFix sina = fast_sin(a), cosa = fast_cos(a);
M(rot, 0, 0) = cosa;
M(rot, 0, 2) = sina;
M(rot, 1, 1) = 1;
M(rot, 2, 0) = -sina;
M(rot, 2, 2) = cosa;
M(rot, 3, 3) = 1;
nglMultMatMat(transformation, &rot);
}
void nglRotateZ(const GLFix a)
{
MATRIX rot;
const GLFix sina = fast_sin(a), cosa = fast_cos(a);
M(rot, 0, 0) = cosa;
M(rot, 0, 1) = -sina;
M(rot, 1, 0) = sina;
M(rot, 1, 1) = cosa;
M(rot, 2, 2) = 1;
M(rot, 3, 3) = 1;
nglMultMatMat(transformation, &rot);
}
inline void pixel(const int x, const int y, const GLFix z, const COLOR c)
{
if(x < 0 || y < 0 || x >= SCREEN_WIDTH || y >= SCREEN_HEIGHT)
return;
const int pitch = x + y*SCREEN_WIDTH;
if(z <= GLFix(CLIP_PLANE) || GLFix(z_buffer[pitch]) <= z)
return;
z_buffer[pitch] = z;
screen[pitch] = c;
}
RGB rgbColor(const COLOR c)
{
const GLFix r = (c >> 11) & 0b11111;
const GLFix g = (c >> 5) & 0b111111;
const GLFix b = (c >> 0) & 0b11111;
return {r / GLFix(0b11111), g / GLFix(0b111111), b / GLFix(0b11111)};
}
COLOR colorRGB(const RGB rgb)
{
return colorRGB(rgb.r, rgb.g, rgb.b);
}
COLOR colorRGB(const GLFix r, const GLFix g, const GLFix b)
{
const int r1 = (r * GLFix(0b11111)).round();
const int g1 = (g * GLFix(0b111111)).round();
const int b1 = (b * GLFix(0b11111)).round();
return ((r1 & 0b11111) << 11) | ((g1 & 0b111111) << 5) | (b1 & 0b11111);
}
GLFix nglZBufferAt(const unsigned int x, const unsigned int y)
{
if(x >= SCREEN_WIDTH || y >= SCREEN_HEIGHT)
return 0;
return z_buffer[x + y * SCREEN_WIDTH];
}
//Doesn't interpolate colors even if enabled
void nglDrawLine3D(const VERTEX *v1, const VERTEX *v2)
{
//TODO: Z-Clipping
VERTEX v1_p = *v1, v2_p = *v2;
nglPerspective(&v1_p);
nglPerspective(&v2_p);
const GLFix diff_x = v2_p.x - v1_p.x;
const GLFix dy = (v2_p.y - v1_p.y) / diff_x;
const COLOR c = v1_p.c;
//Height > width? -> Interpolate X
if(dy > GLFix(1) || dy < GLFix(-1))
{
if(v2_p.y < v1_p.y)
std::swap(v1_p, v2_p);
const GLFix diff_y = v2_p.y - v1_p.y;
const GLFix dx = (v2_p.x - v1_p.x) / diff_y;
const GLFix dz = (v2_p.z - v1_p.z) / diff_y;
int end_y = v2_p.y;
if(end_y >= SCREEN_HEIGHT)
end_y = SCREEN_HEIGHT - 1;
for(; v1_p.y <= GLFix(end_y); ++v1_p.y)
{
pixel(v1_p.x, v1_p.y, v1_p.z, c);
v1_p.x += dx;
v1_p.z += dz;
}
}
else
{
if(v2_p.x < v1_p.x)
std::swap(v1_p, v2_p);
const GLFix dz = (v2_p.z - v1_p.z) / diff_x;
int end_x = v2_p.x;
if(end_x >= SCREEN_WIDTH)
end_x = SCREEN_WIDTH - 1;
for(; v1_p.x <= GLFix(end_x); ++v1_p.x)
{
pixel(v1_p.x, v1_p.y, v1_p.z, c);
v1_p.y += dy;
v1_p.z += dz;
}
}
}
//I hate code duplication more than macros and includes
#ifdef TEXTURE_SUPPORT
#define TRANSPARENCY
#include "triangle.inc.h"
#undef TRANSPARENCY
#undef TEXTURE_SUPPORT
#define FORCE_COLOR
#include "triangle.inc.h"
#define TEXTURE_SUPPORT
#undef FORCE_COLOR
#endif
#include "triangle.inc.h"
static void interpolateVertexXLeft(const VERTEX *from, const VERTEX *to, VERTEX *res)
{
GLFix diff = to->x - from->x;
GLFix end = 0;
GLFix t = (end - from->x) / diff;
res->x = end;
res->y = from->y + (to->y - from->y) * t;
res->z = from->z + (to->z - from->z) * t;
#ifdef TEXTURE_SUPPORT
res->u = from->u + (to->u - from->u) * t;
res->u = res->u.wholes();
res->v = from->v + (to->v - from->v) * t;
res->v = res->v.wholes();
#endif
#ifdef INTERPOLATE_COLORS
RGB c_from = rgbColor(from->c);
RGB c_to = rgbColor(to->c);
res->c = colorRGB(c_from.r + (c_to.r - c_from.r) * t, c_from.r + (c_to.r - c_from.r) * t, c_from.r + (c_to.r - c_from.r) * t);
#else
res->c = from->c;
#endif
}
//Left X clipping
void nglDrawTriangleXRightZClipped(const VERTEX *low, const VERTEX *middle, const VERTEX *high)
{
const VERTEX* invisible[3];
const VERTEX* visible[3];
int count_invisible = -1, count_visible = -1;
if(low->x < GLFix(0))
invisible[++count_invisible] = low;
else
visible[++count_visible] = low;
if(middle->x < GLFix(0))
invisible[++count_invisible] = middle;
else
visible[++count_visible] = middle;
if(high->x < GLFix(0))
invisible[++count_invisible] = high;
else
visible[++count_visible] = high;
//Interpolated vertices
VERTEX v1, v2;
switch(count_visible)
{
case -1:
break;
case 0:
interpolateVertexXLeft(invisible[0], visible[0], &v1);
interpolateVertexXLeft(invisible[1], visible[0], &v2);
nglDrawTriangleXZClipped(visible[0], &v1, &v2);
break;
case 1:
interpolateVertexXLeft(invisible[0], visible[0], &v1);
interpolateVertexXLeft(invisible[0], visible[1], &v2);
nglDrawTriangleXZClipped(visible[0], visible[1], &v1);
nglDrawTriangleXZClipped(visible[1], &v1, &v2);
break;
case 2:
nglDrawTriangleXZClipped(visible[0], visible[1], visible[2]);
break;
}
}
static void interpolateVertexXRight(const VERTEX *from, const VERTEX *to, VERTEX *res)
{
GLFix diff = to->x - from->x;
GLFix end = (SCREEN_WIDTH - 1);
GLFix t = (end - from->x) / diff;
res->x = end;
res->y = from->y + (to->y - from->y) * t;
res->z = from->z + (to->z - from->z) * t;
#ifdef TEXTURE_SUPPORT
res->u = from->u + (to->u - from->u) * t;
res->u = res->u.wholes();
res->v = from->v + (to->v - from->v) * t;
res->v = res->v.wholes();
#endif
#ifdef INTERPOLATE_COLORS
RGB c_from = rgbColor(from->c);
RGB c_to = rgbColor(to->c);
res->c = colorRGB(c_from.r + (c_to.r - c_from.r) * t, c_from.r + (c_to.r - c_from.r) * t, c_from.r + (c_to.r - c_from.r) * t);
#else
res->c = from->c;
#endif
}
//Right X clipping
void nglDrawTriangleZClipped(const VERTEX *low, const VERTEX *middle, const VERTEX *high)
{
//If not on screen, skip
if((low->x < GLFix(0) && middle->x < GLFix(0) && high->x < GLFix(0))
|| (low->x >= GLFix(SCREEN_WIDTH) && middle->x >= GLFix(SCREEN_WIDTH) && high->x >= GLFix(SCREEN_WIDTH))
|| (low->y < GLFix(0) && middle->y < GLFix(0) && high->y < GLFix(0))
|| (low->y >= GLFix(SCREEN_HEIGHT) && middle->y >= GLFix(SCREEN_HEIGHT) && high->y >= GLFix(SCREEN_HEIGHT)))
return;
const VERTEX* invisible[3];
const VERTEX* visible[3];
int count_invisible = -1, count_visible = -1;
if(low->x > GLFix(SCREEN_WIDTH-1))
invisible[++count_invisible] = low;
else
visible[++count_visible] = low;
if(middle->x > GLFix(SCREEN_WIDTH-1))
invisible[++count_invisible] = middle;
else
visible[++count_visible] = middle;
if(high->x > GLFix(SCREEN_WIDTH-1))
invisible[++count_invisible] = high;
else
visible[++count_visible] = high;
//Interpolated vertices
VERTEX v1, v2;
switch(count_visible)
{
case -1:
break;
case 0:
interpolateVertexXRight(invisible[0], visible[0], &v1);
interpolateVertexXRight(invisible[1], visible[0], &v2);
nglDrawTriangleXRightZClipped(visible[0], &v1, &v2);
break;
case 1:
interpolateVertexXRight(invisible[0], visible[0], &v1);
interpolateVertexXRight(invisible[0], visible[1], &v2);
nglDrawTriangleXRightZClipped(visible[0], visible[1], &v1);
nglDrawTriangleXRightZClipped(visible[1], &v1, &v2);
break;
case 2:
nglDrawTriangleXRightZClipped(visible[0], visible[1], visible[2]);
break;
}
}
#ifdef Z_CLIPPING
void nglInterpolateVertexZ(const VERTEX *from, const VERTEX *to, VERTEX *res)
{
GLFix diff = to->z - from->z;
GLFix t = (GLFix(CLIP_PLANE) - from->z) / diff;
res->x = from->x + (to->x - from->x) * t;
res->y = from->y + (to->y - from->y) * t;
res->z = CLIP_PLANE;
#ifdef TEXTURE_SUPPORT
res->u = from->u + (to->u - from->u) * t;
res->u = res->u.wholes();
res->v = from->v + (to->v - from->v) * t;
res->v = res->v.wholes();
#endif
#ifdef INTERPOLATE_COLORS
RGB c_from = rgbColor(from->c);
RGB c_to = rgbColor(to->c);
res->c = colorRGB(c_from.r + (c_to.r - c_from.r) * t, c_from.r + (c_to.r - c_from.r) * t, c_from.r + (c_to.r - c_from.r) * t);
#else
res->c = from->c;
#endif
}
#endif
bool nglIsBackface(const VERTEX *v1, const VERTEX *v2, const VERTEX *v3)
{
int x1 = v2->x - v1->x, x2 = v3->x - v1->x;
int y1 = v2->y - v1->y, y2 = v3->y - v1->y;
return x1 * y2 < x2 * y1;
}
bool nglIsBackface(const VECTOR3 *v1, const VECTOR3 *v2, const VECTOR3 *v3)
{
int x1 = v2->x - v1->x, x2 = v3->x - v1->x;
int y1 = v2->y - v1->y, y2 = v3->y - v1->y;
return x1 * y2 < x2 * y1;
}
bool nglDrawTriangle(const VERTEX *low, const VERTEX *middle, const VERTEX *high, bool backface_culling)
{
#ifndef Z_CLIPPING
if(low->z < GLFix(CLIP_PLANE) || middle->z < GLFix(CLIP_PLANE) || high->z < GLFix(CLIP_PLANE))
return true;
VERTEX low_p = *low, middle_p = *middle, high_p = *high;
nglPerspective(&low_p);
nglPerspective(&middle_p);
nglPerspective(&high_p);
if(backface_culling && nglIsBackface(&low_p, &middle_p, &high_p))
return false;
nglDrawTriangleZClipped(&low_p, &middle_p, &high_p);
return true;
#else
if(low->z < GLFix(CLIP_PLANE) && middle->z < GLFix(CLIP_PLANE) && high->z < GLFix(CLIP_PLANE))
return true;
VERTEX invisible[3];
VERTEX visible[3];
int count_invisible = -1, count_visible = -1;
if(low->z < GLFix(CLIP_PLANE))
invisible[++count_invisible] = *low;
else
visible[++count_visible] = *low;
if(middle->z < GLFix(CLIP_PLANE))
invisible[++count_invisible] = *middle;
else
visible[++count_visible] = *middle;
if(high->z < GLFix(CLIP_PLANE))
invisible[++count_invisible] = *high;
else
visible[++count_visible] = *high;
//Interpolated vertices
VERTEX v1, v2;
switch(count_visible)
{
case -1:
return true;
case 0:
nglInterpolateVertexZ(&invisible[0], &visible[0], &v1);
nglInterpolateVertexZ(&invisible[1], &visible[0], &v2);
nglPerspective(&visible[0]);
nglPerspective(&v1);
nglPerspective(&v2);
if(backface_culling && nglIsBackface(&visible[0], &v1, &v2))
return false;
nglDrawTriangleZClipped(&visible[0], &v1, &v2);
return true;
case 1:
nglInterpolateVertexZ(&visible[0], &invisible[0], &v1);
nglInterpolateVertexZ(&visible[1], &invisible[0], &v2);
nglPerspective(&visible[0]);
nglPerspective(&visible[1]);
nglPerspective(&v1);
if(backface_culling && nglIsBackface(&visible[0], &visible[1], &v1))
return false;
nglPerspective(&v2);
nglDrawTriangleZClipped(&visible[0], &visible[1], &v1);
nglDrawTriangleZClipped(&visible[1], &v1, &v2);
return true;
case 2:
nglPerspective(&visible[0]);
nglPerspective(&visible[1]);
nglPerspective(&visible[2]);
if(backface_culling && nglIsBackface(&visible[0], &visible[1], &visible[2]))
return false;
nglDrawTriangleZClipped(&visible[0], &visible[1], &visible[2]);
return true;
default:
return true;
}
#endif
}
void nglSetColor(const COLOR c)
{
color = c;
}
void nglAddVertices(const VERTEX *buffer, unsigned int length)
{
while(length--)
nglAddVertex(buffer++);
}
void nglAddVertex(const VERTEX &vertex)
{
nglAddVertex(&vertex);
}
void nglAddVertex(const VERTEX* vertex)
{
VERTEX *current_vertex = &vertices[vertices_count];
current_vertex->c = vertex->c;
#ifdef TEXTURE_SUPPORT
current_vertex->u = vertex->u;
current_vertex->v = vertex->v;
#endif
nglMultMatVectRes(transformation, vertex, current_vertex);
++vertices_count;
switch(draw_mode)
{
case GL_TRIANGLES:
if(vertices_count != 3)
break;
vertices_count = 0;
#ifdef WIREFRAME_MODE
nglDrawLine3D(&vertices[0], &vertices[1]);
nglDrawLine3D(&vertices[0], &vertices[2]);
nglDrawLine3D(&vertices[2], &vertices[1]);
#else
nglDrawTriangle(&vertices[0], &vertices[1], &vertices[2], !texture || (vertices[0].c & TEXTURE_DRAW_BACKFACE) != TEXTURE_DRAW_BACKFACE);
#endif
break;
case GL_QUADS:
if(vertices_count != 4)
break;
vertices_count = 0;
#ifdef WIREFRAME_MODE
nglDrawLine3D(&vertices[0], &vertices[1]);
nglDrawLine3D(&vertices[1], &vertices[2]);
nglDrawLine3D(&vertices[2], &vertices[3]);
nglDrawLine3D(&vertices[3], &vertices[0]);
#else
if(nglDrawTriangle(&vertices[0], &vertices[1], &vertices[2], !texture || (vertices[0].c & TEXTURE_DRAW_BACKFACE) != TEXTURE_DRAW_BACKFACE))
nglDrawTriangle(&vertices[2], &vertices[3], &vertices[0], false);
#endif
break;
case GL_QUAD_STRIP:
if(vertices_count != 4)
break;
vertices_count = 2;
#ifdef WIREFRAME_MODE
nglDrawLine3D(&vertices[0], &vertices[1]);
nglDrawLine3D(&vertices[1], &vertices[2]);
nglDrawLine3D(&vertices[2], &vertices[3]);
nglDrawLine3D(&vertices[3], &vertices[0]);
#else
if(nglDrawTriangle(&vertices[0], &vertices[1], &vertices[2], !texture || (vertices[0].c & TEXTURE_DRAW_BACKFACE) != TEXTURE_DRAW_BACKFACE))
nglDrawTriangle(&vertices[2], &vertices[3], &vertices[0], false);
#endif
vertices[0] = vertices[2];
vertices[1] = vertices[3];
break;
case GL_LINE_STRIP:
if(vertices_count != 2)
break;
vertices_count = 1;
nglDrawLine3D(&vertices[0], &vertices[1]);
vertices[0] = vertices[1];
break;
}
}
const TEXTURE *nglGetTexture()
{
return texture;
}
void glBindTexture(const TEXTURE *tex)
{
texture = tex;
if(tex && tex->has_transparency && tex->transparent_color != 0)
printf("Bound texture doesn't have black as transparent color!\n");
}
void nglSetNearPlane(const GLFix new_near_plane)
{
near_plane = new_near_plane;
}
GLFix nglGetNearPlane()
{
return near_plane;
}
void glColor3f(const GLFix r, const GLFix g, const GLFix b)
{
color = colorRGB(r, g, b);
}
void glTexCoord2f(const GLFix nu, const GLFix nv)
{
u = nu;
v = nv;
}
void glVertex3f(const GLFix x, const GLFix y, const GLFix z)
{
const VERTEX ver{x, y, z, u, v, color};
nglAddVertex(&ver);
}
void glBegin(GLDrawMode mode)
{
vertices_count = 0;
draw_mode = mode;
}
void glClear(const int buffers)
{
if(buffers & GL_COLOR_BUFFER_BIT)
std::fill(screen, screen + SCREEN_WIDTH*SCREEN_HEIGHT, color);
if(buffers & GL_DEPTH_BUFFER_BIT)
std::fill(z_buffer, z_buffer + SCREEN_WIDTH*SCREEN_HEIGHT, UINT16_MAX);
}
void glLoadIdentity()
{
*transformation = {}; // Copy empty matrix into transformation
P(transformation, 0, 0) = P(transformation, 1, 1) = P(transformation, 2, 2) = P(transformation, 3, 3) = 1;
}
void glTranslatef(const GLFix x, const GLFix y, const GLFix z)
{
MATRIX trans;
M(trans, 0, 0) = M(trans, 1, 1) = M(trans, 2, 2) = M(trans, 3, 3) = 1;
M(trans, 0, 3) = x;
M(trans, 1, 3) = y;
M(trans, 2, 3) = z;
nglMultMatMat(transformation, &trans);
}
void glScale3f(const GLFix x, const GLFix y, const GLFix z)
{
MATRIX scale;
M(scale, 0, 0) = x;
M(scale, 1, 1) = y;
M(scale, 2, 2) = z;
M(scale, 3, 3) = 1;
nglMultMatMat(transformation, &scale);
}
void glPopMatrix()
{
if(matrix_stack_left == MATRIX_STACK_SIZE)
{
printf("Error: No matrix left on the stack anymore!\n");
return;
}
++matrix_stack_left;
--transformation;
}
void glPushMatrix()
{
if(matrix_stack_left == 0)
{
printf("Error: Matrix stack limit reached!\n");
return;
}
matrix_stack_left--;
++transformation;
*transformation = *(transformation - 1);
}
uint16_t* glGetZBuffer()
{
return z_buffer;
}