debugdraw.cpp 52 KB

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