diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | src/camera.cpp | 28 | ||||
| -rw-r--r-- | src/dielectric.cpp | 32 | ||||
| -rw-r--r-- | src/hittable_list.cpp | 28 | ||||
| -rw-r--r-- | src/lambertian.cpp | 18 | ||||
| -rw-r--r-- | src/main.cpp | 2 | ||||
| -rw-r--r-- | src/metal.cpp | 14 | ||||
| -rw-r--r-- | src/ray.cpp | 11 | ||||
| -rw-r--r-- | src/sphere.cpp | 38 |
9 files changed, 11 insertions, 165 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c185ebe..1fd25b7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,13 +6,8 @@ add_executable( main.cpp vec3.cpp color.cpp - ray.cpp sphere.cpp - hittable_list.cpp camera.cpp - lambertian.cpp - metal.cpp - dielectric.cpp vulkan-loader.cpp utils.cpp vk-renderer.cpp diff --git a/src/camera.cpp b/src/camera.cpp index c7e2e35..6cb7b83 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -5,34 +5,22 @@ camera::camera( point3 position, point3 target, - double v_fov, - double aspect_ratio, - double aperture, - double focus_dist + float v_fov, + float aspect_ratio, + float aperture, + float focus_dist ) : position(position), lens_radius(aperture / 2.0) { - forward = (target - position).unit(); + auto forward = (target - position).unit(); right = forward.cross(vec3{ 0.0, 1.0, 0.0 }).unit(); up = right.cross(forward).unit(); auto h = tan(deg_to_rad(v_fov) / 2.0); - viewport_height = 2.0 * h; - viewport_width = viewport_height * aspect_ratio; + auto viewport_height = 2.0 * h; + auto viewport_width = viewport_height * aspect_ratio; horizontal = focus_dist * viewport_width * right; vertical = focus_dist * viewport_height * up; - bottom_left_pixel = position - horizontal / 2.0 - vertical / 2.0 - forward * focus_dist; -} - -ray camera::get_ray(double u, double v) const { - vec3 rd = lens_radius * random_in_unit_disk(); - vec3 offset = rd.x * right + rd.y * up; - - point3 viewport_pixel = bottom_left_pixel + u * horizontal + v * vertical; - - return ray { - position + offset, - viewport_pixel - position - offset - }; + first_pixel = position - horizontal / 2.0 - vertical / 2.0 + focus_dist * forward; } diff --git a/src/dielectric.cpp b/src/dielectric.cpp deleted file mode 100644 index 80ca069..0000000 --- a/src/dielectric.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "dielectric.hpp" -#include "rt.hpp" -#include "hittable.hpp" - -dielectric::dielectric(double index_of_refraction) - : ior(index_of_refraction) {} - -bool dielectric::scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const { - auto unit_direction = r.direction.unit(); - auto refraction_ratio = info.front_face ? (1.0 / ior) : ior; - double cos_theta = fmin(info.normal.dot(-unit_direction), 1.0); - double sin_theta = sqrt(1.0 - cos_theta * cos_theta); - - vec3 direction; - if (refraction_ratio * sin_theta > 1.0 || reflectance(cos_theta, refraction_ratio) > randd()) { - direction = reflect(unit_direction, info.normal); - } else { - direction = refract(unit_direction, info.normal, refraction_ratio); - } - - scattered = ray { info.point, direction }; - attenuation = { 1.0, 1.0, 1.0 }; - - return true; -} - -double dielectric::reflectance(double cosine, double ref_idx) { - // Use Schlick's approximation for reflectance. - auto r0 = (1 - ref_idx) / (1 + ref_idx); - r0 = r0 * r0; - return r0 + (1 - r0) * pow ((1 - cosine), 5); -} diff --git a/src/hittable_list.cpp b/src/hittable_list.cpp deleted file mode 100644 index 9140304..0000000 --- a/src/hittable_list.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "hittable_list.hpp" - -hittable_list::hittable_list() {}; - -void hittable_list::add(std::shared_ptr<hittable> hittable) { - hittables.push_back(hittable); -} - -void hittable_list::clear() { - hittables.clear(); -} - - -bool hittable_list::hit(const ray& r, double t_min, double t_max, struct hit_info& info) const { - struct hit_info temp_info; - bool hit_anything = false; - double closest_hit = t_max; - - for (auto& hittable: hittables) { - if (hittable->hit(r, t_min, closest_hit, temp_info)) { - hit_anything = true; - closest_hit = temp_info.t; - info = temp_info; - } - } - - return hit_anything; -} diff --git a/src/lambertian.cpp b/src/lambertian.cpp deleted file mode 100644 index 16ab146..0000000 --- a/src/lambertian.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "lambertian.hpp" -#include "hittable.hpp" - -lambertian::lambertian(const color& albedo) : albedo(albedo) {} - -bool lambertian::scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const { - // auto scatter_direction = info.normal + random_in_unit_sphere(); - // auto scatter_direction = info.normal + random_unit_vector(); - auto scatter_direction = info.normal + random_in_hemisphere(info.normal); - - if (scatter_direction.near_zero()) { - scatter_direction = info.normal; - } - - scattered = ray { info.point, scatter_direction }; - attenuation = albedo; - return true; -} diff --git a/src/main.cpp b/src/main.cpp index 06a602a..03706b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,7 +98,7 @@ int main() { .sky_color = sky_color, .ground_color = ground_color, - .cam = { camera_position, camera.up, camera.right, camera.forward, camera.viewport_width, camera.viewport_height, camera.lens_radius, distance_to_focus }, + .cam = camera, .spheres = {}, diff --git a/src/metal.cpp b/src/metal.cpp deleted file mode 100644 index 196c95d..0000000 --- a/src/metal.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "metal.hpp" -#include "hittable.hpp" -#include "vec3.hpp" - -metal::metal(const color& albedo, double f) - : albedo(albedo), fuzz(f < 1 ? f : 1) {} - -bool metal::scatter(const ray& r, const struct hit_info& info, color& attenuation, ray& scattered) const { - auto reflected = reflect(r.direction.unit(), info.normal); - - scattered = ray { info.point, reflected + fuzz * random_in_unit_sphere() }; - attenuation = albedo; - return (scattered.direction.dot(info.normal) > 0.0); -} diff --git a/src/ray.cpp b/src/ray.cpp deleted file mode 100644 index 9a8bd36..0000000 --- a/src/ray.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "ray.hpp" - -ray::ray() -{} - -ray::ray(const point3& origin, const vec3& direction): origin(origin), direction(direction) -{} - -point3 ray::at(double t) const { - return origin + direction * t; -} diff --git a/src/sphere.cpp b/src/sphere.cpp index e7be715..7ac4185 100644 --- a/src/sphere.cpp +++ b/src/sphere.cpp @@ -1,39 +1,5 @@ #include "sphere.hpp" -sphere::sphere(point3 center, double radius, std::shared_ptr<material> material) - : center(center), radius(radius), mat_ptr(material) +sphere::sphere(point3 center, material material, float radius) + : center(center), radius(radius), mat(material) {} - -bool sphere::hit(const ray& r, double t_min, double t_max, struct hit_info& info) const { - // Vector from the ray origin to sphere position - vec3 origin_center = r.origin - center; - - double a = r.direction.dot(r.direction); - double half_b = r.direction.dot(origin_center); - double c = origin_center.dot(origin_center) - radius * radius; - double discriminant = half_b * half_b - a * c; - - if (discriminant < 0) { - return false; - } - - double sq_discriminant = sqrt(discriminant); - double t = (-half_b - sq_discriminant) / a ; - - if (t_min > t || t_max < t) { - t = (-half_b + sq_discriminant) / a ; - - if (t_min > t || t_max < t) { - return false; - } - } - - info.t = t; - info.point = r.at(t); - info.mat_ptr = mat_ptr; - - vec3 out_normal = (info.point - center) / radius; - info.set_face_normal(r, out_normal); - - return true; -} |
