interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / allen-scene / native / night.c

780 lines · 37394 bytes Raw Blame History
1/* night.c -- the night sky and the merging black holes; see night.h.
2 *
3 * The sky is the real one (ui/sky_real.c, from ui/sky_data.c): the
4 * Milky Way from a photograph of the whole sky, worked out at a quarter of
5 * the screen's resolution (it is smooth anyway; the photograph's own
6 * stars are taken out), scaled up with a 4 x 4 ordered dither so the faint
7 * gradients show no bands; then the catalogue's stars, at full resolution,
8 * on top.
9 *
10 * Bent light: a point mass moves the image of a star seen at angle theta
11 * from it to where the star really is, beta = theta - E^2 / theta (E the
12 * Einstein radius). Each pixel shows the sky at its own beta (with both
13 * holes' pulls added), so stars behind a hole stretch into arcs round it and
14 * a second, inner image appears on the far side. The pull is eased to zero
15 * between 2.2 E and 4.4 E so the bent region has no edge. It is worked out
16 * exactly near the holes and on an 8-pixel grid (and interpolated) further
17 * out, where it is smooth. The ripple adds a displacement along the radius
18 * that stretches one way and squeezes the other (cos 2 theta), the pattern
19 * a gravitational wave makes. */
20#include "night.h"
21#include "sky_real.h"
22
23#define S(v) ((int32_t)(((int64_t)(v) * (int64_t)n->k + 128) / 256))
24#define Q30 (1ull << 30)
25
26/* ---- the galaxies: the real ones in the view (sky_real.c) ---- */
27
28static uint8_t band_t[256]; /* the sweeping band: ((1 + cos) / 2)^6, 0..255 */
29static uint32_t pow2_t[257]; /* 2^(k/256), Q16: for the dusk's distances without a square root per pixel */
30
31static uint64_t pow2_q16(uint32_t x) /* 2^(x / 65536), Q16; x below 40 << 16 */
32{
33 uint32_t n = x >> 16, f = x & 65535u, k = f >> 8, g = f & 255u;
34 uint64_t m = pow2_t[k] + ((uint64_t)(pow2_t[k + 1] - pow2_t[k]) * g >> 8);
35 return m << n;
36}
37
38size_t ck_night_mem_bytes(uint32_t w, uint32_t h, uint32_t k)
39{
40 (void)k;
41 size_t n = (size_t)w * h * 4 + (size_t)(w / 4 + 1) * (h / 4 + 1) + 64;
42 for (unsigned i = 0; i < CK_NIGHT_GALAXIES; i++) {
43 ck_rect b = ck_sky_galaxy_bound(w, i);
44 n += (size_t)b.w * (size_t)b.h * 2;
45 }
46 return n;
47}
48
49/* exp(-(v / width)^2), Q16 */
50static uint32_t gauss(int64_t v, int64_t width) /* exp(-(v/width)^2), Q16 */
51{
52 int64_t q = v * 256 / (width ? width : 1);
53 if (q > 4095 || q < -4095)
54 return 0;
55 return ck_exp_neg((uint32_t)(q * q >> 8));
56}
57
58/* ---- the soft layer (the Milky Way): a quarter of the resolution, 16ths of a level ---- */
59
60static void scale_up(ck_night *n, const uint16_t *q, int32_t qw)
61{
62 static const uint8_t bayer[16] = {0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5};
63 for (int32_t y = 0; y < n->h; y++) {
64 int32_t qy = y >> 2, fy = y & 3;
65 const uint16_t *r0 = q + (size_t)qy * (size_t)qw * 3, *r1 = r0 + (size_t)qw * 3;
66 uint32_t *row = n->sky + (size_t)y * (size_t)n->w;
67 for (int32_t x = 0; x < n->w; x++) {
68 int32_t qx = x >> 2, fx = x & 3;
69 uint32_t d = bayer[(y & 3) * 4 + (x & 3)], c = 0;
70 for (int k = 0; k < 3; k++) {
71 uint32_t a = r0[qx * 3 + k] * (uint32_t)(4 - fx) + r0[qx * 3 + 3 + k] * (uint32_t)fx;
72 uint32_t b = r1[qx * 3 + k] * (uint32_t)(4 - fx) + r1[qx * 3 + 3 + k] * (uint32_t)fx;
73 uint32_t v = ((a * (uint32_t)(4 - fy) + b * (uint32_t)fy) / 16 + d) >> 4;
74 c = c << 8 | (v > 255 ? 255u : v);
75 }
76 row[x] = c;
77 }
78 }
79}
80
81/* ---- coming out of the dark ---- */
82
83static void appear_map(ck_night *n)
84{
85 int32_t ah = n->h / 4 + 1;
86 int64_t far = 0;
87 int32_t cs[4][2] = {{0, 0}, {n->w, 0}, {0, n->h}, {n->w, n->h}};
88 for (int i = 0; i < 4; i++) {
89 int64_t dx = cs[i][0] - n->cx, dy = cs[i][1] - n->cy, d = (int64_t)ck_isqrt((uint64_t)(dx * dx + dy * dy));
90 far = d > far ? d : far;
91 }
92 for (int32_t y = 0; y < ah; y++)
93 for (int32_t x = 0; x < n->aw; x++) {
94 int64_t px = (int64_t)x * 4, py = (int64_t)y * 4, dx = px - n->cx, dy = py - n->cy;
95 uint32_t a = ck_fbm((int32_t)(px * 640 / n->h), (int32_t)(py * 640 / n->h), 909, 4) >> 8;
96 uint32_t r = (uint32_t)((int64_t)ck_isqrt((uint64_t)(dx * dx + dy * dy)) * 255 / (far ? far : 1));
97 n->appear[(size_t)y * (size_t)n->aw + (size_t)x] = (uint8_t)((a * 6 + r * 4) / 10);
98 }
99}
100
101uint32_t ck_night_faded(const ck_night *n, int32_t x, int32_t y, uint32_t l)
102{
103 if (n->t >= CK_NIGHT_FADE_END_MS)
104 return 256;
105 x = ck_clamp(x, 0, n->w - 1), y = ck_clamp(y, 0, n->h - 1);
106 int32_t from = 300 + n->appear[(size_t)(y >> 2) * (size_t)n->aw + (size_t)(x >> 2)] * 12 + (int32_t)(255 - l) * 8;
107 int32_t dt = (int32_t)n->t - from;
108 return dt <= 0 ? 0 : dt >= 1365 ? 256 : ck_smooth(dt * 48 >> 8);
109}
110
111static uint32_t fade_px(const ck_night *n, uint32_t c, int32_t x, int32_t y)
112{
113 uint32_t r = c >> 16 & 255, g = c >> 8 & 255, b = c & 255, l = r > g ? r : g;
114 l = l > b ? l : b;
115 uint32_t f = ck_night_faded(n, x, y, l);
116 return f >= 256 ? c : ck_scale(c, f);
117}
118
119int ck_night_init(ck_night *n, uint32_t w, uint32_t h, uint32_t k, int32_t cx, int32_t cy, void *mem, size_t len,
120 void *scratch)
121{
122 if (!mem || !scratch || w < 64 || h < 64 || w > CK_NIGHT_W_MAX || len < ck_night_mem_bytes(w, h, k))
123 return -1;
124 (void)ck_sin(0); /* the tables, before anything else could ask for them */
125 for (unsigned i = 0; i < 256; i++) {
126 uint64_t c = (uint64_t)(16384 + ck_cos(i * 256u)) >> 1; /* Q14 */
127 uint64_t c2 = c * c >> 14, c6 = (c2 * c2 >> 14) * c2 >> 14;
128 band_t[i] = (uint8_t)(c6 * 255 >> 14);
129 }
130 uint64_t e2 = 1ull << 30; /* 2^(k/256): one factor of 2^(1/256) (Q30) per step */
131 for (unsigned i = 0; i < 256; i++) {
132 pow2_t[i] = (uint32_t)((e2 + (1u << 13)) >> 14);
133 e2 = e2 * 1076652777ull >> 30;
134 }
135 pow2_t[256] = 131072;
136 *n = (ck_night){0};
137 n->w = (int32_t)w, n->h = (int32_t)h, n->k = k, n->cx = cx, n->cy = cy;
138 uint8_t *p = mem;
139 n->sky = (uint32_t *)p;
140 p += (size_t)w * h * 4;
141 n->appear = p;
142 n->aw = (int32_t)(w / 4 + 1);
143 p += (size_t)(w / 4 + 1) * (h / 4 + 1);
144 int32_t qw = (int32_t)(w / 4 + 2), qh = (int32_t)(h / 4 + 2);
145 n->view = ck_sky_the_view; /* default view: GW150914 region */
146 ck_sky_milkyway_q(scratch, qw, qh, n->w, cx, cy, &n->view);
147 scale_up(n, scratch, qw);
148 ck_canvas cv = {n->sky, n->w, n->h};
149 ck_sky_draw_stars(&cv, k, cx, cy, &n->view, n->tw, CK_NIGHT_TWINKLES, &n->ntw);
150 for (unsigned i = 0; i < CK_NIGHT_GALAXIES; i++) {
151 ck_night_galaxy *g = &n->gal[i];
152 g->box = ck_sky_galaxy_box(w, h, cx, cy, &n->view, i);
153 g->light = p;
154 g->phase = p + (size_t)g->box.w * (size_t)g->box.h;
155 p += (size_t)g->box.w * (size_t)g->box.h * 2;
156 ck_sky_galaxy_maps(g, i, w, cx, cy, &n->view);
157 }
158 appear_map(n);
159 ck_night_at(n, 0);
160 return 0;
161}
162
163/* ---- the black holes and the ripple at a time ---- */
164
165static uint64_t root8(uint64_t l) /* l^(1/8), Q30 */
166{
167 for (int i = 0; i < 3; i++)
168 l = ck_isqrt(l << 30);
169 return l;
170}
171
172#define ORBIT_MS 5200u /* one turn of the wide orbit */
173
174static uint32_t orbit_angle(uint32_t t, int32_t *sep16, int32_t a0)
175{
176 uint32_t spiral_from = (CK_NIGHT_SPIRAL_MS - CK_NIGHT_HOLES_MS) * 65536u / ORBIT_MS;
177 if (t < CK_NIGHT_SPIRAL_MS) {
178 *sep16 = a0;
179 return (t - CK_NIGHT_HOLES_MS) * 65536u / ORBIT_MS;
180 }
181 uint64_t left = ((uint64_t)(CK_NIGHT_MERGE_MS - t) << 30) / (CK_NIGHT_MERGE_MS - CK_NIGHT_SPIRAL_MS);
182 uint64_t r = root8(left), r2 = r * r >> 30, r5 = (r2 * r2 >> 30) * r >> 30;
183 *sep16 = (int32_t)((uint64_t)a0 * r2 >> 30);
184 /* the spiral's angle carries on at the wide orbit's speed: K (1 - L^(5/8)), K = 8/5 x its turns */
185 uint64_t turns = (uint64_t)65536 * 8 * (CK_NIGHT_MERGE_MS - CK_NIGHT_SPIRAL_MS) / (5ull * ORBIT_MS);
186 return spiral_from + (uint32_t)(turns * (Q30 - r5) >> 30);
187}
188
189void ck_night_at(ck_night *n, uint32_t t)
190{
191 n->t = t;
192 n->holes = 0;
193 n->falling = 0;
194 n->lens = (ck_rect){0, 0, 0, 0};
195 n->ripple_w = 0;
196 int32_t e_full = S(60) * 16, rs_full = S(20) * 16, a0 = S(270) * 16;
197 int32_t cx = n->cx * 16 + 8, cy = n->cy * 16 + 8;
198 int32_t sep;
199 uint32_t last = orbit_angle(CK_NIGHT_MERGE_MS - 1, &sep, a0);
200 if (t >= CK_NIGHT_HOLES_MS && t < CK_NIGHT_MERGE_MS) {
201 uint32_t ang = orbit_angle(t, &sep, a0);
202 int32_t e = (int32_t)((int64_t)e_full * ck_smooth(ck_prog(t, CK_NIGHT_HOLES_MS, CK_NIGHT_HOLES_MS + 2600)) >> 8);
203 int32_t rs = (int32_t)((int64_t)rs_full *
204 ck_smooth(ck_prog(t, CK_NIGHT_HOLES_MS + 1400, CK_NIGHT_HOLES_MS + 3400)) >> 8);
205 int32_t hx = (int32_t)((int64_t)(sep / 2) * ck_cos(ang) >> 14);
206 int32_t hy = (int32_t)((int64_t)(sep / 2) * ck_sin(ang) >> 15); /* the orbit leans away: half as tall */
207 n->hole[0] = (__typeof__(n->hole[0])){cx + hx, cy + hy, e, rs};
208 n->hole[1] = (__typeof__(n->hole[1])){cx - hx, cy - hy, e, rs};
209 n->holes = 2;
210 } else if (t >= CK_NIGHT_MERGE_MS && t < CK_NIGHT_GONE_MS) {
211 uint32_t tm = t - CK_NIGHT_MERGE_MS;
212 int32_t e = e_full * 362 / 256, rs0 = rs_full * 323 / 256, rs1 = rs_full * 2;
213 int32_t rs = rs0 + (int32_t)((int64_t)(rs1 - rs0) * ck_smooth(ck_prog(tm, 0, 180)) >> 8);
214 /* it rings: a quick wobble that dies away */
215 int64_t wob = (int64_t)ck_sin(tm * 65536u / 95u) * ck_exp_neg(tm * 256u / 260u) >> 16;
216 rs += (int32_t)(rs * wob * 7 / 100 >> 14);
217 uint32_t keep = 256 - ck_smooth(ck_prog(t, CK_NIGHT_MERGE_MS + 900, CK_NIGHT_GONE_MS));
218 n->hole[0] = (__typeof__(n->hole[0])){cx, cy, (int32_t)((int64_t)e * keep >> 8),
219 (int32_t)((int64_t)rs * (keep * keep >> 8) >> 8)};
220 n->holes = 1;
221 }
222 for (unsigned i = 0; i < n->holes; i++) {
223 int32_t reach = (n->hole[i].e * 44 / 10 >> 4) + 2;
224 if (n->hole[i].e <= 0 && n->hole[i].rs <= 0)
225 continue;
226 reach = ck_max(reach, (n->hole[i].rs * 16 / 10 >> 4) + 2);
227 ck_rect b = {(n->hole[i].x >> 4) - reach, (n->hole[i].y >> 4) - reach, 2 * reach + 1, 2 * reach + 1};
228 n->lens = ck_union(n->lens, b);
229 }
230 ck_rect all = {0, 0, n->w, n->h};
231 if (n->lens.w && !ck_clip(&n->lens, &all))
232 n->lens = (ck_rect){0, 0, 0, 0};
233 if (t >= CK_NIGHT_MERGE_MS && t < CK_NIGHT_MERGE_MS + CK_NIGHT_RIPPLE_MS) {
234 uint32_t tr = t - CK_NIGHT_MERGE_MS;
235 int32_t w = ck_clamp(S(60), 4, 200), reach = w * 22 / 10;
236 int64_t far = 0;
237 int32_t cs[4][2] = {{0, 0}, {n->w, 0}, {0, n->h}, {n->w, n->h}};
238 for (int i = 0; i < 4; i++) {
239 int64_t dx = cs[i][0] - n->cx, dy = cs[i][1] - n->cy, d = (int64_t)ck_isqrt((uint64_t)(dx * dx + dy * dy));
240 far = d > far ? d : far;
241 }
242 far += reach;
243 n->ripple_r = (int32_t)(far * tr / CK_NIGHT_RIPPLE_MS);
244 n->ripple_w = w;
245 uint32_t env = ck_smooth(ck_prog(tr, 0, 220)) *
246 (256 - ck_smooth(ck_prog(tr, CK_NIGHT_RIPPLE_MS * 6 / 10, CK_NIGHT_RIPPLE_MS))) >> 8;
247 int64_t amp = (int64_t)S(6) * 16 * (256 - 150 * n->ripple_r / far) >> 8;
248 amp = amp * env >> 8;
249 int32_t lambda = ck_max(S(110), 8);
250 for (int32_t i = 0; i <= 2 * reach && i < 1024; i++) {
251 int32_t o = i - reach;
252 int64_t s = ck_sin((uint32_t)((int64_t)o * 65536 / lambda));
253 n->ripple_disp[i] = (int32_t)((amp * s >> 14) * gauss(o, w) >> 16);
254 n->ripple_light[i] = (uint8_t)((uint64_t)gauss(o, w * 6 / 10) * env * 46 >> 24);
255 }
256 /* it stretches along the line the pair last lay on, and squeezes across it */
257 n->ripple_c2 = ck_cos(2 * last);
258 n->ripple_s2 = ck_sin(2 * last);
259 }
260}
261
262/* ---- drawing the base ---- */
263
264static uint32_t lerp16(uint32_t a, uint32_t b, uint32_t f) /* f: 0..16 */
265{
266 return ((((a & 0xff00ffu) * (16 - f) + (b & 0xff00ffu) * f) >> 4) & 0xff00ffu) |
267 ((((a & 0x00ff00u) * (16 - f) + (b & 0x00ff00u) * f) >> 4) & 0x00ff00u);
268}
269
270/* the sky at (sx, sy), 16ths of a pixel (pixel i's centre at 16 i + 8) */
271static uint32_t sample(const ck_night *n, int32_t sx, int32_t sy)
272{
273 sx = ck_clamp(sx - 8, 0, (n->w - 1) * 16);
274 sy = ck_clamp(sy - 8, 0, (n->h - 1) * 16);
275 int32_t ix = sx >> 4, iy = sy >> 4, ix1 = ck_min(ix + 1, n->w - 1), iy1 = ck_min(iy + 1, n->h - 1);
276 const uint32_t *r0 = n->sky + (size_t)iy * (size_t)n->w, *r1 = n->sky + (size_t)iy1 * (size_t)n->w;
277 uint32_t fx = (uint32_t)sx & 15u, fy = (uint32_t)sy & 15u;
278 return lerp16(lerp16(r0[ix], r0[ix1], fx), lerp16(r1[ix], r1[ix1], fx), fy);
279}
280
281/* the holes' pull on the light at (X, Y) (16ths of a pixel): added to (sx, sy), where the sky is read from */
282static void lens_at(const ck_night *n, int64_t X, int64_t Y, int64_t *sx, int64_t *sy)
283{
284 for (unsigned i = 0; i < n->holes; i++) {
285 int64_t e = n->hole[i].e, dx = X - n->hole[i].x, dy = Y - n->hole[i].y, d2 = dx * dx + dy * dy;
286 int64_t out = e * e * 1936 / 100, in = e * e * 484 / 100; /* (4.4 E)^2, (2.2 E)^2 */
287 if (e <= 0 || d2 >= out)
288 continue;
289 if (d2 < 64)
290 d2 = 64;
291 int64_t f = e * e * 65536 / d2;
292 if (d2 > in) {
293 int64_t w = (out - d2) * 65536 / (out - in);
294 f = f * (w * w >> 16) >> 16;
295 }
296 *sx -= dx * f >> 16;
297 *sy -= dy * f >> 16;
298 }
299}
300
301/* where (X, Y) (16ths of a pixel) looks in the sky: offset (ax, ay), and the ripple's own faint light */
302static void warp(const ck_night *n, int64_t X, int64_t Y, int32_t *ax, int32_t *ay, uint32_t *light)
303{
304 int64_t sx = 0, sy = 0;
305 *light = 0;
306 lens_at(n, X, Y, &sx, &sy);
307 if (n->ripple_w) {
308 int64_t dx = X - (n->cx * 16 + 8), dy = Y - (n->cy * 16 + 8), d2 = dx * dx + dy * dy;
309 int64_t r16 = (int64_t)ck_isqrt((uint64_t)d2), reach = n->ripple_w * 22 / 10;
310 int64_t i = (r16 >> 4) - n->ripple_r + reach;
311 if (r16 > 0 && i >= 0 && i <= 2 * reach && i < 1024) {
312 int64_t q = ((dx * dx - dy * dy) * n->ripple_c2 + 2 * dx * dy * n->ripple_s2) / d2; /* cos 2(theta - psi), Q14 */
313 int64_t m = (int64_t)n->ripple_disp[i] * (5734 + (q * 10650 >> 14)) >> 14;
314 sx -= m * dx / r16;
315 sy -= m * dy / r16;
316 *light = n->ripple_light[i];
317 }
318 }
319 *ax = (int32_t)sx;
320 *ay = (int32_t)sy;
321}
322
323static uint32_t finish(const ck_night *n, uint32_t c, int32_t x, int32_t y, uint32_t light)
324{
325 if (n->t < CK_NIGHT_FADE_END_MS)
326 c = fade_px(n, c, x, y);
327 return light ? ck_add(c, CK_RGB(86, 108, 176), light) : c;
328}
329
330/* a pixel close to a hole: the pull worked out exactly, then the shadow and its thin ring of light */
331static uint32_t near_px(const ck_night *n, int32_t x, int32_t y)
332{
333 int64_t X = (int64_t)x * 16 + 8, Y = (int64_t)y * 16 + 8;
334 int32_t ax, ay;
335 uint32_t light;
336 warp(n, X, Y, &ax, &ay, &light);
337 uint32_t c = finish(n, sample(n, (int32_t)X + ax, (int32_t)Y + ay), x, y, light);
338 for (unsigned i = 0; i < n->holes; i++) {
339 int64_t rs = n->hole[i].rs, dx = X - n->hole[i].x, dy = Y - n->hole[i].y, d2 = dx * dx + dy * dy;
340 if (rs <= 0 || d2 >= rs * rs * 9 / 4)
341 continue;
342 int32_t r = (int32_t)ck_isqrt((uint64_t)d2);
343 c = ck_mix(c, 0, ck_cover(r - (int32_t)rs));
344 int32_t ring = ck_abs(r - (int32_t)(rs * 104 / 100));
345 uint32_t a = ring < 22 ? (uint32_t)(22 - ring) * 6 : 0;
346 if (r > rs)
347 a += (uint32_t)((rs * 3 / 2 - r) * 46 / (rs / 2 + 1)) * (r < rs * 3 / 2);
348 c = ck_add(c, CK_RGB(255, 228, 196), a);
349 }
350 return c;
351}
352
353static int near_hole(const ck_night *n, int32_t x, int32_t y)
354{
355 int64_t X = (int64_t)x * 16 + 8, Y = (int64_t)y * 16 + 8;
356 for (unsigned i = 0; i < n->holes; i++) {
357 int64_t reach = ck_max(n->hole[i].e, n->hole[i].rs * 3 / 2) + 8 * 16;
358 int64_t dx = X - n->hole[i].x, dy = Y - n->hole[i].y;
359 if (dx * dx + dy * dy < reach * reach)
360 return 1;
361 }
362 return 0;
363}
364
365/* does the 8 x 8 block at (bx, by) meet the band the ripple bends? */
366static int in_ripple(const ck_night *n, int32_t bx, int32_t by)
367{
368 if (!n->ripple_w)
369 return 0;
370 int64_t reach = n->ripple_w * 22 / 10 + 1, lo = ck_max(0, n->ripple_r - (int32_t)reach), hi = n->ripple_r + reach;
371 int64_t nx = ck_clamp(n->cx, bx, bx + 8) - n->cx, ny = ck_clamp(n->cy, by, by + 8) - n->cy;
372 int64_t fx = ck_max(ck_abs(bx - n->cx), ck_abs(bx + 8 - n->cx)), fy = ck_max(ck_abs(by - n->cy), ck_abs(by + 8 - n->cy));
373 return nx * nx + ny * ny <= hi * hi && fx * fx + fy * fy >= lo * lo;
374}
375
376void ck_night_base(const ck_night *n, ck_canvas *out, const ck_rect *r)
377{
378 ck_rect d = *r, all = {0, 0, n->w, n->h};
379 if (!ck_clip(&d, &all))
380 return;
381 int fading = n->t < CK_NIGHT_FADE_END_MS;
382 for (int32_t y = d.y; y < d.y + d.h; y++) {
383 const uint32_t *src = n->sky + (size_t)y * (size_t)n->w;
384 uint32_t *dst = out->px + (size_t)y * (size_t)out->w;
385 if (fading)
386 for (int32_t x = d.x; x < d.x + d.w; x++)
387 dst[x] = fade_px(n, src[x], x, y);
388 else
389 for (int32_t x = d.x; x < d.x + d.w; x++)
390 dst[x] = src[x];
391 }
392 if (!n->lens.w && !n->ripple_w)
393 return;
394 /* the bent parts, in 8 x 8 blocks on a grid fixed to the screen, so any split of r draws the same */
395 for (int32_t by = d.y & ~7; by < d.y + d.h; by += 8)
396 for (int32_t bx = d.x & ~7; bx < d.x + d.w; bx += 8) {
397 ck_rect b = {bx, by, 8, 8};
398 int lens = n->lens.w && ck_meets(b, &n->lens);
399 if (!lens && !in_ripple(n, bx, by))
400 continue;
401 if (!ck_clip(&b, &d))
402 continue;
403 if (lens && near_hole(n, bx + 4, by + 4)) {
404 for (int32_t y = b.y; y < b.y + b.h; y++)
405 for (int32_t x = b.x; x < b.x + b.w; x++)
406 out->px[(size_t)y * (size_t)out->w + (size_t)x] = near_px(n, x, y);
407 continue;
408 }
409 int32_t ax[4], ay[4];
410 uint32_t li[4];
411 for (int c = 0; c < 4; c++)
412 warp(n, ((int64_t)bx + (c & 1) * 8) * 16, ((int64_t)by + (c >> 1) * 8) * 16, &ax[c], &ay[c], &li[c]);
413 for (int32_t y = b.y; y < b.y + b.h; y++) {
414 int32_t wy = 2 * (y - by) + 1;
415 for (int32_t x = b.x; x < b.x + b.w; x++) {
416 int32_t wx = 2 * (x - bx) + 1;
417 int32_t w00 = (16 - wx) * (16 - wy), w10 = wx * (16 - wy), w01 = (16 - wx) * wy, w11 = wx * wy;
418 int32_t sx = (ax[0] * w00 + ax[1] * w10 + ax[2] * w01 + ax[3] * w11) >> 8;
419 int32_t sy = (ay[0] * w00 + ay[1] * w10 + ay[2] * w01 + ay[3] * w11) >> 8;
420 uint32_t l = (uint32_t)((int32_t)li[0] * w00 + (int32_t)li[1] * w10 + (int32_t)li[2] * w01 +
421 (int32_t)li[3] * w11) >> 8;
422 uint32_t c = sample(n, x * 16 + 8 + sx, y * 16 + 8 + sy);
423 out->px[(size_t)y * (size_t)out->w + (size_t)x] = finish(n, c, x, y, l);
424 }
425 }
426 }
427}
428
429/* ---- the dusk: everything on the screen falls into one black hole ---- */
430
431size_t ck_night_picture_bytes(uint32_t w, uint32_t h)
432{
433 size_t n = 64 + (size_t)w * h * 4;
434 for (unsigned i = 1; i < CK_NIGHT_LEVELS; i++)
435 n += (size_t)(w >> i) * (size_t)(h >> i) * 4 * 2;
436 return n;
437}
438
439/* half the width and height of `from` (sw x sh) into to: the mean of each 2 x 2 */
440static void halve(const uint32_t *from, uint32_t sw, uint32_t sh, uint32_t *to)
441{
442 uint32_t dw = sw / 2, dh = sh / 2;
443 for (uint32_t y = 0; y < dh; y++) {
444 const uint32_t *r0 = from + (size_t)(2 * y) * sw, *r1 = r0 + sw;
445 uint32_t *d = to + (size_t)y * dw;
446 for (uint32_t x = 0; x < dw; x++) {
447 uint32_t a = r0[2 * x], b = r0[2 * x + 1], c = r1[2 * x], e = r1[2 * x + 1];
448 /* red and blue summed together, green on its own: the sums do not run into each other */
449 uint32_t rb = (((a & 0xff00ffu) + (b & 0xff00ffu) + (c & 0xff00ffu) + (e & 0xff00ffu)) >> 2) & 0xff00ffu;
450 uint32_t g = (((a & 0xff00u) + (b & 0xff00u) + (c & 0xff00u) + (e & 0xff00u)) >> 2) & 0xff00u;
451 d[x] = rb | g;
452 }
453 }
454}
455
456void ck_night_picture_take(const ck_night *n, const uint32_t *from, void *mem, ck_night_picture *kept)
457{
458 uint32_t w = (uint32_t)n->w, h = (uint32_t)n->h, *p = mem;
459 size_t count = (size_t)w * h;
460 for (size_t i = 0; i < count; i++)
461 p[i] = from[i];
462 kept->level[0] = p;
463 kept->sky[0] = n->sky;
464 p += count;
465 for (unsigned l = 1; l < CK_NIGHT_LEVELS; l++) {
466 uint32_t sw = w >> (l - 1), sh = h >> (l - 1);
467 halve(kept->level[l - 1], sw, sh, p);
468 kept->level[l] = p;
469 p += (size_t)(sw / 2) * (sh / 2);
470 halve(kept->sky[l - 1], sw, sh, p);
471 kept->sky[l] = p;
472 p += (size_t)(sw / 2) * (sh / 2);
473 }
474}
475
476void ck_night_fall(ck_night *n, const ck_night_dusk *d)
477{
478 n->falling = 1;
479 n->dusk = *d;
480 int32_t cx = n->cx * 16 + 8, cy = n->cy * 16 + 8, rs = d->rs;
481 n->hole[0] = (__typeof__(n->hole[0])){cx, cy, d->e, rs};
482 n->holes = 1;
483 n->ripple_w = 0;
484 n->lens = (ck_rect){0, 0, n->w, n->h};
485 /* The disk and the ring, by distance from the hole, in 1024 steps out to 3.3 shadow radii. The disk is
486 * dark inside 1.15 radii, brightest at 1.5 and dies away beyond; its colour runs from warm white at the
487 * inner edge through gold to the blue of the sky's glow. The photon ring is a thin bright line at 1.06
488 * radii, a pixel and a quarter each way (wider on a big screen). */
489 n->disk_far = rs * 33 / 10 + 16;
490 int32_t in = rs * 115 / 100, peak = rs * 150 / 100, ring = rs * 106 / 100, hw = 20 + rs / 64;
491 for (unsigned i = 0; i < 1024; i++) {
492 int32_t r = (int32_t)((int64_t)n->disk_far * (int64_t)i / 1024);
493 uint32_t a = 0, c = CK_RGB(255, 222, 180);
494 if (r >= in && peak > in) {
495 if (r < peak) {
496 uint32_t u = (uint32_t)((int64_t)(r - in) * 256 / (peak - in));
497 a = ck_smooth((int32_t)u) * 220 >> 8;
498 c = ck_mix(CK_RGB(255, 222, 180), CK_RGB(255, 212, 150), u);
499 } else {
500 uint32_t v = (uint32_t)((int64_t)(r - peak) * 256 / (rs / 2 + 1)); /* half radii past the peak, 256 each */
501 a = (ck_exp_neg(v) >> 8) * 220 >> 8;
502 c = ck_mix(CK_RGB(255, 212, 150), CK_RGB(150, 170, 255), ck_smooth((int32_t)(v * 256 / 360)));
503 }
504 }
505 int32_t off = ck_abs(r - ring);
506 if (off < hw) {
507 uint32_t ar = (uint32_t)(hw - off) * 255 / (uint32_t)hw;
508 ar = ar * ar >> 8;
509 c = a + ar ? ck_mix(c, CK_RGB(255, 244, 228), ar * 256 / (a + ar)) : c;
510 a += ar;
511 }
512 a = a * d->disk >> 8;
513 n->disk_a[i] = (uint8_t)(a > 255 ? 255 : a);
514 n->disk_c[i] = c;
515 }
516}
517
518/* a corner of the 8 x 8 grid during the dusk: where the picture is read from, and the distance to the hole */
519struct corner {
520 int32_t ax, ay; /* offset to the picture point shown here, 16ths of a pixel */
521 int32_t sx, sy; /* that point itself, 16ths (for telling whether a block shows only the dark beyond the edge) */
522 uint32_t crowd; /* how much brighter the crowded light is drawn, 256 = as it is */
523 uint32_t h; /* log2 of the crowding of the area, Q16 */
524 int32_t r; /* distance from the hole, 16ths of a pixel */
525 int32_t inv; /* 2^28 / r */
526 uint32_t vig; /* how much of the light the edges of the screen keep here, 0..256 */
527};
528
529static void corner_at(const ck_night *n, int32_t x, int32_t y, struct corner *c)
530{
531 const ck_night_dusk *d = &n->dusk;
532 int64_t X = (int64_t)x * 16, Y = (int64_t)y * 16, dx = X - n->hole[0].x, dy = Y - n->hole[0].y;
533 int64_t d2 = dx * dx + dy * dy;
534 if (d2 < 256)
535 d2 = 256;
536 uint32_t l0 = ck_log2_q16((uint64_t)d2), l1 = ck_log2_q16((uint64_t)(d2 + d->fallen)), h = l1 - l0;
537 c->h = h;
538 uint64_t crowd = pow2_q16(h) >> 8;
539 c->crowd = crowd > d->crowd ? d->crowd : (uint32_t)crowd;
540 /* the picture point: the same direction turned by swirl per doubling, at the stretched distance */
541 int64_t r = (int64_t)(pow2_q16(l0 >> 1) >> 16); /* the distance, 16ths, without a square root */
542 uint32_t ang = (uint32_t)((uint64_t)d->swirl * (h >> 1) >> 16);
543 ang = (uint32_t)((int64_t)ang * d->calm / (d->calm + r));
544 int64_t stretch = (int64_t)pow2_q16(h >> 1), cs = ck_cos(ang), sn = ck_sin(ang);
545 int64_t ux = (dx * cs - dy * sn) >> 14, uy = (dx * sn + dy * cs) >> 14;
546 int64_t sx = (ux * stretch >> 16) - dx, sy = (uy * stretch >> 16) - dy;
547 lens_at(n, X, Y, &sx, &sy);
548 /* far beyond the picture is still far beyond it: held where the block arithmetic stays in 32 bits */
549 sx = sx > (1 << 22) ? (1 << 22) : sx < -(1 << 22) ? -(1 << 22) : sx;
550 sy = sy > (1 << 22) ? (1 << 22) : sy < -(1 << 22) ? -(1 << 22) : sy;
551 c->ax = (int32_t)sx;
552 c->ay = (int32_t)sy;
553 c->sx = (int32_t)X + c->ax;
554 c->sy = (int32_t)Y + c->ay;
555 c->r = (int32_t)r;
556 c->inv = (int32_t)((1 << 28) / (r > 0 ? r : 1));
557 /* the edges go dark first: from `near` out to `far` the light falls off smoothly by up to `shade` */
558 c->vig = 256 - (d->shade * ck_smooth(ck_prog((uint32_t)r, (uint32_t)d->near, (uint32_t)d->far)) >> 8);
559}
560
561/* the kept picture at (sx, sy) (16ths of a pixel of copy l, w x h): the picture itself inside its edges, the
562 * sky mirrored in them beyond (so the stars flow in from past the screen), black further out than that. */
563static uint32_t sample_kept(const ck_night_picture *kept, unsigned l, int32_t w, int32_t h, int32_t sx, int32_t sy)
564{
565 const uint32_t *px = kept->level[l];
566 int32_t W = (w - 1) * 16, H = (h - 1) * 16;
567 sx -= 8, sy -= 8;
568 if (sx < 0 || sy < 0 || sx >= W || sy >= H) {
569 sx = sx < 0 ? -sx : sx >= W ? 2 * W - sx : sx;
570 sy = sy < 0 ? -sy : sy >= H ? 2 * H - sy : sy;
571 if (sx < 0 || sy < 0 || sx >= W || sy >= H)
572 return 0;
573 px = kept->sky[l];
574 }
575 int32_t ix = sx >> 4, iy = sy >> 4;
576 const uint32_t *r0 = px + (size_t)iy * (size_t)w, *r1 = r0 + w;
577 uint32_t fx = (uint32_t)sx & 15u, fy = (uint32_t)sy & 15u;
578 return lerp16(lerp16(r0[ix], r0[ix + 1], fx), lerp16(r1[ix], r1[ix + 1], fx), fy);
579}
580
581/* c brightened g/256 times (g above 256), the excess warmer than the light itself */
582static uint32_t brighten(uint32_t c, uint32_t g)
583{
584 if (g <= 256 || !c)
585 return c;
586 uint32_t x = g - 256;
587 uint32_t r = ck_ch(c, 16) * (256 + x) >> 8, gg = ck_ch(c, 8) * (256 + x * 7 / 8) >> 8, b = ck_ch(c, 0) * (256 + x * 5 / 8) >> 8;
588 return (r > 255 ? 255u : r) << 16 | (gg > 255 ? 255u : gg) << 8 | (b > 255 ? 255u : b);
589}
590
591/* One 8 x 8 block of the dusk, clipped to d, from its four corners. Near the hole every pixel is worked out;
592 * further out, where the picture is streaming past, the block is worked out two pixels at a time each way
593 * (the screen does not show the difference there and the frame costs a third as much). */
594static void fall_block(const ck_night *n, const ck_night_picture *kept, ck_canvas *out, const ck_rect *d, int32_t bx,
595 int32_t by, const struct corner *c00, const struct corner *c10, const struct corner *c01,
596 const struct corner *c11)
597{
598 ck_rect b = {bx, by, 8, 8};
599 if (!ck_clip(&b, d))
600 return;
601 const ck_night_dusk *k = &n->dusk;
602 int32_t rs = k->rs, hx = n->hole[0].x, hy = n->hole[0].y;
603 int32_t rmin = ck_min(ck_min(c00->r, c10->r), ck_min(c01->r, c11->r)) - 182; /* the block's diagonal: 8 px * sqrt 2 */
604 int32_t rmax = ck_max(ck_max(c00->r, c10->r), ck_max(c01->r, c11->r));
605 int32_t W16 = n->w * 32 + 16, H16 = n->h * 32 + 16, X16 = -n->w * 16 - 16, Y16 = -n->h * 16 - 16;
606 /* all four corners read from the dark beyond the same edge of the mirrored sky: so does everything between */
607 int beyond = (c00->sx < X16 && c10->sx < X16 && c01->sx < X16 && c11->sx < X16) ||
608 (c00->sy < Y16 && c10->sy < Y16 && c01->sy < Y16 && c11->sy < Y16) ||
609 (c00->sx > W16 && c10->sx > W16 && c01->sx > W16 && c11->sx > W16) ||
610 (c00->sy > H16 && c10->sy > H16 && c01->sy > H16 && c11->sy > H16);
611 int disk = k->disk && rmin < n->disk_far, edge = rmin < rs + 16, fine = rmin < k->fine;
612 /* the light kept here: the closing of the horizon, and the edges of the screen going dark first */
613 uint32_t keep = k->keep * ((c00->vig + c10->vig + c01->vig + c11->vig) >> 2) >> 8;
614 if ((!keep && !disk) || rmax < rs - 16 || (beyond && !disk)) { /* only the dark: inside the shadow, or past the edge */
615 for (int32_t y = b.y; y < b.y + b.h; y++)
616 for (int32_t x = b.x; x < b.x + b.w; x++)
617 out->px[(size_t)y * (size_t)out->w + (size_t)x] = 0;
618 return;
619 }
620 /* which copy of the picture: the one whose pixels are about as big as the crowding here, leaning to the
621 * smaller copy (the picture is streaming, and the smaller copies are read far faster); once the light is
622 * nearly gone, the copies at an eighth and below, which no eye can tell apart at that brightness */
623 uint32_t hm = (c00->h >> 2) + (c10->h >> 2) + (c01->h >> 2) + (c11->h >> 2), lvl = (hm + 98304u) >> 17;
624 lvl = keep < 16 && lvl < 3 ? 3 : lvl;
625 lvl = lvl >= CK_NIGHT_LEVELS ? CK_NIGHT_LEVELS - 1 : lvl;
626 int32_t lw = n->w >> lvl, lh = n->h >> lvl;
627 int crowded = c00->crowd > 256 || c10->crowd > 256 || c01->crowd > 256 || c11->crowd > 256;
628 int64_t cb = ck_cos(k->beam), sb = ck_sin(k->beam);
629 uint32_t inv_far = (uint32_t)((1u << 30) / (uint32_t)(n->disk_far > 0 ? n->disk_far : 1));
630 if (!fine) { /* pairs start on even pixels, so a block cut by the clip still draws the same pixels */
631 ck_rect e = {b.x & ~1, b.y & ~1, 0, 0};
632 e.w = ((b.x + b.w + 1) & ~1) - e.x, e.h = ((b.y + b.h + 1) & ~1) - e.y;
633 if (e.x >= d->x && e.y >= d->y && e.x + e.w <= d->x + d->w && e.y + e.h <= d->y + d->h)
634 b = e;
635 else
636 fine = 1; /* an odd clip: every pixel, so nothing is written outside it */
637 }
638 int32_t step = fine ? 1 : 2;
639 for (int32_t y = b.y; y < b.y + b.h; y += step) {
640 int32_t wy = 2 * (y - by) + step;
641 uint32_t *row = out->px + (size_t)y * (size_t)out->w;
642 for (int32_t x = b.x; x < b.x + b.w; x += step) {
643 int32_t wx = 2 * (x - bx) + step;
644 int32_t w00 = (16 - wx) * (16 - wy), w10 = wx * (16 - wy), w01 = (16 - wx) * wy, w11 = wx * wy;
645 uint32_t c = 0;
646 if (!beyond) {
647 int32_t ax = (c00->ax * w00 + c10->ax * w10 + c01->ax * w01 + c11->ax * w11) >> 8;
648 int32_t ay = (c00->ay * w00 + c10->ay * w10 + c01->ay * w01 + c11->ay * w11) >> 8;
649 c = sample_kept(kept, lvl, lw, lh, (x * 16 + 8 + ax) >> lvl, (y * 16 + 8 + ay) >> lvl);
650 if (c && crowded) {
651 uint32_t g = (uint32_t)((int32_t)c00->crowd * w00 + (int32_t)c10->crowd * w10 +
652 (int32_t)c01->crowd * w01 + (int32_t)c11->crowd * w11) >> 8;
653 c = brighten(c, g);
654 }
655 if (keep < 256) /* the picture goes before the disk does */
656 c = ck_scale(c, keep);
657 }
658 if (disk || edge) {
659 int32_t r = (c00->r * w00 + c10->r * w10 + c01->r * w01 + c11->r * w11) >> 8;
660 if (disk && r < n->disk_far) {
661 uint32_t i = (uint32_t)r * inv_far >> 20, a = n->disk_a[i < 1024 ? i : 1023];
662 if (a) {
663 /* brighter on the side turning toward the eye: by the cosine of the angle from the beam */
664 int64_t inv = ((int64_t)c00->inv * w00 + (int64_t)c10->inv * w10 + (int64_t)c01->inv * w01 +
665 (int64_t)c11->inv * w11) >> 8;
666 int64_t dot = ((int64_t)(x * 16 + 8 - hx) * cb + (int64_t)(y * 16 + 8 - hy) * sb) >> 14;
667 int64_t cosb = dot * inv >> 14; /* Q14 */
668 a = (uint32_t)((int64_t)a * (16384 + (cosb * 110 >> 8)) >> 14);
669 c = ck_add(c, n->disk_c[i < 1024 ? i : 1023], a > 255 ? 255 : a);
670 }
671 }
672 if (edge)
673 c = ck_mix(c, 0, ck_cover(r - rs));
674 }
675 row[x] = c;
676 if (!fine)
677 row[x + 1] = c, row[x + out->w] = c, row[x + out->w + 1] = c;
678 }
679 }
680}
681
682#define CHUNK 32 /* blocks of a row worked at once (their corners shared); 2.4 KiB of stack for the two rows */
683
684void ck_night_base_from(const ck_night *n, const ck_night_picture *kept, ck_canvas *out, const ck_rect *r)
685{
686 ck_rect d = *r, all = {0, 0, n->w, n->h};
687 if (!ck_clip(&d, &all))
688 return;
689 if (!n->falling) {
690 ck_night_base(n, out, &d);
691 return;
692 }
693 struct corner rows[2][CHUNK + 1];
694 int32_t bx0 = d.x & ~7, bx1 = d.x + d.w, by0 = d.y & ~7, by1 = d.y + d.h;
695 for (int32_t cx0 = bx0; cx0 < bx1; cx0 += CHUNK * 8) {
696 int32_t ncol = ck_min(CHUNK, (bx1 - cx0 + 7) / 8);
697 struct corner *top = rows[0], *bottom = rows[1];
698 for (int32_t i = 0; i <= ncol; i++)
699 corner_at(n, cx0 + 8 * i, by0, &top[i]);
700 for (int32_t by = by0; by < by1; by += 8) {
701 for (int32_t i = 0; i <= ncol; i++)
702 corner_at(n, cx0 + 8 * i, by + 8, &bottom[i]);
703 for (int32_t i = 0; i < ncol; i++)
704 fall_block(n, kept, out, &d, cx0 + 8 * i, by, &top[i], &top[i + 1], &bottom[i], &bottom[i + 1]);
705 struct corner *t = top;
706 top = bottom, bottom = t;
707 }
708 }
709}
710
711/* ---- the lights on top ---- */
712
713ck_rect ck_night_star_box(const ck_night *n, unsigned i)
714{
715 const ck_night_star *s = &n->tw[i];
716 return (ck_rect){(s->x - s->r) >> 4, (s->y - s->r) >> 4, (2 * s->r >> 4) + 2, (2 * s->r >> 4) + 2};
717}
718
719void ck_night_lights(const ck_night *n, ck_canvas *out, const ck_rect *r, uint32_t shimmer, uint32_t voice)
720{
721 for (unsigned i = 0; i < n->ntw; i++) {
722 const ck_night_star *s = &n->tw[i];
723 ck_rect b = ck_night_star_box(n, i);
724 if (!ck_meets(b, r) || (n->lens.w && ck_meets(b, &n->lens)))
725 continue;
726 uint32_t tw = (uint32_t)(16384 + ck_sin((n->t + s->phase) % s->period * 65536u / s->period)) >> 7;
727 uint32_t a = (tw * tw >> 8) * 120 >> 8;
728 a = a * ck_night_faded(n, s->x >> 4, s->y >> 4, 255) >> 8;
729 ck_glow(out, r, s->x, s->y, s->r, s->color, a);
730 }
731 if (!shimmer && !voice)
732 return;
733 for (unsigned i = 0; i < CK_NIGHT_GALAXIES; i++) {
734 const ck_night_galaxy *g = &n->gal[i];
735 ck_rect b = g->box;
736 if (!g->box.w || !ck_clip(&b, r))
737 continue;
738 uint32_t sweep = n->t % g->period * 256u / g->period;
739 for (int32_t y = b.y; y < b.y + b.h; y++) {
740 const uint8_t *lp = g->light + (size_t)(y - g->box.y) * (size_t)g->box.w - (size_t)g->box.x;
741 const uint8_t *pp = g->phase + (size_t)(y - g->box.y) * (size_t)g->box.w - (size_t)g->box.x;
742 uint32_t *row = out->px + (size_t)y * (size_t)out->w;
743 for (int32_t x = b.x; x < b.x + b.w; x++) {
744 uint32_t l = lp[x];
745 if (!l)
746 continue;
747 uint32_t a = (l * band_t[(pp[x] - sweep) & 255u] >> 8) * shimmer >> 8;
748 a += (l * l >> 8) * voice >> 9;
749 if (a)
750 row[x] = ck_add(row[x], g->tint, a);
751 }
752 }
753 }
754}
755/* ---- rebuilding the sky for a new view ---- */
756
757/* Resets the sky background and redraws it with the new view v. The sky's
758 * glow (dark sky background) is written first, then the Milky Way and the
759 * catalogue stars on top of it. Galaxy bounds and maps are updated too.
760 * scratch: at least n->w * n->h * 4 bytes of temporary workspace. */
761void ck_night_view(ck_night *n, const ck_sky_view *v, void *scratch)
762{
763 n->view = *v;
764 int32_t qw = (int32_t)(n->w / 4 + 2), qh = (int32_t)(n->h / 4 + 2);
765 /* clear the sky to the dark-sky glow (the same glow set in the original
766 * sky grid; bilinear_u8 of the empty dark grid gives the glow level) */
767 ck_canvas cv = {n->sky, n->w, n->h};
768 ck_sky_milkyway_q(scratch, qw, qh, n->w, n->cx, n->cy, &n->view);
769 scale_up(n, scratch, qw);
770 /* redraw stars over the new Milky Way */
771 ck_sky_draw_stars(&cv, n->k, n->cx, n->cy, &n->view, n->tw, CK_NIGHT_TWINKLES, &n->ntw);
772 /* recompute galaxy boxes (some may enter or leave the view) */
773 uint8_t *p = (uint8_t *)n->sky + (size_t)n->w * n->h * 4;
774 p += (size_t)(n->w / 4 + 1) * (n->h / 4 + 1);
775 for (unsigned i = 0; i < CK_NIGHT_GALAXIES; i++) {
776 ck_night_galaxy *g = &n->gal[i];
777 g->box = ck_sky_galaxy_box((uint32_t)n->w, (uint32_t)n->h, n->cx, n->cy, &n->view, i);
778 ck_sky_galaxy_maps(g, i, (uint32_t)n->w, n->cx, n->cy, &n->view);
779 }
780}