From 3935a609141623e7cfb3fa339da1a166e3917018 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 20 Dec 2020 14:49:20 +0100 Subject: Ray tracer renderer Use ray for rendering Setup basic rendering stuff camera / viewport --- src/CMakeLists.txt | 2 +- src/color.cpp | 8 +++---- src/main.cpp | 63 ++++++++++++++++++++++++++++++++++++++++-------------- src/ray.cpp | 8 +++++++ src/vec3.cpp | 24 ++++++++++++--------- 5 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 src/ray.cpp (limited to 'src') 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(255.0 * pixel_color.x) << ' ' + << static_cast(255.0 * pixel_color.y) << ' ' + << static_cast(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; } -- cgit v1.3