Simple OpenGL program on C# under Linux

June 2013 · 2 minute read

Today I’ll tell you how to write a very simple OpenGL program on C# at Mono platform. Finally we’re going to get something like this:

Result of OpenGL app

Several companies and organizations have developed their own collections of bindings for OpenGL under .NET (Mono), the leader of which is the Tao Framework. The Tao Framework for .NET is a collection of bindings to facilitate cross-platform media application development utilizing the .NET and Mono platforms. But I failed to use it properly under Linux, so I found Open ToolKit, that is a free, cross-platform OpenGL and OpenAL wrapper for C# and other .NET languages.

To use it you should download it from official website of openTK and extract from archive to your favorite location. Create empty project in your favorite IDE. In Linux (currently OpenSUSE 12.3) I use MonoDevelop. In your project References section right click and add reference to OpenTK.dll which you can find via opentk/Binaries/OpenTK/Release path (and also add System.Drawing.dll assembly reference for Color stuff).

Now add some .cs file to your solution and paste this code:

using System;

using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK.Input;

namespace OpenGLStart
	public class SimpleWindow : GameWindow
		public SimpleWindow()
			: base(400, 300)

		protected override void OnUpdateFrame (FrameEventArgs e)
			base.OnUpdateFrame (e);

		protected override void OnRenderFrame (FrameEventArgs e)

			// draw something simple using OpenGL

			// ...


			// red apex
			GL.Color3(1.0, 0.0, 0.0);
			GL.Vertex2(-1.0, -1.0);

			// green apex
			GL.Color3(0.0, 1.0, 0.0);
			GL.Vertex2(1.0, -1.0);

			// blue apex
			GL.Color3(0.0, 0.0, 1.0);
			GL.Vertex2(0, 1.0);



		protected override void OnLoad (EventArgs e)

		protected override void OnResize (EventArgs e)
			GL.Viewport(0, 0, Width, Height);
			GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);
			base.OnResize (e);


		public static void Main(string[] args)
			using (var SimpleWindow1 = new SimpleWindow())

Code is quite self-explanatory. We use code in OnRenderFrame to draw main picture and some helpful stuff like OnResize to handle window resizing well. Main() method just creates the window and runs it. Now you can run it and get window with colored triangle.

Buy me a coffeeBuy me a coffee
comments powered by Disqus