From 7a48ecd394f7b3b8f0f6e18606f681ffac3e3a7c Mon Sep 17 00:00:00 2001 From: Julian T Date: Wed, 25 Mar 2020 18:53:37 +0100 Subject: Create new fitting scene to test area lights --- main.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++------------------- ray.c | 4 ++-- scene.c | 5 ++-- scene.h | 2 +- 4 files changed, 64 insertions(+), 28 deletions(-) diff --git a/main.c b/main.c index 82b376c..56d565b 100644 --- a/main.c +++ b/main.c @@ -26,7 +26,11 @@ typedef struct { unsigned percent = 0; pthread_mutex_t percentlock; -char container[ CONTAINER_SIZE(5, 4, 1) ]; +#define OBJECTS 9 +#define MATERIALS 6 +#define LIGHTS 1 + +char container[ CONTAINER_SIZE(OBJECTS, MATERIALS, LIGHTS) ]; container_t *cont = (container_t *) container; // Implement random @@ -37,7 +41,7 @@ COORD_T ray_rand(void *seed) int main() { - container_init(cont, 5, 4, 1); + container_init(cont, OBJECTS, MATERIALS, LIGHTS); // Init space_t space_t *s = container_prepare_space(cont);; @@ -46,11 +50,11 @@ int main() color_set(&s->ambient, 0.09, 0.09, 0.09); color_set(&s->back, 0.8, 0.8, 0.8); color_set(&s->env_color, 0.13, 0.13, 0.13); - s->env_samples = 16; + s->env_samples = 0; // Set viewpoint options - vector_set(&s->view.position, 0, 16, 6); - vector_set(&s->view.target, 0, 0, 6); + vector_set(&s->view.position, 0, 5, 5); + vector_set(&s->view.target, 0, 0, 5); s->view.width = TESTW; s->view.height = TESTH; @@ -71,9 +75,9 @@ int main() material_t *m2 = add_material(cont); vector_set(&m2->color, 1, 1, 1); - m2->defuse = 0.0; + m2->defuse = 0; m2->specular = 0.5; - m2->shine = 80; + m2->shine = 50; m2->reflective = 1; material_t *mpl = add_material(cont); @@ -81,38 +85,69 @@ int main() mpl->defuse = 1; mpl->specular = 0.0; mpl->shine = 50; - mpl->reflective = 0.0; + mpl->reflective = 0; + + material_t *mplgreen = add_material(cont); + memcpy(mplgreen, mpl, sizeof(material_t)); + vector_set(&mplgreen->color, 0.3, 0.3, 1); + //mplgreen->reflective = 1; + //mplgreen->defuse = 0; + + material_t *mplred = add_material(cont); + memcpy(mplred, mplgreen, sizeof(material_t)); + vector_set(&mplred->color, 1, 0.3, 0.3); viewpoint_init(&s->view); - object_t *o = add_object(cont, TYPE_SPHERE); - vector_set(&o->sph.center, 0, 4, 7); - o->sph.radius = 5; - o->m = m2; - + object_t *o; o = add_object(cont, TYPE_SPHERE); - vector_set(&o->sph.center, 8, 8, 4); - o->sph.radius = 2; - o->m = m3; + vector_set(&o->sph.center, -2, -1, 7); + o->sph.radius = 1.5; + o->m = m2; o = add_object(cont, TYPE_SPHERE); - vector_set(&o->sph.center, -10, 9, 5); - o->sph.radius = 3; + vector_set(&o->sph.center, 0, 1, 3); + o->sph.radius = 1; o->m = m; o = add_object(cont, TYPE_SPHERE); - vector_set(&o->sph.center, -10, -5, 5); - o->sph.radius = 3; + vector_set(&o->sph.center, 1, 1, 5); + o->sph.radius = 1; o->m = m; o = add_object(cont, TYPE_PLANE); - vector_set(&o->pl.start, 0, 0, 2); + vector_set(&o->pl.start, 0, 0, 0); + vector_set(&o->pl.norm, 0, 0, 1); + o->m = mpl; + + o = add_object(cont, TYPE_PLANE); + vector_set(&o->pl.start, 0, 0, 10); vector_set(&o->pl.norm, 0, 0, 1); o->m = mpl; + o = add_object(cont, TYPE_PLANE); + vector_set(&o->pl.start, 0, -3, 0); + vector_set(&o->pl.norm, 0, 1, 0); + o->m = mpl; + + o = add_object(cont, TYPE_PLANE); + vector_set(&o->pl.start, 0, 10, 0); + vector_set(&o->pl.norm, 0, 1, 0); + o->m = mpl; + + o = add_object(cont, TYPE_PLANE); + vector_set(&o->pl.start, -5, 0, 0); + vector_set(&o->pl.norm, 1, 0, 0); + o->m = mplgreen; + + o = add_object(cont, TYPE_PLANE); + vector_set(&o->pl.start, 5, 0, 0); + vector_set(&o->pl.norm, 1, 0, 0); + o->m = mplred; + light_t *l = add_light(cont); - vector_set(&l->pos, 20, 10, 30); - color_set(&l->defuse, 0.3, 0.3, 0.3); + vector_set(&l->pos, 3, 0, 1); + color_set(&l->defuse, 1, 1, 1); color_set(&l->specular, 0.5, 0.5, 0.5); pgm_write_header(stdout, TESTW, TESTH); diff --git a/ray.c b/ray.c index a41866c..eba29c0 100644 --- a/ray.c +++ b/ray.c @@ -276,7 +276,7 @@ int ray_trace_recur(space_t *s, color_t *dest, ray_t *ray, unsigned hop, COORD_T // Calculate normal vector vector_t N; - obj_norm_at(o, &N, r.start); + obj_norm_at(o, &N, r.start, ray->direction); // Check if we should calculate light if (o->m->defuse + o->m->specular > ZERO_APROX) { @@ -290,7 +290,7 @@ int ray_trace_recur(space_t *s, color_t *dest, ray_t *ray, unsigned hop, COORD_T } // Calculate reflection vector - if (hop < 2 && o->m->reflective > ZERO_APROX) { + if (hop < 10 && o->m->reflective > ZERO_APROX) { vector_scale(r.direction, &N, 2 * vector_dot(ray->direction, &N)); vector_sub(r.direction, ray->direction, r.direction); diff --git a/scene.c b/scene.c index 6221fe8..730d8c8 100644 --- a/scene.c +++ b/scene.c @@ -116,7 +116,7 @@ material_t *add_material(container_t *cont) return m; } -void obj_norm_at(object_t *o, vector_t *dest, vector_t *point) +void obj_norm_at(object_t *o, vector_t *dest, vector_t *point, vector_t *direction) { switch(o->type) { case TYPE_SPHERE: @@ -124,7 +124,8 @@ void obj_norm_at(object_t *o, vector_t *dest, vector_t *point) vector_scale_inv(dest, dest, vector_len(dest)); break; case TYPE_PLANE: - vector_copy(dest, &o->pl.norm); + // Calculate if the point is in front + vector_scale(dest, &o->pl.norm, (vector_dot(direction, &o->pl.norm) > 0) ? -1 : 1); break; } diff --git a/scene.h b/scene.h index b311ccc..08ed9e9 100644 --- a/scene.h +++ b/scene.h @@ -105,6 +105,6 @@ object_t *add_object(container_t *cont, unsigned type); light_t *add_light(container_t *cont); material_t *add_material(container_t *cont); -void obj_norm_at(object_t *o, vector_t *dest, vector_t *point); +void obj_norm_at(object_t *o, vector_t *dest, vector_t *point, vector_t *direction); #endif -- cgit v1.2.3