debugdraw.cpp 52 KB

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