Panda3D
cullBinBackToFront.cxx
1 // Filename: cullBinBackToFront.cxx
2 // Created by: drose (28Feb02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "cullBinBackToFront.h"
16 #include "graphicsStateGuardianBase.h"
17 #include "geometricBoundingVolume.h"
18 #include "cullableObject.h"
19 #include "cullHandler.h"
20 #include "pStatTimer.h"
21 
22 #include <algorithm>
23 
24 
25 TypeHandle CullBinBackToFront::_type_handle;
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: CullBinBackToFront::Destructor
29 // Access: Public, Virtual
30 // Description:
31 ////////////////////////////////////////////////////////////////////
32 CullBinBackToFront::
33 ~CullBinBackToFront() {
34  Objects::iterator oi;
35  for (oi = _objects.begin(); oi != _objects.end(); ++oi) {
36  CullableObject *object = (*oi)._object;
37  delete object;
38  }
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: CullBinBackToFront::make_bin
43 // Access: Public, Static
44 // Description: Factory constructor for passing to the CullBinManager.
45 ////////////////////////////////////////////////////////////////////
47 make_bin(const string &name, GraphicsStateGuardianBase *gsg,
48  const PStatCollector &draw_region_pcollector) {
49  return new CullBinBackToFront(name, gsg, draw_region_pcollector);
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: CullBinBackToFront::add_object
54 // Access: Public, Virtual
55 // Description: Adds a geom, along with its associated state, to
56 // the bin for rendering.
57 ////////////////////////////////////////////////////////////////////
59 add_object(CullableObject *object, Thread *current_thread) {
60  // Determine the center of the bounding volume.
61  CPT(BoundingVolume) volume = object->_geom->get_bounds(current_thread);
62  if (volume->is_empty()) {
63  delete object;
64  return;
65  }
66 
67  const GeometricBoundingVolume *gbv;
68  DCAST_INTO_V(gbv, volume);
69 
70  LPoint3 center = gbv->get_approx_center();
71  nassertv(object->_internal_transform != (const TransformState *)NULL);
72  center = center * object->_internal_transform->get_mat();
73 
74  PN_stdfloat distance = _gsg->compute_distance_to(center);
75  _objects.push_back(ObjectData(object, distance));
76 }
77 
78 ////////////////////////////////////////////////////////////////////
79 // Function: CullBinBackToFront::finish_cull
80 // Access: Public
81 // Description: Called after all the geoms have been added, this
82 // indicates that the cull process is finished for this
83 // frame and gives the bins a chance to do any
84 // post-processing (like sorting) before moving on to
85 // draw.
86 ////////////////////////////////////////////////////////////////////
88 finish_cull(SceneSetup *, Thread *current_thread) {
89  PStatTimer timer(_cull_this_pcollector, current_thread);
90  sort(_objects.begin(), _objects.end());
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: CullBinBackToFront::draw
95 // Access: Public, Virtual
96 // Description: Draws all the geoms in the bin, in the appropriate
97 // order.
98 ////////////////////////////////////////////////////////////////////
100 draw(bool force, Thread *current_thread) {
101  PStatTimer timer(_draw_this_pcollector, current_thread);
102  Objects::const_iterator oi;
103  for (oi = _objects.begin(); oi != _objects.end(); ++oi) {
104  CullableObject *object = (*oi)._object;
105  CullHandler::draw(object, _gsg, force, current_thread);
106  }
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: CullBinBackToFront::fill_result_graph
111 // Access: Protected, Virtual
112 // Description: Called by CullBin::make_result_graph() to add all the
113 // geoms to the special cull result scene graph.
114 ////////////////////////////////////////////////////////////////////
115 void CullBinBackToFront::
116 fill_result_graph(CullBin::ResultGraphBuilder &builder) {
117  Objects::const_iterator oi;
118  for (oi = _objects.begin(); oi != _objects.end(); ++oi) {
119  CullableObject *object = (*oi)._object;
120  builder.add_object(object);
121  }
122 }
virtual void finish_cull(SceneSetup *scene_setup, Thread *current_thread)
Called after all the geoms have been added, this indicates that the cull process is finished for this...
A collection of Geoms and their associated state, for a particular scene.
Definition: cullBin.h:44
A lightweight class that can be used to automatically start and stop a PStatCollector around a sectio...
Definition: pStatTimer.h:34
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
A lightweight class that represents a single element that may be timed and/or counted via stats...
This is another abstract class, for a general class of bounding volumes that actually enclose points ...
static void draw(CullableObject *object, GraphicsStateGuardianBase *gsg, bool force, Thread *current_thread)
Draws the indicated CullableObject, with full support for decals if they are attached to the object...
Definition: cullHandler.I:24
The smallest atom of cull.
static CullBin * make_bin(const string &name, GraphicsStateGuardianBase *gsg, const PStatCollector &draw_region_pcollector)
Factory constructor for passing to the CullBinManager.
A specific kind of CullBin that sorts geometry in order from furthest to nearest based on the center ...
virtual void draw(bool force, Thread *current_thread)
Draws all the geoms in the bin, in the appropriate order.
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
A thread; that is, a lightweight process.
Definition: thread.h:51
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This object holds the camera position, etc., and other general setup information for rendering a part...
Definition: sceneSetup.h:35
virtual void add_object(CullableObject *object, Thread *current_thread)
Adds a geom, along with its associated state, to the bin for rendering.