From d9cf26902ea55b8526ef1aa326b326be8cd42c76 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Fri, 25 Oct 2024 17:47:45 +0200 Subject: [PATCH] chore: Cleanup --- lab1/.placeholder | 1 - lab1/Plane.cpp | 5 +- lab1/Sphere.cpp | 16 +++---- lab1/Triangle.cpp | 7 ++- lab1/TriangleSoup.cpp | 3 +- lab1/first_hit.cpp | 18 ++++---- lab1/viewing_ray.cpp | 5 +- src/DirectionalLight.cpp | 5 +- src/PointLight.cpp | 3 +- src/blinn_phong_shading.cpp | 91 +++++++++++++++++++++---------------- src/raycolor.cpp | 17 ++++--- src/reflect.cpp | 7 ++- 12 files changed, 93 insertions(+), 85 deletions(-) delete mode 100644 lab1/.placeholder diff --git a/lab1/.placeholder b/lab1/.placeholder deleted file mode 100644 index 22afa84..0000000 --- a/lab1/.placeholder +++ /dev/null @@ -1 +0,0 @@ -'cause Git doesn't like empty directories. \ No newline at end of file diff --git a/lab1/Plane.cpp b/lab1/Plane.cpp index 2c18def..b5061cc 100644 --- a/lab1/Plane.cpp +++ b/lab1/Plane.cpp @@ -2,11 +2,10 @@ #include "Ray.h" bool Plane::intersect( - const Ray & ray, const double min_t, double & t, Eigen::Vector3d & n) const -{ + const Ray &ray, const double min_t, double &t, Eigen::Vector3d &n) const { /* Based on * Computer Graphics, Chapter 4. Ray Tracing - * Peter Lamber & Glenn Van Wallendael + * Peter Lambert & Glenn Van Wallendael */ t = -1; diff --git a/lab1/Sphere.cpp b/lab1/Sphere.cpp index 3ffc9c0..c184f98 100644 --- a/lab1/Sphere.cpp +++ b/lab1/Sphere.cpp @@ -1,8 +1,8 @@ #include "Sphere.h" #include "Ray.h" + bool Sphere::intersect( - const Ray & ray, const double min_t, double & t, Eigen::Vector3d & n) const -{ + const Ray &ray, const double min_t, double &t, Eigen::Vector3d &n) const { /* Based on * Fundamentals in Computer Graphics - Fourth Edition, Chapter 4.4.1. Ray-Sphere Intersection. * Steve Marschner & Peter Shirley @@ -10,17 +10,17 @@ bool Sphere::intersect( /* Intersection points occur when points on the ray satisfy the implicit equation. */ - double a = ray.direction.dot(ray.direction); - double b = ray.direction.dot(ray.origin - center); - double c = (ray.origin - center).dot(ray.origin - center) - radius * radius; - double discriminant = (2 * b) * (2 * b) - 4 * a * c; + const double a = ray.direction.dot(ray.direction); + const double b = ray.direction.dot(ray.origin - center); + const double c = (ray.origin - center).dot(ray.origin - center) - radius * radius; + const double discriminant = (2 * b) * (2 * b) - 4 * a * c; /* If the discriminant is less than zero, the ray does not intersect with the sphere. */ if (discriminant < 0) return false; /* Calculate the first intersection t. */ - double under_root = b * b - a * c; - double ta = (0 - b - sqrt(under_root)) / a; + const double under_root = b * b - a * c; + const double ta = (0 - b - sqrt(under_root)) / a; double tb = (0 - b + sqrt(under_root)) / a; /* Choose the smallest that is still larger than min_t. */ if (ta < min_t && tb < min_t) { diff --git a/lab1/Triangle.cpp b/lab1/Triangle.cpp index 42538ff..f4318a5 100644 --- a/lab1/Triangle.cpp +++ b/lab1/Triangle.cpp @@ -3,8 +3,7 @@ #include // hint bool Triangle::intersect( - const Ray & ray, const double min_t, double & t, Eigen::Vector3d & n) const -{ + const Ray &ray, const double min_t, double &t, Eigen::Vector3d &n) const { /* Based on * Fundamentals in Computer Graphics - Fourth Edition, Chapter 4.4.2. Ray-Triangle Intersection. * Steve Marschner & Peter Shirley @@ -23,7 +22,7 @@ bool Triangle::intersect( A.col(0) = a - b; A.col(1) = a - c; A.col(2) = d; - double determinant_A = A.determinant(); + const double determinant_A = A.determinant(); Eigen::Matrix3d t_matrix; t_matrix.col(0) = a - b; @@ -56,4 +55,4 @@ bool Triangle::intersect( n = edge1.cross(edge2).normalized(); /* Don't forget to normalize the vector. */ return true; -} \ No newline at end of file +} diff --git a/lab1/TriangleSoup.cpp b/lab1/TriangleSoup.cpp index 26a258e..4b6cdc2 100644 --- a/lab1/TriangleSoup.cpp +++ b/lab1/TriangleSoup.cpp @@ -4,8 +4,7 @@ #include "first_hit.h" bool TriangleSoup::intersect( - const Ray & ray, const double min_t, double & t, Eigen::Vector3d & n) const -{ + const Ray &ray, const double min_t, double &t, Eigen::Vector3d &n) const { int hit_id; /* Ignored. */ return first_hit(ray, min_t, triangles, hit_id, t, n); } diff --git a/lab1/first_hit.cpp b/lab1/first_hit.cpp index d748cff..b133b2d 100644 --- a/lab1/first_hit.cpp +++ b/lab1/first_hit.cpp @@ -1,22 +1,21 @@ #include "first_hit.h" bool first_hit( - const Ray & ray, + const Ray &ray, const double min_t, - const std::vector< std::shared_ptr > & objects, - int & hit_id, - double & t, - Eigen::Vector3d & n) -{ + const std::vector > &objects, + int &hit_id, + double &t, + Eigen::Vector3d &n) { + double t_temp; + Eigen::Vector3d n_temp; + bool hit = false; /* Initialize to a value that will never be used. */ t = -1; /* Alternatively, use a max integer value, and remove the check t == -1. */ /* Store intermediate results that are not necessarily the first hit. */ - double t_temp; - Eigen::Vector3d n_temp; - for (int i = 0; i < objects.size(); ++i) { if (objects[i]->intersect(ray, min_t, t_temp, n_temp)) { hit = true; @@ -30,4 +29,3 @@ bool first_hit( return hit; } - diff --git a/lab1/viewing_ray.cpp b/lab1/viewing_ray.cpp index a643a9c..fb75b71 100644 --- a/lab1/viewing_ray.cpp +++ b/lab1/viewing_ray.cpp @@ -1,13 +1,12 @@ #include "viewing_ray.h" void viewing_ray( - const Camera & camera, + const Camera &camera, const int i, const int j, const int width, const int height, - Ray & ray) -{ + Ray &ray) { /* Based on * Fundamentals in Computer Graphics - Fourth Edition, Chapter 4.3.2 Perspective views. * Steve Marschner & Peter Shirley diff --git a/src/DirectionalLight.cpp b/src/DirectionalLight.cpp index 9c2c8b6..d251808 100644 --- a/src/DirectionalLight.cpp +++ b/src/DirectionalLight.cpp @@ -2,8 +2,7 @@ #include void DirectionalLight::direction( - const Eigen::Vector3d & q, Eigen::Vector3d & d, double & max_t) const -{ - d = - this->d; + const Eigen::Vector3d &q, Eigen::Vector3d &d, double &max_t) const { + d = -this->d; max_t = std::numeric_limits::infinity(); } diff --git a/src/PointLight.cpp b/src/PointLight.cpp index 9dafbf2..0c4162d 100644 --- a/src/PointLight.cpp +++ b/src/PointLight.cpp @@ -1,8 +1,7 @@ #include "PointLight.h" void PointLight::direction( - const Eigen::Vector3d & q, Eigen::Vector3d & d, double & max_t) const -{ + const Eigen::Vector3d &q, Eigen::Vector3d &d, double &max_t) const { d = (p - q); /* Direction towards the light */ max_t = d.norm(); /* Distance to the light */ d.normalize(); /* Normalize the direction */ diff --git a/src/blinn_phong_shading.cpp b/src/blinn_phong_shading.cpp index ab2c44d..8bd6354 100644 --- a/src/blinn_phong_shading.cpp +++ b/src/blinn_phong_shading.cpp @@ -2,51 +2,56 @@ // Hint: #include "first_hit.h" -const double AMBIENT_INTENSITY = 0.1; -const double FUDGE_FACTOR = 1e-6; +// Hint: use I_a = 0.1 to get a subtle, ambient light contribution. +constexpr double AMBIENT_INTENSITY = 0.1; +constexpr double FUDGE_FACTOR = 1e-6; /* Helper functions */ Eigen::Vector3d lambert( - const Eigen::Vector3d& n, - const Eigen::Vector3d& kd, - const Eigen::Vector3d& l, - const Eigen::Vector3d& I); -Eigen::Vector3d blinn_phong( - const Eigen::Vector3d& v, - const Eigen::Vector3d& n, - const Eigen::Vector3d& ks, - double p, - const Eigen::Vector3d& l, - const Eigen::Vector3d& I); + const Eigen::Vector3d &n, + const Eigen::Vector3d &kd, + const Eigen::Vector3d &l, + const Eigen::Vector3d &I); + +Eigen::Vector3d blinn_phong( + const Eigen::Vector3d &v, + const Eigen::Vector3d &n, + const Eigen::Vector3d &ks, + double p, + const Eigen::Vector3d &l, + const Eigen::Vector3d &I); -// Hint: use I_a = 0.1 to get a subtle, ambient light contribution. Eigen::Vector3d blinn_phong_shading( - const Ray & ray, - const int & hit_id, - const double & t, - const Eigen::Vector3d & n, - const std::vector< std::shared_ptr > & objects, - const std::vector > & lights) -{ - Eigen::Vector3d rgb, light_direction, _n_shadow; + const Ray &ray, + const int &hit_id, + const double &t, + const Eigen::Vector3d &n, + const std::vector > &objects, + const std::vector > &lights) { + /* Based on + * Fundamentals of Computer Graphics - Fourth Edition, Chapter 4.5. Shading and Chapter 4.7. Shadows. + * Steve Marschner & Peter Shirley + */ + Eigen::Vector3d light_direction, _n_shadow; Ray shadow_ray; double max_t, _t_shadow; int shadow_id; - Eigen::Vector3d v = -ray.direction.normalized(); - Eigen::Vector3d hit = ray.origin + t * ray.direction; + const Eigen::Vector3d v = -ray.direction.normalized(); + const Eigen::Vector3d hit = ray.origin + t * ray.direction; shadow_ray.origin = hit; - std::shared_ptr material = objects[hit_id]->material; + const std::shared_ptr material = objects[hit_id]->material; /* Ambient lighting. */ - rgb = material->ka * AMBIENT_INTENSITY; + Eigen::Vector3d rgb = material->ka * AMBIENT_INTENSITY; - for (const auto & light : lights) { + for (const auto &light: lights) { light->direction(hit, light_direction, max_t); /* Shadows */ shadow_ray.direction = light_direction; - bool shadow = first_hit(shadow_ray, FUDGE_FACTOR, objects, shadow_id, _t_shadow, _n_shadow) && _t_shadow < max_t; + const bool shadow = first_hit(shadow_ray, FUDGE_FACTOR, objects, shadow_id, _t_shadow, _n_shadow) && _t_shadow < + max_t; if (!shadow) { /* Diffuse component (Lambertian shading) */ rgb += lambert(n, material->kd, light_direction, light->I); @@ -62,6 +67,10 @@ Eigen::Vector3d blinn_phong_shading( /** * Compute the Lambertian shading model. * + * Based on + * Fundamentals of Computer Graphics - Fourth Edition, Chapter 4.5.1. Lambertian Shading. + * Steve Marschner & Peter Shirley + * * Inputs: * n surface normal * kd diffuse reflection coefficient @@ -69,17 +78,20 @@ Eigen::Vector3d blinn_phong_shading( * I intensity of light source */ Eigen::Vector3d lambert( - const Eigen::Vector3d& n, - const Eigen::Vector3d& kd, - const Eigen::Vector3d& l, - const Eigen::Vector3d& I) -{ + const Eigen::Vector3d &n, + const Eigen::Vector3d &kd, + const Eigen::Vector3d &l, + const Eigen::Vector3d &I) { return (kd.array() * I.array()).matrix() * std::max(0.0, n.dot(l)); } /** * Compute the Blinn-Phong shading model. * + * Based on + * Fundamentals of Computer Graphics - Fourth Edition, Chapter 4.5.2. Blinn-Phong Shading. + * Steve Marschner & Peter Shirley + * * Inputs: * v direction to viewer * n surface normal @@ -89,14 +101,13 @@ Eigen::Vector3d lambert( * I intensity of light source */ Eigen::Vector3d blinn_phong( - const Eigen::Vector3d& v, - const Eigen::Vector3d& n, - const Eigen::Vector3d& ks, + const Eigen::Vector3d &v, + const Eigen::Vector3d &n, + const Eigen::Vector3d &ks, const double p, - const Eigen::Vector3d& l, - const Eigen::Vector3d& I) -{ + const Eigen::Vector3d &l, + const Eigen::Vector3d &I) { /* Calculate the half vector of v and l. */ - Eigen::Vector3d h = (v + l).normalized(); + const Eigen::Vector3d h = (v + l).normalized(); return (ks.array() * I.array()).matrix() * std::pow(std::max(0.0, n.dot(h)), p); } diff --git a/src/raycolor.cpp b/src/raycolor.cpp index ed76da5..d56b609 100644 --- a/src/raycolor.cpp +++ b/src/raycolor.cpp @@ -3,20 +3,23 @@ #include "blinn_phong_shading.h" #include "reflect.h" -const double FUDGE_FACTOR = 1e-6; +constexpr double FUDGE_FACTOR = 1e-6; // Hint: for the mirror reflections, you can use raycolor recursively. That is what 'num_recursive_calls' is for. // The maximum number of recursive calls should be 10. In other words, to get the color of a mirror reflection, // stop after 10 bounces. // This is of course only important when you hand in your code. In the meantime, to speed stuff up, you can lower it to 5. bool raycolor( - const Ray & ray, + const Ray &ray, const double min_t, - const std::vector< std::shared_ptr > & objects, - const std::vector< std::shared_ptr > & lights, + const std::vector > &objects, + const std::vector > &lights, const int num_recursive_calls, - Eigen::Vector3d & rgb) -{ + Eigen::Vector3d &rgb) { + /* Based on + * Fundamentals of Computer Graphics - Fourth Edition, Chapter 4.6. A Ray-Tracing Program. + * Steve Marschner & Peter Shirley + */ int hit_id; double t; Eigen::Vector3d n, reflect_rgb; @@ -24,7 +27,7 @@ bool raycolor( /* Compute viewing array. */ if (!first_hit(ray, min_t, objects, hit_id, t, n)) { - rgb = Eigen::Vector3d(0,0,0); + rgb = Eigen::Vector3d(0, 0, 0); return false; } diff --git a/src/reflect.cpp b/src/reflect.cpp index 9c23305..4b6ef9a 100644 --- a/src/reflect.cpp +++ b/src/reflect.cpp @@ -1,6 +1,9 @@ #include -Eigen::Vector3d reflect(const Eigen::Vector3d & in, const Eigen::Vector3d & n) -{ +Eigen::Vector3d reflect(const Eigen::Vector3d &in, const Eigen::Vector3d &n) { + /* Based on + * Fundamentals of Computer Graphics - Fourth Edition, Chapter 4.8. Ideal Specular Reflection. + * Steve Marschner & Peter Shirley + */ return in - 2 * in.dot(n) * n; }