From b3233ae7283ac7e14092ecc36365a62f1b994e9c Mon Sep 17 00:00:00 2001 From: Nigel Date: Sat, 4 Mar 2023 18:43:44 +0100 Subject: [PATCH] Basic PPM output image with vector implementation and utility functions --- .gitignore | 6 +++ README.md | 8 ++++ build.ps1 | 4 ++ premake5.lua | 17 +++++++++ src/color.h | 12 ++++++ src/program.cpp | 27 ++++++++++++++ src/vector3.h | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 171 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 build.ps1 create mode 100644 premake5.lua create mode 100644 src/color.h create mode 100644 src/program.cpp create mode 100644 src/vector3.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05d2278 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +bin/ +tools/premake +obj/ +*.ppm +*.sln +*.vcxproj diff --git a/README.md b/README.md new file mode 100644 index 0000000..7d59e33 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Raytracing in one weekend + + +[!img]() + + + +### Original author Post: https://raytracing.github.io/books/RayTracingInOneWeekend.html#overview \ No newline at end of file diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..a14d5ea --- /dev/null +++ b/build.ps1 @@ -0,0 +1,4 @@ +./tools/premake/premake5 +msbuild ./RayTracingInOneWeekend.sln +# ./bin/Debug/RayTracingInOneWeekend.exe + diff --git a/premake5.lua b/premake5.lua new file mode 100644 index 0000000..5d59bf8 --- /dev/null +++ b/premake5.lua @@ -0,0 +1,17 @@ +workspace "RaytracingInOneWeekend" + + configurations{"Debug", "Release"} + +project "RaytracingInOneWeekend" + kind "ConsoleApp" + language "C++" + targetdir "bin/%{cfg.buildcfg}" + + files { "src/**.h" , "src/**.cpp" } + + filter "configurations:Debug" + defines{"DEBUG"} + symbols "On" + filter "configurations:Release" + defines{"NDEBUG"} + optimize "On" \ No newline at end of file diff --git a/src/color.h b/src/color.h new file mode 100644 index 0000000..a04d078 --- /dev/null +++ b/src/color.h @@ -0,0 +1,12 @@ +#pragma once +#include "vector3.h" + +#include + +void write_color(std::ostream& out, color pixel_color){ + // Write the translated [0, 255] value of each color component + + out << static_cast( 255.999 * pixel_color.x()) << ' ' + << static_cast ( 255.999 * pixel_color.y()) << ' ' + << static_cast ( 255.999 * pixel_color.z()) << '\n'; +} \ No newline at end of file diff --git a/src/program.cpp b/src/program.cpp new file mode 100644 index 0000000..1164905 --- /dev/null +++ b/src/program.cpp @@ -0,0 +1,27 @@ +#include "color.h" +#include "vector3.h" + +#include + + +int main () +{ + // Image + const int image_width = 256; + const int image_height = 256; + + // Render + std::cout<< "P3\n" << image_width << ' ' << image_height << "\n255\n"; + for (int j = image_height-1; j >= 0; --j) + { + std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush; + for(int i = 0; i < image_width; ++i) + { + color pixel_color(double(i)/(image_width-1), double(j)/(image_height-1), 0.25); + write_color(std::cout , pixel_color); + } + } + + std::cerr << "\nDone.\n"; + return 0; +} \ No newline at end of file diff --git a/src/vector3.h b/src/vector3.h new file mode 100644 index 0000000..755810f --- /dev/null +++ b/src/vector3.h @@ -0,0 +1,97 @@ +#pragma once +#include +#include + +using std::sqrt; + +class vec3{ + public: + vec3() :e{0,0,0}{} + vec3(double e0, double e1, double e2): e{e0, e1, e2}{} + + double x() const {return e[0];} + double y() const {return e[1];} + double z() const {return e[2];} + + vec3 operator-() const {return vec3(-e[0], -e[1], -e[2]);} + double operator[](int i)const {return e[i];} + double& operator[](int i){return e[i];} + + vec3& operator+=(const vec3& v){ + e[0] += v.e[0]; + e[1] += v.e[1]; + e[2] += v.e[2]; + return *this; + } + + vec3& operator*=(const double t){ + e[0] *= t; + e[1] *= t; + e[2] *= t; + return *this; + } + + vec3& operator/= (const double t){ + return *this *= 1/t; + } + + double length() const { + return sqrt(length_squared()); + } + + double length_squared() const { + return e[0]*e[0] + e[1]*e[1] + e[2]*e[2]; + } + + double e[3]; +}; + +// Type aliases for vec3 +using point3 = vec3; // 3D point +using color = vec3; // RGB color + +// Utitility 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/(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(vec3 v){ + return v / v.length(); +}