debugdraw.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include "debugdraw.h"
  7. #include <bx/fpumath.h>
  8. #include <bx/radixsort.h>
  9. #include <bx/uint32_t.h>
  10. #include <bx/crtimpl.h>
  11. struct DebugVertex
  12. {
  13. float m_x;
  14. float m_y;
  15. float m_z;
  16. float m_len;
  17. uint32_t m_abgr;
  18. static void init()
  19. {
  20. ms_decl
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::TexCoord0, 1, bgfx::AttribType::Float)
  24. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  25. .end();
  26. }
  27. static bgfx::VertexDecl ms_decl;
  28. };
  29. bgfx::VertexDecl DebugVertex::ms_decl;
  30. struct DebugShapeVertex
  31. {
  32. float m_x;
  33. float m_y;
  34. float m_z;
  35. float m_mask;
  36. static void init()
  37. {
  38. ms_decl
  39. .begin()
  40. .add(bgfx::Attrib::Position, 4, bgfx::AttribType::Float)
  41. .end();
  42. }
  43. static bgfx::VertexDecl ms_decl;
  44. };
  45. bgfx::VertexDecl DebugShapeVertex::ms_decl;
  46. static DebugShapeVertex s_cubeVertices[8] =
  47. {
  48. {-1.0f, 1.0f, 1.0f, 0.0f },
  49. { 1.0f, 1.0f, 1.0f, 0.0f },
  50. {-1.0f, -1.0f, 1.0f, 0.0f },
  51. { 1.0f, -1.0f, 1.0f, 0.0f },
  52. {-1.0f, 1.0f, -1.0f, 0.0f },
  53. { 1.0f, 1.0f, -1.0f, 0.0f },
  54. {-1.0f, -1.0f, -1.0f, 0.0f },
  55. { 1.0f, -1.0f, -1.0f, 0.0f },
  56. };
  57. static const uint16_t s_cubeIndices[36] =
  58. {
  59. 0, 1, 2, // 0
  60. 1, 3, 2,
  61. 4, 6, 5, // 2
  62. 5, 6, 7,
  63. 0, 2, 4, // 4
  64. 4, 2, 6,
  65. 1, 5, 3, // 6
  66. 5, 7, 3,
  67. 0, 4, 1, // 8
  68. 4, 5, 1,
  69. 2, 3, 6, // 10
  70. 6, 3, 7,
  71. };
  72. static const uint8_t s_circleLod[] =
  73. {
  74. 37,
  75. 29,
  76. 23,
  77. 17,
  78. 11,
  79. };
  80. static uint8_t getCircleLod(uint8_t _lod)
  81. {
  82. _lod = _lod > BX_COUNTOF(s_circleLod)-1 ? BX_COUNTOF(s_circleLod)-1 : _lod;
  83. return s_circleLod[_lod];
  84. }
  85. uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = 0, void* _normals0 = NULL, uint16_t _normalStride0 = 0)
  86. {
  87. if (NULL != _pos0)
  88. {
  89. struct Gen
  90. {
  91. Gen(void* _pos, uint16_t _posStride, void* _normals, uint16_t _normalStride, uint8_t _subdiv)
  92. : m_pos( (uint8_t*)_pos)
  93. , m_normals( (uint8_t*)_normals)
  94. , m_posStride(_posStride)
  95. , m_normalStride(_normalStride)
  96. {
  97. static const float scale = 1.0f;
  98. static const float golden = 1.6180339887f;
  99. static const float len = bx::fsqrt(golden*golden + 1.0f);
  100. static const float ss = 1.0f/len * scale;
  101. static const float ll = ss*golden;
  102. static const float vv[12][4] =
  103. {
  104. { -ll, 0.0f, -ss, 0.0f },
  105. { ll, 0.0f, -ss, 0.0f },
  106. { ll, 0.0f, ss, 0.0f },
  107. { -ll, 0.0f, ss, 0.0f },
  108. { -ss, ll, 0.0f, 0.0f },
  109. { ss, ll, 0.0f, 0.0f },
  110. { ss, -ll, 0.0f, 0.0f },
  111. { -ss, -ll, 0.0f, 0.0f },
  112. { 0.0f, -ss, ll, 0.0f },
  113. { 0.0f, ss, ll, 0.0f },
  114. { 0.0f, ss, -ll, 0.0f },
  115. { 0.0f, -ss, -ll, 0.0f },
  116. };
  117. m_numVertices = 0;
  118. triangle(vv[ 0], vv[ 4], vv[ 3], scale, _subdiv);
  119. triangle(vv[ 0], vv[10], vv[ 4], scale, _subdiv);
  120. triangle(vv[ 4], vv[10], vv[ 5], scale, _subdiv);
  121. triangle(vv[ 5], vv[10], vv[ 1], scale, _subdiv);
  122. triangle(vv[ 5], vv[ 1], vv[ 2], scale, _subdiv);
  123. triangle(vv[ 5], vv[ 2], vv[ 9], scale, _subdiv);
  124. triangle(vv[ 5], vv[ 9], vv[ 4], scale, _subdiv);
  125. triangle(vv[ 3], vv[ 4], vv[ 9], scale, _subdiv);
  126. triangle(vv[ 0], vv[ 3], vv[ 7], scale, _subdiv);
  127. triangle(vv[ 0], vv[ 7], vv[11], scale, _subdiv);
  128. triangle(vv[11], vv[ 7], vv[ 6], scale, _subdiv);
  129. triangle(vv[11], vv[ 6], vv[ 1], scale, _subdiv);
  130. triangle(vv[ 1], vv[ 6], vv[ 2], scale, _subdiv);
  131. triangle(vv[ 2], vv[ 6], vv[ 8], scale, _subdiv);
  132. triangle(vv[ 8], vv[ 6], vv[ 7], scale, _subdiv);
  133. triangle(vv[ 8], vv[ 7], vv[ 3], scale, _subdiv);
  134. triangle(vv[ 0], vv[11], vv[10], scale, _subdiv);
  135. triangle(vv[ 1], vv[10], vv[11], scale, _subdiv);
  136. triangle(vv[ 2], vv[ 8], vv[ 9], scale, _subdiv);
  137. triangle(vv[ 3], vv[ 9], vv[ 8], scale, _subdiv);
  138. }
  139. void addVert(const float* _v)
  140. {
  141. float* verts = (float*)m_pos;
  142. verts[0] = _v[0];
  143. verts[1] = _v[1];
  144. verts[2] = _v[2];
  145. m_pos += m_posStride;
  146. if (NULL != m_normals)
  147. {
  148. float* normals = (float*)m_normals;
  149. bx::vec3Norm(normals, _v);
  150. m_normals += m_normalStride;
  151. }
  152. m_numVertices++;
  153. }
  154. void triangle(const float* _v0, const float* _v1, const float* _v2, float _scale, uint8_t _subdiv)
  155. {
  156. if (0 == _subdiv)
  157. {
  158. addVert(_v0);
  159. addVert(_v1);
  160. addVert(_v2);
  161. }
  162. else
  163. {
  164. float tmp0[4];
  165. float tmp1[4];
  166. float v01[4];
  167. bx::vec3Add(tmp0, _v0, _v1);
  168. bx::vec3Norm(tmp1, tmp0);
  169. bx::vec3Mul(v01, tmp1, _scale);
  170. float v12[4];
  171. bx::vec3Add(tmp0, _v1, _v2);
  172. bx::vec3Norm(tmp1, tmp0);
  173. bx::vec3Mul(v12, tmp1, _scale);
  174. float v20[4];
  175. bx::vec3Add(tmp0, _v2, _v0);
  176. bx::vec3Norm(tmp1, tmp0);
  177. bx::vec3Mul(v20, tmp1, _scale);
  178. --_subdiv;
  179. triangle(_v0, v01, v20, _scale, _subdiv);
  180. triangle(_v1, v12, v01, _scale, _subdiv);
  181. triangle(_v2, v20, v12, _scale, _subdiv);
  182. triangle(v01, v12, v20, _scale, _subdiv);
  183. }
  184. }
  185. uint8_t* m_pos;
  186. uint8_t* m_normals;
  187. uint16_t m_posStride;
  188. uint16_t m_normalStride;
  189. uint32_t m_numVertices;
  190. } gen(_pos0, _posStride0, _normals0, _normalStride0, _subdiv0);
  191. }
  192. uint32_t numVertices = 20*3*bx::uint32_max(1, (uint32_t)bx::fpow(4.0f, _subdiv0) );
  193. return numVertices;
  194. }
  195. void getPoint(float* _result, Axis::Enum _axis, float _x, float _y)
  196. {
  197. switch (_axis)
  198. {
  199. case Axis::X:
  200. _result[0] = 0.0f;
  201. _result[1] = _x;
  202. _result[2] = _y;
  203. break;
  204. case Axis::Y:
  205. _result[0] = _y;
  206. _result[1] = 0.0f;
  207. _result[2] = _x;
  208. break;
  209. default:
  210. _result[0] = _x;
  211. _result[1] = _y;
  212. _result[2] = 0.0f;
  213. break;
  214. }
  215. }
  216. #include "vs_debugdraw_lines.bin.h"
  217. #include "fs_debugdraw_lines.bin.h"
  218. #include "vs_debugdraw_lines_stipple.bin.h"
  219. #include "fs_debugdraw_lines_stipple.bin.h"
  220. #include "vs_debugdraw_fill.bin.h"
  221. #include "fs_debugdraw_fill.bin.h"
  222. #include "vs_debugdraw_fill_lit.bin.h"
  223. #include "fs_debugdraw_fill_lit.bin.h"
  224. struct EmbeddedShader
  225. {
  226. bgfx::RendererType::Enum type;
  227. const uint8_t* data;
  228. uint32_t size;
  229. };
  230. #define BGFX_DECLARE_SHADER_EMBEDDED(_name) \
  231. { \
  232. { bgfx::RendererType::Direct3D9, BX_CONCATENATE(_name, _dx9 ), sizeof(BX_CONCATENATE(_name, _dx9 ) ) }, \
  233. { bgfx::RendererType::Direct3D11, BX_CONCATENATE(_name, _dx11), sizeof(BX_CONCATENATE(_name, _dx11) ) }, \
  234. { bgfx::RendererType::Direct3D12, BX_CONCATENATE(_name, _dx11), sizeof(BX_CONCATENATE(_name, _dx11) ) }, \
  235. { bgfx::RendererType::OpenGL, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  236. { bgfx::RendererType::OpenGLES, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  237. { bgfx::RendererType::Vulkan, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  238. { bgfx::RendererType::Metal, BX_CONCATENATE(_name, _mtl ), sizeof(BX_CONCATENATE(_name, _mtl ) ) }, \
  239. { bgfx::RendererType::Count, NULL, 0 }, \
  240. }
  241. static const EmbeddedShader s_embeddedShaders[][8] =
  242. {
  243. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_lines),
  244. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_lines),
  245. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_lines_stipple),
  246. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_lines_stipple),
  247. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_fill),
  248. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_fill),
  249. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_fill_lit),
  250. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_fill_lit),
  251. };
  252. static bgfx::ShaderHandle createEmbeddedShader(bgfx::RendererType::Enum _type, uint32_t _index)
  253. {
  254. for (const EmbeddedShader* es = s_embeddedShaders[_index]; bgfx::RendererType::Count != es->type; ++es)
  255. {
  256. if (_type == es->type)
  257. {
  258. return bgfx::createShader(bgfx::makeRef(es->data, es->size) );
  259. }
  260. }
  261. bgfx::ShaderHandle handle = BGFX_INVALID_HANDLE;
  262. return handle;
  263. }
  264. struct DebugDraw
  265. {
  266. DebugDraw()
  267. : m_state(State::Count)
  268. {
  269. }
  270. void init(bx::AllocatorI* _allocator)
  271. {
  272. m_allocator = _allocator;
  273. #if BX_CONFIG_ALLOCATOR_CRT
  274. if (NULL == _allocator)
  275. {
  276. static bx::CrtAllocator allocator;
  277. m_allocator = &allocator;
  278. }
  279. #endif // BX_CONFIG_ALLOCATOR_CRT
  280. DebugVertex::init();
  281. DebugShapeVertex::init();
  282. bgfx::RendererType::Enum type = bgfx::getRendererType();
  283. m_program[Program::Lines] =
  284. bgfx::createProgram(createEmbeddedShader(type, 0)
  285. , createEmbeddedShader(type, 1)
  286. , true
  287. );
  288. m_program[Program::LinesStipple] =
  289. bgfx::createProgram(createEmbeddedShader(type, 2)
  290. , createEmbeddedShader(type, 3)
  291. , true
  292. );
  293. m_program[Program::Fill] =
  294. bgfx::createProgram(createEmbeddedShader(type, 4)
  295. , createEmbeddedShader(type, 5)
  296. , true
  297. );
  298. m_program[Program::FillLit] =
  299. bgfx::createProgram(createEmbeddedShader(type, 6)
  300. , createEmbeddedShader(type, 7)
  301. , true
  302. );
  303. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 4);
  304. void* vertices[Mesh::Count] = {};
  305. uint16_t* indices[Mesh::Count] = {};
  306. uint16_t stride = DebugShapeVertex::ms_decl.getStride();
  307. uint32_t startVertex = 0;
  308. uint32_t startIndex = 0;
  309. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  310. {
  311. Mesh::Enum id = Mesh::Enum(Mesh::Sphere0+mesh);
  312. const uint32_t tess = 3-mesh;
  313. const uint32_t numVertices = genSphere(tess);
  314. const uint32_t numIndices = numVertices;
  315. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  316. genSphere(tess, vertices[id], stride);
  317. uint16_t* trilist = (uint16_t*)BX_ALLOC(m_allocator, numIndices*sizeof(uint16_t) );
  318. for (uint32_t ii = 0; ii < numIndices; ++ii)
  319. {
  320. trilist[ii] = uint16_t(ii);
  321. }
  322. uint16_t numLineListIndices = bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  323. , NULL
  324. , 0
  325. , trilist
  326. , numIndices
  327. , false
  328. );
  329. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  330. uint16_t* indicesOut = indices[id];
  331. memcpy(indicesOut, trilist, numIndices*sizeof(uint16_t) );
  332. bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  333. , &indicesOut[numIndices]
  334. , numLineListIndices*sizeof(uint16_t)
  335. , trilist
  336. , numIndices
  337. , false
  338. );
  339. m_mesh[id].m_startVertex = startVertex;
  340. m_mesh[id].m_numVertices = numVertices;
  341. m_mesh[id].m_startIndex[0] = startIndex;
  342. m_mesh[id].m_numIndices[0] = numIndices;
  343. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  344. m_mesh[id].m_numIndices[1] = numLineListIndices;
  345. startVertex += numVertices;
  346. startIndex += numIndices + numLineListIndices;
  347. BX_FREE(m_allocator, trilist);
  348. }
  349. m_mesh[Mesh::Cube].m_startVertex = startVertex;
  350. m_mesh[Mesh::Cube].m_numVertices = BX_COUNTOF(s_cubeVertices);
  351. m_mesh[Mesh::Cube].m_startIndex[0] = startIndex;
  352. m_mesh[Mesh::Cube].m_numIndices[0] = BX_COUNTOF(s_cubeIndices);
  353. m_mesh[Mesh::Cube].m_startIndex[1] = 0;
  354. m_mesh[Mesh::Cube].m_numIndices[1] = 0;
  355. startVertex += m_mesh[Mesh::Cube].m_numVertices;
  356. startIndex += m_mesh[Mesh::Cube].m_numIndices[0];
  357. const bgfx::Memory* vb = bgfx::alloc(startVertex*stride);
  358. const bgfx::Memory* ib = bgfx::alloc(startIndex*sizeof(uint16_t) );
  359. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  360. {
  361. Mesh::Enum id = Mesh::Enum(Mesh::Sphere0+mesh);
  362. memcpy(&vb->data[m_mesh[id].m_startVertex * stride]
  363. , vertices[id]
  364. , m_mesh[id].m_numVertices*stride
  365. );
  366. memcpy(&ib->data[m_mesh[id].m_startIndex[0] * sizeof(uint16_t)]
  367. , indices[id]
  368. , (m_mesh[id].m_numIndices[0]+m_mesh[id].m_numIndices[1])*sizeof(uint16_t)
  369. );
  370. BX_FREE(m_allocator, vertices[id]);
  371. BX_FREE(m_allocator, indices[id]);
  372. }
  373. memcpy(&vb->data[m_mesh[Mesh::Cube].m_startVertex * stride]
  374. , s_cubeVertices
  375. , sizeof(s_cubeVertices)
  376. );
  377. memcpy(&ib->data[m_mesh[Mesh::Cube].m_startIndex[0] * sizeof(uint16_t)]
  378. , s_cubeIndices
  379. , sizeof(s_cubeIndices)
  380. );
  381. m_vbh = bgfx::createVertexBuffer(vb, DebugShapeVertex::ms_decl);
  382. m_ibh = bgfx::createIndexBuffer(ib);
  383. m_mtx = 0;
  384. m_viewId = 0;
  385. m_pos = 0;
  386. m_indexPos = 0;
  387. m_vertexPos = 0;
  388. }
  389. void shutdown()
  390. {
  391. bgfx::destroyIndexBuffer(m_ibh);
  392. bgfx::destroyVertexBuffer(m_vbh);
  393. for (uint32_t ii = 0; ii < Program::Count; ++ii)
  394. {
  395. bgfx::destroyProgram(m_program[ii]);
  396. }
  397. bgfx::destroyUniform(u_params);
  398. }
  399. void begin(uint8_t _viewId)
  400. {
  401. BX_CHECK(State::Count == m_state);
  402. m_viewId = _viewId;
  403. m_mtx = 0;
  404. m_state = State::None;
  405. m_stack = 0;
  406. Attrib& attrib = m_attrib[0];
  407. attrib.m_scale = 1.0f;
  408. attrib.m_offset = 0.0f;
  409. attrib.m_abgr = UINT32_MAX;
  410. attrib.m_stipple = false;
  411. attrib.m_wireframe = false;
  412. attrib.m_lod = 0;
  413. }
  414. void end()
  415. {
  416. BX_CHECK(0 == m_stack, "Invalid stack %d.", m_stack);
  417. flush();
  418. m_state = State::Count;
  419. }
  420. void push()
  421. {
  422. BX_CHECK(State::Count != m_state);
  423. ++m_stack;
  424. m_attrib[m_stack] = m_attrib[m_stack-1];
  425. }
  426. void pop()
  427. {
  428. BX_CHECK(State::Count != m_state);
  429. if (m_attrib[m_stack].m_stipple != m_attrib[m_stack-1].m_stipple)
  430. {
  431. flush();
  432. }
  433. --m_stack;
  434. }
  435. void setTransform(const void* _mtx)
  436. {
  437. BX_CHECK(State::Count != m_state);
  438. flush();
  439. if (NULL == _mtx)
  440. {
  441. m_mtx = 0;
  442. return;
  443. }
  444. bgfx::Transform transform;
  445. m_mtx = bgfx::allocTransform(&transform, 1);
  446. memcpy(transform.data, _mtx, 64);
  447. }
  448. void setTranslate(float _x, float _y, float _z)
  449. {
  450. float mtx[16];
  451. bx::mtxTranslate(mtx, _x, _y, _z);
  452. setTransform(mtx);
  453. }
  454. void setTranslate(const float* _pos)
  455. {
  456. setTranslate(_pos[0], _pos[1], _pos[2]);
  457. }
  458. void setColor(uint32_t _abgr)
  459. {
  460. BX_CHECK(State::Count != m_state);
  461. m_attrib[m_stack].m_abgr = _abgr;
  462. }
  463. void setLod(uint8_t _lod)
  464. {
  465. BX_CHECK(State::Count != m_state);
  466. m_attrib[m_stack].m_lod = _lod;
  467. }
  468. void setWireframe(bool _wireframe)
  469. {
  470. BX_CHECK(State::Count != m_state);
  471. m_attrib[m_stack].m_wireframe = _wireframe;
  472. }
  473. void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
  474. {
  475. BX_CHECK(State::Count != m_state);
  476. Attrib& attrib = m_attrib[m_stack];
  477. if (attrib.m_stipple != _stipple)
  478. {
  479. flush();
  480. }
  481. attrib.m_stipple = _stipple;
  482. attrib.m_offset = _offset;
  483. attrib.m_scale = _scale;
  484. }
  485. void moveTo(float _x, float _y, float _z = 0.0f)
  486. {
  487. BX_CHECK(State::Count != m_state);
  488. softFlush();
  489. m_state = State::MoveTo;
  490. DebugVertex& vertex = m_cache[m_pos];
  491. vertex.m_x = _x;
  492. vertex.m_y = _y;
  493. vertex.m_z = _z;
  494. Attrib& attrib = m_attrib[m_stack];
  495. vertex.m_abgr = attrib.m_abgr;
  496. vertex.m_len = attrib.m_offset;
  497. m_vertexPos = m_pos;
  498. }
  499. void moveTo(const void* _pos)
  500. {
  501. BX_CHECK(State::Count != m_state);
  502. const float* pos = (const float*)_pos;
  503. moveTo(pos[0], pos[1], pos[2]);
  504. }
  505. void moveTo(Axis::Enum _axis, float _x, float _y)
  506. {
  507. float pos[3];
  508. getPoint(pos, _axis, _x, _y);
  509. moveTo(pos);
  510. }
  511. void lineTo(float _x, float _y, float _z = 0.0f)
  512. {
  513. BX_CHECK(State::Count != m_state);
  514. if (State::None == m_state)
  515. {
  516. moveTo(_x, _y, _z);
  517. return;
  518. }
  519. if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
  520. {
  521. uint32_t pos = m_pos;
  522. uint32_t vertexPos = m_vertexPos;
  523. flush();
  524. memcpy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
  525. if (vertexPos == pos)
  526. {
  527. m_pos = 1;
  528. }
  529. else
  530. {
  531. memcpy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
  532. m_pos = 2;
  533. }
  534. m_state = State::LineTo;
  535. }
  536. else if (State::MoveTo == m_state)
  537. {
  538. ++m_pos;
  539. m_state = State::LineTo;
  540. }
  541. uint16_t prev = m_pos-1;
  542. uint16_t curr = m_pos++;
  543. DebugVertex& vertex = m_cache[curr];
  544. vertex.m_x = _x;
  545. vertex.m_y = _y;
  546. vertex.m_z = _z;
  547. Attrib& attrib = m_attrib[m_stack];
  548. vertex.m_abgr = attrib.m_abgr;
  549. vertex.m_len = attrib.m_offset;
  550. float tmp[3];
  551. bx::vec3Sub(tmp, &vertex.m_x, &m_cache[prev].m_x);
  552. float len = bx::vec3Length(tmp) * attrib.m_scale;
  553. vertex.m_len = m_cache[prev].m_len + len;
  554. m_indices[m_indexPos++] = prev;
  555. m_indices[m_indexPos++] = curr;
  556. }
  557. void lineTo(const void* _pos)
  558. {
  559. BX_CHECK(State::Count != m_state);
  560. const float* pos = (const float*)_pos;
  561. lineTo(pos[0], pos[1], pos[2]);
  562. }
  563. void lineTo(Axis::Enum _axis, float _x, float _y)
  564. {
  565. float pos[3];
  566. getPoint(pos, _axis, _x, _y);
  567. lineTo(pos);
  568. }
  569. void close()
  570. {
  571. BX_CHECK(State::Count != m_state);
  572. DebugVertex& vertex = m_cache[m_vertexPos];
  573. lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
  574. m_state = State::None;
  575. }
  576. void draw(const Aabb& _aabb)
  577. {
  578. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  579. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  580. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  581. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  582. close();
  583. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  584. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  585. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  586. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  587. close();
  588. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  589. lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  590. moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  591. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  592. moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  593. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  594. moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  595. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  596. }
  597. void draw(const Cylinder& _cylinder, bool _capsule)
  598. {
  599. BX_UNUSED(_cylinder, _capsule);
  600. }
  601. void draw(const Disk& _disk)
  602. {
  603. BX_UNUSED(_disk);
  604. }
  605. void draw(const Obb& _obb)
  606. {
  607. const Attrib& attrib = m_attrib[m_stack];
  608. if (attrib.m_wireframe)
  609. {
  610. setTransform(_obb.m_mtx);
  611. moveTo(-1.0f, -1.0f, -1.0f);
  612. lineTo( 1.0f, -1.0f, -1.0f);
  613. lineTo( 1.0f, 1.0f, -1.0f);
  614. lineTo(-1.0f, 1.0f, -1.0f);
  615. close();
  616. moveTo(-1.0f, 1.0f, 1.0f);
  617. lineTo( 1.0f, 1.0f, 1.0f);
  618. lineTo( 1.0f, -1.0f, 1.0f);
  619. lineTo(-1.0f, -1.0f, 1.0f);
  620. close();
  621. moveTo( 1.0f, -1.0f, -1.0f);
  622. lineTo( 1.0f, -1.0f, 1.0f);
  623. moveTo( 1.0f, 1.0f, -1.0f);
  624. lineTo( 1.0f, 1.0f, 1.0f);
  625. moveTo(-1.0f, 1.0f, -1.0f);
  626. lineTo(-1.0f, 1.0f, 1.0f);
  627. moveTo(-1.0f, -1.0f, -1.0f);
  628. lineTo(-1.0f, -1.0f, 1.0f);
  629. setTransform(NULL);
  630. }
  631. else
  632. {
  633. draw(Mesh::Cube, _obb.m_mtx, false);
  634. }
  635. }
  636. void draw(const Sphere& _sphere)
  637. {
  638. const Attrib& attrib = m_attrib[m_stack];
  639. float mtx[16];
  640. bx::mtxSRT(mtx
  641. , _sphere.m_radius
  642. , _sphere.m_radius
  643. , _sphere.m_radius
  644. , 0.0f
  645. , 0.0f
  646. , 0.0f
  647. , _sphere.m_center[0]
  648. , _sphere.m_center[1]
  649. , _sphere.m_center[2]
  650. );
  651. uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
  652. ? uint8_t(Mesh::SphereMaxLod)
  653. : attrib.m_lod
  654. ;
  655. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, attrib.m_wireframe);
  656. }
  657. void draw(const float* _viewProj)
  658. {
  659. Plane planes[6];
  660. buildFrustumPlanes(planes, _viewProj);
  661. float points[24];
  662. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  663. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  664. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  665. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  666. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  667. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  668. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  669. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  670. moveTo(&points[ 0]);
  671. lineTo(&points[ 3]);
  672. lineTo(&points[ 6]);
  673. lineTo(&points[ 9]);
  674. close();
  675. moveTo(&points[12]);
  676. lineTo(&points[15]);
  677. lineTo(&points[18]);
  678. lineTo(&points[21]);
  679. close();
  680. moveTo(&points[ 0]);
  681. lineTo(&points[12]);
  682. moveTo(&points[ 3]);
  683. lineTo(&points[15]);
  684. moveTo(&points[ 6]);
  685. lineTo(&points[18]);
  686. moveTo(&points[ 9]);
  687. lineTo(&points[21]);
  688. }
  689. void draw(const void* _viewProj)
  690. {
  691. draw( (const float*)_viewProj);
  692. }
  693. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  694. {
  695. const Attrib& attrib = m_attrib[m_stack];
  696. const uint32_t num = getCircleLod(attrib.m_lod);
  697. const float step = bx::pi * 2.0f / num;
  698. _degrees = bx::fwrap(_degrees, 360.0f);
  699. float pos[3];
  700. getPoint(pos, _axis
  701. , bx::fsin(step * 0)*_radius
  702. , bx::fcos(step * 0)*_radius
  703. );
  704. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  705. uint32_t n = uint32_t(num*_degrees/360.0f);
  706. for (uint32_t ii = 1; ii < n+1; ++ii)
  707. {
  708. getPoint(pos, _axis
  709. , bx::fsin(step * ii)*_radius
  710. , bx::fcos(step * ii)*_radius
  711. );
  712. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  713. }
  714. moveTo(_x, _y, _z);
  715. getPoint(pos, _axis
  716. , bx::fsin(step * 0)*_radius
  717. , bx::fcos(step * 0)*_radius
  718. );
  719. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  720. getPoint(pos, _axis
  721. , bx::fsin(step * n)*_radius
  722. , bx::fcos(step * n)*_radius
  723. );
  724. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  725. lineTo(_x, _y, _z);
  726. }
  727. void drawCircle(const float* _normal, const float* _center, float _radius)
  728. {
  729. const Attrib& attrib = m_attrib[m_stack];
  730. const uint32_t num = getCircleLod(attrib.m_lod);
  731. const float step = bx::pi * 2.0f / num;
  732. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  733. float udir[3];
  734. float vdir[3];
  735. calcPlaneUv(plane, udir, vdir);
  736. float pos[3];
  737. bx::vec3Add(pos, vdir, _center);
  738. moveTo(pos);
  739. for (uint32_t ii = 1; ii < num; ++ii)
  740. {
  741. float tmp0[3];
  742. float tmp1[3];
  743. bx::vec3Mul(pos, udir, bx::fsin(step * ii)*_radius);
  744. bx::vec3Mul(tmp0, vdir, bx::fcos(step * ii)*_radius);
  745. bx::vec3Add(tmp1, pos, tmp0);
  746. bx::vec3Add(pos, tmp1, _center);
  747. lineTo(pos);
  748. }
  749. }
  750. void drawCircle(const void* _normal, const void* _center, float _radius)
  751. {
  752. drawCircle( (const float*)_normal, (const float*)_center, _radius);
  753. }
  754. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius)
  755. {
  756. const Attrib& attrib = m_attrib[m_stack];
  757. const uint32_t num = getCircleLod(attrib.m_lod);
  758. const float step = bx::pi * 2.0f / num;
  759. float pos[3];
  760. getPoint(pos, _axis
  761. , bx::fsin(step * 0)*_radius
  762. , bx::fcos(step * 0)*_radius
  763. );
  764. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  765. for (uint32_t ii = 1; ii < num; ++ii)
  766. {
  767. getPoint(pos, _axis
  768. , bx::fsin(step * ii)*_radius
  769. , bx::fcos(step * ii)*_radius
  770. );
  771. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  772. }
  773. close();
  774. }
  775. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight)
  776. {
  777. push();
  778. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  779. moveTo(_x, _y, _z);
  780. lineTo(_x + _len, _y, _z);
  781. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  782. moveTo(_x, _y, _z);
  783. lineTo(_x, _y + _len, _z);
  784. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  785. moveTo(_x, _y, _z);
  786. lineTo(_x, _y, _z + _len);
  787. pop();
  788. }
  789. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  790. {
  791. float udir[3];
  792. float vdir[3];
  793. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  794. calcPlaneUv(plane, udir, vdir);
  795. bx::vec3Mul(udir, udir, _step);
  796. bx::vec3Mul(vdir, vdir, _step);
  797. const uint32_t num = (_size/2)*2+1;
  798. const float halfExtent = float(_size/2);
  799. float umin[3];
  800. bx::vec3Mul(umin, udir, -halfExtent);
  801. float umax[3];
  802. bx::vec3Mul(umax, udir, halfExtent);
  803. float vmin[3];
  804. bx::vec3Mul(vmin, vdir, -halfExtent);
  805. float vmax[3];
  806. bx::vec3Mul(vmax, vdir, halfExtent);
  807. float tmp[3];
  808. float xs[3];
  809. float xe[3];
  810. bx::vec3Add(tmp, umin, vmin);
  811. bx::vec3Add(xs, _center, tmp);
  812. bx::vec3Add(tmp, umax, vmin);
  813. bx::vec3Add(xe, _center, tmp);
  814. float ys[3];
  815. float ye[3];
  816. bx::vec3Add(tmp, umin, vmin);
  817. bx::vec3Add(ys, _center, tmp);
  818. bx::vec3Add(tmp, umin, vmax);
  819. bx::vec3Add(ye, _center, tmp);
  820. for (uint32_t ii = 0; ii < num; ++ii)
  821. {
  822. moveTo(xs);
  823. lineTo(xe);
  824. bx::vec3Add(xs, xs, vdir);
  825. bx::vec3Add(xe, xe, vdir);
  826. moveTo(ys);
  827. lineTo(ye);
  828. bx::vec3Add(ys, ys, udir);
  829. bx::vec3Add(ye, ye, udir);
  830. }
  831. }
  832. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  833. {
  834. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  835. }
  836. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  837. {
  838. push();
  839. setTranslate(_center);
  840. const uint32_t num = (_size/2)*2-1;
  841. const float halfExtent = float(_size/2) * _step;
  842. setColor(0xff606060);
  843. float yy = -halfExtent + _step;
  844. for (uint32_t ii = 0; ii < num; ++ii)
  845. {
  846. moveTo(_axis, -halfExtent, yy);
  847. lineTo(_axis, halfExtent, yy);
  848. moveTo(_axis, yy, -halfExtent);
  849. lineTo(_axis, yy, halfExtent);
  850. yy += _step;
  851. }
  852. setColor(0xff101010);
  853. moveTo(_axis, -halfExtent, -halfExtent);
  854. lineTo(_axis, -halfExtent, halfExtent);
  855. lineTo(_axis, halfExtent, halfExtent);
  856. lineTo(_axis, halfExtent, -halfExtent);
  857. close();
  858. moveTo(_axis, -halfExtent, 0.0f);
  859. lineTo(_axis, halfExtent, 0.0f);
  860. moveTo(_axis, 0.0f, -halfExtent);
  861. lineTo(_axis, 0.0f, halfExtent);
  862. pop();
  863. }
  864. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  865. {
  866. drawGrid(_axis, (const float*)_center, _size, _step);
  867. }
  868. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  869. {
  870. push();
  871. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  872. drawCircle(Axis::X, _x, _y, _z, _radius);
  873. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  874. drawCircle(Axis::Y, _x, _y, _z, _radius);
  875. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  876. drawCircle(Axis::Z, _x, _y, _z, _radius);
  877. pop();
  878. }
  879. private:
  880. struct Mesh
  881. {
  882. enum Enum
  883. {
  884. Sphere0,
  885. Sphere1,
  886. Sphere2,
  887. Sphere3,
  888. Cube,
  889. Count,
  890. SphereMaxLod = Sphere3 - Sphere0,
  891. };
  892. uint32_t m_startVertex;
  893. uint32_t m_numVertices;
  894. uint32_t m_startIndex[2];
  895. uint32_t m_numIndices[2];
  896. };
  897. struct Program
  898. {
  899. enum Enum
  900. {
  901. Lines,
  902. LinesStipple,
  903. Fill,
  904. FillLit,
  905. Count
  906. };
  907. };
  908. void draw(Mesh::Enum _mesh, const float* _mtx, bool _wireframe) const
  909. {
  910. const Mesh& mesh = m_mesh[_mesh];
  911. const Attrib& attrib = m_attrib[m_stack];
  912. if (0 != mesh.m_numIndices[_wireframe])
  913. {
  914. bgfx::setIndexBuffer(m_ibh
  915. , mesh.m_startIndex[_wireframe]
  916. , mesh.m_numIndices[_wireframe]
  917. );
  918. }
  919. float params[4][4] =
  920. {
  921. {
  922. 0.0f,
  923. -1.0f,
  924. 0.0f,
  925. 3.0f,
  926. },
  927. {
  928. 1.0f,
  929. 0.9f,
  930. 0.8f,
  931. 0.0f,
  932. },
  933. {
  934. 0.2f,
  935. 0.22f,
  936. 0.5f,
  937. 0.0f,
  938. },
  939. {
  940. ( (attrib.m_abgr>>24) )/255.0f,
  941. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  942. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  943. ( (attrib.m_abgr )&0xff)/255.0f,
  944. },
  945. };
  946. bx::vec3Norm(params[0], params[0]);
  947. bgfx::setUniform(u_params, params, 4);
  948. bgfx::setTransform(_mtx);
  949. bgfx::setVertexBuffer(m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  950. bgfx::setState(0
  951. | BGFX_STATE_RGB_WRITE
  952. | BGFX_STATE_DEPTH_TEST_LESS
  953. | BGFX_STATE_DEPTH_WRITE
  954. | BGFX_STATE_CULL_CW
  955. | (_wireframe ? BGFX_STATE_PT_LINES : 0)
  956. );
  957. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  958. }
  959. void softFlush()
  960. {
  961. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  962. {
  963. flush();
  964. }
  965. }
  966. void flush()
  967. {
  968. if (0 != m_pos)
  969. {
  970. if (bgfx::checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  971. {
  972. bgfx::TransientVertexBuffer tvb;
  973. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  974. memcpy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  975. bgfx::TransientIndexBuffer tib;
  976. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  977. memcpy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  978. bgfx::setVertexBuffer(&tvb);
  979. bgfx::setIndexBuffer(&tib);
  980. bgfx::setState(0
  981. | BGFX_STATE_RGB_WRITE
  982. | BGFX_STATE_PT_LINES
  983. | BGFX_STATE_DEPTH_TEST_LEQUAL
  984. | BGFX_STATE_DEPTH_WRITE
  985. // | BGFX_STATE_BLEND_ALPHA
  986. );
  987. bgfx::setTransform(m_mtx);
  988. bgfx::ProgramHandle program = m_program[m_attrib[m_stack].m_stipple ? 1 : 0];
  989. bgfx::submit(m_viewId, program);
  990. }
  991. m_state = State::None;
  992. m_pos = 0;
  993. m_indexPos = 0;
  994. m_vertexPos = 0;
  995. }
  996. }
  997. struct State
  998. {
  999. enum Enum
  1000. {
  1001. None,
  1002. MoveTo,
  1003. LineTo,
  1004. Count
  1005. };
  1006. };
  1007. static const uint32_t cacheSize = 1024;
  1008. static const uint32_t stackSize = 16;
  1009. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1010. DebugVertex m_cache[cacheSize+1];
  1011. uint32_t m_mtx;
  1012. uint16_t m_indices[cacheSize*2];
  1013. uint16_t m_pos;
  1014. uint16_t m_indexPos;
  1015. uint16_t m_vertexPos;
  1016. uint8_t m_viewId;
  1017. uint8_t m_stack;
  1018. struct Attrib
  1019. {
  1020. float m_offset;
  1021. float m_scale;
  1022. uint32_t m_abgr;
  1023. bool m_stipple;
  1024. bool m_wireframe;
  1025. uint8_t m_lod;
  1026. };
  1027. Attrib m_attrib[stackSize];
  1028. State::Enum m_state;
  1029. Mesh m_mesh[Mesh::Count];
  1030. bgfx::ProgramHandle m_program[Program::Count];
  1031. bgfx::UniformHandle u_params;
  1032. bgfx::VertexBufferHandle m_vbh;
  1033. bgfx::IndexBufferHandle m_ibh;
  1034. bx::AllocatorI* m_allocator;
  1035. };
  1036. static DebugDraw s_dd;
  1037. void ddInit(bx::AllocatorI* _allocator)
  1038. {
  1039. s_dd.init(_allocator);
  1040. }
  1041. void ddShutdown()
  1042. {
  1043. s_dd.shutdown();
  1044. }
  1045. void ddBegin(uint8_t _viewId)
  1046. {
  1047. s_dd.begin(_viewId);
  1048. }
  1049. void ddEnd()
  1050. {
  1051. s_dd.end();
  1052. }
  1053. void ddPush()
  1054. {
  1055. s_dd.push();
  1056. }
  1057. void ddPop()
  1058. {
  1059. s_dd.pop();
  1060. }
  1061. void ddSetColor(uint32_t _abgr)
  1062. {
  1063. s_dd.setColor(_abgr);
  1064. }
  1065. void ddSetLod(uint8_t _lod)
  1066. {
  1067. s_dd.setLod(_lod);
  1068. }
  1069. void ddSetWireframe(bool _wireframe)
  1070. {
  1071. s_dd.setWireframe(_wireframe);
  1072. }
  1073. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1074. {
  1075. s_dd.setStipple(_stipple, _scale, _offset);
  1076. }
  1077. void ddSetTransform(const void* _mtx)
  1078. {
  1079. s_dd.setTransform(_mtx);
  1080. }
  1081. void ddSetTranslate(float _x, float _y, float _z)
  1082. {
  1083. s_dd.setTranslate(_x, _y, _z);
  1084. }
  1085. void ddMoveTo(float _x, float _y, float _z)
  1086. {
  1087. s_dd.moveTo(_x, _y, _z);
  1088. }
  1089. void ddMoveTo(const void* _pos)
  1090. {
  1091. s_dd.moveTo(_pos);
  1092. }
  1093. void ddLineTo(float _x, float _y, float _z)
  1094. {
  1095. s_dd.lineTo(_x, _y, _z);
  1096. }
  1097. void ddLineTo(const void* _pos)
  1098. {
  1099. s_dd.lineTo(_pos);
  1100. }
  1101. void ddClose()
  1102. {
  1103. s_dd.close();
  1104. }
  1105. void ddDraw(const Aabb& _aabb)
  1106. {
  1107. s_dd.draw(_aabb);
  1108. }
  1109. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1110. {
  1111. s_dd.draw(_cylinder, _capsule);
  1112. }
  1113. void ddDraw(const Disk& _disk)
  1114. {
  1115. s_dd.draw(_disk);
  1116. }
  1117. void ddDraw(const Obb& _obb)
  1118. {
  1119. s_dd.draw(_obb);
  1120. }
  1121. void ddDraw(const Sphere& _sphere)
  1122. {
  1123. s_dd.draw(_sphere);
  1124. }
  1125. void ddDraw(const void* _viewProj)
  1126. {
  1127. s_dd.draw(_viewProj);
  1128. }
  1129. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1130. {
  1131. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1132. }
  1133. void ddDrawCircle(const void* _normal, const void* _center, float _radius)
  1134. {
  1135. s_dd.drawCircle(_normal, _center, _radius);
  1136. }
  1137. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius)
  1138. {
  1139. s_dd.drawCircle(_axis, _x, _y, _z, _radius);
  1140. }
  1141. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight)
  1142. {
  1143. s_dd.drawAxis(_x, _y, _z, _len, _hightlight);
  1144. }
  1145. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1146. {
  1147. s_dd.drawGrid(_normal, _center, _size, _step);
  1148. }
  1149. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1150. {
  1151. s_dd.drawGrid(_axis, _center, _size, _step);
  1152. }
  1153. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1154. {
  1155. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1156. }