Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenTK: Why is GraphicsMode not available?

Tags:

c#

opengl

opentk

I just started to learn OpenTK and stumbled upon a problem when going through this tutorial.

This is what I tried:

using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace Testing
{
    public class GraphicsWindow : GameWindow
    {
        public GraphicsWindow(int width, int height, string title) : base(width, height, GraphicsMode.Default, title)
        {
            
        }
    }
}

The Enum GraphicsMode is not found for some reason (should be found in namespace OpenTK.Graphics). Another one is that GameWindow does not have a constructor taking 4 arguments.

I have installed the latest version of the OpenTK Nuget package (4.0.6). The project I created targets .NET Core.

Any ideas?

like image 659
user2685022 Avatar asked Mar 01 '23 23:03

user2685022


1 Answers

The tutorial is based on OpenTK 3.x, which was written for .NET framwork. OpenTK 4.x is written for .NET core. In 3.x the GameWindow was part of the OpenTK.Graphics namespace. Now the class is contained in OpenTK.Windowing.Desktop and behaves different. The constructor has 2 arguments, GameWindowSettings and NativeWindowSettings.

namespace Testing
{
    public class GraphicsWindow : GameWindow
    {
        public GraphicsWindow(int width, int height, string title)
            : base(
                  new GameWindowSettings(),
                  new NativeWindowSettings()
                  {
                      Size = new OpenTK.Mathematics.Vector2i(width, height),
                      Title = title
                  })
            { }
    }
}

Alternatively create a static factory method:

namespace Testing
{
    public class GraphicsWindow : GameWindow
    {
        public static GraphicsWindow New(int width, int height, string title)
        {
            GameWindowSettings setting = new GameWindowSettings();
            NativeWindowSettings nativeSettings = new NativeWindowSettings();
            nativeSettings.Size = new OpenTK.Mathematics.Vector2i(width, height);
            nativeSettings.Title = title;
            return new GraphicsWindow(setting, nativeSettings);
        }

        public GraphicsWindow(GameWindowSettings setting, NativeWindowSettings nativeSettings) 
            : base(setting, nativeSettings)
        {}
    }
}
var myGraphicsWindow = GraphicsWindow.New(800, 600);

See also OpenTK_hello_triangle

like image 161
Rabbid76 Avatar answered Mar 11 '23 08:03

Rabbid76