| 1 | // Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QApplication> |
| 5 | #include <QPropertyAnimation> |
| 6 | |
| 7 | #include <Qt3DCore/QEntity> |
| 8 | #include <Qt3DCore/QTransform> |
| 9 | #include <Qt3DCore/QAspectEngine> |
| 10 | |
| 11 | #include <Qt3DRender/QCamera> |
| 12 | #include <Qt3DRender/QCameraLens> |
| 13 | #include <Qt3DRender/QRenderAspect> |
| 14 | #include <Qt3DRender/QTexture> |
| 15 | #include <Qt3DRender/QDirectionalLight> |
| 16 | #include <Qt3DRender/QGeometryRenderer> |
| 17 | |
| 18 | #include <Qt3DInput/QInputAspect> |
| 19 | |
| 20 | #include <Qt3DExtras/QForwardRenderer> |
| 21 | #include <Qt3DExtras/QDiffuseMapMaterial> |
| 22 | #include <Qt3DExtras/QCuboidMesh> |
| 23 | #include <Qt3DExtras/QOrbitCameraController> |
| 24 | #include <Qt3DExtras/Qt3DWindow> |
| 25 | |
| 26 | #include "videoplayer.h" |
| 27 | |
| 28 | Qt3DCore::QEntity *createScene(Qt3DExtras::Qt3DWindow *view, Qt3DRender::QAbstractTexture *diffuseTexture) |
| 29 | { |
| 30 | // Root entity |
| 31 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity; |
| 32 | |
| 33 | // Material |
| 34 | Qt3DExtras::QDiffuseMapMaterial *material = new Qt3DExtras::QDiffuseMapMaterial(rootEntity); |
| 35 | material->setDiffuse(diffuseTexture); |
| 36 | material->setAmbient(QColor(30, 30, 30)); |
| 37 | |
| 38 | // Sphere |
| 39 | Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity); |
| 40 | Qt3DExtras::QCuboidMesh *cuboidMesh = new Qt3DExtras::QCuboidMesh; |
| 41 | Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; |
| 42 | |
| 43 | transform->setRotationX(180); |
| 44 | |
| 45 | QPropertyAnimation *cubeRotateTransformAnimation = new QPropertyAnimation(transform); |
| 46 | cubeRotateTransformAnimation->setTargetObject(transform); |
| 47 | cubeRotateTransformAnimation->setPropertyName("rotationY"); |
| 48 | cubeRotateTransformAnimation->setStartValue(QVariant::fromValue(0)); |
| 49 | cubeRotateTransformAnimation->setEndValue(QVariant::fromValue(360)); |
| 50 | cubeRotateTransformAnimation->setDuration(10000); |
| 51 | cubeRotateTransformAnimation->setLoopCount(-1); |
| 52 | cubeRotateTransformAnimation->start(); |
| 53 | |
| 54 | sphereEntity->addComponent(cuboidMesh); |
| 55 | sphereEntity->addComponent(transform); |
| 56 | sphereEntity->addComponent(material); |
| 57 | |
| 58 | // Camera |
| 59 | Qt3DRender::QCamera *camera = view->camera(); |
| 60 | camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); |
| 61 | camera->setPosition(QVector3D(0, 0, -5.0f)); |
| 62 | camera->setViewCenter(QVector3D(0, 0, 0)); |
| 63 | |
| 64 | // For camera controls |
| 65 | Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity); |
| 66 | camController->setLinearSpeed( 50.0f ); |
| 67 | camController->setLookSpeed( 180.0f ); |
| 68 | camController->setCamera(camera); |
| 69 | |
| 70 | Qt3DRender::QDirectionalLight *light = new Qt3DRender::QDirectionalLight(); |
| 71 | light->setIntensity(0.8f); |
| 72 | light->setWorldDirection(camera->viewVector()); |
| 73 | rootEntity->addComponent(light); |
| 74 | |
| 75 | return rootEntity; |
| 76 | } |
| 77 | |
| 78 | int main(int argc, char* argv[]) |
| 79 | { |
| 80 | QSurfaceFormat format = QSurfaceFormat::defaultFormat(); |
| 81 | format.setMajorVersion(4); |
| 82 | format.setMinorVersion(5); |
| 83 | format.setProfile(QSurfaceFormat::CoreProfile); |
| 84 | format.setRenderableType(QSurfaceFormat::OpenGL); |
| 85 | QSurfaceFormat::setDefaultFormat(format); |
| 86 | |
| 87 | // Will make Qt3D and QOpenGLWidget share a common context |
| 88 | QApplication::setAttribute(Qt::AA_ShareOpenGLContexts); |
| 89 | |
| 90 | QApplication app(argc, argv); |
| 91 | |
| 92 | // Multimedia player |
| 93 | TextureWidget textureWidget; |
| 94 | VideoPlayer *videoPlayer = new VideoPlayer(&textureWidget); |
| 95 | |
| 96 | textureWidget.resize(800, 600); |
| 97 | textureWidget.show(); |
| 98 | |
| 99 | // Texture object that Qt3D uses to access the texture from the video player |
| 100 | Qt3DRender::QSharedGLTexture *sharedTexture = new Qt3DRender::QSharedGLTexture(); |
| 101 | |
| 102 | QObject::connect(&textureWidget, &TextureWidget::textureIdChanged, |
| 103 | sharedTexture, &Qt3DRender::QSharedGLTexture::setTextureId); |
| 104 | |
| 105 | // Qt3D Scene |
| 106 | Qt3DExtras::Qt3DWindow view; |
| 107 | Qt3DCore::QEntity *scene = createScene(&view, sharedTexture); |
| 108 | view.setRootEntity(scene); |
| 109 | view.show(); |
| 110 | |
| 111 | return app.exec(); |
| 112 | } |