C++ OpenGL ES in GUI

I am considering using Xamarin.Forms for a project (Android, iOS and WP) that will require 3D rendering and am planning on using OpenGL ES to do the rendering. Now I know that Android and iOS have support or an OpenGL view in Xamarin.Forms that can be rendered to using OpenTK. Unfortunately GLES is only available in C++ using ANGLE on WP at the moment. I do though know that both Android and iOS have support through GLES in C++. I would therefore like to know if I would somehow be able to hook C++ GLES code up to a view in Xamarin.Forms?

If not, would it in any way be possible with Xamarin, as in even if I use native UI kits?

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    If you can do it at all on iOS and Android then you can most likely do it from Xamarin.Forms using a custom view renderer.

    I'm not sure your assumption that it can only be done in C++ is valid, though. Does this not do what you want to do? Or for Android maybe this?

  • GerharddeClercqGerharddeClercq ZAMember

    @adamkemp My larger problem is that if this is possible, how would I go about achieving this as it has me pretty stumped at the moment?

    PS. I know I can use C# for GLES on iOS and Android but my problem is with Windows Phone.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    I don't know much about Windows Phone, but the principle is the same: if you can get GLES rendering working at all in a view then you can do it in Xamarin.Forms. So you can reduce the problem to a simpler one: how do you do OpenGL rendering on Windows Phone in general? Once you've solved that you can apply a custom renderer in Xamarin.Forms to get it in Forms.

  • GerharddeClercqGerharddeClercq ZAMember

    @adamkemp I know how to do this on Windows Phone using the native XAML SwapChainPanel and I can create a custom Xamarin.Forms control from the Xamarin and then connect it to the C++. My problem is that with Xamarin I can only figure out how to use C#.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Is this .Net C++ or plain unmanaged C++? If it's .Net then I assume you can produce a .Net assembly, which can then be consumed by Xamarin as a reference just like any other .Net assembly. If it's unmanaged C++ then you will have to create a C# wrapper using P/Invoke and then statically link the C++ code in to your build. It's a pain, but it's still doable.

    It's still not crystal clear to me what you have in mind for your architecture. If you could show me a sample application showing how this would work without Xamarin then I might be able to give more specific advice.

  • GerharddeClercqGerharddeClercq ZAMember

    @adamkemp The C++ has not actually been written yet. I would just like to be able to write stock OpenGL ES in C++ and then let it render to an element on my Xamarin GUI. Now the part of just writing the GLES in stock C++ can easily be achieved on all three mobile platforms simply by referencing the OpenGL ES2 libraries. You can just look at any stock standard C++ OpenGL ES 2 code (like you can get from Khronos) if you want something to work with.

    My problem is the way in which I have to hook up the GLES to a rendering surface. In Windows Phone this will be easy because Xamarin.Forms builds on top of the native XAML ui system which provides me with a rendering surface that I can just pass to .Net C++ simply by adding a tiny bit of CX or whatever Microsoft call their proprietary C++. In this senario I have full access to the native controls and can manipulate and access them in native ways.

    Now for iOS and Android I am at a loss with Xamarin because I have no idea how to link the C++ GLES. On Android for example all the examples explain how one can either use the Android EGL to get a full screen surface and then just render your entire screen full with GLES. The other documented option is that of creating a GLES surface in the Jave GUI and then using Android C++ glue to hook it up to the C++ GLES. For iOS I am assuming that something similar is possible using Objective-C and C++ and the native iOS GUI. Now on Xamarin the confusion comes in where one suddenly does not directly work with the native GUI anymore but instead with the Xamarin C# wrappers and I have no idea how to hook up a Xamarin GLES surface in either Android or iOS to the GLES because I cannot simply do it like one would in Objective-C or Java.

    Now I have not really looked into the iOS implementation all that much yet as I am still stuck on Android. With Android in Xamarin I firstly cannot see a GLSurfaceView in Xamarin that I can even try to use in the normal Android way. Secondly if I look at the standard Android OpenGL template then I can see that the underlying Xamarin system just implements and hooks up the whole system using OpenTK directly. It therefore does not appear as if though I have any way to get access to the native android GLSurfaceView so that I can hook it up to the C++ in the normal Android way.

    Please correct me if I am missing something here.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Why do you want to use C++ for this? That seems to be what makes this trickier than it has to be. Where does that requirement come from?

  • GerharddeClercqGerharddeClercq ZAMember

    @adamkemp As I have already explained (but probably not very well), my problom is that OpenTK is not available on Windows Phone. Only C++ GLES is available on WP and both iOS and Android support C++ GLES in their native configurations. I therefore assumed that it should be feasible to use it as a cross platform solution for 3D rendering. I guess I might also be able to get a bit of a performance boost but that is not really all that important. Furthermore though, using plain GLES will allow me to use the renderer in a website two because I will be able to compile it to WebGL using emscripten.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    The iOS example I linked to above appears to be using an API that also just wraps a C++ API. That makes me think that if you do some of the setup steps that it does then you could probably call into your own C++ code to do the real rendering. I don't know nearly enough about the APIs to tell you exactly how to do that, but I do believe it's possible, and I think if you study that example and pick it apart enough you may be able to figure it out. It gets you an OpenGL surface as a starting point. You just have to figure out how to insert your own code in the rendering part.

  • GerharddeClercqGerharddeClercq ZAMember

    @adamkemp I have been looking around at some more Android and iOS GLES examples and have found that it might be possible. It seems that as long as one can manage to call the draw and create functions in C++ when they are called for in C# then it should work. I am assuming this because after looking in more detail at the examples I found that in neither the Android nor the iOS code was the surface ever registered on a so called "renderer" class that receives these events. I will have to check if it works tomorrow but I did not think that it could be this simple considering the WP implementation actually requires registering the surface to the renderer. I have though also found the native GLKViewController and GLSurfaceView components in Xamarin so hopefully that should also help. Here's hoping for the best.

    PS. If correct, I am assuming that the proper OpenGL backend in iOS and Android as oppesed to the DirectX wrapped one in WP implements EGL in such a way that it cycles through all the generated surfaces and that whatever rendering code is run will be rendered to the currently active surface. Therefore I would assume that only some careful timing (responding to events) is needed. But I could be wrong...

  • SingingSpanielSingingSpaniel USMember

    @GerharddeClercq I am looking into the same thing, running a C++ render loop inside an OpenGL view in Xamarin.Forms. Have you made any progress?

    Thank you

  • GerharddeClercqGerharddeClercq ZAMember

    @SingingSpaniel yes, I'm making steady progress. I'll have an Instructables and a template ready before the end of the week if you don't mind waiting.

  • SingingSpanielSingingSpaniel USMember

    @GerharddeClercq, the tutorial looks great! Thank you for sharing

  • GerharddeClercqGerharddeClercq ZAMember

    @SingingSpaniel I'd appreciate it if you (and anyone else reading this) can vote for it in the "Coded Creation" competition (http://www.instructables.com/contest/codedcreations/ or just the vote flag on the top right of the post).

Sign In or Register to comment.