Android Open GL Texture Mapped Spheres With Multi Touch Control

earth

While writing my (note quite finished) android app I needed to be able to render texture mapped 3D spheres in OpenGL. Unfortunately the Android SDK does not include GLUT and I was not happy using free glut for various reasons. So in the end I generated my own class to render spheres, and I thought other developers might find it useful, hence this page.

This example now has two forks in BitBucket, one adding single touch control to rotate the sphere and the second example is multi touch to allow you to zoom in and out.  Details below.

The Algorithm

SingleStrip
The sphere is rendered in a series of strips made up of triangles. Each strip starts at the “north pole” and spirals around the sphere to the “south pole”. This ensures the minimum number of triangles are rendered, so there is no overlap. You can see a single strip on the right.

sphere-tessellation
Depending on the “depth” value the triangles are subdivided to increase the accuracy of the triangles to the form of a sphere. These images show a series of spheres with depths of 1,2,3 and 4.

As you can see the number of triangles increases by a factor of 4 with each increment in the depth. I think for most purposes a depth of 3 is a good compromise between smoothness and number of triangles.

The Code

You can download the complete Android Studio project using this link in BitBucket.

Testing

For development and initial testing I use Virtual Box running an Android instance.

You can also deploy and run/debug directly once you use:

adb connect machine_ip_address

The code has also been tested on a Nexus 10 (Running Android 4.3) and works well. If you spot any issues then please let me know.

Single Touch

I’ve forked the above project and made some minimal changes to illustrate how to rotate the sphere using a single touch control event. This project is here in BitBucket.

Multi Touch

I’ve forked the Single Touch project above and made some more minimal changes to add how to zoom the sphere in and out. This project is here in BitBucket.

Both touch controls are basically implemented using the instructions detailed in Vogella’s excellent article here.

Comments are closed.