1/****************************************************************************
2**
3** Copyright (C) 2019 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 <Qt3DExtras/QCuboidMesh>
58#include <Qt3DExtras/QDiffuseMapMaterial>
59#include <Qt3DRender/QTextureDataUpdate>
60#include <cstring>
61
62
63Scene::Scene(Qt3DCore::QEntity *rootEntity)
64 : m_rootEntity(rootEntity)
65{
66 // Cuboid shape data
67 Qt3DExtras::QCuboidMesh *cuboid = new Qt3DExtras::QCuboidMesh();
68
69 // CuboidMesh Transform
70 m_transform = new Qt3DCore::QTransform();
71 m_transform->setScale(4.0f);;
72
73 // create texture
74 m_texture = new Qt3DRender::QTexture2D();
75 m_texture->setWidth(512);
76 m_texture->setHeight(512);
77 m_texture->setFormat(Qt3DRender::QTexture2D::RGBA8_UNorm);
78 m_texture->setLayers(1);
79 m_texture->setGenerateMipMaps(false);
80 m_texture->setMinificationFilter(Qt3DRender::QTexture2D::Nearest);
81 m_texture->setMagnificationFilter(Qt3DRender::QTexture2D::Nearest);
82
83 Qt3DExtras::QDiffuseMapMaterial *mat = new Qt3DExtras::QDiffuseMapMaterial();
84 mat->setAmbient(QColor(64, 64, 64));
85 mat->setSpecular(QColor(255, 255, 255));
86 mat->setDiffuse(m_texture);
87
88 //Cuboid
89 m_cuboidEntity = new Qt3DCore::QEntity(m_rootEntity);
90 m_cuboidEntity->addComponent(comp: cuboid);
91 m_cuboidEntity->addComponent(comp: mat);
92 m_cuboidEntity->addComponent(comp: m_transform);
93}
94
95Scene::~Scene()
96{
97}
98
99void Scene::run()
100{
101 QTimer *timer = new QTimer(this);
102 connect(sender: timer, signal: &QTimer::timeout, receiver: this, slot: &Scene::updateTimer);
103 timer->start(msec: 50);
104}
105
106void Scene::updateTimer()
107{
108 static int yPos = 0;
109 static uchar pixelValue = 255;
110
111 m_transform->setRotation(QQuaternion::fromAxisAndAngle(x: 0, y: 1, z: 0, angle: m_angle));
112 m_angle += 0.4f;
113
114 QByteArray rawPixelData;
115 const int rows = 32;
116 rawPixelData.resize(size: 512 * 4 * rows * sizeof(uchar));
117 uchar *pixels = reinterpret_cast<uchar *>(rawPixelData.data());
118 std::memset(s: pixels, c: pixelValue, n: 512 * rows * 4 * sizeof(uchar));
119
120 // QTextureImageData hold our raw content and size of subcontent
121 Qt3DRender::QTextureImageDataPtr imageData = Qt3DRender::QTextureImageDataPtr::create();
122 imageData->setWidth(512);
123 imageData->setHeight(rows);
124 imageData->setMipLevels(1);
125 imageData->setLayers(1);
126 // We upload the 4 components of an RGBA pixel as 4 uchar
127 imageData->setPixelFormat(QOpenGLTexture::RGBA);
128 imageData->setPixelType(QOpenGLTexture::UInt8);
129 imageData->setFormat(QOpenGLTexture::RGBA8_UNorm);
130 imageData->setData(data: rawPixelData, blockSize: 4, isCompressed: false);
131
132 // QTextureDataUpdate references our updated content
133 // as well as x, y offsets where content needs to be uploaded
134 Qt3DRender::QTextureDataUpdate update;
135 update.setX(0);
136 update.setY(yPos);
137 update.setMipLevel(0);
138 update.setData(imageData);
139 // Send update to texture
140 m_texture->updateData(update);
141
142 if (yPos + rows >= 512)
143 pixelValue = pixelValue > 0U ? 0U : 255U;
144 yPos = (yPos + rows) % 512;
145
146}
147

source code of qt3d/tests/manual/texture-updates-cpp/scene.cpp