Panda3D
 All Classes Functions Variables Enumerations
sceneGraphReducer.h
00001 // Filename: sceneGraphReducer.h
00002 // Created by:  drose (14Mar02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef SCENEGRAPHREDUCER_H
00016 #define SCENEGRAPHREDUCER_H
00017 
00018 #include "pandabase.h"
00019 #include "transformState.h"
00020 #include "renderAttrib.h"
00021 #include "renderState.h"
00022 #include "accumulatedAttribs.h"
00023 #include "geomTransformer.h"
00024 #include "pStatCollector.h"
00025 #include "pStatTimer.h"
00026 #include "typedObject.h"
00027 #include "pointerTo.h"
00028 #include "graphicsStateGuardianBase.h"
00029 
00030 class PandaNode;
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //       Class : SceneGraphReducer
00034 // Description : An interface for simplifying ("flattening") scene
00035 //               graphs by eliminating unneeded nodes and collapsing
00036 //               out unneeded state changes and transforms.
00037 //
00038 //               This class is designed so that it may be inherited
00039 //               from and specialized, if needed, to fine-tune the
00040 //               flattening behavior, but normally the default
00041 //               behavior is sufficient.
00042 ////////////////////////////////////////////////////////////////////
00043 class EXPCL_PANDA_PGRAPH SceneGraphReducer {
00044 PUBLISHED:
00045   INLINE SceneGraphReducer(GraphicsStateGuardianBase *gsg = NULL);
00046   INLINE ~SceneGraphReducer();
00047 
00048   enum AttribTypes {
00049     TT_transform           = 0x001,
00050     TT_color               = 0x002,
00051     TT_color_scale         = 0x004,
00052     TT_tex_matrix          = 0x008,
00053     TT_clip_plane          = 0x010,
00054     TT_cull_face           = 0x020,
00055     TT_apply_texture_color = 0x040,
00056     TT_other               = 0x080,
00057   };
00058 
00059   enum CombineSiblings {
00060     CS_geom_node       = 0x001,
00061     CS_within_radius   = 0x002,
00062     CS_other           = 0x004,
00063     CS_recurse         = 0x008,
00064   };
00065 
00066   enum CollectVertexData {
00067     // If set, two GeomVertexDatas with different names will not be
00068     // collected together.
00069     CVD_name           = 0x001,
00070 
00071     // If set, a ModelNode begins a subgraph of nodes whose
00072     // GeomVertexDatas will not be collected with nodes outside the
00073     // subgraph.
00074     CVD_model          = 0x002,
00075 
00076     // If set, a non-identity transform begins a subgraph of nodes
00077     // whose GeomVertexDatas will not be collected with nodes outside
00078     // the subgraph.
00079     CVD_transform      = 0x004,
00080 
00081     // If set, GeomVertexDatas with any usage_hint other than
00082     // UH_static will not be collected with any other Geoms in a
00083     // different GeomNode.  However, two different dynamic Geoms
00084     // within the same node might still be collected together.
00085     CVD_avoid_dynamic  = 0x008,
00086 
00087     // If set, only those GeomVertexDatas within the same node might
00088     // be collected together.
00089     CVD_one_node_only  = 0x010,
00090 
00091     // If set, two GeomVertexDatas with different formats will not be
00092     // collected together.  If not set, GeomVertexDatas of different
00093     // formats may be combined by expanding all GeomVertexDatas to the
00094     // union of all defined columns.
00095     CVD_format         = 0x020,
00096 
00097     // If set, two GeomVertexDatas with different usage hints (for
00098     // instance, UH_static vs. UH_dynamic) will not be collected
00099     // together.
00100     CVD_usage_hint     = 0x040,
00101 
00102     // If set, GeomVertexDatas with unanimated vertices will not be
00103     // combined with GeomVertexDatas with animated vertices.
00104     CVD_animation_type = 0x080, 
00105   };
00106 
00107   enum MakeNonindexed {
00108     // If set, only composite primitives such as tristrips and trifans
00109     // will be made nonindexed; simple primitives such as triangles
00110     // will be left indexed.
00111     MN_composite_only  = 0x001,
00112 
00113     // If set any GeomVertexData with any animation indication will
00114     // not be adjusted, whether the animation is to be performed on
00115     // the CPU or on the graphics pipe.
00116     MN_avoid_animated  = 0x002,
00117 
00118     // If set, any GeomVertexData or Geom with a usage_hint other than
00119     // UH_static will not be made nonindexed.
00120     MN_avoid_dynamic   = 0x004,
00121   };
00122 
00123   void set_gsg(GraphicsStateGuardianBase *gsg);
00124   void clear_gsg();
00125   INLINE GraphicsStateGuardianBase *get_gsg() const;
00126 
00127   INLINE void set_combine_radius(PN_stdfloat combine_radius);
00128   INLINE PN_stdfloat get_combine_radius() const;
00129 
00130   INLINE void apply_attribs(PandaNode *node, int attrib_types = ~(TT_clip_plane | TT_cull_face | TT_apply_texture_color));
00131   INLINE void apply_attribs(PandaNode *node, const AccumulatedAttribs &attribs,
00132                             int attrib_types, GeomTransformer &transformer);
00133 
00134   int flatten(PandaNode *root, int combine_siblings_bits);
00135 
00136   int remove_column(PandaNode *root, const InternalName *column);
00137 
00138   int make_compatible_state(PandaNode *root);
00139   
00140   INLINE int make_compatible_format(PandaNode *root, int collect_bits = ~0);
00141   void decompose(PandaNode *root);
00142 
00143   INLINE int collect_vertex_data(PandaNode *root, int collect_bits = ~0);
00144   INLINE int make_nonindexed(PandaNode *root, int nonindexed_bits = ~0);
00145   void unify(PandaNode *root, bool preserve_order);
00146   void remove_unused_vertices(PandaNode *root);
00147 
00148   INLINE void premunge(PandaNode *root, const RenderState *initial_state);
00149   bool check_live_flatten(PandaNode *node);
00150 
00151 protected:
00152   void r_apply_attribs(PandaNode *node, const AccumulatedAttribs &attribs,
00153                        int attrib_types, GeomTransformer &transformer);
00154 
00155   int r_flatten(PandaNode *grandparent_node, PandaNode *parent_node,
00156                 int combine_siblings_bits);
00157   int flatten_siblings(PandaNode *parent_node,
00158                        int combine_siblings_bits);
00159 
00160   bool consider_child(PandaNode *grandparent_node,
00161                       PandaNode *parent_node, PandaNode *child_node);
00162   bool consider_siblings(PandaNode *parent_node, PandaNode *child1,
00163                          PandaNode *child2);
00164 
00165   bool do_flatten_child(PandaNode *grandparent_node, 
00166                         PandaNode *parent_node, PandaNode *child_node);
00167 
00168   PandaNode *do_flatten_siblings(PandaNode *parent_node, 
00169                                  PandaNode *child1, PandaNode *child2);
00170 
00171   PT(PandaNode) collapse_nodes(PandaNode *node1, PandaNode *node2, 
00172                                bool siblings);
00173   void choose_name(PandaNode *preserve, PandaNode *source1, 
00174                    PandaNode *source2);
00175 
00176   int r_remove_column(PandaNode *node, const InternalName *column,
00177                       GeomTransformer &transformer);
00178 
00179   int r_make_compatible_state(PandaNode *node, GeomTransformer &transformer);
00180 
00181   int r_collect_vertex_data(PandaNode *node, int collect_bits,
00182                             GeomTransformer &transformer, bool format_only);
00183   int r_make_nonindexed(PandaNode *node, int collect_bits);
00184   void r_unify(PandaNode *node, int max_indices, bool preserve_order);
00185   void r_register_vertices(PandaNode *node, GeomTransformer &transformer);
00186   void r_decompose(PandaNode *node);
00187 
00188   void r_premunge(PandaNode *node, const RenderState *state);
00189 
00190 private:
00191   PT(GraphicsStateGuardianBase) _gsg;
00192   PN_stdfloat _combine_radius;
00193   GeomTransformer _transformer;
00194 
00195   static PStatCollector _flatten_collector;
00196   static PStatCollector _apply_collector;
00197   static PStatCollector _remove_column_collector;
00198   static PStatCollector _compatible_state_collector;
00199   static PStatCollector _collect_collector;
00200   static PStatCollector _make_nonindexed_collector;
00201   static PStatCollector _unify_collector;
00202   static PStatCollector _remove_unused_collector;
00203   static PStatCollector _premunge_collector;
00204 };
00205 
00206 #include "sceneGraphReducer.I"
00207 
00208 #endif
 All Classes Functions Variables Enumerations