OpenGL vertex array, opengl Vertex

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.