Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / Windows Forms

SharpGL: A C# OpenGL Class Library

Rate me:
Please Sign up or sign in to vote.
4.94/5 (178 votes)
7 Apr 2014GPL35 min read 1.9M   63.1K   463   336
Use OpenGL in WinForms or WPF applications, directly or with a powerful Scene Graph.

sharpgl/SharpGL-Logo.jpg

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.

Image 2

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

sharpgl/RadialBlurSample.jpg

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.

sharpgl/WpfTeapotSample.jpg

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

sharpgl/TexturesSample.jpg

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

sharpgl/HitTestSample.jpg

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.
glLineWidth(3.0f);
glPointSize(2.0f);

Then it would be written as:

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

//  Set the line width and point size.
gl.LineWidth(3.0f);
gl.PointSize(2.0f);

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

or:

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

sharpgl/Screenshot_CodeHints.png

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

sharpgl/Screenshot_Enumerations.png

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

Extensions and Core Support to 4.2

sharpgl/Screenshot_Extensions.png

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'.

sharpgl/ConfirmInstallation.png

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.

sharpgl/NewWpfApplication.png

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.

sharpgl/WpfApp.png

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 http://www.dwmkerr.com.

License

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


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions

 
AnswerRe: VS 2013 Extension Pin
Dave Kerr8-Dec-14 17:50
mentorDave Kerr8-Dec-14 17:50 
QuestionVSIX Installation failed - Visual Studio Express Editions Pin
AORD17-Nov-14 22:30
AORD17-Nov-14 22:30 
AnswerRe: VSIX Installation failed - Visual Studio Express Editions Pin
Dave Kerr19-Nov-14 3:23
mentorDave Kerr19-Nov-14 3:23 
AnswerRe: VSIX Installation failed - Visual Studio Express Editions Pin
Dave Kerr25-Jan-15 1:12
mentorDave Kerr25-Jan-15 1:12 
GeneralRe: VSIX Installation failed - Visual Studio Express Editions Pin
AORD25-Jan-15 7:23
AORD25-Jan-15 7:23 
GeneralRe: VSIX Installation failed - Visual Studio Express Editions Pin
Dave Kerr26-Jan-15 21:45
mentorDave Kerr26-Jan-15 21:45 
QuestionSharpGL tutorial & 3D rendering?? Pin
Member 1088161316-Jun-14 22:56
Member 1088161316-Jun-14 22:56 
AnswerRe: SharpGL tutorial & 3D rendering?? Pin
Dave Kerr22-Jun-14 7:52
mentorDave Kerr22-Jun-14 7:52 
Hi Lena,

Thanks for your message! OK, here's what I'd do.

1. Avoid loading the greyscale image into a memory buffer, it seems like you are doing this only to get at input data for building a 3D modal, and you need to transform that data from a greyscale image into geometry. Just use the .NET code in System.Drawing to load the image, then loop over every pixel and create an integer or byte with the intensity. Then you have an array of values to work with.
2. Now for the geometry - you have a 2d array of intensity values, so loop over each and draw it, with the colour as a function of the height.

This will work for drawing dots, for drawing a surface you've have to make the set of points into a set of triangles, in this case you will get a very 'spiky' image if you don't have millions of points very close to each other:

http://gamedev.stackexchange.com/questions/45044/generating-triangles-from-a-square-grid[^]

So you'll have to pick an algorithm to use for triangulation/tesselation. The choice of this will depend on the type of geometry you may have - if you have very smooth geometry with lots of points you can use basic approaches to this, if you have a very small number of points with lots of differences (i.e. lots of cliffs, mountains, peaks, abysses etc) then you'll have to consider more sophisticated approaches to smoothing.

As a last point, once you've generated the geometry in C# or VB, build it into a vertex buffer array (there's a sample for this in the code) and render it with a pixel shader for maximum performance - otherwise you can just use immediate mode if performance isn't critical.

Good luck!

QuestionRe: SharpGL tutorial & 3D rendering?? Pin
Member 1088161325-Jun-14 23:04
Member 1088161325-Jun-14 23:04 
AnswerRe: SharpGL tutorial & 3D rendering?? Pin
Dave Kerr27-Jun-14 4:41
mentorDave Kerr27-Jun-14 4:41 
GeneralRe: SharpGL tutorial & 3D rendering?? Pin
Member 108816131-Jul-14 20:19
Member 108816131-Jul-14 20:19 
GeneralRe: SharpGL tutorial & 3D rendering?? Pin
Dave Kerr21-Jul-14 6:48
mentorDave Kerr21-Jul-14 6:48 
QuestionRe: SharpGL tutorial & 3D rendering?? Pin
Member 1088161328-Jul-14 20:53
Member 1088161328-Jul-14 20:53 
QuestionHow to use the WGS84 (Latitude, Longitude) projection Pin
AhmedGis201114-Apr-14 7:28
AhmedGis201114-Apr-14 7:28 
AnswerRe: How to use the WGS84 (Latitude, Longitude) projection Pin
Dave Kerr14-Apr-14 7:59
mentorDave Kerr14-Apr-14 7:59 
QuestionWhich OpenGL version? Pin
Leonardo Bouchan12-Apr-14 10:04
Leonardo Bouchan12-Apr-14 10:04 
AnswerRe: Which OpenGL version? Pin
Dave Kerr12-Apr-14 20:54
mentorDave Kerr12-Apr-14 20:54 
QuestionLicense Pin
LittleFox949-Apr-14 5:32
LittleFox949-Apr-14 5:32 
AnswerRe: License Pin
Dave Kerr9-Apr-14 5:36
mentorDave Kerr9-Apr-14 5:36 
GeneralRe: License Pin
LittleFox949-Apr-14 5:57
LittleFox949-Apr-14 5:57 
QuestionDrawText could not show Chinese! Pin
ydongydong11-Mar-14 2:48
ydongydong11-Mar-14 2:48 
AnswerRe: DrawText could not show Chinese! Pin
Dave Kerr11-Mar-14 5:56
mentorDave Kerr11-Mar-14 5:56 
GeneralRe: DrawText could not show Chinese! Pin
ydongydong12-Mar-14 15:53
ydongydong12-Mar-14 15:53 
QuestionCan't Install VSIX With VS2012 Pin
Ashley Staggs15-Sep-13 0:33
Ashley Staggs15-Sep-13 0:33 
AnswerRe: Can't Install VSIX With VS2012 Pin
Dave Kerr15-Sep-13 5:37
mentorDave Kerr15-Sep-13 5:37 

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.