版权声明:本文为博主原创文章,未经博主允许不得转载。如需转载,请私信博主。 https://blog.csdn.net/TurboIan/article/details/61025376
由于毕业设计设计机器人仿真,但一直想学习Qt。于是,痛下决心,从MFC平台转移到Qt上来。现在开始学习如何使用Qt 3D做一个机器人仿真环境出来。分析了一下教程的例程,不是很难,但为了以后查阅方便,所以现在这里。
下面是例程的运行结果:
我选择这个例子是因为许多例程中直接是显示模型出来的,但没有提到如何结合按钮来实现一些交互。这个例子就很好,便于学习。
#include "scenemodifier.h"
#include <QGuiApplication>
#include <Qt3DRender/qcamera.h>
#include <Qt3DCore/qentity.h>
#include <Qt3DRender/qcameralens.h>
//界面需要的头文件
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QCommandLinkButton>
#include <QtGui/QScreen>
//Qt3D头文件,具体作用后续再讲
#include <Qt3DInput/QInputAspect>
#include <Qt3DExtras/qtorusmesh.h>
#include <Qt3DRender/qmesh.h>
#include <Qt3DRender/qtechnique.h>
#include <Qt3DRender/qmaterial.h>
#include <Qt3DRender/qeffect.h>
#include <Qt3DRender/qtexture.h>
#include <Qt3DRender/qrenderpass.h>
#include <Qt3DRender/qsceneloader.h>
#include <Qt3DRender/qpointlight.h>
#include <Qt3DCore/qtransform.h>
#include <Qt3DCore/qaspectengine.h>
#include <Qt3DRender/qrenderaspect.h>
#include <Qt3DExtras/qforwardrenderer.h>
#include <Qt3DExtras/qt3dwindow.h>
#include <Qt3DExtras/qfirstpersoncameracontroller.h>
int main(int argc, char **argv)
{
//这句雷打不打要写...
QApplication app(argc, argv);
//首先创建一个场景,用于显示你要绘制或者导入模型,关键语句
Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
//设置场景背景颜色
view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));
//如果将3d放置在界面中,则需要创建一个widget来“装”它,container这名字起得生动
QWidget *container = QWidget::createWindowContainer(view);
//设置场景大小
QSize screenSize = view->screen()->size();
//当你缩小对话框时,你希望它最小是多少
container->setMinimumSize(QSize(100, 200));
//当你缩小对话框时,你希望它最大是多少
container->setMaximumSize(screenSize);
//关键语句,创建对话框
QWidget *widget = new QWidget;
//水平布局
QHBoxLayout *hLayout = new QHBoxLayout(widget);
//垂直布局
QVBoxLayout *vLayout = new QVBoxLayout();
vLayout->setAlignment(Qt::AlignTop);
//将3d场景veiw放到widget中,关键语
hLayout->addWidget(container, 1);
hLayout->addLayout(vLayout);
widget->setWindowTitle(QStringLiteral("Basic shapes"));
//不懂
Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect;
view->registerAspect(input);
//创建根实体,我理解就是创建一个基础实体,然后用它来创建其他的实体
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
// 创建相机,设置视角
Qt3DRender::QCamera *cameraEntity = view->camera();
//观测视角
cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
//相机放哪里?
cameraEntity->setPosition(QVector3D(0, 0, 20.0f));
cameraEntity->setUpVector(QVector3D(0, 1, 0));
//视角中心在哪里?
cameraEntity->setViewCenter(QVector3D(0, 0, 0));
//创建各种模型实体
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(cameraEntity->position());
lightEntity->addComponent(lightTransform);
// 设置相机控制,什么第一视场什么的,不是很懂,待补充。
Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
camController->setCamera(cameraEntity);
//场景修改?
SceneModifier *modifier = new SceneModifier(rootEntity);
// 关键语句,将这么多实体加载进view当中...
view->setRootEntity(rootEntity);
//手工码控件
QCommandLinkButton *info = new QCommandLinkButton();
info->setText(QStringLiteral("Qt3D ready-made meshes"));
info->setDescription(QString::fromLatin1("Qt3D provides several ready-made meshes, like torus, cylinder, cone, "
"cube, plane and sphere."));
info->setIconSize(QSize(0,0));
QCheckBox *torusCB = new QCheckBox(widget);
torusCB->setChecked(true);
torusCB->setText(QStringLiteral("Torus"));
QCheckBox *coneCB = new QCheckBox(widget);
coneCB->setChecked(true);
coneCB->setText(QStringLiteral("Cone"));
QCheckBox *cylinderCB = new QCheckBox(widget);
cylinderCB->setChecked(true);
cylinderCB->setText(QStringLiteral("Cylinder"));
QCheckBox *cuboidCB = new QCheckBox(widget);
cuboidCB->setChecked(true);
cuboidCB->setText(QStringLiteral("Cuboid"));
QCheckBox *planeCB = new QCheckBox(widget);
planeCB->setChecked(true);
planeCB->setText(QStringLiteral("Plane"));
QCheckBox *sphereCB = new QCheckBox(widget);
sphereCB->setChecked(true);
sphereCB->setText(QStringLiteral("Sphere"));
//
vLayout->addWidget(info);
vLayout->addWidget(torusCB);
vLayout->addWidget(coneCB);
vLayout->addWidget(cylinderCB);
vLayout->addWidget(cuboidCB);
vLayout->addWidget(planeCB);
vLayout->addWidget(sphereCB);
//信号与槽好基友,根据checkBox的状态来选择是否显示模型
QObject::connect(torusCB, &QCheckBox::stateChanged,
modifier, &SceneModifier::enableTorus);
QObject::connect(coneCB, &QCheckBox::stateChanged,
modifier, &SceneModifier::enableCone);
QObject::connect(cylinderCB, &QCheckBox::stateChanged,
modifier, &SceneModifier::enableCylinder);
QObject::connect(cuboidCB, &QCheckBox::stateChanged,
modifier, &SceneModifier::enableCuboid);
QObject::connect(planeCB, &QCheckBox::stateChanged,
modifier, &SceneModifier::enablePlane);
QObject::connect(sphereCB, &QCheckBox::stateChanged,
modifier, &SceneModifier::enableSphere);
//先设置默认情况
torusCB->setChecked(true);
coneCB->setChecked(true);
cylinderCB->setChecked(true);
cuboidCB->setChecked(true);
planeCB->setChecked(true);
sphereCB->setChecked(true);
//显示窗口
widget->show();
//设置大小
widget->resize(1200, 800);
//雷打不动第二句
return app.exec();
}
分析得简单粗暴,但会继续研究这个代码,并且不断更新,直到自己对这里的每一句都非常熟悉!