OpenGL version 3.3 使用extension 的两种方法

以vbo(Vertex Buffer Object)为例
1) 使用”glext.h”

#define GL_EXT_PROTOTYPES
#include “GL/glut.h”
#include “GL/glext.h”

////////////////////
之后可直接调用glBindBuffer, glGenBuffers等函数
///////////////////

2) 不使用”glext.h”
#include “GL/glx.h”

#define GET_PROC_ADDRESS( str ) glXGetProcAddress( (const GLubyte *)str )
PFNGLBINDBUFFERARBPROC glBindBuffer = NULL;
PFNGLDELETEBUFFERSARBPROC glDeleteBuffers = NULL;
PFNGLGENBUFFERSARBPROC glGenBuffers = NULL;
PFNGLBUFFERDATAARBPROC glBufferData = NULL;
PFNGLBUFFERSUBDATAPROC glBufferSubData = NULL;

void initVBO() {
glBindBuffer = (PFNGLBINDBUFFERARBPROC)GET_PROC_ADDRESS(“glBindBuffer”);
glDeleteBuffers = (PFNGLDELETEBUFFERSARBPROC)GET_PROC_ADDRESS(“glDeleteBuffers”);
glGenBuffers = (PFNGLGENBUFFERSARBPROC)GET_PROC_ADDRESS(“glGenBuffers”);
glBufferData = (PFNGLBUFFERDATAARBPROC)GET_PROC_ADDRESS(“glBufferData”);
glBufferSubData = (PFNGLBUFFERSUBDATAPROC)GET_PROC_ADDRESS(“glBufferSubData”);

}

int main() {
//init opengl driver
initVBO();
}

简要说明一下, 第一种方法使用glext.h, 其中已经包括extension的相应声明, 注意加入”define GL_GLEXT_PROTOTYPES”
对于第二种方法, 通过在文件内声明extension, 需要使用glx来获取函数的地址。

第一种方法的好处的代码简洁, 使用方便;但是不同的机器的glext不同, 因此在发布时需要将glext.h一直发布。 而第二种则没有这个麻烦。

显然如果你在加入”define GL_EXT_PROTOTYPES”同时使用”glext.h”的情况下, 还对glBindBuffer等作相应extension的声明的话, 则会有重复定义的错误.

About seckcoder

People behind the pixels. View all posts by seckcoder

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: