debugdraw.cpp 45 KB

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