## GlVertexpointer and Specifying Drawing Vertices

The glVertexPointer method is responsible for specifying an array of points to be drawn. Each point is specified in three dimensions, so each point will have three values: x, y, and z. Here's how you can specify three points in an array:

 -0.5f, -0.5f, 0, //p1: (x1,y1,z1) 0.5f, -0.5f, 0, //p2: : (x1,y1,z1) 0.0f, 0.5f, 0 //p3: : (x1,y1,z1)

This structure is a contiguous array of floats kept in a Java-based float array. Don't worry about typing or compiling this code anywhere yetâ€”our goal at this point is just to give you an idea of how these OpenGL ES methods work. We will give you the working examples and code when we help you develop a test harness in the last section of this chapter.

You might be wondering what units are used for the coordinates in points p1, p2, and p3. The short answer is that as you model your 3D space, these coordinate units can be anything you'd like. But subsequently, you will need to specify something called a bounding volume (or bounding box) that quantifies these coordinates. For example, you can specify the bounding box as a cube with 5-inch sides or a cube with 2-inch sides. These coordinates are also known as world coordinates because you are conceptualizing your world independent of the physical device's limitations. We will further explain these coordinates in the subsection "Understanding the Camera and Coordinates." For now, assume that you are using a cube that is 2 units across all its sides and centered at (x=0,y=0,z=0).

Note The terms bounding volume, bounding box, viewing volume, viewing box, and frustum all refer to the same concept: the pyramid-shaped 3D volume that determines what is visible onscreen. You'll learn more in the "glFrustum and the Viewing Volume" subsection under "Understanding the Camera and Coordinates."

You can also assume that the origin is at the center of visual display. The z axis will be negative going into the display (away from you) and positive coming out of the display (toward you). x will go positive as you move right and negative as you move left. However, these coordinates will also depend on the direction from which you are viewing the scene.

To draw these points, you need to pass them to OpenGL ES through the glVertexPointer method. For efficiency reasons, however, glVertexPointer takes a native buffer that is language-agnostic rather than an array of floats. For this, you need to convert the Java-based array of floats to an acceptable C-like native buffer. You'll need to use the java.nio classes to convert the float array into the native buffer. Here's the sample code to do that:

jva.nio.ByteBuffer vbb = java.nio.ByteBuffer.allocateDirect(3 * 3 * 4); vbb.order(ByteOrder.nativeOrder());

java.nio.FloatBuffer mFVertexBuffer = vbb.asFloatBuffer();

The byte buffer is a buffer of memory ordered into bytes. Each point has three floats because of the three axes, and each float is 4 bytes. So together you get 3 * 4 bytes for each point. Plus, a triangle has three points. So you need 3 * 3 * 4 bytes to hold all three float points of a triangle.

Once you have the points gathered into a native buffer, you can call glVertexPointer like this:

glVertexPointer(

// each value is a float value in the buffer GL10.GL_FLOAT,

// Between two points there is no space 0,

// pointer to the start of the buffer mFVertexBuffer);

Let's talk about the arguments of this method. The first argument tells OpenGL ES how many dimensions there are in a point or a vertex. In this case, we specified 3 for x, y, and z. You could also specify 2 for just x and y. In that case, z would be zero. Note that this first argument is not the number of points in the buffer, but the number of dimensions used. So if you pass 20 points to draw a number of triangles, you will not pass 20 as the first argument; you would pass 2 or 3, depending on the number of dimensions used.

The second argument indicates that the coordinates need to be interpreted as floats. The third argument, called a stride, points to the number of bytes separating each point. In this case, it is zero because one point immediately follows the other. Sometimes you can add color attributes as part of the buffer after each point. If you want to do that, you'd use a stride to skip those as part of the vertex specification. The last argument is the pointer to the buffer containing the points.

Now you understand how to set up the array of points to be drawn. Next, you'll see how you'd actually draw this array of points using the glDrawElements method.

0 0

### Responses

• marcus
How i know the pixel coordinates of a draw cube with vertex opengl android?
7 years ago