Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/char/character.cxx

Go to the documentation of this file.
00001 // Filename: character.cxx
00002 // Created by:  drose (06Mar02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "character.h"
00020 #include "characterJoint.h"
00021 #include "computedVertices.h"
00022 #include "config_char.h"
00023 
00024 #include "geomNode.h"
00025 #include "datagram.h"
00026 #include "datagramIterator.h"
00027 #include "bamReader.h"
00028 #include "bamWriter.h"
00029 #include "pStatTimer.h"
00030 #include "animControl.h"
00031 #include "clockObject.h"
00032 #include "pStatTimer.h"
00033 
00034 TypeHandle Character::_type_handle;
00035 
00036 #ifndef CPPPARSER
00037 PStatCollector Character::_anim_pcollector("App:Animation");
00038 #endif
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: Character::Copy Constructor
00042 //       Access: Protected
00043 //  Description: Use make_copy() or copy_subgraph() to copy a Character.
00044 ////////////////////////////////////////////////////////////////////
00045 Character::
00046 Character(const Character &copy) :
00047   PartBundleNode(copy, new CharacterJointBundle(copy.get_bundle()->get_name())),
00048   _cv(DynamicVertices::deep_copy(copy._cv)),
00049   _computed_vertices(copy._computed_vertices),
00050   _parts(copy._parts),
00051   _char_pcollector(copy._char_pcollector)
00052 {
00053   // Now make a copy of the joint/slider hierarchy.  We could just use
00054   // the copy_subgraph feature of the PartBundleNode's copy
00055   // constructor, but if we do it ourselves we can simultaneously
00056   // update our _parts list.
00057 
00058   copy_joints(get_bundle(), copy.get_bundle());
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: Character::Constructor
00063 //       Access: Public
00064 //  Description:
00065 ////////////////////////////////////////////////////////////////////
00066 Character::
00067 Character(const string &name) :
00068   PartBundleNode(name, new CharacterJointBundle(name)),
00069   _char_pcollector(_anim_pcollector, name)
00070 {
00071 }
00072 
00073 ////////////////////////////////////////////////////////////////////
00074 //     Function: Character::Destructor
00075 //       Access: Public
00076 //  Description:
00077 ////////////////////////////////////////////////////////////////////
00078 Character::
00079 ~Character() {
00080 }
00081 
00082 ////////////////////////////////////////////////////////////////////
00083 //     Function: Character::make_copy
00084 //       Access: Public, Virtual
00085 //  Description: The Character make_copy() function will make a new
00086 //               copy of the Character, with all of its joints copied,
00087 //               and with a new set of dynamic vertex arrays all ready
00088 //               to go, but it will not copy any of the original
00089 //               Character's geometry, so the new Character won't look
00090 //               like much.  Use copy_subgraph() to make a full copy
00091 //               of the Character.
00092 ////////////////////////////////////////////////////////////////////
00093 PandaNode *Character::
00094 make_copy() const {
00095   return new Character(*this);
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: Character::safe_to_transform
00100 //       Access: Public, Virtual
00101 //  Description: Returns true if it is generally safe to transform
00102 //               this particular kind of Node by calling the xform()
00103 //               method, false otherwise.  For instance, it's usually
00104 //               a bad idea to attempt to xform a Character.
00105 ////////////////////////////////////////////////////////////////////
00106 bool Character::
00107 safe_to_transform() const {
00108   return false;
00109 }
00110 
00111 ////////////////////////////////////////////////////////////////////
00112 //     Function: Character::has_cull_callback
00113 //       Access: Public, Virtual
00114 //  Description: Should be overridden by derived classes to return
00115 //               true if cull_callback() has been defined.  Otherwise,
00116 //               returns false to indicate cull_callback() does not
00117 //               need to be called for this node during the cull
00118 //               traversal.
00119 ////////////////////////////////////////////////////////////////////
00120 bool Character::
00121 has_cull_callback() const {
00122   return true;
00123 }
00124 
00125 ////////////////////////////////////////////////////////////////////
00126 //     Function: Character::cull_callback
00127 //       Access: Public, Virtual
00128 //  Description: If has_cull_callback() returns true, this function
00129 //               will be called during the cull traversal to perform
00130 //               any additional operations that should be performed at
00131 //               cull time.  This may include additional manipulation
00132 //               of render state or additional visible/invisible
00133 //               decisions, or any other arbitrary operation.
00134 //
00135 //               By the time this function is called, the node has
00136 //               already passed the bounding-volume test for the
00137 //               viewing frustum, and the node's transform and state
00138 //               have already been applied to the indicated
00139 //               CullTraverserData object.
00140 //
00141 //               The return value is true if this node should be
00142 //               visible, or false if it should be culled.
00143 ////////////////////////////////////////////////////////////////////
00144 bool Character::
00145 cull_callback(CullTraverser *, CullTraverserData &) {
00146   // For now, we update the character during the cull traversal; this
00147   // prevents us from needlessly updating characters that aren't in
00148   // the view frustum.  We may need a better way to do this
00149   // optimization later, to handle characters that might animate
00150   // themselves in front of the view frustum.
00151   update_to_now();
00152   return true;
00153 }
00154 
00155 ////////////////////////////////////////////////////////////////////
00156 //     Function: Character::update_to_now
00157 //       Access: Published
00158 //  Description: Advances the character's frame to the current time,
00159 //               and then calls update().  This can be used by show
00160 //               code to force an update of the character's position
00161 //               to the current frame, regardless of whether the
00162 //               character is currently onscreen and animating.
00163 ////////////////////////////////////////////////////////////////////
00164 void Character::
00165 update_to_now() {
00166   double now = ClockObject::get_global_clock()->get_frame_time();
00167   get_bundle()->advance_time(now);
00168 
00169   if (char_cat.is_spam()) {
00170     char_cat.spam() << "Animating " << *this << " at time " << now << "\n";
00171   }
00172 
00173   update();
00174 }
00175 
00176 ////////////////////////////////////////////////////////////////////
00177 //     Function: Character::update
00178 //       Access: Published
00179 //  Description: Recalculates the Character's joints and vertices for
00180 //               the current frame.  Normally this is performed
00181 //               automatically during the render and need not be
00182 //               called explicitly.
00183 ////////////////////////////////////////////////////////////////////
00184 void Character::
00185 update() {
00186   // Statistics
00187   PStatTimer timer(_char_pcollector);
00188 
00189   // First, update all the joints and sliders.
00190   bool any_changed = get_bundle()->update();
00191 
00192   // Now update the vertices, if we need to.  This is likely to be a
00193   // slow operation.
00194   if (any_changed || even_animation) {
00195     if (_computed_vertices != (ComputedVertices *)NULL) {
00196       _computed_vertices->update(this);
00197     }
00198   }
00199 }
00200 
00201 ////////////////////////////////////////////////////////////////////
00202 //     Function: Character::force_update
00203 //       Access: Published
00204 //  Description: Recalculates the character even if we think it
00205 //               doesn't need it.
00206 ////////////////////////////////////////////////////////////////////
00207 void Character::
00208 force_update() {
00209   // Statistics
00210   PStatTimer timer(_char_pcollector);
00211 
00212   // First, update all the joints and sliders.
00213   get_bundle()->force_update();
00214 
00215   // Now update the vertices.
00216   if (_computed_vertices != (ComputedVertices *)NULL) {
00217     _computed_vertices->update(this);
00218   }
00219 }
00220 
00221 ////////////////////////////////////////////////////////////////////
00222 //     Function: Character::copy_joints
00223 //       Access: Private
00224 //  Description: Recursively walks the joint/slider hierarchy and
00225 //               creates a new copy of the hierarchy.
00226 ////////////////////////////////////////////////////////////////////
00227 void Character::
00228 copy_joints(PartGroup *copy, PartGroup *orig) {
00229   if (copy->get_type() != orig->get_type()) {
00230     char_cat.warning()
00231       << "Don't know how to copy " << orig->get_type() << "\n";
00232   }
00233 
00234   PartGroup::Children::const_iterator ci;
00235   for (ci = orig->_children.begin(); ci != orig->_children.end(); ++ci) {
00236     PartGroup *orig_child = (*ci);
00237     PartGroup *copy_child = orig_child->make_copy();
00238     copy->_children.push_back(copy_child);
00239     copy_joints(copy_child, orig_child);
00240   }
00241 
00242   Parts::iterator pi = find(_parts.begin(), _parts.end(), orig);
00243   if (pi != _parts.end()) {
00244     (*pi) = copy;
00245   }
00246 }
00247 
00248 ////////////////////////////////////////////////////////////////////
00249 //     Function: Character::r_copy_children
00250 //       Access: Protected, Virtual
00251 //  Description: This is called by r_copy_subgraph(); the copy has
00252 //               already been made of this particular node (and this
00253 //               is the copy); this function's job is to copy all of
00254 //               the children from the original.
00255 //
00256 //               Note that it includes the parameter inst_map, which
00257 //               is a map type, and is not (and cannot be) exported
00258 //               from PANDA.DLL.  Thus, any derivative of PandaNode
00259 //               that is not also a member of PANDA.DLL *cannot*
00260 //               access this map, and probably should not even
00261 //               override this function.
00262 ////////////////////////////////////////////////////////////////////
00263 void Character::
00264 r_copy_children(const PandaNode *from, PandaNode::InstanceMap &inst_map) {
00265   // We assume there will be no instancing going on below the
00266   // Character node.  If there is, too bad; it will get flattened out.
00267 
00268   // We preempt the node's r_copy_children() operation with our own
00269   // function that keeps track of the old vs. new nodes and also
00270   // updates any Geoms we find with our new dynamic vertices.
00271 
00272   const Character *from_char;
00273   DCAST_INTO_V(from_char, from);
00274   NodeMap node_map;
00275   r_copy_char(this, from_char, from_char, node_map);
00276   copy_node_pointers(from_char, node_map);
00277 }
00278 
00279 
00280 ////////////////////////////////////////////////////////////////////
00281 //     Function: Character::r_copy_char
00282 //       Access: Private
00283 //  Description: Recursively walks the scene graph hiernodehy below the
00284 //               Character node, duplicating it while noting the
00285 //               orig:copy node mappings, and also updates any
00286 //               GeomNodes found.
00287 ////////////////////////////////////////////////////////////////////
00288 void Character::
00289 r_copy_char(PandaNode *dest, const PandaNode *source,
00290             const Character *from, Character::NodeMap &node_map) {
00291   if (source->is_geom_node()) {
00292     const GeomNode *source_gnode;
00293     GeomNode *dest_gnode;
00294     DCAST_INTO_V(source_gnode, source);
00295     DCAST_INTO_V(dest_gnode, dest);
00296 
00297     dest_gnode->remove_all_geoms();
00298     int num_geoms = source_gnode->get_num_geoms();
00299     for (int i = 0; i < num_geoms; i++) {
00300       Geom *geom = source_gnode->get_geom(i);
00301       const RenderState *state = source_gnode->get_geom_state(i);
00302       dest_gnode->add_geom(copy_geom(geom, from), state);
00303     }
00304   }
00305 
00306   int num_children = source->get_num_children();
00307   for (int i = 0; i < num_children; i++) {
00308     const PandaNode *source_child = source->get_child(i);
00309     int source_sort = source->get_child_sort(i);
00310 
00311     PandaNode *dest_child;
00312     if (source_child->is_of_type(Character::get_class_type())) {
00313       // We make a special case for nodes of type Character.  If we
00314       // encounter one of these, we have a Character under a
00315       // Character, and the nested Character's copy should be called
00316       // instead of ours.
00317       dest_child = source_child->copy_subgraph();
00318 
00319     } else {
00320       // Otherwise, we assume that make_copy() will make a suitable
00321       // copy of the node.  This does limit the sorts of things we can
00322       // have parented to a Character and expect copy_subgraph() to
00323       // work correctly.  Too bad.
00324       dest_child = source_child->make_copy();
00325       r_copy_char(dest_child, source_child, from, node_map);
00326     }
00327     dest->add_child(dest_child, source_sort);
00328     node_map[source_child] = dest_child;
00329   }
00330 }
00331 
00332 ////////////////////////////////////////////////////////////////////
00333 //     Function: Character::copy_geom
00334 //       Access: Private
00335 //  Description: Makes a new copy of the Geom with the dynamic vertex
00336 //               arrays replaced to reference this Character instead
00337 //               of the other one.  If no arrays have changed, simply
00338 //               returns the same Geom.
00339 ////////////////////////////////////////////////////////////////////
00340 PT(Geom) Character::
00341 copy_geom(Geom *source, const Character *from) {
00342   GeomBindType bind;
00343   PTA_ushort index;
00344 
00345   PTA_Vertexf coords;
00346   PTA_Normalf norms;
00347   PTA_Colorf colors;
00348   PTA_TexCoordf texcoords;
00349 
00350   PT(Geom) dest = source;
00351 
00352   source->get_coords(coords, index);
00353   if ((coords != (void *)NULL) && (coords == (from->_cv._coords))) {
00354     if (dest == source) {
00355       dest = source->make_copy();
00356     }
00357     dest->set_coords(_cv._coords, index);
00358   }
00359 
00360   source->get_normals(norms, bind, index);
00361   if (bind != G_OFF && norms == from->_cv._norms) {
00362     if (dest == source) {
00363       dest = source->make_copy();
00364     }
00365     dest->set_normals(_cv._norms, bind, index);
00366   }
00367 
00368   source->get_colors(colors, bind, index);
00369   if (bind != G_OFF && colors == from->_cv._colors) {
00370     if (dest == source) {
00371       dest = source->make_copy();
00372     }
00373     dest->set_colors(_cv._colors, bind, index);
00374   }
00375 
00376   source->get_texcoords(texcoords, bind, index);
00377   if (bind != G_OFF && texcoords == from->_cv._texcoords) {
00378     if (dest == source) {
00379       dest = source->make_copy();
00380     }
00381     dest->set_texcoords(_cv._texcoords, bind, index);
00382   }
00383 
00384   return dest;
00385 }
00386 
00387 ////////////////////////////////////////////////////////////////////
00388 //     Function: Character::copy_node_pointers
00389 //       Access: Public
00390 //  Description: Creates _net_transform_nodes and _local_transform_nodes
00391 //               as appropriate in each of the Character's joints, as
00392 //               copied from the other Character.
00393 ////////////////////////////////////////////////////////////////////
00394 void Character::
00395 copy_node_pointers(const Character *from, const Character::NodeMap &node_map) {
00396   nassertv(_parts.size() == from->_parts.size());
00397   for (int i = 0; i < (int)_parts.size(); i++) {
00398     if (_parts[i]->is_of_type(CharacterJoint::get_class_type())) {
00399       nassertv(_parts[i] != from->_parts[i]);
00400       CharacterJoint *source_joint;
00401       CharacterJoint *dest_joint;
00402       DCAST_INTO_V(source_joint, from->_parts[i]);
00403       DCAST_INTO_V(dest_joint, _parts[i]);
00404 
00405       CharacterJoint::NodeList::const_iterator ai;
00406       for (ai = source_joint->_net_transform_nodes.begin();
00407            ai != source_joint->_net_transform_nodes.end();
00408            ++ai) {
00409         PandaNode *source_node = (*ai);
00410 
00411         NodeMap::const_iterator mi;
00412         mi = node_map.find(source_node);
00413         if (mi != node_map.end()) {
00414           PandaNode *dest_node = (*mi).second;
00415 
00416           // Here's an internal joint that the source Character was
00417           // animating directly.  We'll animate our corresponding
00418           // joint the same way.
00419           dest_joint->add_net_transform(dest_node);
00420         }
00421       }
00422 
00423       for (ai = source_joint->_local_transform_nodes.begin();
00424            ai != source_joint->_local_transform_nodes.end();
00425            ++ai) {
00426         PandaNode *source_node = (*ai);
00427 
00428         NodeMap::const_iterator mi;
00429         mi = node_map.find(source_node);
00430         if (mi != node_map.end()) {
00431           PandaNode *dest_node = (*mi).second;
00432 
00433           // Here's an internal joint that the source Character was
00434           // animating directly.  We'll animate our corresponding
00435           // joint the same way.
00436           dest_joint->add_local_transform(dest_node);
00437         }
00438       }
00439     }
00440   }
00441 }
00442 
00443 
00444 ////////////////////////////////////////////////////////////////////
00445 //     Function: Character::register_with_read_factory
00446 //       Access: Public, Static
00447 //  Description: Tells the BamReader how to create objects of type
00448 //               Character.
00449 ////////////////////////////////////////////////////////////////////
00450 void Character::
00451 register_with_read_factory() {
00452   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00453 }
00454 
00455 ////////////////////////////////////////////////////////////////////
00456 //     Function: Character::write_datagram
00457 //       Access: Public, Virtual
00458 //  Description: Writes the contents of this object to the datagram
00459 //               for shipping out to a Bam file.
00460 ////////////////////////////////////////////////////////////////////
00461 void Character::
00462 write_datagram(BamWriter *manager, Datagram &dg) {
00463   PartBundleNode::write_datagram(manager, dg);
00464   _cv.write_datagram(manager, dg);
00465   manager->write_pointer(dg, _computed_vertices);
00466 
00467   dg.add_uint16(_parts.size());
00468   Parts::const_iterator pi;
00469   for (pi = _parts.begin(); pi != _parts.end(); pi++) {
00470     manager->write_pointer(dg, (*pi));
00471   }
00472 }
00473 
00474 ////////////////////////////////////////////////////////////////////
00475 //     Function: Character::complete_pointers
00476 //       Access: Public, Virtual
00477 //  Description: Receives an array of pointers, one for each time
00478 //               manager->read_pointer() was called in fillin().
00479 //               Returns the number of pointers processed.
00480 ////////////////////////////////////////////////////////////////////
00481 int Character::
00482 complete_pointers(TypedWritable **p_list, BamReader *manager) {
00483   int pi = PartBundleNode::complete_pointers(p_list, manager);
00484   _computed_vertices = DCAST(ComputedVertices, p_list[pi++]);
00485 
00486   int num_parts = _parts.size();
00487   for (int i = 0; i < num_parts; i++) {
00488     _parts[i] = DCAST(PartGroup, p_list[pi++]);
00489   }
00490 
00491   return pi;
00492 }
00493 
00494 ////////////////////////////////////////////////////////////////////
00495 //     Function: Character::make_from_bam
00496 //       Access: Protected, Static
00497 //  Description: This function is called by the BamReader's factory
00498 //               when a new object of type Character is encountered
00499 //               in the Bam file.  It should create the Character
00500 //               and extract its information from the file.
00501 ////////////////////////////////////////////////////////////////////
00502 TypedWritable *Character::
00503 make_from_bam(const FactoryParams &params) {
00504   Character *node = new Character("");
00505   DatagramIterator scan;
00506   BamReader *manager;
00507 
00508   parse_params(params, scan, manager);
00509   node->fillin(scan, manager);
00510 
00511   return node;
00512 }
00513 
00514 ////////////////////////////////////////////////////////////////////
00515 //     Function: Character::fillin
00516 //       Access: Protected
00517 //  Description: This internal function is called by make_from_bam to
00518 //               read in all of the relevant data from the BamFile for
00519 //               the new Character.
00520 ////////////////////////////////////////////////////////////////////
00521 void Character::
00522 fillin(DatagramIterator &scan, BamReader *manager) {
00523   PartBundleNode::fillin(scan, manager);
00524   _cv.fillin(scan, manager);
00525   manager->read_pointer(scan);
00526 
00527   // Read the number of parts to expect in the _parts list, and then
00528   // fill the array up with NULLs.  We'll fill in the actual values in
00529   // complete_pointers, later.
00530   int num_parts = scan.get_uint16();
00531   _parts.clear();
00532   _parts.reserve(num_parts);
00533   for (int i = 0; i < num_parts; i++) {
00534     manager->read_pointer(scan);
00535     _parts.push_back((PartGroup *)NULL);
00536   }
00537 
00538 #ifdef DO_PSTATS
00539   // Reinitialize our collector with our name, now that we know it.
00540   if (has_name()) {
00541     _char_pcollector =
00542       PStatCollector(_anim_pcollector, get_name());
00543   }
00544 #endif
00545 }

Generated on Fri May 2 00:35:20 2003 for Panda by doxygen1.3