VisibilityTester.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "VisibilityTester.h"
  2. #include "Scene.h"
  3. #include "ModelNode.h"
  4. #include "ModelNodePatch.h"
  5. #include "Material.h"
  6. #include "Sphere.h"
  7. #include "PointLight.h"
  8. #include "SpotLight.h"
  9. //======================================================================================================================
  10. // CmpLength::operator() =
  11. //======================================================================================================================
  12. inline bool VisibilityTester::CmpLength::operator()(const SceneRenderable* a, const SceneRenderable* b) const
  13. {
  14. return (a->getWorldTransform().origin - o).getLengthSquared() < (b->getWorldTransform().origin - o).getLengthSquared();
  15. }
  16. //======================================================================================================================
  17. // Constructor =
  18. //======================================================================================================================
  19. VisibilityTester::VisibilityTester(const Scene& scene_):
  20. scene(scene_)
  21. {}
  22. //======================================================================================================================
  23. // test =
  24. //======================================================================================================================
  25. void VisibilityTester::test(const Camera& cam)
  26. {
  27. //
  28. // Clean
  29. //
  30. msRenderables.clear();
  31. bsRenderables.clear();
  32. pointLights.clear();
  33. spotLights.clear();
  34. //
  35. // Collect the lights
  36. //
  37. Scene::Types<Light>::ConstIterator itl = scene.getLights().begin();
  38. for(; itl != scene.getLights().end(); itl++)
  39. {
  40. const Light& light = *(*itl);
  41. // Point
  42. switch(light.getType())
  43. {
  44. case Light::LT_POINT:
  45. {
  46. const PointLight& pointl = static_cast<const PointLight&>(light);
  47. Sphere sphere(pointl.getWorldTransform().origin, pointl.getRadius());
  48. if(cam.insideFrustum(sphere))
  49. {
  50. pointLights.push_back(VisibleLight<PointLight>());
  51. pointLights.back().light = &pointl;
  52. }
  53. break;
  54. }
  55. // Spot
  56. case Light::LT_SPOT:
  57. {
  58. const SpotLight& spotl = static_cast<const SpotLight&>(light);
  59. if(cam.insideFrustum(spotl.getCamera()))
  60. {
  61. spotLights.push_back(VisibleLight<SpotLight>());
  62. spotLights.back().light = &spotl;
  63. }
  64. break;
  65. }
  66. }
  67. }
  68. //
  69. // Collect the renderables
  70. //
  71. Scene::Types<ModelNode>::ConstIterator it = scene.getModelNodes().begin();
  72. for(; it != scene.getModelNodes().end(); it++)
  73. {
  74. boost::ptr_vector<ModelNodePatch>::const_iterator it2 = (*it)->getModelNodePatches().begin();
  75. for(; it2 != (*it)->getModelNodePatches().end(); it2++)
  76. {
  77. const ModelNodePatch& modelNodePatch = *it2;
  78. // First check if its rendered by a light
  79. Types<VisibleLight<SpotLight> >::Iterator itsl = spotLights.begin();
  80. for(; itsl != spotLights.end(); itsl++)
  81. {
  82. const SpotLight& spotLight = *(itsl->light);
  83. if(test(modelNodePatch, spotLight.getCamera()))
  84. {
  85. itsl->renderables.push_back(&modelNodePatch);
  86. }
  87. }
  88. /// @todo Perform real tests
  89. if(test(modelNodePatch, cam))
  90. {
  91. if(modelNodePatch.getCpMtl().isBlendingEnabled())
  92. {
  93. bsRenderables.push_back(&modelNodePatch);
  94. }
  95. else
  96. {
  97. msRenderables.push_back(&modelNodePatch);
  98. }
  99. }
  100. }
  101. }
  102. //
  103. // Sort the renderables from closest to the camera to the farthest
  104. //
  105. std::sort(msRenderables.begin(), msRenderables.end(), CmpLength(cam.getWorldTransform().origin));
  106. std::sort(bsRenderables.begin(), bsRenderables.end(), CmpLength(cam.getWorldTransform().origin));
  107. //
  108. // Short the light's renderables
  109. //
  110. Types<VisibleLight<SpotLight> >::Iterator itsl = spotLights.begin();
  111. for(; itsl != spotLights.end(); itsl++)
  112. {
  113. std::sort(itsl->renderables.begin(), itsl->renderables.end(), CmpLength(itsl->light->getWorldTransform().origin));
  114. }
  115. }