@@ -116,6 +116,12 @@ int spacenavHandlerIm()
116116 fixTranslation = !fixTranslation;
117117 }
118118 }
119+ } else {
120+ #ifdef _WIN32
121+ Sleep (1 );
122+ #else
123+ usleep (1000 );
124+ #endif
119125 }
120126 }
121127#else
@@ -253,11 +259,18 @@ extern bool classLabels;
253259
254260// Show Interrupt handlers:
255261static bool interrupted = false ;
262+ static int last_input_ms = -1000000 ;
263+ static const int redisplay_grace_ms = 100 ;
256264
257265void interruptDrawing () { interrupted = true ; }
258266
259267void checkForInterrupt () { interrupted = false ; }
260268
269+ static inline void noteInputActivity ()
270+ {
271+ last_input_ms = glutGet (GLUT_ELAPSED_TIME );
272+ }
273+
261274bool isInterrupted ()
262275{
263276#ifndef __APPLE__
@@ -286,6 +299,7 @@ void keyPressedIm(unsigned char key, int x, int y)
286299{
287300 // We need to update unless we are in an animation
288301 if (haveToUpdate != 3 ) haveToUpdate = 1 ;
302+ noteInputActivity ();
289303 interruptDrawing ();
290304 ImGuiIO &io = ImGui::GetIO ();
291305 ImGui_ImplGLUT_KeyboardFunc (key, x, y);
@@ -297,6 +311,7 @@ void keyPressedIm(unsigned char key, int x, int y)
297311void keyPressedUpIm (unsigned char key, int x, int y)
298312{
299313 if (haveToUpdate != 3 ) haveToUpdate = 1 ;
314+ noteInputActivity ();
300315 interruptDrawing ();
301316 ImGui_ImplGLUT_KeyboardUpFunc (key, x, y);
302317 callbacks::glut::keyReleased (key, x, y);
@@ -307,6 +322,7 @@ void keyPressedUpIm(unsigned char key, int x, int y)
307322void mouseButtonIm (int button, int state, int x, int y)
308323{
309324 if (haveToUpdate != 3 ) haveToUpdate = 1 ;
325+ noteInputActivity ();
310326 interruptDrawing ();
311327 ImGui_ImplGLUT_MouseFunc (button, state, x, y);
312328 callbacks::glut::mouseButton (button, state, x, y);
@@ -323,6 +339,7 @@ void reshapeIm(int width, int height)
323339void mouseMoveIm (int x, int y)
324340{
325341 if (haveToUpdate != 3 ) haveToUpdate = 1 ;
342+ noteInputActivity ();
326343 interruptDrawing ();
327344 ImGui_ImplGLUT_MotionFunc (x, y);
328345 ImGuiIO &io = ImGui::GetIO ();
@@ -334,7 +351,6 @@ void mouseMoveIm(int x, int y)
334351static vgm::Quat qRotVgm = vgm::Quat(1 .f, 0 .f, 0 .f, 0 .f);
335352static double t[3 ] = {0 , 0 , 0 }, mat[16 ], rPT[3 ];
336353static int colorTypeVal = 0 ;
337- static int modal_renderings = 0 ;
338354static int colorMapVal = 0 ;
339355static bool cam_mouse_nav_bool = true ;
340356static bool always_all_pts = false ;
@@ -357,7 +373,7 @@ static bool coloranimbool;
357373 * After this function, you have to manually collect and render, e.g.:
358374 * ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
359375 */
360- void renderImGuiWindows (bool update_logic = true )
376+ void renderImGuiWindows ()
361377{
362378 // ImGUI Renderings First:
363379 ImGui_ImplOpenGL2_NewFrame ();
@@ -371,8 +387,8 @@ void renderImGuiWindows(bool update_logic = true)
371387 ImGui::SetNextWindowPos (ImVec2 (START_WIDTH_IMGUI * 0.83 , START_HEIGHT_IMGUI * 0.01 ),
372388 ImGuiCond_FirstUseEver);
373389 ImGui::SetNextWindowSize (ImVec2 (START_WIDTH_IMGUI * 0.165 , START_HEIGHT_IMGUI * 0.80 ),
374- ImGuiCond_FirstUseEver);
375- ImGui::SetNextWindowCollapsed (true , ImGuiCond_FirstUseEver);
390+ ImGuiCond_FirstUseEver);
391+ ImGui::SetNextWindowCollapsed (false , ImGuiCond_FirstUseEver);
376392 ImGui::Begin (" Selection" );
377393
378394 if (ImGui::TreeNode (" Draw" )) {
@@ -626,8 +642,8 @@ void renderImGuiWindows(bool update_logic = true)
626642 ImGui::SetNextWindowPos (ImVec2 (START_WIDTH_IMGUI * 0.01 , START_HEIGHT_IMGUI * 0.01 ),
627643 ImGuiCond_FirstUseEver);
628644 ImGui::SetNextWindowSize (ImVec2 (START_WIDTH_IMGUI * 0.655 , START_HEIGHT_IMGUI * 0.20 ),
629- ImGuiCond_FirstUseEver);
630- ImGui::SetNextWindowCollapsed (true , ImGuiCond_FirstUseEver);
645+ ImGuiCond_FirstUseEver);
646+ ImGui::SetNextWindowCollapsed (false , ImGuiCond_FirstUseEver);
631647 if (ImGui::Begin (" Controls" )) {
632648
633649 bool table_exists = ImGui::BeginTable (
@@ -645,18 +661,18 @@ void renderImGuiWindows(bool update_logic = true)
645661
646662 // Column 2
647663 ImGui::TableNextColumn ();
648- ImGui::Text (" Zoom settings " );
664+ ImGui::Text (" FoV/ Zoom" );
649665 if (showViewMode == 0 ) {
650- ImGui::SliderFloat (" Field of View " , &cangle, 1.0 , 180.0 , " %.1f" );
666+ ImGui::SliderFloat (" " , &cangle, 1.0 , 180.0 , " %.1f" );
651667 ImGui::Text (" Currently in: Normal view." );
652668 }
653669 if (showViewMode == 1 ) {
654- ImGui::SliderFloat (" Parallel Zoom " , &pzoom, 0.1 , 100000.0 , " %.1f" ,
670+ ImGui::SliderFloat (" " , &pzoom, 0.1 , 100000.0 , " %.1f" ,
655671 ImGuiSliderFlags_Logarithmic);
656672 ImGui::Text (" Currently in: Ortographic view." );
657673 }
658674 if (showViewMode == 2 ) {
659- ImGui::SliderFloat (" Rotate Zoom " , &rzoom, 0.001 , 100000.0 , " %.3f" ,
675+ ImGui::SliderFloat (" " , &rzoom, 0.001 , 100000.0 , " %.3f" ,
660676 ImGuiSliderFlags_Logarithmic);
661677 ImGui::Text (" Currently in: Rotate view." );
662678 }
@@ -703,7 +719,7 @@ void renderImGuiWindows(bool update_logic = true)
703719 // Column 4
704720 ImGui::TableNextColumn ();
705721 ImGui::Text (" Camera" );
706- ImGui::SliderInt (" Choose camera " , &signed_int_cam_choice, 0 , cams.size ());
722+ ImGui::SliderInt (" " , &signed_int_cam_choice, 0 , cams.size ());
707723 cam_choice = signed_int_cam_choice;
708724 if (ImGui::Button (" Add camera" ))
709725 callAddCamera (0 );
@@ -767,24 +783,15 @@ void renderImGuiWindows(bool update_logic = true)
767783 }
768784
769785 // If non-idle and always all
770- } else if (always_all_pts && update_logic ) {
786+ } else if (always_all_pts) {
771787 if (!mousemoving && !keypressed) {
772788 if (pointmode != 1 ) {
773789 pointmode = 1 ;
774- // Don't issue a redisplay here either for the same reason.
775- // The mouse release event likely already triggered a redisplay.
776- // Only if we need to force a re-draw (e.g. if idle) would we need this,
777- // but transitioning from moving to not moving implies an event happened.
778- // glutPostRedisplay();
779790 }
780791 } else {
781792 // If we are moving, we switch to reduced points immediately
782793 if (pointmode != -1 ) {
783794 pointmode = -1 ;
784- // Don't issue a redisplay; the interaction event (motion/mouse)
785- // already issued a redisplay. Issuing another one here might
786- // cause a double-buffer swap glitch or race condition.
787- // glutPostRedisplay();
788795 }
789796 }
790797 } else if (!always_reduce_pts && !always_all_pts && (mousemoving || keypressed)) {
@@ -983,7 +990,7 @@ void DrawPointsIm(GLenum mode, bool interruptable)
983990 if (!invert) {
984991 glDisable (GL_COLOR_LOGIC_OP );
985992 }
986-
993+ // Just put the already available render data above the points
987994 ImGui_ImplOpenGL2_RenderDrawData (ImGui::GetDrawData ());
988995 // Reset color to before state
989996 if (invert) {
@@ -1224,7 +1231,7 @@ void displayIm()
12241231#ifdef _WIN32
12251232 Sleep (5 ); // legacy show: Sleep(25)
12261233#else
1227- usleep (50000 ); // legacy show: usleep(250000)
1234+ usleep (1000 ); // legacy show: usleep(250000)
12281235#endif
12291236 }
12301237
@@ -1237,12 +1244,6 @@ void displayIm()
12371244
12381245 // Draw the buffer
12391246 glutSwapBuffers ();
1240- // Update if ImGui wants attention or reduce pts (interactive mode)
1241- /* TODO : Think about a fourth mode, similar to -1 (reduce all), but only when interacted */
1242- if (pointmode == -1 || ImGui::GetIO ().WantCaptureMouse || ImGui::GetIO ().WantCaptureKeyboard ) {
1243- if (pointmode != 0 )
1244- glutPostRedisplay ();
1245- }
12461247}
12471248
12481249/* *
@@ -1254,18 +1255,38 @@ void displayIm()
12541255 */
12551256void idleIm (void )
12561257{
1258+ // Adaptive Sleep
1259+ // If there is nothing to be done, we can sleep a bit longer to save CPU cycles
1260+ if (haveToUpdate == 0 && (fullydisplayed || pointmode == 1 || pointmode == -1 )) {
12571261#ifdef _WIN32
1258- Sleep (1 );
1262+ Sleep (20 );
12591263#else
1260- usleep (1000 );
1264+ usleep (20000 );
12611265#endif
1262-
1266+ } else {
1267+ // If we are accumulating points (pointmode == 0) or updating, yield briefly
1268+ #ifdef _WIN32
1269+ Sleep (1 );
1270+ #else
1271+ usleep (1000 );
1272+ #endif
1273+ }
12631274
12641275 if (glutGetWindow () != window_id)
12651276 glutSetWindow (window_id);
12661277
12671278 // return as nothing has to be updated
12681279 if (haveToUpdate == 0 ) {
1280+ // Continuous redraw is only needed during active interaction.
1281+ // This keeps CPU usage low when the scene is stable.
1282+ int now_ms = glutGet (GLUT_ELAPSED_TIME );
1283+ bool active_input = (mousemoving && mouseNavButton != -1 ) || keypressed;
1284+ bool redraw_grace = (now_ms - last_input_ms) <= redisplay_grace_ms;
1285+ if (active_input || redraw_grace) {
1286+ update_callback ();
1287+ return ;
1288+ }
1289+
12691290 if (!fullydisplayed && !mousemoving && !keypressed && pointmode == 0 ) {
12701291 glDrawBuffer (buffermode);
12711292 // Call the display function
0 commit comments