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