CCNavMeshDebugDraw.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "navmesh/CCNavMeshDebugDraw.h"
  22. #if CC_USE_NAVMESH
  23. #include "renderer/CCGLProgramCache.h"
  24. #include "renderer/ccGLStateCache.h"
  25. #include "renderer/CCRenderer.h"
  26. #include "renderer/CCRenderState.h"
  27. #include "base/CCDirector.h"
  28. #include "base/ccMacros.h"
  29. NS_CC_BEGIN
  30. NavMeshDebugDraw::NavMeshDebugDraw()
  31. : _currentPrimitive(nullptr)
  32. , _primitiveType(GL_POINTS)
  33. , _currentDepthMask(true)
  34. , _dirtyBuffer(true)
  35. {
  36. _stateBlock = RenderState::StateBlock::create();
  37. _stateBlock->setCullFace(true);
  38. _stateBlock->setCullFaceSide(RenderState::CullFaceSide::CULL_FACE_SIDE_BACK);
  39. _stateBlock->setDepthTest(true);
  40. _stateBlock->setBlend(true);
  41. _stateBlock->setBlendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED);
  42. CC_SAFE_RETAIN(_stateBlock);
  43. _customCmd.set3D(true);
  44. _customCmd.setTransparent(true);
  45. _program = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR);
  46. glGenBuffers(1, &_vbo);
  47. }
  48. void NavMeshDebugDraw::vertex(const float /*x*/, const float /*y*/, const float /*z*/, unsigned int /*color*/, const float /*u*/, const float /*v*/)
  49. {
  50. }
  51. void NavMeshDebugDraw::vertex(const float* pos, unsigned int color, const float* uv)
  52. {
  53. vertex(pos[0], pos[1], pos[2], color, uv[0], uv[1]);
  54. }
  55. void NavMeshDebugDraw::vertex(const float x, const float y, const float z, unsigned int color)
  56. {
  57. if (!_currentPrimitive) return;
  58. V3F_C4F vertex = { Vec3(x, y, z), getColor(color) };
  59. _vertices.push_back(vertex);
  60. _dirtyBuffer = true;
  61. }
  62. void NavMeshDebugDraw::vertex(const float* pos, unsigned int color)
  63. {
  64. vertex(pos[0], pos[1], pos[2], color);
  65. }
  66. NavMeshDebugDraw::~NavMeshDebugDraw()
  67. {
  68. CC_SAFE_RELEASE(_stateBlock);
  69. for (auto iter : _primitiveList){
  70. delete iter;
  71. }
  72. glDeleteBuffers(1, &_vbo);
  73. }
  74. void NavMeshDebugDraw::depthMask(bool state)
  75. {
  76. _currentDepthMask = state;
  77. }
  78. void NavMeshDebugDraw::begin(duDebugDrawPrimitives prim, float size /*= 1.0f*/)
  79. {
  80. if (_currentPrimitive) return;
  81. _currentPrimitive = new (std::nothrow) Primitive;
  82. _currentPrimitive->type = getPrimitiveType(prim);
  83. _currentPrimitive->depthMask = _currentDepthMask;
  84. _currentPrimitive->start = _vertices.size();
  85. _currentPrimitive->size = size;
  86. }
  87. void NavMeshDebugDraw::end()
  88. {
  89. if (!_currentPrimitive) return;
  90. _currentPrimitive->end = _vertices.size();
  91. _primitiveList.push_back(_currentPrimitive);
  92. _currentPrimitive = nullptr;
  93. }
  94. Vec4 NavMeshDebugDraw::getColor(unsigned int col)
  95. {
  96. const unsigned int r = col & 0xff;
  97. const unsigned int g = (col >> 8) & 0xff;
  98. const unsigned int b = (col >> 16) & 0xff;
  99. const unsigned int a = (col >> 24) & 0xff;
  100. float factor = 1.0f / 255.0f;
  101. return Vec4(r, g, b, a) * factor;
  102. }
  103. GLenum NavMeshDebugDraw::getPrimitiveType(duDebugDrawPrimitives prim)
  104. {
  105. switch (prim)
  106. {
  107. case DU_DRAW_POINTS:
  108. return GL_POINTS;
  109. case DU_DRAW_LINES:
  110. return GL_LINES;
  111. case DU_DRAW_TRIS:
  112. return GL_TRIANGLES;
  113. default:
  114. return GL_POINTS;
  115. }
  116. }
  117. void NavMeshDebugDraw::drawImplement(const cocos2d::Mat4& transform, uint32_t /*flags*/)
  118. {
  119. _program->use();
  120. _program->setUniformsForBuiltins(transform);
  121. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  122. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR);
  123. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4F), (GLvoid *)offsetof(V3F_C4F, position));
  124. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(V3F_C4F), (GLvoid *)offsetof(V3F_C4F, color));
  125. if (_dirtyBuffer){
  126. glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4F)* _vertices.size(), &_vertices[0], GL_STATIC_DRAW);
  127. _dirtyBuffer = false;
  128. }
  129. for (auto &iter : _primitiveList){
  130. if (iter->type == GL_POINTS)
  131. continue;
  132. _stateBlock->setDepthWrite(iter->depthMask);
  133. if (iter->type == GL_LINES){
  134. glLineWidth(iter->size);
  135. }
  136. _stateBlock->bind();
  137. glDrawArrays(iter->type, iter->start, iter->end - iter->start);
  138. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, iter->end - iter->start);
  139. }
  140. glBindBuffer(GL_ARRAY_BUFFER, 0);
  141. }
  142. void NavMeshDebugDraw::draw(Renderer* renderer)
  143. {
  144. _customCmd.init(0, Mat4::IDENTITY, Node::FLAGS_RENDER_AS_3D);
  145. _customCmd.func = CC_CALLBACK_0(NavMeshDebugDraw::drawImplement, this, Mat4::IDENTITY, 0);
  146. renderer->addCommand(&_customCmd);
  147. }
  148. void NavMeshDebugDraw::clear()
  149. {
  150. _vertices.clear();
  151. for (auto iter : _primitiveList){
  152. delete iter;
  153. }
  154. _primitiveList.clear();
  155. }
  156. NS_CC_END
  157. #endif //CC_USE_NAVMESH