From d0aac98dacf3963f058f52970d9262a363d3535d Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Fri, 25 Jan 2008 03:35:58 +0000 Subject: [PATCH] SceneGraphReducer needs to support new ColorAttrib semantics (not quite finished yet) --- panda/src/pgraph/config_pgraph.cxx | 3 +- panda/src/pgraph/geomNode.cxx | 1 + panda/src/pgraph/geomTransformer.cxx | 38 ++++++++++++++++++++++++ panda/src/pgraph/geomTransformer.h | 2 ++ panda/src/pgraph/sceneGraphReducer.cxx | 41 ++++++++++++++++++++++++++ panda/src/pgraph/sceneGraphReducer.h | 5 ++++ 6 files changed, 89 insertions(+), 1 deletion(-) diff --git a/panda/src/pgraph/config_pgraph.cxx b/panda/src/pgraph/config_pgraph.cxx index c6ad07d83e..4530d310b9 100644 --- a/panda/src/pgraph/config_pgraph.cxx +++ b/panda/src/pgraph/config_pgraph.cxx @@ -98,6 +98,7 @@ #include "transformState.h" #include "transparencyAttrib.h" #include "nodePathLerps.h" +#include "shaderGenerator.h" #include "dconfig.h" @@ -434,7 +435,7 @@ init_libpgraph() { ShadeModelAttrib::init_type(); ShaderInput::init_type(); ShaderAttrib::init_type(); - Shader::init_type(); + ShaderGenerator::init_type(); ShowBoundsEffect::init_type(); Spotlight::init_type(); StateMunger::init_type(); diff --git a/panda/src/pgraph/geomNode.cxx b/panda/src/pgraph/geomNode.cxx index 315d86b5da..e30589eb05 100644 --- a/panda/src/pgraph/geomNode.cxx +++ b/panda/src/pgraph/geomNode.cxx @@ -141,6 +141,7 @@ apply_attribs_to_vertices(const AccumulatedAttribs &attribs, int attrib_types, const ColorAttrib *ca = DCAST(ColorAttrib, geom_attribs._color); if (ca->get_color_type() == ColorAttrib::T_flat) { if (transformer.set_color(new_geom, ca->get_color())) { + entry._state = entry._state->add_attrib(ColorAttrib::make_vertex()); any_changed = true; } } diff --git a/panda/src/pgraph/geomTransformer.cxx b/panda/src/pgraph/geomTransformer.cxx index b66039a3d9..ae8115ff0e 100644 --- a/panda/src/pgraph/geomTransformer.cxx +++ b/panda/src/pgraph/geomTransformer.cxx @@ -477,6 +477,44 @@ remove_column(GeomNode *node, const InternalName *column) { return any_changed; } +//////////////////////////////////////////////////////////////////// +// Function: GeomTransformer::apply_colors +// Access: Public +// Description: Checks if the GeomNode has differing ColorAttribs. +// If so, all the colors for all the Geoms are pushed +// down into the vertices, and the differing +// ColorAttribs are removed. +//////////////////////////////////////////////////////////////////// +bool GeomTransformer:: +apply_colors(GeomNode *node) { + if (node->get_num_geoms() < 2) { + return false; + } + + bool need_apply = false; + + GeomNode::CDWriter cdata(node->_cycler); + GeomNode::GeomList::iterator gi; + GeomNode::GeomList &geoms = *(cdata->modify_geoms()); + + const RenderAttrib *first = geoms[0]._state->get_attrib(ColorAttrib::get_class_type()); + for (gi = geoms.begin(); gi != geoms.end(); ++gi) { + GeomNode::GeomEntry &entry = (*gi); + if (entry._state->get_attrib(ColorAttrib::get_class_type()) != first) { + need_apply = true; + break; + } + } + + if (!need_apply) { + return false; + } + + // NOT IMPLEMENTED YET. DOESNT DO ANYTHING. + + return true; +} + //////////////////////////////////////////////////////////////////// // Function: GeomTransformer::reverse_normals // Access: Public diff --git a/panda/src/pgraph/geomTransformer.h b/panda/src/pgraph/geomTransformer.h index 6ae2ec02b5..a09cdd38df 100644 --- a/panda/src/pgraph/geomTransformer.h +++ b/panda/src/pgraph/geomTransformer.h @@ -75,6 +75,8 @@ public: bool remove_column(Geom *geom, const InternalName *column); bool remove_column(GeomNode *node, const InternalName *column); + bool apply_colors(GeomNode *node); + bool reverse_normals(Geom *geom); bool doubleside(GeomNode *node); bool reverse(GeomNode *node); diff --git a/panda/src/pgraph/sceneGraphReducer.cxx b/panda/src/pgraph/sceneGraphReducer.cxx index 7ae0a01d00..f3c029c39f 100644 --- a/panda/src/pgraph/sceneGraphReducer.cxx +++ b/panda/src/pgraph/sceneGraphReducer.cxx @@ -31,6 +31,7 @@ PStatCollector SceneGraphReducer::_flatten_collector("*:Flatten:flatten"); PStatCollector SceneGraphReducer::_apply_collector("*:Flatten:apply"); PStatCollector SceneGraphReducer::_remove_column_collector("*:Flatten:remove column"); +PStatCollector SceneGraphReducer::_apply_colors_collector("*:Flatten:apply colors"); PStatCollector SceneGraphReducer::_collect_collector("*:Flatten:collect"); PStatCollector SceneGraphReducer::_make_nonindexed_collector("*:Flatten:make nonindexed"); PStatCollector SceneGraphReducer::_unify_collector("*:Flatten:unify"); @@ -147,6 +148,21 @@ remove_column(PandaNode *root, const InternalName *column) { return r_remove_column(root, column, _transformer); } +//////////////////////////////////////////////////////////////////// +// Function: SceneGraphReducer::apply_colors +// Access: Published +// Description: Searches for GeomNodes that contain multiple Geoms +// that differ only in their ColorAttribs. If such a +// GeomNode is found, then all the colors are pushed +// down into the vertices. This makes it feasible for +// the geoms to be unified later. +//////////////////////////////////////////////////////////////////// +int SceneGraphReducer:: +apply_colors(PandaNode *root) { + PStatTimer timer(_apply_colors_collector); + return r_apply_colors(root, _transformer); +} + //////////////////////////////////////////////////////////////////// // Function: SceneGraphReducer::unify // Access: Published @@ -741,6 +757,31 @@ r_remove_column(PandaNode *node, const InternalName *column, return num_changed; } +//////////////////////////////////////////////////////////////////// +// Function: SceneGraphReducer::r_apply_colors +// Access: Private +// Description: The recursive implementation of apply_colors(). +//////////////////////////////////////////////////////////////////// +int SceneGraphReducer:: +r_apply_colors(PandaNode *node, GeomTransformer &transformer) { + int num_changed = 0; + + if (node->is_geom_node()) { + if (transformer.apply_colors(DCAST(GeomNode, node))) { + ++num_changed; + } + } + + PandaNode::Children children = node->get_children(); + int num_children = children.get_num_children(); + for (int i = 0; i < num_children; ++i) { + num_changed += + r_apply_colors(children.get_child(i), transformer); + } + + return num_changed; +} + //////////////////////////////////////////////////////////////////// // Function: SceneGraphReducer::r_collect_vertex_data // Access: Private diff --git a/panda/src/pgraph/sceneGraphReducer.h b/panda/src/pgraph/sceneGraphReducer.h index c38d824fd7..3ddc7f184b 100644 --- a/panda/src/pgraph/sceneGraphReducer.h +++ b/panda/src/pgraph/sceneGraphReducer.h @@ -138,6 +138,8 @@ PUBLISHED: int remove_column(PandaNode *root, const InternalName *column); + int apply_colors(PandaNode *root); + INLINE int make_compatible_format(PandaNode *root, int collect_bits = ~0); void decompose(PandaNode *root); @@ -175,6 +177,8 @@ protected: int r_remove_column(PandaNode *node, const InternalName *column, GeomTransformer &transformer); + int r_apply_colors(PandaNode *node, GeomTransformer &transformer); + int r_collect_vertex_data(PandaNode *node, int collect_bits, GeomTransformer &transformer, bool format_only); int r_make_nonindexed(PandaNode *node, int collect_bits); @@ -191,6 +195,7 @@ private: static PStatCollector _flatten_collector; static PStatCollector _apply_collector; static PStatCollector _remove_column_collector; + static PStatCollector _apply_colors_collector; static PStatCollector _collect_collector; static PStatCollector _make_nonindexed_collector; static PStatCollector _unify_collector;