1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-14 13:30:48 +03:00

0026392: Visualization, TKD3DHost - provide straight-forward base for integration of TKOpenGl viewer into D3D-based application

D3DHost_GraphicDriver - new graphic driver providing D3D host for OpenGL workspace.
WNT_Window - handle virtual dimensions within virtual window.
OpenGl_FrameBuffer::Init() - add protection against 0 dimensions.
V3d_View::IsInvalidated() - add method to check view cache validation state.

ViewerTest::ViewerInit() - create virtual window without decorations on Windows.
This commit is contained in:
kgv
2015-07-20 11:08:12 +03:00
committed by bugmaster
parent 380eaf77b9
commit 62e1beed96
38 changed files with 1318 additions and 751 deletions

View File

@@ -1,152 +0,0 @@
#include "BridgeFBO.hxx"
#include <OpenGl_ArbFBO.hxx>
#include <Graphic3d_TextureParams.hxx>
#include <d3dx9.h>
// list of required libraries
#pragma comment(lib, "TKOpenGl.lib")
#pragma comment(lib, "opengl32.lib")
// =======================================================================
// function : Init
// purpose :
// =======================================================================
Standard_Boolean BridgeFBO::Init (const Handle(OpenGl_Context)& theGlContext,
void* theD3DDevice)
{
const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
if (aFuncs->wglDXOpenDeviceNV == NULL)
{
return Standard_False;
}
myGlD3DHandle = aFuncs->wglDXOpenDeviceNV (theD3DDevice);
if (myGlD3DHandle == NULL)
{
std::cerr << "Could not create the GL <-> DirectX Interop" << std::endl;
return Standard_False;
}
theGlContext->arbFBO->glGenFramebuffers (1, &myGlFBufferId);
return Standard_True;
}
// =======================================================================
// function : Release
// purpose :
// =======================================================================
void BridgeFBO::Release (OpenGl_Context* theGlContext)
{
if (myGlD3DHandle != NULL)
{
const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
if (myGlD3DSharedColorHandle != NULL)
{
aFuncs->wglDXUnregisterObjectNV (myGlD3DHandle, myGlD3DSharedColorHandle);
myGlD3DSharedColorHandle = NULL;
}
aFuncs->wglDXCloseDeviceNV (myGlD3DHandle);
myGlD3DHandle = NULL;
}
OpenGl_FrameBuffer::Release (theGlContext);
}
// =======================================================================
// function : RegisterD3DColorBuffer
// purpose :
// =======================================================================
Standard_Boolean BridgeFBO::RegisterD3DColorBuffer (const Handle(OpenGl_Context)& theGlContext,
void* theD3DBuffer,
void* theBufferShare)
{
const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
if (myGlD3DSharedColorHandle != NULL)
{
if (!aFuncs->wglDXUnregisterObjectNV (myGlD3DHandle, myGlD3DSharedColorHandle))
{
return Standard_False;
}
myGlD3DSharedColorHandle = NULL;
}
if (!aFuncs->wglDXSetResourceShareHandleNV (theD3DBuffer, theBufferShare))
{
return Standard_False;
}
myColorTexture->Release (theGlContext.operator->());
myColorTexture->Create (theGlContext);
myGlD3DSharedColorHandle = aFuncs->wglDXRegisterObjectNV (myGlD3DHandle,
theD3DBuffer, myColorTexture->TextureId(), GL_TEXTURE_2D, WGL_ACCESS_WRITE_DISCARD_NV);
if (myGlD3DSharedColorHandle == NULL)
{
std::cerr << "Could not register color buffer" << std::endl;
return Standard_False;
}
return Standard_True;
}
// =======================================================================
// function : Resize
// purpose :
// =======================================================================
void BridgeFBO::Resize (const Handle(OpenGl_Context)& theGlContext,
int theSizeX,
int theSizeY)
{
myVPSizeX = theSizeX;
myVPSizeY = theSizeY;
myDepthStencilTexture->Init (theGlContext, GL_DEPTH24_STENCIL8,
GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
myVPSizeX, myVPSizeY, Graphic3d_TOT_2D);
}
// =======================================================================
// function : BindBuffer
// purpose :
// =======================================================================
void BridgeFBO::BindBuffer (const Handle(OpenGl_Context)& theGlContext)
{
if (myGlD3DSharedColorHandle != NULL)
{
// Lock for OpenGL
const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
aFuncs->wglDXLockObjectsNV (myGlD3DHandle, 1, &myGlD3DSharedColorHandle);
}
OpenGl_FrameBuffer::BindBuffer (theGlContext);
theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, myColorTexture->TextureId(), 0);
theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_TEXTURE_2D, myDepthStencilTexture->TextureId(), 0);
if (theGlContext->arbFBO->glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
std::cerr << "Invalid FBO can not be bound!\n";
Release (theGlContext.operator->());
}
}
// =======================================================================
// function : UnbindBuffer
// purpose :
// =======================================================================
void BridgeFBO::UnbindBuffer (const Handle(OpenGl_Context)& theGlContext)
{
// Unbind the frame buffer
OpenGl_FrameBuffer::UnbindBuffer (theGlContext);
if (myGlD3DSharedColorHandle != NULL)
{
// Unlock for Direct3D
const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
aFuncs->wglDXUnlockObjectsNV (myGlD3DHandle, 1, &myGlD3DSharedColorHandle);
}
}

View File

@@ -1,53 +0,0 @@
#ifndef BRIDGE_FBO_HEADER
#define BRIDGE_FBO_HEADER
#include <OpenGl_GlCore20.hxx>
#include <OpenGl_FrameBuffer.hxx>
//! Implements bridge FBO for direct rendering to Direct3D surfaces.
class BridgeFBO : public OpenGl_FrameBuffer
{
public:
//! Creates new bridge FBO.
BridgeFBO() : OpenGl_FrameBuffer()
{
//
}
//! Releases resources of bridge FBO.
~BridgeFBO()
{
Release (NULL);
}
//! Releases resources of bridge FBO.
virtual void Release (OpenGl_Context* theGlContext);
//! Initializes OpenGL FBO for Direct3D interoperability.
Standard_Boolean Init (const Handle(OpenGl_Context)& theGlContext,
void* theD3DDevice);
//! Binds Direcr3D color buffer to OpenGL texture.
Standard_Boolean RegisterD3DColorBuffer (const Handle(OpenGl_Context)& theGlContext,
void* theD3DBuffer,
void* theBufferShare);
//! Locks Direct3D objects for OpenGL drawing.
virtual void BindBuffer (const Handle(OpenGl_Context)& theGlContext);
//! Unlocks Direct3D objects after OpenGL drawing.
virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlContext);
//! Resizes buffer according to Direct3D surfaces.
void Resize (const Handle(OpenGl_Context)& theGlContext,
int theSizeX,
int theSizeY);
private:
HANDLE myGlD3DHandle;
HANDLE myGlD3DSharedColorHandle;
};
#endif

View File

@@ -1,278 +0,0 @@
#include <iostream>
#include <windows.h>
#include <d3dx9.h>
using namespace System::Runtime::InteropServices;
typedef HRESULT (WINAPI *DIRECT3DCREATE9EX)(UINT SDKVersion, IDirect3D9Ex**);
LRESULT WINAPI MsgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProcW (hWnd, msg, wParam, lParam);
}
WNDCLASSEXW THE_WNDCLASS_D3D =
{
sizeof (WNDCLASSEXW),
CS_CLASSDC, MsgProc, NULL, NULL,
GetModuleHandle (NULL),
NULL, NULL, NULL, NULL, L"D3D", NULL
};
// =======================================================================
// function : GetVertexProcessingCaps
// purpose :
// =======================================================================
DWORD GetVertexProcessingCaps (LPDIRECT3D9 theD3D)
{
D3DCAPS9 aCaps;
return !SUCCEEDED (theD3D->GetDeviceCaps (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &aCaps))
|| !(aCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
? D3DCREATE_SOFTWARE_VERTEXPROCESSING
: D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
//! Encapsulates state of Direct3D renderer.
class D3DRender
{
public:
D3DRender (int theSizeX = 512,
int theSizeY = 512)
: D3D (NULL),
D3DEx (NULL),
D3DDevice (NULL),
D3DDeviceEx (NULL),
D3DColorSurf (NULL),
D3DColorSurfShare (NULL),
FuncCreate9Ex (NULL),
WinSizeX (theSizeX),
WinSizeY (theSizeY)
{
CheckRegisterClass();
WinHandle = CreateWindowW (L"D3D", L"D3D",
WS_OVERLAPPEDWINDOW, 0, 0, 1, 1,
NULL, NULL, THE_WNDCLASS_D3D.hInstance, NULL);
Init();
}
~D3DRender()
{
// do not release class instance shared between all renderers
//UnregisterClass (NULL, THE_WNDCLASS_D3D.hInstance);
if (D3DDevice != NULL)
{
D3DDevice->Release();
}
if (D3DDeviceEx != NULL)
{
D3DDeviceEx->Release();
}
if (D3D != NULL)
{
D3D->Release();
}
if (D3DEx != NULL)
{
D3DEx->Release();
}
if (D3DColorSurf != NULL)
{
D3DColorSurf->Release();
D3DColorSurfShare = NULL;
}
}
//! Creates Direct3D render target.
bool CreateRenderTarget()
{
if (!SetWindowPos (WinHandle, 0, 0, 0, WinSizeX, WinSizeY, 0))
{
return false;
}
if (D3DColorSurf != NULL)
{
D3DColorSurf->Release();
D3DColorSurfShare = NULL;
}
// Note: Render target surface should be lockable on
// Windows XP and non-lockable on Windows Vista or higher
if (FAILED (D3DDevice->CreateRenderTarget (WinSizeX, WinSizeY,
D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, FuncCreate9Ex != NULL,
&D3DColorSurf, FuncCreate9Ex == NULL ? NULL : &D3DColorSurfShare)))
{
return false;
}
D3DDevice->SetRenderTarget (0, D3DColorSurf);
return true;
}
private:
void Init()
{
// Vista requires the D3D "Ex" functions for optimal performance.
// The Ex functions are only supported with WDDM drivers, so they will not be available on XP.
HMODULE aD3D9 = GetModuleHandleW (L"d3d9");
FuncCreate9Ex = (DIRECT3DCREATE9EX )GetProcAddress (aD3D9, "Direct3DCreate9Ex");
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS aParams;
ZeroMemory (&aParams, sizeof(aParams));
aParams.Windowed = TRUE;
aParams.BackBufferHeight = 1;
aParams.BackBufferWidth = 1;
aParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
aParams.BackBufferFormat = D3DFMT_X8R8G8B8;
(FuncCreate9Ex != NULL) ? InitializeD3DEx (aParams) : InitializeD3D (aParams);
}
bool InitializeD3D (D3DPRESENT_PARAMETERS theParams)
{
D3D = Direct3DCreate9 (D3D_SDK_VERSION);
if (D3D == NULL)
return false;
DWORD aVertProcessCaps = GetVertexProcessingCaps (D3D);
HRESULT aResult = D3D->CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, WinHandle,
aVertProcessCaps | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
&theParams, &D3DDevice);
return SUCCEEDED (aResult);
}
bool InitializeD3DEx (D3DPRESENT_PARAMETERS theParams)
{
if (FAILED (FuncCreate9Ex (D3D_SDK_VERSION, &D3DEx))
|| FAILED (D3DEx->QueryInterface (__uuidof (IDirect3D9), reinterpret_cast<void**> (&D3D))))
{
return false;
}
DWORD aVertProcessCaps = GetVertexProcessingCaps (D3D);
if (FAILED (D3DEx->CreateDeviceEx (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, WinHandle,
aVertProcessCaps | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
&theParams, NULL, &D3DDeviceEx)))
return false;
return SUCCEEDED (D3DDeviceEx->QueryInterface (__uuidof (IDirect3DDevice9), reinterpret_cast<void**> (&D3DDevice)));
}
static void CheckRegisterClass()
{
static bool isRegistered = false;
if (!isRegistered)
{
RegisterClassExW (&THE_WNDCLASS_D3D);
isRegistered = true;
}
}
public:
LPDIRECT3D9 D3D;
LPDIRECT3D9EX D3DEx;
LPDIRECT3DDEVICE9 D3DDevice;
LPDIRECT3DDEVICE9EX D3DDeviceEx;
LPDIRECT3DSURFACE9 D3DColorSurf;
HANDLE D3DColorSurfShare;
DIRECT3DCREATE9EX FuncCreate9Ex;
HWND WinHandle;
INT WinSizeX;
INT WinSizeY;
};
public ref struct WndSize
{
public:
WndSize(int theSizeX, int theSizeY)
{
mySize = new SIZE();
mySize->cx = theSizeX;
mySize->cy = theSizeY;
}
~WndSize()
{
delete mySize;
}
property int cx
{
int get() { return mySize->cx; }
}
property int cy
{
int get() { return mySize->cy; }
}
LPSIZE GetPointer()
{
return mySize;
}
private:
LPSIZE mySize;
};
public ref class Direct3DProxy
{
public :
Direct3DProxy()
{
//
}
// =======================================================================
// function : InitializeScene
// purpose :
// =======================================================================
static System::IntPtr InitRender ([Out] System::IntPtr% theWinHandle,
[Out] System::IntPtr% theD3DDevice)
{
D3DRender* aRender = new D3DRender();
theWinHandle = System::IntPtr(aRender->WinHandle);
theD3DDevice = System::IntPtr(aRender->D3DDevice);
return System::IntPtr(aRender);
}
// =======================================================================
// function : ResizeWindow
// purpose :
// =======================================================================
static void ResizeWindow ([In] System::IntPtr% theRender,
[In] WndSize^% theWndSize,
[Out] System::IntPtr% theColorSurf,
[Out] System::IntPtr% theColorSurfShare)
{
D3DRender* aRender = reinterpret_cast<D3DRender*> (theRender.ToPointer());
LPSIZE aSize = theWndSize->GetPointer();
aRender->WinSizeX = aSize->cx;
aRender->WinSizeY = aSize->cy;
aRender->CreateRenderTarget();
theColorSurf = System::IntPtr(aRender->D3DColorSurf);
theColorSurfShare = System::IntPtr(aRender->D3DColorSurfShare);
}
// =======================================================================
// function : ReleaseRender
// purpose :
// =======================================================================
static void ReleaseRender ([In] System::IntPtr% theRender)
{
D3DRender* aRender = reinterpret_cast<D3DRender*> (theRender.ToPointer());
delete aRender;
}
};

View File

@@ -1,16 +1,15 @@
#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>
//for object display
#include <V3d_Viewer.hxx>
#include <V3d_View.hxx>
@@ -47,11 +46,14 @@
#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,9 +72,10 @@ 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");
@@ -87,76 +87,26 @@ public:
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(myGraphicDriver()->D3dColorSurface (myView()->View()));
}
/// <summary>
@@ -729,33 +679,6 @@ public:
return -1;
}
/// <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>
@@ -1047,7 +970,6 @@ 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_D3DHost_GraphicDriver> myGraphicDriver;
};

View File

@@ -348,10 +348,6 @@
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\BridgeFBO.cxx"
>
</File>
<File
RelativePath=".\OCCTProxyD3D.cpp"
>
@@ -362,10 +358,6 @@
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\BridgeFBO.hxx"
>
</File>
</Filter>
<Filter
Name="Resource Files"

View File

@@ -190,13 +190,8 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="BridgeFBO.cxx" />
<ClCompile Include="Direct3DProxy.cpp" />
<ClCompile Include="OCCTProxyD3D.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="BridgeFBO.hxx" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@@ -11,9 +11,6 @@ namespace IE_WPF_D3D
/// </summary>
class D3DViewer
{
/// <summary> Direct3D renderer. </summary>
private IntPtr myD3DRender;
/// <summary> Direct3D output image. </summary>
private D3DImage myD3DImage = new D3DImage ();
@@ -60,24 +57,14 @@ namespace IE_WPF_D3D
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))
if (!Viewer.InitViewer())
{
MessageBox.Show ("Failed to initialize OpenGL-Direct3D interoperability!",
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
myIsFailed = true;
if (myD3DRender != IntPtr.Zero)
Direct3DProxy.ReleaseRender(ref myD3DRender);
return;
}
@@ -87,32 +74,28 @@ namespace IE_WPF_D3D
}
}
/// <summary> Initializes Direct3D-OCCT rendering. </summary>
/// <summary> Releases 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> Initializes Direct3D-OCCT rendering. </summary>
/// <summary> Performs Direct3D-OCCT rendering. </summary>
private void OnRendering (object sender, EventArgs e)
{
UpdateScene ();
}
/// <summary> Current size of rendering window. </summary>
private WndSize mySize = new WndSize(1, 1);
/// <summary> Initializes Direct3D-OCCT rendering. </summary>
/// <summary> Performs Direct3D-OCCT rendering. </summary>
private void UpdateScene ()
{
if (!myIsFailed && myD3DImage.IsFrontBufferAvailable && myColorSurf != IntPtr.Zero)
if (!myIsFailed
&& myD3DImage.IsFrontBufferAvailable
&& myColorSurf != IntPtr.Zero
&& (myD3DImage.PixelWidth != 0 && myD3DImage.PixelHeight != 0))
{
myD3DImage.Lock ();
{
@@ -120,7 +103,7 @@ namespace IE_WPF_D3D
Viewer.View.RedrawView ();
// Invalidate the updated region of the D3DImage
myD3DImage.AddDirtyRect(new Int32Rect(0, 0, mySize.cx, mySize.cy));
myD3DImage.AddDirtyRect(new Int32Rect(0, 0, myD3DImage.PixelWidth, myD3DImage.PixelHeight));
}
myD3DImage.Unlock ();
}
@@ -129,23 +112,16 @@ namespace IE_WPF_D3D
/// <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, IntPtr.Zero);
myColorSurf = Viewer.View.ResizeBridgeFBO (theSizeX, theSizeY);
myD3DImage.SetBackBuffer (D3DResourceType.IDirect3DSurface9, myColorSurf);
}
myD3DImage.Unlock ();
Viewer.View.ResizeBridgeFBO(mySize.cx, mySize.cy, myColorSurf, aColorSurfShare);
}
}

View File

@@ -161,7 +161,7 @@ namespace IE_WPF_D3D
ImageBrush anImage = new ImageBrush (aViwer.Image);
anImage.RelativeTransform = new ScaleTransform (1.0, -1.0, 0.5, 0.5);
//anImage.RelativeTransform = new ScaleTransform (1.0, -1.0, 0.5, 0.5);
g.Background = anImage;
g.MouseMove += new MouseEventHandler (g_MouseMove);

View File

@@ -87,9 +87,9 @@ namespace IE_WPF_D3D
DegenerateMode = true;
}
public bool InitInterop (IntPtr theHWND, IntPtr theD3DDevice)
public bool InitViewer()
{
return View.InitViewer (theHWND, theD3DDevice);
return View.InitViewer();
}
public void ImportModel (ModelFormat theFormat)

View File

@@ -1,12 +1,18 @@
@echo off
if NOT DEFINED DXSDK_DIR (
echo ERROR: DirectX SDK is required in order to build the sample but it is not found in your system. Please install DirectX SDK and retry.
exit /B
)
call "%~dp0..\..\env.bat" %1 %2 %3
if NOT DEFINED DXSDK_DIR (
if "%VCVER%" == "vc9" (
echo ERROR: DirectX SDK is required in order to build the sample but it is not found in your system. Please install DirectX SDK and retry.
exit /B
)
if "%VCVER%" == "vc10" (
echo ERROR: DirectX SDK is required in order to build the sample but it is not found in your system. Please install DirectX SDK and retry.
exit /B
)
)
if ["%CASDEB%"] == [""] (
call "%~dp0..\..\msvc.bat" %VCVER% win%ARCH% Release %~dp0\CSharp_D3D.sln
) else (