Qt 3D: Basic Shapes C++ Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"scenemodifier.h"
#include <QGuiApplication>
#include <Qt3DRender/qcamera.h>
#include <Qt3DCore/qentity.h>
#include <Qt3DRender/qcameralens.h>
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QCommandLinkButton>
#include <QtGui/QScreen>
#include <Qt3DInput/QInputAspect>
#include <Qt3DExtras/qtorusmesh.h>
#include <Qt3DRender/qmesh.h>
#include <Qt3DRender/qtechnique.h>
#include <Qt3DRender/qmaterial.h>
#include <Qt3DRender/qeffect.h>
#include <Qt3DRender/qtexture.h>
#include <Qt3DRender/qrenderpass.h>
#include <Qt3DRender/qsceneloader.h>
#include <Qt3DRender/qpointlight.h>
#include <Qt3DCore/qtransform.h>
#include <Qt3DCore/qaspectengine.h>
#include <Qt3DRender/qrenderaspect.h>
#include <Qt3DExtras/qforwardrenderer.h>
#include <Qt3DExtras/qt3dwindow.h>
#include <Qt3DExtras/qfirstpersoncameracontroller.h>
int
main(int
argc, char
**
argv)
{
QApplication app(argc, argv);
Qt3DExtras::
Qt3DWindow *
view =
new
Qt3DExtras::
Qt3DWindow();
view-&
gt;defaultFrameGraph()-&
gt;setClearColor(QColor(QRgb(0x4d4d4f
)));
QWidget *
container =
QWidget::
createWindowContainer(view);
QSize screenSize =
view-&
gt;screen()-&
gt;size();
container-&
gt;setMinimumSize(QSize(200
, 100
));
container-&
gt;setMaximumSize(screenSize);
QWidget *
widget =
new
QWidget;
QHBoxLayout *
hLayout =
new
QHBoxLayout(widget);
QVBoxLayout *
vLayout =
new
QVBoxLayout();
vLayout-&
gt;setAlignment(Qt::
AlignTop);
hLayout-&
gt;addWidget(container, 1
);
hLayout-&
gt;addLayout(vLayout);
widget-&
gt;setWindowTitle(QStringLiteral("Basic shapes"
));
Qt3DInput::
QInputAspect *
input =
new
Qt3DInput::
QInputAspect;
view-&
gt;registerAspect(input);
// Root entity
Qt3DCore::
QEntity *
rootEntity =
new
Qt3DCore::
QEntity();
// Camera
Qt3DRender::
QCamera *
cameraEntity =
view-&
gt;camera();
cameraEntity-&
gt;lens()-&
gt;setPerspectiveProjection(45.0
f, 16.0
f/
9.0
f, 0.1
f, 1000.0
f);
cameraEntity-&
gt;setPosition(QVector3D(0
, 0
, 20.0
f));
cameraEntity-&
gt;setUpVector(QVector3D(0
, 1
, 0
));
cameraEntity-&
gt;setViewCenter(QVector3D(0
, 0
, 0
));
Qt3DCore::
QEntity *
lightEntity =
new
Qt3DCore::
QEntity(rootEntity);
Qt3DRender::
QPointLight *
light =
new
Qt3DRender::
QPointLight(lightEntity);
light-&
gt;setColor("white"
);
light-&
gt;setIntensity(1
);
lightEntity-&
gt;addComponent(light);
Qt3DCore::
QTransform *
lightTransform =
new
Qt3DCore::
QTransform(lightEntity);
lightTransform-&
gt;setTranslation(cameraEntity-&
gt;position());
lightEntity-&
gt;addComponent(lightTransform);
// For camera controls
Qt3DExtras::
QFirstPersonCameraController *
camController =
new
Qt3DExtras::
QFirstPersonCameraController(rootEntity);
camController-&
gt;setCamera(cameraEntity);
// Scenemodifier
SceneModifier *
modifier =
new
SceneModifier(rootEntity);
// Set root object of the scene
view-&
gt;setRootEntity(rootEntity);
// Create control widgets
QCommandLinkButton *
info =
new
QCommandLinkButton();
info-&
gt;setText(QStringLiteral("Qt3D ready-made meshes"
));
info-&
gt;setDescription(QString::
fromLatin1("Qt3D provides several ready-made meshes, like torus, cylinder, cone, "
"cube, plane and sphere."
));
info-&
gt;setIconSize(QSize(0
,0
));
QCheckBox *
torusCB =
new
QCheckBox(widget);
torusCB-&
gt;setChecked(true
);
torusCB-&
gt;setText(QStringLiteral("Torus"
));
QCheckBox *
coneCB =
new
QCheckBox(widget);
coneCB-&
gt;setChecked(true
);
coneCB-&
gt;setText(QStringLiteral("Cone"
));
QCheckBox *
cylinderCB =
new
QCheckBox(widget);
cylinderCB-&
gt;setChecked(true
);
cylinderCB-&
gt;setText(QStringLiteral("Cylinder"
));
QCheckBox *
cuboidCB =
new
QCheckBox(widget);
cuboidCB-&
gt;setChecked(true
);
cuboidCB-&
gt;setText(QStringLiteral("Cuboid"
));
QCheckBox *
planeCB =
new
QCheckBox(widget);
planeCB-&
gt;setChecked(true
);
planeCB-&
gt;setText(QStringLiteral("Plane"
));
QCheckBox *
sphereCB =
new
QCheckBox(widget);
sphereCB-&
gt;setChecked(true
);
sphereCB-&
gt;setText(QStringLiteral("Sphere"
));
vLayout-&
gt;addWidget(info);
vLayout-&
gt;addWidget(torusCB);
vLayout-&
gt;addWidget(coneCB);
vLayout-&
gt;addWidget(cylinderCB);
vLayout-&
gt;addWidget(cuboidCB);
vLayout-&
gt;addWidget(planeCB);
vLayout-&
gt;addWidget(sphereCB);
QObject::
connect(torusCB, &
amp;QCheckBox::
stateChanged,
modifier, &
amp;SceneModifier::
enableTorus);
QObject::
connect(coneCB, &
amp;QCheckBox::
stateChanged,
modifier, &
amp;SceneModifier::
enableCone);
QObject::
connect(cylinderCB, &
amp;QCheckBox::
stateChanged,
modifier, &
amp;SceneModifier::
enableCylinder);
QObject::
connect(cuboidCB, &
amp;QCheckBox::
stateChanged,
modifier, &
amp;SceneModifier::
enableCuboid);
QObject::
connect(planeCB, &
amp;QCheckBox::
stateChanged,
modifier, &
amp;SceneModifier::
enablePlane);
QObject::
connect(sphereCB, &
amp;QCheckBox::
stateChanged,
modifier, &
amp;SceneModifier::
enableSphere);
torusCB-&
gt;setChecked(true
);
coneCB-&
gt;setChecked(true
);
cylinderCB-&
gt;setChecked(true
);
cuboidCB-&
gt;setChecked(true
);
planeCB-&
gt;setChecked(true
);
sphereCB-&
gt;setChecked(true
);
// Show window
widget-&
gt;show();
widget-&
gt;resize(1200
, 800
);
return
app.exec();
}