125

Is there free OpenGL support libraries for C#? If so, which one do I use and where do I find sample projects?

Does C# provide classes for OpenGL?

1
  • I know this is an extremely old thread, but Unity might be of interest to you Commented Jul 28, 2017 at 1:32

9 Answers 9

132

OpenTK is an improvement over the Tao API, as it uses idiomatic C# style with overloading, strongly-typed enums, exceptions, and standard .NET types:

GL.Begin(BeginMode.Points);
GL.Color3(Color.Yellow);
GL.Vertex3(Vector3.Up);

as opposed to Tao which merely mirrors the C API:

Gl.glBegin(Gl.GL_POINTS);   // double "gl" prefix
Gl.glColor3ub(255, 255, 0); // have to pass RGB values as separate args
Gl.glVertex3f(0, 1, 0);     // explicit "f" qualifier

This makes for harder porting but is incredibly nice to use.

As a bonus it provides font rendering, texture loading, input handling, audio, math...

Update 18th January 2016: Today the OpenTK maintainer has stepped away from the project, leaving its future uncertain. The forums are filled with spam. The maintainer recommends moving to MonoGame or SDL2#.

Update 30th June 2020: OpenTK has had new maintainers for a while now and has an active discord community. So the previous recommendation of using another library isn't necessarily true.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't think these functions are very representative, because they are all deprecated and removed from the OpenGl core profile.
True, those function are removed from core profile. But the arguments still valid. "OpenTK is an improvement over the Tao API, as it uses idiomatic C# style with overloading, strongly-typed enums, exceptions, and standard .NET types"
The linked website is invalid for this post now.
Alternatively there is also Silk.NET which spawned off of OpenTK, it does many of the original things as well as some new bindings: github.com/dotnet/Silk.NET
18

I think what @korona meant was since it's just a C API, you can consume it from C# directly with a heck of a lot of typing like this:

[DllImport("opengl32")]
public static extern void glVertex3f(float x, float y, float z);

You unfortunately would need to do this for every single OpenGL function you call, and is basically what Tao has done for you.

2 Comments

How would you go about that on Linux (with mono)?
16

Tao is supposed to be a nice framework.

From their site:

The Tao Framework for .NET is a collection of bindings to facilitate cross-platform media application development utilizing the .NET and Mono platforms.

2 Comments

The new website for Tao is: Tao Framework
See answer above. Tao has been superseded by OpenTK: github.com/opentk/opentk.
11

SharpGL is a project that lets you use OpenGL in your Windows Forms or WPF applications.

Comments

7

You can OpenGL without a wrapper and use it natively in C#. Just as Jeff Mc said, you would have to import all the functions you need with DllImport.

What he left out is having to create context before you can use any of the OpenGL functions. It's not hard, but there are few other not-so-intuitive DllImports that need to be done.

I have created an example C# project in VS2012 with almost the bare minimum necessary to get OpenGL running on Windows box. It only paints the window blue, but it should be enough to get you started. The example can be found at http://www.glinos-labs.org/?q=programming-opengl-csharp. Look for the No Wrapper example at the bottom.

1 Comment

your link is dead, please can you share your project. Thanks.
7

I would also recommend the Tao Framework. But one additional note:

Take a look at these tutorials: http://www.taumuon.co.uk/jabuka/

8 Comments

I have no idea what happened to Tao as the page went down quite a while ago.
The new website for Tao is: Tao Framework
Tao seems to be a moving target. I updated the link, cause it has also moved from mono now to sourceforge.
Wow, November 2005... does OpenGL not change much?
@JonHarrop yeah looks like they haven't set a redirect there using a github page now opentk.github.io
|
4

What would you like these support libraries to do? Just using OpenGL from C# is simple enough and does not require any additional libraries afaik.

3 Comments

In what namespace can these classes be found?
OpenGL is not part of .NET, as of version 4.0
@MegaByte They must be accessed via platform invoke.
2

Concerning the (somewhat off topic I know but since it was brought up earlier) XNA vs OpenGL choice, it might be beneficial in several cases to go with OpenGL instead of XNA (and in other XNA instead of OpenGL...).

If you are going to run the applications on Linux or Mac using Mono, it might be a good choice to go with OpenGL. Also, which isn't so widely known I think, if you have customers that are going to run your applications in a Citrix environment, then DirectX/Direct3D/XNA won't be as economical a choice as OpenGL. The reason for this is that OpenGL applications can be co-hosted on a lesser number of servers (due to performance issues a single server cannot host an infinite number of application instances) than DirectX/XNA applications which demands dedicated virtual servers to run in hardware accelerated mode. Other requirements exists like supported graphics cards etc but I will keep to the XNA vs OpenGL issue. As an IT Architect, Software developer etc this will have to be considered before choosing between OpenGL and DirectX/XNA...

A side note is that WebGL is based on OpenGL ES3 afaik...

As a further note, these are not the only considerations, but they might make the choice easier for some...

Comments

1

XNA 2.0 requires a minimum of a shader 1.1 card. While old tech, not everyone has one. Some newer laptops (in our experience Toshiba tablets with Intel graphics) have no shader 1.1 support. XNA simply wont run on these machines.

This is a significant issue for us and we have shifted to Tao and OpenGL. Plus with Tao we have bindings for audio & Lua support.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.