Create scene data structure
This commit is contained in:
12
demo.c
12
demo.c
@@ -15,7 +15,9 @@
|
||||
static const vec3_t camera_pos = { 0.0, 1.5, -2.0 };
|
||||
static const vec3_t camera_target = { 0.0, 1.0, 1.0 };
|
||||
|
||||
static const obj_t scene[] = {
|
||||
static const vec3_t sky = { 0.6, 0.7, 1.0 };
|
||||
|
||||
static const obj_t objs[] = {
|
||||
SPHERE(1.0, 1.0, 3.0, 1.0, LAMBERTIAN(0.6, 0.2, 0.8)),
|
||||
SPHERE(-0.8, 1.0, 3.8, 1.0, LAMBERTIAN(1.0, 1.0, 1.0)),
|
||||
SPHERE(-1.0, 0.2, 2.6, 0.2, LAMBERTIAN(0.2, 0.9, 0.3)),
|
||||
@@ -30,10 +32,16 @@ static pix_t pixbuf[W * H];
|
||||
|
||||
int main()
|
||||
{
|
||||
const scene_t scene = {
|
||||
.sky_colour = sky,
|
||||
.objs = objs,
|
||||
.obj_count = NELEMS(objs),
|
||||
};
|
||||
|
||||
img_t img = { .pix = pixbuf };
|
||||
camera_t camera = camera_init(
|
||||
camera_pos, camera_target, FOV, W, H, SAMPLES_PER_PIXEL);
|
||||
camera_render(&camera, scene, NELEMS(scene), &img);
|
||||
camera_render(&camera, &scene, &img);
|
||||
|
||||
ff_write(STDOUT_FILENO, img);
|
||||
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
#define CAMERA_H
|
||||
|
||||
#include "img.h"
|
||||
#include "obj.h"
|
||||
#include "vec3.h"
|
||||
#include "scene.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -19,7 +18,6 @@ camera_t camera_init(
|
||||
uint32_t img_height, unsigned samples_per_pixel);
|
||||
|
||||
void camera_render(
|
||||
const camera_t *camera, const obj_t *scene, unsigned scene_count,
|
||||
img_t *img_out);
|
||||
const camera_t *camera, const scene_t *scene, img_t *img_out);
|
||||
|
||||
#endif
|
||||
|
||||
12
include/scene.h
Normal file
12
include/scene.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef SCENE_H
|
||||
#define SCENE_H
|
||||
|
||||
#include "obj.h"
|
||||
|
||||
typedef struct {
|
||||
vec3_t sky_colour;
|
||||
obj_t *objs;
|
||||
unsigned obj_count;
|
||||
} scene_t;
|
||||
|
||||
#endif
|
||||
24
src/camera.c
24
src/camera.c
@@ -23,35 +23,34 @@
|
||||
|
||||
typedef struct {
|
||||
const camera_t *camera;
|
||||
const obj_t *scene;
|
||||
const scene_t *scene;
|
||||
pix_t *pixels;
|
||||
rng_t rng;
|
||||
unsigned start_y, row_count, scene_count;
|
||||
unsigned start_y, row_count;
|
||||
atomic_uint *progress;
|
||||
} work_slice_t;
|
||||
|
||||
static const vec3_t up = { 0.0, 1.0, 0.0 };
|
||||
|
||||
static const vec3_t lightblue = { 0.6, 0.7, 1.0 };
|
||||
static const vec3_t white = { 1.0, 1.0, 1.0 };
|
||||
static const vec3_t black = { 0.0, 0.0, 0.0 };
|
||||
|
||||
static vec3_t
|
||||
trace(ray_t ray, const obj_t *scene, unsigned scene_count, rng_t *rng)
|
||||
static vec3_t trace(ray_t ray, const scene_t *scene, rng_t *rng)
|
||||
{
|
||||
vec3_t colour = white;
|
||||
for (unsigned i = 0; i < MAX_ITER; ++i) {
|
||||
hit_t hit = { .t = DBL_MAX };
|
||||
material_t material = {};
|
||||
for (unsigned j = 0; j < scene_count; ++j) {
|
||||
if (scene[j].intersect(scene[j].params, ray, &hit, MIN_T, hit.t))
|
||||
material = scene[j].material;
|
||||
for (unsigned j = 0; j < scene->obj_count; ++j) {
|
||||
const obj_t *obj = scene->objs + j;
|
||||
if (obj->intersect(obj->params, ray, &hit, MIN_T, hit.t))
|
||||
material = obj->material;
|
||||
}
|
||||
|
||||
if (hit.t == DBL_MAX) {
|
||||
const double a = (ray.dir.y + 1.0) / 2.0;
|
||||
const vec3_t bg = vec3_add(
|
||||
vec3_scale(lightblue, a), vec3_scale(white, 1 - a));
|
||||
vec3_scale(scene->sky_colour, a), vec3_scale(white, 1 - a));
|
||||
return vec3_hadamard(colour, bg);
|
||||
}
|
||||
|
||||
@@ -104,8 +103,7 @@ static int render_thread(void *arg)
|
||||
.orig = camera->pos,
|
||||
.dir = vec3_unit(vec3_sub(jittered_pix, camera->pos)),
|
||||
};
|
||||
const vec3_t sample = trace(
|
||||
ray, slice->scene, slice->scene_count, &slice->rng);
|
||||
const vec3_t sample = trace(ray, slice->scene, &slice->rng);
|
||||
|
||||
colour = vec3_add(colour, vec3_scale(sample, sample_weight));
|
||||
}
|
||||
@@ -155,8 +153,7 @@ camera_t camera_init(
|
||||
}
|
||||
|
||||
void camera_render(
|
||||
const camera_t *camera, const obj_t *scene, unsigned scene_count,
|
||||
img_t *img_out)
|
||||
const camera_t *camera, const scene_t *scene, img_t *img_out)
|
||||
{
|
||||
img_out->w = camera->img_width;
|
||||
img_out->h = camera->img_height;
|
||||
@@ -172,7 +169,6 @@ void camera_render(
|
||||
for (unsigned i = 0; i < NTHREADS; ++i) {
|
||||
slices[i].camera = camera;
|
||||
slices[i].scene = scene;
|
||||
slices[i].scene_count = scene_count;
|
||||
slices[i].pixels = img_out->pix;
|
||||
slices[i].rng = rng_init(i);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user