Qt文档阅读笔记-Qt Quick 3D - Simple Example解析

程序运行截图如下:

还是动的。

官方出这篇博文的目的是如何教我们使用Qt Quick 3D去渲染简单的场景。

 

首先是设置其场景。

在main.qml文件中设置整个场景,导入QtQuick3D组建,用于使用各种3D数据类型:

  import QtQuick3D 1.14

  Window {
      id: window
      width: 1280
      height: 720
      visible: true

      View3D {
          id: view
          anchors.fill: parent

          //! [environment]
          environment: SceneEnvironment {
              clearColor: "skyblue"
              backgroundMode: SceneEnvironment.Color
          }
          //! [environment]

          //! [camera]
          PerspectiveCamera {
              position: Qt.vector3d(0, 200, -300)
              rotation: Qt.vector3d(30, 0, 0)
          }
          //! [camera]

          //! [light]
          DirectionalLight {
              rotation: Qt.vector3d(30, 70, 0)
          }
          //! [light]

          //! [objects]
          Model {
              position: Qt.vector3d(0, -200, 0)
              source: "#Cylinder"
              scale: Qt.vector3d(2, 0.2, 1)
              materials: [ DefaultMaterial {
                      diffuseColor: "red"
                  }
              ]
          }

          Model {
              position: Qt.vector3d(0, 150, 0)
              source: "#Sphere"

              materials: [ DefaultMaterial {
                      diffuseColor: "blue"
                  }
              ]

              SequentialAnimation on y {
                  loops: Animation.Infinite
                  NumberAnimation {
                      duration: 3000
                      to: -150
                      from: 150
                      easing.type:Easing.InQuad
                  }
                  NumberAnimation {
                      duration: 3000
                      to: 150
                      from: -150
                      easing.type:Easing.OutQuad
                  }
              }
          }
          //! [objects]
      }
  }

定义了场景所处的环境。在这个例子中将background的颜色改成了‘skyblue’。

  environment: SceneEnvironment {
      clearColor: "skyblue"
      backgroundMode: SceneEnvironment.Color
  }

随后定义了场景中的相机。在此官方实例中使用了PerspectiveCamera充当相机,将相机向后移,这个后是指人倒退向后这个,并且给一个看的角度

  PerspectiveCamera {
      position: Qt.vector3d(0, 200, -300)
      rotation: Qt.vector3d(30, 0, 0)
  }

在画物体前,还需要一个光源去照,在物理里面,只有光照到物体上,反射的光进入眼睛才会有颜色,没有光就是一坨黑。

  DirectionalLight {
      rotation: Qt.vector3d(30, 70, 0)
  }

其次是画简单的物体。

画一个用红色圆柱弄的圆盘,并且画一个球,这个球有一些动态效果

  Model {

      position: Qt.vector3d(0, -200, 0)
      source: "#Cylinder"
      scale: Qt.vector3d(2, 0.2, 1)
      materials: [ DefaultMaterial {
              diffuseColor: "red"
          }
      ]
  }

  Model {
      position: Qt.vector3d(0, 150, 0)
      source: "#Sphere"

      materials: [ DefaultMaterial {
              diffuseColor: "blue"
          }
      ]

      SequentialAnimation on y {
          loops: Animation.Infinite
          NumberAnimation {
              duration: 3000
              to: -150
              from: 150
              easing.type:Easing.InQuad
          }
          NumberAnimation {
              duration: 3000
              to: 150
              from: -150
              easing.type:Easing.OutQuad
          }
      }
  }

源码如下:

simple.pro

QT += quick quick3d-private

target.path = $$[QT_INSTALL_EXAMPLES]/quick3d/simple
INSTALLS += target

SOURCES += \
    main.cpp

RESOURCES += \
    qml.qrc

OTHER_FILES += \
    doc/src/*.*

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <QtGui>
#include <QtQuick3D/private/qquick3dviewport_p.h>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QSurfaceFormat::setDefaultFormat(QQuick3DViewport::idealSurfaceFormat(4));

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

main.qml

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick3D 1.14

Window {
    id: window
    width: 1280
    height: 720
    visible: true

    View3D {
        id: view
        anchors.fill: parent

        //! [environment]
        environment: SceneEnvironment {
            clearColor: "skyblue"
            backgroundMode: SceneEnvironment.Color
        }
        //! [environment]

        //! [camera]
        PerspectiveCamera {
            position: Qt.vector3d(0, 200, -300)
            rotation: Qt.vector3d(30, 0, 0)
        }
        //! [camera]

        //! [light]
        DirectionalLight {
            rotation: Qt.vector3d(30, 70, 0)
        }
        //! [light]

        //! [objects]
        Model {
            position: Qt.vector3d(0, -200, 0)
            source: "#Cylinder"
            scale: Qt.vector3d(2, 0.2, 1)
            materials: [ DefaultMaterial {
                    diffuseColor: "red"
                }
            ]
        }

        Model {
            position: Qt.vector3d(0, 150, 0)
            source: "#Sphere"

            materials: [ DefaultMaterial {
                    diffuseColor: "blue"
                }
            ]

            SequentialAnimation on y {
                loops: Animation.Infinite
                NumberAnimation {
                    duration: 3000
                    to: -150
                    from: 150
                    easing.type:Easing.InQuad
                }
                NumberAnimation {
                    duration: 3000
                    to: 150
                    from: -150
                    easing.type:Easing.OutQuad
                }
            }
        }
        //! [objects]
    }
}

 

已标记关键词 清除标记
相关推荐
<p> <b><span style="font-size:14px;"></span><span style="font-size:14px;background-color:#FFE500;">【Java面试宝典】</span></b><br /> <span style="font-size:14px;">1、68讲视频课,500道大厂Java常见面试题+100个Java面试技巧与答题公式+10万字核心知识解析+授课老师1对1面试指导+无限次回放</span><br /> <span style="font-size:14px;">2、这门课程基于胡书敏老师8年Java面试经验,调研近百家互联网公司及面试官的问题打造而成,从筛选简历和面试官角度,给出能帮助候选人能面试成功的面试技巧。</span><br /> <span style="font-size:14px;">3、通过学习这门课程,你能系统掌握Java核心、数据库、Java框架、分布式组件、Java简历准备、面试实战技巧等面试必考知识点。</span><br /> <span style="font-size:14px;">4、知识点+项目经验案例,每一个都能做为面试的作品展现。</span><br /> <span style="font-size:14px;">5、本课程已经在线下的培训课程中经过实际检验,老师每次培训结束后,都能帮助同学们运用面试技巧,成功找到更好的工作。</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【超人气讲师】</b></span><br /> <span style="font-size:14px;">胡书敏 | 10年大厂工作经验,8年Java面试官经验,5年线下Java职业培训经验,5年架构师经验</span><br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><b>【报名须知】</b></span><br /> <span style="font-size:14px;">上课模式是什么?</span><br /> <span style="font-size:14px;">课程采取录播模式,课程永久有效,可无限次观看</span><br /> <span style="font-size:14px;">课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化</span><br /> <br /> <br /> <span style="font-size:14px;background-color:#FFE500;"><strong>如何开始学习?</strong></span><br /> <span style="font-size:14px;">PC端:报名成功后可以直接进入课程学习</span><br /> <span style="font-size:14px;">移动端:<span style="font-family:Helvetica;font-size:14px;background-color:#FFFFFF;">CSDN 学院APP(注意不是CSDN APP哦)</span></span> </p>
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页