1/****************************************************************************
2**
3** Copyright (C) 2016 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 "scene.h"
52
53#include <QtCore/QDebug>
54#include <QtCore/QTimer>
55#include <QtCore/QDateTime>
56
57#include <QtGui/QPainter>
58#include <Qt3DRender/QPaintedTextureImage>
59#include <Qt3DRender/QTexture>
60
61#include <Qt3DExtras/QCuboidMesh>
62#include <Qt3DExtras/QDiffuseMapMaterial>
63
64class MyTextureImage : public Qt3DRender::QPaintedTextureImage
65{
66public:
67 MyTextureImage(int w, int h)
68 {
69 setSize(QSize(w, h));
70 }
71
72protected:
73 void paint(QPainter *painter)
74 {
75 int w = painter->device()->width();
76 int h = painter->device()->height();
77
78 painter->setViewport(x: 0, y: h, w, h: -h);
79
80 // clear to white
81 painter->fillRect(x: 0, y: 0, w, h, b: QColor(255, 255, 255));
82
83 // draw some text
84 painter->setPen(QColor(255, 0, 0));
85 painter->setFont(QFont("Times", 30, QFont::Normal, true));
86 painter->drawText(x: w / 8, y: h / 6, s: QDateTime::currentDateTime().toString());
87
88 painter->setPen(QPen(QBrush(QColor(0, 0, 0)) ,10));
89 painter->setBrush(QColor(0, 0, 255));
90
91 // draw some circles
92 for (int i = 0; i < m_circleCount; i++) {
93 float r = w / 8.f;
94 float cw = 0.25 * w + 0.5 * h * (i % 2);
95 float ch = 0.375 * w + 0.375 * h * (i / 2);
96 painter->drawEllipse(center: QPointF(cw, ch), rx: r, ry: r);
97 }
98
99 m_circleCount = (m_circleCount % 4) + 1;
100 }
101
102 int m_circleCount = 1;
103};
104
105Scene::Scene(Qt3DCore::QEntity *rootEntity)
106 : m_rootEntity(rootEntity)
107{
108 // Cuboid shape data
109 Qt3DExtras::QCuboidMesh *cuboid = new Qt3DExtras::QCuboidMesh();
110
111 // CuboidMesh Transform
112 m_transform = new Qt3DCore::QTransform();
113 m_transform->setScale(4.0f);;
114
115 // create texture
116 m_paintedTextureImage = new MyTextureImage(128, 128);
117 m_paintedTextureImage->update();
118
119 Qt3DRender::QTexture2D *tex = new Qt3DRender::QTexture2D();
120 tex->addTextureImage(textureImage: m_paintedTextureImage);
121
122 Qt3DExtras::QDiffuseMapMaterial *mat = new Qt3DExtras::QDiffuseMapMaterial();
123 mat->setAmbient(QColor(64, 64, 64));
124 mat->setSpecular(QColor(255, 255, 255));
125 mat->setDiffuse(tex);
126
127 //Cuboid
128 m_cuboidEntity = new Qt3DCore::QEntity(m_rootEntity);
129 m_cuboidEntity->addComponent(comp: cuboid);
130 m_cuboidEntity->addComponent(comp: mat);
131 m_cuboidEntity->addComponent(comp: m_transform);
132
133 QTimer *timer = new QTimer(this);
134 connect(sender: timer, signal: &QTimer::timeout, receiver: this, slot: &Scene::updateTimer);
135 timer->start(msec: 50);
136}
137
138Scene::~Scene()
139{
140}
141
142void Scene::redrawTexture()
143{
144 m_paintedTextureImage->update();
145}
146
147void Scene::setSize(QSize size)
148{
149 m_paintedTextureImage->setSize(size);
150}
151
152void Scene::updateTimer()
153{
154 m_transform->setRotation(QQuaternion::fromAxisAndAngle(x: 0, y: 1, z: 0, angle: m_angle));
155 m_angle += 0.4f;
156}
157

source code of qt3d/tests/manual/paintedtexture-cpp/scene.cpp