1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
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 "qplanegeometry.h" |
5 | #include "qplanegeometry_p.h" |
6 | |
7 | #include <Qt3DCore/qattribute.h> |
8 | #include <Qt3DCore/qbuffer.h> |
9 | |
10 | #include <limits> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | using namespace Qt3DCore; |
15 | |
16 | namespace Qt3DExtras { |
17 | |
18 | namespace { |
19 | |
20 | QByteArray (float w, float h, const QSize &resolution, bool mirrored) |
21 | { |
22 | Q_ASSERT(w > 0.0f); |
23 | Q_ASSERT(h > 0.0f); |
24 | Q_ASSERT(resolution.width() >= 2); |
25 | Q_ASSERT(resolution.height() >= 2); |
26 | |
27 | const int nVerts = resolution.width() * resolution.height(); |
28 | |
29 | // Populate a buffer with the interleaved per-vertex data with |
30 | // vec3 pos, vec2 texCoord, vec3 normal, vec4 tangent |
31 | const quint32 elementSize = 3 + 2 + 3 + 4; |
32 | const quint32 stride = elementSize * sizeof(float); |
33 | QByteArray bufferBytes; |
34 | bufferBytes.resize(size: stride * nVerts); |
35 | float* fptr = reinterpret_cast<float*>(bufferBytes.data()); |
36 | |
37 | const float x0 = -w / 2.0f; |
38 | const float z0 = -h / 2.0f; |
39 | const float dx = w / (resolution.width() - 1); |
40 | const float dz = h / (resolution.height() - 1); |
41 | const float du = 1.0 / (resolution.width() - 1); |
42 | const float dv = 1.0 / (resolution.height() - 1); |
43 | |
44 | // Iterate over z |
45 | for (int j = 0; j < resolution.height(); ++j) { |
46 | const float z = z0 + static_cast<float>(j) * dz; |
47 | const float v = static_cast<float>(j) * dv; |
48 | |
49 | // Iterate over x |
50 | for (int i = 0; i < resolution.width(); ++i) { |
51 | const float x = x0 + static_cast<float>(i) * dx; |
52 | const float u = static_cast<float>(i) * du; |
53 | |
54 | // position |
55 | *fptr++ = x; |
56 | *fptr++ = 0.0; |
57 | *fptr++ = z; |
58 | |
59 | // texture coordinates |
60 | *fptr++ = u; |
61 | *fptr++ = mirrored ? 1.0f - v : v; |
62 | |
63 | // normal |
64 | *fptr++ = 0.0f; |
65 | *fptr++ = 1.0f; |
66 | *fptr++ = 0.0f; |
67 | |
68 | // tangent |
69 | *fptr++ = 1.0f; |
70 | *fptr++ = 0.0f; |
71 | *fptr++ = 0.0f; |
72 | *fptr++ = 1.0f; |
73 | } |
74 | } |
75 | |
76 | return bufferBytes; |
77 | } |
78 | |
79 | QByteArray (const QSize &resolution) |
80 | { |
81 | // Create the index data. 2 triangles per rectangular face |
82 | const int faces = 2 * (resolution.width() - 1) * (resolution.height() - 1); |
83 | const qsizetype indices = 3 * faces; |
84 | Q_ASSERT(indices < std::numeric_limits<quint16>::max()); |
85 | QByteArray indexBytes; |
86 | indexBytes.resize(size: indices * sizeof(quint16)); |
87 | quint16* indexPtr = reinterpret_cast<quint16*>(indexBytes.data()); |
88 | |
89 | // Iterate over z |
90 | for (int j = 0; j < resolution.height() - 1; ++j) { |
91 | const int rowStartIndex = j * resolution.width(); |
92 | const int nextRowStartIndex = (j + 1) * resolution.width(); |
93 | |
94 | // Iterate over x |
95 | for (int i = 0; i < resolution.width() - 1; ++i) { |
96 | // Split quad into two triangles |
97 | *indexPtr++ = rowStartIndex + i; |
98 | *indexPtr++ = nextRowStartIndex + i; |
99 | *indexPtr++ = rowStartIndex + i + 1; |
100 | |
101 | *indexPtr++ = nextRowStartIndex + i; |
102 | *indexPtr++ = nextRowStartIndex + i + 1; |
103 | *indexPtr++ = rowStartIndex + i + 1; |
104 | } |
105 | } |
106 | |
107 | return indexBytes; |
108 | } |
109 | |
110 | } // anonymous |
111 | |
112 | /*! |
113 | * \qmltype PlaneGeometry |
114 | * \instantiates Qt3DExtras::QPlaneGeometry |
115 | * \inqmlmodule Qt3D.Extras |
116 | * \brief PlaneGeometry allows creation of a plane in 3D space. |
117 | * |
118 | * The PlaneGeometry type is most commonly used internally by the PlaneMesh type |
119 | * but can also be used in custom GeometryRenderer types. |
120 | */ |
121 | |
122 | /*! |
123 | * \qmlproperty real PlaneGeometry::width |
124 | * |
125 | * Holds the plane width. |
126 | */ |
127 | |
128 | /*! |
129 | * \qmlproperty real PlaneGeometry::height |
130 | * |
131 | * Holds the plane height. |
132 | */ |
133 | |
134 | /*! |
135 | * \qmlproperty size PlaneGeometry::resolution |
136 | * |
137 | * Holds the plane resolution. |
138 | */ |
139 | |
140 | /*! |
141 | * \qmlproperty bool PlaneGeometry::mirrored |
142 | * \since 5.9 |
143 | * |
144 | * Controls if the UV coordinates of the plane should be flipped vertically. |
145 | */ |
146 | |
147 | /*! |
148 | * \qmlproperty Attribute PlaneGeometry::positionAttribute |
149 | * |
150 | * Holds the geometry position attribute. |
151 | */ |
152 | |
153 | /*! |
154 | * \qmlproperty Attribute PlaneGeometry::normalAttribute |
155 | * |
156 | * Holds the geometry normal attribute. |
157 | */ |
158 | |
159 | /*! |
160 | * \qmlproperty Attribute PlaneGeometry::texCoordAttribute |
161 | * |
162 | * Holds the geometry texture coordinate attribute. |
163 | */ |
164 | |
165 | /*! |
166 | * \qmlproperty Attribute PlaneGeometry::tangentAttribute |
167 | * |
168 | * Holds the geometry tangent attribute. |
169 | */ |
170 | |
171 | /*! |
172 | * \qmlproperty Attribute PlaneGeometry::indexAttribute |
173 | * |
174 | * Holds the geometry index attribute. |
175 | */ |
176 | |
177 | /*! |
178 | * \class Qt3DExtras::QPlaneGeometry |
179 | * \ingroup qt3d-extras-geometries |
180 | * \inheaderfile Qt3DExtras/QPlaneGeometry |
181 | * \inmodule Qt3DExtras |
182 | * \brief The QPlaneGeometry class allows creation of a plane in 3D space. |
183 | * \since 5.7 |
184 | * \ingroup geometries |
185 | * |
186 | * The QPlaneGeometry class is most commonly used internally by the QPlaneMesh |
187 | * but can also be used in custom Qt3DRender::QGeometryRenderer subclasses. |
188 | */ |
189 | |
190 | /*! |
191 | * \fn Qt3DExtras::QPlaneGeometry::QPlaneGeometry(QNode *parent) |
192 | * |
193 | * Constructs a new QPlaneGeometry with \a parent. |
194 | */ |
195 | QPlaneGeometry::(QPlaneGeometry::QNode *parent) |
196 | : QGeometry(*new QPlaneGeometryPrivate(), parent) |
197 | { |
198 | Q_D(QPlaneGeometry); |
199 | d->init(); |
200 | } |
201 | |
202 | /*! |
203 | * \internal |
204 | */ |
205 | QPlaneGeometry::(QPlaneGeometryPrivate &dd, QNode *parent) |
206 | : QGeometry(dd, parent) |
207 | { |
208 | Q_D(QPlaneGeometry); |
209 | d->init(); |
210 | } |
211 | |
212 | /*! |
213 | * \internal |
214 | */ |
215 | QPlaneGeometry::() |
216 | { |
217 | } |
218 | |
219 | /*! |
220 | * Updates vertices based on mesh resolution, width, and height properties. |
221 | */ |
222 | void QPlaneGeometry::() |
223 | { |
224 | Q_D(QPlaneGeometry); |
225 | const int nVerts = d->m_meshResolution.width() * d->m_meshResolution.height(); |
226 | |
227 | d->m_positionAttribute->setCount(nVerts); |
228 | d->m_normalAttribute->setCount(nVerts); |
229 | d->m_texCoordAttribute->setCount(nVerts); |
230 | d->m_tangentAttribute->setCount(nVerts); |
231 | d->m_vertexBuffer->setData(d->generateVertexData()); |
232 | } |
233 | |
234 | /*! |
235 | * Updates indices based on mesh resolution. |
236 | */ |
237 | void QPlaneGeometry::() |
238 | { |
239 | Q_D(QPlaneGeometry); |
240 | const int faces = 2 * (d->m_meshResolution.width() - 1) * (d->m_meshResolution.height() - 1); |
241 | // Each primitive has 3 vertices |
242 | d->m_indexAttribute->setCount(faces * 3); |
243 | d->m_indexBuffer->setData(d->generateIndexData()); |
244 | |
245 | } |
246 | |
247 | void QPlaneGeometry::(const QSize &resolution) |
248 | { |
249 | Q_D(QPlaneGeometry); |
250 | if (d->m_meshResolution == resolution) |
251 | return; |
252 | d->m_meshResolution = resolution; |
253 | updateVertices(); |
254 | updateIndices(); |
255 | emit resolutionChanged(resolution); |
256 | } |
257 | |
258 | void QPlaneGeometry::(float width) |
259 | { |
260 | Q_D(QPlaneGeometry); |
261 | if (width == d->m_width) |
262 | return; |
263 | d->m_width = width; |
264 | updateVertices(); |
265 | emit widthChanged(width); |
266 | } |
267 | |
268 | void QPlaneGeometry::(float height) |
269 | { |
270 | Q_D(QPlaneGeometry); |
271 | if (height == d->m_height) |
272 | return; |
273 | d->m_height = height; |
274 | updateVertices(); |
275 | emit heightChanged(height); |
276 | } |
277 | |
278 | void QPlaneGeometry::(bool mirrored) |
279 | { |
280 | Q_D(QPlaneGeometry); |
281 | if (mirrored == d->m_mirrored) |
282 | return; |
283 | d->m_mirrored = mirrored; |
284 | updateVertices(); |
285 | emit mirroredChanged(mirrored); |
286 | } |
287 | |
288 | /*! |
289 | * \property QPlaneGeometry::resolution |
290 | * |
291 | * Holds the plane resolution. |
292 | */ |
293 | QSize QPlaneGeometry::() const |
294 | { |
295 | Q_D(const QPlaneGeometry); |
296 | return d->m_meshResolution; |
297 | } |
298 | |
299 | /*! |
300 | * \property QPlaneGeometry::width |
301 | * |
302 | * Holds the plane width. |
303 | */ |
304 | float QPlaneGeometry::() const |
305 | { |
306 | Q_D(const QPlaneGeometry); |
307 | return d->m_width; |
308 | } |
309 | |
310 | /*! |
311 | * \property QPlaneGeometry::height |
312 | * |
313 | * Holds the plane height. |
314 | */ |
315 | float QPlaneGeometry::() const |
316 | { |
317 | Q_D(const QPlaneGeometry); |
318 | return d->m_height; |
319 | } |
320 | |
321 | /*! |
322 | * \property QPlaneGeometry::mirrored |
323 | * \since 5.9 |
324 | * |
325 | * Controls if the UV coordinates of the plane should be flipped vertically. |
326 | */ |
327 | bool QPlaneGeometry::() const |
328 | { |
329 | Q_D(const QPlaneGeometry); |
330 | return d->m_mirrored; |
331 | } |
332 | |
333 | /*! |
334 | * \property QPlaneGeometry::positionAttribute |
335 | * |
336 | * Holds the geometry position attribute. |
337 | */ |
338 | QAttribute *QPlaneGeometry::() const |
339 | { |
340 | Q_D(const QPlaneGeometry); |
341 | return d->m_positionAttribute; |
342 | } |
343 | |
344 | /*! |
345 | * \property QPlaneGeometry::normalAttribute |
346 | * |
347 | * Holds the geometry normal attribute. |
348 | */ |
349 | QAttribute *QPlaneGeometry::() const |
350 | { |
351 | Q_D(const QPlaneGeometry); |
352 | return d->m_normalAttribute; |
353 | } |
354 | |
355 | /*! |
356 | * \property QPlaneGeometry::texCoordAttribute |
357 | * |
358 | * Holds the geometry texture coordinate attribute. |
359 | */ |
360 | QAttribute *QPlaneGeometry::() const |
361 | { |
362 | Q_D(const QPlaneGeometry); |
363 | return d->m_texCoordAttribute; |
364 | } |
365 | |
366 | /*! |
367 | * \property QPlaneGeometry::tangentAttribute |
368 | * |
369 | * Holds the geometry tangent attribute. |
370 | */ |
371 | QAttribute *QPlaneGeometry::() const |
372 | { |
373 | Q_D(const QPlaneGeometry); |
374 | return d->m_tangentAttribute; |
375 | } |
376 | |
377 | /*! |
378 | * \property QPlaneGeometry::indexAttribute |
379 | * |
380 | * Holds the geometry index attribute. |
381 | */ |
382 | QAttribute *QPlaneGeometry::() const |
383 | { |
384 | Q_D(const QPlaneGeometry); |
385 | return d->m_indexAttribute; |
386 | } |
387 | |
388 | QPlaneGeometryPrivate::() |
389 | : QGeometryPrivate() |
390 | , m_width(1.0f) |
391 | , m_height(1.0f) |
392 | , m_meshResolution(QSize(2, 2)) |
393 | , m_mirrored(false) |
394 | , m_positionAttribute(nullptr) |
395 | , m_normalAttribute(nullptr) |
396 | , m_texCoordAttribute(nullptr) |
397 | , m_tangentAttribute(nullptr) |
398 | , m_indexAttribute(nullptr) |
399 | , m_vertexBuffer(nullptr) |
400 | , m_indexBuffer(nullptr) |
401 | { |
402 | } |
403 | |
404 | void QPlaneGeometryPrivate::() |
405 | { |
406 | Q_Q(QPlaneGeometry); |
407 | m_positionAttribute = new QAttribute(q); |
408 | m_normalAttribute = new QAttribute(q); |
409 | m_texCoordAttribute = new QAttribute(q); |
410 | m_tangentAttribute = new QAttribute(q); |
411 | m_indexAttribute = new QAttribute(q); |
412 | m_vertexBuffer = new Qt3DCore::QBuffer(q); |
413 | m_indexBuffer = new Qt3DCore::QBuffer(q); |
414 | |
415 | const int nVerts = m_meshResolution.width() * m_meshResolution.height(); |
416 | const int stride = (3 + 2 + 3 + 4) * sizeof(float); |
417 | const int faces = 2 * (m_meshResolution.width() - 1) * (m_meshResolution.height() - 1); |
418 | |
419 | m_positionAttribute->setName(QAttribute::defaultPositionAttributeName()); |
420 | m_positionAttribute->setVertexBaseType(QAttribute::Float); |
421 | m_positionAttribute->setVertexSize(3); |
422 | m_positionAttribute->setAttributeType(QAttribute::VertexAttribute); |
423 | m_positionAttribute->setBuffer(m_vertexBuffer); |
424 | m_positionAttribute->setByteStride(stride); |
425 | m_positionAttribute->setCount(nVerts); |
426 | |
427 | m_texCoordAttribute->setName(QAttribute::defaultTextureCoordinateAttributeName()); |
428 | m_texCoordAttribute->setVertexBaseType(QAttribute::Float); |
429 | m_texCoordAttribute->setVertexSize(2); |
430 | m_texCoordAttribute->setAttributeType(QAttribute::VertexAttribute); |
431 | m_texCoordAttribute->setBuffer(m_vertexBuffer); |
432 | m_texCoordAttribute->setByteStride(stride); |
433 | m_texCoordAttribute->setByteOffset(3 * sizeof(float)); |
434 | m_texCoordAttribute->setCount(nVerts); |
435 | |
436 | m_normalAttribute->setName(QAttribute::defaultNormalAttributeName()); |
437 | m_normalAttribute->setVertexBaseType(QAttribute::Float); |
438 | m_normalAttribute->setVertexSize(3); |
439 | m_normalAttribute->setAttributeType(QAttribute::VertexAttribute); |
440 | m_normalAttribute->setBuffer(m_vertexBuffer); |
441 | m_normalAttribute->setByteStride(stride); |
442 | m_normalAttribute->setByteOffset(5 * sizeof(float)); |
443 | m_normalAttribute->setCount(nVerts); |
444 | |
445 | m_tangentAttribute->setName(QAttribute::defaultTangentAttributeName()); |
446 | m_tangentAttribute->setVertexBaseType(QAttribute::Float); |
447 | m_tangentAttribute->setVertexSize(4); |
448 | m_tangentAttribute->setAttributeType(QAttribute::VertexAttribute); |
449 | m_tangentAttribute->setBuffer(m_vertexBuffer); |
450 | m_tangentAttribute->setByteStride(stride); |
451 | m_tangentAttribute->setByteOffset(8 * sizeof(float)); |
452 | m_tangentAttribute->setCount(nVerts); |
453 | |
454 | m_indexAttribute->setAttributeType(QAttribute::IndexAttribute); |
455 | m_indexAttribute->setVertexBaseType(QAttribute::UnsignedShort); |
456 | m_indexAttribute->setBuffer(m_indexBuffer); |
457 | |
458 | // Each primitive has 3 vertives |
459 | m_indexAttribute->setCount(faces * 3); |
460 | |
461 | m_vertexBuffer->setData(generateVertexData()); |
462 | m_indexBuffer->setData(generateIndexData()); |
463 | |
464 | q->addAttribute(attribute: m_positionAttribute); |
465 | q->addAttribute(attribute: m_texCoordAttribute); |
466 | q->addAttribute(attribute: m_normalAttribute); |
467 | q->addAttribute(attribute: m_tangentAttribute); |
468 | q->addAttribute(attribute: m_indexAttribute); |
469 | } |
470 | |
471 | QByteArray QPlaneGeometryPrivate::() const |
472 | { |
473 | return createPlaneVertexData(w: m_width, h: m_height, resolution: m_meshResolution, mirrored: m_mirrored); |
474 | } |
475 | |
476 | QByteArray QPlaneGeometryPrivate::() const |
477 | { |
478 | return createPlaneIndexData(resolution: m_meshResolution); |
479 | } |
480 | |
481 | } // Qt3DExtras |
482 | |
483 | QT_END_NAMESPACE |
484 | |
485 | #include "moc_qplanegeometry.cpp" |
486 | |