1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsgimagenode.h"
5
6QT_BEGIN_NAMESPACE
7
8/*!
9 \class QSGImageNode
10 \brief The QSGImageNode class is provided for convenience to easily draw
11 textured content using the QML scene graph.
12
13 \inmodule QtQuick
14 \since 5.8
15
16 \warning The image node class must have a texture before being
17 added to the scene graph to be rendered.
18 */
19
20/*!
21 \fn void QSGImageNode::setRect(const QRectF &rect)
22
23 Sets the target rect of this image node to \a rect.
24 */
25
26/*!
27 \fn void QSGImageNode::setRect(qreal x, qreal y, qreal w, qreal h)
28 \overload
29
30 Sets the rectangle of this image node to begin at (\a x, \a y) and have
31 width \a w and height \a h.
32 */
33
34/*!
35 \fn QRectF QSGImageNode::rect() const
36
37 Returns the target rect of this image node.
38 */
39
40/*!
41 \fn void QSGImageNode::setSourceRect(const QRectF &rect)
42
43 Sets the source rect of this image node to \a rect.
44 */
45
46/*!
47 \fn void QSGImageNode::setSourceRect(qreal x, qreal y, qreal w, qreal h)
48 \overload
49
50 Sets the rectangle of this image node to show its texture from (\a x, \a y) and
51 have width \a w and height \a h relatively to the QSGTexture::textureSize.
52 */
53
54/*!
55 \fn QRectF QSGImageNode::sourceRect() const
56
57 Returns the source rect of this image node.
58 */
59
60/*!
61 \fn void QSGImageNode::setTexture(QSGTexture *texture)
62
63 Sets the texture of this image node to \a texture.
64
65 Use setOwnsTexture() to set whether the node should take
66 ownership of the texture. By default, the node does not
67 take ownership.
68
69 \warning An image node must have a texture before being added to the
70 scenegraph to be rendered.
71 */
72
73/*!
74 \fn QSGTexture *QSGImageNode::texture() const
75
76 Returns the texture for this image node.
77 */
78
79/*!
80 \fn void QSGImageNode::setFiltering(QSGTexture::Filtering filtering)
81
82 Sets the filtering to be used for this image node to \a filtering.
83
84 For smooth scaling, use QSGTexture::Linear. For normal scaling, use
85 QSGTexture::Nearest.
86 */
87
88/*!
89 \fn QSGTexture::Filtering QSGImageNode::filtering() const
90
91 Returns the filtering for this image node.
92 */
93
94/*!
95 \fn void QSGImageNode::setMipmapFiltering(QSGTexture::Filtering filtering)
96
97 Sets the mipmap filtering to be used for this image node to \a filtering.
98
99 For smooth scaling between mip maps, use QSGTexture::Linear.
100 For normal scaling, use QSGTexture::Nearest.
101 */
102
103/*!
104 \fn QSGTexture::Filtering QSGImageNode::mipmapFiltering() const
105
106 Returns the mipmap filtering for this image node.
107 */
108
109/*!
110 \fn void QSGImageNode::setAnisotropyLevel(QSGTexture::AnisotropyLevel level)
111
112 Sets this image node's anistropy level to \a level.
113*/
114
115/*!
116 \fn QSGTexture::AnisotropyLevel QSGImageNode::anisotropyLevel() const
117
118 Returns this image node's anistropy level.
119*/
120
121/*!
122 \enum QSGImageNode::TextureCoordinatesTransformFlag
123
124 The TextureCoordinatesTransformFlag enum is used to specify the mode used
125 to generate texture coordinates for a textured quad.
126
127 \value NoTransform Texture coordinates are oriented with window coordinates
128 i.e. with origin at top-left.
129
130 \value MirrorHorizontally Texture coordinates are inverted in the horizontal axis with
131 respect to window coordinates
132
133 \value MirrorVertically Texture coordinates are inverted in the vertical axis with
134 respect to window coordinates
135 */
136
137/*!
138 \fn void QSGImageNode::setTextureCoordinatesTransform(TextureCoordinatesTransformMode mode)
139
140 Sets the method used to generate texture coordinates to \a mode. This can
141 be used to obtain correct orientation of the texture. This is commonly
142 needed when using a third-party OpenGL library to render to texture as
143 OpenGL has an inverted y-axis relative to Qt Quick.
144 */
145
146/*!
147 \fn QSGImageNode::TextureCoordinatesTransformMode QSGImageNode::textureCoordinatesTransform() const
148
149 Returns the mode used to generate texture coordinates for this node.
150 */
151
152/*!
153 \fn void QSGImageNode::setOwnsTexture(bool owns)
154
155 Sets whether the node takes ownership of the texture to \a owns.
156
157 By default, the node does not take ownership of the texture.
158 */
159
160/*!
161 \fn bool QSGImageNode::ownsTexture() const
162
163 \return \c true if the node takes ownership of the texture; otherwise \c false.
164 */
165
166/*!
167 Updates the geometry \a g with the \a texture, the coordinates
168 in \a rect, and the texture coordinates from \a sourceRect.
169
170 \a g is assumed to be a triangle strip of four vertices of type
171 QSGGeometry::TexturedPoint2D.
172
173 \a texCoordMode is used for normalizing the \a sourceRect.
174 */
175void QSGImageNode::rebuildGeometry(QSGGeometry *g,
176 QSGTexture *texture,
177 const QRectF &rect,
178 QRectF sourceRect,
179 TextureCoordinatesTransformMode texCoordMode)
180{
181 if (!texture)
182 return;
183
184 if (!sourceRect.width() || !sourceRect.height()) {
185 QSize ts = texture->textureSize();
186 sourceRect = QRectF(0, 0, ts.width(), ts.height());
187 }
188
189 // Maybe transform the texture coordinates
190 if (texCoordMode.testFlag(flag: QSGImageNode::MirrorHorizontally)) {
191 float tmp = sourceRect.left();
192 sourceRect.setLeft(sourceRect.right());
193 sourceRect.setRight(tmp);
194 }
195 if (texCoordMode.testFlag(flag: QSGImageNode::MirrorVertically)) {
196 float tmp = sourceRect.top();
197 sourceRect.setTop(sourceRect.bottom());
198 sourceRect.setBottom(tmp);
199 }
200
201 QSGGeometry::updateTexturedRectGeometry(g, rect, sourceRect: texture->convertToNormalizedSourceRect(rect: sourceRect));
202}
203
204QT_END_NAMESPACE
205

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