OffMeshConnectionTool.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #define _USE_MATH_DEFINES
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <float.h>
  23. #include "SDL.h"
  24. #include "SDL_opengl.h"
  25. #ifdef __APPLE__
  26. # include <OpenGL/glu.h>
  27. #else
  28. # include <GL/glu.h>
  29. #endif
  30. #include "imgui.h"
  31. #include "OffMeshConnectionTool.h"
  32. #include "InputGeom.h"
  33. #include "Sample.h"
  34. #include "Recast.h"
  35. #include "RecastDebugDraw.h"
  36. #include "DetourDebugDraw.h"
  37. #ifdef WIN32
  38. # define snprintf _snprintf
  39. #endif
  40. OffMeshConnectionTool::OffMeshConnectionTool() :
  41. m_sample(0),
  42. m_hitPosSet(0),
  43. m_bidir(true),
  44. m_oldFlags(0)
  45. {
  46. }
  47. OffMeshConnectionTool::~OffMeshConnectionTool()
  48. {
  49. if (m_sample)
  50. {
  51. m_sample->setNavMeshDrawFlags(m_oldFlags);
  52. }
  53. }
  54. void OffMeshConnectionTool::init(Sample* sample)
  55. {
  56. if (m_sample != sample)
  57. {
  58. m_sample = sample;
  59. m_oldFlags = m_sample->getNavMeshDrawFlags();
  60. m_sample->setNavMeshDrawFlags(m_oldFlags & ~DU_DRAWNAVMESH_OFFMESHCONS);
  61. }
  62. }
  63. void OffMeshConnectionTool::reset()
  64. {
  65. m_hitPosSet = false;
  66. }
  67. void OffMeshConnectionTool::handleMenu()
  68. {
  69. if (imguiCheck("One Way", !m_bidir))
  70. m_bidir = false;
  71. if (imguiCheck("Bidirectional", m_bidir))
  72. m_bidir = true;
  73. }
  74. void OffMeshConnectionTool::handleClick(const float* /*s*/, const float* p, bool shift)
  75. {
  76. if (!m_sample) return;
  77. InputGeom* geom = m_sample->getInputGeom();
  78. if (!geom) return;
  79. if (shift)
  80. {
  81. // Delete
  82. // Find nearest link end-point
  83. float nearestDist = FLT_MAX;
  84. int nearestIndex = -1;
  85. const float* verts = geom->getOffMeshConnectionVerts();
  86. for (int i = 0; i < geom->getOffMeshConnectionCount()*2; ++i)
  87. {
  88. const float* v = &verts[i*3];
  89. float d = rcVdistSqr(p, v);
  90. if (d < nearestDist)
  91. {
  92. nearestDist = d;
  93. nearestIndex = i/2; // Each link has two vertices.
  94. }
  95. }
  96. // If end point close enough, delete it.
  97. if (nearestIndex != -1 &&
  98. sqrtf(nearestDist) < m_sample->getAgentRadius())
  99. {
  100. geom->deleteOffMeshConnection(nearestIndex);
  101. }
  102. }
  103. else
  104. {
  105. // Create
  106. if (!m_hitPosSet)
  107. {
  108. rcVcopy(m_hitPos, p);
  109. m_hitPosSet = true;
  110. }
  111. else
  112. {
  113. const unsigned char area = SAMPLE_POLYAREA_JUMP;
  114. const unsigned short flags = SAMPLE_POLYFLAGS_JUMP;
  115. geom->addOffMeshConnection(m_hitPos, p, m_sample->getAgentRadius(), m_bidir ? 1 : 0, area, flags);
  116. m_hitPosSet = false;
  117. }
  118. }
  119. }
  120. void OffMeshConnectionTool::handleToggle()
  121. {
  122. }
  123. void OffMeshConnectionTool::handleStep()
  124. {
  125. }
  126. void OffMeshConnectionTool::handleUpdate(const float /*dt*/)
  127. {
  128. }
  129. void OffMeshConnectionTool::handleRender()
  130. {
  131. duDebugDraw& dd = m_sample->getDebugDraw();
  132. const float s = m_sample->getAgentRadius();
  133. if (m_hitPosSet)
  134. duDebugDrawCross(&dd, m_hitPos[0],m_hitPos[1]+0.1f,m_hitPos[2], s, duRGBA(0,0,0,128), 2.0f);
  135. InputGeom* geom = m_sample->getInputGeom();
  136. if (geom)
  137. geom->drawOffMeshConnections(&dd, true);
  138. }
  139. void OffMeshConnectionTool::handleRenderOverlay(double* proj, double* model, int* view)
  140. {
  141. GLdouble x, y, z;
  142. // Draw start and end point labels
  143. if (m_hitPosSet && gluProject((GLdouble)m_hitPos[0], (GLdouble)m_hitPos[1], (GLdouble)m_hitPos[2],
  144. model, proj, view, &x, &y, &z))
  145. {
  146. imguiDrawText((int)x, (int)(y-25), IMGUI_ALIGN_CENTER, "Start", imguiRGBA(0,0,0,220));
  147. }
  148. // Tool help
  149. const int h = view[3];
  150. if (!m_hitPosSet)
  151. {
  152. imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "LMB: Create new connection. SHIFT+LMB: Delete existing connection, click close to start or end point.", imguiRGBA(255,255,255,192));
  153. }
  154. else
  155. {
  156. imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "LMB: Set connection end point and finish.", imguiRGBA(255,255,255,192));
  157. }
  158. }