cullBinBackToFront.cxx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file cullBinBackToFront.cxx
  10. * @author drose
  11. * @date 2002-02-28
  12. */
  13. #include "cullBinBackToFront.h"
  14. #include "graphicsStateGuardianBase.h"
  15. #include "geometricBoundingVolume.h"
  16. #include "cullableObject.h"
  17. #include "cullHandler.h"
  18. #include "pStatTimer.h"
  19. #include <algorithm>
  20. TypeHandle CullBinBackToFront::_type_handle;
  21. /**
  22. * Factory constructor for passing to the CullBinManager.
  23. */
  24. CullBin *CullBinBackToFront::
  25. make_bin(const std::string &name, GraphicsStateGuardianBase *gsg,
  26. const PStatCollector &draw_region_pcollector) {
  27. return new CullBinBackToFront(name, gsg, draw_region_pcollector);
  28. }
  29. /**
  30. * Adds a geom, along with its associated state, to the bin for rendering.
  31. */
  32. void CullBinBackToFront::
  33. add_object(CullableObject *object, Thread *current_thread) {
  34. // Determine the center of the bounding volume.
  35. CPT(BoundingVolume) volume = object->_geom->get_bounds(current_thread);
  36. if (volume->is_empty()) {
  37. // No point in culling objects with no volume.
  38. return;
  39. }
  40. const GeometricBoundingVolume *gbv = volume->as_geometric_bounding_volume();
  41. nassertv(gbv != nullptr);
  42. LPoint3 center = gbv->get_approx_center();
  43. nassertv(object->_internal_transform != nullptr);
  44. center = center * object->_internal_transform->get_mat();
  45. PN_stdfloat distance = _gsg->compute_distance_to(center);
  46. _objects.push_back(ObjectData(object, distance));
  47. }
  48. /**
  49. * Called after all the geoms have been added, this indicates that the cull
  50. * process is finished for this frame and gives the bins a chance to do any
  51. * post-processing (like sorting) before moving on to draw.
  52. */
  53. void CullBinBackToFront::
  54. finish_cull(SceneSetup *, Thread *current_thread) {
  55. PStatTimer timer(_cull_this_pcollector, current_thread);
  56. sort(_objects.begin(), _objects.end());
  57. }
  58. /**
  59. * Draws all the geoms in the bin, in the appropriate order.
  60. */
  61. void CullBinBackToFront::
  62. draw(bool force, Thread *current_thread) {
  63. PStatTimer timer(_draw_this_pcollector, current_thread);
  64. for (const ObjectData &data : _objects) {
  65. data._object->draw(_gsg, force, current_thread);
  66. }
  67. }
  68. /**
  69. * Called by CullBin::make_result_graph() to add all the geoms to the special
  70. * cull result scene graph.
  71. */
  72. void CullBinBackToFront::
  73. fill_result_graph(CullBin::ResultGraphBuilder &builder) {
  74. Objects::const_iterator oi;
  75. for (oi = _objects.begin(); oi != _objects.end(); ++oi) {
  76. CullableObject *object = (*oi)._object;
  77. builder.add_object(object);
  78. }
  79. }