From 3396e69b472f446315f61a4aaf969bc5535b77d9 Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Wed, 18 Oct 2023 19:56:49 +0300 Subject: [PATCH] Save multiple buffers from RayTracer --- RayTracer/applications/Rayt.cpp | 12 ++++---- RayTracer/private/RayTracer.cpp | 50 ++++++++++++++++++++++++++------- RayTracer/public/RayTracer.hpp | 12 ++++++-- TODO | 4 +++ 4 files changed, 59 insertions(+), 19 deletions(-) diff --git a/RayTracer/applications/Rayt.cpp b/RayTracer/applications/Rayt.cpp index 68313a6..96dab57 100644 --- a/RayTracer/applications/Rayt.cpp +++ b/RayTracer/applications/Rayt.cpp @@ -11,7 +11,7 @@ using namespace tp; -void writeImage(const RayTracer::RenderBuffer& output) { +void writeImage(const RayTracer::RenderBuffer& output, const char* name) { // Save the data to a PNG file struct urgb { uint1 r, g, b, a; @@ -29,7 +29,7 @@ void writeImage(const RayTracer::RenderBuffer& output) { } } - if (stbi_write_png("output.png", converted.size().x, converted.size().y, 4, converted.getBuff(), converted.size().x * 4) != 0) { + if (stbi_write_png(name, converted.size().x, converted.size().y, 4, converted.getBuff(), converted.size().x * 4) != 0) { // Image saved successfully printf("Image saved successfully.\n"); } else { @@ -57,9 +57,7 @@ void renderCommand(const String& scenePath) { loadScene(scene, scenePath, settings); - RayTracer::RenderBuffer output; - output.reserve(RayTracer::RenderBuffer::Index2D(settings.size.x, settings.size.y)); - + RayTracer::OutputBuffers output; RayTracer rayt; pthread_t my_thread; @@ -83,7 +81,9 @@ void renderCommand(const String& scenePath) { printf("\nRender finished with average render time per sample - %i (ms)\n", end - start); - writeImage(output); + writeImage(output.normals, "normals.png"); + writeImage(output.color, "color.png"); + writeImage(output.depth, "depth.png"); } int main(int argc, const char** argv) { diff --git a/RayTracer/private/RayTracer.cpp b/RayTracer/private/RayTracer.cpp index f97f069..de01eb1 100644 --- a/RayTracer/private/RayTracer.cpp +++ b/RayTracer/private/RayTracer.cpp @@ -121,9 +121,12 @@ void RayTracer::cycle(const RayCastData& castData, LightData& out, uhalni depth) } } -void RayTracer::render(const Scene& scene, RayTracer::RenderBuffer& buff, const RenderSettings& settings) { +void RayTracer::render(const Scene& scene, OutputBuffers& out, const RenderSettings& settings) { + out.color.reserve({settings.size.x, settings.size.y}); + out.normals.reserve({settings.size.x, settings.size.y}); + out.depth.reserve({settings.size.x, settings.size.y}); + mScene = &scene; - mBuff = &buff; mSettings = settings; auto pos = mScene->mCamera.getPos(); @@ -146,14 +149,17 @@ void RayTracer::render(const Scene& scene, RayTracer::RenderBuffer& buff, const }; Vec3F iterPoint = {0, 0, 0}; - Vec3F deltaX = right * halnf(width / (alnf) buff.size().x); - Vec3F deltaY = up * halnf(-height / (alnf) buff.size().y); + Vec3F deltaX = right * halnf(width / (alnf) mSettings.size.x); + Vec3F deltaY = up * halnf(-height / (alnf) mSettings.size.y); - ualni maxIterations = buff.size().x * buff.size().y; + ualni maxIterations = mSettings.size.x * mSettings.size.y; ualni currIter = 0; - for (RayTracer::RenderBuffer::Index i = 0; i < buff.size().x; i++) { - for (RayTracer::RenderBuffer::Index j = 0; j < buff.size().y; j++) { + halnf maxDepth = 0; + halnf minDepth = mScene->mCamera.getFar(); + + for (auto i = 0; i < mSettings.size.x; i++) { + for (auto j = 0; j < mSettings.size.y; j++) { iterPoint = planeLeftTop + ((deltaX * (halnf) i) + (deltaY * (halnf) j)); ray.dir = (iterPoint - pos).unitV(); @@ -163,19 +169,43 @@ void RayTracer::render(const Scene& scene, RayTracer::RenderBuffer& buff, const LightData lightData; cycle(castData, lightData, mSettings.depth); + const auto normal = castData.trig->getNormal(); + const auto depth = (halnf) (castData.hitPos - ray.pos).length(); + lightData.intensity = clamp(lightData.intensity, 0.f, 1.f); RGBA col = {lightData.intensity, lightData.intensity, lightData.intensity, 1.f}; - buff.set({i, j}, col); + out.color.set({i, j}, col); + out.normals.set({i, j}, {normal.x * 0.5f + 0.5f, normal.y * 0.5f + 0.5f, normal.z * 0.5f + 0.5f, 1.f}); + out.depth.set({i, j}, {depth, depth, depth, 1.f}); + + if (maxDepth < depth) { + maxDepth = depth; + } + if (minDepth > depth) { + minDepth = depth; + } + } else { - buff.set({i, j}, 0.f); + out.color.set({i, j}, 0.f); + out.normals.set({i, j}, 0.f); + out.depth.set({i, j}, 0.f); } - auto tmp = buff.get({i, j}); + // auto tmp = buff.get({i, j}); // printf(" %f, %f, %f, %f, ", tmp.r, tmp.g, tmp.b, tmp.a); mProgress.percentage = (halnf) currIter / (halnf) maxIterations; currIter++; } } + + for (auto i = 0; i < mSettings.size.x * mSettings.size.y; i++) { + auto& col = out.depth.getBuff()[i]; + if (col.a == 1.f) { + col.r = (col.r - minDepth) / (maxDepth - minDepth); + col.g = col.r; + col.b = col.r; + } + } } diff --git a/RayTracer/public/RayTracer.hpp b/RayTracer/public/RayTracer.hpp index 998e386..9506f14 100644 --- a/RayTracer/public/RayTracer.hpp +++ b/RayTracer/public/RayTracer.hpp @@ -44,16 +44,23 @@ namespace tp { struct Progress { halnf percentage = 0.f; } mProgress; - + struct RenderSettings { uhalni depth = 2; uhalni spray = 1; Vec2I size; }; + struct OutputBuffers { + RenderBuffer normals; + RenderBuffer color; + RenderBuffer depth; + // albedo, reflectance ... + }; + public: RayTracer() = default; - void render(const Scene& scene, RenderBuffer& buff, const RenderSettings& settings); + void render(const Scene& scene, OutputBuffers& out, const RenderSettings& settings); private: struct RayCastData { @@ -74,6 +81,5 @@ namespace tp { private: RenderSettings mSettings; const Scene* mScene = nullptr; - RenderBuffer* mBuff = nullptr; }; } diff --git a/TODO b/TODO index e9cd1c3..073e706 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,9 @@ Testing !! +Change clang format and apply to all files!!! +rename Language module to FormalLanguage +make more general structure for feature renders + Merge parser and tokenizer module into grammar module Make two grammars Regular and Context-free For each grammar make grammar rules parser, example sentence generation and sentence parsing