Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty.  In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would be
//    appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
//    misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include "DetourNavMesh.h"
#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourNavMeshBuilder.h"
#include "DetourAlloc.h"
#include "DetourAssert.h"

static unsigned short MESH_NULL_IDX = 0xffff;


struct BVItem
{
    unsigned short bmin[3];
    unsigned short bmax[3];
    int i;
};

static int compareItemX(const void* va, const void* vb)
{
    const BVItem* a = (const BVItem*)va;
    const BVItem* b = (const BVItem*)vb;
    if (a->bmin[0] < b->bmin[0])
        return -1;
    if (a->bmin[0] > b->bmin[0])
        return 1;
    return 0;
}

static int compareItemY(const void* va, const void* vb)
{
    const BVItem* a = (const BVItem*)va;
    const BVItem* b = (const BVItem*)vb;
    if (a->bmin[1] < b->bmin[1])
        return -1;
    if (a->bmin[1] > b->bmin[1])
        return 1;
    return 0;
}

static int compareItemZ(const void* va, const void* vb)
{
    const BVItem* a = (const BVItem*)va;
    const BVItem* b = (const BVItem*)vb;
    if (a->bmin[2] < b->bmin[2])
        return -1;
    if (a->bmin[2] > b->bmin[2])
        return 1;
    return 0;
}

static void calcExtends(BVItem* items, const int /*nitems*/, const int imin, const int imax,
                        unsigned short* bmin, unsigned short* bmax)
{
    bmin[0] = items[imin].bmin[0];
    bmin[1] = items[imin].bmin[1];
    bmin[2] = items[imin].bmin[2];
    
    bmax[0] = items[imin].bmax[0];
    bmax[1] = items[imin].bmax[1];
    bmax[2] = items[imin].bmax[2];
    
    for (int i = imin+1; i < imax; ++i)
    {
        const BVItem& it = items[i];
        if (it.bmin[0] < bmin[0]) bmin[0] = it.bmin[0];
        if (it.bmin[1] < bmin[1]) bmin[1] = it.bmin[1];
        if (it.bmin[2] < bmin[2]) bmin[2] = it.bmin[2];
        
        if (it.bmax[0] > bmax[0]) bmax[0] = it.bmax[0];
        if (it.bmax[1] > bmax[1]) bmax[1] = it.bmax[1];
        if (it.bmax[2] > bmax[2]) bmax[2] = it.bmax[2];
    }
}

inline int longestAxis(unsigned short x, unsigned short y, unsigned short z)
{
    int    axis = 0;
    unsigned short maxVal = x;
    if (y > maxVal)
    {
        axis = 1;
        maxVal = y;
    }
    if (z > maxVal)
    {
        axis = 2;
        maxVal = z;
    }
    return axis;
}

static void subdivide(BVItem* items, int nitems, int imin, int imax, int& curNode, dtBVNode* nodes)
{
    int inum = imax - imin;
    int icur = curNode;
    
    dtBVNode& node = nodes[curNode++];
    
    if (inum == 1)
    {
        // Leaf
        node.bmin[0] = items[imin].bmin[0];
        node.bmin[1] = items[imin].bmin[1];
        node.bmin[2] = items[imin].bmin[2];
        
        node.bmax[0] = items[imin].bmax[0];
        node.bmax[1] = items[imin].bmax[1];
        node.bmax[2] = items[imin].bmax[2];
        
        node.i = items[imin].i;
    }
    else
    {
        // Split
        calcExtends(items, nitems, imin, imax, node.bmin, node.bmax);
        
        int    axis = longestAxis(node.bmax[0] - node.bmin[0],
                               node.bmax[1] - node.bmin[1],
                               node.bmax[2] - node.bmin[2]);
        
        if (axis == 0)
        {
            // Sort along x-axis
            qsort(items+imin, inum, sizeof(BVItem), compareItemX);
        }
        else if (axis == 1)
        {
            // Sort along y-axis
            qsort(items+imin, inum, sizeof(BVItem), compareItemY);
        }
        else
        {
            // Sort along z-axis
            qsort(items+imin, inum, sizeof(BVItem), compareItemZ);
        }
        
        int isplit = imin+inum/2;
        
        // Left
        subdivide(items, nitems, imin, isplit, curNode, nodes);
        // Right
        subdivide(items, nitems, isplit, imax, curNode, nodes);
        
        int iescape = curNode - icur;
        // Negative index means escape.
        node.i = -iescape;
    }
}

static int createBVTree(const unsigned short* verts, const int /*nverts*/,
                        const unsigned short* polys, const int npolys, const int nvp,
                        const float cs, const float ch,
                        const int /*nnodes*/, dtBVNode* nodes)
{
    // Build tree
    BVItem* items = (BVItem*)dtAlloc(sizeof(BVItem)*npolys, DT_ALLOC_TEMP);
    for (int i = 0; i < npolys; i++)
    {
        BVItem& it = items[i];
        it.i = i;
        // Calc polygon bounds.
        const unsigned short* p = &polys[i*nvp*2];
        it.bmin[0] = it.bmax[0] = verts[p[0]*3+0];
        it.bmin[1] = it.bmax[1] = verts[p[0]*3+1];
        it.bmin[2] = it.bmax[2] = verts[p[0]*3+2];
        
        for (int j = 1; j < nvp; ++j)
        {
            if (p[j] == MESH_NULL_IDX) break;
            unsigned short x = verts[p[j]*3+0];
            unsigned short y = verts[p[j]*3+1];
            unsigned short z = verts[p[j]*3+2];
            
            if (x < it.bmin[0]) it.bmin[0] = x;
            if (y < it.bmin[1]) it.bmin[1] = y;
            if (z < it.bmin[2]) it.bmin[2] = z;
            
            if (x > it.bmax[0]) it.bmax[0] = x;
            if (y > it.bmax[1]) it.bmax[1] = y;
            if (z > it.bmax[2]) it.bmax[2] = z;
        }
        // Remap y
        it.bmin[1] = (unsigned short)dtMathFloorf((float)it.bmin[1]*ch/cs);
        it.bmax[1] = (unsigned short)dtMathCeilf((float)it.bmax[1]*ch/cs);
    }
    
    int curNode = 0;
    subdivide(items, npolys, 0, npolys, curNode, nodes);
    
    dtFree(items);
    
    return curNode;
}

static unsigned char classifyOffMeshPoint(const float* pt, const float* bmin, const float* bmax)
{
    static const unsigned char XP = 1<<0;
    static const unsigned char ZP = 1<<1;
    static const unsigned char XM = 1<<2;
    static const unsigned char ZM = 1<<3;    

    unsigned char outcode = 0; 
    outcode |= (pt[0] >= bmax[0]) ? XP : 0;
    outcode |= (pt[2] >= bmax[2]) ? ZP : 0;
    outcode |= (pt[0] < bmin[0])  ? XM : 0;
    outcode |= (pt[2] < bmin[2])  ? ZM : 0;

    switch (outcode)
    {
    case XP: return 0;
    case XP|ZP: return 1;
    case ZP: return 2;
    case XM|ZP: return 3;
    case XM: return 4;
    case XM|ZM: return 5;
    case ZM: return 6;
    case XP|ZM: return 7;
    };

    return 0xff;    
}

// TODO: Better error handling.

/// @par
/// 
/// The output data array is allocated using the detour allocator (dtAlloc()).  The method
/// used to free the memory will be determined by how the tile is added to the navigation
/// mesh.
///
/// @see dtNavMesh, dtNavMesh::addTile()
bool dtCreateNavMeshData(dtNavMeshCreateParams* params, unsigned char** outData, int* outDataSize)
{
    if (params->nvp > DT_VERTS_PER_POLYGON)
        return false;
    if (params->vertCount >= 0xffff)
        return false;
    if (!params->vertCount || !params->verts)
        return false;
    if (!params->polyCount || !params->polys)
        return false;

    const int nvp = params->nvp;
    
    // Classify off-mesh connection points. We store only the connections
    // whose start point is inside the tile.
    unsigned char* offMeshConClass = 0;
    int storedOffMeshConCount = 0;
    int offMeshConLinkCount = 0;
    
    if (params->offMeshConCount > 0)
    {
        offMeshConClass = (unsigned char*)dtAlloc(sizeof(unsigned char)*params->offMeshConCount*2, DT_ALLOC_TEMP);
        if (!offMeshConClass)
            return false;

        // Find tight heigh bounds, used for culling out off-mesh start locations.
        float hmin = FLT_MAX;
        float hmax = -FLT_MAX;
        
        if (params->detailVerts && params->detailVertsCount)
        {
            for (int i = 0; i < params->detailVertsCount; ++i)
            {
                const float h = params->detailVerts[i*3+1];
                hmin = dtMin(hmin,h);
                hmax = dtMax(hmax,h);
            }
        }
        else
        {
            for (int i = 0; i < params->vertCount; ++i)
            {
                const unsigned short* iv = &params->verts[i*3];
                const float h = params->bmin[1] + iv[1] * params->ch;
                hmin = dtMin(hmin,h);
                hmax = dtMax(hmax,h);
            }
        }
        hmin -= params->walkableClimb;
        hmax += params->walkableClimb;
        float bmin[3], bmax[3];
        dtVcopy(bmin, params->bmin);
        dtVcopy(bmax, params->bmax);
        bmin[1] = hmin;
        bmax[1] = hmax;

        for (int i = 0; i < params->offMeshConCount; ++i)
        {
            const float* p0 = &params->offMeshConVerts[(i*2+0)*3];
            const float* p1 = &params->offMeshConVerts[(i*2+1)*3];
            offMeshConClass[i*2+0] = classifyOffMeshPoint(p0, bmin, bmax);
            offMeshConClass[i*2+1] = classifyOffMeshPoint(p1, bmin, bmax);

            // Zero out off-mesh start positions which are not even potentially touching the mesh.
            if (offMeshConClass[i*2+0] == 0xff)
            {
                if (p0[1] < bmin[1] || p0[1] > bmax[1])
                    offMeshConClass[i*2+0] = 0;
            }

            // Cound how many links should be allocated for off-mesh connections.
            if (offMeshConClass[i*2+0] == 0xff)
                offMeshConLinkCount++;
            if (offMeshConClass[i*2+1] == 0xff)
                offMeshConLinkCount++;

            if (offMeshConClass[i*2+0] == 0xff)
                storedOffMeshConCount++;
        }
    }
    
    // Off-mesh connectionss are stored as polygons, adjust values.
    const int totPolyCount = params->polyCount + storedOffMeshConCount;
    const int totVertCount = params->vertCount + storedOffMeshConCount*2;
    
    // Find portal edges which are at tile borders.
    int edgeCount = 0;
    int portalCount = 0;
    for (int i = 0; i < params->polyCount; ++i)
    {
        const unsigned short* p = &params->polys[i*2*nvp];
        for (int j = 0; j < nvp; ++j)
        {
            if (p[j] == MESH_NULL_IDX) break;
            edgeCount++;
            
            if (p[nvp+j] & 0x8000)
            {
                unsigned short dir = p[nvp+j] & 0xf;
                if (dir != 0xf)
                    portalCount++;
            }
        }
    }

    const int maxLinkCount = edgeCount + portalCount*2 + offMeshConLinkCount*2;
    
    // Find unique detail vertices.
    int uniqueDetailVertCount = 0;
    int detailTriCount = 0;
    if (params->detailMeshes)
    {
        // Has detail mesh, count unique detail vertex count and use input detail tri count.
        detailTriCount = params->detailTriCount;
        for (int i = 0; i < params->polyCount; ++i)
        {
            const unsigned short* p = &params->polys[i*nvp*2];
            int ndv = params->detailMeshes[i*4+1];
            int nv = 0;
            for (int j = 0; j < nvp; ++j)
            {
                if (p[j] == MESH_NULL_IDX) break;
                nv++;
            }
            ndv -= nv;
            uniqueDetailVertCount += ndv;
        }
    }
    else
    {
        // No input detail mesh, build detail mesh from nav polys.
        uniqueDetailVertCount = 0; // No extra detail verts.
        detailTriCount = 0;
        for (int i = 0; i < params->polyCount; ++i)
        {
            const unsigned short* p = &params->polys[i*nvp*2];
            int nv = 0;
            for (int j = 0; j < nvp; ++j)
            {
                if (p[j] == MESH_NULL_IDX) break;
                nv++;
            }
            detailTriCount += nv-2;
        }
    }
    
    // Calculate data size
    const int headerSize = dtAlign4(sizeof(dtMeshHeader));
    const int vertsSize = dtAlign4(sizeof(float)*3*totVertCount);
    const int polysSize = dtAlign4(sizeof(dtPoly)*totPolyCount);
    const int linksSize = dtAlign4(sizeof(dtLink)*maxLinkCount);
    const int detailMeshesSize = dtAlign4(sizeof(dtPolyDetail)*params->polyCount);
    const int detailVertsSize = dtAlign4(sizeof(float)*3*uniqueDetailVertCount);
    const int detailTrisSize = dtAlign4(sizeof(unsigned char)*4*detailTriCount);
    const int bvTreeSize = params->buildBvTree ? dtAlign4(sizeof(dtBVNode)*params->polyCount*2) : 0;
    const int offMeshConsSize = dtAlign4(sizeof(dtOffMeshConnection)*storedOffMeshConCount);
    
    const int dataSize = headerSize + vertsSize + polysSize + linksSize +
                         detailMeshesSize + detailVertsSize + detailTrisSize +
                         bvTreeSize + offMeshConsSize;
                         
    unsigned char* data = (unsigned char*)dtAlloc(sizeof(unsigned char)*dataSize, DT_ALLOC_PERM);
    if (!data)
    {
        dtFree(offMeshConClass);
        return false;
    }
    memset(data, 0, dataSize);
    
    unsigned char* d = data;
    dtMeshHeader* header = (dtMeshHeader*)d; d += headerSize;
    float* navVerts = (float*)d; d += vertsSize;
    dtPoly* navPolys = (dtPoly*)d; d += polysSize;
    d += linksSize;
    dtPolyDetail* navDMeshes = (dtPolyDetail*)d; d += detailMeshesSize;
    float* navDVerts = (float*)d; d += detailVertsSize;
    unsigned char* navDTris = (unsigned char*)d; d += detailTrisSize;
    dtBVNode* navBvtree = (dtBVNode*)d; d += bvTreeSize;
    dtOffMeshConnection* offMeshCons = (dtOffMeshConnection*)d; d += offMeshConsSize;
    
    
    // Store header
    header->magic = DT_NAVMESH_MAGIC;
    header->version = DT_NAVMESH_VERSION;
    header->x = params->tileX;
    header->y = params->tileY;
    header->layer = params->tileLayer;
    header->userId = params->userId;
    header->polyCount = totPolyCount;
    header->vertCount = totVertCount;
    header->maxLinkCount = maxLinkCount;
    dtVcopy(header->bmin, params->bmin);
    dtVcopy(header->bmax, params->bmax);
    header->detailMeshCount = params->polyCount;
    header->detailVertCount = uniqueDetailVertCount;
    header->detailTriCount = detailTriCount;
    header->bvQuantFactor = 1.0f / params->cs;
    header->offMeshBase = params->polyCount;
    header->walkableHeight = params->walkableHeight;
    header->walkableRadius = params->walkableRadius;
    header->walkableClimb = params->walkableClimb;
    header->offMeshConCount = storedOffMeshConCount;
    header->bvNodeCount = params->buildBvTree ? params->polyCount*2 : 0;
    
    const int offMeshVertsBase = params->vertCount;
    const int offMeshPolyBase = params->polyCount;
    
    // Store vertices
    // Mesh vertices
    for (int i = 0; i < params->vertCount; ++i)
    {
        const unsigned short* iv = &params->verts[i*3];
        float* v = &navVerts[i*3];
        v[0] = params->bmin[0] + iv[0] * params->cs;
        v[1] = params->bmin[1] + iv[1] * params->ch;
        v[2] = params->bmin[2] + iv[2] * params->cs;
    }
    // Off-mesh link vertices.
    int n = 0;
    for (int i = 0; i < params->offMeshConCount; ++i)
    {
        // Only store connections which start from this tile.
        if (offMeshConClass[i*2+0] == 0xff)
        {
            const float* linkv = &params->offMeshConVerts[i*2*3];
            float* v = &navVerts[(offMeshVertsBase + n*2)*3];
            dtVcopy(&v[0], &linkv[0]);
            dtVcopy(&v[3], &linkv[3]);
            n++;
        }
    }
    
    // Store polygons
    // Mesh polys
    const unsigned short* src = params->polys;
    for (int i = 0; i < params->polyCount; ++i)
    {
        dtPoly* p = &navPolys[i];
        p->vertCount = 0;
        p->flags = params->polyFlags[i];
        p->setArea(params->polyAreas[i]);
        p->setType(DT_POLYTYPE_GROUND);
        for (int j = 0; j < nvp; ++j)
        {
            if (src[j] == MESH_NULL_IDX) break;
            p->verts[j] = src[j];
            if (src[nvp+j] & 0x8000)
            {
                // Border or portal edge.
                unsigned short dir = src[nvp+j] & 0xf;
                if (dir == 0xf) // Border
                    p->neis[j] = 0;
                else if (dir == 0) // Portal x-
                    p->neis[j] = DT_EXT_LINK | 4;
                else if (dir == 1) // Portal z+
                    p->neis[j] = DT_EXT_LINK | 2;
                else if (dir == 2) // Portal x+
                    p->neis[j] = DT_EXT_LINK | 0;
                else if (dir == 3) // Portal z-
                    p->neis[j] = DT_EXT_LINK | 6;
            }
            else
            {
                // Normal connection
                p->neis[j] = src[nvp+j]+1;
            }
            
            p->vertCount++;
        }
        src += nvp*2;
    }
    // Off-mesh connection vertices.
    n = 0;
    for (int i = 0; i < params->offMeshConCount; ++i)
    {
        // Only store connections which start from this tile.
        if (offMeshConClass[i*2+0] == 0xff)
        {
            dtPoly* p = &navPolys[offMeshPolyBase+n];
            p->vertCount = 2;
            p->verts[0] = (unsigned short)(offMeshVertsBase + n*2+0);
            p->verts[1] = (unsigned short)(offMeshVertsBase + n*2+1);
            p->flags = params->offMeshConFlags[i];
            p->setArea(params->offMeshConAreas[i]);
            p->setType(DT_POLYTYPE_OFFMESH_CONNECTION);
            n++;
        }
    }

    // Store detail meshes and vertices.
    // The nav polygon vertices are stored as the first vertices on each mesh.
    // We compress the mesh data by skipping them and using the navmesh coordinates.
    if (params->detailMeshes)
    {
        unsigned short vbase = 0;
        for (int i = 0; i < params->polyCount; ++i)
        {
            dtPolyDetail& dtl = navDMeshes[i];
            const int vb = (int)params->detailMeshes[i*4+0];
            const int ndv = (int)params->detailMeshes[i*4+1];
            const int nv = navPolys[i].vertCount;
            dtl.vertBase = (unsigned int)vbase;
            dtl.vertCount = (unsigned char)(ndv-nv);
            dtl.triBase = (unsigned int)params->detailMeshes[i*4+2];
            dtl.triCount = (unsigned char)params->detailMeshes[i*4+3];
            // Copy vertices except the first 'nv' verts which are equal to nav poly verts.
            if (ndv-nv)
            {
                memcpy(&navDVerts[vbase*3], &params->detailVerts[(vb+nv)*3], sizeof(float)*3*(ndv-nv));
                vbase += (unsigned short)(ndv-nv);
            }
        }
        // Store triangles.
        memcpy(navDTris, params->detailTris, sizeof(unsigned char)*4*params->detailTriCount);
    }
    else
    {
        // Create dummy detail mesh by triangulating polys.
        int tbase = 0;
        for (int i = 0; i < params->polyCount; ++i)
        {
            dtPolyDetail& dtl = navDMeshes[i];
            const int nv = navPolys[i].vertCount;
            dtl.vertBase = 0;
            dtl.vertCount = 0;
            dtl.triBase = (unsigned int)tbase;
            dtl.triCount = (unsigned char)(nv-2);
            // Triangulate polygon (local indices).
            for (int j = 2; j < nv; ++j)
            {
                unsigned char* t = &navDTris[tbase*4];
                t[0] = 0;
                t[1] = (unsigned char)(j-1);
                t[2] = (unsigned char)j;
                // Bit for each edge that belongs to poly boundary.
                t[3] = (1<<2);
                if (j == 2) t[3] |= (1<<0);
                if (j == nv-1) t[3] |= (1<<4);
                tbase++;
            }
        }
    }

    // Store and create BVtree.
    // TODO: take detail mesh into account! use byte per bbox extent?
    if (params->buildBvTree)
    {
        createBVTree(params->verts, params->vertCount, params->polys, params->polyCount,
                     nvp, params->cs, params->ch, params->polyCount*2, navBvtree);
    }
    
    // Store Off-Mesh connections.
    n = 0;
    for (int i = 0; i < params->offMeshConCount; ++i)
    {
        // Only store connections which start from this tile.
        if (offMeshConClass[i*2+0] == 0xff)
        {
            dtOffMeshConnection* con = &offMeshCons[n];
            con->poly = (unsigned short)(offMeshPolyBase + n);
            // Copy connection end-points.
            const float* endPts = &params->offMeshConVerts[i*2*3];
            dtVcopy(&con->pos[0], &endPts[0]);
            dtVcopy(&con->pos[3], &endPts[3]);
            con->rad = params->offMeshConRad[i];
            con->flags = params->offMeshConDir[i] ? DT_OFFMESH_CON_BIDIR : 0;
            con->side = offMeshConClass[i*2+1];
            if (params->offMeshConUserID)
                con->userId = params->offMeshConUserID[i];
            n++;
        }
    }
        
    dtFree(offMeshConClass);
    
    *outData = data;
    *outDataSize = dataSize;
    
    return true;
}

bool dtNavMeshHeaderSwapEndian(unsigned char* data, const int /*dataSize*/)
{
    dtMeshHeader* header = (dtMeshHeader*)data;
    
    int swappedMagic = DT_NAVMESH_MAGIC;
    int swappedVersion = DT_NAVMESH_VERSION;
    dtSwapEndian(&swappedMagic);
    dtSwapEndian(&swappedVersion);
    
    if ((header->magic != DT_NAVMESH_MAGIC || header->version != DT_NAVMESH_VERSION) &&
        (header->magic != swappedMagic || header->version != swappedVersion))
    {
        return false;
    }
        
    dtSwapEndian(&header->magic);
    dtSwapEndian(&header->version);
    dtSwapEndian(&header->x);
    dtSwapEndian(&header->y);
    dtSwapEndian(&header->layer);
    dtSwapEndian(&header->userId);
    dtSwapEndian(&header->polyCount);
    dtSwapEndian(&header->vertCount);
    dtSwapEndian(&header->maxLinkCount);
    dtSwapEndian(&header->detailMeshCount);
    dtSwapEndian(&header->detailVertCount);
    dtSwapEndian(&header->detailTriCount);
    dtSwapEndian(&header->bvNodeCount);
    dtSwapEndian(&header->offMeshConCount);
    dtSwapEndian(&header->offMeshBase);
    dtSwapEndian(&header->walkableHeight);
    dtSwapEndian(&header->walkableRadius);
    dtSwapEndian(&header->walkableClimb);
    dtSwapEndian(&header->bmin[0]);
    dtSwapEndian(&header->bmin[1]);
    dtSwapEndian(&header->bmin[2]);
    dtSwapEndian(&header->bmax[0]);
    dtSwapEndian(&header->bmax[1]);
    dtSwapEndian(&header->bmax[2]);
    dtSwapEndian(&header->bvQuantFactor);

    // Freelist index and pointers are updated when tile is added, no need to swap.

    return true;
}

/// @par
///
/// @warning This function assumes that the header is in the correct endianess already. 
/// Call #dtNavMeshHeaderSwapEndian() first on the data if the data is expected to be in wrong endianess 
/// to start with. Call #dtNavMeshHeaderSwapEndian() after the data has been swapped if converting from 
/// native to foreign endianess.
bool dtNavMeshDataSwapEndian(unsigned char* data, const int /*dataSize*/)
{
    // Make sure the data is in right format.
    dtMeshHeader* header = (dtMeshHeader*)data;
    if (header->magic != DT_NAVMESH_MAGIC)
        return false;
    if (header->version != DT_NAVMESH_VERSION)
        return false;
    
    // Patch header pointers.
    const int headerSize = dtAlign4(sizeof(dtMeshHeader));
    const int vertsSize = dtAlign4(sizeof(float)*3*header->vertCount);
    const int polysSize = dtAlign4(sizeof(dtPoly)*header->polyCount);
    const int linksSize = dtAlign4(sizeof(dtLink)*(header->maxLinkCount));
    const int detailMeshesSize = dtAlign4(sizeof(dtPolyDetail)*header->detailMeshCount);
    const int detailVertsSize = dtAlign4(sizeof(float)*3*header->detailVertCount);
    const int detailTrisSize = dtAlign4(sizeof(unsigned char)*4*header->detailTriCount);
    const int bvtreeSize = dtAlign4(sizeof(dtBVNode)*header->bvNodeCount);
    const int offMeshLinksSize = dtAlign4(sizeof(dtOffMeshConnection)*header->offMeshConCount);
    
    unsigned char* d = data + headerSize;
    float* verts = (float*)d; d += vertsSize;
    dtPoly* polys = (dtPoly*)d; d += polysSize;
    /*dtLink* links = (dtLink*)d;*/ d += linksSize;
    dtPolyDetail* detailMeshes = (dtPolyDetail*)d; d += detailMeshesSize;
    float* detailVerts = (float*)d; d += detailVertsSize;
    /*unsigned char* detailTris = (unsigned char*)d;*/ d += detailTrisSize;
    dtBVNode* bvTree = (dtBVNode*)d; d += bvtreeSize;
    dtOffMeshConnection* offMeshCons = (dtOffMeshConnection*)d; d += offMeshLinksSize;
    
    // Vertices
    for (int i = 0; i < header->vertCount*3; ++i)
    {
        dtSwapEndian(&verts[i]);
    }

    // Polys
    for (int i = 0; i < header->polyCount; ++i)
    {
        dtPoly* p = &polys[i];
        // poly->firstLink is update when tile is added, no need to swap.
        for (int j = 0; j < DT_VERTS_PER_POLYGON; ++j)
        {
            dtSwapEndian(&p->verts[j]);
            dtSwapEndian(&p->neis[j]);
        }
        dtSwapEndian(&p->flags);
    }

    // Links are rebuild when tile is added, no need to swap.

    // Detail meshes
    for (int i = 0; i < header->detailMeshCount; ++i)
    {
        dtPolyDetail* pd = &detailMeshes[i];
        dtSwapEndian(&pd->vertBase);
        dtSwapEndian(&pd->triBase);
    }
    
    // Detail verts
    for (int i = 0; i < header->detailVertCount*3; ++i)
    {
        dtSwapEndian(&detailVerts[i]);
    }

    // BV-tree
    for (int i = 0; i < header->bvNodeCount; ++i)
    {
        dtBVNode* node = &bvTree[i];
        for (int j = 0; j < 3; ++j)
        {
            dtSwapEndian(&node->bmin[j]);
            dtSwapEndian(&node->bmax[j]);
        }
        dtSwapEndian(&node->i);
    }

    // Off-mesh Connections.
    for (int i = 0; i < header->offMeshConCount; ++i)
    {
        dtOffMeshConnection* con = &offMeshCons[i];
        for (int j = 0; j < 6; ++j)
            dtSwapEndian(&con->pos[j]);
        dtSwapEndian(&con->rad);
        dtSwapEndian(&con->poly);
    }
    
    return true;
}

Commits for Divide-Framework/trunk/Source Code/Libs/src/ReCast/Detour/Source/DetourNavMeshBuilder.cpp

Diff revisions: vs.
Revision Author Commited Message
337 Diff Diff IonutCava picture IonutCava Wed 17 Dec, 2014 17:25:16 +0000

[Ionut]
- Replaced all tabs with 4 spaces (some spacing may be off now. no way to check all the files.)
- Moved implementation details for ParamHandler, BoundingBox, BoundingSphere and ProfileTimer to proper .inl and .cpp files. (more to follow)
- Update all forward declared enums with the required storage type (as requested by the C++11 standard)

315 Diff Diff IonutCava picture IonutCava Mon 25 Aug, 2014 15:23:33 +0000

[Ionut]
- Update ReCast to the latest version available on GitHub
- Improved nav mesh target point selection
- Added basic “go to enemy flag” logic for the WarScene teams

152 Diff Diff k1ngp1n picture k1ngp1n Thu 20 Jun, 2013 14:23:31 +0000

[Ionut] [[BR]]
- New rendering pipeline (experimental and unstable): [[BR]]
— OpenGL 3.x clean context (glLight commands are still used for now, but UBO support is there, but not used yet) [[BR]]
— Custom matrix stacks and UBO based shader data (UBOs cause some link errors on some nVidia cards)[[BR]]
— Multi-threaded texture loading and shader compilation (mt shader compilation disabled for now. needs more tests) [[BR]]
— VAO rendering is self contained (no more enable/disable calls before a draw request) [[BR]]
— Render instance class used for rendering information [[BR]]
[[BR]]
- New unique ID generation base class: GUIDWrapper. Each class that derives from this, gets an application wide unique id on construction [[BR]]
- Added base code for an in-game editor using CEGUI [[BR]]
- Replaced some mutex locks with lockless data structures or atomic data [[BR]]
- CEGUI 0.8.2, FTGL-GL3 and GLIM 0.4 modified to support GLEW_MX if defined [[BR]]
- Dropped VS2008 support [[BR]]
- Networking library, server framework and ReCast files are now projects inside the Divide Framework solution [[BR]]
- New console command “setFov” that modifies the horizontal FoV value in realtime [[BR]]
- New console command “recompileShader” that reparses shader files, recompiles shaders and relinks shader programs who’s names match or contain the argument string [[BR]]
- New debug utility: axis angle drawn in corner of the screen if needed [[BR]]
- More ‘const’-s pasted around the code, more loop performance tweaks (allocations before a loop, tighter loops, etc), more utility member functions added to various classes [[BR]]

151 k1ngp1n picture k1ngp1n Tue 07 May, 2013 00:00:52 +0000

[Ionut] [[BR]]
- Better ReCast integration [[BR]]
— Any entity can be used as an obstacle in the NavMesh if that’s specified in each scene’s assets.xml file for said entity [[BR]]
— New console command, “createNavMesh”, that passes geometry data to ReCast [[BR]]
-— NavMesh generation is not yet complete [[BR]]
- Better PhysX integration [[BR]]
— Each scene passes data to and gets data from the Physics API better and safer[[BR]]
- Multi-threaded support extended: [[BR]]
— Event class replaced with Task [[BR]]
— Kernel uses a threadpool for scene tasks [[BR]]
— Each task can be added to a different threadpool if needed [[BR]]
— Most critical sections use the new boost::atomic wrapper for thread safety [[BR]]
- Rendering system updated [[BR]]
— VBO’s keep track of the geometry type internally [[BR]]
— Rendering by specific element list is internal to the VBO [[BR]]
— VBO’s can draw a “lite” version that doesn’t send Tangent, BiTangent and TexCoord data for depth rendering (disabled) [[BR]]
- Reflectors now offer a preview of the generated reflective texture if needed [[BR]]
- Buttons are now added and rendered by CEGUI [[BR]]
[[BR]]
Deprecation: [[BR]]
- Removed fixed pipeline material system [[BR]]
- Removed BoundingBox late update system [[BR]]
- Shadow references in the material system are now depth references [[BR]]
[[BR]]
Bug fixes: [[BR]]
- Problematic FBO double bind check removed [[BR]]
- Faster terrain rendering via chunk size selection through each scene’s terrain.xml[[BR]]
- Rendering is paused when the window is minimized [[BR]]
- CEGUI and impostors now render properly in deferred rendering mode [[BR]]
- Fixed various level 4 (VS2008) warnings [[BR]]
- Better state management and cleaning [[BR]]
- Better SceneGraph node management and trasformation/AABB update calls [[BR]]
- AITenisScene has proper collisions now [[BR]]