OpenGL vertex Array

Source: Internet
Author: User

When we draw a shape manually, we first think of coordinate points instead of API. If we want to draw a straight line, first we can put the coordinates of the two points together, which makes it easier to understand, openGL's vertex array serves this purpose.

First, let's review the functions that draw straight lines.

Example 1

void drawOneLine(GLfloat x1,GLfloat y1,GLfloat x2,GLfloat y2){   glBegin(GL_LINES);   glVertex2f ((x1),(y1));    glVertex2f ((x2),(y2));   glEnd();}

You need to call glvertex2f twice and input two vertices. Now let's look at another method.

Store coordinates in an array

Example 2

void drawLineWithArray(){    GLint vertices[]={25,25,                      100,100};    glEnableClientState(GL_VERTEX_ARRAY);    glVertexPointer(2,GL_INT,0,vertices);    glBegin(GL_LINES);    glArrayElement(0);    glArrayElement(1);    glEnd();}

Vertices records two coordinates (x1, Y1) = (25,25), (X2, Y2) = (100,100)
However, the index value is 0, 1, 2, 3. To identify a coordinate point, a function is required to split the array.
Glvertexpointer is used for this function. It can be called this arrayHybrid ArrayThe parameter specifies the pairing points of the vertex array (for example, coordinates are located at 2, and colors are set to 3), data types, and arrays.
Note: When using Vertex arrays, you must first callGlenableclientstateEnable the vertex Array Function and call it when you do not need it.GldisableclientstateTo disable

GlarrayelementThen, the corresponding function is called Based on the vertex array. Only one vertex is called at a time.

The effects of Example 2 are the same as those of Example 1. It seems that it has not changed to a simple one, but it has become more complicated.

Enable multiple vertex Arrays

In addition to the coordinate array of the specified vertex, you can also start other vertex arrays, such as color and surface normal.

Draw two straight lines in different colors
Example 3

void drawTwoLineWithArray(){    GLint vertices[]={25,25,                      100,100,                      120,120,                      200,200};    GLfloat colors[]={1.0, 0.0, 0.0,                      1.0, 0.0, 0.0,                      0.0, 1.0, 0.0,                      0.0, 1.0, 0.0};    glEnableClientState(GL_VERTEX_ARRAY);    glEnableClientState(GL_COLOR_ARRAY);    glVertexPointer(2,GL_INT,0,vertices);    glColorPointer(3,GL_FLOAT,0,colors);    glBegin(GL_LINES);    glArrayElement(0);    glArrayElement(1);    glArrayElement(2);    glArrayElement(3);    glEnd();}

Note: each time you enable a vertex array, you must call glenableclientstate to activate it.

The effect is as follows:

Span

In Example 3, there is a coordinate array and a color array. If two arrays are merged, the coordinate and color data are in the same array. Now the code is modified as follows:

Example 4

GLfloat data[]=  {1.0, 0.0, 0.0,25.0,25.0,                  1.0, 0.0, 0.0,100.0,100.0,                  0.0, 1.0, 0.0,120.0,120.0,                  0.0, 1.0, 0.0,200.0,200.0};glEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_COLOR_ARRAY);glColorPointer(3,GL_FLOAT,5*sizeof(GLfloat),&data[0]);glVertexPointer(2,GL_FLOAT,5*sizeof(GLfloat),&data[3]);

The second parameter specifies the span, such as color. The data is retrieved from the first in each group of data in the array, and then the data is retrieved across five, coordinate vertices start from 4th of each data set and take data across 5
Note: because the data type needs to be calculated across spans, the Data Types of arrays must be the same

The above code works the same.

List of unreferenced vertex Arrays

1. It provides a gldrawelements function to call glarrayelement cyclically, but an array of indexes must be defined.

Example 5

void drawTwoLineWithArray2(){    GLfloat data[]=  {1.0, 0.0, 0.0,25.0,25.0,                      1.0, 0.0, 0.0,100.0,100.0,                      0.0, 1.0, 0.0,120.0,120.0,                      0.0, 1.0, 0.0,200.0,200.0};    GLubyte index[]= {0,1,2,3} ;        glEnableClientState(GL_VERTEX_ARRAY);    glEnableClientState(GL_COLOR_ARRAY);    glColorPointer(3,GL_FLOAT,5*sizeof(GLfloat),&data[0]);    glVertexPointer(2,GL_FLOAT,5*sizeof(GLfloat),&data[3]);        glDrawElements(GL_LINES,4,GL_UNSIGNED_BYTE,index);    }

2. Starting from the index

glDrawArrays(GL_LINES,0,4);

This function is simpler, allowing users to directly access four elements from 0th.

When there are many vertices, the vertex array should be a very useful function.

Hybrid Array

The following are the most simplified operations:

The glinterleavedarrays function activates various vertex arrays based on parameters and stores vertices as follows:

void drawTwoLineWithArray3(){    GLfloat data[]=  {1.0, 0.0, 0.0,25.0,25.0,0.0,                      1.0, 0.0, 0.0,100.0,100.0,0.0,                      0.0, 1.0, 0.0,120.0,120.0,0.0,                      0.0, 1.0, 0.0,200.0,200.0,0.0};            glInterleavedArrays(GL_C3F_V3F,0,data);    glDrawArrays(GL_LINES,0,4);    }

The results are the same.
The code for removing the color array is as follows:

GLfloat data[]=  {25.0,25.0,                  100.0,100.0,                  120.0,120.0,                  200.0,200.0};glInterleavedArrays(GL_V2F,0,data);glDrawArrays(GL_LINES,0,4);

The above code is highly informative.

From: http://www.cnblogs.com/Clingingboy/archive/2010/10/16/1853304.html

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.