Panda3D

cullBinFrontToBack.cxx
Go to the documentation of this file.
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 cullBinFrontToBack.cxx
10 * @author drose
11 * @date 2002-05-29
12 */
13
14 #include "cullBinFrontToBack.h"
15 #include "graphicsStateGuardianBase.h"
16 #include "geometricBoundingVolume.h"
17 #include "cullableObject.h"
18 #include "cullHandler.h"
19 #include "pStatTimer.h"
20
21 #include <algorithm>
22
23
24 TypeHandle CullBinFrontToBack::_type_handle;
25
26 /**
27 *
28 */
29 CullBinFrontToBack::
30 ~CullBinFrontToBack() {
31 Objects::iterator oi;
32 for (oi = _objects.begin(); oi != _objects.end(); ++oi) {
33 CullableObject *object = (*oi)._object;
34 delete object;
35 }
36 }
37
38 /**
39 * Factory constructor for passing to the CullBinManager.
40 */
41 CullBin *CullBinFrontToBack::
42 make_bin(const std::string &name, GraphicsStateGuardianBase *gsg,
43 const PStatCollector &draw_region_pcollector) {
44 return new CullBinFrontToBack(name, gsg, draw_region_pcollector);
45 }
46
47 /**
48 * Adds a geom, along with its associated state, to the bin for rendering.
49 */
50 void CullBinFrontToBack::
51 add_object(CullableObject *object, Thread *current_thread) {
52 // Determine the center of the bounding volume.
53 CPT(BoundingVolume) volume = object->_geom->get_bounds();
54 if (volume->is_empty()) {
55 delete object;
56 return;
57 }
58
59 const GeometricBoundingVolume *gbv = volume->as_geometric_bounding_volume();
60 nassertv(gbv != nullptr);
61
62 LPoint3 center = gbv->get_approx_center();
63 nassertv(object->_internal_transform != nullptr);
64 center = center * object->_internal_transform->get_mat();
65
66 PN_stdfloat distance = _gsg->compute_distance_to(center);
67 _objects.push_back(ObjectData(object, distance));
68 }
69
70 /**
71 * Called after all the geoms have been added, this indicates that the cull
72 * process is finished for this frame and gives the bins a chance to do any
73 * post-processing (like sorting) before moving on to draw.
74 */
75 void CullBinFrontToBack::
76 finish_cull(SceneSetup *, Thread *current_thread) {
77 PStatTimer timer(_cull_this_pcollector, current_thread);
78 sort(_objects.begin(), _objects.end());
79 }
80
81 /**
82 * Draws all the geoms in the bin, in the appropriate order.
83 */
84 void CullBinFrontToBack::
85 draw(bool force, Thread *current_thread) {
86 PStatTimer timer(_draw_this_pcollector, current_thread);
87
88 Objects::const_iterator oi;
89 for (oi = _objects.begin(); oi != _objects.end(); ++oi) {
90 CullableObject *object = (*oi)._object;
91
92 if (object->_draw_callback == nullptr) {
93 nassertd(object->_geom != nullptr) continue;
94
95 _gsg->set_state_and_transform(object->_state, object->_internal_transform);
96
97 GeomPipelineReader geom_reader(object->_geom, current_thread);
98 GeomVertexDataPipelineReader data_reader(object->_munged_data, current_thread);
99 data_reader.check_array_readers();
100 geom_reader.draw(_gsg, &data_reader, force);
101 } else {
102 // It has a callback associated.
103 object->draw_callback(_gsg, force, current_thread);
104 // Now the callback has taken care of drawing.
105 }
106 }
107 }
108
109 /**
110 * Called by CullBin::make_result_graph() to add all the geoms to the special
111 * cull result scene graph.
112 */
113 void CullBinFrontToBack::
114 fill_result_graph(CullBin::ResultGraphBuilder &builder) {
115 Objects::const_iterator oi;
116 for (oi = _objects.begin(); oi != _objects.end(); ++oi) {
117 CullableObject *object = (*oi)._object;
118 builder.add_object(object);
119 }
120 }
BoundingVolume
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
Definition: boundingVolume.h:41
CullBinFrontToBack
A specific kind of CullBin that sorts geometry in order from nearest to furthest based on the center ...
Definition: cullBinFrontToBack.h:32
CullBinFrontToBack::make_bin
static CullBin * make_bin(const std::string &name, GraphicsStateGuardianBase *gsg, const PStatCollector &draw_region_pcollector)
Factory constructor for passing to the CullBinManager.
Definition: cullBinFrontToBack.cxx:42
CullBinFrontToBack::draw
virtual void draw(bool force, Thread *current_thread)
Draws all the geoms in the bin, in the appropriate order.
Definition: cullBinFrontToBack.cxx:85
CullBinFrontToBack::add_object
virtual void add_object(CullableObject *object, Thread *current_thread)
Adds a geom, along with its associated state, to the bin for rendering.
Definition: cullBinFrontToBack.cxx:51
CullBinFrontToBack::finish_cull
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...
Definition: cullBinFrontToBack.cxx:76
CullBin
A collection of Geoms and their associated state, for a particular scene.
Definition: cullBin.h:40
CullableObject
The smallest atom of cull.
Definition: cullableObject.h:41
GeomPipelineReader
Encapsulates the data from a Geom, pre-fetched for one stage of the pipeline.
Definition: geom.h:405
GeomPipelineReader::draw
bool draw(GraphicsStateGuardianBase *gsg, const GeomVertexDataPipelineReader *data_reader, bool force) const
The implementation of Geom::draw().
Definition: geom.cxx:1849
GeomVertexDataPipelineReader
Encapsulates the data from a GeomVertexData, pre-fetched for one stage of the pipeline.
Definition: geomVertexData.h:442
GeometricBoundingVolume
This is another abstract class, for a general class of bounding volumes that actually enclose points ...
Definition: geometricBoundingVolume.h:29
GeometricBoundingVolume::as_geometric_bounding_volume
virtual GeometricBoundingVolume * as_geometric_bounding_volume() final
Virtual downcast method.
Definition: geometricBoundingVolume.cxx:24
GraphicsStateGuardianBase
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
Definition: graphicsStateGuardianBase.h:110
PStatCollector
A lightweight class that represents a single element that may be timed and/or counted via stats.
Definition: pStatCollector.h:43
PStatTimer
A lightweight class that can be used to automatically start and stop a PStatCollector around a sectio...
Definition: pStatTimer.h:30
SceneSetup
This object holds the camera position, etc., and other general setup information for rendering a part...
Definition: sceneSetup.h:32
Thread
A thread; that is, a lightweight process.
Definition: thread.h:46
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
cullBinFrontToBack.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
cullHandler.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
cullableObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
geometricBoundingVolume.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
graphicsStateGuardianBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pStatTimer.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.