The easiest way to have OpenGL and GLUT working with any .NET language (including C#, VB.NET, etc) is using a binding library like the TAO framework:
There are two versions of the library if you want to use it with the Microsoft's .NET framework or with MONO (http://www.mono-project.com).
Using TAO with Windows
These are the steps to have the Tao Framework working on a Windows machine:
- Download Tao from the website and install it.
- Create a new "Console Application" project with Visual Studio (you can download a free copy of Visual Studio Express from Microsoft's website or the full version from the BYU CS Academic Alliance)
- Add a new reference to Tao Framework OpenGL Binding and Tao Framework FreeGLUT Binding
Sample Code
This is a code snippet on how to use the library:
using System;
using Tao.OpenGl;
using Tao.FreeGlut;
using System.Collections.Generic;
namespace SampleOpenGL
{
class MainClass
{
public static void Main(string[] args)
{
MainClass m = new MainClass();
m.StartGl();
}
public void StartGl()
{
InitGlutScreen(800, 800, "Hello World");
InitGlutWireEvents();
Glut.glutMainLoop();
}
private void InitGlutScreen(int width, int height, string title)
{
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.GLUT_SINGLE | Glut.GLUT_RGB);
Glut.glutInitWindowSize(width, height);
Glut.glutCreateWindow(title);
InitGl();
}
private void InitGl()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glOrtho(0, 1, 0, 1, -1, 1);
}
private void InitGlutWireEvents()
{
Glut.glutDisplayFunc(new Glut.DisplayCallback(Display));
Glut.glutKeyboardFunc(new Glut.KeyboardCallback(Keyboard));
Glut.glutReshapeFunc(new Glut.ReshapeCallback(Reshape));
Glut.glutMouseFunc(new Glut.MouseCallback(Mouse));
Glut.glutMotionFunc(new Glut.MotionCallback(Motion));
}
private static void Display()
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Gl.glBegin(Gl.GL_POLYGON);
Gl.glVertex3f(0.25f, 0.25f, 0f);
Gl.glVertex3f(0.75f, 0.25f, 0f);
Gl.glVertex3f(0.25f, 0.75f, 0f);
Gl.glVertex3f(0.75f, 0.75f, 0f);
Gl.glEnd();
Gl.glFlush();
}
private static void Keyboard(byte key, int x, int y)
{
switch (key)
{
case 27: //this is the ESC key
Environment.Exit(0);
break;
}
}
private static void Mouse(int button, int state, int x, int y)
{
}
private static void Motion(int x, int y)
{
}
private static void Reshape(int w, int h)
{
}
}
}
Using Windows 64 bits
If you are using a Windows 64 bits version (XP or Vista) you will need to change the Platform target (under the project properties) to x86 because the GLUT library was compiled for 32 bit systems.
Using Linux, Mac OS X or other *nix systems
You can use this library (Tao) with MONO in many platforms including Linux, MacOS X, FreeBSD, Solaris, etc. You will need the latest version of MONO on your machine and preferably Monodevelop.
These are the steps to have it working on Linux using Monodevelop:
- Download and install the latest version of MONO and Monodevlop from the MONO website (http://www.mono-project.com)
- Download the MONO version of the Tao Framework from the website.
- In Monodevelop create a new Console Project.
- Add a Reference to the Tao OpenGL and Tao FreeGLUT DLLs.
- Make sure you have FreeGLUT installed on your system and preferably the latest drivers for you 3D graphics card.
Enjoy!





