
OpenTK and WPF interoperation
Posted Wednesday, 9 September, 2009 - 15:28 by the Fiddler| Project: | The Open Toolkit library |
| Version: | 0.9.9-2b |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | in progress (review) |
Right now, it is possible to create an OpenTK.GLControl and host it in a WPF using a WindowsFormsHost.
The question is: can we create an OpenGL context directly on a WPF window and do away with the host control?
The attached solution creates a WPF solution, obtains its handle and creates a GraphicsContext. This works without errors, however OpenGL rendering results in artifacts. The msdn entry for the OnRender method, notes that:
The rendering instructions for this element are not used directly when this method is invoked, and are instead preserved for later asynchronous use by layout and drawing.
This explains why the rendering artifacts appear: OpenGL tries to render to the window directly and does not participate into WPF composition.
One *could* use offscreen OpenGL rendering, perform a readback and present the results into the WPF window, but this is inefficient. It would be nice to find a better solution or confirm that it cannot be done.
Any ideas?
| Attachment | Size |
|---|---|
| OpenTKWPF.zip | 1.81 MB |


Comments
#11
Yes, modern systems SwapBuffers is pretty fast on modern hardware (microseconds) and the ReadPixels call overshadows it completely.
Auxiliary buffers are a relic of the past, there's absolutely no reason to use them nowadays.
#12
Ok, so I will remove their use...
But what will be the best solution... I have also try to use FBO but I can't !
When I call :
Gl.glGenRenderbuffersEXT(1, out DepthRenderbuffer);
It crash !!
Also, how do I specify to OpenGL to draw on the FBO
Here is my code to create the FBO :
Can you help me to debug it ?
Thanks
#13
Do you get a crash or a NullReferenceException? The former indicates a driver bug, while the latter means that your video card does not support FBOs.
Have you checked for FBO support? Search the extension string for "EXT_framebuffer_object":
Gl.glGetString(Gl.GL_EXTENSIONS).Contains("EXT_framebuffer_object"). Many Intel cards don't support this (try updating your drivers).#14
Your're right, I have update my drivers and now all is fine...
Except that when I try to create the BitmapSource... the application is blocked !!!
Here is the code of the class, can you take a closer look... it sounds that the FBO is locked or something like this !
#15
Try moving the line
just after the ReadPixels call (before you create the BitmapSource).
#16
Thanks,
I have already try... but it is the same situation.
Even, without FBO it works like this... I have just added a call to "CreateFrameBuffer" !
#17
I am not familiar with WPF so I can't tell what's wrong here. Wild guess: BitmapSource expects to be created on the main thread.
A quick google search uncovered an interesting hack to modify an existing BitmapSource directly (instead of creating a System.Drawing.Bitmap). This should be much faster and could avoid the threading issues (you create the BitmapSource once in the main thread and modify its data as needed).
#18
I already use it in another version... it change nothing because we already the buffer pointer here.
#19
I have also try this (base on another sample about FBO) , but :
1) I can only create 2 PBOs
2) It display nothing ! and I don't know why !
To generate the PBO :
To generate the image :
#20
Another problem is that when I use glReadPixel the image is flipped vertically !
The OpenGl image is draw from the bottom to the top... and the XP image is from the top to the bottom.
Is there a way to change this ?