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_pos = { 0.0, 1.5, -2.0 };
|
||||||
static const vec3_t camera_target = { 0.0, 1.0, 1.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(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(-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)),
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
|
const scene_t scene = {
|
||||||
|
.sky_colour = sky,
|
||||||
|
.objs = objs,
|
||||||
|
.obj_count = NELEMS(objs),
|
||||||
|
};
|
||||||
|
|
||||||
img_t img = { .pix = pixbuf };
|
img_t img = { .pix = pixbuf };
|
||||||
camera_t camera = camera_init(
|
camera_t camera = camera_init(
|
||||||
camera_pos, camera_target, FOV, W, H, SAMPLES_PER_PIXEL);
|
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);
|
ff_write(STDOUT_FILENO, img);
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
#define CAMERA_H
|
#define CAMERA_H
|
||||||
|
|
||||||
#include "img.h"
|
#include "img.h"
|
||||||
#include "obj.h"
|
#include "scene.h"
|
||||||
#include "vec3.h"
|
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@@ -19,7 +18,6 @@ camera_t camera_init(
|
|||||||
uint32_t img_height, unsigned samples_per_pixel);
|
uint32_t img_height, unsigned samples_per_pixel);
|
||||||
|
|
||||||
void camera_render(
|
void camera_render(
|
||||||
const camera_t *camera, const obj_t *scene, unsigned scene_count,
|
const camera_t *camera, const scene_t *scene, img_t *img_out);
|
||||||
img_t *img_out);
|
|
||||||
|
|
||||||
#endif
|
#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 {
|
typedef struct {
|
||||||
const camera_t *camera;
|
const camera_t *camera;
|
||||||
const obj_t *scene;
|
const scene_t *scene;
|
||||||
pix_t *pixels;
|
pix_t *pixels;
|
||||||
rng_t rng;
|
rng_t rng;
|
||||||
unsigned start_y, row_count, scene_count;
|
unsigned start_y, row_count;
|
||||||
atomic_uint *progress;
|
atomic_uint *progress;
|
||||||
} work_slice_t;
|
} work_slice_t;
|
||||||
|
|
||||||
static const vec3_t up = { 0.0, 1.0, 0.0 };
|
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 white = { 1.0, 1.0, 1.0 };
|
||||||
static const vec3_t black = { 0.0, 0.0, 0.0 };
|
static const vec3_t black = { 0.0, 0.0, 0.0 };
|
||||||
|
|
||||||
static vec3_t
|
static vec3_t trace(ray_t ray, const scene_t *scene, rng_t *rng)
|
||||||
trace(ray_t ray, const obj_t *scene, unsigned scene_count, rng_t *rng)
|
|
||||||
{
|
{
|
||||||
vec3_t colour = white;
|
vec3_t colour = white;
|
||||||
for (unsigned i = 0; i < MAX_ITER; ++i) {
|
for (unsigned i = 0; i < MAX_ITER; ++i) {
|
||||||
hit_t hit = { .t = DBL_MAX };
|
hit_t hit = { .t = DBL_MAX };
|
||||||
material_t material = {};
|
material_t material = {};
|
||||||
for (unsigned j = 0; j < scene_count; ++j) {
|
for (unsigned j = 0; j < scene->obj_count; ++j) {
|
||||||
if (scene[j].intersect(scene[j].params, ray, &hit, MIN_T, hit.t))
|
const obj_t *obj = scene->objs + j;
|
||||||
material = scene[j].material;
|
if (obj->intersect(obj->params, ray, &hit, MIN_T, hit.t))
|
||||||
|
material = obj->material;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hit.t == DBL_MAX) {
|
if (hit.t == DBL_MAX) {
|
||||||
const double a = (ray.dir.y + 1.0) / 2.0;
|
const double a = (ray.dir.y + 1.0) / 2.0;
|
||||||
const vec3_t bg = vec3_add(
|
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);
|
return vec3_hadamard(colour, bg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,8 +103,7 @@ static int render_thread(void *arg)
|
|||||||
.orig = camera->pos,
|
.orig = camera->pos,
|
||||||
.dir = vec3_unit(vec3_sub(jittered_pix, camera->pos)),
|
.dir = vec3_unit(vec3_sub(jittered_pix, camera->pos)),
|
||||||
};
|
};
|
||||||
const vec3_t sample = trace(
|
const vec3_t sample = trace(ray, slice->scene, &slice->rng);
|
||||||
ray, slice->scene, slice->scene_count, &slice->rng);
|
|
||||||
|
|
||||||
colour = vec3_add(colour, vec3_scale(sample, sample_weight));
|
colour = vec3_add(colour, vec3_scale(sample, sample_weight));
|
||||||
}
|
}
|
||||||
@@ -155,8 +153,7 @@ camera_t camera_init(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void camera_render(
|
void camera_render(
|
||||||
const camera_t *camera, const obj_t *scene, unsigned scene_count,
|
const camera_t *camera, const scene_t *scene, img_t *img_out)
|
||||||
img_t *img_out)
|
|
||||||
{
|
{
|
||||||
img_out->w = camera->img_width;
|
img_out->w = camera->img_width;
|
||||||
img_out->h = camera->img_height;
|
img_out->h = camera->img_height;
|
||||||
@@ -172,7 +169,6 @@ void camera_render(
|
|||||||
for (unsigned i = 0; i < NTHREADS; ++i) {
|
for (unsigned i = 0; i < NTHREADS; ++i) {
|
||||||
slices[i].camera = camera;
|
slices[i].camera = camera;
|
||||||
slices[i].scene = scene;
|
slices[i].scene = scene;
|
||||||
slices[i].scene_count = scene_count;
|
|
||||||
slices[i].pixels = img_out->pix;
|
slices[i].pixels = img_out->pix;
|
||||||
slices[i].rng = rng_init(i);
|
slices[i].rng = rng_init(i);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user