Graphical drawing of Vbo under OpenGL

Source: Internet
Author: User

In order to avoid repeatedly transmitting the same fixed-point data to the video card, the cache object can be used to upload data to the video card when plotting a large number of vertex data.

Preparing data

Our display data is a cube, as shown below

Vertex data structures are color (RGBA) Normal (XYZ) coordinates (XYZ)

Vertex data is stored in vertices, the index data for the fixed-point is stored in indices, and the handle to the cached object VertexBuffer and IndexBuffer

struct Custom_vertex{float R, G, B, A;float NX, NY, Nz;float x, Y, z;}; Custom_vertex vertices[] = {{1.0f, 0.0f, 0.0f, 1.0f, -0.577f, 0.577f, 0.577f, -1.0f, 1.0f, 1.0f},{1.0f, 0.0f, 0.0f, 1.0 F, -0.577f, -0.577f, 0.577f, -1.0f, -1.0f, 1.0f},{1.0f, 0.0f, 0.0f, 1.0f, 0.577f, 0.577f, 0.577f, 1.0f, 1.0f, 1.0f},{1 .0f, 0.0f, 0.0f, 1.0f, 0.577f, -0.577f, 0.577f, 1.0f, -1.0f, 1.0f},{1.0f, 0.0f, 0.0f, 1.0f, 0.577f, 0.577f, -0.577f, 1.0 F, 1.0f, -1.0f},{1.0f, 0.0f, 0.0f, 1.0f, 0.577f, -0.577f, -0.577f, 1.0f, -1.0f, -1.0f},{1.0f, 0.0f, 0.0f, 1.0f,-0.577 F, 0.577f, -0.577f, -1.0f, 1.0f, -1.0f},{1.0f, 0.0f, 0.0f, 1.0f, -0.577f, -0.577f, -0.577f, -1.0f, -1.0f, -1.0f},}; Gluint indices[] = {0, 2, 3, 4, 5, 3,4, 6, 7, 5,6, 0, 1, 7,6, 4, 2, 0,1, 3, 5, 7,};int num_indices = sizeof (indices) /sizeof (indices[0]); int num_vertices = sizeof (vertices)/sizeof (vertices[0]); Gluint VertexBuffer; Gluint IndexBuffer;
Initialize

Glgenbuffers creates a buffer object, the handle (identifier of the created object) is stored in VertexBuffer, the number of objects created in this example is 1

Glbindbuffer activates the buffer, specifying the current buffered object, so that the action is performed on the object. Gl_array_buffer represents vertex data and Gl_element_array_buffer represents the index data.

The function has three functions:

1.buffer (not 0) creates a new buffered object for first use

2.buffer activates the object if it is already created

3.buffer is 0 to stop using the object

Glbufferdata allocates the data space for the cached object and then copies the data from memory to the buffered object, gl_static_draw that the data is copied only once and can be used multiple times for drawing.


Vertex cache Glgenbuffers (1, &vertexbuffer); Glbindbuffer (Gl_array_buffer, vertexbuffer); Glbufferdata (GL_ARRAY_ Buffer, num_vertices * sizeof (CUSTOM_VERTEX), vertices, gl_static_draw);//Index cache Glgenbuffers (1, &indexbuffer); Glbindbuffer (Gl_element_array_buffer, IndexBuffer); Glbufferdata (Gl_element_array_buffer, num_indices * 4, indices, Gl_static_draw);//Unbind Glbindbuffer (gl_array_buffer, 0); Glbindbuffer (Gl_element_array_buffer, 0);

using Cached Objects

    Activates the Cache object    Glbindbuffer (Gl_array_buffer, vertexbuffer);    Glbindbuffer (Gl_element_array_buffer, indexbuffer);    Specifies the array format    glinterleavedarrays (gl_c4f_n3f_v3f, 0, NULL);    Start Drawing    gldrawelements (Gl_quads, Num_indices, Gl_unsigned_int, NULL);    Draw complete, unbind    //  reprint Please specify Http://blog.csdn.net/boksic     //If in  doubt welcome message    Glbindbuffer (gl_array_ BUFFER, 0);    Glbindbuffer (gl_element_array_buffer, 0);


The effect is as follows

Update of cached data

The next step is to update the vertex data, which can be obtained using glmapbuffer (which is suitable for a large number of updates, and other functions such as glbuffersubdata should be used in some updates)

In this example, the coordinates of all vertices are updated to allow them to move in sinusoidal motion at any time

T + = 0.0001; glfloat* Data;data = (glfloat*) glmapbuffer (Gl_array_buffer, Gl_read_write), if (Data! = (glfloat*) NULL) {for (int i = 0; I & Lt Num_vertices; i++) {DATA[10 * i + 8] + = 0.001*cos (t);} Glunmapbuffer (Gl_array_buffer);}

Results:











Graphical drawing of Vbo under OpenGL

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.