DEV Community

Dave
Dave

Posted on

If you wondered why OpenTK didn't work right on your Linux box, it isn't you.

Four days. Four days I worked on trying to get my Linux Mint machine to do OpenGL windows/graphics using the OpenTK package on Nuget. I could open a window, but I couldn't seem to write anything to it.

Then, I got creative.

This was where I was earlier this week, just before Thanksgiving. I am starting a C# project with a math focus. Yes, I've already been told there is an easier way. That's not the point. I'm in the middle of learning more programming skills, especially C#. My project needed a way to display the output of math functions (like y=x^2). I needed a simple window graphic system to display a plot of the function.

I had worked a little with OpenGL in C and it seemed my best choice for the graphing. Gtk does have a C# library, but the drawing (using Cairo) just seemed that it wasn't straightforward. However, if I used OpenGL, I needed it in C#. That left me with the four days of searching how to get this done. I kept returning to OpenTK (The OpenGL Toolkit). But, it wasn't working out.

Here is my set up:
A Linux Mint 19 OS.
Visual Studio Code for editing/building/etc.
.NET Core 3.1.

I could install the OpenTK Nuget package. It didn't say it wouldn't work, just that it didn't exactly target .NET Core 3.1. It was targeting .NET Frameworks and I should have realized that this has been problematic before.

To a small degree, the library did work. I could open the basic window class (GameWindow they call it). I learned how to navigate the namespaces (There are several and they aren't exactly common to each other). When I called "dotnet run" on my test projects, I could usually open a window. I could even set the background colors of the window.

Yet, try as I might, I couldn't get anything to draw ON the windows. The programs built. VS Code's Intellisense said I was using correct library syntax; yet, no glory.

I was just about to give up when I went looking for any other kind of library package that would work. Many of them are undocumented and ancient (OpenGL is kind of old, too, but still in use). Finally, in one of my searches on Nuget.org, I stumbled across the package OpenTK.NetStandard.

After erasing my test project and starting over with OpenTK.NetStandard, I was quite surprised when I could walk through a simple tutorial and the triangle finally appeared on the screen in my OpenTK project (It seems all OpenGL tutorials start with a triangle). No fuss. No muss. No pulling hair out (which I don't have).

I've been busy for the last two days trying to make everything work that I need for my project. I still have some practice to go before I'm ready to use it, but it is finally promising.

I have the basics of a small tutorial project that I've just uploaded to github.com. I'll put a link below along with the two final tutorials I followed and a link to at least one other that worked. Many of the tutorials leave out that they are using OpenTK on .NET Frameworks and running on a Windows machine. What I'm posting here is just the differences between what I've discovered and the OpenTK/OpenGL lessons I've run across.

Once you have a working model on your development system, then you can often read anyone's tutorials, lessons, or StackOverflow and you'll have a better chance of it working, in my observed opinion (narrow that may be).

The repository should build on most Debian/Ubuntu flavors, I think. However, depending on what you're using, you might need to start a new project and copy in the code to get it to work on your machine. You can search Nuget.org for OpenTK.NetStandard and they have good instructions for a command line install of the package.

The repository has a small console menu that opens at first run. It will give you a choice of three windows:
An old style OpenGL window.
A modern style OpenGL window.
An old style 2D OpenGl window.

Old style, in my parlance refers to the method of drawing by using GL.Begin to GL.End methods in your window class. The modern style makes use of externally defined and added shaders along with Vertex Buffer Objects to draw with. At this point, I'm too new to know the real difference. But, what I learned in programming OpenGL in C is termed 'old style' in C#. I do think it is easier to use, especially for my simple 2D drawing/plotting.

The one 2D window does have an added keyboard handler to move a square left or right on the screen. If I keep working on it, I'll add up and down.

There are probably many things I could have done to make things easier. However, one of my rules, especially in coding, is NO BLACK BOXES. I want to know most of what goes on 'under the hood'. By coding all of my needs and not turning any of it over to fancier 'bells and whistles' apps, I learn more. And, in the end, for me at least, that's what it's really about

The github repository is here:
OpenTK NEtCore Tutorial

The two tutorials I used for the 'old style' and modern is here:
Neo Kabuto Tutorial 1

Neo Kabuto Tutorial 2

This OpenTK/OpenGL tutorial is also good.

Top comments (0)