PySide6.QtOpenGL.QOpenGLFramebufferObject¶
- class QOpenGLFramebufferObject¶
QOpenGLFramebufferObject
类封装了一个 OpenGL 帧缓冲对象。更多…概要¶
方法¶
def
__init__()
def
attachment()
def
bind()
def
format()
def
handle()
def
height()
def
isBound()
def
isValid()
def
release()
def
setAttachment()
def
size()
def
sizes()
def
takeTexture()
def
texture()
def
textures()
def
toImage()
def
width()
静态函数¶
注意
本文档可能包含从C++自动翻译到Python的代码片段。我们始终欢迎对代码片段翻译的贡献。如果您发现翻译问题,您也可以通过在我们的https:/bugreports.qt.io/projects/PYSIDE上创建工单来告知我们。
详细描述¶
QOpenGLFramebufferObject
类封装了一个由GL_EXT_framebuffer_object
扩展定义的 OpenGL 帧缓冲对象。它提供了一个渲染表面,可以通过QOpenGLPaintDevice
使用 QPainter 进行绘制,或者使用原生 OpenGL 调用来渲染。这个表面可以绑定并在你自己的 OpenGL 绘图代码中作为常规纹理使用。默认情况下,QOpenGLFramebufferObject
类生成一个 2D OpenGL 纹理(使用GL_TEXTURE_2D
目标),作为内部渲染目标。在创建
QOpenGLFramebufferObject
时,拥有当前的OpenGL上下文非常重要,否则初始化将失败。如果你想QPainter正确渲染,请使用
CombinedDepthStencil
附件创建QOpenGLFrameBufferObject实例。请注意,你需要创建一个每像素有多个采样的QOpenGLFramebufferObject
,以便在使用QPainter绘制时对图元进行抗锯齿处理。要创建一个多重采样的帧缓冲对象,你应该使用带有QOpenGLFramebufferObjectFormat
参数的构造函数之一,并将samples()
属性设置为非零值。对于多采样帧缓冲对象,会创建一个颜色渲染缓冲区,否则会创建一个具有指定纹理目标的纹理。颜色渲染缓冲区或纹理将具有指定的内部格式,并将绑定到帧缓冲对象中的
GL_COLOR_ATTACHMENT0
附件。如果OpenGL实现支持,还支持多个渲染目标。这里将存在多个纹理(或者,在多采样的情况下,渲染缓冲区),并且每个都将附加到
GL_COLOR_ATTACHMENT0
,1
,2
,…如果你想使用启用了多重采样的帧缓冲对象作为纹理,首先需要使用 QOpenGLContext::blitFramebuffer() 将其复制到常规的帧缓冲对象中。
可以在单独的线程中使用QPainter和
QOpenGLPaintDevice
绘制到QOpenGLFramebufferObject
中。- class Attachment¶
此枚举类型用于配置在创建帧缓冲对象时附加的深度和模板缓冲区。
常量
描述
QOpenGLFramebufferObject.NoAttachment
没有向帧缓冲对象添加附件。请注意,当渲染到没有任何深度或模板缓冲区的帧缓冲对象时,OpenGL深度和模板测试将不起作用。这是默认值。
QOpenGLFramebufferObject.CombinedDepthStencil
如果存在
GL_EXT_packed_depth_stencil
扩展,则会附加一个组合的深度和模板缓冲区。如果该扩展不存在,则仅附加深度缓冲区。QOpenGLFramebufferObject.Depth
深度缓冲区附加到帧缓冲区对象。
另请参阅
- class FramebufferRestorePolicy¶
此枚举类型用于配置在调用blitFramebuffer()时与恢复帧缓冲区绑定相关的行为。
常量
描述
QOpenGLFramebufferObject.DontRestoreFramebufferBinding
不恢复之前的帧缓冲区绑定。调用者负责根据需要跟踪和设置帧缓冲区绑定。
QOpenGLFramebufferObject.RestoreFramebufferBindingToDefault
在blit操作之后,绑定默认的帧缓冲区。
QOpenGLFramebufferObject.RestoreFrameBufferBinding
恢复之前绑定的帧缓冲区。这可能会很昂贵,因为需要查询当前绑定的帧缓冲区。
另请参阅
- __init__(size, format)¶
- Parameters:
size –
QSize
format –
QOpenGLFramebufferObjectFormat
- __init__(size[, target=GL_TEXTURE_2D])
- Parameters:
size –
QSize
target – int
- __init__(width, height, format)
- Parameters:
width – int
height – int
format –
QOpenGLFramebufferObjectFormat
- __init__(width, height[, target=GL_TEXTURE_2D])
- Parameters:
width – int
height – int
target – int
- __init__(size, attachment[, target=GL_TEXTURE_2D[, internalFormat=0]])
- Parameters:
size –
QSize
attachment –
Attachment
target – int
internalFormat – int
- __init__(width, height, attachment[, target=GL_TEXTURE_2D[, internalFormat=0]])
- Parameters:
width – int
height – int
attachment –
Attachment
target – int
internalFormat – int
- addColorAttachment(width, height[, internalFormat=0])
- Parameters:
width – int
height – int
internalFormat – int
- bind()¶
- Return type:
布尔
将渲染从默认的窗口系统提供的帧缓冲区切换到此帧缓冲区对象。成功时返回
true
,否则返回false。- static bindDefault()¶
- Return type:
布尔
将渲染切换回默认的窗口系统提供的帧缓冲区。成功时返回
true
,否则返回false。- static blitFramebuffer(target, source[, buffers=GL_COLOR_BUFFER_BIT[, filter=GL_NEAREST]])¶
- Parameters:
target –
QOpenGLFramebufferObject
source –
QOpenGLFramebufferObject
buffers – 整数
filter – 整数
- static blitFramebuffer(target, targetRect, source, sourceRect[, buffers=GL_COLOR_BUFFER_BIT[, filter=GL_NEAREST]])
- Parameters:
target –
QOpenGLFramebufferObject
targetRect –
QRect
source –
QOpenGLFramebufferObject
sourceRect –
QRect
buffers – int
filter – int
- static blitFramebuffer(target, targetRect, source, sourceRect, buffers, filter, readColorAttachmentIndex, drawColorAttachmentIndex)
- Parameters:
target –
QOpenGLFramebufferObject
targetRect –
QRect
source –
QOpenGLFramebufferObject
sourceRect –
QRect
buffers – 整数
filter – 整数
readColorAttachmentIndex – int
drawColorAttachmentIndex – int
- static blitFramebuffer(target, targetRect, source, sourceRect, buffers, filter, readColorAttachmentIndex, drawColorAttachmentIndex, restorePolicy)
- Parameters:
target –
QOpenGLFramebufferObject
targetRect –
QRect
source –
QOpenGLFramebufferObject
sourceRect –
QRect
buffers – 整数
filter – 整数
readColorAttachmentIndex – int
drawColorAttachmentIndex – int
restorePolicy –
FramebufferRestorePolicy
- format()¶
- Return type:
- handle()¶
- Return type:
整数
返回此帧缓冲对象的OpenGL帧缓冲对象句柄(由
glGenFrameBuffersEXT()
函数返回)。此句柄可用于将新图像或缓冲区附加到帧缓冲。用户负责清理和销毁这些对象。- static hasOpenGLFramebufferBlit()¶
- Return type:
布尔
如果此系统上存在OpenGL
GL_EXT_framebuffer_blit
扩展,则返回true
;否则返回false
。另请参阅
- static hasOpenGLFramebufferObjects()¶
- Return type:
布尔
如果系统中存在OpenGL
GL_EXT_framebuffer_object
扩展,则返回true
;否则返回false
。- height()¶
- Return type:
整数
返回帧缓冲对象附件的高度。
- isBound()¶
- Return type:
布尔
- isValid()¶
- Return type:
布尔
如果帧缓冲对象有效,则返回
true
。如果初始化过程失败,用户将一个无效的缓冲区附加到帧缓冲区对象,或者如果纹理目标是
GL_TEXTURE_2D
,指定了非二次幂的宽度/高度作为纹理大小,帧缓冲区可能会变得无效。如果OpenGL版本是2.0或更高,或者存在GL_ARB_texture_non_power_of_two扩展,则非二次幂的限制不适用。如果创建帧缓冲区的QOpenGLContext被销毁,并且没有其他共享上下文可以接管帧缓冲区的所有权,帧缓冲区也可能变为无效。
- release()¶
- Return type:
布尔
将渲染切换回默认的窗口系统提供的帧缓冲区。成功时返回
true
,否则返回false。另请参阅
- setAttachment(attachment)¶
- Parameters:
附件 –
Attachment
- takeTexture()¶
- Return type:
整数
返回附加到此帧缓冲对象上的纹理的纹理ID。纹理的所有权转移给调用者。
如果帧缓冲对象当前已绑定,将隐式执行
release()
。在下次调用bind()
时,将创建一个新的纹理。如果使用了多重采样帧缓冲对象,则没有纹理,此函数的返回值将无效。同样,不完整的帧缓冲对象也将返回0。
- takeTexture(colorAttachmentIndex)
- Parameters:
colorAttachmentIndex – int
- Return type:
整数
- texture()¶
- Return type:
整数
返回作为此帧缓冲对象中默认渲染目标附加的纹理的纹理ID。此纹理ID可以在您自己的OpenGL代码中绑定为普通纹理。
如果使用了多重采样帧缓冲对象,则从此函数返回的值将无效。
当附加了多个纹理时,返回值是第一个纹理的ID。
另请参阅
- textures()¶
- Return type:
无符号整数列表
返回此帧缓冲对象的内容作为QImage。
如果
flipped
为 true,图像将从 OpenGL 坐标翻转到位图坐标。如果与QOpenGLPaintDevice
一起使用,flipped
应该是paintFlipped()
值的相反值。返回的图像格式为预乘的ARGB32或RGB32。后者仅在internalTextureFormat()设置为
GL_RGB
时使用。自Qt 5.2起,当不支持读取到(A)RGB32时,该函数将回退到预乘的RGBA8888或RGBx8888,这包括OpenGL ES。自Qt 5.4起,如果内部格式为RGB10_A2,则返回A2BGR30图像,自Qt 5.12起,如果内部格式为RGBA16,则返回RGBA64图像。如果在帧缓冲区中的渲染没有考虑到预乘alpha,则创建一个具有非预乘格式的包装QImage。在执行诸如QImage::save()之类的操作之前,这是必要的,因为否则图像数据将被取消预乘,即使它最初并没有被预乘。要在不复制像素数据的情况下创建这样的包装器,请执行以下操作:
QImage fboImage(fbo.toImage()); QImage image(fboImage.constBits(), fboImage.width(), fboImage.height(), QImage::Format_ARGB32);
对于多重采样的帧缓冲对象,样本使用
GL_EXT_framebuffer_blit
扩展进行解析。如果该扩展不可用,返回的图像内容将是未定义的。对于单采样帧缓冲区,内容通过
glReadPixels
获取。这可能是一项昂贵且低效的操作。因此,建议尽可能少使用此函数。另请参阅
- toImage(flipped, colorAttachmentIndex)
- Parameters:
flipped – 布尔值
colorAttachmentIndex – int
- Return type:
- width()¶
- Return type:
整数
返回帧缓冲对象附件的宽度。