Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Jorquera committed Mar 2, 2024
0 parents commit e5eb174
Show file tree
Hide file tree
Showing 19 changed files with 790 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
build/
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.28)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(raytracer)

file(GLOB srcs
"${PROJECT_SOURCE_DIR}/src/*.h"
"${PROJECT_SOURCE_DIR}/src/*.cpp"
)

add_executable(raytracer ${srcs})
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Pedro Jorquera

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Raytracer

A simple C++ Raytracer.

![render image](docs/render.png)

## Features

* Raytracing sphere geometries with front face detection.
* Positionable camera.
* Antialising.
* Simple diffuse material.
* Lambertian reflection.
* Gamma correction.
* Light scattering.
* Reflectance.
* Fuzzy reflection.
* Refraction with Snell's law.
* Schlick approximation.
* Defocus blur.

## Building

```
mkdir build
cd build
cmake ..
make
```

## Running

```
cd build
./raytracer
```

## Generating Xcode Project

```
mkdir build
cd build
cmake .. -GXcode
```

## MIT License

Copyright 2024 Pedro Jorquera.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Binary file added docs/render.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions src/camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#import "camera.h"
#include "material.h"

#include <iostream>

using namespace std;

inline Color background(const Ray& ray) {
const auto unitDir = ray.dir().unit();
const auto a = 0.5 * (unitDir.y() + 1.0);
return (1.0 - a) * Color(1.0, 1.0, 1.0) + a * Color(0.5, 0.7, 1.0);
}

inline Color color(const Ray& ray, int depth, const shared_ptr<const Intersectable>& intersectable) {
if (depth <= 0) return Color(0.0, 0.0, 0.0);
Hit hit;
if (intersectable->intersects(ray, Interval(0.001), hit)) {
Ray scattered;
Color attenuation;
if (hit.material()->scatter(ray, hit, attenuation, scattered))
return attenuation * color(scattered, depth - 1, intersectable);
return Color(0,0,0);
}

return background(ray);
}

Camera::Camera(double aspect, int samplesPerPixel,
int maxDepth, int imageWidth, double vfov, Vector lookFrom, Vector lookAt, Vector vup, double defocusAngle, double focusDist): _aspect(aspect), _samplesPerPixel(samplesPerPixel), _maxDepth(maxDepth), _lookFrom(lookFrom),
_lookAt(lookAt), _vup(vup), _defocusAngle(defocusAngle), _focusDist(focusDist)
{
auto imageHeight = int(imageWidth / aspect);
imageHeight = (imageHeight < 1) ? 1 : imageHeight;
_frameBuffer = make_shared<FrameBuffer>(imageWidth, imageHeight);

const auto theta = vfov * M_PI / 180.0;
const auto h = tan(theta / 2.0);
const auto viewportHeight = 2.0 * h * _focusDist;
const auto viewportWidth = viewportHeight * (double(imageWidth) / imageHeight);
_viewport = { viewportWidth, viewportHeight };

_w = (_lookFrom - _lookAt).unit();
_u = Vector::cross(_vup, _w).unit();
_v = Vector::cross(_w, _u);

const auto viewportU = _viewport.width * _u;
const auto viewportV = _viewport.height * -_v;
_viewportDeltaU = viewportU / imageWidth;
_viewportDeltaV = viewportV / imageHeight;
const auto viewportUpperLeft = _lookFrom - (_focusDist * _w) - (viewportU / 2.0) - (viewportV / 2.0);
_pixel00 = viewportUpperLeft + 0.5 * (_viewportDeltaU + _viewportDeltaV);

const auto defocusRadius = _focusDist * tan((_defocusAngle / 2.0) * M_PI / 180.0);
_defocusDiskU = _u * defocusRadius;
_defocusDiskV = _v * defocusRadius;
}

void Camera::render(const shared_ptr<const Intersectable>& intersectable, const string& filename) {
const auto sampleScaling = 1.0 / _samplesPerPixel;
for (int y = 0; y < _frameBuffer->height(); ++y) {
clog << "\rRendering... " << int((y + 1) * 100.0 / _frameBuffer->height()) << "%" << flush;
for (int x = 0; x < _frameBuffer->width(); ++x) {
auto pixelColor = Color(0.0, 0.0, 0.0);
for(int sample = 0; sample < _samplesPerPixel; ++sample) {
auto pixelSample = _pixel00 + x * _viewportDeltaU + y * _viewportDeltaV;
if (_samplesPerPixel > 1) {
const auto pixelSampleSquare =
(-0.5 + randomDouble()) * _viewportDeltaU +
(-0.5 + randomDouble()) * _viewportDeltaV;
pixelSample += pixelSampleSquare;
}
const auto rayOrigin = (_defocusAngle <= 0.0) ? _lookFrom : defocusDiskSample();
const auto rayDirection = pixelSample - rayOrigin;
const auto ray = Ray(rayOrigin, rayDirection);
pixelColor += color(ray, _maxDepth, intersectable);
}
_frameBuffer->draw(x, y, (pixelColor * sampleScaling).gammaCorrected());
}
}
clog << endl;
clog << "Saving image " << filename;
_frameBuffer->save(filename);
clog << endl;
}
61 changes: 61 additions & 0 deletions src/camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include "vector.h"
#include "intersectable.h"
#include "framebuffer.h"

#include <string>
#include <memory>

typedef struct {
double width;
double height;
} Viewport;

class Camera {

private:

double _aspect;
int _samplesPerPixel;
int _maxDepth;
std::shared_ptr<FrameBuffer> _frameBuffer;
Viewport _viewport;
Point _pixel00;
Vector _viewportDeltaU;
Vector _viewportDeltaV;

Point _lookFrom;
Point _lookAt;
Vector _vup;
Vector _u, _v, _w;

double _defocusAngle;
double _focusDist;
Vector _defocusDiskU;
Vector _defocusDiskV;

Point defocusDiskSample() {
const auto p = Vector::randomInUnitDisc();
return _lookFrom + (p.x() * _defocusDiskU) + (p.y() * _defocusDiskV);
}

public:

Camera(double aspect = 16.0 / 9.0,
int samplesPerPixel = 500,
int maxDepth = 50,
int imageWidth = 1800,
double vfov = 20.0,
Vector lookFrom = Vector(13.0, 2.0, 3.0),
Vector lookAt = Vector(0.0, 0.0, 0.0),
Vector vup = Vector(0.0, 1.0, 0.0),
double defocusAngle = 0.6,
double focusDist = 10.0);

double aspect() const { return _aspect; }
const Viewport& viewport() const { return _viewport; }

void render(const std::shared_ptr<const Intersectable>& intersectable, const std::string& filename);

};
37 changes: 37 additions & 0 deletions src/framebuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "framebuffer.h"
#include "interval.h"

#include <fstream>

using namespace std;

FrameBuffer::FrameBuffer(int width, int height):_width(width),_height(height) {
_buffer = new unsigned char[_height * _width * 3];
}

FrameBuffer::~FrameBuffer() {
delete [] _buffer;
}

void FrameBuffer::draw(int x, int y, Color color) {
static const Interval intensity(0.000, 0.999);
const auto pixelOffset = 3 * (x + y * _width);
_buffer[pixelOffset + 0] = 256 * intensity.clamp(color.r());
_buffer[pixelOffset + 1] = 256 * intensity.clamp(color.g());
_buffer[pixelOffset + 2] = 256 * intensity.clamp(color.b());
}

void FrameBuffer::save(const std::string& filename) const {
ofstream outputFile(filename);
outputFile << "P3\n" << _width << ' ' << _height << "\n255\n";
for (int y = 0; y < _height; ++y) {
for (int x = 0; x < _width; ++x) {
const auto pixelOffset = 3 * (x + y * _width);
outputFile << int(_buffer[pixelOffset + 0]) << ' '
<< int(_buffer[pixelOffset + 1]) << ' '
<< int(_buffer[pixelOffset + 2])
<< '\n';
}
}
outputFile.close();
}
25 changes: 25 additions & 0 deletions src/framebuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "vector.h"

#include <string>

class FrameBuffer {

private:

int _width;
int _height;
unsigned char* _buffer;

public:

FrameBuffer(int width, int height);
virtual ~FrameBuffer();

int width() const { return _width; }
int height() const { return _height; }
void draw(int x, int y, Color color);
void save(const std::string& filename) const;

};
51 changes: 51 additions & 0 deletions src/intersectable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include "ray.h"
#include "interval.h"

#include <memory>

class Material;

class Hit {

private:

Point _point;
Vector _normal;
std::shared_ptr<Material> _material;
double _t;
bool _fronFace;

public:

void setPoint(const Point& point) { _point = point; }
void setNormal(const Vector& normal) { _normal = normal; }
void setMaterial(const std::shared_ptr<Material>& material) { _material = material; }
void setT(double t) { _t = t; }
void setFrontFace(bool frontFace) { _fronFace = frontFace; }

const Point& point() const { return _point; }
const Vector& normal() const { return _normal; }
const std::shared_ptr<Material>& material() const { return _material; }
double t() const { return _t; }
bool frontFace() const { return _fronFace; }

void setFaceNormal(const Ray& ray, const Vector& outwardNormal) {
_fronFace = Vector::dot(ray.dir(), outwardNormal) < 0.0;
_normal = _fronFace ? outwardNormal : -outwardNormal;
}

};

class Intersectable {

public:

virtual ~Intersectable() {}
virtual bool intersects(const Ray& ray,
const Interval& interval,
Hit& hit
) const = 0;

};
36 changes: 36 additions & 0 deletions src/interval.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <limits>

class Interval {

private:

double _min;
double _max;

public:

Interval(double min = 0,
double max = std::numeric_limits<double>::infinity()):
_min(min), _max(max) {}

double min() const {
return _min;
}

double max() const {
return _max;
}

bool contains(double x) const {
return _min < x && x < _max;
}

double clamp(double x) const {
if (x < _min) return _min;
if (x > _max) return _max;
return x;
}

};
Loading

0 comments on commit e5eb174

Please sign in to comment.