From dd03048f6e75741bf24337fa1bed9131dd84c268 Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 20 Dec 2007 22:33:44 +0000 Subject: [PATCH] add SceneGraphReducer::decompose() --- panda/src/pgraph/geomNode.cxx | 32 ++++++++++++++++++++ panda/src/pgraph/geomNode.h | 1 + panda/src/pgraph/geomTransformer.cxx | 2 +- panda/src/pgraph/sceneGraphReducer.cxx | 42 ++++++++++++++++++++++++++ panda/src/pgraph/sceneGraphReducer.h | 3 ++ 5 files changed, 79 insertions(+), 1 deletion(-) diff --git a/panda/src/pgraph/geomNode.cxx b/panda/src/pgraph/geomNode.cxx index 6422956304..315d86b5da 100644 --- a/panda/src/pgraph/geomNode.cxx +++ b/panda/src/pgraph/geomNode.cxx @@ -514,6 +514,38 @@ check_valid() const { return true; } +//////////////////////////////////////////////////////////////////// +// Function: GeomNode::decompose +// Access: Published +// Description: Calls decompose() on each Geom with the GeomNode. +// This decomposes higher-order primitive types, like +// triangle strips, into lower-order types like indexed +// triangles. Normally there is no reason to do this, +// but it can be useful as an early preprocessing step, +// to allow a later call to unify() to proceed more +// quickly. +// +// See also SceneGraphReducer::decompose(), which is the +// normal way this is called. +//////////////////////////////////////////////////////////////////// +void GeomNode:: +decompose() { + Thread *current_thread = Thread::get_current_thread(); + OPEN_ITERATE_CURRENT_AND_UPSTREAM(_cycler, current_thread) { + CDStageWriter cdata(_cycler, pipeline_stage, current_thread); + + GeomList::iterator gi; + PT(GeomList) geoms = cdata->modify_geoms(); + for (gi = geoms->begin(); gi != geoms->end(); ++gi) { + GeomEntry &entry = (*gi); + nassertv(entry._geom.test_ref_count_integrity()); + PT(Geom) geom = entry._geom.get_write_pointer(); + geom->decompose_in_place(); + } + } + CLOSE_ITERATE_CURRENT_AND_UPSTREAM(_cycler); +} + //////////////////////////////////////////////////////////////////// // Function: GeomNode::unify // Access: Published diff --git a/panda/src/pgraph/geomNode.h b/panda/src/pgraph/geomNode.h index 889e81bd38..e8dea1b07f 100644 --- a/panda/src/pgraph/geomNode.h +++ b/panda/src/pgraph/geomNode.h @@ -80,6 +80,7 @@ PUBLISHED: INLINE void remove_all_geoms(); bool check_valid() const; + void decompose(); void unify(int max_indices, bool preserve_order); void write_geoms(ostream &out, int indent_level) const; diff --git a/panda/src/pgraph/geomTransformer.cxx b/panda/src/pgraph/geomTransformer.cxx index 8441b038cd..b66039a3d9 100644 --- a/panda/src/pgraph/geomTransformer.cxx +++ b/panda/src/pgraph/geomTransformer.cxx @@ -280,7 +280,7 @@ set_color(Geom *geom, const Colorf &color) { //////////////////////////////////////////////////////////////////// -// Function: GeomTransformer::transform_texcoords +// Function: GeomTransformer::set_color // Access: Public // Description: Overrides the color indicated within the GeomNode // with the given replacement color. Returns true if diff --git a/panda/src/pgraph/sceneGraphReducer.cxx b/panda/src/pgraph/sceneGraphReducer.cxx index edb6fcd0e0..7ae0a01d00 100644 --- a/panda/src/pgraph/sceneGraphReducer.cxx +++ b/panda/src/pgraph/sceneGraphReducer.cxx @@ -25,6 +25,7 @@ #include "plist.h" #include "pmap.h" #include "geomNode.h" +#include "config_gobj.h" #include "thread.h" PStatCollector SceneGraphReducer::_flatten_collector("*:Flatten:flatten"); @@ -166,6 +167,28 @@ unify(PandaNode *root, bool preserve_order) { r_unify(root, max_indices, preserve_order); } +//////////////////////////////////////////////////////////////////// +// Function: SceneGraphReducer::decompose +// Access: Published +// Description: Calls decompose() on every GeomNode at this level and +// below. +// +// There is usually no reason to call this explicitly, +// since unify() will do this anyway if it needs to be +// done. However, calling it ahead of time can make +// that future call to unify() run a little bit faster. +// +// This operation has no effect if the config variable +// preserve-triangle-strips has been set true. +//////////////////////////////////////////////////////////////////// +void SceneGraphReducer:: +decompose(PandaNode *root) { + if (!preserve_triangle_strips) { + PStatTimer timer(_unify_collector); + r_decompose(root); + } +} + //////////////////////////////////////////////////////////////////// // Function: SceneGraphReducer::r_apply_attribs // Access: Protected @@ -845,6 +868,25 @@ r_unify(PandaNode *node, int max_indices, bool preserve_order) { Thread::consider_yield(); } +//////////////////////////////////////////////////////////////////// +// Function: SceneGraphReducer::r_decompose +// Access: Private +// Description: The recursive implementation of decompose(). +//////////////////////////////////////////////////////////////////// +void SceneGraphReducer:: +r_decompose(PandaNode *node) { + if (node->is_geom_node()) { + GeomNode *geom_node = DCAST(GeomNode, node); + geom_node->decompose(); + } + + PandaNode::Children children = node->get_children(); + int num_children = children.get_num_children(); + for (int i = 0; i < num_children; ++i) { + r_decompose(children.get_child(i)); + } +} + //////////////////////////////////////////////////////////////////// // Function: SceneGraphReducer::r_premunge // Access: Private diff --git a/panda/src/pgraph/sceneGraphReducer.h b/panda/src/pgraph/sceneGraphReducer.h index 7667083e9c..c38d824fd7 100644 --- a/panda/src/pgraph/sceneGraphReducer.h +++ b/panda/src/pgraph/sceneGraphReducer.h @@ -139,6 +139,8 @@ PUBLISHED: int remove_column(PandaNode *root, const InternalName *column); INLINE int make_compatible_format(PandaNode *root, int collect_bits = ~0); + void decompose(PandaNode *root); + INLINE int collect_vertex_data(PandaNode *root, int collect_bits = ~0); INLINE int make_nonindexed(PandaNode *root, int nonindexed_bits = ~0); void unify(PandaNode *root, bool preserve_order); @@ -177,6 +179,7 @@ protected: GeomTransformer &transformer, bool format_only); int r_make_nonindexed(PandaNode *node, int collect_bits); void r_unify(PandaNode *node, int max_indices, bool preserve_order); + void r_decompose(PandaNode *node); void r_premunge(PandaNode *node, const RenderState *state);