Add function to calculate sphere normal vector on given point on the sphere.
This commit is contained in:
@@ -27,6 +27,8 @@ public:
|
||||
Object();
|
||||
|
||||
virtual Intersect intersect(Ray r);
|
||||
virtual Tuple normalAt(Tuple point);
|
||||
|
||||
void setTransform(Matrix transform);
|
||||
Ray transform(Ray r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
|
||||
Ray invTransform(Ray r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
|
||||
|
||||
@@ -18,6 +18,7 @@ class Sphere : public Object
|
||||
public:
|
||||
/* All sphere are at (0, 0, 0) and radius 1 in the object space */
|
||||
virtual Intersect intersect(Ray r);
|
||||
virtual Tuple normalAt(Tuple point);
|
||||
};
|
||||
|
||||
#endif //DORAYME_SPHERE_H
|
||||
|
||||
@@ -24,6 +24,11 @@ Intersect Object::intersect(Ray r)
|
||||
return Intersect();
|
||||
};
|
||||
|
||||
Tuple Object::normalAt(Tuple point)
|
||||
{
|
||||
return Vector(0, 0, 0);
|
||||
}
|
||||
|
||||
void Object::setTransform(Matrix transform)
|
||||
{
|
||||
this->transformMatrix = transform;
|
||||
|
||||
@@ -35,4 +35,16 @@ Intersect Sphere::intersect(Ray r)
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Tuple Sphere::normalAt(Tuple point)
|
||||
{
|
||||
Tuple object_point = this->inverseTransform * point;
|
||||
Tuple object_normal = (object_point - Point(0, 0, 0)).normalise();
|
||||
Tuple world_normal = this->inverseTransform.transpose() * object_normal;
|
||||
|
||||
/* 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();
|
||||
}
|
||||
Reference in New Issue
Block a user