aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/color.h2
-rw-r--r--src/ray.cc16
-rw-r--r--src/ray.h22
-rw-r--r--src/vec3.cc56
-rw-r--r--src/vec3.h98
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();
-}
diff --git a/src/vec3.h b/src/vec3.h
index 56490bc..a8b8265 100644
--- a/src/vec3.h
+++ b/src/vec3.h
@@ -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