RT Multisampling

This commit is contained in:
IlyaShurupov 2023-10-19 19:11:29 +03:00 committed by Ilya Shurupov
parent 97a99f84f5
commit e9daedd6ea
7 changed files with 120 additions and 37 deletions

View file

@ -78,6 +78,17 @@ int readRenderSettings(lua_State* L, tp::RayTracer::RenderSettings& settings) {
} }
lua_pop(L, 1); // Pop the 'spray' field lua_pop(L, 1); // Pop the 'spray' field
// Read depth field
lua_getfield(L, -1, "multisampling");
if (lua_isnumber(L, -1)) {
settings.multisampling = (int) lua_tonumber(L, -1);
} else {
printf("RenderSettings 'depth' field is missing or not a number.\n");
lua_pop(L, 1); // Pop the 'depth' field
return 0; // Error
}
lua_pop(L, 1); // Pop the 'depth' field
return 1; // Success return 1; // Success
} }

View file

@ -9,7 +9,17 @@ Camera = {
Lights = { Lights = {
{ {
pos = { 4, 2, 3 }, pos = { -0.5, 3.5, 1 },
intensity = 5 intensity = 1
} },
{
pos = { 0, 0, 1 },
intensity = 0.5
},
}
RenderSettings = {
depth = 1,
spray = 10,
multisampling = 1,
} }

View file

@ -6,3 +6,21 @@ Camera = {
size_x = 400, size_x = 400,
size_y = 600, size_y = 600,
} }
Lights = {
{
pos = { -0.5, 3.5, 1 },
intensity = 1
},
{
pos = { 0, 0, 1 },
intensity = 0.5
},
}
RenderSettings = {
depth = 1,
spray = 10,
multisampling = 1,
}

View file

@ -20,5 +20,6 @@ Lights = {
RenderSettings = { RenderSettings = {
depth = 1, depth = 1,
spray = 10, spray = 3,
multisampling = 16,
} }

View file

@ -156,11 +156,28 @@ void RayTracer::render(const Scene& scene, OutputBuffers& out, const RenderSetti
ualni currIter = 0; ualni currIter = 0;
halnf maxDepth = 0; halnf maxDepth = 0;
halnf minDepth = mScene->mCamera.getFar(); halnf minDepth = mScene->mCamera.getFar() * mSettings.multisampling;
auto accumulateColor = [](RGBA& col, const RGBA& in) {
col.r += in.r;
col.g += in.g;
col.b += in.b;
col.a = 1;
};
auto divideColor = [](RGBA& col, const halnf num) {
col.r /= num;
col.g /= num;
col.b /= num;
};
for (auto i = 0; i < mSettings.size.x; i++) { for (auto i = 0; i < mSettings.size.x; i++) {
for (auto j = 0; j < mSettings.size.y; j++) { for (auto j = 0; j < mSettings.size.y; j++) {
iterPoint = planeLeftTop + ((deltaX * (halnf) i) + (deltaY * (halnf) j)); for (auto sample = 0; sample < mSettings.multisampling; sample++) {
auto randX = randomFloat();
auto randY = randomFloat();
iterPoint = planeLeftTop + ((deltaX * (halnf) (i + randX)) + (deltaY * (halnf) (j + randY)));
ray.dir = (iterPoint - pos).unitV(); ray.dir = (iterPoint - pos).unitV();
castRay(ray, castData, mScene->mCamera.getFar()); castRay(ray, castData, mScene->mCamera.getFar());
@ -175,16 +192,9 @@ void RayTracer::render(const Scene& scene, OutputBuffers& out, const RenderSetti
lightData.intensity = clamp(lightData.intensity, 0.f, 1.f); lightData.intensity = clamp(lightData.intensity, 0.f, 1.f);
RGBA col = {lightData.intensity, lightData.intensity, lightData.intensity, 1.f}; RGBA col = {lightData.intensity, lightData.intensity, lightData.intensity, 1.f};
out.color.set({i, j}, col); accumulateColor(out.color.get({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}); accumulateColor(out.normals.get({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}); accumulateColor(out.depth.get({i, j}), {depth, depth, depth, 1.f});
if (maxDepth < depth) {
maxDepth = depth;
}
if (minDepth > depth) {
minDepth = depth;
}
} else { } else {
out.color.set({i, j}, 0.f); out.color.set({i, j}, 0.f);
@ -194,12 +204,32 @@ void RayTracer::render(const Scene& scene, OutputBuffers& out, const RenderSetti
// 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); // printf(" %f, %f, %f, %f, ", tmp.r, tmp.g, tmp.b, tmp.a);
}
mProgress.percentage = (halnf) currIter / (halnf) maxIterations; mProgress.percentage = (halnf) currIter / (halnf) maxIterations;
currIter++; currIter++;
} }
} }
for (auto i = 0; i < mSettings.size.x * mSettings.size.y; i++) {
divideColor(out.color.getBuff()[i], (halnf) mSettings.multisampling);
divideColor(out.normals.getBuff()[i], (halnf) mSettings.multisampling);
divideColor(out.depth.getBuff()[i], (halnf) mSettings.multisampling);
}
for (auto i = 0; i < mSettings.size.x * mSettings.size.y; i++) {
if (!out.depth.getBuff()[i].a) {
continue;
}
const auto depth = out.depth.getBuff()[i].r;
if (maxDepth < depth) {
maxDepth = depth;
}
if (minDepth > depth) {
minDepth = depth;
}
}
for (auto i = 0; i < mSettings.size.x * mSettings.size.y; i++) { for (auto i = 0; i < mSettings.size.x * mSettings.size.y; i++) {
auto& col = out.depth.getBuff()[i]; auto& col = out.depth.getBuff()[i];
if (col.a == 1.f) { if (col.a == 1.f) {

View file

@ -48,6 +48,7 @@ namespace tp {
struct RenderSettings { struct RenderSettings {
uhalni depth = 2; uhalni depth = 2;
uhalni spray = 1; uhalni spray = 1;
ualni multisampling = 1;
Vec2I size; Vec2I size;
}; };

24
TODO
View file

@ -9,14 +9,26 @@ Make two grammars Regular and Context-free
For each grammar make grammar rules parser, example sentence generation and sentence parsing For each grammar make grammar rules parser, example sentence generation and sentence parsing
Make automations itself a universal tool - NFA DFA conversions Make automations itself a universal tool - NFA DFA conversions
RayTracer: Math:
Core: FFT
Normals
Lua:
Camera Settings
Light Sources Settings (Point, Direction, Spot)
RayTracer:
Features:
Normals flag
Material, Normals per trig info
Quality:
Render eq
Materials
Lenses
Exposure
Efficiency:
Casting Accelerators
Important sampling
Threading
Parser: Parser:
CF Grammar: CF Grammar:
optimize generation of sentences optimize generation of sentences