Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

beginning opengl game programming 2004 phần 8 pptx
Nội dung xem thử
Mô tả chi tiết
Now that you understand the reasons for using vertex arrays, it’s time to learn how they
are used.
Array-Based Data
So far, we’ve been using relatively simple objects in our demos, and thus, we’ve been able
to describe them explicitly in the code. In a real game, however, you’ll be working with
models containing hundreds or even thousands of polygons, and describing such complicated models directly in the code just isn’t practical—even if you manage to create
decent-looking results, it’s going to be a nightmare to maintain. Instead, one of the following two approaches is usually taken:
■ Load the model from a file. Dozens of great modeling packages enable you to create a model visually and then export the geometric data to a file, which can be read
by your program. This approach offers the greatest flexibility. Model loading will
be discussed in much greater detail later in the book.
■ Generate the model procedurally. Some things you want to represent can be
implicitly described with equations due to patterns they contain or because they
possess some random properties that you can generate on the fly. A good example
of this is fractals. Geometric data for fractals can be created by a procedure that
produces the same values every frame.
Whichever approach is used, it should be fairly obvious that you don’t want to repeat all
the work every frame—you certainly don’t want to be constantly reading a model from
disk, and even procedural methods can have enough overhead to have an adverse effect
on performance. Instead, you’ll take the geometric data these methods generate and store
it in arrays, which you can then access as needed.
This process can be summarized in the following steps:
1. Generate the data you need, either procedurally or from a model file on disk.
2. Save this data in an array or set of arrays (for example, you could put the position
of each vertex in one array, the vertex normal in another, color in another, and so
on).
With your data stored in arrays, it’s ready for use by OpenGL’s vertex array functions.
Enabling Vertex Arrays
Like most OpenGL features, to be able to use vertex arrays, you must first enable them.
You might expect this to be done with glEnable(), but it’s not. OpenGL provides a separate pair of functions to control vertex array support:
void glEnableClientState(GLenum array);
void glDisableClientState(GLenum array);
228 Chapter 10 ■ Up Your Performance
10 BOGL_GP CH10 3/1/04 10:05 AM Page 228
TLFeBOOK
The array parameter is a flag indicating which type of array you’re enabling (or disabling).
Each type of vertex attribute you want to use (for example, position, normal, color) can
be stored in an array, and you need to enable whichever attributes you are using individually, using one of the flags listed in Table 10.2.
TIP
It is common in OpenGL documentation to refer to all these array types collectively as vertex arrays,
which can be confusing because there is also a specific array type that is called a vertex array. That
said, they are collectively referred to as vertex arrays because each array contains data that is referenced on a per-vertex basis. The array type containing positional information is specifically called
a vertex array because the data stored in it is used internally as if calls to glVertex() were being
made. If you’ll notice, the name of each array type roughly corresponds to the name of the OpenGL
call that will be made on the data it contains (color arrays mimic glColor(), texture coordinate
arrays mimic glTexCoord(), and so on).
Working with Arrays
After you have enabled the array types that you will be using, the next step is to give
OpenGL some data to work with. It’s up to you to create arrays and fill them with the data
you will be using (procedurally, from files, or by any other means, as we’ve already discussed). Then you need to tell OpenGL about these arrays so it can use them. The function used to do this depends on the type of array you’re using. Let’s look at each function
in detail.
Vertex Arrays 229
Table 10.2 Array Type Flags
Flag Meaning
GL_VERTEX_ARRAY Enables an array containing the position of each vertex.
GL_NORMAL_ARRAY Enables an array containing the vertex normal for each vertex
GL_COLOR_ARRAY Enables an array containing color information for each vertex
GL_SECONDARY_COLOR_ARRAY Enables an array containing color information for each vertex
GL_INDEX_ARRAY Enables an array containing indices to a color palette for each vertex
GL_FOG_COORD_ARRAY** Enables an array containing the fog coordinate for each vertex
GL_TEXTURE_COORD_ARRAY Enables an array containing the texture coordinate for each vertex
GL_EDGE_FLAG_ARRAY Enables an array containing an edge flag for each vertex
*Available only via the EXT_secondary_color extension under Windows.
**Available only via the EXT_fog_coord extension under Windows.
10 BOGL_GP CH10 3/1/04 10:05 AM Page 229
TLFeBOOK