Skip to content

Commit

Permalink
Refactored the scene definition
Browse files Browse the repository at this point in the history
I moved the scene definition out into its own file, and reduced the image resolution and other parameters to
decrease the runtime. I also added a new image to "The Evolution of Celerity".
  • Loading branch information
wermos committed Feb 26, 2024
1 parent e0e71eb commit a129c5e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 57 deletions.
7 changes: 7 additions & 0 deletions Celerity/include/scenes/scene.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "hittableList.hpp"

namespace Scenes {
HittableList randomScene();
}
63 changes: 6 additions & 57 deletions Celerity/src/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
#include <functional> // For std::ref and std::cref
#include <atomic> // For std:atomic<T>

// Object-related includes
#include "sphere.hpp"
#include "hittableList.hpp"

// Material-related includes
#include "lambertian.hpp"
#include "metal.hpp"
#include "dielectric.hpp"

// Camera includes
#include "camera.hpp"

// Scene includes
#include "scene.hpp"

// Renderer includes
#include "renderer.hpp"

Expand All @@ -26,63 +20,18 @@
// STB Image include
#include "stb_image_library.hpp"

HittableList randomScene() {
HittableList world;

auto groundMaterial = std::make_shared<Lambertian>(color(0.5, 0.5, 0.5));
world.add(std::make_shared<Sphere>(point3(0, -1000, 0), 1000, groundMaterial));

for (int a = -11; a < 11; a++) {
for (int b = -11; b < 11; b++) {
auto chooseMaterial = randomDouble();
point3 center(a + 0.9 * randomDouble(), 0.2, b + 0.9 * randomDouble());

if ((center - point3(4, 0.2, 0)).length() > 0.9) {
std::shared_ptr<Material> sphereMaterial;

if (chooseMaterial < 0.8) {
// diffuse material
auto albedo = color::random() * color::random();
sphereMaterial = std::make_shared<Lambertian>(albedo);
world.add(std::make_shared<Sphere>(center, 0.2, sphereMaterial));
} else if (chooseMaterial < 0.95) {
// metal material
auto albedo = color::random(0.5, 1);
auto fuzz = randomDouble(0, 0.5);
sphereMaterial = std::make_shared<Metal>(albedo, fuzz);
world.add(std::make_shared<Sphere>(center, 0.2, sphereMaterial));
} else {
// glass material
sphereMaterial = std::make_shared<Dielectric>(1.5);
world.add(std::make_shared<Sphere>(center, 0.2, sphereMaterial));
}
}
}
}

auto material1 = std::make_shared<Dielectric>(1.5);
world.add(std::make_shared<Sphere>(point3(0, 1, 0), 1.0, material1));

auto material2 = std::make_shared<Lambertian>(color(0.4, 0.2, 0.1));
world.add(std::make_shared<Sphere>(point3(-4, 1, 0), 1.0, material2));

auto material3 = std::make_shared<Metal>(color(0.7, 0.6, 0.5), 0.0);
world.add(std::make_shared<Sphere>(point3(4, 1, 0), 1.0, material3));

return world;
}

#include "float.hpp"

int main() {
// Image
constexpr Float aspectRatio = 16.0 / 9.0;
constexpr std::size_t imageWidth = 1920;
constexpr std::size_t imageWidth = 480;
constexpr std::size_t imageHeight = static_cast<std::size_t>(imageWidth / aspectRatio);
constexpr std::size_t samplesPerPixel = 500;
constexpr std::size_t maxRayDepth = 50;

// World
auto world = randomScene();
auto world = Scenes::randomScene();

// Camera
point3 lookFrom(13, 2, 3);
Expand Down
65 changes: 65 additions & 0 deletions Celerity/src/scenes/scene.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Object-related includes
#include "sphere.hpp"
#include "hittableList.hpp"

// Material-related includes
#include "lambertian.hpp"
#include "metal.hpp"
#include "dielectric.hpp"

#include "color.hpp"
#include "vec3.hpp"

#include "utility.hpp"

#include "scene.hpp"

#include <memory>

namespace Scenes {
HittableList randomScene() {
HittableList world;

auto groundMaterial = std::make_shared<Lambertian>(color(0.5, 0.5, 0.5));
world.add(std::make_shared<Sphere>(point3(0, -1000, 0), 1000, groundMaterial));

for (int a = -11; a < 11; a++) {
for (int b = -11; b < 11; b++) {
auto chooseMaterial = randomDouble();
point3 center(a + 0.9 * randomDouble(), 0.2, b + 0.9 * randomDouble());

if ((center - point3(4, 0.2, 0)).length() > 0.9) {
std::shared_ptr<Material> sphereMaterial;

if (chooseMaterial < 0.8) {
// diffuse material
auto albedo = color::random() * color::random();
sphereMaterial = std::make_shared<Lambertian>(albedo);
world.add(std::make_shared<Sphere>(center, 0.2, sphereMaterial));
} else if (chooseMaterial < 0.95) {
// metal material
auto albedo = color::random(0.5, 1);
auto fuzz = randomDouble(0, 0.5);
sphereMaterial = std::make_shared<Metal>(albedo, fuzz);
world.add(std::make_shared<Sphere>(center, 0.2, sphereMaterial));
} else {
// glass material
sphereMaterial = std::make_shared<Dielectric>(1.5);
world.add(std::make_shared<Sphere>(center, 0.2, sphereMaterial));
}
}
}
}

auto material1 = std::make_shared<Dielectric>(1.5);
world.add(std::make_shared<Sphere>(point3(0, 1, 0), 1.0, material1));

auto material2 = std::make_shared<Lambertian>(color(0.4, 0.2, 0.1));
world.add(std::make_shared<Sphere>(point3(-4, 1, 0), 1.0, material2));

auto material3 = std::make_shared<Metal>(color(0.7, 0.6, 0.5), 0.0);
world.add(std::make_shared<Sphere>(point3(4, 1, 0), 1.0, material3));

return world;
}
}
6 changes: 6 additions & 0 deletions The Evolution of Celerity/Explanations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ pic (22) Finally works! Runtime was ~32 mins. I used 500 samples, and the

pic (23) Made a full sized 1920 x 1080p image. Runtime was 75 mins. Same number of
samples as last time.

pic (24) Restarted work on the raytracer, inspired by the Image Synthesis assignment.
As can be seen, my raytracer has taken a battering from the cold, unforgiving
winds of time, but still works for the most part. For now, I'll focus on a few
of the code quality changes I had been meaning to do, before working on fixing
the bugs and adding new features.
Binary file added The Evolution of Celerity/pic (24).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a129c5e

Please sign in to comment.