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.
A picture paints a thousand words, so let's see some screenshots first!
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:
Then it would be written as:
OpenGL gl = openGLCtrl1.OpenGL;
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:
glColor3f(0.5f, 0.5f, 0.5f);
glVertex3f(2.0f, 3.0f 4.0f);
glVertex4f(2.0f, 1.0f, 10.0f, 1.0f);
Would be written as:
gl.Color3(0.5f, 0.5f, 0.5f);
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:
is written as:
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.
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.
Using SharpGL in a WPF Application - http://www.codeproject.com/KB/WPF/openglinwpf.aspx
SharpGL Documentatin - http://sharpgl.codeplex.com/documentation
Keep Up To Date
Up to date information on SharpGL development is available from the CodePlex site. SharpGL is hosted on CodePlex, at http://sharpgl.codeplex.com.
I write about various interesting areas I come across when developing SharpGL on my blog at http://www.dwmkerr.com.
I am looking for feature requests for
SharpGL 2.1 and the roadmap! Add feature requests from the CodePlex page's 'Issues' section or via the comments below.