BsDrawHelper.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Utility/BsDrawHelper.h"
  4. #include "Mesh/BsMesh.h"
  5. #include "Math/BsAABox.h"
  6. #include "Math/BsSphere.h"
  7. #include "RenderAPI/BsVertexDataDesc.h"
  8. #include "Mesh/BsMeshHeap.h"
  9. #include "Utility/BsShapeMeshes3D.h"
  10. #include "Text/BsTextData.h"
  11. #include "Math/BsVector2.h"
  12. #include "Math/BsQuaternion.h"
  13. namespace bs
  14. {
  15. const UINT32 DrawHelper::VERTEX_BUFFER_GROWTH = 4096;
  16. const UINT32 DrawHelper::INDEX_BUFFER_GROWTH = 4096 * 2;
  17. DrawHelper::DrawHelper()
  18. :mLayer(1), mNumActiveMeshes(0)
  19. {
  20. mTransform = Matrix4::IDENTITY;
  21. mSolidVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  22. mSolidVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  23. mSolidVertexDesc->addVertElem(VET_FLOAT3, VES_NORMAL);
  24. mSolidVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  25. mWireVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  26. mWireVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  27. mWireVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  28. mLineVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  29. mLineVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  30. mLineVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  31. mTextVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
  32. mTextVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  33. mTextVertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
  34. mTextVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  35. mSolidMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mSolidVertexDesc);
  36. mWireMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mWireVertexDesc);
  37. mLineMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mLineVertexDesc);
  38. mTextMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mTextVertexDesc);
  39. }
  40. DrawHelper::~DrawHelper()
  41. {
  42. BS_ASSERT(mNumActiveMeshes == 0 && "Not all DrawHelper meshes were freed on shutdown.");
  43. }
  44. void DrawHelper::setColor(const Color& color)
  45. {
  46. mColor = color;
  47. }
  48. void DrawHelper::setTransform(const Matrix4& transform)
  49. {
  50. mTransform = transform;
  51. }
  52. void DrawHelper::setLayer(UINT64 layer)
  53. {
  54. mLayer = layer;
  55. }
  56. void DrawHelper::cube(const Vector3& position, const Vector3& extents)
  57. {
  58. mSolidCubeData.push_back(CubeData());
  59. CubeData& cubeData = mSolidCubeData.back();
  60. cubeData.position = position;
  61. cubeData.extents = extents;
  62. cubeData.color = mColor;
  63. cubeData.transform = mTransform;
  64. cubeData.layer = mLayer;
  65. cubeData.center = mTransform.multiplyAffine(position);
  66. }
  67. void DrawHelper::sphere(const Vector3& position, float radius, UINT32 quality)
  68. {
  69. mSolidSphereData.push_back(SphereData());
  70. SphereData& sphereData = mSolidSphereData.back();
  71. sphereData.position = position;
  72. sphereData.radius = radius;
  73. sphereData.quality = quality;
  74. sphereData.color = mColor;
  75. sphereData.transform = mTransform;
  76. sphereData.layer = mLayer;
  77. sphereData.center = mTransform.multiplyAffine(position);
  78. }
  79. void DrawHelper::wireCube(const Vector3& position, const Vector3& extents)
  80. {
  81. mWireCubeData.push_back(CubeData());
  82. CubeData& cubeData = mWireCubeData.back();
  83. cubeData.position = position;
  84. cubeData.extents = extents;
  85. cubeData.color = mColor;
  86. cubeData.transform = mTransform;
  87. cubeData.layer = mLayer;
  88. cubeData.center = mTransform.multiplyAffine(position);
  89. }
  90. void DrawHelper::wireSphere(const Vector3& position, float radius, UINT32 quality)
  91. {
  92. mWireSphereData.push_back(SphereData());
  93. SphereData& sphereData = mWireSphereData.back();
  94. sphereData.position = position;
  95. sphereData.radius = radius;
  96. sphereData.quality = quality;
  97. sphereData.color = mColor;
  98. sphereData.transform = mTransform;
  99. sphereData.layer = mLayer;
  100. sphereData.center = mTransform.multiplyAffine(position);
  101. }
  102. void DrawHelper::line(const Vector3& start, const Vector3& end)
  103. {
  104. mLineData.push_back(LineData());
  105. LineData& lineData = mLineData.back();
  106. lineData.start = start;
  107. lineData.end = end;
  108. lineData.color = mColor;
  109. lineData.transform = mTransform;
  110. lineData.layer = mLayer;
  111. lineData.center = mTransform.multiplyAffine((start + end) * 0.5f);
  112. }
  113. void DrawHelper::lineList(const Vector<Vector3>& lines)
  114. {
  115. if (lines.size() < 2)
  116. return;
  117. mLineListData.push_back(LineListData());
  118. LineListData& lineListData = mLineListData.back();
  119. Vector3 center(BsZero);
  120. for (auto& point : lines)
  121. center += point;
  122. lineListData.lines = lines;
  123. lineListData.color = mColor;
  124. lineListData.transform = mTransform;
  125. lineListData.layer = mLayer;
  126. lineListData.center = center / (float)lines.size();;
  127. }
  128. void DrawHelper::frustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
  129. {
  130. mFrustumData.push_back(FrustumData());
  131. FrustumData& frustumData = mFrustumData.back();
  132. frustumData.position = position;
  133. frustumData.aspect = aspect;
  134. frustumData.FOV = FOV;
  135. frustumData.nearDist = near;
  136. frustumData.farDist = far;
  137. frustumData.color = mColor;
  138. frustumData.transform = mTransform;
  139. frustumData.layer = mLayer;
  140. frustumData.center = mTransform.multiplyAffine(position);
  141. }
  142. void DrawHelper::cone(const Vector3& base, const Vector3& normal, float height, float radius, const Vector2& scale,
  143. UINT32 quality)
  144. {
  145. mConeData.push_back(ConeData());
  146. ConeData& coneData = mConeData.back();
  147. coneData.base = base;
  148. coneData.normal = normal;
  149. coneData.height = height;
  150. coneData.radius = radius;
  151. coneData.scale = scale;
  152. coneData.quality = quality;
  153. coneData.color = mColor;
  154. coneData.transform = mTransform;
  155. coneData.layer = mLayer;
  156. coneData.center = mTransform.multiplyAffine(base + normal * height * 0.5f);
  157. }
  158. void DrawHelper::wireCone(const Vector3& base, const Vector3& normal, float height, float radius, const Vector2& scale,
  159. UINT32 quality)
  160. {
  161. mWireConeData.push_back(ConeData());
  162. ConeData& coneData = mWireConeData.back();
  163. coneData.base = base;
  164. coneData.normal = normal;
  165. coneData.height = height;
  166. coneData.radius = radius;
  167. coneData.scale = scale;
  168. coneData.quality = quality;
  169. coneData.color = mColor;
  170. coneData.transform = mTransform;
  171. coneData.layer = mLayer;
  172. coneData.center = mTransform.multiplyAffine(base + normal * height * 0.5f);
  173. }
  174. void DrawHelper::disc(const Vector3& position, const Vector3& normal, float radius, UINT32 quality)
  175. {
  176. mDiscData.push_back(DiscData());
  177. DiscData& discData = mDiscData.back();
  178. discData.position = position;
  179. discData.normal = normal;
  180. discData.radius = radius;
  181. discData.quality = quality;
  182. discData.color = mColor;
  183. discData.transform = mTransform;
  184. discData.layer = mLayer;
  185. discData.center = mTransform.multiplyAffine(position);
  186. }
  187. void DrawHelper::wireDisc(const Vector3& position, const Vector3& normal, float radius, UINT32 quality)
  188. {
  189. mWireDiscData.push_back(DiscData());
  190. DiscData& discData = mWireDiscData.back();
  191. discData.position = position;
  192. discData.normal = normal;
  193. discData.radius = radius;
  194. discData.quality = quality;
  195. discData.color = mColor;
  196. discData.transform = mTransform;
  197. discData.layer = mLayer;
  198. discData.center = mTransform.multiplyAffine(position);
  199. }
  200. void DrawHelper::arc(const Vector3& position, const Vector3& normal, float radius,
  201. Degree startAngle, Degree amountAngle, UINT32 quality)
  202. {
  203. mArcData.push_back(ArcData());
  204. ArcData& arcData = mArcData.back();
  205. arcData.position = position;
  206. arcData.normal = normal;
  207. arcData.radius = radius;
  208. arcData.startAngle = startAngle;
  209. arcData.amountAngle = amountAngle;
  210. arcData.quality = quality;
  211. arcData.color = mColor;
  212. arcData.transform = mTransform;
  213. arcData.layer = mLayer;
  214. arcData.center = mTransform.multiplyAffine(position);
  215. }
  216. void DrawHelper::wireArc(const Vector3& position, const Vector3& normal, float radius,
  217. Degree startAngle, Degree amountAngle, UINT32 quality)
  218. {
  219. mWireArcData.push_back(ArcData());
  220. ArcData& arcData = mWireArcData.back();
  221. arcData.position = position;
  222. arcData.normal = normal;
  223. arcData.radius = radius;
  224. arcData.startAngle = startAngle;
  225. arcData.amountAngle = amountAngle;
  226. arcData.quality = quality;
  227. arcData.color = mColor;
  228. arcData.transform = mTransform;
  229. arcData.layer = mLayer;
  230. arcData.center = mTransform.multiplyAffine(position);
  231. }
  232. void DrawHelper::rectangle(const Rect3& area)
  233. {
  234. mRect3Data.push_back(Rect3Data());
  235. Rect3Data& rectData = mRect3Data.back();
  236. rectData.area = area;
  237. rectData.color = mColor;
  238. rectData.transform = mTransform;
  239. rectData.layer = mLayer;
  240. rectData.center = mTransform.multiplyAffine(area.getCenter());
  241. }
  242. void DrawHelper::text(const Vector3& position, const WString& text, const HFont& font, UINT32 size)
  243. {
  244. if (!font.isLoaded() || text.empty())
  245. return;
  246. mText2DData.push_back(Text2DData());
  247. Text2DData& textData = mText2DData.back();
  248. textData.position = position;
  249. textData.color = mColor;
  250. textData.transform = mTransform;
  251. textData.layer = mLayer;
  252. textData.center = mTransform.multiplyAffine(position);
  253. textData.text = text;
  254. textData.font = font;
  255. textData.size = size;
  256. }
  257. void DrawHelper::wireMesh(const SPtr<MeshData>& meshData)
  258. {
  259. if (meshData == nullptr)
  260. return;
  261. mWireMeshData.push_back(WireMeshData());
  262. WireMeshData& wireMeshData = mWireMeshData.back();
  263. wireMeshData.meshData = meshData;
  264. wireMeshData.color = mColor;
  265. wireMeshData.transform = mTransform;
  266. wireMeshData.layer = mLayer;
  267. wireMeshData.center = mTransform.multiplyAffine(Vector3::ZERO);
  268. }
  269. void DrawHelper::clear()
  270. {
  271. mSolidCubeData.clear();
  272. mWireCubeData.clear();
  273. mSolidSphereData.clear();
  274. mWireSphereData.clear();
  275. mLineData.clear();
  276. mLineListData.clear();
  277. mRect3Data.clear();
  278. mFrustumData.clear();
  279. mFrustumData.clear();
  280. mDiscData.clear();
  281. mWireDiscData.clear();
  282. mArcData.clear();
  283. mWireArcData.clear();
  284. mConeData.clear();
  285. mWireConeData.clear();
  286. mText2DData.clear();
  287. mWireMeshData.clear();
  288. }
  289. void DrawHelper::buildMeshes(SortType sorting, const Vector3& reference, UINT64 layers)
  290. {
  291. mMeshes.clear();
  292. enum class ShapeType
  293. {
  294. Cube, Sphere, WireCube, WireSphere, WireCone, Line, LineList, Frustum,
  295. Cone, Disc, WireDisc, Arc, WireArc, Rectangle, Text, WireMesh
  296. };
  297. struct RawData
  298. {
  299. ShapeType shapeType;
  300. MeshType meshType;
  301. UINT32 idx;
  302. UINT32 textIdx;
  303. float distance;
  304. UINT32 numVertices;
  305. UINT32 numIndices;
  306. };
  307. /************************************************************************/
  308. /* Sort everything according to specified sorting rule */
  309. /************************************************************************/
  310. UINT32 idx = 0;
  311. Vector<RawData> allShapes;
  312. UINT32 localIdx = 0;
  313. for (auto& shapeData : mSolidCubeData)
  314. {
  315. if ((shapeData.layer & layers) == 0)
  316. {
  317. localIdx++;
  318. continue;
  319. }
  320. allShapes.push_back(RawData());
  321. RawData& rawData = allShapes.back();
  322. rawData.idx = localIdx++;
  323. rawData.textIdx = 0;
  324. rawData.meshType = MeshType::Solid;
  325. rawData.shapeType = ShapeType::Cube;
  326. rawData.distance = shapeData.center.distance(reference);
  327. ShapeMeshes3D::getNumElementsAABox(rawData.numVertices, rawData.numIndices);
  328. }
  329. localIdx = 0;
  330. for (auto& shapeData : mSolidSphereData)
  331. {
  332. if ((shapeData.layer & layers) == 0)
  333. {
  334. localIdx++;
  335. continue;
  336. }
  337. allShapes.push_back(RawData());
  338. RawData& rawData = allShapes.back();
  339. rawData.idx = localIdx++;
  340. rawData.textIdx = 0;
  341. rawData.meshType = MeshType::Solid;
  342. rawData.shapeType = ShapeType::Sphere;
  343. rawData.distance = shapeData.center.distance(reference);
  344. ShapeMeshes3D::getNumElementsSphere(shapeData.quality,
  345. rawData.numVertices, rawData.numIndices);
  346. }
  347. localIdx = 0;
  348. for (auto& shapeData : mConeData)
  349. {
  350. if ((shapeData.layer & layers) == 0)
  351. {
  352. localIdx++;
  353. continue;
  354. }
  355. allShapes.push_back(RawData());
  356. RawData& rawData = allShapes.back();
  357. rawData.idx = localIdx++;
  358. rawData.textIdx = 0;
  359. rawData.meshType = MeshType::Solid;
  360. rawData.shapeType = ShapeType::Cone;
  361. rawData.distance = shapeData.center.distance(reference);
  362. ShapeMeshes3D::getNumElementsCone(shapeData.quality,
  363. rawData.numVertices, rawData.numIndices);
  364. }
  365. localIdx = 0;
  366. for (auto& shapeData : mDiscData)
  367. {
  368. if ((shapeData.layer & layers) == 0)
  369. {
  370. localIdx++;
  371. continue;
  372. }
  373. allShapes.push_back(RawData());
  374. RawData& rawData = allShapes.back();
  375. rawData.idx = localIdx++;
  376. rawData.textIdx = 0;
  377. rawData.meshType = MeshType::Solid;
  378. rawData.shapeType = ShapeType::Disc;
  379. rawData.distance = shapeData.center.distance(reference);
  380. ShapeMeshes3D::getNumElementsDisc(shapeData.quality,
  381. rawData.numVertices, rawData.numIndices);
  382. }
  383. localIdx = 0;
  384. for (auto& shapeData : mArcData)
  385. {
  386. if ((shapeData.layer & layers) == 0)
  387. {
  388. localIdx++;
  389. continue;
  390. }
  391. allShapes.push_back(RawData());
  392. RawData& rawData = allShapes.back();
  393. rawData.idx = localIdx++;
  394. rawData.textIdx = 0;
  395. rawData.meshType = MeshType::Solid;
  396. rawData.shapeType = ShapeType::Arc;
  397. rawData.distance = shapeData.center.distance(reference);
  398. ShapeMeshes3D::getNumElementsArc(shapeData.quality,
  399. rawData.numVertices, rawData.numIndices);
  400. }
  401. localIdx = 0;
  402. for (auto& shapeData : mRect3Data)
  403. {
  404. if ((shapeData.layer & layers) == 0)
  405. {
  406. localIdx++;
  407. continue;
  408. }
  409. allShapes.push_back(RawData());
  410. RawData& rawData = allShapes.back();
  411. rawData.idx = localIdx++;
  412. rawData.textIdx = 0;
  413. rawData.meshType = MeshType::Solid;
  414. rawData.shapeType = ShapeType::Rectangle;
  415. rawData.distance = shapeData.center.distance(reference);
  416. ShapeMeshes3D::getNumElementsQuad(rawData.numVertices, rawData.numIndices);
  417. }
  418. localIdx = 0;
  419. for (auto& shapeData : mWireCubeData)
  420. {
  421. if ((shapeData.layer & layers) == 0)
  422. {
  423. localIdx++;
  424. continue;
  425. }
  426. allShapes.push_back(RawData());
  427. RawData& rawData = allShapes.back();
  428. rawData.idx = localIdx++;
  429. rawData.textIdx = 0;
  430. rawData.meshType = MeshType::Line;
  431. rawData.shapeType = ShapeType::WireCube;
  432. rawData.distance = shapeData.center.distance(reference);
  433. ShapeMeshes3D::getNumElementsWireAABox(rawData.numVertices, rawData.numIndices);
  434. }
  435. localIdx = 0;
  436. for (auto& shapeData : mWireSphereData)
  437. {
  438. if ((shapeData.layer & layers) == 0)
  439. {
  440. localIdx++;
  441. continue;
  442. }
  443. allShapes.push_back(RawData());
  444. RawData& rawData = allShapes.back();
  445. rawData.idx = localIdx++;
  446. rawData.textIdx = 0;
  447. rawData.meshType = MeshType::Line;
  448. rawData.shapeType = ShapeType::WireSphere;
  449. rawData.distance = shapeData.center.distance(reference);
  450. ShapeMeshes3D::getNumElementsWireSphere(shapeData.quality,
  451. rawData.numVertices, rawData.numIndices);
  452. }
  453. localIdx = 0;
  454. for (auto& shapeData : mWireConeData)
  455. {
  456. if ((shapeData.layer & layers) == 0)
  457. {
  458. localIdx++;
  459. continue;
  460. }
  461. allShapes.push_back(RawData());
  462. RawData& rawData = allShapes.back();
  463. rawData.idx = localIdx++;
  464. rawData.textIdx = 0;
  465. rawData.meshType = MeshType::Line;
  466. rawData.shapeType = ShapeType::WireCone;
  467. rawData.distance = shapeData.center.distance(reference);
  468. ShapeMeshes3D::getNumElementsWireCone(shapeData.quality,
  469. rawData.numVertices, rawData.numIndices);
  470. }
  471. localIdx = 0;
  472. for (auto& shapeData : mLineData)
  473. {
  474. if ((shapeData.layer & layers) == 0)
  475. {
  476. localIdx++;
  477. continue;
  478. }
  479. allShapes.push_back(RawData());
  480. RawData& rawData = allShapes.back();
  481. rawData.idx = localIdx++;
  482. rawData.textIdx = 0;
  483. rawData.meshType = MeshType::Line;
  484. rawData.shapeType = ShapeType::Line;
  485. rawData.distance = shapeData.center.distance(reference);
  486. rawData.numVertices = 2;
  487. rawData.numIndices = 2;
  488. }
  489. localIdx = 0;
  490. for (auto& shapeData : mLineListData)
  491. {
  492. if ((shapeData.layer & layers) == 0)
  493. {
  494. localIdx++;
  495. continue;
  496. }
  497. allShapes.push_back(RawData());
  498. RawData& rawData = allShapes.back();
  499. UINT32 numLines = (UINT32)shapeData.lines.size() / 2;
  500. rawData.idx = localIdx++;
  501. rawData.textIdx = 0;
  502. rawData.meshType = MeshType::Line;
  503. rawData.shapeType = ShapeType::LineList;
  504. rawData.distance = shapeData.center.distance(reference);
  505. rawData.numVertices = numLines * 2;
  506. rawData.numIndices = numLines * 2;
  507. }
  508. localIdx = 0;
  509. for (auto& shapeData : mFrustumData)
  510. {
  511. if ((shapeData.layer & layers) == 0)
  512. {
  513. localIdx++;
  514. continue;
  515. }
  516. allShapes.push_back(RawData());
  517. RawData& rawData = allShapes.back();
  518. rawData.idx = localIdx++;
  519. rawData.textIdx = 0;
  520. rawData.meshType = MeshType::Line;
  521. rawData.shapeType = ShapeType::Frustum;
  522. rawData.distance = shapeData.center.distance(reference);
  523. ShapeMeshes3D::getNumElementsFrustum(rawData.numVertices, rawData.numIndices);
  524. }
  525. localIdx = 0;
  526. for (auto& shapeData : mWireDiscData)
  527. {
  528. if ((shapeData.layer & layers) == 0)
  529. {
  530. localIdx++;
  531. continue;
  532. }
  533. allShapes.push_back(RawData());
  534. RawData& rawData = allShapes.back();
  535. rawData.idx = localIdx++;
  536. rawData.textIdx = 0;
  537. rawData.meshType = MeshType::Line;
  538. rawData.shapeType = ShapeType::WireDisc;
  539. rawData.distance = shapeData.center.distance(reference);
  540. ShapeMeshes3D::getNumElementsWireDisc(shapeData.quality,
  541. rawData.numVertices, rawData.numIndices);
  542. }
  543. localIdx = 0;
  544. for (auto& shapeData : mWireArcData)
  545. {
  546. if ((shapeData.layer & layers) == 0)
  547. {
  548. localIdx++;
  549. continue;
  550. }
  551. allShapes.push_back(RawData());
  552. RawData& rawData = allShapes.back();
  553. rawData.idx = localIdx++;
  554. rawData.textIdx = 0;
  555. rawData.meshType = MeshType::Line;
  556. rawData.shapeType = ShapeType::WireArc;
  557. rawData.distance = shapeData.center.distance(reference);
  558. ShapeMeshes3D::getNumElementsWireArc(shapeData.quality,
  559. rawData.numVertices, rawData.numIndices);
  560. }
  561. localIdx = 0;
  562. for (auto& shapeData : mWireMeshData)
  563. {
  564. if ((shapeData.layer & layers) == 0)
  565. {
  566. localIdx++;
  567. continue;
  568. }
  569. allShapes.push_back(RawData());
  570. RawData& rawData = allShapes.back();
  571. rawData.idx = localIdx++;
  572. rawData.textIdx = 0;
  573. rawData.meshType = MeshType::Wire;
  574. rawData.shapeType = ShapeType::WireMesh;
  575. rawData.distance = shapeData.center.distance(reference);
  576. rawData.numVertices = shapeData.meshData->getNumVertices();
  577. rawData.numIndices = shapeData.meshData->getNumIndices();
  578. }
  579. struct TextRenderData
  580. {
  581. UINT32 page;
  582. SPtr<TextData<>> textData;
  583. };
  584. UnorderedMap<UINT32, TextRenderData> textRenderData;
  585. UINT32 textIdx = 0;
  586. localIdx = 0;
  587. for (auto& shapeData : mText2DData)
  588. {
  589. if ((shapeData.layer & layers) == 0)
  590. {
  591. localIdx++;
  592. continue;
  593. }
  594. SPtr<TextData<>> textData = bs_shared_ptr_new<TextData<>>(shapeData.text, shapeData.font, shapeData.size);
  595. UINT32 numPages = textData->getNumPages();
  596. for (UINT32 j = 0; j < numPages; j++)
  597. {
  598. UINT32 numQuads = textData->getNumQuadsForPage(j);
  599. allShapes.push_back(RawData());
  600. RawData& rawData = allShapes.back();
  601. rawData.idx = localIdx;
  602. rawData.textIdx = textIdx;
  603. rawData.meshType = MeshType::Text;
  604. rawData.shapeType = ShapeType::Text;
  605. rawData.distance = shapeData.center.distance(reference);
  606. rawData.numVertices = numQuads * 4;
  607. rawData.numIndices = numQuads * 6;
  608. TextRenderData& renderData = textRenderData[textIdx];
  609. renderData.page = j;
  610. renderData.textData = textData;
  611. textIdx++;
  612. idx++;
  613. }
  614. localIdx++;
  615. }
  616. if (sorting == SortType::FrontToBack)
  617. {
  618. std::sort(begin(allShapes), end(allShapes),
  619. [&](const RawData& x, const RawData& y)
  620. {
  621. return x.distance < y.distance;
  622. });
  623. }
  624. else if (sorting == SortType::BackToFront)
  625. {
  626. std::sort(begin(allShapes), end(allShapes),
  627. [&](const RawData& x, const RawData& y)
  628. {
  629. return y.distance < x.distance;
  630. });
  631. }
  632. /************************************************************************/
  633. /* Create batches */
  634. /************************************************************************/
  635. struct Batch
  636. {
  637. MeshType type;
  638. HTexture texture;
  639. UINT32 startIdx;
  640. UINT32 endIdx;
  641. UINT32 numVertices;
  642. UINT32 numIndices;
  643. };
  644. UINT32 numShapes = (UINT32)allShapes.size();
  645. Vector<Batch> batches;
  646. if (numShapes > 0)
  647. {
  648. batches.push_back(Batch());
  649. {
  650. Batch& currentBatch = batches.back();
  651. currentBatch.startIdx = 0;
  652. currentBatch.type = allShapes[0].meshType;
  653. currentBatch.numVertices = allShapes[0].numVertices;
  654. currentBatch.numIndices = allShapes[0].numIndices;
  655. if (allShapes[0].meshType == MeshType::Text)
  656. {
  657. TextRenderData& renderData = textRenderData[allShapes[0].textIdx];
  658. currentBatch.texture = renderData.textData->getTextureForPage(renderData.page);
  659. }
  660. }
  661. for (UINT32 i = 1; i < numShapes; i++)
  662. {
  663. Batch& currentBatch = batches.back();
  664. HTexture texture;
  665. if (allShapes[i].meshType == MeshType::Text)
  666. {
  667. TextRenderData& renderData = textRenderData[allShapes[i].textIdx];
  668. texture = renderData.textData->getTextureForPage(renderData.page);
  669. }
  670. bool startNewBatch = allShapes[i].meshType != currentBatch.type || texture != currentBatch.texture;
  671. if (startNewBatch)
  672. {
  673. currentBatch.endIdx = i - 1;
  674. batches.push_back(Batch());
  675. Batch& newBatch = batches.back();
  676. newBatch.startIdx = i;
  677. newBatch.type = allShapes[i].meshType;
  678. newBatch.numVertices = allShapes[i].numVertices;
  679. newBatch.numIndices = allShapes[i].numIndices;
  680. newBatch.texture = texture;
  681. }
  682. else
  683. {
  684. currentBatch.endIdx = i;
  685. currentBatch.numVertices += allShapes[i].numVertices;
  686. currentBatch.numIndices += allShapes[i].numIndices;
  687. }
  688. }
  689. {
  690. Batch& currentBatch = batches.back();
  691. currentBatch.endIdx = numShapes - 1;
  692. }
  693. }
  694. /************************************************************************/
  695. /* Generate geometry for each batch */
  696. /************************************************************************/
  697. for (auto& batch : batches)
  698. {
  699. if (batch.type == MeshType::Solid)
  700. {
  701. SPtr<MeshData> meshData = bs_shared_ptr_new<MeshData>(batch.numVertices, batch.numIndices, mSolidVertexDesc);
  702. UINT32 curVertexOffset = 0;
  703. UINT32 curIndexOffet = 0;
  704. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  705. auto normalIter = meshData->getVec3DataIter(VES_NORMAL);
  706. auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
  707. for (UINT32 i = batch.startIdx; i <= batch.endIdx; i++)
  708. {
  709. RawData& shapeData = allShapes[i];
  710. Matrix4* transform = nullptr;
  711. RGBA color = 0;
  712. switch (shapeData.shapeType)
  713. {
  714. case ShapeType::Cube:
  715. {
  716. CubeData& cubeData = mSolidCubeData[shapeData.idx];
  717. AABox box(cubeData.position - cubeData.extents, cubeData.position + cubeData.extents);
  718. ShapeMeshes3D::solidAABox(box, meshData, curVertexOffset, curIndexOffet);
  719. transform = &cubeData.transform;
  720. color = cubeData.color.getAsRGBA();
  721. }
  722. break;
  723. case ShapeType::Sphere:
  724. {
  725. SphereData& sphereData = mSolidSphereData[shapeData.idx];
  726. Sphere sphere(sphereData.position, sphereData.radius);
  727. ShapeMeshes3D::solidSphere(sphere, meshData, curVertexOffset, curIndexOffet, sphereData.quality);
  728. transform = &sphereData.transform;
  729. color = sphereData.color.getAsRGBA();
  730. }
  731. break;
  732. case ShapeType::Cone:
  733. {
  734. ConeData& coneData = mConeData[shapeData.idx];
  735. ShapeMeshes3D::solidCone(coneData.base, coneData.normal, coneData.height, coneData.radius,
  736. coneData.scale, meshData, curVertexOffset, curIndexOffet, coneData.quality);
  737. transform = &coneData.transform;
  738. color = coneData.color.getAsRGBA();
  739. }
  740. break;
  741. case ShapeType::Disc:
  742. {
  743. DiscData& discData = mDiscData[shapeData.idx];
  744. ShapeMeshes3D::solidDisc(discData.position, discData.radius, discData.normal,
  745. meshData, curVertexOffset, curIndexOffet, discData.quality);
  746. transform = &discData.transform;
  747. color = discData.color.getAsRGBA();
  748. }
  749. break;
  750. case ShapeType::Arc:
  751. {
  752. ArcData& arcData = mArcData[shapeData.idx];
  753. ShapeMeshes3D::solidArc(arcData.position, arcData.radius, arcData.normal,
  754. arcData.startAngle, arcData.amountAngle, meshData, curVertexOffset, curIndexOffet, arcData.quality);
  755. transform = &arcData.transform;
  756. color = arcData.color.getAsRGBA();
  757. }
  758. break;
  759. case ShapeType::Rectangle:
  760. {
  761. Rect3Data& rectData = mRect3Data[shapeData.idx];
  762. ShapeMeshes3D::solidQuad(rectData.area, meshData, curVertexOffset, curIndexOffet);
  763. transform = &rectData.transform;
  764. color = rectData.color.getAsRGBA();
  765. }
  766. break;
  767. default:
  768. break;
  769. }
  770. Matrix4 transformIT = transform->inverseAffine().transpose();
  771. for (UINT32 i = 0; i < shapeData.numVertices; i++)
  772. {
  773. Vector3 worldPos = transform->multiplyAffine(positionIter.getValue());
  774. Vector3 worldNormal = transformIT.multiplyAffine(normalIter.getValue());
  775. positionIter.addValue(worldPos);
  776. normalIter.addValue(worldNormal);
  777. colorIter.addValue(color);
  778. }
  779. curVertexOffset += shapeData.numVertices;
  780. curIndexOffet += shapeData.numIndices;
  781. }
  782. mMeshes.push_back(ShapeMeshData());
  783. ShapeMeshData& newMesh = mMeshes.back();
  784. newMesh.mesh = mSolidMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  785. newMesh.type = MeshType::Solid;
  786. }
  787. else if (batch.type == MeshType::Wire)
  788. {
  789. SPtr<MeshData> meshData = bs_shared_ptr_new<MeshData>(batch.numVertices, batch.numIndices, mWireVertexDesc);
  790. UINT32 curVertexOffset = 0;
  791. UINT32 curIndexOffset = 0;
  792. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  793. auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
  794. for (UINT32 i = batch.startIdx; i <= batch.endIdx; i++)
  795. {
  796. RawData& shapeData = allShapes[i];
  797. Matrix4* transform = nullptr;
  798. RGBA color = 0;
  799. switch (shapeData.shapeType)
  800. {
  801. case ShapeType::WireMesh:
  802. {
  803. WireMeshData& wireMeshData = mWireMeshData[shapeData.idx];
  804. transform = &wireMeshData.transform;
  805. color = wireMeshData.color.getAsRGBA();
  806. auto vertIterRead = wireMeshData.meshData->getVec3DataIter(VES_POSITION);
  807. for (UINT32 j = 0; j < vertIterRead.getNumElements(); j++)
  808. {
  809. Vector3 worldPos = transform->multiplyAffine(vertIterRead.getValue());
  810. positionIter.addValue(worldPos);
  811. colorIter.addValue(color);
  812. vertIterRead.moveNext();
  813. }
  814. UINT32* srcIndexData = wireMeshData.meshData->getIndices32();
  815. UINT32* destIndexData = meshData->getIndices32() + curIndexOffset;
  816. for(UINT32 j = 0; j < shapeData.numIndices; j++)
  817. destIndexData[j] = srcIndexData[j] + curVertexOffset;
  818. curVertexOffset += shapeData.numVertices;
  819. curIndexOffset += shapeData.numIndices;
  820. }
  821. break;
  822. default:
  823. break;
  824. }
  825. }
  826. mMeshes.push_back(ShapeMeshData());
  827. ShapeMeshData& newMesh = mMeshes.back();
  828. newMesh.mesh = mWireMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  829. newMesh.type = MeshType::Wire;
  830. }
  831. else if(batch.type == MeshType::Line)
  832. {
  833. SPtr<MeshData> meshData = bs_shared_ptr_new<MeshData>(batch.numVertices,
  834. batch.numIndices, mLineVertexDesc);
  835. UINT32 curVertexOffset = 0;
  836. UINT32 curIndexOffet = 0;
  837. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  838. auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
  839. for (UINT32 i = batch.startIdx; i <= batch.endIdx; i++)
  840. {
  841. RawData& shapeData = allShapes[i];
  842. Matrix4* transform = nullptr;
  843. RGBA color = 0;
  844. switch (shapeData.shapeType)
  845. {
  846. case ShapeType::WireCube:
  847. {
  848. CubeData& cubeData = mWireCubeData[shapeData.idx];
  849. AABox box(cubeData.position - cubeData.extents, cubeData.position + cubeData.extents);
  850. ShapeMeshes3D::wireAABox(box, meshData, curVertexOffset, curIndexOffet);
  851. transform = &cubeData.transform;
  852. color = cubeData.color.getAsRGBA();
  853. }
  854. break;
  855. case ShapeType::WireSphere:
  856. {
  857. SphereData& sphereData = mWireSphereData[shapeData.idx];
  858. Sphere sphere(sphereData.position, sphereData.radius);
  859. ShapeMeshes3D::wireSphere(sphere, meshData, curVertexOffset, curIndexOffet, sphereData.quality);
  860. transform = &sphereData.transform;
  861. color = sphereData.color.getAsRGBA();
  862. }
  863. break;
  864. case ShapeType::WireCone:
  865. {
  866. ConeData& coneData = mWireConeData[shapeData.idx];
  867. ShapeMeshes3D::wireCone(coneData.base, coneData.normal, coneData.height, coneData.radius,
  868. coneData.scale, meshData, curVertexOffset, curIndexOffet, coneData.quality);
  869. transform = &coneData.transform;
  870. color = coneData.color.getAsRGBA();
  871. }
  872. break;
  873. case ShapeType::Line:
  874. {
  875. LineData& lineData = mLineData[shapeData.idx];
  876. ShapeMeshes3D::pixelLine(lineData.start, lineData.end, meshData, curVertexOffset, curIndexOffet);
  877. transform = &lineData.transform;
  878. color = lineData.color.getAsRGBA();
  879. }
  880. break;
  881. case ShapeType::LineList:
  882. {
  883. LineListData& lineListData = mLineListData[shapeData.idx];
  884. ShapeMeshes3D::pixelLineList(lineListData.lines, meshData, curVertexOffset, curIndexOffet);
  885. transform = &lineListData.transform;
  886. color = lineListData.color.getAsRGBA();
  887. }
  888. break;
  889. case ShapeType::Frustum:
  890. {
  891. FrustumData& frustumData = mFrustumData[shapeData.idx];
  892. ShapeMeshes3D::wireFrustum(frustumData.position, frustumData.aspect, frustumData.FOV, frustumData.nearDist,
  893. frustumData.farDist, meshData, curVertexOffset, curIndexOffet);
  894. transform = &frustumData.transform;
  895. color = frustumData.color.getAsRGBA();
  896. }
  897. break;
  898. case ShapeType::WireDisc:
  899. {
  900. DiscData& discData = mWireDiscData[shapeData.idx];
  901. ShapeMeshes3D::wireDisc(discData.position, discData.radius, discData.normal,
  902. meshData, curVertexOffset, curIndexOffet, discData.quality);
  903. transform = &discData.transform;
  904. color = discData.color.getAsRGBA();
  905. }
  906. break;
  907. case ShapeType::WireArc:
  908. {
  909. ArcData& arcData = mWireArcData[shapeData.idx];
  910. ShapeMeshes3D::wireArc(arcData.position, arcData.radius, arcData.normal,
  911. arcData.startAngle, arcData.amountAngle, meshData, curVertexOffset, curIndexOffet, arcData.quality);
  912. transform = &arcData.transform;
  913. color = arcData.color.getAsRGBA();
  914. }
  915. break;
  916. default:
  917. break;
  918. }
  919. for (UINT32 i = 0; i < shapeData.numVertices; i++)
  920. {
  921. Vector3 worldPos = transform->multiplyAffine(positionIter.getValue());
  922. positionIter.addValue(worldPos);
  923. colorIter.addValue(color);
  924. }
  925. curVertexOffset += shapeData.numVertices;
  926. curIndexOffet += shapeData.numIndices;
  927. }
  928. mMeshes.push_back(ShapeMeshData());
  929. ShapeMeshData& newMesh = mMeshes.back();
  930. newMesh.mesh = mLineMeshHeap->alloc(meshData, DOT_LINE_LIST);
  931. newMesh.type = MeshType::Line;
  932. }
  933. else // Text
  934. {
  935. SPtr<MeshData> meshData = bs_shared_ptr_new<MeshData>(batch.numVertices,
  936. batch.numIndices, mTextVertexDesc);
  937. UINT32 curVertexOffset = 0;
  938. UINT32 curIndexOffet = 0;
  939. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  940. auto uvIter = meshData->getVec2DataIter(VES_TEXCOORD);
  941. auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
  942. for (UINT32 i = batch.startIdx; i <= batch.endIdx; i++)
  943. {
  944. RawData& shapeData = allShapes[i];
  945. Text2DData& text2DData = mText2DData[shapeData.idx];
  946. TextRenderData& renderData = textRenderData[shapeData.textIdx];
  947. UINT32 numQuads = renderData.textData->getNumQuadsForPage(renderData.page);
  948. UINT32* indices = meshData->getIndices32();
  949. // Note: Need temporary buffers because TextLine doesn't support arbitrary vertex stride. Eventually
  950. // that should be supported (should be almost trivial to implement)
  951. Vector2* tempVertices = bs_stack_alloc<Vector2>(shapeData.numVertices);
  952. Vector2* tempUVs = bs_stack_alloc<Vector2>(shapeData.numVertices);
  953. UINT32 numLines = renderData.textData->getNumLines();
  954. UINT32 quadOffset = 0;
  955. for (UINT32 j = 0; j < numLines; j++)
  956. {
  957. const TextDataBase::TextLine& line = renderData.textData->getLine(j);
  958. UINT32 writtenQuads = line.fillBuffer(renderData.page, tempVertices, tempUVs, indices, quadOffset, numQuads);
  959. quadOffset += writtenQuads;
  960. }
  961. Vector3 translation = text2DData.transform.getTranslation();
  962. Vector2 accum(BsZero);
  963. for (UINT32 j = 0; j < shapeData.numVertices; j++)
  964. accum += tempVertices[j];
  965. Vector2 center2D = accum / (float)shapeData.numVertices;
  966. Vector3 lookAt = Vector3::normalize(reference - translation);
  967. Quaternion rotation;
  968. rotation.lookRotation(lookAt, Vector3::UNIT_Y);
  969. float scale = translation.distance(reference) * 0.0025f; // 0.0025 = arbitrary scale to make the text look okay in world space
  970. // Scale by negative because we want to flip the vertices (they're upside down because GUI shader expects them as such)
  971. Matrix4 transform = Matrix4::TRS(translation, rotation, Vector3::ONE);
  972. for (UINT32 j = 0; j < shapeData.numVertices; j++)
  973. {
  974. Vector2 localPos2D = tempVertices[j] - center2D;
  975. localPos2D = localPos2D * -scale;
  976. Vector3 localPos(localPos2D.x, localPos2D.y, 0.0f);
  977. Vector3 worldPos = transform.multiplyAffine(localPos);
  978. positionIter.addValue(worldPos);
  979. uvIter.addValue(tempUVs[j]);
  980. colorIter.addValue(text2DData.color.getAsRGBA());
  981. }
  982. bs_stack_free(tempUVs);
  983. bs_stack_free(tempVertices);
  984. curVertexOffset += shapeData.numVertices;
  985. curIndexOffet += shapeData.numIndices;
  986. }
  987. mMeshes.push_back(ShapeMeshData());
  988. ShapeMeshData& newMesh = mMeshes.back();
  989. newMesh.mesh = mTextMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  990. newMesh.type = MeshType::Text;
  991. newMesh.texture = batch.texture;
  992. }
  993. }
  994. mNumActiveMeshes += (UINT32)mMeshes.size();
  995. }
  996. void DrawHelper::clearMeshes(const Vector<ShapeMeshData>& meshes)
  997. {
  998. for (auto meshData : meshes)
  999. {
  1000. if (meshData.type == MeshType::Solid)
  1001. mSolidMeshHeap->dealloc(meshData.mesh);
  1002. if (meshData.type == MeshType::Wire)
  1003. mWireMeshHeap->dealloc(meshData.mesh);
  1004. else if (meshData.type == MeshType::Line)
  1005. mLineMeshHeap->dealloc(meshData.mesh);
  1006. else // Text
  1007. mTextMeshHeap->dealloc(meshData.mesh);
  1008. }
  1009. mNumActiveMeshes -= (UINT32)meshes.size();
  1010. }
  1011. }