meshRenderSystem.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "T3D/systems/render/meshRenderSystem.h"
  2. #include "gfx/gfxTransformSaver.h"
  3. #include "lighting/lightQuery.h"
  4. #include "renderInstance/renderPassManager.h"
  5. #include "materials/materialManager.h"
  6. #include "materials/baseMatInstance.h"
  7. void MeshRenderSystem::render(SceneManager *sceneManager, SceneRenderState* state)
  8. {
  9. if (sceneManager == nullptr || state == nullptr)
  10. return;
  11. Frustum viewFrustum = state->getCullingFrustum();
  12. MatrixF camTransform = state->getCameraTransform();
  13. U32 count = MeshRenderSystemInterface::all.size();
  14. for (U32 i = 0; i < count; i++)
  15. {
  16. //Server side items exist for data, but we don't actually render them
  17. if (!MeshRenderSystemInterface::all[i]->mIsClient)
  18. continue;
  19. //First, do frustum culling
  20. if (viewFrustum.isCulled(MeshRenderSystemInterface::all[i]->mBounds))
  21. continue;
  22. // Set the query box for the container query. Never
  23. // make it larger than the frustum's AABB. In the editor,
  24. // always query the full frustum as that gives objects
  25. // the opportunity to render editor visualizations even if
  26. // they are otherwise not in view.
  27. if (!state->getCullingFrustum().getBounds().isOverlapped(state->getRenderArea()))
  28. {
  29. // This handles fringe cases like flying backwards into a zone where you
  30. // end up pretty much standing on a zone border and looking directly into
  31. // its "walls". In that case the traversal area will be behind the frustum
  32. // (remember that the camera isn't where visibility starts, it's the near
  33. // distance).
  34. continue;
  35. }
  36. //We can then sort our objects by range since we have it already, so we can do occlusion culling be rendering front-to-back
  37. //if we've made it this far, call down to the render function to actually display our stuff
  38. renderInterface(i, state);
  39. }
  40. }
  41. void MeshRenderSystem::renderInterface(U32 interfaceIndex, SceneRenderState* state)
  42. {
  43. //Fetch
  44. MeshRenderSystemInterface* interface = MeshRenderSystemInterface::all[interfaceIndex];
  45. if (interface->mShapeInstance == nullptr)
  46. return;
  47. Point3F cameraOffset;
  48. interface->mTransform.getColumn(3, &cameraOffset);
  49. cameraOffset -= state->getDiffuseCameraPosition();
  50. F32 dist = cameraOffset.len();
  51. if (dist < 0.01f)
  52. dist = 0.01f;
  53. Point3F objScale = interface->mScale;
  54. F32 invScale = (1.0f / getMax(getMax(objScale.x, objScale.y), objScale.z));
  55. interface->mShapeInstance->setDetailFromDistance(state, dist * invScale);
  56. if (interface->mShapeInstance->getCurrentDetail() < 0)
  57. return;
  58. GFXTransformSaver saver;
  59. // Set up our TS render state.
  60. TSRenderState rdata;
  61. rdata.setSceneState(state);
  62. rdata.setFadeOverride(1.0f);
  63. rdata.setOriginSort(false);
  64. // We might have some forward lit materials
  65. // so pass down a query to gather lights.
  66. LightQuery query;
  67. query.init(interface->mSphere);
  68. rdata.setLightQuery(&query);
  69. MatrixF mat = interface->mTransform;
  70. mat.scale(objScale);
  71. GFX->setWorldMatrix(mat);
  72. interface->mShapeInstance->render(rdata);
  73. }