/// <summary> Initializes Direct3D-OCCT rendering. </summary>
        public void StopRenderingScene()
        {
            // This method is called when WPF loses its Direct3D device,
            // so we should just release our custom Direct3D scene
            CompositionTarget.Rendering -= OnRendering;

            if (myD3DRender != IntPtr.Zero)
            {
                Direct3DProxy.ReleaseRender(ref myD3DRender);
            }

            myColorSurf = IntPtr.Zero;
        }
        /// <summary> Resizes Direct3D surfaces and OpenGL FBO. </summary>
        public void Resize(int theSizeX, int theSizeY)
        {
            mySize = new WndSize(theSizeX, theSizeY);

            if (!myIsFailed && myD3DImage.IsFrontBufferAvailable)
            {
                IntPtr aColorSurfShare;

                // Initialize Direct3D device and render target
                Direct3DProxy.ResizeWindow(ref myD3DRender, ref mySize, out myColorSurf, out aColorSurfShare);

                // Set the back buffer for Direct3D WPF image
                myD3DImage.Lock();
                {
                    myD3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, myColorSurf);
                }
                myD3DImage.Unlock();

                Viewer.View.ResizeBridgeFBO(mySize.cx, mySize.cy, myColorSurf, aColorSurfShare);
            }
        }
        /// <summary> Initializes Direct3D-OCCT rendering. </summary>
        private void BeginRenderingScene()
        {
            if (myIsFailed)
            {
                return;
            }

            if (myD3DImage.IsFrontBufferAvailable)
            {
                IntPtr aWinHandle;
                IntPtr aD3DDevice;

                // Initialize Direct3D device and render target
                myD3DRender = Direct3DProxy.InitRender(out aWinHandle, out aD3DDevice);

                Viewer = new OCCViewer();

                if (!Viewer.InitInterop(aWinHandle, aD3DDevice))
                {
                    MessageBox.Show("Failed to initialize OpenGL-Direct3D interoperability!",
                                    "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                    myIsFailed = true;

                    if (myD3DRender != IntPtr.Zero)
                    {
                        Direct3DProxy.ReleaseRender(ref myD3DRender);
                    }

                    return;
                }

                // Leverage the Rendering event of WPF composition
                // target to update the our custom Direct3D scene
                CompositionTarget.Rendering += OnRendering;
            }
        }