我已经设置了一个简单的3D场景,其中包含自定义的QGeometryRenderer和QGeometry。自定义 QGeometry 是从层文件加载的。
class ColorMeshGeometry : public Qt3DRender::QGeometry
{
Q_OBJECT
public:
ColorMeshGeometry(QString meshFile, ColorMeshRenderer *parent);
~ColorMeshGeometry();
void GeometryCenter(QVector3D* center);
};
class ColorMeshRenderer : public Qt3DRender::QGeometryRenderer
{
ColorMeshGeometry* m_geometry;
Q_OBJECT
public:
explicit ColorMeshRenderer(QString meshFile, Qt3DCore::QNode *parent = 0)
{
m_geometry = new ColorMeshGeometry(meshFile, this);
setGeometry(m_geometry);
}
~ColorMeshRenderer();
void ViewCenter(QVector3D* center);
};
这是设置这一切的代码:
uto view = new Qt3DExtras::Qt3DWindow();
auto container = createWindowContainer(view, this);
auto rootEntity = new Qt3DCore::QEntity();
ColorMeshRenderer* mesh = new ColorMeshRenderer(filename, rootEntity);
view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x111111)));
view->camera()->lens()->setPerspectiveProjection(45.0f, view->width()/view->height(), 0.01f, 100000.0f);
view->camera()->setPosition(QVector3D(0.f, -512.f, 500.0f));
view->camera()->setViewCenter(QVector3D(0, 0, 0));
auto material = new Qt3DExtras::QPerVertexColorMaterial(rootEntity);
auto picker = new Qt3DRender::QObjectPicker(rootEntity);
picker->setHoverEnabled(false);
picker->setDragEnabled(false);
auto plyEntity = new Qt3DCore::QEntity(rootEntity);
plyEntity->addComponent(mesh);
plyEntity->addComponent(material);
plyEntity->addComponent(picker);
connect(picker, &Qt3DRender::QObjectPicker::pressed, this, &ViewerWidget::picker_Clicked);
Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
camController->setCamera(view->camera());
view->setRootEntity(rootEntity);
QObjectPicker::p ressed 事件永远不会触发。如果我改用 Qt3DExtras::QTorusMesh,则会触发按下的事件。QObjectPicker还需要实现什么才能使用自定义网格?
编辑:
完整的示例代码可以在这里找到。
对此的修复最终是透视投影。该项目的后窗格最终走得很远,修复是:
view->camera()->lens()->setPerspectiveProjection(45.0f, view->width()/view->height(), 0.01f, 5000.0f);