zoukankan      html  css  js  c++  java
  • QT3D场景快速绘制入门学习

           QT中实现3D绘制的方式:

    1)   使用QT OpenGL模块(QOpenGLWidget等)

    2)   使用QT 3D C++类(QEntity等)

    3)   使用QT 3D QML类(Entity等)

     

    QT3D场景提供了一种快速设置3D场景的一种方式,用户凭借着封装好的实体可以快速的在顶层实体(画布)当中增加各种各样的实体,并且通过3DMax软件构造的OBJ文件与QT3D实现信息交互可以的帮助用户摆脱OpenGL的用代码绘制图形的繁琐。

    下面以QT Demo Basic Shapes C++ Example”为例来讲解下创建3D场景的一般步骤和用到的具体C++类:

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
     
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
        
    // 1、创建3D场景视图窗口view
        Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
        view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));
        
    // 2、创建放置3D场景视图窗口的容器,场景view需要先放在一个容器中
        QWidget *container = QWidget::createWindowContainer(view);
        QSize screenSize = view->screen()->size();
        container->setMinimumSize(QSize(
    200100));
        container->setMaximumSize(screenSize);
        
    // 3、创建一个主窗口Widget,进行适当布局操作
        QWidget *widget = new QWidget;
        QHBoxLayout *hLayout = 
    new QHBoxLayout(widget);
        QVBoxLayout *vLayout = 
    new QVBoxLayout();
        vLayout->setAlignment(Qt::AlignTop);
        
    // 4、将3D场景容器加入布局当中
        hLayout->addWidget(container, 1);
        hLayout->addLayout(vLayout);
        
    // 给应用程序主窗口设置一个标题
        widget->setWindowTitle(QStringLiteral("Basic shapes"));
        
    // 5、创建根实体(Root Entity)对象,即所谓的“画布”,并将其设置到3D场景视图中去
        Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
        view->setRootEntity(rootEntity);
        
    // 6、在显示3D图形的过程当中,摄像机是必不可少的,只有摄像机摆放的合适人眼才能看到3D建模的样子
        Qt3DRender::QCamera *cameraEntity = view->camera();
        cameraEntity->lens()->setPerspectiveProjection(
    45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);
        cameraEntity->setPosition(QVector3D(
    0020.0f));
        cameraEntity->setUpVector(QVector3D(
    010));
        cameraEntity->setViewCenter(QVector3D(
    000));
        
    // 7、在“画布”中加入子实体“光照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(cameraEntity->position());
        lightEntity->addComponent(lightTransform);
        
    // 8、摄像机控制配置(这里配置以第一人称视角控制场景摄影机)
        Qt3DExtras::QFirstPersonCameraController *camController =
            
    new Qt3DExtras::QFirstPersonCameraController(rootEntity);
        camController->setCamera(cameraEntity);
        
    // 9、现在就可以绘制各种3D场景的实体图形了
        // 在这里有如下的几种方法可以选择,
        // 首先就是用OpenGL画出实体(适合OpenGL的老手,不适于新手),
        // 然后就是用Qt自带的基本实体(容易构造,简单易学),
        // 最后就是用例如3DMax导出的OBJ文件实现与QT之间的信息交互(容易上手,成为越来越多人的首选)。
        // 本例采用的就是Qt基本实体去创建(以球体为例)
        // 在Qt中任何实体加入到三维模型中最为简单的配置方法分为以下几步:
        // 首先再根实体中创建实体[设置实体名称可选],
        Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity);
        m_ sphereEntity ->setObjectName(
    "Sphere");
        
    // 然后配置渲染(轮廓形状Mesh、材质Materials、变换Transform)方面与实体相关的元素,
        // Mesh(实体轮廓形状,由一个个网格组成,网格越多显示效果越好)
        Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh();
        sphereMesh->setRings(
    20);
        sphereMesh->setSlices(
    20);
        sphereMesh->setRadius(
    2);
        
    //
     仅仅有Mesh,一个实体只会显示一片黑,Material提供了实体表面的呈现
        // Material(表面贴图等材质显示,有多种不同类型的材质可选)
        Qt3DExtras::QPhongMaterial *sphereMaterial = new Qt3DExtras::QPhongMaterial();
        sphereMaterial->setDiffuse(QColor(QRgb(0xff0000)));
        
    // Transform(缩放大小、旋转以及在三维模型中的位置等)
        // 实现实体旋转功能说明:
        // void setRotation(const QQuaternion &rotation)        绕点旋转
        // void setRotationX(float rotationX)               绕X轴旋转
        // void setRotationY(float rotationY)               绕Y轴旋转
        // void setRotationZ(float rotationZ)               绕Z轴旋转
        Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform();
        sphereTransform->setScale(
    1.3f);
        sphereTransform->setTranslation(QVector3D(
    0.0f, .0.f, 0.0f));
        
    // 可选:给实体加入鼠标拾取功能
        Qt3DRender::QObjectPicker *picker = new Qt3DRender::QObjectPicker(sphereEntity);
        picker->setHoverEnabled(
    true);
        picker->setEnabled(
    true);
        connect(picker, &Qt3DRender::QObjectPicker::clicked, 
    this, &SceneModifier::mouseClicked);
        connect(picker, &Qt3DRender::QObjectPicker::pressed, 
    this, &SceneModifier::mousePressed);
        connect(picker, &Qt3DRender::QObjectPicker::released, 
    this, &SceneModifier::mouseReleased);
        connect(picker, &Qt3DRender::QObjectPicker::entered, 
    this, &SceneModifier::mouseEntered);
        connect(picker, &Qt3DRender::QObjectPicker::exited, 
    this, &SceneModifier::mouseExited);
        
    // 最后将各渲染组件加入到实体list中去。
        sphereEntity->addComponent(sphereMesh);
        sphereEntity->addComponent(sphereMaterial);
        sphereEntity->addComponent(sphereTransform);
        sphereEntity->addComponent(picker);
        
    // 10、设置实体可显示
        sphereEntity ->setEnabled(true);
        
    // 显示主窗口
        widget->show();
        widget->resize(
    1200800);

        
    return app.exec();
    }

    附截图一张

  • 相关阅读:
    HDU 6071
    HDU 6073
    HDU 2124 Repair the Wall(贪心)
    HDU 2037 今年暑假不AC(贪心)
    HDU 1257 最少拦截系统(贪心)
    HDU 1789 Doing Homework again(贪心)
    HDU 1009 FatMouse' Trade(贪心)
    HDU 2216 Game III(BFS)
    HDU 1509 Windows Message Queue(队列)
    HDU 1081 To The Max(动态规划)
  • 原文地址:https://www.cnblogs.com/MakeView660/p/10319228.html
Copyright © 2011-2022 走看看