Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » OpenGL » General » Revisions
 

SharpGL: A C# OpenGL Class Library

By , 11 Dec 2011
 
This is an old version of the currently published article.
Screenshot.jpg

Headline: SharpGL 2.0 Beta 1 is available to be tried out from the SharpGL CodePlex site - the latest download is at http://sharpgl.codeplex.com/releases/view/74704.

Introduction

Use OpenGL in your C# applications with SharpGL, it's a breeze! Just drag an OpenGLControl onto your Windows Form and handle the 'OpenGLDraw' function - now just call ordinary OpenGL functions!

Keeping Track of SharpGL

Before I go much further, here are some useful bits of information:

SharpGL is now hosted on CodePlex at http://sharpgl.codeplex.com

SharpGL is currently at version 2.0 Beta 1 - this version adds support for WPF, OpenGL 4.2 extensions and more.

I am also writing about the various challenges of creating the new version on my blog at http://www.dwmkerr.com.

Using SharpGL in WinForms Applications

SharpGL provides you with two controls for designing forms. The OpenGLControl, which lets you do standard OpenGL drawing in a C# application, and the SceneControl, which does the same with added support for polygons/persistence/picking and more. The screenshot above shows the SceneControl in action, with the supplied 'SceneBuilder' application. The screenshot below shows some 'old fasioned' OpenGL drawing, with calls to 'glBegin' and 'glEnd', etc.

Screenshot-Small.jpg

If you want to get OpenGL in your application quickly, there's no easier way. There are eight example applications in the download that show you how to use some common features.

Getting Started

Create a Windows Forms application, use the SharpGL DLL as a reference, and drop an OpenGLControl onto the form. Then handle the 'OpenGLDraw' event and you're ready to go!

Calls that in C++ would look like...

glBegin(GL_LINES);
    glVertex3f(1.0f, 1.0f, 1.0f); ...etc...

...will have to look like:

OpenGL gl = someForm.someOpenGLControl.OpenGL;

gl.Begin(OpenGL.LINES); 
		gl.Vertex(1, 1, 1); ...etc...

Porting over existing OpenGL code is therefore trivial. Every OpenGL and GLU library function has been imported and fully commented - no need to look through reference books for function parameters, the Code Hints will show you everything you need to see!

screenshot_codehints.jpg

The Scene Graph

The Scene Graph contains classes like 'Texture' and 'Camera' to make working with certain types of object much easier. You don't have to use them, you can use certain components or you can use the whole lot. Or you can mix and match. There is an example application that shows how to do texturing with the Scene Graph.

Using SharpGL in WPF Applications

The screenshot below shows SharpGL being used in a WPF application:

MainWindow-Final.png

There is a full article describing how to use SharpGL in a WPF application on CodeProject, at http://www.codeproject.com/KB/WPF/openglinwpf.aspx.

Updates

New to SharpGL 2.0 Beta 1

Note: Beta 1 is available on the CodePlex site only and won't be uploaded to this page until the Beta testing is complete.

  • Hardware acceleration
  • OpenGL Extensions Support
  • Core OpenGL Functionality up to OpenGL 4.2
  • A dedicated WPF library

New to SharpGL 1.83

  • Five example applications in the download
  • Numerous bug fixes, updates and optimisations

New to SharpGL 1.8

  • Three new example applications
  • Significant improvements to texturing code
  • Rollup Controls in SceneBuilder keep the interface clutter free
  • A new materials editor in the Modify tab allows quick material editing
  • All SceneObjects in SharpGL now automatically optimise themselves to use display lists. This has made the Scene drawing extremely fast.
  • An overhaul of the GDI code by Lee Davies and myself has removed the large memory leak and hugely increased the overall performace of the library and application.
  • Many more minor updates and bug fixes are documented in the updates document in the project.

You can now automatically load simple 3D objects from Caligari trueSpace files. This makes creating applications a bit more simple, as you can test the polygon classes with real objects. A Polygon in the SharpGL scene graph is very powerful, they can cast real time shadows. A set of 'Builders' have been added to the SceneBuilder application, allowing you to build polygons from scratch, play around with materials, etc.

SceneBuilder (the Test Application) is a simple application showing some of what SharpGL can do, and all the source code is bundled with it, you can use it to build many aspects of a scene.

Points of Interest

The library is great to use, the Scene Graph 'Scene' object lets you do picking, you can control objects via the mouse, even parts of objects, such as the control points of NURBS and evaluators can be moved around. The polygons can be edited by Face, Vertex or as whole objects, and cast shadows over other objects.

Many of the 'kludgy' aspects of OpenGL such as the limits on the number of lights have been smoothed over. Using lights as an example, when the scene is created, the maximum number of lights is ascertained, and you cannot go over that limit. The extent of Mouse control is amazing, and very easy to implement in your own classes. The Persistence code is some of the best code I've ever written, you can call a function in the persistence engine, passing a type of object, and immediately a File Open / Save dialog will be shown with all the available file formats there for the user to select from, then the object will be created from the file or saved to the file. This means in the SceneBuilder app, it takes about two lines to be able to load a polygon from file, with all the possible formats automatically shown.

Keep Up To Date

Up to date information on SharpGL development is available from the CodePlex site.

SharpGL is now 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.

Feature Requests

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.

License

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 www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralQuite CoolprofessionalBrisingr Aerowing15-May-13 15:05 
GeneralRe: Quite CoolmvpDave Kerr16-May-13 2:27 
GeneralMy vote of 5memberMax Holder18-Apr-13 2:25 
GeneralRe: My vote of 5mvpDave Kerr18-Apr-13 2:32 
BugWhat if (frameRate is 0) but (timerDrawing.Enabled is false) @OpenGLControl.SetupDrawingTimer() ?memberbitzhuwei16-Apr-13 16:05 
GeneralRe: What if (frameRate is 0) but (timerDrawing.Enabled is false) @OpenGLControl.SetupDrawingTimer() ?mvpDave Kerr16-Apr-13 21:25 
GeneralFantastic stuff thank you.membermakaveli_000020-Feb-13 7:33 
GeneralRe: Fantastic stuff thank you.mvpDave Kerr20-Feb-13 21:37 
GeneralRe: Fantastic stuff thank you.memberJuan Manuel Romero Martin7-Apr-13 12:12 
GeneralRe: Fantastic stuff thank you.membermakaveli_00007-Apr-13 13:23 
Questiontext in 3d spacemembertcpmv1-Jan-13 10:15 
AnswerRe: text in 3d spacemvpDave Kerr2-Jan-13 21:32 
Generaltext in 3d spacemembertcpmv3-Jan-13 4:58 
GeneralRe: text in 3d spacemvpDave Kerr3-Jan-13 7:10 
QuestionWhen I initialize the OpenGL, why I must Translate the modelview.memberzy3327197942-Dec-12 15:23 
AnswerRe: When I initialize the OpenGL, why I must Translate the modelview.mvpDave Kerr4-Dec-12 3:12 
QuestionCan't get startedmemberMWBate29-Nov-12 8:01 
AnswerRe: Can't get started - solvedmemberMWBate3-Dec-12 16:48 
GeneralRe: Can't get started - solvedmvpDave Kerr4-Dec-12 3:12 
QuestionProblem with Intel HD 3000 on laptopmemberandrea tosetto24-Nov-12 4:32 
Question2d ImagesmemberMember 778994826-Oct-12 8:21 
Questionwhyt the run result is always balckgroundmemberlxp211022-Oct-12 4:57 
Bugbug in framerate setter [modified]memberchristophe.thalet13-Oct-12 8:00 
GeneralRe: bug in framerate settermvpDave Kerr13-Oct-12 23:27 
Generala problemmemberxitangzi16-Jul-12 22:57 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130619.1 | Last Updated 11 Dec 2011
Article Copyright 2002 by Dave Kerr
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid