Qt3d:围绕对象旋转相机(C ++)

时间:2018-02-04 03:17:57

标签: opengl 3d qt3d

我有一个简单的Qt3D示例(附在下面),我希望能够围绕对象旋转相机 - 此时对象围绕相机旋转。我已经找到了一些其他框架的建议,我想知道在Qt3D中最好的方法是什么?

的main.cpp

#include <QApplication>
#include <QWidget>

#include "Viewer3d.h"


int main(int argc, char* argv[]) {
    QApplication app(argc, argv);

    auto* v3d = new Viewer3d(nullptr);

    v3d->setMinimumSize(800 * 2, 600 * 2);
    v3d->show();

    return app.exec();
}

Viewer3d.h

#pragma once

#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DCore/QAspectEngine>

#include <Qt3DRender/qrenderaspect.h>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QMaterial>
#include <Qt3DRender/QPointLight>

#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QTorusMesh>
#include <Qt3DExtras/QCuboidMesh>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DExtras/QFirstPersonCameraController>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DExtras/QPhongMaterial>
#include <QtWidgets/QWidget>
#include <QScreen>

class Viewer3d: public QWidget {
public:
    explicit Viewer3d(QWidget* parent= nullptr);

private:
    Qt3DCore::QEntity* m_rootEntity;
    Qt3DExtras::Qt3DWindow *m_view;

};

Viewer3d.cpp

#include <QHBoxLayout>
#include "Viewer3d.h"

Viewer3d::Viewer3d(QWidget *parent): QWidget(parent) {
    m_view = new Qt3DExtras::Qt3DWindow();
    m_view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));

    m_rootEntity = new Qt3DCore::QEntity;

    auto* torus = new Qt3DCore::QEntity(m_rootEntity);

    auto* mesh = new Qt3DExtras::QTorusMesh;
    mesh->setRadius(5);
    mesh->setMinorRadius(1);
    mesh->setRings(100);
    mesh->setSlices(20);

    auto* cube = new Qt3DCore::QEntity(m_rootEntity);

    auto cubeMesh = new Qt3DExtras::QCuboidMesh;
    cubeMesh->setXExtent(4);
    cubeMesh->setYExtent(4);
    cubeMesh->setZExtent(4);

    auto* transform = new Qt3DCore::QTransform;
    transform->setTranslation(QVector3D(0, 0, 0));
//    transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1,0,0), 45.f ));

    Qt3DRender::QMaterial* material = new Qt3DExtras::QPhongMaterial(m_rootEntity);

    torus->addComponent(mesh);
    torus->addComponent(transform);
    torus->addComponent(material);

    cube->addComponent(cubeMesh);
    cube->addComponent(transform);
    cube->addComponent(material);



    // Camera
    Qt3DRender::QCamera *cameraEntity = m_view->camera();
//    cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    cameraEntity->setPosition(QVector3D(0, 0, 40.0f));
//    cameraEntity->setViewCenter(QVector3D(0, 0, 0));

//    cameraEntity->setPosition(QVector3D(0, 0, 500.0f));
//    cameraEntity->setUpVector(QVector3D(0, 1, 0));
//    cameraEntity->setViewCenter(QVector3D(0, 0, 0));
//    cameraEntity->transform()->setScale(1.f);


    // manipulator
    auto* manipulator = new Qt3DExtras::QOrbitCameraController (m_rootEntity);
    manipulator->setLinearSpeed(50.f);
    manipulator->setLookSpeed(180.f);
    manipulator->setCamera(cameraEntity);

    // light
    auto *lightEntity = new Qt3DCore::QEntity(m_rootEntity);
    auto *light = new Qt3DRender::QPointLight(lightEntity);
    light->setColor("white");
    light->setIntensity(1);
    lightEntity->addComponent(light);
    auto *lightTransform = new Qt3DCore::QTransform(lightEntity);
    lightTransform->setTranslation(cameraEntity->position());
    lightEntity->addComponent(lightTransform);

    // Set root object of the scene
    m_view->setRootEntity(m_rootEntity);

    QWidget *container = QWidget::createWindowContainer(m_view);
    QSize screenSize = m_view->screen()->size();
    container->setMinimumSize(QSize(200, 100));
    container->setMaximumSize(screenSize);

    auto *hLayout = new QHBoxLayout(this);
    hLayout->addWidget(container, 1);

    setLayout(hLayout);

}

Qt3D中是否有直接的方法?

2 个答案:

答案 0 :(得分:0)

如果你知道如何在现场旋转它,并移动它。基本上尝试颠倒你翻译的顺序。如果你旋转某些东西,一切都围绕数学原点旋转(0,0)。因此,如果您先旋转它(使它旋转),那么您将它移动到距离,它将在远处旋转。或者从其他角度来看,如果你正在围绕它进行轨道运动,那么你就是这样。

答案 1 :(得分:0)

https://github.com/sonichy/HTYCAD

QVector3D vector3D = camera->position();
qreal r = qSqrt(vector3D.x() * vector3D.x() + vector3D.z() * vector3D.z());
for(qreal a=0; a<2 * M_PI; a+= 0.01){
    qreal z = r * qSin(a);
    qreal x = r * qCos(a);
    vector3D.setX(x);
    vector3D.setZ(z);
    camera->setPosition(vector3D);
    camera->setUpVector(QVector3D(0, 1, 0));
    ui->statusBar->showMessage("a = " + QString::number(a) + ", Camera Position (" + QString::number(camera->position().x()) + ", " + QString::number(camera->position().y()) + ", " + QString::number(camera->position().z()) + ")");
    QEventLoop eventloop;
    QTimer::singleShot(100, &eventloop, SLOT(quit()));
    eventloop.exec();
}