Cross Platform
Android
iOS
Mac
 

Hello GLCube

This tutorial provides developers with a getting started guide to writing their first "Hello, GLCube" application. All the neccesary steps required to create, build, compile, and debug this "Hello, GLCube" OpenTK application will be covered.

Overview

MonoDroid provides two different mechanisms for OpenGLES support:

  1. By using the OpenTK.dll assembly included with Xamarin.Android and using the OpenTK.Platform.Android.AndroidGameView helper class.
  2. By using the Javax.Microedition.Khronos.Egl.IEGL11 interface exposed by the Javax.Microedition.Khronos.Egl.EGLContext.EGL11 property. This has the advantage of being what the Android platform natively exposes.

This tutorial covers the OpenTK mechanism. Using the OpenTK API has the advantage that the code will work also on Xamarin.iOS.

Source

The source for this sample is in the monodroid-samples/GLCube directory.

Code Walkthrough

Before we can get to AndroidGameView use, we need to cover startup.

Activity Startup

The GLCubeActivity.cs file is responsible for launching the application. This is done by using the Android Resources infrastructure. Specifically, the call to SetContentView(R.layout.main) causes the Resources/layout/main.xml to be loaded and parsed. Since main.xml refers to a <mono.samples.gLCube.PaintingView/> element, the Android runtime will load the Mono.Samples.GLCube.PaintingView type found in PaintingView.cs, and the FindViewById(R.id.paintingview) further ensures that a PaintingView instance is created.

Note: Since PaintingView is being created via the XML file, PaintingView must provide a public constructor accepting an System.IntPtr instance, which refers to the native proxy for the PaintingView type.

AndroidGameView Use

PaintingView is an AndroidGameView subclass, which is the Android equivalent of the iPhoneOSGameView (we mention this only because iPhoneOSGameView currently has documentation, while AndroidGameView doesn't yet).

The major logic is done by overriding the AndroidGameView.OnLoad method, and within PaintingView.OnLoad() we add new event handlers to the AndroidGameView.UpdateFrame and AndroidGameView.RenderFrame events. The UpdateFrame handler modifies some member variables each time UpdateFrame is invoked, while RenderFrame calls PaintingView.RenderCube(), which performs the actual OpenGLES calls via OpenTK.Graphics.ES11.GL methods.

Finally, PaintingView.OnLoad() calls AndroidGameView.Run(), which starts the thread loop, causing UpdateFrame and RenderFrame to be invoked in succession until the app exits.