Dbg.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "anki/renderer/Dbg.h"
  2. #include "anki/renderer/Renderer.h"
  3. #include "anki/resource/ShaderProgramResource.h"
  4. #include "anki/scene/SceneGraph.h"
  5. #include "anki/scene/Light.h"
  6. #include "anki/core/Logger.h"
  7. namespace anki {
  8. //==============================================================================
  9. Dbg::~Dbg()
  10. {}
  11. //==============================================================================
  12. void Dbg::init(const Renderer::Initializer& initializer)
  13. {
  14. enabled = initializer.dbg.enabled;
  15. enableBits(DF_ALL);
  16. try
  17. {
  18. fbo.create();
  19. fbo.setColorAttachments({&r->getPps().getFai()});
  20. fbo.setOtherAttachment(GL_DEPTH_ATTACHMENT, r->getMs().getDepthFai());
  21. if(!fbo.isComplete())
  22. {
  23. throw ANKI_EXCEPTION("FBO is incomplete");
  24. }
  25. }
  26. catch(std::exception& e)
  27. {
  28. throw ANKI_EXCEPTION("Cannot create debug FBO") << e;
  29. }
  30. drawer.reset(new DebugDrawer);
  31. sceneDrawer.reset(new SceneDebugDrawer(drawer.get()));
  32. }
  33. //==============================================================================
  34. static Bool isLeft(const Vec2& p0, const Vec2& p1, const Vec2& p2)
  35. {
  36. return (p1.x() - p0.x()) * (p2.y() - p0.y())
  37. > (p2.x() - p0.x()) * (p1.y() - p0.y());
  38. }
  39. void calcConvexHull2D(const Vec2* ANKI_RESTRICT points,
  40. const U32 n, Vec2* ANKI_RESTRICT opoints, U32& on)
  41. {
  42. const U maxPoints = 9;
  43. ANKI_ASSERT(n <= maxPoints && n > 3);
  44. Array<Vec2, maxPoints * 2 + 1> deque;
  45. // initial bottom and top deque indices
  46. U bot = n - 2, top = bot + 3;
  47. // 3rd vertex is at both bot and top
  48. deque[bot] = deque[top] = points[2];
  49. if(isLeft(points[0], points[1], points[2]))
  50. {
  51. deque[bot + 1] = points[0];
  52. deque[bot + 2] = points[1]; // ccw vertices are: 2, 0, 1, 2
  53. }
  54. else
  55. {
  56. deque[bot + 1] = points[1];
  57. deque[bot + 2] = points[0]; // ccw vertices are: 2, 1, 0, 2
  58. }
  59. // compute the hull on the deque D[]
  60. for(U i = 3; i < n; i++)
  61. {
  62. // test if next vertex is inside the deque hull
  63. if ((isLeft(deque[bot], deque[bot + 1], points[i]))
  64. && (isLeft(deque[top - 1], deque[top], points[i])))
  65. {
  66. continue; // skip an interior vertex
  67. }
  68. while(!isLeft(deque[top - 1], deque[top], points[i]))
  69. {
  70. --top;
  71. }
  72. ++top;
  73. deque[top] = points[i];
  74. while(!isLeft(deque[bot], deque[bot + 1], points[i]))
  75. {
  76. ++bot;
  77. }
  78. --bot;
  79. deque[bot] = points[i];
  80. }
  81. // transcribe deque to the output hull array opoints
  82. U h; // hull vertex counter
  83. for(h = 0; h < (top - bot); h++)
  84. {
  85. opoints[h] = deque[bot + h];
  86. }
  87. on = h;
  88. }
  89. struct ShortLexicographicallyFunctor
  90. {
  91. Bool operator()(const Vec2& a, const Vec2& b)
  92. {
  93. return a.x() < b.x() || (a.x() == b.x() && a.y() < b.y());
  94. }
  95. };
  96. F32 cross(const Vec2& o, const Vec2& a, const Vec2& b)
  97. {
  98. return (a.x() - o.x()) * (b.y() - o.y())
  99. - (a.y() - o.y()) * (b.x() - o.x());
  100. }
  101. void calcConvexHull2D_(Vec2* ANKI_RESTRICT p,
  102. const U32 n, Vec2* ANKI_RESTRICT o, U32& outn)
  103. {
  104. I k = 0;
  105. std::sort(&p[0], &p[n], ShortLexicographicallyFunctor());
  106. // Build lower hull
  107. for(I i = 0; i < n; i++)
  108. {
  109. while(k >= 2 && !isLeft(o[k - 2], o[k - 1], p[i]))
  110. {
  111. --k;
  112. }
  113. o[k++] = p[i];
  114. }
  115. // Build upper hull
  116. for(I i = n - 2, t = k + 1; i >= 0; i--)
  117. {
  118. while(k >= t && !isLeft(o[k - 2], o[k - 1], p[i]))
  119. {
  120. --k;
  121. }
  122. o[k++] = p[i];
  123. }
  124. outn = k;
  125. }
  126. void Dbg::run()
  127. {
  128. ANKI_ASSERT(enabled);
  129. fbo.bind();
  130. GlStateSingleton::get().disable(GL_BLEND);
  131. GlStateSingleton::get().enable(GL_DEPTH_TEST, depthTest);
  132. drawer->setViewProjectionMatrix(r->getViewProjectionMatrix());
  133. drawer->setModelMatrix(Mat4::getIdentity());
  134. //drawer->drawGrid();
  135. #if 0
  136. SceneGraph& scene = r->getSceneGraph();
  137. for(auto it = scene.getSceneNodesBegin();
  138. it != scene.getSceneNodesEnd(); it++)
  139. {
  140. SceneNode* node = *it;
  141. Spatial* sp = node->getSpatial();
  142. if(flagsEnabled(DF_SPATIAL) && sp)
  143. {
  144. sceneDrawer->draw(*node);
  145. }
  146. }
  147. // Draw sectors
  148. for(const Sector* sector : scene.getSectorGroup().getSectors())
  149. {
  150. //if(sector->isVisible())
  151. {
  152. if(flagsEnabled(DF_SECTOR))
  153. {
  154. sceneDrawer->draw(*sector);
  155. }
  156. if(flagsEnabled(DF_OCTREE))
  157. {
  158. sceneDrawer->draw(sector->getOctree());
  159. }
  160. }
  161. }
  162. // Physics
  163. if(flagsEnabled(DF_PHYSICS))
  164. {
  165. scene.getPhysics().debugDraw();
  166. }
  167. #endif
  168. {
  169. drawer->setColor(Vec3(0.1, 0.1, 0.1));
  170. Aabb box(Vec3(-1.0, 1.0, -1.0), Vec3(1.5, 2.0, 1.5));
  171. CollisionDebugDrawer coldrawer(drawer.get());
  172. box.accept(coldrawer);
  173. drawer->setColor(Vec3(1.0));
  174. const Vec3& min = box.getMin();
  175. const Vec3& max = box.getMax();
  176. Array<Vec4, 8> points = {{
  177. Vec4(max.x(), max.y(), max.z(), 1.0), // right top front
  178. Vec4(min.x(), max.y(), max.z(), 1.0), // left top front
  179. Vec4(min.x(), min.y(), max.z(), 1.0), // left bottom front
  180. Vec4(max.x(), min.y(), max.z(), 1.0), // right bottom front
  181. Vec4(max.x(), max.y(), min.z(), 1.0), // right top back
  182. Vec4(min.x(), max.y(), min.z(), 1.0), // left top back
  183. Vec4(min.x(), min.y(), min.z(), 1.0), // left bottom back
  184. Vec4(max.x(), min.y(), min.z(), 1.0)}}; // right bottom back
  185. drawer->setViewProjectionMatrix(Mat4::getIdentity());
  186. drawer->setModelMatrix(Mat4::getIdentity());
  187. const Mat4& mat = r->getViewProjectionMatrix();
  188. Array<Vec2, 8> points2D;
  189. Array<Vec2, 8 * 2> hullPoints2D;
  190. static const Array<Vec3, 9> colors = {{
  191. Vec3(1.0, 0.0, 0.0),
  192. Vec3(0.0, 1.0, 0.0),
  193. Vec3(0.0, 0.0, 1.0),
  194. Vec3(1.0, 0.0, 1.0),
  195. Vec3(1.0, 1.0, 0.0),
  196. Vec3(0.0, 1.0, 1.0),
  197. Vec3(1.0, 1.0, 1.0),
  198. Vec3(1.0, 0.5, 1.0),
  199. Vec3(1.0, 0.5, 0.5)
  200. }};
  201. drawer->begin();
  202. for(U i = 0; i < 8; i++)
  203. {
  204. Vec4& point = points[i];
  205. point = mat * point;
  206. point /= point.w();
  207. points2D[i] = point.xy();
  208. /*drawer->setColor(colors[i]);
  209. drawer->pushBackVertex(Vec3(point.x(), point.y(), -0.1));*/
  210. }
  211. drawer->end();
  212. U32 outPoints;
  213. calcConvexHull2D_(&points2D[0], 8, &hullPoints2D[0], outPoints);
  214. drawer->begin();
  215. for(U i = 0; i < outPoints - 1; i++)
  216. {
  217. drawer->setColor(colors[i]);
  218. drawer->pushBackVertex(Vec3(hullPoints2D[i], -0.1));
  219. drawer->setColor(colors[i] / 2.0);
  220. drawer->pushBackVertex(Vec3(hullPoints2D[i + 1], -0.1));
  221. }
  222. /*drawer->pushBackVertex(Vec3(hullPoints2D[outPoints - 1], -0.1));
  223. drawer->pushBackVertex(Vec3(hullPoints2D[0], -0.1));*/
  224. drawer->end();
  225. }
  226. drawer->flush();
  227. }
  228. } // end namespace anki