octree

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

commit ba7443a2cdd8fe83d60ce3236d3d2d4470210139
parent 563ba2d1774f8d785977e68883919605b2f610eb
Author: Pavel Renev <an2qzavok@gmail.com>
Date:   Sun, 28 Mar 2021 16:08:05 +0000

implement perspective projection

Diffstat:
Mrender.c | 36+++++++++++++++++++++++-------------
1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/render.c b/render.c @@ -3,6 +3,7 @@ #include "octree.h" +#define BSIZE 512 #define SQR(X) (X * X) typedef struct Vect Vect; @@ -62,11 +63,15 @@ subvect(Vect d1, Vect d2) return addvect(d1, (Vect){-d2.x, -d2.y, -d2.z}); } + Vect -topolar(Vect v) +todecart(Vect v) { - return (Vect){atan(v.y/v.z)*2/PI, - atan(v.x/v.z)*2/PI, vectmod(v)}; + return (Vect) { + v.z * sin(v.x), + v.z * sin(v.y), + hypot(v.z * cos(v.y), v.z * cos(v.x)), + }; } double @@ -84,7 +89,7 @@ spherehit(Ray *v, Sphere s) d = vectmod(subvect(v->pos, s.pos)) - s.radius; v->pos = addvect(v->pos, vectxvect(vectnorm(v->dir), (Vect){d, d, d})); - if (d < 0.00001) { + if (d < 0.001) { v->dir = subvect(s.pos, v->pos); return 1; } @@ -116,11 +121,16 @@ ray(Ray v) u32int render(int x, int y) { + double fov; Vect p; u32int r, g, b; Ray v; - v.pos = (Vect){ (double)x/128.0 - 1, (double)y/128.0 - 1, -100 }; - v.dir = (Vect){ 0, 0, 1 }; + fov = PI/2; + v.pos = (Vect) { 0, 0, -1 }; + v.dir = todecart((Vect) { + fov*((double)x/BSIZE - 0.5), + fov*((double)y/BSIZE - 0.5), + 1}); p = ray(v); r = (u32int)(0xff * p.x)&0xff; g = (u32int)(0xff * p.y)&0xff; @@ -132,17 +142,17 @@ void main(void) { int i, x, y; - u32int buf[256*256]; + u32int buf[SQR(BSIZE)]; 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); + fprint(1, "%11d ", BSIZE); + fprint(1, "%11d ", BSIZE); + for (i = 0; i < SQR(BSIZE); i++) { + x = i%BSIZE; + y = BSIZE-(i/BSIZE); buf[i] = render(x, y); } - write(1, buf, 4 * 256 * 256); + write(1, buf, 4 * SQR(BSIZE)); exits(nil); }