我最近刚开始在工作中使用 QT3D 进行副项目。我很惊讶你得到看起来相当不错的东西的速度有多快(至少对我来说是这样)。
但我仍然有渲染速度和 CPU 使用率的问题。我编写了一个小型测试程序来展示这一点(见下文)。当我们使用 Ubuntu 时,虽然这可能是 NVIDIA 驱动程序的问题,但我也在家里的 Windows PC 上对其进行了测试。
并得到了相同的结果:
- CPU 使用率非常高 - 一个核心始终 100%。
- 如果应用程序正在运行,GPU 的使用情况并没有真正的区别。
所以我最好的猜测是它使用软件渲染而不是硬件?有任何想法吗?
该示例仅在上方带有光源的随机位置上绘制了 2000 个具有随机大小的 Cuboid。
#include <QGuiApplication>
#include <QRandomGenerator>
#include <QHBoxLayout>
#include <Qt3DRender/QCamera>
#include <Qt3DCore/QEntity>
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtGui/QScreen>
#include <Qt3DCore/QTransform>
#include <Qt3DCore/QAspectEngine>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DRender/QPointLight>
#include <Qt3DExtras/QCuboidMesh>
#include <Qt3DExtras/QDiffuseSpecularMaterial>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QOrbitCameraController>
int main(int argc, char **argv)
{
//basic window/widget stuff
QApplication app(argc, argv);
Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));
QWidget *container = QWidget::createWindowContainer(view);
QSize screenSize = view->screen()->size();
container->setMinimumSize(QSize(200, 100));
container->setMaximumSize(screenSize);
QWidget *widget = new QWidget;
QHBoxLayout *hLayout = new QHBoxLayout(widget);
hLayout->addWidget(container, 1);
//root entity
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
view->setRootEntity(rootEntity);
//setup camera
view->camera()->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 10.0f, 5000.0f);
view->camera()->setPosition(QVector3D(0, 0, 3000));
view->camera()->setUpVector(QVector3D(0, 1, 0));
view->camera()->setViewCenter(QVector3D(0, 0, 0));
//orbit camera controller
Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
camController->setCamera(view->camera());
camController->setLookSpeed(500);
//add light
Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
light->setColor("white");
light->setIntensity(1);
lightEntity->addComponent(light);
Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
lightTransform->setTranslation(QVector3D(0, 5000, 0));
lightEntity->addComponent(lightTransform);
//add objects
QRandomGenerator rng(1234);
for(int i = 0; i <= 2000; i++)
{
Qt3DCore::QEntity* shapeEntity = new Qt3DCore::QEntity(rootEntity);
Qt3DExtras::QCuboidMesh* mesh = new Qt3DExtras::QCuboidMesh();
mesh->setXExtent(int(rng.generate() % 30)+20);
mesh->setYExtent(int(rng.generate() % 30)+20);
mesh->setZExtent(int(rng.generate() % 30)+20);
shapeEntity->addComponent(mesh);
Qt3DExtras::QDiffuseSpecularMaterial *material = new Qt3DExtras::QDiffuseSpecularMaterial();
material->setAmbient(QColor(Qt::red).darker(150));
material->setDiffuse(QColor(Qt::red));
shapeEntity->addComponent(material);
Qt3DCore::QTransform* pTrans = new Qt3DCore::QTransform();
pTrans->setTranslation(QVector3D(int(rng.generate() % 2000)-1000, int(rng.generate() % 2000)-1000, int(rng.generate() % 2000)-1000));
shapeEntity->addComponent(pTrans);
}
//show
widget->show();
widget->resize(1200, 800);
return app.exec();
}