How to use OpenTK?

I'm going to create a console project at first:

dotnet new console --name OpenGLApp
cd OpenGLApp 
We need to use library which provides multimedia components. So, I'm going to use SFML.Net. But it is optional. Probably, you can use SDL or GLFW instead of SFML. However, I don't have no idea if they are works as well in .NET Core.
dotnet add package SFML.Net
and finally add the following package:
dotnet add package OpenTK
My general purpose in this article is to create a ready-made template that we can use in future projects. In short, we will not have to repeat the above process again and again.
Firstly, I'm going to create file named Windows.cs, and I will create Window class in this file. This file is going to inherit from RenderWindow class that is from SFML library. After that, we are going integrate OpenTK to the Window class:
using System;
using System.IO;
using OpenTK.Graphics.OpenGL;
using OpenTK.Windowing.Desktop;
using SFML.Window;
using SFML.Graphics;
using SFML.System;

namespace OpenTKTutorial
{
    public class Window : RenderWindow
    {
        static GameWindow? gameWindow;
        static GameWindowSettings? gameWindowSettings;
        static NativeWindowSettings? nativeWindowSettings;
        
        View? view;
        
        public Window(VideoMode mode, string title) : base(mode, title)
        {
            
        }
    }
}
OpenTK is integrated via InitOpenTKSettings function:
        private static void InitOpenTKSettings()
        {
            gameWindowSettings = new GameWindowSettings();
            
            nativeWindowSettings = new NativeWindowSettings();
            nativeWindowSettings.Profile = OpenTK.Windowing.Common.ContextProfile.Any;

            gameWindow = new GameWindow(gameWindowSettings, nativeWindowSettings);
            // we won't use opentk window itself.
            gameWindow.IsVisible = false;
        }}
Let's examine the settings above. We set gameWindow as unvisible. Because we don't use it. We are going to use SFML Window instead of it. Let's create our SFML window in InitWindowSettings method:
        private void InitWindowSettings()
        {
            this.SetVerticalSyncEnabled(true);
            view = new View(new FloatRect(0, 0, this.Size.X, this.Size.Y));
            this.SetView(view);
            this.Closed += (o, e) => { this.Close(); };
            this.Resized += (o, e) => { Resize((int)e.Width, (int)e.Height); };
            this.SetActive(true);
            GL.Viewport(0, 0, (int)this.Size.X, (int)this.Size.Y);
        }
        
        private void Resize(int width, int height)
        {
            view = new View(new FloatRect(0, 0, width, height));
            this.SetView(view);
            GL.Viewport(0, 0, width, height);
        }
If you notice, we defined InitOpenTKSettings as static. I used because of conflict problem about windows. So we need to run InitOpenTKSettings method before constructor method. I have learned that if I use it inside a static constructor I can run it earlier than this constructor.(Detailed information from https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors) So like this:
using OpenTK.Graphics.OpenGL;
using SFML.Window;

namespace OpenTKTutorial
{
    public class App
    {
        Window? window;

        public App()
        {
            window = new Window(new VideoMode(1024, 768), "OpenTK Test");
            // clear with this color
            GL.ClearColor(0f, 1f, 0f, 1f);
        }

        private void Update()
        {
            // update
        }

        private void Draw()
        {
            Clear();
        }

        private void Clear()
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);
        }

        public void Execute()
        {
            while(window.IsOpen)
            {
                window.DispatchEvents();
                
                Update();
                Draw();

                window.Display();
            }
        }
    }   
}
Let's test it:
using System;

namespace OpenTKTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
            app.Execute();
        }
    }
}

No comments:

Post a Comment