Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
PoneyUHC committed Sep 6, 2024
2 parents e3b37cc + 08f16bf commit a349f4a
Show file tree
Hide file tree
Showing 37 changed files with 684 additions and 252 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

CXX = g++
RM = rm -f
CXXFLAGS = -O3 -Wall -Wpedantic -std=c++17 -MMD -MP -fopenmp
CXXFLAGS = -O3 -Wall -Wpedantic -std=c++20 -MMD -MP -fopenmp

EXEC = render.exe
OUTPUT_DIR = output
Expand Down Expand Up @@ -37,7 +37,7 @@ $(EXEC): $(OBJS_LOCATION)

$(BUILD_OBJS_DIR)/%.o: %.cpp Makefile
@$(eval CURRENT_TARGET=$(shell expr $(CURRENT_TARGET) + 1))
@printf "\rCompiling file $(CURRENT_TARGET) / $(NB_TARGETS)"
@printf "\rCompiling file $(CURRENT_TARGET) / $(NB_TARGETS) "
@if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi
@$(CXX) $(CXXFLAGS) -I $(SRC_DIR) -c $< -o $@

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

- Book 2 section 4 : texture mapping
- Book 2 section 8 : instances
- make buildchain crossplatform with CMake
- test to replace RNG for grid sampling in antialiasing
- centralize asset loading in a resource manager
- find a good name for the project
- have a format describing scenes instead of code description
- add log system
- add render queue
2 changes: 1 addition & 1 deletion run.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if [ "$result" -ne "0" ]; then
fi

source ./config.bash
./build/executable/render.exe $1
./build/executable/render.exe $1 $2 $3 $4
result=$?

if [ "$result" -eq "0" ]; then
Expand Down
13 changes: 7 additions & 6 deletions src/assets/image_asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "image_asset.hpp"

#include "utils.hpp"
#include "logger.hpp"

#define STB_IMAGE_IMPLEMENTATION
#include "third_party/stb_image.h"
Expand All @@ -17,11 +18,11 @@ namespace fs = std::filesystem;

int ImageAsset::Load()
{
clog << "Loading " << m_filepath << " ..." << endl;
fs::path path(m_filepath);
Logger::LogInfo("Loading " + m_filepath + " ...");

fs::path path(m_filepath);
if(!fs::exists(m_filepath)){
cerr << "\t" << m_filepath << ": file not found" << endl;
Logger::LogError("\t" + m_filepath + ": file not found");
return false;
}

Expand All @@ -30,8 +31,8 @@ int ImageAsset::Load()
float* fdata;
fdata = stbi_loadf(path.c_str(), &m_width, &m_height, &n, m_bytes_per_pixel);
if(fdata == NULL){
cerr << "\t" << m_filepath << ": error while loading file" << endl;
cerr << "\t" << stbi_failure_reason() << endl;
Logger::LogError("\t" + m_filepath + ": error while loading file");
Logger::LogError("\t" + string(stbi_failure_reason()));
return false;
}

Expand All @@ -45,7 +46,7 @@ int ImageAsset::Load()
m_bdata[i] = rgb_normalized_to_8bits(m_fdata[i]);
}

clog << "Loaded " << m_filepath << " successfully" << endl;
Logger::LogInfo("Loaded " + m_filepath + " successfully");
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/export/png_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include "png_exporter.hpp"

#include "utils.hpp"
#include "logger.hpp"

#include <cstring>
#include <fstream>
#include <filesystem>
#include <iostream>


using namespace std;
Expand Down Expand Up @@ -98,7 +98,7 @@ void PngExporter::ImageToScanline(uint8_t filtering, uint8_t *dest) const
for(uint32_t j=0; j<m_height; ++j){
*data_ptr++ = filtering;
for(uint32_t i=0; i<m_width; ++i){
RGBColor& color = m_data_buffer[j*m_width + i];
RGBColor color = m_data_buffer[j*m_width + i];
color[0] = linear_to_gamma(color[0]);
color[1] = linear_to_gamma(color[1]);
color[2] = linear_to_gamma(color[2]);
Expand Down Expand Up @@ -220,7 +220,7 @@ int PngExporter::Export(int width, int height, shared_ptr<RGBColor[]> buffer)

m_output_fs = ofstream(m_filepath, std::ios::out | std::ios::binary);
if ( !m_output_fs.is_open()) {
cerr << __FUNCTION__ << " : failed to open " << m_filepath << endl;
Logger::LogError(string(__FUNCTION__) + " : failed to open " + m_filepath);
return 1;
}

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 @@ -3,9 +3,9 @@
#include "renderer/camera.hpp"
#include "math/interval.hpp"
#include "utils.hpp"
#include "logger.hpp"

#include <filesystem>
#include <iostream>
#include <fstream>


Expand All @@ -23,7 +23,7 @@ int PpmExporter::Export(int width, int height, std::shared_ptr<RGBColor[]> buffe

ofstream file(m_filepath);
if ( !file.is_open()) {
cerr << __FUNCTION__ << " : failed to open " << m_filepath << endl;
Logger::LogError(string(__FUNCTION__) + " : failed to open " + m_filepath);
return 1;
}

Expand All @@ -32,7 +32,7 @@ int PpmExporter::Export(int width, int height, std::shared_ptr<RGBColor[]> buffe
for(int j=0; j<height; ++j){
for(int i=0; i<width; ++i){

RGBColor& color = buffer[j* width + i];
RGBColor color = buffer[j* width + i];
color[0] = linear_to_gamma(color[0]);
color[1] = linear_to_gamma(color[1]);
color[2] = linear_to_gamma(color[2]);
Expand Down
18 changes: 18 additions & 0 deletions src/geometry/aabb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ int AABB::LongestAxis() const
}


AABB::AABB(const Interval &x, const Interval &y, const Interval &z)
: m_x{x}, m_y{y}, m_z{z}
{
PadToMinimums();
}


AABB::AABB(const Point3 &a, const Point3 &b)
{
m_x = (a[0] <= b[0]) ? Interval(a[0], b[0]) : Interval(b[0], a[0]);
m_y = (a[1] <= b[1]) ? Interval(a[1], b[1]) : Interval(b[1], a[1]);
m_z = (a[2] <= b[2]) ? Interval(a[2], b[2]) : Interval(b[2], a[2]);

PadToMinimums();
}


Expand Down Expand Up @@ -75,3 +84,12 @@ bool AABB::Hit(const Ray &ray, const Interval &interval) const

return true;
}


void AABB::PadToMinimums()
{
static double delta = 0.0001;
if(m_x.Size() < delta) m_x.Expand(delta);
if(m_y.Size() < delta) m_y.Expand(delta);
if(m_z.Size() < delta) m_z.Expand(delta);
}
6 changes: 5 additions & 1 deletion src/geometry/aabb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ class AABB {
public:

AABB() {}
AABB(const Interval& x, const Interval& y, const Interval& z): m_x{x}, m_y{y}, m_z{z} {}
AABB(const Interval& x, const Interval& y, const Interval& z);
AABB(const Point3& a, const Point3& b);
AABB(const AABB& aabb1, const AABB& aabb2);

bool Hit(const Ray& ray, const Interval& interval) const;
const Interval& IndexToInterval(int index) const;
int LongestAxis() const;

// To avoid intersection miss in case of exterme intervals,
// we pad the bounding boxes a bit
void PadToMinimums();

static const AABB Empty, Universe;
};
64 changes: 64 additions & 0 deletions src/geometry/quad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

#include "quad.hpp"

#include "math/ray.hpp"


using namespace std;


Quad::Quad(const Point3 &q, const Vec3 &u, const Vec3 &v, shared_ptr<IMaterial> material)
: m_q{q}, m_u{u}, m_v{v}, m_material{material}
{
Vec3 n = m_u.Cross(m_v);
m_normal = n.Normalized();
m_d = m_normal.Dot(m_q);
m_w = n / n.Dot(n);

AABB diag1_aabb = AABB(m_q, m_q + m_u + m_v);
AABB diag2_aabb = AABB(m_q + m_u, m_q + m_v);
m_aabb = AABB(diag1_aabb, diag2_aabb);
}


bool Quad::Hit(const Ray &ray, const Interval &interval, HitRecord &outRecord) const
{
double denominator = m_normal.Dot(ray.Direction());
if(std::fabs(denominator) < 1e-8){
return false;
}

double t = (m_d - m_normal.Dot(ray.Origin())) / denominator;

if(! interval.Contains(t)){
return false;
}

Vec3 intersection = ray.At(t);

Vec3 plan_centered_hitpoint = intersection - m_q;
double alpha = m_w.Dot(plan_centered_hitpoint.Cross(m_v));
double beta = m_w.Dot(m_u.Cross(plan_centered_hitpoint));

if(! IsInQuad(alpha, beta, outRecord)){
return false;
}

outRecord.t = t;
outRecord.hit_point = intersection;
outRecord.material = m_material;
outRecord.SetFaceNormal(ray, m_normal);

return true;
}


bool Quad::IsInQuad(double alpha, double beta, HitRecord& hit_record) const {
static const Interval unit_interval = Interval(0.0, 1.0);

// set it anyway to make code branchless
hit_record.u = alpha;
hit_record.v = beta;

return unit_interval.Contains(alpha) && unit_interval.Contains(beta);
}
38 changes: 38 additions & 0 deletions src/geometry/quad.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

#pragma once


#include "geometry/IHittable.hpp"

#include "math/vec.hpp"


class Quad : public IHittable {

private:

Point3 m_q;
Vec3 m_u;
Vec3 m_v;

Vec3 m_normal;
// d is the intersection equation result for the plan
// for a point Q on the plane, n.Q = d
double m_d;
// w is a constant for a given quadrilateral
// It simplifies the check to know if we are inside the quad
Vec3 m_w;

AABB m_aabb;
std::shared_ptr<IMaterial> m_material;

bool IsInQuad(double alpha, double beta, HitRecord& hit_record) const;


public:

Quad(const Point3& q, const Vec3& u, const Vec3& v, std::shared_ptr<IMaterial> material);

bool Hit(const Ray& ray, const Interval& interval, HitRecord& outRecord) const override;
AABB GetAABB() const override { return m_aabb; }
};
3 changes: 2 additions & 1 deletion src/geometry/sphere.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class Sphere : public IHittable {

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

AABB m_aabb;
std::shared_ptr<IMaterial> m_material;


public:
Expand Down
36 changes: 36 additions & 0 deletions src/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include "logger.hpp"

#include <sstream>
#include <chrono>
#include <iostream>


using namespace std;
using namespace std::chrono;


string Logger::GetCurrentTimeFormatted()
{
auto now = std::chrono::system_clock::now();
return format("[{:%T}]", zoned_time{ current_zone(), floor<milliseconds>(now)});
}


void Logger::Log(const std::string &log, const LogLevel& level)
{
if(level < s_level){
return;
}

ostringstream string_builder;
string_builder << GetCurrentTimeFormatted() << " ";
string_builder << s_colored_name_lookup.at(level) << " ";
string_builder << log;
string_builder << (s_overwrite ? '\r' : '\n');

const string& formatted_log = string_builder.str();
for(ostream* stream : s_outputs){
*stream << formatted_log << flush;
}
}
Loading

0 comments on commit a349f4a

Please sign in to comment.