15
16
17
18
19
20
21
22
23
24
25
26
29
30
31
32
35
36
37
38
41
42
43
44
47
48
49
50
51
54
55
56
57
60
61
62
63
66
67
68
69
71CapsuleGeometryPhysics::CapsuleGeometryPhysics()
76void CapsuleGeometryPhysics::setEnableNormals(
bool enable)
78 if (m_enableNormals == enable)
81 m_enableNormals = enable;
82 emit enableNormalsChanged();
87void CapsuleGeometryPhysics::setEnableUV(
bool enable)
89 if (m_enableUV == enable)
93 emit enableUVChanged();
98void CapsuleGeometryPhysics::setLongitudes(
int longitudes)
100 longitudes = qMin(longitudes, 1024);
102 if (m_longitudes == longitudes)
105 m_longitudes = longitudes;
106 emit longitudesChanged();
111void CapsuleGeometryPhysics::setLatitudes(
int latitudes)
113 latitudes = qMin(latitudes, 1024);
115 if (m_latitudes == latitudes)
118 m_latitudes = latitudes;
119 emit latitudesChanged();
124void CapsuleGeometryPhysics::setRings(
int rings)
126 rings = qMin(rings, 1024);
128 if (m_rings == rings)
137void CapsuleGeometryPhysics::setHeight(
float height)
139 if (m_height == height)
143 emit heightChanged();
148void CapsuleGeometryPhysics::setDiameter(
float diameter)
150 if (m_diameter == diameter)
153 m_diameter = diameter;
154 emit diameterChanged();
169void CapsuleGeometryPhysics::updateData()
173 constexpr float EPSILON = 0.001f;
174 const float radius = m_diameter * 0.5f;
177 int verifLats = qMax(2, m_latitudes);
178 if (verifLats % 2 != 0) {
183 uint32_t verifLons = qMax(3, m_longitudes);
184 uint32_t verifRings = qMax(0, m_rings);
185 float verifDepth = qMax(EPSILON, m_height);
186 float verifRad = qMax(EPSILON, radius);
189 bool calcMiddle = verifRings > 0;
190 uint32_t halfLats = verifLats / 2;
191 uint32_t halfLatsn1 = halfLats - 1;
192 uint32_t halfLatsn2 = halfLats - 2;
193 uint32_t verifRingsp1 = verifRings + 1;
194 uint32_t verifLonsp1 = verifLons + 1;
195 uint32_t lonsHalfLatn1 = halfLatsn1 * verifLons;
196 uint32_t lonsRingsp1 = verifRingsp1 * verifLons;
197 float halfDepth = verifDepth * 0.5f;
198 float summit = halfDepth + verifRad;
201 uint32_t idxVNEquator = verifLonsp1 + verifLons * halfLatsn2;
202 uint32_t idxVCyl = idxVNEquator + verifLons;
203 uint32_t idxVSEquator = idxVCyl;
205 idxVSEquator += verifLons * verifRings;
207 uint32_t idxVSouth = idxVSEquator + verifLons;
208 uint32_t idxVSouthCap = idxVSouth + verifLons * halfLatsn2;
209 uint32_t idxVSouthPole = idxVSouthCap + verifLons;
212 uint32_t idxVtNEquator = verifLons + verifLonsp1 * halfLatsn1;
213 uint32_t idxVtCyl = idxVtNEquator + verifLonsp1;
214 uint32_t idxVtSEquator = idxVtCyl;
216 idxVtSEquator += verifLonsp1 * verifRings;
218 uint32_t idxVtSHemi = idxVtSEquator + verifLonsp1;
219 uint32_t idxVtSPolar = idxVtSHemi + verifLonsp1 * halfLatsn2;
220 uint32_t idxVtSCap = idxVtSPolar + verifLonsp1;
223 uint32_t idxVnSouth = idxVNEquator + verifLons;
224 uint32_t idxVnSouthCap = idxVnSouth + verifLons * halfLatsn2;
225 uint32_t idxVnSouthPole = idxVnSouthCap + verifLons;
228 uint32_t idxFsCyl = verifLons + lonsHalfLatn1 * 2;
229 uint32_t idxFsSouthEquat = idxFsCyl + lonsRingsp1 * 2;
230 uint32_t idxFsSouthHemi = idxFsSouthEquat + lonsHalfLatn1 * 2;
233 uint32_t verticesLen = idxVSouthPole + 1;
234 uint32_t texturesLen = idxVtSCap + verifLons;
235 uint32_t normalsLen = idxVnSouthPole + 1;
236 uint32_t facesLen = idxFsSouthHemi + verifLons;
239 auto vertices = QList<QVector3D>(verticesLen);
240 auto vertexTextures = QList<QVector2D>(texturesLen);
241 auto vertexNormals = QList<QVector3D>(normalsLen);
245 auto faces = QList<std::array<Face, 3>>(facesLen);
248 vertices[0] = QVector3D(-summit, 0.f, 0.f);
249 vertexNormals[0] = QVector3D(-1.f, 0.f, 0.f);
252 vertices[idxVSouthPole] = QVector3D(summit, 0.f, 0.f);
253 vertexNormals[idxVnSouthPole] = QVector3D(1.f, 0.f, 0.f);
256 QList<
float> sinThetaCache = QList<
float>(verifLons);
257 QList<
float> cosThetaCache = QList<
float>(verifLons);
258 float toTheta = 2 * M_PI / verifLons;
259 float toPhi = M_PI / verifLats;
260 float toTexHorizontal = 1.f / verifLons;
261 float toTexVertical = 1.f / halfLats;
263 for (uint32_t j = 0; j < verifLons; ++j) {
266 float theta = j * toTheta;
267 float sinTheta = sin(theta);
268 float cosTheta = cos(theta);
269 sinThetaCache[j] = sinTheta;
270 cosThetaCache[j] = cosTheta;
273 float sTex = (j + 0.5f) * toTexHorizontal;
274 vertexTextures[j] = QVector2D(sTex, 1.f);
275 vertexTextures[idxVtSCap + j] = QVector2D(sTex, 0.f);
278 float x = verifRad * cosTheta;
279 float z = verifRad * sinTheta;
282 vertices[idxVNEquator + j] = QVector3D(-halfDepth, x, -z);
283 vertices[idxVSEquator + j] = QVector3D(halfDepth, x, -z);
286 vertexNormals[idxVNEquator + j] = QVector3D(0.f, cosTheta, -sinTheta);
289 uint32_t jNextVt = j + 1;
290 uint32_t jNextV = jNextVt % verifLons;
293 faces[j] = {
Face { 0, j, 0 },
Face { jNextVt, verifLons + j, jNextVt },
294 Face { 1 + jNextV, verifLons + jNextVt, 1 + jNextV } };
297 faces[idxFsSouthHemi + j] = {
298 Face { idxVSouthPole, idxVtSCap + j, idxVnSouthPole },
299 Face { idxVSouthCap + jNextV, idxVtSPolar + jNextVt, idxVnSouthCap + jNextV },
300 Face { idxVSouthCap + j, idxVtSPolar + j, idxVnSouthCap + j }
305 float vtAspectRatio = 0.f;
306 switch (m_uvProfile) {
307 case CapsuleGeometryPhysics::UvProfile::Fixed:
308 vtAspectRatio = 0.33333333f;
310 case CapsuleGeometryPhysics::UvProfile::Aspect:
311 vtAspectRatio = verifRad / (verifDepth + verifRad + verifRad);
313 case CapsuleGeometryPhysics::UvProfile::Uniform:
314 vtAspectRatio = (
float)halfLats / (verifRingsp1 + verifLats);
317 float vtAspectSouth = vtAspectRatio;
318 float vtAspectNorth = 1.f - vtAspectRatio;
321 QList<
float> sTexCache = QList<
float>(verifLonsp1);
324 for (uint32_t j = 0; j < verifLonsp1; ++j) {
325 float sTex = j * toTexHorizontal;
327 vertexTextures[idxVtNEquator + j] = QVector2D(sTex, vtAspectNorth);
328 vertexTextures[idxVtSEquator + j] = QVector2D(sTex, vtAspectSouth);
332 uint32_t vHemiOffsetNorth = 1;
333 uint32_t vHemiOffsetSouth = idxVSouth;
334 uint32_t vtHemiOffsetNorth = verifLons;
335 uint32_t vtHemiOffsetSouth = idxVtSHemi;
336 uint32_t vnHemiOffsetSouth = idxVnSouth;
337 uint32_t fHemiOffsetNorth = verifLons;
338 uint32_t fHemiOffsetSouth = idxFsSouthEquat;
340 for (uint32_t i = 0; i < halfLatsn1; ++i) {
341 uint32_t iLonsCurr = i * verifLons;
342 float ip1f = i + 1.f;
343 float phi = ip1f * toPhi;
344 float sinPhiSouth = sin(phi);
345 float cosPhiSouth = cos(phi);
349 float cosPhiNorth = sinPhiSouth;
350 float sinPhiNorth = -cosPhiSouth;
353 float rhoCosPhiNorth = verifRad * cosPhiNorth;
354 float rhoSinPhiNorth = verifRad * sinPhiNorth;
355 float yOffsetNorth = halfDepth - rhoSinPhiNorth;
358 float rhoCosPhiSouth = verifRad * cosPhiSouth;
359 float rhoSinPhiSouth = verifRad * sinPhiSouth;
360 float yOffsetSouth = -halfDepth - rhoSinPhiSouth;
363 uint32_t vCurrLatN = 1 + iLonsCurr;
364 uint32_t vNextLatN = vCurrLatN + verifLons;
367 uint32_t vCurrLatS = idxVSEquator + iLonsCurr;
368 uint32_t vNextLatS = vCurrLatS + verifLons;
371 uint32_t vtCurrLatN = verifLons + i * verifLonsp1;
372 uint32_t vtNextLatN = vtCurrLatN + verifLonsp1;
375 uint32_t vtCurrLatS = idxVtSEquator + i * verifLonsp1;
376 uint32_t vtNextLatS = vtCurrLatS + verifLonsp1;
379 uint32_t vnCurrLatN = 1 + iLonsCurr;
380 uint32_t vnNextLatN = vnCurrLatN + verifLons;
383 uint32_t vnCurrLatS = idxVNEquator + iLonsCurr;
384 uint32_t vnNextLatS = vnCurrLatS + verifLons;
387 for (uint32_t j = 0; j < verifLons; ++j) {
388 float sinTheta = sinThetaCache[j];
389 float cosTheta = cosThetaCache[j];
392 vertices[vHemiOffsetNorth] =
393 QVector3D(-yOffsetNorth, rhoCosPhiNorth * cosTheta, -rhoCosPhiNorth * sinTheta);
396 vertexNormals[vHemiOffsetNorth] =
397 QVector3D(sinPhiNorth, cosPhiNorth * cosTheta, -cosPhiNorth * sinTheta);
400 vertices[vHemiOffsetSouth] =
401 QVector3D(-yOffsetSouth, rhoCosPhiSouth * cosTheta, -rhoCosPhiSouth * sinTheta);
404 vertexNormals[vnHemiOffsetSouth] =
405 QVector3D(sinPhiSouth, cosPhiSouth * cosTheta, -cosPhiSouth * sinTheta);
411 uint32_t jNextVt = j + 1;
412 uint32_t jNextV = jNextVt % verifLons;
415 uint32_t vn00 = vCurrLatN + j;
416 uint32_t vn01 = vNextLatN + j;
417 uint32_t vn11 = vNextLatN + jNextV;
418 uint32_t vn10 = vCurrLatN + jNextV;
421 uint32_t vs00 = vCurrLatS + j;
422 uint32_t vs01 = vNextLatS + j;
423 uint32_t vs11 = vNextLatS + jNextV;
424 uint32_t vs10 = vCurrLatS + jNextV;
427 uint32_t vtn00 = vtCurrLatN + j;
428 uint32_t vtn01 = vtNextLatN + j;
429 uint32_t vtn11 = vtNextLatN + jNextVt;
430 uint32_t vtn10 = vtCurrLatN + jNextVt;
433 uint32_t vts00 = vtCurrLatS + j;
434 uint32_t vts01 = vtNextLatS + j;
435 uint32_t vts11 = vtNextLatS + jNextVt;
436 uint32_t vts10 = vtCurrLatS + jNextVt;
439 uint32_t vnn00 = vnCurrLatN + j;
440 uint32_t vnn01 = vnNextLatN + j;
441 uint32_t vnn11 = vnNextLatN + jNextV;
442 uint32_t vnn10 = vnCurrLatN + jNextV;
445 uint32_t vns00 = vnCurrLatS + j;
446 uint32_t vns01 = vnNextLatS + j;
447 uint32_t vns11 = vnNextLatS + jNextV;
448 uint32_t vns10 = vnCurrLatS + jNextV;
451 faces[fHemiOffsetNorth] = {
Face { vn00, vtn00, vnn00 },
Face { vn11, vtn11, vnn11 },
452 Face { vn10, vtn10, vnn10 } };
454 faces[fHemiOffsetNorth + 1] = {
Face { vn00, vtn00, vnn00 },
455 Face { vn01, vtn01, vnn01 },
456 Face { vn11, vtn11, vnn11 } };
459 faces[fHemiOffsetSouth] = {
Face { vs00, vts00, vns00 },
Face { vs11, vts11, vns11 },
460 Face { vs10, vts10, vns10 } };
462 faces[fHemiOffsetSouth + 1] = {
Face { vs00, vts00, vns00 },
463 Face { vs01, vts01, vns01 },
464 Face { vs11, vts11, vns11 } };
466 fHemiOffsetNorth += 2;
467 fHemiOffsetSouth += 2;
473 float tTexFac = ip1f * toTexVertical;
474 float tTexNorth = 1.f - tTexFac + tTexFac * vtAspectNorth;
475 float tTexSouth = vtAspectSouth * (1.f - tTexFac);
478 for (uint32_t j = 0; j < verifLonsp1; ++j) {
479 float sTex = sTexCache[j];
481 vertexTextures[vtHemiOffsetNorth] = QVector2D(sTex, tTexNorth);
482 vertexTextures[vtHemiOffsetSouth] = QVector2D(sTex, tTexSouth);
495 float toFac = 1.f / verifRingsp1;
496 uint32_t vCylOffset = idxVCyl;
497 uint32_t vtCylOffset = idxVtCyl;
498 for (uint32_t m = 1; m < verifRingsp1; ++m) {
499 float fac = m * toFac;
500 float cmplFac = 1.f - fac;
503 for (uint32_t j = 0; j < verifLons; ++j) {
504 QVector3D vEquatorNorth = vertices[idxVNEquator + j];
505 QVector3D vEquatorSouth = vertices[idxVSEquator + j];
511 vertices[vCylOffset] =
512 QVector3D(cmplFac * vEquatorNorth.x() + fac * vEquatorSouth.x(),
513 cmplFac * vEquatorNorth.y() + fac * vEquatorSouth.y(),
514 cmplFac * vEquatorNorth.z() + fac * vEquatorSouth.z());
520 float tTex = cmplFac * vtAspectNorth + fac * vtAspectSouth;
521 for (uint32_t j = 0; j < verifLonsp1; ++j) {
522 float sTex = sTexCache[j];
523 vertexTextures[vtCylOffset] = QVector2D(sTex, tTex);
530 uint32_t fCylOffset = idxFsCyl;
531 for (uint32_t m = 0; m < verifRingsp1; ++m) {
532 uint32_t vCurrRing = idxVNEquator + m * verifLons;
533 uint32_t vNextRing = vCurrRing + verifLons;
535 uint32_t vtCurrRing = idxVtNEquator + m * verifLonsp1;
536 uint32_t vtNextRing = vtCurrRing + verifLonsp1;
538 for (uint32_t j = 0; j < verifLons; ++j) {
539 uint32_t jNextVt = j + 1;
540 uint32_t jNextV = jNextVt % verifLons;
543 uint32_t v00 = vCurrRing + j;
544 uint32_t v01 = vNextRing + j;
545 uint32_t v11 = vNextRing + jNextV;
546 uint32_t v10 = vCurrRing + jNextV;
549 uint32_t vt00 = vtCurrRing + j;
550 uint32_t vt01 = vtNextRing + j;
551 uint32_t vt11 = vtNextRing + jNextVt;
552 uint32_t vt10 = vtCurrRing + jNextVt;
555 uint32_t vn0 = idxVNEquator + j;
556 uint32_t vn1 = idxVNEquator + jNextV;
558 faces[fCylOffset] = {
Face { v00, vt00, vn0 },
Face { v11, vt11, vn1 },
559 Face { v10, vt10, vn1 } };
561 faces[fCylOffset + 1] = {
Face { v00, vt00, vn0 },
Face { v01, vt01, vn0 },
562 Face { v11, vt11, vn1 } };
568 uint32_t stride = 3 *
sizeof(
float);
569 uint32_t strideNormal = 0;
570 uint32_t strideUV = 0;
572 if (m_enableNormals) {
573 strideNormal = stride;
574 stride += 3 *
sizeof(
float);
578 stride += 2 *
sizeof(
float);
581 QByteArray vertexData(vertices.length() * stride, Qt::Initialization::Uninitialized);
582 QByteArray indexData(faces.length() * 3 *
sizeof(quint32), Qt::Initialization::Uninitialized);
584 const auto getVertexPtr = [&](
const int vertexIdx) {
585 return reinterpret_cast<QVector3D *>(vertexData.data() + stride * vertexIdx);
587 const auto getNormalPtr = [&](
const int vertexIdx) {
588 return reinterpret_cast<QVector3D *>(vertexData.data() + stride * vertexIdx + strideNormal);
590 const auto getTexturePtr = [&](
const int vertexIdx) {
591 return reinterpret_cast<QVector2D *>(vertexData.data() + stride * vertexIdx + strideUV);
594 uint32_t *indexPtr =
reinterpret_cast<uint32_t *>(indexData.data());
596 for (qsizetype i = 0; i < vertices.length(); i++) {
597 *getVertexPtr(i) = vertices[i];
600 for (qsizetype i = 0; i < faces.length(); i++) {
601 const auto vertexIndices =
602 std::array<uint32_t, 3> { faces[i][0].vertexIdx, faces[i][1].vertexIdx,
603 faces[i][2].vertexIdx };
604 *indexPtr = vertexIndices[0];
606 *indexPtr = vertexIndices[1];
608 *indexPtr = vertexIndices[2];
611 if (m_enableNormals) {
612 const auto normalIndices =
613 std::array<uint32_t, 3> { faces[i][0].normalIdx, faces[i][1].normalIdx,
614 faces[i][2].normalIdx };
615 *getNormalPtr(vertexIndices[0]) = vertexNormals[normalIndices[0]];
616 *getNormalPtr(vertexIndices[1]) = vertexNormals[normalIndices[1]];
617 *getNormalPtr(vertexIndices[2]) = vertexNormals[normalIndices[2]];
621 const auto textureIndices =
622 std::array<uint32_t, 3> { faces[i][0].textureIdx, faces[i][1].textureIdx,
623 faces[i][2].textureIdx };
624 *getTexturePtr(vertexIndices[0]) = vertexTextures[textureIndices[0]];
625 *getTexturePtr(vertexIndices[1]) = vertexTextures[textureIndices[1]];
626 *getTexturePtr(vertexIndices[2]) = vertexTextures[textureIndices[2]];
630 addAttribute(QQuick3DGeometry::Attribute::PositionSemantic, 0,
631 QQuick3DGeometry::Attribute::ComponentType::F32Type);
632 if (m_enableNormals) {
633 addAttribute(QQuick3DGeometry::Attribute::NormalSemantic, strideNormal,
634 QQuick3DGeometry::Attribute::ComponentType::F32Type);
637 addAttribute(QQuick3DGeometry::Attribute::TexCoordSemantic, strideUV,
638 QQuick3DGeometry::Attribute::ComponentType::F32Type);
640 addAttribute(QQuick3DGeometry::Attribute::IndexSemantic, 0,
641 QQuick3DGeometry::Attribute::ComponentType::U32Type);
644 setVertexData(vertexData);
645 setIndexData(indexData);
647 setBounds(QVector3D(-radius - 0.5f * m_height, -radius, -radius),
648 QVector3D(radius + 0.5f * m_height, radius, radius));
Combined button and popup list for selecting options.