c++ - 如何使用 qt 放大 3D 网格?

标签 c++ qt qt3d

我正在使用 Wheelevent 在 QWidget View 中放大/缩小,在使用事件平移 QCamera 时,是否有来自 qt api 的解决方案向一个点移动或将相机缩放到特定点?我搜索了很多部分,但不幸的是没有找到有用的东西。

最佳答案

编辑:Stefan Reinhardt 建议使用QAbstractCameraController 来实现您想做的事情。我提供的示例是一个快速而简单的解决方案。我同意使用相机 Controller 是 Qt3D 中的预期方式。


Qt3D API 不直接支持这一点,但您可以自己轻松实现它。

这是一个最低限度的工作示例,它应该可以帮助您入门(注意,我想当滚动经过 View 中心时,您必须调整向上 vector ):

main.cpp:

#include <QApplication>
#include "graphicswindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    GraphicsWindow graphicsWindow;
    graphicsWindow.show();
    return a.exec();
}

graphicswindow.h:

#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DCore/QEntity>

class GraphicsWindow : public Qt3DExtras::Qt3DWindow {
public:
    GraphicsWindow();
    void wheelEvent ( QWheelEvent * event ) override;

private:
    Qt3DCore::QEntity *createScene();
};

graphicswindow.cpp:

#include "graphicswindow.h"
#include <QMouseEvent>
#include <QVector3D>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QMaterial>
#include <Qt3DExtras/QGoochMaterial>
#include <Qt3DExtras/QCuboidMesh>

GraphicsWindow::GraphicsWindow() : Qt3DExtras::Qt3DWindow() {
    // You could also create a dedicated setup method
    Qt3DCore::QEntity *root = createScene();
    setRootEntity(root);
    Qt3DRender::QCamera *camera = this->camera();
    camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    camera->setPosition(QVector3D(20.0, 20.0, 20.0f));
    camera->setViewCenter(QVector3D(0, 0, 0));
}

void GraphicsWindow::wheelEvent(QWheelEvent *event) {
    QVector3D camPos = this->camera()->position();
    camPos.normalize();
    camPos = this->camera()->position() - QVector3D(event->delta() / 300.f,
                                                event->delta() / 300.f,
                                                event->delta() / 300.f);
    this->camera()->setPosition(camPos);
}


Qt3DCore::QEntity* GraphicsWindow::createScene() {
    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;

    Qt3DRender::QMaterial *material = new Qt3DExtras::QGoochMaterial(rootEntity);

    //Cube
    Qt3DCore::QEntity *cubeEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DExtras::QCuboidMesh *cubeMesh = new Qt3DExtras::QCuboidMesh;

    cubeEntity->addComponent(cubeMesh);
    cubeEntity->addComponent(material);

    return rootEntity;
}

关于c++ - 如何使用 qt 放大 3D 网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51708350/

相关文章:

c++ - 根据操作系统架构使用数据类型

c++ - 如果 atomic_flag 变量是类的成员,我该如何初始化它?

Qt3D "qt.glx: qglx_findConfig: Failed to finding matching FBConfig"警告

qt - 如何使用 QML 在 Qt3D 中启用 GL 功能

c++ - 在 C++ 的进程中查找加载的 DLL 的内存地址

c++ - 在 C++ 中获取地址的内容

python - 如何查找信号是否连接到任何东西

c++ - 单元测试大型Qt项目

c++ - OpenCV OpenGLDrawCallback 没有被调用

c++ - 如何使用Qt 3D库加载和显示Blender .obj源文件场景