raytrace2.h

C source for Real-time ray tracing on a round ESP32 display, 60 lines. Raw file

#pragma once

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define RT2_W 466
#define RT2_H 466
#define RT2_CELL 8
#define RT2_GW ((RT2_W + RT2_CELL - 1) / RT2_CELL)
#define RT2_GH ((RT2_H + RT2_CELL - 1) / RT2_CELL)
#define RT2_CW (RT2_GW + 1)
#define RT2_CH (RT2_GH + 1)
#define RT2_NS 4

#define RT2_HIT_NONE 0
#define RT2_HIT_GROUND 1
#define RT2_HIT_SPHERE 2

#define RT2_SHADOW_P 1u
#define RT2_SHADOW_R 2u
#define RT2_FLUSH_MAX 8

typedef struct {
    int16_t x0, y0, x1, y1;
} rt2_flush_t;

typedef struct {
    uint16_t *pixels;
    uint16_t *bg; /* prerendered floor+sky, no shadows */
    void *corners; /* corner_t[RT2_CW * RT2_CH], PSRAM */
    uint16_t *vis;
    uint16_t *cvis;
    int vis_n;
    int cvis_n;
    rt2_flush_t flush[RT2_FLUSH_MAX];
    int flush_n;
    uint32_t frames;
    uint32_t fps;
    uint32_t fps_acc;
    int64_t fps_t0;
    float yaw;
    float pitch;
    int view_dirty;
    int show_boxes;
    int force_flush;
} raytrace2_t;

int raytrace2_init(raytrace2_t *t);
void raytrace2_free(raytrace2_t *t);
void raytrace2_orbit(raytrace2_t *t, float dx, float dy);
void raytrace2_toggle_boxes(raytrace2_t *t);
void raytrace2_frame(raytrace2_t *t);
void raytrace2_draw_fps(raytrace2_t *t);

#ifdef __cplusplus
}
#endif