View Full Version : c# UI c++ core Opengl

Ruggero Visitnin
12-14-2014, 01:13 PM
Hi all,
i spent some weeks studying how to create a solid UI for my Opengl Engine and i chosen C#, i built a C++/CLI dll for Core OpenGL Engine, the only problem is how can i create a context in my c# UI in which draw OpenGL?
My first choice, was to code my own context like i did for WIN32 but after some search on the web i did not find anything on how to do it, what i found is that a lot of people use OpenTK to do OpenGL in c#.

So, i have only two questions: the first is, since OpenGL draws in the thread where is located, can i use OpenTK only for context creation and then call all my OpenGL functions from C++ code?
if the answer for the first question is affirmative, can i use GLEW in c++ without find big pain?

thanks advance.

01-09-2015, 03:25 AM
provided there are TONS of examples over internet:
here's one by codeproject:
Unmanaged C++ OpenGL code in C# form (http://www.codeproject.com/Articles/127141/Unmanaged-C-OpenGL-Drawing-and-C-WinForms-WPF-inte)

You can both create a window with C# and then only calls opengl functions from C++, or setup the window in C++. the link above explain the 1st alternative, while in my opinion you should do the 2nd alternative(you create a context with some library like GLFW or SFML), the 1st is usefull if you want to use some winforms, but since you already have your GUI and you want to render from C++), I would instead setup all rendering from C++ so that you can more easily setup function pointers (you call from C# the C++ function that setup everything), after that the C# part of your program becomes something for managing stuff and lifetimes (because in example there's some application logic that's critical and you want C# syntax to handle it, I was thinking to persistent data in example) without you have to warry about memory allocation. Of course you should keep into account the overhead for wrapping C++ functions so if you are in performance critical areas you have to do some up-front design to decide the granularity of wrapping C++ with C#.

If you create the window with C++ and get function pointers you won't get many troubles (context/window by SFML, function pointers by GLEW and you are done)