Opengl Qt3d:围绕对象旋转摄影机(C+;+;)

Opengl Qt3d:围绕对象旋转摄影机(C+;+;),opengl,3d,qt3d,Opengl,3d,Qt3d,我有一个简单的Qt3D示例(附在下面),我希望能够围绕对象旋转摄影机-现在对象围绕摄影机旋转。我为其他框架找到了一些建议,我想知道在Qt3D中实现这一点的最佳方法是什么 main.cpp #include <QApplication> #include <QWidget> #include "Viewer3d.h" int main(int argc, char* argv[]) { QApplication app(argc, argv); au

我有一个简单的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”
int main(int argc,char*argv[]){
QApplication应用程序(argc、argv);
auto*v3d=新的Viewer3d(nullptr);
v3d->setMinimumSize(800*2600*2);
v3d->show();
返回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;

};
#pragma一次
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类Viewer3d:publicqwidget{
公众:
显式Viewer3d(QWidget*parent=nullptr);
私人:
Qt3DCore::QEntity*m_rootEntity;
Qt3DExtras::Qt3DWindow*m_视图;
};
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);

}
#包括
#包括“Viewer3d.h”
Viewer3d::Viewer3d(QWidget*父项):QWidget(父项){
m_view=新的Qt3DExtras::Qt3DWindow();
m_view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4f));
m_rootEntity=新的Qt3DCore::QEntity;
auto*torus=new Qt3DCore::QEntity(m_rootEntity);
auto*mesh=新Qt3DExtras::QTorusMesh;
网格->设置半径(5);
网格->setMinorRadius(1);
网格->设置(100);
网片->固定片(20);
auto*cube=new Qt3DCore::QEntity(m_rootEntity);
auto cubeMesh=新的Qt3DExtras::QCuboidMesh;
cubeMesh->setXExtent(4);
cubeMesh->setYExtent(4);
cubeMesh->setZExtent(4);
auto*transform=new Qt3DCore::QTransform;
transform->setTranslation(QVector3D(0,0,0));
//变换->设置旋转(QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),45.f));
Qt3DRender::QMaterial*material=新的Qt3DExtras::QPhongMaterial(m_rootEntity);
圆环->添加组件(网格);
环面->添加组件(变换);
圆环->添加组件(材料);
多维数据集->添加组件(立方体网格);
多维数据集->添加组件(转换);
立方体->添加组件(材料);
//摄像机
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,0500.0f));
//cameraEntity->setUpVector(QVector3D(0,1,0));
//cameraEntity->setViewCenter(QVector3D(0,0,0));
//cameraEntity->transform()->setScale(1.f);
//操纵器
自动*操纵器=新Qt3DExtras::QOrbitCameraController(m_rootEntity);
操纵器->设定线速度(50.f);
机械手->设定速度(180.f);
操纵器->设置摄影机(cameraEntity);
//轻的
auto*lightetity=newqt3dcore::QEntity(m_rootEntity);
自动*灯光=新的Qt3DRender::QPointLight(亮度);
灯光->设置颜色(“白色”);
光照->设置强度(1);
亮度->添加组件(灯光);
auto*lightTransform=新Qt3DCore::QTTransform(亮度);
lightTransform->setTranslation(cameraEntity->position());
Lightetity->addComponent(lightTransform);
//设置场景的根对象
m_view->setRootEntity(m_rootEntity);
QWidget*container=QWidget::createWindowContainer(m_视图);
QSize screenSize=m_view->screen()->size();
容器->设置最小尺寸(QSize(200100));
容器->设置最大尺寸(屏幕尺寸);
auto*hLayout=新的QHBoxLayout(此);
hLayout->addWidget(容器,1);
设置布局(hLayout);
}

在Qt3D中是否有一种简单的方法可以实现这一点?

如果您知道如何当场旋转并移动它。基本上试着颠倒你翻译的顺序。如果旋转某个对象,则所有对象都围绕数学原点(0,0)旋转。如果你先旋转它(所以它是旋转的),然后你把它移到远处,它就会在远处旋转。或者从另一个角度来看,如果你在绕着它转,你会像在绕着它转一样。

QVector3D-vector3D=camera->position();
qreal r=qSqrt(vector3D.x()*vector3D.x()+vector3D.z()*vector3D.z());
对于(qreal a=0;asetPosition(vector3D);
摄像机->设置向量(QVector3D(0,1,0));
ui->statusBar->showMessage(“a=“+QString::number(a)+”,相机位置(“+QString::number(相机->位置().x())+”,“+QString::number(相机->位置().y())+”,“+QString::number(相机->位置().z())+”);
QEventLoop事件循环;
QTimer::singleShot(100,&eventloop,SLOT(quit());
eventloop.exec();
}

除了内置类
qorbitcameraconroller
可以用4行代码初始化外,还有什么更简单的方法可以实现呢?问题不在于相机控制器-我想知道如何修改它,使它围绕对象旋转,而不是相反。这就是
qorbitcameraconroller
的工作原理s、 它围绕对象旋转摄影机。
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();
}