debugdraw.cpp 50 KB

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