From 935c8ebff783e5fc106bf895da333ee83433ecd9 Mon Sep 17 00:00:00 2001 From: Godzil Date: Sat, 22 Feb 2020 15:12:06 +0000 Subject: [PATCH] Add support for multiple lights --- source/include/world.h | 2 +- source/world.cpp | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/source/include/world.h b/source/include/world.h index bcb3af3..c06669d 100644 --- a/source/include/world.h +++ b/source/include/world.h @@ -44,7 +44,7 @@ public: Tuple shadeHit(Computation comps, uint32_t depthCount = 4); Tuple colourAt(Ray r, uint32_t depthCount = 4); - bool isShadowed(Tuple point); + bool isShadowed(Tuple point, uint32_t light = 0); Colour reflectColour(Computation comps, uint32_t depthCount = 4); Colour refractedColour(Computation comps, uint32_t depthCount = 4); diff --git a/source/world.cpp b/source/world.cpp index a75315c..ca870f0 100644 --- a/source/world.cpp +++ b/source/world.cpp @@ -95,12 +95,17 @@ Intersect World::intersect(Ray r) Tuple World::shadeHit(Computation comps, uint32_t depthCount) { /* TODO: Add support for more than one light */ + uint32_t lightIndex; - bool isThereAnObstacle = this->isShadowed(comps.overHitPoint); + Tuple surface = Colour(0, 0, 0); - Tuple surface = comps.object->material.lighting(*this->lightList[0], comps.overHitPoint, comps.eyeVector, - comps.normalVector, comps.object, isThereAnObstacle); + for(lightIndex = 0; lightIndex < this->lightCount; lightIndex++) + { + bool isThereAnObstacle = this->isShadowed(comps.overHitPoint, lightIndex); + surface = surface + comps.object->material.lighting(*this->lightList[lightIndex], comps.overHitPoint, comps.eyeVector, + comps.normalVector, comps.object, isThereAnObstacle); + } Tuple reflected = this->reflectColour(comps, depthCount); Tuple refracted = this->refractedColour(comps, depthCount); @@ -109,7 +114,6 @@ Tuple World::shadeHit(Computation comps, uint32_t depthCount) double reflectance = comps.schlick(); return surface + reflected * reflectance + refracted * (1 - reflectance); - } return surface + reflected + refracted; @@ -130,11 +134,11 @@ Tuple World::colourAt(Ray r, uint32_t depthCount) } } -bool World::isShadowed(Tuple point) +bool World::isShadowed(Tuple point, uint32_t light) { /* TODO: Add support for more than one light */ - Tuple v = this->lightList[0]->position - point; + Tuple v = this->lightList[light]->position - point; double distance = v.magnitude(); Tuple direction = v.normalise();