Click here to Skip to main content
13,287,116 members (56,865 online)
Click here to Skip to main content
Add your own
alternative version


458 bookmarked
Posted 4 Nov 2002

SharpGL: A C# OpenGL Class Library

, 7 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Use OpenGL in WinForms or WPF applications, directly or with a powerful Scene Graph.


SharpGL is a project that lets you use OpenGL in your Windows Forms or WPF applications with ease! With all OpenGL functions up to the latest 4.2 release, all major extensions, a powerful scene graph and project templates for Visual Studio, you have all the tools you need to create compelling 3D applications - or to port existing code to a new platform.

Too busy to read the introduction to SharpGL? Check out the Getting Started Guide instead.

A picture paints a thousand words, so let's see some screenshots first!

Cel Shading - This is the latest sample, it shows you how to use modern OpenGL features like Vertex Buffers and Shaders. It will be written up as as complete article soon.

Radial Blur - The Radial Blur Sample shows how you can create advanced lighting effects using standard OpenGL functions.


Utah Teapot WPF Sample - Direct OpenGL Rendering in a WPF application - with no airspace or Windows Forms Host objects. The SharpGL OpenGLControl provides an OpenGL rendering surface directly in your application.


Texturing Sample - Speed up OpenGL development by utilising classes like the Texture class, used to load and transform image data into OpenGL textures.


Hit Testing Sample - By deriving scene elements from IBoundable, you can let your own custom classes participate in hit testing.


What is SharpGL?

SharpGL is a collection of class libraries that let you use OpenGL functionality in your code. Class libraries are:

SharpGL - Contains the main OpenGL object - this object wraps all OpenGL functions, enumerations and extensions.

SharpGL.SceneGraph - Contains all wrappers for OpenGL objects and Scene Elements - Lights, Materials, Textures, NURBs, Shaders and more.

SharpGL.WinForms - Contains Windows Forms Controls for your applications.

SharpGL.WPF - Contains WPF Controls for your applications.

SharpGL.Serialization - Contains classes used to load geometry and data from 3D Studio Max files, Discreet obj files and trueSpace files.

Between these libraries, SharpGL gives to a wrapper to all current OpenGL functions, all major extensions and a rich set of objects for advanced functionality. You can use SharpGL to perform 'traditional' OpenGL drawing or you can use the SceneGraph to implement more application specific tasks.

How do I use SharpGL?

It's simple! If you have a chunk of code like the below:

//  Set the line width and point size.

Then it would be written as:

//  Get a reference to the OpenGL object.
OpenGL gl = openGLCtrl1.OpenGL;

//  Set the line width and point size.

This is the first fundamental - any OpenGL function that begins with 'gl' or 'glu' is a member function of the SharpGL.OpenGL object, with the 'gl' or 'glu' removed.

This takes care of the most basic functions - however, there is an exception to this rule. The code below:

//  Set the color.
glColor3f(0.5f, 0.5f, 0.5f);

//  Output some vertices.
glVertex3f(2.0f, 3.0f 4.0f);
glVertex4f(2.0f, 1.0f, 10.0f, 1.0f);

Would be written as:

//  Set the color.
gl.Color3(0.5f, 0.5f, 0.5f);

//  Output some vertices.
gl.Vertex3(2.0f, 3.0f 4.0f);
gl.Vertex4(2.0f, 1.0f, 10.0f, 1.0f);

Note the absense of the 'f' at the end of the function. This is the second fundamental - SharpGL wrapper functions never have a type postfix. OpenGL functions often end in 'f' (for float), 'fv' (for float vector), 'i' (for integer) and so on. We do not need to have unique names in C# so we do not apply these postfixes to the function name. We do keep the number however - as in the case where arrays are passed in, the wrapper needs to know how many elements are to be used.

This takes care of functions that take fundamental types as parameters. For other types of functions, we have one more thing to remember. The code below:

//  Set the polygon mode.
glPolygonMode(GL_FRONT, GL_FILL);

is written as:

//  Set the polygon mode.
gl.PolygonMode(OpenGL.GL_FRONT, OpenGL.GL_FILL);


//  Set the polygon mode.
gl.PolygonMode(PolygonFace.Front, PolygonMode.Fill);

This describes the final fundamental. OpenGL constants are all defined as constant members of the SharpGL.OpenGL class, and have exactly the same names.

This takes care of how 'standard' C OpenGL code would be written using SharpGL. As an aid to the programmer, many of the standard OpenGL functions have overloads that take strong types. In the snippet above (PolygonMode) we can use OpenGL constants or the PolygonFace and PolygonMode enumerations - both are perfectly valid. The first is more in line with typical C code, the second gives the developer more in the way of intellisense to aid them.

Coding with SharpGL

Coding with SharpGL is very straightforward. Some of the benefits you will notice are:

Code Hints and Intellisense


All core OpenGL functions are fully documented, meaning you get the information you need as you type - less looking through The Red Book and more productivity.

Enumerations for Core Functions


Improve readability and reduce errors by using type-safe enumerations for core functions.

Extensions and Core Support to 4.2


All major extension functions available, deprecated functions are marked as deprecated, extensions that have made it into the standard are present both in extension form and core form.

Your first SharpGL Application

You can have a SharpGL application running in five minutes - here's how.

1. Install the SharpGL Visual Studio Extension

Download the SharpGL Visual Studio Extension and extract it. Double click on the *.vsix file - the install confirmation will be shown. Choose 'Install'.


2. Run Visual Studio and create a New Project

Run Visual Studio and choose 'New Project'. You'll see that under C# there are two new templates - SharpGL Windows Forms Application and SharpGL WPF Application. Choose your preferred platform.


3. Run the Application

Hit Ctrl-F5 or press 'Run'. Your new SharpGL application runs up, showing a rotating pyramid. You have three functions by default:

OpenGLDraw - Used to do OpenGL rendering.
OpenGLInitialized - Used to perform any OpenGL initialization.
Resize - Used to create a projection transformation.

The code that comes with the template does the basic for you - and there are many sample applications provided with the source code as baselines for your own project.


Related Articles

SharpGL is big. OpenGL is big and very mature now. Rather than squeeze too much detail into this article, I will write separate articles on specific topics. If you have any suggestions or questions then please comment below.

OpenGL in .NET - Getting Started

SharpGL Documentation

Using SharpGL in a WPF Application

Keep Up To Date

Up to date information on SharpGL development is available from the SharpGL CodePlex site or the SharpGL GitHub site. You can always get the latest binaries from Nuget, just check out the Getting Started Guide.

I write about various interesting areas I come across when developing SharpGL on my blog at


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Dave Kerr
Software Developer
United Kingdom United Kingdom
Follow my blog at and find out about my charity at

You may also be interested in...


Comments and Discussions

GeneralFantastic stuff thank you. Pin
makaveli_000020-Feb-13 8:33
membermakaveli_000020-Feb-13 8:33 
GeneralRe: Fantastic stuff thank you. Pin
Dave Kerr20-Feb-13 22:37
mvpDave Kerr20-Feb-13 22:37 
GeneralRe: Fantastic stuff thank you. Pin
Juan Manuel Romero Martin7-Apr-13 13:12
memberJuan Manuel Romero Martin7-Apr-13 13:12 
GeneralRe: Fantastic stuff thank you. Pin
makaveli_00007-Apr-13 14:23
membermakaveli_00007-Apr-13 14:23 
Questiontext in 3d space Pin
tcpmv1-Jan-13 11:15
membertcpmv1-Jan-13 11:15 
AnswerRe: text in 3d space Pin
Dave Kerr2-Jan-13 22:32
mvpDave Kerr2-Jan-13 22:32 
Generaltext in 3d space Pin
tcpmv3-Jan-13 5:58
membertcpmv3-Jan-13 5:58 
GeneralRe: text in 3d space Pin
Dave Kerr3-Jan-13 8:10
mvpDave Kerr3-Jan-13 8:10 
QuestionWhen I initialize the OpenGL, why I must Translate the modelview. Pin
zy3327197942-Dec-12 16:23
memberzy3327197942-Dec-12 16:23 
AnswerRe: When I initialize the OpenGL, why I must Translate the modelview. Pin
Dave Kerr4-Dec-12 4:12
mvpDave Kerr4-Dec-12 4:12 
QuestionCan't get started Pin
MWBate29-Nov-12 9:01
memberMWBate29-Nov-12 9:01 
AnswerRe: Can't get started - solved Pin
MWBate3-Dec-12 17:48
memberMWBate3-Dec-12 17:48 
GeneralRe: Can't get started - solved Pin
Dave Kerr4-Dec-12 4:12
mvpDave Kerr4-Dec-12 4:12 
QuestionProblem with Intel HD 3000 on laptop Pin
andrea tosetto24-Nov-12 5:32
memberandrea tosetto24-Nov-12 5:32 
I've got a problem with an implementation of sharpgl on my laptop, my graphic card is the one in subject, the problem seems to be related to deep buffer. I know that there are problems with opengl and the graphic card, but the strange thing is that the previous implementation of sharpgl works correctly.
What I can do to setup sharpGL 2.0 to behave like the previous version? Or if you have any suggestion it will be appreciated.


Question2d Images Pin
Member 778994826-Oct-12 9:21
memberMember 778994826-Oct-12 9:21 
Questionwhyt the run result is always balckground Pin
lxp211022-Oct-12 5:57
memberlxp211022-Oct-12 5:57 
Bugbug in framerate setter Pin
christophe.thalet13-Oct-12 9:00
memberchristophe.thalet13-Oct-12 9:00 
GeneralRe: bug in framerate setter Pin
Dave Kerr14-Oct-12 0:27
mvpDave Kerr14-Oct-12 0:27 
Generala problem Pin
xitangzi16-Jul-12 23:57
memberxitangzi16-Jul-12 23:57 
GeneralRe: a problem Pin
lxp211022-Oct-12 5:42
memberlxp211022-Oct-12 5:42 
BugSharpGL runs really slow Pin
c4th0d36-Jul-12 9:50
memberc4th0d36-Jul-12 9:50 
GeneralRe: SharpGL runs really slow Pin
Dave Kerr6-Jul-12 21:21
mvpDave Kerr6-Jul-12 21:21 
GeneralRe: SharpGL runs really slow Pin
c4th0d37-Jul-12 0:20
memberc4th0d37-Jul-12 0:20 
GeneralRe: SharpGL runs really slow Pin
Dave Kerr8-Jul-12 12:06
mvpDave Kerr8-Jul-12 12:06 
GeneralRe: SharpGL runs really slow Pin
petya21647-Sep-12 7:18
memberpetya21647-Sep-12 7:18 
QuestionNo Documentation Pin
Member 893273127-May-12 12:56
memberMember 893273127-May-12 12:56 
QuestionCombining Emgu CV + SharpGL Pin
locklems9025-Apr-12 19:08
memberlocklems9025-Apr-12 19:08 
GeneralMy vote of 5 Pin
Oshtri Deka21-Mar-12 2:43
memberOshtri Deka21-Mar-12 2:43 
GeneralRe: My vote of 5 Pin
Dave Kerr25-Apr-12 23:19
mvpDave Kerr25-Apr-12 23:19 
GeneralRe: My vote of 5 Pin
Dave Kerr25-Apr-12 23:20
mvpDave Kerr25-Apr-12 23:20 
GeneralThanks for the code Pin
DaveyGun14-Mar-12 13:25
memberDaveyGun14-Mar-12 13:25 
GeneralRe: Thanks for the code Pin
Dave Kerr16-Mar-12 2:36
mvpDave Kerr16-Mar-12 2:36 
GeneralMy vote of 5 Pin
Sharjith27-Feb-12 10:20
memberSharjith27-Feb-12 10:20 
GeneralRe: My vote of 5 Pin
Dave Kerr5-Mar-12 12:34
mvpDave Kerr5-Mar-12 12:34 
GeneralMy vote of 5 Pin
liversour22-Feb-12 22:09
memberliversour22-Feb-12 22:09 
GeneralRe: My vote of 5 Pin
Dave Kerr22-Feb-12 23:26
mvpDave Kerr22-Feb-12 23:26 
GeneralMy vote of 5 Pin
Rahul Rajat Singh22-Feb-12 22:05
memberRahul Rajat Singh22-Feb-12 22:05 
GeneralRe: My vote of 5 Pin
Dave Kerr22-Feb-12 22:55
mvpDave Kerr22-Feb-12 22:55 
GeneralRe: My vote of 5 Pin
Dave Kerr22-Feb-12 22:55
mvpDave Kerr22-Feb-12 22:55 
GeneralMy vote of 5 Pin
Patrick Harris22-Feb-12 12:56
memberPatrick Harris22-Feb-12 12:56 
GeneralThank You! (vote of 5) Pin
Patrick Harris22-Feb-12 12:52
memberPatrick Harris22-Feb-12 12:52 
GeneralRe: Thank You! (vote of 5) Pin
Dave Kerr22-Feb-12 22:54
mvpDave Kerr22-Feb-12 22:54 
GeneralMy vote of 5 Pin
Florian Rappl22-Feb-12 11:51
memberFlorian Rappl22-Feb-12 11:51 
GeneralRe: My vote of 5 Pin
Dave Kerr22-Feb-12 22:54
mvpDave Kerr22-Feb-12 22:54 
GeneralMy vote of 5 Pin
voloda217-Feb-12 3:31
membervoloda217-Feb-12 3:31 
GeneralRe: My vote of 5 Pin
Dave Kerr20-Feb-12 2:11
mvpDave Kerr20-Feb-12 2:11 
QuestionThanks Pin
luizr7-Feb-12 14:34
memberluizr7-Feb-12 14:34 
AnswerRe: Thanks Pin
Dave Kerr20-Feb-12 2:11
mvpDave Kerr20-Feb-12 2:11 
Questiongreat work!!! Pin
Paolo Cimatti10-Nov-11 23:25
memberPaolo Cimatti10-Nov-11 23:25 
AnswerRe: great work!!! Pin
Dave Kerr10-Nov-11 23:44
mentorDave Kerr10-Nov-11 23:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 7 Apr 2014
Article Copyright 2002 by Dave Kerr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid