Code Jamming Everyday IT problems, solutions and discussions

Simple OpenGL program on C# under Linux

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:

OpenTK Game Window<figcaption class="caption wp-caption-text">Result of OpenGL app</figcaption></figure>

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)
		{
			GL.Clear(ClearBufferMask.ColorBufferBit);

			// draw something simple using OpenGL

			// ...

			GL.Begin(BeginMode.Triangles);

			// 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);

			GL.End();
			this.SwapBuffers();

		}

		protected override void OnLoad (EventArgs e)
		{
			GL.ClearColor(Color4.RoyalBlue);
		}

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

		}

		[STAThread]
		public static void Main(string[] args)
		{
			using (var SimpleWindow1 = new SimpleWindow())
			{
				SimpleWindow1.Run();
			}
		}
	}
}

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