1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qsgimagenode.h"
41
42QT_BEGIN_NAMESPACE
43
44/*!
45 \class QSGImageNode
46 \brief The QSGImageNode class is provided for convenience to easily draw
47 textured content using the QML scene graph.
48
49 \inmodule QtQuick
50 \since 5.8
51
52 \warning The image node class must have a texture before being
53 added to the scene graph to be rendered.
54 */
55
56/*!
57 \fn void QSGImageNode::setRect(const QRectF &rect)
58
59 Sets the target rect of this image node to \a rect.
60 */
61
62/*!
63 \fn void QSGImageNode::setRect(qreal x, qreal y, qreal w, qreal h)
64 \overload
65
66 Sets the rectangle of this image node to begin at (\a x, \a y) and have
67 width \a w and height \a h.
68 */
69
70/*!
71 \fn QRectF QSGImageNode::rect() const
72
73 Returns the target rect of this image node.
74 */
75
76/*!
77 \fn void QSGImageNode::setSourceRect(const QRectF &rect)
78
79 Sets the source rect of this image node to \a rect.
80 */
81
82/*!
83 \fn void QSGImageNode::setSourceRect(qreal x, qreal y, qreal w, qreal h)
84 \overload
85
86 Sets the rectangle of this image node to show its texture from (\a x, \a y) and
87 have width \a w and height \a h relatively to the QSGTexture::textureSize.
88 */
89
90/*!
91 \fn QRectF QSGImageNode::sourceRect() const
92
93 Returns the source rect of this image node.
94 */
95
96/*!
97 \fn void QSGImageNode::setTexture(QSGTexture *texture)
98
99 Sets the texture of this image node to \a texture.
100
101 Use setOwnsTexture() to set whether the node should take
102 ownership of the texture. By default, the node does not
103 take ownership.
104
105 \warning An image node must have a texture before being added to the
106 scenegraph to be rendered.
107 */
108
109/*!
110 \fn QSGTexture *QSGImageNode::texture() const
111
112 Returns the texture for this image node.
113 */
114
115/*!
116 \fn void QSGImageNode::setFiltering(QSGTexture::Filtering filtering)
117
118 Sets the filtering to be used for this image node to \a filtering.
119
120 For smooth scaling, use QSGTexture::Linear. For normal scaling, use
121 QSGTexture::Nearest.
122 */
123
124/*!
125 \fn QSGTexture::Filtering QSGImageNode::filtering() const
126
127 Returns the filtering for this image node.
128 */
129
130/*!
131 \fn void QSGImageNode::setMipmapFiltering(QSGTexture::Filtering filtering)
132
133 Sets the mipmap filtering to be used for this image node to \a filtering.
134
135 For smooth scaling between mip maps, use QSGTexture::Linear.
136 For normal scaling, use QSGTexture::Nearest.
137 */
138
139/*!
140 \fn QSGTexture::Filtering QSGImageNode::mipmapFiltering() const
141
142 Returns the mipmap filtering for this image node.
143 */
144
145/*!
146 \enum QSGImageNode::TextureCoordinatesTransformFlag
147
148 The TextureCoordinatesTransformFlag enum is used to specify the mode used
149 to generate texture coordinates for a textured quad.
150
151 \value NoTransform Texture coordinates are oriented with window coordinates
152 i.e. with origin at top-left.
153
154 \value MirrorHorizontally Texture coordinates are inverted in the horizontal axis with
155 respect to window coordinates
156
157 \value MirrorVertically Texture coordinates are inverted in the vertical axis with
158 respect to window coordinates
159 */
160
161/*!
162 \fn void QSGImageNode::setTextureCoordinatesTransform(TextureCoordinatesTransformMode mode)
163
164 Sets the method used to generate texture coordinates to \a mode. This can
165 be used to obtain correct orientation of the texture. This is commonly
166 needed when using a third-party OpenGL library to render to texture as
167 OpenGL has an inverted y-axis relative to Qt Quick.
168 */
169
170/*!
171 \fn QSGImageNode::TextureCoordinatesTransformMode QSGImageNode::textureCoordinatesTransform() const
172
173 Returns the mode used to generate texture coordinates for this node.
174 */
175
176/*!
177 \fn void QSGImageNode::setOwnsTexture(bool owns)
178
179 Sets whether the node takes ownership of the texture to \a owns.
180
181 By default, the node does not take ownership of the texture.
182 */
183
184/*!
185 \fn bool QSGImageNode::ownsTexture() const
186
187 \return \c true if the node takes ownership of the texture; otherwise \c false.
188 */
189
190/*!
191 Updates the geometry \a g with the \a texture, the coordinates
192 in \a rect, and the texture coordinates from \a sourceRect.
193
194 \a g is assumed to be a triangle strip of four vertices of type
195 QSGGeometry::TexturedPoint2D.
196
197 \a texCoordMode is used for normalizing the \a sourceRect.
198 */
199void QSGImageNode::rebuildGeometry(QSGGeometry *g,
200 QSGTexture *texture,
201 const QRectF &rect,
202 QRectF sourceRect,
203 TextureCoordinatesTransformMode texCoordMode)
204{
205 if (!texture)
206 return;
207
208 if (!sourceRect.width() || !sourceRect.height()) {
209 QSize ts = texture->textureSize();
210 sourceRect = QRectF(0, 0, ts.width(), ts.height());
211 }
212
213 // Maybe transform the texture coordinates
214 if (texCoordMode.testFlag(flag: QSGImageNode::MirrorHorizontally)) {
215 float tmp = sourceRect.left();
216 sourceRect.setLeft(sourceRect.right());
217 sourceRect.setRight(tmp);
218 }
219 if (texCoordMode.testFlag(flag: QSGImageNode::MirrorVertically)) {
220 float tmp = sourceRect.top();
221 sourceRect.setTop(sourceRect.bottom());
222 sourceRect.setBottom(tmp);
223 }
224
225 QSGGeometry::updateTexturedRectGeometry(g, rect, sourceRect: texture->convertToNormalizedSourceRect(rect: sourceRect));
226}
227
228QT_END_NAMESPACE
229

source code of qtdeclarative/src/quick/scenegraph/util/qsgimagenode.cpp