debugdraw.cpp 44 KB

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