Sign in to follow this  
ThreeDParadox

OpenGL c# and OpenGL

Recommended Posts

Im looking for a tutorial on implimenting openGL into c#. I DONT want to use all the precompiled dlls and libs im finding like CsGL and TAO and such. Im looking for an understanding of doing it myself. If anyone can shed some light on this i would appriciate it.

Share this post


Link to post
Share on other sites
You can import DLL functions in C#, which is how you would access OpenGL without a 3rd party library.

Share this post


Link to post
Share on other sites
I don't use tao either, as it's enormous. I prefer to just create bindings to opengl functions as I need them. It's easy:


public enum DrawMode : int
{
Points = 0x00000000,
Lines = 0x00000001,
LineStrip = 0x00000003,
LineLoop = 0x00000002,
Triangles = 0x00000004,
TriangleStrip = 0x00000005,
TriangleFan = 0x00000006,
Quads = 0x00000007,
QuadStrip = 0x00000008,
Polygon = 0x00000009
}

public sealed class gl
{
private const string GLLIB = "OpenGL32.dll";

[DllImport( GLLIB, EntryPoint = "glBegin" ), SuppressUnmanagedCodeSecurity]
public static extern void Begin( DrawMode drawMode );
}


Share this post


Link to post
Share on other sites
what about setting up the pixel format. And you would have to import every single function you will be using? Does anyone have an example app. somewhere on the net i could browse?

Share this post


Link to post
Share on other sites
You may have to import all of the functions related to GDI/DCs from the Windows DLLs that they are stored in. However, if you can somehow get a context from a form window by using some .NET method, then that would save a bit of work.

Yes, you have to import every function that you plan to use.

CsGL's source code is available from their sourceforge page, including the part where one sets up the window format. Of course, if you model your source after theirs, it would only be proper to credit them for that.

gl.h and glext.h have all of the enums listed, like shown in gharen2's post for the basic primitive types.

I admire you for your bravery. This is the very reason why I "wimped out" and went with TAO when using C#. :)

Share this post


Link to post
Share on other sites
Yes you'll have to import every function, but once you've done it a few times it's easy, and you only have to do it once per function. Creates a nice little growing library of imports you can reuse elsewhere.

If you want to speed up the process a bit, don't do it the way I did. Just setup constants instead of pretty c# enums. I only made them enums because a) it's prettier [smile] and b) it prevents you from using the wrong constant in a function (for example, GL_LINES versus GL_LINE).

As taby said, all you'd have to do is look at the source of gl.h and import each function as I did. There's some that are trickier, such as glGetStrings: the .net framework isn't smart enough to convert the returned value to a string, so you have to do it yourself.

Window initialisation is trickier, you can either setup imports for the appropriate win32 functions, which you can find in many opengl samples online. Or you can use SDL to do it for you.

And yes, you can use opengl in a windows form or control. All opengl needs is a handle to a window, which the Form.Handle property supplies you.

Oh, and you can use Marshal.GetDelegateForFunctionPointer to setup opengl extensions.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this