首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用qml qt3d (qt)旋转物体的角度?

如何使用qml qt3d (qt)旋转物体的角度?
EN

Stack Overflow用户
提问于 2020-07-28 08:39:04
回答 1查看 1.4K关注 0票数 2

我想在窗口屏幕上旋转这个物体。我正在使用Qt/QML/Qt3D。

我编写了一些代码这里来在对象窗口显示屏幕中添加一个按钮。在这个按钮的帮助下,我可以将显示屏幕中的物体旋转(90度和180度)。

QML源代码:

代码语言:javascript
运行
复制
import QtQuick.Controls 2.2
import QtQuick.Dialogs 1.2

import QtQuick.Scene3D 2.0

import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0

ApplicationWindow
{
    visible: true
    width: 640
    height: 480
    title: qsTr("3D Viewer")

    header: ToolBar
    {
        ToolButton
        {
            text: "Open 3D Model"
            onPressed:
            {
                fileDialog.open()
            }
        }
    }

    FileDialog
    {
        id: fileDialog
        onAccepted:
        {
            sceneLoader.source = fileDialog.fileUrl
        }
    }

    Scene3D
    {
        anchors.fill: parent

        aspects: ["input", "logic"]
        cameraAspectRatioMode: Scene3D.AutomaticAspectRatio

        Entity
        {
            id: sceneRoot

            Camera
            {
                id: camera
                projectionType: CameraLens.PerspectiveProjection
                fieldOfView: 30
                aspectRatio: 16/9
                nearPlane : 0.1
                farPlane : 1000.0
                position: Qt.vector3d( 10.0, 0.0, 0.0 )
                upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
                viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
            }

            OrbitCameraController
            {
                camera: camera
            }

            components: [
                RenderSettings
                {
                    activeFrameGraph: ForwardRenderer
                    {
                        clearColor: Qt.rgba(0, 0.5, 1, 1)
                        camera: camera
                    }
                },
                InputSettings
                {
                }
            ]

            Entity
            {
                id: monkeyEntity
                components: [
                    SceneLoader
                    {
                        id: sceneLoader
                    }
                ]
            }
        }
    }
}

因此,主要的问题是:什么(假设Transform组件?)在这个源文件中应该添加代码以改变加载模型的角度?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-28 08:55:42

下面是如何在Qt (而不是Qt3D)中执行此操作的一个示例:https://doc.qt.io/qt-5/qml-qtquick-item.html#rotation-prop

代码语言:javascript
运行
复制
Rectangle {
   color: "red"
   x: 25; y: 25; width: 50; height: 50
   rotation: 30  // !
}

此外,您还可以绕另一个轴旋转,下面是有关它的信息:https://doc.qt.io/qt-5/qml-qtquick-rotation.html

示例:

代码语言:javascript
运行
复制
Rectangle {
    width: 100; height: 100
    color: "blue"
    transform: Rotation {
        origin.x: 25;
        origin.y: 25;
        axis { x: 0; y: 1; z: 0 };
        angle: 45
    }
}

更新对于Qt3D,至少使用2.15版本的Qt3D.Core,并使用Transform。下面是编辑您的代码:

代码语言:javascript
运行
复制
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.15
import QtQuick.Dialogs 1.2

import QtQuick.Scene3D 2.15

import Qt3D.Core 2.15
import Qt3D.Render 2.15
import Qt3D.Input 2.15
import Qt3D.Extras 2.15

ApplicationWindow
{
    visible: true
    width: 640
    height: 480
    title: qsTr("3D Viewer")

    header: ToolBar
    {
        RowLayout {

            ToolButton
            {
                text: "Open 3D Model"
                onPressed:
                {
                    fileDialog.open()
                }
            }

            ToolButton
            {
                text: "Rotate"
                onPressed:
                {
                    transform.rotation = Qt.quaternion(1, 0.1, 0, 0);
                }
            }
        }
    }

    FileDialog
    {
        id: fileDialog
        onAccepted:
        {
            sceneLoader.source = fileDialog.fileUrl
        }
    }

    Scene3D
    {
        anchors.fill: parent

        aspects: ["input", "logic"]
        cameraAspectRatioMode: Scene3D.AutomaticAspectRatio

        Entity
        {
            id: sceneRoot

            Camera
            {
                id: camera
                projectionType: CameraLens.PerspectiveProjection
                fieldOfView: 30
                aspectRatio: 16/9
                nearPlane : 0.1
                farPlane : 1000.0
                position: Qt.vector3d( 10.0, 0.0, 0.0 )
                upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
                viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
            }

            OrbitCameraController
            {
                camera: camera
            }

            components: [
                RenderSettings
                {
                    activeFrameGraph: ForwardRenderer
                    {
                        clearColor: Qt.rgba(0, 0.5, 1, 1)
                        camera: camera
                    }
                },
                InputSettings
                {
                }
            ]

            Entity
            {
                id: monkeyEntity
                components: [
                    SceneLoader
                    {
                        id: sceneLoader
                    },
                    Transform {
                        id: transform
                    }
                ]
            }
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63130177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档