debugdraw.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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. bgfx::destroyProgram(m_program[0]);
  394. bgfx::destroyProgram(m_program[1]);
  395. bgfx::destroyProgram(m_program[2]);
  396. bgfx::destroyUniform(u_params);
  397. }
  398. void begin(uint8_t _viewId)
  399. {
  400. BX_CHECK(State::Count == m_state);
  401. m_viewId = _viewId;
  402. m_mtx = 0;
  403. m_state = State::None;
  404. m_stack = 0;
  405. Attrib& attrib = m_attrib[0];
  406. attrib.m_scale = 1.0f;
  407. attrib.m_offset = 0.0f;
  408. attrib.m_abgr = UINT32_MAX;
  409. attrib.m_stipple = false;
  410. attrib.m_wireframe = false;
  411. attrib.m_lod = 0;
  412. }
  413. void end()
  414. {
  415. BX_CHECK(0 == m_stack, "Invalid stack %d.", m_stack);
  416. flush();
  417. m_state = State::Count;
  418. }
  419. void push()
  420. {
  421. BX_CHECK(State::Count != m_state);
  422. ++m_stack;
  423. m_attrib[m_stack] = m_attrib[m_stack-1];
  424. }
  425. void pop()
  426. {
  427. BX_CHECK(State::Count != m_state);
  428. if (m_attrib[m_stack].m_stipple != m_attrib[m_stack-1].m_stipple)
  429. {
  430. flush();
  431. }
  432. --m_stack;
  433. }
  434. void setTransform(const void* _mtx)
  435. {
  436. BX_CHECK(State::Count != m_state);
  437. flush();
  438. if (NULL == _mtx)
  439. {
  440. m_mtx = 0;
  441. return;
  442. }
  443. bgfx::Transform transform;
  444. m_mtx = bgfx::allocTransform(&transform, 1);
  445. memcpy(transform.data, _mtx, 64);
  446. }
  447. void setTranslate(float _x, float _y, float _z)
  448. {
  449. float mtx[16];
  450. bx::mtxTranslate(mtx, _x, _y, _z);
  451. setTransform(mtx);
  452. }
  453. void setTranslate(const float* _pos)
  454. {
  455. setTranslate(_pos[0], _pos[1], _pos[2]);
  456. }
  457. void setColor(uint32_t _abgr)
  458. {
  459. BX_CHECK(State::Count != m_state);
  460. m_attrib[m_stack].m_abgr = _abgr;
  461. }
  462. void setLod(uint8_t _lod)
  463. {
  464. BX_CHECK(State::Count != m_state);
  465. m_attrib[m_stack].m_lod = _lod;
  466. }
  467. void setWireframe(bool _wireframe)
  468. {
  469. BX_CHECK(State::Count != m_state);
  470. m_attrib[m_stack].m_wireframe = _wireframe;
  471. }
  472. void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
  473. {
  474. BX_CHECK(State::Count != m_state);
  475. Attrib& attrib = m_attrib[m_stack];
  476. if (attrib.m_stipple != _stipple)
  477. {
  478. flush();
  479. }
  480. attrib.m_stipple = _stipple;
  481. attrib.m_offset = _offset;
  482. attrib.m_scale = _scale;
  483. }
  484. void moveTo(float _x, float _y, float _z = 0.0f)
  485. {
  486. BX_CHECK(State::Count != m_state);
  487. softFlush();
  488. m_state = State::MoveTo;
  489. DebugVertex& vertex = m_cache[m_pos];
  490. vertex.m_x = _x;
  491. vertex.m_y = _y;
  492. vertex.m_z = _z;
  493. Attrib& attrib = m_attrib[m_stack];
  494. vertex.m_abgr = attrib.m_abgr;
  495. vertex.m_len = attrib.m_offset;
  496. m_vertexPos = m_pos;
  497. }
  498. void moveTo(const void* _pos)
  499. {
  500. BX_CHECK(State::Count != m_state);
  501. const float* pos = (const float*)_pos;
  502. moveTo(pos[0], pos[1], pos[2]);
  503. }
  504. void moveTo(Axis::Enum _axis, float _x, float _y)
  505. {
  506. float pos[3];
  507. getPoint(pos, _axis, _x, _y);
  508. moveTo(pos);
  509. }
  510. void lineTo(float _x, float _y, float _z = 0.0f)
  511. {
  512. BX_CHECK(State::Count != m_state);
  513. if (State::None == m_state)
  514. {
  515. moveTo(_x, _y, _z);
  516. return;
  517. }
  518. if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
  519. {
  520. uint32_t pos = m_pos;
  521. uint32_t vertexPos = m_vertexPos;
  522. flush();
  523. memcpy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
  524. if (vertexPos == pos)
  525. {
  526. m_pos = 1;
  527. }
  528. else
  529. {
  530. memcpy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
  531. m_pos = 2;
  532. }
  533. m_state = State::LineTo;
  534. }
  535. else if (State::MoveTo == m_state)
  536. {
  537. ++m_pos;
  538. m_state = State::LineTo;
  539. }
  540. uint16_t prev = m_pos-1;
  541. uint16_t curr = m_pos++;
  542. DebugVertex& vertex = m_cache[curr];
  543. vertex.m_x = _x;
  544. vertex.m_y = _y;
  545. vertex.m_z = _z;
  546. Attrib& attrib = m_attrib[m_stack];
  547. vertex.m_abgr = attrib.m_abgr;
  548. vertex.m_len = attrib.m_offset;
  549. float tmp[3];
  550. bx::vec3Sub(tmp, &vertex.m_x, &m_cache[prev].m_x);
  551. float len = bx::vec3Length(tmp) * attrib.m_scale;
  552. vertex.m_len = m_cache[prev].m_len + len;
  553. m_indices[m_indexPos++] = prev;
  554. m_indices[m_indexPos++] = curr;
  555. }
  556. void lineTo(const void* _pos)
  557. {
  558. BX_CHECK(State::Count != m_state);
  559. const float* pos = (const float*)_pos;
  560. lineTo(pos[0], pos[1], pos[2]);
  561. }
  562. void lineTo(Axis::Enum _axis, float _x, float _y)
  563. {
  564. float pos[3];
  565. getPoint(pos, _axis, _x, _y);
  566. lineTo(pos);
  567. }
  568. void close()
  569. {
  570. BX_CHECK(State::Count != m_state);
  571. DebugVertex& vertex = m_cache[m_vertexPos];
  572. lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
  573. m_state = State::None;
  574. }
  575. void draw(const Aabb& _aabb)
  576. {
  577. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  578. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  579. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  580. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  581. close();
  582. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  583. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  584. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  585. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  586. close();
  587. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  588. lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  589. moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  590. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  591. moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  592. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  593. moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  594. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  595. }
  596. void draw(const Cylinder& _cylinder, bool _capsule)
  597. {
  598. BX_UNUSED(_cylinder, _capsule);
  599. }
  600. void draw(const Disk& _disk)
  601. {
  602. BX_UNUSED(_disk);
  603. }
  604. void draw(const Obb& _obb)
  605. {
  606. const Attrib& attrib = m_attrib[m_stack];
  607. if (attrib.m_wireframe)
  608. {
  609. setTransform(_obb.m_mtx);
  610. moveTo(-1.0f, -1.0f, -1.0f);
  611. lineTo( 1.0f, -1.0f, -1.0f);
  612. lineTo( 1.0f, 1.0f, -1.0f);
  613. lineTo(-1.0f, 1.0f, -1.0f);
  614. close();
  615. moveTo(-1.0f, 1.0f, 1.0f);
  616. lineTo( 1.0f, 1.0f, 1.0f);
  617. lineTo( 1.0f, -1.0f, 1.0f);
  618. lineTo(-1.0f, -1.0f, 1.0f);
  619. close();
  620. moveTo( 1.0f, -1.0f, -1.0f);
  621. lineTo( 1.0f, -1.0f, 1.0f);
  622. moveTo( 1.0f, 1.0f, -1.0f);
  623. lineTo( 1.0f, 1.0f, 1.0f);
  624. moveTo(-1.0f, 1.0f, -1.0f);
  625. lineTo(-1.0f, 1.0f, 1.0f);
  626. moveTo(-1.0f, -1.0f, -1.0f);
  627. lineTo(-1.0f, -1.0f, 1.0f);
  628. setTransform(NULL);
  629. }
  630. else
  631. {
  632. draw(Mesh::Cube, _obb.m_mtx, false);
  633. }
  634. }
  635. void draw(const Sphere& _sphere)
  636. {
  637. const Attrib& attrib = m_attrib[m_stack];
  638. float mtx[16];
  639. bx::mtxSRT(mtx
  640. , _sphere.m_radius
  641. , _sphere.m_radius
  642. , _sphere.m_radius
  643. , 0.0f
  644. , 0.0f
  645. , 0.0f
  646. , _sphere.m_center[0]
  647. , _sphere.m_center[1]
  648. , _sphere.m_center[2]
  649. );
  650. uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
  651. ? uint8_t(Mesh::SphereMaxLod)
  652. : attrib.m_lod
  653. ;
  654. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, attrib.m_wireframe);
  655. }
  656. void draw(const float* _viewProj)
  657. {
  658. Plane planes[6];
  659. buildFrustumPlanes(planes, _viewProj);
  660. float points[24];
  661. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  662. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  663. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  664. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  665. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  666. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  667. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  668. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  669. moveTo(&points[ 0]);
  670. lineTo(&points[ 3]);
  671. lineTo(&points[ 6]);
  672. lineTo(&points[ 9]);
  673. close();
  674. moveTo(&points[12]);
  675. lineTo(&points[15]);
  676. lineTo(&points[18]);
  677. lineTo(&points[21]);
  678. close();
  679. moveTo(&points[ 0]);
  680. lineTo(&points[12]);
  681. moveTo(&points[ 3]);
  682. lineTo(&points[15]);
  683. moveTo(&points[ 6]);
  684. lineTo(&points[18]);
  685. moveTo(&points[ 9]);
  686. lineTo(&points[21]);
  687. }
  688. void draw(const void* _viewProj)
  689. {
  690. draw( (const float*)_viewProj);
  691. }
  692. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  693. {
  694. const Attrib& attrib = m_attrib[m_stack];
  695. const uint32_t num = getCircleLod(attrib.m_lod);
  696. const float step = bx::pi * 2.0f / num;
  697. _degrees = bx::fwrap(_degrees, 360.0f);
  698. float pos[3];
  699. getPoint(pos, _axis
  700. , bx::fsin(step * 0)*_radius
  701. , bx::fcos(step * 0)*_radius
  702. );
  703. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  704. uint32_t n = uint32_t(num*_degrees/360.0f);
  705. for (uint32_t ii = 1; ii < n+1; ++ii)
  706. {
  707. getPoint(pos, _axis
  708. , bx::fsin(step * ii)*_radius
  709. , bx::fcos(step * ii)*_radius
  710. );
  711. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  712. }
  713. moveTo(_x, _y, _z);
  714. getPoint(pos, _axis
  715. , bx::fsin(step * 0)*_radius
  716. , bx::fcos(step * 0)*_radius
  717. );
  718. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  719. getPoint(pos, _axis
  720. , bx::fsin(step * n)*_radius
  721. , bx::fcos(step * n)*_radius
  722. );
  723. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  724. lineTo(_x, _y, _z);
  725. }
  726. void drawCircle(const float* _normal, const float* _center, float _radius)
  727. {
  728. const Attrib& attrib = m_attrib[m_stack];
  729. const uint32_t num = getCircleLod(attrib.m_lod);
  730. const float step = bx::pi * 2.0f / num;
  731. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  732. float udir[3];
  733. float vdir[3];
  734. calcPlaneUv(plane, udir, vdir);
  735. float pos[3];
  736. bx::vec3Add(pos, vdir, _center);
  737. moveTo(pos);
  738. for (uint32_t ii = 1; ii < num; ++ii)
  739. {
  740. float tmp0[3];
  741. float tmp1[3];
  742. bx::vec3Mul(pos, udir, bx::fsin(step * ii)*_radius);
  743. bx::vec3Mul(tmp0, vdir, bx::fcos(step * ii)*_radius);
  744. bx::vec3Add(tmp1, pos, tmp0);
  745. bx::vec3Add(pos, tmp1, _center);
  746. lineTo(pos);
  747. }
  748. }
  749. void drawCircle(const void* _normal, const void* _center, float _radius)
  750. {
  751. drawCircle( (const float*)_normal, (const float*)_center, _radius);
  752. }
  753. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius)
  754. {
  755. const Attrib& attrib = m_attrib[m_stack];
  756. const uint32_t num = getCircleLod(attrib.m_lod);
  757. const float step = bx::pi * 2.0f / num;
  758. float pos[3];
  759. getPoint(pos, _axis
  760. , bx::fsin(step * 0)*_radius
  761. , bx::fcos(step * 0)*_radius
  762. );
  763. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  764. for (uint32_t ii = 1; ii < num; ++ii)
  765. {
  766. getPoint(pos, _axis
  767. , bx::fsin(step * ii)*_radius
  768. , bx::fcos(step * ii)*_radius
  769. );
  770. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  771. }
  772. close();
  773. }
  774. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight)
  775. {
  776. push();
  777. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  778. moveTo(_x, _y, _z);
  779. lineTo(_x + _len, _y, _z);
  780. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  781. moveTo(_x, _y, _z);
  782. lineTo(_x, _y + _len, _z);
  783. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  784. moveTo(_x, _y, _z);
  785. lineTo(_x, _y, _z + _len);
  786. pop();
  787. }
  788. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  789. {
  790. float udir[3];
  791. float vdir[3];
  792. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  793. calcPlaneUv(plane, udir, vdir);
  794. bx::vec3Mul(udir, udir, _step);
  795. bx::vec3Mul(vdir, vdir, _step);
  796. const uint32_t num = (_size/2)*2+1;
  797. const float halfExtent = float(_size/2);
  798. float umin[3];
  799. bx::vec3Mul(umin, udir, -halfExtent);
  800. float umax[3];
  801. bx::vec3Mul(umax, udir, halfExtent);
  802. float vmin[3];
  803. bx::vec3Mul(vmin, vdir, -halfExtent);
  804. float vmax[3];
  805. bx::vec3Mul(vmax, vdir, halfExtent);
  806. float tmp[3];
  807. float xs[3];
  808. float xe[3];
  809. bx::vec3Add(tmp, umin, vmin);
  810. bx::vec3Add(xs, _center, tmp);
  811. bx::vec3Add(tmp, umax, vmin);
  812. bx::vec3Add(xe, _center, tmp);
  813. float ys[3];
  814. float ye[3];
  815. bx::vec3Add(tmp, umin, vmin);
  816. bx::vec3Add(ys, _center, tmp);
  817. bx::vec3Add(tmp, umin, vmax);
  818. bx::vec3Add(ye, _center, tmp);
  819. for (uint32_t ii = 0; ii < num; ++ii)
  820. {
  821. moveTo(xs);
  822. lineTo(xe);
  823. bx::vec3Add(xs, xs, vdir);
  824. bx::vec3Add(xe, xe, vdir);
  825. moveTo(ys);
  826. lineTo(ye);
  827. bx::vec3Add(ys, ys, udir);
  828. bx::vec3Add(ye, ye, udir);
  829. }
  830. }
  831. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  832. {
  833. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  834. }
  835. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  836. {
  837. push();
  838. setTranslate(_center);
  839. const uint32_t num = (_size/2)*2-1;
  840. const float halfExtent = float(_size/2) * _step;
  841. setColor(0xff606060);
  842. float yy = -halfExtent + _step;
  843. for (uint32_t ii = 0; ii < num; ++ii)
  844. {
  845. moveTo(_axis, -halfExtent, yy);
  846. lineTo(_axis, halfExtent, yy);
  847. moveTo(_axis, yy, -halfExtent);
  848. lineTo(_axis, yy, halfExtent);
  849. yy += _step;
  850. }
  851. setColor(0xff101010);
  852. moveTo(_axis, -halfExtent, -halfExtent);
  853. lineTo(_axis, -halfExtent, halfExtent);
  854. lineTo(_axis, halfExtent, halfExtent);
  855. lineTo(_axis, halfExtent, -halfExtent);
  856. close();
  857. moveTo(_axis, -halfExtent, 0.0f);
  858. lineTo(_axis, halfExtent, 0.0f);
  859. moveTo(_axis, 0.0f, -halfExtent);
  860. lineTo(_axis, 0.0f, halfExtent);
  861. pop();
  862. }
  863. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  864. {
  865. drawGrid(_axis, (const float*)_center, _size, _step);
  866. }
  867. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  868. {
  869. push();
  870. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  871. drawCircle(Axis::X, _x, _y, _z, _radius);
  872. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  873. drawCircle(Axis::Y, _x, _y, _z, _radius);
  874. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  875. drawCircle(Axis::Z, _x, _y, _z, _radius);
  876. pop();
  877. }
  878. private:
  879. struct Mesh
  880. {
  881. enum Enum
  882. {
  883. Sphere0,
  884. Sphere1,
  885. Sphere2,
  886. Sphere3,
  887. Cube,
  888. Count,
  889. SphereMaxLod = Sphere3 - Sphere0,
  890. };
  891. uint32_t m_startVertex;
  892. uint32_t m_numVertices;
  893. uint32_t m_startIndex[2];
  894. uint32_t m_numIndices[2];
  895. };
  896. struct Program
  897. {
  898. enum Enum
  899. {
  900. Lines,
  901. LinesStipple,
  902. Fill,
  903. FillLit,
  904. Count
  905. };
  906. };
  907. void draw(Mesh::Enum _mesh, const float* _mtx, bool _wireframe) const
  908. {
  909. const Mesh& mesh = m_mesh[_mesh];
  910. const Attrib& attrib = m_attrib[m_stack];
  911. if (0 != mesh.m_numIndices[_wireframe])
  912. {
  913. bgfx::setIndexBuffer(m_ibh
  914. , mesh.m_startIndex[_wireframe]
  915. , mesh.m_numIndices[_wireframe]
  916. );
  917. }
  918. float params[4][4] =
  919. {
  920. {
  921. 0.0f,
  922. -1.0f,
  923. 0.0f,
  924. 3.0f,
  925. },
  926. {
  927. 1.0f,
  928. 0.9f,
  929. 0.8f,
  930. 0.0f,
  931. },
  932. {
  933. 0.2f,
  934. 0.22f,
  935. 0.5f,
  936. 0.0f,
  937. },
  938. {
  939. ( (attrib.m_abgr>>24) )/255.0f,
  940. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  941. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  942. ( (attrib.m_abgr )&0xff)/255.0f,
  943. },
  944. };
  945. bx::vec3Norm(params[0], params[0]);
  946. bgfx::setUniform(u_params, params, 4);
  947. bgfx::setTransform(_mtx);
  948. bgfx::setVertexBuffer(m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  949. bgfx::setState(0
  950. | BGFX_STATE_RGB_WRITE
  951. | BGFX_STATE_DEPTH_TEST_LESS
  952. | BGFX_STATE_DEPTH_WRITE
  953. | BGFX_STATE_CULL_CW
  954. | (_wireframe ? BGFX_STATE_PT_LINES : 0)
  955. );
  956. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  957. }
  958. void softFlush()
  959. {
  960. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  961. {
  962. flush();
  963. }
  964. }
  965. void flush()
  966. {
  967. if (0 != m_pos)
  968. {
  969. if (bgfx::checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  970. {
  971. bgfx::TransientVertexBuffer tvb;
  972. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  973. memcpy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  974. bgfx::TransientIndexBuffer tib;
  975. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  976. memcpy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  977. bgfx::setVertexBuffer(&tvb);
  978. bgfx::setIndexBuffer(&tib);
  979. bgfx::setState(0
  980. | BGFX_STATE_RGB_WRITE
  981. | BGFX_STATE_PT_LINES
  982. | BGFX_STATE_DEPTH_TEST_LEQUAL
  983. | BGFX_STATE_DEPTH_WRITE
  984. // | BGFX_STATE_BLEND_ALPHA
  985. );
  986. bgfx::setTransform(m_mtx);
  987. bgfx::ProgramHandle program = m_program[m_attrib[m_stack].m_stipple ? 1 : 0];
  988. bgfx::submit(m_viewId, program);
  989. }
  990. m_state = State::None;
  991. m_pos = 0;
  992. m_indexPos = 0;
  993. m_vertexPos = 0;
  994. }
  995. }
  996. struct State
  997. {
  998. enum Enum
  999. {
  1000. None,
  1001. MoveTo,
  1002. LineTo,
  1003. Count
  1004. };
  1005. };
  1006. static const uint32_t cacheSize = 1024;
  1007. static const uint32_t stackSize = 16;
  1008. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1009. DebugVertex m_cache[cacheSize+1];
  1010. uint32_t m_mtx;
  1011. uint16_t m_indices[cacheSize*2];
  1012. uint16_t m_pos;
  1013. uint16_t m_indexPos;
  1014. uint16_t m_vertexPos;
  1015. uint8_t m_viewId;
  1016. uint8_t m_stack;
  1017. struct Attrib
  1018. {
  1019. float m_offset;
  1020. float m_scale;
  1021. uint32_t m_abgr;
  1022. bool m_stipple;
  1023. bool m_wireframe;
  1024. uint8_t m_lod;
  1025. };
  1026. Attrib m_attrib[stackSize];
  1027. State::Enum m_state;
  1028. Mesh m_mesh[Mesh::Count];
  1029. bgfx::ProgramHandle m_program[Program::Count];
  1030. bgfx::UniformHandle u_params;
  1031. bgfx::VertexBufferHandle m_vbh;
  1032. bgfx::IndexBufferHandle m_ibh;
  1033. bx::AllocatorI* m_allocator;
  1034. };
  1035. static DebugDraw s_dd;
  1036. void ddInit(bx::AllocatorI* _allocator)
  1037. {
  1038. s_dd.init(_allocator);
  1039. }
  1040. void ddShutdown()
  1041. {
  1042. s_dd.shutdown();
  1043. }
  1044. void ddBegin(uint8_t _viewId)
  1045. {
  1046. s_dd.begin(_viewId);
  1047. }
  1048. void ddEnd()
  1049. {
  1050. s_dd.end();
  1051. }
  1052. void ddPush()
  1053. {
  1054. s_dd.push();
  1055. }
  1056. void ddPop()
  1057. {
  1058. s_dd.pop();
  1059. }
  1060. void ddSetColor(uint32_t _abgr)
  1061. {
  1062. s_dd.setColor(_abgr);
  1063. }
  1064. void ddSetLod(uint8_t _lod)
  1065. {
  1066. s_dd.setLod(_lod);
  1067. }
  1068. void ddSetWireframe(bool _wireframe)
  1069. {
  1070. s_dd.setWireframe(_wireframe);
  1071. }
  1072. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1073. {
  1074. s_dd.setStipple(_stipple, _scale, _offset);
  1075. }
  1076. void ddSetTransform(const void* _mtx)
  1077. {
  1078. s_dd.setTransform(_mtx);
  1079. }
  1080. void ddSetTranslate(float _x, float _y, float _z)
  1081. {
  1082. s_dd.setTranslate(_x, _y, _z);
  1083. }
  1084. void ddMoveTo(float _x, float _y, float _z)
  1085. {
  1086. s_dd.moveTo(_x, _y, _z);
  1087. }
  1088. void ddMoveTo(const void* _pos)
  1089. {
  1090. s_dd.moveTo(_pos);
  1091. }
  1092. void ddLineTo(float _x, float _y, float _z)
  1093. {
  1094. s_dd.lineTo(_x, _y, _z);
  1095. }
  1096. void ddLineTo(const void* _pos)
  1097. {
  1098. s_dd.lineTo(_pos);
  1099. }
  1100. void ddClose()
  1101. {
  1102. s_dd.close();
  1103. }
  1104. void ddDraw(const Aabb& _aabb)
  1105. {
  1106. s_dd.draw(_aabb);
  1107. }
  1108. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1109. {
  1110. s_dd.draw(_cylinder, _capsule);
  1111. }
  1112. void ddDraw(const Disk& _disk)
  1113. {
  1114. s_dd.draw(_disk);
  1115. }
  1116. void ddDraw(const Obb& _obb)
  1117. {
  1118. s_dd.draw(_obb);
  1119. }
  1120. void ddDraw(const Sphere& _sphere)
  1121. {
  1122. s_dd.draw(_sphere);
  1123. }
  1124. void ddDraw(const void* _viewProj)
  1125. {
  1126. s_dd.draw(_viewProj);
  1127. }
  1128. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1129. {
  1130. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1131. }
  1132. void ddDrawCircle(const void* _normal, const void* _center, float _radius)
  1133. {
  1134. s_dd.drawCircle(_normal, _center, _radius);
  1135. }
  1136. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius)
  1137. {
  1138. s_dd.drawCircle(_axis, _x, _y, _z, _radius);
  1139. }
  1140. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight)
  1141. {
  1142. s_dd.drawAxis(_x, _y, _z, _len, _hightlight);
  1143. }
  1144. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1145. {
  1146. s_dd.drawGrid(_normal, _center, _size, _step);
  1147. }
  1148. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1149. {
  1150. s_dd.drawGrid(_axis, _center, _size, _step);
  1151. }
  1152. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1153. {
  1154. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1155. }