BsDrawHelperTemplate.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #pragma once
  2. #include "BsPrerequisites.h"
  3. #include "BsDebugDrawMaterialInfo.h"
  4. #include "CmColor.h"
  5. #include "CmAABox.h"
  6. namespace BansheeEngine
  7. {
  8. enum class DebugDrawCoordType
  9. {
  10. Pixel,
  11. Normalized
  12. };
  13. enum class DrawStyle
  14. {
  15. Fill,
  16. Border,
  17. FillAndBorder
  18. };
  19. enum class DebugDrawType
  20. {
  21. ClipSpace,
  22. ScreenSpace,
  23. WorldSpace
  24. };
  25. struct DebugDrawCommand
  26. {
  27. HMesh mesh;
  28. DebugDraw2DClipSpaceMatInfo matInfo2DClipSpace;
  29. DebugDraw2DScreenSpaceMatInfo matInfo2DScreenSpace;
  30. DebugDraw3DMatInfo matInfo3D;
  31. DebugDrawType type;
  32. Vector3 worldCenter;
  33. float endTime;
  34. };
  35. class BS_EXPORT DrawHelperTemplateBase
  36. {
  37. public:
  38. void render(const HCamera& camera, RenderQueue& renderQueue);
  39. protected:
  40. UnorderedMap<const Viewport*, Vector<DebugDrawCommand>::type>::type mCommandsPerViewport;
  41. };
  42. template <class T>
  43. class BS_EXPORT DrawHelperTemplate : public DrawHelperTemplateBase
  44. {
  45. protected:
  46. void line_Pixel(const T& a, const T& b, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
  47. {
  48. UINT32* indexData = meshData->getIndices32();
  49. UINT8* positionData = meshData->getElementData(VES_POSITION);
  50. UINT8* colorData = meshData->getElementData(VES_COLOR);
  51. assert((vertexOffset + 2) <= meshData->getNumVertices());
  52. assert((indexOffset + 2) <= meshData->getNumIndices());
  53. line_Pixel(a, b, color, positionData, colorData, vertexOffset, meshData->getVertexDesc()->getVertexStride(), indexData, indexOffset);
  54. }
  55. void line_AA(const T& a, const T& b, float width, float borderWidth, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
  56. {
  57. UINT32* indexData = meshData->getIndices32();
  58. UINT8* positionData = meshData->getElementData(VES_POSITION);
  59. UINT8* colorData = meshData->getElementData(VES_COLOR);
  60. assert((vertexOffset + 8) <= meshData->getNumVertices());
  61. assert((indexOffset + 30) <= meshData->getNumIndices());
  62. line_AA(a, b, width, borderWidth, color, positionData, colorData, vertexOffset, meshData->getVertexDesc()->getVertexStride(), indexData, indexOffset);
  63. }
  64. void lineList_Pixel(const typename Vector<T>::type& linePoints, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
  65. {
  66. assert(linePoints.size() % 2 == 0);
  67. assert((vertexOffset + linePoints.size() * 2) <= meshData->getNumVertices());
  68. assert((indexOffset + linePoints.size() * 2) <= meshData->getNumIndices());
  69. UINT32 curVertOffset = vertexOffset;
  70. UINT32 curIdxOffset = indexOffset;
  71. UINT32* indexData = meshData->getIndices32();
  72. UINT8* positionData = meshData->getElementData(VES_POSITION);
  73. UINT8* colorData = meshData->getElementData(VES_COLOR);
  74. UINT32 numPoints = (UINT32)linePoints.size();
  75. for(UINT32 i = 0; i < numPoints; i += 2)
  76. {
  77. line_Pixel(linePoints[i], linePoints[i + 1], color, positionData, colorData, curVertOffset, meshData->getVertexDesc()->getVertexStride(), indexData, curIdxOffset);
  78. curVertOffset += 2;
  79. curIdxOffset += 2;
  80. }
  81. }
  82. void lineList_AA(const typename Vector<T>::type& linePoints, float width, float borderWidth, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
  83. {
  84. assert(linePoints.size() % 2 == 0);
  85. assert((vertexOffset + linePoints.size() * 4) <= meshData->getNumVertices());
  86. assert((indexOffset + linePoints.size() * 15) <= meshData->getNumIndices());
  87. UINT32 curVertOffset = vertexOffset;
  88. UINT32 curIdxOffset = indexOffset;
  89. UINT32* indexData = meshData->getIndices32();
  90. UINT8* positionData = meshData->getElementData(VES_POSITION);
  91. UINT8* colorData = meshData->getElementData(VES_COLOR);
  92. UINT32 numPoints = (UINT32)linePoints.size();
  93. for(UINT32 i = 0; i < numPoints; i += 2)
  94. {
  95. line_AA(linePoints[i], linePoints[i + 1], width, borderWidth, color, positionData, colorData, curVertOffset, meshData->getVertexDesc()->getVertexStride(), indexData, curIdxOffset);
  96. curVertOffset += 8;
  97. curIdxOffset += 30;
  98. }
  99. }
  100. void line_Pixel(const T& a, const T& b, const Color& color, UINT8* outVertices, UINT8* outColors,
  101. UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
  102. {
  103. outVertices += (vertexOffset * vertexStride);
  104. outColors += (vertexOffset * vertexStride);
  105. T* vertices = (T*)outVertices;
  106. (*vertices) = a;
  107. vertices = (T*)(outVertices + vertexStride);
  108. (*vertices) = b;
  109. UINT32* colors = (UINT32*)outColors;
  110. (*colors) = color.getAsRGBA();
  111. colors = (UINT32*)(outColors + vertexStride);
  112. (*colors) = color.getAsRGBA();
  113. outIndices += indexOffset;
  114. outIndices[0] = vertexOffset + 0;
  115. outIndices[1] = vertexOffset + 1;
  116. }
  117. virtual void line_AA(const T& a, const T& b, float width, float borderWidth, const Color& color, UINT8* outVertices, UINT8* outColors,
  118. UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset) = 0;
  119. virtual void polygon_AA(const typename Vector<T>::type& points, float borderWidth, const Color& color, UINT8* outVertices, UINT8* outColors,
  120. UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset) = 0;
  121. void polygonFill_Pixel(const typename Vector<T>::type& points, UINT8* outVertices,
  122. UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
  123. {
  124. outVertices += (vertexOffset * vertexStride);
  125. for(auto& point : points)
  126. {
  127. T* vertices = (T*)outVertices;
  128. (*vertices) = point;
  129. outVertices += vertexStride;
  130. }
  131. outIndices += indexOffset;
  132. INT32 numPoints = (INT32)points.size();
  133. UINT32 idxCnt = 0;
  134. for(int i = 2; i < numPoints; i++)
  135. {
  136. outIndices[idxCnt++] = vertexOffset;
  137. outIndices[idxCnt++] = vertexOffset + i - 1;
  138. outIndices[idxCnt++] = vertexOffset + i;
  139. }
  140. }
  141. void polygonBorder_Pixel(const typename Vector<T>::type& points, const Color& color, UINT8* outVertices, UINT8* outColors,
  142. UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
  143. {
  144. INT32 numPoints = (INT32)points.size();
  145. UINT32 curVertOffset = vertexOffset;
  146. UINT32 curIdxOffset = indexOffset;
  147. for(INT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
  148. {
  149. line_Pixel(points[j], points[i], color, outVertices, outColors, curVertOffset, vertexStride, outIndices, curIdxOffset);
  150. curVertOffset += 2;
  151. curIdxOffset += 2;
  152. }
  153. }
  154. };
  155. }