aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt3
-rw-r--r--compile_commands.json17
-rw-r--r--include/color.hpp2
-rw-r--r--include/ray.hpp17
-rw-r--r--include/vec3.hpp18
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/color.cpp8
-rw-r--r--src/main.cpp63
-rw-r--r--src/ray.cpp8
-rw-r--r--src/vec3.cpp24
10 files changed, 121 insertions, 41 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 14493f2..abb196a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,7 +1,8 @@
cmake_minimum_required(VERSION 3.10)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
-project(path-tracer VERSION 1.0)
+project(path-tracer VERSION 1.0 LANGUAGES CXX)
add_subdirectory(src)
diff --git a/compile_commands.json b/compile_commands.json
new file mode 100644
index 0000000..820d63c
--- /dev/null
+++ b/compile_commands.json
@@ -0,0 +1,17 @@
+[
+{
+ "directory": "/home/quentin/Projects/path-tracer/build/src",
+ "command": "/usr/bin/c++ -I/home/quentin/Projects/path-tracer/include -o CMakeFiles/path-tracer.dir/main.cpp.o -c /home/quentin/Projects/path-tracer/src/main.cpp",
+ "file": "/home/quentin/Projects/path-tracer/src/main.cpp"
+},
+{
+ "directory": "/home/quentin/Projects/path-tracer/build/src",
+ "command": "/usr/bin/c++ -I/home/quentin/Projects/path-tracer/include -o CMakeFiles/path-tracer.dir/vec3.cpp.o -c /home/quentin/Projects/path-tracer/src/vec3.cpp",
+ "file": "/home/quentin/Projects/path-tracer/src/vec3.cpp"
+},
+{
+ "directory": "/home/quentin/Projects/path-tracer/build/src",
+ "command": "/usr/bin/c++ -I/home/quentin/Projects/path-tracer/include -o CMakeFiles/path-tracer.dir/color.cpp.o -c /home/quentin/Projects/path-tracer/src/color.cpp",
+ "file": "/home/quentin/Projects/path-tracer/src/color.cpp"
+}
+] \ No newline at end of file
diff --git a/include/color.hpp b/include/color.hpp
index 3acdf35..f300e76 100644
--- a/include/color.hpp
+++ b/include/color.hpp
@@ -5,6 +5,6 @@
#include "vec3.hpp"
-void write_color(std::ostream& out, color pixel_color);
+void write_color(std::ostream& out, const color& pixel_color);
#endif // !__COLOR_HPP_
diff --git a/include/ray.hpp b/include/ray.hpp
new file mode 100644
index 0000000..3f8542b
--- /dev/null
+++ b/include/ray.hpp
@@ -0,0 +1,17 @@
+#ifndef __RAY_HPP_
+#define __RAY_HPP_
+
+#include "vec3.hpp"
+
+class ray {
+ public:
+
+ ray(const point3& origin, const vec3& direction);
+
+ point3 at(double t);
+
+ point3 origin;
+ vec3 direction;
+};
+
+#endif // !__RAY_HPP_
diff --git a/include/vec3.hpp b/include/vec3.hpp
index b76792b..801b0b5 100644
--- a/include/vec3.hpp
+++ b/include/vec3.hpp
@@ -26,24 +26,26 @@ class vec3 {
vec3 cross(vec3& vec);
- vec3 unit_vector();
+ vec3 unit();
double x, y, z;
};
-vec3 operator+(vec3& u, vec3& v);
+vec3 lerp(vec3 u, vec3 v, double t);
-vec3 operator-(vec3& u, vec3& v);
+vec3 operator+(vec3 u, vec3 v);
-vec3 operator*(vec3& u, vec3& v);
+vec3 operator-(vec3 u, vec3 v);
-vec3 operator*(vec3& u, double scale);
+vec3 operator*(vec3 u, vec3 v);
-vec3 operator*(double scale, vec3& u);
+vec3 operator*(vec3 u, double scale);
-vec3 operator/(vec3& u, double scale);
+vec3 operator*(double scale, vec3 u);
-std::ostream& operator<<(std::ostream& out, vec3& vec);
+vec3 operator/(vec3 u, double scale);
+
+std::ostream& operator<<(std::ostream& out, vec3 vec);
using point3 = vec3;
using color = vec3;
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index f2c0e1b..4d8b1f6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,3 +1,3 @@
include_directories(${CMAKE_SOURCE_DIR}/include)
-add_executable(path-tracer main.cpp vec3.cpp color.cpp)
+add_executable(path-tracer main.cpp vec3.cpp color.cpp ray.cpp)
diff --git a/src/color.cpp b/src/color.cpp
index 2777d39..53a79f9 100644
--- a/src/color.cpp
+++ b/src/color.cpp
@@ -1,7 +1,7 @@
#include "color.hpp"
-void write_color(std::ostream& out, color pixel_color) {
- out << pixel_color.x << ' '
- << pixel_color.y << ' '
- << pixel_color.z << ' ';
+void write_color(std::ostream& out, const color& pixel_color) {
+ out << static_cast<uint32_t>(255.0 * pixel_color.x) << ' '
+ << static_cast<uint32_t>(255.0 * pixel_color.y) << ' '
+ << static_cast<uint32_t>(255.0 * pixel_color.z) << std::endl;
}
diff --git a/src/main.cpp b/src/main.cpp
index 8049bac..e3b69b1 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2,31 +2,62 @@
#include "color.hpp"
#include "vec3.hpp"
+#include "ray.hpp"
+
+color ray_color(ray& r) {
+ auto unit_dir = r.direction.unit();
+ auto t = (unit_dir.y + 1.0) * 0.5;
+
+ return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0);
+}
int main() {
- size_t width = 256;
- size_t height = 256;
+ // Image dimensions
+ const size_t width = 400;
+ const size_t height = width / (16.0 / 9.0);
+ // const size_t width = 256;
+ // const size_t height = 256;
- // PPM format header
- std::cout << "P3" << std::endl;
+ const double aspect_ratio = width / height;
+
+ // Viewport dimensions
+ const double viewport_width = 2.0;
+ const double viewport_height = viewport_width / aspect_ratio;
+
+ // Unit vectors for viewport pixels
+ const vec3 horizontal { viewport_width / width, 0.0, 0.0 };
+ const vec3 vertical { 0.0, viewport_height / height, 0.0 };
+ std::cerr << "Horizontal : " << horizontal << std::endl;
+ std::cerr << "Vertical : " << vertical << std::endl;
- std::cout << width << std::endl;
- std::cout << height << std::endl;
+ // We use a right handed coord system
+ const point3 eye_pos {};
+ const double focal_length = 1.0;
- // Max color
- std::cout << "255" << std::endl;
+ const point3 viewport_first_pixel =
+ horizontal * -(viewport_width / 2.0) +
+ vertical * -(viewport_height / 2.0) +
+ point3{ 0.0, 0.0, -focal_length };
+
+ const point3 first_pixel = eye_pos + viewport_first_pixel;
+
+ // PPM format header
+ std::cout << "P3\n" << width << '\n' << height << "\n 255" << std::endl;
std::cerr << "Generating image" << std::endl;
- for (size_t column = 0; column < width; column++) {
- std::cerr << width - column << " lines remaining" << std::endl;
+ for (ssize_t row = height - 1; row >= 0; row--) {
+ std::cerr << height - row << " lines remaining" << std::endl;
+ for (size_t column = 0; column < width; column++) {
+ vec3 x = horizontal * column;
+ vec3 y = vertical * row;
+
+ vec3 pixel_pos = first_pixel + x + y;
+ vec3 direction = pixel_pos - eye_pos;
+
+ ray ray { eye_pos, direction };
- for (size_t row = 0; row < height; row++) {
- auto r = double(column) / (width-1);
- auto g = double(row) / (height-1);
- auto b = 0.25;
- color c(r, g, b);
- write_color(std::cout, c);
+ write_color(std::cout, ray_color(ray));
}
}
diff --git a/src/ray.cpp b/src/ray.cpp
new file mode 100644
index 0000000..2d479b7
--- /dev/null
+++ b/src/ray.cpp
@@ -0,0 +1,8 @@
+#include "ray.hpp"
+
+ray::ray(const point3& origin, const vec3& direction): origin(origin), direction(direction)
+{}
+
+point3 ray::at(double t) {
+ return origin + direction * t;
+}
diff --git a/src/vec3.cpp b/src/vec3.cpp
index b26680c..28523ab 100644
--- a/src/vec3.cpp
+++ b/src/vec3.cpp
@@ -53,34 +53,38 @@ vec3 vec3::cross(vec3& vec) {
);
}
-vec3 vec3::unit_vector() {
+vec3 vec3::unit() {
return *this / length();
}
-vec3 operator+(vec3& u, vec3& v) {
+// vec3 lerp(vec3 u, vec3 v, double t) {
+// return (1.0 - t)
+// }
+
+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) {
+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) {
+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) {
+vec3 operator*(vec3 u, double scale) {
return vec3(u.x * scale, u.y * scale, u.z * scale);
}
-vec3 operator*(double scale, vec3& u) {
+vec3 operator*(double scale, vec3 u) {
return u * scale;
}
-vec3 operator/(vec3& u, double scale) {
- return 1.0/scale * u;
+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;
+std::ostream& operator<<(std::ostream& out, vec3 vec) {
+ return out << vec.x << ' ' << vec.y << ' ' << vec.z << std::endl;
}