Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
gridgeometry.cpp
Go to the documentation of this file.
1
// Copyright (C) 2019 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
6
#
include
"gridgeometry_p.h"
7
#
include
<
QtQuick3DRuntimeRender
/
private
/
qssgrendergeometry_p
.
h
>
8
9
/*!
10
\qmltype GridGeometry
11
\inqmlmodule QtQuick3D.Helpers
12
\inherits Geometry
13
\brief A custom geometry provider for rendering grids.
14
15
This helper implements grid geometry, which allows showing a grid in a scene.
16
17
For example, the following snippet would display a grid with 19 cells in
18
both directions in a scene that has one light. Without further
19
transformations, the grid is facing the camera by default.
20
21
\image gridgeometry.jpg
22
{19 by 19 cell grid facing camera with directional lighting}
23
24
\badcode
25
View3D {
26
anchors.fill: parent
27
camera: camera
28
29
PerspectiveCamera {
30
id: camera
31
position: Qt.vector3d(0, 0, 600)
32
}
33
34
DirectionalLight {
35
position: Qt.vector3d(-500, 500, -100)
36
color: Qt.rgba(0.4, 0.2, 0.6, 1.0)
37
ambientColor: Qt.rgba(0.1, 0.1, 0.1, 1.0)
38
}
39
40
Model {
41
scale: Qt.vector3d(100, 100, 100)
42
geometry: GridGeometry {
43
horizontalLines: 20
44
verticalLines: 20
45
}
46
materials: [ DefaultMaterial { } ]
47
}
48
}
49
\endcode
50
51
\sa {Qt Quick 3D - Custom Geometry Example}, Model
52
*/
53
54
/*! \qmlproperty int GridGeometry::horizontalLines
55
Specifies the number of horizontal lines in a grid. The default value is 1000.
56
*/
57
/*! \qmlproperty int GridGeometry::verticalLines
58
Specifies the number of vertical lines in a grid. The default value is 1000.
59
*/
60
/*! \qmlproperty real GridGeometry::horizontalStep
61
Specifies the spacing between horizontal lines. The default value is 0.1.
62
*/
63
/*! \qmlproperty real GridGeometry::verticalStep
64
Specifies the spacing between vertical lines. The default value is 0.1.
65
*/
66
67
GridGeometry::GridGeometry()
68
: QQuick3DGeometry()
69
{
70
updateData();
71
}
72
73
GridGeometry::~GridGeometry()
74
{
75
76
}
77
78
int
GridGeometry::horizontalLines()
const
79
{
80
return
m_horLines;
81
}
82
83
int
GridGeometry::verticalLines()
const
84
{
85
return
m_vertLines;
86
}
87
88
float
GridGeometry::horizontalStep()
const
89
{
90
return
m_horStep;
91
}
92
93
float
GridGeometry::verticalStep()
const
94
{
95
return
m_vertStep;
96
}
97
98
void
GridGeometry::setHorizontalLines(
int
count)
99
{
100
count = qMax(count, 1);
101
if
(m_horLines == count)
102
return
;
103
m_horLines = qMax(count, 1);
104
emit horizontalLinesChanged();
105
updateData();
106
update();
107
emit geometryChanged();
108
}
109
110
void
GridGeometry::setVerticalLines(
int
count)
111
{
112
count = qMax(count, 1);
113
if
(m_vertLines == count)
114
return
;
115
m_vertLines = qMax(count, 1);
116
emit verticalLinesChanged();
117
updateData();
118
update();
119
emit geometryChanged();
120
}
121
122
void
GridGeometry::setHorizontalStep(
float
step)
123
{
124
step = qMax(step, 0.0f);
125
if
(qFuzzyCompare(m_horStep, step))
126
return
;
127
m_horStep = step;
128
emit horizontalStepChanged();
129
updateData();
130
update();
131
emit geometryChanged();
132
}
133
134
void
GridGeometry::setVerticalStep(
float
step)
135
{
136
step = qMax(step, 0.0f);
137
if
(qFuzzyCompare(m_vertStep, step))
138
return
;
139
m_vertStep = step;
140
emit verticalStepChanged();
141
updateData();
142
update();
143
emit geometryChanged();
144
}
145
146
static
void
fillVertexData
(QByteArray &vertexData,
int
horLines,
float
horStep,
147
int
vertLines,
float
vertStep)
148
{
149
const
int
size = (horLines + vertLines) *
sizeof
(
float
) * 8 * 2;
150
vertexData.resize(size);
151
float
*dataPtr =
reinterpret_cast
<
float
*>(vertexData.data());
152
153
float
y0 = -
float
(horLines - 1) * horStep * .5f;
154
float
x0 = -
float
(vertLines - 1) * vertStep * .5f;
155
float
y1 = -y0;
156
float
x1 = -x0;
157
158
for
(
int
i = 0; i < horLines; ++i) {
159
// start position
160
dataPtr[0] = x0;
161
dataPtr[1] = y0 + i * horStep;
162
dataPtr[2] = .0f;
163
dataPtr[3] = 1.f;
164
165
dataPtr[4] = 0;
166
dataPtr[5] = 0;
167
dataPtr[6] = 1.f;
168
dataPtr[7] = 0;
169
170
dataPtr += 8;
171
172
// end position
173
dataPtr[0] = x1;
174
dataPtr[1] = y0 + i * horStep;
175
dataPtr[2] = .0f;
176
dataPtr[3] = 1.f;
177
178
dataPtr[4] = 0;
179
dataPtr[5] = 0;
180
dataPtr[6] = 1.f;
181
dataPtr[7] = 0;
182
183
dataPtr += 8;
184
}
185
186
for
(
int
i = 0; i < vertLines; ++i) {
187
// start position
188
dataPtr[0] = x0 + i * vertStep;
189
dataPtr[1] = y0;
190
dataPtr[2] = .0f;
191
dataPtr[3] = 1.f;
192
193
dataPtr[4] = 0;
194
dataPtr[5] = 0;
195
dataPtr[6] = 1.f;
196
dataPtr[7] = 0;
197
198
dataPtr += 8;
199
200
// end position
201
dataPtr[0] = x0 + i * vertStep;
202
dataPtr[1] = y1;
203
dataPtr[2] = .0f;
204
dataPtr[3] = 1.f;
205
206
dataPtr[4] = 0;
207
dataPtr[5] = 0;
208
dataPtr[6] = 1.f;
209
dataPtr[7] = 0;
210
211
dataPtr += 8;
212
}
213
}
214
215
void
GridGeometry::updateData()
216
{
217
QByteArray vertexData;
218
fillVertexData(vertexData, m_horLines, m_horStep, m_vertLines, m_vertStep);
219
clear();
220
addAttribute(QQuick3DGeometry::Attribute::PositionSemantic, 0,
221
QQuick3DGeometry::Attribute::ComponentType::F32Type);
222
addAttribute(QQuick3DGeometry::Attribute::NormalSemantic, 16,
223
QQuick3DGeometry::Attribute::ComponentType::F32Type);
224
setStride(32);
225
setVertexData(vertexData);
226
setPrimitiveType(QQuick3DGeometry::PrimitiveType::Lines);
227
setBounds(QVector3D(
float
(-m_vertLines / 2),
float
(-m_horLines / 2), 0.0f) * m_horStep,
228
QVector3D(
float
(m_vertLines / 2),
float
(m_horLines / 2), 0.0f) * m_vertStep);
229
}
fillVertexData
static void fillVertexData(QByteArray &vertexData, int horLines, float horStep, int vertLines, float vertStep)
Definition
gridgeometry.cpp:146
qtquick3d
src
helpers
gridgeometry.cpp
Generated on
for Qt by
1.18.0