| 1746 | |
| 1747 | |
| 1748 | dtStatus dtBuildTileCachePolyMesh(dtTileCacheAlloc* alloc, |
| 1749 | dtTileCacheContourSet& lcset, |
| 1750 | dtTileCachePolyMesh& mesh) |
| 1751 | { |
| 1752 | dtAssert(alloc); |
| 1753 | |
| 1754 | int maxVertices = 0; |
| 1755 | int maxTris = 0; |
| 1756 | int maxVertsPerCont = 0; |
| 1757 | for (int i = 0; i < lcset.nconts; ++i) |
| 1758 | { |
| 1759 | // Skip null contours. |
| 1760 | if (lcset.conts[i].nverts < 3) continue; |
| 1761 | maxVertices += lcset.conts[i].nverts; |
| 1762 | maxTris += lcset.conts[i].nverts - 2; |
| 1763 | maxVertsPerCont = dtMax(maxVertsPerCont, lcset.conts[i].nverts); |
| 1764 | } |
| 1765 | |
| 1766 | // TODO: warn about too many vertices? |
| 1767 | |
| 1768 | mesh.nvp = MAX_VERTS_PER_POLY; |
| 1769 | |
| 1770 | dtFixedArray<unsigned char> vflags(alloc, maxVertices); |
| 1771 | if (!vflags) |
| 1772 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 1773 | memset(vflags, 0, maxVertices); |
| 1774 | |
| 1775 | mesh.verts = (unsigned short*)alloc->alloc(sizeof(unsigned short)*maxVertices*3); |
| 1776 | if (!mesh.verts) |
| 1777 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 1778 | |
| 1779 | mesh.polys = (unsigned short*)alloc->alloc(sizeof(unsigned short)*maxTris*MAX_VERTS_PER_POLY*2); |
| 1780 | if (!mesh.polys) |
| 1781 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 1782 | |
| 1783 | mesh.areas = (unsigned char*)alloc->alloc(sizeof(unsigned char)*maxTris); |
| 1784 | if (!mesh.areas) |
| 1785 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 1786 | |
| 1787 | mesh.flags = (unsigned short*)alloc->alloc(sizeof(unsigned short)*maxTris); |
| 1788 | if (!mesh.flags) |
| 1789 | return DT_FAILURE | DT_OUT_OF_MEMORY; |
| 1790 | |
| 1791 | // Just allocate and clean the mesh flags array. The user is resposible for filling it. |
| 1792 | memset(mesh.flags, 0, sizeof(unsigned short) * maxTris); |
| 1793 | |
| 1794 | mesh.nverts = 0; |
| 1795 | mesh.npolys = 0; |
| 1796 | |
| 1797 | memset(mesh.verts, 0, sizeof(unsigned short)*maxVertices*3); |
| 1798 | memset(mesh.polys, 0xff, sizeof(unsigned short)*maxTris*MAX_VERTS_PER_POLY*2); |
| 1799 | memset(mesh.areas, 0, sizeof(unsigned char)*maxTris); |
| 1800 | |
| 1801 | unsigned short firstVert[VERTEX_BUCKET_COUNT2]; |
| 1802 | for (int i = 0; i < VERTEX_BUCKET_COUNT2; ++i) |
| 1803 | firstVert[i] = DT_TILECACHE_NULL_IDX; |
| 1804 | |
| 1805 | dtFixedArray<unsigned short> nextVert(alloc, maxVertices); |
no test coverage detected