2

I want to rotate the object in the window screen. I am using Qt/QML/Qt3D.

I write some code here to add a button in the object window display screen. With the help of this button I could rotate the object in the display screen about (90 and 180) degrees.

QML source code:

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
                    }
                ]
            }
        }
    }
}

So, the main question is: what (assume Transform component?) and where should be added code to this source file to change angle of loaded model?

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Amit Saini
  • 136
  • 2
  • 16

1 Answers1

2

Here is one example of how to do this in Qt (not Qt3D): https://doc.qt.io/qt-5/qml-qtquick-item.html#rotation-prop

Rectangle {
   color: "red"
   x: 25; y: 25; width: 50; height: 50
   rotation: 30  // !
}

Also you can rotate around other axis, here is information about it: https://doc.qt.io/qt-5/qml-qtquick-rotation.html

Example:

Rectangle {
    width: 100; height: 100
    color: "blue"
    transform: Rotation {
        origin.x: 25;
        origin.y: 25;
        axis { x: 0; y: 1; z: 0 };
        angle: 45
    }
}

Update. For Qt3D use at least version 2.15 for Qt3D.Core and use Transform. Here is edited your code:

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
                    }
                ]
            }
        }
    }
}
Ihor Drachuk
  • 1,265
  • 7
  • 17
  • Thanks for this.Sir if I want to add a new button in the window screen .for this I added some code in this main.qml(https://bitbucket.org/amahta/3d_viewer/src/master/main.qml). so that when I click the button then object rotate about 90 and 180 degree.So please help me to write some code for button – Amit Saini Jul 28 '20 at 12:08
  • Can you share with me 3D model that your app could load? It will help me to investigate your problem. Also, probably you can try to use `Transform` from `Qt 3D` module: https://doc.qt.io/qt-5/qml-qt3d-core-transform.html – Ihor Drachuk Jul 28 '20 at 14:56
  • sir ,can you tell me that at where I add your code in my code so that my object interact with this rotate angle. and it successful run . – Amit Saini Aug 04 '20 at 06:34
  • Sorry, this code is for standard controls. For Qt3D you should use another. But I can't test your code without your 3D models, so can't provide well-tested answer for *exactly* Qt3D. If you share 3D model - it'll try to edit answer for Qt3D specific. – Ihor Drachuk Aug 04 '20 at 09:59
  • for 3D model I have the .ply file .In my task when I select the .ply(3d model) file qt screen display the 3D model .but model are not rotating only scaling .So please tell me how to rotate it using mouse and without mouse I want to add the button so that when I click the button model rotate any angle which are given. – Amit Saini Aug 04 '20 at 11:56
  • Thank you very much sir – Amit Saini Aug 05 '20 at 14:13
  • sir, can I specified the angle for rotation i.e Transform { id:transform angle:90 } – Amit Saini Aug 06 '20 at 11:39
  • 1
    Yes, you can. Angle is specified as `quaternion` or `rotationX`, ..Y, ..Z as Euler angle – Ihor Drachuk Aug 06 '20 at 20:29
  • Sir,If I rotate the object on the same axis ,what will do it ? because I want to view the anterior posterior of the object. – Amit Saini Aug 07 '20 at 10:02
  • hi sir , I have used elevation and azimuth value to rotate the object on the same axis .so please tell me at where add these two value(elevation and azimuth)? – Amit Saini Aug 11 '20 at 07:25