aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-08-13 21:17:30 +0200
committerJulian T <julian@jtle.dk>2020-08-13 21:17:30 +0200
commit65d26692ae8736f67f3951f088b26086eeb6b829 (patch)
tree7043254e116f2fab7690b5565d114a6afac3cd9b /src
parent5b0b916c561f602723b9ae80f5462a7939b652a1 (diff)
Added nicer ui which can save
Diffstat (limited to 'src')
-rw-r--r--src/render.cpp17
-rw-r--r--src/render.hpp17
2 files changed, 21 insertions, 13 deletions
diff --git a/src/render.cpp b/src/render.cpp
index 0bbe5cb..c278797 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -13,28 +13,26 @@
const Vec3d up = Vec3d(0, 1, 0);
-Sampler::Sampler() {
- m_seed = 0;
-}
-
-void Sampler::seed(unsigned seed) {
+void Random::seed(unsigned seed) {
for (unsigned i = 0; i < seed; i++) {
rand_r(&m_seed);
}
}
-double Sampler::random() {
+double Random::operator()() {
return (double)rand_r(&m_seed) / (double)RAND_MAX;
}
+Sampler::Sampler(Random &src) : m_src(src) { }
+
Vec3d Sampler::sample(const Vec3d &norm) {
/*
auto theta = asin(pow(1 - random(), (double)1 / (1 + SAMPLING_POWER)));
auto phi = 2 * M_PI * random();
*/
- auto theta = 2.0 * M_PI * random();
- auto phi = acos(2.0 * random() - 1.0);
+ auto theta = 2.0 * M_PI * m_src();
+ auto phi = acos(2.0 * m_src() - 1.0);
auto sinphi = sin(phi);
@@ -48,6 +46,7 @@ Vec3d Sampler::sample(const Vec3d &norm) {
}
Renderer::Renderer(const Scene &scn, Vec3d eye, Vec3d target, unsigned width, unsigned height, unsigned maxhops) :
+ m_sampler(m_random),
m_scn(scn)
{
m_eye = eye;
@@ -88,11 +87,11 @@ Ray Renderer::findray(double x, double y) const {
}
Color Renderer::render(unsigned x, unsigned y, unsigned samples) {
- auto r = findray(x, y);
Color sum(0, 0, 0);
for (unsigned i = 0; i < samples; i++) {
+ auto r = findray(x + m_random(), y + m_random());
sum += pathtrace_sample(r, 0);
}
diff --git a/src/render.hpp b/src/render.hpp
index 1274bb5..7557fce 100644
--- a/src/render.hpp
+++ b/src/render.hpp
@@ -5,18 +5,25 @@
#include "ray.hpp"
#include "scene.hpp"
+class Random {
+ public:
+ void seed(unsigned seed);
+ double operator()();
+
+ private:
+ unsigned m_seed;
+};
+
// Samples a random direction in a hemisphere, cosine weighed
// https://blog.thomaspoulet.fr/uniform-sampling-on-unit-hemisphere/
class Sampler {
public:
- Sampler();
- void seed(unsigned seed);
+ Sampler(Random &src);
Vec3d sample(const Vec3d &norm);
private:
- double random();
- unsigned m_seed;
+ Random &m_src;
};
class Renderer {
@@ -40,6 +47,8 @@ class Renderer {
const Scene &m_scn;
+ Random m_random;
+
// User options
Vec3d m_eye, m_target;
unsigned m_maxhops;