Skip to content

Commit

Permalink
Hackily fix platform-dependent gamma correction
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jul 4, 2024
1 parent 636a47a commit cd1b451
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 16 deletions.
19 changes: 10 additions & 9 deletions apps/hellotriangle/hellotriangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -89,11 +91,13 @@ void main()
const auto transform = identity<Transform>().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);
Expand Down Expand Up @@ -137,10 +141,7 @@ int main(int, char**)
return 0;
#else
osc::App app;
app.setup_main_loop<HelloTriangleScreen>();
ScopeGuard guard{[&app](){ app.teardown_main_loop(); }};
while (app.do_main_loop_step()) {
}
app.show<HelloTriangleScreen>();
return 0;
#endif
}
2 changes: 1 addition & 1 deletion src/OpenSimCreator/UI/MainUIScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/oscar/Graphics/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
14 changes: 14 additions & 0 deletions src/oscar/Platform/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion src/oscar/Platform/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/oscar/UI/Screens/TabTestingScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_demos/CookiecutterScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/oscar_learnopengl/Lighting/LOGLLightingMapsTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit cd1b451

Please sign in to comment.