Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
PoneyUHC committed Jul 11, 2024
2 parents 3857190 + 9e53064 commit 5620ed7
Show file tree
Hide file tree
Showing 26 changed files with 341 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ EXEC_NAME = render.exe
OUTPUT_DIR = output
SRC_DIR = src
SRC = $(wildcard $(SRC_DIR)/*.cpp) $(wildcard $(SRC_DIR)/**/*.cpp)
CFLAGS = -g -Og -Wall -Wpedantic -std=c++17
CFLAGS = -g -O3 -Wall -Wpedantic -std=c++17

default: render.exe

Expand Down
8 changes: 8 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

- Section 11
- improve compilation time
- move as much code as possible in .cpp files
- create blobs
- accelerate with GPU compute shaders
- use raytracing extensions if possible
- make buildchain crossplatform with CMake
12 changes: 4 additions & 8 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ Camera::Camera(const Point3& camera_center, double aspect_ratio, int width, doub
}


std::optional<Vec3> Camera::GetPixelPosition(int i, int j) const
Vec3 Camera::GetPixelPosition(int i, int j) const
{
if(i < 0 || i >= m_image_width || j < 0 || j >= m_image_width) {
return std::nullopt;
}

return m_pixel00_loc + m_pixel_delta_u * i + m_pixel_delta_v * j;
}

Expand All @@ -39,9 +35,9 @@ Vec3 Camera::SamplePositionAroundPixel(int i, int j) const
{
Vec3 offset = sample_in_unit_square();

Vec3 samplePosition = GetPixelPosition(i, j).value()
+ offset.x() * m_pixel_delta_u
+ offset.y() * m_pixel_delta_v;
Vec3 samplePosition = GetPixelPosition(i, j)
+ offset.X() * m_pixel_delta_u
+ offset.Y() * m_pixel_delta_v;

return samplePosition;
}
6 changes: 1 addition & 5 deletions src/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#pragma once

#include "vec.hpp"
#include "ray.hpp"
#include "utils.hpp"

#include <optional>


class Camera {
Expand Down Expand Up @@ -40,7 +36,7 @@ class Camera {
inline double ImageWidth() const { return m_image_width; }
inline Point3 CameraCenter() const { return m_camera_center; }

std::optional<Point3> GetPixelPosition(int i, int j) const;
Point3 GetPixelPosition(int i, int j) const;

Vec3 SamplePositionAroundPixel(int i, int j) const;

Expand Down
6 changes: 3 additions & 3 deletions src/export/ppm_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ int PpmExporter::Export(int width, int height, std::shared_ptr<RGBColor[]> buffe
for(int i=0; i<width; ++i){

RGBColor color = buffer[j* width + i];
double r = color.x();
double g = color.y();
double b = color.z();
double r = color.X();
double g = color.Y();
double b = color.Z();

r = linear_to_gamma(r);
g = linear_to_gamma(g);
Expand Down
11 changes: 11 additions & 0 deletions src/geometry/IHittable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include "geometry/IHittable.hpp"

#include "ray.hpp"


void HitRecord::SetFaceNormal(const Ray& ray, const Vec3& outwardNormal)
{
front_face = ray.Direction().Dot(outwardNormal) < 0.0;
normal = front_face ? outwardNormal : -outwardNormal;
}
21 changes: 10 additions & 11 deletions src/geometry/IHittable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@
#pragma once

#include "vec.hpp"
#include "ray.hpp"
#include "interval.hpp"

#include <memory>


class IHittable;
class IHittable;
class Material;
class Interval;
class Ray;


struct HitRecord {

Point3 hitPoint;
Vec3 normal;
double t;
bool front_face;
double t = 0.0;
bool front_face = true;
std::shared_ptr<IHittable> object;
std::shared_ptr<Material> material;


void SetFaceNormal(const Ray& ray, const Vec3& outwardNormal)
{
front_face = ray.Direction().Dot(outwardNormal) < 0.0;
normal = front_face ? outwardNormal : -outwardNormal;
}

void SetFaceNormal(const Ray& ray, const Vec3& outwardNormal);
};


// REFACTOR: create a subclass for IHittable (Mesh for example), that is a hittable that has a material
class IHittable {

public:
Expand Down
2 changes: 2 additions & 0 deletions src/geometry/scene.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include "geometry/scene.hpp"

#include "interval.hpp"


using namespace std;

Expand Down
1 change: 1 addition & 0 deletions src/geometry/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <memory>
#include <vector>


class Scene : public IHittable {

private:
Expand Down
4 changes: 4 additions & 0 deletions src/geometry/sphere.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

#include "sphere.hpp"

#include "ray.hpp"
#include "interval.hpp"

#include <cmath>


Expand Down Expand Up @@ -39,6 +42,7 @@ bool Sphere::Hit(const Ray& ray, const Interval& interval, HitRecord& outRecord)
outRecord.hitPoint = ray.At(root);
Vec3 outwardNormal = (outRecord.hitPoint - m_center) / m_radius;
outRecord.SetFaceNormal(ray, outwardNormal);
outRecord.material = m_material;

return true;
}
6 changes: 5 additions & 1 deletion src/geometry/sphere.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
#include "geometry/IHittable.hpp"


class Material;


class Sphere : public IHittable {

private:

Point3 m_center;
double m_radius;
std::shared_ptr<Material> m_material;


public:

Sphere(const Point3& center, double radius) : m_center{center}, m_radius{radius} {};
Sphere(const Point3& center, double radius, std::shared_ptr<Material> material) : m_center{center}, m_radius{radius}, m_material{material} {};

bool Hit(const Ray& ray, const Interval& interval, HitRecord& outRecord) const override;

Expand Down
3 changes: 3 additions & 0 deletions src/interval.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

#include "interval.hpp"

#include <cmath>


const Interval Interval::Empty = Interval(INFINITY, -INFINITY);
const Interval Interval::Universe = Interval(-INFINITY, INFINITY);
2 changes: 0 additions & 2 deletions src/interval.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

#pragma once

#include <cmath>


class Interval {

Expand Down
62 changes: 46 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "utils.hpp"
#include "geometry/sphere.hpp"
#include "geometry/scene.hpp"
#include "material/lambertian.hpp"
#include "material/metal.hpp"

#include <iostream>
#include <memory>
Expand All @@ -13,37 +15,65 @@
using namespace std;


int main(int argc, char *argv[]){

if (argc != 2){
cout << "Usage : " << argv[0] << " width" << endl;
return 1;
}

int width = atoi(argv[1]);


shared_ptr<Camera> InitCamera(int width)
{
Point3 cameraPosition = Point3{0,0,0};
double aspectRatio = 16.0 / 9.0;
double focalLength = 1.0;

auto camera = make_shared<Camera>(cameraPosition, aspectRatio, width, focalLength);
int height = camera->ImageHeight();
return make_shared<Camera>(cameraPosition, aspectRatio, width, focalLength);
}


shared_ptr<Scene> InitScene()
{
auto material_ground = make_shared<Lambertian>(RGBColor(0.8, 0.8, 0.0));
auto material_center = make_shared<Lambertian>(RGBColor(0.1, 0.2, 0.5));
auto material_left = make_shared<Metal>(RGBColor(0.8, 0.8, 0.8), 0.3);
auto material_right = make_shared<Metal>(RGBColor(0.8, 0.6, 0.2), 1.0);

auto sphere1 = make_shared<Sphere>(Point3( 0.0, -100.5, -1.0), 100.0, material_ground);
auto sphere2 = make_shared<Sphere>(Point3( 0.0, 0.0, -1.2), 0.5, material_center);
auto sphere3 = make_shared<Sphere>(Point3(-1.0, 0.0, -1.0), 0.5, material_left);
auto sphere4 = make_shared<Sphere>(Point3( 1.0, 0.0, -1.0), 0.5, material_right);

auto scene = make_shared<Scene>();
auto sphere1 = make_shared<Sphere>(Point3(0,0,-1), 0.5);
auto sphere2 = make_shared<Sphere>(Point3(0,-100.5,-1), 100);
scene->AddObject(sphere1);
scene->AddObject(sphere2);
scene->AddObject(sphere3);
scene->AddObject(sphere4);

return scene;
}


PathTracingRenderer InitRenderer(shared_ptr<Camera> camera, shared_ptr<Scene> scene)
{
PathTracingRendererParams params;
params.aa_sample_per_pixel = 20;
params.max_depth = 5;
PathTracingRenderer renderer(camera, scene, std::move(params));

return PathTracingRenderer(camera, scene, std::move(params));
}


int main(int argc, char *argv[]){

if (argc != 2){
cout << "Usage : " << argv[0] << " width" << endl;
return 1;
}

int width = atoi(argv[1]);

shared_ptr<Camera> camera = InitCamera(width);
shared_ptr<Scene> scene = InitScene();

PathTracingRenderer renderer = InitRenderer(camera, scene);
renderer.Render();

PpmExporter ppmExporter("output/render.ppm");
ppmExporter.Export(width, height, renderer.GetBuffer());
ppmExporter.Export(camera->ImageWidth(), camera->ImageHeight(), renderer.GetBuffer());

return 0;
}
28 changes: 28 additions & 0 deletions src/material/lambertian.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#include "lambertian.hpp"

#include "vec.hpp"
#include "ray.hpp"
#include "geometry/IHittable.hpp"


using namespace std;


bool Lambertian::Scatter(
const Ray& incoming_ray,
const HitRecord& rec,
RGBColor& attenuation,
Ray& scattered_ray
) const
{
Vec3 scatter_direction = rec.normal + Vec3::RandomUnitVector();

if (scatter_direction.IsNearZero()){
scatter_direction = rec.normal;
}

scattered_ray = Ray(rec.hitPoint, scatter_direction);
attenuation = m_albedo;
return true;
}
25 changes: 25 additions & 0 deletions src/material/lambertian.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#pragma once

#include "material/material.hpp"


class Lambertian : public Material {

private:

RGBColor m_albedo;


public:

Lambertian(const RGBColor& albedo): m_albedo{albedo} {};

virtual bool Scatter(
const Ray& incoming_ray,
const HitRecord& rec,
RGBColor& attenuation,
Ray& scattered_ray
) const override;

};
27 changes: 27 additions & 0 deletions src/material/material.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

#pragma once

#include "vec.hpp"


class Ray;
class HitRecord;


class Material {

public:

virtual ~Material() {}
virtual bool Scatter(
const Ray& incoming_ray,
const HitRecord& rec,
RGBColor& attenuation,
Ray& scattered_ray
) const
{
return false;
}


};
Loading

0 comments on commit 5620ed7

Please sign in to comment.