debugdraw.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  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 "debugdraw.h"
  7. #include <bx/fpumath.h>
  8. #include <bx/radixsort.h>
  9. #include <bx/uint32_t.h>
  10. #include <bx/crtimpl.h>
  11. struct DebugVertex
  12. {
  13. float m_x;
  14. float m_y;
  15. float m_z;
  16. float m_len;
  17. uint32_t m_abgr;
  18. static void init()
  19. {
  20. ms_decl
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::TexCoord0, 1, bgfx::AttribType::Float)
  24. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  25. .end();
  26. }
  27. static bgfx::VertexDecl ms_decl;
  28. };
  29. bgfx::VertexDecl DebugVertex::ms_decl;
  30. struct DebugShapeVertex
  31. {
  32. float m_x;
  33. float m_y;
  34. float m_z;
  35. uint8_t m_indices[4];
  36. static void init()
  37. {
  38. ms_decl
  39. .begin()
  40. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  41. .add(bgfx::Attrib::Indices, 4, bgfx::AttribType::Uint8)
  42. .end();
  43. }
  44. static bgfx::VertexDecl ms_decl;
  45. };
  46. bgfx::VertexDecl DebugShapeVertex::ms_decl;
  47. static DebugShapeVertex s_cubeVertices[8] =
  48. {
  49. {-1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
  50. { 1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
  51. {-1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
  52. { 1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
  53. {-1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
  54. { 1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
  55. {-1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
  56. { 1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
  57. };
  58. static const uint16_t s_cubeIndices[36] =
  59. {
  60. 0, 1, 2, // 0
  61. 1, 3, 2,
  62. 4, 6, 5, // 2
  63. 5, 6, 7,
  64. 0, 2, 4, // 4
  65. 4, 2, 6,
  66. 1, 5, 3, // 6
  67. 5, 7, 3,
  68. 0, 4, 1, // 8
  69. 4, 5, 1,
  70. 2, 3, 6, // 10
  71. 6, 3, 7,
  72. };
  73. static const uint8_t s_circleLod[] =
  74. {
  75. 37,
  76. 29,
  77. 23,
  78. 17,
  79. 11,
  80. };
  81. static uint8_t getCircleLod(uint8_t _lod)
  82. {
  83. _lod = _lod > BX_COUNTOF(s_circleLod)-1 ? BX_COUNTOF(s_circleLod)-1 : _lod;
  84. return s_circleLod[_lod];
  85. }
  86. static void circle(float* _out, float _angle)
  87. {
  88. float sa = bx::fsin(_angle);
  89. float ca = bx::fcos(_angle);
  90. _out[0] = sa;
  91. _out[1] = ca;
  92. }
  93. static void squircle(float* _out, float _angle)
  94. {
  95. float sa = bx::fsin(_angle);
  96. float ca = bx::fcos(_angle);
  97. _out[0] = bx::fsqrt(bx::fabsolute(sa) ) * bx::fsign(sa);
  98. _out[1] = bx::fsqrt(bx::fabsolute(ca) ) * bx::fsign(ca);
  99. }
  100. uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = 0, void* _normals0 = NULL, uint16_t _normalStride0 = 0)
  101. {
  102. if (NULL != _pos0)
  103. {
  104. struct Gen
  105. {
  106. Gen(void* _pos, uint16_t _posStride, void* _normals, uint16_t _normalStride, uint8_t _subdiv)
  107. : m_pos( (uint8_t*)_pos)
  108. , m_normals( (uint8_t*)_normals)
  109. , m_posStride(_posStride)
  110. , m_normalStride(_normalStride)
  111. {
  112. static const float scale = 1.0f;
  113. static const float golden = 1.6180339887f;
  114. static const float len = bx::fsqrt(golden*golden + 1.0f);
  115. static const float ss = 1.0f/len * scale;
  116. static const float ll = ss*golden;
  117. static const float vv[12][4] =
  118. {
  119. { -ll, 0.0f, -ss, 0.0f },
  120. { ll, 0.0f, -ss, 0.0f },
  121. { ll, 0.0f, ss, 0.0f },
  122. { -ll, 0.0f, ss, 0.0f },
  123. { -ss, ll, 0.0f, 0.0f },
  124. { ss, ll, 0.0f, 0.0f },
  125. { ss, -ll, 0.0f, 0.0f },
  126. { -ss, -ll, 0.0f, 0.0f },
  127. { 0.0f, -ss, ll, 0.0f },
  128. { 0.0f, ss, ll, 0.0f },
  129. { 0.0f, ss, -ll, 0.0f },
  130. { 0.0f, -ss, -ll, 0.0f },
  131. };
  132. m_numVertices = 0;
  133. triangle(vv[ 0], vv[ 4], vv[ 3], scale, _subdiv);
  134. triangle(vv[ 0], vv[10], vv[ 4], scale, _subdiv);
  135. triangle(vv[ 4], vv[10], vv[ 5], scale, _subdiv);
  136. triangle(vv[ 5], vv[10], vv[ 1], scale, _subdiv);
  137. triangle(vv[ 5], vv[ 1], vv[ 2], scale, _subdiv);
  138. triangle(vv[ 5], vv[ 2], vv[ 9], scale, _subdiv);
  139. triangle(vv[ 5], vv[ 9], vv[ 4], scale, _subdiv);
  140. triangle(vv[ 3], vv[ 4], vv[ 9], scale, _subdiv);
  141. triangle(vv[ 0], vv[ 3], vv[ 7], scale, _subdiv);
  142. triangle(vv[ 0], vv[ 7], vv[11], scale, _subdiv);
  143. triangle(vv[11], vv[ 7], vv[ 6], scale, _subdiv);
  144. triangle(vv[11], vv[ 6], vv[ 1], scale, _subdiv);
  145. triangle(vv[ 1], vv[ 6], vv[ 2], scale, _subdiv);
  146. triangle(vv[ 2], vv[ 6], vv[ 8], scale, _subdiv);
  147. triangle(vv[ 8], vv[ 6], vv[ 7], scale, _subdiv);
  148. triangle(vv[ 8], vv[ 7], vv[ 3], scale, _subdiv);
  149. triangle(vv[ 0], vv[11], vv[10], scale, _subdiv);
  150. triangle(vv[ 1], vv[10], vv[11], scale, _subdiv);
  151. triangle(vv[ 2], vv[ 8], vv[ 9], scale, _subdiv);
  152. triangle(vv[ 3], vv[ 9], vv[ 8], scale, _subdiv);
  153. }
  154. void addVert(const float* _v)
  155. {
  156. float* verts = (float*)m_pos;
  157. verts[0] = _v[0];
  158. verts[1] = _v[1];
  159. verts[2] = _v[2];
  160. m_pos += m_posStride;
  161. if (NULL != m_normals)
  162. {
  163. float* normals = (float*)m_normals;
  164. bx::vec3Norm(normals, _v);
  165. m_normals += m_normalStride;
  166. }
  167. m_numVertices++;
  168. }
  169. void triangle(const float* _v0, const float* _v1, const float* _v2, float _scale, uint8_t _subdiv)
  170. {
  171. if (0 == _subdiv)
  172. {
  173. addVert(_v0);
  174. addVert(_v1);
  175. addVert(_v2);
  176. }
  177. else
  178. {
  179. float tmp0[4];
  180. float tmp1[4];
  181. float v01[4];
  182. bx::vec3Add(tmp0, _v0, _v1);
  183. bx::vec3Norm(tmp1, tmp0);
  184. bx::vec3Mul(v01, tmp1, _scale);
  185. float v12[4];
  186. bx::vec3Add(tmp0, _v1, _v2);
  187. bx::vec3Norm(tmp1, tmp0);
  188. bx::vec3Mul(v12, tmp1, _scale);
  189. float v20[4];
  190. bx::vec3Add(tmp0, _v2, _v0);
  191. bx::vec3Norm(tmp1, tmp0);
  192. bx::vec3Mul(v20, tmp1, _scale);
  193. --_subdiv;
  194. triangle(_v0, v01, v20, _scale, _subdiv);
  195. triangle(_v1, v12, v01, _scale, _subdiv);
  196. triangle(_v2, v20, v12, _scale, _subdiv);
  197. triangle(v01, v12, v20, _scale, _subdiv);
  198. }
  199. }
  200. uint8_t* m_pos;
  201. uint8_t* m_normals;
  202. uint16_t m_posStride;
  203. uint16_t m_normalStride;
  204. uint32_t m_numVertices;
  205. } gen(_pos0, _posStride0, _normals0, _normalStride0, _subdiv0);
  206. }
  207. uint32_t numVertices = 20*3*bx::uint32_max(1, (uint32_t)bx::fpow(4.0f, _subdiv0) );
  208. return numVertices;
  209. }
  210. void getPoint(float* _result, Axis::Enum _axis, float _x, float _y)
  211. {
  212. switch (_axis)
  213. {
  214. case Axis::X:
  215. _result[0] = 0.0f;
  216. _result[1] = _x;
  217. _result[2] = _y;
  218. break;
  219. case Axis::Y:
  220. _result[0] = _y;
  221. _result[1] = 0.0f;
  222. _result[2] = _x;
  223. break;
  224. default:
  225. _result[0] = _x;
  226. _result[1] = _y;
  227. _result[2] = 0.0f;
  228. break;
  229. }
  230. }
  231. #include "vs_debugdraw_lines.bin.h"
  232. #include "fs_debugdraw_lines.bin.h"
  233. #include "vs_debugdraw_lines_stipple.bin.h"
  234. #include "fs_debugdraw_lines_stipple.bin.h"
  235. #include "vs_debugdraw_fill.bin.h"
  236. #include "fs_debugdraw_fill.bin.h"
  237. #include "vs_debugdraw_fill_lit.bin.h"
  238. #include "fs_debugdraw_fill_lit.bin.h"
  239. struct EmbeddedShader
  240. {
  241. bgfx::RendererType::Enum type;
  242. const uint8_t* data;
  243. uint32_t size;
  244. };
  245. #define BGFX_DECLARE_SHADER_EMBEDDED(_name) \
  246. { \
  247. { bgfx::RendererType::Direct3D9, BX_CONCATENATE(_name, _dx9 ), sizeof(BX_CONCATENATE(_name, _dx9 ) ) }, \
  248. { bgfx::RendererType::Direct3D11, BX_CONCATENATE(_name, _dx11), sizeof(BX_CONCATENATE(_name, _dx11) ) }, \
  249. { bgfx::RendererType::Direct3D12, BX_CONCATENATE(_name, _dx11), sizeof(BX_CONCATENATE(_name, _dx11) ) }, \
  250. { bgfx::RendererType::OpenGL, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  251. { bgfx::RendererType::OpenGLES, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  252. { bgfx::RendererType::Vulkan, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  253. { bgfx::RendererType::Metal, BX_CONCATENATE(_name, _mtl ), sizeof(BX_CONCATENATE(_name, _mtl ) ) }, \
  254. { bgfx::RendererType::Count, NULL, 0 }, \
  255. }
  256. static const EmbeddedShader s_embeddedShaders[][8] =
  257. {
  258. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_lines),
  259. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_lines),
  260. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_lines_stipple),
  261. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_lines_stipple),
  262. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_fill),
  263. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_fill),
  264. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_fill_lit),
  265. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_fill_lit),
  266. };
  267. static bgfx::ShaderHandle createEmbeddedShader(bgfx::RendererType::Enum _type, uint32_t _index)
  268. {
  269. for (const EmbeddedShader* es = s_embeddedShaders[_index]; bgfx::RendererType::Count != es->type; ++es)
  270. {
  271. if (_type == es->type)
  272. {
  273. return bgfx::createShader(bgfx::makeRef(es->data, es->size) );
  274. }
  275. }
  276. bgfx::ShaderHandle handle = BGFX_INVALID_HANDLE;
  277. return handle;
  278. }
  279. struct DebugDraw
  280. {
  281. DebugDraw()
  282. : m_depthTestLess(true)
  283. , m_state(State::Count)
  284. {
  285. }
  286. void init(bool _depthTestLess, bx::AllocatorI* _allocator)
  287. {
  288. m_allocator = _allocator;
  289. m_depthTestLess = _depthTestLess;
  290. #if BX_CONFIG_ALLOCATOR_CRT
  291. if (NULL == _allocator)
  292. {
  293. static bx::CrtAllocator allocator;
  294. m_allocator = &allocator;
  295. }
  296. #endif // BX_CONFIG_ALLOCATOR_CRT
  297. DebugVertex::init();
  298. DebugShapeVertex::init();
  299. bgfx::RendererType::Enum type = bgfx::getRendererType();
  300. m_program[Program::Lines] =
  301. bgfx::createProgram(createEmbeddedShader(type, 0)
  302. , createEmbeddedShader(type, 1)
  303. , true
  304. );
  305. m_program[Program::LinesStipple] =
  306. bgfx::createProgram(createEmbeddedShader(type, 2)
  307. , createEmbeddedShader(type, 3)
  308. , true
  309. );
  310. m_program[Program::Fill] =
  311. bgfx::createProgram(createEmbeddedShader(type, 4)
  312. , createEmbeddedShader(type, 5)
  313. , true
  314. );
  315. m_program[Program::FillLit] =
  316. bgfx::createProgram(createEmbeddedShader(type, 6)
  317. , createEmbeddedShader(type, 7)
  318. , true
  319. );
  320. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 4);
  321. void* vertices[Mesh::Count] = {};
  322. uint16_t* indices[Mesh::Count] = {};
  323. uint16_t stride = DebugShapeVertex::ms_decl.getStride();
  324. uint32_t startVertex = 0;
  325. uint32_t startIndex = 0;
  326. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  327. {
  328. Mesh::Enum id = Mesh::Enum(Mesh::Sphere0+mesh);
  329. const uint8_t tess = uint8_t(3-mesh);
  330. const uint32_t numVertices = genSphere(tess);
  331. const uint32_t numIndices = numVertices;
  332. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  333. memset(vertices[id], 0, numVertices*stride);
  334. genSphere(tess, vertices[id], stride);
  335. uint16_t* trilist = (uint16_t*)BX_ALLOC(m_allocator, numIndices*sizeof(uint16_t) );
  336. for (uint32_t ii = 0; ii < numIndices; ++ii)
  337. {
  338. trilist[ii] = uint16_t(ii);
  339. }
  340. uint32_t numLineListIndices = bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  341. , NULL
  342. , 0
  343. , trilist
  344. , numIndices
  345. , false
  346. );
  347. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  348. uint16_t* indicesOut = indices[id];
  349. memcpy(indicesOut, trilist, numIndices*sizeof(uint16_t) );
  350. bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  351. , &indicesOut[numIndices]
  352. , numLineListIndices*sizeof(uint16_t)
  353. , trilist
  354. , numIndices
  355. , false
  356. );
  357. m_mesh[id].m_startVertex = startVertex;
  358. m_mesh[id].m_numVertices = numVertices;
  359. m_mesh[id].m_startIndex[0] = startIndex;
  360. m_mesh[id].m_numIndices[0] = numIndices;
  361. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  362. m_mesh[id].m_numIndices[1] = numLineListIndices;
  363. startVertex += numVertices;
  364. startIndex += numIndices + numLineListIndices;
  365. BX_FREE(m_allocator, trilist);
  366. }
  367. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  368. {
  369. Mesh::Enum id = Mesh::Enum(Mesh::Cone0+mesh);
  370. const uint32_t num = getCircleLod(mesh);
  371. const float step = bx::pi * 2.0f / num;
  372. const uint32_t numVertices = num+1;
  373. const uint32_t numIndices = num*6;
  374. const uint32_t numLineListIndices = num*4;
  375. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  376. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  377. memset(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  378. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  379. uint16_t* index = indices[id];
  380. vertex[num].m_x = 0.0f;
  381. vertex[num].m_y = 0.0f;
  382. vertex[num].m_z = 0.0f;
  383. vertex[num].m_indices[0] = 1;
  384. for (uint32_t ii = 0; ii < num; ++ii)
  385. {
  386. const float angle = step * ii;
  387. float xy[2];
  388. circle(xy, angle);
  389. vertex[ii].m_x = xy[1];
  390. vertex[ii].m_y = 0.0f;
  391. vertex[ii].m_z = xy[0];
  392. vertex[ii].m_indices[0] = 0;
  393. index[ii*3+0] = num;
  394. index[ii*3+1] = (ii+1)%num;
  395. index[ii*3+2] = ii;
  396. index[num*3+ii*3+0] = 0;
  397. index[num*3+ii*3+1] = ii;
  398. index[num*3+ii*3+2] = (ii+1)%num;
  399. index[numIndices+ii*2+0] = ii;
  400. index[numIndices+ii*2+1] = num;
  401. index[numIndices+num*2+ii*2+0] = ii;
  402. index[numIndices+num*2+ii*2+1] = (ii+1)%num;
  403. }
  404. m_mesh[id].m_startVertex = startVertex;
  405. m_mesh[id].m_numVertices = numVertices;
  406. m_mesh[id].m_startIndex[0] = startIndex;
  407. m_mesh[id].m_numIndices[0] = numIndices;
  408. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  409. m_mesh[id].m_numIndices[1] = numLineListIndices;
  410. startVertex += numVertices;
  411. startIndex += numIndices + numLineListIndices;
  412. }
  413. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  414. {
  415. Mesh::Enum id = Mesh::Enum(Mesh::Cylinder0+mesh);
  416. const uint32_t num = getCircleLod(mesh);
  417. const float step = bx::pi * 2.0f / num;
  418. const uint32_t numVertices = num*2;
  419. const uint32_t numIndices = num*12;
  420. const uint32_t numLineListIndices = num*6;
  421. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  422. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  423. memset(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  424. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  425. uint16_t* index = indices[id];
  426. for (uint32_t ii = 0; ii < num; ++ii)
  427. {
  428. const float angle = step * ii;
  429. float xy[2];
  430. circle(xy, angle);
  431. vertex[ii].m_x = xy[1];
  432. vertex[ii].m_y = 0.0f;
  433. vertex[ii].m_z = xy[0];
  434. vertex[ii].m_indices[0] = 0;
  435. vertex[ii+num].m_x = xy[1];
  436. vertex[ii+num].m_y = 0.0f;
  437. vertex[ii+num].m_z = xy[0];
  438. vertex[ii+num].m_indices[0] = 1;
  439. index[ii*6+0] = ii+num;
  440. index[ii*6+1] = (ii+1)%num;
  441. index[ii*6+2] = ii;
  442. index[ii*6+3] = ii+num;
  443. index[ii*6+4] = (ii+1)%num+num;
  444. index[ii*6+5] = (ii+1)%num;
  445. index[num*6+ii*6+0] = 0;
  446. index[num*6+ii*6+1] = ii;
  447. index[num*6+ii*6+2] = (ii+1)%num;
  448. index[num*6+ii*6+3] = num;
  449. index[num*6+ii*6+4] = (ii+1)%num+num;
  450. index[num*6+ii*6+5] = ii+num;
  451. index[numIndices+ii*2+0] = ii;
  452. index[numIndices+ii*2+1] = ii+num;
  453. index[numIndices+num*2+ii*2+0] = ii;
  454. index[numIndices+num*2+ii*2+1] = (ii+1)%num;
  455. index[numIndices+num*4+ii*2+0] = num + ii;
  456. index[numIndices+num*4+ii*2+1] = num + (ii+1)%num;
  457. }
  458. m_mesh[id].m_startVertex = startVertex;
  459. m_mesh[id].m_numVertices = numVertices;
  460. m_mesh[id].m_startIndex[0] = startIndex;
  461. m_mesh[id].m_numIndices[0] = numIndices;
  462. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  463. m_mesh[id].m_numIndices[1] = numLineListIndices;
  464. startVertex += numVertices;
  465. startIndex += numIndices + numLineListIndices;
  466. }
  467. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  468. {
  469. Mesh::Enum id = Mesh::Enum(Mesh::Capsule0+mesh);
  470. // const uint8_t tess = uint8_t(3-mesh);
  471. const uint32_t numVertices = 0;
  472. const uint32_t numIndices = 0;
  473. const uint32_t numLineListIndices = 0;
  474. m_mesh[id].m_startVertex = startVertex;
  475. m_mesh[id].m_numVertices = numVertices;
  476. m_mesh[id].m_startIndex[0] = startIndex;
  477. m_mesh[id].m_numIndices[0] = numIndices;
  478. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  479. m_mesh[id].m_numIndices[1] = numLineListIndices;
  480. startVertex += numVertices;
  481. startIndex += numIndices + numLineListIndices;
  482. }
  483. m_mesh[Mesh::Cube].m_startVertex = startVertex;
  484. m_mesh[Mesh::Cube].m_numVertices = BX_COUNTOF(s_cubeVertices);
  485. m_mesh[Mesh::Cube].m_startIndex[0] = startIndex;
  486. m_mesh[Mesh::Cube].m_numIndices[0] = BX_COUNTOF(s_cubeIndices);
  487. m_mesh[Mesh::Cube].m_startIndex[1] = 0;
  488. m_mesh[Mesh::Cube].m_numIndices[1] = 0;
  489. startVertex += m_mesh[Mesh::Cube].m_numVertices;
  490. startIndex += m_mesh[Mesh::Cube].m_numIndices[0];
  491. const bgfx::Memory* vb = bgfx::alloc(startVertex*stride);
  492. const bgfx::Memory* ib = bgfx::alloc(startIndex*sizeof(uint16_t) );
  493. for (uint32_t mesh = Mesh::Sphere0; mesh < Mesh::Cube; ++mesh)
  494. {
  495. Mesh::Enum id = Mesh::Enum(mesh);
  496. memcpy(&vb->data[m_mesh[id].m_startVertex * stride]
  497. , vertices[id]
  498. , m_mesh[id].m_numVertices*stride
  499. );
  500. memcpy(&ib->data[m_mesh[id].m_startIndex[0] * sizeof(uint16_t)]
  501. , indices[id]
  502. , (m_mesh[id].m_numIndices[0]+m_mesh[id].m_numIndices[1])*sizeof(uint16_t)
  503. );
  504. BX_FREE(m_allocator, vertices[id]);
  505. BX_FREE(m_allocator, indices[id]);
  506. }
  507. memcpy(&vb->data[m_mesh[Mesh::Cube].m_startVertex * stride]
  508. , s_cubeVertices
  509. , sizeof(s_cubeVertices)
  510. );
  511. memcpy(&ib->data[m_mesh[Mesh::Cube].m_startIndex[0] * sizeof(uint16_t)]
  512. , s_cubeIndices
  513. , sizeof(s_cubeIndices)
  514. );
  515. m_vbh = bgfx::createVertexBuffer(vb, DebugShapeVertex::ms_decl);
  516. m_ibh = bgfx::createIndexBuffer(ib);
  517. m_mtx = 0;
  518. m_viewId = 0;
  519. m_pos = 0;
  520. m_indexPos = 0;
  521. m_vertexPos = 0;
  522. }
  523. void shutdown()
  524. {
  525. bgfx::destroyIndexBuffer(m_ibh);
  526. bgfx::destroyVertexBuffer(m_vbh);
  527. for (uint32_t ii = 0; ii < Program::Count; ++ii)
  528. {
  529. bgfx::destroyProgram(m_program[ii]);
  530. }
  531. bgfx::destroyUniform(u_params);
  532. }
  533. void begin(uint8_t _viewId)
  534. {
  535. BX_CHECK(State::Count == m_state);
  536. m_viewId = _viewId;
  537. m_mtx = 0;
  538. m_state = State::None;
  539. m_stack = 0;
  540. Attrib& attrib = m_attrib[0];
  541. attrib.m_state = 0
  542. | BGFX_STATE_RGB_WRITE
  543. | (m_depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER)
  544. | BGFX_STATE_CULL_CW
  545. | BGFX_STATE_DEPTH_WRITE
  546. ;
  547. attrib.m_scale = 1.0f;
  548. attrib.m_offset = 0.0f;
  549. attrib.m_abgr = UINT32_MAX;
  550. attrib.m_stipple = false;
  551. attrib.m_wireframe = false;
  552. attrib.m_lod = 0;
  553. }
  554. void end()
  555. {
  556. BX_CHECK(0 == m_stack, "Invalid stack %d.", m_stack);
  557. flush();
  558. m_state = State::Count;
  559. }
  560. void push()
  561. {
  562. BX_CHECK(State::Count != m_state);
  563. ++m_stack;
  564. m_attrib[m_stack] = m_attrib[m_stack-1];
  565. }
  566. void pop()
  567. {
  568. BX_CHECK(State::Count != m_state);
  569. const Attrib& curr = m_attrib[m_stack];
  570. const Attrib& prev = m_attrib[m_stack-1];
  571. if (curr.m_stipple != prev.m_stipple
  572. || curr.m_state != prev.m_state)
  573. {
  574. flush();
  575. }
  576. --m_stack;
  577. }
  578. void setTransform(const void* _mtx)
  579. {
  580. BX_CHECK(State::Count != m_state);
  581. flush();
  582. if (NULL == _mtx)
  583. {
  584. m_mtx = 0;
  585. return;
  586. }
  587. bgfx::Transform transform;
  588. m_mtx = bgfx::allocTransform(&transform, 1);
  589. memcpy(transform.data, _mtx, 64);
  590. }
  591. void setTranslate(float _x, float _y, float _z)
  592. {
  593. float mtx[16];
  594. bx::mtxTranslate(mtx, _x, _y, _z);
  595. setTransform(mtx);
  596. }
  597. void setTranslate(const float* _pos)
  598. {
  599. setTranslate(_pos[0], _pos[1], _pos[2]);
  600. }
  601. void setState(bool _depthTest, bool _depthWrite, bool _clockwise)
  602. {
  603. const uint64_t depthTest = m_depthTestLess
  604. ? BGFX_STATE_DEPTH_TEST_LESS
  605. : BGFX_STATE_DEPTH_TEST_GREATER
  606. ;
  607. m_attrib[m_stack].m_state &= ~(0
  608. | BGFX_STATE_DEPTH_TEST_MASK
  609. | BGFX_STATE_DEPTH_WRITE
  610. | BGFX_STATE_CULL_CW
  611. | BGFX_STATE_CULL_CCW
  612. );
  613. m_attrib[m_stack].m_state |= _depthTest
  614. ? depthTest
  615. : 0
  616. ;
  617. m_attrib[m_stack].m_state |= _depthWrite
  618. ? BGFX_STATE_DEPTH_WRITE
  619. : 0
  620. ;
  621. m_attrib[m_stack].m_state |= _clockwise
  622. ? BGFX_STATE_CULL_CW
  623. : BGFX_STATE_CULL_CCW
  624. ;
  625. }
  626. void setColor(uint32_t _abgr)
  627. {
  628. BX_CHECK(State::Count != m_state);
  629. m_attrib[m_stack].m_abgr = _abgr;
  630. }
  631. void setLod(uint8_t _lod)
  632. {
  633. BX_CHECK(State::Count != m_state);
  634. m_attrib[m_stack].m_lod = _lod;
  635. }
  636. void setWireframe(bool _wireframe)
  637. {
  638. BX_CHECK(State::Count != m_state);
  639. m_attrib[m_stack].m_wireframe = _wireframe;
  640. }
  641. void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
  642. {
  643. BX_CHECK(State::Count != m_state);
  644. Attrib& attrib = m_attrib[m_stack];
  645. if (attrib.m_stipple != _stipple)
  646. {
  647. flush();
  648. }
  649. attrib.m_stipple = _stipple;
  650. attrib.m_offset = _offset;
  651. attrib.m_scale = _scale;
  652. }
  653. void moveTo(float _x, float _y, float _z = 0.0f)
  654. {
  655. BX_CHECK(State::Count != m_state);
  656. softFlush();
  657. m_state = State::MoveTo;
  658. DebugVertex& vertex = m_cache[m_pos];
  659. vertex.m_x = _x;
  660. vertex.m_y = _y;
  661. vertex.m_z = _z;
  662. Attrib& attrib = m_attrib[m_stack];
  663. vertex.m_abgr = attrib.m_abgr;
  664. vertex.m_len = attrib.m_offset;
  665. m_vertexPos = m_pos;
  666. }
  667. void moveTo(const void* _pos)
  668. {
  669. BX_CHECK(State::Count != m_state);
  670. const float* pos = (const float*)_pos;
  671. moveTo(pos[0], pos[1], pos[2]);
  672. }
  673. void moveTo(Axis::Enum _axis, float _x, float _y)
  674. {
  675. float pos[3];
  676. getPoint(pos, _axis, _x, _y);
  677. moveTo(pos);
  678. }
  679. void lineTo(float _x, float _y, float _z = 0.0f)
  680. {
  681. BX_CHECK(State::Count != m_state);
  682. if (State::None == m_state)
  683. {
  684. moveTo(_x, _y, _z);
  685. return;
  686. }
  687. if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
  688. {
  689. uint32_t pos = m_pos;
  690. uint32_t vertexPos = m_vertexPos;
  691. flush();
  692. memcpy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
  693. if (vertexPos == pos)
  694. {
  695. m_pos = 1;
  696. }
  697. else
  698. {
  699. memcpy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
  700. m_pos = 2;
  701. }
  702. m_state = State::LineTo;
  703. }
  704. else if (State::MoveTo == m_state)
  705. {
  706. ++m_pos;
  707. m_state = State::LineTo;
  708. }
  709. uint16_t prev = m_pos-1;
  710. uint16_t curr = m_pos++;
  711. DebugVertex& vertex = m_cache[curr];
  712. vertex.m_x = _x;
  713. vertex.m_y = _y;
  714. vertex.m_z = _z;
  715. Attrib& attrib = m_attrib[m_stack];
  716. vertex.m_abgr = attrib.m_abgr;
  717. vertex.m_len = attrib.m_offset;
  718. float tmp[3];
  719. bx::vec3Sub(tmp, &vertex.m_x, &m_cache[prev].m_x);
  720. float len = bx::vec3Length(tmp) * attrib.m_scale;
  721. vertex.m_len = m_cache[prev].m_len + len;
  722. m_indices[m_indexPos++] = prev;
  723. m_indices[m_indexPos++] = curr;
  724. }
  725. void lineTo(const void* _pos)
  726. {
  727. BX_CHECK(State::Count != m_state);
  728. const float* pos = (const float*)_pos;
  729. lineTo(pos[0], pos[1], pos[2]);
  730. }
  731. void lineTo(Axis::Enum _axis, float _x, float _y)
  732. {
  733. float pos[3];
  734. getPoint(pos, _axis, _x, _y);
  735. lineTo(pos);
  736. }
  737. void close()
  738. {
  739. BX_CHECK(State::Count != m_state);
  740. DebugVertex& vertex = m_cache[m_vertexPos];
  741. lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
  742. m_state = State::None;
  743. }
  744. void draw(const Aabb& _aabb)
  745. {
  746. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  747. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  748. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  749. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  750. close();
  751. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  752. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  753. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  754. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  755. close();
  756. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  757. lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  758. moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  759. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  760. moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  761. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  762. moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  763. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  764. }
  765. void draw(const Cylinder& _cylinder, bool _capsule)
  766. {
  767. BX_UNUSED(_cylinder, _capsule);
  768. }
  769. void draw(const Disk& _disk)
  770. {
  771. BX_UNUSED(_disk);
  772. }
  773. void draw(const Obb& _obb)
  774. {
  775. const Attrib& attrib = m_attrib[m_stack];
  776. if (attrib.m_wireframe)
  777. {
  778. setTransform(_obb.m_mtx);
  779. moveTo(-1.0f, -1.0f, -1.0f);
  780. lineTo( 1.0f, -1.0f, -1.0f);
  781. lineTo( 1.0f, 1.0f, -1.0f);
  782. lineTo(-1.0f, 1.0f, -1.0f);
  783. close();
  784. moveTo(-1.0f, 1.0f, 1.0f);
  785. lineTo( 1.0f, 1.0f, 1.0f);
  786. lineTo( 1.0f, -1.0f, 1.0f);
  787. lineTo(-1.0f, -1.0f, 1.0f);
  788. close();
  789. moveTo( 1.0f, -1.0f, -1.0f);
  790. lineTo( 1.0f, -1.0f, 1.0f);
  791. moveTo( 1.0f, 1.0f, -1.0f);
  792. lineTo( 1.0f, 1.0f, 1.0f);
  793. moveTo(-1.0f, 1.0f, -1.0f);
  794. lineTo(-1.0f, 1.0f, 1.0f);
  795. moveTo(-1.0f, -1.0f, -1.0f);
  796. lineTo(-1.0f, -1.0f, 1.0f);
  797. setTransform(NULL);
  798. }
  799. else
  800. {
  801. draw(Mesh::Cube, _obb.m_mtx, 1, false);
  802. }
  803. }
  804. void draw(const Sphere& _sphere)
  805. {
  806. const Attrib& attrib = m_attrib[m_stack];
  807. float mtx[16];
  808. bx::mtxSRT(mtx
  809. , _sphere.m_radius
  810. , _sphere.m_radius
  811. , _sphere.m_radius
  812. , 0.0f
  813. , 0.0f
  814. , 0.0f
  815. , _sphere.m_center[0]
  816. , _sphere.m_center[1]
  817. , _sphere.m_center[2]
  818. );
  819. uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
  820. ? uint8_t(Mesh::SphereMaxLod)
  821. : attrib.m_lod
  822. ;
  823. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe);
  824. }
  825. void drawFrustum(const float* _viewProj)
  826. {
  827. Plane planes[6];
  828. buildFrustumPlanes(planes, _viewProj);
  829. float points[24];
  830. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  831. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  832. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  833. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  834. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  835. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  836. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  837. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  838. moveTo(&points[ 0]);
  839. lineTo(&points[ 3]);
  840. lineTo(&points[ 6]);
  841. lineTo(&points[ 9]);
  842. close();
  843. moveTo(&points[12]);
  844. lineTo(&points[15]);
  845. lineTo(&points[18]);
  846. lineTo(&points[21]);
  847. close();
  848. moveTo(&points[ 0]);
  849. lineTo(&points[12]);
  850. moveTo(&points[ 3]);
  851. lineTo(&points[15]);
  852. moveTo(&points[ 6]);
  853. lineTo(&points[18]);
  854. moveTo(&points[ 9]);
  855. lineTo(&points[21]);
  856. }
  857. void drawFrustum(const void* _viewProj)
  858. {
  859. drawFrustum( (const float*)_viewProj);
  860. }
  861. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  862. {
  863. const Attrib& attrib = m_attrib[m_stack];
  864. const uint32_t num = getCircleLod(attrib.m_lod);
  865. const float step = bx::pi * 2.0f / num;
  866. _degrees = bx::fwrap(_degrees, 360.0f);
  867. float pos[3];
  868. getPoint(pos, _axis
  869. , bx::fsin(step * 0)*_radius
  870. , bx::fcos(step * 0)*_radius
  871. );
  872. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  873. uint32_t n = uint32_t(num*_degrees/360.0f);
  874. for (uint32_t ii = 1; ii < n+1; ++ii)
  875. {
  876. getPoint(pos, _axis
  877. , bx::fsin(step * ii)*_radius
  878. , bx::fcos(step * ii)*_radius
  879. );
  880. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  881. }
  882. moveTo(_x, _y, _z);
  883. getPoint(pos, _axis
  884. , bx::fsin(step * 0)*_radius
  885. , bx::fcos(step * 0)*_radius
  886. );
  887. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  888. getPoint(pos, _axis
  889. , bx::fsin(step * n)*_radius
  890. , bx::fcos(step * n)*_radius
  891. );
  892. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  893. lineTo(_x, _y, _z);
  894. }
  895. void drawCircle(const float* _normal, const float* _center, float _radius, float _weight = 0.0f)
  896. {
  897. const Attrib& attrib = m_attrib[m_stack];
  898. const uint32_t num = getCircleLod(attrib.m_lod);
  899. const float step = bx::pi * 2.0f / num;
  900. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  901. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  902. float udir[3];
  903. float vdir[3];
  904. calcPlaneUv(plane, udir, vdir);
  905. float pos[3];
  906. float tmp0[3];
  907. float tmp1[3];
  908. float xy0[2];
  909. float xy1[2];
  910. circle(xy0, 0.0f);
  911. squircle(xy1, 0.0f);
  912. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  913. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  914. bx::vec3Add(tmp1, pos, tmp0);
  915. bx::vec3Add(pos, tmp1, _center);
  916. moveTo(pos);
  917. for (uint32_t ii = 1; ii < num; ++ii)
  918. {
  919. float angle = step * ii;
  920. circle(xy0, angle);
  921. squircle(xy1, angle);
  922. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  923. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  924. bx::vec3Add(tmp1, pos, tmp0);
  925. bx::vec3Add(pos, tmp1, _center);
  926. lineTo(pos);
  927. }
  928. close();
  929. }
  930. void drawCircle(const void* _normal, const void* _center, float _radius, float _weight = 0.0f)
  931. {
  932. drawCircle( (const float*)_normal, (const float*)_center, _radius, _weight);
  933. }
  934. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight = 0.0f)
  935. {
  936. const Attrib& attrib = m_attrib[m_stack];
  937. const uint32_t num = getCircleLod(attrib.m_lod);
  938. const float step = bx::pi * 2.0f / num;
  939. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  940. float xy0[2];
  941. float xy1[2];
  942. circle(xy0, 0.0f);
  943. squircle(xy1, 0.0f);
  944. float pos[3];
  945. getPoint(pos, _axis
  946. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  947. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  948. );
  949. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  950. for (uint32_t ii = 1; ii < num; ++ii)
  951. {
  952. float angle = step * ii;
  953. circle(xy0, angle);
  954. squircle(xy1, angle);
  955. getPoint(pos, _axis
  956. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  957. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  958. );
  959. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  960. }
  961. close();
  962. }
  963. void drawCone(const float* _from, const float* _to, float _radius, float _weight = 0.0f)
  964. {
  965. const Attrib& attrib = m_attrib[m_stack];
  966. BX_UNUSED(_weight);
  967. float tmp0[3];
  968. bx::vec3Sub(tmp0, _from, _to);
  969. float normal[3];
  970. bx::vec3Norm(normal, tmp0);
  971. float mtx[2][16];
  972. bx::mtxFromNormal(mtx[0], normal, _radius, _from);
  973. memcpy(mtx[1], mtx[0], 64);
  974. mtx[1][12] = _to[0];
  975. mtx[1][13] = _to[1];
  976. mtx[1][14] = _to[2];
  977. uint8_t lod = attrib.m_lod > Mesh::ConeMaxLod
  978. ? uint8_t(Mesh::ConeMaxLod)
  979. : attrib.m_lod
  980. ;
  981. draw(Mesh::Enum(Mesh::Cone0 + lod), mtx[0], 2, attrib.m_wireframe);
  982. }
  983. void drawCone(const void* _from, const void* _to, float _radius, float _weight = 0.0f)
  984. {
  985. drawCone( (const float*)_from, (const float*)_to, _radius, _weight);
  986. }
  987. void drawCylinder(const float* _from, const float* _to, float _radius, float _weight = 0.0f)
  988. {
  989. const Attrib& attrib = m_attrib[m_stack];
  990. BX_UNUSED(_weight);
  991. float tmp0[3];
  992. bx::vec3Sub(tmp0, _from, _to);
  993. float normal[3];
  994. bx::vec3Norm(normal, tmp0);
  995. float mtx[2][16];
  996. bx::mtxFromNormal(mtx[0], normal, _radius, _from);
  997. memcpy(mtx[1], mtx[0], 64);
  998. mtx[1][12] = _to[0];
  999. mtx[1][13] = _to[1];
  1000. mtx[1][14] = _to[2];
  1001. uint8_t lod = attrib.m_lod > Mesh::CylinderMaxLod
  1002. ? uint8_t(Mesh::CylinderMaxLod)
  1003. : attrib.m_lod
  1004. ;
  1005. draw(Mesh::Enum(Mesh::Cylinder0 + lod), mtx[0], 2, attrib.m_wireframe);
  1006. }
  1007. void drawCylinder(const void* _from, const void* _to, float _radius, float _weight = 0.0f)
  1008. {
  1009. drawCylinder( (const float*)_from, (const float*)_to, _radius, _weight);
  1010. }
  1011. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight)
  1012. {
  1013. push();
  1014. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1015. moveTo(_x, _y, _z);
  1016. lineTo(_x + _len, _y, _z);
  1017. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1018. moveTo(_x, _y, _z);
  1019. lineTo(_x, _y + _len, _z);
  1020. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1021. moveTo(_x, _y, _z);
  1022. lineTo(_x, _y, _z + _len);
  1023. pop();
  1024. }
  1025. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  1026. {
  1027. float udir[3];
  1028. float vdir[3];
  1029. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  1030. calcPlaneUv(plane, udir, vdir);
  1031. bx::vec3Mul(udir, udir, _step);
  1032. bx::vec3Mul(vdir, vdir, _step);
  1033. const uint32_t num = (_size/2)*2+1;
  1034. const float halfExtent = float(_size/2);
  1035. float umin[3];
  1036. bx::vec3Mul(umin, udir, -halfExtent);
  1037. float umax[3];
  1038. bx::vec3Mul(umax, udir, halfExtent);
  1039. float vmin[3];
  1040. bx::vec3Mul(vmin, vdir, -halfExtent);
  1041. float vmax[3];
  1042. bx::vec3Mul(vmax, vdir, halfExtent);
  1043. float tmp[3];
  1044. float xs[3];
  1045. float xe[3];
  1046. bx::vec3Add(tmp, umin, vmin);
  1047. bx::vec3Add(xs, _center, tmp);
  1048. bx::vec3Add(tmp, umax, vmin);
  1049. bx::vec3Add(xe, _center, tmp);
  1050. float ys[3];
  1051. float ye[3];
  1052. bx::vec3Add(tmp, umin, vmin);
  1053. bx::vec3Add(ys, _center, tmp);
  1054. bx::vec3Add(tmp, umin, vmax);
  1055. bx::vec3Add(ye, _center, tmp);
  1056. for (uint32_t ii = 0; ii < num; ++ii)
  1057. {
  1058. moveTo(xs);
  1059. lineTo(xe);
  1060. bx::vec3Add(xs, xs, vdir);
  1061. bx::vec3Add(xe, xe, vdir);
  1062. moveTo(ys);
  1063. lineTo(ye);
  1064. bx::vec3Add(ys, ys, udir);
  1065. bx::vec3Add(ye, ye, udir);
  1066. }
  1067. }
  1068. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1069. {
  1070. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  1071. }
  1072. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  1073. {
  1074. push();
  1075. setTranslate(_center);
  1076. const uint32_t num = (_size/2)*2-1;
  1077. const float halfExtent = float(_size/2) * _step;
  1078. setColor(0xff606060);
  1079. float yy = -halfExtent + _step;
  1080. for (uint32_t ii = 0; ii < num; ++ii)
  1081. {
  1082. moveTo(_axis, -halfExtent, yy);
  1083. lineTo(_axis, halfExtent, yy);
  1084. moveTo(_axis, yy, -halfExtent);
  1085. lineTo(_axis, yy, halfExtent);
  1086. yy += _step;
  1087. }
  1088. setColor(0xff101010);
  1089. moveTo(_axis, -halfExtent, -halfExtent);
  1090. lineTo(_axis, -halfExtent, halfExtent);
  1091. lineTo(_axis, halfExtent, halfExtent);
  1092. lineTo(_axis, halfExtent, -halfExtent);
  1093. close();
  1094. moveTo(_axis, -halfExtent, 0.0f);
  1095. lineTo(_axis, halfExtent, 0.0f);
  1096. moveTo(_axis, 0.0f, -halfExtent);
  1097. lineTo(_axis, 0.0f, halfExtent);
  1098. pop();
  1099. }
  1100. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1101. {
  1102. drawGrid(_axis, (const float*)_center, _size, _step);
  1103. }
  1104. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1105. {
  1106. push();
  1107. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  1108. drawCircle(Axis::X, _x, _y, _z, _radius);
  1109. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  1110. drawCircle(Axis::Y, _x, _y, _z, _radius);
  1111. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  1112. drawCircle(Axis::Z, _x, _y, _z, _radius);
  1113. pop();
  1114. }
  1115. private:
  1116. struct Mesh
  1117. {
  1118. enum Enum
  1119. {
  1120. Sphere0,
  1121. Sphere1,
  1122. Sphere2,
  1123. Sphere3,
  1124. Cone0,
  1125. Cone1,
  1126. Cone2,
  1127. Cone3,
  1128. Cylinder0,
  1129. Cylinder1,
  1130. Cylinder2,
  1131. Cylinder3,
  1132. Capsule0,
  1133. Capsule1,
  1134. Capsule2,
  1135. Capsule3,
  1136. Cube,
  1137. Count,
  1138. SphereMaxLod = Sphere3 - Sphere0,
  1139. ConeMaxLod = Cone3 - Cone0,
  1140. CylinderMaxLod = Cylinder3 - Cylinder0,
  1141. CapsuleMaxLod = Capsule3 - Capsule0,
  1142. };
  1143. uint32_t m_startVertex;
  1144. uint32_t m_numVertices;
  1145. uint32_t m_startIndex[2];
  1146. uint32_t m_numIndices[2];
  1147. };
  1148. struct Program
  1149. {
  1150. enum Enum
  1151. {
  1152. Lines,
  1153. LinesStipple,
  1154. Fill,
  1155. FillLit,
  1156. Count
  1157. };
  1158. };
  1159. void draw(Mesh::Enum _mesh, const float* _mtx, uint16_t _num, bool _wireframe) const
  1160. {
  1161. const Mesh& mesh = m_mesh[_mesh];
  1162. const Attrib& attrib = m_attrib[m_stack];
  1163. if (0 != mesh.m_numIndices[_wireframe])
  1164. {
  1165. bgfx::setIndexBuffer(m_ibh
  1166. , mesh.m_startIndex[_wireframe]
  1167. , mesh.m_numIndices[_wireframe]
  1168. );
  1169. }
  1170. float params[4][4] =
  1171. {
  1172. {
  1173. 0.0f,
  1174. -1.0f,
  1175. 0.0f,
  1176. 3.0f,
  1177. },
  1178. {
  1179. 1.0f,
  1180. 0.9f,
  1181. 0.8f,
  1182. 0.0f,
  1183. },
  1184. {
  1185. 0.2f,
  1186. 0.22f,
  1187. 0.5f,
  1188. 0.0f,
  1189. },
  1190. {
  1191. ( (attrib.m_abgr>>24) )/255.0f,
  1192. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  1193. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  1194. ( (attrib.m_abgr )&0xff)/255.0f,
  1195. },
  1196. };
  1197. bx::vec3Norm(params[0], params[0]);
  1198. bgfx::setUniform(u_params, params, 4);
  1199. bgfx::setTransform(_mtx, _num);
  1200. bgfx::setVertexBuffer(m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1201. bgfx::setState(0
  1202. | attrib.m_state
  1203. | (_wireframe ? BGFX_STATE_PT_LINES|BGFX_STATE_LINEAA|BGFX_STATE_BLEND_ALPHA : 0)
  1204. );
  1205. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1206. }
  1207. void softFlush()
  1208. {
  1209. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1210. {
  1211. flush();
  1212. }
  1213. }
  1214. void flush()
  1215. {
  1216. if (0 != m_pos)
  1217. {
  1218. if (bgfx::checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  1219. {
  1220. bgfx::TransientVertexBuffer tvb;
  1221. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  1222. memcpy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  1223. bgfx::TransientIndexBuffer tib;
  1224. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1225. memcpy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1226. const Attrib& attrib = m_attrib[m_stack];
  1227. bgfx::setVertexBuffer(&tvb);
  1228. bgfx::setIndexBuffer(&tib);
  1229. bgfx::setState(0
  1230. | BGFX_STATE_RGB_WRITE
  1231. | BGFX_STATE_PT_LINES
  1232. | attrib.m_state
  1233. | BGFX_STATE_LINEAA
  1234. | BGFX_STATE_BLEND_ALPHA
  1235. );
  1236. bgfx::setTransform(m_mtx);
  1237. bgfx::ProgramHandle program = m_program[attrib.m_stipple ? 1 : 0];
  1238. bgfx::submit(m_viewId, program);
  1239. }
  1240. m_state = State::None;
  1241. m_pos = 0;
  1242. m_indexPos = 0;
  1243. m_vertexPos = 0;
  1244. }
  1245. }
  1246. struct State
  1247. {
  1248. enum Enum
  1249. {
  1250. None,
  1251. MoveTo,
  1252. LineTo,
  1253. Count
  1254. };
  1255. };
  1256. static const uint32_t cacheSize = 1024;
  1257. static const uint32_t stackSize = 16;
  1258. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1259. DebugVertex m_cache[cacheSize+1];
  1260. uint32_t m_mtx;
  1261. uint16_t m_indices[cacheSize*2];
  1262. uint16_t m_pos;
  1263. uint16_t m_indexPos;
  1264. uint16_t m_vertexPos;
  1265. uint8_t m_viewId;
  1266. uint8_t m_stack;
  1267. bool m_depthTestLess;
  1268. struct Attrib
  1269. {
  1270. uint64_t m_state;
  1271. float m_offset;
  1272. float m_scale;
  1273. uint32_t m_abgr;
  1274. bool m_stipple;
  1275. bool m_wireframe;
  1276. uint8_t m_lod;
  1277. };
  1278. Attrib m_attrib[stackSize];
  1279. State::Enum m_state;
  1280. Mesh m_mesh[Mesh::Count];
  1281. bgfx::ProgramHandle m_program[Program::Count];
  1282. bgfx::UniformHandle u_params;
  1283. bgfx::VertexBufferHandle m_vbh;
  1284. bgfx::IndexBufferHandle m_ibh;
  1285. bx::AllocatorI* m_allocator;
  1286. };
  1287. static DebugDraw s_dd;
  1288. void ddInit(bool _depthTestLess, bx::AllocatorI* _allocator)
  1289. {
  1290. s_dd.init(_depthTestLess, _allocator);
  1291. }
  1292. void ddShutdown()
  1293. {
  1294. s_dd.shutdown();
  1295. }
  1296. void ddBegin(uint8_t _viewId)
  1297. {
  1298. s_dd.begin(_viewId);
  1299. }
  1300. void ddEnd()
  1301. {
  1302. s_dd.end();
  1303. }
  1304. void ddPush()
  1305. {
  1306. s_dd.push();
  1307. }
  1308. void ddPop()
  1309. {
  1310. s_dd.pop();
  1311. }
  1312. void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1313. {
  1314. s_dd.setState(_depthTest, _depthWrite, _clockwise);
  1315. }
  1316. void ddSetColor(uint32_t _abgr)
  1317. {
  1318. s_dd.setColor(_abgr);
  1319. }
  1320. void ddSetLod(uint8_t _lod)
  1321. {
  1322. s_dd.setLod(_lod);
  1323. }
  1324. void ddSetWireframe(bool _wireframe)
  1325. {
  1326. s_dd.setWireframe(_wireframe);
  1327. }
  1328. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1329. {
  1330. s_dd.setStipple(_stipple, _scale, _offset);
  1331. }
  1332. void ddSetTransform(const void* _mtx)
  1333. {
  1334. s_dd.setTransform(_mtx);
  1335. }
  1336. void ddSetTranslate(float _x, float _y, float _z)
  1337. {
  1338. s_dd.setTranslate(_x, _y, _z);
  1339. }
  1340. void ddMoveTo(float _x, float _y, float _z)
  1341. {
  1342. s_dd.moveTo(_x, _y, _z);
  1343. }
  1344. void ddMoveTo(const void* _pos)
  1345. {
  1346. s_dd.moveTo(_pos);
  1347. }
  1348. void ddLineTo(float _x, float _y, float _z)
  1349. {
  1350. s_dd.lineTo(_x, _y, _z);
  1351. }
  1352. void ddLineTo(const void* _pos)
  1353. {
  1354. s_dd.lineTo(_pos);
  1355. }
  1356. void ddClose()
  1357. {
  1358. s_dd.close();
  1359. }
  1360. void ddDraw(const Aabb& _aabb)
  1361. {
  1362. s_dd.draw(_aabb);
  1363. }
  1364. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1365. {
  1366. s_dd.draw(_cylinder, _capsule);
  1367. }
  1368. void ddDraw(const Disk& _disk)
  1369. {
  1370. s_dd.draw(_disk);
  1371. }
  1372. void ddDraw(const Obb& _obb)
  1373. {
  1374. s_dd.draw(_obb);
  1375. }
  1376. void ddDraw(const Sphere& _sphere)
  1377. {
  1378. s_dd.draw(_sphere);
  1379. }
  1380. void ddDrawFrustum(const void* _viewProj)
  1381. {
  1382. s_dd.drawFrustum(_viewProj);
  1383. }
  1384. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1385. {
  1386. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1387. }
  1388. void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1389. {
  1390. s_dd.drawCircle(_normal, _center, _radius, _weight);
  1391. }
  1392. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1393. {
  1394. s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight);
  1395. }
  1396. void ddDrawCone(const void* _from, const void* _to, float _radius, float _weight)
  1397. {
  1398. s_dd.drawCone(_from, _to, _radius, _weight);
  1399. }
  1400. void ddDrawCylinder(const void* _from, const void* _to, float _radius, float _weight)
  1401. {
  1402. s_dd.drawCylinder(_from, _to, _radius, _weight);
  1403. }
  1404. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight)
  1405. {
  1406. s_dd.drawAxis(_x, _y, _z, _len, _hightlight);
  1407. }
  1408. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1409. {
  1410. s_dd.drawGrid(_normal, _center, _size, _step);
  1411. }
  1412. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1413. {
  1414. s_dd.drawGrid(_axis, _center, _size, _step);
  1415. }
  1416. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1417. {
  1418. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1419. }