OpenGL vertex array, opengl Vertex
In OpenGL, if you want to draw elements, the following operations are generally used:
glBegin(GL_LINES); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(1.0f, 0.0f, 0.0f);glEnd();
However, if the number of vertices is too large, This method requires frequent function calls, which is too inefficient. This requires the use of vertex arrays.
1. Enable vertex Array
OpenGL is a state machine. Before using a function, you must enable this function. After using this function, you can also disable it.
The code for enabling and disabling vertex arrays is as follows:
Glableclientstate (GL_VERTEX_ARRAY); // enable glDisableClientState (GL_VERTEX_ARRAY); // disable
GL_VERTEX_ARRAY is an array type. There are eight types:
- GL_VERTEX_ARRAY
- GL_COLOR_ARRAY
- GL_SECOND_COLOR_ARRAY
- GL_INDEX_ARRAY
- GL_NORMAL_ARRAY
- GL_FOG_COORDINATE_ARRAY
- GL_TEXTURE_COORD_ARRAY
- GL_FLAG_ARRAY
2. Specify an array
After the vertex array is enabled, specify which array is the vertex array. The function is as follows:
** Void glVertexPointer (GLint size,
GLenum type,
GLsizei stride,
Const GLvoid * pointer );**
Parameters:
Size-Number of coordinates of each vertex, which must be 2, 3, or 4.
Type-Data type of vertex coordinates, GL_SHORT, GL_INT, GL_FLOAT or GL_DOUBLE
Stride-the offset between two adjacent vertices, in bytes. If it is 0, the surface vertices are tightly stored.
Pointer-memory address of the first coordinate of the first item in the array.
In addition, glColorPointer and glIndexPointer are used to specify other arrays.
The usage is as follows:
GLfloat vertexs [] = {0.0f, 0.0f, 0.0f, 0.2f, 0.0f, 0.0f,-0.2f, 0.0f, 0.0f, fill, 0.2f, 0.0f, 0.0f,-0.2f, 0.0f, 0.0f, 0.0f, 0.2f, 0.0f, 0.0f,-0.2f}; GLubyte indexs [] = {0, 1, 3, 4, 5, 6}; glableclientstate (GL_VERTEX_ARRAY ); // enable vertex array glableclientstate (GL_INDEX_ARRAY); // enable Index Array glVertexPointer (3, GL_FLOAT, 0, vertexs); // specify vertex array glIndexPointer (GL_UNSIGNED_BYTE, 0, indexs ); // specify the Index Array
3. Drawing
There are three functions for plotting.
Void glArrayElement (GLint I );
Specify the vertex through the index array and place it between glBegin () and glEnd. For example:
glBegin(GL_POINTS); glArrayElement(indexs[0]); glArrayElement(indexs[1]);glEnd();
Void glDrawElements (GLenum mode, GLsizei count, GLenum type, const GLvoid * indices );
Mode-primitive type, such as GL_POINTS and GL_LINES
Count-number of elements. The index array exists in indices.
Type-the data type of the index array, which must be GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT
Indices-Index Array
Example:
glDrawElements(GL_POINTS,7,GL_UNSIGNED_BYTE,indexs);
Void glDrawArrays (GLenum mode, GLint first, GLsizei count );
For each enabled array (vertex array, color array, index array), draw fromFirstToFirst + count-1. The type is mode, for example, GL_POINTS.
glDrawArrays(GL_POINTS,0,7);