Sample.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 RECASTSAMPLE_H
  19. #define RECASTSAMPLE_H
  20. #include "Recast.h"
  21. #include "SampleInterfaces.h"
  22. /// Tool types.
  23. enum SampleToolType
  24. {
  25. TOOL_NONE = 0,
  26. TOOL_TILE_EDIT,
  27. TOOL_TILE_HIGHLIGHT,
  28. TOOL_TEMP_OBSTACLE,
  29. TOOL_NAVMESH_TESTER,
  30. TOOL_NAVMESH_PRUNE,
  31. TOOL_OFFMESH_CONNECTION,
  32. TOOL_CONVEX_VOLUME,
  33. TOOL_CROWD,
  34. MAX_TOOLS
  35. };
  36. /// These are just sample areas to use consistent values across the samples.
  37. /// The use should specify these base on his needs.
  38. enum SamplePolyAreas
  39. {
  40. SAMPLE_POLYAREA_GROUND,
  41. SAMPLE_POLYAREA_WATER,
  42. SAMPLE_POLYAREA_ROAD,
  43. SAMPLE_POLYAREA_DOOR,
  44. SAMPLE_POLYAREA_GRASS,
  45. SAMPLE_POLYAREA_JUMP,
  46. };
  47. enum SamplePolyFlags
  48. {
  49. SAMPLE_POLYFLAGS_WALK = 0x01, // Ability to walk (ground, grass, road)
  50. SAMPLE_POLYFLAGS_SWIM = 0x02, // Ability to swim (water).
  51. SAMPLE_POLYFLAGS_DOOR = 0x04, // Ability to move through doors.
  52. SAMPLE_POLYFLAGS_JUMP = 0x08, // Ability to jump.
  53. SAMPLE_POLYFLAGS_DISABLED = 0x10, // Disabled polygon
  54. SAMPLE_POLYFLAGS_ALL = 0xffff // All abilities.
  55. };
  56. class SampleDebugDraw : public DebugDrawGL
  57. {
  58. public:
  59. virtual unsigned int areaToCol(unsigned int area);
  60. };
  61. enum SamplePartitionType
  62. {
  63. SAMPLE_PARTITION_WATERSHED,
  64. SAMPLE_PARTITION_MONOTONE,
  65. SAMPLE_PARTITION_LAYERS,
  66. };
  67. struct SampleTool
  68. {
  69. virtual ~SampleTool() {}
  70. virtual int type() = 0;
  71. virtual void init(class Sample* sample) = 0;
  72. virtual void reset() = 0;
  73. virtual void handleMenu() = 0;
  74. virtual void handleClick(const float* s, const float* p, bool shift) = 0;
  75. virtual void handleRender() = 0;
  76. virtual void handleRenderOverlay(double* proj, double* model, int* view) = 0;
  77. virtual void handleToggle() = 0;
  78. virtual void handleStep() = 0;
  79. virtual void handleUpdate(const float dt) = 0;
  80. };
  81. struct SampleToolState {
  82. virtual ~SampleToolState() {}
  83. virtual void init(class Sample* sample) = 0;
  84. virtual void reset() = 0;
  85. virtual void handleRender() = 0;
  86. virtual void handleRenderOverlay(double* proj, double* model, int* view) = 0;
  87. virtual void handleUpdate(const float dt) = 0;
  88. };
  89. class Sample
  90. {
  91. protected:
  92. class InputGeom* m_geom;
  93. class dtNavMesh* m_navMesh;
  94. class dtNavMeshQuery* m_navQuery;
  95. class dtCrowd* m_crowd;
  96. unsigned char m_navMeshDrawFlags;
  97. float m_cellSize;
  98. float m_cellHeight;
  99. float m_agentHeight;
  100. float m_agentRadius;
  101. float m_agentMaxClimb;
  102. float m_agentMaxSlope;
  103. float m_regionMinSize;
  104. float m_regionMergeSize;
  105. float m_edgeMaxLen;
  106. float m_edgeMaxError;
  107. float m_vertsPerPoly;
  108. float m_detailSampleDist;
  109. float m_detailSampleMaxError;
  110. int m_partitionType;
  111. bool m_filterLowHangingObstacles;
  112. bool m_filterLedgeSpans;
  113. bool m_filterWalkableLowHeightSpans;
  114. SampleTool* m_tool;
  115. SampleToolState* m_toolStates[MAX_TOOLS];
  116. BuildContext* m_ctx;
  117. SampleDebugDraw m_dd;
  118. dtNavMesh* loadAll(const char* path);
  119. void saveAll(const char* path, const dtNavMesh* mesh);
  120. public:
  121. Sample();
  122. virtual ~Sample();
  123. void setContext(BuildContext* ctx) { m_ctx = ctx; }
  124. void setTool(SampleTool* tool);
  125. SampleToolState* getToolState(int type) { return m_toolStates[type]; }
  126. void setToolState(int type, SampleToolState* s) { m_toolStates[type] = s; }
  127. SampleDebugDraw& getDebugDraw() { return m_dd; }
  128. virtual void handleSettings();
  129. virtual void handleTools();
  130. virtual void handleDebugMode();
  131. virtual void handleClick(const float* s, const float* p, bool shift);
  132. virtual void handleToggle();
  133. virtual void handleStep();
  134. virtual void handleRender();
  135. virtual void handleRenderOverlay(double* proj, double* model, int* view);
  136. virtual void handleMeshChanged(class InputGeom* geom);
  137. virtual bool handleBuild();
  138. virtual void handleUpdate(const float dt);
  139. virtual void collectSettings(struct BuildSettings& settings);
  140. virtual class InputGeom* getInputGeom() { return m_geom; }
  141. virtual class dtNavMesh* getNavMesh() { return m_navMesh; }
  142. virtual class dtNavMeshQuery* getNavMeshQuery() { return m_navQuery; }
  143. virtual class dtCrowd* getCrowd() { return m_crowd; }
  144. virtual float getAgentRadius() { return m_agentRadius; }
  145. virtual float getAgentHeight() { return m_agentHeight; }
  146. virtual float getAgentClimb() { return m_agentMaxClimb; }
  147. unsigned char getNavMeshDrawFlags() const { return m_navMeshDrawFlags; }
  148. void setNavMeshDrawFlags(unsigned char flags) { m_navMeshDrawFlags = flags; }
  149. void updateToolStates(const float dt);
  150. void initToolStates(Sample* sample);
  151. void resetToolStates();
  152. void renderToolStates();
  153. void renderOverlayToolStates(double* proj, double* model, int* view);
  154. void resetCommonSettings();
  155. void handleCommonSettings();
  156. private:
  157. // Explicitly disabled copy constructor and copy assignment operator.
  158. Sample(const Sample&);
  159. Sample& operator=(const Sample&);
  160. };
  161. #endif // RECASTSAMPLE_H