From a64a0186cdeee9189bd4db1303f82908cfe279a4 Mon Sep 17 00:00:00 2001 From: Morten Hustveit Date: Tue, 2 Dec 2014 21:30:55 -0500 Subject: ray.c: Prevent lights from subtracting color in the shade Also add some ambient light. --- ray.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'ray.c') diff --git a/ray.c b/ray.c index 2470777..483dc0c 100644 --- a/ray.c +++ b/ray.c @@ -56,6 +56,7 @@ static Light lights[] = { {.position={-3, 3, -4}, .diffuse={0, .6, .6}}, {.position={0, 30, -4}, .diffuse={1, 1, 1}} }; +static float ambient[3] = {0.2, 0.1, 0.1}; static void trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int mask) { @@ -92,8 +93,11 @@ trace(const float s[3], const float d[3], float pixel[3], int n, unsigned int ma float lr_dot = dot(l, nearest_r); if (lr_dot > 0) { float scale = lr_dot / sqrtf(dot(l, l)) / (1 << n); - for(int k = 0; k < 3; ++k) - pixel[k] += lights[m].diffuse[k] * objects[nearest_object].diffuse[k] * scale; + for(int k = 0; k < 3; ++k) { + float diffuse = lights[m].diffuse[k] * objects[nearest_object].diffuse[k] * scale; + if (diffuse < 0.0f) diffuse = 0.0f; + pixel[k] += diffuse + ambient[k] * objects[nearest_object].diffuse[k]; + } } } -- cgit v1.2.3