History

More dual contour bits.

Andrzej Kapolka committed 12 years ago

Commit fa06bef1 parent 12056392 files · +251 −37Browse files

Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 2 files). Show all

Unified · Split

interface/src/MetavoxelSystem.cpp+250−36
@@ -144,7 +144,8 @@ RayHeightfieldIntersectionVisitor::RayHeightfieldIntersectionVisitor(const glm:: · 143 unmodified lines
144 intersectionDistance(FLT_MAX) {144 intersectionDistance(FLT_MAX) {
145}145}
146 146
147static const float EIGHT_BIT_MAXIMUM_RECIPROCAL = 1.0f / 255.0f;147static const int EIGHT_BIT_MAXIMUM = 255;
148static const float EIGHT_BIT_MAXIMUM_RECIPROCAL = 1.0f / EIGHT_BIT_MAXIMUM;
148 149
149int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance) {150int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance) {
150 if (!info.isLeaf) {151 if (!info.isLeaf) {
@@ -1267,8 +1268,6 @@ HeightfieldFetchVisitor::HeightfieldFetchVisitor(const MetavoxelLOD& lod, const · 1116 unmodified lines
1267 _intersections(intersections) {1268 _intersections(intersections) {
1268}1269}
1269 1270
1270const int EIGHT_BIT_MAXIMUM = 255;
1271
1272int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) {1271int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) {
1273 Box bounds = info.getBounds();1272 Box bounds = info.getBounds();
1274 const Box& heightBounds = _buffer->getHeightBounds();1273 const Box& heightBounds = _buffer->getHeightBounds();
@@ -1549,6 +1548,14 @@ VoxelAugmentVisitor::VoxelAugmentVisitor(const MetavoxelLOD& lod) : · 274 unmodified lines
1549 Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), lod) {1548 Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), lod) {
1550}1549}
1551 1550
1551class EdgeCrossing {
1552public:
1553 glm::vec3 point;
1554 glm::vec3 normal;
1555 QRgb color;
1556 char material;
1557};
1558
1552int VoxelAugmentVisitor::visit(MetavoxelInfo& info) {1559int VoxelAugmentVisitor::visit(MetavoxelInfo& info) {
1553 if (!info.isLeaf) {1560 if (!info.isLeaf) {
1554 return DEFAULT_ORDER;1561 return DEFAULT_ORDER;
@@ -1561,7 +1568,9 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 6 unmodified lines
1561 QVector<VoxelPoint> vertices;1568 QVector<VoxelPoint> vertices;
1562 QVector<int> indices;1569 QVector<int> indices;
1563 1570
1564 const QVector<QRgb>& contents = color->getContents();1571 const QVector<QRgb>& colorContents = color->getContents();
1572 const QByteArray& materialContents = material->getContents();
1573 const QVector<QRgb>& hermiteContents = hermite->getContents();
1565 int size = color->getSize();1574 int size = color->getSize();
1566 int area = size * size;1575 int area = size * size;
1567 int offset3 = size + 1;1576 int offset3 = size + 1;
@@ -1569,7 +1578,12 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line
1569 int offset6 = area + size;1578 int offset6 = area + size;
1570 int offset7 = area + size + 1;1579 int offset7 = area + size + 1;
1571 1580
1572 const QRgb* srcZ = contents.constData();1581 const QRgb* colorZ = colorContents.constData();
1582 const QRgb* hermiteData = hermiteContents.constData();
1583 int hermiteStride = hermite->getSize() * VoxelHermiteData::EDGE_COUNT;
1584 int hermiteArea = hermiteStride * hermite->getSize();
1585
1586 const char* materialData = materialContents.constData();
1573 1587
1574 int expanded = size + 1;1588 int expanded = size + 1;
1575 QVector<int> lineIndices(expanded, -1);1589 QVector<int> lineIndices(expanded, -1);
@@ -1577,60 +1591,256 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line
1577 QVector<int> planeIndices(expanded * expanded, -1);1591 QVector<int> planeIndices(expanded * expanded, -1);
1578 QVector<int> lastPlaneIndices(expanded * expanded, -1);1592 QVector<int> lastPlaneIndices(expanded * expanded, -1);
1579 1593
1594 const int EDGES_PER_CUBE = 12;
1595 EdgeCrossing crossings[EDGES_PER_CUBE];
1596
1580 float highest = size - 1.0f;1597 float highest = size - 1.0f;
1581 float scale = info.size / highest;1598 float scale = info.size / highest;
1582 const int ALPHA_OFFSET = 24;1599 const int ALPHA_OFFSET = 24;
1583 for (int z = 0; z < expanded; z++) {1600 for (int z = 0; z < expanded; z++) {
1584 const QRgb* srcY = srcZ;1601 const QRgb* colorY = colorZ;
1585 for (int y = 0; y < expanded; y++) {1602 for (int y = 0; y < expanded; y++) {
1586 int lastIndex;1603 int lastIndex;
1587 const QRgb* src = srcY;1604 const QRgb* colorX = colorY;
1588 for (int x = 0; x < expanded; x++) {1605 for (int x = 0; x < expanded; x++) {
1589 int alpha0 = src[0] >> ALPHA_OFFSET;1606 int alpha0 = colorX[0] >> ALPHA_OFFSET;
1590 int alpha1 = alpha0, alpha2 = alpha0, alpha4 = alpha0;1607 int alpha1 = alpha0, alpha2 = alpha0, alpha4 = alpha0;
1591 int alphaTotal = alpha0;1608 int alphaTotal = alpha0;
1592 int possibleTotal = EIGHT_BIT_MAXIMUM;1609 int possibleTotal = EIGHT_BIT_MAXIMUM;
1593 if (x != 0 && x != size) {1610 bool middleX = (x != 0 && x != size);
1594 alphaTotal += (alpha1 = src[1] >> ALPHA_OFFSET);1611 bool middleY = (y != 0 && y != size);
1612 bool middleZ = (z != 0 && z != size);
1613 if (middleZ) {
1614 alphaTotal += (alpha4 = colorX[area] >> ALPHA_OFFSET);
1615 possibleTotal += EIGHT_BIT_MAXIMUM;
1616 }
1617
1618 int alpha5 = alpha4, alpha6 = alpha4;
1619 if (middleY) {
1620 alphaTotal += (alpha2 = colorX[size] >> ALPHA_OFFSET);
1595 possibleTotal += EIGHT_BIT_MAXIMUM;1621 possibleTotal += EIGHT_BIT_MAXIMUM;
1596 1622
1597 if (y != 0 && y != size) {1623 if (middleZ) {
1598 alphaTotal += (src[offset3] >> ALPHA_OFFSET);1624 alphaTotal += (alpha6 = colorX[offset6] >> ALPHA_OFFSET);
1625 possibleTotal += EIGHT_BIT_MAXIMUM;
1626 }
1627 }
1628
1629 int alpha3 = alpha2, alpha7 = alpha6;
1630 if (middleX) {
1631 alphaTotal += (alpha1 = colorX[1] >> ALPHA_OFFSET);
1632 possibleTotal += EIGHT_BIT_MAXIMUM;
1633
1634 if (middleY) {
1635 alphaTotal += (alpha3 = colorX[offset3] >> ALPHA_OFFSET);
1599 possibleTotal += EIGHT_BIT_MAXIMUM;1636 possibleTotal += EIGHT_BIT_MAXIMUM;
1600 1637
1601 if (z != 0 && z != size) {1638 if (middleZ) {
1602 alphaTotal += (src[offset7] >> ALPHA_OFFSET);1639 alphaTotal += (alpha7 = colorX[offset7] >> ALPHA_OFFSET);
1603 possibleTotal += EIGHT_BIT_MAXIMUM;1640 possibleTotal += EIGHT_BIT_MAXIMUM;
1604 }1641 }
1605 }1642 }
1606 if (z != 0 && z != size) {1643 if (middleZ) {
1607 alphaTotal += (src[offset5] >> ALPHA_OFFSET);1644 alphaTotal += (alpha5 = colorX[offset5] >> ALPHA_OFFSET);
1608 possibleTotal += EIGHT_BIT_MAXIMUM;1645 possibleTotal += EIGHT_BIT_MAXIMUM;
1609 }1646 }
1610 }1647 }
1611 if (y != 0 && y != size) {1648 if (alphaTotal == 0 || alphaTotal == possibleTotal) {
1612 alphaTotal += (alpha2 = src[size] >> ALPHA_OFFSET);1649 if (x != 0) {
1613 possibleTotal += EIGHT_BIT_MAXIMUM;1650 colorX++;
1614
1615 if (z != 0 && z != size) {
1616 alphaTotal += (src[offset6] >> ALPHA_OFFSET);
1617 possibleTotal += EIGHT_BIT_MAXIMUM;
1618 }1651 }
1652 continue; // no corners set/all corners set
1619 }1653 }
1620 if (z != 0 && z != size) {1654 int clampedX = qMax(x - 1, 0), clampedY = qMax(y - 1, 0), clampedZ = qMax(z - 1, 0);
1621 alphaTotal += (alpha4 = src[area] >> ALPHA_OFFSET);1655 const QRgb* hermiteBase = hermiteData + clampedZ * hermiteArea + clampedY * hermiteStride +
1622 possibleTotal += EIGHT_BIT_MAXIMUM;1656 clampedX * VoxelHermiteData::EDGE_COUNT;
1657 const char* materialBase = materialData + clampedZ * area + clampedY * size + clampedX;
1658 int crossingCount = 0;
1659 if (middleX) {
1660 if (alpha0 != alpha1) {
1661 QRgb hermite = hermiteBase[0];
1662 EdgeCrossing& crossing = crossings[crossingCount++];
1663 crossing.normal = unpackNormal(hermite);
1664 if (alpha0 == 0) {
1665 crossing.color = colorX[1];
1666 crossing.material = materialBase[1];
1667 } else {
1668 crossing.color = colorX[0];
1669 crossing.material = materialBase[0];
1670 }
1671 crossing.point = glm::vec3(qAlpha(hermite), 0.0f, 0.0f);
1672 }
1673 if (middleY) {
1674 if (alpha1 != alpha3) {
1675 QRgb hermite = hermiteBase[VoxelHermiteData::EDGE_COUNT + 1];
1676 EdgeCrossing& crossing = crossings[crossingCount++];
1677 crossing.normal = unpackNormal(hermite);
1678 if (alpha1 == 0) {
1679 crossing.color = colorX[offset3];
1680 crossing.material = materialBase[offset3];
1681 } else {
1682 crossing.color = colorX[1];
1683 crossing.material = materialBase[1];
1684 }
1685 crossing.point = glm::vec3(EIGHT_BIT_MAXIMUM, qAlpha(hermite), 0.0f);
1686 }
1687 if (alpha2 != alpha3) {
1688 QRgb hermite = hermiteBase[hermiteStride];
1689 EdgeCrossing& crossing = crossings[crossingCount++];
1690 crossing.normal = unpackNormal(hermite);
1691 if (alpha2 == 0) {
1692 crossing.color = colorX[offset3];
1693 crossing.material = materialBase[offset3];
1694 } else {
1695 crossing.color = colorX[size];
1696 crossing.material = materialBase[size];
1697 }
1698 crossing.point = glm::vec3(qAlpha(hermite), EIGHT_BIT_MAXIMUM, 0.0f);
1699 }
1700 if (middleZ) {
1701 if (alpha3 != alpha7) {
1702 QRgb hermite = hermiteBase[hermiteStride + VoxelHermiteData::EDGE_COUNT + 2];
1703 EdgeCrossing& crossing = crossings[crossingCount++];
1704 crossing.normal = unpackNormal(hermite);
1705 if (alpha3 == 0) {
1706 crossing.color = colorX[offset7];
1707 crossing.material = materialBase[offset7];
1708 } else {
1709 crossing.color = colorX[offset3];
1710 crossing.material = materialBase[offset3];
1711 }
1712 crossing.point = glm::vec3(EIGHT_BIT_MAXIMUM, EIGHT_BIT_MAXIMUM, qAlpha(hermite));
1713 }
1714 if (alpha5 != alpha7) {
1715 QRgb hermite = hermiteBase[hermiteArea + VoxelHermiteData::EDGE_COUNT + 1];
1716 EdgeCrossing& crossing = crossings[crossingCount++];
1717 crossing.normal = unpackNormal(hermite);
1718 if (alpha5 == 0) {
1719 crossing.color = colorX[offset7];
1720 crossing.material = materialBase[offset7];
1721 } else {
1722 crossing.color = colorX[offset5];
1723 crossing.material = materialBase[offset5];
1724 }
1725 crossing.point = glm::vec3(EIGHT_BIT_MAXIMUM, qAlpha(hermite), EIGHT_BIT_MAXIMUM);
1726 }
1727 if (alpha6 != alpha7) {
1728 QRgb hermite = hermiteBase[hermiteArea + hermiteStride];
1729 EdgeCrossing& crossing = crossings[crossingCount++];
1730 crossing.normal = unpackNormal(hermite);
1731 if (alpha6 == 0) {
1732 crossing.color = colorX[offset7];
1733 crossing.material = materialBase[offset7];
1734 } else {
1735 crossing.color = colorX[offset6];
1736 crossing.material = materialBase[offset6];
1737 }
1738 crossing.point = glm::vec3(qAlpha(hermite), EIGHT_BIT_MAXIMUM, EIGHT_BIT_MAXIMUM);
1739 }
1740 }
1741 }
1742 if (middleZ) {
1743 if (alpha1 != alpha5) {
1744 QRgb hermite = hermiteBase[VoxelHermiteData::EDGE_COUNT + 2];
1745 EdgeCrossing& crossing = crossings[crossingCount++];
1746 crossing.normal = unpackNormal(hermite);
1747 if (alpha1 == 0) {
1748 crossing.color = colorX[offset5];
1749 crossing.material = materialBase[offset5];
1750 } else {
1751 crossing.color = colorX[1];
1752 crossing.material = materialBase[1];
1753 }
1754 crossing.point = glm::vec3(EIGHT_BIT_MAXIMUM, 0.0f, qAlpha(hermite));
1755 }
1756 if (alpha4 != alpha5) {
1757 QRgb hermite = hermiteBase[hermiteArea];
1758 EdgeCrossing& crossing = crossings[crossingCount++];
1759 crossing.normal = unpackNormal(hermite);
1760 if (alpha4 == 0) {
1761 crossing.color = colorX[offset5];
1762 crossing.material = materialBase[offset5];
1763 } else {
1764 crossing.color = colorX[area];
1765 crossing.material = materialBase[area];
1766 }
1767 crossing.point = glm::vec3(qAlpha(hermite), 0.0f, EIGHT_BIT_MAXIMUM);
1768 }
1769 }
1623 }1770 }
1624 1771 if (middleY) {
1625 if (x != 0) {1772 if (alpha0 != alpha2) {
1626 src++;1773 QRgb hermite = hermiteBase[1];
1774 EdgeCrossing& crossing = crossings[crossingCount++];
1775 crossing.normal = unpackNormal(hermite);
1776 if (alpha0 == 0) {
1777 crossing.color = colorX[size];
1778 crossing.material = materialBase[size];
1779 } else {
1780 crossing.color = colorX[0];
1781 crossing.material = materialBase[0];
1782 }
1783 crossing.point = glm::vec3(0.0f, qAlpha(hermite), 0.0f);
1784 }
1785 if (middleZ) {
1786 if (alpha2 != alpha6) {
1787 QRgb hermite = hermiteBase[hermiteStride + 2];
1788 EdgeCrossing& crossing = crossings[crossingCount++];
1789 crossing.normal = unpackNormal(hermite);
1790 if (alpha2 == 0) {
1791 crossing.color = colorX[offset6];
1792 crossing.material = materialBase[offset6];
1793 } else {
1794 crossing.color = colorX[size];
1795 crossing.material = materialBase[size];
1796 }
1797 crossing.point = glm::vec3(0.0f, EIGHT_BIT_MAXIMUM, qAlpha(hermite));
1798 }
1799 if (alpha4 != alpha6) {
1800 QRgb hermite = hermiteBase[hermiteArea + 1];
1801 EdgeCrossing& crossing = crossings[crossingCount++];
1802 crossing.normal = unpackNormal(hermite);
1803 if (alpha4 == 0) {
1804 crossing.color = colorX[offset6];
1805 crossing.material = materialBase[offset6];
1806 } else {
1807 crossing.color = colorX[area];
1808 crossing.material = materialBase[area];
1809 }
1810 crossing.point = glm::vec3(0.0f, qAlpha(hermite), EIGHT_BIT_MAXIMUM);
1811 }
1812 }
1627 }1813 }
1628 if (alphaTotal == 0 || alphaTotal == possibleTotal) {1814 if (middleZ && alpha0 != alpha4) {
1629 continue; // no corners set/all corners set1815 QRgb hermite = hermiteBase[2];
1816 EdgeCrossing& crossing = crossings[crossingCount++];
1817 crossing.normal = unpackNormal(hermite);
1818 if (alpha0 == 0) {
1819 crossing.color = colorX[area];
1820 crossing.material = materialBase[area];
1821 } else {
1822 crossing.color = colorX[0];
1823 crossing.material = materialBase[0];
1824 }
1825 crossing.point = glm::vec3(0.0f, 0.0f, qAlpha(hermite));
1826 }
1827 glm::vec3 center;
1828 glm::vec3 normal;
1829 int red = 0, green = 0, blue = 0;
1830 for (int i = 0; i < crossingCount; i++) {
1831 const EdgeCrossing& crossing = crossings[i];
1832 center += crossing.point;
1833 normal += crossing.normal;
1834 red += qRed(crossing.color);
1835 green += qGreen(crossing.color);
1836 blue += qBlue(crossing.color);
1630 }1837 }
1631 VoxelPoint point = { info.minimum + glm::vec3(glm::clamp(x - 0.5f, 0.0f, highest),1838 normal = glm::normalize(normal);
1632 glm::clamp(y - 0.5f, 0.0f, highest), glm::clamp(z - 0.5f, 0.0f, highest)) * scale,1839 center /= crossingCount;
1633 { 0, 0, 0 }, { 0, 127, 0} };1840 VoxelPoint point = { info.minimum + (glm::vec3(clampedX, clampedY, clampedZ) +
1841 center * EIGHT_BIT_MAXIMUM_RECIPROCAL) * scale,
1842 { (quint8)(red / crossingCount), (quint8)(green / crossingCount), (quint8)(blue / crossingCount) },
1843 { (char)(normal.x * 127.0f), (char)(normal.y * 127.0f), (char)(normal.z * 127.0f) } };
1634 int index = vertices.size();1844 int index = vertices.size();
1635 vertices.append(point);1845 vertices.append(point);
1636 1846
@@ -1686,17 +1896,21 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 49 unmodified lines
1686 lastIndex = index;1896 lastIndex = index;
1687 lineIndices[x] = index;1897 lineIndices[x] = index;
1688 planeIndices[y * expanded + x] = index;1898 planeIndices[y * expanded + x] = index;
1899
1900 if (x != 0) {
1901 colorX++;
1902 }
1689 }1903 }
1690 lineIndices.swap(lastLineIndices);1904 lineIndices.swap(lastLineIndices);
1691 1905
1692 if (y != 0) {1906 if (y != 0) {
1693 srcY += size;1907 colorY += size;
1694 }1908 }
1695 }1909 }
1696 planeIndices.swap(lastPlaneIndices);1910 planeIndices.swap(lastPlaneIndices);
1697 1911
1698 if (z != 0) {1912 if (z != 0) {
1699 srcZ += area;1913 colorZ += area;
1700 }1914 }
1701 }1915 }
1702 1916