禁用 Qt 3d 中的所有光源

Ale*_*ph0 3 c++ 3d qt qt3d qt5.12

在我的公司,从旧的 3D 引擎转向 Qt3d。这项工作的目标之一是将旧 3D 引擎的渲染视图与 Qt3d 渲染进行比较。

为此,我编写了一个小型示例应用程序,我可以在其中比较新旧渲染。仍然存在很多差异。我的第一个想法是切换两个引擎中的所有光源并比较两个渲染的轮廓。

现在,有一些事情我真的不明白,这与 Qt3d 照明模型有关。

在我的小示例应用程序中,我定义了一个简单的球体网格、一个相机和一个复选框,可以禁用点光源。球体由phong 反射模型照亮。

不,如果我关掉灯,我会期待观察器中出现一个简单的黑色球体,因为实际上没有光。相反,仍然有一些照明(来自不同的来源)。我认为,还有一些其他光源,默认情况下处于激活状态。

如何禁用 Qt3d 中的所有光源?作为一个附带问题,我还想知道为什么meshMaterial->setAmbient(QColor(255, 0, 0));对材料没有明显的影响。您在这里输入什么并不重要。

#include <QApplication>
#include <QWidget>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QFrame>
#include <Qt3DCore/QTransform.h>
#include <Qt3DRender/QCamera.h>
#include <Qt3DRender/QRenderSettings.h>
#include <Qt3DRender/QPointLight.h>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/qforwardrenderer.h>
#include <Qt3DExtras/Qt3DWindow.h>
#include <Qt3DExtras/QFirstPersonCameraController.h>

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);

    auto view = new Qt3DExtras::Qt3DWindow();
    view->defaultFrameGraph()->setClearColor(QColor(255,255,255));

    auto rootEntity = new Qt3DCore::QEntity();
    view->setRootEntity(rootEntity);

    auto cameraEntity = view->camera();
    cameraEntity->lens()->setPerspectiveProjection(45.0f, 1., 0.1f, 10000.0f);
    cameraEntity->setPosition(QVector3D(5, 5, 5));
    cameraEntity->setUpVector(QVector3D(0, 1, 0));
    cameraEntity->setViewCenter(QVector3D(0, 0, 0));

    auto lightEntity = new Qt3DCore::QEntity(rootEntity);

    auto light = new Qt3DRender::QPointLight(lightEntity);
    light->setColor("white");
    light->setIntensity(1);
    lightEntity->addComponent(light);

    auto lightTransform = new Qt3DCore::QTransform(lightEntity);
    lightTransform->setTranslation(cameraEntity->position());
    lightEntity->addComponent(lightTransform);

    lightEntity->setEnabled(false);

    // For camera controls
    auto camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
    camController->setCamera(cameraEntity);

    auto mesh = new Qt3DExtras::QSphereMesh();
    mesh->setRadius(1.);

    auto meshMaterial = new Qt3DExtras::QPhongMaterial();
    meshMaterial->setDiffuse(QColor(0, 255, 0));
    meshMaterial->setAmbient(QColor(255, 0, 0));
    meshMaterial->setSpecular(QColor(0,0,255));
    meshMaterial->setShininess(23);

    auto meshEntity = new Qt3DCore::QEntity(rootEntity);
    meshEntity->addComponent(mesh);
    meshEntity->addComponent(meshMaterial);
    meshEntity->setEnabled(true);

    auto disableLight = new QCheckBox();
    auto container = QWidget::createWindowContainer(view);
    QFrame frame;
    frame.setLayout(new QVBoxLayout);
    frame.layout()->addWidget(container);
    frame.layout()->addWidget(disableLight);
    QObject::connect(disableLight, &QCheckBox::stateChanged, [lightEntity](auto state) {
        lightEntity->setEnabled(state == Qt::CheckState::Checked);
    });
    frame.setFixedSize(500, 500);
    frame.show();
    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

Edd*_*man 7

如果您的 Qt3D 场景中没有创建灯光实体​​,Qt3D 将为您添加一个。这是为了防止用户在没有光的情况下看不到任何东西。一旦您自己添加了一盏灯,默认的灯就会被省略

您可以通过添加强度设置为零的灯光来解决此默认行为:

DirectionalLight {
    worldDirection: Qt.vector3d(-1, 1, -1)
    intensity: 0.0
}
Run Code Online (Sandbox Code Playgroud)

这将为您带来以下效果:使用 PhongMaterial 使用长方体和球体网格进行测试:

使用 PhongMaterial 测试长方体和球体网格

因此,修改光的强度属性可能会得到您想要的结果。