diff --git a/sandboxes/opencsg/CMakeLists.txt b/sandboxes/opencsg/CMakeLists.txt index 145912431..a6256250d 100644 --- a/sandboxes/opencsg/CMakeLists.txt +++ b/sandboxes/opencsg/CMakeLists.txt @@ -11,10 +11,8 @@ find_package(wxWidgets 3.1 REQUIRED COMPONENTS core base gl html) find_package(OpenGL REQUIRED) find_package(GLEW REQUIRED) find_package(OpenCSG REQUIRED) - find_package(GLUT REQUIRED) include(${wxWidgets_USE_FILE}) - target_link_libraries(opencsg_example libslic3r) target_include_directories(opencsg_example PRIVATE ${wxWidgets_INCLUDE_DIRS}) target_compile_definitions(opencsg_example PRIVATE ${wxWidgets_DEFINITIONS}) @@ -22,7 +20,6 @@ target_compile_definitions(opencsg_example PRIVATE ${wxWidgets_DEFINITIONS}) target_link_libraries(opencsg_example ${wxWidgets_LIBRARIES} OpenCSG::opencsg GLEW::GLEW - GLUT::GLUT OpenGL::GL #-lXrandr -lXext -lX11 ) diff --git a/sandboxes/opencsg/GLScene.cpp b/sandboxes/opencsg/GLScene.cpp index 9df3b601e..5744e8be7 100644 --- a/sandboxes/opencsg/GLScene.cpp +++ b/sandboxes/opencsg/GLScene.cpp @@ -1,5 +1,3 @@ -#include - #include "GLScene.hpp" #include #include @@ -7,12 +5,6 @@ #include -#ifdef __APPLE__ -#include -#else -#include -#endif - #include #ifndef NDEBUG @@ -54,60 +46,8 @@ inline void glAssertRecentCall() { } namespace Slic3r { namespace GL { Scene::Scene() = default; - Scene::~Scene() = default; -void renderfps () { - using Clock = std::chrono::high_resolution_clock; - using Duration = Clock::duration; - using TimePoint = Clock::time_point; - - static std::ostringstream fpsStream; - static int frames = 0; - static TimePoint last = Clock::now(); - - static const double resolution = 0.01; - static double fps = 0.; - - auto to_sec = [](Duration d) -> double { - return d.count() * double(Duration::period::num) / Duration::period::den; - }; - - ++frames; - - TimePoint msec = Clock::now(); - double seconds = to_sec(msec - last); - if (seconds >= resolution) { - last = msec; - - fps = 0.5 * (fps + frames / seconds); - - fpsStream.str(""); - fpsStream << "fps: " << std::setprecision(4) << fps << std::ends; - - frames = 0; - } - - glDisable(GL_DEPTH_TEST); - glLoadIdentity(); - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glColor3f(0.0f, 0.0f, 0.0f); - glRasterPos2f(-1.0f, -1.0f); - glDisable(GL_LIGHTING); - std::string s = fpsStream.str(); - for (unsigned int i=0; iview(); render_scene(); - renderfps(); +// renderfps(); + m_fps_counter.update(); swap_buffers(); } diff --git a/sandboxes/opencsg/GLScene.hpp b/sandboxes/opencsg/GLScene.hpp index 62863dc0b..4fd524af2 100644 --- a/sandboxes/opencsg/GLScene.hpp +++ b/sandboxes/opencsg/GLScene.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -211,6 +212,60 @@ public: void set_clip_z(double z) { m_clip_z = z; } }; +class FpsCounter { + vector> m_listeners; + + using Clock = std::chrono::high_resolution_clock; + using Duration = Clock::duration; + using TimePoint = Clock::time_point; + + int m_frames = 0; + TimePoint m_last = Clock::now(), m_window = m_last; + + double m_resolution = 0.1, m_window_size = 1.0; + double m_fps = 0.; + + static double to_sec(Duration d) + { + return d.count() * double(Duration::period::num) / Duration::period::den; + } + +public: + + void update() + { + ++m_frames; + + TimePoint msec = Clock::now(); + + double seconds_window = to_sec(msec - m_window); + m_fps = 0.5 * m_fps + 0.5 * (m_frames / seconds_window); + + if (to_sec(msec - m_last) >= m_resolution) { + m_last = msec; + for (auto &l : m_listeners) l(m_fps); + } + + if (seconds_window >= m_window_size) { + m_frames = 0; + m_window = msec; + } + } + + void add_listener(std::function lst) + { + m_listeners.emplace_back(lst); + } + + void clear_listeners() { m_listeners = {}; } + + void set_notification_interval(double seconds); + void set_measure_window_size(double seconds); + + double get_notification_interval() const { return m_resolution; } + double get_mesure_window_size() const { return m_window_size; } +}; + class PerspectiveCamera: public Camera { public: @@ -296,6 +351,7 @@ protected: } m_scene_cache; shptr m_camera; + FpsCounter m_fps_counter; public: @@ -323,6 +379,14 @@ public: virtual void clear_screen(); virtual void render_scene(); + + template void set_fps_counter(_FpsCounter &&fpsc) + { + m_fps_counter = std::forward<_FpsCounter>(fpsc); + } + + const FpsCounter &get_fps_counter() const { return m_fps_counter; } + FpsCounter &get_fps_counter() { return m_fps_counter; } }; class Controller : public std::enable_shared_from_this, diff --git a/sandboxes/opencsg/main.cpp b/sandboxes/opencsg/main.cpp index 3f764fe62..28aa89f6d 100644 --- a/sandboxes/opencsg/main.cpp +++ b/sandboxes/opencsg/main.cpp @@ -240,6 +240,12 @@ MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size): auto optimization_select = add_combobox("Optimization", { "Default", "ForceOn", "On", "Off" }); depth_select->Disable(); + auto fpstext = new wxStaticText(control_panel, wxID_ANY, ""); + console_sizer->Add(fpstext, 0, wxALL, 5); + m_canvas->get_fps_counter().add_listener([fpstext](double fps) { + fpstext->SetLabel(wxString::Format("fps: %.2f", fps) ); + }); + controlsizer->Add(slider_sizer, 0, wxEXPAND); controlsizer->Add(console_sizer, 1, wxEXPAND);