本页内容

Qt3DRender::QMultiSampleAntiAliasing Class

class Qt3DRender::QMultiSampleAntiAliasing

启用多采样抗锯齿。更多...

标题: #include <qmultisampleantialiasing.h>
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 实例。

© 2026 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.