I've been playing around with OpenTK and it's been great following along the examples/tutorials.
I really want to have the OpenGL render in a smaller portion of the Window so I've been trying to use GLWpfControl, and I want to do it without XAML but I can't get it running properly.
I've been trying to get it to run through dotnet CLI from a console template. Here is my code:
using System;
using System.Windows;
using OpenTK.Wpf;
using OpenTK.Graphics.OpenGL;
namespace Test {
public class MyWindow : Window {
[STAThread]
public static void Main() {
Application app = new Application();
app.Run(new MyWindow());
}
public MyWindow() {
GLWpfControl wpfControl = new GLWpfControl();
wpfControl.Render += GLWpfOnRender;
this.Content = wpfControl;
var settings = new GLWpfControlSettings();
wpfControl.Start(settings);
}
void GLWpfOnRender(TimeSpan delta) {
Console.WriteLine(delta.TotalMilliseconds);
GL.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
}
}
}
I'm expecting the control to be rendered with a red color, however it is black. It isn't black without the call to Start() and GLWpfOnRender is being called repeatedly after the call to Start().
Here is the .csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<RootNamespace>test_2</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenTK" Version="4.7.4" />
<PackageReference Include="OpenTK.GLWpfControl" Version="4.2.2" />
</ItemGroup>
</Project>
I think I'm missing something that might be done when you call InitializeComponent() while using XAML. I've been doing a lot of reading but I'm stumped.
here's a link to what I'm trying to replicate: https://github.com/opentk/GLWpfControl/tree/aefe945f65e4659bd6f9f82083a4a746c2fb03fc/src
Edit: I've inherited and overriden GLWpfControl, and able to use the DrawingContext to print text and do all sorts of stuff. So GL is not working with the control for whatever reason?
Okay I got it working. Turns out there are some known issues with two graphics cards / Intel HD graphics. I had my GPU disabled. Enabled, it works fine. My own fault for not checking issues tab on github in the first place.