Skip to content

Commit

Permalink
on 6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolara93 committed Sep 22, 2024
0 parents commit f6fd68c
Show file tree
Hide file tree
Showing 122 changed files with 98,517 additions and 0 deletions.
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# CMakeList.txt : CMake project for RayTracing, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)

# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()

project ("RayTracing")

# Add source to this project's executable.
add_executable (RayTracing "RayTracing.cpp" "RayTracing.h" "Vec3.h" "Color.h" "Ray.h" "Hittable.h" "Sphere.h")

if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET RayTracing PROPERTY CXX_STANDARD 20)
endif()

# TODO: Add tests and install targets if needed.
61 changes: 61 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
]
}
19 changes: 19 additions & 0 deletions Color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include "Vec3.h"
#include <iostream>

using Color = Vec3;

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 = static_cast<int>(255.999 * r);
int gbyte = static_cast<int>(255.999 * g);
int bbyte = static_cast<int>(255.999 * b);

// Print the pixel color components
out << rbyte << ' ' << gbyte << ' ' << bbyte << '\n';
}
20 changes: 20 additions & 0 deletions Ray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include "Vec3.h"

class Ray {
public:
Ray() {}

Ray(const Point3& origin, const Vec3& direction) : orig(origin), dir(direction) {}

const Point3& origin() const { return orig; };
const Vec3& direction() const { return dir; };

Point3 at(double t) const {
return orig + t * dir;
}

private:
Point3 orig;
Vec3 dir;
};
78 changes: 78 additions & 0 deletions RayTracing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "RayTracing.h"

double hit_sphere(const Point3& center, double radius, const Ray& r)
{
Vec3 oc = center - r.origin();
auto a = r.direction().length_squared();
auto h = dot(r.direction(), oc);
auto c = oc.length_squared() - radius*radius;
auto discriminant = h * h - a * c;
if (discriminant < 0)
{
return -1.0;
} else
{
return (h - std::sqrt(discriminant)) / a;
}
}

Color ray_color(const Ray& r)
{
auto t = hit_sphere(Point3(0, 0, -1), 0.5, r);
if (t > 0.0 )
{
Vec3 N = unit_vector(r.at(t) - Vec3(0, 0, -1));
return 0.5 * Color(N.x() + 1, N.y() + 1, N.z() + 1);
}
Vec3 unit_direction = unit_vector(r.direction());
auto a = 0.5 * (unit_direction.y() + 1.0);
return (1.0 - a) * Color(1.0, 1.0, 1.0) + a * Color(0.5, 0.7, 1.0);
}

int main()
{
// image dimensions
auto aspect_ratio{ 16.0 / 9.0 };
int image_width{ 400 };

// Calculate the image height, and ensure that it's at least 1.
int image_height = static_cast<int>(image_width / aspect_ratio);
image_height = (image_height < 1) ? 1 : image_height;

// Camera
auto focal_length = 1.0;
auto viewport_height = 2.0;
auto viewport_width = viewport_height * (static_cast<double>(image_width) / image_height);
auto camera_center = Point3(0, 0, 0);

// Calculate the vectors across the horizontal and down the vertical viewport edges
auto viewport_u = Vec3(viewport_width, 0, 0);
auto viewport_v = Vec3(0, -viewport_height, 0);

// Calculate the horizontal and vertical delta vectors from pixel to pixel
auto pixel_delta_u{ viewport_u / image_width };
auto pixel_delta_v{ viewport_v / image_height };

// Calculate the location of upper left pixel
auto viewport_upper_left = camera_center
- Vec3(0, 0, focal_length) - viewport_u / 2 - viewport_v / 2;
auto pixel100_loc = viewport_upper_left + 0.5 * (pixel_delta_u + pixel_delta_v);

// Render our image
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
for (int j = 0; j < image_height; j++)
{
std::clog << "\rScanlines remaining: " << (image_height -j) << ' ' << std::flush;
for (int i = 0; i < image_width; i++)
{
auto pixel_center = pixel100_loc + (i * pixel_delta_u) + (j * pixel_delta_v);
auto ray_direction = pixel_center - camera_center;
Ray r(camera_center, ray_direction);

Color pixel_color = ray_color(r);
write_color(std::cout, pixel_color);
}
}
std::clog << "\rDone.\n";
return 0;
}
11 changes: 11 additions & 0 deletions RayTracing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RayTracing.h : Include file for standard system include files,
// or project specific include files.

#pragma once

#include <iostream>
#include "Vec3.h"
#include "Color.h"
#include "Ray.h"

// TODO: Reference additional headers your program requires here.
88 changes: 88 additions & 0 deletions Vec3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#pragma once
#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 { 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*=(double t) {
e[0] *= t;
e[1] *= t;
e[2] *= t;
return *this;
}

Vec3& operator/=(double t) {
return *this *= 1 / t;
}
double length_squared() const {
return e[0]*e[0] + e[1]*e[1] + e[2]*e[2];
}
double length() const {
return std::sqrt(length_squared());
}
};

using Point3 = Vec3; // Point3 is nothing more but an alias for our Vec3 class

// 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 (1 / t) * v;
}

inline double dot(const Vec3& u, const Vec3& v) { // The dot product of two vectors v = <v1,v2,v3> and u = <u1,u2,u3>
return u.e[0] * v.e[0] + // is defined by v1u1 + v2u2 + v3u3
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();
}

Loading

0 comments on commit f6fd68c

Please sign in to comment.