Simple TriangleRendererjava

Now that you have the test harness built, you can use the following coordinates to draw an OpenGL triangle:

-0.5f, -0.5f, 0, //pl: (xl,yl,zl) 0.5f, -0.5f, 0, //p2: (xl,yl,zl) 0.0f, 0.5f, 0 //p3: (xl,yl,zl)

Our goal in this example is to take these coordinates and tell OpenGL ES to draw them as a triangle. Based on our discussion in the "Essential Drawing with OpenGL ES" subsection under "Using OpenGL ES," you should be able to figure out how the code in Listing 10-12 accomplishes that.

Listing 10-12. Drawing a Simple Triangle //filename: SimpleTriangleRenderer.java public class SimpleTriangleRenderer extends AbstractRenderer {

//Number of points or vertices we want to use private final static int VERTS = 3;

//A raw native buffer to hold the point coordinates private FloatBuffer mFVertexBuffer;

//A raw native buffer to hold indices //allowing a reuse of points. private ShortBuffer mIndexBuffer;

public SimpleTriangleRenderer(Context context) {

ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);

vbb.order(ByteOrder.nativeOrder());

mFVertexBuffer = vbb.asFloatBuffer();

ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);

ibb.order(ByteOrder.nativeOrder());

mIndexBuffer = ibb.asShortBuffer();

-0.5f, -0.5f, 0, // (x1,y1,zl) 0.5f, -0.5f, 0, 0.0f, 0.5f, 0

for (int i = 0; i < VERTS; i++) { for(int j = 0; j < 3; j++) {

mFVertexBuffer.put(coords[i*3+j]);

mIndexBuffer.put(myIndecesArray[i]);

mFVertexBuffer.position(o); mlndexBuffer.position(o);

//overridden method protected void draw(GLl0 gl)

gl.glVertexPointer(3, GL10.GL_FL0AT, 0, mFVertexBuffer); gl.glDrawElements(GLl0.GL_TRIANGLES, VERTS,

GL10.GL_UNSIGNED_SH0RT, mIndexBuffer);

Notice how focused and sparse this code is. This level of simplicity and directness should encourage experimentation with OpenGL. The code in Listing 10-12 sets up the draw method based on the aforementioned principles for drawing a triangle. In preparation for the draw method, the code identifies the points and transports the point coordinates to a buffer. You do the same with indices for those points. Then you draw the triangle using glDrawElements.

Once you compile all this code, you can invoke the OpenGLTestHarnessActivity by using the following code segment in response to one of your menu items:

Intent intent = new Intent(activity,OpenGLTestHarnessActivity.class); activity.startActivity(intent);

In this small code snippet, the variable activity points to the activity from which your menu item is invoked. If you are doing it in the same derived class of Activity, you can simply use the this variable.

You will also need to register this activity in the AndroidManifest.xml file for your application. Here is an example:

<activity android:name=".OpenGLTestHarnessActivity"

android:label="OpenGL Test Harness"/>

With all of this code in place, you should see the Figure 10-3 screen in your emulator when you run the program and invoke the activity.

Figure 10-3. A simple OpenGL triangle
0 0

Post a comment