## An Example

Let's write a simple example called Vertices3Test. We want to draw two triangles, one with z being -3 for each vertex and one with z being -5 for each vertex. We'll also use per-vertex color. Since we haven't discussed how to use a perspective projection, we'll just use an orthographic projection with appropriate near and far clipping planes so that the triangles are in the view frustum (e.g., near is 10 and far is -10). Figure 10-1 shows the scene.

Figure 10-1. A red triangle (front) and a green triangle (back) in 3D space

The red triangle is in front of the green triangle. We can say "in front" because our camera is located at the origin looking down the negative z-axis by default in OpenGL ES (which actually doesn't have the notation of a camera). The green triangle is also shifted a little to the right so that we can see a portion of it when viewed from the front. It should be overlapped by the red triangle for the most part. Listing 10-2 shows the code for rendering this scene.

Listing 10-2. Vertices3Test.java: Drawing Two Triangles package com.badlogic.androidgames.gl3d;

import javax.microedition.khronos.opengles.GL10;

public class Vertices3Test extends GLGame {

@Override public Screen getStartScreen() {

return new Vertices3Screen(this);

class Vertices3Screen extends GLScreen { Vertices3 vertices;

public Vertices3Screen(Game game) { super(game);

vertices = new Vertices3(glGraphics, 6, 0, true, false); vertices.setVertices(new float[] { -0.5f, -0.5f, -3, 1, 0, 0, 1,

0.5f, -0.5f, -3, 1, 0, 0, 1, 0.0f, 0.5f, -3, 1, 0, 0, 1,

^Override public void present(float deltaTime) { GL10 gl = glGraphics.getGL(); gl.glClear(GL10. GL_COLOR_BUFFER_BIT);

gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight()); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrthof(-1, 1, -1, 1, 10, -10); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); vertices.bind();

vertices.draw(GL10.GL_TRIANGLES, 0, 6); vertices.unbind();

^Override public void

^Override public void

^Override public void

^Override public void

This is the complete source file, as you can see. For the following examples we'll go back to only showing the relevant portions, since the rest stays mostly the same, apart from the class names.

We have a Vertices3 member in Vertices3Screen, which we initialize in the constructor. We have six vertices in total, a color per vertex, and no texture coordinates. Since neither triangle shares vertices with the other, we don't use indexed geometry. This information is passed to the Vertices3 constructor. Next we set the actual vertices with a call to Vertices3.setVertices(). The first three lines specify the red triangle in the front, and the other three lines specify the green triangle in the back, slightly offset to the right by 0.5 units. The third float on each line is the z-coordinate of the respective vertex.

In the present() method we first clear the screen and set the viewport, as always. Next we load an orthographic projection matrix, setting up a view frustum big enough to show all of our scene. Finally we just render the two triangles contained within the Vertices3 instance. Figure 10-2 shows the output of this program.

update(float deltaTime) {

Figure 10-2. The two triangles, but something's wrong

Now that is strange. According to our theory, the red triangle (in the middle) should be in front of the green triangle. Our camera is located at the origin looking down the negative z-axis, and from Figure 10-1 we see that the red triangle is closer to the origin than the green triangle. What's happening here?

OpenGL ES will render the triangles in the order we specify them in the Vertices3 instance. Since we specified the red triangle first, it will get drawn first. We could change the order of the triangles to fix this. But what if our camera weren't looking down the negative z-axis, but from behind? We'd again have to sort the triangles before rendering according to their distance from the camera. That can't be the solution. And it isn't. We'll fix this in a minute. Let's first get rid of this orthographic projection and use a perspective one instead.

0 0