C# WPF - Drawing using OpenGL

preview_player
Показать описание
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 :)
Рекомендации по теме
Комментарии
Автор

If anyone ever experiences extreme lag using this, It might actually be the focus visual. The standard focus visual uses dash arrays which are really render intensive, and it causes the render thread to be blocked for a short time, and the WriteableBitmap's Lock function requires the render thread to make some sort of copy of the pixels (or something along those lines)

kettlesimulator
Автор

OpenTK is a good choice, I also went for that, but I didnt use Gamewindow, instead rendered into a usercontrol within a native wpf window.

ParaparaPartycrow-san
Автор

i want openGL draws my wpf application.(because i want open it on a headless linux). can it be?

alideveloper
welcome to shbcf.ru