| |
| |
| struct scene_t { uint16_t frame; }; |
| struct full_state_t { scene_t scene; }; |
|
|
| #define INLINE inline |
|
|
| #define PIPELINEC_SUGAR |
| #define FABS(x) float_abs(x) |
| #define RSQRT(x) inversesqrt(x) |
| #define SQRT(x) sqrt(x) |
|
|
| struct color_t { |
| float r; |
| float g; |
| float b; |
| }; |
|
|
| struct vec_t { |
| float x; |
| float y; |
| float z; |
| }; |
|
|
| struct plane_t { |
| vec_t origin; |
| vec_t normal; |
| }; |
|
|
| struct ray_t { |
| vec_t origin; |
| vec_t direction; |
| }; |
|
|
| struct sphere_t { |
| vec_t center; |
| float radius; |
| }; |
|
|
|
|
|
|
| INLINE float fmuladd(float a, float b, float c) |
| { |
| |
| |
| |
| return a*b+c; |
| |
| } |
|
|
| INLINE float dot_product(vec_t a, vec_t b) |
| { |
| return fmuladd(a.z, b.z, fmuladd(a.x, b.x, a.y * b.y)); |
| } |
|
|
| INLINE vec_t add(vec_t a, vec_t b) |
| { |
| vec_t r; |
|
|
| r.x = a.x + b.x; |
| r.y = a.y + b.y; |
| r.z = a.z + b.z; |
|
|
| return r; |
| } |
|
|
| INLINE vec_t subtract(vec_t a, vec_t b) |
| { |
| vec_t r; |
|
|
| r.x = a.x - b.x; |
| r.y = a.y - b.y; |
| r.z = a.z - b.z; |
|
|
| return r; |
| } |
|
|
| INLINE vec_t scalar_mul(vec_t a, float m) |
| { |
| vec_t r; |
|
|
| r.x = m * a.x; |
| r.y = m * a.y; |
| r.z = m * a.z; |
|
|
| return r; |
| } |
|
|
| INLINE vec_t normalize_vec(vec_t v) |
| { |
| return scalar_mul(v, RSQRT(dot_product(v, v))); |
| } |
|
|
| INLINE vec_t reflect(vec_t I, vec_t N) { |
| |
| return subtract(I, scalar_mul(N, dot_product(I, N)*2.0)); |
| } |
|
|
|
|
|
|
| struct plane_intersect_ret_t { vec_t intersection; bool cond; }; |
|
|
| INLINE plane_intersect_ret_t plane_intersect(plane_t p, ray_t r) |
| { |
| plane_intersect_ret_t ret; |
| ret.cond = false; |
| float denom = dot_product(p.normal, r.direction); |
|
|
| |
| if (float_to_uint(denom) != 0) |
| { |
| vec_t p0r0 = subtract(p.origin, r.origin); |
|
|
| float t = dot_product(p0r0, p.normal) / denom; |
|
|
| ret.intersection = add(r.origin, scalar_mul(r.direction, t)); |
| |
| ret.cond = (t >= 0.0); |
| } |
| return ret; |
| } |
|
|
| struct sphere_intersect_ret_t { vec_t intersection; vec_t normal; bool cond; }; |
|
|
| INLINE sphere_intersect_ret_t sphere_intersect(sphere_t s, ray_t r) |
| { |
| sphere_intersect_ret_t ret; |
| ret.cond = false; |
| vec_t c0r0 = subtract(s.center, r.origin); |
|
|
| float tca = dot_product(r.direction, c0r0); |
|
|
| if (tca >= 0.0) |
| { |
| float d2 = dot_product(c0r0, c0r0); |
| d2 = d2 - tca * tca; |
|
|
| float radius2 = s.radius * s.radius; |
| if (d2 <= radius2) |
| { |
| float thc = SQRT(radius2 - d2); |
| float t0 = tca - thc; |
| float t1 = tca + thc; |
|
|
| |
| t0 = (t0 >= t1) ? t1 : t0; |
|
|
| |
|
|
| ret.intersection = add(r.origin, scalar_mul(r.direction, t0)); |
|
|
| vec_t normal = subtract(ret.intersection, s.center); |
| ret.normal = normalize_vec(normal); |
| ret.cond = true; |
| } |
| } |
| return ret; |
| } |
|
|
| INLINE color_t trace1(ray_t ray, plane_t aplane) |
| { |
| color_t c; |
|
|
| plane_intersect_ret_t plint = plane_intersect(aplane, ray); |
| vec_t plane_intersection = plint.intersection; |
| bool plane_intersects = plint.cond; |
|
|
| if ((!plane_intersects) != 0 | (FABS(plane_intersection.z) > 20.0) != 0 | (FABS(plane_intersection.x) > 20.0) != 0){ |
| c.b = ray.direction.y <= 1.0 ? ray.direction.y*ray.direction.y : (float)1.0; |
| c.r = c.b; |
| c.g = c.r; |
| } |
| else |
| { |
| int6_t plx = (int32_t)FABS(plane_intersection.x+20.0); |
| int6_t plz = (int32_t)FABS(plane_intersection.z+20.0); |
| bool checker = (( plx & 4 ) ^ (plz & 4)) != 4; |
|
|
| if(checker){ |
| c.r = 1.0; |
| c.g = 1.0; |
| c.b = 1.0; |
| } |
| else { |
| c.r = 0.2; |
| c.g = 0.1; |
| c.b = 0.1; |
| } |
|
|
| float intensity = (float)1.0-SQRT(plane_intersection.x*plane_intersection.x + plane_intersection.z*plane_intersection.z)*0.025; |
| c.r = c.r * intensity; |
| c.g = c.g * intensity; |
| c.b = c.b * intensity; |
| } |
|
|
| return c; |
| } |
|
|
| #define ALPHA_K ((float)0.85) |
| #define ALPHA_COLOR(c) ((c) * ALPHA_K + (1.0-ALPHA_K)) |
|
|
| INLINE color_t trace0(ray_t ray, plane_t aplane, sphere_t asphere) |
| { |
| color_t c; |
|
|
| sphere_intersect_ret_t spint = sphere_intersect(asphere, ray); |
| vec_t sphere_intersection = spint.intersection; |
| vec_t sphere_normal = spint.normal; |
| ray_t ray_to_trace; |
| |
| if (spint.cond){ |
| ray_t ray2; |
| float dp = dot_product(ray.direction, sphere_normal); |
| ray2.direction = subtract(ray.direction, scalar_mul(sphere_normal, (dp*2.0))); |
| ray2.origin = sphere_intersection; |
| ray_to_trace = ray2; |
| } |
| else |
| { |
| ray_to_trace = ray; |
| } |
| |
| c = trace1(ray_to_trace, aplane); |
| |
| if(spint.cond) |
| { |
| c.r = ALPHA_COLOR(c.r)*(243./256.); |
| c.g = ALPHA_COLOR(c.g)*(201./256.); |
| c.b = ALPHA_COLOR(c.b)*(104./256.); |
| } |
|
|
| return c; |
| } |
|
|
| color_t traceray(uint16_t i, uint16_t j, uint32_t frame, ray_t acamera, plane_t aplane, sphere_t asphere) |
| { |
| ray_t ray; |
| ray.origin = acamera.origin; |
|
|
| #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 |
|
|
| static const float aspect = (float)(FRAME_HEIGHT*16)/(float)(FRAME_WIDTH*9); |
| static const float scale = 1./(float)FRAME_WIDTH; |
| float x = (float)cx*scale*aspect; |
| float y = (float)cy*scale; |
| |
| |
| ray.origin.x = (float)frame*0.01; |
| ray.origin.z = (float)-20.0-(float)frame*0.05; |
|
|
| ray.direction.x = x; |
| ray.direction.y = y; |
| ray.direction.z = 1.0; |
| ray.direction = normalize_vec(ray.direction); |
|
|
| return trace0(ray, aplane, asphere); |
| } |
|
|
| struct animation_pos_t |
| { |
| ray_t camera; |
| plane_t plane; |
| sphere_t sphere; |
| uint32_t frame; |
| float sphere_vel; |
| }; |
|
|
|
|
| INLINE animation_pos_t animation(uint32_t frame) |
| { |
| animation_pos_t positions; |
| |
| positions.camera.origin.x = 0.0; positions.camera.origin.y = 15.0; positions.camera.origin.z = 0.0; |
| positions.camera.direction.x = 0.0; positions.camera.direction.y = 0.0; positions.camera.direction.z = 10.0; |
| positions.plane.origin.x = 0.0; positions.plane.origin.y = 0.0; positions.plane.origin.z = 0.0; |
| positions.plane.normal.x = 0.0; positions.plane.normal.y = 1.0; positions.plane.normal.z = 0.0; |
| positions.sphere.center.x = 3.0; positions.sphere.center.y = 6.0; positions.sphere.center.z = -10.0; positions.sphere.radius = 2.5; |
| |
| |
| |
| |
| positions.sphere.center.x = (float)frame*0.05; |
| positions.camera.origin.z = (float)frame*0.1 - 10.0; |
| |
| |
| |
| |
| |
| |
| |
| |
| return positions; |
| } |
|
|
|
|
| #ifndef __PIPELINEC__ |
| full_state_t full_update(full_state_t state, bool reset, bool button_state) |
| { |
| state.scene.frame = (state.scene.frame+1)&0xFF; |
| if(reset) state.scene.frame = 0; |
| return state; |
| } |
|
|
| pixel_t render_pixel(uint16_t x, uint16_t y) |
| { |
| scene_t scene = state.scene; |
| animation_pos_t animation_pos = animation(scene.frame); |
| color_t c = traceray(x, y, scene.frame, animation_pos.camera, animation_pos.plane, animation_pos.sphere); |
| pixel_t p; |
| p.r = (int9_t)(c.r*255.0); |
| p.g = (int9_t)(c.g*255.0); |
| p.b = (int9_t)(c.b*255.0); |
| return p; |
| } |
|
|
| #else |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #endif |
|
|