OpenGL ES Support on Mobile with XE6


Appmethod, RAD Studio, Delphi and C++Builder XE6 all make it really easy to work with OpenGL ES on mobile devices. Under the covers FireMonkey is implemented with OpenGL ES on mobile (iOS & Android), OpenGL on OS X and DirectX on Windows. It provides a number of useful abstractions for working with 2D and 3D graphics, but sometimes you just want to get down to a lower level.
Here is all you need to access an OpenGL ES rendering context in your FireMonkey mobile application. This example is in Object Pascal, but should be easy enough to adapt to C++.

Create a new FireMonkey Mobile application
Select 3D application
Add FMX.Types3D to the Interface uses clause
In the Object Inspector, create a new event handler for the OnRender event for your form
You now have access to the OpenGL render context.

You can work with the TContext3D that is passed in via a parameter, and your code will work across platforms automatically. If you want to work with the OpenGL ES APIs directly you can do that too with the following uses clause in your Implementation section:
// Gives you access to the FMX wrappers for GLES
// Direct access to the Android GLES implementation
Androidapi.Gles, FMX.Context.GLES.Android;
// More useful units for Android
//, FMX.Platform.Android, Androidapi.Gles2, Androidapi.JNI.OpenGL,
// Androidapi.Glesext, Androidapi.Gles2ext;
// Direct access to the iOS GLES implementation
iOSapi.OpenGLES, FMX.Context.GLES.iOS;
// More useful units for iOS
//, iOSapi.GLKIT, FMX.Platform.iOS;
And here is an example event handler with a couple calls to the OpenGL ES APIs:
procedure TForm1.Form3DRender(Sender: TObject; Context: TContext3D);
glClearColor(1, 1, 0, 1);
This accesses the iOS and Android equivalents of the same OpenGL ES APIs. Thanks to the compiler directives, and the cross platform nature of OpenGL ES, this code just works. I’m not an OpenGL expert, but I looked through the OpenGL ES API and all the routines I tested worked, but I never did anything interesting with them.

Comments are closed.