debugdraw.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bgfx/embedded_shader.h>
  7. #include "debugdraw.h"
  8. #include "../bgfx_utils.h"
  9. #include "../packrect.h"
  10. #include <bx/math.h>
  11. #include <bx/sort.h>
  12. #include <bx/uint32_t.h>
  13. #include <bx/handlealloc.h>
  14. struct DebugVertex
  15. {
  16. float m_x;
  17. float m_y;
  18. float m_z;
  19. float m_len;
  20. uint32_t m_abgr;
  21. static void init()
  22. {
  23. ms_decl
  24. .begin()
  25. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  26. .add(bgfx::Attrib::TexCoord0, 1, bgfx::AttribType::Float)
  27. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  28. .end();
  29. }
  30. static bgfx::VertexDecl ms_decl;
  31. };
  32. bgfx::VertexDecl DebugVertex::ms_decl;
  33. struct DebugUvVertex
  34. {
  35. float m_x;
  36. float m_y;
  37. float m_z;
  38. float m_u;
  39. float m_v;
  40. uint32_t m_abgr;
  41. static void init()
  42. {
  43. ms_decl
  44. .begin()
  45. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  46. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  47. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  48. .end();
  49. }
  50. static bgfx::VertexDecl ms_decl;
  51. };
  52. bgfx::VertexDecl DebugUvVertex::ms_decl;
  53. struct DebugShapeVertex
  54. {
  55. float m_x;
  56. float m_y;
  57. float m_z;
  58. uint8_t m_indices[4];
  59. static void init()
  60. {
  61. ms_decl
  62. .begin()
  63. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  64. .add(bgfx::Attrib::Indices, 4, bgfx::AttribType::Uint8)
  65. .end();
  66. }
  67. static bgfx::VertexDecl ms_decl;
  68. };
  69. bgfx::VertexDecl DebugShapeVertex::ms_decl;
  70. struct DebugMeshVertex
  71. {
  72. float m_x;
  73. float m_y;
  74. float m_z;
  75. static void init()
  76. {
  77. ms_decl
  78. .begin()
  79. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  80. .end();
  81. }
  82. static bgfx::VertexDecl ms_decl;
  83. };
  84. bgfx::VertexDecl DebugMeshVertex::ms_decl;
  85. static DebugShapeVertex s_cubeVertices[8] =
  86. {
  87. {-1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
  88. { 1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
  89. {-1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
  90. { 1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
  91. {-1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
  92. { 1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
  93. {-1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
  94. { 1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
  95. };
  96. static const uint16_t s_cubeIndices[36] =
  97. {
  98. 0, 1, 2, // 0
  99. 1, 3, 2,
  100. 4, 6, 5, // 2
  101. 5, 6, 7,
  102. 0, 2, 4, // 4
  103. 4, 2, 6,
  104. 1, 5, 3, // 6
  105. 5, 7, 3,
  106. 0, 4, 1, // 8
  107. 4, 5, 1,
  108. 2, 3, 6, // 10
  109. 6, 3, 7,
  110. };
  111. static const uint8_t s_circleLod[] =
  112. {
  113. 37,
  114. 29,
  115. 23,
  116. 17,
  117. 11,
  118. };
  119. static uint8_t getCircleLod(uint8_t _lod)
  120. {
  121. _lod = _lod > BX_COUNTOF(s_circleLod)-1 ? BX_COUNTOF(s_circleLod)-1 : _lod;
  122. return s_circleLod[_lod];
  123. }
  124. static void circle(float* _out, float _angle)
  125. {
  126. float sa = bx::sin(_angle);
  127. float ca = bx::cos(_angle);
  128. _out[0] = sa;
  129. _out[1] = ca;
  130. }
  131. static void squircle(float* _out, float _angle)
  132. {
  133. float sa = bx::sin(_angle);
  134. float ca = bx::cos(_angle);
  135. _out[0] = bx::sqrt(bx::abs(sa) ) * bx::sign(sa);
  136. _out[1] = bx::sqrt(bx::abs(ca) ) * bx::sign(ca);
  137. }
  138. uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = 0, void* _normals0 = NULL, uint16_t _normalStride0 = 0)
  139. {
  140. if (NULL != _pos0)
  141. {
  142. struct Gen
  143. {
  144. Gen(void* _pos, uint16_t _posStride, void* _normals, uint16_t _normalStride, uint8_t _subdiv)
  145. : m_pos( (uint8_t*)_pos)
  146. , m_normals( (uint8_t*)_normals)
  147. , m_posStride(_posStride)
  148. , m_normalStride(_normalStride)
  149. {
  150. static const float scale = 1.0f;
  151. static const float golden = 1.6180339887f;
  152. static const float len = bx::sqrt(golden*golden + 1.0f);
  153. static const float ss = 1.0f/len * scale;
  154. static const float ll = ss*golden;
  155. static const float vv[12][4] =
  156. {
  157. { -ll, 0.0f, -ss, 0.0f },
  158. { ll, 0.0f, -ss, 0.0f },
  159. { ll, 0.0f, ss, 0.0f },
  160. { -ll, 0.0f, ss, 0.0f },
  161. { -ss, ll, 0.0f, 0.0f },
  162. { ss, ll, 0.0f, 0.0f },
  163. { ss, -ll, 0.0f, 0.0f },
  164. { -ss, -ll, 0.0f, 0.0f },
  165. { 0.0f, -ss, ll, 0.0f },
  166. { 0.0f, ss, ll, 0.0f },
  167. { 0.0f, ss, -ll, 0.0f },
  168. { 0.0f, -ss, -ll, 0.0f },
  169. };
  170. m_numVertices = 0;
  171. triangle(vv[ 0], vv[ 4], vv[ 3], scale, _subdiv);
  172. triangle(vv[ 0], vv[10], vv[ 4], scale, _subdiv);
  173. triangle(vv[ 4], vv[10], vv[ 5], scale, _subdiv);
  174. triangle(vv[ 5], vv[10], vv[ 1], scale, _subdiv);
  175. triangle(vv[ 5], vv[ 1], vv[ 2], scale, _subdiv);
  176. triangle(vv[ 5], vv[ 2], vv[ 9], scale, _subdiv);
  177. triangle(vv[ 5], vv[ 9], vv[ 4], scale, _subdiv);
  178. triangle(vv[ 3], vv[ 4], vv[ 9], scale, _subdiv);
  179. triangle(vv[ 0], vv[ 3], vv[ 7], scale, _subdiv);
  180. triangle(vv[ 0], vv[ 7], vv[11], scale, _subdiv);
  181. triangle(vv[11], vv[ 7], vv[ 6], scale, _subdiv);
  182. triangle(vv[11], vv[ 6], vv[ 1], scale, _subdiv);
  183. triangle(vv[ 1], vv[ 6], vv[ 2], scale, _subdiv);
  184. triangle(vv[ 2], vv[ 6], vv[ 8], scale, _subdiv);
  185. triangle(vv[ 8], vv[ 6], vv[ 7], scale, _subdiv);
  186. triangle(vv[ 8], vv[ 7], vv[ 3], scale, _subdiv);
  187. triangle(vv[ 0], vv[11], vv[10], scale, _subdiv);
  188. triangle(vv[ 1], vv[10], vv[11], scale, _subdiv);
  189. triangle(vv[ 2], vv[ 8], vv[ 9], scale, _subdiv);
  190. triangle(vv[ 3], vv[ 9], vv[ 8], scale, _subdiv);
  191. }
  192. void addVert(const float* _v)
  193. {
  194. float* verts = (float*)m_pos;
  195. verts[0] = _v[0];
  196. verts[1] = _v[1];
  197. verts[2] = _v[2];
  198. m_pos += m_posStride;
  199. if (NULL != m_normals)
  200. {
  201. float* normals = (float*)m_normals;
  202. bx::vec3Norm(normals, _v);
  203. m_normals += m_normalStride;
  204. }
  205. m_numVertices++;
  206. }
  207. void triangle(const float* _v0, const float* _v1, const float* _v2, float _scale, uint8_t _subdiv)
  208. {
  209. if (0 == _subdiv)
  210. {
  211. addVert(_v0);
  212. addVert(_v1);
  213. addVert(_v2);
  214. }
  215. else
  216. {
  217. float tmp0[4];
  218. float tmp1[4];
  219. float v01[4];
  220. bx::vec3Add(tmp0, _v0, _v1);
  221. bx::vec3Norm(tmp1, tmp0);
  222. bx::vec3Mul(v01, tmp1, _scale);
  223. float v12[4];
  224. bx::vec3Add(tmp0, _v1, _v2);
  225. bx::vec3Norm(tmp1, tmp0);
  226. bx::vec3Mul(v12, tmp1, _scale);
  227. float v20[4];
  228. bx::vec3Add(tmp0, _v2, _v0);
  229. bx::vec3Norm(tmp1, tmp0);
  230. bx::vec3Mul(v20, tmp1, _scale);
  231. --_subdiv;
  232. triangle(_v0, v01, v20, _scale, _subdiv);
  233. triangle(_v1, v12, v01, _scale, _subdiv);
  234. triangle(_v2, v20, v12, _scale, _subdiv);
  235. triangle(v01, v12, v20, _scale, _subdiv);
  236. }
  237. }
  238. uint8_t* m_pos;
  239. uint8_t* m_normals;
  240. uint16_t m_posStride;
  241. uint16_t m_normalStride;
  242. uint32_t m_numVertices;
  243. } gen(_pos0, _posStride0, _normals0, _normalStride0, _subdiv0);
  244. }
  245. uint32_t numVertices = 20*3*bx::uint32_max(1, (uint32_t)bx::pow(4.0f, _subdiv0) );
  246. return numVertices;
  247. }
  248. void getPoint(float* _result, Axis::Enum _axis, float _x, float _y)
  249. {
  250. switch (_axis)
  251. {
  252. case Axis::X:
  253. _result[0] = 0.0f;
  254. _result[1] = _x;
  255. _result[2] = _y;
  256. break;
  257. case Axis::Y:
  258. _result[0] = _y;
  259. _result[1] = 0.0f;
  260. _result[2] = _x;
  261. break;
  262. default:
  263. _result[0] = _x;
  264. _result[1] = _y;
  265. _result[2] = 0.0f;
  266. break;
  267. }
  268. }
  269. #include "vs_debugdraw_lines.bin.h"
  270. #include "fs_debugdraw_lines.bin.h"
  271. #include "vs_debugdraw_lines_stipple.bin.h"
  272. #include "fs_debugdraw_lines_stipple.bin.h"
  273. #include "vs_debugdraw_fill.bin.h"
  274. #include "vs_debugdraw_fill_mesh.bin.h"
  275. #include "fs_debugdraw_fill.bin.h"
  276. #include "vs_debugdraw_fill_lit.bin.h"
  277. #include "vs_debugdraw_fill_lit_mesh.bin.h"
  278. #include "fs_debugdraw_fill_lit.bin.h"
  279. #include "vs_debugdraw_fill_texture.bin.h"
  280. #include "fs_debugdraw_fill_texture.bin.h"
  281. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  282. {
  283. BGFX_EMBEDDED_SHADER(vs_debugdraw_lines),
  284. BGFX_EMBEDDED_SHADER(fs_debugdraw_lines),
  285. BGFX_EMBEDDED_SHADER(vs_debugdraw_lines_stipple),
  286. BGFX_EMBEDDED_SHADER(fs_debugdraw_lines_stipple),
  287. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill),
  288. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_mesh),
  289. BGFX_EMBEDDED_SHADER(fs_debugdraw_fill),
  290. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_lit),
  291. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_lit_mesh),
  292. BGFX_EMBEDDED_SHADER(fs_debugdraw_fill_lit),
  293. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_texture),
  294. BGFX_EMBEDDED_SHADER(fs_debugdraw_fill_texture),
  295. BGFX_EMBEDDED_SHADER_END()
  296. };
  297. #define SPRITE_TEXTURE_SIZE 1024
  298. template<uint16_t MaxHandlesT = 256, uint16_t TextureSizeT = 1024>
  299. struct SpriteT
  300. {
  301. SpriteT()
  302. : m_ra(TextureSizeT, TextureSizeT)
  303. {
  304. }
  305. SpriteHandle create(uint16_t _width, uint16_t _height)
  306. {
  307. SpriteHandle handle = { bx::kInvalidHandle };
  308. if (m_handleAlloc.getNumHandles() < m_handleAlloc.getMaxHandles() )
  309. {
  310. Pack2D pack;
  311. if (m_ra.find(_width, _height, pack) )
  312. {
  313. handle.idx = m_handleAlloc.alloc();
  314. if (isValid(handle) )
  315. {
  316. m_pack[handle.idx] = pack;
  317. }
  318. else
  319. {
  320. m_ra.clear(pack);
  321. }
  322. }
  323. }
  324. return handle;
  325. }
  326. void destroy(SpriteHandle _sprite)
  327. {
  328. const Pack2D& pack = m_pack[_sprite.idx];
  329. m_ra.clear(pack);
  330. m_handleAlloc.free(_sprite.idx);
  331. }
  332. const Pack2D& get(SpriteHandle _sprite) const
  333. {
  334. return m_pack[_sprite.idx];
  335. }
  336. bx::HandleAllocT<MaxHandlesT> m_handleAlloc;
  337. Pack2D m_pack[MaxHandlesT];
  338. RectPack2DT<256> m_ra;
  339. };
  340. template<uint16_t MaxHandlesT = 256>
  341. struct GeometryT
  342. {
  343. GeometryT()
  344. {
  345. }
  346. GeometryHandle create(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
  347. {
  348. BX_UNUSED(_numVertices, _vertices, _numIndices, _indices);
  349. GeometryHandle handle = { m_handleAlloc.alloc() };
  350. if (isValid(handle) )
  351. {
  352. Geometry& geometry = m_geometry[handle.idx];
  353. geometry.m_vbh = bgfx::createVertexBuffer(
  354. bgfx::copy(_vertices, _numVertices*sizeof(DdVertex) )
  355. , DebugMeshVertex::ms_decl
  356. );
  357. geometry.m_topologyNumIndices[0] = _numIndices;
  358. geometry.m_topologyNumIndices[1] = bgfx::topologyConvert(
  359. bgfx::TopologyConvert::TriListToLineList
  360. , NULL
  361. , 0
  362. , _indices
  363. , _numIndices
  364. , false
  365. );
  366. const uint32_t numIndices = 0
  367. + geometry.m_topologyNumIndices[0]
  368. + geometry.m_topologyNumIndices[1]
  369. ;
  370. const bgfx::Memory* mem = bgfx::alloc(numIndices*sizeof(uint16_t) );
  371. uint16_t* indices = (uint16_t*)mem->data;
  372. bx::memCopy(&indices[0], _indices, _numIndices*sizeof(uint16_t) );
  373. bgfx::topologyConvert(
  374. bgfx::TopologyConvert::TriListToLineList
  375. , &indices[geometry.m_topologyNumIndices[0] ]
  376. , geometry.m_topologyNumIndices[1]*sizeof(uint16_t)
  377. , _indices
  378. , _numIndices
  379. , false
  380. );
  381. geometry.m_ibh = bgfx::createIndexBuffer(mem);
  382. }
  383. return handle;
  384. }
  385. void destroy(GeometryHandle _handle)
  386. {
  387. Geometry& geometry = m_geometry[_handle.idx];
  388. bgfx::destroy(geometry.m_vbh);
  389. bgfx::destroy(geometry.m_ibh);
  390. m_handleAlloc.free(_handle.idx);
  391. }
  392. struct Geometry
  393. {
  394. Geometry()
  395. {
  396. m_vbh.idx = bx::kInvalidHandle;
  397. m_ibh.idx = bx::kInvalidHandle;
  398. m_topologyNumIndices[0] = 0;
  399. m_topologyNumIndices[1] = 0;
  400. }
  401. bgfx::VertexBufferHandle m_vbh;
  402. bgfx::IndexBufferHandle m_ibh;
  403. uint32_t m_topologyNumIndices[2];
  404. };
  405. bx::HandleAllocT<MaxHandlesT> m_handleAlloc;
  406. Geometry m_geometry[MaxHandlesT];
  407. };
  408. struct Attrib
  409. {
  410. uint64_t m_state;
  411. float m_offset;
  412. float m_scale;
  413. float m_spin;
  414. uint32_t m_abgr;
  415. bool m_stipple;
  416. bool m_wireframe;
  417. uint8_t m_lod;
  418. };
  419. struct DebugDraw
  420. {
  421. DebugDraw()
  422. : m_depthTestLess(true)
  423. , m_state(State::Count)
  424. {
  425. }
  426. void init(bool _depthTestLess, bx::AllocatorI* _allocator)
  427. {
  428. m_allocator = _allocator;
  429. m_depthTestLess = _depthTestLess;
  430. if (NULL == _allocator)
  431. {
  432. static bx::DefaultAllocator allocator;
  433. m_allocator = &allocator;
  434. }
  435. DebugVertex::init();
  436. DebugUvVertex::init();
  437. DebugShapeVertex::init();
  438. DebugMeshVertex::init();
  439. bgfx::RendererType::Enum type = bgfx::getRendererType();
  440. m_program[Program::Lines] = bgfx::createProgram(
  441. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_lines")
  442. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_lines")
  443. , true
  444. );
  445. m_program[Program::LinesStipple] = bgfx::createProgram(
  446. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_lines_stipple")
  447. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_lines_stipple")
  448. , true
  449. );
  450. m_program[Program::Fill] = bgfx::createProgram(
  451. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill")
  452. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill")
  453. , true
  454. );
  455. m_program[Program::FillMesh] = bgfx::createProgram(
  456. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_mesh")
  457. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill")
  458. , true
  459. );
  460. m_program[Program::FillLit] = bgfx::createProgram(
  461. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_lit")
  462. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_lit")
  463. , true
  464. );
  465. m_program[Program::FillLitMesh] = bgfx::createProgram(
  466. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_lit_mesh")
  467. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_lit")
  468. , true
  469. );
  470. m_program[Program::FillTexture] = bgfx::createProgram(
  471. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_texture")
  472. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_texture")
  473. , true
  474. );
  475. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 4);
  476. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  477. m_texture = bgfx::createTexture2D(SPRITE_TEXTURE_SIZE, SPRITE_TEXTURE_SIZE, false, 1, bgfx::TextureFormat::BGRA8);
  478. void* vertices[Mesh::Count] = {};
  479. uint16_t* indices[Mesh::Count] = {};
  480. uint16_t stride = DebugShapeVertex::ms_decl.getStride();
  481. uint32_t startVertex = 0;
  482. uint32_t startIndex = 0;
  483. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  484. {
  485. Mesh::Enum id = Mesh::Enum(Mesh::Sphere0+mesh);
  486. const uint8_t tess = uint8_t(3-mesh);
  487. const uint32_t numVertices = genSphere(tess);
  488. const uint32_t numIndices = numVertices;
  489. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  490. bx::memSet(vertices[id], 0, numVertices*stride);
  491. genSphere(tess, vertices[id], stride);
  492. uint16_t* trilist = (uint16_t*)BX_ALLOC(m_allocator, numIndices*sizeof(uint16_t) );
  493. for (uint32_t ii = 0; ii < numIndices; ++ii)
  494. {
  495. trilist[ii] = uint16_t(ii);
  496. }
  497. uint32_t numLineListIndices = bgfx::topologyConvert(
  498. bgfx::TopologyConvert::TriListToLineList
  499. , NULL
  500. , 0
  501. , trilist
  502. , numIndices
  503. , false
  504. );
  505. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  506. uint16_t* indicesOut = indices[id];
  507. bx::memCopy(indicesOut, trilist, numIndices*sizeof(uint16_t) );
  508. bgfx::topologyConvert(
  509. bgfx::TopologyConvert::TriListToLineList
  510. , &indicesOut[numIndices]
  511. , numLineListIndices*sizeof(uint16_t)
  512. , trilist
  513. , numIndices
  514. , false
  515. );
  516. m_mesh[id].m_startVertex = startVertex;
  517. m_mesh[id].m_numVertices = numVertices;
  518. m_mesh[id].m_startIndex[0] = startIndex;
  519. m_mesh[id].m_numIndices[0] = numIndices;
  520. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  521. m_mesh[id].m_numIndices[1] = numLineListIndices;
  522. startVertex += numVertices;
  523. startIndex += numIndices + numLineListIndices;
  524. BX_FREE(m_allocator, trilist);
  525. }
  526. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  527. {
  528. Mesh::Enum id = Mesh::Enum(Mesh::Cone0+mesh);
  529. const uint32_t num = getCircleLod(uint8_t(mesh) );
  530. const float step = bx::kPi * 2.0f / num;
  531. const uint32_t numVertices = num+1;
  532. const uint32_t numIndices = num*6;
  533. const uint32_t numLineListIndices = num*4;
  534. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  535. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  536. bx::memSet(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  537. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  538. uint16_t* index = indices[id];
  539. vertex[num].m_x = 0.0f;
  540. vertex[num].m_y = 0.0f;
  541. vertex[num].m_z = 0.0f;
  542. vertex[num].m_indices[0] = 1;
  543. for (uint32_t ii = 0; ii < num; ++ii)
  544. {
  545. const float angle = step * ii;
  546. float xy[2];
  547. circle(xy, angle);
  548. vertex[ii].m_x = xy[1];
  549. vertex[ii].m_y = 0.0f;
  550. vertex[ii].m_z = xy[0];
  551. vertex[ii].m_indices[0] = 0;
  552. index[ii*3+0] = uint16_t(num);
  553. index[ii*3+1] = uint16_t( (ii+1)%num);
  554. index[ii*3+2] = uint16_t(ii);
  555. index[num*3+ii*3+0] = 0;
  556. index[num*3+ii*3+1] = uint16_t(ii);
  557. index[num*3+ii*3+2] = uint16_t( (ii+1)%num);
  558. index[numIndices+ii*2+0] = uint16_t(ii);
  559. index[numIndices+ii*2+1] = uint16_t(num);
  560. index[numIndices+num*2+ii*2+0] = uint16_t(ii);
  561. index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
  562. }
  563. m_mesh[id].m_startVertex = startVertex;
  564. m_mesh[id].m_numVertices = numVertices;
  565. m_mesh[id].m_startIndex[0] = startIndex;
  566. m_mesh[id].m_numIndices[0] = numIndices;
  567. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  568. m_mesh[id].m_numIndices[1] = numLineListIndices;
  569. startVertex += numVertices;
  570. startIndex += numIndices + numLineListIndices;
  571. }
  572. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  573. {
  574. Mesh::Enum id = Mesh::Enum(Mesh::Cylinder0+mesh);
  575. const uint32_t num = getCircleLod(uint8_t(mesh) );
  576. const float step = bx::kPi * 2.0f / num;
  577. const uint32_t numVertices = num*2;
  578. const uint32_t numIndices = num*12;
  579. const uint32_t numLineListIndices = num*6;
  580. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  581. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  582. bx::memSet(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  583. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  584. uint16_t* index = indices[id];
  585. for (uint32_t ii = 0; ii < num; ++ii)
  586. {
  587. const float angle = step * ii;
  588. float xy[2];
  589. circle(xy, angle);
  590. vertex[ii].m_x = xy[1];
  591. vertex[ii].m_y = 0.0f;
  592. vertex[ii].m_z = xy[0];
  593. vertex[ii].m_indices[0] = 0;
  594. vertex[ii+num].m_x = xy[1];
  595. vertex[ii+num].m_y = 0.0f;
  596. vertex[ii+num].m_z = xy[0];
  597. vertex[ii+num].m_indices[0] = 1;
  598. index[ii*6+0] = uint16_t(ii+num);
  599. index[ii*6+1] = uint16_t( (ii+1)%num);
  600. index[ii*6+2] = uint16_t(ii);
  601. index[ii*6+3] = uint16_t(ii+num);
  602. index[ii*6+4] = uint16_t( (ii+1)%num+num);
  603. index[ii*6+5] = uint16_t( (ii+1)%num);
  604. index[num*6+ii*6+0] = uint16_t(0);
  605. index[num*6+ii*6+1] = uint16_t(ii);
  606. index[num*6+ii*6+2] = uint16_t( (ii+1)%num);
  607. index[num*6+ii*6+3] = uint16_t(num);
  608. index[num*6+ii*6+4] = uint16_t( (ii+1)%num+num);
  609. index[num*6+ii*6+5] = uint16_t(ii+num);
  610. index[numIndices+ii*2+0] = uint16_t(ii);
  611. index[numIndices+ii*2+1] = uint16_t(ii+num);
  612. index[numIndices+num*2+ii*2+0] = uint16_t(ii);
  613. index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
  614. index[numIndices+num*4+ii*2+0] = uint16_t(num + ii);
  615. index[numIndices+num*4+ii*2+1] = uint16_t(num + (ii+1)%num);
  616. }
  617. m_mesh[id].m_startVertex = startVertex;
  618. m_mesh[id].m_numVertices = numVertices;
  619. m_mesh[id].m_startIndex[0] = startIndex;
  620. m_mesh[id].m_numIndices[0] = numIndices;
  621. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  622. m_mesh[id].m_numIndices[1] = numLineListIndices;
  623. startVertex += numVertices;
  624. startIndex += numIndices + numLineListIndices;
  625. }
  626. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  627. {
  628. Mesh::Enum id = Mesh::Enum(Mesh::Capsule0+mesh);
  629. const uint32_t num = getCircleLod(uint8_t(mesh) );
  630. const float step = bx::kPi * 2.0f / num;
  631. const uint32_t numVertices = num*2;
  632. const uint32_t numIndices = num*6;
  633. const uint32_t numLineListIndices = num*6;
  634. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  635. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  636. bx::memSet(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  637. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  638. uint16_t* index = indices[id];
  639. for (uint32_t ii = 0; ii < num; ++ii)
  640. {
  641. const float angle = step * ii;
  642. float xy[2];
  643. circle(xy, angle);
  644. vertex[ii].m_x = xy[1];
  645. vertex[ii].m_y = 0.0f;
  646. vertex[ii].m_z = xy[0];
  647. vertex[ii].m_indices[0] = 0;
  648. vertex[ii+num].m_x = xy[1];
  649. vertex[ii+num].m_y = 0.0f;
  650. vertex[ii+num].m_z = xy[0];
  651. vertex[ii+num].m_indices[0] = 1;
  652. index[ii*6+0] = uint16_t(ii+num);
  653. index[ii*6+1] = uint16_t( (ii+1)%num);
  654. index[ii*6+2] = uint16_t(ii);
  655. index[ii*6+3] = uint16_t(ii+num);
  656. index[ii*6+4] = uint16_t( (ii+1)%num+num);
  657. index[ii*6+5] = uint16_t( (ii+1)%num);
  658. // index[num*6+ii*6+0] = uint16_t(0);
  659. // index[num*6+ii*6+1] = uint16_t(ii);
  660. // index[num*6+ii*6+2] = uint16_t( (ii+1)%num);
  661. // index[num*6+ii*6+3] = uint16_t(num);
  662. // index[num*6+ii*6+4] = uint16_t( (ii+1)%num+num);
  663. // index[num*6+ii*6+5] = uint16_t(ii+num);
  664. index[numIndices+ii*2+0] = uint16_t(ii);
  665. index[numIndices+ii*2+1] = uint16_t(ii+num);
  666. index[numIndices+num*2+ii*2+0] = uint16_t(ii);
  667. index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
  668. index[numIndices+num*4+ii*2+0] = uint16_t(num + ii);
  669. index[numIndices+num*4+ii*2+1] = uint16_t(num + (ii+1)%num);
  670. }
  671. m_mesh[id].m_startVertex = startVertex;
  672. m_mesh[id].m_numVertices = numVertices;
  673. m_mesh[id].m_startIndex[0] = startIndex;
  674. m_mesh[id].m_numIndices[0] = numIndices;
  675. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  676. m_mesh[id].m_numIndices[1] = numLineListIndices;
  677. startVertex += numVertices;
  678. startIndex += numIndices + numLineListIndices;
  679. }
  680. m_mesh[Mesh::Cube].m_startVertex = startVertex;
  681. m_mesh[Mesh::Cube].m_numVertices = BX_COUNTOF(s_cubeVertices);
  682. m_mesh[Mesh::Cube].m_startIndex[0] = startIndex;
  683. m_mesh[Mesh::Cube].m_numIndices[0] = BX_COUNTOF(s_cubeIndices);
  684. m_mesh[Mesh::Cube].m_startIndex[1] = 0;
  685. m_mesh[Mesh::Cube].m_numIndices[1] = 0;
  686. startVertex += m_mesh[Mesh::Cube].m_numVertices;
  687. startIndex += m_mesh[Mesh::Cube].m_numIndices[0];
  688. const bgfx::Memory* vb = bgfx::alloc(startVertex*stride);
  689. const bgfx::Memory* ib = bgfx::alloc(startIndex*sizeof(uint16_t) );
  690. for (uint32_t mesh = Mesh::Sphere0; mesh < Mesh::Cube; ++mesh)
  691. {
  692. Mesh::Enum id = Mesh::Enum(mesh);
  693. bx::memCopy(&vb->data[m_mesh[id].m_startVertex * stride]
  694. , vertices[id]
  695. , m_mesh[id].m_numVertices*stride
  696. );
  697. bx::memCopy(&ib->data[m_mesh[id].m_startIndex[0] * sizeof(uint16_t)]
  698. , indices[id]
  699. , (m_mesh[id].m_numIndices[0]+m_mesh[id].m_numIndices[1])*sizeof(uint16_t)
  700. );
  701. BX_FREE(m_allocator, vertices[id]);
  702. BX_FREE(m_allocator, indices[id]);
  703. }
  704. bx::memCopy(&vb->data[m_mesh[Mesh::Cube].m_startVertex * stride]
  705. , s_cubeVertices
  706. , sizeof(s_cubeVertices)
  707. );
  708. bx::memCopy(&ib->data[m_mesh[Mesh::Cube].m_startIndex[0] * sizeof(uint16_t)]
  709. , s_cubeIndices
  710. , sizeof(s_cubeIndices)
  711. );
  712. m_vbh = bgfx::createVertexBuffer(vb, DebugShapeVertex::ms_decl);
  713. m_ibh = bgfx::createIndexBuffer(ib);
  714. m_viewId = 0;
  715. m_pos = 0;
  716. m_indexPos = 0;
  717. m_vertexPos = 0;
  718. m_posQuad = 0;
  719. }
  720. void shutdown()
  721. {
  722. bgfx::destroy(m_ibh);
  723. bgfx::destroy(m_vbh);
  724. for (uint32_t ii = 0; ii < Program::Count; ++ii)
  725. {
  726. bgfx::destroy(m_program[ii]);
  727. }
  728. bgfx::destroy(u_params);
  729. bgfx::destroy(s_texColor);
  730. bgfx::destroy(m_texture);
  731. }
  732. SpriteHandle createSprite(uint16_t _width, uint16_t _height, const void* _data)
  733. {
  734. SpriteHandle handle = m_sprite.create(_width, _height);
  735. if (isValid(handle) )
  736. {
  737. const Pack2D& pack = m_sprite.get(handle);
  738. bgfx::updateTexture2D(
  739. m_texture
  740. , 0
  741. , 0
  742. , pack.m_x
  743. , pack.m_y
  744. , pack.m_width
  745. , pack.m_height
  746. , bgfx::copy(_data, pack.m_width*pack.m_height*4)
  747. );
  748. }
  749. return handle;
  750. }
  751. void destroy(SpriteHandle _handle)
  752. {
  753. m_sprite.destroy(_handle);
  754. }
  755. GeometryHandle createGeometry(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
  756. {
  757. return m_geometry.create(_numVertices, _vertices, _numIndices, _indices);
  758. }
  759. void destroy(GeometryHandle _handle)
  760. {
  761. m_geometry.destroy(_handle);
  762. }
  763. void begin(bgfx::ViewId _viewId)
  764. {
  765. BX_CHECK(State::Count == m_state);
  766. m_viewId = _viewId;
  767. m_state = State::None;
  768. m_stack = 0;
  769. Attrib& attrib = m_attrib[0];
  770. attrib.m_state = 0
  771. | BGFX_STATE_WRITE_RGB
  772. | (m_depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER)
  773. | BGFX_STATE_CULL_CW
  774. | BGFX_STATE_WRITE_Z
  775. ;
  776. attrib.m_scale = 1.0f;
  777. attrib.m_spin = 0.0f;
  778. attrib.m_offset = 0.0f;
  779. attrib.m_abgr = UINT32_MAX;
  780. attrib.m_stipple = false;
  781. attrib.m_wireframe = false;
  782. attrib.m_lod = 0;
  783. m_mtxStackCurrent = 0;
  784. m_mtxStack[m_mtxStackCurrent].reset();
  785. }
  786. void end()
  787. {
  788. BX_CHECK(0 == m_stack, "Invalid stack %d.", m_stack);
  789. flushQuad();
  790. flush();
  791. m_state = State::Count;
  792. }
  793. void push()
  794. {
  795. BX_CHECK(State::Count != m_state);
  796. ++m_stack;
  797. m_attrib[m_stack] = m_attrib[m_stack-1];
  798. }
  799. void pop()
  800. {
  801. BX_CHECK(State::Count != m_state);
  802. const Attrib& curr = m_attrib[m_stack];
  803. const Attrib& prev = m_attrib[m_stack-1];
  804. if (curr.m_stipple != prev.m_stipple
  805. || curr.m_state != prev.m_state)
  806. {
  807. flush();
  808. }
  809. --m_stack;
  810. }
  811. void setTransform(const void* _mtx, uint16_t _num = 1)
  812. {
  813. BX_CHECK(State::Count != m_state);
  814. flush();
  815. MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
  816. if (NULL == _mtx)
  817. {
  818. stack.reset();
  819. return;
  820. }
  821. bgfx::Transform transform;
  822. stack.mtx = bgfx::allocTransform(&transform, _num);
  823. stack.num = _num;
  824. stack.data = transform.data;
  825. bx::memCopy(transform.data, _mtx, _num*64);
  826. }
  827. void setTranslate(float _x, float _y, float _z)
  828. {
  829. float mtx[16];
  830. bx::mtxTranslate(mtx, _x, _y, _z);
  831. setTransform(mtx);
  832. }
  833. void setTranslate(const float* _pos)
  834. {
  835. setTranslate(_pos[0], _pos[1], _pos[2]);
  836. }
  837. void pushTransform(const void* _mtx, uint16_t _num)
  838. {
  839. BX_CHECK(m_mtxStackCurrent < BX_COUNTOF(m_mtxStack), "Out of matrix stack!");
  840. BX_CHECK(State::Count != m_state);
  841. flush();
  842. float* mtx = NULL;
  843. const MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
  844. if (NULL == stack.data)
  845. {
  846. mtx = (float*)_mtx;
  847. }
  848. else
  849. {
  850. mtx = (float*)alloca(_num*64);
  851. for (uint16_t ii = 0; ii < _num; ++ii)
  852. {
  853. const float* mtxTransform = (const float*)_mtx;
  854. bx::mtxMul(&mtx[ii*16], &mtxTransform[ii*16], stack.data);
  855. }
  856. }
  857. m_mtxStackCurrent++;
  858. setTransform(mtx, _num);
  859. }
  860. void popTransform()
  861. {
  862. BX_CHECK(State::Count != m_state);
  863. flush();
  864. m_mtxStackCurrent--;
  865. }
  866. void pushTranslate(float _x, float _y, float _z)
  867. {
  868. float mtx[16];
  869. bx::mtxTranslate(mtx, _x, _y, _z);
  870. pushTransform(mtx, 1);
  871. }
  872. void pushTranslate(const float* _pos)
  873. {
  874. pushTranslate(_pos[0], _pos[1], _pos[2]);
  875. }
  876. void setState(bool _depthTest, bool _depthWrite, bool _clockwise)
  877. {
  878. const uint64_t depthTest = m_depthTestLess
  879. ? BGFX_STATE_DEPTH_TEST_LESS
  880. : BGFX_STATE_DEPTH_TEST_GREATER
  881. ;
  882. uint64_t state = m_attrib[m_stack].m_state & ~(0
  883. | BGFX_STATE_DEPTH_TEST_MASK
  884. | BGFX_STATE_WRITE_Z
  885. | BGFX_STATE_CULL_CW
  886. | BGFX_STATE_CULL_CCW
  887. );
  888. state |= _depthTest
  889. ? depthTest
  890. : 0
  891. ;
  892. state |= _depthWrite
  893. ? BGFX_STATE_WRITE_Z
  894. : 0
  895. ;
  896. state |= _clockwise
  897. ? BGFX_STATE_CULL_CW
  898. : BGFX_STATE_CULL_CCW
  899. ;
  900. if (m_attrib[m_stack].m_state != state)
  901. {
  902. flush();
  903. }
  904. m_attrib[m_stack].m_state = state;
  905. }
  906. void setColor(uint32_t _abgr)
  907. {
  908. BX_CHECK(State::Count != m_state);
  909. m_attrib[m_stack].m_abgr = _abgr;
  910. }
  911. void setLod(uint8_t _lod)
  912. {
  913. BX_CHECK(State::Count != m_state);
  914. m_attrib[m_stack].m_lod = _lod;
  915. }
  916. void setWireframe(bool _wireframe)
  917. {
  918. BX_CHECK(State::Count != m_state);
  919. m_attrib[m_stack].m_wireframe = _wireframe;
  920. }
  921. void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
  922. {
  923. BX_CHECK(State::Count != m_state);
  924. Attrib& attrib = m_attrib[m_stack];
  925. if (attrib.m_stipple != _stipple)
  926. {
  927. flush();
  928. }
  929. attrib.m_stipple = _stipple;
  930. attrib.m_offset = _offset;
  931. attrib.m_scale = _scale;
  932. }
  933. void setSpin(float _spin)
  934. {
  935. Attrib& attrib = m_attrib[m_stack];
  936. attrib.m_spin = _spin;
  937. }
  938. void moveTo(float _x, float _y, float _z = 0.0f)
  939. {
  940. BX_CHECK(State::Count != m_state);
  941. softFlush();
  942. m_state = State::MoveTo;
  943. DebugVertex& vertex = m_cache[m_pos];
  944. vertex.m_x = _x;
  945. vertex.m_y = _y;
  946. vertex.m_z = _z;
  947. Attrib& attrib = m_attrib[m_stack];
  948. vertex.m_abgr = attrib.m_abgr;
  949. vertex.m_len = attrib.m_offset;
  950. m_vertexPos = m_pos;
  951. }
  952. void moveTo(const void* _pos)
  953. {
  954. BX_CHECK(State::Count != m_state);
  955. const float* pos = (const float*)_pos;
  956. moveTo(pos[0], pos[1], pos[2]);
  957. }
  958. void moveTo(Axis::Enum _axis, float _x, float _y)
  959. {
  960. float pos[3];
  961. getPoint(pos, _axis, _x, _y);
  962. moveTo(pos);
  963. }
  964. void lineTo(float _x, float _y, float _z = 0.0f)
  965. {
  966. BX_CHECK(State::Count != m_state);
  967. if (State::None == m_state)
  968. {
  969. moveTo(_x, _y, _z);
  970. return;
  971. }
  972. if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
  973. {
  974. uint32_t pos = m_pos;
  975. uint32_t vertexPos = m_vertexPos;
  976. flush();
  977. bx::memCopy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
  978. if (vertexPos == pos)
  979. {
  980. m_pos = 1;
  981. }
  982. else
  983. {
  984. bx::memCopy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
  985. m_pos = 2;
  986. }
  987. m_state = State::LineTo;
  988. }
  989. else if (State::MoveTo == m_state)
  990. {
  991. ++m_pos;
  992. m_state = State::LineTo;
  993. }
  994. uint16_t prev = m_pos-1;
  995. uint16_t curr = m_pos++;
  996. DebugVertex& vertex = m_cache[curr];
  997. vertex.m_x = _x;
  998. vertex.m_y = _y;
  999. vertex.m_z = _z;
  1000. Attrib& attrib = m_attrib[m_stack];
  1001. vertex.m_abgr = attrib.m_abgr;
  1002. vertex.m_len = attrib.m_offset;
  1003. float tmp[3];
  1004. bx::vec3Sub(tmp, &vertex.m_x, &m_cache[prev].m_x);
  1005. float len = bx::vec3Length(tmp) * attrib.m_scale;
  1006. vertex.m_len = m_cache[prev].m_len + len;
  1007. m_indices[m_indexPos++] = prev;
  1008. m_indices[m_indexPos++] = curr;
  1009. }
  1010. void lineTo(const void* _pos)
  1011. {
  1012. BX_CHECK(State::Count != m_state);
  1013. const float* pos = (const float*)_pos;
  1014. lineTo(pos[0], pos[1], pos[2]);
  1015. }
  1016. void lineTo(Axis::Enum _axis, float _x, float _y)
  1017. {
  1018. float pos[3];
  1019. getPoint(pos, _axis, _x, _y);
  1020. lineTo(pos);
  1021. }
  1022. void close()
  1023. {
  1024. BX_CHECK(State::Count != m_state);
  1025. DebugVertex& vertex = m_cache[m_vertexPos];
  1026. lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
  1027. m_state = State::None;
  1028. }
  1029. void draw(const Aabb& _aabb)
  1030. {
  1031. const Attrib& attrib = m_attrib[m_stack];
  1032. if (attrib.m_wireframe)
  1033. {
  1034. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  1035. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  1036. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  1037. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  1038. close();
  1039. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  1040. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  1041. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  1042. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  1043. close();
  1044. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  1045. lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  1046. moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  1047. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  1048. moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  1049. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  1050. moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  1051. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  1052. }
  1053. else
  1054. {
  1055. Obb obb;
  1056. aabbToObb(obb, _aabb);
  1057. draw(Mesh::Cube, obb.m_mtx, 1, false);
  1058. }
  1059. }
  1060. void draw(const Cylinder& _cylinder, bool _capsule)
  1061. {
  1062. drawCylinder(_cylinder.m_pos, _cylinder.m_end, _cylinder.m_radius, _capsule);
  1063. }
  1064. void draw(const Disk& _disk)
  1065. {
  1066. drawCircle(_disk.m_normal, _disk.m_center, _disk.m_radius, 0.0f);
  1067. }
  1068. void draw(const Obb& _obb)
  1069. {
  1070. const Attrib& attrib = m_attrib[m_stack];
  1071. if (attrib.m_wireframe)
  1072. {
  1073. pushTransform(_obb.m_mtx, 1);
  1074. moveTo(-1.0f, -1.0f, -1.0f);
  1075. lineTo( 1.0f, -1.0f, -1.0f);
  1076. lineTo( 1.0f, 1.0f, -1.0f);
  1077. lineTo(-1.0f, 1.0f, -1.0f);
  1078. close();
  1079. moveTo(-1.0f, 1.0f, 1.0f);
  1080. lineTo( 1.0f, 1.0f, 1.0f);
  1081. lineTo( 1.0f, -1.0f, 1.0f);
  1082. lineTo(-1.0f, -1.0f, 1.0f);
  1083. close();
  1084. moveTo( 1.0f, -1.0f, -1.0f);
  1085. lineTo( 1.0f, -1.0f, 1.0f);
  1086. moveTo( 1.0f, 1.0f, -1.0f);
  1087. lineTo( 1.0f, 1.0f, 1.0f);
  1088. moveTo(-1.0f, 1.0f, -1.0f);
  1089. lineTo(-1.0f, 1.0f, 1.0f);
  1090. moveTo(-1.0f, -1.0f, -1.0f);
  1091. lineTo(-1.0f, -1.0f, 1.0f);
  1092. popTransform();
  1093. }
  1094. else
  1095. {
  1096. draw(Mesh::Cube, _obb.m_mtx, 1, false);
  1097. }
  1098. }
  1099. void draw(const Sphere& _sphere)
  1100. {
  1101. const Attrib& attrib = m_attrib[m_stack];
  1102. float mtx[16];
  1103. bx::mtxSRT(mtx
  1104. , _sphere.m_radius
  1105. , _sphere.m_radius
  1106. , _sphere.m_radius
  1107. , 0.0f
  1108. , 0.0f
  1109. , 0.0f
  1110. , _sphere.m_center[0]
  1111. , _sphere.m_center[1]
  1112. , _sphere.m_center[2]
  1113. );
  1114. uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
  1115. ? uint8_t(Mesh::SphereMaxLod)
  1116. : attrib.m_lod
  1117. ;
  1118. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe);
  1119. }
  1120. void setUParams(const Attrib& _attrib, bool _wireframe)
  1121. {
  1122. const float flip = 0 == (_attrib.m_state & BGFX_STATE_CULL_CCW) ? 1.0f : -1.0f;
  1123. const uint8_t alpha = _attrib.m_abgr >> 24;
  1124. float params[4][4] =
  1125. {
  1126. { // lightDir
  1127. 0.0f * flip,
  1128. -1.0f * flip,
  1129. 0.0f * flip,
  1130. 3.0f, // shininess
  1131. },
  1132. { // skyColor
  1133. 1.0f,
  1134. 0.9f,
  1135. 0.8f,
  1136. 0.0f, // unused
  1137. },
  1138. { // groundColor.xyz0
  1139. 0.2f,
  1140. 0.22f,
  1141. 0.5f,
  1142. 0.0f, // unused
  1143. },
  1144. { // matColor
  1145. ( (_attrib.m_abgr) & 0xff) / 255.0f,
  1146. ( (_attrib.m_abgr >> 8) & 0xff) / 255.0f,
  1147. ( (_attrib.m_abgr >> 16) & 0xff) / 255.0f,
  1148. (alpha) / 255.0f,
  1149. },
  1150. };
  1151. bx::vec3Norm(params[0], params[0]);
  1152. bgfx::setUniform(u_params, params, 4);
  1153. bgfx::setState(0
  1154. | _attrib.m_state
  1155. | (_wireframe ? BGFX_STATE_PT_LINES | BGFX_STATE_LINEAA | BGFX_STATE_BLEND_ALPHA
  1156. : (alpha < 0xff) ? BGFX_STATE_BLEND_ALPHA : 0)
  1157. );
  1158. }
  1159. void draw(GeometryHandle _handle)
  1160. {
  1161. Geometry::Geometry& geometry = m_geometry.m_geometry[_handle.idx];
  1162. bgfx::setVertexBuffer(0, geometry.m_vbh);
  1163. const Attrib& attrib = m_attrib[m_stack];
  1164. const bool wireframe = attrib.m_wireframe;
  1165. setUParams(attrib, wireframe);
  1166. if (wireframe)
  1167. {
  1168. bgfx::setIndexBuffer(
  1169. geometry.m_ibh
  1170. , geometry.m_topologyNumIndices[0]
  1171. , geometry.m_topologyNumIndices[1]
  1172. );
  1173. }
  1174. else if (0 != geometry.m_topologyNumIndices[0])
  1175. {
  1176. bgfx::setIndexBuffer(
  1177. geometry.m_ibh
  1178. , 0
  1179. , geometry.m_topologyNumIndices[0]
  1180. );
  1181. }
  1182. bgfx::setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1183. bgfx::ProgramHandle program = m_program[wireframe ? Program::FillMesh : Program::FillLitMesh];
  1184. bgfx::submit(m_viewId, program);
  1185. }
  1186. void draw(bool _lineList, uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
  1187. {
  1188. flush();
  1189. if (_numVertices == bgfx::getAvailTransientVertexBuffer(_numVertices, DebugMeshVertex::ms_decl) )
  1190. {
  1191. bgfx::TransientVertexBuffer tvb;
  1192. bgfx::allocTransientVertexBuffer(&tvb, _numVertices, DebugMeshVertex::ms_decl);
  1193. bx::memCopy(tvb.data, _vertices, _numVertices * DebugMeshVertex::ms_decl.m_stride);
  1194. bgfx::setVertexBuffer(0, &tvb);
  1195. if (0 < _numIndices)
  1196. {
  1197. bgfx::TransientIndexBuffer tib;
  1198. bgfx::allocTransientIndexBuffer(&tib, _numIndices);
  1199. bx::memCopy(tib.data, _indices, _numIndices * sizeof(uint16_t) );
  1200. bgfx::setIndexBuffer(&tib);
  1201. }
  1202. const Attrib& attrib = m_attrib[m_stack];
  1203. const bool wireframe = _lineList;
  1204. setUParams(attrib, wireframe);
  1205. bgfx::setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1206. bgfx::ProgramHandle program = m_program[wireframe ? Program::FillMesh : Program::FillLitMesh];
  1207. bgfx::submit(m_viewId, program);
  1208. }
  1209. }
  1210. void drawFrustum(const float* _viewProj)
  1211. {
  1212. Plane planes[6];
  1213. buildFrustumPlanes(planes, _viewProj);
  1214. float points[24];
  1215. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  1216. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  1217. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  1218. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  1219. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  1220. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  1221. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  1222. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  1223. moveTo(&points[ 0]);
  1224. lineTo(&points[ 3]);
  1225. lineTo(&points[ 6]);
  1226. lineTo(&points[ 9]);
  1227. close();
  1228. moveTo(&points[12]);
  1229. lineTo(&points[15]);
  1230. lineTo(&points[18]);
  1231. lineTo(&points[21]);
  1232. close();
  1233. moveTo(&points[ 0]);
  1234. lineTo(&points[12]);
  1235. moveTo(&points[ 3]);
  1236. lineTo(&points[15]);
  1237. moveTo(&points[ 6]);
  1238. lineTo(&points[18]);
  1239. moveTo(&points[ 9]);
  1240. lineTo(&points[21]);
  1241. }
  1242. void drawFrustum(const void* _viewProj)
  1243. {
  1244. drawFrustum( (const float*)_viewProj);
  1245. }
  1246. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1247. {
  1248. const Attrib& attrib = m_attrib[m_stack];
  1249. const uint32_t num = getCircleLod(attrib.m_lod);
  1250. const float step = bx::kPi * 2.0f / num;
  1251. _degrees = bx::wrap(_degrees, 360.0f);
  1252. float pos[3];
  1253. getPoint(pos, _axis
  1254. , bx::sin(step * 0)*_radius
  1255. , bx::cos(step * 0)*_radius
  1256. );
  1257. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1258. uint32_t n = uint32_t(num*_degrees/360.0f);
  1259. for (uint32_t ii = 1; ii < n+1; ++ii)
  1260. {
  1261. getPoint(pos, _axis
  1262. , bx::sin(step * ii)*_radius
  1263. , bx::cos(step * ii)*_radius
  1264. );
  1265. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1266. }
  1267. moveTo(_x, _y, _z);
  1268. getPoint(pos, _axis
  1269. , bx::sin(step * 0)*_radius
  1270. , bx::cos(step * 0)*_radius
  1271. );
  1272. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1273. getPoint(pos, _axis
  1274. , bx::sin(step * n)*_radius
  1275. , bx::cos(step * n)*_radius
  1276. );
  1277. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1278. lineTo(_x, _y, _z);
  1279. }
  1280. void drawCircle(const float* _normal, const float* _center, float _radius, float _weight)
  1281. {
  1282. const Attrib& attrib = m_attrib[m_stack];
  1283. const uint32_t num = getCircleLod(attrib.m_lod);
  1284. const float step = bx::kPi * 2.0f / num;
  1285. _weight = bx::clamp(_weight, 0.0f, 2.0f);
  1286. float udir[3];
  1287. float vdir[3];
  1288. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1289. float pos[3];
  1290. float tmp0[3];
  1291. float tmp1[3];
  1292. float xy0[2];
  1293. float xy1[2];
  1294. circle(xy0, 0.0f);
  1295. squircle(xy1, 0.0f);
  1296. bx::vec3Mul(pos, udir, bx::lerp(xy0[0], xy1[0], _weight)*_radius);
  1297. bx::vec3Mul(tmp0, vdir, bx::lerp(xy0[1], xy1[1], _weight)*_radius);
  1298. bx::vec3Add(tmp1, pos, tmp0);
  1299. bx::vec3Add(pos, tmp1, _center);
  1300. moveTo(pos);
  1301. for (uint32_t ii = 1; ii < num; ++ii)
  1302. {
  1303. float angle = step * ii;
  1304. circle(xy0, angle);
  1305. squircle(xy1, angle);
  1306. bx::vec3Mul(pos, udir, bx::lerp(xy0[0], xy1[0], _weight)*_radius);
  1307. bx::vec3Mul(tmp0, vdir, bx::lerp(xy0[1], xy1[1], _weight)*_radius);
  1308. bx::vec3Add(tmp1, pos, tmp0);
  1309. bx::vec3Add(pos, tmp1, _center);
  1310. lineTo(pos);
  1311. }
  1312. close();
  1313. }
  1314. void drawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1315. {
  1316. drawCircle( (const float*)_normal, (const float*)_center, _radius, _weight);
  1317. }
  1318. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1319. {
  1320. const Attrib& attrib = m_attrib[m_stack];
  1321. const uint32_t num = getCircleLod(attrib.m_lod);
  1322. const float step = bx::kPi * 2.0f / num;
  1323. _weight = bx::clamp(_weight, 0.0f, 2.0f);
  1324. float xy0[2];
  1325. float xy1[2];
  1326. circle(xy0, 0.0f);
  1327. squircle(xy1, 0.0f);
  1328. float pos[3];
  1329. getPoint(pos, _axis
  1330. , bx::lerp(xy0[0], xy1[0], _weight)*_radius
  1331. , bx::lerp(xy0[1], xy1[1], _weight)*_radius
  1332. );
  1333. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1334. for (uint32_t ii = 1; ii < num; ++ii)
  1335. {
  1336. float angle = step * ii;
  1337. circle(xy0, angle);
  1338. squircle(xy1, angle);
  1339. getPoint(pos, _axis
  1340. , bx::lerp(xy0[0], xy1[0], _weight)*_radius
  1341. , bx::lerp(xy0[1], xy1[1], _weight)*_radius
  1342. );
  1343. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1344. }
  1345. close();
  1346. }
  1347. void drawQuad(const float* _normal, const float* _center, float _size)
  1348. {
  1349. const Attrib& attrib = m_attrib[m_stack];
  1350. float udir[3];
  1351. float vdir[3];
  1352. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1353. const float halfExtent = _size*0.5f;
  1354. float umin[3];
  1355. bx::vec3Mul(umin, udir, -halfExtent);
  1356. float umax[3];
  1357. bx::vec3Mul(umax, udir, halfExtent);
  1358. float vmin[3];
  1359. bx::vec3Mul(vmin, vdir, -halfExtent);
  1360. float vmax[3];
  1361. bx::vec3Mul(vmax, vdir, halfExtent);
  1362. float pt[3];
  1363. float tmp[3];
  1364. bx::vec3Add(tmp, umin, vmin);
  1365. bx::vec3Add(pt, _center, tmp);
  1366. moveTo(pt);
  1367. bx::vec3Add(tmp, umax, vmin);
  1368. bx::vec3Add(pt, _center, tmp);
  1369. lineTo(pt);
  1370. bx::vec3Add(tmp, umax, vmax);
  1371. bx::vec3Add(pt, _center, tmp);
  1372. lineTo(pt);
  1373. bx::vec3Add(tmp, umin, vmax);
  1374. bx::vec3Add(pt, _center, tmp);
  1375. lineTo(pt);
  1376. close();
  1377. }
  1378. void drawQuad(SpriteHandle _handle, const float* _normal, const float* _center, float _size)
  1379. {
  1380. if (m_posQuad == BX_COUNTOF(m_cacheQuad) )
  1381. {
  1382. flushQuad();
  1383. }
  1384. const Attrib& attrib = m_attrib[m_stack];
  1385. float udir[3];
  1386. float vdir[3];
  1387. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1388. const Pack2D& pack = m_sprite.get(_handle);
  1389. const float invTextureSize = 1.0f/SPRITE_TEXTURE_SIZE;
  1390. const float us = pack.m_x * invTextureSize;
  1391. const float vs = pack.m_y * invTextureSize;
  1392. const float ue = (pack.m_x + pack.m_width ) * invTextureSize;
  1393. const float ve = (pack.m_y + pack.m_height) * invTextureSize;
  1394. const float aspectRatio = float(pack.m_width)/float(pack.m_height);
  1395. const float halfExtentU = aspectRatio*_size*0.5f;
  1396. const float halfExtentV = 1.0f/aspectRatio*_size*0.5f;
  1397. float umin[3];
  1398. bx::vec3Mul(umin, udir, -halfExtentU);
  1399. float umax[3];
  1400. bx::vec3Mul(umax, udir, halfExtentU);
  1401. float vmin[3];
  1402. bx::vec3Mul(vmin, vdir, -halfExtentV);
  1403. float vmax[3];
  1404. bx::vec3Mul(vmax, vdir, halfExtentV);
  1405. DebugUvVertex* vertex = &m_cacheQuad[m_posQuad];
  1406. m_posQuad += 4;
  1407. float pt[3];
  1408. float tmp[3];
  1409. bx::vec3Add(tmp, umin, vmin);
  1410. bx::vec3Add(pt, _center, tmp);
  1411. vertex->m_x = pt[0];
  1412. vertex->m_y = pt[1];
  1413. vertex->m_z = pt[2];
  1414. vertex->m_u = us;
  1415. vertex->m_v = vs;
  1416. vertex->m_abgr = attrib.m_abgr;
  1417. ++vertex;
  1418. bx::vec3Add(tmp, umax, vmin);
  1419. bx::vec3Add(pt, _center, tmp);
  1420. vertex->m_x = pt[0];
  1421. vertex->m_y = pt[1];
  1422. vertex->m_z = pt[2];
  1423. vertex->m_u = ue;
  1424. vertex->m_v = vs;
  1425. vertex->m_abgr = attrib.m_abgr;
  1426. ++vertex;
  1427. bx::vec3Add(tmp, umin, vmax);
  1428. bx::vec3Add(pt, _center, tmp);
  1429. vertex->m_x = pt[0];
  1430. vertex->m_y = pt[1];
  1431. vertex->m_z = pt[2];
  1432. vertex->m_u = us;
  1433. vertex->m_v = ve;
  1434. vertex->m_abgr = attrib.m_abgr;
  1435. ++vertex;
  1436. bx::vec3Add(tmp, umax, vmax);
  1437. bx::vec3Add(pt, _center, tmp);
  1438. vertex->m_x = pt[0];
  1439. vertex->m_y = pt[1];
  1440. vertex->m_z = pt[2];
  1441. vertex->m_u = ue;
  1442. vertex->m_v = ve;
  1443. vertex->m_abgr = attrib.m_abgr;
  1444. ++vertex;
  1445. }
  1446. void drawQuad(bgfx::TextureHandle _handle, const float* _normal, const float* _center, float _size)
  1447. {
  1448. BX_UNUSED(_handle, _normal, _center, _size);
  1449. }
  1450. void drawCone(const float* _from, const float* _to, float _radius)
  1451. {
  1452. const Attrib& attrib = m_attrib[m_stack];
  1453. float tmp0[3];
  1454. bx::vec3Sub(tmp0, _from, _to);
  1455. float normal[3];
  1456. bx::vec3Norm(normal, tmp0);
  1457. float mtx[2][16];
  1458. bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
  1459. bx::memCopy(mtx[1], mtx[0], 64);
  1460. mtx[1][12] = _to[0];
  1461. mtx[1][13] = _to[1];
  1462. mtx[1][14] = _to[2];
  1463. uint8_t lod = attrib.m_lod > Mesh::ConeMaxLod
  1464. ? uint8_t(Mesh::ConeMaxLod)
  1465. : attrib.m_lod
  1466. ;
  1467. draw(Mesh::Enum(Mesh::Cone0 + lod), mtx[0], 2, attrib.m_wireframe);
  1468. }
  1469. void drawCone(const void* _from, const void* _to, float _radius)
  1470. {
  1471. drawCone( (const float*)_from, (const float*)_to, _radius);
  1472. }
  1473. void drawCylinder(const float* _from, const float* _to, float _radius, bool _capsule)
  1474. {
  1475. const Attrib& attrib = m_attrib[m_stack];
  1476. float tmp0[3];
  1477. bx::vec3Sub(tmp0, _from, _to);
  1478. float normal[3];
  1479. bx::vec3Norm(normal, tmp0);
  1480. float mtx[2][16];
  1481. bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
  1482. bx::memCopy(mtx[1], mtx[0], 64);
  1483. mtx[1][12] = _to[0];
  1484. mtx[1][13] = _to[1];
  1485. mtx[1][14] = _to[2];
  1486. if (_capsule)
  1487. {
  1488. uint8_t lod = attrib.m_lod > Mesh::CapsuleMaxLod
  1489. ? uint8_t(Mesh::CapsuleMaxLod)
  1490. : attrib.m_lod
  1491. ;
  1492. draw(Mesh::Enum(Mesh::Capsule0 + lod), mtx[0], 2, attrib.m_wireframe);
  1493. Sphere sphere;
  1494. bx::vec3Move(sphere.m_center, _from);
  1495. sphere.m_radius = _radius;
  1496. draw(sphere);
  1497. bx::vec3Move(sphere.m_center, _to);
  1498. draw(sphere);
  1499. }
  1500. else
  1501. {
  1502. uint8_t lod = attrib.m_lod > Mesh::CylinderMaxLod
  1503. ? uint8_t(Mesh::CylinderMaxLod)
  1504. : attrib.m_lod
  1505. ;
  1506. draw(Mesh::Enum(Mesh::Cylinder0 + lod), mtx[0], 2, attrib.m_wireframe);
  1507. }
  1508. }
  1509. void drawCylinder(const void* _from, const void* _to, float _radius, bool _capsule)
  1510. {
  1511. drawCylinder( (const float*)_from, (const float*)_to, _radius, _capsule);
  1512. }
  1513. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight, float _thickness)
  1514. {
  1515. push();
  1516. if (_thickness > 0.0f)
  1517. {
  1518. float from[3] = { _x, _y, _z };
  1519. float mid[3];
  1520. float to[3];
  1521. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1522. mid[0] = _x + _len - _thickness;
  1523. mid[1] = _y;
  1524. mid[2] = _z;
  1525. to[0] = _x + _len;
  1526. to[1] = _y;
  1527. to[2] = _z;
  1528. drawCylinder(from, mid, _thickness, false);
  1529. drawCone(mid, to, _thickness);
  1530. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1531. mid[0] = _x;
  1532. mid[1] = _y + _len - _thickness;
  1533. mid[2] = _z;
  1534. to[0] = _x;
  1535. to[1] = _y + _len;
  1536. to[2] = _z;
  1537. drawCylinder(from, mid, _thickness, false);
  1538. drawCone(mid, to, _thickness);
  1539. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1540. mid[0] = _x;
  1541. mid[1] = _y;
  1542. mid[2] = _z + _len - _thickness;
  1543. to[0] = _x;
  1544. to[1] = _y;
  1545. to[2] = _z + _len;
  1546. drawCylinder(from, mid, _thickness, false);
  1547. drawCone(mid, to, _thickness);
  1548. }
  1549. else
  1550. {
  1551. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1552. moveTo(_x, _y, _z);
  1553. lineTo(_x + _len, _y, _z);
  1554. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1555. moveTo(_x, _y, _z);
  1556. lineTo(_x, _y + _len, _z);
  1557. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1558. moveTo(_x, _y, _z);
  1559. lineTo(_x, _y, _z + _len);
  1560. }
  1561. pop();
  1562. }
  1563. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  1564. {
  1565. const Attrib& attrib = m_attrib[m_stack];
  1566. float udir[3];
  1567. float vdir[3];
  1568. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1569. bx::vec3Mul(udir, udir, _step);
  1570. bx::vec3Mul(vdir, vdir, _step);
  1571. const uint32_t num = (_size/2)*2+1;
  1572. const float halfExtent = float(_size/2);
  1573. float umin[3];
  1574. bx::vec3Mul(umin, udir, -halfExtent);
  1575. float umax[3];
  1576. bx::vec3Mul(umax, udir, halfExtent);
  1577. float vmin[3];
  1578. bx::vec3Mul(vmin, vdir, -halfExtent);
  1579. float vmax[3];
  1580. bx::vec3Mul(vmax, vdir, halfExtent);
  1581. float tmp[3];
  1582. float xs[3];
  1583. float xe[3];
  1584. bx::vec3Add(tmp, umin, vmin);
  1585. bx::vec3Add(xs, _center, tmp);
  1586. bx::vec3Add(tmp, umax, vmin);
  1587. bx::vec3Add(xe, _center, tmp);
  1588. float ys[3];
  1589. float ye[3];
  1590. bx::vec3Add(tmp, umin, vmin);
  1591. bx::vec3Add(ys, _center, tmp);
  1592. bx::vec3Add(tmp, umin, vmax);
  1593. bx::vec3Add(ye, _center, tmp);
  1594. for (uint32_t ii = 0; ii < num; ++ii)
  1595. {
  1596. moveTo(xs);
  1597. lineTo(xe);
  1598. bx::vec3Add(xs, xs, vdir);
  1599. bx::vec3Add(xe, xe, vdir);
  1600. moveTo(ys);
  1601. lineTo(ye);
  1602. bx::vec3Add(ys, ys, udir);
  1603. bx::vec3Add(ye, ye, udir);
  1604. }
  1605. }
  1606. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1607. {
  1608. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  1609. }
  1610. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  1611. {
  1612. push();
  1613. pushTranslate(_center);
  1614. const uint32_t num = (_size/2)*2-1;
  1615. const float halfExtent = float(_size/2) * _step;
  1616. setColor(0xff606060);
  1617. float yy = -halfExtent + _step;
  1618. for (uint32_t ii = 0; ii < num; ++ii)
  1619. {
  1620. moveTo(_axis, -halfExtent, yy);
  1621. lineTo(_axis, halfExtent, yy);
  1622. moveTo(_axis, yy, -halfExtent);
  1623. lineTo(_axis, yy, halfExtent);
  1624. yy += _step;
  1625. }
  1626. setColor(0xff101010);
  1627. moveTo(_axis, -halfExtent, -halfExtent);
  1628. lineTo(_axis, -halfExtent, halfExtent);
  1629. lineTo(_axis, halfExtent, halfExtent);
  1630. lineTo(_axis, halfExtent, -halfExtent);
  1631. close();
  1632. moveTo(_axis, -halfExtent, 0.0f);
  1633. lineTo(_axis, halfExtent, 0.0f);
  1634. moveTo(_axis, 0.0f, -halfExtent);
  1635. lineTo(_axis, 0.0f, halfExtent);
  1636. pop();
  1637. }
  1638. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1639. {
  1640. drawGrid(_axis, (const float*)_center, _size, _step);
  1641. }
  1642. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1643. {
  1644. push();
  1645. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  1646. drawCircle(Axis::X, _x, _y, _z, _radius, 0.0f);
  1647. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  1648. drawCircle(Axis::Y, _x, _y, _z, _radius, 0.0f);
  1649. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  1650. drawCircle(Axis::Z, _x, _y, _z, _radius, 0.0f);
  1651. pop();
  1652. }
  1653. private:
  1654. struct Mesh
  1655. {
  1656. enum Enum
  1657. {
  1658. Sphere0,
  1659. Sphere1,
  1660. Sphere2,
  1661. Sphere3,
  1662. Cone0,
  1663. Cone1,
  1664. Cone2,
  1665. Cone3,
  1666. Cylinder0,
  1667. Cylinder1,
  1668. Cylinder2,
  1669. Cylinder3,
  1670. Capsule0,
  1671. Capsule1,
  1672. Capsule2,
  1673. Capsule3,
  1674. Cube,
  1675. Count,
  1676. SphereMaxLod = Sphere3 - Sphere0,
  1677. ConeMaxLod = Cone3 - Cone0,
  1678. CylinderMaxLod = Cylinder3 - Cylinder0,
  1679. CapsuleMaxLod = Capsule3 - Capsule0,
  1680. };
  1681. uint32_t m_startVertex;
  1682. uint32_t m_numVertices;
  1683. uint32_t m_startIndex[2];
  1684. uint32_t m_numIndices[2];
  1685. };
  1686. struct Program
  1687. {
  1688. enum Enum
  1689. {
  1690. Lines,
  1691. LinesStipple,
  1692. Fill,
  1693. FillMesh,
  1694. FillLit,
  1695. FillLitMesh,
  1696. FillTexture,
  1697. Count
  1698. };
  1699. };
  1700. void draw(Mesh::Enum _mesh, const float* _mtx, uint16_t _num, bool _wireframe)
  1701. {
  1702. pushTransform(_mtx, _num);
  1703. const Mesh& mesh = m_mesh[_mesh];
  1704. if (0 != mesh.m_numIndices[_wireframe])
  1705. {
  1706. bgfx::setIndexBuffer(m_ibh
  1707. , mesh.m_startIndex[_wireframe]
  1708. , mesh.m_numIndices[_wireframe]
  1709. );
  1710. }
  1711. const Attrib& attrib = m_attrib[m_stack];
  1712. setUParams(attrib, _wireframe);
  1713. MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
  1714. bgfx::setTransform(stack.mtx, stack.num);
  1715. bgfx::setVertexBuffer(0, m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1716. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1717. popTransform();
  1718. }
  1719. void softFlush()
  1720. {
  1721. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1722. {
  1723. flush();
  1724. }
  1725. }
  1726. void flush()
  1727. {
  1728. if (0 != m_pos)
  1729. {
  1730. if (checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  1731. {
  1732. bgfx::TransientVertexBuffer tvb;
  1733. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  1734. bx::memCopy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  1735. bgfx::TransientIndexBuffer tib;
  1736. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1737. bx::memCopy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1738. const Attrib& attrib = m_attrib[m_stack];
  1739. bgfx::setVertexBuffer(0, &tvb);
  1740. bgfx::setIndexBuffer(&tib);
  1741. bgfx::setState(0
  1742. | BGFX_STATE_WRITE_RGB
  1743. | BGFX_STATE_PT_LINES
  1744. | attrib.m_state
  1745. | BGFX_STATE_LINEAA
  1746. | BGFX_STATE_BLEND_ALPHA
  1747. );
  1748. bgfx::setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1749. bgfx::ProgramHandle program = m_program[attrib.m_stipple ? 1 : 0];
  1750. bgfx::submit(m_viewId, program);
  1751. }
  1752. m_state = State::None;
  1753. m_pos = 0;
  1754. m_indexPos = 0;
  1755. m_vertexPos = 0;
  1756. }
  1757. }
  1758. void flushQuad()
  1759. {
  1760. if (0 != m_posQuad)
  1761. {
  1762. const uint32_t numIndices = m_posQuad/4*6;
  1763. if (checkAvailTransientBuffers(m_posQuad, DebugUvVertex::ms_decl, numIndices) )
  1764. {
  1765. bgfx::TransientVertexBuffer tvb;
  1766. bgfx::allocTransientVertexBuffer(&tvb, m_posQuad, DebugUvVertex::ms_decl);
  1767. bx::memCopy(tvb.data, m_cacheQuad, m_posQuad * DebugUvVertex::ms_decl.m_stride);
  1768. bgfx::TransientIndexBuffer tib;
  1769. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1770. uint16_t* indices = (uint16_t*)tib.data;
  1771. for (uint16_t ii = 0, num = m_posQuad/4; ii < num; ++ii)
  1772. {
  1773. uint16_t startVertex = ii*4;
  1774. indices[0] = startVertex+0;
  1775. indices[1] = startVertex+1;
  1776. indices[2] = startVertex+2;
  1777. indices[3] = startVertex+1;
  1778. indices[4] = startVertex+3;
  1779. indices[5] = startVertex+2;
  1780. indices += 6;
  1781. }
  1782. const Attrib& attrib = m_attrib[m_stack];
  1783. bgfx::setVertexBuffer(0, &tvb);
  1784. bgfx::setIndexBuffer(&tib);
  1785. bgfx::setState(0
  1786. | (attrib.m_state & ~BGFX_STATE_CULL_MASK)
  1787. );
  1788. bgfx::setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1789. bgfx::setTexture(0, s_texColor, m_texture);
  1790. bgfx::submit(m_viewId, m_program[Program::FillTexture]);
  1791. }
  1792. m_posQuad = 0;
  1793. }
  1794. }
  1795. struct State
  1796. {
  1797. enum Enum
  1798. {
  1799. None,
  1800. MoveTo,
  1801. LineTo,
  1802. Count
  1803. };
  1804. };
  1805. static const uint32_t cacheSize = 1024;
  1806. static const uint32_t stackSize = 16;
  1807. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1808. DebugVertex m_cache[cacheSize+1];
  1809. uint16_t m_indices[cacheSize*2];
  1810. uint16_t m_pos;
  1811. uint16_t m_indexPos;
  1812. uint16_t m_vertexPos;
  1813. static const uint32_t cacheQuadSize = 1024;
  1814. DebugUvVertex m_cacheQuad[cacheQuadSize];
  1815. uint16_t m_posQuad;
  1816. uint32_t m_mtxStackCurrent;
  1817. struct MatrixStack
  1818. {
  1819. void reset()
  1820. {
  1821. mtx = 0;
  1822. num = 1;
  1823. data = NULL;
  1824. }
  1825. uint32_t mtx;
  1826. uint16_t num;
  1827. float* data;
  1828. };
  1829. MatrixStack m_mtxStack[32];
  1830. bgfx::ViewId m_viewId;
  1831. uint8_t m_stack;
  1832. bool m_depthTestLess;
  1833. Attrib m_attrib[stackSize];
  1834. State::Enum m_state;
  1835. Mesh m_mesh[Mesh::Count];
  1836. typedef SpriteT<256, SPRITE_TEXTURE_SIZE> Sprite;
  1837. Sprite m_sprite;
  1838. typedef GeometryT<256> Geometry;
  1839. Geometry m_geometry;
  1840. bgfx::UniformHandle s_texColor;
  1841. bgfx::TextureHandle m_texture;
  1842. bgfx::ProgramHandle m_program[Program::Count];
  1843. bgfx::UniformHandle u_params;
  1844. bgfx::VertexBufferHandle m_vbh;
  1845. bgfx::IndexBufferHandle m_ibh;
  1846. bx::AllocatorI* m_allocator;
  1847. };
  1848. static DebugDraw s_dd;
  1849. void ddInit(bool _depthTestLess, bx::AllocatorI* _allocator)
  1850. {
  1851. s_dd.init(_depthTestLess, _allocator);
  1852. }
  1853. void ddShutdown()
  1854. {
  1855. s_dd.shutdown();
  1856. }
  1857. SpriteHandle ddCreateSprite(uint16_t _width, uint16_t _height, const void* _data)
  1858. {
  1859. return s_dd.createSprite(_width, _height, _data);
  1860. }
  1861. void ddDestroy(SpriteHandle _handle)
  1862. {
  1863. s_dd.destroy(_handle);
  1864. }
  1865. GeometryHandle ddCreateGeometry(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
  1866. {
  1867. return s_dd.createGeometry(_numVertices, _vertices, _numIndices, _indices);
  1868. }
  1869. void ddDestroy(GeometryHandle _handle)
  1870. {
  1871. s_dd.destroy(_handle);
  1872. }
  1873. void ddBegin(uint16_t _viewId)
  1874. {
  1875. s_dd.begin(_viewId);
  1876. }
  1877. void ddEnd()
  1878. {
  1879. s_dd.end();
  1880. }
  1881. void ddPush()
  1882. {
  1883. s_dd.push();
  1884. }
  1885. void ddPop()
  1886. {
  1887. s_dd.pop();
  1888. }
  1889. void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1890. {
  1891. s_dd.setState(_depthTest, _depthWrite, _clockwise);
  1892. }
  1893. void ddSetColor(uint32_t _abgr)
  1894. {
  1895. s_dd.setColor(_abgr);
  1896. }
  1897. void ddSetLod(uint8_t _lod)
  1898. {
  1899. s_dd.setLod(_lod);
  1900. }
  1901. void ddSetWireframe(bool _wireframe)
  1902. {
  1903. s_dd.setWireframe(_wireframe);
  1904. }
  1905. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1906. {
  1907. s_dd.setStipple(_stipple, _scale, _offset);
  1908. }
  1909. void ddSetSpin(float _spin)
  1910. {
  1911. s_dd.setSpin(_spin);
  1912. }
  1913. void ddSetTransform(const void* _mtx)
  1914. {
  1915. s_dd.setTransform(_mtx);
  1916. }
  1917. void ddSetTranslate(float _x, float _y, float _z)
  1918. {
  1919. s_dd.setTranslate(_x, _y, _z);
  1920. }
  1921. void ddMoveTo(float _x, float _y, float _z)
  1922. {
  1923. s_dd.moveTo(_x, _y, _z);
  1924. }
  1925. void ddMoveTo(const void* _pos)
  1926. {
  1927. s_dd.moveTo(_pos);
  1928. }
  1929. void ddLineTo(float _x, float _y, float _z)
  1930. {
  1931. s_dd.lineTo(_x, _y, _z);
  1932. }
  1933. void ddLineTo(const void* _pos)
  1934. {
  1935. s_dd.lineTo(_pos);
  1936. }
  1937. void ddClose()
  1938. {
  1939. s_dd.close();
  1940. }
  1941. void ddDraw(const Aabb& _aabb)
  1942. {
  1943. s_dd.draw(_aabb);
  1944. }
  1945. void ddDraw(const Cylinder& _cylinder)
  1946. {
  1947. s_dd.draw(_cylinder, false);
  1948. }
  1949. void ddDraw(const Capsule& _capsule)
  1950. {
  1951. s_dd.draw( *( (const Cylinder*)&_capsule), true);
  1952. }
  1953. void ddDraw(const Disk& _disk)
  1954. {
  1955. s_dd.draw(_disk);
  1956. }
  1957. void ddDraw(const Obb& _obb)
  1958. {
  1959. s_dd.draw(_obb);
  1960. }
  1961. void ddDraw(const Sphere& _sphere)
  1962. {
  1963. s_dd.draw(_sphere);
  1964. }
  1965. void ddDraw(const Cone& _cone)
  1966. {
  1967. ddDrawCone(_cone.m_pos, _cone.m_end, _cone.m_radius);
  1968. }
  1969. void ddDraw(GeometryHandle _handle)
  1970. {
  1971. s_dd.draw(_handle);
  1972. }
  1973. void ddDrawLineList(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
  1974. {
  1975. s_dd.draw(true, _numVertices, _vertices, _numIndices, _indices);
  1976. }
  1977. void ddDrawTriList(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
  1978. {
  1979. s_dd.draw(false, _numVertices, _vertices, _numIndices, _indices);
  1980. }
  1981. void ddDrawFrustum(const void* _viewProj)
  1982. {
  1983. s_dd.drawFrustum(_viewProj);
  1984. }
  1985. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1986. {
  1987. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1988. }
  1989. void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1990. {
  1991. s_dd.drawCircle(_normal, _center, _radius, _weight);
  1992. }
  1993. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1994. {
  1995. s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight);
  1996. }
  1997. void ddDrawQuad(const float* _normal, const float* _center, float _size)
  1998. {
  1999. s_dd.drawQuad(_normal, _center, _size);
  2000. }
  2001. void ddDrawQuad(SpriteHandle _handle, const float* _normal, const float* _center, float _size)
  2002. {
  2003. s_dd.drawQuad(_handle, _normal, _center, _size);
  2004. }
  2005. void ddDrawQuad(bgfx::TextureHandle _handle, const float* _normal, const float* _center, float _size)
  2006. {
  2007. s_dd.drawQuad(_handle, _normal, _center, _size);
  2008. }
  2009. void ddDrawCone(const void* _from, const void* _to, float _radius)
  2010. {
  2011. s_dd.drawCone(_from, _to, _radius);
  2012. }
  2013. void ddDrawCylinder(const void* _from, const void* _to, float _radius)
  2014. {
  2015. s_dd.drawCylinder(_from, _to, _radius, false);
  2016. }
  2017. void ddDrawCapsule(const void* _from, const void* _to, float _radius)
  2018. {
  2019. s_dd.drawCylinder(_from, _to, _radius, true);
  2020. }
  2021. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight, float _thickness)
  2022. {
  2023. s_dd.drawAxis(_x, _y, _z, _len, _hightlight, _thickness);
  2024. }
  2025. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  2026. {
  2027. s_dd.drawGrid(_normal, _center, _size, _step);
  2028. }
  2029. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  2030. {
  2031. s_dd.drawGrid(_axis, _center, _size, _step);
  2032. }
  2033. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  2034. {
  2035. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  2036. }