Go OpenGL implements vertex array drawing vertices via VBO

Source: Internet
Author: User

#include"stdlib.h"#include<OpenGL/glext.h>#include<GLUT/GLUT.h>#defineBuffer_offset (bytes) ((glubyte*) NULL + (bytes))#defineVERTICES 0#defineINDICES 1#defineNum_buffers 6Gluint Buffers[num_buffers]; Glfloat vertices[][3] = {      {-1.0f,-1.0f,-1.0f},      {1.0f,-1.0f,-1.0f},      {1.0f,1.0f,-1.0f},      {-1.0f,1.0f,-1.0f},      {-1.0f,-1.0f,1.0f},      {1.0f,-1.0f,1.0f},      {1.0f,1.0f,1.0f},      {-1.0f,1.0f,1.0f}  }; Glubyte indices[][4] = {      {0,1,2,3},      {4,7,6,5},      {0,4,5,1},      {3,2,6,7},      {0,3,7,4},      {1,5,6,2}  }; Glfloat*bdata; voidChangesize (intWinth) {Glviewport (0,0, (Glsizei) W, (Glsizei) h);      Glmatrixmode (gl_projection);      Glloadidentity (); Gluperspective (45.0,(Double) w/(Double) H,0.01, -);      Glmatrixmode (Gl_modelview);  Glloadidentity (); }    voidKeyDown (unsignedCharKeyintXinty) {Switch(key) { Case'b': Bdata= (glfloat*) Glmapbuffer (gl_array_buffer,gl_read_write);  for(inti =0; I < -; i + +)              {                  * (bdata + i) *=1.1f;              } glunmapbuffer (Gl_array_buffer);              Glutpostredisplay ();  Break; }  }    voidMouseintbuttonintStateintXinty) {}voidDisplayvoid) {glloadidentity (); Gltranslatef (0.0f,0.0f,-5.0f); Glrotatef (20009,1.0f,1.0f,0.0f); //6. Draw, this place note is the last parameter, this and ordinary non-VBO the biggest difference is hereGldrawelements (Gl_quads, -, Gl_unsigned_byte,buffer_offset (0)); //gldrawarrays (gl_triangle_fan, 0, 8); glutswapbuffers (); }    voidInitvoid) {Glclearcolor (0.0f,0.0f,0.0f,0.0f);      Glshademodel (Gl_flat); Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit); //1. Create a Buffer object, the system will automatically create an easing area identifier into the buffers, you can use the Glisbuffer () function to determine whether an identifier is usedglgenbuffers (num_buffers, buffers); //2. Activates the buffer object, which indicates which buffer will be affected by the future selection of the buffer object. If you want a financial buffer object, you can use zero as the identifier for the buffer object, and the first parameter target can be Gl_array_buffer,gl_element_array_fufferGlbindbuffer (gl_array_buffer,buffers[vertices]); //3. Allocating and initializing buffer objects with dataGlbufferdata (Gl_array_buffer,sizeof(vertices), vertices,gl_dynamic_draw); //4. Specify an offset relative to the start of the bufferGlvertexpointer (3, Gl_float,0, Buffer_offset (0)); //5. Be sure not to forget to startglenableclientstate (Gl_vertex_array);      Glbindbuffer (Gl_element_array_buffer,buffers[indices]); Glbufferdata (Gl_element_array_buffer,sizeof(indices), indices,gl_static_draw); }    intMainintargcChar*argv[]) {Glutinit (&argc, argv); Glutinitdisplaymode (gl_double| Glut_rgba |glut_depth); Glutinitwindowsize ( +, +); Glutcreatewindow ("Buffer Demo");      Init ();      Glutreshapefunc (changesize);      Glutmousefunc (mouse);      Glutkeyboardfunc (KeyDown);           Glutdisplayfunc (display);      Glutmainloop (); return 0; } 

SOURCE Link: OpenGL uses VBO to achieve vertex array drawing vertices

Go OpenGL implements vertex array drawing vertices via VBO

Related Article

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.