]> OCCT Git - occt.git/commitdiff
0032723: Draw Harness, ViewerTest - sloppy animation in WebGL
authorkgv <kgv@opencascade.com>
Tue, 7 Dec 2021 17:07:04 +0000 (20:07 +0300)
committersmoskvin <smoskvin@opencascade.com>
Thu, 9 Dec 2021 22:23:40 +0000 (01:23 +0300)
Fixed emscripten_async_call() calls to use requestAnimationFrame() instead of setTimeout().
ViewerTest_EventManager::handleViewRedraw() - fixed queuing new onWasmRedrawView()
before previous one has been processed (leading to multiplying of pending redraws executed at the same time).
RWGltf_TriangulationReader::readDracoBuffer - suppressed CLang compilation warnings.

samples/webgl/WasmOcctView.cpp
samples/webgl/WasmOcctView.h
src/RWGltf/RWGltf_TriangulationReader.cxx
src/Standard/Standard_WarningsDisable.hxx
src/ViewerTest/ViewerTest_EventManager.cxx
src/ViewerTest/ViewerTest_EventManager.hxx

index 53e0096030fdbfbc91beead7e82388018ed82377..8aa88568b9e67c57ef46e9ff8a10aa3b99fa7ca6 100644 (file)
@@ -114,7 +114,7 @@ WasmOcctView& WasmOcctView::Instance()
 // ================================================================
 WasmOcctView::WasmOcctView()
 : myDevicePixelRatio (1.0f),
-  myUpdateRequests (0)
+  myNbUpdateRequests (0)
 {
   addActionHotKeys (Aspect_VKey_NavForward,        Aspect_VKey_W, Aspect_VKey_W | Aspect_VKeyFlags_SHIFT);
   addActionHotKeys (Aspect_VKey_NavBackward ,      Aspect_VKey_S, Aspect_VKey_S | Aspect_VKeyFlags_SHIFT);
@@ -395,9 +395,11 @@ void WasmOcctView::ProcessInput()
     // Queue onRedrawView()/redrawView callback to redraw canvas after all user input is flushed by browser.
     // Redrawing viewer on every single message would be a pointless waste of resources,
     // as user will see only the last drawn frame due to WebGL implementation details.
-    if (++myUpdateRequests == 1)
+    // -1 in emscripten_async_call() redirects to requestAnimationFrame();
+    // requestPostAnimationFrame() is a better under development alternative.
+    if (++myNbUpdateRequests == 1)
     {
-      emscripten_async_call (onRedrawView, this, 0);
+      emscripten_async_call (onRedrawView, this, -1);
     }
   }
 }
@@ -424,6 +426,7 @@ void WasmOcctView::redrawView()
 {
   if (!myView.IsNull())
   {
+    myNbUpdateRequests = 0;
     FlushViewEvents (myContext, myView, true);
   }
 }
@@ -435,13 +438,14 @@ void WasmOcctView::redrawView()
 void WasmOcctView::handleViewRedraw (const Handle(AIS_InteractiveContext)& theCtx,
                                      const Handle(V3d_View)& theView)
 {
-  myUpdateRequests = 0;
   AIS_ViewController::handleViewRedraw (theCtx, theView);
   if (myToAskNextFrame)
   {
     // ask more frames
-    ++myUpdateRequests;
-    emscripten_async_call (onRedrawView, this, 0);
+    if (++myNbUpdateRequests == 1)
+    {
+      emscripten_async_call (onRedrawView, this, -1);
+    }
   }
 }
 
index 58921a6bed416912c221a014ae0b9ef461803666..e5be3c6cb34ff866a99da2ff498a3184c222c255 100644 (file)
@@ -249,7 +249,7 @@ private:
   TCollection_AsciiString        myCanvasId;         //!< canvas element id on HTML page
   Graphic3d_Vec2i                myWinSizeOld;
   float                          myDevicePixelRatio; //!< device pixel ratio for handling high DPI displays
-  unsigned int                   myUpdateRequests;   //!< counter for unhandled update requests
+  unsigned int                   myNbUpdateRequests; //!< counter for unhandled update requests
 
 };
 
index 16ad9b9fa65538a88507985c4ada457fcc96c927..c528ae613442b6229b69a66453c09655160f4c21 100644 (file)
@@ -224,7 +224,7 @@ bool RWGltf_TriangulationReader::readDracoBuffer (const Handle(RWGltf_GltfLatePr
 
 #ifdef HAVE_DRACO
   std::vector<char> aReadData;
-  aReadData.resize (theGltfData.StreamLength);
+  aReadData.resize ((size_t )theGltfData.StreamLength);
   aSharedStream->read (aReadData.data(), (std::streamsize )theGltfData.StreamLength);
   if (!aSharedStream->good())
   {
index ca47a76d8e2d4d202d46a2900ca25b5011abb8ca..25000d599115f27ff2c1c3ecc0947f2d0fc3e451 100644 (file)
@@ -31,6 +31,7 @@
   #pragma clang diagnostic push
   #pragma clang diagnostic ignored "-Wall"
   #pragma clang diagnostic ignored "-Wextra"
+  #pragma clang diagnostic ignored "-Wshorten-64-to-32"
 #elif defined(_MSC_VER)
   #pragma warning(push, 0)
 #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
index 3335af811fceee269ef91f1429f67d59f0d64aab..772dd1f98469f583b678773e039b6c189dd1528e 100644 (file)
   #include <emscripten.h>
   #include <emscripten/html5.h>
 
-  //! Callback flushing events and redrawing the WebGL canvas.
-  static void onWasmRedrawView (void* )
+//=======================================================================
+//function : onWasmRedrawView
+//purpose  :
+//=======================================================================
+void ViewerTest_EventManager::onWasmRedrawView (void* )
+{
+  Handle(ViewerTest_EventManager) aViewCtrl = ViewerTest::CurrentEventManager();
+  if (!aViewCtrl.IsNull())
   {
-    Handle(ViewerTest_EventManager) aViewCtrl = ViewerTest::CurrentEventManager();
+    aViewCtrl->myNbUpdateRequests = 0;
+
     const Handle(V3d_View)& aView = ViewerTest::CurrentView();
     const Handle(AIS_InteractiveContext)& aCtx = ViewerTest::GetAISContext();
-    if (!aViewCtrl.IsNull() && !aView.IsNull() && !aCtx.IsNull())
+    if (!aView.IsNull() && !aCtx.IsNull())
     {
       aViewCtrl->ProcessExpose();
     }
   }
+}
 #endif
 
 Standard_IMPORT Standard_Boolean Draw_Interprete (const char* theCommand);
@@ -76,7 +84,7 @@ ViewerTest_EventManager::ViewerTest_EventManager (const Handle(V3d_View)&
   myView (theView),
   myToPickPnt (Standard_False),
   myIsTmpContRedraw (Standard_False),
-  myUpdateRequests (0)
+  myNbUpdateRequests (0)
 {
   myViewAnimation = GlobalViewAnimation();
 
@@ -183,7 +191,6 @@ void ViewerTest_EventManager::ProcessExpose()
 void ViewerTest_EventManager::handleViewRedraw (const Handle(AIS_InteractiveContext)& theCtx,
                                                 const Handle(V3d_View)& theView)
 {
-  myUpdateRequests = 0;
   AIS_ViewController::handleViewRedraw (theCtx, theView);
 
   // On non-Windows platforms Aspect_Window::InvalidateContent() from rendering thread does not work as expected
@@ -202,10 +209,12 @@ void ViewerTest_EventManager::handleViewRedraw (const Handle(AIS_InteractiveCont
     }
 
     // ask more frames
-    ++myUpdateRequests;
-  #if defined(__EMSCRIPTEN__)
-    emscripten_async_call (onWasmRedrawView, this, 0);
-  #endif
+    if (++myNbUpdateRequests == 1)
+    {
+    #if defined(__EMSCRIPTEN__)
+      emscripten_async_call (onWasmRedrawView, this, -1);
+    #endif
+    }
   }
   else if (myIsTmpContRedraw)
   {
@@ -256,11 +265,11 @@ void ViewerTest_EventManager::ProcessInput()
   // Queue onWasmRedrawView() callback to redraw canvas after all user input is flushed by browser.
   // Redrawing viewer on every single message would be a pointless waste of resources,
   // as user will see only the last drawn frame due to WebGL implementation details.
-  if (++myUpdateRequests == 1)
+  // -1 in emscripten_async_call() redirects to requestAnimationFrame();
+  // requestPostAnimationFrame() is a better under development alternative.
+  if (++myNbUpdateRequests == 1)
   {
-  #if defined(__EMSCRIPTEN__)
-    emscripten_async_call (onWasmRedrawView, this, 0);
-  #endif
+    emscripten_async_call (onWasmRedrawView, this, -1);
   }
 #else
   // handle synchronously
index 23dfe7dceeb2c03ba56a7fae10e6c031c91ccc26..0bfbe32727eb46824b01840b6df9112435e01f31 100644 (file)
@@ -138,6 +138,13 @@ protected:
                                                     unsigned int theModifNew,
                                                     double       theTimeStamp);
 
+private:
+
+#if defined(__EMSCRIPTEN__)
+  //! Callback flushing events and redrawing the WebGL canvas.
+  static void onWasmRedrawView (void* );
+#endif
+
 private:
 
   Handle(AIS_InteractiveContext) myCtx;
@@ -148,7 +155,7 @@ private:
   Standard_Boolean myToPickPnt;
   Standard_Boolean myIsTmpContRedraw;
 
-  unsigned int     myUpdateRequests; //!< counter for unhandled update requests
+  unsigned int     myNbUpdateRequests; //!< counter for unhandled update requests
 
 };