From 63a0ec8f28eff476646d4abca555fc60706a4c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?arvid=20schr=C3=B6der?= Date: Mon, 12 Dec 2022 14:56:14 +0100 Subject: [PATCH] Merge issue cleanup --- renderer/kdtreerenderer.cpp | 22 +-------- renderer/simplerenderer.cpp | 98 ++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 71 deletions(-) diff --git a/renderer/kdtreerenderer.cpp b/renderer/kdtreerenderer.cpp index dd81097..f52afad 100644 --- a/renderer/kdtreerenderer.cpp +++ b/renderer/kdtreerenderer.cpp @@ -38,25 +38,5 @@ Texture KDTreeRenderer::renderKDTree(FastScene const &scene, Camera const &camer image.setPixelAt(x, y, (1.0f - factor) * g + factor * r); } } - - int max = 0; - float const aspectRatio = static_cast(height) / static_cast(width); - for (int x = 0; x < image.width(); ++x) { - std::vector ints; - ints.reserve(height); - for (int y = 0; y < image.height(); ++y) { - Ray ray = camera.createRay((static_cast(x) / image.width() * 2.0f - 1), - -(static_cast(y) / image.height() * 2 - 1) * aspectRatio); - ints.push_back(scene.countNodeIntersections(ray)); - max = std::max(ints[y], max); - } - intersections.push_back(std::move(ints)); - } - for (int x = 0; x < image.width(); ++x) { - for (int y = 0; y < image.height(); ++y) { - image.setPixelAt(x, y, clamped(static_cast(max - intersections[x][y]) / max * GREEN + - static_cast(intersections[x][y]) / max * RED)); - } - } - return image; + return image; } \ No newline at end of file diff --git a/renderer/simplerenderer.cpp b/renderer/simplerenderer.cpp index 90f719c..feb2495 100644 --- a/renderer/simplerenderer.cpp +++ b/renderer/simplerenderer.cpp @@ -5,76 +5,74 @@ #include #include #include -#include void SimpleRenderer::renderThread(const Scene *scene, Camera const *camera, Texture *image, int width, int widthStep, int widthOffset, int height, int heightStep, int heightOffset, std::atomic *k, int const stepSize) { - float const aspectRatio = static_cast(height) / width; - for (int y = heightOffset; y < height; y += heightStep) { - for (int x = widthOffset; x < width; x += widthStep) { - Ray ray = camera->createRay((static_cast(x) / width * 2 - 1), -(static_cast(y) / height * 2 - 1) * aspectRatio); - image->setPixelAt(x, y, clamped(scene->traceRay(ray))); + float const aspectRatio = static_cast(height) / width; + for (int y = heightOffset; y < height; y += heightStep) { + for (int x = widthOffset; x < width; x += widthStep) { + Ray ray = camera->createRay((static_cast(x) / width * 2 - 1), -(static_cast(y) / height * 2 - 1) * aspectRatio); + image->setPixelAt(x, y, clamped(scene->traceRay(ray))); - // Super hacky progress bar! - if (++*k % stepSize == 0) { - std::cout << "=" << std::flush; - } + // Super hacky progress bar! + if (++*k % stepSize == 0) { + std::cout << "=" << std::flush; + } + } } - } } Texture SimpleRenderer::renderImage(Scene const &scene, Camera const &camera, int width, int height) { - Texture image(width, height); + Texture image(width, height); - // Setup timer - std::chrono::steady_clock::time_point start, stop; + // Setup timer + std::chrono::steady_clock::time_point start, stop; - // Reset Ray counting - Ray::resetRayCount(); + // Reset Ray counting + Ray::resetRayCount(); - // Super-hacky progress bar! - std::cout << "(SimpleRenderer): Begin rendering..." << std::endl; - std::cout << "| 0%"; - int const barSizeGoal = 100; - int const stepSize = width / barSizeGoal; - for (int i = 0; i < width / stepSize - 3 - 5; ++i) - std::cout << " "; - std::cout << "100% |" << std::endl << "|"; - std::atomic k(0); + // Super-hacky progress bar! + std::cout << "(SimpleRenderer): Begin rendering..." << std::endl; + std::cout << "| 0%"; + int const barSize = 50; + int const stepSize = (width * height) / barSize; + for (int i = 0; i < barSize - 3 - 5; ++i) + std::cout << " "; + std::cout << "100% |" << std::endl << "|"; + std::atomic k(0); - // Start timer - start = std::chrono::steady_clock::now(); + /* Start timer */ start = std::chrono::steady_clock::now(); - // Spawn a thread for every logical processor -1, calling the renderThread function - int const nThreads = std::thread::hardware_concurrency(); - std::vector threads; - for (int t = 0; t < nThreads - 1; ++t) { - threads.emplace_back(renderThread, &scene, &camera, &image, width, nThreads, t, height, 1, 0, &k, stepSize); - } + // Spawn a thread for every logical processor -1, calling the renderThread function + int const nThreads = std::thread::hardware_concurrency(); + std::vector threads; + for (int t = 0; t < nThreads - 1; ++t) { + threads.emplace_back(renderThread, &scene, &camera, &image, width, nThreads, t, height, 1, 0, &k, stepSize); + } - // Call the renderThread function yourself - renderThread(&scene, &camera, &image, width, nThreads, nThreads - 1, height, 1, 0, &k, stepSize); + // Call the renderThread function yourself + renderThread(&scene, &camera, &image, width, nThreads, nThreads - 1, height, 1, 0, &k, stepSize); - // Rejoin the threads - for (int t = 0; t < nThreads - 1; ++t) { - threads[t].join(); - } + // Rejoin the threads + for (int t = 0; t < nThreads - 1; ++t) { + threads[t].join(); + } - // Stop timer - stop = std::chrono::steady_clock::now(); + // Stop timer + stop = std::chrono::steady_clock::now(); - std::cout << "| Done!" << std::endl; + std::cout << "| Done!" << std::endl; - // Calculate the Time taken in seconds - double seconds = std::chrono::duration_cast>(stop - start).count(); + // Calculate the Time taken in seconds + double seconds = std::chrono::duration_cast>(stop - start).count(); - std::cout << "Time: " << seconds << "s" << std::endl; + std::cout << "Time: " << seconds << "s" << std::endl; - // Get the number of seconds per ray - int rays = Ray::getRayCount(); + // Get the number of seconds per ray + int rays = Ray::getRayCount(); - std::cout << "Paths: " << rays << std::endl; - std::cout << "Paths per second: " << std::fixed << std::setprecision(0) << rays / seconds << std::endl; + std::cout << "Paths: " << rays << std::endl; + std::cout << "Paths per second: " << std::fixed << std::setprecision(0) << rays / seconds << std::endl; - return image; + return image; }