Introduction to OpenGL in Android
OpenGL stands for Open Graphics Library. It is basically an API used by the android to support high-performance graphics like 2D and 3D graphics. The overall use of OpenGL for transferring the data from the CPU to the GPU. This is done by OpenGL Objects. It is a cross-platform graphics API. In this topic, we are going to learn about OpenGL in Android.
OpenGL Objects are made up of states and data and are the ones to help transmit data between the CPU and GPU. The OpenGL ES is a popular specification of OpenGL API which is especially dedicated for embedded systems.
OpenGL in Android is supported by Android using the Native Development Kit (NDK) and its framework API. The foundational classes in the Android framework help you create and edit graphics. The OpenGL ES API used with these classes is called GLSurfVaceiew.Renderer and GLSurfaceView. A good understanding of implementing these classes is helpful in using OpenGL in your Android application.
You can draw and manipulate objects in this view. It’s easy to use this class as you can create an object of GLSurfaceView and add a Renderer to it. You can capture touch screen events by extending the GLSurfaceView class to implement the touch listeners.
The GLSurfaceView.Renderer interface contains methods that are required for drawing graphics in a GLSurfaceView. The implementation of this interface should be provided as a separate class attached to the GLSurfaceView instance using GLSurfaceView.setRenderer().
You need to implement the following methods for GLSurfaceView.Renderer interface implementation:
4.5 (2,644 ratings)
- onSurfaceCreated(): This method is called during the creation of the GLSurfaceView.
- onDrawFrame(): This method is called during each redraw of the GLSurfaceView.
- onSurfaceChanged(): This method is called when the GLSurfaceView geometry changes(size, orientation etc).
How does OpenGL in Android works?
There exist several types of OpenGL Objects. For example, a Vertex Buffer Object that can store vertices of a character. The second example is Texture that can store image data.
Data such as vertices, normals and UV coordinates which represents the characteristics of mesh are loaded into a Vertex Buffer Object and then sent to the GPU for processing. Once it is in the GPU, these data would go through that is known as the OpenGL Rendering Pipeline.
The main tasks for which Rendering Pipeline is responsible are the conversion of the vertices to the right coordinate system, assembling of vertices of a character, application of color or texture and displaying of the character on default framebuffer which is the screen.
The rendering pipeline process of OpenGL in Android consists of six stages as below :
- Per-Vertex Operation
- Primitive Assembly
- Primitive Processing
- Fragment Processing
- Per-Fragment Operation
The first and foremost step to render an image is geometry data has to be converted from one coordinate system into another coordinate system.
The vertices are collected into pairs of 2, 3and more in this particular step and the primitive is assembled, for example, a triangle.
When the primitives have been assembled, they are tested to check if they are falling within a View-Volume. In case they do not pass this particular test, they would get ignored in further steps. This test is known as Clipping.
Then Primitives are broken down into chunks of smaller units and corresponding to that of pixels in the framebuffer. Each of these smaller units is then known as Fragments.
When primitive has been rasterized, then color or texture is applied to the geometry.
Lastly, the fragments are submitted to various tests such as :
- Pixel Ownership test
- Scissor test
- Alpha test
- Stencil test
- Depth test
From these six stages, two stages are being controlled by programs which are called Shaders.
Shader, in short, is a small program that is developed by you only that lives in the GPU. There is a special graphics language known as OpenGL Shading Language(GLSL) in which a shader is written. The two important stages in the OpenGL pipeline where a shader takes place are called “Per-Vertex Processing” and “Per-Fragment Processing” stages.
The shader that is processed in the “Per-Vertex” stage is known as Vertex Shader. The shader that is processed in the “Per-Fragment” stage is known as Fragment Shader. The ultimate and basic goal of the vertex shader is to give the final transformation of vertices of the character to the rendering pipeline whereas the goal of the fragment shader is to give the coloring and texture data to each of the headings of pixel to the framebuffer.
When data is passed through the OpenGL Rendering Pipeline, then either the 3D or 2D model will appear on the screen of your device.
Advice for OpenGL in Android
In case you are a beginner in OpenGL programmer, then some of these points below might not have been yet encountered by you. Below are some guidelines for you to take care of while using OpenGL in Android. They are common mistakes. So always keep these in mind.
- Improperly Scaling of Normals for Lighting
- Poor Tessellation Hurts Lighting
- Always Remember Your Matrix Mode
- Overflowing of the Projection Matrix Stack
- Not Setting All the Mipmap Levels
- Reading Back Luminance Pixels
Pre-requisites for OpenGL in Android
Below are the prerequisite required for OpenGL in Android.
- C, that is, programming language.
- C++ knowledge isn’t necessary but is helpful.
- Usage of various libraries that can be either Static or dynamic.
- Knowledge of Vectors in both 2D and 3D.
- Basic level maths concepts.
These are all main and basic concepts that are needed to start with OpenGL. You might need to learn some more math concepts but after you get to the intermediate level. But that totally depends upon you. You can pass over many things with the help of different libraries.
Conclusion: OpenGL in Android
Therefore, OpenGL is completely functional API that is primitive-level and allowing the programmer to effectively address and take advantage of graphics hardware. A large number of high-level libraries as well as applications use OpenGL because of its performance, programming ease, extensibility as well as widespread support.
This has been a guide to OpenGL in Android. Here we have discussed the Process of working and the pre-requisites of OpenGL in Android. You can also go through our other suggested articles to learn more –