diff --git a/examples/deferred.cpp b/examples/deferred.cpp index bc934f23..a94e0034 100644 --- a/examples/deferred.cpp +++ b/examples/deferred.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -33,6 +34,9 @@ struct deferred_app void render() override; gfx::deferred_renderer renderer; + gfx::framebuffer pre_gamma_framebuffer; + gfx::texture_2d pre_gamma_texture; + gfx::gamma_correction gamma_correction; geom::spherical_camera camera; @@ -63,6 +67,8 @@ deferred_app::deferred_app() camera.azimuthal_angle = 0.f; camera.elevation_angle = geom::rad(30.f); + pre_gamma_texture.nearest_filter(); + // 0 - vec3 position // 1 - vec4 color (used if color & texture are not set) // 2 - vec2 texcoord (used if texture is set) @@ -282,6 +288,10 @@ void deferred_app::on_resize(int width, int height) { app::on_resize(width, height); camera.set_fov(camera.fov_y, static_cast(width) / height); + + pre_gamma_texture.load({width, height}); + pre_gamma_framebuffer.color(pre_gamma_texture); + pre_gamma_framebuffer.assert_complete(); } void deferred_app::on_mouse_move(int x, int y, int dx, int dy) @@ -353,16 +363,13 @@ void deferred_app::render() } gfx::deferred_renderer::options options; - options.framebuffer = &gfx::framebuffer::null(); - options.draw_buffer = gl::BACK; - options.viewport = {{{0, width()}, {0, height()}}}; options.camera = &camera; - options.clear_color = gfx::light(gfx::blue, 0.8f).as_color_4f(); + options.clear_color = geom::vector{0.f, 0.f, 0.1f, 1.f}; options.ambient = {1.f, 1.f, 1.f}; options.directional_lights.emplace_back(); - options.directional_lights[0].color = {5.f, 5.f, 5.f}; + options.directional_lights[0].color = {1.f, 1.f, 1.f}; options.directional_lights[0].direction = {1.f, 2.f, 3.f}; options.point_lights.emplace_back(); @@ -403,9 +410,32 @@ void deferred_app::render() objects.push_back(obj); } + float const gamma = 2.2f; + options.max_intensity = 15.f; - renderer.render(objects, options); + gfx::framebuffer::null().bind(); + gl::DrawBuffer(gl::BACK); + + gl::ClearColor(0.f, 0.f, 0.0f, 0.f); + gl::Clear(gl::COLOR_BUFFER_BIT); + + + { + gfx::render_target target; + target.framebuffer = &pre_gamma_framebuffer; + target.draw_buffer = gl::COLOR_ATTACHMENT0; + target.viewport = {{{0, width()}, {0, height()}}}; + renderer.render(objects, target, options); + } + + { + gfx::render_target target; + target.framebuffer = &gfx::framebuffer::null(); + target.draw_buffer = gl::BACK_LEFT; + target.viewport = {{{0, width()}, {0, height()}}}; + gamma_correction.invoke(pre_gamma_texture, target, {1.f / gamma}); + } { gfx::painter::text_options opts; @@ -415,7 +445,7 @@ void deferred_app::render() opts.y = gfx::painter::y_align::top; opts.c = gfx::yellow.as_color_rgba(); - painter.text({10.f, 10.f}, util::to_string("FPS: ", 1.f / frame_time.average()), opts); + painter.text({10.f, 10.f}, util::to_string("FPS: ", std::round(1.f / frame_time.average())), opts); } gl::Enable(gl::BLEND);