diff --git a/src/widgets/3dview/CMakeLists.txt b/src/widgets/3dview/CMakeLists.txt index 90d8ce5..2ddc732 100644 --- a/src/widgets/3dview/CMakeLists.txt +++ b/src/widgets/3dview/CMakeLists.txt @@ -1,24 +1,25 @@ set(3d_SRCS axisgnomonentity.cpp + cameracontroller.cpp fileloader.cpp gcodeto4d.cpp gridmesh.cpp linemesh.cpp linemeshgeometry.cpp viewer3d.cpp ) qt5_add_resources(3dfiles_RCS viewer3d.qrc) add_library(Atelier3D STATIC ${3d_SRCS} ${3dfiles_RCS}) target_link_libraries(Atelier3D Qt5::Core Qt5::Qml Qt5::Quick Qt5::Widgets Qt5::3DCore Qt5::3DExtras Qt5::3DRender Qt5::3DInput ) diff --git a/src/widgets/3dview/SceneEntity.qml b/src/widgets/3dview/SceneEntity.qml index 29a56ee..71f5e28 100644 --- a/src/widgets/3dview/SceneEntity.qml +++ b/src/widgets/3dview/SceneEntity.qml @@ -1,77 +1,77 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2017-2018> Author: Patrick José Pereira - patrickjp@kde.org Kevin Ottens - ervin@kde.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ import QtQuick 2.7 import Qt3D.Core 2.0 import Qt3D.Render 2.0 import Qt3D.Input 2.0 import Qt3D.Extras 2.0 import Atelier 1.0 Entity { id: sceneRoot property string currentFile Camera { id: camera fieldOfView: 45 position: Qt.vector3d( 10.0, -10.0, 35.0 ) upVector: Qt.vector3d( 0.0, 0.85, 0.75 ) viewCenter: Qt.vector3d( 10.0, 10.0, 0.0 ) } - FirstPersonCameraController { camera: camera } + CameraController { camera: camera } components: [ RenderSettings { activeFrameGraph: ForwardRenderer { camera: camera frustumCulling: false } }, InputSettings { } ] Entity { id: gridEntity components: [ PhongMaterial { ambient: "darkBlue" }, GridMesh {} ] } Entity { id: lineEntity components: [ PhongMaterial { ambient: "darkGreen" }, LineMesh { readonly property string currentFile: sceneRoot.currentFile onCurrentFileChanged: readAndRun(currentFile) } ] } AxisGnomonEntity { scale: 0.05 position: Qt.vector2d(0.075, 0.075) } } diff --git a/src/widgets/3dview/cameracontroller.cpp b/src/widgets/3dview/cameracontroller.cpp new file mode 100644 index 0000000..1b59266 --- /dev/null +++ b/src/widgets/3dview/cameracontroller.cpp @@ -0,0 +1,66 @@ +/* Atelier KDE Printer Host for 3D Printing + Copyright (C) <2018> + Author: Kevin Ottens - ervin@kde.org + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License or any later version accepted by the membership of + KDE e.V. (or its successor approved by the membership of KDE + e.V.), which shall act as a proxy defined in Section 14 of + version 3 of the license. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "cameracontroller.h" + +#include + +namespace +{ +inline float clampInputs(float input1, float input2) +{ + float axisValue = input1 + input2; + return (axisValue < -1) ? -1 : (axisValue > 1) ? 1 : axisValue; +} + +inline float zoomDistance(QVector3D firstPoint, QVector3D secondPoint) +{ + return (secondPoint - firstPoint).lengthSquared(); +} +} + +CameraController::CameraController(QNode *parent) + : Qt3DExtras::QAbstractCameraController(parent) +{ +} + +CameraController::~CameraController() +{ +} + +void CameraController::moveCamera(const Qt3DExtras::QAbstractCameraController::InputState &state, float dt) +{ + auto cam = camera(); + if (!cam) { + return; + } + + // Mouse + if (state.leftMouseButtonActive) { + cam->pan(state.rxAxisValue * lookSpeed() * dt); + cam->tilt(state.ryAxisValue * lookSpeed() * dt); + } + + // Keyboard + cam->panAboutViewCenter((state.txAxisValue * lookSpeed()) * dt, QVector3D(0.0f, 0.0f, 1.0f)); + cam->translate(QVector3D(0.0f, 0.0f, state.tyAxisValue * linearSpeed() * dt), + Qt3DRender::QCamera::DontTranslateViewCenter); +} diff --git a/src/widgets/3dview/cameracontroller.h b/src/widgets/3dview/cameracontroller.h new file mode 100644 index 0000000..4d8e7f7 --- /dev/null +++ b/src/widgets/3dview/cameracontroller.h @@ -0,0 +1,35 @@ +/* Atelier KDE Printer Host for 3D Printing + Copyright (C) <2018> + Author: Kevin Ottens - ervin@kde.org + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License or any later version accepted by the membership of + KDE e.V. (or its successor approved by the membership of KDE + e.V.), which shall act as a proxy defined in Section 14 of + version 3 of the license. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#pragma once + +#include + +class CameraController : public Qt3DExtras::QAbstractCameraController +{ + Q_OBJECT +public: + explicit CameraController(Qt3DCore::QNode *parent = nullptr); + ~CameraController(); + +private: + void moveCamera(const QAbstractCameraController::InputState &state, float dt) override; +}; diff --git a/src/widgets/3dview/viewer3d.cpp b/src/widgets/3dview/viewer3d.cpp index b8f7a15..85c1134 100644 --- a/src/widgets/3dview/viewer3d.cpp +++ b/src/widgets/3dview/viewer3d.cpp @@ -1,78 +1,80 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2017> Author: Patrick José Pereira - patrickjp@kde.org Chris Rizzitello - rizzitello@kde.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include #include #include #include #include #include #include "axisgnomonentity.h" +#include "cameracontroller.h" #include "gridmesh.h" #include "viewer3d.h" #include "linemesh.h" Viewer3D::Viewer3D(QWidget *parent) : QWidget(parent) , _lineMesh(new LineMesh) { Q_INIT_RESOURCE(viewer3d); qmlRegisterType("Atelier", 1, 0, "AxisGnomonEntity"); + qmlRegisterType("Atelier", 1, 0, "CameraController"); qmlRegisterType("Atelier", 1, 0, "GridMesh"); qmlRegisterType("Atelier", 1, 0, "LineMesh"); _view = new QQuickView(&_engine, nullptr); auto format = QSurfaceFormat(); format.setVersion(3, 1); format.setProfile(QSurfaceFormat::CoreProfile); _view->setFormat(format); _view->setResizeMode(QQuickView::SizeRootObjectToView); _view->setSource(QUrl(QStringLiteral("qrc:/viewer3d.qml"))); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(QWidget::createWindowContainer(_view)); QObject *item = _view->rootObject(); //Connect the drop pass from the QML part. connect(item, SIGNAL(droppedUrls(QVariant)), this, SLOT(dropCatch(QVariant))); this->setLayout(mainLayout); } Viewer3D::~Viewer3D() { } void Viewer3D::dropCatch(const QVariant &var) { emit droppedUrls(var.value >()); } void Viewer3D::drawModel(QString file) { QObject *object = _view->rootObject(); QObject *fileName = object->findChild(QStringLiteral("fileName")); fileName->setProperty("text", QVariant(file)); }