00001 // Filename: cullBinBackToFront.cxx 00002 // Created by: drose (28Feb02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "cullBinBackToFront.h" 00020 #include "graphicsStateGuardianBase.h" 00021 #include "geometricBoundingVolume.h" 00022 #include "cullableObject.h" 00023 #include "cullHandler.h" 00024 00025 #include <algorithm> 00026 00027 00028 TypeHandle CullBinBackToFront::_type_handle; 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Function: CullBinBackToFront::Destructor 00032 // Access: Public, Virtual 00033 // Description: 00034 //////////////////////////////////////////////////////////////////// 00035 CullBinBackToFront:: 00036 ~CullBinBackToFront() { 00037 Objects::iterator oi; 00038 for (oi = _objects.begin(); oi != _objects.end(); ++oi) { 00039 CullableObject *object = (*oi)._object; 00040 delete object; 00041 } 00042 } 00043 00044 //////////////////////////////////////////////////////////////////// 00045 // Function: CullBinBackToFront::add_object 00046 // Access: Public, Virtual 00047 // Description: Adds a geom, along with its associated state, to 00048 // the bin for rendering. 00049 //////////////////////////////////////////////////////////////////// 00050 void CullBinBackToFront:: 00051 add_object(CullableObject *object) { 00052 // Determine the center of the bounding volume. 00053 const BoundingVolume &volume = object->_geom->get_bound(); 00054 00055 if (!volume.is_empty() && 00056 volume.is_of_type(GeometricBoundingVolume::get_class_type())) { 00057 const GeometricBoundingVolume *gbv; 00058 DCAST_INTO_V(gbv, &volume); 00059 00060 LPoint3f center = gbv->get_approx_center(); 00061 nassertv(object->_transform != (const TransformState *)NULL); 00062 center = center * object->_transform->get_mat(); 00063 00064 float distance = _gsg->compute_distance_to(center); 00065 _objects.push_back(ObjectData(object, distance)); 00066 } 00067 } 00068 00069 //////////////////////////////////////////////////////////////////// 00070 // Function: CullBinBackToFront::finish_cull 00071 // Access: Public 00072 // Description: Called after all the geoms have been added, this 00073 // indicates that the cull process is finished for this 00074 // frame and gives the bins a chance to do any 00075 // post-processing (like sorting) before moving on to 00076 // draw. 00077 //////////////////////////////////////////////////////////////////// 00078 void CullBinBackToFront:: 00079 finish_cull() { 00080 sort(_objects.begin(), _objects.end()); 00081 } 00082 00083 //////////////////////////////////////////////////////////////////// 00084 // Function: CullBinBackToFront::draw 00085 // Access: Public 00086 // Description: Draws all the geoms in the bin, in the appropriate 00087 // order. 00088 //////////////////////////////////////////////////////////////////// 00089 void CullBinBackToFront:: 00090 draw() { 00091 Objects::const_iterator oi; 00092 for (oi = _objects.begin(); oi != _objects.end(); ++oi) { 00093 CullableObject *object = (*oi)._object; 00094 CullHandler::draw(object, _gsg); 00095 } 00096 } 00097