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 <QtCore/QDebug>
SceneModifier::
SceneModifier(Qt3DCore::
QEntity *
rootEntity)
:
m_rootEntity(rootEntity)
{
// Torus shape data
m_torus =
new
Qt3DExtras::
QTorusMesh();
m_torus-&
gt;setRadius(1.0
f);
m_torus-&
gt;setMinorRadius(0.4
f);
m_torus-&
gt;setRings(100
);
m_torus-&
gt;setSlices(20
);
// TorusMesh Transform
Qt3DCore::
QTransform *
torusTransform =
new
Qt3DCore::
QTransform();
torusTransform-&
gt;setScale(2.0
f);
torusTransform-&
gt;setRotation(QQuaternion::
fromAxisAndAngle(QVector3D(0.0
f, 1.0
f, 0.0
f), 25.0
f));
torusTransform-&
gt;setTranslation(QVector3D(5.0
f, 4.0
f, 0.0
f));
Qt3DExtras::
QPhongMaterial *
torusMaterial =
new
Qt3DExtras::
QPhongMaterial();
torusMaterial-&
gt;setDiffuse(QColor(QRgb(0xbeb32b
)));
// Torus
m_torusEntity =
new
Qt3DCore::
QEntity(m_rootEntity);
m_torusEntity-&
gt;addComponent(m_torus);
m_torusEntity-&
gt;addComponent(torusMaterial);
m_torusEntity-&
gt;addComponent(torusTransform);
// Cone shape data
Qt3DExtras::
QConeMesh *
cone =
new
Qt3DExtras::
QConeMesh();
cone-&
gt;setTopRadius(0.5
);
cone-&
gt;setBottomRadius(1
);
cone-&
gt;setLength(3
);
cone-&
gt;setRings(50
);
cone-&
gt;setSlices(20
);
// ConeMesh Transform
Qt3DCore::
QTransform *
coneTransform =
new
Qt3DCore::
QTransform();
coneTransform-&
gt;setScale(1.5
f);
coneTransform-&
gt;setRotation(QQuaternion::
fromAxisAndAngle(QVector3D(1.0
f, 0.0
f, 0.0
f), 45.0
f));
coneTransform-&
gt;setTranslation(QVector3D(0.0
f, 4.0
f, -
1.5
));
Qt3DExtras::
QPhongMaterial *
coneMaterial =
new
Qt3DExtras::
QPhongMaterial();
coneMaterial-&
gt;setDiffuse(QColor(QRgb(0x928327
)));
// Cone
m_coneEntity =
new
Qt3DCore::
QEntity(m_rootEntity);
m_coneEntity-&
gt;addComponent(cone);
m_coneEntity-&
gt;addComponent(coneMaterial);
m_coneEntity-&
gt;addComponent(coneTransform);
// Cylinder shape data
Qt3DExtras::
QCylinderMesh *
cylinder =
new
Qt3DExtras::
QCylinderMesh();
cylinder-&
gt;setRadius(1
);
cylinder-&
gt;setLength(3
);
cylinder-&
gt;setRings(100
);
cylinder-&
gt;setSlices(20
);
// CylinderMesh Transform
Qt3DCore::
QTransform *
cylinderTransform =
new
Qt3DCore::
QTransform();
cylinderTransform-&
gt;setScale(1.5
f);
cylinderTransform-&
gt;setRotation(QQuaternion::
fromAxisAndAngle(QVector3D(1.0
f, 0.0
f, 0.0
f), 45.0
f));
cylinderTransform-&
gt;setTranslation(QVector3D(-
5.0
f, 4.0
f, -
1.5
));
Qt3DExtras::
QPhongMaterial *
cylinderMaterial =
new
Qt3DExtras::
QPhongMaterial();
cylinderMaterial-&
gt;setDiffuse(QColor(QRgb(0x928327
)));
// Cylinder
m_cylinderEntity =
new
Qt3DCore::
QEntity(m_rootEntity);
m_cylinderEntity-&
gt;addComponent(cylinder);
m_cylinderEntity-&
gt;addComponent(cylinderMaterial);
m_cylinderEntity-&
gt;addComponent(cylinderTransform);
// Cuboid shape data
Qt3DExtras::
QCuboidMesh *
cuboid =
new
Qt3DExtras::
QCuboidMesh();
// CuboidMesh Transform
Qt3DCore::
QTransform *
cuboidTransform =
new
Qt3DCore::
QTransform();
cuboidTransform-&
gt;setScale(4.0
f);
cuboidTransform-&
gt;setTranslation(QVector3D(5.0
f, -
4.0
f, 0.0
f));
Qt3DExtras::
QPhongMaterial *
cuboidMaterial =
new
Qt3DExtras::
QPhongMaterial();
cuboidMaterial-&
gt;setDiffuse(QColor(QRgb(0x665423
)));
//Cuboid
m_cuboidEntity =
new
Qt3DCore::
QEntity(m_rootEntity);
m_cuboidEntity-&
gt;addComponent(cuboid);
m_cuboidEntity-&
gt;addComponent(cuboidMaterial);
m_cuboidEntity-&
gt;addComponent(cuboidTransform);
// Plane shape data
Qt3DExtras::
QPlaneMesh *
planeMesh =
new
Qt3DExtras::
QPlaneMesh();
planeMesh-&
gt;setWidth(2
);
planeMesh-&
gt;setHeight(2
);
// Plane mesh transform
Qt3DCore::
QTransform *
planeTransform =
new
Qt3DCore::
QTransform();
planeTransform-&
gt;setScale(1.3
f);
planeTransform-&
gt;setRotation(QQuaternion::
fromAxisAndAngle(QVector3D(1.0
f, 0.0
f, 0.0
f), 45.0
f));
planeTransform-&
gt;setTranslation(QVector3D(0.0
f, -
4.0
f, 0.0
f));
Qt3DExtras::
QPhongMaterial *
planeMaterial =
new
Qt3DExtras::
QPhongMaterial();
planeMaterial-&
gt;setDiffuse(QColor(QRgb(0xa69929
)));
// Plane
m_planeEntity =
new
Qt3DCore::
QEntity(m_rootEntity);
m_planeEntity-&
gt;addComponent(planeMesh);
m_planeEntity-&
gt;addComponent(planeMaterial);
m_planeEntity-&
gt;addComponent(planeTransform);
// Sphere shape data
Qt3DExtras::
QSphereMesh *
sphereMesh =
new
Qt3DExtras::
QSphereMesh();
sphereMesh-&
gt;setRings(20
);
sphereMesh-&
gt;setSlices(20
);
sphereMesh-&
gt;setRadius(2
);
// Sphere mesh transform
Qt3DCore::
QTransform *
sphereTransform =
new
Qt3DCore::
QTransform();
sphereTransform-&
gt;setScale(1.3
f);
sphereTransform-&
gt;setTranslation(QVector3D(-
5.0
f, -
4.0
f, 0.0
f));
Qt3DExtras::
QPhongMaterial *
sphereMaterial =
new
Qt3DExtras::
QPhongMaterial();
sphereMaterial-&
gt;setDiffuse(QColor(QRgb(0xa69929
)));
// Sphere
m_sphereEntity =
new
Qt3DCore::
QEntity(m_rootEntity);
m_sphereEntity-&
gt;addComponent(sphereMesh);
m_sphereEntity-&
gt;addComponent(sphereMaterial);
m_sphereEntity-&
gt;addComponent(sphereTransform);
}
SceneModifier::
~
SceneModifier()
{
}
void
SceneModifier::
enableTorus(bool
enabled)
{
m_torusEntity-&
gt;setEnabled(enabled);
}
void
SceneModifier::
enableCone(bool
enabled)
{
m_coneEntity-&
gt;setEnabled(enabled);
}
void
SceneModifier::
enableCylinder(bool
enabled)
{
m_cylinderEntity-&
gt;setEnabled(enabled);
}
void
SceneModifier::
enableCuboid(bool
enabled)
{
m_cuboidEntity-&
gt;setEnabled(enabled);
}
void
SceneModifier::
enablePlane(bool
enabled)
{
m_planeEntity-&
gt;setEnabled(enabled);
}
void
SceneModifier::
enableSphere(bool
enabled)
{
m_sphereEntity-&
gt;setEnabled(enabled);
}