OpenGl Window or other options in C++

Hi,

I am trying to develop a plug-in for Rhino that has a separate window to hold some graphical information - lets say like Grasshopper does....although it is completely different and much simpler, I still need a separate 2D "GUI" window (Mfc Dialogs wont do). So I have been thinking of doing it in OpenGL but I don`t know how to create and open a new window with an OpenGL context, within a Rhino plugin. Any advice on that? Or maybe a short example?

I also had the idea of using some libraries like SDL or SFML for my GUI. Is it possible to do that? I assume I would have to link them statically somehow, but is it still possible for everything to be “contained” in the .rhp at the end?

Long story short, I need to create a simple graphical GUI window within my plug-in, in C++, and any advice on a good (and possible) method would be highly highly appreciated.

Thanks a lot!

Toma

Hi Toma,

I order to make worthy suggestions we will probably need more information on what you are looking to create.

– Dale

Hi Dale,

it is hard for me to go into details but i made a small graphic:

I need a separate window so that I can create some “graphs” that connect different objects and thus create dependencies. Not entirely in the Grasshopper way (where “geometrical dependencies” are created) but for the purpose of a certain “grouping” of objects and later manipulation. This could maybe done in Grasshopper with some .NET programming, but I am working in C++ and I want to do it “from scratch” because I want to test some ideas that need complex object grouping and manipulation and I want complete control of the methods and the graphics.

So all I would really like to know is if there is a way for me to create a separate window that I can use to create these “graphs”…a type of GUI basically…and if that is possible I am trying to find out what would be the best way to do that…

Thanks!

Since Rhino already uses OpenGl libraries I am hoping for an answer that looks something like this:

  • Yes it is possible
  • You just have to…

Any ideas?

Thanks!

If it were me (writing in C++ and using MFC), I’d probably create a floating windows, perhaps a modeless dialog box. If you want this UI to dock, then Rhino has C++ framework for this. As for what to draw in the Window, OpenGL seems like overkill for just 2-D diagrams. GDI is probably sufficient.

Well that depends. If opening an OpenGl window within Rhino is possible and relatively easy, that leaves a lot of room for exploration. I mean if I want to interactively draw and move graphs within a modeless dialog…be able to zoom in and out…that will take a lot of research and coding (not so many examples on the internet for something like that, because I guess people don`t use mfc dialogs for that)…so if creating a separate window was possible, I would rather direct my efforts, research and coding in that direction.

I found this post: .Net OpenGL Context . It is about .NET but menno mentions that “it is being done by the madCAM plug-in, but I am not aware of the details. So it can be done.” I was just hoping for some directions if anyone has some experience with it…

I just realized that .NET has WPF…that is exactly what I need basically. But i don`t find anything that resembles it for C++.(?) Not fair…but still not a good enough reason to switch to C# I guess…

Hi…I am still banging my head about this problem…and I am not sure what to do. GDI would be a hard, slow road, and at the end it is based on raster graphics…not vector graphics like WPF…so zooming and similar types of control would be even more difficult to program…not to mention all the transformations, mouse events, etc.

Is there any way to use WPF within a C++ plug-in?

If the answer is yes, great! How does one do that?

If the answer is no, then the question is if I can make a C# plug-in (use WPF) and call C++ methods for various calculations? This is very important because I really need the C++ speed and efficiency for some optimization methods, and I need WPF for the UI… and I just have to combine them somehow.

Thanks a lot!

Yes you can interoperate a C# plug-in that has WPF user interface windows with a C++ DLL that can do calculations. We do this frequently.
The information you are looking for is called Platform Invoke (PInvoke) and allows a C# plug-in to call C++ functions that are exposed using __declspec(dllexport) inside an extern "C" {} block:

C++ side, make a DLL called MyCalculations.dll:

extern "C"
{
    __declspec(dllexport) void RunMyFunction(int a, double b, ON_Curve* crv)
    {
        // do stuff 
    }
}

C# side:

public Result RunCommand(RhinoDoc doc, RunMode mode)
{
    ObjRef cRef;
    Result res = RhinoGet.GetOneObject("Curve", false, ObjectType.Curve, out cRef)
    Curve crv = cRef.Curve();
    IntPtr pCrv = Rhino.Runtime.Interop.NonConstPointer(crv);
    RunMyFunction(4, 85.4, pCrv); // call the C++ native function
    return Result.Success;
}

[DllImport("MyCalculations.dll")] // P-invoke magic loads the C++ dll and finds the exported function
private static extern void RunMyFunction(int a, double b, IntPtr pCrv);

Thanks!

I guess that is the way I will have to do it. Just one question before I start my journey:

When i am creatting my C++ DLL, does that include a standard Rhino Plugin (RHP)? (because that is basically a DLL) In other words, can I simply take one of my C++ plugins (with all the Rhino classes and functions inside) ,expose the functions by inserting _declspec(dllexport) and putting them inside a C block and invoke it from a C# plugin?

Here are a couple of samples worth reviewing:


Yes, you can expose stuff from a C++ plug-in, just make sure that you DllImport the RHP file with the .rhp extension.