20
Chapter
Programming 3D Graphics
with OpenGL
In this chapter, we will talk extensively about working with the OpenGL ES graphics API on the Android Platform. OpenGL ES is a version of OpenGL that is optimized for embedded systems and other low-powered devices such as mobile phones.
The Android Platform supports OpenGL ES 1.0 and OpenGL ES 2.0. The OpenGL ES 2.0 is available only from API level 8 corresponding to Android SDK release 2.2. As of this writing, there are some issues with using Java bindings to OpenGL ES 2.0. See the notes and recommendations on OpenGL ES 2.0 in a separate section towards the end of this chapter. The primary issue is the lack of support for OpenGL ES 2.0 in the emulator. Android 3.0 has strengthened the opportunities for OpenGL by introducing Renderscript. Renderscript is meant for better performance by allowing to run native code programmed in a "c" like language. This code even may be executed on the GPU (Graphical Processing Unit). Renderscript is also designed to allow for cross-platform compatibility. When performance is not critical, programmers are still advised to use the Java bindings for much of the OpenGL work. Due to time limitations we didn't cover the Renderscript in this edition of the book. We have provided a reference URL to a Renderscript (from Google) programming guide at the end of this chapter.
The Android SDK distribution comes with a number of OpenGL ES samples. However, the documentation on how to get started with OpenGL ES is minimal to nonexistent in the SDK. The underlying assumption is that OpenGL ES is an open standard that programmers can learn from outside of Android. As a result, Android online OpenGL resources and the Android OpenGL code samples assume you’re already familiar with OpenGL.
In this chapter, we will help you with these minor roadblocks. With few OpenGL prerequisites, by the end of this chapter, you’ll be comfortable with programming in OpenGL. We will do this by introducing almost no mathematics (unlike many OpenGL books).
In the first section of the chapter, we’ll provide an overview of OpenGL, OpenGL ES, and some competing standards.
In the second section, we will explain the theory behind OpenGL. This is a critical section to read if you are new to OpenGL. In this section, we will cover OpenGL coordinates, its idea of a camera, and the essential OpenGL ES drawing APIs.
In the third section, we will explain how you interact with the OpenGL ES API on Android. This section covers GLSurfaceView and the Renderer interface and how they work together to draw using OpenGL. In one of our simple examples, we will draw a simple triangle to show how drawing is impacted by changing the OpenGL scene setup APIs.
Note: The OpenGL camera concept is similar but distinct from the Camera class in Android’s graphics package, which you learned about in Chapter 6. Whereas Android’s Camera object from the graphics package simulates 3D-like viewing capabilities by projecting a 2D view moving in 3D space, the OpenGL camera is a paradigm that represents a virtual viewing point. In other words, it models a real-world scene through the viewing perspective of an observer looking through a camera. You’ll learn more in the subsection “Understanding the Camera and Coordinates” under “Using OpenGL ES.” Both cameras are still separate from the handheld device’s physical camera that you use to take pictures or shoot video.
In the fourth section, we will take you a bit deeper into OpenGL ES and introduce the idea of shapes. We will also cover textures and show you how to draw multiple figures during a single draw method. We will then cover the support for OpenGL ES 2.0 by briefly introducing OpenGL shaders and a quick sample. Please note up front that OpenGL ES 2.0 can only be tested on a real device.
We conclude the chapter with a list of resources we found as we researched material for this chapter.
So, let's look into the history and background of OpenGL!
Understanding the History and Background of OpenGL
OpenGL (originally called Open Graphics Library) is a 2D and 3D graphics API that was developed by Silicon Graphics, Inc. (SGI) for its UNIX workstations. Although SGI’s version of OpenGL has been around for a long time, the first standardized spec of OpenGL emerged in 1992. Now widely adopted on all operating systems, the OpenGL standard forms the basis of much of the gaming, computer-aided design (CAD), and even virtual reality (VR) industries.
The OpenGL standard is currently being managed by an industry consortium called The Khronos Group (www.khronos.org), founded in 2000 by companies such as NVIDIA, Sun Microsystems, ATI Technologies, and SGI. You can learn more about the OpenGL spec at the consortium’s web site at:
www.khronos.org/opengl/
The official documentation page for OpenGL is available here:
www.opengl.org/documentation/
As you can see from this documentation page, you have access to books and online resources dedicated to OpenGL. Of these, the gold standard is OpenGL Programming Guide: The Official Guide to Learning OpenGL, Version 1.1, also known as the “red book” of OpenGL. You can find an online version of this book here:
www.glprogramming.com/red/
This book is quite good and quite readable. We did have some difficulty, however, unraveling the nature of units and coordinates that are used to draw. We’ll try to clarify these important ideas regarding what you draw and what you see in OpenGL. These ideas center on setting up the OpenGL camera and a viewing box, also known as a viewing volume or frustum.
Share with your friends: |