1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QApplication> |
52 | #include <QPropertyAnimation> |
53 | |
54 | #include <Qt3DCore/QEntity> |
55 | #include <Qt3DCore/QTransform> |
56 | #include <Qt3DCore/QAspectEngine> |
57 | |
58 | #include <Qt3DRender/QCamera> |
59 | #include <Qt3DRender/QCameraLens> |
60 | #include <Qt3DRender/QRenderAspect> |
61 | #include <Qt3DRender/QTexture> |
62 | #include <Qt3DRender/QDirectionalLight> |
63 | |
64 | #include <Qt3DInput/QInputAspect> |
65 | |
66 | #include <Qt3DExtras/QForwardRenderer> |
67 | #include <Qt3DExtras/QDiffuseMapMaterial> |
68 | #include <Qt3DExtras/QCuboidMesh> |
69 | #include <Qt3DExtras/QOrbitCameraController> |
70 | #include <Qt3DExtras/Qt3DWindow> |
71 | |
72 | #include "videoplayer.h" |
73 | |
74 | Qt3DCore::QEntity *(Qt3DExtras::Qt3DWindow *view, Qt3DRender::QAbstractTexture *diffuseTexture) |
75 | { |
76 | // Root entity |
77 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity; |
78 | |
79 | // Material |
80 | Qt3DExtras::QDiffuseMapMaterial *material = new Qt3DExtras::QDiffuseMapMaterial(rootEntity); |
81 | material->setDiffuse(diffuseTexture); |
82 | material->setAmbient(QColor(30, 30, 30)); |
83 | |
84 | // Sphere |
85 | Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity); |
86 | Qt3DExtras::QCuboidMesh *cuboidMesh = new Qt3DExtras::QCuboidMesh; |
87 | Qt3DCore::QTransform *transform = new Qt3DCore::QTransform; |
88 | |
89 | transform->setRotationX(180); |
90 | |
91 | QPropertyAnimation *cubeRotateTransformAnimation = new QPropertyAnimation(transform); |
92 | cubeRotateTransformAnimation->setTargetObject(transform); |
93 | cubeRotateTransformAnimation->setPropertyName("rotationY" ); |
94 | cubeRotateTransformAnimation->setStartValue(QVariant::fromValue(value: 0)); |
95 | cubeRotateTransformAnimation->setEndValue(QVariant::fromValue(value: 360)); |
96 | cubeRotateTransformAnimation->setDuration(10000); |
97 | cubeRotateTransformAnimation->setLoopCount(-1); |
98 | cubeRotateTransformAnimation->start(); |
99 | |
100 | sphereEntity->addComponent(comp: cuboidMesh); |
101 | sphereEntity->addComponent(comp: transform); |
102 | sphereEntity->addComponent(comp: material); |
103 | |
104 | // Camera |
105 | Qt3DRender::QCamera *camera = view->camera(); |
106 | camera->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f); |
107 | camera->setPosition(QVector3D(0, 0, -5.0f)); |
108 | camera->setViewCenter(QVector3D(0, 0, 0)); |
109 | |
110 | // For camera controls |
111 | Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity); |
112 | camController->setLinearSpeed( 50.0f ); |
113 | camController->setLookSpeed( 180.0f ); |
114 | camController->setCamera(camera); |
115 | |
116 | Qt3DRender::QDirectionalLight *light = new Qt3DRender::QDirectionalLight(); |
117 | light->setIntensity(0.8f); |
118 | light->setWorldDirection(camera->viewVector()); |
119 | rootEntity->addComponent(comp: light); |
120 | |
121 | return rootEntity; |
122 | } |
123 | |
124 | int main(int argc, char* argv[]) |
125 | { |
126 | QSurfaceFormat format = QSurfaceFormat::defaultFormat(); |
127 | format.setMajorVersion(4); |
128 | format.setMinorVersion(5); |
129 | format.setProfile(QSurfaceFormat::CoreProfile); |
130 | format.setRenderableType(QSurfaceFormat::OpenGL); |
131 | QSurfaceFormat::setDefaultFormat(format); |
132 | |
133 | // Will make Qt3D and QOpenGLWidget share a common context |
134 | QApplication::setAttribute(attribute: Qt::AA_ShareOpenGLContexts); |
135 | |
136 | QApplication app(argc, argv); |
137 | |
138 | // Multimedia player |
139 | TextureWidget textureWidget; |
140 | VideoPlayer *videoPlayer = new VideoPlayer(&textureWidget); |
141 | |
142 | textureWidget.resize(w: 800, h: 600); |
143 | textureWidget.show(); |
144 | |
145 | // Texture object that Qt3D uses to access the texture from the video player |
146 | Qt3DRender::QSharedGLTexture *sharedTexture = new Qt3DRender::QSharedGLTexture(); |
147 | |
148 | QObject::connect(sender: &textureWidget, signal: &TextureWidget::textureIdChanged, |
149 | receiver: sharedTexture, slot: &Qt3DRender::QSharedGLTexture::setTextureId); |
150 | |
151 | // Qt3D Scene |
152 | Qt3DExtras::Qt3DWindow view; |
153 | Qt3DCore::QEntity *scene = createScene(view: &view, diffuseTexture: sharedTexture); |
154 | view.setRootEntity(scene); |
155 | view.show(); |
156 | |
157 | return app.exec(); |
158 | } |
159 | |