Click here to Skip to main content
Email Password   helpLost your password?

Screenshot.jpg

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!

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 five example applications in the download that show you how to use some common features. The SharpGL Website also has a set of tutorials that is regularly updated - as well as support information.

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.

New Code and Updates

New to SharpGL v1.83

New to SharpGL v1.8

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

More Tutorials & Features

I am currently spending a large amount of time on SharpGL, after a few very quiet years. If you want more tutorials or new features, now is the time to let me know! Either leave comments on this page or get in touch with me via the SharpGL Website .

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalplease help me
nila12
1:44 13 Mar '10  
please help me
i need a code project opengl

e-mail: elee_saran@yahoo.com
GeneralSharpGL in a User Control
jpkrein
7:01 10 Feb '10  
I'm having trouble getting OpenGLCtrl working as a user control. It works fine on the form directly, but when I wrap it around the user control, nothing is displayed.

Any clues? The Draw() function is being called...

Thanks.
QuestionRendering Directly to a texture buffer or pBuffer.
jmquinlan
14:04 26 Jan '10  
Hey,

I'm using multi-threading to render a fairly computationally expensive scene in the background.

I have this working with standard System.Drawing using the OffscreenDC but its extremely slow if I'm using high resolution rendering.

So what I really want to do is hardware accelerate the rendering since it currently

So is there a way to set an open gl texture as the current render target, so that I can render whereever I whenever I need too. Then I would just need to flip the buffers on the main thread once the rendering task is complete, or even get the contents of the texture to a Bitmap object to be applied to a picture control or something.

I guess I'm looking for the equivalent of the FrameBuffer / RenderBuffers?

regards
Jm
QuestionProblems with 2 OpenGLControls
andrea tosetto
4:21 3 Nov '09  
First many thanks for this code, it's great!!!

I made a little application which shows a box in a form, I would like to use 2 forms that show the same box from 2 different point of view.

I've got some problem when the 2 opengl controls are on the screen:

when I try to reset(just make a LoadIdentity on model matrix) one of the controls sometimes it is the other control that is reset.

Also if I try to draw a different surface on one control some times it is the other one that draw the surface.

I hope that someone could help me!

Many Thanks,

Andrea
GeneralCan not get link to the sharpgl website?
Danielsue
22:48 14 Oct '09  
Many thanks to Dave for sharing his codes, it's really a good job.
But I cannot get link to the website "www.sharpgl.com" recently?
GeneralLow Polygon Limit?
cbankier
20:13 4 Oct '09  
Hi,
I've set up a very simple test scene, which simply draws a bunch of cubes. The trouble I'm having is that using SharpGL, only about 18 cubes get drawn, and any number beyond that simply don't appear.

I have set up the exact same sample using c++ and glut, and it works perfectly to any number of cubes.

The scene is simply a modification to the first example, with the spinning cube and pyramid, that ships with SharpGL. I've simple added a loop and offset the position to draw multiple cubes - the code is too trivial to post really, but I can if that helps.

What limit am I hitting? It doesn't seem reasonable that I'm limited to such a low number of polygons in a scene before they stop being drawn. There is no fancy lighting/texturing or anything, and my system can handle much more demanding scenes using c++ and glut, and I get the same result on two different computers.

Is there anything I am missing here to make this simple test work?
Any help appreciated!
Cheers,
Colin.
GeneralRe: Low Polygon Limit?
cbankier
19:37 7 Oct '09  
I managed to find a workaround for this problem, so I thought I'd post what I did so that it may help implementing a proper solution in a future release.

I found that when I enabled the error checking code in OpenGL.cs, everything worked fine, but without it, half my scene was not being rendered. It seems something was causing the error flag to be set and preventing any further rendering. Simply calling GetError() cleared the flag allowing rendering to continue.

I wasn't able to determine what error was set, and why, but simply modifed the error checking to clear the flag before a Begin and after an End call. This allowed the scene to render properly. Obviously there would be some performance overhead, but this was acceptable for my purposes.

Hopefully this helps solving this issue properly in a future release.
Cheers,
Colin.
GeneralRe: Low Polygon Limit?
filipus5
7:17 25 Oct '09  
I had exactly same problem and it took me about 3 hours to find out whats happening. Than I read your post and it was solved in a minute. Thank you very much for posting that solution. I have no ideal what is causing thous problemsFrown I always used standard GLUT libs for OpenGL and this looked like much better solution for me, but if there are some more problems like that one I will have to move back to C++ and GLUT Frown

Filip
GeneralMemory leak...
WE-JP
18:50 25 Sep '09  
First of all, great work. This will save me a lot of time.

However, I'm getting a problem with some kind of memory leak. I basically added the pertinent lines from example3 into an existing project, then changed the content of openGLControl1_OpenGLDraw to code I have working in C++. After changing all the gl commands to SharpGL, it seems to be fine at first, but when I run the program the memory usage quickly tops out. Even if I'm not running the program, just having the form designer tab open will cause the same leak.

Any suggestions? Anyone else having this problem?
GeneralRe: Memory leak...
noname00
23:48 26 Sep '09  
I have just opened example1.exe and have the same problem. Memory usage increases very fast.

I do not have the knowledge to test this, but it seems that a big amount of memory is being allocated when calling
e.Graphics.DrawImageUnscaled(OpenGL.OpenGLBitmap, 0, 0); and I suppose it is not being released.

I hope this will be fixed, because it is a very useful project.
GeneralRe: Memory leak...
semeltheone
11:49 12 Oct '09  
You can solve the problem by copying the OpenGLBitmap into a temporary image and calling dispose on that image.

OpenGLControl.cs :
Bitmap bitmap = OpenGL.OpenGLBitmap;
e.Graphics.DrawImageUnscaled(bitmap, 0, 0);
bitmap.Dispose();

GeneralRe: Very thank you
topo4949
3:31 30 Oct '09  
I thank you for this source.
I am difficult for memory leak..

thank you
GeneralRe: Memory leak... [modified]
WE-JP
22:09 25 Jan '10  
Thanks for the suggestion, however, it hasn't worked for me.

Where do you suggest I add this? I added it to the control_OpenGLDraw method, but it didn't change anything.

As a note, I work around this problem by setting the frame rate to zero, then calling a method manually whose body is the same as what my _OpenGLDraw method would be. This solves the problem.

modified on Tuesday, January 26, 2010 3:36 AM

GeneralRe: Memory leak...
Kevin Stumpf
2:07 2 Mar '10  
Go to OpenGLControl.cs and change DrawImageUnscaled to

using(var bmp = OpenGL.OpenGLBitmap)
e.Graphics.DrawImageUnscaled(bmp, 0, 0);

Kevin
GeneralThank You Dave
nik195
13:12 7 Sep '09  
many thanks for sharing your example code
It helps me learning OpenGL
I wish I could ask you some questions

Thank You
Nik
GeneralSharpGL Website
Dave Kerr
2:06 22 Aug '09  
Hi all,

Apologies to everyone who needs the website - my hosting package has run out and I'm moving to a new system, it'll still be a week before it's up I'm afraid!
GeneralRe: SharpGL Website
alexche
6:21 9 Sep '09  
hey,
thanks a million for sharing your work.
can you just tell us when the website will be up again? and maybe upload the files on a free server in the meantime?
thanks again!
cheers!
GeneralImproving texture quality
tom20
22:00 20 Aug '09  
I just wanted to point out that you can improve the texture quality by fixing a mistake.

In Texture.cs Create function:
// Set linear filtering mode.
gl.TexParameter(OpenGL.TEXTURE_2D, OpenGL.TEXTURE_MIN_FILTER, OpenGL.LINEAR);

Should be:
// Set linear filtering mode.
gl.TexParameter(OpenGL.TEXTURE_2D, OpenGL.TEXTURE_MIN_FILTER, OpenGL.LINEAR_MIPMAP_LINEAR);

I also added
gl.Build2DMipmaps(OpenGL.TEXTURE_2D, 4, width, height, OpenGL.RGBA, OpenGL.UNSIGNED_BYTE, pixelData);

To use mipmapping you have to go to OpenGL and wrap the Build2DMipmaps function.
GeneralNURBS control points location
student121
7:05 24 Jul '09  
Hi,

Great project!!!
Can you give a simple example of how to change the location of the control points (by code, not with the mouse).

Thank you.
Generalwww.sharpgl.com down
darck-pl
23:25 22 Jul '09  
Anyone who has got any materials from sharpgl.com site please send me to fdeebd@jadamspam.pl or share for example here: http://www.easy-share.com/en Author doesn't answer emails...
GeneralWebsite down?
tjdetwiler
11:27 9 Jun '09  
I noticed sharpgl.com isn't loading... Is this permanent or temporary?
GeneralRe: Website down?
ayotunde
3:21 16 Jun '09  
yeah..i noticed too.. Unsure
Question#D Game
MOSTAFApro
22:09 21 Apr '09  
hi.i need a simple 3D game free source.please help me.
my email is mostafa.goodarzi1@gmail.com
thank you
JokeRe: #D Game
martin_bfg10k
11:11 3 May '09  
Take a look here
GeneralOrthographic projection in SharpGL?
sanctus2099
8:13 21 Apr '09  
I tried to set it up before drawing anything. I noticed that the camera is setup by sharpgl before a draw is made. The thing is I want some 2D drawings and thus I need a orthographic projection to draw with after I draw the 3D scene.
I used a normal code which should work in C++ but it here it doesn't.
If anyone found out how to do this please tell me.
Also I can't understand how I can use a arrays for faster drawings.


Last Updated 15 Jan 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010