Qt 3D c++ (五):代码汇总

2021-01-04 22:28

阅读:730

标签:显示   window   mesh   orm   argv   scree   header   rom   size   

.pro

SOURCES +=     main.cpp     mymodel.cpp
QT += 3dcore 3drender 3dinput 3dextras 3dlogic 3danimation
QT += widgets

HEADERS +=     mymodel.h
mymodel.h

#ifndef MYMODEL_H
#define MYMODEL_H

#include 
#include
#include
#include
#includeclass MyModel : public QObject
{
    Q_OBJECT
public:
    explicit MyModel(Qt3DCore::QEntity *rootEntity);
    ~MyModel();
private:
    Qt3DCore::QEntity *m_rootEntity;
    Qt3DExtras::QTorusMesh *m_torus;
    Qt3DCore::QEntity *m_torusEntity;
public slots:
    void enableTorus(bool enabled);
};

#endif // MYMODEL_H
main.cpp


#include 
#include 

#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include "mymodel.h"

int main(int argc, char *argv[])
{
    //QCoreApplication a(argc, argv);
    QApplication a(argc, argv);
    QWidget *widget = new QWidget;

    Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
    view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x81fdff)));
    //创建画布

    QWidget *container = QWidget::createWindowContainer(view);
    QSize screenSize = view->screen()->size();
    container->setMinimumSize(QSize(200, 100));
    container->setMaximumSize(screenSize);
    //创建画布的容器,并将画布作为参数放进容器 QWidget *container = QWidget::createWindowContainer(view);

    QHBoxLayout *hLayout = new QHBoxLayout(widget);
    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->setAlignment(Qt::AlignTop);
    hLayout->addWidget(container, 1);
    hLayout->addLayout(vLayout);
    //将画布的容器container放到窗口对象widget中,并添加布局

    //至此 画布添加完毕

    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
    //创建Entity对象,这个Entity将会包含相机参数,光线参数,以及3D模型,最后显示的就是这个rootEntity,Mymodel类的对象也是rootEntity的参数

    Qt3DRender::QCamera *cameraEntity = view->camera();
    //创建相机,必须要有相机才能看到3D对象
    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));
    //设置相机参数

    Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect;
    view->registerAspect(input);
    //QInputAspect负责创建物理设备并处理相关的作业,处理前端和后端节点之间的映射,负责根据当前输入设置建立要在特定时间运行的作业的对象。

    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);
    //相机第一人称视角控制

    MyModel *mymodel = new MyModel(rootEntity);
    //创建3D对象
    view->setRootEntity(rootEntity);
    //将3D物体添加到画布上显示

    widget->show();
    widget->resize(1200, 800);
    return a.exec();
}
mymodel.cpp

#include "mymodel.h"


MyModel::MyModel(Qt3DCore::QEntity *rootEntity): m_rootEntity(rootEntity)
{
    m_torus = new Qt3DExtras::QTorusMesh();
    m_torus->setRadius(1.0f);
    m_torus->setMinorRadius(0.4f);
    m_torus->setRings(100);
    m_torus->setSlices(20);
    //设置物体的表面网格

    Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform();
    torusTransform->setScale(2.0f);
    torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0.0f, 1.0f, 0.0f), 25.0f));
    torusTransform->setTranslation(QVector3D(5.0f, 4.0f, 0.0f));
    //创建缩放,平移和旋转组件并将其添加到QTransform组件中

    Qt3DExtras::QPhongMaterial *torusMaterial = new Qt3DExtras::QPhongMaterial();
    torusMaterial->setDiffuse(QColor(QRgb(0xbeb32b)));
    //更改网格的漫反射颜色,我们创建一个QPhongMaterial并设置其漫反射颜色。

    m_torusEntity = new Qt3DCore::QEntity(m_rootEntity);
    m_torusEntity->addComponent(m_torus);
    m_torusEntity->addComponent(torusMaterial);
    m_torusEntity->addComponent(torusTransform);
    //将圆环添加到实体树中,然后通过QEntity使用父实体创建一个,然后添加先前创建的网格,材质并将组件转换到其中来完成。
}

MyModel::~MyModel()
{

}

void MyModel::enableTorus(bool enabled)
{
     m_torusEntity->setEnabled(enabled);
}

 

Qt 3D c++ (五):代码汇总

标签:显示   window   mesh   orm   argv   scree   header   rom   size   

原文地址:https://www.cnblogs.com/leocc325/p/12983704.html


评论


亲,登录后才可以留言!