Skip to content

Commit

Permalink
Implemented a hittable and sphere class.
Browse files Browse the repository at this point in the history
  • Loading branch information
shayshunk committed Jan 31, 2024
1 parent 680e84f commit 843b776
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 129
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
!result.png

# Ignore executables
Image
RayTracer

# Ignore vscde folder
.vscode
25 changes: 25 additions & 0 deletions Include/RTWeekend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef RTWEEKEND_H
#define RTWEEKEND_H

#include <cmath>
#include <limits>
#include <memory>

using std::make_shared;
using std::shared_ptr;
using std::sqrt;

// Constants
double const infinity = std::numeric_limits<double>::infinity();
double const pi = 3.1415926535897932385;

// Utilities
inline double DegreesToRadians(double degrees)
{
return degrees * pi / 180.0;
}

#include "ray.h"
#include "vec3.h"

#endif
2 changes: 1 addition & 1 deletion Include/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using Color = Vector3;
void WriteColor(std::ostream& out, Color pixelColor)
{
// Write the translated [0, 255] value of each primary color
out << static_cast<int>(255.999 * pixelColor.x()) << ", "
out << static_cast<int>(255.999 * pixelColor.x()) << ", "
<< static_cast<int>(255.999 * pixelColor.y()) << ", "
<< static_cast<int>(255.999 * pixelColor.z()) << '\n';
}
Expand Down
11 changes: 10 additions & 1 deletion Include/hittable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ class HitRecord
Point3 p;
Vector3 normal;
double t;
bool frontFace;

void SetFaceNormal(Ray const& r, Vector3 const& outwardNormal)
{
// Sets the hit record normal vector
// NOTE: the outward normal parameter is assumed to be a unit vector
frontFace = Dot(r.Direction(), outwardNormal) < 0;
normal = frontFace ? outwardNormal : -outwardNormal;
}
};

class Hittable
{
public:
virtual ~Hittable() = default;

virtual bool Hit(Ray const& r, float rayTMin, float rayTMax, HitRecord& rec) const = 0;
virtual bool Hit(Ray const& r, double rayTMin, double rayTMax, HitRecord& rec) const = 0;
};

#endif
45 changes: 45 additions & 0 deletions Include/hittableList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef HITTABLE_LIST_H
#define HITTABLE_LIST_H

#include <memory>
#include <vector>

#include "hittable.h"

using std::make_shared;
using std::shared_ptr;

class HittableList : public Hittable
{
public:
std::vector<shared_ptr<Hittable>> objects;

HittableList() {}
HittableList(shared_ptr<Hittable> object) { Add(object); }

void Clear() { objects.clear(); }

void Add(shared_ptr<Hittable> object) { objects.push_back(object); }

bool Hit(Ray const& r, double rayTMin, double rayTMax, HitRecord& rec) const override
{
HitRecord tempRec;
bool hitAnything = false;

double closestSoFar = rayTMax;

for (auto const& object : objects)
{
if (object->Hit(r, rayTMin, closestSoFar, tempRec))
{
hitAnything = true;
closestSoFar = tempRec.t;
rec = tempRec;
}
}

return hitAnything;
}
};

#endif
34 changes: 16 additions & 18 deletions Include/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,41 @@
class Sphere : public Hittable
{
public:
Sphere(Point3 _center, float _radius) : center(_center), radius(_radius) {}
Sphere(Point3 _center, double _radius) : center(_center), radius(_radius) {}

bool Hit(Ray const& r, float rayTMin, float rayTMax, HitRecord& rec) const override
bool Hit(Ray const& r, double rayTMin, double rayTMax, HitRecord& rec) const override
{
Vector3 oc = r.Origin() - center;
auto a = r.Direction().LengthSquared();
auto half_b = Dot(oc, r.Direction());
auto c = oc.LengthSquared() - radius * radius;

float a = r.Direction().LengthSquared();
float half_b = Dot(oc, r.Direction());
float c = oc.LengthSquared() - radius * radius;
float discriminant = half_b * half_b - a * c;

auto discriminant = half_b * half_b - a * c;
if (discriminant < 0)
return false;
auto sqrtd = sqrt(discriminant);

float sqrtD = sqrt(discriminant);

// Find the nearest root in the acceptable range
float root = (-half_b - sqrtD) / a;

if (root <= rayTMin || root >= rayTMin)
// Find the nearest root that lies in the acceptable range.
auto root = (-half_b - sqrtd) / a;
if (root <= rayTMin || rayTMax <= root)
{
root = (-half_b + sqrtD) / a;

if (root <= rayTMin || root >= rayTMin)
root = (-half_b + sqrtd) / a;
if (root <= rayTMin || rayTMax <= root)
return false;
}

rec.t = root;
rec.p = r.at(rec.t);
rec.normal = (rec.p - center) / radius;

Vector3 outwardNormal = (rec.p - center) / radius;
rec.SetFaceNormal(r, outwardNormal);

return true;
}

private:
Point3 center;
float radius;
double radius;
};

#endif
61 changes: 25 additions & 36 deletions FirstImage.cc → RTWeekend.cc
Original file line number Diff line number Diff line change
@@ -1,61 +1,49 @@
#include "RTWeekend.h"

#include <cmath>
#include <iostream>

#include "color.h"
#include "ray.h"
#include "vec3.h"
#include "hittable.h"
#include "hittableList.h"
#include "sphere.h"

using std::cout;

float HitSphere(Point3 const& center, float radius, Ray const& r)
Color RayColor(Ray const& r, Hittable const& world)
{
Vector3 oc = r.Origin() - center;

float a = r.Direction().LengthSquared();
float half_b = Dot(oc, r.Direction());
float c = oc.LengthSquared() - radius * radius;
float discriminant = half_b * half_b - a * c;

if (discriminant < 0)
HitRecord rec;
if (world.Hit(r, 0, infinity, rec))
{
return -1.0;
}
else
{
return (-half_b - sqrt(discriminant)) / a;
}
}

Color RayColor(Ray const& r)
{
float t = HitSphere(Point3(0, 0, -1), 0.5, r);

if (t > 0)
{
Vector3 N = UnitVector(r.at(t) - Vector3(0, 0, -1));
return 0.5 * Color(N.x() + 1, N.y() + 1, N.z() + 1);
return 0.5 * (rec.normal + Color(1, 1, 1));
}

Vector3 unitDirection = UnitVector(r.Direction());
float a = 0.5 * (unitDirection.y()) + 1.0;
double a = 0.5 * (unitDirection.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
float aspectRatio = 16.0 / 9.0;
double aspectRatio = 16.0 / 9.0;
int imageWidth = 400;

// Calculating image height and checking that it's >= 1
int imageHeight = static_cast<int>(imageWidth / aspectRatio);
imageHeight = (imageHeight < 1)? 1 : imageHeight;
imageHeight = (imageHeight < 1) ? 1 : imageHeight;

// World
HittableList world;

world.Add(make_shared<Sphere>(Point3(0, 0, -1), 0.5));
world.Add(make_shared<Sphere>(Point3(0, -100.5, 1), 100));

// Setting up Camera
float focalLength = 1.0;
float viewportHeight = 2.0;
double focalLength = 1.0;
double viewportHeight = 2.0;

float viewportWidth = viewportHeight * (static_cast<float>(imageWidth) / imageHeight);
double viewportWidth = viewportHeight * (static_cast<double>(imageWidth) / imageHeight);
Point3 cameraCenter(0, 0, 0);

// Calculating vectors across the horizontal and down the vertical viewport edges
Expand All @@ -67,15 +55,16 @@ int main()
Vector3 pixelDeltaV = viewportV / imageHeight;

// Calculating the location of the upper left pixel
Point3 viewportUpperLeft = cameraCenter - Vector3(0, 0, focalLength) - (viewportU / 2) - (viewportV / 2);
Point3 viewportUpperLeft = cameraCenter - Vector3(0, 0, focalLength) - (viewportU / 2)
- (viewportV / 2);
Point3 pixel00Location = viewportUpperLeft + (0.5 * (pixelDeltaU + pixelDeltaV));

// Rendering
cout << "P3\n" << imageWidth << ' ' << imageHeight << "\n255\n";

for (int i = 0; i < imageHeight; i++)
{
std::clog << "Scanlines remaining: " << (imageHeight - i) << '\r';
// std::clog << "Scanlines remaining: " << (imageHeight - i) << '\r';
cout.flush();

for (int j = 0; j < imageWidth; j++)
Expand All @@ -85,7 +74,7 @@ int main()

Ray r(cameraCenter, rayDirection);

Color pixelColor = RayColor(r);
Color pixelColor = RayColor(r, world);
WriteColor(cout, pixelColor);
}
}
Expand Down
Binary file modified result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 843b776

Please sign in to comment.