A native control for WPF in OpenTK 3.x and 4.x.
Supported configurations:
Since version 3.0.0, we're using full OpenGL/DirectX interop via OpenGL extensions - NV_DX_interop. This should run almost everywhere with AMAZING PERFORMANCE and is fully supported on Intel, AMD and Nvidia graphics.
This offers a way more clean solution than embedding GLControl and totally solves the airspace problem. As controls can be layered, nested and structured over your 3D view.
<Window
...
xmlns:glWpfControl="clr-namespace:OpenTK.Wpf;assembly=GLWpfControl"
... >
Add the control into a container (Grid, StackPanel, etc.) and add a handler method to the render event.
<Grid>
...
<glWpfControl:GLWpfControl
x:Name="OpenTkControl"
Render="OpenTkControl_OnRender"/>
...
</Grid>
public MainWindow() {
InitializeComponent();
// [...]
var settings = new GLWpfControlSettings
{
MajorVersion = 3,
MinorVersion = 6
};
OpenTkControl.Start(settings);
}
private void OpenTkControl_OnRender(TimeSpan delta) {
GL.ClearColor(Color4.Blue);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
}
For additional examples, see MainWindow.xaml and MainWindow.xaml.cs in the example project.
WPF by design only sends keyboard events to the control that has keybaord focus. To be able to get keyboard focus a control needs to have Focusable==true
(this is the default for GLWpfControl) and IsVisible==true
.
If you however need to get keyboard events idependent of keyboard focus you will have to use the Keyboard.AddPreview*
functions.
These functions allow you to register a preview event that is called before the control with keyboard focus gets the keyboard event.
This replaces the old CanInvokeOnHandledEvents
and RegisterToEventsDirectly
settings.
See Example for an example how to set this up.
$ git clone https://github.com/varon/GLWpfControl.git
$ cd GLWpfControl
or for SSH
$ git clone git@github.com:varon/GLWpfControl.git
$ cd GLWpfControl
build.cmd
or build.sh
.It's possible to bypass the RTT that takes place in WPF D3dImage by stealing the actual D3d handle from WPF and drawing manually. This is incredibly challenging, but would offer APEX performance as zero indirection is required. Currently more of an idea than a work in progress. Contributions welcome - Drop by the Discord server if you want to give this a shot!