I am currently trying to draw into a GLArea of Gtk#. Gtk# sadly doesn't come with the GL functions and only provides context creation.
I am now trying for days to get the latest prerelease of OpenTK (4.0.0-pre.10) work with Gtk#'s GLArea.
using Gtk;
using OpenToolkit.Graphics.OpenGL4;
using OpenToolkit.Windowing.GraphicsLibraryFramework;
namespace Test
{
class Viewport : GLArea
{
protected override bool OnRender(Gdk.GLContext context)
{
GL.ClearColor(0.3f, 0.3f, 0.3f, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
return true;
}
}
class MainWindow : Gtk.Window
{
public MainWindow() : base(WindowType.Toplevel)
{
this.Add(new Viewport());
}
}
class Program
{
static void Main(string[] args)
{
GLFW.Init();
GL.LoadBindings(new GLFWBindingsContext());
Application.Init();
var window = new MainWindow();
window.ShowAll();
Application.Run();
}
}
}
The problem is that the GL functions seem to not get loaded, the first attempt to call one of them gives a System.AccessViolationException.
I couldn't really find any information on how to initialize the OpenTK 4.0 bindings. OpenTK 3 had OpenTK.Toolkit.Init()
which seems to be exactly what i need.
Documentation for OpenTK.Toolkit.Init:
"You must call this method if you are combining OpenTK with a third-party windowing toolkit (e.g. GTK#). In this case, this should be the first method called by your application"
I can't find this function or something similar in version 4. It seems like it just got removed.
I can't use OpenTK 3 either because it lacks support for .Net Core. I get everything to work by creating a OpenTK dummy window and instantly throw it away. That initializes the GL.* functions but you see the second window opening and closing for a second. I really don't want to use workarounds like that.
Does anyone have any insight on how to properly initialize the OpenTK 4.0 bindings? I mean, there has to be a way, the bindings would essentially be useless for a lot of use cases otherwise.
First of all using the GLFW bindings requires an active Opengl Context created by it and seconds of all you might want to use some other bindings context, sadly they provide only the one for GLFW because that's what they chose to use for windowing.
I have made a bindings context that works for Linux and with the help of someone I made something that will work on both windows too. You can grab it from here https://gist.github.com/NepNet/e4e75defb3748f76f10302b585b9839b. I suggest loading the bindings in the OnRealized event of the GLArea and make it's context current if you use the NativeBindingsContext because OpenGL on windows requires an active context for it to return valid pointers the other 2 files are required by it but if you want to target only one platform you can use only the glx context for linux and wgl for windows
edited:
You can load the bindings before starting the gtk app because glx doesn't require an active context to return the function pointers but I sugest loading them in the OnRealized event of the GLArea with an optional static boolean to not load them more than once and If you want to do GL calls in the onrealized I suggest to make the context of the area curent and as for windows I don't have a solution yet, tried with 'opengl32.dll' and wglGetProcAddress and seems to return 0 most of the time even with an active context