.Net OpenGL Context

Hi,

I’ve just started working with Rhino to develop a plugin that renders some custom geometry. At the moment I’ve created a class that extends Display Conduit that does visibility testing of my data and loads/unloads it from memory as appropriate (the data type is far too big to load into RAM all in one go!).

Currently I’m loading the data as standard Rhino data object types which has been ok to prove my plugin is working and doing what it should, however using these standard types does provide issues as they’re not really designed to handle what I’m drawing.

Therefore I’d like to get access to the OpenGL context so I can render the geometry with my own OpenGL code, which may include some custom shaders too.

As far as I’m aware C Sharp doesn’t ship with proper OpenGL support so I’ve downloaded OpenTK and built against it. I now have the full arsenal of OpenGL syntax available to me, however I need some kind of valid OpenGL window context to initialise it. I’ve now hit a blank as I’m not sure how to get that information from the Rhino SDK.

Has anybody else gone down this route with the RhinoCommon SDK, and if so how did you go about being able to write pure OpenGL within a plugin? Or… am I barking up the wrong tree completely and there’s a much simpler way to draw custom geometry?

Many Thanks

Mark

1 Like

I know it is being done by the madCAM plug-in, but I am not aware of the details. So it can be done.

Also, there is the Rhino.Render namespace in RhinoCommon, which may be of use.

Hi Menno,

I’ve spent a long time messing with OpenTK and in the end gave up…

However I have now got a solution working, I just did it the old fashioned way. I’ve created a class with lots of “extern” calls to the Opengl32.dll. Not pretty, but at least it is working and doing what I want now :smile:
Cheers

Mark

OpenTK should have all of these low level wrappers already in place.

Hi Steve,

That’s what I thought… but trying to call through to them always produced memory exception errors because (I believe) the correct GL context wasn’t current.

I tried all kinds of ways to appease OpenTK with pointers to different window handles and calls to “MakeCurrent” to no avail…

In the end I decided it would be quicker just to get my hands dirty and do it manually.

If I get time in the future I will revisit the problem as I’m sure it’ll just be something silly…!

Many Thanks

Mark

Ok, just to provide closure to this for anybody else struggling to get OpenTK to play ball with the Rhino OpenGL context. You need to do the following only once (I do so in the constructor of my conduit):

OpenTK.Platform.IWindowInfo wi = OpenTK.Platform.Utilities.CreateWindowsWindowInfo(RhinoApp.MainWindowHandle());

// Construct a new IGraphicsContext using the IWindowInfo from above.
OpenTK.Graphics.IGraphicsContext context = new OpenTK.Graphics.GraphicsContext(OpenTK.Graphics.GraphicsMode.Default, wi);

context.LoadAll();

What the OpenTK documentation doesn’t mention is the all important context.LoadAll() call. Without it you’ll get the memory exception errors which I was getting.

With this in place you can call OpenTK commands within the render thread for any OpenGL work you wish to do.

Hope this helps!

Mark

4 Likes