tom*_*xey 1 c++ opengl linker qt qglwidget
我正在尝试将我的旧 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>
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用 GLEW,但它与 Qt(或 QOpenGL?)冲突。
我怎样才能摆脱那些未定义的引用?我还需要链接到其他库吗?
提前致谢。
汤克西
Windows 不提供 OpenGL 1.1 之后引入的任何 OpenGL 函数的原型。您必须在运行时解析指向这些函数的指针(通过GetProcAddress-- 或更好的方法QOpenGLContext::getProcAddress,见下文)。
Qt 提供了出色的推动者来简化这项工作:
\n\nQOpenGLShader并QOpenGLShaderProgram允许您管理着色器、着色器程序及其制服。QOpenGLShaderProgram 提供了很好的重载,允许您无缝地传递QVector<N>D或QMatrix<N>x<N>类:
QMatrix4x4 modelMatrix = model->transform();\nQMatrix4x4 modelViewMatrix = camera->viewMatrix() * modelMatrix;\nQMatrix4x4 modelViewProjMatrix = camera->projMatrix() * modelViewMatrix;\n...\nprogram->setUniform("mv", modelViewmatrix);\nprogram->setUniform("mvp", modelViewProjMatrix);\nRun Code Online (Sandbox Code Playgroud)QOpenGLContext::getProcAddress()是一个独立于平台的函数解析器(可与QOpenGLContext::hasExtension()加载特定于扩展的函数结合使用)
QOpenGLContext::functions()返回一个QOpenGLFunctions对象(由上下文拥有),它作为公共 API 提供 OpenGL 2 (+FBO) / OpenGL ES 2 之间的公共子集。\xc2\xb9 它将为您解析幕后的指针,因此您拥有的一切要做的就是打电话
functions->glUniform4f(...);\nRun Code Online (Sandbox Code Playgroud)QOpenGLContext::versionFunctions<VERSION>()将返回一个QAbstractOpenGLFunctions子类,即与模板参数匹配的子类VERSION(如果不能满足请求,则返回 NULL):
QOpenGLFunctions_3_3_Core *functions = 0;\nfunctions = context->versionFunctions<QOpenGLFunctions_3_3_Core>();\nif (!functions) \n error(); // context doesn\'t support the requested version+profile\nfunctions->initializeOpenGLFunctions(context);\n\nfunctions->glSamplerParameterf(...); // OpenGL 3.3 API\nfunctions->glPatchParameteri(...); // COMPILE TIME ERROR, this is OpenGL 4.0 API\nRun Code Online (Sandbox Code Playgroud)作为替代方法,您可以使您的“绘图”类/继承/来自QOpenGLFunctionsX. 您可以像往常一样初始化它们,但这样您可以保持代码如下:
class DrawThings : public QObject, protected QOpenGLFunctions_2_1\n{\n explicit DrawThings(QObject *parent = 0) { ... }\n bool initialize(QOpenGLContext *context)\n {\n return initializeOpenGLFunctions(context);\n }\n void draw()\n {\n Q_ASSERT(isInitialized());\n // works, it\'s calling the one in the QOpenGLFunctions_2_1 scope...\n glUniform4f(...); \n }\n}\nRun Code Online (Sandbox Code Playgroud)\xc2\xb9 模块中还有“匹配”的类QtOpenGL,即QGLContext和QGLFunctions。如果可能,请避免QtOpenGL在新代码中使用,因为它将在几个版本中被弃用,以支持类QOpenGL*。