RecastMeshDetail.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <float.h>
  19. #define _USE_MATH_DEFINES
  20. #include <math.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include "Recast.h"
  25. #include "RecastAlloc.h"
  26. #include "RecastAssert.h"
  27. static const unsigned RC_UNSET_HEIGHT = 0xffff;
  28. struct rcHeightPatch
  29. {
  30. inline rcHeightPatch() : data(0), xmin(0), ymin(0), width(0), height(0) {}
  31. inline ~rcHeightPatch() { rcFree(data); }
  32. unsigned short* data;
  33. int xmin, ymin, width, height;
  34. };
  35. inline float vdot2(const float* a, const float* b)
  36. {
  37. return a[0]*b[0] + a[2]*b[2];
  38. }
  39. inline float vdistSq2(const float* p, const float* q)
  40. {
  41. const float dx = q[0] - p[0];
  42. const float dy = q[2] - p[2];
  43. return dx*dx + dy*dy;
  44. }
  45. inline float vdist2(const float* p, const float* q)
  46. {
  47. return sqrtf(vdistSq2(p,q));
  48. }
  49. inline float vcross2(const float* p1, const float* p2, const float* p3)
  50. {
  51. const float u1 = p2[0] - p1[0];
  52. const float v1 = p2[2] - p1[2];
  53. const float u2 = p3[0] - p1[0];
  54. const float v2 = p3[2] - p1[2];
  55. return u1 * v2 - v1 * u2;
  56. }
  57. static bool circumCircle(const float* p1, const float* p2, const float* p3,
  58. float* c, float& r)
  59. {
  60. static const float EPS = 1e-6f;
  61. const float cp = vcross2(p1, p2, p3);
  62. if (fabsf(cp) > EPS)
  63. {
  64. const float p1Sq = vdot2(p1,p1);
  65. const float p2Sq = vdot2(p2,p2);
  66. const float p3Sq = vdot2(p3,p3);
  67. c[0] = (p1Sq*(p2[2]-p3[2]) + p2Sq*(p3[2]-p1[2]) + p3Sq*(p1[2]-p2[2])) / (2*cp);
  68. c[2] = (p1Sq*(p3[0]-p2[0]) + p2Sq*(p1[0]-p3[0]) + p3Sq*(p2[0]-p1[0])) / (2*cp);
  69. r = vdist2(c, p1);
  70. return true;
  71. }
  72. c[0] = p1[0];
  73. c[2] = p1[2];
  74. r = 0;
  75. return false;
  76. }
  77. static float distPtTri(const float* p, const float* a, const float* b, const float* c)
  78. {
  79. float v0[3], v1[3], v2[3];
  80. rcVsub(v0, c,a);
  81. rcVsub(v1, b,a);
  82. rcVsub(v2, p,a);
  83. const float dot00 = vdot2(v0, v0);
  84. const float dot01 = vdot2(v0, v1);
  85. const float dot02 = vdot2(v0, v2);
  86. const float dot11 = vdot2(v1, v1);
  87. const float dot12 = vdot2(v1, v2);
  88. // Compute barycentric coordinates
  89. const float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
  90. const float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  91. float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
  92. // If point lies inside the triangle, return interpolated y-coord.
  93. static const float EPS = 1e-4f;
  94. if (u >= -EPS && v >= -EPS && (u+v) <= 1+EPS)
  95. {
  96. const float y = a[1] + v0[1]*u + v1[1]*v;
  97. return fabsf(y-p[1]);
  98. }
  99. return FLT_MAX;
  100. }
  101. static float distancePtSeg(const float* pt, const float* p, const float* q)
  102. {
  103. float pqx = q[0] - p[0];
  104. float pqy = q[1] - p[1];
  105. float pqz = q[2] - p[2];
  106. float dx = pt[0] - p[0];
  107. float dy = pt[1] - p[1];
  108. float dz = pt[2] - p[2];
  109. float d = pqx*pqx + pqy*pqy + pqz*pqz;
  110. float t = pqx*dx + pqy*dy + pqz*dz;
  111. if (d > 0)
  112. t /= d;
  113. if (t < 0)
  114. t = 0;
  115. else if (t > 1)
  116. t = 1;
  117. dx = p[0] + t*pqx - pt[0];
  118. dy = p[1] + t*pqy - pt[1];
  119. dz = p[2] + t*pqz - pt[2];
  120. return dx*dx + dy*dy + dz*dz;
  121. }
  122. static float distancePtSeg2d(const float* pt, const float* p, const float* q)
  123. {
  124. float pqx = q[0] - p[0];
  125. float pqz = q[2] - p[2];
  126. float dx = pt[0] - p[0];
  127. float dz = pt[2] - p[2];
  128. float d = pqx*pqx + pqz*pqz;
  129. float t = pqx*dx + pqz*dz;
  130. if (d > 0)
  131. t /= d;
  132. if (t < 0)
  133. t = 0;
  134. else if (t > 1)
  135. t = 1;
  136. dx = p[0] + t*pqx - pt[0];
  137. dz = p[2] + t*pqz - pt[2];
  138. return dx*dx + dz*dz;
  139. }
  140. static float distToTriMesh(const float* p, const float* verts, const int /*nverts*/, const int* tris, const int ntris)
  141. {
  142. float dmin = FLT_MAX;
  143. for (int i = 0; i < ntris; ++i)
  144. {
  145. const float* va = &verts[tris[i*4+0]*3];
  146. const float* vb = &verts[tris[i*4+1]*3];
  147. const float* vc = &verts[tris[i*4+2]*3];
  148. float d = distPtTri(p, va,vb,vc);
  149. if (d < dmin)
  150. dmin = d;
  151. }
  152. if (dmin == FLT_MAX) return -1;
  153. return dmin;
  154. }
  155. static float distToPoly(int nvert, const float* verts, const float* p)
  156. {
  157. float dmin = FLT_MAX;
  158. int i, j, c = 0;
  159. for (i = 0, j = nvert-1; i < nvert; j = i++)
  160. {
  161. const float* vi = &verts[i*3];
  162. const float* vj = &verts[j*3];
  163. if (((vi[2] > p[2]) != (vj[2] > p[2])) &&
  164. (p[0] < (vj[0]-vi[0]) * (p[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) )
  165. c = !c;
  166. dmin = rcMin(dmin, distancePtSeg2d(p, vj, vi));
  167. }
  168. return c ? -dmin : dmin;
  169. }
  170. static unsigned short getHeight(const float fx, const float fy, const float fz,
  171. const float /*cs*/, const float ics, const float ch,
  172. const rcHeightPatch& hp)
  173. {
  174. int ix = (int)floorf(fx*ics + 0.01f);
  175. int iz = (int)floorf(fz*ics + 0.01f);
  176. ix = rcClamp(ix-hp.xmin, 0, hp.width);
  177. iz = rcClamp(iz-hp.ymin, 0, hp.height);
  178. unsigned short h = hp.data[ix+iz*hp.width];
  179. if (h == RC_UNSET_HEIGHT)
  180. {
  181. // Special case when data might be bad.
  182. // Find nearest neighbour pixel which has valid height.
  183. const int off[8*2] = { -1,0, -1,-1, 0,-1, 1,-1, 1,0, 1,1, 0,1, -1,1};
  184. float dmin = FLT_MAX;
  185. for (int i = 0; i < 8; ++i)
  186. {
  187. const int nx = ix+off[i*2+0];
  188. const int nz = iz+off[i*2+1];
  189. if (nx < 0 || nz < 0 || nx >= hp.width || nz >= hp.height) continue;
  190. const unsigned short nh = hp.data[nx+nz*hp.width];
  191. if (nh == RC_UNSET_HEIGHT) continue;
  192. const float d = fabsf(nh*ch - fy);
  193. if (d < dmin)
  194. {
  195. h = nh;
  196. dmin = d;
  197. }
  198. /* const float dx = (nx+0.5f)*cs - fx;
  199. const float dz = (nz+0.5f)*cs - fz;
  200. const float d = dx*dx+dz*dz;
  201. if (d < dmin)
  202. {
  203. h = nh;
  204. dmin = d;
  205. } */
  206. }
  207. }
  208. return h;
  209. }
  210. enum EdgeValues
  211. {
  212. UNDEF = -1,
  213. HULL = -2,
  214. };
  215. static int findEdge(const int* edges, int nedges, int s, int t)
  216. {
  217. for (int i = 0; i < nedges; i++)
  218. {
  219. const int* e = &edges[i*4];
  220. if ((e[0] == s && e[1] == t) || (e[0] == t && e[1] == s))
  221. return i;
  222. }
  223. return UNDEF;
  224. }
  225. static int addEdge(rcContext* ctx, int* edges, int& nedges, const int maxEdges, int s, int t, int l, int r)
  226. {
  227. if (nedges >= maxEdges)
  228. {
  229. ctx->log(RC_LOG_ERROR, "addEdge: Too many edges (%d/%d).", nedges, maxEdges);
  230. return UNDEF;
  231. }
  232. // Add edge if not already in the triangulation.
  233. int e = findEdge(edges, nedges, s, t);
  234. if (e == UNDEF)
  235. {
  236. int* edge = &edges[nedges*4];
  237. edge[0] = s;
  238. edge[1] = t;
  239. edge[2] = l;
  240. edge[3] = r;
  241. return nedges++;
  242. }
  243. else
  244. {
  245. return UNDEF;
  246. }
  247. }
  248. static void updateLeftFace(int* e, int s, int t, int f)
  249. {
  250. if (e[0] == s && e[1] == t && e[2] == UNDEF)
  251. e[2] = f;
  252. else if (e[1] == s && e[0] == t && e[3] == UNDEF)
  253. e[3] = f;
  254. }
  255. static int overlapSegSeg2d(const float* a, const float* b, const float* c, const float* d)
  256. {
  257. const float a1 = vcross2(a, b, d);
  258. const float a2 = vcross2(a, b, c);
  259. if (a1*a2 < 0.0f)
  260. {
  261. float a3 = vcross2(c, d, a);
  262. float a4 = a3 + a2 - a1;
  263. if (a3 * a4 < 0.0f)
  264. return 1;
  265. }
  266. return 0;
  267. }
  268. static bool overlapEdges(const float* pts, const int* edges, int nedges, int s1, int t1)
  269. {
  270. for (int i = 0; i < nedges; ++i)
  271. {
  272. const int s0 = edges[i*4+0];
  273. const int t0 = edges[i*4+1];
  274. // Same or connected edges do not overlap.
  275. if (s0 == s1 || s0 == t1 || t0 == s1 || t0 == t1)
  276. continue;
  277. if (overlapSegSeg2d(&pts[s0*3],&pts[t0*3], &pts[s1*3],&pts[t1*3]))
  278. return true;
  279. }
  280. return false;
  281. }
  282. static void completeFacet(rcContext* ctx, const float* pts, int npts, int* edges, int& nedges, const int maxEdges, int& nfaces, int e)
  283. {
  284. static const float EPS = 1e-5f;
  285. int* edge = &edges[e*4];
  286. // Cache s and t.
  287. int s,t;
  288. if (edge[2] == UNDEF)
  289. {
  290. s = edge[0];
  291. t = edge[1];
  292. }
  293. else if (edge[3] == UNDEF)
  294. {
  295. s = edge[1];
  296. t = edge[0];
  297. }
  298. else
  299. {
  300. // Edge already completed.
  301. return;
  302. }
  303. // Find best point on left of edge.
  304. int pt = npts;
  305. float c[3] = {0,0,0};
  306. float r = -1;
  307. for (int u = 0; u < npts; ++u)
  308. {
  309. if (u == s || u == t) continue;
  310. if (vcross2(&pts[s*3], &pts[t*3], &pts[u*3]) > EPS)
  311. {
  312. if (r < 0)
  313. {
  314. // The circle is not updated yet, do it now.
  315. pt = u;
  316. circumCircle(&pts[s*3], &pts[t*3], &pts[u*3], c, r);
  317. continue;
  318. }
  319. const float d = vdist2(c, &pts[u*3]);
  320. const float tol = 0.001f;
  321. if (d > r*(1+tol))
  322. {
  323. // Outside current circumcircle, skip.
  324. continue;
  325. }
  326. else if (d < r*(1-tol))
  327. {
  328. // Inside safe circumcircle, update circle.
  329. pt = u;
  330. circumCircle(&pts[s*3], &pts[t*3], &pts[u*3], c, r);
  331. }
  332. else
  333. {
  334. // Inside epsilon circum circle, do extra tests to make sure the edge is valid.
  335. // s-u and t-u cannot overlap with s-pt nor t-pt if they exists.
  336. if (overlapEdges(pts, edges, nedges, s,u))
  337. continue;
  338. if (overlapEdges(pts, edges, nedges, t,u))
  339. continue;
  340. // Edge is valid.
  341. pt = u;
  342. circumCircle(&pts[s*3], &pts[t*3], &pts[u*3], c, r);
  343. }
  344. }
  345. }
  346. // Add new triangle or update edge info if s-t is on hull.
  347. if (pt < npts)
  348. {
  349. // Update face information of edge being completed.
  350. updateLeftFace(&edges[e*4], s, t, nfaces);
  351. // Add new edge or update face info of old edge.
  352. e = findEdge(edges, nedges, pt, s);
  353. if (e == UNDEF)
  354. addEdge(ctx, edges, nedges, maxEdges, pt, s, nfaces, UNDEF);
  355. else
  356. updateLeftFace(&edges[e*4], pt, s, nfaces);
  357. // Add new edge or update face info of old edge.
  358. e = findEdge(edges, nedges, t, pt);
  359. if (e == UNDEF)
  360. addEdge(ctx, edges, nedges, maxEdges, t, pt, nfaces, UNDEF);
  361. else
  362. updateLeftFace(&edges[e*4], t, pt, nfaces);
  363. nfaces++;
  364. }
  365. else
  366. {
  367. updateLeftFace(&edges[e*4], s, t, HULL);
  368. }
  369. }
  370. static void delaunayHull(rcContext* ctx, const int npts, const float* pts,
  371. const int nhull, const int* hull,
  372. rcIntArray& tris, rcIntArray& edges)
  373. {
  374. int nfaces = 0;
  375. int nedges = 0;
  376. const int maxEdges = npts*10;
  377. edges.resize(maxEdges*4);
  378. for (int i = 0, j = nhull-1; i < nhull; j=i++)
  379. addEdge(ctx, &edges[0], nedges, maxEdges, hull[j],hull[i], HULL, UNDEF);
  380. int currentEdge = 0;
  381. while (currentEdge < nedges)
  382. {
  383. if (edges[currentEdge*4+2] == UNDEF)
  384. completeFacet(ctx, pts, npts, &edges[0], nedges, maxEdges, nfaces, currentEdge);
  385. if (edges[currentEdge*4+3] == UNDEF)
  386. completeFacet(ctx, pts, npts, &edges[0], nedges, maxEdges, nfaces, currentEdge);
  387. currentEdge++;
  388. }
  389. // Create tris
  390. tris.resize(nfaces*4);
  391. for (int i = 0; i < nfaces*4; ++i)
  392. tris[i] = -1;
  393. for (int i = 0; i < nedges; ++i)
  394. {
  395. const int* e = &edges[i*4];
  396. if (e[3] >= 0)
  397. {
  398. // Left face
  399. int* t = &tris[e[3]*4];
  400. if (t[0] == -1)
  401. {
  402. t[0] = e[0];
  403. t[1] = e[1];
  404. }
  405. else if (t[0] == e[1])
  406. t[2] = e[0];
  407. else if (t[1] == e[0])
  408. t[2] = e[1];
  409. }
  410. if (e[2] >= 0)
  411. {
  412. // Right
  413. int* t = &tris[e[2]*4];
  414. if (t[0] == -1)
  415. {
  416. t[0] = e[1];
  417. t[1] = e[0];
  418. }
  419. else if (t[0] == e[0])
  420. t[2] = e[1];
  421. else if (t[1] == e[1])
  422. t[2] = e[0];
  423. }
  424. }
  425. for (int i = 0; i < tris.size()/4; ++i)
  426. {
  427. int* t = &tris[i*4];
  428. if (t[0] == -1 || t[1] == -1 || t[2] == -1)
  429. {
  430. ctx->log(RC_LOG_WARNING, "delaunayHull: Removing dangling face %d [%d,%d,%d].", i, t[0],t[1],t[2]);
  431. t[0] = tris[tris.size()-4];
  432. t[1] = tris[tris.size()-3];
  433. t[2] = tris[tris.size()-2];
  434. t[3] = tris[tris.size()-1];
  435. tris.resize(tris.size()-4);
  436. --i;
  437. }
  438. }
  439. }
  440. inline float getJitterX(const int i)
  441. {
  442. return (((i * 0x8da6b343) & 0xffff) / 65535.0f * 2.0f) - 1.0f;
  443. }
  444. inline float getJitterY(const int i)
  445. {
  446. return (((i * 0xd8163841) & 0xffff) / 65535.0f * 2.0f) - 1.0f;
  447. }
  448. static bool buildPolyDetail(rcContext* ctx, const float* in, const int nin,
  449. const float sampleDist, const float sampleMaxError,
  450. const rcCompactHeightfield& chf, const rcHeightPatch& hp,
  451. float* verts, int& nverts, rcIntArray& tris,
  452. rcIntArray& edges, rcIntArray& samples)
  453. {
  454. static const int MAX_VERTS = 127;
  455. static const int MAX_TRIS = 255; // Max tris for delaunay is 2n-2-k (n=num verts, k=num hull verts).
  456. static const int MAX_VERTS_PER_EDGE = 32;
  457. float edge[(MAX_VERTS_PER_EDGE+1)*3];
  458. int hull[MAX_VERTS];
  459. int nhull = 0;
  460. nverts = 0;
  461. for (int i = 0; i < nin; ++i)
  462. rcVcopy(&verts[i*3], &in[i*3]);
  463. nverts = nin;
  464. const float cs = chf.cs;
  465. const float ics = 1.0f/cs;
  466. // Tessellate outlines.
  467. // This is done in separate pass in order to ensure
  468. // seamless height values across the ply boundaries.
  469. if (sampleDist > 0)
  470. {
  471. for (int i = 0, j = nin-1; i < nin; j=i++)
  472. {
  473. const float* vj = &in[j*3];
  474. const float* vi = &in[i*3];
  475. bool swapped = false;
  476. // Make sure the segments are always handled in same order
  477. // using lexological sort or else there will be seams.
  478. if (fabsf(vj[0]-vi[0]) < 1e-6f)
  479. {
  480. if (vj[2] > vi[2])
  481. {
  482. rcSwap(vj,vi);
  483. swapped = true;
  484. }
  485. }
  486. else
  487. {
  488. if (vj[0] > vi[0])
  489. {
  490. rcSwap(vj,vi);
  491. swapped = true;
  492. }
  493. }
  494. // Create samples along the edge.
  495. float dx = vi[0] - vj[0];
  496. float dy = vi[1] - vj[1];
  497. float dz = vi[2] - vj[2];
  498. float d = sqrtf(dx*dx + dz*dz);
  499. int nn = 1 + (int)floorf(d/sampleDist);
  500. if (nn >= MAX_VERTS_PER_EDGE) nn = MAX_VERTS_PER_EDGE-1;
  501. if (nverts+nn >= MAX_VERTS)
  502. nn = MAX_VERTS-1-nverts;
  503. for (int k = 0; k <= nn; ++k)
  504. {
  505. float u = (float)k/(float)nn;
  506. float* pos = &edge[k*3];
  507. pos[0] = vj[0] + dx*u;
  508. pos[1] = vj[1] + dy*u;
  509. pos[2] = vj[2] + dz*u;
  510. pos[1] = getHeight(pos[0],pos[1],pos[2], cs, ics, chf.ch, hp)*chf.ch;
  511. }
  512. // Simplify samples.
  513. int idx[MAX_VERTS_PER_EDGE] = {0,nn};
  514. int nidx = 2;
  515. for (int k = 0; k < nidx-1; )
  516. {
  517. const int a = idx[k];
  518. const int b = idx[k+1];
  519. const float* va = &edge[a*3];
  520. const float* vb = &edge[b*3];
  521. // Find maximum deviation along the segment.
  522. float maxd = 0;
  523. int maxi = -1;
  524. for (int m = a+1; m < b; ++m)
  525. {
  526. float dev = distancePtSeg(&edge[m*3],va,vb);
  527. if (dev > maxd)
  528. {
  529. maxd = dev;
  530. maxi = m;
  531. }
  532. }
  533. // If the max deviation is larger than accepted error,
  534. // add new point, else continue to next segment.
  535. if (maxi != -1 && maxd > rcSqr(sampleMaxError))
  536. {
  537. for (int m = nidx; m > k; --m)
  538. idx[m] = idx[m-1];
  539. idx[k+1] = maxi;
  540. nidx++;
  541. }
  542. else
  543. {
  544. ++k;
  545. }
  546. }
  547. hull[nhull++] = j;
  548. // Add new vertices.
  549. if (swapped)
  550. {
  551. for (int k = nidx-2; k > 0; --k)
  552. {
  553. rcVcopy(&verts[nverts*3], &edge[idx[k]*3]);
  554. hull[nhull++] = nverts;
  555. nverts++;
  556. }
  557. }
  558. else
  559. {
  560. for (int k = 1; k < nidx-1; ++k)
  561. {
  562. rcVcopy(&verts[nverts*3], &edge[idx[k]*3]);
  563. hull[nhull++] = nverts;
  564. nverts++;
  565. }
  566. }
  567. }
  568. }
  569. // Tessellate the base mesh.
  570. edges.resize(0);
  571. tris.resize(0);
  572. delaunayHull(ctx, nverts, verts, nhull, hull, tris, edges);
  573. if (tris.size() == 0)
  574. {
  575. // Could not triangulate the poly, make sure there is some valid data there.
  576. ctx->log(RC_LOG_WARNING, "buildPolyDetail: Could not triangulate polygon, adding default data.");
  577. for (int i = 2; i < nverts; ++i)
  578. {
  579. tris.push(0);
  580. tris.push(i-1);
  581. tris.push(i);
  582. tris.push(0);
  583. }
  584. return true;
  585. }
  586. if (sampleDist > 0)
  587. {
  588. // Create sample locations in a grid.
  589. float bmin[3], bmax[3];
  590. rcVcopy(bmin, in);
  591. rcVcopy(bmax, in);
  592. for (int i = 1; i < nin; ++i)
  593. {
  594. rcVmin(bmin, &in[i*3]);
  595. rcVmax(bmax, &in[i*3]);
  596. }
  597. int x0 = (int)floorf(bmin[0]/sampleDist);
  598. int x1 = (int)ceilf(bmax[0]/sampleDist);
  599. int z0 = (int)floorf(bmin[2]/sampleDist);
  600. int z1 = (int)ceilf(bmax[2]/sampleDist);
  601. samples.resize(0);
  602. for (int z = z0; z < z1; ++z)
  603. {
  604. for (int x = x0; x < x1; ++x)
  605. {
  606. float pt[3];
  607. pt[0] = x*sampleDist;
  608. pt[1] = (bmax[1]+bmin[1])*0.5f;
  609. pt[2] = z*sampleDist;
  610. // Make sure the samples are not too close to the edges.
  611. if (distToPoly(nin,in,pt) > -sampleDist/2) continue;
  612. samples.push(x);
  613. samples.push(getHeight(pt[0], pt[1], pt[2], cs, ics, chf.ch, hp));
  614. samples.push(z);
  615. samples.push(0); // Not added
  616. }
  617. }
  618. // Add the samples starting from the one that has the most
  619. // error. The procedure stops when all samples are added
  620. // or when the max error is within treshold.
  621. const int nsamples = samples.size()/4;
  622. for (int iter = 0; iter < nsamples; ++iter)
  623. {
  624. if (nverts >= MAX_VERTS)
  625. break;
  626. // Find sample with most error.
  627. float bestpt[3] = {0,0,0};
  628. float bestd = 0;
  629. int besti = -1;
  630. for (int i = 0; i < nsamples; ++i)
  631. {
  632. const int* s = &samples[i*4];
  633. if (s[3]) continue; // skip added.
  634. float pt[3];
  635. // The sample location is jittered to get rid of some bad triangulations
  636. // which are cause by symmetrical data from the grid structure.
  637. pt[0] = s[0]*sampleDist + getJitterX(i)*cs*0.1f;
  638. pt[1] = s[1]*chf.ch;
  639. pt[2] = s[2]*sampleDist + getJitterY(i)*cs*0.1f;
  640. float d = distToTriMesh(pt, verts, nverts, &tris[0], tris.size()/4);
  641. if (d < 0) continue; // did not hit the mesh.
  642. if (d > bestd)
  643. {
  644. bestd = d;
  645. besti = i;
  646. rcVcopy(bestpt,pt);
  647. }
  648. }
  649. // If the max error is within accepted threshold, stop tesselating.
  650. if (bestd <= sampleMaxError || besti == -1)
  651. break;
  652. // Mark sample as added.
  653. samples[besti*4+3] = 1;
  654. // Add the new sample point.
  655. rcVcopy(&verts[nverts*3],bestpt);
  656. nverts++;
  657. // Create new triangulation.
  658. // TODO: Incremental add instead of full rebuild.
  659. edges.resize(0);
  660. tris.resize(0);
  661. delaunayHull(ctx, nverts, verts, nhull, hull, tris, edges);
  662. }
  663. }
  664. const int ntris = tris.size()/4;
  665. if (ntris > MAX_TRIS)
  666. {
  667. tris.resize(MAX_TRIS*4);
  668. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Shrinking triangle count from %d to max %d.", ntris, MAX_TRIS);
  669. }
  670. return true;
  671. }
  672. static void getHeightData(const rcCompactHeightfield& chf,
  673. const unsigned short* poly, const int npoly,
  674. const unsigned short* verts, const int bs,
  675. rcHeightPatch& hp, rcIntArray& stack)
  676. {
  677. // Floodfill the heightfield to get 2D height data,
  678. // starting at vertex locations as seeds.
  679. // Note: Reads to the compact heightfield are offset by border size (bs)
  680. // since border size offset is already removed from the polymesh vertices.
  681. memset(hp.data, 0, sizeof(unsigned short)*hp.width*hp.height);
  682. stack.resize(0);
  683. static const int offset[9*2] =
  684. {
  685. 0,0, -1,-1, 0,-1, 1,-1, 1,0, 1,1, 0,1, -1,1, -1,0,
  686. };
  687. // Use poly vertices as seed points for the flood fill.
  688. for (int j = 0; j < npoly; ++j)
  689. {
  690. int cx = 0, cz = 0, ci =-1;
  691. int dmin = RC_UNSET_HEIGHT;
  692. for (int k = 0; k < 9; ++k)
  693. {
  694. const int ax = (int)verts[poly[j]*3+0] + offset[k*2+0];
  695. const int ay = (int)verts[poly[j]*3+1];
  696. const int az = (int)verts[poly[j]*3+2] + offset[k*2+1];
  697. if (ax < hp.xmin || ax >= hp.xmin+hp.width ||
  698. az < hp.ymin || az >= hp.ymin+hp.height)
  699. continue;
  700. const rcCompactCell& c = chf.cells[(ax+bs)+(az+bs)*chf.width];
  701. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  702. {
  703. const rcCompactSpan& s = chf.spans[i];
  704. int d = rcAbs(ay - (int)s.y);
  705. if (d < dmin)
  706. {
  707. cx = ax;
  708. cz = az;
  709. ci = i;
  710. dmin = d;
  711. }
  712. }
  713. }
  714. if (ci != -1)
  715. {
  716. stack.push(cx);
  717. stack.push(cz);
  718. stack.push(ci);
  719. }
  720. }
  721. // Find center of the polygon using flood fill.
  722. int pcx = 0, pcz = 0;
  723. for (int j = 0; j < npoly; ++j)
  724. {
  725. pcx += (int)verts[poly[j]*3+0];
  726. pcz += (int)verts[poly[j]*3+2];
  727. }
  728. pcx /= npoly;
  729. pcz /= npoly;
  730. for (int i = 0; i < stack.size(); i += 3)
  731. {
  732. int cx = stack[i+0];
  733. int cy = stack[i+1];
  734. int idx = cx-hp.xmin+(cy-hp.ymin)*hp.width;
  735. hp.data[idx] = 1;
  736. }
  737. while (stack.size() > 0)
  738. {
  739. int ci = stack.pop();
  740. int cy = stack.pop();
  741. int cx = stack.pop();
  742. // Check if close to center of the polygon.
  743. if (rcAbs(cx-pcx) <= 1 && rcAbs(cy-pcz) <= 1)
  744. {
  745. stack.resize(0);
  746. stack.push(cx);
  747. stack.push(cy);
  748. stack.push(ci);
  749. break;
  750. }
  751. const rcCompactSpan& cs = chf.spans[ci];
  752. for (int dir = 0; dir < 4; ++dir)
  753. {
  754. if (rcGetCon(cs, dir) == RC_NOT_CONNECTED) continue;
  755. const int ax = cx + rcGetDirOffsetX(dir);
  756. const int ay = cy + rcGetDirOffsetY(dir);
  757. if (ax < hp.xmin || ax >= (hp.xmin+hp.width) ||
  758. ay < hp.ymin || ay >= (hp.ymin+hp.height))
  759. continue;
  760. if (hp.data[ax-hp.xmin+(ay-hp.ymin)*hp.width] != 0)
  761. continue;
  762. const int ai = (int)chf.cells[(ax+bs)+(ay+bs)*chf.width].index + rcGetCon(cs, dir);
  763. int idx = ax-hp.xmin+(ay-hp.ymin)*hp.width;
  764. hp.data[idx] = 1;
  765. stack.push(ax);
  766. stack.push(ay);
  767. stack.push(ai);
  768. }
  769. }
  770. memset(hp.data, 0xff, sizeof(unsigned short)*hp.width*hp.height);
  771. // Mark start locations.
  772. for (int i = 0; i < stack.size(); i += 3)
  773. {
  774. int cx = stack[i+0];
  775. int cy = stack[i+1];
  776. int ci = stack[i+2];
  777. int idx = cx-hp.xmin+(cy-hp.ymin)*hp.width;
  778. const rcCompactSpan& cs = chf.spans[ci];
  779. hp.data[idx] = cs.y;
  780. }
  781. static const int RETRACT_SIZE = 256;
  782. int head = 0;
  783. while (head*3 < stack.size())
  784. {
  785. int cx = stack[head*3+0];
  786. int cy = stack[head*3+1];
  787. int ci = stack[head*3+2];
  788. head++;
  789. if (head >= RETRACT_SIZE)
  790. {
  791. head = 0;
  792. if (stack.size() > RETRACT_SIZE*3)
  793. memmove(&stack[0], &stack[RETRACT_SIZE*3], sizeof(int)*(stack.size()-RETRACT_SIZE*3));
  794. stack.resize(stack.size()-RETRACT_SIZE*3);
  795. }
  796. const rcCompactSpan& cs = chf.spans[ci];
  797. for (int dir = 0; dir < 4; ++dir)
  798. {
  799. if (rcGetCon(cs, dir) == RC_NOT_CONNECTED) continue;
  800. const int ax = cx + rcGetDirOffsetX(dir);
  801. const int ay = cy + rcGetDirOffsetY(dir);
  802. if (ax < hp.xmin || ax >= (hp.xmin+hp.width) ||
  803. ay < hp.ymin || ay >= (hp.ymin+hp.height))
  804. continue;
  805. if (hp.data[ax-hp.xmin+(ay-hp.ymin)*hp.width] != RC_UNSET_HEIGHT)
  806. continue;
  807. const int ai = (int)chf.cells[(ax+bs)+(ay+bs)*chf.width].index + rcGetCon(cs, dir);
  808. const rcCompactSpan& as = chf.spans[ai];
  809. int idx = ax-hp.xmin+(ay-hp.ymin)*hp.width;
  810. hp.data[idx] = as.y;
  811. stack.push(ax);
  812. stack.push(ay);
  813. stack.push(ai);
  814. }
  815. }
  816. }
  817. static unsigned char getEdgeFlags(const float* va, const float* vb,
  818. const float* vpoly, const int npoly)
  819. {
  820. // Return true if edge (va,vb) is part of the polygon.
  821. static const float thrSqr = rcSqr(0.001f);
  822. for (int i = 0, j = npoly-1; i < npoly; j=i++)
  823. {
  824. if (distancePtSeg2d(va, &vpoly[j*3], &vpoly[i*3]) < thrSqr &&
  825. distancePtSeg2d(vb, &vpoly[j*3], &vpoly[i*3]) < thrSqr)
  826. return 1;
  827. }
  828. return 0;
  829. }
  830. static unsigned char getTriFlags(const float* va, const float* vb, const float* vc,
  831. const float* vpoly, const int npoly)
  832. {
  833. unsigned char flags = 0;
  834. flags |= getEdgeFlags(va,vb,vpoly,npoly) << 0;
  835. flags |= getEdgeFlags(vb,vc,vpoly,npoly) << 2;
  836. flags |= getEdgeFlags(vc,va,vpoly,npoly) << 4;
  837. return flags;
  838. }
  839. /// @par
  840. ///
  841. /// See the #rcConfig documentation for more information on the configuration parameters.
  842. ///
  843. /// @see rcAllocPolyMeshDetail, rcPolyMesh, rcCompactHeightfield, rcPolyMeshDetail, rcConfig
  844. bool rcBuildPolyMeshDetail(rcContext* ctx, const rcPolyMesh& mesh, const rcCompactHeightfield& chf,
  845. const float sampleDist, const float sampleMaxError,
  846. rcPolyMeshDetail& dmesh)
  847. {
  848. rcAssert(ctx);
  849. ctx->startTimer(RC_TIMER_BUILD_POLYMESHDETAIL);
  850. if (mesh.nverts == 0 || mesh.npolys == 0)
  851. return true;
  852. const int nvp = mesh.nvp;
  853. const float cs = mesh.cs;
  854. const float ch = mesh.ch;
  855. const float* orig = mesh.bmin;
  856. const int borderSize = mesh.borderSize;
  857. rcIntArray edges(64);
  858. rcIntArray tris(512);
  859. rcIntArray stack(512);
  860. rcIntArray samples(512);
  861. float verts[256*3];
  862. rcHeightPatch hp;
  863. int nPolyVerts = 0;
  864. int maxhw = 0, maxhh = 0;
  865. rcScopedDelete<int> bounds = (int*)rcAlloc(sizeof(int)*mesh.npolys*4, RC_ALLOC_TEMP);
  866. if (!bounds)
  867. {
  868. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'bounds' (%d).", mesh.npolys*4);
  869. return false;
  870. }
  871. rcScopedDelete<float> poly = (float*)rcAlloc(sizeof(float)*nvp*3, RC_ALLOC_TEMP);
  872. if (!poly)
  873. {
  874. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'poly' (%d).", nvp*3);
  875. return false;
  876. }
  877. // Find max size for a polygon area.
  878. for (int i = 0; i < mesh.npolys; ++i)
  879. {
  880. const unsigned short* p = &mesh.polys[i*nvp*2];
  881. int& xmin = bounds[i*4+0];
  882. int& xmax = bounds[i*4+1];
  883. int& ymin = bounds[i*4+2];
  884. int& ymax = bounds[i*4+3];
  885. xmin = chf.width;
  886. xmax = 0;
  887. ymin = chf.height;
  888. ymax = 0;
  889. for (int j = 0; j < nvp; ++j)
  890. {
  891. if(p[j] == RC_MESH_NULL_IDX) break;
  892. const unsigned short* v = &mesh.verts[p[j]*3];
  893. xmin = rcMin(xmin, (int)v[0]);
  894. xmax = rcMax(xmax, (int)v[0]);
  895. ymin = rcMin(ymin, (int)v[2]);
  896. ymax = rcMax(ymax, (int)v[2]);
  897. nPolyVerts++;
  898. }
  899. xmin = rcMax(0,xmin-1);
  900. xmax = rcMin(chf.width,xmax+1);
  901. ymin = rcMax(0,ymin-1);
  902. ymax = rcMin(chf.height,ymax+1);
  903. if (xmin >= xmax || ymin >= ymax) continue;
  904. maxhw = rcMax(maxhw, xmax-xmin);
  905. maxhh = rcMax(maxhh, ymax-ymin);
  906. }
  907. hp.data = (unsigned short*)rcAlloc(sizeof(unsigned short)*maxhw*maxhh, RC_ALLOC_TEMP);
  908. if (!hp.data)
  909. {
  910. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'hp.data' (%d).", maxhw*maxhh);
  911. return false;
  912. }
  913. dmesh.nmeshes = mesh.npolys;
  914. dmesh.nverts = 0;
  915. dmesh.ntris = 0;
  916. dmesh.meshes = (unsigned int*)rcAlloc(sizeof(unsigned int)*dmesh.nmeshes*4, RC_ALLOC_PERM);
  917. if (!dmesh.meshes)
  918. {
  919. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.meshes' (%d).", dmesh.nmeshes*4);
  920. return false;
  921. }
  922. int vcap = nPolyVerts+nPolyVerts/2;
  923. int tcap = vcap*2;
  924. dmesh.nverts = 0;
  925. dmesh.verts = (float*)rcAlloc(sizeof(float)*vcap*3, RC_ALLOC_PERM);
  926. if (!dmesh.verts)
  927. {
  928. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.verts' (%d).", vcap*3);
  929. return false;
  930. }
  931. dmesh.ntris = 0;
  932. dmesh.tris = (unsigned char*)rcAlloc(sizeof(unsigned char*)*tcap*4, RC_ALLOC_PERM);
  933. if (!dmesh.tris)
  934. {
  935. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.tris' (%d).", tcap*4);
  936. return false;
  937. }
  938. for (int i = 0; i < mesh.npolys; ++i)
  939. {
  940. const unsigned short* p = &mesh.polys[i*nvp*2];
  941. // Store polygon vertices for processing.
  942. int npoly = 0;
  943. for (int j = 0; j < nvp; ++j)
  944. {
  945. if(p[j] == RC_MESH_NULL_IDX) break;
  946. const unsigned short* v = &mesh.verts[p[j]*3];
  947. poly[j*3+0] = v[0]*cs;
  948. poly[j*3+1] = v[1]*ch;
  949. poly[j*3+2] = v[2]*cs;
  950. npoly++;
  951. }
  952. // Get the height data from the area of the polygon.
  953. hp.xmin = bounds[i*4+0];
  954. hp.ymin = bounds[i*4+2];
  955. hp.width = bounds[i*4+1]-bounds[i*4+0];
  956. hp.height = bounds[i*4+3]-bounds[i*4+2];
  957. getHeightData(chf, p, npoly, mesh.verts, borderSize, hp, stack);
  958. // Build detail mesh.
  959. int nverts = 0;
  960. if (!buildPolyDetail(ctx, poly, npoly,
  961. sampleDist, sampleMaxError,
  962. chf, hp, verts, nverts, tris,
  963. edges, samples))
  964. {
  965. return false;
  966. }
  967. // Move detail verts to world space.
  968. for (int j = 0; j < nverts; ++j)
  969. {
  970. verts[j*3+0] += orig[0];
  971. verts[j*3+1] += orig[1] + chf.ch; // Is this offset necessary?
  972. verts[j*3+2] += orig[2];
  973. }
  974. // Offset poly too, will be used to flag checking.
  975. for (int j = 0; j < npoly; ++j)
  976. {
  977. poly[j*3+0] += orig[0];
  978. poly[j*3+1] += orig[1];
  979. poly[j*3+2] += orig[2];
  980. }
  981. // Store detail submesh.
  982. const int ntris = tris.size()/4;
  983. dmesh.meshes[i*4+0] = (unsigned int)dmesh.nverts;
  984. dmesh.meshes[i*4+1] = (unsigned int)nverts;
  985. dmesh.meshes[i*4+2] = (unsigned int)dmesh.ntris;
  986. dmesh.meshes[i*4+3] = (unsigned int)ntris;
  987. // Store vertices, allocate more memory if necessary.
  988. if (dmesh.nverts+nverts > vcap)
  989. {
  990. while (dmesh.nverts+nverts > vcap)
  991. vcap += 256;
  992. float* newv = (float*)rcAlloc(sizeof(float)*vcap*3, RC_ALLOC_PERM);
  993. if (!newv)
  994. {
  995. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'newv' (%d).", vcap*3);
  996. return false;
  997. }
  998. if (dmesh.nverts)
  999. memcpy(newv, dmesh.verts, sizeof(float)*3*dmesh.nverts);
  1000. rcFree(dmesh.verts);
  1001. dmesh.verts = newv;
  1002. }
  1003. for (int j = 0; j < nverts; ++j)
  1004. {
  1005. dmesh.verts[dmesh.nverts*3+0] = verts[j*3+0];
  1006. dmesh.verts[dmesh.nverts*3+1] = verts[j*3+1];
  1007. dmesh.verts[dmesh.nverts*3+2] = verts[j*3+2];
  1008. dmesh.nverts++;
  1009. }
  1010. // Store triangles, allocate more memory if necessary.
  1011. if (dmesh.ntris+ntris > tcap)
  1012. {
  1013. while (dmesh.ntris+ntris > tcap)
  1014. tcap += 256;
  1015. unsigned char* newt = (unsigned char*)rcAlloc(sizeof(unsigned char)*tcap*4, RC_ALLOC_PERM);
  1016. if (!newt)
  1017. {
  1018. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'newt' (%d).", tcap*4);
  1019. return false;
  1020. }
  1021. if (dmesh.ntris)
  1022. memcpy(newt, dmesh.tris, sizeof(unsigned char)*4*dmesh.ntris);
  1023. rcFree(dmesh.tris);
  1024. dmesh.tris = newt;
  1025. }
  1026. for (int j = 0; j < ntris; ++j)
  1027. {
  1028. const int* t = &tris[j*4];
  1029. dmesh.tris[dmesh.ntris*4+0] = (unsigned char)t[0];
  1030. dmesh.tris[dmesh.ntris*4+1] = (unsigned char)t[1];
  1031. dmesh.tris[dmesh.ntris*4+2] = (unsigned char)t[2];
  1032. dmesh.tris[dmesh.ntris*4+3] = getTriFlags(&verts[t[0]*3], &verts[t[1]*3], &verts[t[2]*3], poly, npoly);
  1033. dmesh.ntris++;
  1034. }
  1035. }
  1036. ctx->stopTimer(RC_TIMER_BUILD_POLYMESHDETAIL);
  1037. return true;
  1038. }
  1039. /// @see rcAllocPolyMeshDetail, rcPolyMeshDetail
  1040. bool rcMergePolyMeshDetails(rcContext* ctx, rcPolyMeshDetail** meshes, const int nmeshes, rcPolyMeshDetail& mesh)
  1041. {
  1042. rcAssert(ctx);
  1043. ctx->startTimer(RC_TIMER_MERGE_POLYMESHDETAIL);
  1044. int maxVerts = 0;
  1045. int maxTris = 0;
  1046. int maxMeshes = 0;
  1047. for (int i = 0; i < nmeshes; ++i)
  1048. {
  1049. if (!meshes[i]) continue;
  1050. maxVerts += meshes[i]->nverts;
  1051. maxTris += meshes[i]->ntris;
  1052. maxMeshes += meshes[i]->nmeshes;
  1053. }
  1054. mesh.nmeshes = 0;
  1055. mesh.meshes = (unsigned int*)rcAlloc(sizeof(unsigned int)*maxMeshes*4, RC_ALLOC_PERM);
  1056. if (!mesh.meshes)
  1057. {
  1058. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'pmdtl.meshes' (%d).", maxMeshes*4);
  1059. return false;
  1060. }
  1061. mesh.ntris = 0;
  1062. mesh.tris = (unsigned char*)rcAlloc(sizeof(unsigned char)*maxTris*4, RC_ALLOC_PERM);
  1063. if (!mesh.tris)
  1064. {
  1065. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.tris' (%d).", maxTris*4);
  1066. return false;
  1067. }
  1068. mesh.nverts = 0;
  1069. mesh.verts = (float*)rcAlloc(sizeof(float)*maxVerts*3, RC_ALLOC_PERM);
  1070. if (!mesh.verts)
  1071. {
  1072. ctx->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.verts' (%d).", maxVerts*3);
  1073. return false;
  1074. }
  1075. // Merge datas.
  1076. for (int i = 0; i < nmeshes; ++i)
  1077. {
  1078. rcPolyMeshDetail* dm = meshes[i];
  1079. if (!dm) continue;
  1080. for (int j = 0; j < dm->nmeshes; ++j)
  1081. {
  1082. unsigned int* dst = &mesh.meshes[mesh.nmeshes*4];
  1083. unsigned int* src = &dm->meshes[j*4];
  1084. dst[0] = (unsigned int)mesh.nverts+src[0];
  1085. dst[1] = src[1];
  1086. dst[2] = (unsigned int)mesh.ntris+src[2];
  1087. dst[3] = src[3];
  1088. mesh.nmeshes++;
  1089. }
  1090. for (int k = 0; k < dm->nverts; ++k)
  1091. {
  1092. rcVcopy(&mesh.verts[mesh.nverts*3], &dm->verts[k*3]);
  1093. mesh.nverts++;
  1094. }
  1095. for (int k = 0; k < dm->ntris; ++k)
  1096. {
  1097. mesh.tris[mesh.ntris*4+0] = dm->tris[k*4+0];
  1098. mesh.tris[mesh.ntris*4+1] = dm->tris[k*4+1];
  1099. mesh.tris[mesh.ntris*4+2] = dm->tris[k*4+2];
  1100. mesh.tris[mesh.ntris*4+3] = dm->tris[k*4+3];
  1101. mesh.ntris++;
  1102. }
  1103. }
  1104. ctx->stopTimer(RC_TIMER_MERGE_POLYMESHDETAIL);
  1105. return true;
  1106. }