diff options
author | Julian T <julian@jtle.dk> | 2021-01-24 18:13:44 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-01-24 18:22:11 +0100 |
commit | 41ea2d8f94043d49fc26aaaa8030a73dcfdc2da9 (patch) | |
tree | fcc07052425c65b38c1724f4fc30299a1cb48716 /src/object.cpp | |
parent | a38e6014ea5441e9d29fcb3b5607cd94e4061cff (diff) |
Create rust project
Diffstat (limited to 'src/object.cpp')
-rw-r--r-- | src/object.cpp | 103 |
1 files changed, 0 insertions, 103 deletions
diff --git a/src/object.cpp b/src/object.cpp deleted file mode 100644 index 1449cbb..0000000 --- a/src/object.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "object.hpp" - -#include <math.h> -#include <iostream> -#include "common.hpp" - -void Color::clamp() { - if (m_x > 1) { m_x = 1; } - if (m_y > 1) { m_y = 1; } - if (m_z > 1) { m_z = 1; } -} - -Material::Material(Color color, double defuse, double emissive) { - m_color = color; - m_defuse = defuse; - m_emissive = emissive; -} - -Color Material::reflect(const Vec3d &normal, const Vec3d &in, const Vec3d &out, const Color &incol) const { - return Vec3d(m_color) * m_emissive + - (Vec3d(m_color) * Vec3d(incol)) * (out.dot(normal) * m_defuse); -} - -Sphere::Sphere(const Material &mat, Vec3d center, double radius) : Shape(mat) { - m_center = center; - m_radius = radius; -} - -Plane::Plane(const Material &mat, Vec3d start, Vec3d norm) : Shape(mat) { - m_start = start; - m_norm = norm; - - m_norm.normalize(); -} - -Vec3d Sphere::norm_at(const Vec3d &point, const Vec3d&) const { - auto res = point - m_center; - res.normalize(); - return res; -} - -// https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection -double Sphere::intersect(const Ray &ray, bool skip_dist) const { - // Calculate O - C used multiple places - auto oc = ray.m_start - m_center; - // Calculate components of quadratic formula - // a = 1 when ray.direction is a unit vector - auto a = 1; - auto b = 2 * ray.m_direction.dot(oc); - auto c = oc.dot(oc) - m_radius * m_radius; - - // Solve quadratic function - auto discr = b * b - 4 * a * c; - if (discr < 0) { - // No solution - return -1; - } - if (skip_dist) { - // Do not calculate distance - return 1; - } - - auto q = (b > 0) ? - -0.5 * (b + sqrt(discr)): - -0.5 * (b - sqrt(discr)); - auto t1 = q; // Assuming a = 1 - auto t0 = c / q; - - // Find correct result - if (t0 <= ZERO_APPROX) { - t0 = t1; - } - - if (t0 <= ZERO_APPROX) { - return -1; - } - - return t0; -} - -Vec3d Plane::norm_at(const Vec3d&, const Vec3d &indir) const { - auto scale = m_norm.dot(indir); - return scale > 0 ? -m_norm : m_norm; -} - -// https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-plane-and-ray-disk-intersection -// Requires that vectors are normalized -// Skip dist is ignored as distance must be calculated -double Plane::intersect(const Ray &ray, bool) const { - // If ray is parallel - auto nr = m_norm.dot(ray.m_direction); - if (abs(nr) < ZERO_APPROX) { - return -1; - } - - // Calculate distance - auto dist = m_norm.dot(m_start - ray.m_start) / nr; - if (dist < 0) { - return -1; - } - - return dist; -} |