From cd1b45101debb9d34ff22eff492802455705229b Mon Sep 17 00:00:00 2001 From: Adam Kewley Date: Thu, 4 Jul 2024 13:41:22 +0200 Subject: [PATCH] Hackily fix platform-dependent gamma correction --- apps/hellotriangle/hellotriangle.cpp | 19 ++++++++++--------- src/OpenSimCreator/UI/MainUIScreen.cpp | 2 +- src/oscar/Graphics/Color.h | 5 +++++ src/oscar/Platform/App.cpp | 14 ++++++++++++++ src/oscar/Platform/App.h | 6 +++++- src/oscar/UI/Screens/TabTestingScreen.cpp | 2 +- src/oscar_demos/CookiecutterScreen.cpp | 2 +- .../AdvancedLighting/LOGLNormalMappingTab.cpp | 2 +- .../LOGLParallaxMappingTab.cpp | 2 +- .../Lighting/LOGLLightingMapsTab.cpp | 2 +- 10 files changed, 40 insertions(+), 16 deletions(-) diff --git a/apps/hellotriangle/hellotriangle.cpp b/apps/hellotriangle/hellotriangle.cpp index 41931f5bf..dfbfee284 100644 --- a/apps/hellotriangle/hellotriangle.cpp +++ b/apps/hellotriangle/hellotriangle.cpp @@ -76,6 +76,8 @@ void main() void impl_on_draw() override { + App::upd().clear_screen(); + ui::context::on_start_new_frame(); // ensure target texture matches screen dimensions @@ -89,11 +91,13 @@ void main() const auto transform = identity().with_rotation(angle_axis(Radians{seconds_since_startup}, Vec3{0.0f, 1.0f, 0.0f})); graphics::draw(mesh_, transform, material_, camera_); camera_.render_to(target_texture_); -#ifdef EMSCRIPTEN - graphics::blit_to_screen(target_texture_, Rect{{}, App::get().main_window_dimensions()}, gamma_correcter_); -#else - graphics::blit_to_screen(target_texture_, Rect{{}, App::get().main_window_dimensions()}); -#endif + + if (App::get().is_main_window_gamma_corrected()) { + graphics::blit_to_screen(target_texture_, Rect{{}, App::get().main_window_dimensions()}); + } + else { + graphics::blit_to_screen(target_texture_, Rect{{}, App::get().main_window_dimensions()}, gamma_correcter_); + } ui::begin_panel("window"); ui::draw_float_slider("torus_radius", &edited_torus_parameters_.torus_radius, 0.0f, 5.0f); @@ -137,10 +141,7 @@ int main(int, char**) return 0; #else osc::App app; - app.setup_main_loop(); - ScopeGuard guard{[&app](){ app.teardown_main_loop(); }}; - while (app.do_main_loop_step()) { - } + app.show(); return 0; #endif } diff --git a/src/OpenSimCreator/UI/MainUIScreen.cpp b/src/OpenSimCreator/UI/MainUIScreen.cpp index 04faec7f3..00e8ff35a 100644 --- a/src/OpenSimCreator/UI/MainUIScreen.cpp +++ b/src/OpenSimCreator/UI/MainUIScreen.cpp @@ -326,7 +326,7 @@ class osc::MainUIScreen::Impl final : { OSC_PERF("MainUIScreen/clear_screen"); - App::upd().clear_screen({0.0f, 0.0f, 0.0f, 0.0f}); + App::upd().clear_screen(); } ui::context::on_start_new_frame(); diff --git a/src/oscar/Graphics/Color.h b/src/oscar/Graphics/Color.h index 45910955c..afa1cb852 100644 --- a/src/oscar/Graphics/Color.h +++ b/src/oscar/Graphics/Color.h @@ -22,6 +22,11 @@ namespace osc using iterator = value_type*; using const_iterator = const value_type*; + static constexpr Color dark_grey() + { + return {0.5f, 0.5f, 0.5f}; + } + static constexpr Color half_grey() { return {0.5f, 0.5f, 0.5f}; diff --git a/src/oscar/Platform/App.cpp b/src/oscar/Platform/App.cpp index f05a04622..632d7d54f 100644 --- a/src/oscar/Platform/App.cpp +++ b/src/oscar/Platform/App.cpp @@ -423,6 +423,15 @@ class osc::App::Impl final { return graphics_context_.max_antialiasing_level(); } + bool is_main_window_gamma_corrected() const + { +#ifdef EMSCRIPTEN + return false; +#else + return true; +#endif + } + bool is_in_debug_mode() const { return graphics_context_.is_in_debug_mode(); @@ -906,6 +915,11 @@ AntiAliasingLevel osc::App::max_anti_aliasing_level() const return impl_->max_anti_aliasing_level(); } +bool osc::App::is_main_window_gamma_corrected() const +{ + return impl_->is_main_window_gamma_corrected(); +} + bool osc::App::is_in_debug_mode() const { return impl_->is_in_debug_mode(); diff --git a/src/oscar/Platform/App.h b/src/oscar/Platform/App.h index 2b61df495..b94ab2d6c 100644 --- a/src/oscar/Platform/App.h +++ b/src/oscar/Platform/App.h @@ -189,6 +189,10 @@ namespace osc // returns the maximum number of anti-aliasing samples that the graphics backend supports AntiAliasingLevel max_anti_aliasing_level() const; + // returns true if the main window is backed by a framebuffer/renderbuffer that automatically + // converts the linear outputs (from shaders) into (e.g.) sRGB on-write + bool is_main_window_gamma_corrected() const; + // returns true if the application is rendering in debug mode // // other parts of the application can use this to decide whether to render @@ -248,7 +252,7 @@ namespace osc void request_redraw(); // threadsafe: used to make a waiting loop redraw // fill all pixels in the main window with the given color - void clear_screen(const Color&); + void clear_screen(const Color& = Color::clear()); // sets the main window's subtitle (e.g. document name) void set_main_window_subtitle(std::string_view); diff --git a/src/oscar/UI/Screens/TabTestingScreen.cpp b/src/oscar/UI/Screens/TabTestingScreen.cpp index 7eb34bed7..3e02581f0 100644 --- a/src/oscar/UI/Screens/TabTestingScreen.cpp +++ b/src/oscar/UI/Screens/TabTestingScreen.cpp @@ -54,7 +54,7 @@ class osc::TabTestingScreen::Impl final : void impl_on_draw() override { - App::upd().clear_screen({0.0f, 0.0f, 0.0f, 0.0f}); + App::upd().clear_screen(); ui::context::on_start_new_frame(); current_tab_->on_draw(); ui::context::render(); diff --git a/src/oscar_demos/CookiecutterScreen.cpp b/src/oscar_demos/CookiecutterScreen.cpp index df83df259..9fdcc3fde 100644 --- a/src/oscar_demos/CookiecutterScreen.cpp +++ b/src/oscar_demos/CookiecutterScreen.cpp @@ -56,7 +56,7 @@ class osc::CookiecutterScreen::Impl final { ui::context::on_start_new_frame(); // prepare the 2D UI for drawing a new frame - App::upd().clear_screen(Color::clear()); // set app window bg color + App::upd().clear_screen(); // set app window bg color ui::begin_panel("cookiecutter panel"); ui::draw_text("hello world"); diff --git a/src/oscar_learnopengl/AdvancedLighting/LOGLNormalMappingTab.cpp b/src/oscar_learnopengl/AdvancedLighting/LOGLNormalMappingTab.cpp index 7e5c7e84f..e86d0e293 100644 --- a/src/oscar_learnopengl/AdvancedLighting/LOGLNormalMappingTab.cpp +++ b/src/oscar_learnopengl/AdvancedLighting/LOGLNormalMappingTab.cpp @@ -121,7 +121,7 @@ class osc::LOGLNormalMappingTab::Impl final : public StandardTabImpl { camera_.on_draw(); // clear screen and ensure camera has correct pixel rect - App::upd().clear_screen({0.1f, 0.1f, 0.1f, 1.0f}); + App::upd().clear_screen(Color::dark_grey()); // draw normal-mapped quad { diff --git a/src/oscar_learnopengl/AdvancedLighting/LOGLParallaxMappingTab.cpp b/src/oscar_learnopengl/AdvancedLighting/LOGLParallaxMappingTab.cpp index f85afe037..f72d19c75 100644 --- a/src/oscar_learnopengl/AdvancedLighting/LOGLParallaxMappingTab.cpp +++ b/src/oscar_learnopengl/AdvancedLighting/LOGLParallaxMappingTab.cpp @@ -116,7 +116,7 @@ class osc::LOGLParallaxMappingTab::Impl final : public StandardTabImpl { camera_.on_draw(); // clear screen and ensure camera has correct pixel rect - App::upd().clear_screen({0.1f, 0.1f, 0.1f, 1.0f}); + App::upd().clear_screen(Color::dark_grey()); // draw normal-mapped quad { diff --git a/src/oscar_learnopengl/Lighting/LOGLLightingMapsTab.cpp b/src/oscar_learnopengl/Lighting/LOGLLightingMapsTab.cpp index 45b9a73af..9aab74c54 100644 --- a/src/oscar_learnopengl/Lighting/LOGLLightingMapsTab.cpp +++ b/src/oscar_learnopengl/Lighting/LOGLLightingMapsTab.cpp @@ -74,7 +74,7 @@ class osc::LOGLLightingMapsTab::Impl final : public StandardTabImpl { camera_.on_draw(); // clear screen and ensure camera has correct pixel rect - App::upd().clear_screen({0.1f, 0.1f, 0.1f, 1.0f}); + App::upd().clear_screen(Color::dark_grey()); // draw cube lighting_maps_material_.set_vec3("uViewPos", camera_.position());