将渲染目标添加到QOpenGLWidget的默认帧缓冲区 [英] Add render target to default framebuffer of QOpenGLWidget

查看:401
本文介绍了将渲染目标添加到QOpenGLWidget的默认帧缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向QOpenGLWidget的默认帧缓冲区添加第二个渲染目标.

I'd like to add a second render target to the default framebuffer of a QOpenGLWidget.

原因是我想实现对象拾取,并通过将分段蒙版渲染到gl_FragData[1]中来检查用户是否击中了对象.不幸的是,您只能从窗口小部件中检索GLuint句柄,并且没有QOpenGLFramebufferObject的构造函数接受该句柄,并且没有其他选择来检索帧缓冲区.

The reason is that I'd like to implement object picking and check whether the user hit an object by rendering a segmentation mask into gl_FragData[1]. Unfortunately, you can only retrieve the GLuint handle from the widget and there is no constructor of QOpenGLFramebufferObject that takes in the handle and there is no other option to retrieve the framebuffer.

是否有可能在没有解决方法的情况下将另一个纹理附加到小部件的默认帧缓冲区?

Is there any possibility to attach another texture to the default frame buffer of the widget without workarounds?

我唯一想到的两个选择是:

The only two options I can think of are:

1. 像这样在初始化中使用本机OpenGL调用(我宁愿坚持使用纯Qt)附加纹理(当然,我将存储segmentationTexture以便以后删除它):

1. Attaching a texture using native OpenGL calls (I'd rather stick to pure Qt) like so in the initialization (of course I'll store segmentationTexture to be able to delete it later):

glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject());
QOpenGLTexture *segmentationTexture = new QOpenGLTexture(QOpenGLTexture::BindingTargetBuffer);
// set texture parameters
segmentationTexture.create();
segmentationTexture.bind();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, segmentationTexture.textureId(), 0);
segmentationTexture.release();

,然后在paintGL()

GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, buffers);

在OpenGL绘制调用之前,并使用glReadBuffer(GL_COLOR_ATTACHMENT1);gl_FragData[1]中检索内容.或者,如果这行不通,请仅使用本机OpenGL代码生成纹理.

before the OpenGL draw calls and use glReadBuffer(GL_COLOR_ATTACHMENT1); to retrieve the content from gl_FragData[1]. Or maybe, if this doesn't work, using only native OpenGL code to generate the texture.

2. 创建第二个帧缓冲区对象,将其绑定到paintGL()中,然后使用glBlitFramebuffer与默认帧缓冲区交换内容(以考虑多次采样)以显示渲染,但是使用第二个帧缓冲区从gl_FragData[1]中读取.但这感觉有点讨厌".

2. Create a second framebuffer object, bind it in paintGL() and then swap the content with the default framebuffer using glBlitFramebuffer (to account for multisampling) to display the rendering, but use the second framebuffer to read from gl_FragData[1]. But this feels a bit "nasty".

推荐答案

我想在QOpenGLWidget的默认帧缓冲区中添加第二个渲染目标.

I'd like to add a second render target to the default framebuffer of a QOpenGLWidget.

答案很简单,您不能向任何OpenGL默认帧缓冲区对象添加第二个颜色附件.

The answer is simple, you can't add a second color attachment, to any OpenGL default framebuffer object.

请参见 OpenGL 4.6 API兼容性配置文件规范; 2.1.执行模型第9页

有两种类型的帧缓冲区:当上下文成为当前上下文时,由窗口系统提供的与上下文相关联的帧缓冲区,以及由应用程序创建的帧缓冲区. 窗口系统提供的帧缓冲区称为默认帧缓冲区.可以根据需要创建应用程序创建的帧缓冲区,称为帧缓冲区对象.上下文可以与两个帧缓冲区相关联,其中一个用于读取和绘图操作.默认的帧缓冲区和帧缓冲区对象主要通过用于配置和管理其状态的接口来区分.

There are two classes of framebuffers: a window system-provided framebuffer associated with a context when the context is made current, and application-created framebuffers. The window system-provided framebuffer is referred to as the default framebuffer. Application-created framebuffers, referred to as framebuffer objects, may be created as desired, A context may be associated with two framebuffers, one for each of reading and drawing operations. The default framebuffer and framebuffer objects are distinguished primarily by the interfaces for configuring and managing their state.

GL命令对默认帧缓冲区的影响最终由窗口系统控制,该窗口系统分配帧缓冲区资源,确定 GL可在任何给定时间访问默认帧缓冲区的哪些部分,并向GL传达这些部分的结构.因此,没有GL命令用于初始化GL上下文或配置默认帧缓冲区.
同样,GL无法解决在物理显示设备上显示帧缓冲内容的问题(包括通过伽玛校正等技术对单个帧缓冲值的转换).

The effects of GL commands on the default framebuffer are ultimately controlled by the window system, which allocates framebuffer resources, determines which portions of the default framebuffer the GL may access at any given time, and communicates to the GL how those portions are structured. Therefore, there are no GL commands to initialize a GL context or configure the default framebuffer.
Similarly, display of framebuffer contents on a physical display device (including the transformation of individual framebuffer values by such techniques as gamma correction) is not addressed by the GL.

请参见 OpenGL 4.6 API兼容性配置文件规范; 9.2.绑定和管理框架对象;第340页

帧缓冲对象(名称不为零的对象)在一些重要方面与默认帧缓冲有所不同.首先,与默认帧缓冲区不同,帧缓冲区对象为帧缓冲区中的每个逻辑缓冲区具有可修改的附加点.

Framebuffer objects (those with a non-zero name) differ from the default framebuffer in a few important ways. First and foremost, unlike the default framebuffer, framebuffer objects have modifiable attachment points for each logical buffer in the framebuffer.

这篇关于将渲染目标添加到QOpenGLWidget的默认帧缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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