BsDrawHelper.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. #include "BsDrawHelper.h"
  2. #include "BsMesh.h"
  3. #include "BsAABox.h"
  4. #include "BsSphere.h"
  5. #include "BsVertexDataDesc.h"
  6. #include "BsMeshHeap.h"
  7. #include "BsShapeMeshes3D.h"
  8. namespace BansheeEngine
  9. {
  10. const UINT32 DrawHelper::VERTEX_BUFFER_GROWTH = 4096;
  11. const UINT32 DrawHelper::INDEX_BUFFER_GROWTH = 4096 * 2;
  12. DrawHelper::DrawHelper()
  13. {
  14. mTransform = Matrix4::IDENTITY;
  15. mSolidVertexDesc = bs_shared_ptr<VertexDataDesc>();
  16. mSolidVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  17. mSolidVertexDesc->addVertElem(VET_FLOAT3, VES_NORMAL);
  18. mSolidVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  19. mWireVertexDesc = bs_shared_ptr<VertexDataDesc>();
  20. mWireVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
  21. mWireVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  22. mSolidMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mSolidVertexDesc);
  23. mWireMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mWireVertexDesc);
  24. }
  25. DrawHelper::~DrawHelper()
  26. {
  27. clearMeshes();
  28. }
  29. void DrawHelper::setColor(const Color& color)
  30. {
  31. mColor = color;
  32. }
  33. void DrawHelper::setTransform(const Matrix4& transform)
  34. {
  35. mTransform = transform;
  36. }
  37. void DrawHelper::cube(const Vector3& position, const Vector3& extents)
  38. {
  39. mSolidCubeData.push_back(CubeData());
  40. CubeData& cubeData = mSolidCubeData.back();
  41. cubeData.position = position;
  42. cubeData.extents = extents;
  43. cubeData.color = mColor;
  44. cubeData.transform = mTransform;
  45. cubeData.center = mTransform.multiplyAffine(position);
  46. }
  47. void DrawHelper::sphere(const Vector3& position, float radius, UINT32 quality)
  48. {
  49. mSolidSphereData.push_back(SphereData());
  50. SphereData& sphereData = mSolidSphereData.back();
  51. sphereData.position = position;
  52. sphereData.radius = radius;
  53. sphereData.quality = quality;
  54. sphereData.color = mColor;
  55. sphereData.transform = mTransform;
  56. sphereData.center = mTransform.multiplyAffine(position);
  57. }
  58. void DrawHelper::wireCube(const Vector3& position, const Vector3& extents)
  59. {
  60. mWireCubeData.push_back(CubeData());
  61. CubeData& cubeData = mWireCubeData.back();
  62. cubeData.position = position;
  63. cubeData.extents = extents;
  64. cubeData.color = mColor;
  65. cubeData.transform = mTransform;
  66. cubeData.center = mTransform.multiplyAffine(position);
  67. }
  68. void DrawHelper::wireSphere(const Vector3& position, float radius, UINT32 quality)
  69. {
  70. mWireSphereData.push_back(SphereData());
  71. SphereData& sphereData = mWireSphereData.back();
  72. sphereData.position = position;
  73. sphereData.radius = radius;
  74. sphereData.quality = quality;
  75. sphereData.color = mColor;
  76. sphereData.transform = mTransform;
  77. sphereData.center = mTransform.multiplyAffine(position);
  78. }
  79. void DrawHelper::line(const Vector3& start, const Vector3& end)
  80. {
  81. mLineData.push_back(LineData());
  82. LineData& lineData = mLineData.back();
  83. lineData.start = start;
  84. lineData.end = end;
  85. lineData.color = mColor;
  86. lineData.transform = mTransform;
  87. lineData.center = mTransform.multiplyAffine((start + end) * 0.5f);
  88. }
  89. void DrawHelper::frustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
  90. {
  91. mFrustumData.push_back(FrustumData());
  92. FrustumData& frustumData = mFrustumData.back();
  93. frustumData.position = position;
  94. frustumData.aspect = aspect;
  95. frustumData.FOV = FOV;
  96. frustumData.near = near;
  97. frustumData.far = far;
  98. frustumData.color = mColor;
  99. frustumData.transform = mTransform;
  100. frustumData.center = mTransform.multiplyAffine(position);
  101. }
  102. void DrawHelper::cone(const Vector3& base, const Vector3& normal, float height, float radius, UINT32 quality)
  103. {
  104. mConeData.push_back(ConeData());
  105. ConeData& coneData = mConeData.back();
  106. coneData.base = base;
  107. coneData.normal = normal;
  108. coneData.height = height;
  109. coneData.radius = radius;
  110. coneData.quality = quality;
  111. coneData.color = mColor;
  112. coneData.transform = mTransform;
  113. coneData.center = mTransform.multiplyAffine(base + normal * height * 0.5f);
  114. }
  115. void DrawHelper::disc(const Vector3& position, const Vector3& normal, float radius, UINT32 quality)
  116. {
  117. mDiscData.push_back(DiscData());
  118. DiscData& discData = mDiscData.back();
  119. discData.position = position;
  120. discData.normal = normal;
  121. discData.radius = radius;
  122. discData.quality = quality;
  123. discData.color = mColor;
  124. discData.transform = mTransform;
  125. discData.center = mTransform.multiplyAffine(position);
  126. }
  127. void DrawHelper::wireDisc(const Vector3& position, const Vector3& normal, float radius, UINT32 quality)
  128. {
  129. mWireDiscData.push_back(DiscData());
  130. DiscData& discData = mWireDiscData.back();
  131. discData.position = position;
  132. discData.normal = normal;
  133. discData.radius = radius;
  134. discData.quality = quality;
  135. discData.color = mColor;
  136. discData.transform = mTransform;
  137. discData.center = mTransform.multiplyAffine(position);
  138. }
  139. void DrawHelper::arc(const Vector3& position, const Vector3& normal, float radius,
  140. Degree startAngle, Degree amountAngle, UINT32 quality)
  141. {
  142. mArcData.push_back(ArcData());
  143. ArcData& arcData = mArcData.back();
  144. arcData.position = position;
  145. arcData.normal = normal;
  146. arcData.radius = radius;
  147. arcData.startAngle = startAngle;
  148. arcData.amountAngle = amountAngle;
  149. arcData.quality = quality;
  150. arcData.color = mColor;
  151. arcData.transform = mTransform;
  152. arcData.center = mTransform.multiplyAffine(position);
  153. }
  154. void DrawHelper::wireArc(const Vector3& position, const Vector3& normal, float radius,
  155. Degree startAngle, Degree amountAngle, UINT32 quality)
  156. {
  157. mWireArcData.push_back(ArcData());
  158. ArcData& arcData = mWireArcData.back();
  159. arcData.position = position;
  160. arcData.normal = normal;
  161. arcData.radius = radius;
  162. arcData.startAngle = startAngle;
  163. arcData.amountAngle = amountAngle;
  164. arcData.quality = quality;
  165. arcData.color = mColor;
  166. arcData.transform = mTransform;
  167. arcData.center = mTransform.multiplyAffine(position);
  168. }
  169. void DrawHelper::rectangle(const Rect3& area)
  170. {
  171. mRect3Data.push_back(Rect3Data());
  172. Rect3Data& rectData = mRect3Data.back();
  173. rectData.area = area;
  174. rectData.color = mColor;
  175. rectData.transform = mTransform;
  176. rectData.center = mTransform.multiplyAffine(area.getCenter());
  177. }
  178. void DrawHelper::clear()
  179. {
  180. mSolidCubeData.clear();
  181. mWireCubeData.clear();
  182. mSolidSphereData.clear();
  183. mWireSphereData.clear();
  184. mLineData.clear();
  185. mRect3Data.clear();
  186. mFrustumData.clear();
  187. mFrustumData.clear();
  188. mDiscData.clear();
  189. mWireDiscData.clear();
  190. mArcData.clear();
  191. mWireArcData.clear();
  192. mConeData.clear();
  193. }
  194. void DrawHelper::buildMeshes(SortType sorting, const Vector3& reference)
  195. {
  196. clearMeshes();
  197. enum class ShapeType
  198. {
  199. Cube, Sphere, WireCube, WireSphere, Line, Frustum,
  200. Cone, Disc, WireDisc, Arc, WireArc, Rectangle
  201. };
  202. struct RawData
  203. {
  204. ShapeType shapeType;
  205. MeshType meshType;
  206. UINT32 idx;
  207. float distance;
  208. UINT32 numVertices;
  209. UINT32 numIndices;
  210. };
  211. /************************************************************************/
  212. /* Sort everything according to specified sorting rule */
  213. /************************************************************************/
  214. UINT32 totalNumShapes = (UINT32)(mSolidCubeData.size() + mSolidSphereData.size() +
  215. mWireCubeData.size() + mWireSphereData.size() + mLineData.size() + mFrustumData.size() + mConeData.size() +
  216. mDiscData.size() + mWireDiscData.size() + mArcData.size() + mWireArcData.size() + mRect3Data.size());
  217. UINT32 idx = 0;
  218. Vector<RawData> allShapes(totalNumShapes);
  219. UINT32 localIdx = 0;
  220. for (auto& shapeData : mSolidCubeData)
  221. {
  222. RawData& rawData = allShapes[idx];
  223. rawData.idx = localIdx++;
  224. rawData.meshType = MeshType::Solid;
  225. rawData.shapeType = ShapeType::Cube;
  226. rawData.distance = shapeData.center.distance(reference);
  227. rawData.numVertices = 24;
  228. rawData.numIndices = 36;
  229. idx++;
  230. }
  231. localIdx = 0;
  232. for (auto& shapeData : mSolidSphereData)
  233. {
  234. RawData& rawData = allShapes[idx];
  235. rawData.idx = localIdx++;
  236. rawData.meshType = MeshType::Solid;
  237. rawData.shapeType = ShapeType::Sphere;
  238. rawData.distance = shapeData.center.distance(reference);
  239. ShapeMeshes3D::getNumElementsSphere(shapeData.quality,
  240. rawData.numVertices, rawData.numIndices);
  241. idx++;
  242. }
  243. localIdx = 0;
  244. for (auto& shapeData : mConeData)
  245. {
  246. RawData& rawData = allShapes[idx];
  247. rawData.idx = localIdx++;
  248. rawData.meshType = MeshType::Solid;
  249. rawData.shapeType = ShapeType::Cone;
  250. rawData.distance = shapeData.center.distance(reference);
  251. ShapeMeshes3D::getNumElementsCone(shapeData.quality,
  252. rawData.numVertices, rawData.numIndices);
  253. idx++;
  254. }
  255. localIdx = 0;
  256. for (auto& shapeData : mDiscData)
  257. {
  258. RawData& rawData = allShapes[idx];
  259. rawData.idx = localIdx++;
  260. rawData.meshType = MeshType::Solid;
  261. rawData.shapeType = ShapeType::Disc;
  262. rawData.distance = shapeData.center.distance(reference);
  263. ShapeMeshes3D::getNumElementsDisc(shapeData.quality,
  264. rawData.numVertices, rawData.numIndices);
  265. idx++;
  266. }
  267. localIdx = 0;
  268. for (auto& shapeData : mArcData)
  269. {
  270. RawData& rawData = allShapes[idx];
  271. rawData.idx = localIdx++;
  272. rawData.meshType = MeshType::Solid;
  273. rawData.shapeType = ShapeType::Arc;
  274. rawData.distance = shapeData.center.distance(reference);
  275. ShapeMeshes3D::getNumElementsArc(shapeData.quality,
  276. rawData.numVertices, rawData.numIndices);
  277. idx++;
  278. }
  279. localIdx = 0;
  280. for (auto& shapeData : mRect3Data)
  281. {
  282. RawData& rawData = allShapes[idx];
  283. rawData.idx = localIdx++;
  284. rawData.meshType = MeshType::Solid;
  285. rawData.shapeType = ShapeType::Rectangle;
  286. rawData.distance = shapeData.center.distance(reference);
  287. rawData.numVertices = 8;
  288. rawData.numIndices = 12;
  289. idx++;
  290. }
  291. localIdx = 0;
  292. for (auto& shapeData : mWireCubeData)
  293. {
  294. RawData& rawData = allShapes[idx];
  295. rawData.idx = localIdx++;
  296. rawData.meshType = MeshType::Wire;
  297. rawData.shapeType = ShapeType::WireCube;
  298. rawData.distance = shapeData.center.distance(reference);
  299. rawData.numVertices = 8;
  300. rawData.numIndices = 24;
  301. idx++;
  302. }
  303. localIdx = 0;
  304. for (auto& shapeData : mWireSphereData)
  305. {
  306. RawData& rawData = allShapes[idx];
  307. rawData.idx = localIdx++;
  308. rawData.meshType = MeshType::Wire;
  309. rawData.shapeType = ShapeType::WireSphere;
  310. rawData.distance = shapeData.center.distance(reference);
  311. ShapeMeshes3D::getNumElementsWireSphere(shapeData.quality,
  312. rawData.numVertices, rawData.numIndices);
  313. idx++;
  314. }
  315. localIdx = 0;
  316. for (auto& shapeData : mLineData)
  317. {
  318. RawData& rawData = allShapes[idx];
  319. rawData.idx = localIdx++;
  320. rawData.meshType = MeshType::Wire;
  321. rawData.shapeType = ShapeType::Line;
  322. rawData.distance = shapeData.center.distance(reference);
  323. rawData.numVertices = 2;
  324. rawData.numIndices = 2;
  325. idx++;
  326. }
  327. localIdx = 0;
  328. for (auto& shapeData : mFrustumData)
  329. {
  330. RawData& rawData = allShapes[idx];
  331. rawData.idx = localIdx++;
  332. rawData.meshType = MeshType::Wire;
  333. rawData.shapeType = ShapeType::Frustum;
  334. rawData.distance = shapeData.center.distance(reference);
  335. rawData.numVertices = 8;
  336. rawData.numIndices = 36;
  337. idx++;
  338. }
  339. localIdx = 0;
  340. for (auto& shapeData : mWireDiscData)
  341. {
  342. RawData& rawData = allShapes[idx];
  343. rawData.idx = localIdx++;
  344. rawData.meshType = MeshType::Wire;
  345. rawData.shapeType = ShapeType::WireDisc;
  346. rawData.distance = shapeData.center.distance(reference);
  347. ShapeMeshes3D::getNumElementsWireDisc(shapeData.quality,
  348. rawData.numVertices, rawData.numIndices);
  349. idx++;
  350. }
  351. localIdx = 0;
  352. for (auto& shapeData : mWireArcData)
  353. {
  354. RawData& rawData = allShapes[idx];
  355. rawData.idx = localIdx++;
  356. rawData.meshType = MeshType::Wire;
  357. rawData.shapeType = ShapeType::WireArc;
  358. rawData.distance = shapeData.center.distance(reference);
  359. ShapeMeshes3D::getNumElementsWireArc(shapeData.quality,
  360. rawData.numVertices, rawData.numIndices);
  361. idx++;
  362. }
  363. if (sorting == SortType::FrontToBack)
  364. {
  365. std::sort(begin(allShapes), end(allShapes),
  366. [&](const RawData& x, const RawData& y)
  367. {
  368. return x.distance < y.distance;
  369. });
  370. }
  371. else if (sorting == SortType::BackToFront)
  372. {
  373. std::sort(begin(allShapes), end(allShapes),
  374. [&](const RawData& x, const RawData& y)
  375. {
  376. return y.distance < x.distance;
  377. });
  378. }
  379. /************************************************************************/
  380. /* Create batches */
  381. /************************************************************************/
  382. struct Batch
  383. {
  384. MeshType type;
  385. UINT32 startIdx;
  386. UINT32 endIdx;
  387. UINT32 numVertices;
  388. UINT32 numIndices;
  389. };
  390. Vector<Batch> batches;
  391. if (totalNumShapes > 0)
  392. {
  393. batches.push_back(Batch());
  394. {
  395. Batch& currentBatch = batches.back();
  396. currentBatch.startIdx = 0;
  397. currentBatch.type = allShapes[0].meshType;
  398. currentBatch.numVertices = allShapes[0].numVertices;
  399. currentBatch.numIndices = allShapes[0].numIndices;
  400. }
  401. for (UINT32 i = 1; i < totalNumShapes; i++)
  402. {
  403. Batch& currentBatch = batches.back();
  404. if (allShapes[i].meshType != currentBatch.type)
  405. {
  406. currentBatch.endIdx = i - 1;
  407. batches.push_back(Batch());
  408. Batch& newBatch = batches.back();
  409. newBatch.startIdx = i;
  410. newBatch.type = allShapes[i].meshType;
  411. newBatch.numVertices = allShapes[i].numVertices;
  412. newBatch.numIndices = allShapes[i].numIndices;
  413. }
  414. else
  415. {
  416. currentBatch.endIdx = i;
  417. currentBatch.numVertices += allShapes[i].numVertices;
  418. currentBatch.numIndices += allShapes[i].numIndices;
  419. }
  420. }
  421. {
  422. Batch& currentBatch = batches.back();
  423. currentBatch.endIdx = totalNumShapes - 1;
  424. }
  425. }
  426. /************************************************************************/
  427. /* Generate geometry for each batch */
  428. /************************************************************************/
  429. for (auto& batch : batches)
  430. {
  431. if (batch.type == MeshType::Solid)
  432. {
  433. MeshDataPtr meshData = bs_shared_ptr<MeshData>(batch.numVertices, batch.numIndices, mSolidVertexDesc);
  434. UINT32 curVertexOffset = 0;
  435. UINT32 curIndexOffet = 0;
  436. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  437. auto normalIter = meshData->getVec3DataIter(VES_NORMAL);
  438. auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
  439. for (UINT32 i = batch.startIdx; i <= batch.endIdx; i++)
  440. {
  441. RawData& shapeData = allShapes[i];
  442. Matrix4* transform = nullptr;
  443. RGBA color = 0;
  444. switch (shapeData.shapeType)
  445. {
  446. case ShapeType::Cube:
  447. {
  448. CubeData& cubeData = mSolidCubeData[shapeData.idx];
  449. AABox box(cubeData.position - cubeData.extents, cubeData.position + cubeData.extents);
  450. ShapeMeshes3D::solidAABox(box, meshData, curVertexOffset, curIndexOffet);
  451. transform = &cubeData.transform;
  452. color = cubeData.color.getAsRGBA();
  453. }
  454. break;
  455. case ShapeType::Sphere:
  456. {
  457. SphereData& sphereData = mSolidSphereData[shapeData.idx];
  458. Sphere sphere(sphereData.position, sphereData.radius);
  459. ShapeMeshes3D::solidSphere(sphere, meshData, curVertexOffset, curIndexOffet, sphereData.quality);
  460. transform = &sphereData.transform;
  461. color = sphereData.color.getAsRGBA();
  462. }
  463. break;
  464. case ShapeType::Cone:
  465. {
  466. ConeData& coneData = mConeData[shapeData.idx];
  467. ShapeMeshes3D::solidCone(coneData.base, coneData.normal, coneData.height, coneData.radius,
  468. meshData, curVertexOffset, curIndexOffet, coneData.quality);
  469. transform = &coneData.transform;
  470. color = coneData.color.getAsRGBA();
  471. }
  472. break;
  473. case ShapeType::Disc:
  474. {
  475. DiscData& discData = mDiscData[shapeData.idx];
  476. ShapeMeshes3D::solidDisc(discData.position, discData.radius, discData.normal,
  477. meshData, curVertexOffset, curIndexOffet, discData.quality);
  478. transform = &discData.transform;
  479. color = discData.color.getAsRGBA();
  480. }
  481. break;
  482. case ShapeType::Arc:
  483. {
  484. ArcData& arcData = mArcData[shapeData.idx];
  485. ShapeMeshes3D::solidArc(arcData.position, arcData.radius, arcData.normal,
  486. arcData.startAngle, arcData.amountAngle, meshData, curVertexOffset, curIndexOffet, arcData.quality);
  487. transform = &arcData.transform;
  488. color = arcData.color.getAsRGBA();
  489. }
  490. break;
  491. case ShapeType::Rectangle:
  492. {
  493. Rect3Data rectData = mRect3Data[shapeData.idx];
  494. ShapeMeshes3D::solidQuad(rectData.area, meshData, curVertexOffset, curIndexOffet);
  495. transform = &rectData.transform;
  496. color = rectData.color.getAsRGBA();
  497. }
  498. break;
  499. }
  500. Matrix4 transformIT = transform->inverseAffine().transpose();
  501. for (UINT32 i = 0; i < shapeData.numVertices; i++)
  502. {
  503. Vector3 worldPos = transform->multiplyAffine(positionIter.getValue());
  504. Vector3 worldNormal = transformIT.multiplyAffine(normalIter.getValue());
  505. positionIter.addValue(worldPos);
  506. normalIter.addValue(worldNormal);
  507. colorIter.addValue(color);
  508. }
  509. curVertexOffset += shapeData.numVertices;
  510. curIndexOffet += shapeData.numIndices;
  511. }
  512. mMeshes.push_back(ShapeMeshData());
  513. ShapeMeshData& newMesh = mMeshes.back();
  514. newMesh.mesh = mSolidMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
  515. newMesh.type = MeshType::Solid;
  516. }
  517. else // Wire
  518. {
  519. MeshDataPtr meshData = bs_shared_ptr<MeshData>(batch.numVertices,
  520. batch.numIndices, mWireVertexDesc);
  521. UINT32 curVertexOffset = 0;
  522. UINT32 curIndexOffet = 0;
  523. auto positionIter = meshData->getVec3DataIter(VES_POSITION);
  524. auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
  525. for (UINT32 i = batch.startIdx; i <= batch.endIdx; i++)
  526. {
  527. RawData& shapeData = allShapes[i];
  528. Matrix4* transform = nullptr;
  529. RGBA color = 0;
  530. switch (shapeData.shapeType)
  531. {
  532. case ShapeType::WireCube:
  533. {
  534. CubeData& cubeData = mWireCubeData[shapeData.idx];
  535. AABox box(cubeData.position - cubeData.extents, cubeData.position + cubeData.extents);
  536. ShapeMeshes3D::wireAABox(box, meshData, curVertexOffset, curIndexOffet);
  537. transform = &cubeData.transform;
  538. color = cubeData.color.getAsRGBA();
  539. }
  540. break;
  541. case ShapeType::WireSphere:
  542. {
  543. SphereData& sphereData = mWireSphereData[shapeData.idx];
  544. Sphere sphere(sphereData.position, sphereData.radius);
  545. ShapeMeshes3D::wireSphere(sphere, meshData, curVertexOffset, curIndexOffet, sphereData.quality);
  546. transform = &sphereData.transform;
  547. color = sphereData.color.getAsRGBA();
  548. }
  549. break;
  550. case ShapeType::Line:
  551. {
  552. LineData& lineData = mLineData[shapeData.idx];
  553. ShapeMeshes3D::pixelLine(lineData.start, lineData.end, meshData, curVertexOffset, curIndexOffet);
  554. transform = &lineData.transform;
  555. color = lineData.color.getAsRGBA();
  556. }
  557. break;
  558. case ShapeType::Frustum:
  559. {
  560. FrustumData& frustumData = mFrustumData[shapeData.idx];
  561. ShapeMeshes3D::wireFrustum(frustumData.position, frustumData.aspect, frustumData.FOV, frustumData.near,
  562. frustumData.far, meshData, curVertexOffset, curIndexOffet);
  563. transform = &frustumData.transform;
  564. color = frustumData.color.getAsRGBA();
  565. }
  566. break;
  567. case ShapeType::WireDisc:
  568. {
  569. DiscData& discData = mWireDiscData[shapeData.idx];
  570. ShapeMeshes3D::wireDisc(discData.position, discData.radius, discData.normal,
  571. meshData, curVertexOffset, curIndexOffet, discData.quality);
  572. transform = &discData.transform;
  573. color = discData.color.getAsRGBA();
  574. }
  575. break;
  576. case ShapeType::WireArc:
  577. {
  578. ArcData& arcData = mWireArcData[shapeData.idx];
  579. ShapeMeshes3D::wireArc(arcData.position, arcData.radius, arcData.normal,
  580. arcData.startAngle, arcData.amountAngle, meshData, curVertexOffset, curIndexOffet, arcData.quality);
  581. transform = &arcData.transform;
  582. color = arcData.color.getAsRGBA();
  583. }
  584. break;
  585. }
  586. for (UINT32 i = 0; i < shapeData.numVertices; i++)
  587. {
  588. Vector3 worldPos = transform->multiplyAffine(positionIter.getValue());
  589. positionIter.addValue(worldPos);
  590. colorIter.addValue(color);
  591. }
  592. curVertexOffset += shapeData.numVertices;
  593. curIndexOffet += shapeData.numIndices;
  594. }
  595. mMeshes.push_back(ShapeMeshData());
  596. ShapeMeshData& newMesh = mMeshes.back();
  597. newMesh.mesh = mWireMeshHeap->alloc(meshData, DOT_LINE_LIST);
  598. newMesh.type = MeshType::Wire;
  599. }
  600. }
  601. }
  602. void DrawHelper::clearMeshes()
  603. {
  604. for (auto meshData : mMeshes)
  605. {
  606. if (meshData.type == MeshType::Solid)
  607. mSolidMeshHeap->dealloc(meshData.mesh);
  608. else
  609. mWireMeshHeap->dealloc(meshData.mesh);
  610. }
  611. mMeshes.clear();
  612. }
  613. }