The so-called vertex buffer is a buffer that stores vertex data directly on the GPU and does not need to be copied from the CPU to the GPU. Improve the operation efficiency of the program.
Operation Steps
1. Creating a vertex Buffer object
Gluint Vertexbufferid;
2. Allocating space
Glgenbuffers (1, &vertexbufferid);
3. Binding the current vertex buffer object
Glbindbuffer (Gl_array_buffer, Vertexbufferid);
4. Initializing buffer data
sizeof (vertices), vertices,
5. Enable array of vertex attributes
Glenablevertexattribarray (glkvertexattribposition);
6. Rendering with vertex data
glvertexattribpointer ( glkvertexattribposition, 3, gl_float, gl_ FALSE, sizeof(Scenevertex), NULL);
7. Draw
0,3
Assign all the code below
@interface openglesviewcontroller:glkviewcontroller{ *baseeffect; @end
#import"OpenGLESViewController.h"@implementation openglesviewcontroller@synthesize baseeffect;///////////////////////////////////////////////////////////////////This data type was used to store information for each vertextypedefstruct{GLKVector3 positioncoords;} Scenevertex;//Define vertex data for a triangle to use in exampleStatic ConstScenevertex vertices[] = { {{-0.5f, -0.5f,0.0}},//Lower left corner{{0.5f, -0.5f,0.0}},//Lower right corner{{-0.5f,0.5f,0.0}}//Upper left corner};///////////////////////////////////////////////////////////////////called when the View controller's view is loaded//Perform Initialization Before the view is asked to draw- (void) viewdidload{[Super Viewdidload]; //Verify The type of view created automatically by the//Interface Builder StoryboardGlkview *view = (Glkview *) Self.view; Nsassert ([View Iskindofclass:[glkviewclass]], @"View Controller ' s View is not a glkview"); //Create an OpenGL ES 2.0 context and provide it to the//ViewView.context =[[Eaglcontext alloc] initwithapi:keaglrenderingapiopengles2]; //Make the new context current[Eaglcontext SetCurrentContext:view.context]; //Create A base effect that provides standard OpenGL ES 2.0//Shading Language Programs and set constants to is used for//All subsequent renderingSelf.baseeffect =[[Glkbaseeffect alloc] init]; Self.baseEffect.useConstantColor=gl_true; Self.baseEffect.constantColor=Glkvector4make (1.0f,//Red 1.0f,//Green 1.0f,//Blue 1.0f);//Alpha//Set The background color stored in the current contextGlclearcolor (0.0f,0.0f,0.0f,1.0f);//Background Color//Generate, bind, and initialize contents of a buffer to be//stored in GPU memoryGlgenbuffers (1,//STEP 1&Vertexbufferid); Glbindbuffer (Gl_array_buffer,//STEP 2Vertexbufferid); Glbufferdata (//STEP 3Gl_array_buffer,//Initialize Buffer Contents sizeof(vertices),//Number of bytes to copyVertices//Address of bytes to copyGl_static_draw);//Hint:cache in GPU memory}///////////////////////////////////////////////////////////////////Glkview Delegate method:called by the view controller ' s view//whenever Cocoa Touch asks the view controller ' s view to//draw itself. (In this case, render to a frame buffer that//shares memory with a Core Animation Layer)- (void) Glkview: (Glkview *) View Drawinrect: (cgrect) rect{[Self.baseeffect Preparetodraw]; //Clear Frame Buffer (Erase previous drawing)glclear (gl_color_buffer_bit); //Enable use of positions from bound vertex bufferGlenablevertexattribarray (//STEP 4glkvertexattribposition); Glvertexattribpointer (//STEP 5Glkvertexattribposition,3,//three components per vertexGl_float,//data is floating pointGl_false,//no fixed point scaling sizeof(Scenevertex),//no gaps in dataNULL);//NULL tells GPU to start at//beginning of bound buffer//Draw Triangles using the first three vertices in the//currently bound vertex bufferGldrawarrays (Gl_triangles,//STEP 6 0,//Start with first vertex in currently bound buffer 3);//Use three vertices from currently bound buffer}///////////////////////////////////////////////////////////////////called when the View controller's view has been unloaded//Perform clean-up That's possible when you know the view//controller ' s view won ' is asked to draw again soon.- (void) viewdidunload{[Super Viewdidunload]; //Make the view's context currentGlkview *view = (Glkview *) Self.view; [Eaglcontext SetCurrentContext:view.context]; //Delete buffers aren ' t needed when view is unloaded if(0!=Vertexbufferid) {Gldeletebuffers (1,//STEP 7&Vertexbufferid); Vertexbufferid=0; } //Stop Using the context created In-viewdidload((Glkview *) self.view). Context =Nil; [Eaglcontext Setcurrentcontext:nil];} @end
OpenGL ES 3.0 Vertex buffer