1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 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 "scenemodifier.h" |
52 | |
53 | #include <QGuiApplication> |
54 | |
55 | #include <Qt3DRender/qcamera.h> |
56 | #include <Qt3DCore/qentity.h> |
57 | #include <Qt3DRender/qcameralens.h> |
58 | |
59 | #include <QtWidgets/QApplication> |
60 | #include <QtWidgets/QWidget> |
61 | #include <QtWidgets/QHBoxLayout> |
62 | #include <QtWidgets/QCheckBox> |
63 | #include <QtWidgets/QCommandLinkButton> |
64 | #include <QtGui/QScreen> |
65 | |
66 | #include <Qt3DExtras/qtorusmesh.h> |
67 | #include <Qt3DRender/qmesh.h> |
68 | #include <Qt3DRender/qtechnique.h> |
69 | #include <Qt3DRender/qmaterial.h> |
70 | #include <Qt3DRender/qeffect.h> |
71 | #include <Qt3DRender/qtexture.h> |
72 | #include <Qt3DRender/qrenderpass.h> |
73 | #include <Qt3DRender/qsceneloader.h> |
74 | #include <Qt3DRender/qpointlight.h> |
75 | |
76 | #include <Qt3DCore/qtransform.h> |
77 | #include <Qt3DCore/qaspectengine.h> |
78 | |
79 | #include <Qt3DRender/qrenderaspect.h> |
80 | #include <Qt3DExtras/qforwardrenderer.h> |
81 | |
82 | #include <Qt3DExtras/qt3dwindow.h> |
83 | #include <Qt3DExtras/qfirstpersoncameracontroller.h> |
84 | |
85 | int main(int argc, char **argv) |
86 | { |
87 | QApplication app(argc, argv); |
88 | Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow(); |
89 | view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f))); |
90 | QWidget *container = QWidget::createWindowContainer(window: view); |
91 | QSize screenSize = view->screen()->size(); |
92 | container->setMinimumSize(QSize(200, 100)); |
93 | container->setMaximumSize(screenSize); |
94 | |
95 | QWidget *widget = new QWidget; |
96 | QHBoxLayout *hLayout = new QHBoxLayout(widget); |
97 | QVBoxLayout *vLayout = new QVBoxLayout(); |
98 | vLayout->setAlignment(Qt::AlignTop); |
99 | hLayout->addWidget(container, stretch: 1); |
100 | hLayout->addLayout(layout: vLayout); |
101 | |
102 | widget->setWindowTitle(QStringLiteral("Basic shapes" )); |
103 | |
104 | // Root entity |
105 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); |
106 | |
107 | // Camera |
108 | Qt3DRender::QCamera *cameraEntity = view->camera(); |
109 | |
110 | cameraEntity->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f); |
111 | cameraEntity->setPosition(QVector3D(0, 0, 20.0f)); |
112 | cameraEntity->setUpVector(QVector3D(0, 1, 0)); |
113 | cameraEntity->setViewCenter(QVector3D(0, 0, 0)); |
114 | |
115 | Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity); |
116 | Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity); |
117 | light->setColor("white" ); |
118 | light->setIntensity(1); |
119 | lightEntity->addComponent(comp: light); |
120 | Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity); |
121 | lightTransform->setTranslation(cameraEntity->position()); |
122 | lightEntity->addComponent(comp: lightTransform); |
123 | |
124 | // For camera controls |
125 | Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity); |
126 | camController->setCamera(cameraEntity); |
127 | |
128 | // Scenemodifier |
129 | SceneModifier *modifier = new SceneModifier(rootEntity); |
130 | |
131 | // Set root object of the scene |
132 | view->setRootEntity(rootEntity); |
133 | |
134 | // Create control widgets |
135 | QCommandLinkButton *info = new QCommandLinkButton(); |
136 | info->setText(QStringLiteral("Qt3D ready-made meshes" )); |
137 | info->setDescription(QString::fromLatin1(str: "Qt3D provides several ready-made meshes, like torus, cylinder, cone, " |
138 | "cube, plane and sphere." )); |
139 | info->setIconSize(QSize(0,0)); |
140 | |
141 | QCheckBox *torusCB = new QCheckBox(widget); |
142 | torusCB->setChecked(true); |
143 | torusCB->setText(QStringLiteral("Torus" )); |
144 | |
145 | QCheckBox *coneCB = new QCheckBox(widget); |
146 | coneCB->setChecked(true); |
147 | coneCB->setText(QStringLiteral("Cone" )); |
148 | |
149 | QCheckBox *cylinderCB = new QCheckBox(widget); |
150 | cylinderCB->setChecked(true); |
151 | cylinderCB->setText(QStringLiteral("Cylinder" )); |
152 | |
153 | QCheckBox *cuboidCB = new QCheckBox(widget); |
154 | cuboidCB->setChecked(true); |
155 | cuboidCB->setText(QStringLiteral("Cuboid" )); |
156 | |
157 | QCheckBox *planeCB = new QCheckBox(widget); |
158 | planeCB->setChecked(true); |
159 | planeCB->setText(QStringLiteral("Plane" )); |
160 | |
161 | QCheckBox *sphereCB = new QCheckBox(widget); |
162 | sphereCB->setChecked(true); |
163 | sphereCB->setText(QStringLiteral("Sphere" )); |
164 | |
165 | vLayout->addWidget(info); |
166 | vLayout->addWidget(torusCB); |
167 | vLayout->addWidget(coneCB); |
168 | vLayout->addWidget(cylinderCB); |
169 | vLayout->addWidget(cuboidCB); |
170 | vLayout->addWidget(planeCB); |
171 | vLayout->addWidget(sphereCB); |
172 | |
173 | QObject::connect(sender: torusCB, signal: &QCheckBox::stateChanged, |
174 | receiver: modifier, slot: &SceneModifier::enableTorus); |
175 | QObject::connect(sender: coneCB, signal: &QCheckBox::stateChanged, |
176 | receiver: modifier, slot: &SceneModifier::enableCone); |
177 | QObject::connect(sender: cylinderCB, signal: &QCheckBox::stateChanged, |
178 | receiver: modifier, slot: &SceneModifier::enableCylinder); |
179 | QObject::connect(sender: cuboidCB, signal: &QCheckBox::stateChanged, |
180 | receiver: modifier, slot: &SceneModifier::enableCuboid); |
181 | QObject::connect(sender: planeCB, signal: &QCheckBox::stateChanged, |
182 | receiver: modifier, slot: &SceneModifier::enablePlane); |
183 | QObject::connect(sender: sphereCB, signal: &QCheckBox::stateChanged, |
184 | receiver: modifier, slot: &SceneModifier::enableSphere); |
185 | |
186 | torusCB->setChecked(true); |
187 | coneCB->setChecked(true); |
188 | cylinderCB->setChecked(true); |
189 | cuboidCB->setChecked(true); |
190 | planeCB->setChecked(true); |
191 | sphereCB->setChecked(true); |
192 | |
193 | // Show window |
194 | widget->show(); |
195 | widget->resize(w: 1200, h: 800); |
196 | |
197 | return app.exec(); |
198 | } |
199 | |