rendered paste bodyvoid makePlane(Object *obj, Vector *norm, Scalar d) {
assert(obj != NULL);
Plane *p = malloc(sizeof(Plane));
p->norm = *norm;
p->d = d;
obj->priv = p;
obj->type = PLANE;
obj->hits = hitsPlane;
obj->normalAt = normalAtPlane;
}
// Determine if the ray hits the plane
// If no intersection, NO_HIT is returned
// We are assuming one sided planes
Scalar hitsPlane(Object *obj, Ray *r)
{
assert(obj != NULL);
assert(r != NULL);
assert(obj->type == PLANE);
Plane *plane = (Plane *) obj->priv;
Scalar v1= v3Dot(&r->dir, &plane->norm);
// First, check for parallel rays, then check for hiting the wrong side
if (fabs(v1) < THRESHOLD) {
return NO_HIT;
}
Scalar v2 = -(v3Dot(&plane->norm, &r->p) + plane->d);
return v2/v1;
}
// Return the Plane's normal
Vector normalAtPlane(Object *obj, Vector *hit)
{
assert(obj != NULL);
assert(obj->type == PLANE);
Plane *plane = (Plane *) obj->priv;
return plane->norm;
}