2
0

debugdraw.cpp 50 KB

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