00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00042
00043
00044
00045 Character::
00046 Character(const Character ©) :
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
00054
00055
00056
00057
00058 copy_joints(get_bundle(), copy.get_bundle());
00059 }
00060
00061
00062
00063
00064
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
00075
00076
00077
00078 Character::
00079 ~Character() {
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 PandaNode *Character::
00094 make_copy() const {
00095 return new Character(*this);
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 bool Character::
00107 safe_to_transform() const {
00108 return false;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 bool Character::
00121 has_cull_callback() const {
00122 return true;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 bool Character::
00145 cull_callback(CullTraverser *, CullTraverserData &) {
00146
00147
00148
00149
00150
00151 update_to_now();
00152 return true;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
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
00178
00179
00180
00181
00182
00183
00184 void Character::
00185 update() {
00186
00187 PStatTimer timer(_char_pcollector);
00188
00189
00190 bool any_changed = get_bundle()->update();
00191
00192
00193
00194 if (any_changed || even_animation) {
00195 if (_computed_vertices != (ComputedVertices *)NULL) {
00196 _computed_vertices->update(this);
00197 }
00198 }
00199 }
00200
00201
00202
00203
00204
00205
00206
00207 void Character::
00208 force_update() {
00209
00210 PStatTimer timer(_char_pcollector);
00211
00212
00213 get_bundle()->force_update();
00214
00215
00216 if (_computed_vertices != (ComputedVertices *)NULL) {
00217 _computed_vertices->update(this);
00218 }
00219 }
00220
00221
00222
00223
00224
00225
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
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 void Character::
00264 r_copy_children(const PandaNode *from, PandaNode::InstanceMap &inst_map) {
00265
00266
00267
00268
00269
00270
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
00282
00283
00284
00285
00286
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
00314
00315
00316
00317 dest_child = source_child->copy_subgraph();
00318
00319 } else {
00320
00321
00322
00323
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
00334
00335
00336
00337
00338
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
00389
00390
00391
00392
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
00417
00418
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
00434
00435
00436 dest_joint->add_local_transform(dest_node);
00437 }
00438 }
00439 }
00440 }
00441 }
00442
00443
00444
00445
00446
00447
00448
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
00457
00458
00459
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
00476
00477
00478
00479
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
00496
00497
00498
00499
00500
00501
00502 TypedWritable *Character::
00503 make_from_bam(const FactoryParams ¶ms) {
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
00516
00517
00518
00519
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
00528
00529
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
00540 if (has_name()) {
00541 _char_pcollector =
00542 PStatCollector(_anim_pcollector, get_name());
00543 }
00544 #endif
00545 }