OpenGL and C# are two of my favourite technologies.
In this post I would like to give a small intro on how to develop games or other 3D accelerated applications using them together.
We will go over:
- how to create a window with an OpenGL context that we can render to;
- how to create vertex buffers, load shaders, and render vertices.
For the purpose of this post I will keep things as simple as possible. However, instead of writing a horribly hard-coded example, we will use proper object oriented design to abstract away a lot of the interface of OpenGL.
This will allow us to focus on the intended behaviour of our program, instead of getting lost in the OpenGL API.
I use the same approach for my own work with OpenGL, which has resulted in my comprehensive wrapper library amulware.Graphics, which is for example used in my game Roche Fusion.
The library is open source and can be found here. For the purpose of this post we will however start from scratch.
Given the length of this post and the amount of code included, I split it up into several pages. Here are some links to quickly jump to the part you are interested in:
- Overview (this page)
- OpenTK, Window Management, Basics
- Vertex Buffer and Array Objects, Shaders, Vertex Attributes and Uniforms
(the meat of this post)
- Putting it all together, Conclusion
Before we start, I would like to give a little context on why I like to use OpenGL.
The biggest argument I could make if I was trying to convince someone to use OpenGL is that it is the cross-platform graphics API. This may change over the next few years with the development of Vulkan, but for now OpenGL is used on Linux, Mac, mobile devices, as well as Windows.
The only real alternative, when working low-level, is to use Direct3D, the rendering API of Microsoft’s DirectX. However, DirectX is only supported on Windows, which seems to make the choice obvious.
While earlier versions of OpenGL are by now extremely outdated, the API was redesigned in 2008 for version 3.0. We will use none of the features deprecated and removed since then, and instead focus on OpenGL 3.0 and higher.
Support for those versions is rampant these days, with OpenGL 3.0 being available on AMD Radeon HD2xxx and higher, NVidia 8xxx and higher, and Intel integrated GPU’s since Sandy Bridge.
Virtually anything – barring very low-budget integrated chips – that can be bought today supports OpenGL 4.0 or higher.