c++ - 我可以使 opengl 扩展指针全局化吗?

标签 c++ opengl

使用 opengl/windows 10/visual c++ 2015

我的程序中有一个“窗口”类,它创建并维护一个 win32 屏幕窗口,包括在其上创建一个“现代”opengl 上下文。作为执行此操作的一部分,它获得指向许多 opengl“扩展”函数的指针。

我的问题是我可以全局存储这些扩展指针/静态类,还是我需要为每个实例或窗口保留一个拷贝,因为它们会有不同的 opengl 上下文?对于我创建的每个 opengl 上下文,指针是否有效且相同,或者我是否需要为我创建的每个窗口一遍又一遍地执行此操作?

最佳答案

Are the pointers valid and the same for every opengl context I create or do I need to do it over and over again for every window I create?

这取决于所使用的操作系统和窗口系统。 WGL specification says ,对于每个 OpenGL 上下文,允许 OpenGL 扩展函数指针不同:

wglGetProcAddress():

...

Remarks

The OpenGL library supports multiple implementations of its functions. Extension functions supported in one rendering context are not necessarily available in a separate rendering context. Thus, for a given rendering context in an application, use the function addresses returned by the wglGetProcAddress function only.

对比X11/GLX specifies (page 35, section 3.3.12)无论上下文如何,OpenGL 扩展函数指针都是不变的(即不改变):

GL function pointers returned by glXGetProcAddress are independent of the currently bound context and may be used by any context which supports the extension.

在 Windows 的情况下,最可行的方法是在上下文句柄和包含所有函数指针的结构之间建立映射,即

struct glfunctions {
    /* all the function pointers */
};
std::map<HGLRC,glfunctions> context_glfunctions;

随意向 map 添加某种形式的 LRU,这样上下文的重复查找就不必遍历整个 map 。

关于c++ - 我可以使 opengl 扩展指针全局化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38803133/

相关文章:

opengl - 给定一个 4x4 齐次矩阵,我如何获得 3D 世界坐标?

c++ - 整数 vector 的 initializer_list 语法

c++ - GCC 4.8.x 中的 Bug 处理灵活数组成员?

c++ - OpenCL,float vs uint,预期的性能提升?

使用 GL_LINE_STRIP 的 OpenGL 正交草图绘制

opengl - _REV 后缀在 OpenGL 中是什么意思?

java - 将 C++ long 类型转换为 JNI jlong

c++ - Boost buffer_cast无法从void *转换为PointerToPodType

opengl - openGL 图中的轴

c++ - 为什么 glm::frustumLH_ZO 的值 [2][0] 和 [2][1] 被求反?