Prepare code to be able to get material from some form of a "group leader".

This commit is contained in:
Godzil
2020-02-26 00:32:14 +00:00
parent 5e4cfb84e6
commit 1c00077949
3 changed files with 18 additions and 10 deletions

View File

@@ -11,6 +11,7 @@
#include <stdlib.h>
#include <ray.h>
#include <material.h>
class Shape;
class Intersect;
@@ -19,9 +20,9 @@ struct Computation
{
Computation(Shape *object, double t, Tuple point, Tuple eyev, Tuple normalv, Tuple overHitP,
bool inside, Tuple reflectV = Vector(0, 0, 0), double n1 = 1.0, double n2 = 1.0,
Tuple underHitP = Point(0, 0, 0)) :
Tuple underHitP = Point(0, 0, 0), Material *objMat = nullptr) :
object(object), t(t), hitPoint(point), eyeVector(eyev), normalVector(normalv), inside(inside),
overHitPoint(overHitP), underHitPoint(underHitP), reflectVector(reflectV), n1(n1), n2(n2) { };
overHitPoint(overHitP), underHitPoint(underHitP), reflectVector(reflectV), n1(n1), n2(n2), material(objMat) { };
double schlick()
{
@@ -58,6 +59,8 @@ struct Computation
Tuple normalVector;
Tuple reflectVector;
Material *material;
double n1;
double n2;

View File

@@ -68,6 +68,10 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs)
}
}
Shape *s = this->object;
/* For now don't get root group material */
//while(s->parent != nullptr) { s = s->parent; }
return Computation(this->object,
this->t,
hitP,
@@ -78,5 +82,6 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs)
reflectV,
n1,
n2,
underHitP);
underHitP,
&s->material);
}

View File

@@ -95,20 +95,20 @@ Intersect World::intersect(Ray r)
Tuple World::shadeHit(Computation comps, uint32_t depthCount)
{
uint32_t lightIndex;
Tuple surface = Colour(0, 0, 0);
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,
surface = surface + comps.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);
if ((comps.object->material.reflective > 0) && (comps.object->material.transparency > 0))
if ((comps.material->reflective > 0) && (comps.material->transparency > 0))
{
double reflectance = comps.schlick();
@@ -159,7 +159,7 @@ bool World::isShadowed(Tuple point, uint32_t light)
Colour World::reflectColour(Computation comps, uint32_t depthCount)
{
if ((depthCount == 0) || (comps.object->material.reflective == 0))
if ((depthCount == 0) || (comps.material->reflective == 0))
{
return Colour(0, 0, 0);
}
@@ -168,7 +168,7 @@ Colour World::reflectColour(Computation comps, uint32_t depthCount)
Ray reflectedRay = Ray(comps.overHitPoint, comps.reflectVector);
Tuple hitColour = this->colourAt(reflectedRay, depthCount - 1);
hitColour = hitColour * comps.object->material.reflective;
hitColour = hitColour * comps.material->reflective;
return Colour(hitColour.x, hitColour.y, hitColour.z);
}
@@ -179,7 +179,7 @@ Colour World::refractedColour(Computation comps, uint32_t depthCount)
double cos_i = comps.eyeVector.dot(comps.normalVector);
double sin2_t = (nRatio*nRatio) * (1 - cos_i * cos_i);
if ((sin2_t > 1 ) || (depthCount == 0) || (comps.object->material.transparency == 0))
if ((sin2_t > 1 ) || (depthCount == 0) || (comps.material->transparency == 0))
{
return Colour(0, 0, 0);
}
@@ -189,7 +189,7 @@ Colour World::refractedColour(Computation comps, uint32_t depthCount)
Ray refractedRay = Ray(comps.underHitPoint, direction);
Tuple hitColour = this->colourAt(refractedRay, depthCount - 1) * comps.object->material.transparency;
Tuple hitColour = this->colourAt(refractedRay, depthCount - 1) * comps.material->transparency;
return Colour(hitColour.x, hitColour.y, hitColour.z);
}