1

我正在尝试将我的旧 Qt/OpenGL 游戏从 Linux 移植到 Windows。我正在使用 Qt Creator。undefined reference to 'glUniform4fv@12'它立即编译良好,但在链接阶段出现了很多错误。

我试图链接更多的库-lopengl32 -lglaux -lGLU32 -lglut -lglew32,但它给出了相同的结果。

Qt 也-lQt5OpenGLd默认使用。

我将 QGLWIdget 包括在内:

#define GL_GLEXT_PROTOTYPES
#include <QGLWidget>

我也尝试过使用 GLEW,但它与 Qt(或 QOpenGL?)冲突。

我怎样才能摆脱那些未定义的引用?我必须链接到其他任何图书馆吗?

提前致谢。

汤姆西

4

1 回答 1

5

Windows 不提供 OpenGL 1.1 之后引入的任何 OpenGL 函数的原型。您必须在运行时解析指向这些函数的指针(通过GetProcAddress-- 或更好QOpenGLContext::getProcAddress,见下文)。


Qt 提供了出色的推动力来简化这项工作:

  • QOpenGLShaderQOpenGLShaderProgram允许您管理您的着色器、着色器程序及其制服。QOpenGLShaderProgram 提供了很好的重载,允许您无缝传递QVector<N>DQMatrix<N>x<N>类:

    QMatrix4x4 modelMatrix = model->transform();
    QMatrix4x4 modelViewMatrix = camera->viewMatrix() * modelMatrix;
    QMatrix4x4 modelViewProjMatrix = camera->projMatrix() * modelViewMatrix;
    ...
    program->setUniform("mv", modelViewmatrix);
    program->setUniform("mvp", modelViewProjMatrix);
    
  • QOpenGLContext::getProcAddress()是一个独立于平台的函数解析器(与QOpenGLContext::hasExtension()加载特定于扩展的函数结合使用)

  • QOpenGLContext::functions()返回一个QOpenGLFunctions对象(由上下文拥有),它作为公共 API 提供 OpenGL 2 (+FBO) / OpenGL ES 2 之间的公共子集。¹它将为您解析幕后的指针,所以您所要做的就是打电话

    functions->glUniform4f(...);
    
  • QOpenGLContext::versionFunctions<VERSION>()将返回一个QAbstractOpenGLFunctions子类,即与VERSION模板参数匹配的子类(如果无法满足请求,则返回 NULL):

    QOpenGLFunctions_3_3_Core *functions = 0;
    functions = context->versionFunctions<QOpenGLFunctions_3_3_Core>();
    if (!functions) 
         error(); // context doesn't support the requested version+profile
    functions->initializeOpenGLFunctions(context);
    
    functions->glSamplerParameterf(...); // OpenGL 3.3 API
    functions->glPatchParameteri(...); // COMPILE TIME ERROR, this is OpenGL 4.0 API
    
  • 作为替代方法,您可以将“绘图”类 /inherit/ from 设为QOpenGLFunctionsX. 你可以像往常一样初始化它们,但是这样你可以保留你的代码:

    class DrawThings : public QObject, protected QOpenGLFunctions_2_1
    {
        explicit DrawThings(QObject *parent = 0) { ... }
        bool initialize(QOpenGLContext *context)
        {
            return initializeOpenGLFunctions(context);
        }
        void draw()
        {
            Q_ASSERT(isInitialized());
            // works, it's calling the one in the QOpenGLFunctions_2_1 scope...
            glUniform4f(...); 
        }
    }
    

¹ 模块中还有“匹配”类QtOpenGL,即QGLContextQGLFunctions. 如果可能,请避免QtOpenGL在新代码中使用,因为它将在几个版本中被弃用以支持QOpenGL*类。

于 2013-08-24T15:42:35.863 回答