X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Frt.c;h=18b7f17548674537cd709473ddede1c73870f0a6;hb=ee39e383f97259441df726c76cf26dcef7e9c054;hp=0ad8a7c9809beb63092514d7d25e39d20926678b;hpb=a886acd491daf3faa62d45bf5503eb2c5313f335;p=cyberay diff --git a/src/rt.c b/src/rt.c index 0ad8a7c..18b7f17 100644 --- a/src/rt.c +++ b/src/rt.c @@ -1,4 +1,14 @@ +#include #include "rt.h" +#include "game.h" + +#define TILESZ 32 + +struct tile { + int x, y, width, height; + int sample; + cgm_vec3 *fbptr; +}; struct framebuffer fb; struct thread_pool *tpool; @@ -6,48 +16,102 @@ float view_xform[16]; float vfov = M_PI / 4; static float aspect; +static struct tile *tiles; +static int num_tiles; +static void render_tile(struct tile *tile); static void ray_trace(cgm_vec3 *color, cgm_ray *ray); +static void bgcolor(cgm_vec3 *color, cgm_ray *ray); static void primary_ray(cgm_ray *ray, int x, int y, int sample); int fbsize(int width, int height) { - void *tmp; + int i, j, x, y, xtiles, ytiles; + cgm_vec3 *fbptr; + struct tile *tileptr; - if(!(tmp = malloc(width * height * sizeof *fb.pixels))) { + if(!(fbptr = malloc(width * height * sizeof *fb.pixels))) { + return -1; + } + xtiles = width / TILESZ; + ytiles = height / TILESZ; + if(!(tileptr = malloc(xtiles * ytiles * sizeof *tiles))) { + free(fbptr); return -1; } free(fb.pixels); - fb.pixels = tmp; + fb.pixels = fbptr; fb.width = width; fb.height = height; + free(tiles); + tiles = tileptr; + num_tiles = xtiles * ytiles; + aspect = (float)fb.width / (float)fb.height; + y = 0; + for(i=0; ix = x; + tileptr->y = y; + tileptr->width = width - x < TILESZ ? width - x : TILESZ; + tileptr->height = height - y < TILESZ ? height - y : TILESZ; + tileptr->fbptr = fbptr + x; + tileptr++; + + x += TILESZ; + } + fbptr += width * TILESZ; + y += TILESZ; + } + return 0; } void render(void) { + int i; + + for(i=0; ifbptr; - for(i=0; iheight; i++) { + for(j=0; jwidth; j++) { + primary_ray(&ray, tile->x + j, tile->y + i, tile->sample); + ray_trace(fbptr + j, &ray); } + fbptr += fb.width; } } static void ray_trace(cgm_vec3 *color, cgm_ray *ray) { - color->x = ray->dir.x * 0.5f + 0.5f; - color->y = ray->dir.y * 0.5f + 0.5f; - color->z = 0.0f; + struct rayhit hit; + + if(ray_level(ray, &lvl, FLT_MAX, &hit)) { + color->x = hit.v.norm.x * 0.5 + 0.5; + color->y = hit.v.norm.y * 0.5 + 0.5; + color->z = hit.v.norm.z * 0.5 + 0.5; + } else { + bgcolor(color, ray); + } +} + +static void bgcolor(cgm_vec3 *color, cgm_ray *ray) +{ + color->x = color->y = color->z = 1.0f; } static void primary_ray(cgm_ray *ray, int x, int y, int sample)