0028895: Visualization, V3d_View::SetComputedMode() - HLR calculation is performed...
[occt.git] / samples / CSharp / OCCTProxy_D3D / OCCTProxyD3D.cpp
index aeb5f7c..413674e 100644 (file)
@@ -1,20 +1,19 @@
-#include "BridgeFBO.hxx"
+#include <windows.h>
 
 // include required OCCT headers
 #include <Standard_Version.hxx>
 #include <Message_ProgressIndicator.hxx>
 //for OCC graphic
-#include <Aspect_DisplayConnection.hxx>
 #include <WNT_Window.hxx>
+#include <WNT_WClass.hxx>
 #include <Graphic3d_CView.hxx>
 #include <Graphic3d_Camera.hxx>
 #include <Graphic3d_TextureParams.hxx>
-#include <OpenGl_GraphicDriver.hxx>
-#include <OpenGl_CView.hxx>
+#include <D3DHost_GraphicDriver.hxx>
+#include <D3DHost_View.hxx>
 //for object display
 #include <V3d_Viewer.hxx>
 #include <V3d_View.hxx>
-#include <Visual3d_View.hxx>
 #include <AIS_InteractiveContext.hxx>
 #include <AIS_Shape.hxx>
 //topology
 #pragma comment(lib, "TKService.lib")
 #pragma comment(lib, "TKV3d.lib")
 #pragma comment(lib, "TKOpenGl.lib")
+#pragma comment(lib, "TKD3dHost.lib")
 #pragma comment(lib, "TKIGES.lib")
 #pragma comment(lib, "TKSTEP.lib")
 #pragma comment(lib, "TKStl.lib")
 #pragma comment(lib, "TKVrml.lib")
 
+#pragma comment(lib, "D3D9.lib")
+
 /// <summary>
 /// Proxy class encapsulating calls to OCCT C++ classes within
 /// C++/CLI class visible from .Net (CSharp)
@@ -60,10 +62,7 @@ public ref class OCCTProxyD3D
 {
 public:
 
-  OCCTProxyD3D() : myBridgeFBO (NULL)
-  {
-    //
-  }
+  OCCTProxyD3D() {}
 
   // ============================================
   // Viewer functionality
@@ -73,90 +72,36 @@ public:
   ///Initialize a viewer
   /// </summary>
   /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
-  bool InitViewer (System::IntPtr theWnd)
+  bool InitViewer()
   {
-    myGraphicDriver() = new OpenGl_GraphicDriver (Handle(Aspect_DisplayConnection)());
+    myGraphicDriver() = new D3DHost_GraphicDriver();
+    myGraphicDriver()->ChangeOptions().buffersNoSwap = true;
     //myGraphicDriver()->ChangeOptions().contextDebug = true;
 
-    TCollection_ExtendedString a3DName ("Visu3D");
-    myViewer() = new V3d_Viewer (myGraphicDriver(), a3DName.ToExtString(), "", 1000.0,
-                                 V3d_XposYnegZpos, Quantity_NOC_GRAY30,
-                                 V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT,
-                                 Standard_True, Standard_False);
-
+    myViewer() = new V3d_Viewer (myGraphicDriver());
     myViewer()->SetDefaultLights();
     myViewer()->SetLightOn();
     myView() = myViewer()->CreateView();
-    Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
+
+    static Handle(WNT_WClass) aWClass = new WNT_WClass ("OCC_Viewer", NULL, CS_OWNDC);
+    Handle(WNT_Window) aWNTWindow = new WNT_Window ("OCC_Viewer", aWClass, WS_POPUP, 64, 64, 64, 64);
+    aWNTWindow->SetVirtual (Standard_True);
     myView()->SetWindow(aWNTWindow);
-    if (!aWNTWindow->IsMapped())
-    {
-      aWNTWindow->Map();
-    }
     myAISContext() = new AIS_InteractiveContext (myViewer());
     myAISContext()->UpdateCurrentViewer();
     myView()->MustBeResized();
     return true;
   }
 
-  /// <summary> Initializes OCCT viewer for OpenGL-Direct3D interoperability. </summary>
-  bool InitViewer (System::IntPtr theHWND,
-                   System::IntPtr theD3DDevice)
-  {
-    if (!InitViewer (theHWND))
-    {
-      return false;
-    }
-
-    Graphic3d_CView*       aCView     = reinterpret_cast<Graphic3d_CView*> (myView()->View()->CView());
-    OpenGl_CView*          aCViewGl   = reinterpret_cast<OpenGl_CView*>    (aCView->ptrView);
-    Handle(OpenGl_Context) aGlContext = aCViewGl->WS->GetGlContext();
-    if (aGlContext.IsNull())
-    {
-      return false;
-    }
-    if (!aGlContext->IsCurrent())
-    {
-      aGlContext->MakeCurrent();
-    }
-
-    myBridgeFBO = new BridgeFBO();
-    if (!myBridgeFBO->Init (aGlContext, theD3DDevice.ToPointer()))
-    {
-      return false;
-    }
-
-    aCView->ptrFBO = myBridgeFBO;
-    return true;
-  }
-
   /// <summary> Resizes custom FBO for Direct3D output. </summary>
-  bool ResizeBridgeFBO (int theWinSizeX,
-                        int theWinSizeY,
-                        System::IntPtr theColorSurf,
-                        System::IntPtr theColorSurfShare)
+  System::IntPtr ResizeBridgeFBO (int theWinSizeX,
+                                  int theWinSizeY)
   {
-    if (myBridgeFBO == NULL)
-    {
-      return false;
-    }
-
-    OpenGl_CView* aCView = reinterpret_cast<OpenGl_CView*> (reinterpret_cast<Graphic3d_CView*> (myView()->View()->CView())->ptrView);
-    Handle(OpenGl_Context) aGlContext = aCView->WS->GetGlContext();
-    if (aGlContext.IsNull()
-    || !aGlContext->MakeCurrent())
-    {
-      return false;
-    }
-
-    myBridgeFBO->Resize (aGlContext, theWinSizeX, theWinSizeY);
-    if (!myBridgeFBO->RegisterD3DColorBuffer (aGlContext, theColorSurf.ToPointer(), theColorSurfShare.ToPointer()))
-    {
-      return false;
-    }
-
-    myView()->Camera()->SetAspect (Standard_Real (theWinSizeX) / Standard_Real (theWinSizeY));
-    return true;
+    Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast (myView()->Window());
+    aWNTWindow->SetPos (0, 0, theWinSizeX, theWinSizeY);
+    myView()->MustBeResized();
+    myView()->Invalidate();
+    return System::IntPtr(Handle(D3DHost_View)::DownCast (myView()->View())->D3dColorSurface());
   }
 
   /// <summary>
@@ -203,6 +148,7 @@ public:
     if (!myView().IsNull())
     {
       myView()->SetComputedMode (Standard_False);
+      myView()->Redraw();
     }
   }
 
@@ -214,6 +160,7 @@ public:
     if (!myView().IsNull())
     {
       myView()->SetComputedMode (Standard_True);
+      myView()->Redraw();
     }
   }
 
@@ -235,7 +182,7 @@ public:
   /// <param name="theZoomFactor">Current zoom</param>
   void Place (int theX, int theY, float theZoomFactor)
   {    
-    Quantity_Factor aZoomFactor = theZoomFactor;
+    Standard_Real aZoomFactor = theZoomFactor;
     if (!myView().IsNull())
     {
       myView()->Place (theX, theY, aZoomFactor);
@@ -293,7 +240,7 @@ public:
   {
     if (!myAISContext().IsNull())
     {
-      myAISContext()->Select (theX1, theY1, theX2, theY2, myView());
+      myAISContext()->Select (theX1, theY1, theX2, theY2, myView(), Standard_True);
     }
   }
 
@@ -304,7 +251,7 @@ public:
   {
     if (!myAISContext().IsNull())
     {
-      myAISContext()->Select();
+      myAISContext()->Select (Standard_True);
     }
   }
 
@@ -315,7 +262,7 @@ public:
   {
     if (!myAISContext().IsNull() && !myView().IsNull())
     {
-      myAISContext()->MoveTo (theX, theY, myView());
+      myAISContext()->MoveTo (theX, theY, myView(), Standard_True);
     }
   }
 
@@ -326,7 +273,7 @@ public:
   {
     if (!myAISContext().IsNull() && !myView().IsNull())
     {
-      myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView());
+      myAISContext()->ShiftSelect (theX1, theY1, theX2, theY2, myView(), Standard_True);
     }
   }
 
@@ -337,7 +284,7 @@ public:
   {
     if (!myAISContext().IsNull())
     {
-      myAISContext()->ShiftSelect();
+      myAISContext()->ShiftSelect (Standard_True);
     }
   }
 
@@ -403,7 +350,7 @@ public:
   {
     if (!myView().IsNull())
     {
-      myView()->SetProj (V3d_Xpos);
+      myView()->SetProj (V3d_Yneg);
     }
   }
 
@@ -425,7 +372,7 @@ public:
   {
     if (!myView().IsNull())
     {
-      myView()->SetProj (V3d_Ypos);
+      myView()->SetProj (V3d_Xneg);
     }
   }
 
@@ -436,7 +383,7 @@ public:
   {
     if (!myView().IsNull())
     {
-      myView()->SetProj (V3d_Xneg);
+      myView()->SetProj (V3d_Ypos);
     }
   }
 
@@ -447,7 +394,7 @@ public:
   {
     if (!myView().IsNull())
     {
-      myView()->SetProj (V3d_Yneg);
+      myView()->SetProj (V3d_Xpos);
     }
   }
 
@@ -520,16 +467,15 @@ public:
     AIS_DisplayMode aCurrentMode = theMode == 0
                                  ? AIS_WireFrame
                                  : AIS_Shaded;
-    if (myAISContext()->NbCurrents() == 0
-     || myAISContext()->NbSelected() == 0)
+    if (myAISContext()->NbSelected() == 0)
     {
-       myAISContext()->SetDisplayMode (aCurrentMode);
+       myAISContext()->SetDisplayMode (aCurrentMode, Standard_False);
     }
     else
     {
-       for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
+       for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
        {
-         myAISContext()->SetDisplayMode (myAISContext()->Current(), theMode, Standard_False);
+         myAISContext()->SetDisplayMode (myAISContext()->SelectedInteractive(), theMode, Standard_False);
        }
     }
     myAISContext()->UpdateCurrentViewer();
@@ -546,10 +492,11 @@ public:
     }
 
     Quantity_Color aCol (theR / 255.0, theG / 255.0, theB / 255.0, Quantity_TOC_RGB);
-    for (; myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
+    for (; myAISContext()->MoreSelected(); myAISContext()->NextSelected())
     {
-      myAISContext()->SetColor (myAISContext()->Current(), aCol.Name());
+      myAISContext()->SetColor (myAISContext()->SelectedInteractive(), aCol, false);
     }
+    myAISContext()->UpdateCurrentViewer();
   }
 
   /// <summary>
@@ -595,16 +542,17 @@ public:
     theRed   = 255;
     theGreen = 255;
     theBlue  = 255;
-    myAISContext()->InitCurrent();
-    if (!myAISContext()->MoreCurrent())
+    myAISContext()->InitSelected();
+    if (!myAISContext()->MoreSelected())
     {
       return;
     }
 
-    Handle(AIS_InteractiveObject) aCurrent = myAISContext()->Current();
+    Handle(AIS_InteractiveObject) aCurrent = myAISContext()->SelectedInteractive();
     if (aCurrent->HasColor())
     {
-      Quantity_Color anObjCol = myAISContext()->Color (myAISContext()->Current());
+      Quantity_Color anObjCol;
+      myAISContext()->Color (aCurrent, anObjCol);
       theRed   = int(anObjCol.Red()   * 255.0);
       theGreen = int(anObjCol.Green() * 255.0);
       theBlue  = int(anObjCol.Blue()  * 255.0);
@@ -631,11 +579,9 @@ public:
     {
       return;
     }
-    for(myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
-    {
-      myAISContext()->Erase (myAISContext()->Current(), Standard_True);
-    }
-    myAISContext()->ClearCurrents();
+
+    myAISContext()->EraseSelected (Standard_False);
+    myAISContext()->ClearSelected (Standard_True);
   }
 
   /// <summary>
@@ -655,9 +601,9 @@ public:
     {
       return;
     }
-    for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
+    for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
     {
-      myAISContext()->SetMaterial (myAISContext()->Current(), (Graphic3d_NameOfMaterial )theMaterial);
+      myAISContext()->SetMaterial (myAISContext()->SelectedInteractive(), (Graphic3d_NameOfMaterial )theMaterial, Standard_False);
     }
     myAISContext()->UpdateCurrentViewer();
   }
@@ -671,10 +617,11 @@ public:
     {
       return;
     }
-    for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextSelected())
+    for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
     {
-      myAISContext()->SetTransparency (myAISContext()->Current(), ((Standard_Real )theTrans) / 10.0);
+      myAISContext()->SetTransparency (myAISContext()->Current(), ((Standard_Real )theTrans) / 10.0, Standard_False);
     }
+    myAISContext()->UpdateCurrentViewer();
   }
 
   /// <summary>
@@ -686,8 +633,8 @@ public:
     {
       return false;
     }
-    myAISContext()->InitCurrent();
-    return myAISContext()->MoreCurrent() != Standard_False;
+    myAISContext()->InitSelected();
+    return myAISContext()->MoreSelected() != Standard_False;
   }
 
   /// <summary>
@@ -702,13 +649,13 @@ public:
 
     bool isOneOrMoreInShading   = false;
     bool isOneOrMoreInWireframe = false;
-    for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
+    for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
     {
-      if (myAISContext()->IsDisplayed (myAISContext()->Current(), AIS_Shaded))
+      if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_Shaded))
       {
         isOneOrMoreInShading = true;
       }
-      if (myAISContext()->IsDisplayed (myAISContext()->Current(), AIS_WireFrame))
+      if (myAISContext()->IsDisplayed (myAISContext()->SelectedInteractive(), AIS_WireFrame))
       {
         isOneOrMoreInWireframe = true;
       }
@@ -730,33 +677,6 @@ public:
   }
 
   /// <summary>
-  ///Create new view
-  /// </summary>
-  /// <param name="theWnd">System.IntPtr that contains the window handle (HWND) of the control</param>
-  void CreateNewView (System::IntPtr theWnd)
-  {
-    if (myAISContext().IsNull())
-    {
-      return;
-    }
-
-    myView() = myAISContext()->CurrentViewer()->CreateView();
-    if (myGraphicDriver().IsNull())
-    {
-      myGraphicDriver() = new OpenGl_GraphicDriver (Handle(Aspect_DisplayConnection)());
-      //myGraphicDriver()->ChangeOptions().contextDebug = true;
-    }
-    Handle(WNT_Window) aWNTWindow = new WNT_Window (reinterpret_cast<HWND> (theWnd.ToPointer()));
-    myView()->SetWindow (aWNTWindow);
-    Standard_Integer aWidth = 100, aHeight = 100;
-    aWNTWindow->Size (aWidth, aHeight);
-    if (!aWNTWindow->IsMapped())
-    {
-      aWNTWindow->Map();
-    }
-  }
-
-  /// <summary>
   ///Set AISContext
   /// </summary>
   bool SetAISContext (OCCTProxyD3D^ theViewer)
@@ -772,7 +692,7 @@ public:
   /// <summary>
   ///Get AISContext
   /// </summary>
-  Handle_AIS_InteractiveContext GetContext()
+  Handle(AIS_InteractiveContext) GetContext()
   {
     return myAISContext();
   }
@@ -813,14 +733,11 @@ public:
     {
       return false;
     }
-    if (myAISContext()->HasOpenedContext())
-    {
-      myAISContext()->CloseLocalContext();
-    }
+
     Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
-    myAISContext()->SetMaterial   (aPrs, Graphic3d_NOM_GOLD);
+    myAISContext()->SetMaterial   (aPrs, Graphic3d_NOM_GOLD, Standard_False);
     myAISContext()->SetDisplayMode(aPrs, AIS_Shaded, Standard_False);
-    myAISContext()->Display (aPrs);
+    myAISContext()->Display (aPrs, Standard_True);
     return true;
   }
 
@@ -849,8 +766,9 @@ public:
       {
         for (int aShapeIter = 1; aShapeIter <= aNbShap; ++aShapeIter)
         {
-          myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_True);
+          myAISContext()->Display (new AIS_Shape (aReader.Shape (aShapeIter)), Standard_False);
         }
+        myAISContext()->UpdateCurrentViewer();
       }
     }
     return true;
@@ -881,13 +799,13 @@ public:
   /// <param name="theFileName">Name of export file</param>
   bool ExportBRep (char* theFileName)
   {
-    myAISContext()->InitCurrent();
-    if (!myAISContext()->MoreCurrent())
+    myAISContext()->InitSelected();
+    if (!myAISContext()->MoreSelected())
     {
       return false;
     }
 
-    Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
+    Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
     return !anIS.IsNull()
          && BRepTools::Write (anIS->Shape(), theFileName);
   }
@@ -900,9 +818,9 @@ public:
   {
     STEPControl_StepModelType aType = STEPControl_AsIs;
     STEPControl_Writer        aWriter;
-    for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
+    for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
     {
-      Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
+      Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
       if (anIS.IsNull())
       {
         return false;
@@ -926,9 +844,9 @@ public:
     IGESControl_Controller::Init();
     IGESControl_Writer aWriter (Interface_Static::CVal ("XSTEP.iges.unit"),
                                 Interface_Static::IVal ("XSTEP.iges.writebrep.mode"));
-    for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
+    for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
     {
-      Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
+      Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
       if (anIS.IsNull())
       {
         return false;
@@ -950,9 +868,9 @@ public:
     TopoDS_Compound aRes;
     BRep_Builder    aBuilder;
     aBuilder.MakeCompound (aRes);
-    for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
+    for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
     {
-      Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
+      Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
       if (anIS.IsNull())
       {
         return false;
@@ -974,9 +892,9 @@ public:
     TopoDS_Compound aComp;
     BRep_Builder    aBuilder;
     aBuilder.MakeCompound (aComp);
-    for (myAISContext()->InitCurrent(); myAISContext()->MoreCurrent(); myAISContext()->NextCurrent())
+    for (myAISContext()->InitSelected(); myAISContext()->MoreSelected(); myAISContext()->NextSelected())
     {
-      Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->Current());
+      Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
       if (anIS.IsNull())
       {
         return false;
@@ -1044,10 +962,9 @@ public:
 
 private:
 
-  NCollection_Haft<Handle_V3d_Viewer>             myViewer;
-  NCollection_Haft<Handle_V3d_View>               myView;
-  NCollection_Haft<Handle_AIS_InteractiveContext> myAISContext;
-  NCollection_Haft<Handle_OpenGl_GraphicDriver>   myGraphicDriver;
-  BridgeFBO*                                      myBridgeFBO;     //!< Provides output to Direct3D buffers
+  NCollection_Haft<Handle(V3d_Viewer)>             myViewer;
+  NCollection_Haft<Handle(V3d_View)>               myView;
+  NCollection_Haft<Handle(AIS_InteractiveContext)> myAISContext;
+  NCollection_Haft<Handle(D3DHost_GraphicDriver)>  myGraphicDriver;
 
 };