DetourTileCacheBuilder.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef DETOURTILECACHEBUILDER_H
  19. #define DETOURTILECACHEBUILDER_H
  20. // ATOMIC BEGIN
  21. #include "../../Detour/include/DetourAlloc.h"
  22. #include "../../Detour/include/DetourStatus.h"
  23. // ATOMIC END
  24. static const int DT_TILECACHE_MAGIC = 'D'<<24 | 'T'<<16 | 'L'<<8 | 'R'; ///< 'DTLR';
  25. static const int DT_TILECACHE_VERSION = 1;
  26. static const unsigned char DT_TILECACHE_NULL_AREA = 0;
  27. static const unsigned char DT_TILECACHE_WALKABLE_AREA = 63;
  28. static const unsigned short DT_TILECACHE_NULL_IDX = 0xffff;
  29. struct dtTileCacheLayerHeader
  30. {
  31. int magic; ///< Data magic
  32. int version; ///< Data version
  33. int tx,ty,tlayer;
  34. float bmin[3], bmax[3];
  35. unsigned short hmin, hmax; ///< Height min/max range
  36. unsigned char width, height; ///< Dimension of the layer.
  37. unsigned char minx, maxx, miny, maxy; ///< Usable sub-region.
  38. };
  39. struct dtTileCacheLayer
  40. {
  41. dtTileCacheLayerHeader* header;
  42. unsigned char regCount; ///< Region count.
  43. unsigned char* heights;
  44. unsigned char* areas;
  45. unsigned char* cons;
  46. unsigned char* regs;
  47. };
  48. struct dtTileCacheContour
  49. {
  50. int nverts;
  51. unsigned char* verts;
  52. unsigned char reg;
  53. unsigned char area;
  54. };
  55. struct dtTileCacheContourSet
  56. {
  57. int nconts;
  58. dtTileCacheContour* conts;
  59. };
  60. struct dtTileCachePolyMesh
  61. {
  62. int nvp;
  63. int nverts; ///< Number of vertices.
  64. int npolys; ///< Number of polygons.
  65. unsigned short* verts; ///< Vertices of the mesh, 3 elements per vertex.
  66. unsigned short* polys; ///< Polygons of the mesh, nvp*2 elements per polygon.
  67. unsigned short* flags; ///< Per polygon flags.
  68. unsigned char* areas; ///< Area ID of polygons.
  69. };
  70. struct dtTileCacheAlloc
  71. {
  72. virtual ~dtTileCacheAlloc() { }
  73. virtual void reset()
  74. {
  75. }
  76. virtual void* alloc(const int size)
  77. {
  78. return dtAlloc(size, DT_ALLOC_TEMP);
  79. }
  80. virtual void free(void* ptr)
  81. {
  82. dtFree(ptr);
  83. }
  84. };
  85. struct dtTileCacheCompressor
  86. {
  87. virtual ~dtTileCacheCompressor() { }
  88. virtual int maxCompressedSize(const int bufferSize) = 0;
  89. virtual dtStatus compress(const unsigned char* buffer, const int bufferSize,
  90. unsigned char* compressed, const int maxCompressedSize, int* compressedSize) = 0;
  91. virtual dtStatus decompress(const unsigned char* compressed, const int compressedSize,
  92. unsigned char* buffer, const int maxBufferSize, int* bufferSize) = 0;
  93. };
  94. dtStatus dtBuildTileCacheLayer(dtTileCacheCompressor* comp,
  95. dtTileCacheLayerHeader* header,
  96. const unsigned char* heights,
  97. const unsigned char* areas,
  98. const unsigned char* cons,
  99. unsigned char** outData, int* outDataSize);
  100. void dtFreeTileCacheLayer(dtTileCacheAlloc* alloc, dtTileCacheLayer* layer);
  101. dtStatus dtDecompressTileCacheLayer(dtTileCacheAlloc* alloc, dtTileCacheCompressor* comp,
  102. unsigned char* compressed, const int compressedSize,
  103. dtTileCacheLayer** layerOut);
  104. dtTileCacheContourSet* dtAllocTileCacheContourSet(dtTileCacheAlloc* alloc);
  105. void dtFreeTileCacheContourSet(dtTileCacheAlloc* alloc, dtTileCacheContourSet* cset);
  106. dtTileCachePolyMesh* dtAllocTileCachePolyMesh(dtTileCacheAlloc* alloc);
  107. void dtFreeTileCachePolyMesh(dtTileCacheAlloc* alloc, dtTileCachePolyMesh* lmesh);
  108. dtStatus dtMarkCylinderArea(dtTileCacheLayer& layer, const float* orig, const float cs, const float ch,
  109. const float* pos, const float radius, const float height, 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