Browse Source

add SceneGraphReducer::decompose()

David Rose 18 years ago
parent
commit
dd03048f6e

+ 32 - 0
panda/src/pgraph/geomNode.cxx

@@ -514,6 +514,38 @@ check_valid() const {
   return true;
   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
 //     Function: GeomNode::unify
 //       Access: Published
 //       Access: Published

+ 1 - 0
panda/src/pgraph/geomNode.h

@@ -80,6 +80,7 @@ PUBLISHED:
   INLINE void remove_all_geoms();
   INLINE void remove_all_geoms();
   bool check_valid() const;
   bool check_valid() const;
 
 
+  void decompose();
   void unify(int max_indices, bool preserve_order);
   void unify(int max_indices, bool preserve_order);
 
 
   void write_geoms(ostream &out, int indent_level) const;
   void write_geoms(ostream &out, int indent_level) const;

+ 1 - 1
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
 //       Access: Public
 //  Description: Overrides the color indicated within the GeomNode
 //  Description: Overrides the color indicated within the GeomNode
 //               with the given replacement color.  Returns true if
 //               with the given replacement color.  Returns true if

+ 42 - 0
panda/src/pgraph/sceneGraphReducer.cxx

@@ -25,6 +25,7 @@
 #include "plist.h"
 #include "plist.h"
 #include "pmap.h"
 #include "pmap.h"
 #include "geomNode.h"
 #include "geomNode.h"
+#include "config_gobj.h"
 #include "thread.h"
 #include "thread.h"
 
 
 PStatCollector SceneGraphReducer::_flatten_collector("*:Flatten:flatten");
 PStatCollector SceneGraphReducer::_flatten_collector("*:Flatten:flatten");
@@ -166,6 +167,28 @@ unify(PandaNode *root, bool preserve_order) {
   r_unify(root, max_indices, 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
 //     Function: SceneGraphReducer::r_apply_attribs
 //       Access: Protected
 //       Access: Protected
@@ -845,6 +868,25 @@ r_unify(PandaNode *node, int max_indices, bool preserve_order) {
   Thread::consider_yield();
   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
 //     Function: SceneGraphReducer::r_premunge
 //       Access: Private
 //       Access: Private

+ 3 - 0
panda/src/pgraph/sceneGraphReducer.h

@@ -139,6 +139,8 @@ PUBLISHED:
   int remove_column(PandaNode *root, const InternalName *column);
   int remove_column(PandaNode *root, const InternalName *column);
 
 
   INLINE int make_compatible_format(PandaNode *root, int collect_bits = ~0);
   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 collect_vertex_data(PandaNode *root, int collect_bits = ~0);
   INLINE int make_nonindexed(PandaNode *root, int nonindexed_bits = ~0);
   INLINE int make_nonindexed(PandaNode *root, int nonindexed_bits = ~0);
   void unify(PandaNode *root, bool preserve_order);
   void unify(PandaNode *root, bool preserve_order);
@@ -177,6 +179,7 @@ protected:
                             GeomTransformer &transformer, bool format_only);
                             GeomTransformer &transformer, bool format_only);
   int r_make_nonindexed(PandaNode *node, int collect_bits);
   int r_make_nonindexed(PandaNode *node, int collect_bits);
   void r_unify(PandaNode *node, int max_indices, bool preserve_order);
   void r_unify(PandaNode *node, int max_indices, bool preserve_order);
+  void r_decompose(PandaNode *node);
 
 
   void r_premunge(PandaNode *node, const RenderState *state);
   void r_premunge(PandaNode *node, const RenderState *state);