在 Windows 上使用 OpenGL 扩展 [英] Using OpenGL extensions On Windows

查看:38
本文介绍了在 Windows 上使用 OpenGL 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 OpenGL 扩展下公开的函数.我使用的是 Windows,我该怎么做?

I want to use the functions exposed under the OpenGL extensions. I'm on Windows, how do I do this?

推荐答案

简单的解决方案:使用 GLEW.在此处了解如何.

Easy solution: Use GLEW. See how here.

硬解:如果您有非常充分的理由不使用 GLEW,以下是不使用 GLEW 的方法:

Hard solution: If you have a really strong reason not to use GLEW, here's how to achieve the same without it:

确定您希望使用的 OpenGL 扩展和扩展 API.OpenGL 扩展注册表中列出了 OpenGL 扩展.

Identify the OpenGL extension and the extension APIs you wish to use. OpenGL extensions are listed in the OpenGL Extension Registry.

示例:我希望使用 EXT_framebuffer_object 的功能 扩展名.我希望从此扩展中使用的 API 是:

Example: I wish to use the capabilities of the EXT_framebuffer_object extension. The APIs I wish to use from this extension are:

glGenFramebuffersEXT()
glBindFramebufferEXT()
glFramebufferTexture2DEXT()
glCheckFramebufferStatusEXT()
glDeleteFramebuffersEXT()

检查您的显卡是否支持您要使用的扩展.如果是这样,那么您的工作就快完成了!为您的显卡下载并安装最新的驱动程序和 SDK.

Check if your graphic card supports the extension you wish to use. If it does, then your work is almost done! Download and install the latest drivers and SDKs for your graphics card.

示例:我的 PC 中的显卡是 NVIDIA 6600 GT.因此,我访问了 NVIDIA OpenGL 扩展规范 网页,发现 EXT_framebuffer_object 扩展.然后我下载最新的 NVIDIA OpenGL SDK 并安装它.

Example: The graphics card in my PC is a NVIDIA 6600 GT. So, I visit the NVIDIA OpenGL Extension Specifications webpage and find that the EXT_framebuffer_object extension is supported. I then download the latest NVIDIA OpenGL SDK and install it.

您的显卡制造商提供了一个 gleext.h 头文件(或类似名称的头文件),其中包含使用支持的 OpenGL 扩展所需的所有声明.(请注意,并非所有扩展都可能受支持.)将此头文件放在编译器可以获取的位置,或者将其目录包含在编译器的包含目录列表中.

Your graphic card manufacturer provides a glext.h header file (or a similarly named header file) with all the declarations needed to use the supported OpenGL extensions. (Note that not all extensions might be supported.) Either place this header file somewhere your compiler can pick it up or include its directory in your compiler's include directories list.

在您的代码中添加 #include <gleext.h> 行以将头文件包含到您的代码中.

Add a #include <glext.h> line in your code to include the header file into your code.

打开glext.h,找到您要使用的 API 并获取其相应的难看声明.

示例:我搜索上述帧缓冲区 API 并找到它们对应的丑陋声明:

Example: I search for the above framebuffer APIs and find their corresponding ugly-looking declarations:

typedef void (APIENTRYP PFNGLGENFRAMEBUFFERSEXTPROC) (GLsizei n, GLuint *framebuffers); for GLAPI void APIENTRY glGenFramebuffersEXT (GLsizei, GLuint *);

这意味着你的头文件有两种形式的 API 声明.一种是类似 wgl 的丑陋函数指针声明.另一个是看起来很正常的函数声明.

All this means is that your header file has the API declaration in 2 forms. One is a wgl-like ugly function pointer declaration. The other is a sane looking function declaration.

对于您希望使用的每个扩展 API,将函数名称的代码声明添加为一种难看的字符串类型.

For each extension API you wish to use, add in your code declarations of the function name as a type of the ugly-looking string.

示例:

PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;

虽然看起来很难看,但我们所做的只是声明与扩展 API 对应的类型的函数指针.

Though it looks ugly, all we're doing is to declare function pointers of the type corresponding to the extension API.

用它们的合法函数初始化这些函数指针.这些函数由库或驱动程序公开.我们需要使用 wglGetProcAddress() 函数来做到这一点.

Initialize these function pointers with their rightful functions. These functions are exposed by the library or driver. We need to use wglGetProcAddress() function to do this.

示例:

glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC) wglGetProcAddress("glGenFramebuffersEXT");
glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC) wglGetProcAddress("glBindFramebufferEXT");
glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) wglGetProcAddress("glFramebufferTexture2DEXT");
glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC) wglGetProcAddress("glCheckFramebufferStatusEXT");
glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC) wglGetProcAddress("glDeleteFramebuffersEXT");

不要忘记检查NULL 的函数指针.如果偶然 wglGetProcAddress() 找不到扩展函数,它会用 NULL 初始化指针.

Don't forget to check the function pointers for NULL. If by chance wglGetProcAddress() couldn't find the extension function, it would've initialized the pointer with NULL.

示例:

if (NULL == glGenFramebuffersEXT || NULL == glBindFramebufferEXT || NULL == glFramebufferTexture2DEXT
    || NULL == glCheckFramebufferStatusEXT || NULL == glDeleteFramebuffersEXT)
{
    // Extension functions not loaded!
    exit(1);
}

就是这样,我们完成了!您现在可以使用这些函数指针,就像存在函数调用一样.

That's it, we're done! You can now use these function pointers just as if the function calls existed.

示例:

glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorTex[0], 0);

参考: 超越适用于 Windows 的 OpenGL 1.1 作者:Dave Astle — 这篇文章有点过时,但包含了您需要了解的所有信息,以帮助您了解 Windows 上为什么会出现这种可悲情况以及如何解决它.

Reference: Moving Beyond OpenGL 1.1 for Windows by Dave Astle — The article is a bit dated, but has all the information you need to understand why this pathetic situation exists on Windows and how to get around it.

这篇关于在 Windows 上使用 OpenGL 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆