debugdraw.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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. uint8_t m_indices[4];
  36. static void init()
  37. {
  38. ms_decl
  39. .begin()
  40. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  41. .add(bgfx::Attrib::Indices, 4, bgfx::AttribType::Uint8)
  42. .end();
  43. }
  44. static bgfx::VertexDecl ms_decl;
  45. };
  46. bgfx::VertexDecl DebugShapeVertex::ms_decl;
  47. static DebugShapeVertex s_cubeVertices[8] =
  48. {
  49. {-1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
  50. { 1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
  51. {-1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
  52. { 1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
  53. {-1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
  54. { 1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
  55. {-1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
  56. { 1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
  57. };
  58. static const uint16_t s_cubeIndices[36] =
  59. {
  60. 0, 1, 2, // 0
  61. 1, 3, 2,
  62. 4, 6, 5, // 2
  63. 5, 6, 7,
  64. 0, 2, 4, // 4
  65. 4, 2, 6,
  66. 1, 5, 3, // 6
  67. 5, 7, 3,
  68. 0, 4, 1, // 8
  69. 4, 5, 1,
  70. 2, 3, 6, // 10
  71. 6, 3, 7,
  72. };
  73. static const uint8_t s_circleLod[] =
  74. {
  75. 37,
  76. 29,
  77. 23,
  78. 17,
  79. 11,
  80. };
  81. static uint8_t getCircleLod(uint8_t _lod)
  82. {
  83. _lod = _lod > BX_COUNTOF(s_circleLod)-1 ? BX_COUNTOF(s_circleLod)-1 : _lod;
  84. return s_circleLod[_lod];
  85. }
  86. static void circle(float* _out, float _angle)
  87. {
  88. float sa = bx::fsin(_angle);
  89. float ca = bx::fcos(_angle);
  90. _out[0] = sa;
  91. _out[1] = ca;
  92. }
  93. static void squircle(float* _out, float _angle)
  94. {
  95. float sa = bx::fsin(_angle);
  96. float ca = bx::fcos(_angle);
  97. _out[0] = bx::fsqrt(bx::fabsolute(sa) ) * bx::fsign(sa);
  98. _out[1] = bx::fsqrt(bx::fabsolute(ca) ) * bx::fsign(ca);
  99. }
  100. uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = 0, void* _normals0 = NULL, uint16_t _normalStride0 = 0)
  101. {
  102. if (NULL != _pos0)
  103. {
  104. struct Gen
  105. {
  106. Gen(void* _pos, uint16_t _posStride, void* _normals, uint16_t _normalStride, uint8_t _subdiv)
  107. : m_pos( (uint8_t*)_pos)
  108. , m_normals( (uint8_t*)_normals)
  109. , m_posStride(_posStride)
  110. , m_normalStride(_normalStride)
  111. {
  112. static const float scale = 1.0f;
  113. static const float golden = 1.6180339887f;
  114. static const float len = bx::fsqrt(golden*golden + 1.0f);
  115. static const float ss = 1.0f/len * scale;
  116. static const float ll = ss*golden;
  117. static const float vv[12][4] =
  118. {
  119. { -ll, 0.0f, -ss, 0.0f },
  120. { ll, 0.0f, -ss, 0.0f },
  121. { ll, 0.0f, ss, 0.0f },
  122. { -ll, 0.0f, ss, 0.0f },
  123. { -ss, ll, 0.0f, 0.0f },
  124. { ss, ll, 0.0f, 0.0f },
  125. { ss, -ll, 0.0f, 0.0f },
  126. { -ss, -ll, 0.0f, 0.0f },
  127. { 0.0f, -ss, ll, 0.0f },
  128. { 0.0f, ss, ll, 0.0f },
  129. { 0.0f, ss, -ll, 0.0f },
  130. { 0.0f, -ss, -ll, 0.0f },
  131. };
  132. m_numVertices = 0;
  133. triangle(vv[ 0], vv[ 4], vv[ 3], scale, _subdiv);
  134. triangle(vv[ 0], vv[10], vv[ 4], scale, _subdiv);
  135. triangle(vv[ 4], vv[10], vv[ 5], scale, _subdiv);
  136. triangle(vv[ 5], vv[10], vv[ 1], scale, _subdiv);
  137. triangle(vv[ 5], vv[ 1], vv[ 2], scale, _subdiv);
  138. triangle(vv[ 5], vv[ 2], vv[ 9], scale, _subdiv);
  139. triangle(vv[ 5], vv[ 9], vv[ 4], scale, _subdiv);
  140. triangle(vv[ 3], vv[ 4], vv[ 9], scale, _subdiv);
  141. triangle(vv[ 0], vv[ 3], vv[ 7], scale, _subdiv);
  142. triangle(vv[ 0], vv[ 7], vv[11], scale, _subdiv);
  143. triangle(vv[11], vv[ 7], vv[ 6], scale, _subdiv);
  144. triangle(vv[11], vv[ 6], vv[ 1], scale, _subdiv);
  145. triangle(vv[ 1], vv[ 6], vv[ 2], scale, _subdiv);
  146. triangle(vv[ 2], vv[ 6], vv[ 8], scale, _subdiv);
  147. triangle(vv[ 8], vv[ 6], vv[ 7], scale, _subdiv);
  148. triangle(vv[ 8], vv[ 7], vv[ 3], scale, _subdiv);
  149. triangle(vv[ 0], vv[11], vv[10], scale, _subdiv);
  150. triangle(vv[ 1], vv[10], vv[11], scale, _subdiv);
  151. triangle(vv[ 2], vv[ 8], vv[ 9], scale, _subdiv);
  152. triangle(vv[ 3], vv[ 9], vv[ 8], scale, _subdiv);
  153. }
  154. void addVert(const float* _v)
  155. {
  156. float* verts = (float*)m_pos;
  157. verts[0] = _v[0];
  158. verts[1] = _v[1];
  159. verts[2] = _v[2];
  160. m_pos += m_posStride;
  161. if (NULL != m_normals)
  162. {
  163. float* normals = (float*)m_normals;
  164. bx::vec3Norm(normals, _v);
  165. m_normals += m_normalStride;
  166. }
  167. m_numVertices++;
  168. }
  169. void triangle(const float* _v0, const float* _v1, const float* _v2, float _scale, uint8_t _subdiv)
  170. {
  171. if (0 == _subdiv)
  172. {
  173. addVert(_v0);
  174. addVert(_v1);
  175. addVert(_v2);
  176. }
  177. else
  178. {
  179. float tmp0[4];
  180. float tmp1[4];
  181. float v01[4];
  182. bx::vec3Add(tmp0, _v0, _v1);
  183. bx::vec3Norm(tmp1, tmp0);
  184. bx::vec3Mul(v01, tmp1, _scale);
  185. float v12[4];
  186. bx::vec3Add(tmp0, _v1, _v2);
  187. bx::vec3Norm(tmp1, tmp0);
  188. bx::vec3Mul(v12, tmp1, _scale);
  189. float v20[4];
  190. bx::vec3Add(tmp0, _v2, _v0);
  191. bx::vec3Norm(tmp1, tmp0);
  192. bx::vec3Mul(v20, tmp1, _scale);
  193. --_subdiv;
  194. triangle(_v0, v01, v20, _scale, _subdiv);
  195. triangle(_v1, v12, v01, _scale, _subdiv);
  196. triangle(_v2, v20, v12, _scale, _subdiv);
  197. triangle(v01, v12, v20, _scale, _subdiv);
  198. }
  199. }
  200. uint8_t* m_pos;
  201. uint8_t* m_normals;
  202. uint16_t m_posStride;
  203. uint16_t m_normalStride;
  204. uint32_t m_numVertices;
  205. } gen(_pos0, _posStride0, _normals0, _normalStride0, _subdiv0);
  206. }
  207. uint32_t numVertices = 20*3*bx::uint32_max(1, (uint32_t)bx::fpow(4.0f, _subdiv0) );
  208. return numVertices;
  209. }
  210. void getPoint(float* _result, Axis::Enum _axis, float _x, float _y)
  211. {
  212. switch (_axis)
  213. {
  214. case Axis::X:
  215. _result[0] = 0.0f;
  216. _result[1] = _x;
  217. _result[2] = _y;
  218. break;
  219. case Axis::Y:
  220. _result[0] = _y;
  221. _result[1] = 0.0f;
  222. _result[2] = _x;
  223. break;
  224. default:
  225. _result[0] = _x;
  226. _result[1] = _y;
  227. _result[2] = 0.0f;
  228. break;
  229. }
  230. }
  231. #include "vs_debugdraw_lines.bin.h"
  232. #include "fs_debugdraw_lines.bin.h"
  233. #include "vs_debugdraw_lines_stipple.bin.h"
  234. #include "fs_debugdraw_lines_stipple.bin.h"
  235. #include "vs_debugdraw_fill.bin.h"
  236. #include "fs_debugdraw_fill.bin.h"
  237. #include "vs_debugdraw_fill_lit.bin.h"
  238. #include "fs_debugdraw_fill_lit.bin.h"
  239. struct EmbeddedShader
  240. {
  241. bgfx::RendererType::Enum type;
  242. const uint8_t* data;
  243. uint32_t size;
  244. };
  245. #define BGFX_DECLARE_SHADER_EMBEDDED(_name) \
  246. { \
  247. { bgfx::RendererType::Direct3D9, BX_CONCATENATE(_name, _dx9 ), sizeof(BX_CONCATENATE(_name, _dx9 ) ) }, \
  248. { bgfx::RendererType::Direct3D11, BX_CONCATENATE(_name, _dx11), sizeof(BX_CONCATENATE(_name, _dx11) ) }, \
  249. { bgfx::RendererType::Direct3D12, BX_CONCATENATE(_name, _dx11), sizeof(BX_CONCATENATE(_name, _dx11) ) }, \
  250. { bgfx::RendererType::OpenGL, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  251. { bgfx::RendererType::OpenGLES, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  252. { bgfx::RendererType::Vulkan, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  253. { bgfx::RendererType::Metal, BX_CONCATENATE(_name, _mtl ), sizeof(BX_CONCATENATE(_name, _mtl ) ) }, \
  254. { bgfx::RendererType::Count, NULL, 0 }, \
  255. }
  256. static const EmbeddedShader s_embeddedShaders[][8] =
  257. {
  258. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_lines),
  259. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_lines),
  260. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_lines_stipple),
  261. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_lines_stipple),
  262. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_fill),
  263. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_fill),
  264. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_fill_lit),
  265. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_fill_lit),
  266. };
  267. static bgfx::ShaderHandle createEmbeddedShader(bgfx::RendererType::Enum _type, uint32_t _index)
  268. {
  269. for (const EmbeddedShader* es = s_embeddedShaders[_index]; bgfx::RendererType::Count != es->type; ++es)
  270. {
  271. if (_type == es->type)
  272. {
  273. return bgfx::createShader(bgfx::makeRef(es->data, es->size) );
  274. }
  275. }
  276. bgfx::ShaderHandle handle = BGFX_INVALID_HANDLE;
  277. return handle;
  278. }
  279. struct DebugDraw
  280. {
  281. DebugDraw()
  282. : m_depthTestLess(true)
  283. , m_state(State::Count)
  284. {
  285. }
  286. void init(bool _depthTestLess, bx::AllocatorI* _allocator)
  287. {
  288. m_allocator = _allocator;
  289. m_depthTestLess = _depthTestLess;
  290. #if BX_CONFIG_ALLOCATOR_CRT
  291. if (NULL == _allocator)
  292. {
  293. static bx::CrtAllocator allocator;
  294. m_allocator = &allocator;
  295. }
  296. #endif // BX_CONFIG_ALLOCATOR_CRT
  297. DebugVertex::init();
  298. DebugShapeVertex::init();
  299. bgfx::RendererType::Enum type = bgfx::getRendererType();
  300. m_program[Program::Lines] =
  301. bgfx::createProgram(createEmbeddedShader(type, 0)
  302. , createEmbeddedShader(type, 1)
  303. , true
  304. );
  305. m_program[Program::LinesStipple] =
  306. bgfx::createProgram(createEmbeddedShader(type, 2)
  307. , createEmbeddedShader(type, 3)
  308. , true
  309. );
  310. m_program[Program::Fill] =
  311. bgfx::createProgram(createEmbeddedShader(type, 4)
  312. , createEmbeddedShader(type, 5)
  313. , true
  314. );
  315. m_program[Program::FillLit] =
  316. bgfx::createProgram(createEmbeddedShader(type, 6)
  317. , createEmbeddedShader(type, 7)
  318. , true
  319. );
  320. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 4);
  321. void* vertices[Mesh::Count] = {};
  322. uint16_t* indices[Mesh::Count] = {};
  323. uint16_t stride = DebugShapeVertex::ms_decl.getStride();
  324. uint32_t startVertex = 0;
  325. uint32_t startIndex = 0;
  326. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  327. {
  328. Mesh::Enum id = Mesh::Enum(Mesh::Sphere0+mesh);
  329. const uint8_t tess = uint8_t(3-mesh);
  330. const uint32_t numVertices = genSphere(tess);
  331. const uint32_t numIndices = numVertices;
  332. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  333. memset(vertices[id], 0, numVertices*stride);
  334. genSphere(tess, vertices[id], stride);
  335. uint16_t* trilist = (uint16_t*)BX_ALLOC(m_allocator, numIndices*sizeof(uint16_t) );
  336. for (uint32_t ii = 0; ii < numIndices; ++ii)
  337. {
  338. trilist[ii] = uint16_t(ii);
  339. }
  340. uint32_t numLineListIndices = bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  341. , NULL
  342. , 0
  343. , trilist
  344. , numIndices
  345. , false
  346. );
  347. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  348. uint16_t* indicesOut = indices[id];
  349. memcpy(indicesOut, trilist, numIndices*sizeof(uint16_t) );
  350. bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  351. , &indicesOut[numIndices]
  352. , numLineListIndices*sizeof(uint16_t)
  353. , trilist
  354. , numIndices
  355. , false
  356. );
  357. m_mesh[id].m_startVertex = startVertex;
  358. m_mesh[id].m_numVertices = numVertices;
  359. m_mesh[id].m_startIndex[0] = startIndex;
  360. m_mesh[id].m_numIndices[0] = numIndices;
  361. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  362. m_mesh[id].m_numIndices[1] = numLineListIndices;
  363. startVertex += numVertices;
  364. startIndex += numIndices + numLineListIndices;
  365. BX_FREE(m_allocator, trilist);
  366. }
  367. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  368. {
  369. Mesh::Enum id = Mesh::Enum(Mesh::Cone0+mesh);
  370. const uint32_t num = getCircleLod(uint8_t(mesh) );
  371. const float step = bx::pi * 2.0f / num;
  372. const uint32_t numVertices = num+1;
  373. const uint32_t numIndices = num*6;
  374. const uint32_t numLineListIndices = num*4;
  375. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  376. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  377. memset(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  378. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  379. uint16_t* index = indices[id];
  380. vertex[num].m_x = 0.0f;
  381. vertex[num].m_y = 0.0f;
  382. vertex[num].m_z = 0.0f;
  383. vertex[num].m_indices[0] = 1;
  384. for (uint32_t ii = 0; ii < num; ++ii)
  385. {
  386. const float angle = step * ii;
  387. float xy[2];
  388. circle(xy, angle);
  389. vertex[ii].m_x = xy[1];
  390. vertex[ii].m_y = 0.0f;
  391. vertex[ii].m_z = xy[0];
  392. vertex[ii].m_indices[0] = 0;
  393. index[ii*3+0] = uint16_t(num);
  394. index[ii*3+1] = uint16_t( (ii+1)%num);
  395. index[ii*3+2] = uint16_t(ii);
  396. index[num*3+ii*3+0] = 0;
  397. index[num*3+ii*3+1] = uint16_t(ii);
  398. index[num*3+ii*3+2] = uint16_t( (ii+1)%num);
  399. index[numIndices+ii*2+0] = uint16_t(ii);
  400. index[numIndices+ii*2+1] = uint16_t(num);
  401. index[numIndices+num*2+ii*2+0] = uint16_t(ii);
  402. index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
  403. }
  404. m_mesh[id].m_startVertex = startVertex;
  405. m_mesh[id].m_numVertices = numVertices;
  406. m_mesh[id].m_startIndex[0] = startIndex;
  407. m_mesh[id].m_numIndices[0] = numIndices;
  408. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  409. m_mesh[id].m_numIndices[1] = numLineListIndices;
  410. startVertex += numVertices;
  411. startIndex += numIndices + numLineListIndices;
  412. }
  413. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  414. {
  415. Mesh::Enum id = Mesh::Enum(Mesh::Cylinder0+mesh);
  416. const uint32_t num = getCircleLod(uint8_t(mesh) );
  417. const float step = bx::pi * 2.0f / num;
  418. const uint32_t numVertices = num*2;
  419. const uint32_t numIndices = num*12;
  420. const uint32_t numLineListIndices = num*6;
  421. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  422. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  423. memset(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  424. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  425. uint16_t* index = indices[id];
  426. for (uint32_t ii = 0; ii < num; ++ii)
  427. {
  428. const float angle = step * ii;
  429. float xy[2];
  430. circle(xy, angle);
  431. vertex[ii].m_x = xy[1];
  432. vertex[ii].m_y = 0.0f;
  433. vertex[ii].m_z = xy[0];
  434. vertex[ii].m_indices[0] = 0;
  435. vertex[ii+num].m_x = xy[1];
  436. vertex[ii+num].m_y = 0.0f;
  437. vertex[ii+num].m_z = xy[0];
  438. vertex[ii+num].m_indices[0] = 1;
  439. index[ii*6+0] = uint16_t(ii+num);
  440. index[ii*6+1] = uint16_t( (ii+1)%num);
  441. index[ii*6+2] = uint16_t(ii);
  442. index[ii*6+3] = uint16_t(ii+num);
  443. index[ii*6+4] = uint16_t( (ii+1)%num+num);
  444. index[ii*6+5] = uint16_t( (ii+1)%num);
  445. index[num*6+ii*6+0] = uint16_t(0);
  446. index[num*6+ii*6+1] = uint16_t(ii);
  447. index[num*6+ii*6+2] = uint16_t( (ii+1)%num);
  448. index[num*6+ii*6+3] = uint16_t(num);
  449. index[num*6+ii*6+4] = uint16_t( (ii+1)%num+num);
  450. index[num*6+ii*6+5] = uint16_t(ii+num);
  451. index[numIndices+ii*2+0] = uint16_t(ii);
  452. index[numIndices+ii*2+1] = uint16_t(ii+num);
  453. index[numIndices+num*2+ii*2+0] = uint16_t(ii);
  454. index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
  455. index[numIndices+num*4+ii*2+0] = uint16_t(num + ii);
  456. index[numIndices+num*4+ii*2+1] = uint16_t(num + (ii+1)%num);
  457. }
  458. m_mesh[id].m_startVertex = startVertex;
  459. m_mesh[id].m_numVertices = numVertices;
  460. m_mesh[id].m_startIndex[0] = startIndex;
  461. m_mesh[id].m_numIndices[0] = numIndices;
  462. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  463. m_mesh[id].m_numIndices[1] = numLineListIndices;
  464. startVertex += numVertices;
  465. startIndex += numIndices + numLineListIndices;
  466. }
  467. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  468. {
  469. Mesh::Enum id = Mesh::Enum(Mesh::Capsule0+mesh);
  470. const uint32_t num = getCircleLod(uint8_t(mesh) );
  471. const float step = bx::pi * 2.0f / num;
  472. const uint32_t numVertices = num*2;
  473. const uint32_t numIndices = num*6;
  474. const uint32_t numLineListIndices = num*6;
  475. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  476. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  477. memset(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  478. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  479. uint16_t* index = indices[id];
  480. for (uint32_t ii = 0; ii < num; ++ii)
  481. {
  482. const float angle = step * ii;
  483. float xy[2];
  484. circle(xy, angle);
  485. vertex[ii].m_x = xy[1];
  486. vertex[ii].m_y = 0.0f;
  487. vertex[ii].m_z = xy[0];
  488. vertex[ii].m_indices[0] = 0;
  489. vertex[ii+num].m_x = xy[1];
  490. vertex[ii+num].m_y = 0.0f;
  491. vertex[ii+num].m_z = xy[0];
  492. vertex[ii+num].m_indices[0] = 1;
  493. index[ii*6+0] = uint16_t(ii+num);
  494. index[ii*6+1] = uint16_t( (ii+1)%num);
  495. index[ii*6+2] = uint16_t(ii);
  496. index[ii*6+3] = uint16_t(ii+num);
  497. index[ii*6+4] = uint16_t( (ii+1)%num+num);
  498. index[ii*6+5] = uint16_t( (ii+1)%num);
  499. // index[num*6+ii*6+0] = uint16_t(0);
  500. // index[num*6+ii*6+1] = uint16_t(ii);
  501. // index[num*6+ii*6+2] = uint16_t( (ii+1)%num);
  502. // index[num*6+ii*6+3] = uint16_t(num);
  503. // index[num*6+ii*6+4] = uint16_t( (ii+1)%num+num);
  504. // index[num*6+ii*6+5] = uint16_t(ii+num);
  505. index[numIndices+ii*2+0] = uint16_t(ii);
  506. index[numIndices+ii*2+1] = uint16_t(ii+num);
  507. index[numIndices+num*2+ii*2+0] = uint16_t(ii);
  508. index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
  509. index[numIndices+num*4+ii*2+0] = uint16_t(num + ii);
  510. index[numIndices+num*4+ii*2+1] = uint16_t(num + (ii+1)%num);
  511. }
  512. m_mesh[id].m_startVertex = startVertex;
  513. m_mesh[id].m_numVertices = numVertices;
  514. m_mesh[id].m_startIndex[0] = startIndex;
  515. m_mesh[id].m_numIndices[0] = numIndices;
  516. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  517. m_mesh[id].m_numIndices[1] = numLineListIndices;
  518. startVertex += numVertices;
  519. startIndex += numIndices + numLineListIndices;
  520. }
  521. m_mesh[Mesh::Cube].m_startVertex = startVertex;
  522. m_mesh[Mesh::Cube].m_numVertices = BX_COUNTOF(s_cubeVertices);
  523. m_mesh[Mesh::Cube].m_startIndex[0] = startIndex;
  524. m_mesh[Mesh::Cube].m_numIndices[0] = BX_COUNTOF(s_cubeIndices);
  525. m_mesh[Mesh::Cube].m_startIndex[1] = 0;
  526. m_mesh[Mesh::Cube].m_numIndices[1] = 0;
  527. startVertex += m_mesh[Mesh::Cube].m_numVertices;
  528. startIndex += m_mesh[Mesh::Cube].m_numIndices[0];
  529. const bgfx::Memory* vb = bgfx::alloc(startVertex*stride);
  530. const bgfx::Memory* ib = bgfx::alloc(startIndex*sizeof(uint16_t) );
  531. for (uint32_t mesh = Mesh::Sphere0; mesh < Mesh::Cube; ++mesh)
  532. {
  533. Mesh::Enum id = Mesh::Enum(mesh);
  534. memcpy(&vb->data[m_mesh[id].m_startVertex * stride]
  535. , vertices[id]
  536. , m_mesh[id].m_numVertices*stride
  537. );
  538. memcpy(&ib->data[m_mesh[id].m_startIndex[0] * sizeof(uint16_t)]
  539. , indices[id]
  540. , (m_mesh[id].m_numIndices[0]+m_mesh[id].m_numIndices[1])*sizeof(uint16_t)
  541. );
  542. BX_FREE(m_allocator, vertices[id]);
  543. BX_FREE(m_allocator, indices[id]);
  544. }
  545. memcpy(&vb->data[m_mesh[Mesh::Cube].m_startVertex * stride]
  546. , s_cubeVertices
  547. , sizeof(s_cubeVertices)
  548. );
  549. memcpy(&ib->data[m_mesh[Mesh::Cube].m_startIndex[0] * sizeof(uint16_t)]
  550. , s_cubeIndices
  551. , sizeof(s_cubeIndices)
  552. );
  553. m_vbh = bgfx::createVertexBuffer(vb, DebugShapeVertex::ms_decl);
  554. m_ibh = bgfx::createIndexBuffer(ib);
  555. m_mtx = 0;
  556. m_viewId = 0;
  557. m_pos = 0;
  558. m_indexPos = 0;
  559. m_vertexPos = 0;
  560. }
  561. void shutdown()
  562. {
  563. bgfx::destroyIndexBuffer(m_ibh);
  564. bgfx::destroyVertexBuffer(m_vbh);
  565. for (uint32_t ii = 0; ii < Program::Count; ++ii)
  566. {
  567. bgfx::destroyProgram(m_program[ii]);
  568. }
  569. bgfx::destroyUniform(u_params);
  570. }
  571. void begin(uint8_t _viewId)
  572. {
  573. BX_CHECK(State::Count == m_state);
  574. m_viewId = _viewId;
  575. m_mtx = 0;
  576. m_state = State::None;
  577. m_stack = 0;
  578. Attrib& attrib = m_attrib[0];
  579. attrib.m_state = 0
  580. | BGFX_STATE_RGB_WRITE
  581. | (m_depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER)
  582. | BGFX_STATE_CULL_CW
  583. | BGFX_STATE_DEPTH_WRITE
  584. ;
  585. attrib.m_scale = 1.0f;
  586. attrib.m_offset = 0.0f;
  587. attrib.m_abgr = UINT32_MAX;
  588. attrib.m_stipple = false;
  589. attrib.m_wireframe = false;
  590. attrib.m_lod = 0;
  591. }
  592. void end()
  593. {
  594. BX_CHECK(0 == m_stack, "Invalid stack %d.", m_stack);
  595. flush();
  596. m_state = State::Count;
  597. }
  598. void push()
  599. {
  600. BX_CHECK(State::Count != m_state);
  601. ++m_stack;
  602. m_attrib[m_stack] = m_attrib[m_stack-1];
  603. }
  604. void pop()
  605. {
  606. BX_CHECK(State::Count != m_state);
  607. const Attrib& curr = m_attrib[m_stack];
  608. const Attrib& prev = m_attrib[m_stack-1];
  609. if (curr.m_stipple != prev.m_stipple
  610. || curr.m_state != prev.m_state)
  611. {
  612. flush();
  613. }
  614. --m_stack;
  615. }
  616. void setTransform(const void* _mtx)
  617. {
  618. BX_CHECK(State::Count != m_state);
  619. flush();
  620. if (NULL == _mtx)
  621. {
  622. m_mtx = 0;
  623. return;
  624. }
  625. bgfx::Transform transform;
  626. m_mtx = bgfx::allocTransform(&transform, 1);
  627. memcpy(transform.data, _mtx, 64);
  628. }
  629. void setTranslate(float _x, float _y, float _z)
  630. {
  631. float mtx[16];
  632. bx::mtxTranslate(mtx, _x, _y, _z);
  633. setTransform(mtx);
  634. }
  635. void setTranslate(const float* _pos)
  636. {
  637. setTranslate(_pos[0], _pos[1], _pos[2]);
  638. }
  639. void setState(bool _depthTest, bool _depthWrite, bool _clockwise)
  640. {
  641. const uint64_t depthTest = m_depthTestLess
  642. ? BGFX_STATE_DEPTH_TEST_LESS
  643. : BGFX_STATE_DEPTH_TEST_GREATER
  644. ;
  645. m_attrib[m_stack].m_state &= ~(0
  646. | BGFX_STATE_DEPTH_TEST_MASK
  647. | BGFX_STATE_DEPTH_WRITE
  648. | BGFX_STATE_CULL_CW
  649. | BGFX_STATE_CULL_CCW
  650. );
  651. m_attrib[m_stack].m_state |= _depthTest
  652. ? depthTest
  653. : 0
  654. ;
  655. m_attrib[m_stack].m_state |= _depthWrite
  656. ? BGFX_STATE_DEPTH_WRITE
  657. : 0
  658. ;
  659. m_attrib[m_stack].m_state |= _clockwise
  660. ? BGFX_STATE_CULL_CW
  661. : BGFX_STATE_CULL_CCW
  662. ;
  663. }
  664. void setColor(uint32_t _abgr)
  665. {
  666. BX_CHECK(State::Count != m_state);
  667. m_attrib[m_stack].m_abgr = _abgr;
  668. }
  669. void setLod(uint8_t _lod)
  670. {
  671. BX_CHECK(State::Count != m_state);
  672. m_attrib[m_stack].m_lod = _lod;
  673. }
  674. void setWireframe(bool _wireframe)
  675. {
  676. BX_CHECK(State::Count != m_state);
  677. m_attrib[m_stack].m_wireframe = _wireframe;
  678. }
  679. void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
  680. {
  681. BX_CHECK(State::Count != m_state);
  682. Attrib& attrib = m_attrib[m_stack];
  683. if (attrib.m_stipple != _stipple)
  684. {
  685. flush();
  686. }
  687. attrib.m_stipple = _stipple;
  688. attrib.m_offset = _offset;
  689. attrib.m_scale = _scale;
  690. }
  691. void moveTo(float _x, float _y, float _z = 0.0f)
  692. {
  693. BX_CHECK(State::Count != m_state);
  694. softFlush();
  695. m_state = State::MoveTo;
  696. DebugVertex& vertex = m_cache[m_pos];
  697. vertex.m_x = _x;
  698. vertex.m_y = _y;
  699. vertex.m_z = _z;
  700. Attrib& attrib = m_attrib[m_stack];
  701. vertex.m_abgr = attrib.m_abgr;
  702. vertex.m_len = attrib.m_offset;
  703. m_vertexPos = m_pos;
  704. }
  705. void moveTo(const void* _pos)
  706. {
  707. BX_CHECK(State::Count != m_state);
  708. const float* pos = (const float*)_pos;
  709. moveTo(pos[0], pos[1], pos[2]);
  710. }
  711. void moveTo(Axis::Enum _axis, float _x, float _y)
  712. {
  713. float pos[3];
  714. getPoint(pos, _axis, _x, _y);
  715. moveTo(pos);
  716. }
  717. void lineTo(float _x, float _y, float _z = 0.0f)
  718. {
  719. BX_CHECK(State::Count != m_state);
  720. if (State::None == m_state)
  721. {
  722. moveTo(_x, _y, _z);
  723. return;
  724. }
  725. if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
  726. {
  727. uint32_t pos = m_pos;
  728. uint32_t vertexPos = m_vertexPos;
  729. flush();
  730. memcpy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
  731. if (vertexPos == pos)
  732. {
  733. m_pos = 1;
  734. }
  735. else
  736. {
  737. memcpy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
  738. m_pos = 2;
  739. }
  740. m_state = State::LineTo;
  741. }
  742. else if (State::MoveTo == m_state)
  743. {
  744. ++m_pos;
  745. m_state = State::LineTo;
  746. }
  747. uint16_t prev = m_pos-1;
  748. uint16_t curr = m_pos++;
  749. DebugVertex& vertex = m_cache[curr];
  750. vertex.m_x = _x;
  751. vertex.m_y = _y;
  752. vertex.m_z = _z;
  753. Attrib& attrib = m_attrib[m_stack];
  754. vertex.m_abgr = attrib.m_abgr;
  755. vertex.m_len = attrib.m_offset;
  756. float tmp[3];
  757. bx::vec3Sub(tmp, &vertex.m_x, &m_cache[prev].m_x);
  758. float len = bx::vec3Length(tmp) * attrib.m_scale;
  759. vertex.m_len = m_cache[prev].m_len + len;
  760. m_indices[m_indexPos++] = prev;
  761. m_indices[m_indexPos++] = curr;
  762. }
  763. void lineTo(const void* _pos)
  764. {
  765. BX_CHECK(State::Count != m_state);
  766. const float* pos = (const float*)_pos;
  767. lineTo(pos[0], pos[1], pos[2]);
  768. }
  769. void lineTo(Axis::Enum _axis, float _x, float _y)
  770. {
  771. float pos[3];
  772. getPoint(pos, _axis, _x, _y);
  773. lineTo(pos);
  774. }
  775. void close()
  776. {
  777. BX_CHECK(State::Count != m_state);
  778. DebugVertex& vertex = m_cache[m_vertexPos];
  779. lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
  780. m_state = State::None;
  781. }
  782. void draw(const Aabb& _aabb)
  783. {
  784. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  785. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  786. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  787. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  788. close();
  789. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  790. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  791. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  792. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  793. close();
  794. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  795. lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  796. moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  797. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  798. moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  799. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  800. moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  801. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  802. }
  803. void draw(const Cylinder& _cylinder, bool _capsule)
  804. {
  805. drawCylinder(_cylinder.m_pos, _cylinder.m_end, _cylinder.m_radius, _capsule);
  806. }
  807. void draw(const Disk& _disk)
  808. {
  809. BX_UNUSED(_disk);
  810. }
  811. void draw(const Obb& _obb)
  812. {
  813. const Attrib& attrib = m_attrib[m_stack];
  814. if (attrib.m_wireframe)
  815. {
  816. setTransform(_obb.m_mtx);
  817. moveTo(-1.0f, -1.0f, -1.0f);
  818. lineTo( 1.0f, -1.0f, -1.0f);
  819. lineTo( 1.0f, 1.0f, -1.0f);
  820. lineTo(-1.0f, 1.0f, -1.0f);
  821. close();
  822. moveTo(-1.0f, 1.0f, 1.0f);
  823. lineTo( 1.0f, 1.0f, 1.0f);
  824. lineTo( 1.0f, -1.0f, 1.0f);
  825. lineTo(-1.0f, -1.0f, 1.0f);
  826. close();
  827. moveTo( 1.0f, -1.0f, -1.0f);
  828. lineTo( 1.0f, -1.0f, 1.0f);
  829. moveTo( 1.0f, 1.0f, -1.0f);
  830. lineTo( 1.0f, 1.0f, 1.0f);
  831. moveTo(-1.0f, 1.0f, -1.0f);
  832. lineTo(-1.0f, 1.0f, 1.0f);
  833. moveTo(-1.0f, -1.0f, -1.0f);
  834. lineTo(-1.0f, -1.0f, 1.0f);
  835. setTransform(NULL);
  836. }
  837. else
  838. {
  839. draw(Mesh::Cube, _obb.m_mtx, 1, false);
  840. }
  841. }
  842. void draw(const Sphere& _sphere)
  843. {
  844. const Attrib& attrib = m_attrib[m_stack];
  845. float mtx[16];
  846. bx::mtxSRT(mtx
  847. , _sphere.m_radius
  848. , _sphere.m_radius
  849. , _sphere.m_radius
  850. , 0.0f
  851. , 0.0f
  852. , 0.0f
  853. , _sphere.m_center[0]
  854. , _sphere.m_center[1]
  855. , _sphere.m_center[2]
  856. );
  857. uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
  858. ? uint8_t(Mesh::SphereMaxLod)
  859. : attrib.m_lod
  860. ;
  861. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe);
  862. }
  863. void drawFrustum(const float* _viewProj)
  864. {
  865. Plane planes[6];
  866. buildFrustumPlanes(planes, _viewProj);
  867. float points[24];
  868. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  869. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  870. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  871. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  872. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  873. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  874. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  875. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  876. moveTo(&points[ 0]);
  877. lineTo(&points[ 3]);
  878. lineTo(&points[ 6]);
  879. lineTo(&points[ 9]);
  880. close();
  881. moveTo(&points[12]);
  882. lineTo(&points[15]);
  883. lineTo(&points[18]);
  884. lineTo(&points[21]);
  885. close();
  886. moveTo(&points[ 0]);
  887. lineTo(&points[12]);
  888. moveTo(&points[ 3]);
  889. lineTo(&points[15]);
  890. moveTo(&points[ 6]);
  891. lineTo(&points[18]);
  892. moveTo(&points[ 9]);
  893. lineTo(&points[21]);
  894. }
  895. void drawFrustum(const void* _viewProj)
  896. {
  897. drawFrustum( (const float*)_viewProj);
  898. }
  899. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  900. {
  901. const Attrib& attrib = m_attrib[m_stack];
  902. const uint32_t num = getCircleLod(attrib.m_lod);
  903. const float step = bx::pi * 2.0f / num;
  904. _degrees = bx::fwrap(_degrees, 360.0f);
  905. float pos[3];
  906. getPoint(pos, _axis
  907. , bx::fsin(step * 0)*_radius
  908. , bx::fcos(step * 0)*_radius
  909. );
  910. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  911. uint32_t n = uint32_t(num*_degrees/360.0f);
  912. for (uint32_t ii = 1; ii < n+1; ++ii)
  913. {
  914. getPoint(pos, _axis
  915. , bx::fsin(step * ii)*_radius
  916. , bx::fcos(step * ii)*_radius
  917. );
  918. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  919. }
  920. moveTo(_x, _y, _z);
  921. getPoint(pos, _axis
  922. , bx::fsin(step * 0)*_radius
  923. , bx::fcos(step * 0)*_radius
  924. );
  925. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  926. getPoint(pos, _axis
  927. , bx::fsin(step * n)*_radius
  928. , bx::fcos(step * n)*_radius
  929. );
  930. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  931. lineTo(_x, _y, _z);
  932. }
  933. void drawCircle(const float* _normal, const float* _center, float _radius, float _weight)
  934. {
  935. const Attrib& attrib = m_attrib[m_stack];
  936. const uint32_t num = getCircleLod(attrib.m_lod);
  937. const float step = bx::pi * 2.0f / num;
  938. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  939. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  940. float udir[3];
  941. float vdir[3];
  942. calcPlaneUv(plane, udir, vdir);
  943. float pos[3];
  944. float tmp0[3];
  945. float tmp1[3];
  946. float xy0[2];
  947. float xy1[2];
  948. circle(xy0, 0.0f);
  949. squircle(xy1, 0.0f);
  950. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  951. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  952. bx::vec3Add(tmp1, pos, tmp0);
  953. bx::vec3Add(pos, tmp1, _center);
  954. moveTo(pos);
  955. for (uint32_t ii = 1; ii < num; ++ii)
  956. {
  957. float angle = step * ii;
  958. circle(xy0, angle);
  959. squircle(xy1, angle);
  960. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  961. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  962. bx::vec3Add(tmp1, pos, tmp0);
  963. bx::vec3Add(pos, tmp1, _center);
  964. lineTo(pos);
  965. }
  966. close();
  967. }
  968. void drawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  969. {
  970. drawCircle( (const float*)_normal, (const float*)_center, _radius, _weight);
  971. }
  972. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  973. {
  974. const Attrib& attrib = m_attrib[m_stack];
  975. const uint32_t num = getCircleLod(attrib.m_lod);
  976. const float step = bx::pi * 2.0f / num;
  977. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  978. float xy0[2];
  979. float xy1[2];
  980. circle(xy0, 0.0f);
  981. squircle(xy1, 0.0f);
  982. float pos[3];
  983. getPoint(pos, _axis
  984. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  985. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  986. );
  987. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  988. for (uint32_t ii = 1; ii < num; ++ii)
  989. {
  990. float angle = step * ii;
  991. circle(xy0, angle);
  992. squircle(xy1, angle);
  993. getPoint(pos, _axis
  994. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  995. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  996. );
  997. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  998. }
  999. close();
  1000. }
  1001. void drawCone(const float* _from, const float* _to, float _radius)
  1002. {
  1003. const Attrib& attrib = m_attrib[m_stack];
  1004. float tmp0[3];
  1005. bx::vec3Sub(tmp0, _from, _to);
  1006. float normal[3];
  1007. bx::vec3Norm(normal, tmp0);
  1008. float mtx[2][16];
  1009. bx::mtxFromNormal(mtx[0], normal, _radius, _from);
  1010. memcpy(mtx[1], mtx[0], 64);
  1011. mtx[1][12] = _to[0];
  1012. mtx[1][13] = _to[1];
  1013. mtx[1][14] = _to[2];
  1014. uint8_t lod = attrib.m_lod > Mesh::ConeMaxLod
  1015. ? uint8_t(Mesh::ConeMaxLod)
  1016. : attrib.m_lod
  1017. ;
  1018. draw(Mesh::Enum(Mesh::Cone0 + lod), mtx[0], 2, attrib.m_wireframe);
  1019. }
  1020. void drawCone(const void* _from, const void* _to, float _radius)
  1021. {
  1022. drawCone( (const float*)_from, (const float*)_to, _radius);
  1023. }
  1024. void drawCylinder(const float* _from, const float* _to, float _radius, bool _capsule)
  1025. {
  1026. const Attrib& attrib = m_attrib[m_stack];
  1027. float tmp0[3];
  1028. bx::vec3Sub(tmp0, _from, _to);
  1029. float normal[3];
  1030. bx::vec3Norm(normal, tmp0);
  1031. float mtx[2][16];
  1032. bx::mtxFromNormal(mtx[0], normal, _radius, _from);
  1033. memcpy(mtx[1], mtx[0], 64);
  1034. mtx[1][12] = _to[0];
  1035. mtx[1][13] = _to[1];
  1036. mtx[1][14] = _to[2];
  1037. if (_capsule)
  1038. {
  1039. uint8_t lod = attrib.m_lod > Mesh::CapsuleMaxLod
  1040. ? uint8_t(Mesh::CapsuleMaxLod)
  1041. : attrib.m_lod
  1042. ;
  1043. draw(Mesh::Enum(Mesh::Capsule0 + lod), mtx[0], 2, attrib.m_wireframe);
  1044. Sphere sphere;
  1045. bx::vec3Move(sphere.m_center, _from);
  1046. sphere.m_radius = _radius;
  1047. draw(sphere);
  1048. bx::vec3Move(sphere.m_center, _to);
  1049. draw(sphere);
  1050. }
  1051. else
  1052. {
  1053. uint8_t lod = attrib.m_lod > Mesh::CylinderMaxLod
  1054. ? uint8_t(Mesh::CylinderMaxLod)
  1055. : attrib.m_lod
  1056. ;
  1057. draw(Mesh::Enum(Mesh::Cylinder0 + lod), mtx[0], 2, attrib.m_wireframe);
  1058. }
  1059. }
  1060. void drawCylinder(const void* _from, const void* _to, float _radius, bool _capsule)
  1061. {
  1062. drawCylinder( (const float*)_from, (const float*)_to, _radius, _capsule);
  1063. }
  1064. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight, float _thickness)
  1065. {
  1066. push();
  1067. if (_thickness > 0.0f)
  1068. {
  1069. float from[3] = { _x, _y, _z };
  1070. float mid[3];
  1071. float to[3];
  1072. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1073. mid[0] = _x + _len - _thickness;
  1074. mid[1] = _y;
  1075. mid[2] = _z;
  1076. to[0] = _x + _len;
  1077. to[1] = _y;
  1078. to[2] = _z;
  1079. drawCylinder(from, mid, _thickness, false);
  1080. drawCone(mid, to, _thickness);
  1081. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1082. mid[0] = _x;
  1083. mid[1] = _y + _len - _thickness;
  1084. mid[2] = _z;
  1085. to[0] = _x;
  1086. to[1] = _y + _len;
  1087. to[2] = _z;
  1088. drawCylinder(from, mid, _thickness, false);
  1089. drawCone(mid, to, _thickness);
  1090. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1091. mid[0] = _x;
  1092. mid[1] = _y;
  1093. mid[2] = _z + _len - _thickness;
  1094. to[0] = _x;
  1095. to[1] = _y;
  1096. to[2] = _z + _len;
  1097. drawCylinder(from, mid, _thickness, false);
  1098. drawCone(mid, to, _thickness);
  1099. }
  1100. else
  1101. {
  1102. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1103. moveTo(_x, _y, _z);
  1104. lineTo(_x + _len, _y, _z);
  1105. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1106. moveTo(_x, _y, _z);
  1107. lineTo(_x, _y + _len, _z);
  1108. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1109. moveTo(_x, _y, _z);
  1110. lineTo(_x, _y, _z + _len);
  1111. }
  1112. pop();
  1113. }
  1114. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  1115. {
  1116. float udir[3];
  1117. float vdir[3];
  1118. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  1119. calcPlaneUv(plane, udir, vdir);
  1120. bx::vec3Mul(udir, udir, _step);
  1121. bx::vec3Mul(vdir, vdir, _step);
  1122. const uint32_t num = (_size/2)*2+1;
  1123. const float halfExtent = float(_size/2);
  1124. float umin[3];
  1125. bx::vec3Mul(umin, udir, -halfExtent);
  1126. float umax[3];
  1127. bx::vec3Mul(umax, udir, halfExtent);
  1128. float vmin[3];
  1129. bx::vec3Mul(vmin, vdir, -halfExtent);
  1130. float vmax[3];
  1131. bx::vec3Mul(vmax, vdir, halfExtent);
  1132. float tmp[3];
  1133. float xs[3];
  1134. float xe[3];
  1135. bx::vec3Add(tmp, umin, vmin);
  1136. bx::vec3Add(xs, _center, tmp);
  1137. bx::vec3Add(tmp, umax, vmin);
  1138. bx::vec3Add(xe, _center, tmp);
  1139. float ys[3];
  1140. float ye[3];
  1141. bx::vec3Add(tmp, umin, vmin);
  1142. bx::vec3Add(ys, _center, tmp);
  1143. bx::vec3Add(tmp, umin, vmax);
  1144. bx::vec3Add(ye, _center, tmp);
  1145. for (uint32_t ii = 0; ii < num; ++ii)
  1146. {
  1147. moveTo(xs);
  1148. lineTo(xe);
  1149. bx::vec3Add(xs, xs, vdir);
  1150. bx::vec3Add(xe, xe, vdir);
  1151. moveTo(ys);
  1152. lineTo(ye);
  1153. bx::vec3Add(ys, ys, udir);
  1154. bx::vec3Add(ye, ye, udir);
  1155. }
  1156. }
  1157. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1158. {
  1159. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  1160. }
  1161. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  1162. {
  1163. push();
  1164. setTranslate(_center);
  1165. const uint32_t num = (_size/2)*2-1;
  1166. const float halfExtent = float(_size/2) * _step;
  1167. setColor(0xff606060);
  1168. float yy = -halfExtent + _step;
  1169. for (uint32_t ii = 0; ii < num; ++ii)
  1170. {
  1171. moveTo(_axis, -halfExtent, yy);
  1172. lineTo(_axis, halfExtent, yy);
  1173. moveTo(_axis, yy, -halfExtent);
  1174. lineTo(_axis, yy, halfExtent);
  1175. yy += _step;
  1176. }
  1177. setColor(0xff101010);
  1178. moveTo(_axis, -halfExtent, -halfExtent);
  1179. lineTo(_axis, -halfExtent, halfExtent);
  1180. lineTo(_axis, halfExtent, halfExtent);
  1181. lineTo(_axis, halfExtent, -halfExtent);
  1182. close();
  1183. moveTo(_axis, -halfExtent, 0.0f);
  1184. lineTo(_axis, halfExtent, 0.0f);
  1185. moveTo(_axis, 0.0f, -halfExtent);
  1186. lineTo(_axis, 0.0f, halfExtent);
  1187. pop();
  1188. }
  1189. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1190. {
  1191. drawGrid(_axis, (const float*)_center, _size, _step);
  1192. }
  1193. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1194. {
  1195. push();
  1196. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  1197. drawCircle(Axis::X, _x, _y, _z, _radius, 0.0f);
  1198. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  1199. drawCircle(Axis::Y, _x, _y, _z, _radius, 0.0f);
  1200. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  1201. drawCircle(Axis::Z, _x, _y, _z, _radius, 0.0f);
  1202. pop();
  1203. }
  1204. private:
  1205. struct Mesh
  1206. {
  1207. enum Enum
  1208. {
  1209. Sphere0,
  1210. Sphere1,
  1211. Sphere2,
  1212. Sphere3,
  1213. Cone0,
  1214. Cone1,
  1215. Cone2,
  1216. Cone3,
  1217. Cylinder0,
  1218. Cylinder1,
  1219. Cylinder2,
  1220. Cylinder3,
  1221. Capsule0,
  1222. Capsule1,
  1223. Capsule2,
  1224. Capsule3,
  1225. Cube,
  1226. Count,
  1227. SphereMaxLod = Sphere3 - Sphere0,
  1228. ConeMaxLod = Cone3 - Cone0,
  1229. CylinderMaxLod = Cylinder3 - Cylinder0,
  1230. CapsuleMaxLod = Capsule3 - Capsule0,
  1231. };
  1232. uint32_t m_startVertex;
  1233. uint32_t m_numVertices;
  1234. uint32_t m_startIndex[2];
  1235. uint32_t m_numIndices[2];
  1236. };
  1237. struct Program
  1238. {
  1239. enum Enum
  1240. {
  1241. Lines,
  1242. LinesStipple,
  1243. Fill,
  1244. FillLit,
  1245. Count
  1246. };
  1247. };
  1248. void draw(Mesh::Enum _mesh, const float* _mtx, uint16_t _num, bool _wireframe) const
  1249. {
  1250. const Mesh& mesh = m_mesh[_mesh];
  1251. const Attrib& attrib = m_attrib[m_stack];
  1252. if (0 != mesh.m_numIndices[_wireframe])
  1253. {
  1254. bgfx::setIndexBuffer(m_ibh
  1255. , mesh.m_startIndex[_wireframe]
  1256. , mesh.m_numIndices[_wireframe]
  1257. );
  1258. }
  1259. const float flip = 0 == (attrib.m_state & BGFX_STATE_CULL_CCW) ? 1.0f : -1.0f;
  1260. const uint8_t alpha = attrib.m_abgr>>24;
  1261. float params[4][4] =
  1262. {
  1263. { // lightDir
  1264. 0.0f * flip,
  1265. -1.0f * flip,
  1266. 0.0f * flip,
  1267. 3.0f, // shininess
  1268. },
  1269. { // skyColor
  1270. 1.0f,
  1271. 0.9f,
  1272. 0.8f,
  1273. 0.0f, // unused
  1274. },
  1275. { // groundColor.xyz0
  1276. 0.2f,
  1277. 0.22f,
  1278. 0.5f,
  1279. 0.0f, // unused
  1280. },
  1281. { // matColor
  1282. ( (attrib.m_abgr )&0xff)/255.0f,
  1283. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  1284. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  1285. ( alpha )/255.0f,
  1286. },
  1287. };
  1288. bx::vec3Norm(params[0], params[0]);
  1289. bgfx::setUniform(u_params, params, 4);
  1290. bgfx::setTransform(_mtx, _num);
  1291. bgfx::setVertexBuffer(m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1292. bgfx::setState(0
  1293. | attrib.m_state
  1294. | (_wireframe ? BGFX_STATE_PT_LINES|BGFX_STATE_LINEAA|BGFX_STATE_BLEND_ALPHA
  1295. : (alpha < 0xff) ? BGFX_STATE_BLEND_ALPHA : 0)
  1296. );
  1297. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1298. }
  1299. void softFlush()
  1300. {
  1301. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1302. {
  1303. flush();
  1304. }
  1305. }
  1306. void flush()
  1307. {
  1308. if (0 != m_pos)
  1309. {
  1310. if (bgfx::checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  1311. {
  1312. bgfx::TransientVertexBuffer tvb;
  1313. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  1314. memcpy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  1315. bgfx::TransientIndexBuffer tib;
  1316. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1317. memcpy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1318. const Attrib& attrib = m_attrib[m_stack];
  1319. bgfx::setVertexBuffer(&tvb);
  1320. bgfx::setIndexBuffer(&tib);
  1321. bgfx::setState(0
  1322. | BGFX_STATE_RGB_WRITE
  1323. | BGFX_STATE_PT_LINES
  1324. | attrib.m_state
  1325. | BGFX_STATE_LINEAA
  1326. | BGFX_STATE_BLEND_ALPHA
  1327. );
  1328. bgfx::setTransform(m_mtx);
  1329. bgfx::ProgramHandle program = m_program[attrib.m_stipple ? 1 : 0];
  1330. bgfx::submit(m_viewId, program);
  1331. }
  1332. m_state = State::None;
  1333. m_pos = 0;
  1334. m_indexPos = 0;
  1335. m_vertexPos = 0;
  1336. }
  1337. }
  1338. struct State
  1339. {
  1340. enum Enum
  1341. {
  1342. None,
  1343. MoveTo,
  1344. LineTo,
  1345. Count
  1346. };
  1347. };
  1348. static const uint32_t cacheSize = 1024;
  1349. static const uint32_t stackSize = 16;
  1350. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1351. DebugVertex m_cache[cacheSize+1];
  1352. uint32_t m_mtx;
  1353. uint16_t m_indices[cacheSize*2];
  1354. uint16_t m_pos;
  1355. uint16_t m_indexPos;
  1356. uint16_t m_vertexPos;
  1357. uint8_t m_viewId;
  1358. uint8_t m_stack;
  1359. bool m_depthTestLess;
  1360. struct Attrib
  1361. {
  1362. uint64_t m_state;
  1363. float m_offset;
  1364. float m_scale;
  1365. uint32_t m_abgr;
  1366. bool m_stipple;
  1367. bool m_wireframe;
  1368. uint8_t m_lod;
  1369. };
  1370. Attrib m_attrib[stackSize];
  1371. State::Enum m_state;
  1372. Mesh m_mesh[Mesh::Count];
  1373. bgfx::ProgramHandle m_program[Program::Count];
  1374. bgfx::UniformHandle u_params;
  1375. bgfx::VertexBufferHandle m_vbh;
  1376. bgfx::IndexBufferHandle m_ibh;
  1377. bx::AllocatorI* m_allocator;
  1378. };
  1379. static DebugDraw s_dd;
  1380. void ddInit(bool _depthTestLess, bx::AllocatorI* _allocator)
  1381. {
  1382. s_dd.init(_depthTestLess, _allocator);
  1383. }
  1384. void ddShutdown()
  1385. {
  1386. s_dd.shutdown();
  1387. }
  1388. void ddBegin(uint8_t _viewId)
  1389. {
  1390. s_dd.begin(_viewId);
  1391. }
  1392. void ddEnd()
  1393. {
  1394. s_dd.end();
  1395. }
  1396. void ddPush()
  1397. {
  1398. s_dd.push();
  1399. }
  1400. void ddPop()
  1401. {
  1402. s_dd.pop();
  1403. }
  1404. void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1405. {
  1406. s_dd.setState(_depthTest, _depthWrite, _clockwise);
  1407. }
  1408. void ddSetColor(uint32_t _abgr)
  1409. {
  1410. s_dd.setColor(_abgr);
  1411. }
  1412. void ddSetLod(uint8_t _lod)
  1413. {
  1414. s_dd.setLod(_lod);
  1415. }
  1416. void ddSetWireframe(bool _wireframe)
  1417. {
  1418. s_dd.setWireframe(_wireframe);
  1419. }
  1420. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1421. {
  1422. s_dd.setStipple(_stipple, _scale, _offset);
  1423. }
  1424. void ddSetTransform(const void* _mtx)
  1425. {
  1426. s_dd.setTransform(_mtx);
  1427. }
  1428. void ddSetTranslate(float _x, float _y, float _z)
  1429. {
  1430. s_dd.setTranslate(_x, _y, _z);
  1431. }
  1432. void ddMoveTo(float _x, float _y, float _z)
  1433. {
  1434. s_dd.moveTo(_x, _y, _z);
  1435. }
  1436. void ddMoveTo(const void* _pos)
  1437. {
  1438. s_dd.moveTo(_pos);
  1439. }
  1440. void ddLineTo(float _x, float _y, float _z)
  1441. {
  1442. s_dd.lineTo(_x, _y, _z);
  1443. }
  1444. void ddLineTo(const void* _pos)
  1445. {
  1446. s_dd.lineTo(_pos);
  1447. }
  1448. void ddClose()
  1449. {
  1450. s_dd.close();
  1451. }
  1452. void ddDraw(const Aabb& _aabb)
  1453. {
  1454. s_dd.draw(_aabb);
  1455. }
  1456. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1457. {
  1458. s_dd.draw(_cylinder, _capsule);
  1459. }
  1460. void ddDraw(const Disk& _disk)
  1461. {
  1462. s_dd.draw(_disk);
  1463. }
  1464. void ddDraw(const Obb& _obb)
  1465. {
  1466. s_dd.draw(_obb);
  1467. }
  1468. void ddDraw(const Sphere& _sphere)
  1469. {
  1470. s_dd.draw(_sphere);
  1471. }
  1472. void ddDrawFrustum(const void* _viewProj)
  1473. {
  1474. s_dd.drawFrustum(_viewProj);
  1475. }
  1476. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1477. {
  1478. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1479. }
  1480. void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1481. {
  1482. s_dd.drawCircle(_normal, _center, _radius, _weight);
  1483. }
  1484. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1485. {
  1486. s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight);
  1487. }
  1488. void ddDrawCone(const void* _from, const void* _to, float _radius)
  1489. {
  1490. s_dd.drawCone(_from, _to, _radius);
  1491. }
  1492. void ddDrawCylinder(const void* _from, const void* _to, float _radius, bool _capsule)
  1493. {
  1494. if (_capsule)
  1495. {
  1496. s_dd.push();
  1497. s_dd.setLod(0);
  1498. s_dd.drawCylinder(_from, _to, _radius, true);
  1499. s_dd.pop();
  1500. }
  1501. else
  1502. {
  1503. s_dd.drawCylinder(_from, _to, _radius, false);
  1504. }
  1505. }
  1506. void ddDrawCapsule(const void* _from, const void* _to, float _radius)
  1507. {
  1508. s_dd.drawCylinder(_from, _to, _radius, true);
  1509. }
  1510. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight, float _thickness)
  1511. {
  1512. s_dd.drawAxis(_x, _y, _z, _len, _hightlight, _thickness);
  1513. }
  1514. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1515. {
  1516. s_dd.drawGrid(_normal, _center, _size, _step);
  1517. }
  1518. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1519. {
  1520. s_dd.drawGrid(_axis, _center, _size, _step);
  1521. }
  1522. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1523. {
  1524. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1525. }