Opentk 显示黑屏而不是三角形

时间:2021-07-09 08:49:15

标签: c# opentk

我创建了一个简单的应用程序来显示一个三角形,但它只显示黑色窗口。

    class Program
    {
        static void Main(string[] args)
        {
            GameWindow window = new GameWindow(800,600);
            game game = new game(window);

            window.Run(1.0/60.0);
        }
    }

    class game
    {
        GameWindow window = new GameWindow();
        public game(GameWindow window)
        {
            this.window = window;

            window.Load += Window_Load;
            window.UpdateFrame += Window_UpdateFrame;
            window.RenderFrame += Window_RenderFrame;
        }

        private void Window_RenderFrame(object sender, FrameEventArgs e)
        {
            GL.ClearColor(Color.CornflowerBlue);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.Viewport(0, 0, 800, 600);


            GL.Begin(PrimitiveType.Triangles);
            GL.Color3(Color.OrangeRed);
            GL.Vertex2(0, 0);
            GL.Vertex2(1, 0);
            GL.Vertex2(0, 1);

            GL.End();

            window.SwapBuffers();
        }

        private void Window_UpdateFrame(object sender, FrameEventArgs e)
        {

        }

        private void Window_Load(object sender, EventArgs e)
        {

        }
    }

1 个答案:

答案 0 :(得分:1)

您不小心创建了 2 个 GameWindow 对象。只需创建一个 GameWindow

class Program
{
    static void Main(string[] args)
    {
        GameWindow window = new GameWindow(800, 600);
        game game = new game(window);

        window.Run(1.0 / 60.0);
    }
} 

class game
{
    GameWindow window; // <--- remove: = new GameWindow();
        
    public game(GameWindow window)
    {
        this.window = window;

        window.Load += Window_Load;
        window.UpdateFrame += Window_UpdateFrame;
        window.RenderFrame += Window_RenderFrame;
    }

    // [...]