| 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 "qcuboidgeometry.h" |
| 5 | #include "qcuboidgeometry_p.h" |
| 6 | |
| 7 | #include <Qt3DCore/qattribute.h> |
| 8 | #include <Qt3DCore/qbuffer.h> |
| 9 | #include <Qt3DCore/private/corelogging_p.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | |
| 14 | namespace Qt3DExtras { |
| 15 | |
| 16 | using namespace Qt3DCore; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | enum { |
| 21 | , |
| 22 | , |
| 23 | , |
| 24 | , |
| 25 | , |
| 26 | |
| 27 | }; |
| 28 | |
| 29 | void (float w, float h, const QSize &resolution, |
| 30 | PlaneNormal normal, float planeDistance, |
| 31 | float *vertices) |
| 32 | { |
| 33 | const float a0 = -w / 2.0f; |
| 34 | const float b0 = -h / 2.0f; |
| 35 | const float da = w / (resolution.width() - 1); |
| 36 | const float db = h / (resolution.height() - 1); |
| 37 | const float du = 1.0f / (resolution.width() - 1); |
| 38 | const float dv = 1.0f / (resolution.height() - 1); |
| 39 | |
| 40 | switch (normal) { |
| 41 | case NegativeX: |
| 42 | // Iterate over z |
| 43 | for (int j = resolution.height() - 1; j >= 0; --j) { |
| 44 | const float b = b0 + static_cast<float>(j) * db; |
| 45 | const float v = static_cast<float>(j) * dv; |
| 46 | |
| 47 | // Iterate over y |
| 48 | for (int i = 0; i < resolution.width(); ++i) { |
| 49 | const float a = a0 + static_cast<float>(i) * da; |
| 50 | const float u = static_cast<float>(i) * du; |
| 51 | |
| 52 | // position |
| 53 | *vertices++ = planeDistance; |
| 54 | *vertices++ = a; |
| 55 | *vertices++ = b; |
| 56 | |
| 57 | // texture coordinates |
| 58 | *vertices++ = v; |
| 59 | *vertices++ = u; |
| 60 | |
| 61 | // normal |
| 62 | *vertices++ = -1.0f; |
| 63 | *vertices++ = 0.0f; |
| 64 | *vertices++ = 0.0f; |
| 65 | |
| 66 | // tangent |
| 67 | *vertices++ = 0.0f; |
| 68 | *vertices++ = 0.0f; |
| 69 | *vertices++ = 1.0f; |
| 70 | *vertices++ = 1.0f; |
| 71 | } |
| 72 | } |
| 73 | break; |
| 74 | |
| 75 | case PositiveX: { |
| 76 | // Iterate over z |
| 77 | for (int j = 0; j < resolution.height(); ++j) { |
| 78 | const float b = b0 + static_cast<float>(j) * db; |
| 79 | const float v = static_cast<float>(j) * dv; |
| 80 | |
| 81 | // Iterate over y |
| 82 | for (int i = 0; i < resolution.width(); ++i) { |
| 83 | const float a = a0 + static_cast<float>(i) * da; |
| 84 | const float u = static_cast<float>(i) * du; |
| 85 | |
| 86 | // position |
| 87 | *vertices++ = planeDistance; |
| 88 | *vertices++ = a; |
| 89 | *vertices++ = b; |
| 90 | |
| 91 | // texture coordinates |
| 92 | *vertices++ = 1.0f - v; |
| 93 | *vertices++ = u; |
| 94 | |
| 95 | // normal |
| 96 | *vertices++ = 1.0f; |
| 97 | *vertices++ = 0.0f; |
| 98 | *vertices++ = 0.0f; |
| 99 | |
| 100 | // tangent |
| 101 | *vertices++ = 0.0f; |
| 102 | *vertices++ = 0.0f; |
| 103 | *vertices++ = -1.0f; |
| 104 | *vertices++ = 1.0f; |
| 105 | } |
| 106 | } |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | case NegativeY: |
| 111 | // Iterate over z |
| 112 | for (int j = 0; j < resolution.height(); ++j) { |
| 113 | const float b = b0 + static_cast<float>(j) * db; |
| 114 | const float v = static_cast<float>(j) * dv; |
| 115 | |
| 116 | // Iterate over x |
| 117 | // This iterates in the opposite sense to the other directions |
| 118 | // so that the winding order is correct |
| 119 | for (int i = 0; i < resolution.width(); ++i) { |
| 120 | const float a = a0 + static_cast<float>(i) * da; |
| 121 | const float u = static_cast<float>(i) * du; |
| 122 | |
| 123 | // position |
| 124 | *vertices++ = a; |
| 125 | *vertices++ = planeDistance; |
| 126 | *vertices++ = b; |
| 127 | |
| 128 | // texture coordinates |
| 129 | *vertices++ = u; |
| 130 | *vertices++ = v; |
| 131 | |
| 132 | // normal |
| 133 | *vertices++ = 0.0f; |
| 134 | *vertices++ = -1.0f; |
| 135 | *vertices++ = 0.0f; |
| 136 | |
| 137 | // tangent |
| 138 | *vertices++ = 1.0f; |
| 139 | *vertices++ = 0.0f; |
| 140 | *vertices++ = 0.0f; |
| 141 | *vertices++ = 1.0f; |
| 142 | } |
| 143 | } |
| 144 | break; |
| 145 | |
| 146 | case PositiveY: { |
| 147 | // Iterate over z |
| 148 | for (int j = resolution.height() - 1; j >= 0; --j) { |
| 149 | const float b = b0 + static_cast<float>(j) * db; |
| 150 | const float v = static_cast<float>(j) * dv; |
| 151 | |
| 152 | // Iterate over x |
| 153 | // This iterates in the opposite sense to the other directions |
| 154 | // so that the winding order is correct |
| 155 | for (int i = 0; i < resolution.width(); ++i) { |
| 156 | const float a = a0 + static_cast<float>(i) * da; |
| 157 | const float u = static_cast<float>(i) * du; |
| 158 | |
| 159 | // position |
| 160 | *vertices++ = a; |
| 161 | *vertices++ = planeDistance; |
| 162 | *vertices++ = b; |
| 163 | |
| 164 | // texture coordinates |
| 165 | *vertices++ = u; |
| 166 | *vertices++ = 1.0f - v; |
| 167 | |
| 168 | // normal |
| 169 | *vertices++ = 0.0f; |
| 170 | *vertices++ = 1.0f; |
| 171 | *vertices++ = 0.0f; |
| 172 | |
| 173 | // tangent |
| 174 | *vertices++ = 1.0f; |
| 175 | *vertices++ = 0.0f; |
| 176 | *vertices++ = 0.0f; |
| 177 | *vertices++ = 1.0f; |
| 178 | } |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | case NegativeZ: |
| 184 | // Iterate over y |
| 185 | for (int j = 0; j < resolution.height(); ++j) { |
| 186 | const float b = b0 + static_cast<float>(j) * db; |
| 187 | const float v = static_cast<float>(j) * dv; |
| 188 | |
| 189 | // Iterate over x |
| 190 | for (int i = resolution.width() - 1; i >= 0; --i) { |
| 191 | const float a = a0 + static_cast<float>(i) * da; |
| 192 | const float u = static_cast<float>(i) * du; |
| 193 | |
| 194 | // position |
| 195 | *vertices++ = a; |
| 196 | *vertices++ = b; |
| 197 | *vertices++ = planeDistance; |
| 198 | |
| 199 | // texture coordinates |
| 200 | *vertices++ = 1.0f - u; |
| 201 | *vertices++ = v; |
| 202 | |
| 203 | // normal |
| 204 | *vertices++ = 0.0f; |
| 205 | *vertices++ = 0.0f; |
| 206 | *vertices++ = -1.0f; |
| 207 | |
| 208 | // tangent |
| 209 | *vertices++ = -1.0f; |
| 210 | *vertices++ = 0.0f; |
| 211 | *vertices++ = 0.0f; |
| 212 | *vertices++ = 1.0f; |
| 213 | } |
| 214 | } |
| 215 | break; |
| 216 | |
| 217 | case PositiveZ: { |
| 218 | // Iterate over y |
| 219 | for (int j = 0; j < resolution.height(); ++j) { |
| 220 | const float b = b0 + static_cast<float>(j) * db; |
| 221 | const float v = static_cast<float>(j) * dv; |
| 222 | |
| 223 | // Iterate over x |
| 224 | for (int i = 0; i < resolution.width(); ++i) { |
| 225 | const float a = a0 + static_cast<float>(i) * da; |
| 226 | const float u = static_cast<float>(i) * du; |
| 227 | |
| 228 | // position |
| 229 | *vertices++ = a; |
| 230 | *vertices++ = b; |
| 231 | *vertices++ = planeDistance; |
| 232 | |
| 233 | // texture coordinates |
| 234 | *vertices++ = u; |
| 235 | *vertices++ = v; |
| 236 | |
| 237 | // normal |
| 238 | *vertices++ = 0.0f; |
| 239 | *vertices++ = 0.0f; |
| 240 | *vertices++ = 1.0f; |
| 241 | |
| 242 | // tangent |
| 243 | *vertices++ = 1.0f; |
| 244 | *vertices++ = 0.0f; |
| 245 | *vertices++ = 0.0f; |
| 246 | *vertices++ = 1.0f; |
| 247 | } |
| 248 | } |
| 249 | break; |
| 250 | } |
| 251 | } // switch (normal) |
| 252 | } |
| 253 | |
| 254 | void (const QSize &resolution, quint16 *indices, quint16 &baseVertex) |
| 255 | { |
| 256 | // Populate indices taking care to get correct CCW winding on all faces |
| 257 | // Iterate over v direction (rows) |
| 258 | for (int j = 0; j < resolution.height() - 1; ++j) { |
| 259 | const int rowStartIndex = j * resolution.width() + baseVertex; |
| 260 | const int nextRowStartIndex = (j + 1) * resolution.width() + baseVertex; |
| 261 | |
| 262 | // Iterate over u direction (columns) |
| 263 | for (int i = 0; i < resolution.width() - 1; ++i) { |
| 264 | // Split quad into two triangles |
| 265 | *indices++ = rowStartIndex + i; |
| 266 | *indices++ = rowStartIndex + i + 1; |
| 267 | *indices++ = nextRowStartIndex + i; |
| 268 | |
| 269 | *indices++ = nextRowStartIndex + i; |
| 270 | *indices++ = rowStartIndex + i + 1; |
| 271 | *indices++ = nextRowStartIndex + i + 1; |
| 272 | } |
| 273 | } |
| 274 | baseVertex += resolution.width() * resolution.height(); |
| 275 | } |
| 276 | |
| 277 | QByteArray (float xExtent, |
| 278 | float yExtent, |
| 279 | float zExtent, |
| 280 | const QSize &yzResolution, |
| 281 | const QSize &xzResolution, |
| 282 | const QSize &xyResolution) |
| 283 | { |
| 284 | Q_ASSERT(xExtent > 0.0f && yExtent > 0.0f && zExtent > 0.0); |
| 285 | Q_ASSERT(yzResolution.width() >= 2 && yzResolution.height() >=2); |
| 286 | Q_ASSERT(xzResolution.width() >= 2 && xzResolution.height() >=2); |
| 287 | Q_ASSERT(xyResolution.width() >= 2 && xyResolution.height() >=2); |
| 288 | |
| 289 | const int yzVerts = yzResolution.width() * yzResolution.height(); |
| 290 | const int xzVerts = xzResolution.width() * xzResolution.height(); |
| 291 | const int xyVerts = xyResolution.width() * xyResolution.height(); |
| 292 | const int nVerts = 2 * (yzVerts + xzVerts + xyVerts); |
| 293 | |
| 294 | const quint32 elementSize = 3 + 3 + 2 + 4; |
| 295 | const quint32 stride = elementSize * sizeof(float); |
| 296 | QByteArray vertexBytes; |
| 297 | vertexBytes.resize(size: stride * nVerts); |
| 298 | float* vertices = reinterpret_cast<float*>(vertexBytes.data()); |
| 299 | |
| 300 | createPlaneVertexData(w: yExtent, h: zExtent, resolution: yzResolution, normal: PositiveX, planeDistance: xExtent * 0.5f, vertices); |
| 301 | vertices += yzVerts * elementSize; |
| 302 | createPlaneVertexData(w: yExtent, h: zExtent, resolution: yzResolution, normal: NegativeX, planeDistance: -xExtent * 0.5f, vertices); |
| 303 | vertices += yzVerts * elementSize; |
| 304 | createPlaneVertexData(w: xExtent, h: zExtent, resolution: xzResolution, normal: PositiveY, planeDistance: yExtent * 0.5f, vertices); |
| 305 | vertices += xzVerts * elementSize; |
| 306 | createPlaneVertexData(w: xExtent, h: zExtent, resolution: xzResolution, normal: NegativeY, planeDistance: -yExtent * 0.5f, vertices); |
| 307 | vertices += xzVerts * elementSize; |
| 308 | createPlaneVertexData(w: xExtent, h: yExtent, resolution: xyResolution, normal: PositiveZ, planeDistance: zExtent * 0.5f, vertices); |
| 309 | vertices += xyVerts * elementSize; |
| 310 | createPlaneVertexData(w: xExtent, h: yExtent, resolution: xyResolution, normal: NegativeZ, planeDistance: -zExtent * 0.5f, vertices); |
| 311 | |
| 312 | return vertexBytes; |
| 313 | } |
| 314 | |
| 315 | QByteArray (const QSize &yzResolution, |
| 316 | const QSize &xzResolution, |
| 317 | const QSize &xyResolution) |
| 318 | { |
| 319 | Q_ASSERT(yzResolution.width() >= 2 && yzResolution.height() >= 2); |
| 320 | Q_ASSERT(xzResolution.width() >= 2 && xzResolution.height() >= 2); |
| 321 | Q_ASSERT(xyResolution.width() >= 2 && xyResolution.height() >= 2); |
| 322 | |
| 323 | const int yzIndices = 2 * 3 * (yzResolution.width() - 1) * (yzResolution.height() - 1); |
| 324 | const int xzIndices = 2 * 3 * (xzResolution.width() - 1) * (xzResolution.height() - 1); |
| 325 | const int xyIndices = 2 * 3 * (xyResolution.width() - 1) * (xyResolution.height() - 1); |
| 326 | const qsizetype indexCount = 2 * (yzIndices + xzIndices + xyIndices); |
| 327 | |
| 328 | QByteArray indexData; |
| 329 | indexData.resize(size: indexCount * sizeof(quint16)); |
| 330 | quint16 *indices = reinterpret_cast<quint16 *>(indexData.data()); |
| 331 | quint16 baseIndex = 0; |
| 332 | |
| 333 | createPlaneIndexData(resolution: yzResolution, indices, baseVertex&: baseIndex); |
| 334 | indices += yzIndices; |
| 335 | createPlaneIndexData(resolution: yzResolution, indices, baseVertex&: baseIndex); |
| 336 | indices += yzIndices; |
| 337 | createPlaneIndexData(resolution: xzResolution, indices, baseVertex&: baseIndex); |
| 338 | indices += xzIndices; |
| 339 | createPlaneIndexData(resolution: xzResolution, indices, baseVertex&: baseIndex); |
| 340 | indices += xzIndices; |
| 341 | createPlaneIndexData(resolution: xyResolution, indices, baseVertex&: baseIndex); |
| 342 | indices += xyIndices; |
| 343 | createPlaneIndexData(resolution: xyResolution, indices, baseVertex&: baseIndex); |
| 344 | |
| 345 | return indexData; |
| 346 | } |
| 347 | |
| 348 | } // anonymous |
| 349 | |
| 350 | QCuboidGeometryPrivate::() |
| 351 | : QGeometryPrivate() |
| 352 | , m_xExtent(1.0f) |
| 353 | , m_yExtent(1.0f) |
| 354 | , m_zExtent(1.0f) |
| 355 | , m_yzFaceResolution(2, 2) |
| 356 | , m_xzFaceResolution(2, 2) |
| 357 | , m_xyFaceResolution(2, 2) |
| 358 | , m_positionAttribute(nullptr) |
| 359 | , m_normalAttribute(nullptr) |
| 360 | , m_texCoordAttribute(nullptr) |
| 361 | , m_tangentAttribute(nullptr) |
| 362 | , m_indexAttribute(nullptr) |
| 363 | , m_vertexBuffer(nullptr) |
| 364 | , m_indexBuffer(nullptr) |
| 365 | { |
| 366 | } |
| 367 | |
| 368 | void QCuboidGeometryPrivate::() |
| 369 | { |
| 370 | Q_Q(QCuboidGeometry); |
| 371 | m_positionAttribute = new Qt3DCore::QAttribute(q); |
| 372 | m_normalAttribute = new Qt3DCore::QAttribute(q); |
| 373 | m_texCoordAttribute = new Qt3DCore::QAttribute(q); |
| 374 | m_tangentAttribute = new Qt3DCore::QAttribute(q); |
| 375 | m_indexAttribute = new Qt3DCore::QAttribute(q); |
| 376 | m_vertexBuffer = new Qt3DCore::QBuffer(q); |
| 377 | m_indexBuffer = new Qt3DCore::QBuffer(q); |
| 378 | |
| 379 | // vec3 pos vec2 tex vec3 normal vec4 tangent |
| 380 | const quint32 stride = (3 + 2 + 3 + 4) * sizeof(float); |
| 381 | const int yzIndices = 2 * 3 * (m_yzFaceResolution.width() - 1) * (m_yzFaceResolution.height() - 1); |
| 382 | const int xzIndices = 2 * 3 * (m_xzFaceResolution.width() - 1) * (m_xzFaceResolution.height() - 1); |
| 383 | const int xyIndices = 2 * 3 * (m_xyFaceResolution.width() - 1) * (m_xyFaceResolution.height() - 1); |
| 384 | const int yzVerts = m_yzFaceResolution.width() * m_yzFaceResolution.height(); |
| 385 | const int xzVerts = m_xzFaceResolution.width() * m_xzFaceResolution.height(); |
| 386 | const int xyVerts = m_xyFaceResolution.width() * m_xyFaceResolution.height(); |
| 387 | |
| 388 | const int nVerts = 2 * (yzVerts + xzVerts + xyVerts); |
| 389 | const int indexCount = 2 * (yzIndices + xzIndices + xyIndices); |
| 390 | |
| 391 | m_positionAttribute->setName(QAttribute::defaultPositionAttributeName()); |
| 392 | m_positionAttribute->setVertexBaseType(QAttribute::Float); |
| 393 | m_positionAttribute->setVertexSize(3); |
| 394 | m_positionAttribute->setAttributeType(QAttribute::VertexAttribute); |
| 395 | m_positionAttribute->setBuffer(m_vertexBuffer); |
| 396 | m_positionAttribute->setByteStride(stride); |
| 397 | m_positionAttribute->setCount(nVerts); |
| 398 | |
| 399 | m_texCoordAttribute->setName(QAttribute::defaultTextureCoordinateAttributeName()); |
| 400 | m_texCoordAttribute->setVertexBaseType(QAttribute::Float); |
| 401 | m_texCoordAttribute->setVertexSize(2); |
| 402 | m_texCoordAttribute->setAttributeType(QAttribute::VertexAttribute); |
| 403 | m_texCoordAttribute->setBuffer(m_vertexBuffer); |
| 404 | m_texCoordAttribute->setByteStride(stride); |
| 405 | m_texCoordAttribute->setByteOffset(3 * sizeof(float)); |
| 406 | m_texCoordAttribute->setCount(nVerts); |
| 407 | |
| 408 | m_normalAttribute->setName(QAttribute::defaultNormalAttributeName()); |
| 409 | m_normalAttribute->setVertexBaseType(QAttribute::Float); |
| 410 | m_normalAttribute->setVertexSize(3); |
| 411 | m_normalAttribute->setAttributeType(QAttribute::VertexAttribute); |
| 412 | m_normalAttribute->setBuffer(m_vertexBuffer); |
| 413 | m_normalAttribute->setByteStride(stride); |
| 414 | m_normalAttribute->setByteOffset(5 * sizeof(float)); |
| 415 | m_normalAttribute->setCount(nVerts); |
| 416 | |
| 417 | m_tangentAttribute->setName(QAttribute::defaultTangentAttributeName()); |
| 418 | m_tangentAttribute->setVertexBaseType(QAttribute::Float); |
| 419 | m_tangentAttribute->setVertexSize(4); |
| 420 | m_tangentAttribute->setAttributeType(QAttribute::VertexAttribute); |
| 421 | m_tangentAttribute->setBuffer(m_vertexBuffer); |
| 422 | m_tangentAttribute->setByteStride(stride); |
| 423 | m_tangentAttribute->setByteOffset(8 * sizeof(float)); |
| 424 | m_tangentAttribute->setCount(nVerts); |
| 425 | |
| 426 | m_indexAttribute->setAttributeType(QAttribute::IndexAttribute); |
| 427 | m_indexAttribute->setVertexBaseType(QAttribute::UnsignedShort); |
| 428 | m_indexAttribute->setBuffer(m_indexBuffer); |
| 429 | |
| 430 | m_indexAttribute->setCount(indexCount); |
| 431 | |
| 432 | m_vertexBuffer->setData(generateVertexData()); |
| 433 | m_indexBuffer->setData(generateIndexData()); |
| 434 | |
| 435 | q->addAttribute(attribute: m_positionAttribute); |
| 436 | q->addAttribute(attribute: m_texCoordAttribute); |
| 437 | q->addAttribute(attribute: m_normalAttribute); |
| 438 | q->addAttribute(attribute: m_tangentAttribute); |
| 439 | q->addAttribute(attribute: m_indexAttribute); |
| 440 | } |
| 441 | |
| 442 | QByteArray QCuboidGeometryPrivate::() const |
| 443 | { |
| 444 | return createCuboidVertexData(xExtent: m_xExtent, yExtent: m_yExtent, zExtent: m_zExtent, |
| 445 | yzResolution: m_yzFaceResolution, xzResolution: m_xzFaceResolution, xyResolution: m_xyFaceResolution); |
| 446 | } |
| 447 | |
| 448 | QByteArray QCuboidGeometryPrivate::() const |
| 449 | { |
| 450 | return createCuboidIndexData(yzResolution: m_yzFaceResolution, xzResolution: m_xzFaceResolution, xyResolution: m_xyFaceResolution); |
| 451 | } |
| 452 | |
| 453 | /*! |
| 454 | * \qmltype CuboidGeometry |
| 455 | * \nativetype Qt3DExtras::QCuboidGeometry |
| 456 | * \inqmlmodule Qt3D.Extras |
| 457 | * \brief CuboidGeometry allows creation of a cuboid in 3D space. |
| 458 | * |
| 459 | * The CuboidGeometry type is most commonly used internally by the CuboidMesh type |
| 460 | * but can also be used in custom GeometryRenderer types. |
| 461 | */ |
| 462 | |
| 463 | /*! |
| 464 | * \qmlproperty real CuboidGeometry::xExtent |
| 465 | * |
| 466 | * Holds the x extent of the geometry. |
| 467 | */ |
| 468 | |
| 469 | /*! |
| 470 | * \qmlproperty real CuboidGeometry::yExtent |
| 471 | * |
| 472 | * Holds the y extent of the geometry. |
| 473 | */ |
| 474 | |
| 475 | /*! |
| 476 | * \qmlproperty real CuboidGeometry::zExtent |
| 477 | * |
| 478 | * Holds the z extent of the geometry. |
| 479 | */ |
| 480 | |
| 481 | /*! |
| 482 | * \qmlproperty size CuboidGeometry::yzMeshResolution |
| 483 | * |
| 484 | * Holds the y-z resolution. |
| 485 | * The width and height values of this property specify the number of vertices generated for |
| 486 | * the y-z faces of the mesh. |
| 487 | */ |
| 488 | |
| 489 | /*! |
| 490 | * \qmlproperty size CuboidGeometry::xzMeshResolution |
| 491 | * |
| 492 | * Holds the x-z resolution. |
| 493 | * The width and height values of this property specify the number of vertices generated for |
| 494 | * the x-z faces of the mesh. |
| 495 | */ |
| 496 | |
| 497 | /*! |
| 498 | * \qmlproperty size CuboidGeometry::xyMeshResolution |
| 499 | * |
| 500 | * Holds the x-y resolution. |
| 501 | * The width and height values of this property specify the number of vertices generated for |
| 502 | * the x-y faces of the mesh. |
| 503 | */ |
| 504 | |
| 505 | /*! |
| 506 | * \qmlproperty Attribute CuboidGeometry::positionAttribute |
| 507 | * |
| 508 | * Holds the geometry position attribute. |
| 509 | */ |
| 510 | |
| 511 | /*! |
| 512 | * \qmlproperty Attribute CuboidGeometry::normalAttribute |
| 513 | * |
| 514 | * Holds the geometry normal attribute. |
| 515 | */ |
| 516 | |
| 517 | /*! |
| 518 | * \qmlproperty Attribute CuboidGeometry::texCoordAttribute |
| 519 | * |
| 520 | * Holds the geometry texture coordinate attribute. |
| 521 | */ |
| 522 | |
| 523 | /*! |
| 524 | * \qmlproperty Attribute CuboidGeometry::tangentAttribute |
| 525 | * |
| 526 | * Holds the geometry tangent attribute. |
| 527 | */ |
| 528 | |
| 529 | /*! |
| 530 | * \qmlproperty Attribute CuboidGeometry::indexAttribute |
| 531 | * |
| 532 | * Holds the geometry index attribute. |
| 533 | */ |
| 534 | |
| 535 | /*! |
| 536 | * \class Qt3DExtras::QCuboidGeometry |
| 537 | * \ingroup qt3d-extras-geometries |
| 538 | * \inheaderfile Qt3DExtras/QCuboidGeometry |
| 539 | * \inmodule Qt3DExtras |
| 540 | * \brief The QCuboidGeometry class allows creation of a cuboid in 3D space. |
| 541 | * \since 5.7 |
| 542 | * \ingroup geometries |
| 543 | * \inherits Qt3DCore::QGeometry |
| 544 | * |
| 545 | * The QCuboidGeometry class is most commonly used internally by the QCuboidMesh |
| 546 | * but can also be used in custom Qt3DRender::QGeometryRenderer subclasses. |
| 547 | */ |
| 548 | |
| 549 | /*! |
| 550 | * Constructs a new QCuboidGeometry with \a parent. |
| 551 | */ |
| 552 | QCuboidGeometry::(QNode *parent) |
| 553 | : QGeometry(*new QCuboidGeometryPrivate(), parent) |
| 554 | { |
| 555 | Q_D(QCuboidGeometry); |
| 556 | d->init(); |
| 557 | } |
| 558 | |
| 559 | /*! |
| 560 | * \internal |
| 561 | */ |
| 562 | QCuboidGeometry::(QCuboidGeometryPrivate &dd, QNode *parent) |
| 563 | : QGeometry(dd, parent) |
| 564 | { |
| 565 | Q_D(QCuboidGeometry); |
| 566 | d->init(); |
| 567 | } |
| 568 | |
| 569 | /*! |
| 570 | * \internal |
| 571 | */ |
| 572 | QCuboidGeometry::() |
| 573 | { |
| 574 | } |
| 575 | |
| 576 | /*! |
| 577 | * Updates indices based on mesh resolutions. |
| 578 | */ |
| 579 | void QCuboidGeometry::() |
| 580 | { |
| 581 | Q_D(QCuboidGeometry); |
| 582 | const int yzIndices = 2 * 3 * (d->m_yzFaceResolution.width() - 1) * (d->m_yzFaceResolution.height() - 1); |
| 583 | const int xzIndices = 2 * 3 * (d->m_xzFaceResolution.width() - 1) * (d->m_xzFaceResolution.height() - 1); |
| 584 | const int xyIndices = 2 * 3 * (d->m_xyFaceResolution.width() - 1) * (d->m_xyFaceResolution.height() - 1); |
| 585 | const int indexCount = 2 * (yzIndices + xzIndices + xyIndices); |
| 586 | |
| 587 | d->m_indexAttribute->setCount(indexCount); |
| 588 | d->m_indexBuffer->setData(d->generateIndexData()); |
| 589 | } |
| 590 | |
| 591 | /*! |
| 592 | * Updates vertices based on mesh resolutions. |
| 593 | */ |
| 594 | void QCuboidGeometry::() |
| 595 | { |
| 596 | Q_D(QCuboidGeometry); |
| 597 | const int yzVerts = d->m_yzFaceResolution.width() * d->m_yzFaceResolution.height(); |
| 598 | const int xzVerts = d->m_xzFaceResolution.width() * d->m_xzFaceResolution.height(); |
| 599 | const int xyVerts = d->m_xyFaceResolution.width() * d->m_xyFaceResolution.height(); |
| 600 | const int nVerts = 2 * (yzVerts + xzVerts + xyVerts); |
| 601 | |
| 602 | d->m_positionAttribute->setCount(nVerts); |
| 603 | d->m_normalAttribute->setCount(nVerts); |
| 604 | d->m_texCoordAttribute->setCount(nVerts); |
| 605 | d->m_tangentAttribute->setCount(nVerts); |
| 606 | |
| 607 | d->m_vertexBuffer->setData(d->generateVertexData()); |
| 608 | } |
| 609 | |
| 610 | void QCuboidGeometry::(float xExtent) |
| 611 | { |
| 612 | Q_D(QCuboidGeometry); |
| 613 | if (d->m_xExtent != xExtent) { |
| 614 | d->m_xExtent = xExtent; |
| 615 | updateVertices(); |
| 616 | emit xExtentChanged(xExtent); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | void QCuboidGeometry::(float yExtent) |
| 621 | { |
| 622 | Q_D(QCuboidGeometry); |
| 623 | if (d->m_yExtent != yExtent) { |
| 624 | d->m_yExtent = yExtent; |
| 625 | updateVertices(); |
| 626 | emit yExtentChanged(yExtent); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | void QCuboidGeometry::(float zExtent) |
| 631 | { |
| 632 | Q_D(QCuboidGeometry); |
| 633 | if (d->m_zExtent != zExtent) { |
| 634 | d->m_zExtent = zExtent; |
| 635 | updateVertices(); |
| 636 | emit zExtentChanged(zExtent); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | void QCuboidGeometry::(const QSize &resolution) |
| 641 | { |
| 642 | Q_D(QCuboidGeometry); |
| 643 | if (d->m_yzFaceResolution != resolution) { |
| 644 | d->m_yzFaceResolution = resolution; |
| 645 | updateVertices(); |
| 646 | updateIndices(); |
| 647 | emit yzMeshResolutionChanged(yzMeshResolution: resolution); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | void QCuboidGeometry::(const QSize &resolution) |
| 652 | { |
| 653 | Q_D(QCuboidGeometry); |
| 654 | if (d->m_xzFaceResolution != resolution) { |
| 655 | d->m_xzFaceResolution = resolution; |
| 656 | updateVertices(); |
| 657 | updateIndices(); |
| 658 | emit xzMeshResolutionChanged(xzMeshResolution: resolution); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | void QCuboidGeometry::(const QSize &resolution) |
| 663 | { |
| 664 | Q_D(QCuboidGeometry); |
| 665 | if (d->m_xyFaceResolution != resolution) { |
| 666 | d->m_xyFaceResolution = resolution; |
| 667 | updateVertices(); |
| 668 | updateIndices(); |
| 669 | emit xyMeshResolutionChanged(xyMeshResolution: resolution); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /*! |
| 674 | * \property Qt3DExtras::QCuboidGeometry::xExtent |
| 675 | * |
| 676 | * Holds the x extent of the geometry. |
| 677 | */ |
| 678 | float QCuboidGeometry::() const |
| 679 | { |
| 680 | Q_D(const QCuboidGeometry); |
| 681 | return d->m_xExtent; |
| 682 | } |
| 683 | |
| 684 | /*! |
| 685 | * \property Qt3DExtras::QCuboidGeometry::yExtent |
| 686 | * |
| 687 | * Holds the y extent of the geometry. |
| 688 | */ |
| 689 | float QCuboidGeometry::() const |
| 690 | { |
| 691 | Q_D(const QCuboidGeometry); |
| 692 | return d->m_yExtent; |
| 693 | } |
| 694 | |
| 695 | /*! |
| 696 | * \property Qt3DExtras::QCuboidGeometry::zExtent |
| 697 | * |
| 698 | * Holds the z extent of the geometry. |
| 699 | */ |
| 700 | float QCuboidGeometry::() const |
| 701 | { |
| 702 | Q_D(const QCuboidGeometry); |
| 703 | return d->m_zExtent; |
| 704 | } |
| 705 | |
| 706 | /*! |
| 707 | * \property Qt3DExtras::QCuboidGeometry::yzMeshResolution |
| 708 | * |
| 709 | * Holds the y-z resolution. |
| 710 | * The width and height values of this property specify the number of vertices generated for |
| 711 | * the y-z faces of the mesh. |
| 712 | */ |
| 713 | QSize QCuboidGeometry::() const |
| 714 | { |
| 715 | Q_D(const QCuboidGeometry); |
| 716 | return d->m_yzFaceResolution; |
| 717 | } |
| 718 | |
| 719 | /*! |
| 720 | * \property Qt3DExtras::QCuboidGeometry::xzMeshResolution |
| 721 | * |
| 722 | * Holds the x-z resolution. |
| 723 | * The width and height values of this property specify the number of vertices generated for |
| 724 | * the x-z faces of the mesh. |
| 725 | */ |
| 726 | QSize QCuboidGeometry::() const |
| 727 | { |
| 728 | Q_D(const QCuboidGeometry); |
| 729 | return d->m_xyFaceResolution; |
| 730 | } |
| 731 | |
| 732 | /*! |
| 733 | * \property Qt3DExtras::QCuboidGeometry::xyMeshResolution |
| 734 | * |
| 735 | * Holds the x-y resolution. |
| 736 | * The width and height values of this property specify the number of vertices generated for |
| 737 | * the x-y faces of the mesh. |
| 738 | */ |
| 739 | QSize QCuboidGeometry::() const |
| 740 | { |
| 741 | Q_D(const QCuboidGeometry); |
| 742 | return d->m_xzFaceResolution; |
| 743 | } |
| 744 | |
| 745 | /*! |
| 746 | * \property Qt3DExtras::QCuboidGeometry::positionAttribute |
| 747 | * |
| 748 | * Holds the geometry position attribute. |
| 749 | */ |
| 750 | QAttribute *QCuboidGeometry::() const |
| 751 | { |
| 752 | Q_D(const QCuboidGeometry); |
| 753 | return d->m_positionAttribute; |
| 754 | } |
| 755 | |
| 756 | /*! |
| 757 | * \property Qt3DExtras::QCuboidGeometry::normalAttribute |
| 758 | * |
| 759 | * Holds the geometry normal attribute. |
| 760 | */ |
| 761 | QAttribute *QCuboidGeometry::() const |
| 762 | { |
| 763 | Q_D(const QCuboidGeometry); |
| 764 | return d->m_normalAttribute; |
| 765 | } |
| 766 | |
| 767 | /*! |
| 768 | * \property Qt3DExtras::QCuboidGeometry::texCoordAttribute |
| 769 | * |
| 770 | * Holds the geometry texture coordinate attribute. |
| 771 | */ |
| 772 | QAttribute *QCuboidGeometry::() const |
| 773 | { |
| 774 | Q_D(const QCuboidGeometry); |
| 775 | return d->m_texCoordAttribute; |
| 776 | } |
| 777 | |
| 778 | /*! |
| 779 | * \property Qt3DExtras::QCuboidGeometry::tangentAttribute |
| 780 | * |
| 781 | * Holds the geometry tangent attribute. |
| 782 | */ |
| 783 | QAttribute *QCuboidGeometry::() const |
| 784 | { |
| 785 | Q_D(const QCuboidGeometry); |
| 786 | return d->m_tangentAttribute; |
| 787 | } |
| 788 | |
| 789 | /*! |
| 790 | * \property Qt3DExtras::QCuboidGeometry::indexAttribute |
| 791 | * |
| 792 | * Holds the geometry index attribute. |
| 793 | */ |
| 794 | QAttribute *QCuboidGeometry::() const |
| 795 | { |
| 796 | Q_D(const QCuboidGeometry); |
| 797 | return d->m_indexAttribute; |
| 798 | } |
| 799 | |
| 800 | } // Qt3DExtras |
| 801 | |
| 802 | QT_END_NAMESPACE |
| 803 | |
| 804 | #include "moc_qcuboidgeometry.cpp" |
| 805 | |