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. const Attrib& attrib = m_attrib[m_stack];
  908. if (attrib.m_wireframe)
  909. {
  910. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  911. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  912. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  913. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  914. close();
  915. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  916. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  917. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  918. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  919. close();
  920. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  921. lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  922. moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  923. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  924. moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  925. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  926. moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  927. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  928. }
  929. else
  930. {
  931. Obb obb;
  932. aabbToObb(obb, _aabb);
  933. draw(Mesh::Cube, obb.m_mtx, 1, false);
  934. }
  935. }
  936. void draw(const Cylinder& _cylinder, bool _capsule)
  937. {
  938. drawCylinder(_cylinder.m_pos, _cylinder.m_end, _cylinder.m_radius, _capsule);
  939. }
  940. void draw(const Disk& _disk)
  941. {
  942. drawCircle(_disk.m_normal, _disk.m_center, _disk.m_radius, 0.0f);
  943. }
  944. void draw(const Obb& _obb)
  945. {
  946. const Attrib& attrib = m_attrib[m_stack];
  947. if (attrib.m_wireframe)
  948. {
  949. pushTransform(_obb.m_mtx, 1);
  950. moveTo(-1.0f, -1.0f, -1.0f);
  951. lineTo( 1.0f, -1.0f, -1.0f);
  952. lineTo( 1.0f, 1.0f, -1.0f);
  953. lineTo(-1.0f, 1.0f, -1.0f);
  954. close();
  955. moveTo(-1.0f, 1.0f, 1.0f);
  956. lineTo( 1.0f, 1.0f, 1.0f);
  957. lineTo( 1.0f, -1.0f, 1.0f);
  958. lineTo(-1.0f, -1.0f, 1.0f);
  959. close();
  960. moveTo( 1.0f, -1.0f, -1.0f);
  961. lineTo( 1.0f, -1.0f, 1.0f);
  962. moveTo( 1.0f, 1.0f, -1.0f);
  963. lineTo( 1.0f, 1.0f, 1.0f);
  964. moveTo(-1.0f, 1.0f, -1.0f);
  965. lineTo(-1.0f, 1.0f, 1.0f);
  966. moveTo(-1.0f, -1.0f, -1.0f);
  967. lineTo(-1.0f, -1.0f, 1.0f);
  968. popTransform();
  969. }
  970. else
  971. {
  972. draw(Mesh::Cube, _obb.m_mtx, 1, false);
  973. }
  974. }
  975. void draw(const Sphere& _sphere)
  976. {
  977. const Attrib& attrib = m_attrib[m_stack];
  978. float mtx[16];
  979. bx::mtxSRT(mtx
  980. , _sphere.m_radius
  981. , _sphere.m_radius
  982. , _sphere.m_radius
  983. , 0.0f
  984. , 0.0f
  985. , 0.0f
  986. , _sphere.m_center[0]
  987. , _sphere.m_center[1]
  988. , _sphere.m_center[2]
  989. );
  990. uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
  991. ? uint8_t(Mesh::SphereMaxLod)
  992. : attrib.m_lod
  993. ;
  994. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe);
  995. }
  996. void drawFrustum(const float* _viewProj)
  997. {
  998. Plane planes[6];
  999. buildFrustumPlanes(planes, _viewProj);
  1000. float points[24];
  1001. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  1002. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  1003. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  1004. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  1005. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  1006. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  1007. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  1008. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  1009. moveTo(&points[ 0]);
  1010. lineTo(&points[ 3]);
  1011. lineTo(&points[ 6]);
  1012. lineTo(&points[ 9]);
  1013. close();
  1014. moveTo(&points[12]);
  1015. lineTo(&points[15]);
  1016. lineTo(&points[18]);
  1017. lineTo(&points[21]);
  1018. close();
  1019. moveTo(&points[ 0]);
  1020. lineTo(&points[12]);
  1021. moveTo(&points[ 3]);
  1022. lineTo(&points[15]);
  1023. moveTo(&points[ 6]);
  1024. lineTo(&points[18]);
  1025. moveTo(&points[ 9]);
  1026. lineTo(&points[21]);
  1027. }
  1028. void drawFrustum(const void* _viewProj)
  1029. {
  1030. drawFrustum( (const float*)_viewProj);
  1031. }
  1032. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1033. {
  1034. const Attrib& attrib = m_attrib[m_stack];
  1035. const uint32_t num = getCircleLod(attrib.m_lod);
  1036. const float step = bx::kPi * 2.0f / num;
  1037. _degrees = bx::fwrap(_degrees, 360.0f);
  1038. float pos[3];
  1039. getPoint(pos, _axis
  1040. , bx::fsin(step * 0)*_radius
  1041. , bx::fcos(step * 0)*_radius
  1042. );
  1043. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1044. uint32_t n = uint32_t(num*_degrees/360.0f);
  1045. for (uint32_t ii = 1; ii < n+1; ++ii)
  1046. {
  1047. getPoint(pos, _axis
  1048. , bx::fsin(step * ii)*_radius
  1049. , bx::fcos(step * ii)*_radius
  1050. );
  1051. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1052. }
  1053. moveTo(_x, _y, _z);
  1054. getPoint(pos, _axis
  1055. , bx::fsin(step * 0)*_radius
  1056. , bx::fcos(step * 0)*_radius
  1057. );
  1058. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1059. getPoint(pos, _axis
  1060. , bx::fsin(step * n)*_radius
  1061. , bx::fcos(step * n)*_radius
  1062. );
  1063. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1064. lineTo(_x, _y, _z);
  1065. }
  1066. void drawCircle(const float* _normal, const float* _center, float _radius, float _weight)
  1067. {
  1068. const Attrib& attrib = m_attrib[m_stack];
  1069. const uint32_t num = getCircleLod(attrib.m_lod);
  1070. const float step = bx::kPi * 2.0f / num;
  1071. _weight = bx::clamp(_weight, 0.0f, 2.0f);
  1072. float udir[3];
  1073. float vdir[3];
  1074. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1075. float pos[3];
  1076. float tmp0[3];
  1077. float tmp1[3];
  1078. float xy0[2];
  1079. float xy1[2];
  1080. circle(xy0, 0.0f);
  1081. squircle(xy1, 0.0f);
  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. moveTo(pos);
  1087. for (uint32_t ii = 1; ii < num; ++ii)
  1088. {
  1089. float angle = step * ii;
  1090. circle(xy0, angle);
  1091. squircle(xy1, angle);
  1092. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  1093. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  1094. bx::vec3Add(tmp1, pos, tmp0);
  1095. bx::vec3Add(pos, tmp1, _center);
  1096. lineTo(pos);
  1097. }
  1098. close();
  1099. }
  1100. void drawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1101. {
  1102. drawCircle( (const float*)_normal, (const float*)_center, _radius, _weight);
  1103. }
  1104. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1105. {
  1106. const Attrib& attrib = m_attrib[m_stack];
  1107. const uint32_t num = getCircleLod(attrib.m_lod);
  1108. const float step = bx::kPi * 2.0f / num;
  1109. _weight = bx::clamp(_weight, 0.0f, 2.0f);
  1110. float xy0[2];
  1111. float xy1[2];
  1112. circle(xy0, 0.0f);
  1113. squircle(xy1, 0.0f);
  1114. float pos[3];
  1115. getPoint(pos, _axis
  1116. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  1117. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  1118. );
  1119. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1120. for (uint32_t ii = 1; ii < num; ++ii)
  1121. {
  1122. float angle = step * ii;
  1123. circle(xy0, angle);
  1124. squircle(xy1, angle);
  1125. getPoint(pos, _axis
  1126. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  1127. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  1128. );
  1129. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1130. }
  1131. close();
  1132. }
  1133. void drawQuad(const float* _normal, const float* _center, float _size)
  1134. {
  1135. const Attrib& attrib = m_attrib[m_stack];
  1136. float udir[3];
  1137. float vdir[3];
  1138. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1139. const float halfExtent = _size*0.5f;
  1140. float umin[3];
  1141. bx::vec3Mul(umin, udir, -halfExtent);
  1142. float umax[3];
  1143. bx::vec3Mul(umax, udir, halfExtent);
  1144. float vmin[3];
  1145. bx::vec3Mul(vmin, vdir, -halfExtent);
  1146. float vmax[3];
  1147. bx::vec3Mul(vmax, vdir, halfExtent);
  1148. float pt[3];
  1149. float tmp[3];
  1150. bx::vec3Add(tmp, umin, vmin);
  1151. bx::vec3Add(pt, _center, tmp);
  1152. moveTo(pt);
  1153. bx::vec3Add(tmp, umax, vmin);
  1154. bx::vec3Add(pt, _center, tmp);
  1155. lineTo(pt);
  1156. bx::vec3Add(tmp, umax, vmax);
  1157. bx::vec3Add(pt, _center, tmp);
  1158. lineTo(pt);
  1159. bx::vec3Add(tmp, umin, vmax);
  1160. bx::vec3Add(pt, _center, tmp);
  1161. lineTo(pt);
  1162. close();
  1163. }
  1164. void drawQuad(SpriteHandle _handle, const float* _normal, const float* _center, float _size)
  1165. {
  1166. if (m_posQuad == BX_COUNTOF(m_cacheQuad) )
  1167. {
  1168. flushQuad();
  1169. }
  1170. const Attrib& attrib = m_attrib[m_stack];
  1171. float udir[3];
  1172. float vdir[3];
  1173. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1174. const Pack2D& pack = m_sprite.get(_handle);
  1175. const float invTextureSize = 1.0f/SPRITE_TEXTURE_SIZE;
  1176. const float us = pack.m_x * invTextureSize;
  1177. const float vs = pack.m_y * invTextureSize;
  1178. const float ue = (pack.m_x + pack.m_width ) * invTextureSize;
  1179. const float ve = (pack.m_y + pack.m_height) * invTextureSize;
  1180. const float aspectRatio = float(pack.m_width)/float(pack.m_height);
  1181. const float halfExtentU = aspectRatio*_size*0.5f;
  1182. const float halfExtentV = 1.0f/aspectRatio*_size*0.5f;
  1183. float umin[3];
  1184. bx::vec3Mul(umin, udir, -halfExtentU);
  1185. float umax[3];
  1186. bx::vec3Mul(umax, udir, halfExtentU);
  1187. float vmin[3];
  1188. bx::vec3Mul(vmin, vdir, -halfExtentV);
  1189. float vmax[3];
  1190. bx::vec3Mul(vmax, vdir, halfExtentV);
  1191. DebugUvVertex* vertex = &m_cacheQuad[m_posQuad];
  1192. m_posQuad += 4;
  1193. float pt[3];
  1194. float tmp[3];
  1195. bx::vec3Add(tmp, umin, vmin);
  1196. bx::vec3Add(pt, _center, tmp);
  1197. vertex->m_x = pt[0];
  1198. vertex->m_y = pt[1];
  1199. vertex->m_z = pt[2];
  1200. vertex->m_u = us;
  1201. vertex->m_v = vs;
  1202. vertex->m_abgr = attrib.m_abgr;
  1203. ++vertex;
  1204. bx::vec3Add(tmp, umax, vmin);
  1205. bx::vec3Add(pt, _center, tmp);
  1206. vertex->m_x = pt[0];
  1207. vertex->m_y = pt[1];
  1208. vertex->m_z = pt[2];
  1209. vertex->m_u = ue;
  1210. vertex->m_v = vs;
  1211. vertex->m_abgr = attrib.m_abgr;
  1212. ++vertex;
  1213. bx::vec3Add(tmp, umin, vmax);
  1214. bx::vec3Add(pt, _center, tmp);
  1215. vertex->m_x = pt[0];
  1216. vertex->m_y = pt[1];
  1217. vertex->m_z = pt[2];
  1218. vertex->m_u = us;
  1219. vertex->m_v = ve;
  1220. vertex->m_abgr = attrib.m_abgr;
  1221. ++vertex;
  1222. bx::vec3Add(tmp, umax, vmax);
  1223. bx::vec3Add(pt, _center, tmp);
  1224. vertex->m_x = pt[0];
  1225. vertex->m_y = pt[1];
  1226. vertex->m_z = pt[2];
  1227. vertex->m_u = ue;
  1228. vertex->m_v = ve;
  1229. vertex->m_abgr = attrib.m_abgr;
  1230. ++vertex;
  1231. }
  1232. void drawQuad(bgfx::TextureHandle _handle, const float* _normal, const float* _center, float _size)
  1233. {
  1234. BX_UNUSED(_handle, _normal, _center, _size);
  1235. }
  1236. void drawCone(const float* _from, const float* _to, float _radius)
  1237. {
  1238. const Attrib& attrib = m_attrib[m_stack];
  1239. float tmp0[3];
  1240. bx::vec3Sub(tmp0, _from, _to);
  1241. float normal[3];
  1242. bx::vec3Norm(normal, tmp0);
  1243. float mtx[2][16];
  1244. bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
  1245. bx::memCopy(mtx[1], mtx[0], 64);
  1246. mtx[1][12] = _to[0];
  1247. mtx[1][13] = _to[1];
  1248. mtx[1][14] = _to[2];
  1249. uint8_t lod = attrib.m_lod > Mesh::ConeMaxLod
  1250. ? uint8_t(Mesh::ConeMaxLod)
  1251. : attrib.m_lod
  1252. ;
  1253. draw(Mesh::Enum(Mesh::Cone0 + lod), mtx[0], 2, attrib.m_wireframe);
  1254. }
  1255. void drawCone(const void* _from, const void* _to, float _radius)
  1256. {
  1257. drawCone( (const float*)_from, (const float*)_to, _radius);
  1258. }
  1259. void drawCylinder(const float* _from, const float* _to, float _radius, bool _capsule)
  1260. {
  1261. const Attrib& attrib = m_attrib[m_stack];
  1262. float tmp0[3];
  1263. bx::vec3Sub(tmp0, _from, _to);
  1264. float normal[3];
  1265. bx::vec3Norm(normal, tmp0);
  1266. float mtx[2][16];
  1267. bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
  1268. bx::memCopy(mtx[1], mtx[0], 64);
  1269. mtx[1][12] = _to[0];
  1270. mtx[1][13] = _to[1];
  1271. mtx[1][14] = _to[2];
  1272. if (_capsule)
  1273. {
  1274. uint8_t lod = attrib.m_lod > Mesh::CapsuleMaxLod
  1275. ? uint8_t(Mesh::CapsuleMaxLod)
  1276. : attrib.m_lod
  1277. ;
  1278. draw(Mesh::Enum(Mesh::Capsule0 + lod), mtx[0], 2, attrib.m_wireframe);
  1279. Sphere sphere;
  1280. bx::vec3Move(sphere.m_center, _from);
  1281. sphere.m_radius = _radius;
  1282. draw(sphere);
  1283. bx::vec3Move(sphere.m_center, _to);
  1284. draw(sphere);
  1285. }
  1286. else
  1287. {
  1288. uint8_t lod = attrib.m_lod > Mesh::CylinderMaxLod
  1289. ? uint8_t(Mesh::CylinderMaxLod)
  1290. : attrib.m_lod
  1291. ;
  1292. draw(Mesh::Enum(Mesh::Cylinder0 + lod), mtx[0], 2, attrib.m_wireframe);
  1293. }
  1294. }
  1295. void drawCylinder(const void* _from, const void* _to, float _radius, bool _capsule)
  1296. {
  1297. drawCylinder( (const float*)_from, (const float*)_to, _radius, _capsule);
  1298. }
  1299. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight, float _thickness)
  1300. {
  1301. push();
  1302. if (_thickness > 0.0f)
  1303. {
  1304. float from[3] = { _x, _y, _z };
  1305. float mid[3];
  1306. float to[3];
  1307. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1308. mid[0] = _x + _len - _thickness;
  1309. mid[1] = _y;
  1310. mid[2] = _z;
  1311. to[0] = _x + _len;
  1312. to[1] = _y;
  1313. to[2] = _z;
  1314. drawCylinder(from, mid, _thickness, false);
  1315. drawCone(mid, to, _thickness);
  1316. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1317. mid[0] = _x;
  1318. mid[1] = _y + _len - _thickness;
  1319. mid[2] = _z;
  1320. to[0] = _x;
  1321. to[1] = _y + _len;
  1322. to[2] = _z;
  1323. drawCylinder(from, mid, _thickness, false);
  1324. drawCone(mid, to, _thickness);
  1325. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1326. mid[0] = _x;
  1327. mid[1] = _y;
  1328. mid[2] = _z + _len - _thickness;
  1329. to[0] = _x;
  1330. to[1] = _y;
  1331. to[2] = _z + _len;
  1332. drawCylinder(from, mid, _thickness, false);
  1333. drawCone(mid, to, _thickness);
  1334. }
  1335. else
  1336. {
  1337. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1338. moveTo(_x, _y, _z);
  1339. lineTo(_x + _len, _y, _z);
  1340. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1341. moveTo(_x, _y, _z);
  1342. lineTo(_x, _y + _len, _z);
  1343. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1344. moveTo(_x, _y, _z);
  1345. lineTo(_x, _y, _z + _len);
  1346. }
  1347. pop();
  1348. }
  1349. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  1350. {
  1351. const Attrib& attrib = m_attrib[m_stack];
  1352. float udir[3];
  1353. float vdir[3];
  1354. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1355. bx::vec3Mul(udir, udir, _step);
  1356. bx::vec3Mul(vdir, vdir, _step);
  1357. const uint32_t num = (_size/2)*2+1;
  1358. const float halfExtent = float(_size/2);
  1359. float umin[3];
  1360. bx::vec3Mul(umin, udir, -halfExtent);
  1361. float umax[3];
  1362. bx::vec3Mul(umax, udir, halfExtent);
  1363. float vmin[3];
  1364. bx::vec3Mul(vmin, vdir, -halfExtent);
  1365. float vmax[3];
  1366. bx::vec3Mul(vmax, vdir, halfExtent);
  1367. float tmp[3];
  1368. float xs[3];
  1369. float xe[3];
  1370. bx::vec3Add(tmp, umin, vmin);
  1371. bx::vec3Add(xs, _center, tmp);
  1372. bx::vec3Add(tmp, umax, vmin);
  1373. bx::vec3Add(xe, _center, tmp);
  1374. float ys[3];
  1375. float ye[3];
  1376. bx::vec3Add(tmp, umin, vmin);
  1377. bx::vec3Add(ys, _center, tmp);
  1378. bx::vec3Add(tmp, umin, vmax);
  1379. bx::vec3Add(ye, _center, tmp);
  1380. for (uint32_t ii = 0; ii < num; ++ii)
  1381. {
  1382. moveTo(xs);
  1383. lineTo(xe);
  1384. bx::vec3Add(xs, xs, vdir);
  1385. bx::vec3Add(xe, xe, vdir);
  1386. moveTo(ys);
  1387. lineTo(ye);
  1388. bx::vec3Add(ys, ys, udir);
  1389. bx::vec3Add(ye, ye, udir);
  1390. }
  1391. }
  1392. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1393. {
  1394. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  1395. }
  1396. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  1397. {
  1398. push();
  1399. pushTranslate(_center);
  1400. const uint32_t num = (_size/2)*2-1;
  1401. const float halfExtent = float(_size/2) * _step;
  1402. setColor(0xff606060);
  1403. float yy = -halfExtent + _step;
  1404. for (uint32_t ii = 0; ii < num; ++ii)
  1405. {
  1406. moveTo(_axis, -halfExtent, yy);
  1407. lineTo(_axis, halfExtent, yy);
  1408. moveTo(_axis, yy, -halfExtent);
  1409. lineTo(_axis, yy, halfExtent);
  1410. yy += _step;
  1411. }
  1412. setColor(0xff101010);
  1413. moveTo(_axis, -halfExtent, -halfExtent);
  1414. lineTo(_axis, -halfExtent, halfExtent);
  1415. lineTo(_axis, halfExtent, halfExtent);
  1416. lineTo(_axis, halfExtent, -halfExtent);
  1417. close();
  1418. moveTo(_axis, -halfExtent, 0.0f);
  1419. lineTo(_axis, halfExtent, 0.0f);
  1420. moveTo(_axis, 0.0f, -halfExtent);
  1421. lineTo(_axis, 0.0f, halfExtent);
  1422. pop();
  1423. }
  1424. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1425. {
  1426. drawGrid(_axis, (const float*)_center, _size, _step);
  1427. }
  1428. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1429. {
  1430. push();
  1431. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  1432. drawCircle(Axis::X, _x, _y, _z, _radius, 0.0f);
  1433. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  1434. drawCircle(Axis::Y, _x, _y, _z, _radius, 0.0f);
  1435. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  1436. drawCircle(Axis::Z, _x, _y, _z, _radius, 0.0f);
  1437. pop();
  1438. }
  1439. private:
  1440. struct Mesh
  1441. {
  1442. enum Enum
  1443. {
  1444. Sphere0,
  1445. Sphere1,
  1446. Sphere2,
  1447. Sphere3,
  1448. Cone0,
  1449. Cone1,
  1450. Cone2,
  1451. Cone3,
  1452. Cylinder0,
  1453. Cylinder1,
  1454. Cylinder2,
  1455. Cylinder3,
  1456. Capsule0,
  1457. Capsule1,
  1458. Capsule2,
  1459. Capsule3,
  1460. Cube,
  1461. Count,
  1462. SphereMaxLod = Sphere3 - Sphere0,
  1463. ConeMaxLod = Cone3 - Cone0,
  1464. CylinderMaxLod = Cylinder3 - Cylinder0,
  1465. CapsuleMaxLod = Capsule3 - Capsule0,
  1466. };
  1467. uint32_t m_startVertex;
  1468. uint32_t m_numVertices;
  1469. uint32_t m_startIndex[2];
  1470. uint32_t m_numIndices[2];
  1471. };
  1472. struct Program
  1473. {
  1474. enum Enum
  1475. {
  1476. Lines,
  1477. LinesStipple,
  1478. Fill,
  1479. FillLit,
  1480. FillTexture,
  1481. Count
  1482. };
  1483. };
  1484. void draw(Mesh::Enum _mesh, const float* _mtx, uint16_t _num, bool _wireframe)
  1485. {
  1486. pushTransform(_mtx, _num);
  1487. const Mesh& mesh = m_mesh[_mesh];
  1488. const Attrib& attrib = m_attrib[m_stack];
  1489. if (0 != mesh.m_numIndices[_wireframe])
  1490. {
  1491. bgfx::setIndexBuffer(m_ibh
  1492. , mesh.m_startIndex[_wireframe]
  1493. , mesh.m_numIndices[_wireframe]
  1494. );
  1495. }
  1496. const float flip = 0 == (attrib.m_state & BGFX_STATE_CULL_CCW) ? 1.0f : -1.0f;
  1497. const uint8_t alpha = attrib.m_abgr>>24;
  1498. float params[4][4] =
  1499. {
  1500. { // lightDir
  1501. 0.0f * flip,
  1502. -1.0f * flip,
  1503. 0.0f * flip,
  1504. 3.0f, // shininess
  1505. },
  1506. { // skyColor
  1507. 1.0f,
  1508. 0.9f,
  1509. 0.8f,
  1510. 0.0f, // unused
  1511. },
  1512. { // groundColor.xyz0
  1513. 0.2f,
  1514. 0.22f,
  1515. 0.5f,
  1516. 0.0f, // unused
  1517. },
  1518. { // matColor
  1519. ( (attrib.m_abgr )&0xff)/255.0f,
  1520. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  1521. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  1522. ( alpha )/255.0f,
  1523. },
  1524. };
  1525. bx::vec3Norm(params[0], params[0]);
  1526. bgfx::setUniform(u_params, params, 4);
  1527. MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
  1528. bgfx::setTransform(stack.mtx, stack.num);
  1529. bgfx::setVertexBuffer(0, m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1530. bgfx::setState(0
  1531. | attrib.m_state
  1532. | (_wireframe ? BGFX_STATE_PT_LINES|BGFX_STATE_LINEAA|BGFX_STATE_BLEND_ALPHA
  1533. : (alpha < 0xff) ? BGFX_STATE_BLEND_ALPHA : 0)
  1534. );
  1535. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1536. popTransform();
  1537. }
  1538. void softFlush()
  1539. {
  1540. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1541. {
  1542. flush();
  1543. }
  1544. }
  1545. void flush()
  1546. {
  1547. if (0 != m_pos)
  1548. {
  1549. if (checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  1550. {
  1551. bgfx::TransientVertexBuffer tvb;
  1552. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  1553. bx::memCopy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  1554. bgfx::TransientIndexBuffer tib;
  1555. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1556. bx::memCopy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1557. const Attrib& attrib = m_attrib[m_stack];
  1558. bgfx::setVertexBuffer(0, &tvb);
  1559. bgfx::setIndexBuffer(&tib);
  1560. bgfx::setState(0
  1561. | BGFX_STATE_RGB_WRITE
  1562. | BGFX_STATE_PT_LINES
  1563. | attrib.m_state
  1564. | BGFX_STATE_LINEAA
  1565. | BGFX_STATE_BLEND_ALPHA
  1566. );
  1567. bgfx::setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1568. bgfx::ProgramHandle program = m_program[attrib.m_stipple ? 1 : 0];
  1569. bgfx::submit(m_viewId, program);
  1570. }
  1571. m_state = State::None;
  1572. m_pos = 0;
  1573. m_indexPos = 0;
  1574. m_vertexPos = 0;
  1575. }
  1576. }
  1577. void flushQuad()
  1578. {
  1579. if (0 != m_posQuad)
  1580. {
  1581. const uint32_t numIndices = m_posQuad/4*6;
  1582. if (checkAvailTransientBuffers(m_posQuad, DebugUvVertex::ms_decl, numIndices) )
  1583. {
  1584. bgfx::TransientVertexBuffer tvb;
  1585. bgfx::allocTransientVertexBuffer(&tvb, m_posQuad, DebugUvVertex::ms_decl);
  1586. bx::memCopy(tvb.data, m_cacheQuad, m_posQuad * DebugUvVertex::ms_decl.m_stride);
  1587. bgfx::TransientIndexBuffer tib;
  1588. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1589. uint16_t* indices = (uint16_t*)tib.data;
  1590. for (uint16_t ii = 0, num = m_posQuad/4; ii < num; ++ii)
  1591. {
  1592. uint16_t startVertex = ii*4;
  1593. indices[0] = startVertex+0;
  1594. indices[1] = startVertex+1;
  1595. indices[2] = startVertex+2;
  1596. indices[3] = startVertex+1;
  1597. indices[4] = startVertex+3;
  1598. indices[5] = startVertex+2;
  1599. indices += 6;
  1600. }
  1601. const Attrib& attrib = m_attrib[m_stack];
  1602. bgfx::setVertexBuffer(0, &tvb);
  1603. bgfx::setIndexBuffer(&tib);
  1604. bgfx::setState(0
  1605. | (attrib.m_state & ~BGFX_STATE_CULL_MASK)
  1606. );
  1607. bgfx::setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1608. bgfx::setTexture(0, s_texColor, m_texture);
  1609. bgfx::submit(m_viewId, m_program[Program::FillTexture]);
  1610. }
  1611. m_posQuad = 0;
  1612. }
  1613. }
  1614. struct State
  1615. {
  1616. enum Enum
  1617. {
  1618. None,
  1619. MoveTo,
  1620. LineTo,
  1621. Count
  1622. };
  1623. };
  1624. static const uint32_t cacheSize = 1024;
  1625. static const uint32_t stackSize = 16;
  1626. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1627. DebugVertex m_cache[cacheSize+1];
  1628. uint16_t m_indices[cacheSize*2];
  1629. uint16_t m_pos;
  1630. uint16_t m_indexPos;
  1631. uint16_t m_vertexPos;
  1632. static const uint32_t cacheQuadSize = 1024;
  1633. DebugUvVertex m_cacheQuad[cacheQuadSize];
  1634. uint16_t m_posQuad;
  1635. uint32_t m_mtxStackCurrent;
  1636. struct MatrixStack
  1637. {
  1638. void reset()
  1639. {
  1640. mtx = 0;
  1641. num = 1;
  1642. data = NULL;
  1643. }
  1644. uint32_t mtx;
  1645. uint16_t num;
  1646. float* data;
  1647. };
  1648. MatrixStack m_mtxStack[32];
  1649. uint8_t m_viewId;
  1650. uint8_t m_stack;
  1651. bool m_depthTestLess;
  1652. struct Attrib
  1653. {
  1654. uint64_t m_state;
  1655. float m_offset;
  1656. float m_scale;
  1657. float m_spin;
  1658. uint32_t m_abgr;
  1659. bool m_stipple;
  1660. bool m_wireframe;
  1661. uint8_t m_lod;
  1662. };
  1663. Attrib m_attrib[stackSize];
  1664. State::Enum m_state;
  1665. Mesh m_mesh[Mesh::Count];
  1666. typedef SpriteT<256, SPRITE_TEXTURE_SIZE> Sprite;
  1667. Sprite m_sprite;
  1668. bgfx::UniformHandle s_texColor;
  1669. bgfx::TextureHandle m_texture;
  1670. bgfx::ProgramHandle m_program[Program::Count];
  1671. bgfx::UniformHandle u_params;
  1672. bgfx::VertexBufferHandle m_vbh;
  1673. bgfx::IndexBufferHandle m_ibh;
  1674. bx::AllocatorI* m_allocator;
  1675. };
  1676. static DebugDraw s_dd;
  1677. void ddInit(bool _depthTestLess, bx::AllocatorI* _allocator)
  1678. {
  1679. s_dd.init(_depthTestLess, _allocator);
  1680. }
  1681. void ddShutdown()
  1682. {
  1683. s_dd.shutdown();
  1684. }
  1685. SpriteHandle ddCreateSprite(uint16_t _width, uint16_t _height, const void* _data)
  1686. {
  1687. return s_dd.createSprite(_width, _height, _data);
  1688. }
  1689. void ddDestroy(SpriteHandle _handle)
  1690. {
  1691. s_dd.destroy(_handle);
  1692. }
  1693. void ddBegin(uint8_t _viewId)
  1694. {
  1695. s_dd.begin(_viewId);
  1696. }
  1697. void ddEnd()
  1698. {
  1699. s_dd.end();
  1700. }
  1701. void ddPush()
  1702. {
  1703. s_dd.push();
  1704. }
  1705. void ddPop()
  1706. {
  1707. s_dd.pop();
  1708. }
  1709. void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1710. {
  1711. s_dd.setState(_depthTest, _depthWrite, _clockwise);
  1712. }
  1713. void ddSetColor(uint32_t _abgr)
  1714. {
  1715. s_dd.setColor(_abgr);
  1716. }
  1717. void ddSetLod(uint8_t _lod)
  1718. {
  1719. s_dd.setLod(_lod);
  1720. }
  1721. void ddSetWireframe(bool _wireframe)
  1722. {
  1723. s_dd.setWireframe(_wireframe);
  1724. }
  1725. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1726. {
  1727. s_dd.setStipple(_stipple, _scale, _offset);
  1728. }
  1729. void ddSetSpin(float _spin)
  1730. {
  1731. s_dd.setSpin(_spin);
  1732. }
  1733. void ddSetTransform(const void* _mtx)
  1734. {
  1735. s_dd.setTransform(_mtx);
  1736. }
  1737. void ddSetTranslate(float _x, float _y, float _z)
  1738. {
  1739. s_dd.setTranslate(_x, _y, _z);
  1740. }
  1741. void ddMoveTo(float _x, float _y, float _z)
  1742. {
  1743. s_dd.moveTo(_x, _y, _z);
  1744. }
  1745. void ddMoveTo(const void* _pos)
  1746. {
  1747. s_dd.moveTo(_pos);
  1748. }
  1749. void ddLineTo(float _x, float _y, float _z)
  1750. {
  1751. s_dd.lineTo(_x, _y, _z);
  1752. }
  1753. void ddLineTo(const void* _pos)
  1754. {
  1755. s_dd.lineTo(_pos);
  1756. }
  1757. void ddClose()
  1758. {
  1759. s_dd.close();
  1760. }
  1761. void ddDraw(const Aabb& _aabb)
  1762. {
  1763. s_dd.draw(_aabb);
  1764. }
  1765. void ddDraw(const Cylinder& _cylinder)
  1766. {
  1767. s_dd.draw(_cylinder, false);
  1768. }
  1769. void ddDraw(const Capsule& _capsule)
  1770. {
  1771. s_dd.draw( *( (const Cylinder*)&_capsule), true);
  1772. }
  1773. void ddDraw(const Disk& _disk)
  1774. {
  1775. s_dd.draw(_disk);
  1776. }
  1777. void ddDraw(const Obb& _obb)
  1778. {
  1779. s_dd.draw(_obb);
  1780. }
  1781. void ddDraw(const Sphere& _sphere)
  1782. {
  1783. s_dd.draw(_sphere);
  1784. }
  1785. void ddDraw(const Cone& _cone)
  1786. {
  1787. ddDrawCone(_cone.m_pos, _cone.m_end, _cone.m_radius);
  1788. }
  1789. void ddDrawFrustum(const void* _viewProj)
  1790. {
  1791. s_dd.drawFrustum(_viewProj);
  1792. }
  1793. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1794. {
  1795. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1796. }
  1797. void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1798. {
  1799. s_dd.drawCircle(_normal, _center, _radius, _weight);
  1800. }
  1801. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1802. {
  1803. s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight);
  1804. }
  1805. void ddDrawQuad(const float* _normal, const float* _center, float _size)
  1806. {
  1807. s_dd.drawQuad(_normal, _center, _size);
  1808. }
  1809. void ddDrawQuad(SpriteHandle _handle, const float* _normal, const float* _center, float _size)
  1810. {
  1811. s_dd.drawQuad(_handle, _normal, _center, _size);
  1812. }
  1813. void ddDrawQuad(bgfx::TextureHandle _handle, const float* _normal, const float* _center, float _size)
  1814. {
  1815. s_dd.drawQuad(_handle, _normal, _center, _size);
  1816. }
  1817. void ddDrawCone(const void* _from, const void* _to, float _radius)
  1818. {
  1819. s_dd.drawCone(_from, _to, _radius);
  1820. }
  1821. void ddDrawCylinder(const void* _from, const void* _to, float _radius)
  1822. {
  1823. s_dd.drawCylinder(_from, _to, _radius, false);
  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. }