debugdraw.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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 ? Mesh::SphereMaxLod : attrib.m_lod;
  651. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, attrib.m_wireframe);
  652. }
  653. void draw(const float* _viewProj)
  654. {
  655. Plane planes[6];
  656. buildFrustumPlanes(planes, _viewProj);
  657. float points[24];
  658. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  659. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  660. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  661. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  662. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  663. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  664. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  665. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  666. moveTo(&points[ 0]);
  667. lineTo(&points[ 3]);
  668. lineTo(&points[ 6]);
  669. lineTo(&points[ 9]);
  670. close();
  671. moveTo(&points[12]);
  672. lineTo(&points[15]);
  673. lineTo(&points[18]);
  674. lineTo(&points[21]);
  675. close();
  676. moveTo(&points[ 0]);
  677. lineTo(&points[12]);
  678. moveTo(&points[ 3]);
  679. lineTo(&points[15]);
  680. moveTo(&points[ 6]);
  681. lineTo(&points[18]);
  682. moveTo(&points[ 9]);
  683. lineTo(&points[21]);
  684. }
  685. void draw(const void* _viewProj)
  686. {
  687. draw( (const float*)_viewProj);
  688. }
  689. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  690. {
  691. const Attrib& attrib = m_attrib[m_stack];
  692. const uint32_t num = getCircleLod(attrib.m_lod);
  693. const float step = bx::pi * 2.0f / num;
  694. _degrees = bx::fwrap(_degrees, 360.0f);
  695. float pos[3];
  696. getPoint(pos, _axis
  697. , bx::fsin(step * 0)*_radius
  698. , bx::fcos(step * 0)*_radius
  699. );
  700. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  701. uint32_t n = uint32_t(num*_degrees/360.0f);
  702. for (uint32_t ii = 1; ii < n+1; ++ii)
  703. {
  704. getPoint(pos, _axis
  705. , bx::fsin(step * ii)*_radius
  706. , bx::fcos(step * ii)*_radius
  707. );
  708. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  709. }
  710. moveTo(_x, _y, _z);
  711. getPoint(pos, _axis
  712. , bx::fsin(step * 0)*_radius
  713. , bx::fcos(step * 0)*_radius
  714. );
  715. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  716. getPoint(pos, _axis
  717. , bx::fsin(step * n)*_radius
  718. , bx::fcos(step * n)*_radius
  719. );
  720. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  721. lineTo(_x, _y, _z);
  722. }
  723. void drawCircle(const float* _normal, const float* _center, float _radius)
  724. {
  725. const Attrib& attrib = m_attrib[m_stack];
  726. const uint32_t num = getCircleLod(attrib.m_lod);
  727. const float step = bx::pi * 2.0f / num;
  728. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  729. float udir[3];
  730. float vdir[3];
  731. calcPlaneUv(plane, udir, vdir);
  732. float pos[3];
  733. bx::vec3Add(pos, vdir, _center);
  734. moveTo(pos);
  735. for (uint32_t ii = 1; ii < num; ++ii)
  736. {
  737. float tmp0[3];
  738. float tmp1[3];
  739. bx::vec3Mul(pos, udir, bx::fsin(step * ii)*_radius);
  740. bx::vec3Mul(tmp0, vdir, bx::fcos(step * ii)*_radius);
  741. bx::vec3Add(tmp1, pos, tmp0);
  742. bx::vec3Add(pos, tmp1, _center);
  743. lineTo(pos);
  744. }
  745. }
  746. void drawCircle(const void* _normal, const void* _center, float _radius)
  747. {
  748. drawCircle( (const float*)_normal, (const float*)_center, _radius);
  749. }
  750. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius)
  751. {
  752. const Attrib& attrib = m_attrib[m_stack];
  753. const uint32_t num = getCircleLod(attrib.m_lod);
  754. const float step = bx::pi * 2.0f / num;
  755. float pos[3];
  756. getPoint(pos, _axis
  757. , bx::fsin(step * 0)*_radius
  758. , bx::fcos(step * 0)*_radius
  759. );
  760. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  761. for (uint32_t ii = 1; ii < num; ++ii)
  762. {
  763. getPoint(pos, _axis
  764. , bx::fsin(step * ii)*_radius
  765. , bx::fcos(step * ii)*_radius
  766. );
  767. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  768. }
  769. close();
  770. }
  771. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight)
  772. {
  773. push();
  774. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  775. moveTo(_x, _y, _z);
  776. lineTo(_x + _len, _y, _z);
  777. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  778. moveTo(_x, _y, _z);
  779. lineTo(_x, _y + _len, _z);
  780. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  781. moveTo(_x, _y, _z);
  782. lineTo(_x, _y, _z + _len);
  783. pop();
  784. }
  785. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  786. {
  787. float udir[3];
  788. float vdir[3];
  789. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  790. calcPlaneUv(plane, udir, vdir);
  791. bx::vec3Mul(udir, udir, _step);
  792. bx::vec3Mul(vdir, vdir, _step);
  793. const uint32_t num = (_size/2)*2+1;
  794. const float halfExtent = float(_size/2);
  795. float umin[3];
  796. bx::vec3Mul(umin, udir, -halfExtent);
  797. float umax[3];
  798. bx::vec3Mul(umax, udir, halfExtent);
  799. float vmin[3];
  800. bx::vec3Mul(vmin, vdir, -halfExtent);
  801. float vmax[3];
  802. bx::vec3Mul(vmax, vdir, halfExtent);
  803. float tmp[3];
  804. float xs[3];
  805. float xe[3];
  806. bx::vec3Add(tmp, umin, vmin);
  807. bx::vec3Add(xs, _center, tmp);
  808. bx::vec3Add(tmp, umax, vmin);
  809. bx::vec3Add(xe, _center, tmp);
  810. float ys[3];
  811. float ye[3];
  812. bx::vec3Add(tmp, umin, vmin);
  813. bx::vec3Add(ys, _center, tmp);
  814. bx::vec3Add(tmp, umin, vmax);
  815. bx::vec3Add(ye, _center, tmp);
  816. for (uint32_t ii = 0; ii < num; ++ii)
  817. {
  818. moveTo(xs);
  819. lineTo(xe);
  820. bx::vec3Add(xs, xs, vdir);
  821. bx::vec3Add(xe, xe, vdir);
  822. moveTo(ys);
  823. lineTo(ye);
  824. bx::vec3Add(ys, ys, udir);
  825. bx::vec3Add(ye, ye, udir);
  826. }
  827. }
  828. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  829. {
  830. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  831. }
  832. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  833. {
  834. push();
  835. setTranslate(_center);
  836. const uint32_t num = (_size/2)*2-1;
  837. const float halfExtent = float(_size/2) * _step;
  838. setColor(0xff606060);
  839. float yy = -halfExtent + _step;
  840. for (uint32_t ii = 0; ii < num; ++ii)
  841. {
  842. moveTo(_axis, -halfExtent, yy);
  843. lineTo(_axis, halfExtent, yy);
  844. moveTo(_axis, yy, -halfExtent);
  845. lineTo(_axis, yy, halfExtent);
  846. yy += _step;
  847. }
  848. setColor(0xff101010);
  849. moveTo(_axis, -halfExtent, -halfExtent);
  850. lineTo(_axis, -halfExtent, halfExtent);
  851. lineTo(_axis, halfExtent, halfExtent);
  852. lineTo(_axis, halfExtent, -halfExtent);
  853. close();
  854. moveTo(_axis, -halfExtent, 0.0f);
  855. lineTo(_axis, halfExtent, 0.0f);
  856. moveTo(_axis, 0.0f, -halfExtent);
  857. lineTo(_axis, 0.0f, halfExtent);
  858. pop();
  859. }
  860. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  861. {
  862. drawGrid(_axis, (const float*)_center, _size, _step);
  863. }
  864. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  865. {
  866. push();
  867. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  868. drawCircle(Axis::X, _x, _y, _z, _radius);
  869. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  870. drawCircle(Axis::Y, _x, _y, _z, _radius);
  871. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  872. drawCircle(Axis::Z, _x, _y, _z, _radius);
  873. pop();
  874. }
  875. private:
  876. struct Mesh
  877. {
  878. enum Enum
  879. {
  880. Sphere0,
  881. Sphere1,
  882. Sphere2,
  883. Sphere3,
  884. Cube,
  885. Count,
  886. SphereMaxLod = Sphere3 - Sphere0,
  887. };
  888. uint32_t m_startVertex;
  889. uint32_t m_numVertices;
  890. uint32_t m_startIndex[2];
  891. uint32_t m_numIndices[2];
  892. };
  893. struct Program
  894. {
  895. enum Enum
  896. {
  897. Lines,
  898. LinesStipple,
  899. Fill,
  900. FillLit,
  901. Count
  902. };
  903. };
  904. void draw(Mesh::Enum _mesh, const float* _mtx, bool _wireframe) const
  905. {
  906. const Mesh& mesh = m_mesh[_mesh];
  907. const Attrib& attrib = m_attrib[m_stack];
  908. if (0 != mesh.m_numIndices[_wireframe])
  909. {
  910. bgfx::setIndexBuffer(m_ibh
  911. , mesh.m_startIndex[_wireframe]
  912. , mesh.m_numIndices[_wireframe]
  913. );
  914. }
  915. float params[4][4] =
  916. {
  917. {
  918. 0.0f,
  919. -1.0f,
  920. 0.0f,
  921. 3.0f,
  922. },
  923. {
  924. 1.0f,
  925. 0.9f,
  926. 0.8f,
  927. 0.0f,
  928. },
  929. {
  930. 0.2f,
  931. 0.22f,
  932. 0.5f,
  933. 0.0f,
  934. },
  935. {
  936. ( (attrib.m_abgr>>24) )/255.0f,
  937. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  938. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  939. ( (attrib.m_abgr )&0xff)/255.0f,
  940. },
  941. };
  942. bx::vec3Norm(params[0], params[0]);
  943. bgfx::setUniform(u_params, params, 4);
  944. bgfx::setTransform(_mtx);
  945. bgfx::setVertexBuffer(m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  946. bgfx::setState(0
  947. | BGFX_STATE_RGB_WRITE
  948. | BGFX_STATE_DEPTH_TEST_LESS
  949. | BGFX_STATE_DEPTH_WRITE
  950. | BGFX_STATE_CULL_CW
  951. | (_wireframe ? BGFX_STATE_PT_LINES : 0)
  952. );
  953. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  954. }
  955. void softFlush()
  956. {
  957. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  958. {
  959. flush();
  960. }
  961. }
  962. void flush()
  963. {
  964. if (0 != m_pos)
  965. {
  966. if (bgfx::checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  967. {
  968. bgfx::TransientVertexBuffer tvb;
  969. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  970. memcpy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  971. bgfx::TransientIndexBuffer tib;
  972. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  973. memcpy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  974. bgfx::setVertexBuffer(&tvb);
  975. bgfx::setIndexBuffer(&tib);
  976. bgfx::setState(0
  977. | BGFX_STATE_RGB_WRITE
  978. | BGFX_STATE_PT_LINES
  979. | BGFX_STATE_DEPTH_TEST_LEQUAL
  980. | BGFX_STATE_DEPTH_WRITE
  981. // | BGFX_STATE_BLEND_ALPHA
  982. );
  983. bgfx::setTransform(m_mtx);
  984. bgfx::ProgramHandle program = m_program[m_attrib[m_stack].m_stipple ? 1 : 0];
  985. bgfx::submit(m_viewId, program);
  986. }
  987. m_state = State::None;
  988. m_pos = 0;
  989. m_indexPos = 0;
  990. m_vertexPos = 0;
  991. }
  992. }
  993. struct State
  994. {
  995. enum Enum
  996. {
  997. None,
  998. MoveTo,
  999. LineTo,
  1000. Count
  1001. };
  1002. };
  1003. static const uint32_t cacheSize = 1024;
  1004. static const uint32_t stackSize = 16;
  1005. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1006. DebugVertex m_cache[cacheSize+1];
  1007. uint32_t m_mtx;
  1008. uint16_t m_indices[cacheSize*2];
  1009. uint16_t m_pos;
  1010. uint16_t m_indexPos;
  1011. uint16_t m_vertexPos;
  1012. uint8_t m_viewId;
  1013. uint8_t m_stack;
  1014. struct Attrib
  1015. {
  1016. float m_offset;
  1017. float m_scale;
  1018. uint32_t m_abgr;
  1019. bool m_stipple;
  1020. bool m_wireframe;
  1021. uint8_t m_lod;
  1022. };
  1023. Attrib m_attrib[stackSize];
  1024. State::Enum m_state;
  1025. Mesh m_mesh[Mesh::Count];
  1026. bgfx::ProgramHandle m_program[Program::Count];
  1027. bgfx::UniformHandle u_params;
  1028. bgfx::VertexBufferHandle m_vbh;
  1029. bgfx::IndexBufferHandle m_ibh;
  1030. bx::AllocatorI* m_allocator;
  1031. };
  1032. static DebugDraw s_dd;
  1033. void ddInit(bx::AllocatorI* _allocator)
  1034. {
  1035. s_dd.init(_allocator);
  1036. }
  1037. void ddShutdown()
  1038. {
  1039. s_dd.shutdown();
  1040. }
  1041. void ddBegin(uint8_t _viewId)
  1042. {
  1043. s_dd.begin(_viewId);
  1044. }
  1045. void ddEnd()
  1046. {
  1047. s_dd.end();
  1048. }
  1049. void ddPush()
  1050. {
  1051. s_dd.push();
  1052. }
  1053. void ddPop()
  1054. {
  1055. s_dd.pop();
  1056. }
  1057. void ddSetColor(uint32_t _abgr)
  1058. {
  1059. s_dd.setColor(_abgr);
  1060. }
  1061. void ddSetLod(uint8_t _lod)
  1062. {
  1063. s_dd.setLod(_lod);
  1064. }
  1065. void ddSetWireframe(bool _wireframe)
  1066. {
  1067. s_dd.setWireframe(_wireframe);
  1068. }
  1069. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1070. {
  1071. s_dd.setStipple(_stipple, _scale, _offset);
  1072. }
  1073. void ddSetTransform(const void* _mtx)
  1074. {
  1075. s_dd.setTransform(_mtx);
  1076. }
  1077. void ddSetTranslate(float _x, float _y, float _z)
  1078. {
  1079. s_dd.setTranslate(_x, _y, _z);
  1080. }
  1081. void ddMoveTo(float _x, float _y, float _z)
  1082. {
  1083. s_dd.moveTo(_x, _y, _z);
  1084. }
  1085. void ddMoveTo(const void* _pos)
  1086. {
  1087. s_dd.moveTo(_pos);
  1088. }
  1089. void ddLineTo(float _x, float _y, float _z)
  1090. {
  1091. s_dd.lineTo(_x, _y, _z);
  1092. }
  1093. void ddLineTo(const void* _pos)
  1094. {
  1095. s_dd.lineTo(_pos);
  1096. }
  1097. void ddClose()
  1098. {
  1099. s_dd.close();
  1100. }
  1101. void ddDraw(const Aabb& _aabb)
  1102. {
  1103. s_dd.draw(_aabb);
  1104. }
  1105. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1106. {
  1107. s_dd.draw(_cylinder, _capsule);
  1108. }
  1109. void ddDraw(const Disk& _disk)
  1110. {
  1111. s_dd.draw(_disk);
  1112. }
  1113. void ddDraw(const Obb& _obb)
  1114. {
  1115. s_dd.draw(_obb);
  1116. }
  1117. void ddDraw(const Sphere& _sphere)
  1118. {
  1119. s_dd.draw(_sphere);
  1120. }
  1121. void ddDraw(const void* _viewProj)
  1122. {
  1123. s_dd.draw(_viewProj);
  1124. }
  1125. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1126. {
  1127. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1128. }
  1129. void ddDrawCircle(const void* _normal, const void* _center, float _radius)
  1130. {
  1131. s_dd.drawCircle(_normal, _center, _radius);
  1132. }
  1133. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius)
  1134. {
  1135. s_dd.drawCircle(_axis, _x, _y, _z, _radius);
  1136. }
  1137. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight)
  1138. {
  1139. s_dd.drawAxis(_x, _y, _z, _len, _hightlight);
  1140. }
  1141. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1142. {
  1143. s_dd.drawGrid(_normal, _center, _size, _step);
  1144. }
  1145. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1146. {
  1147. s_dd.drawGrid(_axis, _center, _size, _step);
  1148. }
  1149. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1150. {
  1151. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1152. }