debugdraw.cpp 50 KB

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