From acfaff3741380f81bd67f1cffb2e307f5f1c5802 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Tue, 17 Dec 2019 18:39:01 +0100 Subject: [PATCH] Dont use glut for fps measure. --- sandboxes/opencsg/CMakeLists.txt | 3 +- sandboxes/opencsg/GLScene.cpp | 58 ++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/sandboxes/opencsg/CMakeLists.txt b/sandboxes/opencsg/CMakeLists.txt index cf66867a5..145912431 100644 --- a/sandboxes/opencsg/CMakeLists.txt +++ b/sandboxes/opencsg/CMakeLists.txt @@ -11,7 +11,7 @@ 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) + find_package(GLUT REQUIRED) include(${wxWidgets_USE_FILE}) @@ -22,6 +22,7 @@ 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 2c3d1ffcd..9df3b601e 100644 --- a/sandboxes/opencsg/GLScene.cpp +++ b/sandboxes/opencsg/GLScene.cpp @@ -1,3 +1,5 @@ +#include + #include "GLScene.hpp" #include #include @@ -5,11 +7,11 @@ #include -//#ifdef __APPLE__ -//#include -//#else -//#include -//#endif +#ifdef __APPLE__ +#include +#else +#include +#endif #include @@ -56,23 +58,36 @@ Scene::Scene() = default; Scene::~Scene() = default; void renderfps () { - static std::ostringstream fpsStream; - static int fps = 0; - static int ancient = 0; - static int last = 0; - static int msec = 0; + using Clock = std::chrono::high_resolution_clock; + using Duration = Clock::duration; + using TimePoint = Clock::time_point; - last = msec; -// msec = glutGet(GLUT_ELAPSED_TIME); - if (last / 1000 != msec / 1000) { + 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); - float correctedFps = fps * 1000.0f / float(msec - ancient); fpsStream.str(""); - fpsStream << "fps: " << correctedFps << std::ends; + fpsStream << "fps: " << std::setprecision(4) << fps << std::ends; - ancient = msec; - fps = 0; + frames = 0; } + glDisable(GL_DEPTH_TEST); glLoadIdentity(); glMatrixMode(GL_PROJECTION); @@ -82,15 +97,14 @@ void renderfps () { glRasterPos2f(-1.0f, -1.0f); glDisable(GL_LIGHTING); std::string s = fpsStream.str(); -// for (unsigned int i=0; i