Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
PoneyUHC committed Jun 29, 2024
2 parents 7fd1352 + 0bae389 commit 3857190
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 10 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 -O0 -Wall -Wpedantic -std=c++17
CFLAGS = -g -Og -Wall -Wpedantic -std=c++17

default: render.exe

Expand Down
11 changes: 11 additions & 0 deletions src/export/IExporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,15 @@ class IExporter {
virtual ~IExporter() {};
virtual int Export(int width, int height, std::shared_ptr<RGBColor[]> buffer) const = 0;

// Image viewers expect gamma corrected images most of the time
// We approximate the gamme correction with a gamma 2 transform.
// Going from linear space to gamma space, it means 1 / gamma, ie sqrt()
static double linear_to_gamma(double linearComponent){
if(linearComponent > 0){
return sqrt(linearComponent);
}

return 0;
}

};
4 changes: 4 additions & 0 deletions src/export/ppm_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ int PpmExporter::Export(int width, int height, std::shared_ptr<RGBColor[]> buffe
double r = color.x();
double g = color.y();
double b = color.z();

r = linear_to_gamma(r);
g = linear_to_gamma(g);
b = linear_to_gamma(b);

static const Interval intensity(0.000, 0.999);
int rbyte = int(256 * intensity.Clamp(r));
Expand Down
7 changes: 6 additions & 1 deletion src/geometry/IHittable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
#include "ray.hpp"
#include "interval.hpp"

#include <memory>


class IHittable;

struct HitRecord {

Point3 hitPoint;
Vec3 normal;
double t;
bool front_face;
std::shared_ptr<IHittable> object;


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

Expand Down
3 changes: 2 additions & 1 deletion src/geometry/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ using namespace std;
bool Scene::Hit(const Ray& ray, const Interval& interval, HitRecord& outRecord) const
{
HitRecord tmpRecord;
bool bHit;
bool bHit = false;
double closest = interval.Max();

for(const shared_ptr<IHittable>& object : m_objects){
if (object->Hit(ray, Interval(interval.Min(), closest), tmpRecord)){
bHit = true;
closest = tmpRecord.t;
tmpRecord.object = object;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ int main(int argc, char *argv[]){
scene->AddObject(sphere2);

PathTracingRendererParams params;
params.aa_sample_per_pixel = 10;
params.aa_sample_per_pixel = 20;
params.max_depth = 5;
PathTracingRenderer renderer(camera, scene, std::move(params));
renderer.Render();

Expand Down
14 changes: 10 additions & 4 deletions src/renderer/pathtracing_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void PathTracingRenderer::Render() {
RGBColor accumulator(0,0,0);
for(int sample = 0; sample < m_params.aa_sample_per_pixel; ++sample){
Ray sampleRay = SampleRayForPixel(i, j);
accumulator += GetRayColor(sampleRay);
accumulator += GetRayColor(sampleRay, m_params.max_depth);
}

accumulator /= m_params.aa_sample_per_pixel;
Expand All @@ -42,11 +42,17 @@ void PathTracingRenderer::Render() {
}


RGBColor PathTracingRenderer::GetRayColor(const Ray& ray)
RGBColor PathTracingRenderer::GetRayColor(const Ray& ray, size_t depth)
{
if(depth == 0){
return RGBColor(0, 0, 0);
}

HitRecord hitRecord;
if( m_scene->Hit(ray, Interval(0, INFINITY), hitRecord) ){
return 0.5 * (hitRecord.normal + RGBColor(1,1,1));
if( m_scene->Hit(ray, Interval(0.001, INFINITY), hitRecord) ){
Vec3 bounce_direction = hitRecord.normal + Vec3::RandomUnitVector();
Ray newRay = Ray(hitRecord.hitPoint, bounce_direction);
return 0.5 * GetRayColor(newRay, depth-1);
}

Vec3 unitDirection = ray.Direction().Normalized();
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/pathtracing_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

struct PathTracingRendererParams {
int aa_sample_per_pixel;
int max_depth;
};


Expand All @@ -29,7 +30,7 @@ class PathTracingRenderer : public IRenderer {
PathTracingRenderer(std::shared_ptr<Camera> camera, std::shared_ptr<Scene> scene, PathTracingRendererParams&& params);

void Render() override;
RGBColor GetRayColor(const Ray& ray);
RGBColor GetRayColor(const Ray& ray, size_t depth);

inline std::shared_ptr<RGBColor[]> GetBuffer() { return m_buffer; }

Expand Down
2 changes: 1 addition & 1 deletion src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ inline double lerp(double start, double stop, double value)
}


inline Vec3 lerp(Vec3 start, Vec3 stop, double value)
inline Vec3 lerp(const Vec3& start, const Vec3& stop, double value)
{
return start * (1-value) + stop * value;
}
Expand Down
31 changes: 31 additions & 0 deletions src/vec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

#include <cmath>

#include <iostream>


double random_double();
double random_double(double min, double max);


class Vec3 {

Expand Down Expand Up @@ -41,6 +47,31 @@ class Vec3 {
inline double Magnitude() const { return std::sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]); }
inline double MagnitudeSquared() const { return e[0] * e[0] + e[1] * e[1] + e[2] * e[2]; }
inline Vec3 Normalized() const { return *this / this->Magnitude(); }


// Requieres the current object to be a unit vector
inline Vec3 RandomInSameHemisphere() const {
Vec3 other = RandomUnitVector();
return other.Dot(*this) > 0.0 ? other : -other;
}

// Simple random in-square generation is not enough,
// as the distribution in the square would be uniform,
// but not the distribution of the projected vectors on unit-sphere
// Thus, we only take vector generated inside the sphere.
static inline Vec3 RandomUnitVector() {
Vec3 result;
while(true){
result = Vec3::Random(-1, 1);
if(result.MagnitudeSquared() < 1.0){
break;
}
}
return result.Normalized();
}

static inline Vec3 Random() { return Vec3(random_double(), random_double(), random_double()); }
static inline Vec3 Random(double min, double max) { return Vec3(random_double(min, max), random_double(min, max), random_double(min, max)); }
};


Expand Down

0 comments on commit 3857190

Please sign in to comment.