debugdraw.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  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_mtx = 0;
  599. m_viewId = 0;
  600. m_pos = 0;
  601. m_indexPos = 0;
  602. m_vertexPos = 0;
  603. m_posQuad = 0;
  604. }
  605. void shutdown()
  606. {
  607. bgfx::destroy(m_ibh);
  608. bgfx::destroy(m_vbh);
  609. for (uint32_t ii = 0; ii < Program::Count; ++ii)
  610. {
  611. bgfx::destroy(m_program[ii]);
  612. }
  613. bgfx::destroy(u_params);
  614. bgfx::destroy(s_texColor);
  615. bgfx::destroy(m_texture);
  616. }
  617. SpriteHandle createSprite(uint16_t _width, uint16_t _height, const void* _data)
  618. {
  619. SpriteHandle handle = m_sprite.create(_width, _height);
  620. if (isValid(handle) )
  621. {
  622. const Pack2D& pack = m_sprite.get(handle);
  623. bgfx::updateTexture2D(
  624. m_texture
  625. , 0
  626. , 0
  627. , pack.m_x
  628. , pack.m_y
  629. , pack.m_width
  630. , pack.m_height
  631. , bgfx::copy(_data, pack.m_width*pack.m_height*4)
  632. );
  633. }
  634. return handle;
  635. }
  636. void destroy(SpriteHandle _handle)
  637. {
  638. m_sprite.destroy(_handle);
  639. }
  640. void begin(uint8_t _viewId)
  641. {
  642. BX_CHECK(State::Count == m_state);
  643. m_viewId = _viewId;
  644. m_mtx = 0;
  645. m_state = State::None;
  646. m_stack = 0;
  647. Attrib& attrib = m_attrib[0];
  648. attrib.m_state = 0
  649. | BGFX_STATE_RGB_WRITE
  650. | (m_depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER)
  651. | BGFX_STATE_CULL_CW
  652. | BGFX_STATE_DEPTH_WRITE
  653. ;
  654. attrib.m_scale = 1.0f;
  655. attrib.m_spin = 0.0f;
  656. attrib.m_offset = 0.0f;
  657. attrib.m_abgr = UINT32_MAX;
  658. attrib.m_stipple = false;
  659. attrib.m_wireframe = false;
  660. attrib.m_lod = 0;
  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)
  688. {
  689. BX_CHECK(State::Count != m_state);
  690. flush();
  691. if (NULL == _mtx)
  692. {
  693. m_mtx = 0;
  694. return;
  695. }
  696. bgfx::Transform transform;
  697. m_mtx = bgfx::allocTransform(&transform, 1);
  698. bx::memCopy(transform.data, _mtx, 64);
  699. }
  700. void setTranslate(float _x, float _y, float _z)
  701. {
  702. float mtx[16];
  703. bx::mtxTranslate(mtx, _x, _y, _z);
  704. setTransform(mtx);
  705. }
  706. void setTranslate(const float* _pos)
  707. {
  708. setTranslate(_pos[0], _pos[1], _pos[2]);
  709. }
  710. void setState(bool _depthTest, bool _depthWrite, bool _clockwise)
  711. {
  712. const uint64_t depthTest = m_depthTestLess
  713. ? BGFX_STATE_DEPTH_TEST_LESS
  714. : BGFX_STATE_DEPTH_TEST_GREATER
  715. ;
  716. uint64_t state = m_attrib[m_stack].m_state & ~(0
  717. | BGFX_STATE_DEPTH_TEST_MASK
  718. | BGFX_STATE_DEPTH_WRITE
  719. | BGFX_STATE_CULL_CW
  720. | BGFX_STATE_CULL_CCW
  721. );
  722. state |= _depthTest
  723. ? depthTest
  724. : 0
  725. ;
  726. state |= _depthWrite
  727. ? BGFX_STATE_DEPTH_WRITE
  728. : 0
  729. ;
  730. state |= _clockwise
  731. ? BGFX_STATE_CULL_CW
  732. : BGFX_STATE_CULL_CCW
  733. ;
  734. if (m_attrib[m_stack].m_state != state)
  735. {
  736. flush();
  737. }
  738. m_attrib[m_stack].m_state = state;
  739. }
  740. void setColor(uint32_t _abgr)
  741. {
  742. BX_CHECK(State::Count != m_state);
  743. m_attrib[m_stack].m_abgr = _abgr;
  744. }
  745. void setLod(uint8_t _lod)
  746. {
  747. BX_CHECK(State::Count != m_state);
  748. m_attrib[m_stack].m_lod = _lod;
  749. }
  750. void setWireframe(bool _wireframe)
  751. {
  752. BX_CHECK(State::Count != m_state);
  753. m_attrib[m_stack].m_wireframe = _wireframe;
  754. }
  755. void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
  756. {
  757. BX_CHECK(State::Count != m_state);
  758. Attrib& attrib = m_attrib[m_stack];
  759. if (attrib.m_stipple != _stipple)
  760. {
  761. flush();
  762. }
  763. attrib.m_stipple = _stipple;
  764. attrib.m_offset = _offset;
  765. attrib.m_scale = _scale;
  766. }
  767. void setSpin(float _spin)
  768. {
  769. Attrib& attrib = m_attrib[m_stack];
  770. attrib.m_spin = _spin;
  771. }
  772. void moveTo(float _x, float _y, float _z = 0.0f)
  773. {
  774. BX_CHECK(State::Count != m_state);
  775. softFlush();
  776. m_state = State::MoveTo;
  777. DebugVertex& vertex = m_cache[m_pos];
  778. vertex.m_x = _x;
  779. vertex.m_y = _y;
  780. vertex.m_z = _z;
  781. Attrib& attrib = m_attrib[m_stack];
  782. vertex.m_abgr = attrib.m_abgr;
  783. vertex.m_len = attrib.m_offset;
  784. m_vertexPos = m_pos;
  785. }
  786. void moveTo(const void* _pos)
  787. {
  788. BX_CHECK(State::Count != m_state);
  789. const float* pos = (const float*)_pos;
  790. moveTo(pos[0], pos[1], pos[2]);
  791. }
  792. void moveTo(Axis::Enum _axis, float _x, float _y)
  793. {
  794. float pos[3];
  795. getPoint(pos, _axis, _x, _y);
  796. moveTo(pos);
  797. }
  798. void lineTo(float _x, float _y, float _z = 0.0f)
  799. {
  800. BX_CHECK(State::Count != m_state);
  801. if (State::None == m_state)
  802. {
  803. moveTo(_x, _y, _z);
  804. return;
  805. }
  806. if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
  807. {
  808. uint32_t pos = m_pos;
  809. uint32_t vertexPos = m_vertexPos;
  810. flush();
  811. bx::memCopy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
  812. if (vertexPos == pos)
  813. {
  814. m_pos = 1;
  815. }
  816. else
  817. {
  818. bx::memCopy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
  819. m_pos = 2;
  820. }
  821. m_state = State::LineTo;
  822. }
  823. else if (State::MoveTo == m_state)
  824. {
  825. ++m_pos;
  826. m_state = State::LineTo;
  827. }
  828. uint16_t prev = m_pos-1;
  829. uint16_t curr = m_pos++;
  830. DebugVertex& vertex = m_cache[curr];
  831. vertex.m_x = _x;
  832. vertex.m_y = _y;
  833. vertex.m_z = _z;
  834. Attrib& attrib = m_attrib[m_stack];
  835. vertex.m_abgr = attrib.m_abgr;
  836. vertex.m_len = attrib.m_offset;
  837. float tmp[3];
  838. bx::vec3Sub(tmp, &vertex.m_x, &m_cache[prev].m_x);
  839. float len = bx::vec3Length(tmp) * attrib.m_scale;
  840. vertex.m_len = m_cache[prev].m_len + len;
  841. m_indices[m_indexPos++] = prev;
  842. m_indices[m_indexPos++] = curr;
  843. }
  844. void lineTo(const void* _pos)
  845. {
  846. BX_CHECK(State::Count != m_state);
  847. const float* pos = (const float*)_pos;
  848. lineTo(pos[0], pos[1], pos[2]);
  849. }
  850. void lineTo(Axis::Enum _axis, float _x, float _y)
  851. {
  852. float pos[3];
  853. getPoint(pos, _axis, _x, _y);
  854. lineTo(pos);
  855. }
  856. void close()
  857. {
  858. BX_CHECK(State::Count != m_state);
  859. DebugVertex& vertex = m_cache[m_vertexPos];
  860. lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
  861. m_state = State::None;
  862. }
  863. void draw(const Aabb& _aabb)
  864. {
  865. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  866. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  867. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  868. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  869. close();
  870. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  871. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  872. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  873. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  874. close();
  875. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  876. lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  877. moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  878. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  879. moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  880. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  881. moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  882. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  883. }
  884. void draw(const Cylinder& _cylinder, bool _capsule)
  885. {
  886. drawCylinder(_cylinder.m_pos, _cylinder.m_end, _cylinder.m_radius, _capsule);
  887. }
  888. void draw(const Disk& _disk)
  889. {
  890. drawCircle(_disk.m_normal, _disk.m_center, _disk.m_radius, 0.0f);
  891. }
  892. void draw(const Obb& _obb)
  893. {
  894. const Attrib& attrib = m_attrib[m_stack];
  895. if (attrib.m_wireframe)
  896. {
  897. setTransform(_obb.m_mtx);
  898. moveTo(-1.0f, -1.0f, -1.0f);
  899. lineTo( 1.0f, -1.0f, -1.0f);
  900. lineTo( 1.0f, 1.0f, -1.0f);
  901. lineTo(-1.0f, 1.0f, -1.0f);
  902. close();
  903. moveTo(-1.0f, 1.0f, 1.0f);
  904. lineTo( 1.0f, 1.0f, 1.0f);
  905. lineTo( 1.0f, -1.0f, 1.0f);
  906. lineTo(-1.0f, -1.0f, 1.0f);
  907. close();
  908. moveTo( 1.0f, -1.0f, -1.0f);
  909. lineTo( 1.0f, -1.0f, 1.0f);
  910. moveTo( 1.0f, 1.0f, -1.0f);
  911. lineTo( 1.0f, 1.0f, 1.0f);
  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. setTransform(NULL);
  917. }
  918. else
  919. {
  920. draw(Mesh::Cube, _obb.m_mtx, 1, false);
  921. }
  922. }
  923. void draw(const Sphere& _sphere)
  924. {
  925. const Attrib& attrib = m_attrib[m_stack];
  926. float mtx[16];
  927. bx::mtxSRT(mtx
  928. , _sphere.m_radius
  929. , _sphere.m_radius
  930. , _sphere.m_radius
  931. , 0.0f
  932. , 0.0f
  933. , 0.0f
  934. , _sphere.m_center[0]
  935. , _sphere.m_center[1]
  936. , _sphere.m_center[2]
  937. );
  938. uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
  939. ? uint8_t(Mesh::SphereMaxLod)
  940. : attrib.m_lod
  941. ;
  942. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe);
  943. }
  944. void drawFrustum(const float* _viewProj)
  945. {
  946. Plane planes[6];
  947. buildFrustumPlanes(planes, _viewProj);
  948. float points[24];
  949. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  950. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  951. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  952. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  953. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  954. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  955. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  956. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  957. moveTo(&points[ 0]);
  958. lineTo(&points[ 3]);
  959. lineTo(&points[ 6]);
  960. lineTo(&points[ 9]);
  961. close();
  962. moveTo(&points[12]);
  963. lineTo(&points[15]);
  964. lineTo(&points[18]);
  965. lineTo(&points[21]);
  966. close();
  967. moveTo(&points[ 0]);
  968. lineTo(&points[12]);
  969. moveTo(&points[ 3]);
  970. lineTo(&points[15]);
  971. moveTo(&points[ 6]);
  972. lineTo(&points[18]);
  973. moveTo(&points[ 9]);
  974. lineTo(&points[21]);
  975. }
  976. void drawFrustum(const void* _viewProj)
  977. {
  978. drawFrustum( (const float*)_viewProj);
  979. }
  980. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  981. {
  982. const Attrib& attrib = m_attrib[m_stack];
  983. const uint32_t num = getCircleLod(attrib.m_lod);
  984. const float step = bx::kPi * 2.0f / num;
  985. _degrees = bx::fwrap(_degrees, 360.0f);
  986. float pos[3];
  987. getPoint(pos, _axis
  988. , bx::fsin(step * 0)*_radius
  989. , bx::fcos(step * 0)*_radius
  990. );
  991. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  992. uint32_t n = uint32_t(num*_degrees/360.0f);
  993. for (uint32_t ii = 1; ii < n+1; ++ii)
  994. {
  995. getPoint(pos, _axis
  996. , bx::fsin(step * ii)*_radius
  997. , bx::fcos(step * ii)*_radius
  998. );
  999. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1000. }
  1001. moveTo(_x, _y, _z);
  1002. getPoint(pos, _axis
  1003. , bx::fsin(step * 0)*_radius
  1004. , bx::fcos(step * 0)*_radius
  1005. );
  1006. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1007. getPoint(pos, _axis
  1008. , bx::fsin(step * n)*_radius
  1009. , bx::fcos(step * n)*_radius
  1010. );
  1011. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1012. lineTo(_x, _y, _z);
  1013. }
  1014. void drawCircle(const float* _normal, const float* _center, float _radius, float _weight)
  1015. {
  1016. const Attrib& attrib = m_attrib[m_stack];
  1017. const uint32_t num = getCircleLod(attrib.m_lod);
  1018. const float step = bx::kPi * 2.0f / num;
  1019. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  1020. float udir[3];
  1021. float vdir[3];
  1022. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1023. float pos[3];
  1024. float tmp0[3];
  1025. float tmp1[3];
  1026. float xy0[2];
  1027. float xy1[2];
  1028. circle(xy0, 0.0f);
  1029. squircle(xy1, 0.0f);
  1030. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  1031. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  1032. bx::vec3Add(tmp1, pos, tmp0);
  1033. bx::vec3Add(pos, tmp1, _center);
  1034. moveTo(pos);
  1035. for (uint32_t ii = 1; ii < num; ++ii)
  1036. {
  1037. float angle = step * ii;
  1038. circle(xy0, angle);
  1039. squircle(xy1, angle);
  1040. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  1041. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  1042. bx::vec3Add(tmp1, pos, tmp0);
  1043. bx::vec3Add(pos, tmp1, _center);
  1044. lineTo(pos);
  1045. }
  1046. close();
  1047. }
  1048. void drawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1049. {
  1050. drawCircle( (const float*)_normal, (const float*)_center, _radius, _weight);
  1051. }
  1052. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1053. {
  1054. const Attrib& attrib = m_attrib[m_stack];
  1055. const uint32_t num = getCircleLod(attrib.m_lod);
  1056. const float step = bx::kPi * 2.0f / num;
  1057. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  1058. float xy0[2];
  1059. float xy1[2];
  1060. circle(xy0, 0.0f);
  1061. squircle(xy1, 0.0f);
  1062. float pos[3];
  1063. getPoint(pos, _axis
  1064. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  1065. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  1066. );
  1067. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1068. for (uint32_t ii = 1; ii < num; ++ii)
  1069. {
  1070. float angle = step * ii;
  1071. circle(xy0, angle);
  1072. squircle(xy1, angle);
  1073. getPoint(pos, _axis
  1074. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  1075. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  1076. );
  1077. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  1078. }
  1079. close();
  1080. }
  1081. void drawQuad(const float* _normal, const float* _center, float _size)
  1082. {
  1083. const Attrib& attrib = m_attrib[m_stack];
  1084. float udir[3];
  1085. float vdir[3];
  1086. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1087. const float halfExtent = _size*0.5f;
  1088. float umin[3];
  1089. bx::vec3Mul(umin, udir, -halfExtent);
  1090. float umax[3];
  1091. bx::vec3Mul(umax, udir, halfExtent);
  1092. float vmin[3];
  1093. bx::vec3Mul(vmin, vdir, -halfExtent);
  1094. float vmax[3];
  1095. bx::vec3Mul(vmax, vdir, halfExtent);
  1096. float pt[3];
  1097. float tmp[3];
  1098. bx::vec3Add(tmp, umin, vmin);
  1099. bx::vec3Add(pt, _center, tmp);
  1100. moveTo(pt);
  1101. bx::vec3Add(tmp, umax, vmin);
  1102. bx::vec3Add(pt, _center, tmp);
  1103. lineTo(pt);
  1104. bx::vec3Add(tmp, umax, vmax);
  1105. bx::vec3Add(pt, _center, tmp);
  1106. lineTo(pt);
  1107. bx::vec3Add(tmp, umin, vmax);
  1108. bx::vec3Add(pt, _center, tmp);
  1109. lineTo(pt);
  1110. close();
  1111. }
  1112. void drawQuad(SpriteHandle _handle, const float* _normal, const float* _center, float _size)
  1113. {
  1114. if (m_posQuad == BX_COUNTOF(m_cacheQuad) )
  1115. {
  1116. flushQuad();
  1117. }
  1118. const Attrib& attrib = m_attrib[m_stack];
  1119. float udir[3];
  1120. float vdir[3];
  1121. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1122. const Pack2D& pack = m_sprite.get(_handle);
  1123. const float invTextureSize = 1.0f/SPRITE_TEXTURE_SIZE;
  1124. const float us = pack.m_x * invTextureSize;
  1125. const float vs = pack.m_y * invTextureSize;
  1126. const float ue = (pack.m_x + pack.m_width ) * invTextureSize;
  1127. const float ve = (pack.m_y + pack.m_height) * invTextureSize;
  1128. const float aspectRatio = float(pack.m_width)/float(pack.m_height);
  1129. const float halfExtentU = aspectRatio*_size*0.5f;
  1130. const float halfExtentV = 1.0f/aspectRatio*_size*0.5f;
  1131. float umin[3];
  1132. bx::vec3Mul(umin, udir, -halfExtentU);
  1133. float umax[3];
  1134. bx::vec3Mul(umax, udir, halfExtentU);
  1135. float vmin[3];
  1136. bx::vec3Mul(vmin, vdir, -halfExtentV);
  1137. float vmax[3];
  1138. bx::vec3Mul(vmax, vdir, halfExtentV);
  1139. DebugUvVertex* vertex = &m_cacheQuad[m_posQuad];
  1140. m_posQuad += 4;
  1141. float pt[3];
  1142. float tmp[3];
  1143. bx::vec3Add(tmp, umin, vmin);
  1144. bx::vec3Add(pt, _center, tmp);
  1145. vertex->m_x = pt[0];
  1146. vertex->m_y = pt[1];
  1147. vertex->m_z = pt[2];
  1148. vertex->m_u = us;
  1149. vertex->m_v = vs;
  1150. vertex->m_abgr = attrib.m_abgr;
  1151. ++vertex;
  1152. bx::vec3Add(tmp, umax, vmin);
  1153. bx::vec3Add(pt, _center, tmp);
  1154. vertex->m_x = pt[0];
  1155. vertex->m_y = pt[1];
  1156. vertex->m_z = pt[2];
  1157. vertex->m_u = ue;
  1158. vertex->m_v = vs;
  1159. vertex->m_abgr = attrib.m_abgr;
  1160. ++vertex;
  1161. bx::vec3Add(tmp, umin, vmax);
  1162. bx::vec3Add(pt, _center, tmp);
  1163. vertex->m_x = pt[0];
  1164. vertex->m_y = pt[1];
  1165. vertex->m_z = pt[2];
  1166. vertex->m_u = us;
  1167. vertex->m_v = ve;
  1168. vertex->m_abgr = attrib.m_abgr;
  1169. ++vertex;
  1170. bx::vec3Add(tmp, umax, vmax);
  1171. bx::vec3Add(pt, _center, tmp);
  1172. vertex->m_x = pt[0];
  1173. vertex->m_y = pt[1];
  1174. vertex->m_z = pt[2];
  1175. vertex->m_u = ue;
  1176. vertex->m_v = ve;
  1177. vertex->m_abgr = attrib.m_abgr;
  1178. ++vertex;
  1179. }
  1180. void drawQuad(bgfx::TextureHandle _handle, const float* _normal, const float* _center, float _size)
  1181. {
  1182. BX_UNUSED(_handle, _normal, _center, _size);
  1183. }
  1184. void drawCone(const float* _from, const float* _to, float _radius)
  1185. {
  1186. const Attrib& attrib = m_attrib[m_stack];
  1187. float tmp0[3];
  1188. bx::vec3Sub(tmp0, _from, _to);
  1189. float normal[3];
  1190. bx::vec3Norm(normal, tmp0);
  1191. float mtx[2][16];
  1192. bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
  1193. bx::memCopy(mtx[1], mtx[0], 64);
  1194. mtx[1][12] = _to[0];
  1195. mtx[1][13] = _to[1];
  1196. mtx[1][14] = _to[2];
  1197. uint8_t lod = attrib.m_lod > Mesh::ConeMaxLod
  1198. ? uint8_t(Mesh::ConeMaxLod)
  1199. : attrib.m_lod
  1200. ;
  1201. draw(Mesh::Enum(Mesh::Cone0 + lod), mtx[0], 2, attrib.m_wireframe);
  1202. }
  1203. void drawCone(const void* _from, const void* _to, float _radius)
  1204. {
  1205. drawCone( (const float*)_from, (const float*)_to, _radius);
  1206. }
  1207. void drawCylinder(const float* _from, const float* _to, float _radius, bool _capsule)
  1208. {
  1209. const Attrib& attrib = m_attrib[m_stack];
  1210. float tmp0[3];
  1211. bx::vec3Sub(tmp0, _from, _to);
  1212. float normal[3];
  1213. bx::vec3Norm(normal, tmp0);
  1214. float mtx[2][16];
  1215. bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
  1216. bx::memCopy(mtx[1], mtx[0], 64);
  1217. mtx[1][12] = _to[0];
  1218. mtx[1][13] = _to[1];
  1219. mtx[1][14] = _to[2];
  1220. if (_capsule)
  1221. {
  1222. uint8_t lod = attrib.m_lod > Mesh::CapsuleMaxLod
  1223. ? uint8_t(Mesh::CapsuleMaxLod)
  1224. : attrib.m_lod
  1225. ;
  1226. draw(Mesh::Enum(Mesh::Capsule0 + lod), mtx[0], 2, attrib.m_wireframe);
  1227. Sphere sphere;
  1228. bx::vec3Move(sphere.m_center, _from);
  1229. sphere.m_radius = _radius;
  1230. draw(sphere);
  1231. bx::vec3Move(sphere.m_center, _to);
  1232. draw(sphere);
  1233. }
  1234. else
  1235. {
  1236. uint8_t lod = attrib.m_lod > Mesh::CylinderMaxLod
  1237. ? uint8_t(Mesh::CylinderMaxLod)
  1238. : attrib.m_lod
  1239. ;
  1240. draw(Mesh::Enum(Mesh::Cylinder0 + lod), mtx[0], 2, attrib.m_wireframe);
  1241. }
  1242. }
  1243. void drawCylinder(const void* _from, const void* _to, float _radius, bool _capsule)
  1244. {
  1245. drawCylinder( (const float*)_from, (const float*)_to, _radius, _capsule);
  1246. }
  1247. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight, float _thickness)
  1248. {
  1249. push();
  1250. if (_thickness > 0.0f)
  1251. {
  1252. float from[3] = { _x, _y, _z };
  1253. float mid[3];
  1254. float to[3];
  1255. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1256. mid[0] = _x + _len - _thickness;
  1257. mid[1] = _y;
  1258. mid[2] = _z;
  1259. to[0] = _x + _len;
  1260. to[1] = _y;
  1261. to[2] = _z;
  1262. drawCylinder(from, mid, _thickness, false);
  1263. drawCone(mid, to, _thickness);
  1264. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1265. mid[0] = _x;
  1266. mid[1] = _y + _len - _thickness;
  1267. mid[2] = _z;
  1268. to[0] = _x;
  1269. to[1] = _y + _len;
  1270. to[2] = _z;
  1271. drawCylinder(from, mid, _thickness, false);
  1272. drawCone(mid, to, _thickness);
  1273. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1274. mid[0] = _x;
  1275. mid[1] = _y;
  1276. mid[2] = _z + _len - _thickness;
  1277. to[0] = _x;
  1278. to[1] = _y;
  1279. to[2] = _z + _len;
  1280. drawCylinder(from, mid, _thickness, false);
  1281. drawCone(mid, to, _thickness);
  1282. }
  1283. else
  1284. {
  1285. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1286. moveTo(_x, _y, _z);
  1287. lineTo(_x + _len, _y, _z);
  1288. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1289. moveTo(_x, _y, _z);
  1290. lineTo(_x, _y + _len, _z);
  1291. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1292. moveTo(_x, _y, _z);
  1293. lineTo(_x, _y, _z + _len);
  1294. }
  1295. pop();
  1296. }
  1297. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  1298. {
  1299. const Attrib& attrib = m_attrib[m_stack];
  1300. float udir[3];
  1301. float vdir[3];
  1302. bx::vec3TangentFrame(_normal, udir, vdir, attrib.m_spin);
  1303. bx::vec3Mul(udir, udir, _step);
  1304. bx::vec3Mul(vdir, vdir, _step);
  1305. const uint32_t num = (_size/2)*2+1;
  1306. const float halfExtent = float(_size/2);
  1307. float umin[3];
  1308. bx::vec3Mul(umin, udir, -halfExtent);
  1309. float umax[3];
  1310. bx::vec3Mul(umax, udir, halfExtent);
  1311. float vmin[3];
  1312. bx::vec3Mul(vmin, vdir, -halfExtent);
  1313. float vmax[3];
  1314. bx::vec3Mul(vmax, vdir, halfExtent);
  1315. float tmp[3];
  1316. float xs[3];
  1317. float xe[3];
  1318. bx::vec3Add(tmp, umin, vmin);
  1319. bx::vec3Add(xs, _center, tmp);
  1320. bx::vec3Add(tmp, umax, vmin);
  1321. bx::vec3Add(xe, _center, tmp);
  1322. float ys[3];
  1323. float ye[3];
  1324. bx::vec3Add(tmp, umin, vmin);
  1325. bx::vec3Add(ys, _center, tmp);
  1326. bx::vec3Add(tmp, umin, vmax);
  1327. bx::vec3Add(ye, _center, tmp);
  1328. for (uint32_t ii = 0; ii < num; ++ii)
  1329. {
  1330. moveTo(xs);
  1331. lineTo(xe);
  1332. bx::vec3Add(xs, xs, vdir);
  1333. bx::vec3Add(xe, xe, vdir);
  1334. moveTo(ys);
  1335. lineTo(ye);
  1336. bx::vec3Add(ys, ys, udir);
  1337. bx::vec3Add(ye, ye, udir);
  1338. }
  1339. }
  1340. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1341. {
  1342. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  1343. }
  1344. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  1345. {
  1346. push();
  1347. setTranslate(_center);
  1348. const uint32_t num = (_size/2)*2-1;
  1349. const float halfExtent = float(_size/2) * _step;
  1350. setColor(0xff606060);
  1351. float yy = -halfExtent + _step;
  1352. for (uint32_t ii = 0; ii < num; ++ii)
  1353. {
  1354. moveTo(_axis, -halfExtent, yy);
  1355. lineTo(_axis, halfExtent, yy);
  1356. moveTo(_axis, yy, -halfExtent);
  1357. lineTo(_axis, yy, halfExtent);
  1358. yy += _step;
  1359. }
  1360. setColor(0xff101010);
  1361. moveTo(_axis, -halfExtent, -halfExtent);
  1362. lineTo(_axis, -halfExtent, halfExtent);
  1363. lineTo(_axis, halfExtent, halfExtent);
  1364. lineTo(_axis, halfExtent, -halfExtent);
  1365. close();
  1366. moveTo(_axis, -halfExtent, 0.0f);
  1367. lineTo(_axis, halfExtent, 0.0f);
  1368. moveTo(_axis, 0.0f, -halfExtent);
  1369. lineTo(_axis, 0.0f, halfExtent);
  1370. pop();
  1371. }
  1372. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1373. {
  1374. drawGrid(_axis, (const float*)_center, _size, _step);
  1375. }
  1376. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1377. {
  1378. push();
  1379. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  1380. drawCircle(Axis::X, _x, _y, _z, _radius, 0.0f);
  1381. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  1382. drawCircle(Axis::Y, _x, _y, _z, _radius, 0.0f);
  1383. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  1384. drawCircle(Axis::Z, _x, _y, _z, _radius, 0.0f);
  1385. pop();
  1386. }
  1387. private:
  1388. struct Mesh
  1389. {
  1390. enum Enum
  1391. {
  1392. Sphere0,
  1393. Sphere1,
  1394. Sphere2,
  1395. Sphere3,
  1396. Cone0,
  1397. Cone1,
  1398. Cone2,
  1399. Cone3,
  1400. Cylinder0,
  1401. Cylinder1,
  1402. Cylinder2,
  1403. Cylinder3,
  1404. Capsule0,
  1405. Capsule1,
  1406. Capsule2,
  1407. Capsule3,
  1408. Cube,
  1409. Count,
  1410. SphereMaxLod = Sphere3 - Sphere0,
  1411. ConeMaxLod = Cone3 - Cone0,
  1412. CylinderMaxLod = Cylinder3 - Cylinder0,
  1413. CapsuleMaxLod = Capsule3 - Capsule0,
  1414. };
  1415. uint32_t m_startVertex;
  1416. uint32_t m_numVertices;
  1417. uint32_t m_startIndex[2];
  1418. uint32_t m_numIndices[2];
  1419. };
  1420. struct Program
  1421. {
  1422. enum Enum
  1423. {
  1424. Lines,
  1425. LinesStipple,
  1426. Fill,
  1427. FillLit,
  1428. FillTexture,
  1429. Count
  1430. };
  1431. };
  1432. void draw(Mesh::Enum _mesh, const float* _mtx, uint16_t _num, bool _wireframe) const
  1433. {
  1434. const Mesh& mesh = m_mesh[_mesh];
  1435. const Attrib& attrib = m_attrib[m_stack];
  1436. if (0 != mesh.m_numIndices[_wireframe])
  1437. {
  1438. bgfx::setIndexBuffer(m_ibh
  1439. , mesh.m_startIndex[_wireframe]
  1440. , mesh.m_numIndices[_wireframe]
  1441. );
  1442. }
  1443. const float flip = 0 == (attrib.m_state & BGFX_STATE_CULL_CCW) ? 1.0f : -1.0f;
  1444. const uint8_t alpha = attrib.m_abgr>>24;
  1445. float params[4][4] =
  1446. {
  1447. { // lightDir
  1448. 0.0f * flip,
  1449. -1.0f * flip,
  1450. 0.0f * flip,
  1451. 3.0f, // shininess
  1452. },
  1453. { // skyColor
  1454. 1.0f,
  1455. 0.9f,
  1456. 0.8f,
  1457. 0.0f, // unused
  1458. },
  1459. { // groundColor.xyz0
  1460. 0.2f,
  1461. 0.22f,
  1462. 0.5f,
  1463. 0.0f, // unused
  1464. },
  1465. { // matColor
  1466. ( (attrib.m_abgr )&0xff)/255.0f,
  1467. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  1468. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  1469. ( alpha )/255.0f,
  1470. },
  1471. };
  1472. bx::vec3Norm(params[0], params[0]);
  1473. bgfx::setUniform(u_params, params, 4);
  1474. bgfx::setTransform(_mtx, _num);
  1475. bgfx::setVertexBuffer(0, m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1476. bgfx::setState(0
  1477. | attrib.m_state
  1478. | (_wireframe ? BGFX_STATE_PT_LINES|BGFX_STATE_LINEAA|BGFX_STATE_BLEND_ALPHA
  1479. : (alpha < 0xff) ? BGFX_STATE_BLEND_ALPHA : 0)
  1480. );
  1481. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1482. }
  1483. void softFlush()
  1484. {
  1485. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1486. {
  1487. flush();
  1488. }
  1489. }
  1490. void flush()
  1491. {
  1492. if (0 != m_pos)
  1493. {
  1494. if (checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  1495. {
  1496. bgfx::TransientVertexBuffer tvb;
  1497. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  1498. bx::memCopy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  1499. bgfx::TransientIndexBuffer tib;
  1500. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1501. bx::memCopy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1502. const Attrib& attrib = m_attrib[m_stack];
  1503. bgfx::setVertexBuffer(0, &tvb);
  1504. bgfx::setIndexBuffer(&tib);
  1505. bgfx::setState(0
  1506. | BGFX_STATE_RGB_WRITE
  1507. | BGFX_STATE_PT_LINES
  1508. | attrib.m_state
  1509. | BGFX_STATE_LINEAA
  1510. | BGFX_STATE_BLEND_ALPHA
  1511. );
  1512. bgfx::setTransform(m_mtx);
  1513. bgfx::ProgramHandle program = m_program[attrib.m_stipple ? 1 : 0];
  1514. bgfx::submit(m_viewId, program);
  1515. }
  1516. m_state = State::None;
  1517. m_pos = 0;
  1518. m_indexPos = 0;
  1519. m_vertexPos = 0;
  1520. }
  1521. }
  1522. void flushQuad()
  1523. {
  1524. if (0 != m_posQuad)
  1525. {
  1526. const uint32_t numIndices = m_posQuad/4*6;
  1527. if (checkAvailTransientBuffers(m_posQuad, DebugUvVertex::ms_decl, numIndices) )
  1528. {
  1529. bgfx::TransientVertexBuffer tvb;
  1530. bgfx::allocTransientVertexBuffer(&tvb, m_posQuad, DebugUvVertex::ms_decl);
  1531. bx::memCopy(tvb.data, m_cacheQuad, m_posQuad * DebugUvVertex::ms_decl.m_stride);
  1532. bgfx::TransientIndexBuffer tib;
  1533. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1534. uint16_t* indices = (uint16_t*)tib.data;
  1535. for (uint16_t ii = 0, num = m_posQuad/4; ii < num; ++ii)
  1536. {
  1537. uint16_t startVertex = ii*4;
  1538. indices[0] = startVertex+0;
  1539. indices[1] = startVertex+1;
  1540. indices[2] = startVertex+2;
  1541. indices[3] = startVertex+1;
  1542. indices[4] = startVertex+3;
  1543. indices[5] = startVertex+2;
  1544. indices += 6;
  1545. }
  1546. const Attrib& attrib = m_attrib[m_stack];
  1547. bgfx::setVertexBuffer(0, &tvb);
  1548. bgfx::setIndexBuffer(&tib);
  1549. bgfx::setState(0
  1550. | (attrib.m_state & ~BGFX_STATE_CULL_MASK)
  1551. );
  1552. bgfx::setTransform(m_mtx);
  1553. bgfx::setTexture(0, s_texColor, m_texture);
  1554. bgfx::submit(m_viewId, m_program[Program::FillTexture]);
  1555. }
  1556. m_posQuad = 0;
  1557. }
  1558. }
  1559. struct State
  1560. {
  1561. enum Enum
  1562. {
  1563. None,
  1564. MoveTo,
  1565. LineTo,
  1566. Count
  1567. };
  1568. };
  1569. static const uint32_t cacheSize = 1024;
  1570. static const uint32_t stackSize = 16;
  1571. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1572. DebugVertex m_cache[cacheSize+1];
  1573. uint16_t m_indices[cacheSize*2];
  1574. uint16_t m_pos;
  1575. uint16_t m_indexPos;
  1576. uint16_t m_vertexPos;
  1577. static const uint32_t cacheQuadSize = 1024;
  1578. DebugUvVertex m_cacheQuad[cacheQuadSize];
  1579. uint16_t m_posQuad;
  1580. uint32_t m_mtx;
  1581. uint8_t m_viewId;
  1582. uint8_t m_stack;
  1583. bool m_depthTestLess;
  1584. struct Attrib
  1585. {
  1586. uint64_t m_state;
  1587. float m_offset;
  1588. float m_scale;
  1589. float m_spin;
  1590. uint32_t m_abgr;
  1591. bool m_stipple;
  1592. bool m_wireframe;
  1593. uint8_t m_lod;
  1594. };
  1595. Attrib m_attrib[stackSize];
  1596. State::Enum m_state;
  1597. Mesh m_mesh[Mesh::Count];
  1598. typedef SpriteT<256, SPRITE_TEXTURE_SIZE> Sprite;
  1599. Sprite m_sprite;
  1600. bgfx::UniformHandle s_texColor;
  1601. bgfx::TextureHandle m_texture;
  1602. bgfx::ProgramHandle m_program[Program::Count];
  1603. bgfx::UniformHandle u_params;
  1604. bgfx::VertexBufferHandle m_vbh;
  1605. bgfx::IndexBufferHandle m_ibh;
  1606. bx::AllocatorI* m_allocator;
  1607. };
  1608. static DebugDraw s_dd;
  1609. void ddInit(bool _depthTestLess, bx::AllocatorI* _allocator)
  1610. {
  1611. s_dd.init(_depthTestLess, _allocator);
  1612. }
  1613. void ddShutdown()
  1614. {
  1615. s_dd.shutdown();
  1616. }
  1617. SpriteHandle ddCreateSprite(uint16_t _width, uint16_t _height, const void* _data)
  1618. {
  1619. return s_dd.createSprite(_width, _height, _data);
  1620. }
  1621. void ddDestroy(SpriteHandle _handle)
  1622. {
  1623. s_dd.destroy(_handle);
  1624. }
  1625. void ddBegin(uint8_t _viewId)
  1626. {
  1627. s_dd.begin(_viewId);
  1628. }
  1629. void ddEnd()
  1630. {
  1631. s_dd.end();
  1632. }
  1633. void ddPush()
  1634. {
  1635. s_dd.push();
  1636. }
  1637. void ddPop()
  1638. {
  1639. s_dd.pop();
  1640. }
  1641. void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1642. {
  1643. s_dd.setState(_depthTest, _depthWrite, _clockwise);
  1644. }
  1645. void ddSetColor(uint32_t _abgr)
  1646. {
  1647. s_dd.setColor(_abgr);
  1648. }
  1649. void ddSetLod(uint8_t _lod)
  1650. {
  1651. s_dd.setLod(_lod);
  1652. }
  1653. void ddSetWireframe(bool _wireframe)
  1654. {
  1655. s_dd.setWireframe(_wireframe);
  1656. }
  1657. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1658. {
  1659. s_dd.setStipple(_stipple, _scale, _offset);
  1660. }
  1661. void ddSetSpin(float _spin)
  1662. {
  1663. s_dd.setSpin(_spin);
  1664. }
  1665. void ddSetTransform(const void* _mtx)
  1666. {
  1667. s_dd.setTransform(_mtx);
  1668. }
  1669. void ddSetTranslate(float _x, float _y, float _z)
  1670. {
  1671. s_dd.setTranslate(_x, _y, _z);
  1672. }
  1673. void ddMoveTo(float _x, float _y, float _z)
  1674. {
  1675. s_dd.moveTo(_x, _y, _z);
  1676. }
  1677. void ddMoveTo(const void* _pos)
  1678. {
  1679. s_dd.moveTo(_pos);
  1680. }
  1681. void ddLineTo(float _x, float _y, float _z)
  1682. {
  1683. s_dd.lineTo(_x, _y, _z);
  1684. }
  1685. void ddLineTo(const void* _pos)
  1686. {
  1687. s_dd.lineTo(_pos);
  1688. }
  1689. void ddClose()
  1690. {
  1691. s_dd.close();
  1692. }
  1693. void ddDraw(const Aabb& _aabb)
  1694. {
  1695. s_dd.draw(_aabb);
  1696. }
  1697. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1698. {
  1699. s_dd.draw(_cylinder, _capsule);
  1700. }
  1701. void ddDraw(const Disk& _disk)
  1702. {
  1703. s_dd.draw(_disk);
  1704. }
  1705. void ddDraw(const Obb& _obb)
  1706. {
  1707. s_dd.draw(_obb);
  1708. }
  1709. void ddDraw(const Sphere& _sphere)
  1710. {
  1711. s_dd.draw(_sphere);
  1712. }
  1713. void ddDrawFrustum(const void* _viewProj)
  1714. {
  1715. s_dd.drawFrustum(_viewProj);
  1716. }
  1717. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1718. {
  1719. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1720. }
  1721. void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1722. {
  1723. s_dd.drawCircle(_normal, _center, _radius, _weight);
  1724. }
  1725. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1726. {
  1727. s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight);
  1728. }
  1729. void ddDrawQuad(const float* _normal, const float* _center, float _size)
  1730. {
  1731. s_dd.drawQuad(_normal, _center, _size);
  1732. }
  1733. void ddDrawQuad(SpriteHandle _handle, const float* _normal, const float* _center, float _size)
  1734. {
  1735. s_dd.drawQuad(_handle, _normal, _center, _size);
  1736. }
  1737. void ddDrawQuad(bgfx::TextureHandle _handle, const float* _normal, const float* _center, float _size)
  1738. {
  1739. s_dd.drawQuad(_handle, _normal, _center, _size);
  1740. }
  1741. void ddDrawCone(const void* _from, const void* _to, float _radius)
  1742. {
  1743. s_dd.drawCone(_from, _to, _radius);
  1744. }
  1745. void ddDrawCylinder(const void* _from, const void* _to, float _radius, bool _capsule)
  1746. {
  1747. if (_capsule)
  1748. {
  1749. s_dd.push();
  1750. s_dd.setLod(0);
  1751. s_dd.drawCylinder(_from, _to, _radius, true);
  1752. s_dd.pop();
  1753. }
  1754. else
  1755. {
  1756. s_dd.drawCylinder(_from, _to, _radius, false);
  1757. }
  1758. }
  1759. void ddDrawCapsule(const void* _from, const void* _to, float _radius)
  1760. {
  1761. s_dd.drawCylinder(_from, _to, _radius, true);
  1762. }
  1763. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight, float _thickness)
  1764. {
  1765. s_dd.drawAxis(_x, _y, _z, _len, _hightlight, _thickness);
  1766. }
  1767. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1768. {
  1769. s_dd.drawGrid(_normal, _center, _size, _step);
  1770. }
  1771. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1772. {
  1773. s_dd.drawGrid(_axis, _center, _size, _step);
  1774. }
  1775. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1776. {
  1777. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1778. }