Can I make OpenTK and Tao.FreeGlut work together?

1.9k views Asked by At

I'm trying to init window with Tao.FreeGlut 2.1.0 and drawind with OpenTK 1.1, so I make simple program:

public static void Main()
{
    Glut.glutInit();
    Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_RGBA);
    Glut.glutInitWindowSize(1024, 768);
    Glut.glutInitWindowPosition(100, 100);
    Glut.glutCreateWindow("Test");
    Glut.glutDisplayFunc(RenderScene);

    GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    Glut.glutMainLoop();
}

public static void RenderScene()
{
    GL.Clear(ClearBufferMask.ColorBufferBit);
    Glut.glutSwapBuffers();
}

But on GL.ClearColor call it crashes with null ref exception. I'm think, this happens, because OpenTK stores it's own OpenGL context, and it not equal to context created by Glut and not properly initializated. I not found any way to get context from Glut, then I tried do something like:

IWindowInfo window = Utilities.CreateWindowsWindowInfo(Glut.glutGetWindowData());
GraphicsContext context = new GraphicsContext(GraphicsMode.Default, window);
context.MakeCurrent(window);
context.LoadAll();

And now it crashes on context creation, anyway window not correct to OpenTK.

So is there a way to fix it and use Glut with OpenTK? Or am I must not using Glut and create window and context with OpenTK methods?

1

There are 1 answers

0
The Fiddler On BEST ANSWER

Yes, this is possible. From the documentation:

Glut.glutCreateWindow("Test");

// Initialize OpenTK using the current GLUT context
var opentk_context = new GraphicsContext(ContextHandle.Zero, null);

// You can now call GL functions freely
GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);

Note that the GLUT context must be created and current when you call new GraphicsContext(). According to the freeglut documentation, this is the case right after glutCreateWindow() returns.