2

I’m new to OpenGL so this might be a stupid question. It seems like I'm missing something obvious. I can’t see how the new OpenGL classes are supposed to be useful.

There are classes for each version and profile of OpenGL. These classes have explicit methods for all the OpenGL calls supported by each version. Here's a partial list:

  • QOpenGLFunctions_1_0
  • QOpenGLFunctions_1_1
  • QOpenGLFunctions_1_2
  • QOpenGLFunctions_1_3
  • QOpenGLFunctions_1_4

I assume it would be something like the following:

  • query the video card and ask which version(s) of OpenGL it supports.
  • instantiate a copy of the class for the highest version supported using QOpenGLContext::versionFunctions()

So how do I write code using this class? I can't predict which object I will get at run time without knowing explicitly what hardware it will run on. The base class contains no methods since they're different for every derived class. I could write a giant switch but that seems a step backward from using QOpenGLFunctions or just getting the function addresses manually.

4

1 回答 1

3

这些类之所以有用,是因为之前的 QOpenGLFunctions 类只公开了 OpenGL/ES 2.0 功能。现在,他们已经公开了许多 OpenGL 版本的全部功能,允许您利用仅在这些版本中提供的功能。

当然,大多数开发人员不会在运行时为大多数应用程序选择 GL 版本。它们以特定版本为目标,因此 Qt 类可以很好地工作。

如果您正在寻找的是一种在各种 QOpenGLFunctions_* 类之间调用“通用”方法而不知道您正在使用哪个版本的 OpenGL 的方法(同时仍然让自己有机会利用“更高”的特定功能版本),为什么不使用模板?

例如:

template <class T>
class SomeOpenGLRendering {
public:
    SomeOpenGLRendering(T *openglFunctions) : openglFunctions(openglFunctions) {
        openglFunctions->initializeOpenGLFunctions();
    }

    void renderSomething() {
        openglFunctions->glClear(GL_COLOR_BUFFER_BIT);
    }

private:
    T *openglFunctions;
};

然后根据您想要的任何标准(例如,您所说的硬件检测),根据需要创建正确的版本:

SomeOpenGLRendering<QOpenGLFunctions_3_2_Core> r(new QOpenGLFunctions_3_2_Core());
r.renderSomething();
于 2013-10-18T19:44:41.487 回答