-
Notifications
You must be signed in to change notification settings - Fork 0
/
raytracer3.c
150 lines (126 loc) · 4.53 KB
/
raytracer3.c
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
// Ported from https://gabrielgambetta.com/zx-raytracer-3-src.html
#include <arch/zx.h>
#include <intrinsic.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "raytracer.h"
struct sphere spheres[] = {
{0, -1, 4, SQUARED(1.f), INK_RED},
{2, 0, 4, SQUARED(1.f), INK_BLUE},
{-2, 0, 4, SQUARED(1.f), INK_GREEN},
{0, -5001, 0, SQUARED(5000.f), INK_YELLOW}
};
const uint8_t spheres_n = sizeof(spheres) / sizeof(spheres[0]);
uint8_t trace_ray(float dx, float dy) {
uint8_t color = PAPER_BLACK;
float mt = 1e10f;
const float a = 2 * (dx * dx + dy * dy + 1.f);
for (uint8_t i = 0; i < spheres_n; i++) {
const float cx = spheres[i].x;
const float cy = spheres[i].y;
const float cz = spheres[i].z;
const float b = 2 * (dx * cx + dy * cy + cz);
const float c = (cx * cx + cy * cy + cz * cz) - spheres[i].radius_squared;
float d = b * b - 2 * a * c;
if (d < 0) {
continue;
}
d = sqrt(d);
float t = (b + d) / a;
if (t > 0 && t < mt) {
color = spheres[i].color;
mt = t;
}
t = (b - d) / a;
if (t > 0 && t < mt) {
color = spheres[i].color;
mt = t;
}
}
return color;
}
uint8_t color_pixels[64];
uint8_t color_usage[8];
unsigned char* memory = 0;
int main(void) {
// Reset frame counter
memory[23672] = memory[23673] = memory[23674] = 0;
// Re-enable interrupt (for the ROM frame counter)
intrinsic_ei();
zx_cls(PAPER_WHITE | INK_BLACK);
for (int16_t x = 0; x < 256; x += 8) {
for (int16_t y = 0; y < 176; y += 8) {
const uint8_t ctl = trace_ray((x - 128) / 256.f, (y - 88) / -256.f);
const uint8_t ctr = trace_ray((x - 128 + 7) / 256.f, (y - 88) / -256.f);
const uint8_t cbl = trace_ray((x - 128) / 256.f, (y - 88 + 7) / -256.f);
const uint8_t cbr = trace_ray((x - 128 + 7) / 256.f, (y - 88 + 7) / -256.f);
if (ctl == cbr && ctl == ctr && ctl == cbl) {
// All corners same color -> set block background
*zx_pxy2aaddr(x, y) = (ctl << 3) | BRIGHT;
continue;
}
memset(color_usage, 0, 8);
uint8_t ci = 0;
for (int16_t u = x; u < x + 8; u++) {
const float dx = (u - 128) / 256.f;
for (int16_t v = y; v < y + 8; v++) {
uint8_t color;
switch (ci) {
case 0:
color = ctl;
break;
case 7:
color = cbl;
break;
case 56:
color = ctr;
break;
case 63:
color = cbr;
break;
default:
color = trace_ray(dx, (v - 88) / -256.f);
break;
}
color_usage[color]++;
color_pixels[ci++] = color;
}
}
// Find the most and second frequent color in this 8x8 block
uint8_t mfc = 0;
uint8_t color_paper = 0;
for (uint8_t c = 0; c < 8; c++) {
if (color_usage[c] > mfc) {
mfc = color_usage[c];
color_paper = c;
}
}
mfc = 0;
uint8_t color_ink = 0;
for (uint8_t c = 0; c < 8; c++) {
if (c != color_paper && color_usage[c] > mfc) {
mfc = color_usage[c];
color_ink = c;
}
}
// Paper is the most frequent color, ink the second
*zx_pxy2aaddr(x, y) = (color_paper << 3) | color_ink | BRIGHT;
// Plot all secondary colors
ci = 0;
for (int16_t u = x; u < x + 8; u++) {
for (int16_t v = y; v < y + 8; v++) {
if (color_pixels[ci] != color_paper) {
*zx_pxy2saddr(u, v) |= zx_px2bitmask(u);
}
ci++;
}
}
}
}
intrinsic_di();
const uint32_t end = ((uint32_t)(memory[23674]) << 16) | ((uint32_t)(memory[23673]) << 8) | memory[23672];
const uint32_t elapsed = end / 50;
printf("%d\"", elapsed);
return 0;
}