octree

octree tools and accessories
git clone git://nsmpr.xyz/octree.git
Log | Files | Refs

commit cc544ca3d56234599f71a2a9973ee2d13dd30840
parent 209564e16cebbd4dfd05f3cc910daf9b7242edf4
Author: Pavel Renev <an2qzavok@gmail.com>
Date:   Mon, 15 Mar 2021 08:25:01 +0000

render: testbed for raytracing code

Diffstat:
Mmkfile | 2+-
Arender.c | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/mkfile b/mkfile @@ -1,6 +1,6 @@ </$objtype/mkfile -TARG=fs node +TARG=fs node render OFILES= HFILE=octree.h BIN=/$objtype/bin/octree diff --git a/render.c b/render.c @@ -0,0 +1,90 @@ +#include <u.h> +#include <libc.h> + +#include "octree.h" + +typedef struct Pixel Pixel; +typedef struct Dot Dot; +typedef struct Rad Rad; +typedef struct Ray Ray; + +struct Pixel { + double r; + double g; + double b; +}; + +struct Dot { + double x; + double y; + double z; +}; + +struct Rad { + double u; + double v; + double z; +}; + +struct Ray { + Dot pos; + Rad dir; +}; + +Pixel Pwhite = {1, 1, 1}; +Pixel Pblack = {0, 0, 0}; + +Ray sphere = {{0.5, 0.5, 0}, {0, 0, 0.5}}; + +int +spherehit(Ray v, Ray s) +{ + return (hypot(v.pos.x -s.pos.x, v.pos.y - s.pos.y) <= s.dir.z); +} + +Pixel +ray(Ray v) +{ + Pixel p = { + .r = (v.pos.x + v.pos.y)/2, + .g = 0, + .b = 0, + }; + if (spherehit(v, sphere)) { + p = Pblack; + } + return p; +} + +int +render(int x, int y) +{ + Pixel p; + int r, g, b; + Ray v; + v.pos = (Dot){ (double)x/256.0, (double)y/256.0, 0 }; + p = ray(v); + r = 0xff * p.r; + g = 0xff * p.g; + b = 0xff * p.b; + return (r<<16)|(g<<8)|b; +} + +void +main(void) +{ + int i, x, y; + int buf[256*256]; + write(1, " x8r8g8b8 ", 12); + write(1, " 0 ", 12); + write(1, " 0 ", 12); + write(1, " 256 ", 12); + write(1, " 256 ", 12); + for (i = 0; i < 256 * 256; i++) { + x = i%256; + y = 255-(i/256); + buf[i] = render(x, y); + } + write(1, buf, 4 * 256 * 256); + exits(nil); +} +\ No newline at end of file