1/****************************************************************************
2**
3** Copyright (C) 2017 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 <QGuiApplication>
52#include <QFontDatabase>
53#include <Qt3DCore/Qt3DCore>
54#include <Qt3DRender/QGeometryRenderer>
55#include <Qt3DExtras/Qt3DExtras>
56#include <Qt3DExtras/QExtrudedTextMesh>
57
58int main(int argc, char *argv[])
59{
60 QGuiApplication a(argc, argv);
61 Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
62 view->setTitle(QStringLiteral("3D Text CPP"));
63 view->defaultFrameGraph()->setClearColor(QColor(210, 210, 220));
64
65 auto *root = new Qt3DCore::QEntity();
66
67 { // plane
68 auto *plane = new Qt3DCore::QEntity(root);
69 auto *planeMesh = new Qt3DExtras::QPlaneMesh();
70 auto *planeTransform = new Qt3DCore::QTransform();
71 auto *planeMaterial = new Qt3DExtras::QPhongMaterial(root);
72 planeMesh->setWidth(10); planeMesh->setHeight(10);
73 planeTransform->setTranslation(QVector3D(0, 0, 0));
74 planeMaterial->setDiffuse(QColor(150, 150, 150));
75
76 plane->addComponent(planeMaterial);
77 plane->addComponent(planeMesh);
78 plane->addComponent(planeTransform);
79 }
80
81 auto *textMaterial = new Qt3DExtras::QPhongMaterial(root);
82 { // text
83 int i = 0;
84#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
85 const QStringList fonts = QFontDatabase::families();
86#else
87 const QStringList fonts = QFontDatabase().families();
88#endif
89
90 for (const QString &family : fonts)
91 {
92 auto *text = new Qt3DCore::QEntity(root);
93 auto *textMesh = new Qt3DExtras::QExtrudedTextMesh();
94
95 auto *textTransform = new Qt3DCore::QTransform();
96 QFont font(family, 32, -1, false);
97 textTransform->setTranslation(QVector3D(-2.45f, i * .5f, 0));
98 textTransform->setScale(.2f);
99 textMesh->setDepth(.45f);
100 textMesh->setFont(font);
101 textMesh->setText(family);
102 textMaterial->setDiffuse(QColor(111, 150, 255));
103
104 text->addComponent(textMaterial);
105 text->addComponent(textMesh);
106 text->addComponent(textTransform);
107
108 i++;
109 }
110 }
111
112 { // camera
113 float aspect = static_cast<float>(view->screen()->size().width()) / view->screen()->size().height();
114 Qt3DRender::QCamera *camera = view->camera();
115 camera->lens()->setPerspectiveProjection(65.f, aspect, 0.1f, 100.f);
116 camera->setPosition(QVector3D(0, 5, 3));
117 camera->setViewCenter(QVector3D(0, 5, 0));
118
119 auto *cameraController = new Qt3DExtras::QOrbitCameraController(root);
120 cameraController->setCamera(camera);
121 }
122
123 view->setRootEntity(root);
124 view->show();
125
126 return a.exec();
127}
128