From 318a802be749d937445f5255d71de5fbb1a12a0b Mon Sep 17 00:00:00 2001 From: niccolot Date: Thu, 6 Feb 2025 07:10:49 -0600 Subject: [PATCH] now nonskippdf materials can be added to lights hittable list --- .gitignore | 2 ++ src/TheRestOfYourLife/hittable_list.h | 11 ++++++++--- src/TheRestOfYourLife/main.cc | 9 +++++---- src/TheRestOfYourLife/sphere.h | 9 ++++++--- src/TheRestOfYourLife/vec3.h | 4 ++++ 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 66027362..ca224b54 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ build/ /*.ppm + +.vscode/ \ No newline at end of file diff --git a/src/TheRestOfYourLife/hittable_list.h b/src/TheRestOfYourLife/hittable_list.h index 5d331821..88b23498 100644 --- a/src/TheRestOfYourLife/hittable_list.h +++ b/src/TheRestOfYourLife/hittable_list.h @@ -59,9 +59,14 @@ class hittable_list : public hittable { return sum; } - vec3 random(const point3& origin) const override { - auto int_size = int(objects.size()); - return objects[random_int(0, int_size-1)]->random(origin); + vec3 random(const point3& origin) const override { + int int_size = int(objects.size()); + auto vec = objects[random_int(0, int_size-1)]->random(origin); + while (vec.is_nan()) { + vec = objects[random_int(0, int_size-1)]->random(origin); + } + + return vec; } private: diff --git a/src/TheRestOfYourLife/main.cc b/src/TheRestOfYourLife/main.cc index 011d942c..aeda60c9 100644 --- a/src/TheRestOfYourLife/main.cc +++ b/src/TheRestOfYourLife/main.cc @@ -44,7 +44,8 @@ int main() { // Glass Sphere auto glass = make_shared(1.5); - world.add(make_shared(point3(190,90,190), 90, glass)); + auto nonglass = make_shared(color(0.8, 0.2, 0.4)); + world.add(make_shared(point3(190,90,190), 90, nonglass)); // Light Sources auto empty_material = shared_ptr(); @@ -56,9 +57,9 @@ int main() { camera cam; cam.aspect_ratio = 1.0; - cam.image_width = 600; - cam.samples_per_pixel = 100; - cam.max_depth = 50; + cam.image_width = 400; + cam.samples_per_pixel = 1000; + cam.max_depth = 10; cam.background = color(0,0,0); cam.vfov = 40; diff --git a/src/TheRestOfYourLife/sphere.h b/src/TheRestOfYourLife/sphere.h index a1ddd1fa..2879682d 100644 --- a/src/TheRestOfYourLife/sphere.h +++ b/src/TheRestOfYourLife/sphere.h @@ -76,11 +76,14 @@ class sphere : public hittable { if (!this->hit(ray(origin, direction), interval(0.001, infinity), rec)) return 0; - auto dist_squared = (center.at(0) - origin).length_squared(); - auto cos_theta_max = std::sqrt(1 - radius*radius/dist_squared); + auto cos_theta_max = sqrt(1 - radius*radius/(center.at(0) - origin).length_squared()); auto solid_angle = 2*pi*(1-cos_theta_max); - return 1 / solid_angle; + if (std::isnan(solid_angle) || std::fabs(solid_angle) < 1e-3) { + return 0; + } else { + return 1 / solid_angle; + } } vec3 random(const point3& origin) const override { diff --git a/src/TheRestOfYourLife/vec3.h b/src/TheRestOfYourLife/vec3.h index a459bee7..c106fd26 100644 --- a/src/TheRestOfYourLife/vec3.h +++ b/src/TheRestOfYourLife/vec3.h @@ -59,6 +59,10 @@ class vec3 { return (std::fabs(e[0]) < s) && (std::fabs(e[1]) < s) && (std::fabs(e[2]) < s); } + bool is_nan() const { + return std::isnan(e[0]) || std::isnan(e[1]) || std::isnan(e[2]); + } + static vec3 random() { return vec3(random_double(), random_double(), random_double()); }