filmov
tv
C# WPF - Drawing using OpenGL

Показать описание
I'll show you how you can draw OpenGL into WPF. This method involves just copying the pixels from OpenGL to a WPF Image control with the help of a writable bitmap (which is an ImageSource, meaning Images accept it)
I've managed to get 60 FPS in the past with this method (although only rendering primitives shapes like triangles and squares), but the main limiting factor is how you manage rendering and copying pixels. In the past, I used a separate OpenGL rendering thread, and then created a CAS lock in order to "share" the OpenGL context between the WPF main thread and the OpenGL render thread. This allows you to essentially skip frames on the WPF side if the context is in use, which may or may not be what you want
I don't know any other methods for drawing OpenGL into WPF, apart from drawing directly into the window (by creating an OpenGL context around the actual window handle, which i haven't tried but i've heard people have problems with it, either due to pixel overlapping or because of the fact that an OpenGL context and whatever DirectX's equivalent is being on the same window)
Hope this video helped you! You could also draw into an FBO/Texture and copy from that into the WritableBitmap. Not sure if that unlocks any extra potential or maybe allowing you to skip the pixel copy, but maybe :)
I've managed to get 60 FPS in the past with this method (although only rendering primitives shapes like triangles and squares), but the main limiting factor is how you manage rendering and copying pixels. In the past, I used a separate OpenGL rendering thread, and then created a CAS lock in order to "share" the OpenGL context between the WPF main thread and the OpenGL render thread. This allows you to essentially skip frames on the WPF side if the context is in use, which may or may not be what you want
I don't know any other methods for drawing OpenGL into WPF, apart from drawing directly into the window (by creating an OpenGL context around the actual window handle, which i haven't tried but i've heard people have problems with it, either due to pixel overlapping or because of the fact that an OpenGL context and whatever DirectX's equivalent is being on the same window)
Hope this video helped you! You could also draw into an FBO/Texture and copy from that into the WritableBitmap. Not sure if that unlocks any extra potential or maybe allowing you to skip the pixel copy, but maybe :)
Комментарии