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