Download presentation
Presentation is loading. Please wait.
Published byShannon Borell Modified over 10 years ago
1
Vertex Buffer Objects, Vertex Array Objects, Pixel Buffer Objects
2
2 Vertex Buffer Objects Store vertex data (vertex, normal vector, color etc.) Stored on graphics hardware for non- immediate rendering. Substantial performance gains over immediate mode rendering.
3
3 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Creating Vertex Buffer Objects Creating a VBO requires 3 steps: 1. Generate a new buffer object with glGenBuffersARB(). 2. Bind the buffer object with glBindBufferARB(). 3. Copy vertex data to the buffer object with glBufferDataARB().
4
4 C Example /* Initialise VBO - do only once, at start of program */ /* Create a variable to hold the VBO identifier */ unsigned int triangleVBO; /* Vertices of a triangle (counter-clockwise winding) */ float data[] = {1.0, 0.0, 1.0, 0.0, 0.0, -1.0, -1.0, 0.0, 1.0}; /* Create a new VBO and use the variable id to store the VBO id */ glGenBuffers(1, &triangleVBO); /* Make the new VBO active */ glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); /* Upload vertex data to the video device */ glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
5
5 /* Draw Triangle from VBO - do each time window, view point or data changes */ /* Establish its 3 coordinates per vertex with zero stride in this array; necessary here */ glVertexPointer(3, GL_FLOAT, 0, NULL); /* Make the new VBO active. Repeat here incase changed since initialisation */ glBindBuffer(GL_ARRAY_BUFFER, triangleVBO); /* Establish array contains vertices (not normals, colours, texture coords etc) */ glEnableClientState(GL_VERTEX_ARRAY); /* Actually draw the triangle, giving the number of vertices provided */ glDrawArrays(GL_TRIANGLES, 0, sizeof(data) / sizeof(float) / 3); /* Force display to be drawn now */ glFlush();
6
6 Updating Vertex Buffer Objects Use glBufferDataARB() or glBufferSubDataARB() to overwrite data. glMapBufferARB() in order to map the buffer object into client's memory. Use glUnmapBufferARB() to unmap. // bind then map the VBO glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboId); float* ptr = (float*)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); // if the pointer is valid(mapped), update VBO if(ptr) { updateMyVBO(ptr,...); // modify buffer data glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); // unmap it }
7
7 Vertex Array Object Despite the name, they do not store vertices. Used to store openGL state attributes. Contain a reference pointer to a vertex buffer object, but this is not required for all cases (e.g. glDrawArrays).
8
8 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Pixel Buffer Objects Similar concept, allows pixel data to be transferred to gfx card without cpu (through direct memory access). Can perform asynchronous DMA transfer between a PBO and a texture object. Can modify a portion of the buffer object or the entire buffer by using glMapBufferARB() and glUnmapBufferARB().
9
9 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 // "index" is used to copy pixels from a PBO to a texture object // "nextIndex" is used to update pixels in the other PBO index = (index + 1) % 2; if(pboMode == 1) nextIndex = index; // with 1 PBO else if(pboMode == 2) nextIndex = (index + 1) % 2; // with 2 PBOs // bind the texture and PBO glBindTexture(GL_TEXTURE_2D, textureId); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pboIds[index]); // copy pixels from PBO to texture object. Use offset instead of pointer. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_BYTE, 0); // bind PBO to update texture source glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pboIds[nextIndex]); Manipulating Textures
10
10 // Note: See Bellow glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, DATA_SIZE, 0, GL_STREAM_DRAW_ARB); // map the buffer object into client's memory GLubyte* ptr = (GLubyte*)glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB); if(ptr) { // update data directly on the mapped buffer updatePixels(ptr, DATA_SIZE); glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB); // release the mapped buffer } glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); Manipulating Textures // Note that glMapBufferARB() causes sync issue. // If GPU is working with this buffer, glMapBufferARB() will wait(stall) // until GPU to finish its job. To avoid waiting (idle), you can call // first glBufferDataARB() with NULL pointer before glMapBufferARB(). // If you do that, the previous data in PBO will be discarded and // glMapBufferARB() returns a new allocated pointer immediately // even if GPU is still working with the previous data.
11
11 // "index" is used to read pixels from framebuffer to a PBO // "nextIndex" is used to update pixels in the other PBO index = (index + 1) % 2; nextIndex = (index + 1) % 2; // set the target framebuffer to read glReadBuffer(GL_FRONT); // read pixels from framebuffer to PBO glReadPixels() returns immediately. glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[index]); glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_BYTE, 0); // map the PBO to process its data by CPU glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[nextIndex]); GLubyte* ptr = (GLubyte*)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB); if(ptr) { processPixels(ptr,...); glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); } // back to conventional pixel operation glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); Modifying the Front Buffer
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.