Group should work now.
This commit is contained in:
@@ -58,7 +58,7 @@ public:
|
||||
|
||||
void updateTransform();
|
||||
Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
|
||||
Tuple normalToWorld(Tuple normalVector) { return (this->transposedInverseTransform * normalVector).normalise(); };
|
||||
Tuple normalToWorld(Tuple normalVector);
|
||||
|
||||
void setTransform(Matrix transform);
|
||||
void setMaterial(Material material) { this->material = material; };
|
||||
|
||||
@@ -24,6 +24,7 @@ Intersect::Intersect()
|
||||
Intersect::~Intersect()
|
||||
{
|
||||
/* Free stuff */
|
||||
free(this->list);
|
||||
}
|
||||
|
||||
void Intersect::add(Intersection i)
|
||||
|
||||
@@ -18,7 +18,7 @@ Pattern::Pattern(Colour a, Colour b): a(a), b(b)
|
||||
|
||||
Colour Pattern::patternAtObject(Shape *object, Tuple worldPoint)
|
||||
{
|
||||
Tuple objectPoint = object->inverseTransform * worldPoint;
|
||||
Tuple objectPoint = object->worldToObject(worldPoint);
|
||||
Tuple patternPoint = this->inverseTransform * objectPoint;
|
||||
|
||||
return this->patternAt(patternPoint);
|
||||
|
||||
@@ -27,18 +27,25 @@ Intersect Shape::intersect(Ray r)
|
||||
return this->localIntersect(this->invTransform(r));
|
||||
};
|
||||
|
||||
Tuple Shape::normalAt(Tuple point)
|
||||
Tuple Shape::normalToWorld(Tuple normalVector)
|
||||
{
|
||||
Tuple local_point = this->inverseTransform * point;
|
||||
|
||||
Tuple local_normal = this->localNormalAt(local_point);
|
||||
|
||||
Tuple world_normal = this->transposedInverseTransform * local_normal;
|
||||
Tuple world_normal = this->transposedInverseTransform * normalVector;
|
||||
|
||||
/* W may get wrong, so hack it. This is perfectly normal as we are using a 4x4 matrix instead of a 3x3 */
|
||||
world_normal.w = 0;
|
||||
|
||||
return world_normal.normalise();
|
||||
};
|
||||
|
||||
Tuple Shape::normalAt(Tuple point)
|
||||
{
|
||||
Tuple local_point = this->worldToObject(point);
|
||||
|
||||
Tuple local_normal = this->localNormalAt(local_point);
|
||||
|
||||
Tuple world_normal = this->normalToWorld(local_normal);
|
||||
|
||||
return world_normal;
|
||||
}
|
||||
|
||||
void Shape::updateTransform()
|
||||
|
||||
@@ -133,12 +133,34 @@ TEST(TestShape, Converting_a_normal_form_object_to_world_space)
|
||||
s.setTransform(translation(5, 0, 0));
|
||||
g2.addObject(&s);
|
||||
|
||||
Tuple p = s.normalToWorld(Point(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3));
|
||||
Tuple p = s.normalToWorld(Vector(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3));
|
||||
|
||||
/* Temporary lower the precision */
|
||||
set_equal_precision(0.0001);
|
||||
|
||||
ASSERT_EQ(p, Point(0.2857, 0.4286, -0.8571));
|
||||
ASSERT_EQ(p, Vector(0.2857, 0.4286, -0.8571));
|
||||
|
||||
set_equal_precision(FLT_EPSILON);
|
||||
}
|
||||
|
||||
|
||||
TEST(TestShape, Finding_the_normal_on_a_child_object)
|
||||
{
|
||||
Group g1 = Group();
|
||||
g1.setTransform(rotationY(M_PI / 2));
|
||||
Group g2 = Group();
|
||||
g2.setTransform(scaling(1, 2, 3));
|
||||
g1.addObject(&g2);
|
||||
Sphere s = Sphere();
|
||||
s.setTransform(translation(5, 0, 0));
|
||||
g2.addObject(&s);
|
||||
|
||||
Tuple p = s.normalAt(Point(1.7321, 1.1547, -5.5774));
|
||||
|
||||
/* Temporary lower the precision */
|
||||
set_equal_precision(0.0001);
|
||||
|
||||
ASSERT_EQ(p, Vector(0.2857, 0.4286, -0.8571));
|
||||
|
||||
set_equal_precision(FLT_EPSILON);
|
||||
}
|
||||
Reference in New Issue
Block a user