chore: Cleanup
Some checks failed
CMake / build (zip, zip, [self-hosted Linux], raytracing, ux) (push) Has been cancelled
CMake / build (zip, zip, [self-hosted Windows], Release/raytracing.exe, win) (push) Has been cancelled

This commit is contained in:
Tibo De Peuter 2024-10-25 17:47:45 +02:00
parent 023a1db4f1
commit d9cf26902e
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
12 changed files with 93 additions and 85 deletions

View file

@ -1 +0,0 @@
'cause Git doesn't like empty directories.

View file

@ -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;

View file

@ -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) {

View file

@ -3,8 +3,7 @@
#include <Eigen/Geometry> // 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;
}
}

View file

@ -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);
}

View file

@ -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<Object> > & objects,
int & hit_id,
double & t,
Eigen::Vector3d & n)
{
const std::vector<std::shared_ptr<Object> > &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;
}

View file

@ -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

View file

@ -2,8 +2,7 @@
#include <limits>
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<double>::infinity();
}

View file

@ -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 */

View file

@ -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<Object> > & objects,
const std::vector<std::shared_ptr<Light> > & 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<std::shared_ptr<Object> > &objects,
const std::vector<std::shared_ptr<Light> > &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> material = objects[hit_id]->material;
const std::shared_ptr<Material> 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);
}

View file

@ -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<Object> > & objects,
const std::vector< std::shared_ptr<Light> > & lights,
const std::vector<std::shared_ptr<Object> > &objects,
const std::vector<std::shared_ptr<Light> > &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;
}

View file

@ -1,6 +1,9 @@
#include <Eigen/Core>
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;
}