diff --git a/panda/src/pgraph/Sources.pp b/panda/src/pgraph/Sources.pp index ebbfdcf664..a9fce1e39e 100644 --- a/panda/src/pgraph/Sources.pp +++ b/panda/src/pgraph/Sources.pp @@ -22,6 +22,8 @@ cullBin.I cullBin.h \ cullBinAttrib.I cullBinAttrib.h \ cullBinBackToFront.I cullBinBackToFront.h \ + cullBinFixed.I cullBinFixed.h \ + cullBinFrontToBack.I cullBinFrontToBack.h \ cullBinManager.I cullBinManager.h \ cullBinUnsorted.I cullBinUnsorted.h \ cullFaceAttrib.I cullFaceAttrib.h \ @@ -99,6 +101,8 @@ cullBin.cxx \ cullBinAttrib.cxx \ cullBinBackToFront.cxx \ + cullBinFixed.cxx \ + cullBinFrontToBack.cxx \ cullBinManager.cxx \ cullBinUnsorted.cxx \ cullFaceAttrib.cxx \ @@ -175,6 +179,8 @@ cullBin.I cullBin.h \ cullBinAttrib.I cullBinAttrib.h \ cullBinBackToFront.I cullBinBackToFront.h \ + cullBinFixed.I cullBinFixed.h \ + cullBinFrontToBack.I cullBinFrontToBack.h \ cullBinManager.I cullBinManager.h \ cullBinUnsorted.I cullBinUnsorted.h \ cullFaceAttrib.I cullFaceAttrib.h \ diff --git a/panda/src/pgraph/config_pgraph.cxx b/panda/src/pgraph/config_pgraph.cxx index 78c596584b..d64653d389 100644 --- a/panda/src/pgraph/config_pgraph.cxx +++ b/panda/src/pgraph/config_pgraph.cxx @@ -30,6 +30,8 @@ #include "cullBin.h" #include "cullBinAttrib.h" #include "cullBinBackToFront.h" +#include "cullBinFixed.h" +#include "cullBinFrontToBack.h" #include "cullBinUnsorted.h" #include "cullTraverser.h" #include "cullableObject.h" @@ -150,6 +152,8 @@ init_libpgraph() { CullBin::init_type(); CullBinAttrib::init_type(); CullBinBackToFront::init_type(); + CullBinFixed::init_type(); + CullBinFrontToBack::init_type(); CullBinUnsorted::init_type(); CullTraverser::init_type(); CullableObject::init_type(); diff --git a/panda/src/pgraph/cullBinAttrib.cxx b/panda/src/pgraph/cullBinAttrib.cxx index 2049998eee..eaf3ce8d0c 100644 --- a/panda/src/pgraph/cullBinAttrib.cxx +++ b/panda/src/pgraph/cullBinAttrib.cxx @@ -77,6 +77,9 @@ int CullBinAttrib:: compare_to_impl(const RenderAttrib *other) const { const CullBinAttrib *ta; DCAST_INTO_R(ta, other, 0); + if (_draw_order != ta->_draw_order) { + return _draw_order - ta->_draw_order; + } return strcmp(_bin_name.c_str(), ta->_bin_name.c_str()); } diff --git a/panda/src/pgraph/cullBinFixed.I b/panda/src/pgraph/cullBinFixed.I new file mode 100644 index 0000000000..f4d26f62af --- /dev/null +++ b/panda/src/pgraph/cullBinFixed.I @@ -0,0 +1,53 @@ +// Filename: cullBinFixed.I +// Created by: drose (29May02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFixed::ObjectData::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE CullBinFixed::ObjectData:: +ObjectData(CullableObject *object, int draw_order) : + _object(object), + _draw_order(draw_order) +{ +} + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFixed::ObjectData::operator < +// Access: Public +// Description: Specifies the correct sort ordering for these +// objects. +//////////////////////////////////////////////////////////////////// +INLINE bool CullBinFixed::ObjectData:: +operator < (const ObjectData &other) const { + return _draw_order < other._draw_order; +} + + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFixed::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE CullBinFixed:: +CullBinFixed(GraphicsStateGuardianBase *gsg) : + CullBin(gsg) +{ +} diff --git a/panda/src/pgraph/cullBinFixed.cxx b/panda/src/pgraph/cullBinFixed.cxx new file mode 100644 index 0000000000..d0cc032d7f --- /dev/null +++ b/panda/src/pgraph/cullBinFixed.cxx @@ -0,0 +1,97 @@ +// Filename: cullBinFixed.cxx +// Created by: drose (29May02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#include "cullBinFixed.h" +#include "graphicsStateGuardianBase.h" +#include "geometricBoundingVolume.h" +#include "cullableObject.h" +#include "cullHandler.h" + +#include + + +TypeHandle CullBinFixed::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFixed::Destructor +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +CullBinFixed:: +~CullBinFixed() { + Objects::iterator oi; + for (oi = _objects.begin(); oi != _objects.end(); ++oi) { + CullableObject *object = (*oi)._object; + delete object; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFixed::add_object +// Access: Public, Virtual +// Description: Adds a geom, along with its associated state, to +// the bin for rendering. +//////////////////////////////////////////////////////////////////// +void CullBinFixed:: +add_object(CullableObject *object) { + // Determine the center of the bounding volume. + const BoundingVolume &volume = object->_geom->get_bound(); + + if (!volume.is_empty() && + volume.is_of_type(GeometricBoundingVolume::get_class_type())) { + const GeometricBoundingVolume *gbv; + DCAST_INTO_V(gbv, &volume); + + LPoint3f center = gbv->get_approx_center(); + nassertv(object->_transform != (const TransformState *)NULL); + center = center * object->_transform->get_mat(); + + int draw_order = object->_state->get_draw_order(); + _objects.push_back(ObjectData(object, draw_order)); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFixed::finish_cull +// Access: Public +// Description: Called after all the geoms have been added, this +// indicates that the cull process is finished for this +// frame and gives the bins a chance to do any +// post-processing (like sorting) before moving on to +// draw. +//////////////////////////////////////////////////////////////////// +void CullBinFixed:: +finish_cull() { + stable_sort(_objects.begin(), _objects.end()); +} + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFixed::draw +// Access: Public +// Description: Draws all the geoms in the bin, in the appropriate +// order. +//////////////////////////////////////////////////////////////////// +void CullBinFixed:: +draw() { + Objects::const_iterator oi; + for (oi = _objects.begin(); oi != _objects.end(); ++oi) { + CullableObject *object = (*oi)._object; + CullHandler::draw(object, _gsg); + } +} + diff --git a/panda/src/pgraph/cullBinFixed.h b/panda/src/pgraph/cullBinFixed.h new file mode 100644 index 0000000000..4050b933ff --- /dev/null +++ b/panda/src/pgraph/cullBinFixed.h @@ -0,0 +1,86 @@ +// Filename: cullBinFixed.h +// Created by: drose (29May02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#ifndef CULLBINFIXED_H +#define CULLBINFIXED_H + +#include "pandabase.h" + +#include "cullBin.h" +#include "geom.h" +#include "transformState.h" +#include "renderState.h" +#include "pointerTo.h" + +//////////////////////////////////////////////////////////////////// +// Class : CullBinFixed +// Description : A specific kind of CullBin that sorts geometry in +// the order specified by the user-specified draw_order +// parameter. This allows precise relative ordering of +// two objects. +// +// When two or more objects are assigned the same +// draw_order, they are drawn in scene-graph order (as +// with CullBinUnsorted). +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDA CullBinFixed : public CullBin { +public: + INLINE CullBinFixed(GraphicsStateGuardianBase *gsg); + virtual ~CullBinFixed(); + + virtual void add_object(CullableObject *object); + virtual void finish_cull(); + virtual void draw(); + +private: + class ObjectData { + public: + INLINE ObjectData(CullableObject *object, int draw_order); + INLINE bool operator < (const ObjectData &other) const; + + CullableObject *_object; + int _draw_order; + }; + + typedef pvector Objects; + Objects _objects; + +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + CullBin::init_type(); + register_type(_type_handle, "CullBinFixed", + CullBin::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() {init_type(); return get_class_type();} + +private: + static TypeHandle _type_handle; +}; + +#include "cullBinFixed.I" + +#endif + + + diff --git a/panda/src/pgraph/cullBinFrontToBack.I b/panda/src/pgraph/cullBinFrontToBack.I new file mode 100644 index 0000000000..d34aedd79e --- /dev/null +++ b/panda/src/pgraph/cullBinFrontToBack.I @@ -0,0 +1,53 @@ +// Filename: cullBinFrontToBack.I +// Created by: drose (29May02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFrontToBack::ObjectData::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE CullBinFrontToBack::ObjectData:: +ObjectData(CullableObject *object, float dist) : + _object(object), + _dist(dist) +{ +} + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFrontToBack::ObjectData::operator < +// Access: Public +// Description: Specifies the correct sort ordering for these +// objects. +//////////////////////////////////////////////////////////////////// +INLINE bool CullBinFrontToBack::ObjectData:: +operator < (const ObjectData &other) const { + return _dist < other._dist; +} + + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFrontToBack::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE CullBinFrontToBack:: +CullBinFrontToBack(GraphicsStateGuardianBase *gsg) : + CullBin(gsg) +{ +} diff --git a/panda/src/pgraph/cullBinFrontToBack.cxx b/panda/src/pgraph/cullBinFrontToBack.cxx new file mode 100644 index 0000000000..7151899c38 --- /dev/null +++ b/panda/src/pgraph/cullBinFrontToBack.cxx @@ -0,0 +1,97 @@ +// Filename: cullBinFrontToBack.cxx +// Created by: drose (29May02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#include "cullBinFrontToBack.h" +#include "graphicsStateGuardianBase.h" +#include "geometricBoundingVolume.h" +#include "cullableObject.h" +#include "cullHandler.h" + +#include + + +TypeHandle CullBinFrontToBack::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFrontToBack::Destructor +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +CullBinFrontToBack:: +~CullBinFrontToBack() { + Objects::iterator oi; + for (oi = _objects.begin(); oi != _objects.end(); ++oi) { + CullableObject *object = (*oi)._object; + delete object; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFrontToBack::add_object +// Access: Public, Virtual +// Description: Adds a geom, along with its associated state, to +// the bin for rendering. +//////////////////////////////////////////////////////////////////// +void CullBinFrontToBack:: +add_object(CullableObject *object) { + // Determine the center of the bounding volume. + const BoundingVolume &volume = object->_geom->get_bound(); + + if (!volume.is_empty() && + volume.is_of_type(GeometricBoundingVolume::get_class_type())) { + const GeometricBoundingVolume *gbv; + DCAST_INTO_V(gbv, &volume); + + LPoint3f center = gbv->get_approx_center(); + nassertv(object->_transform != (const TransformState *)NULL); + center = center * object->_transform->get_mat(); + + float distance = _gsg->compute_distance_to(center); + _objects.push_back(ObjectData(object, distance)); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFrontToBack::finish_cull +// Access: Public +// Description: Called after all the geoms have been added, this +// indicates that the cull process is finished for this +// frame and gives the bins a chance to do any +// post-processing (like sorting) before moving on to +// draw. +//////////////////////////////////////////////////////////////////// +void CullBinFrontToBack:: +finish_cull() { + sort(_objects.begin(), _objects.end()); +} + +//////////////////////////////////////////////////////////////////// +// Function: CullBinFrontToBack::draw +// Access: Public +// Description: Draws all the geoms in the bin, in the appropriate +// order. +//////////////////////////////////////////////////////////////////// +void CullBinFrontToBack:: +draw() { + Objects::const_iterator oi; + for (oi = _objects.begin(); oi != _objects.end(); ++oi) { + CullableObject *object = (*oi)._object; + CullHandler::draw(object, _gsg); + } +} + diff --git a/panda/src/pgraph/cullBinFrontToBack.h b/panda/src/pgraph/cullBinFrontToBack.h new file mode 100644 index 0000000000..c4444335d5 --- /dev/null +++ b/panda/src/pgraph/cullBinFrontToBack.h @@ -0,0 +1,84 @@ +// Filename: cullBinFrontToBack.h +// Created by: drose (29May02) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#ifndef CULLBINFRONTTOBACK_H +#define CULLBINFRONTTOBACK_H + +#include "pandabase.h" + +#include "cullBin.h" +#include "geom.h" +#include "transformState.h" +#include "renderState.h" +#include "pointerTo.h" + +//////////////////////////////////////////////////////////////////// +// Class : CullBinFrontToBack +// Description : A specific kind of CullBin that sorts geometry in +// order from nearest to furthest based on the center of +// its bounding volume. +// +// This is useful for rendering opaque geometry, taking +// optimal advantage of a hierarchical Z-buffer. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDA CullBinFrontToBack : public CullBin { +public: + INLINE CullBinFrontToBack(GraphicsStateGuardianBase *gsg); + virtual ~CullBinFrontToBack(); + + virtual void add_object(CullableObject *object); + virtual void finish_cull(); + virtual void draw(); + +private: + class ObjectData { + public: + INLINE ObjectData(CullableObject *object, float dist); + INLINE bool operator < (const ObjectData &other) const; + + CullableObject *_object; + float _dist; + }; + + typedef pvector Objects; + Objects _objects; + +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + CullBin::init_type(); + register_type(_type_handle, "CullBinFrontToBack", + CullBin::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() {init_type(); return get_class_type();} + +private: + static TypeHandle _type_handle; +}; + +#include "cullBinFrontToBack.I" + +#endif + + + diff --git a/panda/src/pgraph/cullBinManager.cxx b/panda/src/pgraph/cullBinManager.cxx index 5639837972..656adebd6f 100644 --- a/panda/src/pgraph/cullBinManager.cxx +++ b/panda/src/pgraph/cullBinManager.cxx @@ -18,6 +18,8 @@ #include "cullBinManager.h" #include "cullBinBackToFront.h" +#include "cullBinFrontToBack.h" +#include "cullBinFixed.h" #include "cullBinUnsorted.h" #include "renderState.h" #include "cullResult.h" @@ -203,6 +205,12 @@ make_new_bin(int bin_index, GraphicsStateGuardianBase *gsg) { case BT_back_to_front: return new CullBinBackToFront(gsg); + case BT_front_to_back: + return new CullBinFrontToBack(gsg); + + case BT_fixed: + return new CullBinFixed(gsg); + default: return new CullBinUnsorted(gsg); } @@ -313,6 +321,12 @@ parse_bin_type(const string &bin_type) { } else if (cmp_nocase_uh(bin_type, "backtofront") == 0) { return BT_back_to_front; + } else if (cmp_nocase_uh(bin_type, "front_to_back") == 0) { + return BT_front_to_back; + + } else if (cmp_nocase_uh(bin_type, "fronttoback") == 0) { + return BT_front_to_back; + } else { return BT_invalid; } diff --git a/panda/src/pgraph/cullBinManager.h b/panda/src/pgraph/cullBinManager.h index 16e154f2b4..bfad020912 100644 --- a/panda/src/pgraph/cullBinManager.h +++ b/panda/src/pgraph/cullBinManager.h @@ -45,6 +45,7 @@ PUBLISHED: BT_unsorted, BT_state_sorted, BT_back_to_front, + BT_front_to_back, BT_fixed, }; diff --git a/panda/src/pgraph/nodePath.cxx b/panda/src/pgraph/nodePath.cxx index 93a3e47b16..5c250bd9a6 100644 --- a/panda/src/pgraph/nodePath.cxx +++ b/panda/src/pgraph/nodePath.cxx @@ -1435,7 +1435,7 @@ string NodePath:: get_bin_name() const { nassertr_always(!is_empty(), string()); const RenderAttrib *attrib = - node()->get_attrib(ColorAttrib::get_class_type()); + node()->get_attrib(CullBinAttrib::get_class_type()); if (attrib != (const RenderAttrib *)NULL) { const CullBinAttrib *ba = DCAST(CullBinAttrib, attrib); return ba->get_bin_name(); @@ -1456,7 +1456,7 @@ int NodePath:: get_bin_draw_order() const { nassertr_always(!is_empty(), false); const RenderAttrib *attrib = - node()->get_attrib(ColorAttrib::get_class_type()); + node()->get_attrib(CullBinAttrib::get_class_type()); if (attrib != (const RenderAttrib *)NULL) { const CullBinAttrib *ba = DCAST(CullBinAttrib, attrib); return ba->get_draw_order(); diff --git a/panda/src/pgraph/pgraph_composite1.cxx b/panda/src/pgraph/pgraph_composite1.cxx index 1952885207..f871b7d0ea 100644 --- a/panda/src/pgraph/pgraph_composite1.cxx +++ b/panda/src/pgraph/pgraph_composite1.cxx @@ -11,6 +11,8 @@ #include "cullBin.cxx" #include "cullBinAttrib.cxx" #include "cullBinBackToFront.cxx" +#include "cullBinFixed.cxx" +#include "cullBinFrontToBack.cxx" #include "cullBinManager.cxx" #include "cullBinUnsorted.cxx" #include "cullFaceAttrib.cxx"