aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-03-09 02:21:09 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-03-09 02:21:09 +0800
commit2a65ece16cd237d56c9dac8d9d3d680e9d34b774 (patch)
tree897cbb982ba4e44ee6c40d90223fa1732adac036
parent0671c86d0f0dccd6e94766834c41807b7d6b5388 (diff)
downloadray-tracing-2a65ece16cd237d56c9dac8d9d3d680e9d34b774.tar.gz
ray-tracing-2a65ece16cd237d56c9dac8d9d3d680e9d34b774.tar.xz
Create class vec3
-rw-r--r--color.cc15
-rw-r--r--color.h12
-rw-r--r--main.cc13
-rw-r--r--vec3.cc105
-rw-r--r--vec3.h41
5 files changed, 177 insertions, 9 deletions
diff --git a/color.cc b/color.cc
new file mode 100644
index 0000000..c1eac72
--- /dev/null
+++ b/color.cc
@@ -0,0 +1,15 @@
+#include "color.h"
+
+void write_color(std::ostream &out, const color &pixel_color){
+ auto r = pixel_color.x();
+ auto g = pixel_color.y();
+ auto b = pixel_color.z();
+
+ // Translate the [0,1] component values to the byte range [0,255].
+ int rbyte = int(255.999 * r);
+ int gbyte = int(255.999 * g);
+ int bbyte = int(255.999 * b);
+
+ // Write out the pixel color components.
+ out << rbyte << ' ' << gbyte << ' ' << bbyte << '\n';
+} \ No newline at end of file
diff --git a/color.h b/color.h
new file mode 100644
index 0000000..23359aa
--- /dev/null
+++ b/color.h
@@ -0,0 +1,12 @@
+#ifndef COLOR_H
+#define COLOR_H
+
+#include "vec3.h"
+
+#include <iostream>
+
+using color = vec3;
+
+void write_color(std::ostream &out, const color &pixel_color);
+
+#endif \ No newline at end of file
diff --git a/main.cc b/main.cc
index e92fe3d..e9d2e88 100644
--- a/main.cc
+++ b/main.cc
@@ -1,4 +1,6 @@
#include <iostream>
+#include "vec3.h"
+#include "color.h"
int main() {
// Image
@@ -14,15 +16,8 @@ int main() {
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
for (int i = 0; i < image_width; i++)
{
- auto r = double(i) / (image_width - 1);
- auto g = double(j) / (image_height - 1);
- auto b = 0.0;
-
- int ir = int(255.999 * r);
- int ig = int(255.999 * g);
- int ib = int(255.999 * b);
-
- std::cout << ir << ' ' << ig << ' ' << ib << '\n';
+ auto pixel_color = color(double(i) / (image_width - 1), double(j) / (image_height - 1), 0);
+ write_color(std::cout, pixel_color);
}
}
std::clog << "\rDone. \n";
diff --git a/vec3.cc b/vec3.cc
new file mode 100644
index 0000000..38844c7
--- /dev/null
+++ b/vec3.cc
@@ -0,0 +1,105 @@
+#include "vec3.h"
+
+double vec3::x() const { return e[0]; }
+double vec3::y() const { return e[1]; }
+double vec3::z() const { return e[2]; }
+
+vec3 vec3::operator-() const {
+ return vec3(-e[0], -e[1], -e[2]);
+}
+
+double vec3::operator[](int i) const{
+ return e[i];
+}
+
+double& vec3::operator[](int i){
+ return e[1];
+}
+
+vec3& vec3::operator+=(const vec3 &v)
+{
+ e[0] += v.e[0];
+ e[1] += v.e[1];
+ e[2] += v.e[2];
+ return *this;
+}
+
+vec3& vec3::operator*=(double t)
+{
+ e[0] *= t;
+ e[1] *= t;
+ e[2] *= t;
+ return *this;
+}
+
+vec3& vec3::operator/=(double t)
+{
+ return *this *= 1 / t;
+}
+
+double vec3::length() const
+{
+ return std::sqrt(length_squared());
+}
+
+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 vec3(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 vec3(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 vec3(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 vec3(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 vec3(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/vec3.h b/vec3.h
new file mode 100644
index 0000000..5fe4adf
--- /dev/null
+++ b/vec3.h
@@ -0,0 +1,41 @@
+#ifndef VEC_3
+#define VEC_3
+
+#include <cmath>
+#include <iostream>
+
+class vec3{
+ public:
+ double e[3];
+
+ 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;
+
+ 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;
+
+
+};
+
+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);
+
+#endif