Chapter 6: Antialiasing

This commit is contained in:
Michael Smith
2019-01-21 12:27:52 +01:00
parent 0c01a876b0
commit 0e5ce7ec07
7 changed files with 161 additions and 29 deletions

46
code/sphere.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef SPHERE_H
#define SPHERE_H
#include "target.h"
class sphere: public target
{
public:
sphere() {};
sphere(vec3 cen, float r) : center(cen), radius(r) {};
virtual bool hit(const ray &r, float t_min, float t_max, hit_record &rec) const;
vec3 center;
float radius;
};
bool sphere::hit(const ray &r, float t_min, float t_max, hit_record &rec) const
{
vec3 oc = r.origin() - center;
float a = dot(r.direction(), r.direction());
float b = dot(oc, r.direction());
float c = dot(oc, oc) - radius * radius;
float discriminant = b * b - a * c;
if (discriminant > 0)
{
float temp = (-b - sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min)
{
rec.t = temp;
rec.p = r.point_at_parameter(rec.t);
rec.normal = (rec.p - center) / radius;
return true;
}
temp = (-b + sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min)
{
rec.t = temp;
rec.p = r.point_at_parameter(rec.t);
rec.normal = (rec.p - center) / radius;
return true;
}
}
return false;
}
#endif