BsDrawHelper2D.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include "BsDrawHelper2D.h"
  2. #include "BsRectF.h"
  3. #include "BsMesh.h"
  4. #include "BsTime.h"
  5. #include "BsVector2.h"
  6. #include "BsMaterial.h"
  7. #include "BsPass.h"
  8. #include "BsCoreApplication.h"
  9. #include "BsRenderQueue.h"
  10. #include "BsCamera.h"
  11. #include "BsCoreThreadAccessor.h"
  12. #include "BsBuiltinMaterialManager.h"
  13. #include "BsVertexDataDesc.h"
  14. namespace BansheeEngine
  15. {
  16. DrawHelper2D::DrawHelper2D()
  17. {
  18. mMaterial2DClipSpace = BuiltinMaterialManager::instance().createDebugDraw2DClipSpaceMaterial();
  19. mVertexDesc = bs_shared_ptr<VertexDataDesc>();
  20. mVertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
  21. mVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  22. }
  23. void DrawHelper2D::quad(const RectF& area, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
  24. {
  25. UINT32* indexData = meshData->getIndices32();
  26. UINT8* positionData = meshData->getElementData(VES_POSITION);
  27. assert((vertexOffset + 4) <= meshData->getNumVertices());
  28. assert((indexOffset + 6) <= meshData->getNumIndices());
  29. Vector<Vector2> points;
  30. points.push_back(Vector2(area.x, area.y));
  31. points.push_back(Vector2(area.x + area.width, area.y));
  32. points.push_back(Vector2(area.x + area.width, area.y + area.height));
  33. points.push_back(Vector2(area.x, area.y + area.height));
  34. polygonFill_Pixel(points, positionData, vertexOffset, meshData->getVertexDesc()->getVertexStride(), indexData, indexOffset);
  35. }
  36. void DrawHelper2D::line_Pixel(const Vector2& a, const Vector2& b, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
  37. {
  38. DrawHelperTemplate<Vector2>::line_Pixel(a, b, color, meshData, vertexOffset, indexOffset);
  39. }
  40. void DrawHelper2D::line_AA(const Vector2& a, const Vector2& b, float width, float borderWidth, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
  41. {
  42. DrawHelperTemplate<Vector2>::line_AA(a, b, width, borderWidth, color, meshData, vertexOffset, indexOffset);
  43. }
  44. void DrawHelper2D::lineList_Pixel(const Vector<Vector2>& linePoints, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
  45. {
  46. DrawHelperTemplate<Vector2>::lineList_Pixel(linePoints, color, meshData, vertexOffset, indexOffset);
  47. }
  48. void DrawHelper2D::lineList_AA(const Vector<Vector2>& linePoints, float width, float borderWidth, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
  49. {
  50. DrawHelperTemplate<Vector2>::lineList_AA(linePoints, width, borderWidth, color, meshData, vertexOffset, indexOffset);
  51. }
  52. /************************************************************************/
  53. /* 2D - DRAW */
  54. /************************************************************************/
  55. void DrawHelper2D::drawQuad(const HCamera& camera, const RectF& area, const Color& color, DebugDrawCoordType coordType, float timeout)
  56. {
  57. const Viewport* viewport = camera->getViewport().get();
  58. Vector<DebugDrawCommand>& commands = mCommandsPerViewport[viewport];
  59. commands.push_back(DebugDrawCommand());
  60. DebugDrawCommand& dbgCmd = commands.back();
  61. dbgCmd.endTime = gTime().getTime() + timeout;
  62. MeshDataPtr meshData = bs_shared_ptr<MeshData, ScratchAlloc>(4, 6, mVertexDesc);
  63. RectF actualArea = area;
  64. if(coordType == DebugDrawCoordType::Normalized)
  65. actualArea = normalizedCoordToClipSpace(area);
  66. quad(actualArea, meshData, 0, 0);
  67. UINT32 vertexStride = mVertexDesc->getVertexStride();
  68. UINT8* colorData = meshData->getElementData(VES_COLOR);
  69. UINT32* colors = (UINT32*)colorData;
  70. (*colors) = color.getAsRGBA();
  71. colors = (UINT32*)(colorData + vertexStride);
  72. (*colors) = color.getAsRGBA();
  73. colors = (UINT32*)(colorData + vertexStride * 2);
  74. (*colors) = color.getAsRGBA();
  75. colors = (UINT32*)(colorData + vertexStride * 3);
  76. (*colors) = color.getAsRGBA();
  77. HMesh mesh = Mesh::create(meshData);
  78. dbgCmd.mesh = mesh;
  79. dbgCmd.worldCenter = Vector3::ZERO;
  80. if(coordType == DebugDrawCoordType::Normalized)
  81. {
  82. dbgCmd.type = DebugDrawType::ClipSpace;
  83. dbgCmd.matInfo2DClipSpace = mMaterial2DClipSpace;
  84. }
  85. else
  86. {
  87. dbgCmd.type = DebugDrawType::ScreenSpace;
  88. dbgCmd.matInfo2DScreenSpace = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
  89. }
  90. }
  91. void DrawHelper2D::drawLine_Pixel(const HCamera& camera, const Vector2& a, const Vector2& b, const Color& color, DebugDrawCoordType coordType, float timeout)
  92. {
  93. const Viewport* viewport = camera->getViewport().get();
  94. Vector<DebugDrawCommand>& commands = mCommandsPerViewport[viewport];
  95. commands.push_back(DebugDrawCommand());
  96. DebugDrawCommand& dbgCmd = commands.back();
  97. dbgCmd.endTime = gTime().getTime() + timeout;
  98. MeshDataPtr meshData = bs_shared_ptr<MeshData, ScratchAlloc>(2, 2, mVertexDesc);
  99. Vector2 actualA = a;
  100. Vector2 actualB = b;
  101. if(coordType == DebugDrawCoordType::Normalized)
  102. {
  103. actualA = normalizedCoordToClipSpace(a);
  104. actualB = normalizedCoordToClipSpace(b);
  105. }
  106. line_Pixel(actualA, actualB, color, meshData, 0, 0);
  107. HMesh mesh = Mesh::create(meshData, MeshBufferType::Static, DOT_LINE_LIST);
  108. dbgCmd.mesh = mesh;
  109. dbgCmd.worldCenter = Vector3::ZERO;
  110. if(coordType == DebugDrawCoordType::Normalized)
  111. {
  112. dbgCmd.type = DebugDrawType::ClipSpace;
  113. dbgCmd.matInfo2DClipSpace = mMaterial2DClipSpace;
  114. }
  115. else
  116. {
  117. dbgCmd.type = DebugDrawType::ScreenSpace;
  118. dbgCmd.matInfo2DScreenSpace = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
  119. }
  120. }
  121. void DrawHelper2D::drawLine_AA(const HCamera& camera, const Vector2& a, const Vector2& b, float width, float borderWidth, const Color& color, DebugDrawCoordType coordType, float timeout)
  122. {
  123. const Viewport* viewport = camera->getViewport().get();
  124. Vector<DebugDrawCommand>& commands = mCommandsPerViewport[viewport];
  125. commands.push_back(DebugDrawCommand());
  126. DebugDrawCommand& dbgCmd = commands.back();
  127. dbgCmd.endTime = gTime().getTime() + timeout;
  128. MeshDataPtr meshData = bs_shared_ptr<MeshData, ScratchAlloc>(8, 30, mVertexDesc);
  129. Vector2 actualA = a;
  130. Vector2 actualB = b;
  131. if(coordType == DebugDrawCoordType::Normalized)
  132. {
  133. actualA = normalizedCoordToClipSpace(a);
  134. actualB = normalizedCoordToClipSpace(b);
  135. }
  136. line_AA(actualA, actualB, width, borderWidth, color, meshData, 0, 0);
  137. HMesh mesh = Mesh::create(meshData);
  138. dbgCmd.mesh = mesh;
  139. dbgCmd.worldCenter = Vector3::ZERO;
  140. if(coordType == DebugDrawCoordType::Normalized)
  141. {
  142. dbgCmd.type = DebugDrawType::ClipSpace;
  143. dbgCmd.matInfo2DClipSpace = mMaterial2DClipSpace;
  144. }
  145. else
  146. {
  147. dbgCmd.type = DebugDrawType::ScreenSpace;
  148. dbgCmd.matInfo2DScreenSpace = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
  149. }
  150. }
  151. void DrawHelper2D::drawLineList_Pixel(const HCamera& camera, const Vector<Vector2>& linePoints, const Color& color,
  152. DebugDrawCoordType coordType, float timeout)
  153. {
  154. const Viewport* viewport = camera->getViewport().get();
  155. Vector<DebugDrawCommand>& commands = mCommandsPerViewport[viewport];
  156. commands.push_back(DebugDrawCommand());
  157. DebugDrawCommand& dbgCmd = commands.back();
  158. dbgCmd.endTime = gTime().getTime() + timeout;
  159. MeshDataPtr meshData = bs_shared_ptr<MeshData, ScratchAlloc>(
  160. (UINT32)(linePoints.size() * 2), (UINT32)(linePoints.size() * 2), mVertexDesc);
  161. if(coordType == DebugDrawCoordType::Normalized)
  162. {
  163. Vector<Vector2> points;
  164. UINT32 numPoints = (UINT32)linePoints.size();
  165. for(UINT32 i = 0; i < numPoints; i++)
  166. points.push_back(normalizedCoordToClipSpace(linePoints[i]));
  167. lineList_Pixel(points, color, meshData, 0, 0);
  168. }
  169. else
  170. {
  171. lineList_Pixel(linePoints, color, meshData, 0, 0);
  172. }
  173. HMesh mesh = Mesh::create(meshData, MeshBufferType::Static, DOT_LINE_LIST);
  174. dbgCmd.mesh = mesh;
  175. dbgCmd.worldCenter = Vector3::ZERO;
  176. if(coordType == DebugDrawCoordType::Normalized)
  177. {
  178. dbgCmd.type = DebugDrawType::ClipSpace;
  179. dbgCmd.matInfo2DClipSpace = mMaterial2DClipSpace;
  180. }
  181. else
  182. {
  183. dbgCmd.type = DebugDrawType::ScreenSpace;
  184. dbgCmd.matInfo2DScreenSpace = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
  185. }
  186. }
  187. void DrawHelper2D::drawLineList_AA(const HCamera& camera, const Vector<Vector2>& linePoints, float width, float borderWidth,
  188. const Color& color, DebugDrawCoordType coordType, float timeout)
  189. {
  190. const Viewport* viewport = camera->getViewport().get();
  191. Vector<DebugDrawCommand>& commands = mCommandsPerViewport[viewport];
  192. commands.push_back(DebugDrawCommand());
  193. DebugDrawCommand& dbgCmd = commands.back();
  194. dbgCmd.endTime = gTime().getTime() + timeout;
  195. MeshDataPtr meshData = bs_shared_ptr<MeshData, ScratchAlloc>((UINT32)(linePoints.size() * 4), (UINT32)(linePoints.size() * 15), mVertexDesc);
  196. if(coordType == DebugDrawCoordType::Normalized)
  197. {
  198. Vector<Vector2> points;
  199. UINT32 numPoints = (UINT32)linePoints.size();
  200. for(UINT32 i = 0; i < numPoints; i++)
  201. points.push_back(normalizedCoordToClipSpace(linePoints[i]));
  202. lineList_AA(points, width, borderWidth, color, meshData, 0, 0);
  203. }
  204. else
  205. {
  206. lineList_AA(linePoints, width, borderWidth, color, meshData, 0, 0);
  207. }
  208. HMesh mesh = Mesh::create(meshData);
  209. dbgCmd.mesh = mesh;
  210. dbgCmd.worldCenter = Vector3::ZERO;
  211. if(coordType == DebugDrawCoordType::Normalized)
  212. {
  213. dbgCmd.type = DebugDrawType::ClipSpace;
  214. dbgCmd.matInfo2DClipSpace = mMaterial2DClipSpace;
  215. }
  216. else
  217. {
  218. dbgCmd.type = DebugDrawType::ScreenSpace;
  219. dbgCmd.matInfo2DScreenSpace = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
  220. }
  221. }
  222. void DrawHelper2D::line_AA(const Vector2& a, const Vector2& b, float width, float borderWidth, const Color& color, UINT8* outVertices, UINT8* outColors,
  223. UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
  224. {
  225. Vector2 dir = b - a;
  226. dir.normalize();
  227. Vector2 nrm(dir.y, -dir.x);
  228. Vector<Vector2> points(4);
  229. float r = width - 1.0f;
  230. r *= 0.5f;
  231. if (r < 0.01f)
  232. r = 0.01f;
  233. dir = dir * r;
  234. nrm = nrm * r;
  235. Vector2 v0 = a - dir - nrm;
  236. Vector2 v1 = a - dir + nrm;
  237. Vector2 v2 = b + dir + nrm;
  238. Vector2 v3 = b + dir - nrm;
  239. points[0] = v0;
  240. points[1] = v1;
  241. points[2] = v2;
  242. points[3] = v3;
  243. polygon_AA(points, borderWidth, color, outVertices, outColors, vertexOffset, vertexStride, outIndices, indexOffset);
  244. }
  245. void DrawHelper2D::polygon_AA(const Vector<Vector2>& points, float borderWidth, const Color& color, UINT8* outVertices, UINT8* outColors,
  246. UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
  247. {
  248. UINT32 numCoords = (UINT32)points.size();
  249. outVertices += vertexOffset * vertexStride;
  250. Vector<Vector2> tempNormals(numCoords);
  251. for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
  252. {
  253. const Vector2& v0 = points[j];
  254. const Vector2& v1 = points[i];
  255. Vector2 d = v1 - v0;
  256. d.normalize();
  257. // Rotate by 90 degrees
  258. std::swap(d.x, d.y); // TODO - Not properly ported
  259. d.y = -d.y;
  260. tempNormals[j] = d;
  261. // Also start populating the vertex array
  262. Vector2* vertices = (Vector2*)outVertices;
  263. *vertices = v1;
  264. UINT32* colors = (UINT32*)outColors;
  265. *colors = color.getAsRGBA();
  266. outVertices += vertexStride;
  267. outColors += vertexStride;
  268. }
  269. Color transparentColor = color;
  270. transparentColor.a = 0.0f;
  271. for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
  272. {
  273. const Vector2& n0 = tempNormals[j];
  274. const Vector2& n1 = tempNormals[i];
  275. Vector2 avgNrm = (n0 + n1) * 0.5f;
  276. float magSqrd = avgNrm.squaredLength();
  277. if (magSqrd > 0.000001f)
  278. {
  279. float scale = 1.0f / magSqrd;
  280. if (scale > 10.0f)
  281. scale = 10.0f;
  282. avgNrm = avgNrm * scale;
  283. }
  284. Vector2 tempCoord = points[i] + avgNrm * borderWidth;
  285. // Move it to the vertex array
  286. Vector2* vertices = (Vector2*)outVertices;
  287. *vertices = tempCoord;
  288. UINT32* colors = (UINT32*)outColors;
  289. *colors = transparentColor.getAsRGBA();
  290. outVertices += vertexStride;
  291. outColors += vertexStride;
  292. }
  293. // Populate index buffer
  294. outIndices += indexOffset;
  295. UINT32 idxCnt = 0;
  296. for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
  297. {
  298. outIndices[idxCnt++] = i;
  299. outIndices[idxCnt++] = j;
  300. outIndices[idxCnt++] = numCoords + j;
  301. outIndices[idxCnt++] = numCoords + j;
  302. outIndices[idxCnt++] = numCoords + i;
  303. outIndices[idxCnt++] = i;
  304. }
  305. for(UINT32 i = 2; i < numCoords; ++i)
  306. {
  307. outIndices[idxCnt++] = 0;
  308. outIndices[idxCnt++] = i - 1;
  309. outIndices[idxCnt++] = i;
  310. }
  311. }
  312. RectF DrawHelper2D::normalizedCoordToClipSpace(const RectF& area) const
  313. {
  314. RectF clipSpaceRect;
  315. clipSpaceRect.x = area.x * 2.0f - 1.0f;
  316. clipSpaceRect.width = area.width * 2.0f;
  317. clipSpaceRect.y = -area.y * 2.0f + 1.0f;
  318. clipSpaceRect.height = area.height * -2.0f;
  319. return clipSpaceRect;
  320. }
  321. Vector2 DrawHelper2D::normalizedCoordToClipSpace(const Vector2& pos) const
  322. {
  323. return Vector2(pos.x * 2.0f - 1.0f, -pos.y * 2.0f + 1.0f);
  324. }
  325. }