使用QOpenGLFunctions在C ++对象中封装OpenGL功能 [英] Encapsulating OpenGL functionality in C++ objects while using QOpenGLFunctions

查看:1760
本文介绍了使用QOpenGLFunctions在C ++对象中封装OpenGL功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定将Qt集成到我的OpenGL项目中。到目前为止,我已经使用原始的OpenGL调用来做纹理,帧缓冲,绘制东西等。问题是OpenGL调用隐藏在对象,如纹理对象。例如,你会给纹理对象一个字符串路径在其构造函数,然后该对象将自动加载该文件的纹理数据,并创建一个OpenGL纹理(显然使用OpenGL函数这样做)。由于QOpenGLFunctions是一个对象本身,我要么限制这些调用的位置,要么传递一个引用到我的项目中的一个对象,使得OpenGL函数调用。



这真的是不可取的。有没有我能解决这个问题?我应该重新考虑我的OpenGL对象的设计吗?

解决方案

你有几个选项。我个人认为将渲染代码本地化在软件的有限部分中通常是合乎需要的。显式传递一个 QOpenGLFunctions 对象到需要它的类允许你明确哪些类渲染,并保持一定量的控制。



另一种选择是保留对 QOpenGLFunctions的引用对象,所以每个需要它的人都可以获得一个实例的持有。虽然单身人士的使用可以争论(我真的不想开始关于单身人士的善良或邪恶的宗教辩论),这是一个非常实际的解决方案。或者如果你不在乎隐藏你真正在做的事情,你可以保持引用在一个全局变量。



就我可以告诉从Qt文档( http://qt-project.org/doc/qt-5/qopenglfunctions) .html ),创建多个实例也是完全合法的。它看起来不像包含任何状态的实例。因此,当您构造任何需要OpenGL渲染的对象时,您可以创建一个实例,并将其保存在成员变量中。有一个代码片段显示如何在文档页面上创建一个新的实例:

  QOpenGLFunctions glFuncs(QOpenGLContext :: currentContext ); 

你只需要小心,在上下文为当前时创建实例。 p>

I've decided to integrate Qt into my OpenGL project. Up until now, I've used raw OpenGL calls to make textures, make framebuffers, draw things etc. The problem is that the OpenGL calls are hidden inside objects such as the texture object. For example, you would give the texture object a string path in its constructor, and then the object would automatically load that texture data from the file and create an OpenGL texture with it( Obviously using OpenGL functions to do so ). Thanks to QOpenGLFunctions being an object itself, I either have to restrict where these calls are made, or pass a reference to it to EVERY object in my project that makes an OpenGL function call.

This really isn't desirable. Is there anyway I can get around this? Should I rethink the design of my OpenGL objects?

解决方案

You have a few options. I personally think that having the rendering code localized in a limited part of the software is generally desirable. Explicitly passing a QOpenGLFunctions object to the classes that need it allows you to make it clear which classes do rendering, and maintain some amount of control over it. Unless it gets too cumbersome in your specific architecture, that would probably be my first choice.

Another option is that you keep a reference to the QOpenGLFunctions object in a singleton, so everybody who needs it can get a hold of the instance. While the use of singletons can be argued (and I really don't want to start a religious debate about the goodness or evilness of singletons), that's a very practical solution. Or if you don't care about hiding what you're really doing, you can keep the reference in a global variable.

As far as I can tell from the Qt documentation (http://qt-project.org/doc/qt-5/qopenglfunctions.html), it's also perfectly legal to create multiple instances. It doesn't look like the instances contain any state. So you could create an instance when you construct any object that needs OpenGL rendering, and keep it in a member variable. There's a code fragment that shows how to create a new instance on that documentation page:

QOpenGLFunctions glFuncs(QOpenGLContext::currentContext());

You'll just have to be careful that the instances are created while the context is current.

这篇关于使用QOpenGLFunctions在C ++对象中封装OpenGL功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆