From 5b0b916c561f602723b9ae80f5462a7939b652a1 Mon Sep 17 00:00:00 2001 From: Julian T Date: Thu, 13 Aug 2020 20:06:29 +0200 Subject: Pathtracing working with defuse and emissive lighting --- app/draw.cpp | 11 ++++++++++- app/draw.hpp | 6 ++++++ app/main.cpp | 9 +++++---- app/rendercoord.cpp | 30 +++++++++++++++++++----------- 4 files changed, 40 insertions(+), 16 deletions(-) (limited to 'app') diff --git a/app/draw.cpp b/app/draw.cpp index 2f3a947..469b469 100644 --- a/app/draw.cpp +++ b/app/draw.cpp @@ -3,15 +3,20 @@ #include #include #include +#include #include #include -DrawWidget::DrawWidget(unsigned width, unsigned height) : QWidget() { +DrawWidget::DrawWidget(unsigned width, unsigned height) : QWidget(), m_timer(this) { m_width = width; m_height = height; m_drawbuffer = new QRgb[width * height]; m_img = QImage((uchar*)m_drawbuffer, width, height, QImage::Format_ARGB32); + + QObject::connect(&m_timer, &QTimer::timeout, this, &DrawWidget::redraw); + + m_timer.start(500); } void DrawWidget::paintEvent(QPaintEvent*) { @@ -19,6 +24,10 @@ void DrawWidget::paintEvent(QPaintEvent*) { painter.drawImage(0, 0, m_img); } +void DrawWidget::redraw() { + repaint(); +} + DrawWidget::~DrawWidget() { delete[] m_drawbuffer; } diff --git a/app/draw.hpp b/app/draw.hpp index 41d5d9c..f8e93a4 100644 --- a/app/draw.hpp +++ b/app/draw.hpp @@ -2,6 +2,7 @@ #define DRAW_H #include +#include #include class DrawWidget : public QWidget { @@ -15,9 +16,14 @@ class DrawWidget : public QWidget { unsigned m_width, m_height; ~DrawWidget(); + private slots: + void redraw(); + private: QImage m_img; unsigned char i; + + QTimer m_timer; }; #endif diff --git a/app/main.cpp b/app/main.cpp index e174d4d..48e26fe 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -18,19 +18,20 @@ int main(int argc, char *argv[]) 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); + Material em(Color(1, 1, 1), 0, 2); - scn.addShape(new Sphere(blue, Vec3d(2, 6, -1), 1)); + scn.addShape(new Sphere(red, 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 Sphere(blue, Vec3d(0, 7, 0), 0.5)); 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(em, 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); + Renderer render(scn, Vec3d(0, 5, 4), Vec3d(0, 5, 0), 500, 500, 5); MainWindow main(render); main.show(); diff --git a/app/rendercoord.cpp b/app/rendercoord.cpp index f9fc9f8..9c59acd 100644 --- a/app/rendercoord.cpp +++ b/app/rendercoord.cpp @@ -5,12 +5,13 @@ #include -uint32_t colorToUint32(Color &c) { - c.clamp(); +uint32_t colorToUint32(const Color &c) { + Color cnew = Color(c); + cnew.clamp(); return (0xFF << 24) + - (c.r() << 16) + - (c.g() << 8) + - c.b(); + (cnew.r() << 16) + + (cnew.g() << 8) + + cnew.b(); } // Run by main thread @@ -27,12 +28,19 @@ void RenderThread::run() { while (1) { // Wait for work m_work.acquire(); + m_render.m_sampler.seed(100); - 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(m_render.m_width - x, m_render.m_height - y); - m_writebuffer[x + y * m_render.m_height] = - static_cast(colorToUint32(c)); + // Very expensive, but necesary to get live rendering + Color *sum = new Color[m_render.m_width * m_render.m_height]; + + for (unsigned sample = 1; sample < m_samples+1; sample++) { + for (unsigned x = 0; x < m_render.m_width; x++) { + for (unsigned y = 0; y < m_render.m_height; y++) { + auto index = x + y * m_render.m_height; + sum[index] += m_render.render(m_render.m_width - x, m_render.m_height - y, 1); + + m_writebuffer[index] = colorToUint32(sum[index] / sample); + } } } @@ -64,7 +72,7 @@ RenderCoordinator::RenderCoordinator(QObject *parent, DrawWidget &target, Render QObject::connect(&m_worker, &RenderThread::done, this, &RenderCoordinator::workerDone); - m_worker.render(target.m_drawbuffer, 1); + m_worker.render(target.m_drawbuffer, 100); } -- cgit v1.2.3