Using OpenGL's Vertex Buffer Extension (ARB_vertex_buffer_object)

来源:互联网 发布:台海网络电视台tv透 编辑:程序博客网 时间:2024/05/03 13:27

Introduction

Note: This tutorial assumes basic OpenGL/extensions knowledge.

The ARB_vertex_buffer_object extension was approved by the Architecture Review Board (ARB) on February 12th, 2003. It works on most graphics cards (providing you have the right drivers). But what is it?

This extension moves vertex array data onto the graphics card's memory, which allows for extremely quick access.

Initialization

First of all, we need to be able to actually use the extension's functions. Since the OpenGL headers and library files haven't been updated since OpenGL 1.1 (on Windows), we need to use wglGetProcAddress() to get the functions.

PFNGLBINDBUFFERARBPROC glBindBufferARB = NULL;PFNGLGENBUFFERSARBPROC glGenBuffersARB = NULL;PFNGLBUFFERDATAARBPROC glBufferDataARB = NULL;[check whether extension is supported]glBindBufferARB = (PFNGLBINDBUFFERARBPROC) wglGetProcAddress("glBindBufferARB");glGenBuffersARB = (PFNGLGENBUFFERSARBPROC) wglGetProcAddress("glGenBuffersARB");glBufferDataARB = (PFNGLBUFFERDATAARBPROC) wglGetProcAddress("glBufferDataARB");

We also need a buffer and something to put in it.

GLfloat data[] = { -1.0f,  1.0f, 0.0f,                   -1.0f, -1.0f, 0.0f,                    1.0f, -1.0f, 0.0f };GLuint buffer = 0;

Creating a buffer

The functions used for creating buffers are just like the standard OpenGL texture functions.

glGenBuffersARB(1, &buffer);glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer);glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(data), data, GL_STATIC_DRAW_ARB);

So what does this block of code do? glGenBuffersARB creates a buffer, glBindBufferARB binds it (duh) and glBufferDataARB uploads the vertex array to the graphics card.

Using the buffer

This is the easy part :)

glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer);glVertexPointer(3, GL_FLOAT, 0, 0);glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
原创粉丝点击