Cross-platform OpenGL rendering in Xamarin - c#

We're developing very similar applications for Windows and Android using Xamarin platform and C#. Both will incorporate some on-screen and off-screen 3D rendering, such as 3D mesh visualization.
We'd like to implement this as cross-platform as possible. How is this usually done in Xamarin? Is it better to do rendering in C++ code, or is there a C# wrapper with abstractions over platform-specific context creation etc.?

After some time I can answer my own question.
At least in my case it turned out to be more convenient to use OpenGL from C++. We just built a shared library from C++ code and link it to .NET dll's using DllImport.
Our tool for building C++ libraries is CMake, however you can use other build systems, such as new features of VS2015.
One particular benefit of this solution is that you can reuse this code anywhere else: in iOS application, desktop application for Windows, Mac, Linux, etc.

Related

How to Render OpenGL on Android with C#?

I have an existing application written in C# for .NET 6 that renders OpenGL with the lightweight OpenTK (v4) OpenGL bindings, which support .NET 5+, Open GL 3+, and OpenGL ES 2+.
According to the OpenTK FAQ:
OpenTK runs on Windows, Linux, and MacOS X.
(Older versions of OpenTK supported Android and iOS as well; current OpenTK might work on these, but it’s untested.)
I am excited by .NET 6's promise of one unified platform, but I have only seen cross-platform mobile projects that use Microsoft's new .NET Multi-platform App UI (.NET MAUI) framework. MAUI appears to be a web view shell that contains a Blazor Web App. I found the code for this shell in the src/Core/AndroidNative folder of the repository, but it seems very bloated for my purposes. I want to be able to access Android-specific functions without the bloat of Blazor and a web view. According to this blog post, Xamarin.Android might be the answer, but I am not sure how to introduce it into my existing project.
All I need is a simple shell that:
Creates an Activity with a GLSurfaceView
Obtains an OpenGL ES 2 context
Runs my current C# code with that context
I would prefer the shell to be in C#, since introducing Java code would complicate my build process and fragment my code base.
How can I introduce the needed Android-specific code?
How will this change my build process?
I know that I need to write OpenGL ES-compatible shaders and that I probably need to change how I save files. What other changes will I need to make to my current code to ensure Android compatibility?

Is C# used in iOS and Android projects created by VS2017?

I've recently started learning about Xamarin so please bear with me. I'm not sure if this is an obvious question
Creating a cross-platform mobile app in VS2017 will create 3 projects: Xamarin.Forms, iOS, Android.
So in my Xamarin.Forms project I was able to add a map to my Form, and now I can see the map in my Android emulator. That works great. I havent't touched the other two projects.
Now I have to add some functionality that's native of each platform, so I would need to go, for example, to the Android project to add the widgets, logic, etc.
Is C# used in these native projects or do I need to learn a new language?
I'm asking because Android Studio uses java and iOS uses Objective C, so I'm not sure if I need to learn these languages for cha
If you check the Microsoft website for Xamarin.Forms it answers your question then and there;
Xamarin.Forms expose a complete cross-platform UI toolkit for .NET developers. Build fully-native Android, iOS, and Universal Windows Platform apps using C# in Visual Studio.
But even F# is available as an option.
As far as your platform-specific question goes there is a whole separate guide that teaches you how and when you need platform specific code which can be found here
Is C# used in these native projects or do I need to learn a new language?
I'm asking because Android Studio uses java and iOS uses Objective C, so I'm not sure if I need to learn these languages.
Yes, that is the whole point for the Xamarin native and Xamarin.Forms framework that pure C#/F# developers can become mobile application developers without learning a new language.So existing .Net teams can execute mobile projects.
But in some cases, you might have to convert native Java and Objective-C code to C# so it's always better if you have a basic level of understanding in them.
I will suggest you read the Code Sharing Strategies available in Xamarin Forms before deciding which method do you wanna follow: I personally and the Community prefers using The .Net Standard methodology to share the code you can choose for yourself.
Good luck with your future endeavours and feel free to revert in case of further queries.
Yes, all Xamarin projects use C# (or F#). You do not need to use java or Obj-C/Swift, although there are ways to incorporate native libraries written in those languages into Xamarin projects.
Xamarin has a entire section of docs on how to incorporate platform specific behavior into a Xamarin Forms app.

DLL project should be of which type for cross platform use

We have one application for both C# .NET and Apple iPad. This application will perform similar functionalities. For this we have one protocol layer which we are thinking to keep as common code. For this we are thinking of creating a C++ dll for the protocol module so that it can be used across both C# and iPad. For creating a C++ dll, I have a basic question:
While creating a dll project, which option should we select? We can create a dll for MFC, Win32, ATL etc. What would be the best option for my requirement?
You should not use MFC as these are the Microsoft Foundation Classes not available on iOS anyway. Probably Win32 would be your best guess - but make sure not to include any non-standard Windows header files if you want to use the DLL in non-Windows environments.
I would recommend to frequently compile your file in both environments. You might also want to take a look at multi-platform libraries like boost if you need advanced functionality.
I recommend looking at Mono for building C# apps on both Windows and iOS.
If you want to target the iPad, you need to build for iOS, not Mac OS.
You cannot build dynamic link libraries for iOS, only static libraries. Note, there are equivalents to DLLs on iOS, but only Apple can build them (or you can build them yourself if your iPad is jailbroken, but this will disqualify your app from the AppStore).
iOS is not related to Windows in any way, so Win32 libraries will not run on iOS. Your generic 'protocol module' (if you mean low-level code that can interface to other devices over TCP/IP or similar) will need to have significant differences depending on which platform it is running upon.

How to "install" Directx (or other graphic SDK) in C#?

Is there any possibility to write Directx applications using C# ? How to install directx sdk for C# ? And what is the best (or most used) graphic library (framework) for C# ?
You will find an installer included with the DirectX SDK, stored in the redist folder after you install the SDK on your machine. The SDK download is here. You only need it when you write native code or target .NET version 2 or earlier. .NET 3 and up already has a dependency on DirectX so its installer already covers your needs.
By far the most popular graphics libraries in .NET programming are System.Drawing and System.Windows.Media. They are wrappers for, respectively, GDI+ and a DirectX wrapper named Milcore. GDI+ uses software rendering and is thus highly compatible but not particularly fast. Milcore is used by WPF, it doesn't expose all of the DirectX capabilities, just the ones that a WPF program needs.
For game programming, the XNA framework is a common choice. Biggest reason to use it is its capability of running on multiple platforms, Windows, XBox and Phone, wrapping their respective low-level graphics libraries. DirectX on Windows has plenty of 3rd party wrappers for it, SlimDX is popular. There used to be one from Microsoft named "DirectX For Managed Code" that had wrappers in the Microsoft.DirectX namespace but they deprecated it. The last SDK that included it was February 2010. Not recommended, XNA replaced it.
You should use SharpDx...
http://code.google.com/p/sharpdx/
I recently asked a question about SlimDX, which is a .NET Wrapper around DirectX. It's pretty up to date and more flexible than XNA (for instance, for scientific visualization apps). There are some tutorials although i'm told as its so similar to DirectX all you need to know is how to setup a device and then just use the DirectX equivalents from there.

Is it possible to use WPF on unix?

I heard about Mono project but only in regards to winforms. Will it work with WPF?
WPF under Mono does not exist.
At this point, no group in the Mono project has plans to implement Windows Presentation Foundation APIs as part of the project.
The mono team propose using Silverlight/Moonlight instead:
Silverlight implement a subset of the WPF APIs and is available on Windows, MacOS X and through our own open source effort Moonlight it is available on Linux and other Unix systems.
There is a library called Silverform SDK that aims to provide cross-platform WPF and Silverlight implementation.
The library is implemented in managed code and currently works with OpenTK and Unity3D as render backends. Major functionality, such as binding, layout, main controls and primitives, has already been implemented (check Unity web player demos here). Initially it has been focused on Unity3d render, while support for standalone Mono applications will be added as a separate build in the future.
Disclaimer: I am one of the developers of the library.

Categories