Qt3DRender::QMultiSampleAntiAliasing Class

class Qt3DRender::QMultiSampleAntiAliasing

启用多采样抗锯齿。更多

头文件: #include <QMultiSampleAntiAliasing>
CMake: find_package(Qt6 REQUIRED COMPONENTS 3drender)
target_link_libraries(mytarget PRIVATE Qt6::3drender)
qmake: QT += 3drender
在 QML 中: MultiSampleAntiAliasing
继承: Qt3DRender::QRenderState
状态:已废弃

公共函数

QMultiSampleAntiAliasing(Qt3DCore::QNode *parent = nullptr)

详细描述

Qt3DRender::QMultiSampleAntiAliasing 类可实现多采样抗锯齿。

它可以通过调用QRenderPass::addRenderState() 添加到QRenderPass 中:

QRenderPass *renderPass = new QRenderPass();

QMultiSampleAntiAliasing *msaa = new QMultiSampleAntiAliasing();
renderPass->addRenderState(msaa);

或调用QRenderStateSet::addRenderState() 将其添加到QRenderStateSet 中:

QRenderStateSet *renderStateSet = new QRenderStateSet();

QMultiSampleAntiAliasing *msaa = new QMultiSampleAntiAliasing();
renderStateSet->addRenderState(msaa);

要使多采样生效,必须在启用多采样的情况下分配渲染目标:

QTexture2DMultisample *colorTex = new QTexture2DMultisample;
colorTex->setFormat(QAbstractTexture::RGBA8_UNorm);
colorTex->setWidth(1024);
colorTex->setHeight(1024);

QRenderTargetOutput *color = new QRenderTargetOutput;
color->setAttachmentPoint(QRenderTargetOutput::Color0);
color->setTexture(colorTex);

QTexture2DMultisample *depthStencilTex = new QTexture2DMultisample;
depthStencilTex->setFormat(QAbstractTexture::RGBA8_UNorm);
depthStencilTex->setWidth(1024);
depthStencilTex->setHeight(1024);

QRenderTargetOutput *depthStencil = new QRenderTargetOutput;
depthStencil->setAttachmentPoint(QRenderTargetOutput::DepthStencil);
depthStencil->setTexture(depthStencilTex);

Qt3DRender::QRenderTarget *renderTarget = new Qt3DRender::QRenderTarget;
renderTarget->addOutput(color);
renderTarget->addOutput(depthStencil);

此外,着色器代码必须使用多重采样采样器类型和 texelFetch() 而不是 texture()。

例如

#version 150

uniform sampler2D colorTexture;
in vec2 texCoord;
out vec4 fragColor;

void main()
{
    fragColor = texture(colorTexture, texCoord);
}

的代码,可以重写为

#version 150

uniform sampler2DMS colorTexture;
in vec2 texCoord;
out vec4 fragColor;

void main()
{
    ivec2 tc = ivec2(floor(textureSize(colorTexture) * texCoord));
    vec4 c = texelFetch(colorTexture, tc, 0) +
                texelFetch(colorTexture, tc, 1) +
                texelFetch(colorTexture, tc, 2) +
                texelFetch(colorTexture, tc, 3);
    fragColor = c / 4.0;
}

注意: 当使用 OpenGL 作为图形 API 时,如果 QMultiSampleAntiAliasing 已添加到渲染状态,glEnable(GL_MULTISAMPLE) 将被调用。

成员函数文档

[explicit] QMultiSampleAntiAliasing::QMultiSampleAntiAliasing(Qt3DCore::QNode *parent = nullptr)

构造函数使用指定的parent 创建一个新的QMultiSampleAntiAliasing::QMultiSampleAntiAliasing 实例。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.