summaryrefslogtreecommitdiff
path: root/src/modules/ray/ray_scene.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2017-06-01 12:18:25 -0700
committerVito Caputo <vcaputo@pengaru.com>2017-06-01 12:18:25 -0700
commitf77ab1e77aa3c89c4e923395b6fd75f3cb0bc522 (patch)
tree8ca9158ee93a0be8972a451f3462613adeb8a032 /src/modules/ray/ray_scene.c
parente1276c6e2cdf32772917f903467beafb692bb6ae (diff)
ray: move max depth check out of trace_ray()
We can avoid some unnecessary work at the max depth by checking it in shade_ray() instead.
Diffstat (limited to 'src/modules/ray/ray_scene.c')
-rw-r--r--src/modules/ray/ray_scene.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/src/modules/ray/ray_scene.c b/src/modules/ray/ray_scene.c
index d9b8277..acd65d3 100644
--- a/src/modules/ray/ray_scene.c
+++ b/src/modules/ray/ray_scene.c
@@ -9,7 +9,7 @@
#include "ray_ray.h"
#include "ray_scene.h"
-#define MAX_RECURSION_DEPTH 5
+#define MAX_RECURSION_DEPTH 4
static ray_color_t trace_ray(ray_scene_t *scene, ray_ray_t *ray, unsigned depth);
@@ -109,16 +109,18 @@ static inline ray_color_t shade_ray(ray_scene_t *scene, ray_ray_t *ray, ray_obje
/* generate a reflection ray */
#if 1
- float dot = ray_3f_dot(&ray->direction, &normal);
- ray_ray_t reflected_ray = { .direction = ray_3f_mult_scalar(&normal, dot * 2.0f) };
- ray_3f_t reflection;
+ if (depth < MAX_RECURSION_DEPTH) {
+ float dot = ray_3f_dot(&ray->direction, &normal);
+ ray_ray_t reflected_ray = { .direction = ray_3f_mult_scalar(&normal, dot * 2.0f) };
+ ray_3f_t reflection;
- reflected_ray.origin = intersection;
- reflected_ray.direction = ray_3f_sub(&ray->direction, &reflected_ray.direction);
+ reflected_ray.origin = intersection;
+ reflected_ray.direction = ray_3f_sub(&ray->direction, &reflected_ray.direction);
- reflection = trace_ray(scene, &reflected_ray, depth);
- reflection = ray_3f_mult_scalar(&reflection, surface.specular);
- color = ray_3f_add(&color, &reflection);
+ reflection = trace_ray(scene, &reflected_ray, depth);
+ reflection = ray_3f_mult_scalar(&reflection, surface.specular);
+ color = ray_3f_add(&color, &reflection);
+ }
#endif
/* TODO: generate a refraction ray */
@@ -135,8 +137,6 @@ static ray_color_t trace_ray(ray_scene_t *scene, ray_ray_t *ray, unsigned depth)
unsigned i;
depth++;
- if (depth > MAX_RECURSION_DEPTH)
- return color;
for (i = 0; i < scene->n_objects; i++) {
float distance;
© All Rights Reserved