Qt3D围绕网格旋转相机 [英] Qt3D rotate camera around mesh

查看:691
本文介绍了Qt3D围绕网格旋转相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习Qt/QML/C ++,并试图构建一个非常基本的3D场景以围绕网状对象旋转相机.

I've recently started learning Qt/QML/C++ and trying to build a very basic 3D scene to rotate the camera around a mesh object.

我发现很难遵循这些示例,并且发现文档没有提供任何有用的说明.似乎也没有太多的教程,也许我在错误的地方寻找.

I'm finding it very difficult to follow the examples and I'm finding the documentation doesn't provide any useful instructions. There doesn't seem to be many tutorials out there either, perhaps I'm looking in the wrong places.

main.cpp

#include <Qt3DQuickExtras/qt3dquickwindow.h>
#include <Qt3DQuick/QQmlAspectEngine>

#include <QGuiApplication>
#include <QtQml>

int main(int argc, char **argv)
{
    QGuiApplication app(argc, argv);
    Qt3DExtras::Quick::Qt3DQuickWindow view;

    // Expose the window as a context property so we can set the aspect ratio
    view.engine()->qmlEngine()->rootContext()->setContextProperty("_window", &view);
    view.setSource(QUrl("qrc:/main.qml"));
    view.setWidth(800);
    view.setHeight(600);
    view.show();

    return app.exec();
}

main.qml

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

Entity {
    id: sceneRoot

    Camera {
        id: camera
        projectionType: CameraLens.PerspectiveProjection
        fieldOfView: 25
        aspectRatio: _window.width / _window.height
        nearPlane : 0.1
        farPlane : 1000.0
        position: Qt.vector3d( 0, 0.0, 20.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 { }
    ]

    PhongMaterial {
        id: carMaterial
    }

    Mesh {
        id: carMesh
        source: "resources/aventador.obj"
    }

    Entity {
        id: carEntity
        components: [ carMesh, carMaterial ]
    }
}

如何使相机围绕网格物体旋转?

How do I get the camera to rotate around the mesh object?

推荐答案

OrbitCameraController允许沿轨道路径移动摄像头.要使其围绕网格旋转,可以将相机的viewCenter设置为网格的位置(转换包含网格的实体的变换),然后使用键盘/鼠标旋转它.

The OrbitCameraController allows to move the camera along an orbital path. To make it rotate around the mesh, you could set the viewCenter of the camera to the position of the mesh (translation of the transform of the entity containing the mesh) and use your keyboard/mouse to rotate it around.

因此添加:

Transform{
        id: carTransform
        translation: Qt.vector3d(5.0, 5.0, 5.0) //random values, choose your own
}

,然后将转换添加到实体的组件. 将相机的viewCenter更改为

and add the transform to the components of the entity. Change the viewCenter of the camera to

viewCenter: carTransform.translation

这篇关于Qt3D围绕网格旋转相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆