Browse Source

less flickery lod fading

David Rose 17 years ago
parent
commit
311273c22f

+ 10 - 0
panda/src/pgraph/config_pgraph.cxx

@@ -284,6 +284,16 @@ ConfigVariableDouble lod_fade_time
  PRC_DESC("The default amount of time (in seconds) over which a FadeLODNode "
  PRC_DESC("The default amount of time (in seconds) over which a FadeLODNode "
           "transitions between its different levels."));
           "transitions between its different levels."));
 
 
+ConfigVariableString lod_fade_bin_name
+("lod-fade-bin-name", "fixed",
+ PRC_DESC("The default bin name in which to place the fading part of a "
+          "FadeLODNode transition."));
+
+ConfigVariableInt lod_fade_bin_draw_order
+("lod-fade-bin-draw-order", 0,
+ PRC_DESC("The default bin draw order to assign the fading part of a "
+          "FadeLODNode transition."));
+
 ConfigVariableBool verify_lods
 ConfigVariableBool verify_lods
 ("verify-lods", false,
 ("verify-lods", false,
  PRC_DESC("When this is true, LODNodes will test when they are rendered to "
  PRC_DESC("When this is true, LODNodes will test when they are rendered to "

+ 2 - 0
panda/src/pgraph/config_pgraph.h

@@ -56,6 +56,8 @@ extern ConfigVariableBool flatten_geoms;
 
 
 extern ConfigVariableBool polylight_info;
 extern ConfigVariableBool polylight_info;
 extern ConfigVariableDouble lod_fade_time;
 extern ConfigVariableDouble lod_fade_time;
+extern ConfigVariableString lod_fade_bin_name;
+extern ConfigVariableInt lod_fade_bin_draw_order;
 extern ConfigVariableBool verify_lods;
 extern ConfigVariableBool verify_lods;
 
 
 extern ConfigVariableBool show_vertex_animation;
 extern ConfigVariableBool show_vertex_animation;

+ 25 - 11
panda/src/pgraph/fadeLodNode.I

@@ -12,17 +12,6 @@
 //
 //
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 
 
-////////////////////////////////////////////////////////////////////
-//     Function: FadeLODNode::Copy Constructor
-//       Access: Protected
-//  Description:
-////////////////////////////////////////////////////////////////////
-INLINE FadeLODNode::
-FadeLODNode(const FadeLODNode &copy) :
-  LODNode(copy)
-{
-  _fade_time = copy._fade_time;
-}
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: FadeLODNode::set_fade_time
 //     Function: FadeLODNode::set_fade_time
@@ -44,3 +33,28 @@ get_fade_time() const {
   return _fade_time;
   return _fade_time;
 }
 }
 
 
+
+////////////////////////////////////////////////////////////////////
+//     Function: FadeLODNode::get_fade_bin_name
+//       Access: Published
+//  Description: Returns the cull bin that is assigned to the fading
+//               part of the geometry during a transition.
+////////////////////////////////////////////////////////////////////
+INLINE const string &FadeLODNode::
+get_fade_bin_name() const {
+  return _fade_bin_name;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FadeLODNode::get_fade_bin_draw_order
+//       Access: Published
+//  Description: Returns the draw order that is assigned (along with
+//               the bin name) to the fading part of the geometry
+//               during a transition.
+////////////////////////////////////////////////////////////////////
+INLINE int FadeLODNode::
+get_fade_bin_draw_order() const {
+  return _fade_bin_draw_order;
+}
+
+

+ 97 - 19
panda/src/pgraph/fadeLodNode.cxx

@@ -35,6 +35,22 @@ FadeLODNode(const string &name) :
   set_cull_callback();
   set_cull_callback();
 
 
   _fade_time = lod_fade_time;
   _fade_time = lod_fade_time;
+  _fade_bin_name = lod_fade_bin_name;
+  _fade_bin_draw_order = lod_fade_bin_draw_order;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FadeLODNode::Copy Constructor
+//       Access: Protected
+//  Description:
+////////////////////////////////////////////////////////////////////
+FadeLODNode::
+FadeLODNode(const FadeLODNode &copy) :
+  LODNode(copy)
+{
+  _fade_time = copy._fade_time;
+  _fade_bin_name = copy._fade_bin_name;
+  _fade_bin_draw_order = copy._fade_bin_draw_order;
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -139,6 +155,8 @@ cull_callback(CullTraverser *trav, CullTraverserData &data) {
           PandaNode *child = get_child(ldata->_fade_out);
           PandaNode *child = get_child(ldata->_fade_out);
           if (child != (PandaNode *)NULL) {
           if (child != (PandaNode *)NULL) {
             CullTraverserData next_data_out(data, child);
             CullTraverserData next_data_out(data, child);
+            next_data_out._state = 
+              next_data_out._state->compose(get_fade_1_old_state());
             trav->traverse(next_data_out);
             trav->traverse(next_data_out);
           }
           }
         }
         }
@@ -149,24 +167,23 @@ cull_callback(CullTraverser *trav, CullTraverserData &data) {
             CullTraverserData next_data_in(data, child);
             CullTraverserData next_data_in(data, child);
             
             
             float in_alpha = elapsed / half_fade_time;
             float in_alpha = elapsed / half_fade_time;
-            LVecBase4f alpha_scale(1.0f, 1.0f, 1.0f, in_alpha);
-            
             next_data_in._state = 
             next_data_in._state = 
-              next_data_in._state->compose(get_fade_out_state())->compose
-              (RenderState::make(ColorScaleAttrib::make(alpha_scale)));
+              next_data_in._state->compose(get_fade_1_new_state(in_alpha));
             
             
             trav->traverse(next_data_in);
             trav->traverse(next_data_in);
           }
           }
         }
         }
         
         
       } else if (elapsed < _fade_time) {
       } else if (elapsed < _fade_time) {
-        //SECOND HALF OF FADE:
-        //Fade out the old LOD with z write off and 
-        //draw the opaque new LOD with z write on
+        // SECOND HALF OF FADE:
+        // Fade out the old LOD with z write off and 
+        // draw the opaque new LOD with z write on
         if (ldata->_fade_in >= 0 && ldata->_fade_in < get_num_children()) {
         if (ldata->_fade_in >= 0 && ldata->_fade_in < get_num_children()) {
           PandaNode *child = get_child(ldata->_fade_in);
           PandaNode *child = get_child(ldata->_fade_in);
           if (child != (PandaNode *)NULL) {
           if (child != (PandaNode *)NULL) {
             CullTraverserData next_data_in(data, child);
             CullTraverserData next_data_in(data, child);
+            next_data_in._state = 
+              next_data_in._state->compose(get_fade_2_new_state());
             trav->traverse(next_data_in);
             trav->traverse(next_data_in);
           }
           }
         }
         }
@@ -177,11 +194,8 @@ cull_callback(CullTraverser *trav, CullTraverserData &data) {
             CullTraverserData next_data_out(data, child);
             CullTraverserData next_data_out(data, child);
           
           
             float out_alpha = 1.0f - (elapsed - half_fade_time) / half_fade_time;  
             float out_alpha = 1.0f - (elapsed - half_fade_time) / half_fade_time;  
-            LVecBase4f alpha_scale(1.0f, 1.0f, 1.0f, out_alpha);
-            
             next_data_out._state = 
             next_data_out._state = 
-              next_data_out._state->compose(get_fade_out_state())->compose
-              (RenderState::make(ColorScaleAttrib::make(alpha_scale)));
+              next_data_out._state->compose(get_fade_2_old_state(out_alpha));
             
             
             trav->traverse(next_data_out);
             trav->traverse(next_data_out);
           }
           }
@@ -225,20 +239,84 @@ output(ostream &out) const {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: FadeLODNode::get_fade_out_state
+//     Function: FadeLODNode::set_fade_bin
+//       Access: Published
+//  Description: Specifies the cull bin and draw order that is
+//               assigned to the fading part of the geometry during a
+//               transition.
+////////////////////////////////////////////////////////////////////
+void FadeLODNode::
+set_fade_bin(const string &name, int draw_order) {
+  _fade_bin_name = name;
+  _fade_bin_draw_order = draw_order;
+  _fade_1_new_state.clear();
+  _fade_2_old_state.clear();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FadeLODNode::get_fade_1_old_state
 //       Access: Protected, Static
 //       Access: Protected, Static
-//  Description: Returns a RenderState for rendering the element that
-//               is switching out of visibility.
+//  Description: Returns a RenderState for rendering the old element
+//               during first half of fade.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 CPT(RenderState) FadeLODNode::
 CPT(RenderState) FadeLODNode::
-get_fade_out_state() {
-  // Once someone asks for this pointer, we hold its reference count
-  // and never free it.
+get_fade_1_old_state() {
+  return RenderState::make_empty();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FadeLODNode::get_fade_1_new_state
+//       Access: Protected, Static
+//  Description: Returns a RenderState for rendering the new element
+//               during first half of fade.
+////////////////////////////////////////////////////////////////////
+CPT(RenderState) FadeLODNode::
+get_fade_1_new_state(float in_alpha) {
+  if (_fade_1_new_state == (const RenderState *)NULL) {
+    _fade_1_new_state = RenderState::make
+      (TransparencyAttrib::make(TransparencyAttrib::M_alpha),
+       DepthWriteAttrib::make(DepthWriteAttrib::M_off),
+       CullBinAttrib::make(_fade_bin_name, _fade_bin_draw_order),
+       DepthOffsetAttrib::make());
+  }
+
+  LVecBase4f alpha_scale(1.0f, 1.0f, 1.0f, in_alpha);
+  return _fade_1_new_state->compose
+    (RenderState::make(ColorScaleAttrib::make(alpha_scale)));
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FadeLODNode::get_fade_2_old_state
+//       Access: Protected
+//  Description: Returns a RenderState for rendering the old element
+//               during second half of fade.
+////////////////////////////////////////////////////////////////////
+CPT(RenderState) FadeLODNode::
+get_fade_2_old_state(float out_alpha) {
+  if (_fade_2_old_state == (const RenderState *)NULL) {
+    _fade_2_old_state = RenderState::make
+      (TransparencyAttrib::make(TransparencyAttrib::M_alpha),
+       DepthWriteAttrib::make(DepthWriteAttrib::M_off),
+       CullBinAttrib::make(_fade_bin_name, _fade_bin_draw_order));
+  }
+
+  LVecBase4f alpha_scale(1.0f, 1.0f, 1.0f, out_alpha);
+  return _fade_2_old_state->compose
+    (RenderState::make(ColorScaleAttrib::make(alpha_scale)));
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FadeLODNode::get_fade_2_new_state
+//       Access: Protected, Static
+//  Description: Returns a RenderState for rendering the new element
+//               during second half of fade.
+////////////////////////////////////////////////////////////////////
+CPT(RenderState) FadeLODNode::
+get_fade_2_new_state() {
   static CPT(RenderState) state = (const RenderState *)NULL;
   static CPT(RenderState) state = (const RenderState *)NULL;
   if (state == (const RenderState *)NULL) {
   if (state == (const RenderState *)NULL) {
     state = RenderState::make
     state = RenderState::make
-      (TransparencyAttrib::make(TransparencyAttrib::M_alpha),
-       DepthWriteAttrib::make(DepthWriteAttrib::M_off));
+      (DepthOffsetAttrib::make());
   }
   }
 
 
   return state;
   return state;

+ 14 - 2
panda/src/pgraph/fadeLodNode.h

@@ -28,7 +28,7 @@ PUBLISHED:
   FadeLODNode(const string &name);
   FadeLODNode(const string &name);
 
 
 protected:
 protected:
-  INLINE FadeLODNode(const FadeLODNode &copy);
+  FadeLODNode(const FadeLODNode &copy);
 public:
 public:
   virtual PandaNode *make_copy() const;
   virtual PandaNode *make_copy() const;
   virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
   virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
@@ -38,11 +38,23 @@ PUBLISHED:
   INLINE void set_fade_time(float t);
   INLINE void set_fade_time(float t);
   INLINE float get_fade_time() const;
   INLINE float get_fade_time() const;
 
 
+  void set_fade_bin(const string &name, int draw_order);
+  INLINE const string &get_fade_bin_name() const;
+  INLINE int get_fade_bin_draw_order() const;
+
 private:
 private:
-  static CPT(RenderState) get_fade_out_state();
+  static CPT(RenderState) get_fade_1_old_state();
+  CPT(RenderState) get_fade_1_new_state(float in_alpha);
+  CPT(RenderState) get_fade_2_old_state(float out_alpha);
+  static CPT(RenderState) get_fade_2_new_state();
 
 
 private:
 private:
   float _fade_time;
   float _fade_time;
+  string _fade_bin_name;
+  int _fade_bin_draw_order;
+
+  CPT(RenderState) _fade_1_new_state;
+  CPT(RenderState) _fade_2_old_state;
   
   
 public:
 public:
   static void register_with_read_factory();
   static void register_with_read_factory();