3D Programming Example in Android

To add more depth to our games, we have to get ready to go 3D. 3D is not complicated at all and rendering 3D graphics is quite easy using OpenGL ES in Android.

In a 3D game, as in the real world, everything happens in space. What we would see is defined by the field of view and the players that we “catch” with our eye. We would look at a 3D scene.

Everything you see in the 3D scene makes up the perspective. Perspective projection scales the final image of the objects according to their distance from the observation point. The farther an object is, the smaller it appears, just like in real life.

This is much like a camera. Let’s see how to implement this. We will use triangles in space.

Let’s create an activity with an OpenGL renderer.

 

 

The MainActivity will display a triangle with the same size at different positions. The activity’s onCreate method is straightforward but let’s have a recap line by line:
– Call the cunstructor of the super class to handle state.
– Get rid of the window title.
– Makes the window go full-screen.
– Instantiates the GLSurfaceView to access the OpenGL ES API. It is declared as a private member because we need to use it later on.
– Creates a new instance of our own OpenGL renderer (defined as an inner class), and sets it to be the renderer of the newly created view.
– Sets the newly created view to be the view of the activity.

Moving on to the renderer, we created an inner class MyOpenGLRenderer which implements the Renderer interface. It has 3 methods and these methods are triggered by events from the activity:

onSurfaceCreated is called every time the drawing surface is created. This can happen when the application starts, or when it becomes active after being sent to the background or when the orientation changes. Note that all these events are triggered after a context loss, so every asset (think loaded images) will be lost and have to be recreated. This is the place where all assets and application objects should be (re)created.
onSurfaceChangedoccurs when the screen size changes. This can happen when the device’s orientation changes.
onDrawFrame is where the actual drawing happens. It is triggered continuously after every successful draw.
All life-cycle events get passed a GL10 instance through which we can issue commands to OpenGL ES.
We do all our initialisation in the onSurfaceChanged.

Steps:

– Instantiates triangle1 with a yellow color and size 0.5.
– Then Set the actual viewport. The first two parameters define the lower left corner, while the next 2 the width and height of it. Width and height in this case is set with values passed by Android’s onSurfaceChanged event. If orientation is not locked, the width and height will swap values every time the surface changes between portrait and landscape.
– Set the active matrix to be the projection matrix. OpenGL is a state machine and it has 4 internal matrices to work with. It will be discussed in detail, but for now this is how we tell OpenGL to switch to the perspective matrix so we can perform operations on it.

In the onDrawFrame we simply issue a series of commands to draw the scene which will set the clear values for the colour buffer to opaque black. The attributes are (red, green, blue, alpha) taking values between 0 and 1. 1 being the highest intensity while 0 meaning “no” intensity. For the alpha component 0 is transparent and 1 is opaque.

Note that OpenGL does not work with pixels as measures, but with units and fractions of units. We will have to decide how many units an object will have. For example 1 unit in OpenGL can be 1 meter in the real world, so a human can be 1.8 unit tall.
Now Draw triangle1. It will be drawn 5 units into the screen. We instruct OpenGL to move further 5 units INTO the screen (meaning farther) and 2 units to the right. The translation will be made FROM the last position.

The result will show the scaling of the triangles. The green triangle is the same size as the red one but it appears smaller because it is farther away.

3dprogramming2

When doing orthographic projection, the Z component is completely ignored and there is no scaling effect.

The following listing shows the Triangle used, but you don’t have to worry about understanding it, as I will cover the vertex buffers shortly.