summaryrefslogtreecommitdiff
path: root/ray.c
diff options
context:
space:
mode:
authorMorten Hustveit <morten.hustveit@gmail.com>2014-12-02 21:30:55 -0500
committerMorten Hustveit <morten.hustveit@gmail.com>2014-12-02 21:31:43 -0500
commita64a0186cdeee9189bd4db1303f82908cfe279a4 (patch)
tree431552bb7cba3f7ff67f73bfce1697755ceee2b3 /ray.c
parent5eb4b4bb4e44eec2015ed4ec2bf966e1b0b0f85b (diff)
ray.c: Prevent lights from subtracting color in the shade
Also add some ambient light.
Diffstat (limited to 'ray.c')
-rw-r--r--ray.c8
1 files changed, 6 insertions, 2 deletions
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];
+ }
}
}