Showing posts with label android app programming. Show all posts
Showing posts with label android app programming. Show all posts

Tuesday, February 11, 2014

Android Programming, 3D Images and OpenGL ES

As disused in my previous article "Unity Game Engine's Mesh Renderer, Mesh Filter, Shaders and Materials Overview" a 3D scene is defined by a collection of vertices that include attributes such as position, normals, texture values, etc. For a device to display a 3D animation scene, the scene is transformed into a 2D image through the rendering process (aka the rendering pipeline or graphics pipeline).

This articles discusses the Open Graphics Library specification for Embedded Systems (OpenGL ES) used to define shaders and other elements to render 3D scenes. It also discusses the OpenGL Shading Language specification. And it provides an overview of a simple Android application that uses the OpenGL ES to render animated graphics on Android devices.

OpenGL ES and the OpenGL® Shading Language

For a 3D scene to display there are a series of steps executed through the rendering pipeline. These steps ensure 1) only the objects within the camera's view display, 2) reflections are properly added, and 3) images behind other images are shown/hidden based on the camera's view. The output is then merged and the final image displayed. The last stage of the rendering pipeline finalizes what pixels will display based on the pixels closest to the camera and other variables mentioned. The 3D scene below, from the Stealth game, shows a game scene that has not yet gone through the rendering pipeline.



When we run the game (so that the vertices are transformed into a 2D image), notice we only see the game objects closest to the camera.  And, based on the camera's view objects behind some of the partitions are hidden. As the player moves the view shifts and more processing occurs to render the images that are now within the camera's view.


Developers who specialize in 3D and animation can program some of the stages associated with the rendering pipeline by creating programs called shaders.  Shaders execute inside the graphics processing unit (GPU) and define how the data is processed to, ultimately, display the final image. The ability to create shaders means developers can now create custom effects that were not possible when the rendering pipeline only included pre-programmed stages.

The OpenGL ES is a cross-platform API used to create and compile shaders and execute other related operations including the ability to define buffers that the shaders write to.

OpenGL Shading Language


In addition to the OpenGL ES specification the OpenGL Shading Language specification is necessary to understand the different data types that can be used including vectors, matrices, opaque types, samplers (which are handlers to textures), cube maps, depth textures for shadows, etc. It also defines the statements and structures, variables (including constants) and more. The OpenGL Shading Language support progamming the following shaders: vertex shader, tessellation control shader, tessellation evaluation shader, geometry shader, fragment shader (sometimes called the pixel shader), and compute shader.

Vertex and Fragment Shaders


Each shader type is directly linked to a processor. For example, Vertex shaders run on the vertex processor. The vertex shader is used to read the coordinates of the vertices, transform them to 2D screen coordinates as well as process other attributes of each vertex such as color, normals, etc.

The fragment shaders (also referred to as pixel shader) receives pixel data, calculates the final color of the pixel and passes it to the next stage so the final output can be produced. The pixel shader can change the depth of the pixel to define what pixels should or should not be drawn. By default, the depth defines the distance between the originating triangle and the camera. But, the developer can manipulate the distance by specifying a value that will be used when the final image is created.

(You can read a more general overview of Shaders by reading my previous article Unity Game Engine's Mesh Renderer, Mesh Filter, Shaders and Materials Overview.)

Android and the OpenGL ES


Android includes support for 2D and 3D graphics using the OpenGL ES API. Android supports several versions of OpenGL ES as follows:
  • OpenGL ES 1.0 and 1.1 is supported by Android 1.0.
  • OpenGL ES 2.0 is supported by Android 2.2 (API level 8).
  • OpenGL ES 3.0 is supported by Android 4.3 (API level 18).
Note that when you use a higher version of OpenGL ES with android; all lower versions are supported. For example, if your app uses OpenGL ES 2.0; it also supports OpenGL ES 1.0

Before you start developing your 3D app or game you will want to download and install the Android Developer Tools (ADT) Bundle and access the Android SDK Manager to download the applicable Android SDK platform, etc. For more information, please see my previous article titled:  Programming Mobile Apps for Android With Eclipse.

Note:  When you create an emulator, as outlined in my previous article, make sure you check the option to enable the GPU on the host. As mentioned, OpenGL ES apps require access to a GPU.


Displaying Graphics on Android with OpenGL ES

 

Android Manifest


When you create an Android app that supports OpenGL ES, you must add a declaration to the AndroidManifest.xml file. The uses-feature statement to be added includes the glEsVersion attribute, which provides a way for developers to define the OpenGL ES version required to run the application. For example, if OpenGL ES version 1.0 is required; the glEsVersion attribute is set as 0x00010000. If an application requires OpenGL ES 2.0 and OpenGL ES 3.0; the value of the attribute is set to 0x00030000. It is then understood that the application supports not only OpenGL ES 3.0; but also all lower versions. Here is what the uses-feature statement looks like (in this example the application requires OpenGL ES 2.0):  <uses-feature android:glEsVersion="0x00020000" android:required="true" />

The uses-feature statement takes two other values besides glEsVersion as follows: android:name and android:required. Android:name defines a valid value from the hardware or software list at the bottom of the Uses-Feature API Page. The android:required value defines whether the feature is "required" to run the app. True means the feature is required; false means use the feature if it is available on the device.

The classes that make up the android application project is defined by the graphics and their behavior. Take a look at the simple Hello OpenGL ES projects, which can be downloaded from http://developer.android.com/training/graphics/opengl/environment.html.

Note: Once you download the project, you can use the Import option, available from the File menu in Eclipse. You can then use the "Existing Android Code Into Workspace" option to import the project and then run it as an Android Application. Remember you cannot run the project without an emulator that has GPU support and the android version that corresponds to the required OpenGL ES version, as discussed in Android and the OpenGL ES section above. 



The download includes two android application projects. One requires OpenGL ES version 1.0 the other requires version 2.0. When you run the application it displays a triangle and a square. You can click and rotate the triangle because it was programmed to respond to user interaction.

 Hello OpenGL ES Android Application Project Classes


The Hello OpenGL ES project's core classes are as follows:
  • MyGLSurfaceView
  • MyGLRenderer
  • OpenGLES20Activity
  • Square
  • Triangle

MyGLRenderer implements the GLSurfaceView.Renderer class, which is used to render a frame. In the Hello OpenGL ES project the GLSurfaceView.Render uses GLES20 constants to draw the background color of the frame. It uses the Matrix class to set the camera position as well as calculate and store 4 x 4 column-vectors for rendering. (Below is an example of the 4 x 4 column-vector matrices from the OpenGL ES Android reference:)

  m[offset +  0] m[offset +  4] m[offset +  8] m[offset + 12]
  m
[offset +  1] m[offset +  5] m[offset +  9] m[offset + 13]
  m
[offset +  2] m[offset +  6] m[offset + 10] m[offset + 14]
  m
[offset +  3] m[offset +  7] m[offset + 11] m[offset + 15]


The Hello OpenGL ES project introduces quite a few interesting programming concepts. For example, the MyGLSurfaceView class implements the GLSurfaceView class, which creates a view container (which manages a surface so OpenGL can render to that surface) used to render OpenGL ES graphics as well as capture touch events so users can interact with the graphics. Th android activity class creates an instance of the GLSurfaceView to serve as the Content View for the activity.

In the project the MyGLSurfaceView calls the MyGLRenderer class so the graphics can be rendered using the GLSurfaceView.Renderer class. Both the Triangle and the Square class use the GLES20.glCreateProgram() command to create an empty OpenGL ES program. And the GLES20.glAttachShader command is used to add the vertex and fragment shaders to the program.  The GLES20.glLinkProgram command (or public method) is then used to create the OpenGL program executables. The vertex shader executable runs on the vertex processor and the fragment shader executable runs on the fragment processor.

Since the user can interact with the triangle, the triangle coordinates are captured and used to write the position of the triangle. The MyGLRender class is implemented in a way that ensures, once the triangle is rendered, it is only rendered again when the data changes.

Summary


Overall the Hello OpenGL ES project has a very simple design that executes the key elements of an animated graphics android application. This program can help developers more easily understand how to program graphics using the OpenGL ES API as well as create shaders and assign buffers. For more information you can refer to the following list of resources:


Thursday, September 5, 2013

Programming Mobile Apps for Android With Eclipse

In my previous article titled: Mobile Software Programming: Designing & Building Mobile Apps (Android) I discuss the basic concepts associated with designing and building a mobile application or game for Android. This article provides insight into creating an Android app (whether a game or not game app). It discusses the core files and folders included in an Android app; and, it also discusses the Git tool, which can be used to download project files for the applications (i.e., camera, calendar, calculator, etc.) that come preinstalled on an Android device. If you are just getting started with Android programming this article will help you to follow the Building Your First App tutorial since it provides screen captures and some steps not outlined in the tutorial.

Getting Started


Before building an Android app you must download and install the Android SDK (the ADT Bundle includes the Eclipse IDE). Eclipse is the development platform used to write, run, test and deploy Android apps. When you initially install the SDK all of the tools are not automatically installed. Instead, you will need to relaunch the SDK Manager and select the additional tools that you want to install.

Note:  You will want to make sure you select Google Play Services if you want to build custom apps that will incorporate Google Maps, Google+, display ads from within your app, etc. and/or build games.


Once you install the Android Developer Tools (ADT) Bundle you can launch Eclipse to access the SDK Manager, shown above. From the Eclipse menu you can select Window -> Android SDK Manager (as shown below). When the Android SDK Manager displays simply click on the additional components you want installed.

Android Projects


The article titled Creating an Android Project does a good job of explaining how to create an Android project. There are several concepts you will encounter when working with an Android project. First, Eclipse will prompt you for a Workspace folder. Once you select a Workspace folder for your session (considering that each time you launch the IDE you are starting a session) Eclipse will store your project files in the Workspace folder. You can use a single Workspace for all projects. It's up to you. And, it largely depends on how you want to organize your projects.


If you plan to use a single Workspace, notice you are given the ability to check the "Use this as the default and do not ask again" checkbox so you do not need to specify a Workspace each time you access Eclipse.

Project Folders/Files


When you create a new Android App project Eclipse creates a new folder (in the Workspace folder) using the project name you specify. It adds the project folders and files required to support an Android application project. The following paragraphs provide a brief overview of the key folders/files that make up the Android app project:

.settings - This folder includes the PREFS file that defines Eclipse preferences for the compiler including target platform and compiler compliance that indicates the JDT (Java Development Tools) version  the source should conform to. Eclipse automatically sets these levels.

bin - Includes the project classes and the AndroidManifest.XML file required to run Android applications.



gen - The Gen folder includes the Java Language Source Files for the project. These files define the build configuration and application settings such as the theme settings, activity horizontal and vertical margins specified by the developer (through coding), buttons added to the application, etc.

libs - This folder includes the JAR file, which includes classes used by your application. The following picture shows the contents of the android-support-v4.jar file


res - This folder includes the resource files used by the application including the activity_main.xml file, which includes the layout and other settings for your app. It also includes the images used by your application along with other files.

src - This folder includes the Activity class that is executed when your application runs. For example, it includes theonCreateOptionsMenu(Menu menu) procedure that adds the items to your application's action bar, if it includes one.
 
.project - This file manages project related information such as project name, project description and other elements as shown in the following picture.


You can read more about project folders and files by reading the Managing Projects documentation at http://developer.android.com/tools/projects/index.html .

Running Your App On an Android Device


Once you complete the tutorial or build an application you will want to run it to make sure it works. If you have an Android device you can test your application on your device. However, to do so you must first set the developer options on your device. The Developer options window (shown below) is available from Settings. You must enable USB debugging. Doing this allows Eclipse to deploy your application to your device.


Once you have enabled USB debugging you can connect your device to your computer hosting your Android App development environment. (After I connected my device to my computer I set the USB Connection Type as Mass storage.) You can then select run from the Eclipse menu. Once Eclipse recognizes your device it appears as an option from the Android Device Choose window (invoked by selecting Run).



If you select your device and click OK, Eclipse will deploy your Android app to your device. For more information you can read the documentation titled Run On a Real Device. As shown in the following picture the custom Android App I deployed to my Android phone is called PVL Sample App. (In case you are wondering I do not play Candy Crush or color in the Coloring Book. My little one plays with them (o: )



Creating an Emulator for Your App


If you don't have an Android device don't fret. You can still join the Android development movement. The Eclipse environment provides a way for you to create an emulator to which you can deploy your apps. You can select Android Virtual Device Manager from the Window menu in Eclipse. The Android Virtual Device Manager will display (as shown below).




You can then create a New device. You need to select the type of device you want to create and define the settings for the device




Once you create the device you can use the Android Device Chooser to Launch a new Android Virtual Device, as shown below.


The following picture shows the PVL Sample App deployed to a virtual tablet. You can then test the application's behavior to ensure it performs as it should.



Packages

Java classes are organized by Packages. Classes contain procedures that are used by importing the package (as shown in the picture below). A list of the Android packages and classes can be viewed at http://developer.android.com/reference/packages.html .


Android Training & Sample Apps


Google has posted trainings for individuals who want to learn Android app programming. The trainings are located at http://developer.android.com/training/index.html . It's best to follow the trainings in sequence if you are not an experienced Android app developer because the tutorials start with basic concepts and as the training progresses the concepts increase in complexity.

You can download and view core source project files associated with Android, which includes the project files for the camera, calendar, contacts and other apps on your Android device. To get the files you must first install the Git application. Git is available from the following location:  http://git-scm.com/downloads . Once you have downloaded and installed Git you can clone the Android repository that contains the project files (available from the following location: https://android.googlesource.com/ ).

The URL for the repository is used to direct Git to the repository you want to clone. The Clone Git Repository wizard allows you to select the releases and other options associated with cloning the repository.



You can then create a new project from existing code and tell Eclipse to "copy projects into workspace". The project is then added to the workspace and can be deployed to a real or virtual device for testing. A future post will  provide more insight on downloading, accessing and using Git repository files within Eclipse.

More About Git



The following resources pertain to working with the Android Open Source project . They provide great insight into Git. You will want to read Codelines, Branches, and Releases at  http://source.android.com/source/code-lines.html; and, you may also want to look at Codenames, Tags, and Build Numbers at http://source.android.com/source/build-numbers.html . The documentation on Developing is also a great resource:  http://source.android.com/source/developing.html . Lastly, a useful list of Git Resources is located at http://source.android.com/source/git-resources.html . The resource list includes a link to an online version of the published book  Pro Git for those seeking to gain expert-level Git skills.