On Tue, Jan 25, 2011 at 11:39 AM, Chris Corn <[email protected]>wrote:
> Hi,
> It's been 3 days that I'm trying to integrate Osg inside a Wpf Application.
> I would like to integrate an osgviewer using a class that extends HwndHost
> (defined in System.Windows.Runtime.InteropServices) as shown in an article
> attached to a Forum Topic about WPF (see Ref)
>
> Rapidly: I wrote the BuildWindowCore implementation but I'm getting a
> SEHException every time that I try to create the Graphic Context:
>
>
> Code:
>
> virtual HandleRef BuildWindowCore(HandleRef hwndParent) override {
>
> RECT DrawingCanvas;
> HWND WindowHandle =
> CreateWindowEx(0,_className, _windowName, _dwStyle, 0, 0, 100, 100, (HWND)
> hwndParent.Handle.ToPointer(), 0, _hInstance, 0 );
>
> GetWindowRect(WindowHandle, &DrawingCanvas);
>
> osg::ref_ptr<osg::Referenced> windata = new
> osgViewer::GraphicsWindowWin32::WindowData(WindowHandle);
>
> osg::ref_ptr<osg::GraphicsContext::Traits> traits = new
> osg::GraphicsContext::Traits;
>
> // Setup Graphic Context
> traits->x = 0;
> traits->y = 0;
> traits->width = DrawingCanvas.right - DrawingCanvas.left;
> traits->height = DrawingCanvas.bottom - DrawingCanvas.top;
> traits->windowDecoration = false;
> traits->doubleBuffer = true;
> traits->sharedContext = 0;
> traits->inheritedWindowData = windata;
>
> // Pixel Format Descriptor
> PIXELFORMATDESCRIPTOR pixelFormat = {
> sizeof(PIXELFORMATDESCRIPTOR), 1,
> PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
> PFD_TYPE_RGBA, 32,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> 24, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0
> };
>
> // Get the device context for the Win32 handle to the control
> HDC hdc = ::GetDC(WindowHandle);
>
> // Set the pixel format on the device context
> int pixelFormatIndex = ::ChoosePixelFormat(hdc, &pixelFormat);
>
> ::SetPixelFormat(hdc, pixelFormatIndex, &pixelFormat)
>
> // Create an Open Scene Graph graphics context
> osg::ref_ptr<osg::GraphicsContext> gc =
> osg::GraphicsContext::createGraphicsContext(traits);
>
> return HandleRef(this, IntPtr(WindowHandle));
> }
>
>
>
>
> Do you have some ideas on how solve my problem?
> Am I doing something wrong?
>
> Thank you!
> Have a nice day!,
> Chris
>
> Ref: forum openscenegraph, viewtopic with t=3279
> (sorry but I cant submit links now)
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=35946#35946
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
Hi Chris,
Well, we have a running WPF based application that includes an osg viewer.
Here is how we did that : (i am no WPF coder, so i can't be very specific
about it) :
- in the WPF side, we created a subclass of System.Windows.Window.
In Window_Loaded method, we get a handle of the window like this :
IntPtr winPtr = new WindowInteropHelper(this).Handle;
this handle will be used to initialize the osg viewer.
- We created a simple wrapper dll with a simple C interface that manages an
osg viewer.
our C interface has create/destroy/frame/loadmodel methods.
- this wrapper dll is loaded in the WPF application with a DllImport
statement per method in the C interface.
- back to the wpf side, in Window_Loaded method, we pass the handle of the
window to the create function in the wrapper dll. This function creates an
osg graphic context as you already have and create an osgViewer object using
that context.
- Still in the wpf side, we have a Paint method in which we call the frame
method of the wrapper dll that simply call the frame method of the viewer.
Make sure to add this to the ctor of the window :
CompositionTarget.Rendering += Paint;
I hope this will help you.
Mourad
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org