OpenGL Vertex Array
Although the example given above contains only a small number of coordinate positions, it is generally much more complex to describe a scene that contains several objects. Let's consider describing a simple, very basic object: the unit cube in Figure 3.58, which uses integer coordinates to simplify the subsequent discussion. The direct way to define vertex coordinates is to use a double subscript array, for example:
Glint points [8][3] = {{0,0,0},{0,1,0},{1,0,0},{1,1,0},{0,0,1},{0,1,1},{1,0,1},{1,1,1}};
You can also define a data type for a three-dimensional vertex position, and then give the coordinates of each vertex position as a single subscript array element,
For example:
typedef glint VERTEX3 [3];VERTEX3 pt [8] = {{0,0,0},{0,1,0},{1,0,0},{1,1,0},{0,0,1},{0,1,1},{1,0,1},{1,1,1}};
Here you define the six faces of the object. To do this, call Glbegin (Gl_polygon} or Glbegin (Gl_quads) six times. We must make clear that the vertex order of each polygon conforms to the requirement of counterclockwise order from the outside of the cube when it is observed. In the following program segment, we specify that each cubic polygon is a quad and use a function call to pass the array subscript value to the OpenGL metafile subroutine. Figure 3.59 shows the subscript value of the array pt corresponding to the vertex position of the cube.
void Quad (Glint N1, glint N2, Glint N3, Glint N4) { glbegin (gl_quads); Glvertex3iv (PT [N1]); Glvertex3iv (Pt [N2]); Glvertex3iv (PT [N3]); Glvertex3iv (PT [N4]); Glend ();} void Cube () { Quad (6, 2, 3, 7); Quad (5, 1, 0, 4); Quad (7, 3, 1, 5); Quad (4, 0, 2, 6); Quad (2, 0, 1, 3); Quad (7, 5, 4, 6);}
In this way, specify a polygon to use six OpenGL functions, a total of six polygons need to be specified. After adding a color description and other parameters, the program that displays the cube can easily contain more than 100 OpenGL function calls. Scenes with many complex objects will require more function calls.
as can be seen from the above example, a complex scenario description needs to be described using hundreds of or thousands of coordinates. In addition, various properties and observation parameters must be established for each object. Therefore, objects and scenarios describe the use of a large number of function calls, which puts the requirements on system resources and slows down the execution of the graphical program . Complex display the further problem is that the object surface (the cube in 3.58) usually has shared vertices. Using the methods that have been discussed, these shared vertices need to be specified more than once.
To simplify these issues, OpenGL provides a mechanism to reduce the number of function calls that handle coordinate information. Using the vertex array (vertex array) , you can use a small number of function notes to arrange the scene's descriptive information. The steps are as follows:
1. The reference function glenableclientstate (Gl_vertex_array) activates OpenGL's vertex array attributes.
2. Use the function Glvertexpointer to specify the location and data format of the vertex coordinates.
3. Using subroutines such as gldrawelements to display the scene, the subroutine can handle multiple entities with only a small number of function calls. The
uses the previously defined PT array to implement the three steps in the following program example.
Glenableclientstate (Gl_vertex_array); Glvertexpointer (3, Gl_int, 0, PT); Glubyte Vertindex [] = (6, 2, 3, 7, 5, 1, 0, 4, 7, 3, 1, 5, 4, 0, 2, 6, 2, 0, 1, 3, 7, 5, 4, 6); Gldrawelements (Gl_quads, Gl_unsigned_byte, Verindex);
The First command, Glenableclientstate (Gl_vertex_array), activates the client's ability (in this case, the vertex array) in the customer/server system. Because the client (the machine running the main program) retains the data for the graph, the vertex array must be there. As pointed out in chapter 2nd, the server (such as a workstation) emits the current and displays graphics. Of course, a single computer is both a client and a server. The vertex array attribute of OpenGL is invalidated with the following command.
Gldisableclientstate (Gl_vertex_array);
Next, provide the position and format of the object vertex coordinates for the function Glvertexpointer. The first parameter of Glvertexpointer in this example is 3, which indicates the number of coordinates in each item point description. The data type of the vertex coordinate is specified with the second parameter OpenGL symbol constant in the function. The data type in this example is Gl_int. Additional data types with symbolic constants
Gl_byte, Gl_ Short, gl_float, and gl_double to specify。 The third parameter is used to give the byte displacement between successive vertices. The purpose of this parameter is to allow multiple types of data, such as coordinates and colors, to be bundled within the same array. Since we only give the coordinate data, we assign a value of 0 to this positional parameter. The last parameter of the function Glvertexpointer points to the array of vertices that contain the coordinate values.
all the indexes of the cube vertices are stored in the array vertindex. Each of these indexes is the subscript for the array pt corresponding to that vertex value. The index table is used as the last parameter of the function gldrawelements, and the first parameter that shows the four boundary surface of the cube is the entity gl_quads. The second parameter specifies the number of elements in the array vertindex. Since a quadrilateral has 4 vertices, we specify that the Gldrawelements function takes 4 vertices each time to display a cube face until 24 vertices are exhausted. This completes the display of all polygons for the entire cube using a function call. The third parameter of the Gldrawelements function gives the type of the index value. Because the index used at this time is a small integer,
we specify it as the Gl_unsigned_byte type. The other two types of indexes available are gl_unsigned_short and Gl_unsigned_int。
You can also put other information in the vertex array along with the coordinate values for the scene description. We can specify the object's color values and other properties in the array (referenced by the function gldrawelements). To improve efficiency, you can alternately use various arrays. The methods for implementing these properties are discussed in the next chapter.
Computer Graphics (ii) OUTPUT element _10_ polygon Fill area _8_opengl vertex array