BsDrawHelperTemplate.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "BsDrawHelperTemplate.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 "BsCamera.h"
  11. #include "BsBuiltinMaterialManager.h"
  12. using namespace CamelotFramework;
  13. namespace BansheeEngine
  14. {
  15. void DrawHelperTemplateBase::render(const HCamera& camera, CM::RenderQueue& renderQueue)
  16. {
  17. const Viewport* viewport = camera->getViewport().get();
  18. Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
  19. Matrix4 projMatrixCstm = camera->getProjectionMatrix();
  20. Matrix4 viewMatrixCstm = camera->getViewMatrix();
  21. Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
  22. float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
  23. float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
  24. for(auto& cmd : commands)
  25. {
  26. if(cmd.mesh == nullptr || !cmd.mesh.isLoaded() || !cmd.mesh->isInitialized())
  27. continue;
  28. if(cmd.type == DebugDrawType::ClipSpace)
  29. {
  30. HMaterial mat = cmd.matInfo2DClipSpace.material;
  31. if(mat == nullptr || !mat.isLoaded() || !mat->isInitialized())
  32. continue;
  33. renderQueue.add(mat.getInternalPtr(), cmd.mesh.getInternalPtr(), 0, cmd.worldCenter);
  34. }
  35. else if(cmd.type == DebugDrawType::ScreenSpace)
  36. {
  37. HMaterial mat = cmd.matInfo2DScreenSpace.material;
  38. if(mat == nullptr || !mat.isLoaded() || !mat->isInitialized())
  39. continue;
  40. cmd.matInfo2DScreenSpace.invViewportWidth.set(invViewportWidth);
  41. cmd.matInfo2DScreenSpace.invViewportHeight.set(invViewportHeight);
  42. renderQueue.add(mat.getInternalPtr(), cmd.mesh.getInternalPtr(), 0, cmd.worldCenter);
  43. }
  44. else if(cmd.type == DebugDrawType::WorldSpace)
  45. {
  46. HMaterial mat = cmd.matInfo3D.material;
  47. if(mat == nullptr || !mat.isLoaded() || !mat->isInitialized())
  48. continue;
  49. cmd.matInfo3D.matViewProj.set(viewProjMatrix);
  50. renderQueue.add(mat.getInternalPtr(), cmd.mesh.getInternalPtr(), 0, cmd.worldCenter);
  51. }
  52. }
  53. float curTime = gTime().getTime();
  54. Vector<DebugDrawCommand>::type newCommands;
  55. for(auto& cmd : commands)
  56. {
  57. if(cmd.endTime > curTime)
  58. newCommands.push_back(cmd);
  59. }
  60. commands.swap(newCommands);
  61. }
  62. }