Using OpenGL's Vertex Buffer Extension (ARB_vertex_buffer_object)

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值