This commit is contained in:
Cat Flynn 2023-02-19 01:10:53 +00:00
parent ed4243cf89
commit bd444aba2e
20 changed files with 192 additions and 122 deletions

View File

@ -2,10 +2,8 @@ cmake_minimum_required(VERSION 3.10)
project(snoopy) project(snoopy)
file(GLOB snoopy_src include_directories("include")
"src/*.h" file(GLOB SOURCES "src/*.cpp")
"src/*.cpp"
)
add_executable(snoopy ${snoopy_src}) add_executable(snoopy ${SOURCES})

52
include/camera.h Normal file
View File

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

1
include/foo.h Normal file
View File

@ -0,0 +1 @@
#include <iostream>

View File

@ -1,22 +1,10 @@
#pragma once #pragma once
#include <cmath> #include <cmath>
#include <cstdlib>
#include <limits>
#include <memory>
// usings
using std::shared_ptr;
using std::make_shared;
using std::sqrt;
// constants
const double infinity = std::numeric_limits<double>::infinity(); const double infinity = std::numeric_limits<double>::infinity();
const double pi = 3.1415926535897932385; const double pi = 3.1415926535897932385;
// utility functions
inline double degrees_to_radians(double degrees) inline double degrees_to_radians(double degrees)
{ {
return degrees * pi / 180; return degrees * pi / 180;
@ -40,9 +28,3 @@ inline double clamp(double x, double min, double max)
if (x > max) return max; if (x > max) return max;
return x; return x;
} }
// common headers
#include "error.h"
#include "ray.h"
#include "vec3.h"

18
include/rtweekend.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <cmath>
#include <cstdlib>
#include <limits>
#include <memory>
// usings
using std::shared_ptr;
using std::make_shared;
using std::sqrt;
// common headers
#include "error.h"
#include "ray.h"
#include "vec3.h"

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "math.h"
#include "sphere.h" #include "sphere.h"
#include "colour.h" #include "colour.h"
#include "material.h" #include "material.h"

View File

@ -1,9 +1,8 @@
#pragma once #pragma once
#include <cmath>
#include <iostream> #include <iostream>
#include "rtweekend.h" #include "math.h"
class vec3 class vec3
{ {
@ -129,57 +128,3 @@ inline vec3 unit_vector(vec3 v)
return v / v.length(); 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;
}

37
src/camera.cpp Normal file
View File

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

View File

@ -1,52 +1,29 @@
#pragma once #pragma once
#include "rtweekend.h" #include "math.h"
#include "vec3.h"
#include "ray.h"
#include "image.h" #include "image.h"
class camera class camera
{ {
public: public:
camera( camera(
point3 lookfrom, point3 lookfrom,
point3 lookat, point3 lookat,
vec3 vup, vec3 vup,
double vfov, // vertical field of view in degrees double vfov, // vertical field of view in degrees
double aspect_ratio, double aspect_ratio,
double aperture, double aperture,
double focus_dist) 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); ray get_ray(double s, double t) const;
u_ = unit_vector(cross(vup, w_));
v_ = cross(w_, u_);
origin_ = lookfrom; private:
horizontal_ = focus_dist * viewport_width * u_; point3 origin_;
vertical_ = focus_dist * viewport_height * v_; point3 lower_left_corner_;
lower_left_corner_ = origin_ - horizontal_/2 - vertical_/2 - focus_dist * w_; vec3 horizontal_;
vec3 vertical_;
lens_radius_ = aperture / 2; vec3 u_, v_, w_;
} double lens_radius_;
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_;
}; };

1
src/foo.cpp Normal file
View File

@ -0,0 +1 @@
#include "foo.h"

1
src/math.cpp Normal file
View File

@ -0,0 +1 @@

57
src/vec3.cpp Normal file
View File

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