Qt 3D Simple QML Example

2 篇文章 0 订阅

下面的代码来自QT官方示例

主要不同为添加了中文注释:

/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.2 as QQ2
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0

Entity { // 创建一个实体
    id: sceneRoot // 设置实体的id为sceneRoot

    Camera { // 创建一个相机
        id: camera // 设置相机的id为camera
        projectionType: CameraLens.PerspectiveProjection // 设置相机的投影类型为透视投影
        fieldOfView: 45 // 设置相机的视野角度为45度
        aspectRatio: 16/9 // 设置相机的宽高比为16:9
        nearPlane : 0.1 // 设置相机的近平面
        farPlane : 1000.0 // 设置相机的远平面
        position: Qt.vector3d( 0.0, 0.0, -40.0 ) // 设置相机的位置
        upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) // 设置相机的上向量
        viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) // 设置相机的视点中心
    }

    OrbitCameraController { // 创建一个轨道相机控制器
        camera: camera // 设置轨道相机控制器的相机为camera
    }

    components: [ // 设置实体的组件
        RenderSettings { // 创建一个渲染设置
            activeFrameGraph: ForwardRenderer { // 创建一个前向渲染器
                clearColor: Qt.rgba(0, 0.5, 1, 1) // 设置清除颜色
                camera: camera // 设置相机
            }
        },
        // Event Source will be set by the Qt3DQuickWindow
        InputSettings { } // 创建一个输入设置
    ]

    PhongMaterial { // 创建一个Phong材质
        id: material // 设置材质的id为material
        diffuse: Qt.rgba(1, 0.5, 1, 1)

    }

    TorusMesh { // 创建一个Torus网格
        id: torusMesh // 设置网格的id为torusMesh
        radius: 5 // 设置半径
        minorRadius: 1 // 设置小半径
        rings: 100 // 设置环数
        slices: 20 // 设置切片数
    }

    Transform { // 创建一个变换
        id: torusTransform // 设置变换的id为torusTransform
        scale3D: Qt.vector3d(1.5, 1, 0.5) // 设置缩放
        rotation: fromAxisAndAngle(Qt.vector3d(1, 0, 0), 45) // 设置旋转
    }

    Entity { // 创建一个实体
        id: torusEntity // 设置实体的id为torusEntity
        components: [ torusMesh, material, torusTransform ] // 设置实体的组件
    }

    SphereMesh { // 创建一个Sphere网格
        id: sphereMesh // 设置网格的id为sphereMesh
        radius: 3 // 设置半径
    }

    Transform { // 创建一个变换
        id: sphereTransform // 设置变换的id为sphereTransform
        property real userAngle: 0.0 // 设置一个属性userAngle
        matrix: { // 设置变换矩阵
            var m = Qt.matrix4x4(); // 创建一个4x4矩阵
            m.rotate(userAngle, Qt.vector3d(0, 1, 0)); // 绕y轴旋转userAngle度
            m.translate(Qt.vector3d(20, 0, 0)); // 沿x轴平移20个单位
            return m; // 返回变换矩阵
        }
    }

    QQ2.NumberAnimation { // 创建一个数字动画
        target: sphereTransform // 设置动画的目标为sphereTransform
        property: "userAngle" // 设置动画的属性为userAngle
        duration: 10000 // 设置动画的持续时间为10000ms
        from: 0 // 设置动画的起始值为0
        to: 360 // 设置动画的结束值为360

        loops: QQ2.Animation.Infinite // 设置动画循环次数为无限次
        running: true // 设置动画运行
    }

    Entity { // 创建一个实体
        id: sphereEntity // 设置实体的id为sphereEntity
        components: [ sphereMesh, material, sphereTransform ] // 设置实体的组件
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值