Browse Source

more flatten bugs: fix color-scale support

David Rose 18 years ago
parent
commit
02b2d76274
3 changed files with 83 additions and 3 deletions
  1. 31 3
      panda/src/pgraph/geomNode.cxx
  2. 51 0
      panda/src/pgraph/renderState.cxx
  3. 1 0
      panda/src/pgraph/renderState.h

+ 31 - 3
panda/src/pgraph/geomNode.cxx

@@ -135,7 +135,7 @@ apply_attribs_to_vertices(const AccumulatedAttribs &attribs, int attrib_types,
       entry._state = geom_attribs.collect(entry._state, attrib_types);
       entry._state = geom_attribs.collect(entry._state, attrib_types);
       
       
       bool any_changed = false;
       bool any_changed = false;
-      
+
       if ((attrib_types & SceneGraphReducer::TT_color) != 0) {
       if ((attrib_types & SceneGraphReducer::TT_color) != 0) {
         CPT(RenderAttrib) ra = geom_attribs._color;
         CPT(RenderAttrib) ra = geom_attribs._color;
         int override = geom_attribs._color_override;
         int override = geom_attribs._color_override;
@@ -160,12 +160,40 @@ apply_attribs_to_vertices(const AccumulatedAttribs &attribs, int attrib_types,
         if (geom_attribs._color_scale != (const RenderAttrib *)NULL) {
         if (geom_attribs._color_scale != (const RenderAttrib *)NULL) {
           const ColorScaleAttrib *csa = DCAST(ColorScaleAttrib, geom_attribs._color_scale);
           const ColorScaleAttrib *csa = DCAST(ColorScaleAttrib, geom_attribs._color_scale);
           if (csa->get_scale() != LVecBase4f(1.0f, 1.0f, 1.0f, 1.0f)) {
           if (csa->get_scale() != LVecBase4f(1.0f, 1.0f, 1.0f, 1.0f)) {
-            if (transformer.transform_colors(new_geom, csa->get_scale())) {
-              any_changed = true;
+
+            // Now, if we have an "off" or "flat" color attribute, we
+            // simply modify the color attribute, and leave the
+            // vertices alone.
+            const RenderAttrib *ra = entry._state->get_attrib(ColorAttrib::get_class_type());
+            if (ra != (const RenderAttrib *)NULL) {
+              const ColorAttrib *ca = DCAST(ColorAttrib, ra);
+              if (ca->get_color_type() == ColorAttrib::T_off) {
+                entry._state = entry._state->set_attrib(ColorAttrib::make_vertex());
+                // ColorAttrib::T_off means the color scale becomes
+                // the new color.
+                entry._state = entry._state->set_attrib(ColorAttrib::make_flat(csa->get_scale()));
+
+              } else if (ca->get_color_type() == ColorAttrib::T_flat) {
+                // ColorAttrib::T_off means the color scale modulates
+                // the specified color to produce a new color.
+                const Colorf &c1 = ca->get_color();
+                const LVecBase4f &c2 = csa->get_scale();
+                Colorf color(c1[0] * c2[0], c1[1] * c2[1], 
+                             c1[2] * c2[2], c1[3] * c2[3]);
+                entry._state = entry._state->set_attrib(ColorAttrib::make_flat(color));
+
+              } else {
+                // Otherwise, we have vertex color, and we just scale
+                // it normally.
+                if (transformer.transform_colors(new_geom, csa->get_scale())) {
+                  any_changed = true;
+                }
+              }
             }
             }
           }
           }
         }
         }
       }
       }
+
       if ((attrib_types & SceneGraphReducer::TT_tex_matrix) != 0) {
       if ((attrib_types & SceneGraphReducer::TT_tex_matrix) != 0) {
         if (geom_attribs._tex_matrix != (const RenderAttrib *)NULL) {
         if (geom_attribs._tex_matrix != (const RenderAttrib *)NULL) {
           // Determine which texture coordinate names are used more than
           // Determine which texture coordinate names are used more than

+ 51 - 0
panda/src/pgraph/renderState.cxx

@@ -562,6 +562,57 @@ add_attrib(const RenderAttrib *attrib, int override) const {
   return return_new(new_state);
   return return_new(new_state);
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: RenderState::set_attrib
+//       Access: Published
+//  Description: Returns a new RenderState object that represents the
+//               same as the source state, with the new RenderAttrib
+//               added.  If there is already a RenderAttrib with the
+//               same type, it is replaced unconditionally.  The
+//               override is not changed.
+////////////////////////////////////////////////////////////////////
+CPT(RenderState) RenderState::
+set_attrib(const RenderAttrib *attrib) const {
+  RenderState *new_state = new RenderState;
+  back_insert_iterator<Attributes> result = 
+    back_inserter(new_state->_attributes);
+
+  Attribute new_attribute(attrib, 0);
+  Attributes::const_iterator ai = _attributes.begin();
+
+  while (ai != _attributes.end() && (*ai) < new_attribute) {
+    *result = *ai;
+    ++ai;
+    ++result;
+  }
+
+  if (ai != _attributes.end() && !(new_attribute < (*ai))) {
+    // At this point we know: !((*ai) < new_attribute) &&
+    // !(new_attribute < (*ai)) which means (*ai) == new_attribute, so
+    // there is another attribute of the same type already in the
+    // state.
+
+    (*result) = Attribute(attrib, (*ai)._override);
+    ++ai;
+    ++result;
+
+  } else {
+    // There is not another attribute of the same type already in the
+    // state.
+    *result = new_attribute;
+    ++result;
+  }
+
+
+  while (ai != _attributes.end()) {
+    *result = *ai;
+    ++ai;
+    ++result;
+  }
+
+  return return_new(new_state);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: RenderState::remove_attrib
 //     Function: RenderState::remove_attrib
 //       Access: Published
 //       Access: Published

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

@@ -107,6 +107,7 @@ PUBLISHED:
   CPT(RenderState) invert_compose(const RenderState *other) const;
   CPT(RenderState) invert_compose(const RenderState *other) const;
 
 
   CPT(RenderState) add_attrib(const RenderAttrib *attrib, int override = 0) const;
   CPT(RenderState) add_attrib(const RenderAttrib *attrib, int override = 0) const;
+  CPT(RenderState) set_attrib(const RenderAttrib *attrib) const;
   CPT(RenderState) remove_attrib(TypeHandle type) const;
   CPT(RenderState) remove_attrib(TypeHandle type) const;
 
 
   CPT(RenderState) adjust_all_priorities(int adjustment) const;
   CPT(RenderState) adjust_all_priorities(int adjustment) const;