OpenGL ES Support on Mobile with XE6

OpenGL ES logoAppmethod, 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++.

  1. Create a new FireMonkey Mobile application
  2. Select 3D application
  3. Add FMX.Types3D to the Interface uses clause
  4. In the Object Inspector, create a new event handler for the OnRender event for your form
  5. 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:

uses
  // Gives you access to the FMX wrappers for GLES
  FMX.Context.GLES, 
{$IFDEF ANDROID}
  // 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;
{$ENDIF}
{$IFDEF IOS}
  // Direct access to the iOS GLES implementation
  iOSapi.OpenGLES, FMX.Context.GLES.iOS;
  // More useful units for iOS
  //, iOSapi.GLKIT, FMX.Platform.iOS;
{$ENDIF}

And here is an example event handler with a couple calls to the OpenGL ES APIs:

procedure TForm1.Form3DRender(Sender: TObject; Context: TContext3D);
begin
  glClearColor(1, 1, 0, 1);
  glClear(GL_COLOR_BUFFER_BIT);
end;

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.

This entry was posted in Android, Graphics, iOS, Mobile, Source Code. Bookmark the permalink.

11 Responses to OpenGL ES Support on Mobile with XE6

  1. usagi says:

    Great, Radek.
    This could be a big adventage for games creators.
    Can imagine to prefer using cross platform developing with delphi to build games, not application. Maybe this is more accurate path for delphi than making application? In case of games the size of executables don’t scary..

  2. Definitely. Supporting OpenGL on most platforms but Direct3D on Windows makes it very difficult to do anything that requires custom shaders if you want your project to be cross-platform. Having an OpenGL canvas for Windows is a necessity in a case like that.

  3. Ray says:

    Printing is NOT working on Android 4.4? (sorry out of topic)

    Can you please open the ShareSheet program from Mobile Snippets folder. (C:\Users\Public\Documents\Embarcadero\Studio\14.0\Samples\Object Pascal\Mobile Snippets\ShareSheet)

    Please click the “Share” button to open the ShareSheet, the CloudPrint option should be in the list; but it is NOT in android 4.4.

    Can you please help in finding a way to print images from android 4.4 devices?

    Thank you
    ray

  4. Pingback: OpenGL ES Support on Mobile with XE6 | Delphi P...

  5. Hevin Jose says:

    Many companies in India are developing applications for ios, android, web and windows. Especially in Bangalore are best to developing apps in various purpose like social media, games, ticket booking, etc. Most of the companies like pacewisdom are first prefer to develop mobile apps . This is a development company have many experts to develop the apps within the certain period with good quality.

  6. Pingback: Ten Tips And Tricks For Building Games With Delphi XE5 And XE6 On Android And IOS | Delphi XE5 Firemonkey, Delphi Android, Delphi IOS

  7. Tomek says:

    Do you have any function other the glClear working? I tried to port GLES Triangle sample but nothing is visible except changed color of the backround

  8. Joshua says:

    This is not working in C++. I include the right files:

    #include
    #include
    #include
    #include

    and this is my Form3DRender function:

    void __fastcall TForm3D2::Form3DRender(TObject *Sender, TContext3D *Context)
    {
    glClearColor(1, 1, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    }

    But I get the errors: use of undeclared identifier ‘glClearColor’,
    and: use of undeclared identifier ‘GL_COLOR_BUFFER_BIT’

  9. Jim McKeeth says:

    With the undeclared identifier error it sounds like you need an additional include.

  10. Pingback: FMX:OpenGL-Panel oder Layout in 2D Formular - Delphi-PRAXiS

Leave a Reply