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