aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt4
-rw-r--r--src/color.cpp7
-rw-r--r--src/main.cpp11
-rw-r--r--src/vec3.cpp86
4 files changed, 101 insertions, 7 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8047013..f2c0e1b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1 +1,3 @@
-add_executable(path-tracer main.cpp)
+include_directories(${CMAKE_SOURCE_DIR}/include)
+
+add_executable(path-tracer main.cpp vec3.cpp color.cpp)
diff --git a/src/color.cpp b/src/color.cpp
new file mode 100644
index 0000000..2777d39
--- /dev/null
+++ b/src/color.cpp
@@ -0,0 +1,7 @@
+#include "color.hpp"
+
+void write_color(std::ostream& out, color pixel_color) {
+ out << pixel_color.x << ' '
+ << pixel_color.y << ' '
+ << pixel_color.z << ' ';
+}
diff --git a/src/main.cpp b/src/main.cpp
index 2cd66a9..8049bac 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,5 +1,8 @@
#include <iostream>
+#include "color.hpp"
+#include "vec3.hpp"
+
int main() {
size_t width = 256;
size_t height = 256;
@@ -22,12 +25,8 @@ int main() {
auto r = double(column) / (width-1);
auto g = double(row) / (height-1);
auto b = 0.25;
-
- int ir = static_cast<int>(255.999 * r);
- int ig = static_cast<int>(255.999 * g);
- int ib = static_cast<int>(255.999 * b);
-
- std::cout << ir << ' ' << ig << ' ' << ib << '\n';
+ color c(r, g, b);
+ write_color(std::cout, c);
}
}
diff --git a/src/vec3.cpp b/src/vec3.cpp
new file mode 100644
index 0000000..b26680c
--- /dev/null
+++ b/src/vec3.cpp
@@ -0,0 +1,86 @@
+#include "vec3.hpp"
+
+vec3::vec3(): x(0.0), y(0.0), z(0.0) {}
+
+vec3::vec3(double x, double y, double z): x(x), y(y), z(z) {}
+
+void vec3::operator-() {
+ x = -x;
+ y = -y;
+ z = -z;
+}
+
+vec3& vec3::operator+=(vec3& vec) {
+ x += vec.x;
+ y += vec.y;
+ z += vec.z;
+
+ return *this;
+}
+
+vec3& vec3::operator*=(double scale) {
+ x *= scale;
+ y *= scale;
+ z *= scale;
+
+ return *this;
+}
+
+vec3& vec3::operator/=(double numerator) {
+ x /= numerator;
+ y /= numerator;
+ z /= numerator;
+
+ return *this;
+}
+
+double vec3::length() {
+ return sqrt(length_sq());
+}
+
+double vec3::length_sq() {
+ return x * x + y * y + z * z;
+}
+
+double vec3::dot(vec3& vec) {
+ return x * vec.x + y + vec.y + z + vec.z;
+}
+
+vec3 vec3::cross(vec3& vec) {
+ return vec3(y * vec.z - z * vec.y,
+ z * vec.x - x * vec.z,
+ x * vec.y - y * vec.y
+ );
+}
+
+vec3 vec3::unit_vector() {
+ return *this / length();
+}
+
+vec3 operator+(vec3& u, vec3& v) {
+ return vec3(u.x + v.x, u.y + v.y, u.z + v.z);
+}
+
+vec3 operator-(vec3& u, vec3& v) {
+ return vec3(u.x - v.x, u.y - v.y, u.z - v.z);
+}
+
+vec3 operator*(vec3& u, vec3& v) {
+ return vec3(u.x * v.x, u.y * v.y, u.z * v.z);
+}
+
+vec3 operator*(vec3& u, double scale) {
+ return vec3(u.x * scale, u.y * scale, u.z * scale);
+}
+
+vec3 operator*(double scale, vec3& u) {
+ return u * scale;
+}
+
+vec3 operator/(vec3& u, double scale) {
+ return 1.0/scale * u;
+}
+
+std::ostream& operator<<(std::ostream& out, vec3& vec) {
+ return std::cout << vec.x << ' ' << vec.y << ' ' << vec.z << std::endl;
+}