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
144144 intersectionDistance(FLT_MAX) {
145145 }
146146
147−static const float EIGHT_BIT_MAXIMUM_RECIPROCAL = 1.0f / 255.0f;
147+static const int EIGHT_BIT_MAXIMUM = 255;
148+static const float EIGHT_BIT_MAXIMUM_RECIPROCAL = 1.0f / EIGHT_BIT_MAXIMUM;
148149
149150 int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance) {
150151 if (!info.isLeaf) {
@@ -1267,8 +1268,6 @@ HeightfieldFetchVisitor::HeightfieldFetchVisitor(const MetavoxelLOD& lod, const · 1116 unmodified lines
12671268 _intersections(intersections) {
12681269 }
12691270
1270−const int EIGHT_BIT_MAXIMUM = 255;
1271−
12721271 int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) {
12731272 Box bounds = info.getBounds();
12741273 const Box& heightBounds = _buffer->getHeightBounds();
@@ -1549,6 +1548,14 @@ VoxelAugmentVisitor::VoxelAugmentVisitor(const MetavoxelLOD& lod) : · 274 unmodified lines
15491548 Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), lod) {
15501549 }
15511550
1551+class EdgeCrossing {
1552+public:
1553+ glm::vec3 point;
1554+ glm::vec3 normal;
1555+ QRgb color;
1556+ char material;
1557+};
1558+
15521559 int VoxelAugmentVisitor::visit(MetavoxelInfo& info) {
15531560 if (!info.isLeaf) {
15541561 return DEFAULT_ORDER;
@@ -1561,7 +1568,9 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 6 unmodified lines
15611568 QVector<VoxelPoint> vertices;
15621569 QVector<int> indices;
15631570
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();
15651574 int size = color->getSize();
15661575 int area = size * size;
15671576 int offset3 = size + 1;
@@ -1569,7 +1578,12 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line
15691578 int offset6 = area + size;
15701579 int offset7 = area + size + 1;
15711580
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();
15731587
15741588 int expanded = size + 1;
15751589 QVector<int> lineIndices(expanded, -1);
@@ -1577,60 +1591,256 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line
15771591 QVector<int> planeIndices(expanded * expanded, -1);
15781592 QVector<int> lastPlaneIndices(expanded * expanded, -1);
15791593
1594+ const int EDGES_PER_CUBE = 12;
1595+ EdgeCrossing crossings[EDGES_PER_CUBE];
1596+
15801597 float highest = size - 1.0f;
15811598 float scale = info.size / highest;
15821599 const int ALPHA_OFFSET = 24;
15831600 for (int z = 0; z < expanded; z++) {
1584− const QRgb* srcY = srcZ;
1601+ const QRgb* colorY = colorZ;
15851602 for (int y = 0; y < expanded; y++) {
15861603 int lastIndex;
1587− const QRgb* src = srcY;
1604+ const QRgb* colorX = colorY;
15881605 for (int x = 0; x < expanded; x++) {
1589− int alpha0 = src[0] >> ALPHA_OFFSET;
1606+ int alpha0 = colorX[0] >> ALPHA_OFFSET;
15901607 int alpha1 = alpha0, alpha2 = alpha0, alpha4 = alpha0;
15911608 int alphaTotal = alpha0;
15921609 int possibleTotal = EIGHT_BIT_MAXIMUM;
1593− if (x != 0 && x != size) {
1594− alphaTotal += (alpha1 = src[1] >> ALPHA_OFFSET);
1610+ bool middleX = (x != 0 && x != size);
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);
15951621 possibleTotal += EIGHT_BIT_MAXIMUM;
15961622
1597− if (y != 0 && y != size) {
1598− alphaTotal += (src[offset3] >> ALPHA_OFFSET);
1623+ if (middleZ) {
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);
15991636 possibleTotal += EIGHT_BIT_MAXIMUM;
16001637
1601− if (z != 0 && z != size) {
1602− alphaTotal += (src[offset7] >> ALPHA_OFFSET);
1638+ if (middleZ) {
1639+ alphaTotal += (alpha7 = colorX[offset7] >> ALPHA_OFFSET);
16031640 possibleTotal += EIGHT_BIT_MAXIMUM;
16041641 }
16051642 }
1606− if (z != 0 && z != size) {
1607− alphaTotal += (src[offset5] >> ALPHA_OFFSET);
1643+ if (middleZ) {
1644+ alphaTotal += (alpha5 = colorX[offset5] >> ALPHA_OFFSET);
16081645 possibleTotal += EIGHT_BIT_MAXIMUM;
16091646 }
16101647 }
1611− if (y != 0 && y != size) {
1612− alphaTotal += (alpha2 = src[size] >> ALPHA_OFFSET);
1613− possibleTotal += EIGHT_BIT_MAXIMUM;
1614−
1615− if (z != 0 && z != size) {
1616− alphaTotal += (src[offset6] >> ALPHA_OFFSET);
1617− possibleTotal += EIGHT_BIT_MAXIMUM;
1648+ if (alphaTotal == 0 || alphaTotal == possibleTotal) {
1649+ if (x != 0) {
1650+ colorX++;
16181651 }
1652+ continue; // no corners set/all corners set
16191653 }
1620− if (z != 0 && z != size) {
1621− alphaTotal += (alpha4 = src[area] >> ALPHA_OFFSET);
1622− possibleTotal += EIGHT_BIT_MAXIMUM;
1654+ int clampedX = qMax(x - 1, 0), clampedY = qMax(y - 1, 0), clampedZ = qMax(z - 1, 0);
1655+ const QRgb* hermiteBase = hermiteData + clampedZ * hermiteArea + clampedY * hermiteStride +
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+ }
16231770 }
1624−
1625− if (x != 0) {
1626− src++;
1771+ if (middleY) {
1772+ if (alpha0 != alpha2) {
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+ }
16271813 }
1628− if (alphaTotal == 0 || alphaTotal == possibleTotal) {
1629− continue; // no corners set/all corners set
1814+ if (middleZ && alpha0 != alpha4) {
1815+ 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);
16301837 }
1631− VoxelPoint point = { info.minimum + glm::vec3(glm::clamp(x - 0.5f, 0.0f, highest),
1632− glm::clamp(y - 0.5f, 0.0f, highest), glm::clamp(z - 0.5f, 0.0f, highest)) * scale,
1633− { 0, 0, 0 }, { 0, 127, 0} };
1838+ normal = glm::normalize(normal);
1839+ center /= crossingCount;
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) } };
16341844 int index = vertices.size();
16351845 vertices.append(point);
16361846
@@ -1686,17 +1896,21 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 49 unmodified lines
16861896 lastIndex = index;
16871897 lineIndices[x] = index;
16881898 planeIndices[y * expanded + x] = index;
1899+
1900+ if (x != 0) {
1901+ colorX++;
1902+ }
16891903 }
16901904 lineIndices.swap(lastLineIndices);
16911905
16921906 if (y != 0) {
1693− srcY += size;
1907+ colorY += size;
16941908 }
16951909 }
16961910 planeIndices.swap(lastPlaneIndices);
16971911
16981912 if (z != 0) {
1699− srcZ += area;
1913+ colorZ += area;
17001914 }
17011915 }
17021916