Midway hittable list inclusion
This commit is contained in:
parent
3aea707982
commit
8c1324c074
6 changed files with 141 additions and 2 deletions
2
Makefile
2
Makefile
|
|
@ -1,4 +1,4 @@
|
||||||
raytracer: main.cpp vec3.hpp color.hpp ray.hpp
|
raytracer: main.cpp vec3.hpp color.hpp ray.hpp hittable.hpp sphere.hpp hittable_list.hpp
|
||||||
@g++ -g -Wall -Wextra -Wpedantic main.cpp -o raytracer
|
@g++ -g -Wall -Wextra -Wpedantic main.cpp -o raytracer
|
||||||
|
|
||||||
image: raytracer
|
image: raytracer
|
||||||
|
|
|
||||||
16
hittable.cpp
Normal file
16
hittable.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef HITTABLE_H
|
||||||
|
#define HITTABLE_H
|
||||||
|
|
||||||
|
#include "ray.h"
|
||||||
|
|
||||||
|
struct hit_record {
|
||||||
|
point3 p;
|
||||||
|
vec3 normal;
|
||||||
|
double t;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct hittable {
|
||||||
|
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
25
hittable.hpp
Normal file
25
hittable.hpp
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef HITTABLE_H
|
||||||
|
#define HITTABLE_H
|
||||||
|
|
||||||
|
#include "ray.hpp"
|
||||||
|
|
||||||
|
/* Representation of a hitpoint of a ray against a hittable object */
|
||||||
|
struct hit_record {
|
||||||
|
point3 p;
|
||||||
|
vec3 normal;
|
||||||
|
double t;
|
||||||
|
bool front_face;
|
||||||
|
|
||||||
|
inline void set_face_normal(const ray& r, const vec3& outward_normal)
|
||||||
|
{
|
||||||
|
front_face = dot(r.direction, outward_normal) < 0;
|
||||||
|
normal = front_face ? outward_normal : -outward_normal;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Virtual class that represents objects who could collide against a ray */
|
||||||
|
struct hittable {
|
||||||
|
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
45
hittable_list.hpp
Normal file
45
hittable_list.hpp
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
#ifndef HITTABLE_LIST_H
|
||||||
|
#define HITTABLE_LIST_H
|
||||||
|
|
||||||
|
#include "hittable.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using std::shared_ptr;
|
||||||
|
using std::make_shared;
|
||||||
|
|
||||||
|
struct hittable_list : hittable {
|
||||||
|
/* Attributes */
|
||||||
|
std::vector<shared_ptr<hittable>> objects;
|
||||||
|
|
||||||
|
/* Constructor */
|
||||||
|
hittable_list(shared_ptr<hittable> h) { add(h); }
|
||||||
|
|
||||||
|
/* Methods */
|
||||||
|
void clear() { objects.clear(); }
|
||||||
|
void add (shared_ptr<hittable> h) { objects.push_back(h); }
|
||||||
|
|
||||||
|
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool hittable_list::hit(const ray& r, double t_min, double t_max, hit_record& rec) const
|
||||||
|
{
|
||||||
|
hit_record temp_rec;
|
||||||
|
bool hit_anything = false;
|
||||||
|
double closest_so_far = t_max;
|
||||||
|
|
||||||
|
for (const auto& object : objects)
|
||||||
|
{
|
||||||
|
if (object->hit(r, t_min, closest_so_far, temp_rec))
|
||||||
|
{
|
||||||
|
hit_anything = true;
|
||||||
|
closest_so_far = temp_rec.t;
|
||||||
|
rec = temp_rec;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hit_anything;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
53
sphere.hpp
Normal file
53
sphere.hpp
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
#ifndef SPHERE_H
|
||||||
|
#define SPHERE_H
|
||||||
|
|
||||||
|
#include "hittable.hpp"
|
||||||
|
#include "vec3.hpp"
|
||||||
|
|
||||||
|
struct sphere : hittable {
|
||||||
|
/* Attributes */
|
||||||
|
point3 center;
|
||||||
|
double radius;
|
||||||
|
|
||||||
|
/* Contructor */
|
||||||
|
sphere(point3 c, double r)
|
||||||
|
{
|
||||||
|
center = c;
|
||||||
|
radius = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Virtual methods declaration */
|
||||||
|
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Virtual method implementations */
|
||||||
|
bool sphere::hit(const ray& r, double t_min, double t_max, hit_record& rec) const
|
||||||
|
{
|
||||||
|
vec3 oc = r.origin - center;
|
||||||
|
double a = r.direction.length_squared();
|
||||||
|
double half_b = dot(oc, r.direction);
|
||||||
|
double c = oc.length_squared() - radius*radius;
|
||||||
|
|
||||||
|
double discriminant = half_b*half_b - a*c;
|
||||||
|
if (discriminant < 0)
|
||||||
|
return false;
|
||||||
|
double sqrtd = sqrt(discriminant);
|
||||||
|
|
||||||
|
// Find the nearest root that lies in the acceptable range
|
||||||
|
double root = (-half_b - sqrtd) / a;
|
||||||
|
if (root < t_min || t_max < root)
|
||||||
|
{
|
||||||
|
root = (-half_b + sqrtd) / a;
|
||||||
|
if (root < t_min || t_max < root)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
rec.t = root;
|
||||||
|
rec.p = r.at(rec.t);
|
||||||
|
vec3 outward_normal = (rec.p - center) / radius;
|
||||||
|
rec.set_face_normal(r, outward_normal);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
2
vec3.hpp
2
vec3.hpp
|
|
@ -21,7 +21,7 @@ struct vec3 {
|
||||||
/* Overriden operators */
|
/* Overriden operators */
|
||||||
|
|
||||||
// - operator. Not to be confused with substraction
|
// - operator. Not to be confused with substraction
|
||||||
vec3 operator-()
|
vec3 operator-() const
|
||||||
{
|
{
|
||||||
return vec3(-x, -y, -z);
|
return vec3(-x, -y, -z);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue