Save multiple buffers from RayTracer

This commit is contained in:
IlyaShurupov 2023-10-18 19:56:49 +03:00 committed by Ilya Shurupov
parent 15c5576273
commit 97a99f84f5
4 changed files with 59 additions and 19 deletions

View file

@ -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) {

View file

@ -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);
} else {
buff.set({i, j}, 0.f);
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;
}
auto tmp = buff.get({i, j});
} else {
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});
// 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;
}
}
}

View file

@ -51,9 +51,16 @@ namespace tp {
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;
};
}

4
TODO
View file

@ -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