c++ - 手动检索和存储指向 OpenGL 函数的指针时出现问题

标签 c++ linux windows opengl graphics

我在手动检索和存储指向 OpenGL 函数的指针时遇到了一些麻烦,这是我的代码的“简化片段”版本:

#ifdef WIN32
#include <windows.h>
#endif

#include <GL/gl.h>

class CGLManager
{
public:
    // Manager functions
    bool GetAnyGLFuncAddress( const char *_cName, void *_pFunc );
    bool LoadFunctions( void );

    // OpenGL functions
    void (APIENTRY *glBegin)( GLenum mode );
    void (APIENTRY *glEnd)( void );
    void (APIENTRY *glVertex3f)( GLfloat x, GLfloat y, GLfloat z );

private:
#ifdef WIN32
    HMODULE m_hLib; // opengl32.dll
#else
    void *m_hLib; // libGL.so
#endif
};

这是来源:

extern CGLManager gGL;

// GetAnyGLFuncAddress - Attempt to retrieve the OpenGL function named "_cName" and store it in "_pFunc", returns true if success or false otherwise
bool CGLManager::GetAnyGLFuncAddress( const char *_cName, void *_pFunc )
{
#ifdef WIN32
    // Similar to https://www.opengl.org/wiki/Load_OpenGL_Functions#Windows
    _pFunc = (void *)wglGetProcAddress( _cName );
    if ( _pFunc == 0 || (_pFunc == (void *)0x1) || (_pFunc == (void *)0x2) || (_pFunc == (void *)0x3) || (_pFunc == (void *)-1) )
        _pFunc = (void *)GetProcAddress( m_hLib, _cName );
#else
    // TODO: Test this
    // According to some websites, NVIDIA drivers prefer the ARB implementation over the core one
    _pFunc = (void *)glXGetProcAddressARB( _cName );
    if ( _pFunc == 0 || (_pFunc == (void *)0x1) || (_pFunc == (void *)0x2) || (_pFunc == (void *)0x3) || (_pFunc == (void *)-1) )
        _pFunc = (void *)glXGetProcAddress( _cName );
#endif

    return (_pFunc != NULL);
}

// LoadFunctions - Attempt to retrieve all used OpenGL functions, returns true if all of them were retrieved or false if there is a single failure
bool CGLManager::LoadFunctions( void )
{
    if ( !(GetAnyGLFuncAddress( "glBegin", &gGL.glBegin )) )
        return false;

    if ( !(GetAnyGLFuncAddress( "glEnd", &gGL.glEnd )) )
        return false;

    if ( !(GetAnyGLFuncAddress( "glVertex3f", &gGL.glVertex3f )) )
        return false;

    return true;
}

我的管理器通常是这样工作的:它首先检查游戏引擎使用的是哪个渲染器(软件、OpenGL 或 Direct3D),如果不是 OpenGL,那么我们就停止进一步处理。否则,我们加载库(opengl32.dlllibGL.so)并检查它是否好(同样,如果失败,我们停止),我们检索并使用 LoadFunctions 方法存储指向 OpenGL 函数(glBeginglEndglVertex3f)的指针,如果一切正常,我们返回很好或发生了错误。

现在的问题是: GetAnyGLFuncAddress 方法成功检索 OpenGL 函数(换句话说,glBegin 将返回 true,glARandomMethodThatDontExist 将返回 false)但由于某些原因,LoadFunctions 中的 gGL.glBegin(它是“ friend ”)不会更新,它将始终为 NULL,从而导致一场车祸。

我已经尝试了几个小时,通过在 Internet 和 StackOverflow 上搜索来找到解决方案,但我没有找到任何可以为我提供问题解决方案的答案。

在我在 StackOverflow 上找到的许多网站和答案中,很多人建议使用像 GLEW 这样的 OpenGL 加载库,甚至 OpenGL wiki 也推荐它。 但是,由于我所处环境的性质,我不能使用那些类型的库,也不能直接使用 OpenGL 函数,我知道我正在经历手动完成所有操作的痛苦方式,但我别无选择。

感谢您的回答。

最佳答案

bool CGLManager::GetAnyGLFuncAddress( const char *_cName, void *_pFunc )

这只是普通的、损坏的 C++。 _pFunc 是一个 void*。指针是值。更改 _pFunc 的值不会更改传入变量的值。

您应该只返回指针(NULL 表示失败条件),或者 _pFunc 应该是 void**。不过,这可能需要调用者进行强制转换,并且 GetAnyGLFuncAddress 需要执行 *_pFunc = 来设置值。

如果要返回函数指针,则需要在存储之前将返回的 void* 转换为适当的函数指针类型。这就是为什么您经常看到 OpenGL 加载器对函数指针类型使用 typedef。

typedef void (APIENTRY *(PFN_GLBEGIN))( GLenum );

...

PFN_GLBEGIN glBegin;

...

glBegin = static_cast<PFN_GLBEGIN>(GetAnyGLFuncAddress("glBegin"));

类似的东西。

关于c++ - 手动检索和存储指向 OpenGL 函数的指针时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37597212/

相关文章:

c++ - 如何减少 C++ 中多重集中元素的数量?

c++ - 如何获取 map 值的引用?

linux - Linux RSS 不等于 java Xx + MaxMetaspaceSize 吗?

c++ - 多处理浏览器?

c++ - 计算嵌套包中的类型总数

c++ - Boost mingw链接错误

c++,linux,如何解释 `stat64` 中文件的模式?

linux - TIUSB3410 Linux 驱动

windows - 如何在不重新启动的情况下卸载 Windows 服务并删除其文件

c++ - 如何使用 C++ 检查安装在计算机中的 ShellIconOverLayIdentifers 总数