BsDrawHelper3D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "BsDrawHelper3D.h"
  2. #include "CmFRect.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, DOT_LINE_LIST);
  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);
  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>((UINT32)(linePoints.size() * 2), (UINT32)(linePoints.size() * 2), mVertexDesc, DOT_LINE_LIST);
  90. lineList_Pixel(linePoints, color, meshData, 0, 0);
  91. UINT8* positionData = meshData->getElementData(VES_POSITION);
  92. dbgCmd.worldCenter = calcCenter(positionData, meshData->getNumVertices(), mVertexDesc->getVertexStride());
  93. HMesh mesh = Mesh::create(meshData);
  94. dbgCmd.mesh = mesh;
  95. dbgCmd.type = DebugDrawType::WorldSpace;
  96. dbgCmd.matInfo3D = BuiltinMaterialManager::instance().createDebugDraw3DMaterial();
  97. }
  98. void DrawHelper3D::drawLineList_AA(const HCamera& camera, const CM::Vector<CM::Vector3>::type& linePoints, float width, float borderWidth,
  99. const CM::Color& color, float timeout)
  100. {
  101. const Viewport* viewport = camera->getViewport().get();
  102. Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
  103. commands.push_back(DebugDrawCommand());
  104. DebugDrawCommand& dbgCmd = commands.back();
  105. dbgCmd.endTime = gTime().getTime() + timeout;
  106. MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>((UINT32)(linePoints.size() * 4), (UINT32)(linePoints.size() * 15), mVertexDesc);
  107. lineList_AA(linePoints, width, borderWidth, color, meshData, 0, 0);
  108. UINT8* positionData = meshData->getElementData(VES_POSITION);
  109. dbgCmd.worldCenter = calcCenter(positionData, meshData->getNumVertices(), mVertexDesc->getVertexStride());
  110. HMesh mesh = Mesh::create(meshData);
  111. dbgCmd.mesh = mesh;
  112. dbgCmd.type = DebugDrawType::WorldSpace;
  113. dbgCmd.matInfo3D = BuiltinMaterialManager::instance().createDebugDraw3DMaterial();
  114. }
  115. void DrawHelper3D::drawAABox(const HCamera& camera, const CM::AABox& box, const CM::Color& color, float timeout)
  116. {
  117. const Viewport* viewport = camera->getViewport().get();
  118. Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
  119. commands.push_back(DebugDrawCommand());
  120. DebugDrawCommand& dbgCmd = commands.back();
  121. dbgCmd.endTime = gTime().getTime() + timeout;
  122. MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(8, 36, mVertexDesc);
  123. aabox(box, meshData, 0, 0);
  124. UINT32 vertexStride = mVertexDesc->getVertexStride();
  125. UINT8* colorData = meshData->getElementData(VES_COLOR);
  126. for(UINT32 i = 0; i < meshData->getNumVertices(); i++)
  127. {
  128. UINT32* colors = (UINT32*)colorData;
  129. (*colors) = color.getAsRGBA();
  130. colorData += vertexStride;
  131. }
  132. UINT8* positionData = meshData->getElementData(VES_POSITION);
  133. dbgCmd.worldCenter = calcCenter(positionData, meshData->getNumVertices(), mVertexDesc->getVertexStride());
  134. HMesh mesh = Mesh::create(meshData);
  135. dbgCmd.mesh = mesh;
  136. dbgCmd.type = DebugDrawType::WorldSpace;
  137. dbgCmd.matInfo3D = BuiltinMaterialManager::instance().createDebugDraw3DMaterial();
  138. }
  139. void DrawHelper3D::aabox(const AABox& box, UINT8* outVertices, UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
  140. {
  141. outVertices += (vertexOffset * vertexStride);
  142. Vector3 pt;
  143. pt = box.getCorner(AABox::FAR_LEFT_BOTTOM);
  144. memcpy(outVertices, &pt, sizeof(pt));
  145. outVertices += vertexStride;
  146. pt = box.getCorner(AABox::FAR_RIGHT_BOTTOM);
  147. memcpy(outVertices, &pt, sizeof(pt));
  148. outVertices += vertexStride;
  149. pt = box.getCorner(AABox::FAR_LEFT_TOP);
  150. memcpy(outVertices, &pt, sizeof(pt));
  151. outVertices += vertexStride;
  152. pt = box.getCorner(AABox::FAR_RIGHT_TOP);
  153. memcpy(outVertices, &pt, sizeof(pt));
  154. outVertices += vertexStride;
  155. pt = box.getCorner(AABox::NEAR_LEFT_BOTTOM);
  156. memcpy(outVertices, &pt, sizeof(pt));
  157. outVertices += vertexStride;
  158. pt = box.getCorner(AABox::NEAR_RIGHT_BOTTOM);
  159. memcpy(outVertices, &pt, sizeof(pt));
  160. outVertices += vertexStride;
  161. pt = box.getCorner(AABox::NEAR_LEFT_TOP);
  162. memcpy(outVertices, &pt, sizeof(pt));
  163. outVertices += vertexStride;
  164. pt = box.getCorner(AABox::NEAR_RIGHT_TOP);
  165. memcpy(outVertices, &pt, sizeof(pt));
  166. outVertices += vertexStride;
  167. outIndices += indexOffset;
  168. // Front
  169. outIndices[0] = vertexOffset + 6;
  170. outIndices[1] = vertexOffset + 7;
  171. outIndices[2] = vertexOffset + 5;
  172. outIndices[3] = vertexOffset + 6;
  173. outIndices[4] = vertexOffset + 5;
  174. outIndices[5] = vertexOffset + 4;
  175. // Back
  176. outIndices[6] = vertexOffset + 2;
  177. outIndices[7] = vertexOffset + 1;
  178. outIndices[8] = vertexOffset + 3;
  179. outIndices[9] = vertexOffset + 2;
  180. outIndices[10] = vertexOffset + 0;
  181. outIndices[11] = vertexOffset + 1;
  182. // Left
  183. outIndices[12] = vertexOffset + 2;
  184. outIndices[13] = vertexOffset + 6;
  185. outIndices[14] = vertexOffset + 4;
  186. outIndices[15] = vertexOffset + 2;
  187. outIndices[16] = vertexOffset + 4;
  188. outIndices[17] = vertexOffset + 0;
  189. // Right
  190. outIndices[18] = vertexOffset + 7;
  191. outIndices[19] = vertexOffset + 3;
  192. outIndices[20] = vertexOffset + 1;
  193. outIndices[21] = vertexOffset + 7;
  194. outIndices[22] = vertexOffset + 1;
  195. outIndices[23] = vertexOffset + 5;
  196. // Top
  197. outIndices[24] = vertexOffset + 6;
  198. outIndices[25] = vertexOffset + 2;
  199. outIndices[26] = vertexOffset + 3;
  200. outIndices[27] = vertexOffset + 6;
  201. outIndices[28] = vertexOffset + 3;
  202. outIndices[29] = vertexOffset + 7;
  203. // Bottom
  204. outIndices[30] = vertexOffset + 5;
  205. outIndices[31] = vertexOffset + 1;
  206. outIndices[32] = vertexOffset + 0;
  207. outIndices[33] = vertexOffset + 5;
  208. outIndices[34] = vertexOffset + 0;
  209. outIndices[35] = vertexOffset + 4;
  210. }
  211. CM::Vector3 DrawHelper3D::calcCenter(UINT8* vertices, UINT32 numVertices, UINT32 vertexStride)
  212. {
  213. Vector3 center = Vector3::ZERO;
  214. for(UINT32 i = 0; i < numVertices; i++)
  215. {
  216. Vector3* curVert = (Vector3*)vertices;
  217. center += *curVert;
  218. vertices += vertexStride;
  219. }
  220. center /= (float)numVertices;
  221. return center;
  222. }
  223. 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,
  224. CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
  225. {
  226. CM_EXCEPT(NotImplementedException, "3D AA line drawing not implemented.");
  227. }
  228. void DrawHelper3D::polygon_AA(const CM::Vector<Vector3>::type& points, float borderWidth, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors,
  229. CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
  230. {
  231. CM_EXCEPT(NotImplementedException, "3D AA polygon drawing not implemented.");
  232. }
  233. }