aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / allen-scene / native / night.h
| 1 | /* night.h -- the night sky AIENOS opens on, and the two black holes that |
| 2 | * merge in it. |
| 3 | * |
| 4 | * The sky is made once, into RAM, and it is the real sky (ui/sky_real.h): |
| 5 | * by default the piece of it GW150914, the first black-hole merger ever |
| 6 | * heard, came from; the Milky Way from a photograph, the catalogue's stars |
| 7 | * to the naked-eye limit at their true places, brightness and colour, and |
| 8 | * the galaxies in the view the eye can see (there: the Large and Small |
| 9 | * Magellanic Clouds). Each galaxy also keeps, per pixel, how bright it is |
| 10 | * and where along its long axis that pixel sits, so a band of light can |
| 11 | * later sweep along it (while Allen answers) without the galaxy itself |
| 12 | * changing. |
| 13 | * |
| 14 | * The startup, on the clock from ck_night_init (ms): |
| 15 | * 0 to CK_NIGHT_FADE_END_MS the eyes adjust to the dark: the brightest |
| 16 | * stars first, then the fainter ones, then the |
| 17 | * Milky Way, in slow patches across the sky. |
| 18 | * CK_NIGHT_HOLES_MS two black holes start to bend the starlight |
| 19 | * (gravitational lensing: stars behind them are |
| 20 | * pulled into arcs), then show their shadows, |
| 21 | * circling on a tilted orbit; |
| 22 | * CK_NIGHT_SPIRAL_MS they spiral in, faster and closer, the way a |
| 23 | * real pair does (separation as t^(1/4), angle |
| 24 | * as t^(5/8), t the time left); |
| 25 | * CK_NIGHT_MERGE_MS they merge; a ripple (the gravitational wave, |
| 26 | * stretching one way and squeezing the other) |
| 27 | * runs out through the stars; the one black |
| 28 | * hole left rings, then dissolves by |
| 29 | * CK_NIGHT_GONE_MS. |
| 30 | * |
| 31 | * The dusk (ck_night_fall): when Allen goes to sleep, one black hole opens |
| 32 | * where the pair merged and everything on the screen falls into it. The |
| 33 | * caller keeps the picture as it was (ck_night_picture_take) and the night |
| 34 | * draws it pulled in: a pixel at distance r from the hole shows what was at |
| 35 | * sqrt(r^2 + fallen), turned round the hole a set part of a turn for every |
| 36 | * doubling of that distance; so the picture streams inward, fastest near the |
| 37 | * hole, whipping round as it goes, and the light that crowds in there is |
| 38 | * drawn brighter and warmer. Round the shadow an accretion disk glows, warm |
| 39 | * white at its inner edge through gold and blue to the dark, brighter on the |
| 40 | * side turning toward the eye, with a thin bright photon ring just outside |
| 41 | * the shadow; then the whole picture is dimmed as the horizon closes. |
| 42 | * |
| 43 | * Every pixel is a pure function of its position and the time, so a frame |
| 44 | * redraws only the regions that changed, in any order. Integer only, |
| 45 | * freestanding, no heap. Host tested in ui/tests/allen_ui_test.c. */ |
| 46 | #ifndef AIENOS_NIGHT_H |
| 47 | #define AIENOS_NIGHT_H |
| 48 | #include <stddef.h> |
| 49 | #include <stdint.h> |
| 50 | |
| 51 | #include "paint.h" |
| 52 | #include "sky_data.h" |
| 53 | |
| 54 | #define CK_NIGHT_FADE_END_MS 6800u |
| 55 | #define CK_NIGHT_HOLES_MS 2600u |
| 56 | #define CK_NIGHT_SPIRAL_MS 6000u |
| 57 | #define CK_NIGHT_MERGE_MS 12000u |
| 58 | #define CK_NIGHT_RIPPLE_MS 3600u /* the ripple's run across the screen */ |
| 59 | #define CK_NIGHT_GONE_MS 15200u /* the last black hole has dissolved */ |
| 60 | |
| 61 | #define CK_NIGHT_GALAXIES 6u |
| 62 | #define CK_NIGHT_TWINKLES 256u |
| 63 | #define CK_NIGHT_LEVELS 6u /* a kept picture and its copies at 1/2, 1/4, down to 1/32 size */ |
| 64 | #define CK_NIGHT_W_MAX 8192u /* the widest screen the dusk can draw (one row of 8-pixel blocks at a time) */ |
| 65 | |
| 66 | /* How the dusk stands at a frame; see ck_night_fall. All distances in 16ths of a pixel. */ |
| 67 | typedef struct { |
| 68 | int64_t fallen; /* a pixel at distance r from the hole shows what was at sqrt(r^2 + fallen) */ |
| 69 | uint32_t swirl; /* 65536ths of a turn the picture is turned for each doubling of that distance, close to the hole */ |
| 70 | int32_t calm; /* the turning is weaker further out: by calm / (calm + r) */ |
| 71 | uint32_t crowd; /* the most the crowded light near the hole is brightened: 256 not at all, 1024 four times */ |
| 72 | int32_t e, rs; /* the hole: Einstein radius and shadow radius */ |
| 73 | uint32_t disk; /* how bright the accretion disk and the photon ring are, 0..256 */ |
| 74 | uint32_t beam; /* which way the disk's bright side faces (the side turning toward the eye), 65536ths of a turn */ |
| 75 | uint32_t keep; /* how much of the picture's light is kept, 0..256; the horizon closing takes it to 0 */ |
| 76 | int32_t near, far; /* the edges of the screen go dark first: the light falls off from near out to far ... */ |
| 77 | uint32_t shade; /* ... by this much at far, 0..256 */ |
| 78 | int32_t fine; /* inside this distance every pixel is worked out; beyond it two at a time each way */ |
| 79 | } ck_night_dusk; |
| 80 | |
| 81 | typedef struct { |
| 82 | ck_rect box; |
| 83 | uint8_t *light, *phase; /* box.w * box.h each */ |
| 84 | uint32_t tint; /* the colour of the light that sweeps it */ |
| 85 | uint16_t period; /* ms for the sweep to come round */ |
| 86 | } ck_night_galaxy; |
| 87 | |
| 88 | typedef struct { |
| 89 | int32_t x, y, r; /* 16ths of a pixel */ |
| 90 | uint32_t color; |
| 91 | uint16_t period, phase; /* ms */ |
| 92 | } ck_night_star; |
| 93 | |
| 94 | typedef struct { |
| 95 | int32_t w, h; |
| 96 | uint32_t k; /* 1/256 px per design px (1920 x 1080 design) */ |
| 97 | int32_t cx, cy; /* where the black holes merge, px */ |
| 98 | uint32_t *sky; /* the night, w * h */ |
| 99 | uint8_t *appear; /* when each 4 x 4 cell comes out of the dark (0..255) */ |
| 100 | int32_t aw; |
| 101 | ck_night_galaxy gal[CK_NIGHT_GALAXIES]; |
| 102 | ck_night_star tw[CK_NIGHT_TWINKLES]; |
| 103 | unsigned ntw; |
| 104 | /* state at the time ck_night_at was last given */ |
| 105 | uint32_t t; |
| 106 | unsigned holes; |
| 107 | struct { |
| 108 | int32_t x, y, e, rs; /* 16ths of a pixel: centre, Einstein radius, shadow radius */ |
| 109 | } hole[2]; |
| 110 | ck_rect lens; /* where the light is bent (empty: nowhere) */ |
| 111 | int32_t ripple_r, ripple_w; /* px; ripple_w 0: no ripple */ |
| 112 | int32_t ripple_disp[1024]; /* 16ths of a pixel, by distance from the crest */ |
| 113 | uint8_t ripple_light[1024]; |
| 114 | int32_t ripple_c2, ripple_s2; /* the stretch direction, Q14 */ |
| 115 | /* the dusk (ck_night_fall); falling 0: none */ |
| 116 | int falling; |
| 117 | ck_night_dusk dusk; |
| 118 | uint8_t disk_a[1024]; /* the disk's light by distance from the hole, 0..255 */ |
| 119 | uint32_t disk_c[1024]; /* and its colour there */ |
| 120 | int32_t disk_far; /* the disk reaches this far, 16ths of a pixel */ |
| 121 | ck_sky_view view; /* the current sky view (set by ck_night_init, updated by ck_night_view) */ |
| 122 | } ck_night; |
| 123 | |
| 124 | /* The picture kept for the dusk: the screen as it was, with smaller copies of it and of the bare sky. |
| 125 | * Beyond the picture's edges the dusk shows the sky mirrored, so the stars seem to come from past the |
| 126 | * screen and no edge is seen falling in. */ |
| 127 | typedef struct { |
| 128 | const uint32_t *level[CK_NIGHT_LEVELS]; /* the picture, then it at 1/2, 1/4 ... the width and height */ |
| 129 | const uint32_t *sky[CK_NIGHT_LEVELS]; /* the bare sky the same way (sky[0] is the night's own) */ |
| 130 | } ck_night_picture; |
| 131 | /* Memory for a w x h picture kept this way. */ |
| 132 | size_t ck_night_picture_bytes(uint32_t w, uint32_t h); |
| 133 | /* Copies the w x h picture `from` (rows w apart) into mem (ck_night_picture_bytes) and makes the smaller |
| 134 | * copies of it and of n's sky, each a quarter of the one before by averaging 2 x 2 pixels. */ |
| 135 | void ck_night_picture_take(const ck_night *n, const uint32_t *from, void *mem, ck_night_picture *kept); |
| 136 | |
| 137 | /* Memory for a w x h night at scale k. */ |
| 138 | size_t ck_night_mem_bytes(uint32_t w, uint32_t h, uint32_t k); |
| 139 | /* Makes the sky into mem; scratch: at least w * h * 4 bytes of working |
| 140 | * memory, overwritten. (cx, cy): where the holes merge. 0, or -1. */ |
| 141 | int 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, |
| 142 | void *scratch); |
| 143 | /* Rebuilds the sky for a new view (changing sky mode or location). |
| 144 | * scratch: at least w * h * 4 bytes (same buffer as ck_night_init). */ |
| 145 | void ck_night_view(ck_night *n, const ck_sky_view *v, void *scratch); |
| 146 | /* Moves the night to t ms: the holes, the lens region, the ripple. Ends any dusk. */ |
| 147 | void ck_night_at(ck_night *n, uint32_t t); |
| 148 | /* The dusk, after ck_night_at: one black hole at (cx, cy) with everything falling into it as d says. |
| 149 | * The picture is then drawn with ck_night_base_from. */ |
| 150 | void ck_night_fall(ck_night *n, const ck_night_dusk *d); |
| 151 | /* The sky as seen at the current time (faded in, bent, rippled) into the |
| 152 | * rectangle r of out. */ |
| 153 | void ck_night_base(const ck_night *n, ck_canvas *out, const ck_rect *r); |
| 154 | /* The kept picture (ck_night_picture_take) as the dusk shows it now: pulled into the hole, the shadow, |
| 155 | * the ring and the disk on it, the whole dimmed by keep; into the rectangle r of out. */ |
| 156 | void ck_night_base_from(const ck_night *n, const ck_night_picture *kept, ck_canvas *out, const ck_rect *r); |
| 157 | /* Adds the twinkling and, with shimmer > 0 (0..256), the light sweeping the |
| 158 | * galaxies; voice (0..256) brightens their cores. */ |
| 159 | void ck_night_lights(const ck_night *n, ck_canvas *out, const ck_rect *r, uint32_t shimmer, uint32_t voice); |
| 160 | /* The rectangle a twinkling star needs redrawn. */ |
| 161 | ck_rect ck_night_star_box(const ck_night *n, unsigned i); |
| 162 | /* How far the sky has come out of the dark at (x, y) for light of level l |
| 163 | * (0..255): 0..256. */ |
| 164 | uint32_t ck_night_faded(const ck_night *n, int32_t x, int32_t y, uint32_t l); |
| 165 | #endif |