-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtr_base.cpp
1025 lines (892 loc) · 29.1 KB
/
tr_base.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
"SPHERY VS. SHAPES" by Victor Suarez Rovere and Manuel Suarez
Copyright (C) 2021 Victor Suarez Rovere <[email protected]>
HOW TO PLAY:
Press the left mouse button or the UP KEY to jump.
Score increases when you're on floor, you win when the bar is full.
*/
//#define GOD_MODE
//#define NON_INTERACTIVE
//#define SOFT_SHADOW 1 //2 for smoother border transition
//#define LEVELS
//#define ALTERNATE_UI 3 //level of graphics detail
//ALTERNATE_UI=1 640x480: synths in Arty35T @35.60 MHz, 2199/33280 cells, 0 latency)
//ALTERNATE_UI=2 640x480: synths in Arty35T @27.43 MHz, 3479/33280 cells, 0 latency)
//ALTERNATE_UI=3 640x480: synths in Arty35T @25.74 MHz, 7651/33280 cells, 5 latency)
#define RT_SMALL_UI //enable to reduce raytracing complexity (without RT, 31619(comb only) / 20800 max, with RT ~23702 board=???)
//RT_SMALL_UI 640x480: synths in Arty35T @27.63 MHz, about 27000/33280 cells, 28 latency)
//#define DITHER
//#define ANTIALIAS 6 //default 6, smooth 4
//#define SCREEN_ASPECT 16./9. //or for example 10./9.
#define SCREEN_ASPECT 3./2. //or for example 10./9.
#include "tr.h"
#ifndef SHADER
typedef coord_type hole_t; //TODO: move
#define get_scene() state.scene
#else
scene_t get_scene();
#define hole_t coord_type //TODO: move
#endif
#if defined(ALTERNATE_UI) && ALTERNATE_UI < 2
#undef HEAT_CONSTANT
#endif
#define BLINKY
#ifdef GOD_MODE
//#define NON_INTERACTIVE
#define SCORE_STEP 3
#elif defined(NON_INTERACTIVE)
#define SCORE_STEP 0
#else
#define SCORE_STEP 1
#endif
#ifdef NON_INTERACTIVE
#undef HOLE_BORDER
#undef BLINKY
#undef HEAT_CONSTANT
#define GOD_MODE
#endif
full_state_t state;
hole_t plane_has_hole(hole_t x, hole_t z)
{
hole_t ret = 1.;
x=fixed_shr(x, 4);
z=fixed_shr(z, 5);
int16_t ix = round16(x);
int16_t iz = round16(z);
hole_t fracx = hole_t(ix)-x;
hole_t fracz = hole_t(iz)-z;
uint16_t hx = hash16(ix) >> 5;
uint16_t hz = hash16(iz) >> 5;
if((hx ^ hz) < int16_t(ix+600))
{
hole_t ax = fixed_abs(fracx);
hole_t az = fixed_abs(fracz);
#ifdef RHOMBUS_SIZE
ret = (ax + az) - hole_t(RHOMBUS_SIZE);
#endif
#ifdef LEVELS
#ifndef SHADER //this simplifies automatic conversion to C
if((ix & 0x240)==0x240 || ((ix & ~0x7FF) != 0) )
ret = 0.; //no hole
else
#endif
{
bool fx = (hx & 1) != 0;
bool fz = ((hz>>1) & 1) != 0;
bool hard = (ix & 0x200) != 0;
#ifdef CIRCLE_SIZE
if((((ix & 0xC0) == 0xC0) && (fx ^ fz)))
ret = fracx*fracx + fracz*fracz - CIRCLE_SIZE*CIRCLE_SIZE; //circles
else
#endif
{
#ifdef SQUARE_SIZE
if(hard) //rhombus or squares
ret = fixed_max(ax, az) - hole_t(SQUARE_SIZE);
#else
ret = fixed_max(ax, az) - SQUARE_SIZE;
#endif
}
}
#endif //levels
}
return ret;
}
#ifndef COLOR_DECOMP
inline scene_colors_t scene_colors(IN(scene_t) scene)
{
scene_colors_t r;
r.sphere = scene.sphere.material;
r.plane = scene.plane.material;
r.plane_color1 = scene.plane.color1;
r.plane_color2 = scene.plane.color2;
r.fog = scene.fog;
return r;
}
#else
inline scene_colors_t scene_colors(IN(scene_t) scene)
{
uint2_t channel = scene.current_color_channel;
scene_colors_t r;
if(channel == 0)
{
r.sphere.diffuse_color = scene.sphere.material.diffuse_color.r;
r.sphere.reflect_color = scene.sphere.material.reflect_color.r;
r.plane.diffuse_color = scene.plane.material.diffuse_color.r;
r.plane.reflect_color = scene.plane.material.reflect_color.r;
r.plane_color1 = scene.plane.color1.r;
r.plane_color2 = scene.plane.color2.r;
r.fog = scene.fog.r;
}
else if(channel == 1)
{
r.sphere.diffuse_color = scene.sphere.material.diffuse_color.g;
r.sphere.reflect_color = scene.sphere.material.reflect_color.g;
r.plane.diffuse_color = scene.plane.material.diffuse_color.g;
r.plane.reflect_color = scene.plane.material.reflect_color.g;
r.plane_color1 = scene.plane.color1.g;
r.plane_color2 = scene.plane.color2.g;
r.fog = scene.fog.g;
}
else
{
r.sphere.diffuse_color = scene.sphere.material.diffuse_color.b;
r.sphere.reflect_color = scene.sphere.material.reflect_color.b;
r.plane.diffuse_color = scene.plane.material.diffuse_color.b;
r.plane.reflect_color = scene.plane.material.reflect_color.b;
r.plane_color1 = scene.plane.color1.b;
r.plane_color2 = scene.plane.color2.b;
r.fog = scene.fog.b;
}
return r;
}
#endif //COLOR_DECOMP
#ifndef ALTERNATE_UI
//raytracer math inspired on tinyraytracer https://github.com/ssloy/tinyraytracer
struct point_and_dir
{
vec3 orig, dir;
#ifdef ANTIALIAS
float dist; //FIXME: move appropiately
#endif
};
struct hit_out
{
float dist, borderdist;
point_and_dir hit;
#ifdef ANTIALIAS
float accdist;
#endif
};
#ifdef ANTIALIAS
float calc_accdist(IN(point_and_dir) hitin, IN(hit_out) hitout)
{
float din = hitin.dist + hitout.dist;
float d = float_shift(din, -(ANTIALIAS+6));
return d;
}
#endif
//#warning: this seems to add cells to synth, FIXME: inline and define 'hit_out hitout;' at top
hit_out sphere_hit(bool hit, IN(vec3) center, IN(point_and_dir) hitin, float t, float diff)
{
hit_out hitout;
hitout.dist = hit ? t : RAY_NOINT;
//if(hit) //always calculated to save hardware muxes
{
hitout.hit.orig = hitin.orig + hitin.dir*hitout.dist;
hitout.hit.dir = normalize(hitout.hit.orig - center);
#ifdef ANTIALIAS
hitout.accdist = calc_accdist(hitin, hitout);
#endif
}
hitout.borderdist = diff;
return hitout;
}
hit_out ray_sphere_intersect(IN(vec3) center, IN(point_and_dir) hitin)
{
#ifndef SPHERE_MOTIONBLUR
vec3 rc = hitin.orig - center;
float b = dot(rc, hitin.dir);
float c = dot(rc, rc) - SPHERE_RADIUS*SPHERE_RADIUS;
float diff = b*b - c;
bool nothit = is_negative(diff);
float t = RAY_NOINT;
if(!nothit)
{
t = -(b + sqrt(diff));
nothit = is_negative(t);
if (nothit)
diff = -diff;
}
#else
vec3 rc = hitin.orig - center;
vec3 ro = hitin.orig;
vec3 rd = hitin.dir;
float ve_y(s.yvel*4);
//vec3 ve(0., float(s.yvel), 0); //velocity vector
//motion blur formula from https://www.shadertoy.com/view/MdB3Dw
float A = dot(rc,rd);
float B = dot(rc,rc) - SPHERE_RADIUS*SPHERE_RADIUS;
float C = ve_y*ve_y;
float D = rc.y*ve_y;
float E = rd.y*ve_y;
//float C = dot(ve,ve);
//float D = dot(rc,ve);
//float E = dot(rd,ve);
float aab = A*A - B;
float eec = E*E - C;
float aed = A*E - D;
diff = aed*aed - eec*aab;
if(diff > 0.)
{
float tk = sqrt(diff);
float ta = float_max( 0.0, (aed+tk)/eec );
float tb = float_min( 1.0, (aed-tk)/eec );
float s_ = (tb-ta); //time coverage (factor 2.0 removed)
//diff = s_*SPHERE_RADIUS*SPHERE_RADIUS;
diff = B;
if( ta < tb )
{
ta = (ta+tb)*0.5;
float sqarg = (A-E*ta)*(A-E*ta) - (B+C*ta*ta-2.0*D*ta);
float sq = sqrt( sqarg);
t = -(A-E*ta) - sq;
if(t > 0.)
{
hitout.dist = t;
hitout.accdist = calc_accdist(hitin, hitout);
hitout.hit = hitin.orig + hitin.dir*hitout.dist;
vec3 spveta = {sp.x, sp.y+ve_y*ta, sp.z};
hitout.N = normalize(hitout.hit - spveta);
//hitout.N = normalize(hitout.hit - (sp+ve*ta));
}
}
}
#endif
return sphere_hit(!nothit, center, hitin, t, diff);
}
hit_out ray_plane_intersect(IN(plane_t) plane, IN(point_and_dir) hitin)
{
hit_out hitout;
hitout.dist = RAY_NOINT;
hitout.borderdist = 0.;
vec3 plane_center = object_coord_to_float3(plane.center);
float d;
vec3 pt;
hole_t hole_margin = 0; //FIXME: parser needs initialization
vec3 o;
if (hitin.dir.y != 0.) // avoids division by zero
//if (is_negative(hitin.dir.y)) // avoids division by zero
//if (float_abs(hitin.dir.y) > EPS) // avoid division by zero
{
//d = -(hitin.orig.y-plane_center.y)/hitin.dir.y;
d = float_fast_div_u(hitin.orig.y-plane_center.y, -hitin.dir.y);
pt = hitin.orig + hitin.dir*d;
if (d>EPS) //strict gt
{
o = pt - plane_center;
#ifndef NON_INTERACTIVE
hole_margin = plane_has_hole(hole_t(o.x), hole_t(o.z));
if(!fixed_is_negative(hole_margin))
#endif
{
hitout.dist = d;
#ifdef ANTIALIAS
hitout.accdist = calc_accdist(hitin, hitout);
#endif
hitout.hit.orig = pt;
vec3 N = VECTOR_NURMAL_UPWARDS;
hitout.hit.dir = N; //points upwards
}
hitout.borderdist = float(hole_margin);
}
}
return hitout;
}
color_basic_t sphere_effect(IN(hit_out) hit, IN(render_material_t) hit_material)
{
color_basic_t rcolor = hit_material.diffuse_color;
#ifdef BLINKY
IN(scene_t) scene = get_scene();
IN(scene_colors_t) colors = scene_colors(scene);
IN(sphere_t) s = scene.sphere;
uint16_t frame = scene.frame;
uint8_t tick = frame>>1;
if((tick & 0x3F) != 1 || ((hash16(tick)>>13) & 1) != 0)
{
//eyeballs
float dy = (hit.hit.dir.y-float_shift(float(s.center.y),-6)*1.5); //FIXME: optimize constants 1.5, 1.25
float dx = float_shift(float_abs(hit.hit.dir.z-hit.hit.dir.x)-.6, -1)*1.25;
float d = dx*dx+dy*dy;
coord_type mindist = fixed_shr(s.heat, 4) + .25*.25;
if(coord_type(d) < mindist)
rcolor = d < .15*.15 ? color_basic_t(0.) : color_basic_t(1.2);
}
#endif
return rcolor;
}
#ifdef ANTIALIAS
inline color_type plane_alpha(float borderdist, float dist_z)
{
return color_type(borderdist/dist_z); //float_shift(borderdist/dist_z, -1);
}
float triang(float x )
{
float h = x-float(int(x));
return float_abs(h-.5);
}
#endif
#ifdef SOFT_SHADOW
color_type sphere_shadow(float x, float y, float z)
{
color_type r = 1.;
float d = x*x+z*z;
if(!is_negative(y) && d < SPHERE_RADIUS*SPHERE_RADIUS*4.)
{
d = d - SPHERE_RADIUS*SPHERE_RADIUS;
#if ALTERTNATE_UI > 4
if(is_negative(c))
r = .5;
#else
const float SHADOW_K = .5;
float v = d*float_fast_reciprocal_u(y)*SHADOW_K;
r = color_type(v)+.5;
if(fixed_is_negative(r)) r = 0.;
if(r > 1.) r = 1.;
#if SOFT_SHADOW > 1
r=r*r*(color_type(3.)-(r+r)); //smooths it
#endif
#endif
}
return r;
}
#endif
color_basic_t plane_effect(IN(hit_out) hit)
{
IN(scene_t) scene = get_scene();
IN(scene_colors_t) colors = scene_colors(scene);
IN(plane_t) plane = scene.plane;
color_basic_t rcolor = colors.plane.diffuse_color;
vec3 plane_center = object_coord_to_float3(plane.center);
float hitx = hit.hit.orig.x - plane_center.x;
float hitz = hit.hit.orig.z - plane_center.z;
float ox = float_shift(hitx, -FLOOR_SHIFT); //FIXME: same coordinates in this game
float oz = float_shift(hitz, -FLOOR_SHIFT);
int16_t ix = round16(ox);
int16_t iz = round16(oz);
static const color_type bk = .3;
#if !defined(COLOR_DECOMP) && defined(LEVELS)
color_basic_t color2 = {
(ix & 0x400)!=0 ? bk : colors.plane_color2.r,
(ix & 0x200)!=0 ? bk : colors.plane_color2.g,
(ix & 0x100)!=0 ? bk : colors.plane_color2.b
}; //FIXME: move logic to levels
#else
color_basic_t color2 = colors.plane_color2;
#endif
rcolor = ((ix ^ iz) & 1) != 0 ? colors.plane_color1 : color2;
#ifdef ANTIALIAS
color_basic_t rcolor2 = ((ix ^ iz) & 1) == 0 ? colors.plane_color1 : color2;
float hitdist = hit.accdist;
float opax = triang(ox)/float_shift(hitdist, 1);
float opaz = triang(oz)/float_shift(hitdist, 1);
if(opax > 1.) opax = 1.; if(opax < -1.) opax = -1.;
if(opaz > 1.) opaz = 1.; if(opaz < -1.) opaz = -1.;
float_type opa = opaz*opax+.5;
if(is_negative(opa))
{
rcolor = rcolor2;
//rcolor.x = 1.;
}
else if(opa > 1.)
{
rcolor = rcolor;
//rcolor.y = 1.;
}
else
{
//rcolor = color_select(color_type(opa), rcolor, rcolor2);
float r = opa*float(rcolor.x)-(opa-1.)*float(rcolor2.x);
float g = opa*float(rcolor.y)-(opa-1.)*float(rcolor2.y);
float b = opa*float(rcolor.z)-(opa-1.)*float(rcolor2.z);
rcolor.x = color_type(r);
rcolor.y = color_type(g);
rcolor.z = color_type(b);
//rcolor.z = 1.;
}
#endif
#ifdef HOLE_BORDER
if(hit.borderdist < HOLE_BORDER)
{
#ifndef ANTIALIAS
rcolor = colors.plane.diffuse_color;
#else
float dh = HOLE_BORDER-hit.borderdist;
if(dh < hit.accdist)
{
color_type opacity = plane_alpha(dh, hit.accdist);
rcolor = color_select(opacity, colors.plane.diffuse_color, rcolor); //border inside floor
}
else
rcolor = colors.plane.diffuse_color;
#endif
}
#endif
return rcolor;
}
color_basic_t background_color(float dir_y)
{
color_type y = is_negative(dir_y) ? color_type(0.) : color_type(dir_y*dir_y);
return color_basic_t(y);
}
color_basic_t light_intensity(IN(vec3) hit)
{
#if 0
//this float version takes 2148 FPGA cells
float lz = hit.z-LIGHT_Z;
float dl = hit.x*hit.x + LIGHT_Y*LIGHT_Y + lz*lz;
return color_type(inversesqrt(dl)*LIGHT_Y) + AMBIENT_INTENSITY;
#else
//light_intensity optimized for fixe points
coord_type lz = (coord_type(hit.z)-LIGHT_Z)*coord_type(1./LIGHT_Y);
coord_type lx = coord_type(hit.x)*coord_type(1./LIGHT_Y);
coord_type dl = lx*lx + 1. + lz*lz;
return color_basic_t(inversesqrt(float(dl))); //FIXME: implement RSQRT for fixed points
#endif
}
color_basic_t cast_ray_nested(IN(point_and_dir) hitin)
{
IN(scene_t) scene = get_scene();
IN(scene_colors_t) colors = scene_colors(scene);
#ifdef RT_SMALL_UI
return background_color(hitin.dir.y);
#else
render_material_t hit_material;
hit_material = colors.sphere;//this is what's reflected on the floor
#if 1<PLANE_MAXRECURSIVITY
hit_out hitout = ray_sphere_intersect(object_coord_to_float3(scene.sphere.center), hitin);
#else
#error not tested
hit_out hitout;
hitout.dist = RAY_NOINT;
#endif
#if 1<SPHERE_MAXRECURSIVITY
hit_out hitplane = ray_plane_intersect(scene.plane, hitin);
if (hitplane.dist < hitout.dist)
{
//this controls what's reflected on the sphere
hitout = hitplane;
hit_material = colors.plane;
#ifdef ANTIALIAS
hitout.accdist = float_shift(hitout.accdist, 2); //blur ball reflection
#endif
hit_material.diffuse_color = plane_effect(hitout);
}
#endif
color_basic_t rcolor = color_basic_t(0.);
//#warning solve need to initialize
if (hitout.dist >= float_shift(1., DIST_SHIFT))
rcolor = background_color(hitin.dir.y); //has other direction
else
rcolor = hit_material.diffuse_color*light_intensity(hitout.hit.orig);
return rcolor;
#endif
}
color_basic_t shade(IN(color_basic_t) background, IN(vec3) dir, IN(hit_out) hit, IN(render_material_t) hit_material, color_type minfog)
{
IN(scene_t) scene = get_scene();
IN(scene_colors_t) colors = scene_colors(scene);
color_basic_t rcolor = background;
float fogmix = float_shift(hit.dist, -DIST_SHIFT); //no need to accumulated dist
if (fogmix < 1.)
{
point_and_dir hitreflect;
hitreflect.orig = hit.hit.orig;
hitreflect.dir = reflect(dir, hit.hit.dir);
#ifdef ANTIALIAS
hitreflect.dist = hit.dist; //to accumulate distance
#endif
color_basic_t reflect_color = cast_ray_nested(hitreflect);
color_basic_t li = light_intensity(hit.hit.orig);
#ifdef SOFT_SHADOW
float sx = hit.hit.orig.x - SPHERE_X;
float sy = (float)scene.sphere.center.y;
float sz = hit.hit.orig.z - SPHERE_Z;
li = li*sphere_shadow(sx, sy, sz);
#endif
color_basic_t diffuse_color = hit_material.diffuse_color * (li + color_type(AMBIENT_INTENSITY));
color_basic_t comb_color = diffuse_color + reflect_color*hit_material.reflect_color;
rcolor = color_select(color_max(color_type(fogmix), minfog), colors.fog, comb_color);
}
return rcolor;
}
bool is_star(float x, float y)
{
return ((hashf(x)>>2) & (hashf(y)>>2)) > 0x3E00;
}
color_basic_t cast_ray(IN(point_and_dir) hitin)
{
IN(scene_t) scene = get_scene();
IN(scene_colors_t) colors = scene_colors(scene);
float ys = float_abs(float_shift(hitin.dir.y, 1));
#ifndef RT_SMALL_UI
bool has_star = is_star(hitin.dir.x, hitin.dir.y);
color_basic_t sky = has_star ? color_basic_t(STAR_INTENSITY) : background_color(hitin.dir.y);
color_type mix = ys<1. ? color_type(1)-color_type(ys): color_type(0);
color_basic_t bfog = color_select(mix, colors.fog, sky);
#else
color_basic_t bfog = color_basic_t(ys);
color_type mix = 1.;
#endif
hit_out hitsphere = ray_sphere_intersect(object_coord_to_float3(scene.sphere.center), hitin);
render_material_t sphere_material = colors.sphere; //FIXME: needed?
sphere_material.diffuse_color = sphere_effect(hitsphere, colors.sphere);
#ifndef RT_SMALL_UI
hit_out hitplane = ray_plane_intersect(scene.plane, hitin);
#else
hit_out hitplane; hitplane.dist = RAY_NOINT;
#endif
render_material_t planematerial;
planematerial = colors.plane; //FIXME: needed?
planematerial.diffuse_color = plane_effect(hitplane);
#ifndef ANTIALIAS
bool planehit = hitplane.dist < hitsphere.dist;
render_material_t hit_material;
hit_material = planehit ? planematerial : sphere_material;
hit_out hitout = planehit ? hitplane : hitsphere;
color_type c = planehit ? mix : color_type(0.);
color_basic_t rcolor = shade(bfog, hitin.dir, hitout, hit_material, c); //no fog for sphere
#else //ANTIALIAS=true
//#warning why non default constructor?
color_basic_t rcolor = color_basic_t(0.);
#ifndef RT_SMALL_UI
color_basic_t planecolor = shade(bfog, hitin.dir, hitplane, planematerial, mix); //FIXME: bfog
float sphere_plane_dist = hitplane.dist - hitsphere.dist;
if(hitplane.dist != RAY_NOINT && hitplane.borderdist < hitplane.accdist)
{
color_type planeopacity = plane_alpha(hitplane.borderdist, hitplane.accdist);
planecolor = color_select(planeopacity, planecolor, bfog); //border next to hole
}
if(is_negative(sphere_plane_dist))
{
//plane & plane border
rcolor = planecolor;
}
else
#endif //RT_SMALL_UI
{
color_basic_t spherecolor = shade(bfog, hitin.dir, hitsphere, colors.sphere, 0.);
float aaradius = float_shift(hitsphere.dist, ANTIALIAS-13);
float opacity = hitsphere.borderdist*aaradius;
float da = sphere_plane_dist*aaradius; //FIXME: this shouldn't compresses in y axis when plane is near to the horizontal
if(da >= 0. && da < 1.) //sphere & floor intersection
{
opacity = opacity*da;
}
if(opacity > 1.) opacity = 1.;
if(is_negative(opacity)) opacity = 0.; //FIXME: define clamp function
rcolor = color_select(color_type(opacity), spherecolor, planecolor);
}
#endif //ANTIALIAS
return rcolor;
}
color_basic_t render_pixel_internal(screen_coord_t x, screen_coord_t y)
{
IN(scene_t) scene = get_scene();
IN(scene_colors_t) colors = scene_colors(scene);
point_and_dir hitin;
hitin.orig = object_coord_to_float3(scene.camera);
vec3 camera_dir = {float(x), float(y), float(-1.)};
hitin.dir = normalize(camera_dir);
#ifdef ANTIALIAS
hitin.dist = 0.; //start dist
#endif
return cast_ray(hitin);
}
#else // ALTERNATE_UI = true
uint12_t star_vel(uint12_t a, uint8_t b)
{
uint12_t r = a;
if(b & 1) r = r + a;
if(b & 2) r = r + (a<<1);
if(b & 4) r = r + (a<<2);
return r >> 4;
}
color_basic_t background_color_alt(screen_coord_t x, screen_coord_t y, uint16_t frame, uint16_t off, coord_type z)
{
fixed_type dir_y = (y-.5)*fixed_abs(x*x-1.);
color_basic_t c = (dir_y < 0 ? color_type(0.) : color_type(dir_y*dir_y));
if(z < 4)
{
int16_t cy = (int16_t) fixed_shl(y*z, 9);
int16_t cx = (int16_t) fixed_shl(x*z, 9) + star_vel(off, cy & 7); //add some movement
uint16_t pix_hash = hash16(cx ^ hash16(cy)); //FIXME: pixel hash shold be part of the scene
if((pix_hash & 0xFFC0) == 0) //add star
c = color_basic_t(fixed_shr(((pix_hash<<2) + frame) & 0x7F, 9) + STAR_INTENSITY);
}
return c;
}
color_basic_t render_floor_alt(screen_coord_t x, screen_coord_t y, coord_type px, coord_type py, coord_type pz, color_basic_t c)
{
//#warning check why fixed types cannot be left uninitialized
coord_type inv_y = 0.;
uint8_t ux;
uint8_t uz;
bool drawfog = false;
if (y != 0. && fixed_is_negative(y) == fixed_is_negative(py)) //FIXME: floor is not drawn correctly
{
inv_y = fixed_shl(py, FLOOR_SHIFT)/y;
coord_type ix = inv_y*x-px;
coord_type iz = inv_y + pz;
#ifndef NON_INTERACTIVE
hole_t hole_d = plane_has_hole(ix, coord_type(0)-iz);
if(!fixed_is_negative(hole_d)) //internal area
#endif
{
//if((short(inv_y) >> 10) == 0)
if(inv_y < 512)
{
ux = (short)ix;
uz = (short)iz;
c = ((ux ^ uz)>>FLOOR_SHIFT) & 1 ? K_plane_color2 : K_plane_color1;
#ifdef HOLE_BORDER
if(hole_d < HOLE_BORDER)
c = K_floor_difusse;
#endif
}
drawfog = true;
}
}
else
drawfog = true;
if(drawfog)
c = color_select(fixed_abs(y), c, K_fog_color);
return c;
}
color_basic_t render_pixel_internal_alt(screen_coord_t x, screen_coord_t y)
{
IN(scene_t) scene = get_scene();
IN(scene_colors_t) colors = scene_colors(scene);
coord_type dz = coord_type(scene.camera.z-SPHERE_Z);
coord_type dx = coord_type(x*dz - (scene.sphere.center.x)); //-scene.camera.x
coord_type dy = coord_type(y*dz - (scene.sphere.center.y-scene.camera.y));
#if ALTERNATE_UI > 1
color_basic_t c = background_color_alt(x, y, scene.frame, -(int16_t)scene.plane.center.x, dz*coord_type(.5/CAMERA_Z));
#else
color_basic_t c = K_fog_color;
#endif
bool drawfloor = true;
//draw sphere
static const float SPHERE_R = (-.707)*SPHERE_Z/SPHERE_RADIUS; //FIXME: check if code generator parenthesizes it
if((dx > -SPHERE_R) && (dx < SPHERE_R) && (dy > -SPHERE_R) && (dy < SPHERE_R)) //FIXME: use >=, <=
{
//inside bounding box
if(dx*dx + dy*dy < SPHERE_R*SPHERE_R)
{
c = scene.sphere.material.diffuse_color; //K_gold_color
//else c.b = 1.; //uncomment to display bounding box
drawfloor = scene.sphere.center.y + dy < FLOOR_Y;
}
}
#if ALTERNATE_UI > 2
if(drawfloor)
{
#ifdef PIPELINEC_SUGAR
const float FRAME_HEIGHT_FLOAT = FRAME_HEIGHT;
const float CAMERA_FACTOR = -2.*CAMERA_Z/FRAME_HEIGHT_FLOAT;
#else
static const float CAMERA_FACTOR = -2.*CAMERA_Z/FRAME_HEIGHT;
#endif
c = render_floor_alt(x, y,
scene.plane.center.x, //-scene.camera.x
scene.camera.y*coord_type(CAMERA_FACTOR),
scene.plane.center.z-scene.camera.z, c);
}
#endif
return c;
}
#endif
#ifdef DITHER
inline uint8_t mask_code(uint8_t v) {
v = ((v&1)<<3) | ((v&2)<<1) | ((v&4)>>1) | ((v&8)>>3); //bit reverse
return v ^ (v>>1); //gray
}
inline uint9_t dither(uint8_t x, uint8_t y, uint9_t v)
{
return ((mask_code(y^mask_code(x/*^mask_code(frame)*/))) + v ) & 0x1F0;
//return v & 0x1F0; //simple threshold
}
#endif
full_state_t reset_state(uint16_t score)
{
full_state_t state;
material_t gold;
gold.diffuse_color = K_gold_color;
gold.reflect_color = K_gold_reflect_color;
material_t floor_material;
floor_material.diffuse_color = K_floor_difusse;
floor_material.reflect_color = K_floor_reflect;
state.scene.plane.center = K_plane_center_start;
state.scene.plane.material = floor_material;
state.scene.plane.color1 = K_plane_color1;
state.scene.plane.color2 = K_plane_color2;
state.scene.sphere.center = K_sphere_center_start;
state.scene.sphere.material = gold;
state.scene.sphere.heat = 0.;
state.scene.camera = K_camera_pos_start;
state.scene.frame = 0;
state.scene.scorebar = 0;
state.scene.fog = K_fog_color;
state.plane_y = coord_type(state.scene.plane.center.y);
state.sphere_x = coord_type(state.scene.sphere.center.x);
state.sphere_z = coord_type(state.scene.sphere.center.z);
state.gold_color = gold.diffuse_color;
state.gold_reflect_color = gold.reflect_color;
state.lava_color = K_lava_color;
state.sphere_y = coord_type(state.scene.sphere.center.y);
state.heat = state.scene.sphere.heat;
state.camera_y = coord_type(state.scene.camera.y);
state.camera_z = coord_type(state.scene.camera.z);
state.plane_x = coord_type(FLOOR_X);
state.sphere_xvel = 0.;
state.sphere_yvel = 0.;
state.won = false;
state.score = score;
return state;
}
full_state_t full_update(INOUT(full_state_t) state, bool reset, bool button_state)
{
uint16_t score = state.score;
if(reset) score = 0;
state.plane_x = state.plane_x + state.sphere_xvel;
state.sphere_yvel = state.sphere_yvel + GRAVITY_CONSTANT;
state.sphere_y = state.sphere_y - state.sphere_yvel;
coord_type underground = (state.sphere_y - SPHERE_RADIUS) - FLOOR_Y; //PLANE_Y is always 0
if(state.won)
state.sphere_yvel = fixed_shr(underground, 4);
if(fixed_is_negative(underground))
{
state.sphere_xvel = state.sphere_xvel - XVEL_CONSTANT;
coord_type coord_x = state.sphere_x - state.plane_x;
coord_type coord_z = state.sphere_z - state.plane_x; //z=x
bool half_up = state.sphere_y > state.plane_y;
if(half_up && state.won == false)
{
#ifdef HEAT_CONSTANT
state.heat = state.heat + HEAT_CONSTANT;
#endif
#ifndef GOD_MODE
//TODO: if the plane has a hole can be calculated at rendering time and reused!
if(plane_has_hole(coord_x, coord_z) > -HOLE_GUARD_MARGIN) // > about -.1 gives margin for the ball size
#endif
{
state.score = state.score+SCORE_STEP;
if(state.score >= MAXSCORE && state.won!=true)
state.won = true;
#ifdef GOD_MODE
button_state = state.sphere_xvel < -XVEL_CONSTANT*20. || fixed_is_negative(state.sphere_yvel);
#elif defined(NON_INTERACTIVE)
button_state = true;
#endif
if(button_state)
{
state.sphere_yvel = -JUMP_CONSTANT;
state.sphere_xvel = -XVEL_DEFAULT;
}
else
state.sphere_yvel = -GRAVITY_CONSTANT_LIGHT;
}
}
else
{
state.camera_z = state.camera_z-fixed_shr(underground, ZOOMOUT_CONSTANT); //lose => fadeout
}
}
state.camera_y = state.camera_y + fixed_shr(state.sphere_y - state.camera_y, 5);
#ifdef HEAT_CONSTANT
state.heat = state.heat - color_type(fixed_shr(state.heat, 4)); //cools down
#endif
//write all outputs
state.lose = fixed_is_negative(underground) && ((-int16_t(underground) >> 10) != 0); //underground < -2048.
#ifdef HEAT_CONSTANT // && ALTERNATE_UI > 1
state.diffuse_color = color_select(state.heat, state.lava_color, state.gold_color);
state.reflect_color = state.gold_reflect_color*(color_type(1.) - fixed_shr(state.heat, 2));
#else
state.diffuse_color = state.gold_color;
state.reflect_color = state.gold_reflect_color;
#endif
state.scorebar = state.won ? 0 : state.score;
if(state.lose)
reset = true;
if(reset)
state = reset_state(score);
state.scene.sphere.center.y = state.sphere_y;
state.scene.sphere.heat = state.heat;
state.scene.camera.y = state.camera_y;
state.scene.camera.z = state.camera_z;
state.scene.plane.center.x = state.plane_x;
state.scene.plane.center.z = state.plane_x;
state.scene.sphere.material.diffuse_color = state.diffuse_color;
state.scene.sphere.material.reflect_color = state.reflect_color;
state.scene.sphere.yvel = state.sphere_yvel;
state.scene.scorebar = state.scorebar; //FIXME: move to state update function to make this function wires-only
state.scene.frame = state.scene.frame + 1;
return state;
}
#ifndef SHADER
inline pixel_t render_pixel(uint16_t i, uint16_t j
#ifdef COLOR_DECOMP
, pixel_t pix_in
#endif
)
{
IN(scene_t) scene = get_scene();
#ifndef PIPELINEC_SUGAR
int16_t cx = (i<<1)-FRAME_WIDTH-1;
int16_t cy = -((j<<1)-FRAME_HEIGHT-1);
#else
int16_t cx = i << 1;
cx = cx - (FRAME_WIDTH + 1);
int16_t cy = j << 1;
cy = (FRAME_HEIGHT + 1) - cy;
#endif
const float W = (float)FRAME_WIDTH;
const float H = (float)FRAME_HEIGHT;
static const screen_coord_t ax = 1024.*(SCREEN_ASPECT)/W;
static const screen_coord_t ay = 1024./H;
screen_coord_t x = fixed_shr(cx, 10+1) * ax;
screen_coord_t y = fixed_shr(cy, 10+1) * ay;
pixel_t pix; //ignores alpha
static const uint16_t score_factor = 2048*(FRAME_WIDTH-2*SCORE_MARGINS)/MAXSCORE;
uint16_t scorebar = score_factor*scene.scorebar >> 11;
if(i >= SCORE_MARGINS && i < SCORE_MARGINS + scorebar && j > SCORE_MARGINS && j < 2*SCORE_MARGINS)
{
pix.r = 0; pix.g = 200; pix.b = 0; // pix = color(0., 200./255., 0.);
}
else
{
#ifndef COLOR_DECOMP
#ifdef ALTERNATE_UI
color c = render_pixel_internal_alt(x, y);
///*if((i ^ j) & (1<<7))*/ if(cx>0) c = render_pixel_internal(x, y, scene, scene_colors(scene)); //uncomment for checkerboard
#else
color c = render_pixel_internal(x, y);
#endif
uint16_t r = (uint16_t)fixed_asshort(c.r, 8);
uint16_t g = (uint16_t)fixed_asshort(c.g, 8);
uint16_t b = (uint16_t)fixed_asshort(c.b, 8);
#ifdef DITHER
r = dither(i^j, i, r);
g = dither(i+j, j, g);
b = dither(i, j, b); //i+j, i or i, j or j, i
#endif
pix.r = (r >= 256) ? uint8_t(255):uint8_t(r);
pix.g = (g >= 256) ? uint8_t(255):uint8_t(g);
pix.b = (b >= 256) ? uint8_t(255):uint8_t(b);
#else //COLOR_DECOMP
pix = pix_in;
color_type c = render_pixel_internal(x, y);
uint9_t c9 = (uint9_t)fixed_asshort(c, 8);
//uint8_t c8 = (uint8_t) ((c9 & ~0xFF) ? (uint8_t)0xFF:(uint8_t)c9); //opt version, works?