00001 // Filename: characterJoint.cxx 00002 // Created by: drose (23Feb99) 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 "characterJoint.h" 00020 #include "config_char.h" 00021 00022 #include "compose_matrix.h" 00023 #include "datagram.h" 00024 #include "datagramIterator.h" 00025 #include "bamReader.h" 00026 #include "bamWriter.h" 00027 00028 TypeHandle CharacterJoint::_type_handle; 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Function: CharacterJoint::Default Constructor 00032 // Access: Protected 00033 // Description: For internal use only. 00034 //////////////////////////////////////////////////////////////////// 00035 CharacterJoint:: 00036 CharacterJoint() { 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: CharacterJoint::Copy Constructor 00041 // Access: Protected 00042 // Description: 00043 //////////////////////////////////////////////////////////////////// 00044 CharacterJoint:: 00045 CharacterJoint(const CharacterJoint ©) : 00046 MovingPartMatrix(copy), 00047 _net_transform(copy._net_transform), 00048 _initial_net_transform_inverse(copy._initial_net_transform_inverse) 00049 { 00050 // We don't copy the sets of transform nodes. 00051 } 00052 00053 //////////////////////////////////////////////////////////////////// 00054 // Function: CharacterJoint::Constructor 00055 // Access: Public 00056 // Description: 00057 //////////////////////////////////////////////////////////////////// 00058 CharacterJoint:: 00059 CharacterJoint(PartGroup *parent, const string &name, 00060 const LMatrix4f &initial_value) 00061 : MovingPartMatrix(parent, name, initial_value) 00062 { 00063 // Now that we've constructed and we're in the tree, let's call 00064 // update_internals() to get our _net_transform set properly. 00065 update_internals(parent, true, false); 00066 00067 // And then compute its inverse. This is needed for 00068 // ComputedVertices, during animation. 00069 _initial_net_transform_inverse = invert(_net_transform); 00070 } 00071 00072 //////////////////////////////////////////////////////////////////// 00073 // Function: CharacterJoint::make_copy 00074 // Access: Public, Virtual 00075 // Description: Allocates and returns a new copy of the node. 00076 // Children are not copied, but see copy_subgraph(). 00077 //////////////////////////////////////////////////////////////////// 00078 PartGroup *CharacterJoint:: 00079 make_copy() const { 00080 return new CharacterJoint(*this); 00081 } 00082 00083 //////////////////////////////////////////////////////////////////// 00084 // Function: CharacterJoint::update_internals 00085 // Access: Public, Virtual 00086 // Description: This is called by do_update() whenever the part or 00087 // some ancestor has changed values. It is a hook for 00088 // derived classes to update whatever cache they may 00089 // have that depends on these. 00090 // 00091 // The return value is true if the part has changed as a 00092 // result of the update, or false otherwise. 00093 // 00094 // In the case of a CharacterJoint, of course, it means 00095 // to recompute the joint angles and associated 00096 // transforms for this particular joint. 00097 //////////////////////////////////////////////////////////////////// 00098 bool CharacterJoint:: 00099 update_internals(PartGroup *parent, bool self_changed, bool parent_changed) { 00100 nassertr(parent != (PartGroup *)NULL, false); 00101 00102 bool net_changed = false; 00103 if (parent->is_of_type(CharacterJoint::get_class_type())) { 00104 // The joint is not a toplevel joint; its parent therefore affects 00105 // its net transform. 00106 if (parent_changed || self_changed) { 00107 CharacterJoint *parent_joint = DCAST(CharacterJoint, parent); 00108 00109 _net_transform = _value * parent_joint->_net_transform; 00110 net_changed = true; 00111 } 00112 00113 } else { 00114 // The joint *is* a toplevel joint, and the only thing that 00115 // affects its net transform is the joint itself. 00116 if (self_changed) { 00117 _net_transform = _value; 00118 net_changed = true; 00119 } 00120 } 00121 00122 if (net_changed && !_net_transform_nodes.empty()) { 00123 CPT(TransformState) t = TransformState::make_mat(_net_transform); 00124 00125 NodeList::iterator ai; 00126 ai = _net_transform_nodes.begin(); 00127 while (ai != _net_transform_nodes.end()) { 00128 PandaNode *node = *ai; 00129 node->set_transform(t); 00130 ++ai; 00131 } 00132 } 00133 00134 if (self_changed && !_local_transform_nodes.empty()) { 00135 CPT(TransformState) t = TransformState::make_mat(_value); 00136 00137 NodeList::iterator ai; 00138 ai = _local_transform_nodes.begin(); 00139 while (ai != _local_transform_nodes.end()) { 00140 PandaNode *node = *ai; 00141 node->set_transform(t); 00142 ++ai; 00143 } 00144 } 00145 00146 return self_changed || net_changed; 00147 } 00148 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function: CharacterJoint::add_net_transform 00152 // Access: Published 00153 // Description: Adds the indicated node to the list of nodes that will 00154 // be updated each frame with the joint's net transform 00155 // from the root. Returns true if the node is 00156 // successfully added, false if it had already been 00157 // added. 00158 //////////////////////////////////////////////////////////////////// 00159 bool CharacterJoint:: 00160 add_net_transform(PandaNode *node) { 00161 return _net_transform_nodes.insert(node).second; 00162 } 00163 00164 //////////////////////////////////////////////////////////////////// 00165 // Function: CharacterJoint::remove_net_transform 00166 // Access: Published 00167 // Description: Removes the indicated node from the list of nodes that 00168 // will be updated each frame with the joint's net 00169 // transform from the root. Returns true if the node is 00170 // successfully removed, false if it was not on the 00171 // list. 00172 //////////////////////////////////////////////////////////////////// 00173 bool CharacterJoint:: 00174 remove_net_transform(PandaNode *node) { 00175 return (_net_transform_nodes.erase(node) > 0); 00176 } 00177 00178 //////////////////////////////////////////////////////////////////// 00179 // Function: CharacterJoint::has_net_transform 00180 // Access: Published 00181 // Description: Returns true if the node is on the list of nodes that 00182 // will be updated each frame with the joint's net 00183 // transform from the root, false otherwise. 00184 //////////////////////////////////////////////////////////////////// 00185 bool CharacterJoint:: 00186 has_net_transform(PandaNode *node) const { 00187 return (_net_transform_nodes.count(node) > 0); 00188 } 00189 00190 //////////////////////////////////////////////////////////////////// 00191 // Function: CharacterJoint::clear_net_transforms 00192 // Access: Published 00193 // Description: Removes all nodes from the list of nodes that will be 00194 // updated each frame with the joint's net transform 00195 // from the root. 00196 //////////////////////////////////////////////////////////////////// 00197 void CharacterJoint:: 00198 clear_net_transforms() { 00199 _net_transform_nodes.clear(); 00200 } 00201 00202 //////////////////////////////////////////////////////////////////// 00203 // Function: CharacterJoint::add_local_transform 00204 // Access: Published 00205 // Description: Adds the indicated node to the list of nodes that will 00206 // be updated each frame with the joint's local 00207 // transform from its parent. Returns true if the node 00208 // is successfully added, false if it had already been 00209 // added. 00210 //////////////////////////////////////////////////////////////////// 00211 bool CharacterJoint:: 00212 add_local_transform(PandaNode *node) { 00213 return _local_transform_nodes.insert(node).second; 00214 } 00215 00216 //////////////////////////////////////////////////////////////////// 00217 // Function: CharacterJoint::remove_local_transform 00218 // Access: Published 00219 // Description: Removes the indicated node from the list of nodes that 00220 // will be updated each frame with the joint's local 00221 // transform from its parent. Returns true if the node 00222 // is successfully removed, false if it was not on the 00223 // list. 00224 //////////////////////////////////////////////////////////////////// 00225 bool CharacterJoint:: 00226 remove_local_transform(PandaNode *node) { 00227 return (_local_transform_nodes.erase(node) > 0); 00228 } 00229 00230 //////////////////////////////////////////////////////////////////// 00231 // Function: CharacterJoint::has_local_transform 00232 // Access: Published 00233 // Description: Returns true if the node is on the list of nodes that 00234 // will be updated each frame with the joint's local 00235 // transform from its parent, false otherwise. 00236 //////////////////////////////////////////////////////////////////// 00237 bool CharacterJoint:: 00238 has_local_transform(PandaNode *node) const { 00239 return (_local_transform_nodes.count(node) > 0); 00240 } 00241 00242 //////////////////////////////////////////////////////////////////// 00243 // Function: CharacterJoint::clear_local_transforms 00244 // Access: Published 00245 // Description: Removes all nodes from the list of nodes that will be 00246 // updated each frame with the joint's local transform 00247 // from its parent. 00248 //////////////////////////////////////////////////////////////////// 00249 void CharacterJoint:: 00250 clear_local_transforms() { 00251 _local_transform_nodes.clear(); 00252 } 00253 00254 //////////////////////////////////////////////////////////////////// 00255 // Function: CharacterJoint::get_transform 00256 // Access: Published 00257 // Description: Copies the joint's current transform into the 00258 // indicated matrix. 00259 //////////////////////////////////////////////////////////////////// 00260 void CharacterJoint:: 00261 get_transform(LMatrix4f &transform) const { 00262 transform = _value; 00263 } 00264 00265 //////////////////////////////////////////////////////////////////// 00266 // Function: CharacterJoint::write_datagram 00267 // Access: Public 00268 // Description: Function to write the important information in 00269 // the particular object to a Datagram 00270 //////////////////////////////////////////////////////////////////// 00271 void CharacterJoint:: 00272 write_datagram(BamWriter *manager, Datagram &me) 00273 { 00274 NodeList::iterator ni; 00275 MovingPartMatrix::write_datagram(manager, me); 00276 00277 me.add_uint16(_net_transform_nodes.size()); 00278 for(ni = _net_transform_nodes.begin(); 00279 ni != _net_transform_nodes.end(); 00280 ni++) { 00281 manager->write_pointer(me, (*ni)); 00282 } 00283 00284 me.add_uint16(_local_transform_nodes.size()); 00285 for(ni = _local_transform_nodes.begin(); 00286 ni != _local_transform_nodes.end(); 00287 ni++) { 00288 manager->write_pointer(me, (*ni)); 00289 } 00290 00291 _initial_net_transform_inverse.write_datagram(me); 00292 } 00293 00294 //////////////////////////////////////////////////////////////////// 00295 // Function: CharacterJoint::fillin 00296 // Access: Protected 00297 // Description: Function that reads out of the datagram (or asks 00298 // manager to read) all of the data that is needed to 00299 // re-create this object and stores it in the appropiate 00300 // place 00301 //////////////////////////////////////////////////////////////////// 00302 void CharacterJoint:: 00303 fillin(DatagramIterator &scan, BamReader *manager) { 00304 int i; 00305 MovingPartMatrix::fillin(scan, manager); 00306 00307 _num_net_nodes = scan.get_uint16(); 00308 for(i = 0; i < _num_net_nodes; i++) { 00309 manager->read_pointer(scan); 00310 } 00311 00312 _num_local_nodes = scan.get_uint16(); 00313 for(i = 0; i < _num_local_nodes; i++) { 00314 manager->read_pointer(scan); 00315 } 00316 00317 _initial_net_transform_inverse.read_datagram(scan); 00318 } 00319 00320 //////////////////////////////////////////////////////////////////// 00321 // Function: CharacterJoint::complete_pointers 00322 // Access: Public 00323 // Description: Takes in a vector of pointes to TypedWritable 00324 // objects that correspond to all the requests for 00325 // pointers that this object made to BamReader. 00326 //////////////////////////////////////////////////////////////////// 00327 int CharacterJoint:: 00328 complete_pointers(TypedWritable **p_list, BamReader* manager) 00329 { 00330 int pi = MovingPartMatrix::complete_pointers(p_list, manager); 00331 00332 int i; 00333 for (i = 0; i < _num_net_nodes; i++) { 00334 add_net_transform(DCAST(PandaNode, p_list[pi++])); 00335 } 00336 00337 for (i = 0; i < _num_local_nodes; i++) { 00338 add_local_transform(DCAST(PandaNode, p_list[pi++])); 00339 } 00340 00341 return pi; 00342 } 00343 00344 //////////////////////////////////////////////////////////////////// 00345 // Function: CharacterJoint::make_CharacterJoint 00346 // Access: Protected 00347 // Description: Factory method to generate a CharacterJoint object 00348 //////////////////////////////////////////////////////////////////// 00349 TypedWritable* CharacterJoint:: 00350 make_CharacterJoint(const FactoryParams ¶ms) 00351 { 00352 CharacterJoint *me = new CharacterJoint; 00353 DatagramIterator scan; 00354 BamReader *manager; 00355 00356 parse_params(params, scan, manager); 00357 me->fillin(scan, manager); 00358 return me; 00359 } 00360 00361 //////////////////////////////////////////////////////////////////// 00362 // Function: CharacterJoint::register_with_factory 00363 // Access: Public, Static 00364 // Description: Factory method to generate a CharacterJoint object 00365 //////////////////////////////////////////////////////////////////// 00366 void CharacterJoint:: 00367 register_with_read_factory(void) 00368 { 00369 BamReader::get_factory()->register_factory(get_class_type(), make_CharacterJoint); 00370 } 00371 00372