0

我正在尝试使用 Tao.FreeGlut 2.1.0 初始化窗口并使用 OpenTK 1.1 进行绘制,所以我制作了简单的程序:

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();
}

但是在GL.ClearColor调用时它会因空引用异常而崩溃。我认为,这会发生,因为 OpenTK 存储它自己的 OpenGL 上下文,并且它不等于由 Glut 创建且未正确初始化的上下文。我没有找到任何从 Glut 获取上下文的方法,然后我尝试执行以下操作:

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

现在它在上下文创建时崩溃,无论如何窗口对 OpenTK 不正确。

那么有没有办法修复它并将 Glut 与 OpenTK 一起使用?还是我不能使用 Glut 并使用 OpenTK 方法创建窗口和上下文?

4

1 回答 1

0

是的,这是可能的。从文档中:

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);

请注意,GLUT 上下文必须在您调用new GraphicsContext(). 根据freeglut 文档glutCreateWindow(),退货后就是这种情况。

于 2014-03-30T17:11:35.787 回答