DetourTileCacheBuilder.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  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. #ifndef DETOURTILECACHEBUILDER_H
  19. #define DETOURTILECACHEBUILDER_H
  20. #include "DetourAlloc.h"
  21. #include "DetourStatus.h"
  22. static const int DT_TILECACHE_MAGIC = 'D'<<24 | 'T'<<16 | 'L'<<8 | 'R'; ///< 'DTLR';
  23. static const int DT_TILECACHE_VERSION = 1;
  24. static const unsigned char DT_TILECACHE_NULL_AREA = 0;
  25. static const unsigned char DT_TILECACHE_WALKABLE_AREA = 63;
  26. static const unsigned short DT_TILECACHE_NULL_IDX = 0xffff;
  27. struct dtTileCacheLayerHeader
  28. {
  29. int magic; ///< Data magic
  30. int version; ///< Data version
  31. int tx,ty,tlayer;
  32. float bmin[3], bmax[3];
  33. unsigned short hmin, hmax; ///< Height min/max range
  34. unsigned char width, height; ///< Dimension of the layer.
  35. unsigned char minx, maxx, miny, maxy; ///< Usable sub-region.
  36. };
  37. struct dtTileCacheLayer
  38. {
  39. dtTileCacheLayerHeader* header;
  40. unsigned char regCount; ///< Region count.
  41. unsigned char* heights;
  42. unsigned char* areas;
  43. unsigned char* cons;
  44. unsigned char* regs;
  45. };
  46. struct dtTileCacheContour
  47. {
  48. int nverts;
  49. unsigned char* verts;
  50. unsigned char reg;
  51. unsigned char area;
  52. };
  53. struct dtTileCacheContourSet
  54. {
  55. int nconts;
  56. dtTileCacheContour* conts;
  57. };
  58. struct dtTileCachePolyMesh
  59. {
  60. int nvp;
  61. int nverts; ///< Number of vertices.
  62. int npolys; ///< Number of polygons.
  63. unsigned short* verts; ///< Vertices of the mesh, 3 elements per vertex.
  64. unsigned short* polys; ///< Polygons of the mesh, nvp*2 elements per polygon.
  65. unsigned short* flags; ///< Per polygon flags.
  66. unsigned char* areas; ///< Area ID of polygons.
  67. };
  68. struct dtTileCacheAlloc
  69. {
  70. virtual ~dtTileCacheAlloc() {}
  71. virtual void reset() {}
  72. virtual void* alloc(const size_t size)
  73. {
  74. return dtAlloc(size, DT_ALLOC_TEMP);
  75. }
  76. virtual void free(void* ptr)
  77. {
  78. dtFree(ptr);
  79. }
  80. };
  81. struct dtTileCacheCompressor
  82. {
  83. virtual ~dtTileCacheCompressor() { }
  84. virtual int maxCompressedSize(const int bufferSize) = 0;
  85. virtual dtStatus compress(const unsigned char* buffer, const int bufferSize,
  86. unsigned char* compressed, const int maxCompressedSize, int* compressedSize) = 0;
  87. virtual dtStatus decompress(const unsigned char* compressed, const int compressedSize,
  88. unsigned char* buffer, const int maxBufferSize, int* bufferSize) = 0;
  89. };
  90. dtStatus dtBuildTileCacheLayer(dtTileCacheCompressor* comp,
  91. dtTileCacheLayerHeader* header,
  92. const unsigned char* heights,
  93. const unsigned char* areas,
  94. const unsigned char* cons,
  95. unsigned char** outData, int* outDataSize);
  96. void dtFreeTileCacheLayer(dtTileCacheAlloc* alloc, dtTileCacheLayer* layer);
  97. dtStatus dtDecompressTileCacheLayer(dtTileCacheAlloc* alloc, dtTileCacheCompressor* comp,
  98. unsigned char* compressed, const int compressedSize,
  99. dtTileCacheLayer** layerOut);
  100. dtTileCacheContourSet* dtAllocTileCacheContourSet(dtTileCacheAlloc* alloc);
  101. void dtFreeTileCacheContourSet(dtTileCacheAlloc* alloc, dtTileCacheContourSet* cset);
  102. dtTileCachePolyMesh* dtAllocTileCachePolyMesh(dtTileCacheAlloc* alloc);
  103. void dtFreeTileCachePolyMesh(dtTileCacheAlloc* alloc, dtTileCachePolyMesh* lmesh);
  104. dtStatus dtMarkCylinderArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
  105. const float* pos, const float radius, const float height, const unsigned char areaId);
  106. dtStatus dtMarkBoxArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
  107. const float* bmin, const float* bmax, const unsigned char areaId);
  108. dtStatus dtMarkBoxArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
  109. const float* center, const float* halfExtents, const float* rotAux, const unsigned char areaId);
  110. dtStatus dtBuildTileCacheRegions(dtTileCacheAlloc* alloc,
  111. dtTileCacheLayer& layer,
  112. const int walkableClimb);
  113. dtStatus dtBuildTileCacheContours(dtTileCacheAlloc* alloc,
  114. dtTileCacheLayer& layer,
  115. const int walkableClimb, const float maxError,
  116. dtTileCacheContourSet& lcset);
  117. dtStatus dtBuildTileCachePolyMesh(dtTileCacheAlloc* alloc,
  118. dtTileCacheContourSet& lcset,
  119. dtTileCachePolyMesh& mesh);
  120. /// Swaps the endianess of the compressed tile data's header (#dtTileCacheLayerHeader).
  121. /// Tile layer data does not need endian swapping as it consits only of bytes.
  122. /// @param[in,out] data The tile data array.
  123. /// @param[in] dataSize The size of the data array.
  124. bool dtTileCacheHeaderSwapEndian(unsigned char* data, const int dataSize);
  125. #endif // DETOURTILECACHEBUILDER_H