Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mostly formatting changes, added a few suggestions #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions disposable_notice.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Did I miss the performance tests?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file

47 changes: 31 additions & 16 deletions source/accelerators/bb-accelerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ int BBAccelerator::findSplitIndex(size_t startIndex, size_t endIndex,
return (startIndex + endIndex) / 2;
}

std::unique_ptr<BBNode> BBAccelerator::split(size_t startIndex, size_t endIndex,
std::vector<std::pair<BoundingBox, size_t>>& boxes) {
std::unique_ptr<BBNode> BBAccelerator::split(
size_t startIndex, size_t endIndex,
std::vector<std::pair<BoundingBox, size_t>>& boxes)
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All brackets go after the function

std::unique_ptr<BBNode> node = std::make_unique<BBNode>();
if (endIndex - startIndex == 1) {
node->itemIndex = boxes[startIndex].second;
Expand Down Expand Up @@ -46,12 +48,9 @@ std::unique_ptr<BBNode> BBAccelerator::split(size_t startIndex, size_t endIndex,

float BBAccelerator::pointAxisValue(const Point3& p, const Axis& axis) const {
switch(axis) {
case Axis::X_AXIS:
return p.x;
case Axis::Y_AXIS:
return p.y;
case Axis::Z_AXIS:
return p.z;
case Axis::X_AXIS: return p.x;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intend one tab less to align with switch

case Axis::Y_AXIS: return p.y;
case Axis::Z_AXIS: return p.z;
};
std::cout << "Warning: Undefined axis, returning X_AXIS" << std::endl;
return p.x;
Expand All @@ -70,15 +69,23 @@ float BBAccelerator::calculateGapAverage(std::vector<Point3>& midPoints, const A
- pointAxisValue(midPoints[0], axis)) / (midPoints.size() - 1);
}

Axis BBAccelerator::findSortAxis(const std::vector<std::pair<BoundingBox, size_t>>& boxes,
size_t startIndex, size_t endIndex) const {
Axis BBAccelerator::findSortAxis(
const std::vector<std::pair<BoundingBox, size_t>>& boxes,
size_t startIndex, size_t endIndex) const
{
std::vector<Point3> midPoints;
for (int i = startIndex; i < endIndex; ++i) {
midPoints.push_back(boxes[i].first.mid());
}
float xavg = calculateGapAverage(midPoints, Axis::X_AXIS);
float yavg = calculateGapAverage(midPoints, Axis::Y_AXIS);
float zavg = calculateGapAverage(midPoints, Axis::Z_AXIS);
// float target_axis = std::max(xavg, yag, zavg)
//
// If the above algorithm is not optimal, just hide it
// in a costum procedure.
//
// map (target_axis -> AXIS) use array? some light map?
if (xavg > yavg) {
if (xavg > zavg) {
return Axis::X_AXIS;
Expand All @@ -92,15 +99,20 @@ Axis BBAccelerator::findSortAxis(const std::vector<std::pair<BoundingBox, size_t
}

void BBAccelerator::mergeRoots() {
auto sortNodes = [&](const std::unique_ptr<BBNode>& n0,
auto sortNodes =
[&](const std::unique_ptr<BBNode>& n0,
const std::unique_ptr<BBNode>& n1) {
// assign m_boundingBoxes[no->itemIndex.mid().x
// assign m_boundingBoxes[n1->itemIndex.mid().x
return m_boundingBoxes[n0->itemIndex].mid().x <
m_boundingBoxes[n1->itemIndex].mid().x;
};

std::sort(m_roots.begin(), m_roots.end(), sortNodes);
while (m_roots.size() > 1) {
int endIndex = m_roots.size() - m_roots.size() % 2;
for (int i = 0; i < endIndex; i += 2) {
// Define what this block does in a new procedure
int nextIndex = m_boundingBoxes.size();
BoundingBox b;
b.addBoundingBox(m_boundingBoxes[m_roots[i]->itemIndex]);
Expand All @@ -121,7 +133,9 @@ void BBAccelerator::mergeRoots() {
m_root = m_roots[0].get();
}

void BBAccelerator::initialize(const std::unique_ptr<Entity>* entities, size_t numberOfEntities,
void BBAccelerator::initialize(
const std::unique_ptr<Entity>* entities,
size_t numberOfEntities,
const std::vector<int>& objects) {
m_entities = entities;
std::vector<std::pair<BoundingBox, size_t>> boxes;
Expand All @@ -140,19 +154,20 @@ void BBAccelerator::initialize(const std::unique_ptr<Entity>* entities, size_t n
}

bool BBAccelerator::intersects(const Ray& ray, size_t ignoreID, EntityIntersection* result, bool findAny) const {
const BBNode* stack[128];
int stackBack;
const BBNode* current;
const BBNode* stack[128]; // Move closer to assigment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just want to allocate the array here

int stackBack; // Join with assignment
const BBNode* current; // Would argue about putting it into the while loop
bool hit = false;
const Vector3 invrDir = ONE_VECTOR / ray.direction;
stack[0] = m_root->left.get();
stack[1] = m_root->right.get();
stackBack = 2;
stackBack = 2; // General Suggestion of writting in snake_case

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

while (stackBack > 0) {
current = stack[--stackBack];
if (current->isLeaf()) {
if (m_entities[current->itemIndex]->getID() != ignoreID
&& m_entities[current->itemIndex]->intersects(ray, result)) {
// findAny what? Also foundAnyObject...
if (findAny) {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions source/accelerators/bb-accelerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ class BBAccelerator {
int findSplitIndex(size_t startIndex, size_t endIndex,
const std::vector<std::pair<BoundingBox, size_t>>& boxes,
const Axis& axis) const;

std::unique_ptr<BBNode> split(size_t startIndex, size_t endIndex,
std::vector<std::pair<BoundingBox, size_t>>& boxes);

float pointAxisValue(const Point3& p, const Axis& axis) const;
bool pointCompAxis(const Point3& p0, const Point3& p1, const Axis& axis) const;
float calculateGapAverage(std::vector<Point3>& midPoints, const Axis& axis) const;

Axis findSortAxis(const std::vector<std::pair<BoundingBox, size_t>>& boxes,
size_t startIndex, size_t endIndex) const;

bool intersects(const Ray& ray, size_t ignoreID, EntityIntersection* result, bool findAny) const;
void mergeRoots();

Expand Down
5 changes: 5 additions & 0 deletions source/accelerators/bounding-box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ bool BoundingBox::intersectsAny(const Ray& ray, const Vector3& invDir, float max
}

if (!std::isinf(invDir.x)) {
// Hide the complex conidtion in a procedure

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to refactor these if you can

if (intersectSide(start.x, ray.origin.x, invDir.x,
ray.origin.z, ray.origin.y, ray.direction.z,
ray.direction.y, start.z, start.y, end.z, end.y, maxT) > 0.0f
Expand All @@ -116,6 +117,9 @@ bool BoundingBox::intersectsAny(const Ray& ray, const Vector3& invDir, float max
}

if (!std::isinf(invDir.y)) {
// I would argue this is the same condition,
// just with the order of arguments changed
// eg checkCondition (x, y, z), checkCondition (y, z, x)...
if (intersectSide(start.y, ray.origin.y, invDir.y,
ray.origin.x, ray.origin.z, ray.direction.x,
ray.direction.z, start.x, start.z, end.x, end.z, maxT) > 0.0f
Expand All @@ -127,6 +131,7 @@ bool BoundingBox::intersectsAny(const Ray& ray, const Vector3& invDir, float max
}

if (!std::isinf(invDir.z)) {
// Same here
if (intersectSide(start.z, ray.origin.z, invDir.z,
ray.origin.x, ray.origin.y, ray.direction.x,
ray.direction.y, start.x, start.y, end.x, end.y, maxT) > 0.0f
Expand Down
1 change: 1 addition & 0 deletions source/accelerators/bounding-box.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BoundingBox {
float intersectSide(float p, float o, float d, float ox, float oy,
float dx, float dy, float sx, float sy, float ex, float ey,
float maxT) const;

void updateIfValid(float t, float* currentT, float* maxT) const;

public:
Expand Down
3 changes: 3 additions & 0 deletions source/cameras/camera-sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ class CameraSample {
Sample2D lensPosition;

CameraSample() { }

CameraSample(const CameraSample& other) : filmPosition(other.filmPosition),
lensPosition(other.lensPosition) { }

CameraSample(const Point2& filmPosition, const Sample2D& lensPosition) :
filmPosition(filmPosition), lensPosition(lensPosition) { }

CameraSample(CameraSample&& other) noexcept : filmPosition(std::move(other.filmPosition)),
lensPosition(std::move(other.lensPosition)) { }

Expand Down
7 changes: 5 additions & 2 deletions source/cameras/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

Camera::Camera(const Camera& other) : m_cameraToWorld(other.m_cameraToWorld),
m_dx(other.m_dx), m_dy(other.m_dy), m_rasterToFilm(other.m_rasterToFilm) { }
Camera::Camera(const Transformation* cameraToWorld, int resolutionWidth,
int resolutionHeight) : m_cameraToWorld(cameraToWorld) {

Camera::Camera(
const Transformation* cameraToWorld, int resolutionWidth,
int resolutionHeight) : m_cameraToWorld(cameraToWorld)
{
Transformation toFilm = transform::scale(1.0f / (float)(resolutionWidth),
1.0f / (float)(resolutionHeight), 1.0f);
Transformation centerFilm = transform::translate(-1.0f, 1.0f, 0.0f)
Expand Down
1 change: 1 addition & 0 deletions source/cameras/orthographic-camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "primitives/transformation.h"

OrthographicCamera::OrthographicCamera(const OrthographicCamera& other) : ProjectiveCamera(other) { }

OrthographicCamera::OrthographicCamera(const Transformation* cameraToWorld,
int resolutionWidth, int resolutionHeight,
float lensRadius, float focalDistance, float scale) :
Expand Down
1 change: 1 addition & 0 deletions source/cameras/perspective-camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

PerspectiveCamera::PerspectiveCamera(const PerspectiveCamera& other) :
ProjectiveCamera(other) { }

PerspectiveCamera::PerspectiveCamera(const Transformation* cameraToWorld,
int resolutionWidth, int resolutionHeight,
float lensRadius, float focalDistance, float fov) :
Expand Down
3 changes: 2 additions & 1 deletion source/cameras/projective-camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ ProjectiveCamera::ProjectiveCamera(const Transformation* cameraToWorld,
int resolutionWidth, int resolutionHeight,
const Transformation& projection, float lensRadius, float focalDistance) :
Camera(cameraToWorld, resolutionWidth, resolutionHeight),
m_lensRadius(lensRadius), m_focalDistance(focalDistance) {
m_lensRadius(lensRadius), m_focalDistance(focalDistance)
{
m_unprojection = projection.inverse() * m_rasterToFilm;
Point3 worldDeltasX(1.0f, 0.0f, 0.0f);
Point3 worldDeltasY(0.0f, 1.0f, 0.0f);
Expand Down
1 change: 1 addition & 0 deletions source/core/render-settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct RenderSettings {
RenderSettings(const RenderSettings& other) : resolutionWidth(other.resolutionWidth),
resolutionHeight(other.resolutionHeight), tileSize(other.tileSize), samples(other.samples),
threads(other.threads), filter(other.filter) { }

RenderSettings(unsigned int resW, unsigned int resH, unsigned int tileSize,
unsigned int samples, unsigned int threads, const Filter* filter) :
resolutionWidth(resW), resolutionHeight(resH), tileSize(tileSize), samples(samples),
Expand Down
24 changes: 18 additions & 6 deletions source/core/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@
#include <thread>
#include <functional>

// Generally code is read vertically faster than horizontally
Renderer::Renderer(Scene* scene, Camera* camera, Integrator* integrator,
Sampler* sampler, RenderSettings settings) : m_scene(scene), m_camera(camera),
m_integrator(integrator), m_sampler(sampler), m_settings(settings) { }
Sampler* sampler, RenderSettings settings) :
m_scene(scene), m_camera(camera), m_integrator(integrator),
m_sampler(sampler), m_settings(settings) { }

Image Renderer::render() {
Timer timer;
auto film = std::make_unique<Film>(m_settings.resolutionWidth, m_settings.resolutionHeight, m_settings.filter);
auto film = std::make_unique<Film>(
m_settings.resolutionWidth,
m_settings.resolutionHeight,
m_settings.filter);

auto sampler = m_sampler->clone();
m_scene->initializeAccelerator();
m_integrator->setup(m_scene, m_camera, film.get(), sampler.get(), m_settings);

auto worker = [&] () {
m_integrator->render(m_scene, m_camera, film.get(), sampler.get(), m_settings);
m_integrator->render(
m_scene, m_camera, film.get(),
sampler.get(), m_settings);
};

std::cout << "Starting rendering at: " << m_settings.resolutionWidth
<< " x " << m_settings.resolutionHeight << " resolution, with: " << m_settings.samples
<< " x " << m_settings.resolutionHeight
<< " resolution, with: " << m_settings.samples
<< " samples, using: " << m_settings.threads << " threads" << '\n';

std::vector<std::thread> threads;
Expand All @@ -36,7 +45,10 @@ Image Renderer::render() {

std::cout << "\rCompleted: 100 % " << '\n';
timer.stop();
std::cout << "Finished rendering in: " << timer.getDuration().count() << " seconds" << '\n';
std::cout
<< "Finished rendering in: "
<< timer.getDuration().count()
<< " seconds" << '\n';
return film->getImage();
}

3 changes: 2 additions & 1 deletion source/core/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Scene {
Skybox* m_skybox;
int m_nextID;

void fillIntersectionFromEntity(const EntityIntersection& entityIntersection,
void fillIntersectionFromEntity(
const EntityIntersection& entityIntersection,
Intersection* result) const;

public:
Expand Down
8 changes: 5 additions & 3 deletions source/entities/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class Entity {

public:
Entity() = delete;
Entity(const Entity& other) : Entity(other.m_mesh, other.m_material, other.m_id) { }
Entity(const Triangle* mesh, const Material* material, int id) : m_mesh(mesh),
m_material(material), m_id(id) { }
Entity(const Entity& other) :
Entity(other.m_mesh, other.m_material, other.m_id) { }

Entity(const Triangle* mesh, const Material* material, int id) :
m_mesh(mesh), m_material(material), m_id(id) { }

void fillMeshIntersection(float w0, float w1, float w2, Intersection* result) const;
BoundingBox createBoundingBox() const;
Expand Down
7 changes: 5 additions & 2 deletions source/entities/light-entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ float LightEntity::pdf(const Point3& surfacePoint, const SurfacePoint& lightPoin
return distance * distance / denom;
}

Spectrum LightEntity::sample(Sampler* sampler, const Point3& surfacePoint, SurfacePoint* sampledPoint,
Vector3* direction, float* pdf, float* lightDist) const {
Spectrum LightEntity::sample(
Sampler* sampler, const Point3& surfacePoint,
SurfacePoint* sampledPoint,
Vector3* direction, float* pdf, float* lightDist) const
{
m_mesh->samplePoint(sampler, sampledPoint);
sampledPoint->meshID = m_id;
*pdf = 0.0f;
Expand Down
6 changes: 4 additions & 2 deletions source/entities/light-entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class LightEntity : public Entity {

Spectrum emission(const Point3& surfacePoint, const SurfacePoint& lightPoint) const;
float pdf(const Point3& surfacePoint, const SurfacePoint& lightPoint) const;
Spectrum sample(Sampler* sampler, const Point3& surfacePoint, SurfacePoint* sampledPoint,
Vector3* direction, float* pdf, float* lightDist) const;

Spectrum sample(Sampler* sampler, const Point3& surfacePoint,
SurfacePoint* sampledPoint, Vector3* direction,
float* pdf, float* lightDist) const;

inline const LightEntity* getLight() const { return this; }
};
Expand Down
1 change: 1 addition & 0 deletions source/film/film.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ std::vector<FilmBounds> Film::splitToTiles(unsigned int tileSize) const {
}
}

// Remove boolean?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How?

bool shouldPad = false;

if (filmResW % tileWidth != 0) {
Expand Down
4 changes: 3 additions & 1 deletion source/filters/gaussian-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <cmath>

GaussianFilter::GaussianFilter() : Filter(), m_alpha(1.0f) { }
GaussianFilter::GaussianFilter(const GaussianFilter& other) : Filter(other), m_alpha(other.m_alpha) { }
GaussianFilter::GaussianFilter(const GaussianFilter& other) :
Filter(other), m_alpha(other.m_alpha) { }

GaussianFilter::GaussianFilter(float radius) : Filter(radius), m_alpha(0.5f / radius) { }

float GaussianFilter::evaluate1D(float centeredSamplePoint) const {
Expand Down
3 changes: 3 additions & 0 deletions source/images/checkerboard-image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ CheckerboardImage::CheckerboardImage(int tiles) : Image(tiles * PIXELS_PER_TILE,
bool black = true;
int x = 0;
int y = 0;
// Break this loop in two, avoid flag (But optimization issues I assume)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How?

for (int i = 0; i < getHeight(); ++i) {
for (int j = 0; j < getWidth(); ++j) {
// Return color from a procedure?
if (black) {
setPixel(i, j, RGBColor(0.05f, 0.05f, 0.05f));
}
else {
setPixel(i, j, RGBColor(0.95f, 0.95f, 0.95f));
}

x++;
if (x == PIXELS_PER_TILE) {
black = !black;
Expand Down
Loading