1557 const QVector<QVector3D> &normals,
1558 const QVector<quint32> &indexes,
1559 QVector<MeshVertexSplit> &splitVertices,
1560 float normalMergeAngle,
1561 float normalSplitAngle)
1566 splitVertices.clear();
1568 if (positions.isEmpty() || indexes.isEmpty())
1573 const bool recalculateNormals = normals.size() == positions.size()
1574 && !(qFuzzyIsNull(normalMergeAngle) && qFuzzyIsNull(normalSplitAngle));
1575 const float normalMergeThreshold = qCos(qDegreesToRadians(normalMergeAngle));
1576 const float normalSplitThreshold = qCos(qDegreesToRadians(normalSplitAngle));
1578 quint32 splitVertexCount = positions.size();
1597 QVector<quint32> weldRemap(positions.size());
1598 QVarLengthArray<MeshVertexStream, 2> streams;
1599 streams.append({ positions.constData(),
sizeof(QVector3D),
sizeof(QVector3D) });
1600 if (!recalculateNormals && normals.size() == positions.size())
1601 streams.append({ normals.constData(),
sizeof(QVector3D),
sizeof(QVector3D) });
1602 const quint32 weldedVertexCount = quint32(generateVertexRemap(weldRemap.data(), indexes.constData(),
1604 positions.size(), streams.constData(),
1605 size_t(streams.size())));
1606 QVector<quint32> weldedIndexes(indexes.size());
1607 remapIndexBuffer(weldedIndexes.data(), indexes.constData(), indexes.size(), weldRemap.constData());
1608 QVector<QVector3D> weldedPositions(weldedVertexCount);
1609 remapVertexBuffer(weldedPositions.data(), positions.constData(), positions.size(),
sizeof(QVector3D),
1610 weldRemap.constData());
1617 constexpr quint32 unusedVertex = std::numeric_limits<quint32>::max();
1618 QVector<quint32> weldedToOriginal(weldedVertexCount, unusedVertex);
1619 for (quint32 i = 0, end = quint32(positions.size()); i < end; ++i) {
1620 const quint32 welded = weldRemap.at(i);
1622 if (welded != unusedVertex && weldedToOriginal.at(welded) == unusedVertex)
1623 weldedToOriginal[welded] = i;
1626 const float targetError = std::numeric_limits<
float>::max();
1627 const float *vertexData =
reinterpret_cast<
const float *>(weldedPositions.constData());
1628 const float scaleFactor = simplifyScale(vertexData, weldedVertexCount,
sizeof(QVector3D));
1629 const quint32 indexCount = indexes.size();
1633 const quint32 maxLevelIndexes = indexCount - indexCount / 4 - 1;
1634 const quint32 maxIndexTarget = maxLevelIndexes - maxLevelIndexes % 3;
1635 quint32 indexTarget = 12;
1636 quint32 lastIndexCount = 0;
1637 QVector<MeshLevelOfDetail> lods;
1639 while (indexTarget < indexCount) {
1641 QVector<quint32> newIndexes;
1642 newIndexes.resize(indexCount);
1643 size_t newLength = simplifyMesh(newIndexes.data(), weldedIndexes.constData(), weldedIndexes.size(),
1644 vertexData, weldedVertexCount,
sizeof(QVector3D), indexTarget,
1645 targetError, 0, &error);
1648 if (newLength < lastIndexCount + (lastIndexCount + 1) / 2) {
1649 const quint32 nextTarget = qMin(indexTarget + indexTarget / 2, maxIndexTarget);
1650 if (nextTarget <= indexTarget)
1652 indexTarget = nextTarget;
1657 if (newLength == 0 || newLength > maxLevelIndexes)
1660 newIndexes.resize(newLength);
1664 for (quint32 &index : newIndexes)
1665 index = weldedToOriginal.at(index);
1668 if (recalculateNormals) {
1670 QVector<QVector3D> faceNormals;
1672 QVector<quint32> culledIndexes;
1673 for (quint32 j = 0; j < quint32(newIndexes.size()); j += 3) {
1674 const QVector3D &v0 = positions[newIndexes[j]];
1675 const QVector3D &v1 = positions[newIndexes[j + 1]];
1676 const QVector3D &v2 = positions[newIndexes[j + 2]];
1678 QVector3D faceNormal = QVector3D::crossProduct(v1 - v0, v2 - v0);
1680 const float faceArea = QSSGUtils::vec3::normalize(faceNormal);
1685 if (faceArea != 0.0f) {
1686 faceNormals.append(faceNormal);
1687 faceNormals.append(faceNormal);
1688 faceNormals.append(faceNormal);
1689 culledIndexes.append({newIndexes[j], newIndexes[j + 1], newIndexes[j + 2]});
1693 if (newIndexes.size() != culledIndexes.size())
1694 newIndexes = culledIndexes;
1699 const quint32 newIndexCount = quint32(newIndexes.size());
1700 QHash<QVector3D, QVector<quint32>> positionHash;
1701 for (quint32 i = 0; i < newIndexCount; ++i) {
1702 const quint32 index = newIndexes[i];
1703 const QVector3D position = positions[index];
1704 positionHash[position].append(i);
1711 QVector<QPair<quint32, quint32>> remapIndexes;
1712 for (quint32 positionIndex = 0; positionIndex < newIndexCount; ++positionIndex) {
1713 const quint32 index = newIndexes[positionIndex];
1714 const QVector3D &position = positions[index];
1715 const QVector3D &faceNormal = faceNormals[positionIndex];
1716 QVector3D newNormal;
1718 const auto &sharedPositions = positionHash.value(position);
1719 for (
const auto positionIndex2 : sharedPositions) {
1720 if (positionIndex == positionIndex2) {
1722 newNormal += faceNormal;
1724 const QVector3D &faceNormal2 = faceNormals[positionIndex2];
1725 if (QVector3D::dotProduct(faceNormal2, faceNormal) >= normalMergeThreshold)
1726 newNormal += faceNormal2;
1731 QSSGUtils::vec3::normalize(newNormal);
1742 const QVector3D &originalNormal = normals[index];
1743 const float theta = QVector3D::dotProduct(originalNormal, newNormal);
1744 if (theta < normalSplitThreshold) {
1745 splitVertices.append({ index, newNormal.normalized() });
1746 remapIndexes.append({positionIndex, splitVertexCount++});
1751 for (
const auto &pair : std::as_const(remapIndexes))
1752 newIndexes[pair.first] = pair.second;
1755 lods.append({error * scaleFactor, newIndexes});
1756 lastIndexCount = quint32(newLength);
1759 if (lastIndexCount + (lastIndexCount + 1) / 2 > maxLevelIndexes)
1762 const size_t doubled = qMax(newLength, size_t(indexTarget)) * 2;
1763 const quint32 nextTarget = quint32(qMin<size_t>(doubled, maxIndexTarget));
1764 if (nextTarget <= indexTarget)
1766 indexTarget = nextTarget;