debugdraw.cpp 55 KB

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