OpenGL Vertex Array

Source: Internet
Author: User

In OpenGL, if you are drawing an entity, you will generally use:

glBegin(GL_LINES);   glVertex3f(0.0f0.0f0.0f);    glVertex3f(1.0f0.0f0.0f);glEnd();

However, if the number of vertices is too many, this method requires frequent calls to the function and is inefficient. This will use the vertex array.

1. Enable vertex arrays

OpenGL is a state machine, before using a function, you must first turn on this function, after use, you can also close.

The code for enabling and closing the vertex array is as follows:

glEnableClientState(GL_VERTEX_ARRAY);   //启用glDisableClientState(GL_VERTEX_ARRAY);  //关闭

Where Gl_vertex_array is the type of array, there are eight kinds:

    • 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

With the vertex array enabled, the next step is to specify: which array is the vertex array. The functions are as follows:

**void glvertexpointer (Glint size,
Glenum type,
Glsizei Stride,
const glvoid* pointer); * *

Parameters:

Size-the number of coordinates for each vertex, which must be 2, 3, or 4.

Type-the data type of the vertex coordinate, gl_short, gl_int, gl_float, or gl_double

Stride-the offset between two adjacent vertices, in bytes, and if 0, the surface vertices are tightly stored.

Pointer-the memory address of the first coordinate of the first item in the array.

In addition, Glcolorpointer, Glindexpointer, and so on, are used to specify other arrays.

Here's how to use it:

Glfloat vertexs[] = {0.0f,0.0f,0.0f,0.2f,0.0f,0.0f,                    -0.2f,0.0f,0.0f,0.0f,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,2,3,4,5,6};glenableclientstate (Gl_vertex_array);//enable vertex arrayGlenableclientstate (Gl_index_array);//enable indexed arraysGlvertexpointer (3, Gl_float,0, Vertexs);//Specify vertex arrayGlindexpointer (Gl_unsigned_byte,0, Indexs);//Specify an indexed array
3. Drawing

There are three functions for drawing.

void Glarrayelement (glint i);

Specify vertices from an indexed array, placed 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 types, such as gl_points, Gl_lines

Count-Number of elements, index array exists in indices

Type-the data type of the indexed array must be gl_unsigned_byte, Gl_unsigned_short, or Gl_unsigned_int

Indices-indexed Array

Use methods such as:

glDrawElements(GL_POINTS,7,GL_UNSIGNED_BYTE,indexs);

void Gldrawarrays (Glenum mode, glint first, glsizei count);

For each enabled array (vertex array, array of colors, indexed array), draw elements from first to first+count-1 . Type is mode, for example Gl_points.

glDrawArrays(GL_POINTS,0,7);

OpenGL Vertex Array

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.