| 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 |
|
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <float.h> |
| 23 |
#include "DetourNavMesh.h" |
| 24 |
#include "DetourCommon.h" |
| 25 |
#include "DetourMath.h" |
| 26 |
#include "DetourNavMeshBuilder.h" |
| 27 |
#include "DetourAlloc.h" |
| 28 |
#include "DetourAssert.h" |
| 29 |
|
| 30 |
static unsigned short MESH_NULL_IDX = 0xffff; |
| 31 |
|
| 32 |
|
| 33 |
struct BVItem |
| 34 |
{ |
| 35 |
unsigned short bmin[3]; |
| 36 |
unsigned short bmax[3]; |
| 37 |
int i; |
| 38 |
}; |
| 39 |
|
| 40 |
static int compareItemX(const void* va, const void* vb) |
| 41 |
{ |
| 42 |
const BVItem* a = (const BVItem*)va; |
| 43 |
const BVItem* b = (const BVItem*)vb; |
| 44 |
if (a->bmin[0] < b->bmin[0]) |
| 45 |
return -1; |
| 46 |
if (a->bmin[0] > b->bmin[0]) |
| 47 |
return 1; |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
static int compareItemY(const void* va, const void* vb) |
| 52 |
{ |
| 53 |
const BVItem* a = (const BVItem*)va; |
| 54 |
const BVItem* b = (const BVItem*)vb; |
| 55 |
if (a->bmin[1] < b->bmin[1]) |
| 56 |
return -1; |
| 57 |
if (a->bmin[1] > b->bmin[1]) |
| 58 |
return 1; |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
static int compareItemZ(const void* va, const void* vb) |
| 63 |
{ |
| 64 |
const BVItem* a = (const BVItem*)va; |
| 65 |
const BVItem* b = (const BVItem*)vb; |
| 66 |
if (a->bmin[2] < b->bmin[2]) |
| 67 |
return -1; |
| 68 |
if (a->bmin[2] > b->bmin[2]) |
| 69 |
return 1; |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
static void calcExtends(BVItem* items, const int /*nitems*/, const int imin, const int imax, |
| 74 |
unsigned short* bmin, unsigned short* bmax) |
| 75 |
{ |
| 76 |
bmin[0] = items[imin].bmin[0]; |
| 77 |
bmin[1] = items[imin].bmin[1]; |
| 78 |
bmin[2] = items[imin].bmin[2]; |
| 79 |
|
| 80 |
bmax[0] = items[imin].bmax[0]; |
| 81 |
bmax[1] = items[imin].bmax[1]; |
| 82 |
bmax[2] = items[imin].bmax[2]; |
| 83 |
|
| 84 |
for (int i = imin+1; i < imax; ++i) |
| 85 |
{ |
| 86 |
const BVItem& it = items[i]; |
| 87 |
if (it.bmin[0] < bmin[0]) bmin[0] = it.bmin[0]; |
| 88 |
if (it.bmin[1] < bmin[1]) bmin[1] = it.bmin[1]; |
| 89 |
if (it.bmin[2] < bmin[2]) bmin[2] = it.bmin[2]; |
| 90 |
|
| 91 |
if (it.bmax[0] > bmax[0]) bmax[0] = it.bmax[0]; |
| 92 |
if (it.bmax[1] > bmax[1]) bmax[1] = it.bmax[1]; |
| 93 |
if (it.bmax[2] > bmax[2]) bmax[2] = it.bmax[2]; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
inline int longestAxis(unsigned short x, unsigned short y, unsigned short z) |
| 98 |
{ |
| 99 |
int axis = 0; |
| 100 |
unsigned short maxVal = x; |
| 101 |
if (y > maxVal) |
| 102 |
{ |
| 103 |
axis = 1; |
| 104 |
maxVal = y; |
| 105 |
} |
| 106 |
if (z > maxVal) |
| 107 |
{ |
| 108 |
axis = 2; |
| 109 |
} |
| 110 |
return axis; |
| 111 |
} |
| 112 |
|
| 113 |
static void subdivide(BVItem* items, int nitems, int imin, int imax, int& curNode, dtBVNode* nodes) |
| 114 |
{ |
| 115 |
int inum = imax - imin; |
| 116 |
int icur = curNode; |
| 117 |
|
| 118 |
dtBVNode& node = nodes[curNode++]; |
| 119 |
|
| 120 |
if (inum == 1) |
| 121 |
{ |
| 122 |
// Leaf |
| 123 |
node.bmin[0] = items[imin].bmin[0]; |
| 124 |
node.bmin[1] = items[imin].bmin[1]; |
| 125 |
node.bmin[2] = items[imin].bmin[2]; |
| 126 |
|
| 127 |
node.bmax[0] = items[imin].bmax[0]; |
| 128 |
node.bmax[1] = items[imin].bmax[1]; |
| 129 |
node.bmax[2] = items[imin].bmax[2]; |
| 130 |
|
| 131 |
node.i = items[imin].i; |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
// Split |
| 136 |
calcExtends(items, nitems, imin, imax, node.bmin, node.bmax); |
| 137 |
|
| 138 |
int axis = longestAxis(node.bmax[0] - node.bmin[0], |
| 139 |
node.bmax[1] - node.bmin[1], |
| 140 |
node.bmax[2] - node.bmin[2]); |
| 141 |
|
| 142 |
if (axis == 0) |
| 143 |
{ |
| 144 |
// Sort along x-axis |
| 145 |
qsort(items+imin, inum, sizeof(BVItem), compareItemX); |
| 146 |
} |
| 147 |
else if (axis == 1) |
| 148 |
{ |
| 149 |
// Sort along y-axis |
| 150 |
qsort(items+imin, inum, sizeof(BVItem), compareItemY); |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
// Sort along z-axis |
| 155 |
qsort(items+imin, inum, sizeof(BVItem), compareItemZ); |
| 156 |
} |
| 157 |
|
| 158 |
int isplit = imin+inum/2; |
| 159 |
|
| 160 |
// Left |
| 161 |
subdivide(items, nitems, imin, isplit, curNode, nodes); |
| 162 |
// Right |
| 163 |
subdivide(items, nitems, isplit, imax, curNode, nodes); |
| 164 |
|
| 165 |
int iescape = curNode - icur; |
| 166 |
// Negative index means escape. |
| 167 |
node.i = -iescape; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
static int createBVTree(dtNavMeshCreateParams* params, dtBVNode* nodes, int /*nnodes*/) |
| 172 |
{ |
| 173 |
// Build tree |
| 174 |
float quantFactor = 1 / params->cs; |
| 175 |
BVItem* items = (BVItem*)dtAlloc(sizeof(BVItem)*params->polyCount, DT_ALLOC_TEMP); |
| 176 |
for (int i = 0; i < params->polyCount; i++) |
| 177 |
{ |
| 178 |
BVItem& it = items[i]; |
| 179 |
it.i = i; |
| 180 |
// Calc polygon bounds. Use detail meshes if available. |
| 181 |
if (params->detailMeshes) |
| 182 |
{ |
| 183 |
int vb = (int)params->detailMeshes[i*4+0]; |
| 184 |
int ndv = (int)params->detailMeshes[i*4+1]; |
| 185 |
float bmin[3]; |
| 186 |
float bmax[3]; |
| 187 |
|
| 188 |
const float* dv = ¶ms->detailVerts[vb*3]; |
| 189 |
dtVcopy(bmin, dv); |
| 190 |
dtVcopy(bmax, dv); |
| 191 |
|
| 192 |
for (int j = 1; j < ndv; j++) |
| 193 |
{ |
| 194 |
dtVmin(bmin, &dv[j * 3]); |
| 195 |
dtVmax(bmax, &dv[j * 3]); |
| 196 |
} |
| 197 |
|
| 198 |
// BV-tree uses cs for all dimensions |
| 199 |
it.bmin[0] = (unsigned short)dtClamp((int)((bmin[0] - params->bmin[0])*quantFactor), 0, 0xffff); |
| 200 |
it.bmin[1] = (unsigned short)dtClamp((int)((bmin[1] - params->bmin[1])*quantFactor), 0, 0xffff); |
| 201 |
it.bmin[2] = (unsigned short)dtClamp((int)((bmin[2] - params->bmin[2])*quantFactor), 0, 0xffff); |
| 202 |
|
| 203 |
it.bmax[0] = (unsigned short)dtClamp((int)((bmax[0] - params->bmin[0])*quantFactor), 0, 0xffff); |
| 204 |
it.bmax[1] = (unsigned short)dtClamp((int)((bmax[1] - params->bmin[1])*quantFactor), 0, 0xffff); |
| 205 |
it.bmax[2] = (unsigned short)dtClamp((int)((bmax[2] - params->bmin[2])*quantFactor), 0, 0xffff); |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
const unsigned short* p = ¶ms->polys[i*params->nvp * 2]; |
| 210 |
it.bmin[0] = it.bmax[0] = params->verts[p[0] * 3 + 0]; |
| 211 |
it.bmin[1] = it.bmax[1] = params->verts[p[0] * 3 + 1]; |
| 212 |
it.bmin[2] = it.bmax[2] = params->verts[p[0] * 3 + 2]; |
| 213 |
|
| 214 |
for (int j = 1; j < params->nvp; ++j) |
| 215 |
{ |
| 216 |
if (p[j] == MESH_NULL_IDX) break; |
| 217 |
unsigned short x = params->verts[p[j] * 3 + 0]; |
| 218 |
unsigned short y = params->verts[p[j] * 3 + 1]; |
| 219 |
unsigned short z = params->verts[p[j] * 3 + 2]; |
| 220 |
|
| 221 |
if (x < it.bmin[0]) it.bmin[0] = x; |
| 222 |
if (y < it.bmin[1]) it.bmin[1] = y; |
| 223 |
if (z < it.bmin[2]) it.bmin[2] = z; |
| 224 |
|
| 225 |
if (x > it.bmax[0]) it.bmax[0] = x; |
| 226 |
if (y > it.bmax[1]) it.bmax[1] = y; |
| 227 |
if (z > it.bmax[2]) it.bmax[2] = z; |
| 228 |
} |
| 229 |
// Remap y |
| 230 |
it.bmin[1] = (unsigned short)dtMathFloorf((float)it.bmin[1] * params->ch / params->cs); |
| 231 |
it.bmax[1] = (unsigned short)dtMathCeilf((float)it.bmax[1] * params->ch / params->cs); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
int curNode = 0; |
| 236 |
subdivide(items, params->polyCount, 0, params->polyCount, curNode, nodes); |
| 237 |
|
| 238 |
dtFree(items); |
| 239 |
|
| 240 |
return curNode; |
| 241 |
} |
| 242 |
|
| 243 |
static unsigned char classifyOffMeshPoint(const float* pt, const float* bmin, const float* bmax) |
| 244 |
{ |
| 245 |
static const unsigned char XP = 1<<0; |
| 246 |
static const unsigned char ZP = 1<<1; |
| 247 |
static const unsigned char XM = 1<<2; |
| 248 |
static const unsigned char ZM = 1<<3; |
| 249 |
|
| 250 |
unsigned char outcode = 0; |
| 251 |
outcode |= (pt[0] >= bmax[0]) ? XP : 0; |
| 252 |
outcode |= (pt[2] >= bmax[2]) ? ZP : 0; |
| 253 |
outcode |= (pt[0] < bmin[0]) ? XM : 0; |
| 254 |
outcode |= (pt[2] < bmin[2]) ? ZM : 0; |
| 255 |
|
| 256 |
switch (outcode) |
| 257 |
{ |
| 258 |
case XP: return 0; |
| 259 |
case XP|ZP: return 1; |
| 260 |
case ZP: return 2; |
| 261 |
case XM|ZP: return 3; |
| 262 |
case XM: return 4; |
| 263 |
case XM|ZM: return 5; |
| 264 |
case ZM: return 6; |
| 265 |
case XP|ZM: return 7; |
| 266 |
}; |
| 267 |
|
| 268 |
return 0xff; |
| 269 |
} |
| 270 |
|
| 271 |
// TODO: Better error handling. |
| 272 |
|
| 273 |
/// @par |
| 274 |
/// |
| 275 |
/// The output data array is allocated using the detour allocator (dtAlloc()). The method |
| 276 |
/// used to free the memory will be determined by how the tile is added to the navigation |
| 277 |
/// mesh. |
| 278 |
/// |
| 279 |
/// @see dtNavMesh, dtNavMesh::addTile() |
| 280 |
bool dtCreateNavMeshData(dtNavMeshCreateParams* params, unsigned char** outData, int* outDataSize) |
| 281 |
{ |
| 282 |
if (params->nvp > DT_VERTS_PER_POLYGON) |
| 283 |
return false; |
| 284 |
if (params->vertCount >= 0xffff) |
| 285 |
return false; |
| 286 |
if (!params->vertCount || !params->verts) |
| 287 |
return false; |
| 288 |
if (!params->polyCount || !params->polys) |
| 289 |
return false; |
| 290 |
|
| 291 |
const int nvp = params->nvp; |
| 292 |
|
| 293 |
// Classify off-mesh connection points. We store only the connections |
| 294 |
// whose start point is inside the tile. |
| 295 |
unsigned char* offMeshConClass = 0; |
| 296 |
int storedOffMeshConCount = 0; |
| 297 |
int offMeshConLinkCount = 0; |
| 298 |
|
| 299 |
if (params->offMeshConCount > 0) |
| 300 |
{ |
| 301 |
offMeshConClass = (unsigned char*)dtAlloc(sizeof(unsigned char)*params->offMeshConCount*2, DT_ALLOC_TEMP); |
| 302 |
if (!offMeshConClass) |
| 303 |
return false; |
| 304 |
|
| 305 |
// Find tight heigh bounds, used for culling out off-mesh start locations. |
| 306 |
float hmin = FLT_MAX; |
| 307 |
float hmax = -FLT_MAX; |
| 308 |
|
| 309 |
if (params->detailVerts && params->detailVertsCount) |
| 310 |
{ |
| 311 |
for (int i = 0; i < params->detailVertsCount; ++i) |
| 312 |
{ |
| 313 |
const float h = params->detailVerts[i*3+1]; |
| 314 |
hmin = dtMin(hmin,h); |
| 315 |
hmax = dtMax(hmax,h); |
| 316 |
} |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
for (int i = 0; i < params->vertCount; ++i) |
| 321 |
{ |
| 322 |
const unsigned short* iv = ¶ms->verts[i*3]; |
| 323 |
const float h = params->bmin[1] + iv[1] * params->ch; |
| 324 |
hmin = dtMin(hmin,h); |
| 325 |
hmax = dtMax(hmax,h); |
| 326 |
} |
| 327 |
} |
| 328 |
hmin -= params->walkableClimb; |
| 329 |
hmax += params->walkableClimb; |
| 330 |
float bmin[3], bmax[3]; |
| 331 |
dtVcopy(bmin, params->bmin); |
| 332 |
dtVcopy(bmax, params->bmax); |
| 333 |
bmin[1] = hmin; |
| 334 |
bmax[1] = hmax; |
| 335 |
|
| 336 |
for (int i = 0; i < params->offMeshConCount; ++i) |
| 337 |
{ |
| 338 |
const float* p0 = ¶ms->offMeshConVerts[(i*2+0)*3]; |
| 339 |
const float* p1 = ¶ms->offMeshConVerts[(i*2+1)*3]; |
| 340 |
offMeshConClass[i*2+0] = classifyOffMeshPoint(p0, bmin, bmax); |
| 341 |
offMeshConClass[i*2+1] = classifyOffMeshPoint(p1, bmin, bmax); |
| 342 |
|
| 343 |
// Zero out off-mesh start positions which are not even potentially touching the mesh. |
| 344 |
if (offMeshConClass[i*2+0] == 0xff) |
| 345 |
{ |
| 346 |
if (p0[1] < bmin[1] || p0[1] > bmax[1]) |
| 347 |
offMeshConClass[i*2+0] = 0; |
| 348 |
} |
| 349 |
|
| 350 |
// Cound how many links should be allocated for off-mesh connections. |
| 351 |
if (offMeshConClass[i*2+0] == 0xff) |
| 352 |
offMeshConLinkCount++; |
| 353 |
if (offMeshConClass[i*2+1] == 0xff) |
| 354 |
offMeshConLinkCount++; |
| 355 |
|
| 356 |
if (offMeshConClass[i*2+0] == 0xff) |
| 357 |
storedOffMeshConCount++; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
// Off-mesh connections are stored as polygons, adjust values. |
| 362 |
const int totPolyCount = params->polyCount + storedOffMeshConCount; |
| 363 |
const int totVertCount = params->vertCount + storedOffMeshConCount*2; |
| 364 |
|
| 365 |
// Find portal edges which are at tile borders. |
| 366 |
int edgeCount = 0; |
| 367 |
int portalCount = 0; |
| 368 |
for (int i = 0; i < params->polyCount; ++i) |
| 369 |
{ |
| 370 |
const unsigned short* p = ¶ms->polys[i*2*nvp]; |
| 371 |
for (int j = 0; j < nvp; ++j) |
| 372 |
{ |
| 373 |
if (p[j] == MESH_NULL_IDX) break; |
| 374 |
edgeCount++; |
| 375 |
|
| 376 |
if (p[nvp+j] & 0x8000) |
| 377 |
{ |
| 378 |
unsigned short dir = p[nvp+j] & 0xf; |
| 379 |
if (dir != 0xf) |
| 380 |
portalCount++; |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
const int maxLinkCount = edgeCount + portalCount*2 + offMeshConLinkCount*2; |
| 386 |
|
| 387 |
// Find unique detail vertices. |
| 388 |
int uniqueDetailVertCount = 0; |
| 389 |
int detailTriCount = 0; |
| 390 |
if (params->detailMeshes) |
| 391 |
{ |
| 392 |
// Has detail mesh, count unique detail vertex count and use input detail tri count. |
| 393 |
detailTriCount = params->detailTriCount; |
| 394 |
for (int i = 0; i < params->polyCount; ++i) |
| 395 |
{ |
| 396 |
const unsigned short* p = ¶ms->polys[i*nvp*2]; |
| 397 |
int ndv = params->detailMeshes[i*4+1]; |
| 398 |
int nv = 0; |
| 399 |
for (int j = 0; j < nvp; ++j) |
| 400 |
{ |
| 401 |
if (p[j] == MESH_NULL_IDX) break; |
| 402 |
nv++; |
| 403 |
} |
| 404 |
ndv -= nv; |
| 405 |
uniqueDetailVertCount += ndv; |
| 406 |
} |
| 407 |
} |
| 408 |
else |
| 409 |
{ |
| 410 |
// No input detail mesh, build detail mesh from nav polys. |
| 411 |
uniqueDetailVertCount = 0; // No extra detail verts. |
| 412 |
detailTriCount = 0; |
| 413 |
for (int i = 0; i < params->polyCount; ++i) |
| 414 |
{ |
| 415 |
const unsigned short* p = ¶ms->polys[i*nvp*2]; |
| 416 |
int nv = 0; |
| 417 |
for (int j = 0; j < nvp; ++j) |
| 418 |
{ |
| 419 |
if (p[j] == MESH_NULL_IDX) break; |
| 420 |
nv++; |
| 421 |
} |
| 422 |
detailTriCount += nv-2; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
// Calculate data size |
| 427 |
const int headerSize = dtAlign4(sizeof(dtMeshHeader)); |
| 428 |
const int vertsSize = dtAlign4(sizeof(float)*3*totVertCount); |
| 429 |
const int polysSize = dtAlign4(sizeof(dtPoly)*totPolyCount); |
| 430 |
const int linksSize = dtAlign4(sizeof(dtLink)*maxLinkCount); |
| 431 |
const int detailMeshesSize = dtAlign4(sizeof(dtPolyDetail)*params->polyCount); |
| 432 |
const int detailVertsSize = dtAlign4(sizeof(float)*3*uniqueDetailVertCount); |
| 433 |
const int detailTrisSize = dtAlign4(sizeof(unsigned char)*4*detailTriCount); |
| 434 |
const int bvTreeSize = params->buildBvTree ? dtAlign4(sizeof(dtBVNode)*params->polyCount*2) : 0; |
| 435 |
const int offMeshConsSize = dtAlign4(sizeof(dtOffMeshConnection)*storedOffMeshConCount); |
| 436 |
|
| 437 |
const int dataSize = headerSize + vertsSize + polysSize + linksSize + |
| 438 |
detailMeshesSize + detailVertsSize + detailTrisSize + |
| 439 |
bvTreeSize + offMeshConsSize; |
| 440 |
|
| 441 |
unsigned char* data = (unsigned char*)dtAlloc(sizeof(unsigned char)*dataSize, DT_ALLOC_PERM); |
| 442 |
if (!data) |
| 443 |
{ |
| 444 |
dtFree(offMeshConClass); |
| 445 |
return false; |
| 446 |
} |
| 447 |
memset(data, 0, dataSize); |
| 448 |
|
| 449 |
unsigned char* d = data; |
| 450 |
|
| 451 |
dtMeshHeader* header = dtGetThenAdvanceBufferPointer<dtMeshHeader>(d, headerSize); |
| 452 |
float* navVerts = dtGetThenAdvanceBufferPointer<float>(d, vertsSize); |
| 453 |
dtPoly* navPolys = dtGetThenAdvanceBufferPointer<dtPoly>(d, polysSize); |
| 454 |
d += linksSize; // Ignore links; just leave enough space for them. They'll be created on load. |
| 455 |
dtPolyDetail* navDMeshes = dtGetThenAdvanceBufferPointer<dtPolyDetail>(d, detailMeshesSize); |
| 456 |
float* navDVerts = dtGetThenAdvanceBufferPointer<float>(d, detailVertsSize); |
| 457 |
unsigned char* navDTris = dtGetThenAdvanceBufferPointer<unsigned char>(d, detailTrisSize); |
| 458 |
dtBVNode* navBvtree = dtGetThenAdvanceBufferPointer<dtBVNode>(d, bvTreeSize); |
| 459 |
dtOffMeshConnection* offMeshCons = dtGetThenAdvanceBufferPointer<dtOffMeshConnection>(d, offMeshConsSize); |
| 460 |
|
| 461 |
|
| 462 |
// Store header |
| 463 |
header->magic = DT_NAVMESH_MAGIC; |
| 464 |
header->version = DT_NAVMESH_VERSION; |
| 465 |
header->x = params->tileX; |
| 466 |
header->y = params->tileY; |
| 467 |
header->layer = params->tileLayer; |
| 468 |
header->userId = params->userId; |
| 469 |
header->polyCount = totPolyCount; |
| 470 |
header->vertCount = totVertCount; |
| 471 |
header->maxLinkCount = maxLinkCount; |
| 472 |
dtVcopy(header->bmin, params->bmin); |
| 473 |
dtVcopy(header->bmax, params->bmax); |
| 474 |
header->detailMeshCount = params->polyCount; |
| 475 |
header->detailVertCount = uniqueDetailVertCount; |
| 476 |
header->detailTriCount = detailTriCount; |
| 477 |
header->bvQuantFactor = 1.0f / params->cs; |
| 478 |
header->offMeshBase = params->polyCount; |
| 479 |
header->walkableHeight = params->walkableHeight; |
| 480 |
header->walkableRadius = params->walkableRadius; |
| 481 |
header->walkableClimb = params->walkableClimb; |
| 482 |
header->offMeshConCount = storedOffMeshConCount; |
| 483 |
header->bvNodeCount = params->buildBvTree ? params->polyCount*2 : 0; |
| 484 |
|
| 485 |
const int offMeshVertsBase = params->vertCount; |
| 486 |
const int offMeshPolyBase = params->polyCount; |
| 487 |
|
| 488 |
// Store vertices |
| 489 |
// Mesh vertices |
| 490 |
for (int i = 0; i < params->vertCount; ++i) |
| 491 |
{ |
| 492 |
const unsigned short* iv = ¶ms->verts[i*3]; |
| 493 |
float* v = &navVerts[i*3]; |
| 494 |
v[0] = params->bmin[0] + iv[0] * params->cs; |
| 495 |
v[1] = params->bmin[1] + iv[1] * params->ch; |
| 496 |
v[2] = params->bmin[2] + iv[2] * params->cs; |
| 497 |
} |
| 498 |
// Off-mesh link vertices. |
| 499 |
int n = 0; |
| 500 |
for (int i = 0; i < params->offMeshConCount; ++i) |
| 501 |
{ |
| 502 |
// Only store connections which start from this tile. |
| 503 |
if (offMeshConClass[i*2+0] == 0xff) |
| 504 |
{ |
| 505 |
const float* linkv = ¶ms->offMeshConVerts[i*2*3]; |
| 506 |
float* v = &navVerts[(offMeshVertsBase + n*2)*3]; |
| 507 |
dtVcopy(&v[0], &linkv[0]); |
| 508 |
dtVcopy(&v[3], &linkv[3]); |
| 509 |
n++; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
// Store polygons |
| 514 |
// Mesh polys |
| 515 |
const unsigned short* src = params->polys; |
| 516 |
for (int i = 0; i < params->polyCount; ++i) |
| 517 |
{ |
| 518 |
dtPoly* p = &navPolys[i]; |
| 519 |
p->vertCount = 0; |
| 520 |
p->flags = params->polyFlags[i]; |
| 521 |
p->setArea(params->polyAreas[i]); |
| 522 |
p->setType(DT_POLYTYPE_GROUND); |
| 523 |
for (int j = 0; j < nvp; ++j) |
| 524 |
{ |
| 525 |
if (src[j] == MESH_NULL_IDX) break; |
| 526 |
p->verts[j] = src[j]; |
| 527 |
if (src[nvp+j] & 0x8000) |
| 528 |
{ |
| 529 |
// Border or portal edge. |
| 530 |
unsigned short dir = src[nvp+j] & 0xf; |
| 531 |
if (dir == 0xf) // Border |
| 532 |
p->neis[j] = 0; |
| 533 |
else if (dir == 0) // Portal x- |
| 534 |
p->neis[j] = DT_EXT_LINK | 4; |
| 535 |
else if (dir == 1) // Portal z+ |
| 536 |
p->neis[j] = DT_EXT_LINK | 2; |
| 537 |
else if (dir == 2) // Portal x+ |
| 538 |
p->neis[j] = DT_EXT_LINK | 0; |
| 539 |
else if (dir == 3) // Portal z- |
| 540 |
p->neis[j] = DT_EXT_LINK | 6; |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
// Normal connection |
| 545 |
p->neis[j] = src[nvp+j]+1; |
| 546 |
} |
| 547 |
|
| 548 |
p->vertCount++; |
| 549 |
} |
| 550 |
src += nvp*2; |
| 551 |
} |
| 552 |
// Off-mesh connection vertices. |
| 553 |
n = 0; |
| 554 |
for (int i = 0; i < params->offMeshConCount; ++i) |
| 555 |
{ |
| 556 |
// Only store connections which start from this tile. |
| 557 |
if (offMeshConClass[i*2+0] == 0xff) |
| 558 |
{ |
| 559 |
dtPoly* p = &navPolys[offMeshPolyBase+n]; |
| 560 |
p->vertCount = 2; |
| 561 |
p->verts[0] = (unsigned short)(offMeshVertsBase + n*2+0); |
| 562 |
p->verts[1] = (unsigned short)(offMeshVertsBase + n*2+1); |
| 563 |
p->flags = params->offMeshConFlags[i]; |
| 564 |
p->setArea(params->offMeshConAreas[i]); |
| 565 |
p->setType(DT_POLYTYPE_OFFMESH_CONNECTION); |
| 566 |
n++; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
// Store detail meshes and vertices. |
| 571 |
// The nav polygon vertices are stored as the first vertices on each mesh. |
| 572 |
// We compress the mesh data by skipping them and using the navmesh coordinates. |
| 573 |
if (params->detailMeshes) |
| 574 |
{ |
| 575 |
unsigned short vbase = 0; |
| 576 |
for (int i = 0; i < params->polyCount; ++i) |
| 577 |
{ |
| 578 |
dtPolyDetail& dtl = navDMeshes[i]; |
| 579 |
const int vb = (int)params->detailMeshes[i*4+0]; |
| 580 |
const int ndv = (int)params->detailMeshes[i*4+1]; |
| 581 |
const int nv = navPolys[i].vertCount; |
| 582 |
dtl.vertBase = (unsigned int)vbase; |
| 583 |
dtl.vertCount = (unsigned char)(ndv-nv); |
| 584 |
dtl.triBase = (unsigned int)params->detailMeshes[i*4+2]; |
| 585 |
dtl.triCount = (unsigned char)params->detailMeshes[i*4+3]; |
| 586 |
// Copy vertices except the first 'nv' verts which are equal to nav poly verts. |
| 587 |
if (ndv-nv) |
| 588 |
{ |
| 589 |
memcpy(&navDVerts[vbase*3], ¶ms->detailVerts[(vb+nv)*3], sizeof(float)*3*(ndv-nv)); |
| 590 |
vbase += (unsigned short)(ndv-nv); |
| 591 |
} |
| 592 |
} |
| 593 |
// Store triangles. |
| 594 |
memcpy(navDTris, params->detailTris, sizeof(unsigned char)*4*params->detailTriCount); |
| 595 |
} |
| 596 |
else |
| 597 |
{ |
| 598 |
// Create dummy detail mesh by triangulating polys. |
| 599 |
int tbase = 0; |
| 600 |
for (int i = 0; i < params->polyCount; ++i) |
| 601 |
{ |
| 602 |
dtPolyDetail& dtl = navDMeshes[i]; |
| 603 |
const int nv = navPolys[i].vertCount; |
| 604 |
dtl.vertBase = 0; |
| 605 |
dtl.vertCount = 0; |
| 606 |
dtl.triBase = (unsigned int)tbase; |
| 607 |
dtl.triCount = (unsigned char)(nv-2); |
| 608 |
// Triangulate polygon (local indices). |
| 609 |
for (int j = 2; j < nv; ++j) |
| 610 |
{ |
| 611 |
unsigned char* t = &navDTris[tbase*4]; |
| 612 |
t[0] = 0; |
| 613 |
t[1] = (unsigned char)(j-1); |
| 614 |
t[2] = (unsigned char)j; |
| 615 |
// Bit for each edge that belongs to poly boundary. |
| 616 |
t[3] = (1<<2); |
| 617 |
if (j == 2) t[3] |= (1<<0); |
| 618 |
if (j == nv-1) t[3] |= (1<<4); |
| 619 |
tbase++; |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
// Store and create BVtree. |
| 625 |
if (params->buildBvTree) |
| 626 |
{ |
| 627 |
createBVTree(params, navBvtree, 2*params->polyCount); |
| 628 |
} |
| 629 |
|
| 630 |
// Store Off-Mesh connections. |
| 631 |
n = 0; |
| 632 |
for (int i = 0; i < params->offMeshConCount; ++i) |
| 633 |
{ |
| 634 |
// Only store connections which start from this tile. |
| 635 |
if (offMeshConClass[i*2+0] == 0xff) |
| 636 |
{ |
| 637 |
dtOffMeshConnection* con = &offMeshCons[n]; |
| 638 |
con->poly = (unsigned short)(offMeshPolyBase + n); |
| 639 |
// Copy connection end-points. |
| 640 |
const float* endPts = ¶ms->offMeshConVerts[i*2*3]; |
| 641 |
dtVcopy(&con->pos[0], &endPts[0]); |
| 642 |
dtVcopy(&con->pos[3], &endPts[3]); |
| 643 |
con->rad = params->offMeshConRad[i]; |
| 644 |
con->flags = params->offMeshConDir[i] ? DT_OFFMESH_CON_BIDIR : 0; |
| 645 |
con->side = offMeshConClass[i*2+1]; |
| 646 |
if (params->offMeshConUserID) |
| 647 |
con->userId = params->offMeshConUserID[i]; |
| 648 |
n++; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
dtFree(offMeshConClass); |
| 653 |
|
| 654 |
*outData = data; |
| 655 |
*outDataSize = dataSize; |
| 656 |
|
| 657 |
return true; |
| 658 |
} |
| 659 |
|
| 660 |
bool dtNavMeshHeaderSwapEndian(unsigned char* data, const int /*dataSize*/) |
| 661 |
{ |
| 662 |
dtMeshHeader* header = (dtMeshHeader*)data; |
| 663 |
|
| 664 |
int swappedMagic = DT_NAVMESH_MAGIC; |
| 665 |
int swappedVersion = DT_NAVMESH_VERSION; |
| 666 |
dtSwapEndian(&swappedMagic); |
| 667 |
dtSwapEndian(&swappedVersion); |
| 668 |
|
| 669 |
if ((header->magic != DT_NAVMESH_MAGIC || header->version != DT_NAVMESH_VERSION) && |
| 670 |
(header->magic != swappedMagic || header->version != swappedVersion)) |
| 671 |
{ |
| 672 |
return false; |
| 673 |
} |
| 674 |
|
| 675 |
dtSwapEndian(&header->magic); |
| 676 |
dtSwapEndian(&header->version); |
| 677 |
dtSwapEndian(&header->x); |
| 678 |
dtSwapEndian(&header->y); |
| 679 |
dtSwapEndian(&header->layer); |
| 680 |
dtSwapEndian(&header->userId); |
| 681 |
dtSwapEndian(&header->polyCount); |
| 682 |
dtSwapEndian(&header->vertCount); |
| 683 |
dtSwapEndian(&header->maxLinkCount); |
| 684 |
dtSwapEndian(&header->detailMeshCount); |
| 685 |
dtSwapEndian(&header->detailVertCount); |
| 686 |
dtSwapEndian(&header->detailTriCount); |
| 687 |
dtSwapEndian(&header->bvNodeCount); |
| 688 |
dtSwapEndian(&header->offMeshConCount); |
| 689 |
dtSwapEndian(&header->offMeshBase); |
| 690 |
dtSwapEndian(&header->walkableHeight); |
| 691 |
dtSwapEndian(&header->walkableRadius); |
| 692 |
dtSwapEndian(&header->walkableClimb); |
| 693 |
dtSwapEndian(&header->bmin[0]); |
| 694 |
dtSwapEndian(&header->bmin[1]); |
| 695 |
dtSwapEndian(&header->bmin[2]); |
| 696 |
dtSwapEndian(&header->bmax[0]); |
| 697 |
dtSwapEndian(&header->bmax[1]); |
| 698 |
dtSwapEndian(&header->bmax[2]); |
| 699 |
dtSwapEndian(&header->bvQuantFactor); |
| 700 |
|
| 701 |
// Freelist index and pointers are updated when tile is added, no need to swap. |
| 702 |
|
| 703 |
return true; |
| 704 |
} |
| 705 |
|
| 706 |
/// @par |
| 707 |
/// |
| 708 |
/// @warning This function assumes that the header is in the correct endianess already. |
| 709 |
/// Call #dtNavMeshHeaderSwapEndian() first on the data if the data is expected to be in wrong endianess |
| 710 |
/// to start with. Call #dtNavMeshHeaderSwapEndian() after the data has been swapped if converting from |
| 711 |
/// native to foreign endianess. |
| 712 |
bool dtNavMeshDataSwapEndian(unsigned char* data, const int /*dataSize*/) |
| 713 |
{ |
| 714 |
// Make sure the data is in right format. |
| 715 |
dtMeshHeader* header = (dtMeshHeader*)data; |
| 716 |
if (header->magic != DT_NAVMESH_MAGIC) |
| 717 |
return false; |
| 718 |
if (header->version != DT_NAVMESH_VERSION) |
| 719 |
return false; |
| 720 |
|
| 721 |
// Patch header pointers. |
| 722 |
const int headerSize = dtAlign4(sizeof(dtMeshHeader)); |
| 723 |
const int vertsSize = dtAlign4(sizeof(float)*3*header->vertCount); |
| 724 |
const int polysSize = dtAlign4(sizeof(dtPoly)*header->polyCount); |
| 725 |
const int linksSize = dtAlign4(sizeof(dtLink)*(header->maxLinkCount)); |
| 726 |
const int detailMeshesSize = dtAlign4(sizeof(dtPolyDetail)*header->detailMeshCount); |
| 727 |
const int detailVertsSize = dtAlign4(sizeof(float)*3*header->detailVertCount); |
| 728 |
const int detailTrisSize = dtAlign4(sizeof(unsigned char)*4*header->detailTriCount); |
| 729 |
const int bvtreeSize = dtAlign4(sizeof(dtBVNode)*header->bvNodeCount); |
| 730 |
const int offMeshLinksSize = dtAlign4(sizeof(dtOffMeshConnection)*header->offMeshConCount); |
| 731 |
|
| 732 |
unsigned char* d = data + headerSize; |
| 733 |
float* verts = dtGetThenAdvanceBufferPointer<float>(d, vertsSize); |
| 734 |
dtPoly* polys = dtGetThenAdvanceBufferPointer<dtPoly>(d, polysSize); |
| 735 |
d += linksSize; // Ignore links; they technically should be endian-swapped but all their data is overwritten on load anyway. |
| 736 |
//dtLink* links = dtGetThenAdvanceBufferPointer<dtLink>(d, linksSize); |
| 737 |
dtPolyDetail* detailMeshes = dtGetThenAdvanceBufferPointer<dtPolyDetail>(d, detailMeshesSize); |
| 738 |
float* detailVerts = dtGetThenAdvanceBufferPointer<float>(d, detailVertsSize); |
| 739 |
d += detailTrisSize; // Ignore detail tris; single bytes can't be endian-swapped. |
| 740 |
//unsigned char* detailTris = dtGetThenAdvanceBufferPointer<unsigned char>(d, detailTrisSize); |
| 741 |
dtBVNode* bvTree = dtGetThenAdvanceBufferPointer<dtBVNode>(d, bvtreeSize); |
| 742 |
dtOffMeshConnection* offMeshCons = dtGetThenAdvanceBufferPointer<dtOffMeshConnection>(d, offMeshLinksSize); |
| 743 |
|
| 744 |
// Vertices |
| 745 |
for (int i = 0; i < header->vertCount*3; ++i) |
| 746 |
{ |
| 747 |
dtSwapEndian(&verts[i]); |
| 748 |
} |
| 749 |
|
| 750 |
// Polys |
| 751 |
for (int i = 0; i < header->polyCount; ++i) |
| 752 |
{ |
| 753 |
dtPoly* p = &polys[i]; |
| 754 |
// poly->firstLink is update when tile is added, no need to swap. |
| 755 |
for (int j = 0; j < DT_VERTS_PER_POLYGON; ++j) |
| 756 |
{ |
| 757 |
dtSwapEndian(&p->verts[j]); |
| 758 |
dtSwapEndian(&p->neis[j]); |
| 759 |
} |
| 760 |
dtSwapEndian(&p->flags); |
| 761 |
} |
| 762 |
|
| 763 |
// Links are rebuild when tile is added, no need to swap. |
| 764 |
|
| 765 |
// Detail meshes |
| 766 |
for (int i = 0; i < header->detailMeshCount; ++i) |
| 767 |
{ |
| 768 |
dtPolyDetail* pd = &detailMeshes[i]; |
| 769 |
dtSwapEndian(&pd->vertBase); |
| 770 |
dtSwapEndian(&pd->triBase); |
| 771 |
} |
| 772 |
|
| 773 |
// Detail verts |
| 774 |
for (int i = 0; i < header->detailVertCount*3; ++i) |
| 775 |
{ |
| 776 |
dtSwapEndian(&detailVerts[i]); |
| 777 |
} |
| 778 |
|
| 779 |
// BV-tree |
| 780 |
for (int i = 0; i < header->bvNodeCount; ++i) |
| 781 |
{ |
| 782 |
dtBVNode* node = &bvTree[i]; |
| 783 |
for (int j = 0; j < 3; ++j) |
| 784 |
{ |
| 785 |
dtSwapEndian(&node->bmin[j]); |
| 786 |
dtSwapEndian(&node->bmax[j]); |
| 787 |
} |
| 788 |
dtSwapEndian(&node->i); |
| 789 |
} |
| 790 |
|
| 791 |
// Off-mesh Connections. |
| 792 |
for (int i = 0; i < header->offMeshConCount; ++i) |
| 793 |
{ |
| 794 |
dtOffMeshConnection* con = &offMeshCons[i]; |
| 795 |
for (int j = 0; j < 6; ++j) |
| 796 |
dtSwapEndian(&con->pos[j]); |
| 797 |
dtSwapEndian(&con->rad); |
| 798 |
dtSwapEndian(&con->poly); |
| 799 |
} |
| 800 |
|
| 801 |
return true; |
| 802 |
} |