1 2 3 Ім'я файлу: Курсова (3) (1).docx Розширення: docx Розмір: 2331кб. Дата: 27.01.2023 скачати Пов'язані файли: Кишеньковий довыдник.doc CameraController(Qt3DCore::QNode *parent = Q_NULLPTR); void setCamera(Qt3DRender::QCamera *camera); float lookSpeed() const; void setLookSpeed(float lookSpeed); float linearSpeed() const; void setLinearSpeed(float linearSpeed); protected slots: void activeChanged(bool isActive); void valueChanged(float value); void frameActionTriggered(float dt); private: Qt3DInput::QMouseDevice * m_mouseDevice; Qt3DInput::QKeyboardDevice * m_keyboardDevice; Qt3DInput::QLogicalDevice * m_logicalDevice; Qt3DInput::QAction *m_keyUpAction; Qt3DInput::QAction *m_keyDownAction; Qt3DInput::QAction *m_keyRightAction; Qt3DInput::QAction *m_keyLeftAction; Qt3DInput::QAction *m_keySpaceAction; Qt3DInput::QAction *m_keyLeftShiftAction; Qt3DInput::QActionInput *m_keyUpInput; Qt3DInput::QActionInput *m_keyDownInput; Qt3DInput::QActionInput *m_keyRightInput; Qt3DInput::QActionInput *m_keyLeftInput; Qt3DInput::QActionInput *m_keySpaceInput; Qt3DInput::QActionInput *m_keyLeftShiftInput; Qt3DInput::QAxis *m_xAxis; Qt3DInput::QAxis *m_yAxis; Qt3DInput::QAnalogAxisInput *m_mouseXInput; Qt3DInput::QAnalogAxisInput *m_mouseYInput; Qt3DLogic::QFrameAction *m_frameAction; Qt3DRender::QCamera *m_camera; float m_lookSpeed; float m_linearSpeed; float m_foV; float m_dx; float m_dy; bool m_keyUpPressed; bool m_keyDownPressed; bool m_keyRightPressed; bool m_keyLeftPressed; bool m_keySpacePressed; bool m_keyLeftShiftPressed; }; #endif // CAMERACONTROLLER_H Скрипт untitled2.pro QT +=3dcore 3dlogic 3dextras 3dinput greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ cameracontroller.cpp \ main.cpp HEADERS += \ cameracontroller.h # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target RESOURCES += \ resources.qrc Повний код програми на QML: Основний скрипт main.cpp #include #include int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); Qt3DExtras::Quick::Qt3DQuickWindow view; view.setSource(QUrl(QStringLiteral("qrc:/main.qml"))); view.show(); return app.exec(); } Скрипт main.qml import QtQuick 2.15 import Qt3D.Core 2.0 import Qt3D.Input 2.0 import Qt3D.Logic 2.0 import Qt3D.Extras 2.0 import Qt3D.Render 2.0 Entity { id : rootEntity Camera { id: mainCamera projectionType: CameraLens.PerspectiveProjection fieldOfView: 60.0 aspectRatio: 16.0/9.0 nearPlane: 0.1 farPlane: 1000.0 position: Qt.vector3d(0.0,0.0,40.0) upVector: Qt.vector3d(0.0,1.0,0.0) viewCenter: Qt.vector3d(0.0,0.0,0.0) } TorusMesh { id: torusMesh radius: 15.0 minorRadius: 6.0 slices: 16 rings: 32 } PhongMaterial { id: torusMaterial diffuse: Qt.rgba(1,0,1,1) specular: Qt.rgba(0,1,1,1) } Transform { id: torusTransform translation: Qt.vector3d(-30.0,0.0,0.0) } components: [ RenderSettings { activeFrameGraph: ForwardRenderer { clearColor: Qt.rgba(1,1,1,1) camera: mainCamera } }, InputSettings{} ] CameraController { mainCamera: mainCamera } Entity { id: torusEntity components: [torusMesh, torusMaterial, torusTransform] } PointLight { id: pointLight1 } Transform { id: lightTransform1 translation:Qt.vector3d(-20.0,0.0,20.0) } Entity { id: pointLightEntity1 components:[pointLight1, lightTransform1] } PointLight { id: pointLight2 } Transform { id: lightTransform2 translation:Qt.vector3d(0.0,50.0,0.0) } Entity { id: pointLightEntity2 components:[pointLight2, lightTransform2] } PointLight { id: pointLight3 } Transform { id: lightTransform3 translation:Qt.vector3d(30.0,50.0,0.0) } Entity { id: pointLightEntity3 components:[pointLight3, lightTransform3] } Transform { id: skyBoxTransform translation:Qt.vector3d(0.0,0.0,0.0) scale3D: Qt.vector3d(1,1,1) } SkyboxEntity { id:skyBoxEntity baseName: "qrc:/res/skybox" extension: ".tga" components:[skyBoxTransform] } CuboidMesh { id: cuboidMesh xExtent:12 yExtent:12 zExtent:12 } PhongMaterial { id: cuboidMaterial diffuse: Qt.rgba(0,0,1,1) specular: Qt.rgba(1,1,1,1) } Transform { id: cuboidTransform translation:Qt.vector3d(-30.0,0.0,0.0) rotationX:55 rotationZ:45 } Entity { id: cuboidEntity components:[cuboidTransform,cuboidMaterial,cuboidMesh] } PlaneMesh { id:planeMesh height: 1000 width: 1000 } PhongMaterial { id: planeMaterial diffuse: Qt.rgba(0,1,0.196,1) } Transform { id: planeTransform translation:Qt.vector3d(0.0,-20.0,0.0) } Entity { id:planeEntity components:[planeTransform,planeMesh,planeMaterial] } ConeMesh { id:coneMesh bottomRadius:15 topRadius:3 rings:32 slices:16 length:40 } Transform { id:coneTransform translation:Qt.vector3d(30.0,0.0,0.0) rotationZ:180 } PhongMaterial { id:coneMaterial diffuse:Qt.rgba(1,0.831,0.435,1) } Entity { id:coneEntity components:[coneTransform,coneMesh,coneMaterial] } SphereMesh { id: sphereMesh1 radius:8.5 rings:32 slices:32 } PhongMaterial { id:sphereMaterial1 diffuse: Qt.rgba(0.396,0.278,0,1) } Transform { id:sphereTransform1 translation: Qt.vector3d(25,20,-3) } Entity { id:sphereEntity1 components:[sphereMesh1,sphereTransform1,sphereMaterial1] } SphereMesh { id: sphereMesh2 radius:9 rings:32 slices:32 } PhongMaterial { id:sphereMaterial2 diffuse: Qt.rgba(0.968,0.741,0.976,1) } Transform { id:sphereTransform2 translation: Qt.vector3d(35,20,-3) } Entity { id:sphereEntity2 components:[sphereMesh2,sphereTransform2,sphereMaterial2] } SphereMesh { id: sphereMesh3 radius:9 rings:32 slices:32 } PhongMaterial { id:sphereMaterial3 diffuse: Qt.rgba(0.776,1,0.623,1) } Transform { id:sphereTransform3 translation: Qt.vector3d(30,20,5) } Entity { id:sphereEntity3 components:[sphereMesh3,sphereTransform3,sphereMaterial3] } } Скрипт cameracontroller.qml import QtQuick 2.15 import Qt3D.Core 2.0 import Qt3D.Input 2.0 import Qt3D.Logic 2.0 import Qt3D.Extras 2.0 import Qt3D.Render 2.0 Entity { id: rootEntity property Camera mainCamera property real lookSpeed: 180.0 property real linearSpeed: 50.0 QtObject { id: attributes property bool keyUpPressed: false property bool keyDownPressed: false property bool keyLeftPressed: false property bool keyRightPressed: false property bool keySpacePressed: false property bool keyLeftShiftPressed: false property real dx: 0.0 property real dy: 0.0 } MouseDevice { id: mouseDevice } KeyboardDevice { id: keyboardDevice } LogicalDevice { id: logicalDevice actions: [ Action { inputs: [ ActionInput { buttons: [Qt.Key_Up,Qt.Key_W] sourceDevice: keyboardDevice } ] onActiveChanged: { attributes.keyUpPressed = isActive } }, Action { inputs: [ ActionInput { buttons: [Qt.Key_Down,Qt.Key_S] sourceDevice: keyboardDevice } ] onActiveChanged: { attributes.keyDownPressed = isActive } }, Action { inputs: [ ActionInput { buttons: [Qt.Key_Left,Qt.Key_A] sourceDevice: keyboardDevice } ] onActiveChanged: { attributes.keyLeftPressed = isActive } }, Action { inputs: [ ActionInput { buttons: [Qt.Key_Right, Qt.Key_D] sourceDevice: keyboardDevice } ] onActiveChanged: { attributes.keyRightPressed = isActive } }, Action { inputs: [ ActionInput { buttons: [Qt.Key_Space] sourceDevice: keyboardDevice } ] onActiveChanged: { attributes.keySpacePressed = isActive } }, Action { inputs: [ ActionInput { buttons: [Qt.Key_Shift] sourceDevice: keyboardDevice } ] onActiveChanged: { attributes.keyLeftShiftPressed = isActive } } ] axes: [ Axis { inputs: [ AnalogAxisInput { axis: MouseDevice.X sourceDevice: mouseDevice } ] onValueChanged: { attributes.dx = value } }, Axis { inputs: [ AnalogAxisInput { axis: MouseDevice.Y sourceDevice: mouseDevice } ] onValueChanged: { attributes.dy = value } } ] } FrameAction { onTriggered: { mainCamera.pan(attributes.dx*lookSpeed*dt) mainCamera.tilt(attributes.dy*lookSpeed*dt) if (attributes.keyUpPressed == true) { mainCamera.translate(Qt.vector3d(0,0,2),Camera.TranslateViewCenter) } if (attributes.keyDownPressed == true) { mainCamera.translate(Qt.vector3d(0,0,-2),Camera.TranslateViewCenter) } if (attributes.keyLeftPressed == true) { mainCamera.translate(Qt.vector3d(-2,0,0),Camera.TranslateViewCenter) } if (attributes.keyRightPressed == true) { mainCamera.translate(Qt.vector3d(2,0,0),Camera.TranslateViewCenter) } if (attributes.keySpacePressed == true) { mainCamera.translate(Qt.vector3d(0,2,0),Camera.TranslateViewCenter) } if (attributes.keyLeftShiftPressed == true) { mainCamera.translate(Qt.vector3d(0,-2,0),Camera.TranslateViewCenter) } } } } Скрипт untitled3.pro QT += quick qml 3dcore 3drender 3dinput 3dlogic 3dquick 3dquickextras # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp RESOURCES += qml.qrc \ resources.qrc # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = # Additional import path used to resolve QML modules just for Qt Quick Designer QML_DESIGNER_IMPORT_PATH = # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target 1 2 3 |