BsDrawHelper3D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "BsDrawHelper3D.h"
  2. #include "CmRectF.h"
  3. #include "CmMesh.h"
  4. #include "CmTime.h"
  5. #include "CmVector2.h"
  6. #include "CmMaterial.h"
  7. #include "CmPass.h"
  8. #include "CmApplication.h"
  9. #include "CmRenderQueue.h"
  10. #include "CmException.h"
  11. #include "BsCamera.h"
  12. #include "BsBuiltinMaterialManager.h"
  13. #include "CmVertexDataDesc.h"
  14. using namespace CamelotFramework;
  15. namespace BansheeEngine
  16. {
  17. DrawHelper3D::DrawHelper3D()
  18. {
  19. mVertexDesc = cm_shared_ptr<VertexDataDesc>();
  20. mVertexDesc->addVertElem(VET_FLOAT2, VES_POSITION);
  21. mVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
  22. }
  23. void DrawHelper3D::aabox(const CM::AABox& box, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
  24. {
  25. UINT32* indexData = meshData->getIndices32();
  26. UINT8* positionData = meshData->getElementData(VES_POSITION);
  27. assert((vertexOffset + 8) <= meshData->getNumVertices());
  28. assert((indexOffset + 36) <= meshData->getNumIndices());
  29. aabox(box, positionData, vertexOffset, meshData->getVertexDesc()->getVertexStride(), indexData, indexOffset);
  30. }
  31. void DrawHelper3D::line_Pixel(const Vector3& a, const Vector3& b, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
  32. {
  33. DrawHelperTemplate<Vector3>::line_Pixel(a, b, color, meshData, vertexOffset, indexOffset);
  34. }
  35. void DrawHelper3D::line_AA(const Vector3& a, const Vector3& b, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
  36. {
  37. DrawHelperTemplate<Vector3>::line_AA(a, b, width, borderWidth, color, meshData, vertexOffset, indexOffset);
  38. }
  39. void DrawHelper3D::lineList_Pixel(const CM::Vector<Vector3>::type& linePoints, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
  40. {
  41. DrawHelperTemplate<Vector3>::lineList_Pixel(linePoints, color, meshData, vertexOffset, indexOffset);
  42. }
  43. void DrawHelper3D::lineList_AA(const CM::Vector<Vector3>::type& linePoints, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
  44. {
  45. DrawHelperTemplate<Vector3>::lineList_AA(linePoints, width, borderWidth, color, meshData, vertexOffset, indexOffset);
  46. }
  47. /************************************************************************/
  48. /* DRAW */
  49. /************************************************************************/
  50. void DrawHelper3D::drawLine_Pixel(const HCamera& camera, const Vector3& a, const Vector3& b, const Color& color, float timeout)
  51. {
  52. const Viewport* viewport = camera->getViewport().get();
  53. Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
  54. commands.push_back(DebugDrawCommand());
  55. DebugDrawCommand& dbgCmd = commands.back();
  56. dbgCmd.endTime = gTime().getTime() + timeout;
  57. MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(2, 2, mVertexDesc);
  58. line_Pixel(a, b, color, meshData, 0, 0);
  59. UINT8* positionData = meshData->getElementData(VES_POSITION);
  60. dbgCmd.worldCenter = calcCenter(positionData, meshData->getNumVertices(), mVertexDesc->getVertexStride());
  61. HMesh mesh = Mesh::create(meshData, MeshBufferType::Static, DOT_LINE_LIST);
  62. dbgCmd.mesh = mesh;
  63. dbgCmd.type = DebugDrawType::WorldSpace;
  64. dbgCmd.matInfo3D = BuiltinMaterialManager::instance().createDebugDraw3DMaterial();
  65. }
  66. void DrawHelper3D::drawLine_AA(const HCamera& camera, const Vector3& a, const Vector3& b, float width, float borderWidth, const Color& color, float timeout)
  67. {
  68. const Viewport* viewport = camera->getViewport().get();
  69. Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
  70. commands.push_back(DebugDrawCommand());
  71. DebugDrawCommand& dbgCmd = commands.back();
  72. dbgCmd.endTime = gTime().getTime() + timeout;
  73. MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(8, 30, mVertexDesc);
  74. line_AA(a, b, width, borderWidth, color, meshData, 0, 0);
  75. UINT8* positionData = meshData->getElementData(VES_POSITION);
  76. dbgCmd.worldCenter = calcCenter(positionData, meshData->getNumVertices(), mVertexDesc->getVertexStride());
  77. HMesh mesh = Mesh::create(meshData);
  78. dbgCmd.mesh = mesh;
  79. dbgCmd.type = DebugDrawType::WorldSpace;
  80. dbgCmd.matInfo3D = BuiltinMaterialManager::instance().createDebugDraw3DMaterial();
  81. }
  82. void DrawHelper3D::drawLineList_Pixel(const HCamera& camera, const Vector<Vector3>::type& linePoints, const Color& color, float timeout)
  83. {
  84. const Viewport* viewport = camera->getViewport().get();
  85. Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
  86. commands.push_back(DebugDrawCommand());
  87. DebugDrawCommand& dbgCmd = commands.back();
  88. dbgCmd.endTime = gTime().getTime() + timeout;
  89. MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(
  90. (UINT32)(linePoints.size() * 2), (UINT32)(linePoints.size() * 2), mVertexDesc);
  91. lineList_Pixel(linePoints, color, meshData, 0, 0);
  92. UINT8* positionData = meshData->getElementData(VES_POSITION);
  93. dbgCmd.worldCenter = calcCenter(positionData, meshData->getNumVertices(), mVertexDesc->getVertexStride());
  94. HMesh mesh = Mesh::create(meshData, MeshBufferType::Static, DOT_LINE_LIST);
  95. dbgCmd.mesh = mesh;
  96. dbgCmd.type = DebugDrawType::WorldSpace;
  97. dbgCmd.matInfo3D = BuiltinMaterialManager::instance().createDebugDraw3DMaterial();
  98. }
  99. void DrawHelper3D::drawLineList_AA(const HCamera& camera, const CM::Vector<CM::Vector3>::type& linePoints, float width, float borderWidth,
  100. const CM::Color& color, float timeout)
  101. {
  102. const Viewport* viewport = camera->getViewport().get();
  103. Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
  104. commands.push_back(DebugDrawCommand());
  105. DebugDrawCommand& dbgCmd = commands.back();
  106. dbgCmd.endTime = gTime().getTime() + timeout;
  107. MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>((UINT32)(linePoints.size() * 4), (UINT32)(linePoints.size() * 15), mVertexDesc);
  108. lineList_AA(linePoints, width, borderWidth, color, meshData, 0, 0);
  109. UINT8* positionData = meshData->getElementData(VES_POSITION);
  110. dbgCmd.worldCenter = calcCenter(positionData, meshData->getNumVertices(), mVertexDesc->getVertexStride());
  111. HMesh mesh = Mesh::create(meshData);
  112. dbgCmd.mesh = mesh;
  113. dbgCmd.type = DebugDrawType::WorldSpace;
  114. dbgCmd.matInfo3D = BuiltinMaterialManager::instance().createDebugDraw3DMaterial();
  115. }
  116. void DrawHelper3D::drawAABox(const HCamera& camera, const CM::AABox& box, const CM::Color& color, float timeout)
  117. {
  118. const Viewport* viewport = camera->getViewport().get();
  119. Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
  120. commands.push_back(DebugDrawCommand());
  121. DebugDrawCommand& dbgCmd = commands.back();
  122. dbgCmd.endTime = gTime().getTime() + timeout;
  123. MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(8, 36, mVertexDesc);
  124. aabox(box, meshData, 0, 0);
  125. UINT32 vertexStride = mVertexDesc->getVertexStride();
  126. UINT8* colorData = meshData->getElementData(VES_COLOR);
  127. for(UINT32 i = 0; i < meshData->getNumVertices(); i++)
  128. {
  129. UINT32* colors = (UINT32*)colorData;
  130. (*colors) = color.getAsRGBA();
  131. colorData += vertexStride;
  132. }
  133. UINT8* positionData = meshData->getElementData(VES_POSITION);
  134. dbgCmd.worldCenter = calcCenter(positionData, meshData->getNumVertices(), mVertexDesc->getVertexStride());
  135. HMesh mesh = Mesh::create(meshData);
  136. dbgCmd.mesh = mesh;
  137. dbgCmd.type = DebugDrawType::WorldSpace;
  138. dbgCmd.matInfo3D = BuiltinMaterialManager::instance().createDebugDraw3DMaterial();
  139. }
  140. void DrawHelper3D::aabox(const AABox& box, UINT8* outVertices, UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
  141. {
  142. outVertices += (vertexOffset * vertexStride);
  143. Vector3 pt;
  144. pt = box.getCorner(AABox::FAR_LEFT_BOTTOM);
  145. memcpy(outVertices, &pt, sizeof(pt));
  146. outVertices += vertexStride;
  147. pt = box.getCorner(AABox::FAR_RIGHT_BOTTOM);
  148. memcpy(outVertices, &pt, sizeof(pt));
  149. outVertices += vertexStride;
  150. pt = box.getCorner(AABox::FAR_LEFT_TOP);
  151. memcpy(outVertices, &pt, sizeof(pt));
  152. outVertices += vertexStride;
  153. pt = box.getCorner(AABox::FAR_RIGHT_TOP);
  154. memcpy(outVertices, &pt, sizeof(pt));
  155. outVertices += vertexStride;
  156. pt = box.getCorner(AABox::NEAR_LEFT_BOTTOM);
  157. memcpy(outVertices, &pt, sizeof(pt));
  158. outVertices += vertexStride;
  159. pt = box.getCorner(AABox::NEAR_RIGHT_BOTTOM);
  160. memcpy(outVertices, &pt, sizeof(pt));
  161. outVertices += vertexStride;
  162. pt = box.getCorner(AABox::NEAR_LEFT_TOP);
  163. memcpy(outVertices, &pt, sizeof(pt));
  164. outVertices += vertexStride;
  165. pt = box.getCorner(AABox::NEAR_RIGHT_TOP);
  166. memcpy(outVertices, &pt, sizeof(pt));
  167. outVertices += vertexStride;
  168. outIndices += indexOffset;
  169. // Front
  170. outIndices[0] = vertexOffset + 6;
  171. outIndices[1] = vertexOffset + 7;
  172. outIndices[2] = vertexOffset + 5;
  173. outIndices[3] = vertexOffset + 6;
  174. outIndices[4] = vertexOffset + 5;
  175. outIndices[5] = vertexOffset + 4;
  176. // Back
  177. outIndices[6] = vertexOffset + 2;
  178. outIndices[7] = vertexOffset + 1;
  179. outIndices[8] = vertexOffset + 3;
  180. outIndices[9] = vertexOffset + 2;
  181. outIndices[10] = vertexOffset + 0;
  182. outIndices[11] = vertexOffset + 1;
  183. // Left
  184. outIndices[12] = vertexOffset + 2;
  185. outIndices[13] = vertexOffset + 6;
  186. outIndices[14] = vertexOffset + 4;
  187. outIndices[15] = vertexOffset + 2;
  188. outIndices[16] = vertexOffset + 4;
  189. outIndices[17] = vertexOffset + 0;
  190. // Right
  191. outIndices[18] = vertexOffset + 7;
  192. outIndices[19] = vertexOffset + 3;
  193. outIndices[20] = vertexOffset + 1;
  194. outIndices[21] = vertexOffset + 7;
  195. outIndices[22] = vertexOffset + 1;
  196. outIndices[23] = vertexOffset + 5;
  197. // Top
  198. outIndices[24] = vertexOffset + 6;
  199. outIndices[25] = vertexOffset + 2;
  200. outIndices[26] = vertexOffset + 3;
  201. outIndices[27] = vertexOffset + 6;
  202. outIndices[28] = vertexOffset + 3;
  203. outIndices[29] = vertexOffset + 7;
  204. // Bottom
  205. outIndices[30] = vertexOffset + 5;
  206. outIndices[31] = vertexOffset + 1;
  207. outIndices[32] = vertexOffset + 0;
  208. outIndices[33] = vertexOffset + 5;
  209. outIndices[34] = vertexOffset + 0;
  210. outIndices[35] = vertexOffset + 4;
  211. }
  212. CM::Vector3 DrawHelper3D::calcCenter(UINT8* vertices, UINT32 numVertices, UINT32 vertexStride)
  213. {
  214. Vector3 center = Vector3::ZERO;
  215. for(UINT32 i = 0; i < numVertices; i++)
  216. {
  217. Vector3* curVert = (Vector3*)vertices;
  218. center += *curVert;
  219. vertices += vertexStride;
  220. }
  221. center /= (float)numVertices;
  222. return center;
  223. }
  224. void DrawHelper3D::line_AA(const CM::Vector3& a, const CM::Vector3& b, float width, float borderWidth, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors,
  225. CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
  226. {
  227. CM_EXCEPT(NotImplementedException, "3D AA line drawing not implemented.");
  228. }
  229. void DrawHelper3D::polygon_AA(const CM::Vector<Vector3>::type& points, float borderWidth, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors,
  230. CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
  231. {
  232. CM_EXCEPT(NotImplementedException, "3D AA polygon drawing not implemented.");
  233. }
  234. }