diff --git a/renderer.cc b/renderer.cc index fa8f538..fdb683a 100644 --- a/renderer.cc +++ b/renderer.cc @@ -311,6 +311,33 @@ int main() { 3/16.f,11/16.f, 2/16.f,10/16.f }, { 15/16.f, 7/16.f,14/16.f, 6/16.f } }; + auto Regular = [](auto&& plot) + { + return [&](auto& poly, unsigned x,unsigned y,auto z, float u,float v, float lu,float lv, auto&&... args) + { + return plot(poly, x,y,z, + [=](auto&& fetch) { return fetch(u,v); }, + [=](auto&& fetch) { return fetch(lu,lv); }, + args...); + }; + }; + auto Filtered = [&](auto&& plot) + { + return [&](auto& poly, unsigned x,unsigned y,auto z, float u,float v, float lu,float lv, auto&&... args) + { + auto bilinear_filter = [&](float x,float y, auto&& fetch) + { + float xif = std::trunc(x), xf = x-xif; unsigned xi = xif; + float yif = std::trunc(y), yf = y-yif; unsigned yi = yif; + return mix(yf, mix(xf, fetch(xi,yi ), fetch(xi+1,yi )), + mix(xf, fetch(xi,yi+1), fetch(xi+1,yi+1))); + }; + return plot(poly, x,y,z, + [=](auto&& fetch) { return bilinear_filter(u,v, fetch); }, + [=](auto&& fetch) { return bilinear_filter(lu,lv, fetch); }, + args...); + }; + }; auto Dithered = [](auto&& plot) { return [&](auto& poly, unsigned x,unsigned y,auto z, float u,float v, float lu,float lv, auto&&... args) @@ -319,24 +346,34 @@ int main() unsigned lvi = lv + bayer4x4_f[y%4][x%4]; unsigned ui = u + bayer4x4_f[y%4][x%4]; unsigned vi = v + bayer4x4_f[y%4][x%4]; - return plot(poly, x,y,z, ui,vi, lui,lvi, args...); + return plot(poly, x,y,z, + [=](auto&& fetch) { return fetch(ui, vi); }, + [=](auto&& fetch) { return fetch(lui, lvi); }, + args...); }; }; - auto PlotBase = [&](auto& poly, auto,auto,float z, unsigned u,unsigned v, unsigned lu,unsigned lv, float r,float g,float b) + auto PlotBase = [&](auto& poly, auto,auto,auto, auto&& texelf, auto&& luxelf, float r,float g,float b) { - auto texel = bitmap[ (poly.vbase + ((v - poly.vbase) & poly.vsize)) * txW + auto texel = texelf([&](unsigned u,unsigned v) { + return bitmap[ (poly.vbase + ((v - poly.vbase) & poly.vsize)) * txW + (poly.ubase + ((u - poly.ubase) & poly.usize)) ]; + }); auto rgb = std::tie(r,g,b); if(poly.flags == 0) { // Retrieve r,g,b from lightmap - auto luxel = lightmap.get(std::clamp(lu, poly.lubase, poly.lubase+poly.lusize), + auto luxel = luxelf([&](unsigned lu,unsigned lv) { + return lightmap.get(std::clamp(lu, poly.lubase, poly.lubase+poly.lusize), std::clamp(lv, poly.lvbase, poly.lvbase+poly.lvsize)); + }); rgb = rgb * luxel; } return texel * rgb; }; - auto Plot = Dithered(PlotBase); + //auto Plot = Regular(PlotBase); + auto Plot = Filtered(PlotBase); + //auto Plot = Dithered(PlotBase); + std::vector LightmapCalculator; for(std::size_t max=48, n=0; n