aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-08-11 21:21:02 +0200
committerJulian T <julian@jtle.dk>2020-08-11 21:21:02 +0200
commit690b72664ca8d471f5c117f6ed87aeae2de0a208 (patch)
tree8bc30efc009462d4390ac6eb8fe28ccbdbbd88a1
parent3b8893902ac5f529faf15accaa3fb5360771d3b3 (diff)
Defuse coloring
-rw-r--r--app/main.cpp21
-rw-r--r--app/rendercoord.cpp2
-rw-r--r--src/object.cpp19
-rw-r--r--src/object.hpp30
-rw-r--r--src/render.cpp9
-rw-r--r--src/render.hpp14
6 files changed, 59 insertions, 36 deletions
diff --git a/app/main.cpp b/app/main.cpp
index 64e413a..e174d4d 100644
--- a/app/main.cpp
+++ b/app/main.cpp
@@ -14,16 +14,21 @@ int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Scene scn;
- scn.addShape(new Sphere(Vec3d(2, 6, -1), 1));
- scn.addShape(new Sphere(Vec3d(0, 4, -1), 1.3));
- scn.addShape(new Sphere(Vec3d(-2, 5, -2), 1.3));
+
+ Material blue(Color(0.3, 0.3, 1), 1);
+ Material red(Color(1, 0.3, 0.3), 1);
+ Material white(Color(1, 1, 1), 1);
+
+ scn.addShape(new Sphere(blue, Vec3d(2, 6, -1), 1));
+ scn.addShape(new Sphere(white, Vec3d(0, 4, -1), 1.3));
+ scn.addShape(new Sphere(white, Vec3d(-2, 5, -2), 1.3));
//scn.addShape(new Sphere(Vec3d(0, 7, 0), 0.5));
- scn.addShape(new Plane(Vec3d(0, 0, 0), Vec3d(0, 1, 0)));
- scn.addShape(new Plane(Vec3d(0, 10, 0), Vec3d(0, 1, 0)));
- scn.addShape(new Plane(Vec3d(0, 0, -5), Vec3d(0, 0, 1)));
- scn.addShape(new Plane(Vec3d(-5, 0, 0), Vec3d(1, 0, 0)));
- scn.addShape(new Plane(Vec3d(5, 0, 0), Vec3d(1, 0, 0)));
+ scn.addShape(new Plane(white, Vec3d(0, 0, 0), Vec3d(0, 1, 0)));
+ scn.addShape(new Plane(white, Vec3d(0, 10, 0), Vec3d(0, 1, 0)));
+ scn.addShape(new Plane(white, Vec3d(0, 0, -5), Vec3d(0, 0, 1)));
+ scn.addShape(new Plane(red, Vec3d(-5, 0, 0), Vec3d(1, 0, 0)));
+ scn.addShape(new Plane(blue, Vec3d(5, 0, 0), Vec3d(1, 0, 0)));
Renderer render(scn, Vec3d(0, 5, 4), Vec3d(0, 5, 0), 500, 500);
diff --git a/app/rendercoord.cpp b/app/rendercoord.cpp
index 8db7c97..f9fc9f8 100644
--- a/app/rendercoord.cpp
+++ b/app/rendercoord.cpp
@@ -30,7 +30,7 @@ void RenderThread::run() {
for (unsigned x = 0; x < m_render.m_width; x++) {
for (unsigned y = 0; y < m_render.m_height; y++) {
- auto c = m_render.render(x, y);
+ auto c = m_render.render(m_render.m_width - x, m_render.m_height - y);
m_writebuffer[x + y * m_render.m_height] =
static_cast<QRgb>(colorToUint32(c));
}
diff --git a/src/object.cpp b/src/object.cpp
index 179785a..6c61d07 100644
--- a/src/object.cpp
+++ b/src/object.cpp
@@ -4,12 +4,27 @@
#include <iostream>
#include "common.hpp"
-Sphere::Sphere(Vec3d center, double radius) {
+void Color::clamp() {
+ if (m_x > 1) { m_x = 1; }
+ if (m_y > 1) { m_y = 1; }
+ if (m_z > 1) { m_z = 1; }
+}
+
+Material::Material(Color color, double defuse) {
+ m_color = color;
+ m_defuse = defuse;
+}
+
+Color Material::reflect(const Vec3d &normal, const Vec3d &in, const Vec3d &out) const {
+ return Vec3d(m_color) * (out.dot(normal) * m_defuse);
+}
+
+Sphere::Sphere(const Material &mat, Vec3d center, double radius) : Shape(mat) {
m_center = center;
m_radius = radius;
}
-Plane::Plane(Vec3d start, Vec3d norm) {
+Plane::Plane(const Material &mat, Vec3d start, Vec3d norm) : Shape(mat) {
m_start = start;
m_norm = norm;
diff --git a/src/object.hpp b/src/object.hpp
index ac5122f..2957acb 100644
--- a/src/object.hpp
+++ b/src/object.hpp
@@ -5,20 +5,44 @@
#include "vector.hpp"
#include "ray.hpp"
+class Color : public Vec3d {
+ public:
+ Color() {}
+ Color(const Vec3d &v) : Vec3d(v) {}
+ Color(double r, double g, double b) : Vec3d(r, g, b) {}
+
+ uint8_t r() { return m_x * 255; }
+ uint8_t g() { return m_y * 255; }
+ uint8_t b() { return m_z * 255; }
+
+ void clamp();
+
+};
+
+// Implements phong BRDF
class Material {
+ public:
+ Material(Color color, double defuse);
+
+ Color reflect(const Vec3d &normal, const Vec3d &in, const Vec3d &out) const;
+ private:
+ Color m_color;
+ double m_defuse;
};
class Shape {
public:
- void setMaterial(std::shared_ptr<Material> m);
+ Shape(const Material &mat) : m_mat(mat) { }
virtual Vec3d norm_at(const Vec3d &point, const Vec3d &indir) const = 0;
virtual double intersect(const Ray &ray, bool skip_dist) const = 0;
+
+ const Material &m_mat;
};
class Sphere : public Shape {
public:
- Sphere(Vec3d center, double radius);
+ Sphere(const Material &mat, Vec3d center, double radius);
Vec3d norm_at(const Vec3d &point, const Vec3d &indir) const;
double intersect(const Ray &ray, bool skip_dist) const;
@@ -29,7 +53,7 @@ class Sphere : public Shape {
class Plane : public Shape {
public:
- Plane(Vec3d start, Vec3d norm);
+ Plane(const Material &mat, Vec3d start, Vec3d norm);
Vec3d norm_at(const Vec3d &point, const Vec3d &indir) const;
double intersect(const Ray &ray, bool skip_dist) const;
diff --git a/src/render.cpp b/src/render.cpp
index 5601863..197226e 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -9,12 +9,6 @@
const Vec3d up = Vec3d(0, 1, 0);
-void Color::clamp() {
- if (m_x > 1) { m_x = 1; }
- if (m_y > 1) { m_y = 1; }
- if (m_z > 1) { m_z = 1; }
-}
-
Renderer::Renderer(const Scene &scn, Vec3d eye, Vec3d target, unsigned width, unsigned height) :
m_scn(scn)
{
@@ -84,8 +78,7 @@ Color Renderer::pathtrace_sample(const Ray &r, unsigned hop) {
return Color(0, 0, 0);
}
- auto green = Color(0, 1, 0);
- return Color(green * norm.dot(tolight));
+ return res->m_mat.reflect(norm, r.m_direction, tolight);
}
const Shape* Renderer::cast_ray(const Ray &r, double chk_dist, double *dist_ret) {
diff --git a/src/render.hpp b/src/render.hpp
index 3fd84b1..bb4e9c5 100644
--- a/src/render.hpp
+++ b/src/render.hpp
@@ -5,20 +5,6 @@
#include "ray.hpp"
#include "scene.hpp"
-class Color : public Vec3d {
- public:
- Color() {}
- Color(const Vec3d &v) : Vec3d(v) {}
- Color(double r, double g, double b) : Vec3d(r, g, b) {}
-
- uint8_t r() { return m_x * 255; }
- uint8_t g() { return m_y * 255; }
- uint8_t b() { return m_z * 255; }
-
- void clamp();
-
-};
-
class Renderer {
public:
Renderer(const Scene &scn, Vec3d eye, Vec3d target, unsigned width, unsigned height);