diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a58964..3636658 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,8 @@ cmake_minimum_required(VERSION 3.10) project(snoopy) -file(GLOB snoopy_src - "src/*.h" - "src/*.cpp" -) +include_directories("include") +file(GLOB SOURCES "src/*.cpp") -add_executable(snoopy ${snoopy_src}) +add_executable(snoopy ${SOURCES}) diff --git a/include/camera.h b/include/camera.h new file mode 100644 index 0000000..0f68329 --- /dev/null +++ b/include/camera.h @@ -0,0 +1,52 @@ +#pragma once + +#include "rtweekend.h" +#include "image.h" + +class camera +{ + public: + camera( + point3 lookfrom, + point3 lookat, + vec3 vup, + double vfov, // vertical field of view in degrees + double aspect_ratio, + double aperture, + double focus_dist) + { + auto theta = degrees_to_radians(vfov); + auto h = tan(theta/2); + auto viewport_height = 2.0 * h; + auto viewport_width = aspect_ratio * viewport_height; + + w_ = unit_vector(lookfrom - lookat); + u_ = unit_vector(cross(vup, w_)); + v_ = cross(w_, u_); + + origin_ = lookfrom; + horizontal_ = focus_dist * viewport_width * u_; + vertical_ = focus_dist * viewport_height * v_; + lower_left_corner_ = origin_ - horizontal_/2 - vertical_/2 - focus_dist * w_; + + lens_radius_ = aperture / 2; + } + + ray get_ray(double s, double t) const + { + vec3 rd = lens_radius_ * random_in_unit_disk(); + vec3 offset = (u_ * rd.x()) + (v_ * rd.y()); + + return ray( + origin_ + offset, + lower_left_corner_ + s*horizontal_ + t*vertical_ - origin_ - offset); + } + + private: + point3 origin_; + point3 lower_left_corner_; + vec3 horizontal_; + vec3 vertical_; + vec3 u_, v_, w_; + double lens_radius_; +}; diff --git a/src/colour.h b/include/colour.h similarity index 100% rename from src/colour.h rename to include/colour.h diff --git a/src/error.h b/include/error.h similarity index 100% rename from src/error.h rename to include/error.h diff --git a/include/foo.h b/include/foo.h new file mode 100644 index 0000000..604782e --- /dev/null +++ b/include/foo.h @@ -0,0 +1 @@ +#include diff --git a/src/hittable.h b/include/hittable.h similarity index 100% rename from src/hittable.h rename to include/hittable.h diff --git a/src/hittable_list.h b/include/hittable_list.h similarity index 100% rename from src/hittable_list.h rename to include/hittable_list.h diff --git a/src/image.h b/include/image.h similarity index 100% rename from src/image.h rename to include/image.h diff --git a/src/material.h b/include/material.h similarity index 100% rename from src/material.h rename to include/material.h diff --git a/src/rtweekend.h b/include/math.h similarity index 71% rename from src/rtweekend.h rename to include/math.h index e79e95a..0a27e02 100644 --- a/src/rtweekend.h +++ b/include/math.h @@ -1,22 +1,10 @@ #pragma once #include -#include -#include -#include - -// usings - -using std::shared_ptr; -using std::make_shared; -using std::sqrt; - -// constants const double infinity = std::numeric_limits::infinity(); const double pi = 3.1415926535897932385; -// utility functions inline double degrees_to_radians(double degrees) { return degrees * pi / 180; @@ -40,9 +28,3 @@ inline double clamp(double x, double min, double max) if (x > max) return max; return x; } - -// common headers - -#include "error.h" -#include "ray.h" -#include "vec3.h" diff --git a/src/ray.h b/include/ray.h similarity index 100% rename from src/ray.h rename to include/ray.h diff --git a/include/rtweekend.h b/include/rtweekend.h new file mode 100644 index 0000000..e5ae6a2 --- /dev/null +++ b/include/rtweekend.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include +#include + +// usings + +using std::shared_ptr; +using std::make_shared; +using std::sqrt; + +// common headers + +#include "error.h" +#include "ray.h" +#include "vec3.h" diff --git a/src/scene.h b/include/scene.h similarity index 99% rename from src/scene.h rename to include/scene.h index 33acf32..45a5131 100644 --- a/src/scene.h +++ b/include/scene.h @@ -1,5 +1,6 @@ #pragma once +#include "math.h" #include "sphere.h" #include "colour.h" #include "material.h" diff --git a/src/sphere.h b/include/sphere.h similarity index 100% rename from src/sphere.h rename to include/sphere.h diff --git a/src/vec3.h b/include/vec3.h similarity index 69% rename from src/vec3.h rename to include/vec3.h index d76dcff..a7ac81f 100755 --- a/src/vec3.h +++ b/include/vec3.h @@ -1,9 +1,8 @@ #pragma once -#include #include -#include "rtweekend.h" +#include "math.h" class vec3 { @@ -129,57 +128,3 @@ inline vec3 unit_vector(vec3 v) return v / v.length(); } -vec3 random_in_unit_sphere() -{ - while (true) - { - auto p = vec3::random(-1,1); - if (p.length_squared() >= 1) continue; - return p; - } -} - -vec3 random_unit_vector() -{ - auto a = random_double(0, 2*pi); - auto z = random_double(-1,1); - auto r = sqrt(1 - z*z); - return vec3(r*cos(a), r*sin(a), z); -} - -vec3 random_in_hemisphere(const vec3& normal) -{ - vec3 in_unit_sphere = random_in_unit_sphere(); - if (dot(in_unit_sphere, normal) > 0.0) - { - return in_unit_sphere; - } - else - { - return -in_unit_sphere; - } -} - -vec3 random_in_unit_disk() -{ - while(true) - { - auto p = vec3(random_double(-1,1), random_double(-1,1), 0); - if (p.length_squared() >= 1) continue; - return p; - } -} - -vec3 reflect(const vec3& v, const vec3& n) -{ - return v - 2*dot(v,n)*n; -} - -vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat) -{ - auto cos_theta = dot(-uv, n); - vec3 r_out_parallel = etai_over_etat * (uv + cos_theta*n); - vec3 r_out_perp = -sqrt(1.0 - r_out_parallel.length_squared()) * n; - - return r_out_parallel + r_out_perp; -} diff --git a/src/camera.cpp b/src/camera.cpp new file mode 100644 index 0000000..b29fa37 --- /dev/null +++ b/src/camera.cpp @@ -0,0 +1,37 @@ +#include "camera.h" + +camera::camera( + point3 lookfrom, + point3 lookat, + vec3 vup, + double vfov, // vertical field of view in degrees + double aspect_ratio, + double aperture, + double focus_dist) +{ + auto theta = degrees_to_radians(vfov); + auto h = tan(theta/2); + auto viewport_height = 2.0 * h; + auto viewport_width = aspect_ratio * viewport_height; + + w_ = unit_vector(lookfrom - lookat); + u_ = unit_vector(cross(vup, w_)); + v_ = cross(w_, u_); + + origin_ = lookfrom; + horizontal_ = focus_dist * viewport_width * u_; + vertical_ = focus_dist * viewport_height * v_; + lower_left_corner_ = origin_ - horizontal_/2 - vertical_/2 - focus_dist * w_; + + lens_radius_ = aperture / 2; +} + +ray camera::get_ray(double s, double t) const +{ + vec3 rd = lens_radius_ * random_in_unit_disk(); + vec3 offset = (u_ * rd.x()) + (v_ * rd.y()); + + return ray( + origin_ + offset, + lower_left_corner_ + s*horizontal_ + t*vertical_ - origin_ - offset); +} diff --git a/src/camera.h b/src/camera.h index 0f68329..f8b423f 100644 --- a/src/camera.h +++ b/src/camera.h @@ -1,52 +1,29 @@ #pragma once -#include "rtweekend.h" +#include "math.h" +#include "vec3.h" +#include "ray.h" #include "image.h" class camera { - public: - camera( - point3 lookfrom, - point3 lookat, - vec3 vup, - double vfov, // vertical field of view in degrees - double aspect_ratio, - double aperture, - double focus_dist) - { - auto theta = degrees_to_radians(vfov); - auto h = tan(theta/2); - auto viewport_height = 2.0 * h; - auto viewport_width = aspect_ratio * viewport_height; +public: + camera( + point3 lookfrom, + point3 lookat, + vec3 vup, + double vfov, // vertical field of view in degrees + double aspect_ratio, + double aperture, + double focus_dist); - w_ = unit_vector(lookfrom - lookat); - u_ = unit_vector(cross(vup, w_)); - v_ = cross(w_, u_); + ray get_ray(double s, double t) const; - origin_ = lookfrom; - horizontal_ = focus_dist * viewport_width * u_; - vertical_ = focus_dist * viewport_height * v_; - lower_left_corner_ = origin_ - horizontal_/2 - vertical_/2 - focus_dist * w_; - - lens_radius_ = aperture / 2; - } - - ray get_ray(double s, double t) const - { - vec3 rd = lens_radius_ * random_in_unit_disk(); - vec3 offset = (u_ * rd.x()) + (v_ * rd.y()); - - return ray( - origin_ + offset, - lower_left_corner_ + s*horizontal_ + t*vertical_ - origin_ - offset); - } - - private: - point3 origin_; - point3 lower_left_corner_; - vec3 horizontal_; - vec3 vertical_; - vec3 u_, v_, w_; - double lens_radius_; +private: + point3 origin_; + point3 lower_left_corner_; + vec3 horizontal_; + vec3 vertical_; + vec3 u_, v_, w_; + double lens_radius_; }; diff --git a/src/foo.cpp b/src/foo.cpp new file mode 100644 index 0000000..f4de601 --- /dev/null +++ b/src/foo.cpp @@ -0,0 +1 @@ +#include "foo.h" diff --git a/src/math.cpp b/src/math.cpp new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/math.cpp @@ -0,0 +1 @@ + diff --git a/src/vec3.cpp b/src/vec3.cpp new file mode 100644 index 0000000..eb73211 --- /dev/null +++ b/src/vec3.cpp @@ -0,0 +1,57 @@ +#include "vec3.h" + +vec3 random_unit_vector() +{ + auto a = random_double(0, 2*pi); + auto z = random_double(-1,1); + auto r = sqrt(1 - z*z); + return vec3(r*cos(a), r*sin(a), z); +} + +vec3 reflect(const vec3& v, const vec3& n) +{ + return v - 2*dot(v,n)*n; +} + +vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat) +{ + auto cos_theta = dot(-uv, n); + vec3 r_out_parallel = etai_over_etat * (uv + cos_theta*n); + vec3 r_out_perp = -sqrt(1.0 - r_out_parallel.length_squared()) * n; + + return r_out_parallel + r_out_perp; +} + +vec3 random_in_unit_disk() +{ + while(true) + { + auto p = vec3(random_double(-1,1), random_double(-1,1), 0); + if (p.length_squared() >= 1) continue; + return p; + } +} + +vec3 random_in_unit_sphere() +{ + while (true) + { + auto p = vec3::random(-1,1); + if (p.length_squared() >= 1) continue; + return p; + } +} + +vec3 random_in_hemisphere(const vec3& normal) +{ + vec3 in_unit_sphere = random_in_unit_sphere(); + if (dot(in_unit_sphere, normal) > 0.0) + { + return in_unit_sphere; + } + else + { + return -in_unit_sphere; + } +} +