Panda3D
sceneGraphReducer.h
1 // Filename: sceneGraphReducer.h
2 // Created by: drose (14Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef SCENEGRAPHREDUCER_H
16 #define SCENEGRAPHREDUCER_H
17 
18 #include "pandabase.h"
19 #include "transformState.h"
20 #include "renderAttrib.h"
21 #include "renderState.h"
22 #include "accumulatedAttribs.h"
23 #include "geomTransformer.h"
24 #include "pStatCollector.h"
25 #include "pStatTimer.h"
26 #include "typedObject.h"
27 #include "pointerTo.h"
28 #include "graphicsStateGuardianBase.h"
29 
30 class PandaNode;
31 
32 ////////////////////////////////////////////////////////////////////
33 // Class : SceneGraphReducer
34 // Description : An interface for simplifying ("flattening") scene
35 // graphs by eliminating unneeded nodes and collapsing
36 // out unneeded state changes and transforms.
37 //
38 // This class is designed so that it may be inherited
39 // from and specialized, if needed, to fine-tune the
40 // flattening behavior, but normally the default
41 // behavior is sufficient.
42 ////////////////////////////////////////////////////////////////////
43 class EXPCL_PANDA_PGRAPH SceneGraphReducer {
44 PUBLISHED:
45  INLINE SceneGraphReducer(GraphicsStateGuardianBase *gsg = NULL);
46  INLINE ~SceneGraphReducer();
47 
48  enum AttribTypes {
49  TT_transform = 0x001,
50  TT_color = 0x002,
51  TT_color_scale = 0x004,
52  TT_tex_matrix = 0x008,
53  TT_clip_plane = 0x010,
54  TT_cull_face = 0x020,
55  TT_apply_texture_color = 0x040,
56  TT_other = 0x080,
57  };
58 
59  enum CombineSiblings {
60  CS_geom_node = 0x001,
61  CS_within_radius = 0x002,
62  CS_other = 0x004,
63  CS_recurse = 0x008,
64  };
65 
66  enum CollectVertexData {
67  // If set, two GeomVertexDatas with different names will not be
68  // collected together.
69  CVD_name = 0x001,
70 
71  // If set, a ModelNode begins a subgraph of nodes whose
72  // GeomVertexDatas will not be collected with nodes outside the
73  // subgraph.
74  CVD_model = 0x002,
75 
76  // If set, a non-identity transform begins a subgraph of nodes
77  // whose GeomVertexDatas will not be collected with nodes outside
78  // the subgraph.
79  CVD_transform = 0x004,
80 
81  // If set, GeomVertexDatas with any usage_hint other than
82  // UH_static will not be collected with any other Geoms in a
83  // different GeomNode. However, two different dynamic Geoms
84  // within the same node might still be collected together.
85  CVD_avoid_dynamic = 0x008,
86 
87  // If set, only those GeomVertexDatas within the same node might
88  // be collected together.
89  CVD_one_node_only = 0x010,
90 
91  // If set, two GeomVertexDatas with different formats will not be
92  // collected together. If not set, GeomVertexDatas of different
93  // formats may be combined by expanding all GeomVertexDatas to the
94  // union of all defined columns.
95  CVD_format = 0x020,
96 
97  // If set, two GeomVertexDatas with different usage hints (for
98  // instance, UH_static vs. UH_dynamic) will not be collected
99  // together.
100  CVD_usage_hint = 0x040,
101 
102  // If set, GeomVertexDatas with unanimated vertices will not be
103  // combined with GeomVertexDatas with animated vertices.
104  CVD_animation_type = 0x080,
105  };
106 
107  enum MakeNonindexed {
108  // If set, only composite primitives such as tristrips and trifans
109  // will be made nonindexed; simple primitives such as triangles
110  // will be left indexed.
111  MN_composite_only = 0x001,
112 
113  // If set any GeomVertexData with any animation indication will
114  // not be adjusted, whether the animation is to be performed on
115  // the CPU or on the graphics pipe.
116  MN_avoid_animated = 0x002,
117 
118  // If set, any GeomVertexData or Geom with a usage_hint other than
119  // UH_static will not be made nonindexed.
120  MN_avoid_dynamic = 0x004,
121  };
122 
123  void set_gsg(GraphicsStateGuardianBase *gsg);
124  void clear_gsg();
125  INLINE GraphicsStateGuardianBase *get_gsg() const;
126 
127  INLINE void set_combine_radius(PN_stdfloat combine_radius);
128  INLINE PN_stdfloat get_combine_radius() const;
129 
130  INLINE void apply_attribs(PandaNode *node, int attrib_types = ~(TT_clip_plane | TT_cull_face | TT_apply_texture_color));
131  INLINE void apply_attribs(PandaNode *node, const AccumulatedAttribs &attribs,
132  int attrib_types, GeomTransformer &transformer);
133 
134  int flatten(PandaNode *root, int combine_siblings_bits);
135 
136  int remove_column(PandaNode *root, const InternalName *column);
137 
138  int make_compatible_state(PandaNode *root);
139 
140  INLINE int make_compatible_format(PandaNode *root, int collect_bits = ~0);
141  void decompose(PandaNode *root);
142 
143  INLINE int collect_vertex_data(PandaNode *root, int collect_bits = ~0);
144  INLINE int make_nonindexed(PandaNode *root, int nonindexed_bits = ~0);
145  void unify(PandaNode *root, bool preserve_order);
146  void remove_unused_vertices(PandaNode *root);
147 
148  INLINE void premunge(PandaNode *root, const RenderState *initial_state);
149  bool check_live_flatten(PandaNode *node);
150 
151 protected:
152  void r_apply_attribs(PandaNode *node, const AccumulatedAttribs &attribs,
153  int attrib_types, GeomTransformer &transformer);
154 
155  int r_flatten(PandaNode *grandparent_node, PandaNode *parent_node,
156  int combine_siblings_bits);
157  int flatten_siblings(PandaNode *parent_node,
158  int combine_siblings_bits);
159 
160  bool consider_child(PandaNode *grandparent_node,
161  PandaNode *parent_node, PandaNode *child_node);
162  bool consider_siblings(PandaNode *parent_node, PandaNode *child1,
163  PandaNode *child2);
164 
165  bool do_flatten_child(PandaNode *grandparent_node,
166  PandaNode *parent_node, PandaNode *child_node);
167 
168  PandaNode *do_flatten_siblings(PandaNode *parent_node,
169  PandaNode *child1, PandaNode *child2);
170 
171  PT(PandaNode) collapse_nodes(PandaNode *node1, PandaNode *node2,
172  bool siblings);
173  void choose_name(PandaNode *preserve, PandaNode *source1,
174  PandaNode *source2);
175 
176  int r_remove_column(PandaNode *node, const InternalName *column,
177  GeomTransformer &transformer);
178 
179  int r_make_compatible_state(PandaNode *node, GeomTransformer &transformer);
180 
181  int r_collect_vertex_data(PandaNode *node, int collect_bits,
182  GeomTransformer &transformer, bool format_only);
183  int r_make_nonindexed(PandaNode *node, int collect_bits);
184  void r_unify(PandaNode *node, int max_indices, bool preserve_order);
185  void r_register_vertices(PandaNode *node, GeomTransformer &transformer);
186  void r_decompose(PandaNode *node);
187 
188  void r_premunge(PandaNode *node, const RenderState *state);
189 
190 private:
191  PT(GraphicsStateGuardianBase) _gsg;
192  PN_stdfloat _combine_radius;
193  GeomTransformer _transformer;
194 
195  static PStatCollector _flatten_collector;
196  static PStatCollector _apply_collector;
197  static PStatCollector _remove_column_collector;
198  static PStatCollector _compatible_state_collector;
199  static PStatCollector _collect_collector;
200  static PStatCollector _make_nonindexed_collector;
201  static PStatCollector _unify_collector;
202  static PStatCollector _remove_unused_collector;
203  static PStatCollector _premunge_collector;
204 };
205 
206 #include "sceneGraphReducer.I"
207 
208 #endif
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
An interface for simplifying ("flattening") scene graphs by eliminating unneeded nodes and collapsing...
A lightweight class that represents a single element that may be timed and/or counted via stats...
This class is used by the SceneGraphReducer to maintain and accumulate the set of attributes we have ...
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
An object specifically designed to transform the vertices of a Geom without disturbing indexing or af...