如何使用 qt 放大 3D 网格?
How to zoom toward a 3D mesh with qt?
我正在使用 Wheelevent 在 QWidget
视图中缩放 in/out,在使用事件平移 QCamera
时,是否有来自 qt [=15] 的解决方案=] 向一个点移动或将相机缩放到特定点?我搜索了很多部分,但不幸的是没有找到有用的东西。
编辑: Stefan Reinhardt 建议使用 QAbstractCameraController
来实现您想做的事情。我提供的示例是一个快速而简单的解决方案。我同意使用相机控制器是 Qt3D 中的预期方式。
Qt3DAPI不直接支持这个,但是你可以自己轻松实现。
这是一个最低限度的工作示例,应该可以帮助您入门(注意,我猜当滚动经过视图中心时,您必须调整向上矢量):
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;
}
我正在使用 Wheelevent 在 QWidget
视图中缩放 in/out,在使用事件平移 QCamera
时,是否有来自 qt [=15] 的解决方案=] 向一个点移动或将相机缩放到特定点?我搜索了很多部分,但不幸的是没有找到有用的东西。
编辑: Stefan Reinhardt 建议使用 QAbstractCameraController
来实现您想做的事情。我提供的示例是一个快速而简单的解决方案。我同意使用相机控制器是 Qt3D 中的预期方式。
Qt3DAPI不直接支持这个,但是你可以自己轻松实现。
这是一个最低限度的工作示例,应该可以帮助您入门(注意,我猜当滚动经过视图中心时,您必须调整向上矢量):
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;
}