diff options
author | tamasmeszaros <meszaros.q@gmail.com> | 2019-12-18 14:00:28 +0300 |
---|---|---|
committer | tamasmeszaros <meszaros.q@gmail.com> | 2019-12-18 14:00:28 +0300 |
commit | 472c4c885d4430c47595b176fa4edc25259b9758 (patch) | |
tree | 71738f34e365bc30d0a3eb8684a62d92dbb893e3 /sandboxes | |
parent | acfaff3741380f81bd67f1cffb2e307f5f1c5802 (diff) |
Separate fps counter and remove glut dependency
Diffstat (limited to 'sandboxes')
-rw-r--r-- | sandboxes/opencsg/CMakeLists.txt | 3 | ||||
-rw-r--r-- | sandboxes/opencsg/GLScene.cpp | 68 | ||||
-rw-r--r-- | sandboxes/opencsg/GLScene.hpp | 64 | ||||
-rw-r--r-- | sandboxes/opencsg/main.cpp | 6 |
4 files changed, 73 insertions, 68 deletions
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 <chrono> - #include "GLScene.hpp" #include <libslic3r/Utils.hpp> #include <libslic3r/SLAPrint.hpp> @@ -7,12 +5,6 @@ #include <GL/glew.h> -#ifdef __APPLE__ -#include <GLUT/glut.h> -#else -#include <GL/glut.h> -#endif - #include <boost/log/trivial.hpp> #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; i<s.size(); ++i) { - glutBitmapCharacter(GLUT_BITMAP_8_BY_13, s[i]); - } - glEnable(GL_LIGHTING); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - glEnable(GL_DEPTH_TEST); - - glFlush(); -} - void Display::render_scene() { GLfloat color[] = {1.f, 1.f, 0.f, 0.f}; @@ -367,12 +307,9 @@ Display::~Display() } void Display::set_active(long width, long height) -{ - static int argc = 0; - +{ if (!m_initialized) { glewInit(); - glutInit(&argc, nullptr); m_initialized = true; } @@ -415,7 +352,8 @@ void Display::repaint() m_camera->view(); 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 <vector> #include <memory> +#include <chrono> #include <libslic3r/Geometry.hpp> #include <libslic3r/Model.hpp> @@ -211,6 +212,60 @@ public: void set_clip_z(double z) { m_clip_z = z; } }; +class FpsCounter { + vector<std::function<void(double)>> 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<void(double)> 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<Camera> m_camera; + FpsCounter m_fps_counter; public: @@ -323,6 +379,14 @@ public: virtual void clear_screen(); virtual void render_scene(); + + template<class _FpsCounter> 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<Controller>, 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); |