diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-03-09 23:31:27 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-03-09 23:31:27 +0800 |
| commit | 4014436905aace9acd47dde245b88bd5f69335ec (patch) | |
| tree | 412bb61300cdd225bf3f79f3927f6076f718f094 /src | |
| parent | 7aa2a67d8381c9a88d69a3f53d74a7f079214263 (diff) | |
| download | ray-tracing-4014436905aace9acd47dde245b88bd5f69335ec.tar.gz ray-tracing-4014436905aace9acd47dde245b88bd5f69335ec.tar.xz | |
Sending Rays Into the Scene
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/color.h | 2 | ||||
| -rw-r--r-- | src/ray.cc | 16 | ||||
| -rw-r--r-- | src/ray.h | 22 | ||||
| -rw-r--r-- | src/vec3.cc | 56 | ||||
| -rw-r--r-- | src/vec3.h | 98 |
6 files changed, 111 insertions, 84 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 167e18b..c0dc29c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ set(LIB_SOURCES vec3.cc color.cc + ray.cc ) add_library(RayTracerCore STATIC ${LIB_SOURCES}) diff --git a/src/color.h b/src/color.h index 43be7ff..3e96a5b 100644 --- a/src/color.h +++ b/src/color.h @@ -23,6 +23,6 @@ using color = vec3; -void write_color(std::ostream &out, const color &pixel_color); +void write_color( std::ostream& out, const color& pixel_color ); #endif
\ No newline at end of file diff --git a/src/ray.cc b/src/ray.cc new file mode 100644 index 0000000..718fee2 --- /dev/null +++ b/src/ray.cc @@ -0,0 +1,16 @@ +#include "ray.h" + +const point3& ray::origin() const +{ + return this->orig; +} + +const vec3& ray::direction() const +{ + return this->dir; +} + +point3 ray::at( double t ) const +{ + return this->orig + t * this->dir; +}
\ No newline at end of file diff --git a/src/ray.h b/src/ray.h new file mode 100644 index 0000000..d9c42dc --- /dev/null +++ b/src/ray.h @@ -0,0 +1,22 @@ +#ifndef RAY_H +#define RAY_H + +#include "vec3.h" + +class ray +{ +public: + ray() {} + ray( const point3& origin, const vec3& direction ) : orig( origin ), dir( direction ) {} + + const point3& origin() const; + const vec3& direction() const; + + point3 at( double t ) const; + +private: + point3 orig; + vec3 dir; +}; + +#endif
\ No newline at end of file diff --git a/src/vec3.cc b/src/vec3.cc index d01f90a..4332c56 100644 --- a/src/vec3.cc +++ b/src/vec3.cc @@ -74,59 +74,3 @@ double vec3::length_squared() const { return e[0] * e[0] + e[1] * e[1] + e[2] * e[2]; } - -// point3 is just an alias for vec3, but useful for geometric clarity in the code. -using point3 = vec3; - -// Vector Utility Functions - -inline std::ostream& operator<<( std::ostream& out, const vec3& v ) -{ - return out << v.e[0] << ' ' << v.e[1] << ' ' << v.e[2]; -} - -inline vec3 operator+( const vec3& u, const vec3& v ) -{ - return { u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2] }; -} - -inline vec3 operator-( const vec3& u, const vec3& v ) -{ - return { u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] - v.e[2] }; -} - -inline vec3 operator*( const vec3& u, const vec3& v ) -{ - return { u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2] }; -} - -inline vec3 operator*( double t, const vec3& v ) -{ - return { t * v.e[0], t * v.e[1], t * v.e[2] }; -} - -inline vec3 operator*( const vec3& v, double t ) -{ - return t * v; -} - -inline vec3 operator/( const vec3& v, double t ) -{ - return ( 1 / t ) * v; -} - -inline double dot( const vec3& u, const vec3& v ) -{ - return u.e[0] * v.e[0] + u.e[1] * v.e[1] + u.e[2] * v.e[2]; -} - -inline vec3 cross( const vec3& u, const vec3& v ) -{ - return { - u.e[1] * v.e[2] - u.e[2] * v.e[1], u.e[2] * v.e[0] - u.e[0] * v.e[2], u.e[0] * v.e[1] - u.e[1] * v.e[0] }; -} - -inline vec3 unit_vector( const vec3& v ) -{ - return v / v.length(); -} @@ -21,38 +21,82 @@ #include <cmath> #include <iostream> -class vec3{ - public: - std::array<double, 3> e; +class vec3 +{ +public: + std::array<double, 3> e; - vec3() : e{0, 0, 0} {} - vec3(double e0, double e1, double e2) : e{e0, e1, e2} {} + vec3() : e { 0, 0, 0 } {} + vec3( double e0, double e1, double e2 ) : e { e0, e1, e2 } {} - double x() const; - double y() const; - double z() const; + double x() const; + double y() const; + double z() const; - vec3 operator-() const; - double operator[](int i) const; - double &operator[](int i); - vec3& operator+=(const vec3 & other); - vec3& operator*=(double t); - vec3& operator/=(double t); - double length() const; - double length_squared() const; + vec3 operator-() const; + double operator[]( int i ) const; + double& operator[]( int i ); + vec3& operator+=( const vec3& other ); + vec3& operator*=( double t ); + vec3& operator/=( double t ); + double length() const; + double length_squared() const; +}; +// point3 is just an alias for vec3, but useful for geometric clarity in the code. +using point3 = vec3; -}; +// Vector Utility Functions + +inline std::ostream& operator<<( std::ostream& out, const vec3& v ) +{ + return out << v.e[0] << ' ' << v.e[1] << ' ' << v.e[2]; +} + +inline vec3 operator+( const vec3& u, const vec3& v ) +{ + return { u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2] }; +} + +inline vec3 operator-( const vec3& u, const vec3& v ) +{ + return { u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] - v.e[2] }; +} + +inline vec3 operator*( const vec3& u, const vec3& v ) +{ + return { u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2] }; +} + +inline vec3 operator*( double t, const vec3& v ) +{ + return { t * v.e[0], t * v.e[1], t * v.e[2] }; +} + +inline vec3 operator*( const vec3& v, double t ) +{ + return t * v; +} + +inline vec3 operator/( const vec3& v, double t ) +{ + return ( 1 / t ) * v; +} + +inline double dot( const vec3& u, const vec3& v ) +{ + return u.e[0] * v.e[0] + u.e[1] * v.e[1] + u.e[2] * v.e[2]; +} + +inline vec3 cross( const vec3& u, const vec3& v ) +{ + return { + u.e[1] * v.e[2] - u.e[2] * v.e[1], u.e[2] * v.e[0] - u.e[0] * v.e[2], u.e[0] * v.e[1] - u.e[1] * v.e[0] }; +} -inline std::ostream &operator<<(std::ostream &out, const vec3 &v); -inline vec3 operator+(const vec3 &u, const vec3 &v); -inline vec3 operator-(const vec3 &u, const vec3 &v); -inline vec3 operator*(const vec3 &u, const vec3 &v); -inline vec3 operator*(double t, const vec3 &v); -inline vec3 operator*(const vec3 &v, double t); -inline vec3 operator/(const vec3 &v, double t); -inline double dot(const vec3 &u, const vec3 &v); -inline vec3 cross(const vec3 &u, const vec3 &v); -inline vec3 unit_vector(const vec3 &v); +inline vec3 unit_vector( const vec3& v ) +{ + return v / v.length(); +} #endif |
