Adding shadows!

This commit is contained in:
Godzil
2020-02-20 17:46:03 +00:00
parent 5198888df6
commit cf5597ad6d
11 changed files with 145 additions and 24 deletions

View File

@@ -16,11 +16,13 @@ class Shape;
struct Computation
{
Computation(Shape *object, double t, Tuple point, Tuple eyev, Tuple normalv, bool inside) :
object(object), t(t), hitPoint(point), eyeVector(eyev), normalVector(normalv), inside(inside) { };
Computation(Shape *object, double t, Tuple point, Tuple eyev, Tuple normalv, Tuple overHitP, bool inside) :
object(object), t(t), hitPoint(point), eyeVector(eyev), normalVector(normalv), inside(inside), overHitPoint(overHitP) { };
Shape *object;
double t;
Tuple hitPoint;
Tuple overHitPoint;
Tuple eyeVector;
Tuple normalVector;

View File

@@ -25,7 +25,7 @@ public:
public:
Material() : colour(Colour(1, 1, 1)), ambient(0.1), diffuse(0.9), specular(0.9), shininess(200) {};
Colour lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector);
Colour lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector, bool inShadow = false);
bool operator==(const Material &b) const { return double_equal(this->ambient, b.ambient) &&
double_equal(this->diffuse, b.diffuse) &&

View File

@@ -13,6 +13,7 @@
#include <math.h>
void set_equal_precision(double v);
double getEpsilon();
bool double_equal(double a, double b);
double deg_to_rad(double deg);

View File

@@ -43,6 +43,7 @@ public:
Tuple shadeHit(Computation comps);;
Tuple colourAt(Ray r);
bool isShadowed(Tuple point);
Intersect intersect(Ray r);

View File

@@ -22,11 +22,13 @@ Computation Intersection::prepareComputation(Ray r)
normalV = -normalV;
}
Tuple overHitP = hitP + normalV * getEpsilon();
return Computation(this->object,
this->t,
hitP,
eyeV,
normalV,
overHitP,
inside);
}

View File

@@ -18,6 +18,11 @@ void set_equal_precision(double v)
current_precision = v;
}
double getEpsilon()
{
return current_precision;
}
bool double_equal(double a, double b)
{
return fabs(a - b) < current_precision;

View File

@@ -10,7 +10,7 @@
#include <material.h>
#include <colour.h>
Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector)
Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector, bool inShadow)
{
Tuple lightVector = (light.position - point).normalise();
Tuple reflectVector = Tuple(0, 0, 0, 0);
@@ -25,31 +25,33 @@ Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple norma
ambientColour = effectiveColour * this->ambient;
lightDotNormal = lightVector.dot(normalVector);
if (lightDotNormal < 0)
if (!inShadow)
{
diffuseColour = Colour(0, 0, 0);
specularColour = Colour(0, 0, 0);
}
else
{
diffuseColour = effectiveColour * this->diffuse * lightDotNormal;
reflectVector = -lightVector.reflect(normalVector);
lightDotNormal = lightVector.dot(normalVector);
reflectDotEye = reflectVector.dot(eyeVector);
if (reflectDotEye < 0)
if (lightDotNormal < 0)
{
diffuseColour = Colour(0, 0, 0);
specularColour = Colour(0, 0, 0);
}
else
{
double factor = pow(reflectDotEye, this->shininess);
specularColour = light.intensity * this->specular * factor;
diffuseColour = effectiveColour * this->diffuse * lightDotNormal;
reflectVector = -lightVector.reflect(normalVector);
reflectDotEye = reflectVector.dot(eyeVector);
if (reflectDotEye < 0)
{
specularColour = Colour(0, 0, 0);
}
else
{
double factor = pow(reflectDotEye, this->shininess);
specularColour = light.intensity * this->specular * factor;
}
}
}
finalColour = ambientColour + diffuseColour + specularColour;
return Colour(finalColour.x, finalColour.y, finalColour.z);

View File

@@ -94,7 +94,12 @@ Intersect World::intersect(Ray r)
Tuple World::shadeHit(Computation comps)
{
return comps.object->material.lighting(*this->lightList[0], comps.hitPoint, comps.eyeVector, comps.normalVector);
/* TODO: Add support for more than one light */
bool isThereAnObstacle = this->isShadowed(comps.overHitPoint);
return comps.object->material.lighting(*this->lightList[0], comps.overHitPoint, comps.eyeVector,
comps.normalVector, isThereAnObstacle);
}
Tuple World::colourAt(Ray r)
@@ -109,4 +114,23 @@ Tuple World::colourAt(Ray r)
{
return this->shadeHit(hit.prepareComputation(r));
}
}
}
bool World::isShadowed(Tuple point)
{
/* TODO: Add support for more than one light */
Tuple v = this->lightList[0]->position - point;
double distance = v.magnitude();
Tuple direction = v.normalise();
Ray r = Ray(point, direction);
Intersection h = this->intersect(r).hit();
if (!h.nothing() && h.t < distance)
{
return true;
}
return false;
}