From a43389da33785e246b00bef761bf8787251d2f6c Mon Sep 17 00:00:00 2001 From: denizdiktas Date: Mon, 17 Jul 2023 13:00:51 +0300 Subject: [PATCH] bug fix: confirmed that the previous opengl error was due to the inactive OpenGL context. Fixed it by moving the update function into paintGL(). This causes a delay when updating the approximation, will probably need multithreaded solution (low-priority / future work) --- .../demo/earth/Main_widget.cpp | 27 ++++++++++++++----- .../demo/earth/Main_widget.h | 7 +++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Arrangement_on_surface_2/demo/earth/Main_widget.cpp b/Arrangement_on_surface_2/demo/earth/Main_widget.cpp index 76354624552..577bbb95bf9 100644 --- a/Arrangement_on_surface_2/demo/earth/Main_widget.cpp +++ b/Arrangement_on_surface_2/demo/earth/Main_widget.cpp @@ -202,7 +202,8 @@ void Main_widget::initializeGL() init_geometry(); init_shader_programs(); - init_country_borders(0.001); + m_current_approx_error = 0.001; + init_country_borders(m_current_approx_error); init_country_selection(); glClearColor(0, 0, 0, 1); @@ -213,6 +214,7 @@ void Main_widget::initializeGL() m_timer.start(12, this); } + void Main_widget::init_camera() { m_camera.set_pos(0, 0, 3); @@ -224,8 +226,9 @@ void Main_widget::init_camera() Message_manager::add("zoom_changed", [&] { qDebug() << "ZOOM CHANGED!!!"; - const auto error = compute_backprojected_error(0.5); - qDebug() << "new error = " << error; + //const auto error = compute_backprojected_error(0.5); + //qDebug() << "new error = " << error; + m_update_approx_error = true; //qDebug() << "re-initializing the country borders.."; //init_country_borders(error); }); @@ -404,9 +407,8 @@ void Main_widget::resizeGL(int w, int h) const qreal z_near = 0.1, z_far = 100.0, fov = 45.0; m_camera.perspective(fov, aspect, z_near, z_far); - // compute the world-space error for the given pixel-error - const auto err = compute_backprojected_error(0.5f); - std::cout << "error = " << err << std::endl; + // signal to look into the approximation error + m_update_approx_error = true; } template @@ -418,6 +420,19 @@ void draw_safe(T& ptr) void Main_widget::paintGL() { + if (m_update_approx_error) + { + const auto error = compute_backprojected_error(0.5); + qDebug() << "new approx error = " << error; + qDebug() << "current error = " << m_current_approx_error; + if(error < m_current_approx_error) + { + init_country_borders(error); + m_current_approx_error = error; + } + m_update_approx_error = false; + } + QMatrix4x4 model; model.rotate(-90, 1,0,0); // this makes z-axes point upwards! const auto view = m_camera.get_view_matrix(); diff --git a/Arrangement_on_surface_2/demo/earth/Main_widget.h b/Arrangement_on_surface_2/demo/earth/Main_widget.h index 47e1ea720d5..e31fd75f5e8 100644 --- a/Arrangement_on_surface_2/demo/earth/Main_widget.h +++ b/Arrangement_on_surface_2/demo/earth/Main_widget.h @@ -98,6 +98,13 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase // view-port int m_vp_width = 0, m_vp_height = 0; + // After zooming in or making the viewport larger, the approximation-error + // needs to be updated and checked against the old value. If a lower approxi- + // mation error is needed the necessary graphics-side updates need to be made + // INSIDE the paintGL (or whereever the OpenGL context is active)! + bool m_update_approx_error = false; + float m_current_approx_error; + // Timer for continuous screen-updates QBasicTimer m_timer; };