Qt Quick 3D系列(二):鼠标控制3D模型旋转缩放

2023-05-16

        上一篇文章Qt Quick 3D系列(一):加载3d模型我们讲述了如何在Qt Quick 3D中显示一个3D模型,那么显示了3D模型后如何使用鼠标进行旋转呢?在Qt 3D中提供了OrbitCameraController用于控制相机的轨道(个人觉得不好用),在Qt Quick 3D中,没有直接控制相机的函数,但是直接使用MouseArear对节点Node进行控制会更加的灵活和方便。

        咱们接着在上一篇文章中的代码进行修改,首先在View3D中新增节点cameraNode,将相机移到该节点中,代码:

Node{
    id:cameraNode
    
    PerspectiveCamera {
        id: camera
        z: 15
    }
}

        此时我们只要旋转节点,节点中的子控件(相机)就以节点中心为顶点进行旋转,在View3D中添加MouseArea并添加滚轮缩放和鼠标左键按住拖动的代码:

MouseArea{
    id:mouse
    anchors.fill: parent
    property int cx: 0
    property int cy: 0
    onWheel: {
        if(wheel.angleDelta.y>0)
            camera.z = camera.z+5
        else
            camera.z = camera.z-5
    }
    onPressed: {
        cx = mouse.x
        cy = mouse.y
    }

    onPositionChanged: {
        var intervalX = mouse.x-cx
        var intervalY = mouse.y-cy
        cameraNode.eulerRotation.y = intervalX+cameraNode.eulerRotation.y
        cameraNode.eulerRotation.x = cameraNode.eulerRotation.x-intervalY
        cx = mouse.x
        cy = mouse.y

    }
}

运行程序就可以使用滚轮和鼠标左键对模型就行控制了,如图:

 示例程序GitHub:https://github.com/zjgo007/QtQuick3D/tree/master/Control3dModel

QML主要代码:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick3D 1.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    View3D {
        id: view3D
        anchors.fill: parent
        environment: sceneEnvironment
        SceneEnvironment {
            id: sceneEnvironment
            antialiasingQuality: SceneEnvironment.High
            antialiasingMode: SceneEnvironment.MSAA
        }

        MouseArea{
            id:mouse
            anchors.fill: parent
            property int cx: 0
            property int cy: 0
            onWheel: {
                if(wheel.angleDelta.y>0)
                    camera.z = camera.z+5
                else
                    camera.z = camera.z-5
            }
            onPressed: {
                cx = mouse.x
                cy = mouse.y
            }

            onPositionChanged: {
                var intervalX = mouse.x-cx
                var intervalY = mouse.y-cy
                cameraNode.eulerRotation.y = intervalX+cameraNode.eulerRotation.y
                cameraNode.eulerRotation.x = cameraNode.eulerRotation.x-intervalY
                cx = mouse.x
                cy = mouse.y
            }
        }
        Node {
            id: node
            DirectionalLight {
                id: directionalLight
            }

            Model {
                id: cubeModel
                source: "test.mesh"
                DefaultMaterial {
                    id: cubeMaterial
                    diffuseColor: "#4aee45"
                }
                materials: cubeMaterial
            }
        }

        Node{
            id:cameraNode
            PerspectiveCamera {
                id: camera
                z: 15
            }
        }
    }
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Qt Quick 3D系列(二):鼠标控制3D模型旋转缩放 的相关文章

随机推荐