debugdraw.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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::fclamp(_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::fclamp(_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, bool _capsule)
  1822. {
  1823. if (_capsule)
  1824. {
  1825. s_dd.push();
  1826. s_dd.setLod(0);
  1827. s_dd.drawCylinder(_from, _to, _radius, true);
  1828. s_dd.pop();
  1829. }
  1830. else
  1831. {
  1832. s_dd.drawCylinder(_from, _to, _radius, false);
  1833. }
  1834. }
  1835. void ddDrawCapsule(const void* _from, const void* _to, float _radius)
  1836. {
  1837. s_dd.drawCylinder(_from, _to, _radius, true);
  1838. }
  1839. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight, float _thickness)
  1840. {
  1841. s_dd.drawAxis(_x, _y, _z, _len, _hightlight, _thickness);
  1842. }
  1843. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1844. {
  1845. s_dd.drawGrid(_normal, _center, _size, _step);
  1846. }
  1847. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1848. {
  1849. s_dd.drawGrid(_axis, _center, _size, _step);
  1850. }
  1851. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1852. {
  1853. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1854. }