[OpenGL Study Notes 04] vertex Array

Source: Internet
Author: User
Tags array example
Through previous learning, we know that if you want to draw a geometric image, you must constantly call the plotting function, such as drawing a polygon with 20 sides, call at least 22 functions (including glbegin and glend ).
Therefore, OpenGL provides a series of vertex array functions to reduce the number of function calls to improve performance. In addition, vertices can be used to avoid redundant processing of vertex sharing.
1. Simple Example

Let's review how we draw a straight line:

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

For the above example, we use the vertex array as follows:

Void drawlinewitharray () {glint vertices [] = {25, 100,100}; glableclientstate (gl_vertex_array); // enable the vertex array glvertexpointer (2, gl_int, 0, vertices ); // specify the array data glbegin (gl_lines); glarrayelement (0); // dereference and render glarrayelement (1); glend ();}

2. Three steps to use the vertex Array (1) Enable vertex array first
// Specify the array to be started (8 available arrays, including gl_vertex_array, gl_color_array, and gl_index_array) void glableclientstate (glenum array );
(2) specify array data
// Size indicates the number of vertex coordinates (2, 3, 4), type indicates the data type, stride indicates the byte offset between consecutive vertices (0 indicates closely adjacent ), pointer indicates the first address of the array void glvertexpointer (glint size, glenum type, glsizei stride, const glvoid * pointer); // void glcolorpointer (glint size, glenum type, glsizei stride, const glvoid * pointer); void glcolorpointer (glenum type, glsizei stride, const glvoid * pointer );...
(3) dereference and rendering Unreference a single
// Obtain the ith vertex data of the currently enabled array (starting from 0) void glarrayelement (glint ith );
Multiple referers
// Mode specifies the type of the element to be created (the same as the glbegin parameter). Count indicates the number of vertices, and type indicates the vertex data type, indices indicates the index array's first address // gldrawelements serves as multiple glarrayelement (indices [I]) void gldrawelements (glenum mode, glsize count, glenum type, const glvoid * indices ); // equivalent to primcount gldrawelements (mode, Count [I], type, indices [I]) Statement void glmultidrawelements (glenum mode, glsize * count, glenum type, const glvoid ** indices, glsizei primcount); // It is equivalent to a gldrawelements with a range of [start, end] void gldrawrangeelements (glenum mode, gluint start, gluint end, glsize count, glenum type, const glvoid * indices); // create a sequence of elements from each enabled array in the range of [first, first + count-1] void gldrawarrays (glenum mode, glint first, glsizei count); // equivalent to primcount gldrawarrays (mode, first [I], Count [I]) void glmultidrawarrays (glenum mode, glint * First, glsizei * count, glsizei primcount );

3. Using a vertex array example, we now have a vertex array and a color array. We use them to draw lines:
# Include <Gl/glut. h> void display (void) {glclear (gl_color_buffer_bit); // clear the color buffer static glint vertices [] = {25, 100,100,120,120,200,200}; static 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}; glubyte index [] = {,}; glableclientstate (gl_vertex_array ); glableclientstate (gl_color_array); glvertexpointer (2, gl_int, 0, vertices); glcolorpointer (3, gl_float, 0, colors); gldrawelements (gl_lines, 4, rows, index ); // This statement is equivalent to the following comments: // glbegin (gl_lines); // glarrayelement (0); // glarrayelement (1); // glarrayelement (2 ); // glarrayelement (3); // glend (); glflush ();} void reshape (int w, int h) {glviewport (0, 0, (glsizei) W, (glsizei) H); // adjust the pixel matrix size of the drawing glmatrixmode (gl_projection); // specify the current matrix as the projection matrix glloadidentity (); // set the current matrix to the unit matrix gluortho2d (0.0, (gldouble) W, 0.0, (gldouble) H);} void Init (void) {glclearcolor (0.0, 0.0, 0.0, 0.0); // The setting window is cleared to black glmatrixmode (gl_projection); // you can specify the current matrix as the projection matrix glloadidentity (); // set the current matrix as the unit matrix glortho (0.0, 1.0, 0.0, 1.0,-1.0, 1.0); // specify the coordinate system glortho (xmin, xmax, ymin, Ymax, zmin, zmax);} int main (INT argc, char ** argv) {gluinit (& argc, argv ); gluinitwindowsize (250,250); gluinitwindowposition (100,100); glucreatewindow ("hello"); Init (); gludisplayfunc (Display); glureshapefunc (reshape); glumainloop (); Return 0 ;}
:


4. vertex array Span

Let's briefly review the example above:

Void display () {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}; glableclientstate (gl_vertex_array); glableclientstate (gl_color_array); glvertexpointer (2, gl_int, 0, vertices); // obtain from vertices [0, get two entries at a time, and get glcolorpointer (3, gl_float, 0, colors), glbegin (gl_lines), glarrayelement (0), glarrayelement (1) From vertices [2] Next time ); glarrayelement (2); glarrayelement (3); glend ();}

Now we merge them into an array, The stride parameter specifies how to span data (because the data type needs to be calculated, the Data Type of the array needs to be the same). The details are as follows:

Void display () {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}; // synthesize an array of glableclientstate (gl_vertex_array); glableclientstate (gl_color_array); glcolorpointer (3, gl_float, 5 * sizeof (glfloat), & Data [0]); // start from data [0] And get 3 at a time, obtain 3 data entries from data [0 + 5] next time... glvertexpointer (2, gl_float, 5 * sizeof (glfloat), & Data [3]); // glbegin (gl_lines); glarrayelement (0); glarrayelement (1 ); glarrayelement (2); glarrayelement (3); glend ();}

5. Hybrid Array Glinterleavedarrays is equivalent to: Step 1 enable array + Step 2 Specify array data.

// Initialize all eight arrays and disable arrays not specified by format. Stride is the byte offset between consecutive vertices, and pointer is the first address of the array void glinterleavedarrays (glenum format, glsizei stride, const glvoid * pointer );


Let's use a Hybrid Array to rebuild the above example:

Void display () {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 ); // enable the color array and vertex array and specify the data gldrawarrays (gl_lines,); // It is equivalent to four glarrayelement (I) calls in a loop and I is 0 to 3}
The code for removing the color array is as follows:

void drawTwoLineWithArray2(){    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);}

You can see at a glance...


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.