Panda3D
 All Classes Functions Variables Enumerations
characterJointEffect.cxx
00001 // Filename: characterJointEffect.cxx
00002 // Created by:  drose (26Jul06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "characterJointEffect.h"
00016 #include "cullTraverser.h"
00017 #include "cullTraverserData.h"
00018 #include "nodePath.h"
00019 #include "look_at.h"
00020 #include "bamReader.h"
00021 #include "bamWriter.h"
00022 #include "datagram.h"
00023 #include "datagramIterator.h"
00024 
00025 TypeHandle CharacterJointEffect::_type_handle;
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: CharacterJointEffect::make
00029 //       Access: Published, Static
00030 //  Description: Constructs a new CharacterJointEffect object that
00031 //               references the indicated character.  When a relative
00032 //               get_transform() is called on the node that contains
00033 //               the CharacterJointEffect, it will implicitly call
00034 //               character->update() first.
00035 ////////////////////////////////////////////////////////////////////
00036 CPT(RenderEffect) CharacterJointEffect::
00037 make(Character *character) {
00038   CharacterJointEffect *effect = new CharacterJointEffect;
00039   effect->_character = character;
00040 
00041   CPT(RenderEffect) new_effect_raw = return_new(effect);
00042   const CharacterJointEffect *new_effect;
00043   DCAST_INTO_R(new_effect, new_effect_raw, new_effect_raw);
00044 
00045   // It is possible that the CharacterJointEffect we have now is a
00046   // different CharacterJointEffect to a different Character which has
00047   // since been deleted, but which had the same memory address of our
00048   // current character.  If this happened, we have to force-update the
00049   // CharacterJointEffect to tell its weak pointer that it is no
00050   // longer invalid (and that it now points to this once-again-live
00051   // Character object).
00052 
00053   // This is a little weird, because it means any nodes that used to
00054   // be pointing to a deleted Character object (and knew they were
00055   // pointing to a deleted Character object) will suddenly be pointing
00056   // to a new, non-deleted Character object--and the wrong Character
00057   // object, no less.  But there's no other way to handle this, since
00058   // we can't make the CharacterJointEffect's compare function base
00059   // itself on whether its pointer is valid or not.
00060 
00061   if (!new_effect->_character.is_valid_pointer()) {
00062     nassertr(new_effect->_character.get_orig() == character, new_effect_raw);
00063     ((CharacterJointEffect *)new_effect)->_character = character;
00064   }
00065 
00066   return new_effect_raw;
00067 }
00068 
00069 ////////////////////////////////////////////////////////////////////
00070 //     Function: CharacterJointEffect::safe_to_transform
00071 //       Access: Public, Virtual
00072 //  Description: Returns true if it is generally safe to transform
00073 //               this particular kind of RenderEffect by calling the
00074 //               xform() method, false otherwise.
00075 ////////////////////////////////////////////////////////////////////
00076 bool CharacterJointEffect::
00077 safe_to_transform() const {
00078   // We now accept that it will be OK to transform the joint--we allow
00079   // this on the assumption that anything that transforms the joint
00080   // will also transform the Character node, above the joint.
00081   return true;
00082 }
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //     Function: CharacterJointEffect::safe_to_combine
00086 //       Access: Public, Virtual
00087 //  Description: Returns true if this kind of effect can safely be
00088 //               combined with sibling nodes that share the exact same
00089 //               effect, or false if this is not a good idea.
00090 ////////////////////////////////////////////////////////////////////
00091 bool CharacterJointEffect::
00092 safe_to_combine() const {
00093   return false;
00094 }
00095 
00096 ////////////////////////////////////////////////////////////////////
00097 //     Function: CharacterJointEffect::output
00098 //       Access: Public, Virtual
00099 //  Description: 
00100 ////////////////////////////////////////////////////////////////////
00101 void CharacterJointEffect::
00102 output(ostream &out) const {
00103   out << get_type();
00104   if (_character.is_valid_pointer()) {
00105     out << "(" << _character->get_name() << ")";
00106   } else {
00107     out << "(**invalid**)";
00108   }
00109 }
00110 
00111 ////////////////////////////////////////////////////////////////////
00112 //     Function: CharacterJointEffect::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 effect during the cull
00118 //               traversal.
00119 ////////////////////////////////////////////////////////////////////
00120 bool CharacterJointEffect::
00121 has_cull_callback() const {
00122   return true;
00123 }
00124 
00125 ////////////////////////////////////////////////////////////////////
00126 //     Function: CharacterJointEffect::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 //               At the time this function is called, the current
00136 //               node's transform and state have not yet been applied
00137 //               to the net_transform and net_state.  This callback
00138 //               may modify the node_transform and node_state to apply
00139 //               an effective change to the render state at this
00140 //               level.
00141 ////////////////////////////////////////////////////////////////////
00142 void CharacterJointEffect::
00143 cull_callback(CullTraverser *trav, CullTraverserData &data,
00144               CPT(TransformState) &node_transform,
00145               CPT(RenderState) &) const {
00146   CPT(TransformState) dummy_transform = TransformState::make_identity();
00147   adjust_transform(dummy_transform, node_transform, data.node());
00148 }
00149 
00150 ////////////////////////////////////////////////////////////////////
00151 //     Function: CharacterJointEffect::has_adjust_transform
00152 //       Access: Public, Virtual
00153 //  Description: Should be overridden by derived classes to return
00154 //               true if adjust_transform() has been defined, and
00155 //               therefore the RenderEffect has some effect on the
00156 //               node's apparent local and net transforms.
00157 ////////////////////////////////////////////////////////////////////
00158 bool CharacterJointEffect::
00159 has_adjust_transform() const {
00160   return true;
00161 }
00162 
00163 ////////////////////////////////////////////////////////////////////
00164 //     Function: CharacterJointEffect::adjust_transform
00165 //       Access: Public, Virtual
00166 //  Description: Performs some operation on the node's apparent net
00167 //               and/or local transforms.  This will only be called if
00168 //               has_adjust_transform() is redefined to return true.
00169 //
00170 //               Both parameters are in/out.  The original transforms
00171 //               will be passed in, and they may (or may not) be
00172 //               modified in-place by the RenderEffect.
00173 ////////////////////////////////////////////////////////////////////
00174 void CharacterJointEffect::
00175 adjust_transform(CPT(TransformState) &net_transform,
00176                  CPT(TransformState) &node_transform,
00177                  PandaNode *node) const {
00178   if (_character.is_valid_pointer()) {
00179     _character->update();
00180   }
00181   node_transform = node->get_transform();
00182 }
00183 
00184 
00185 ////////////////////////////////////////////////////////////////////
00186 //     Function: CharacterJointEffect::compare_to_impl
00187 //       Access: Protected, Virtual
00188 //  Description: Intended to be overridden by derived CharacterJointEffect
00189 //               types to return a unique number indicating whether
00190 //               this CharacterJointEffect is equivalent to the other one.
00191 //
00192 //               This should return 0 if the two CharacterJointEffect objects
00193 //               are equivalent, a number less than zero if this one
00194 //               should be sorted before the other one, and a number
00195 //               greater than zero otherwise.
00196 //
00197 //               This will only be called with two CharacterJointEffect
00198 //               objects whose get_type() functions return the same.
00199 ////////////////////////////////////////////////////////////////////
00200 int CharacterJointEffect::
00201 compare_to_impl(const RenderEffect *other) const {
00202   const CharacterJointEffect *ta;
00203   DCAST_INTO_R(ta, other, 0);
00204 
00205   if (_character.get_orig() != ta->_character.get_orig()) {
00206     return _character.get_orig() < ta->_character.get_orig() ? -1 : 1;
00207   }
00208 
00209   // As tempting as it is to include the sense of whether the
00210   // character pointer is valid in this sorting, we can't, because
00211   // that property might change without warning--which would
00212   // invalidate the CharacterJointEffect's position in any maps if we
00213   // used it to determine its sort.
00214 
00215   return 0;
00216 }
00217 
00218 ////////////////////////////////////////////////////////////////////
00219 //     Function: CharacterJointEffect::register_with_read_factory
00220 //       Access: Public, Static
00221 //  Description: Tells the BamReader how to create objects of type
00222 //               CharacterJointEffect.
00223 ////////////////////////////////////////////////////////////////////
00224 void CharacterJointEffect::
00225 register_with_read_factory() {
00226   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00227 }
00228 
00229 ////////////////////////////////////////////////////////////////////
00230 //     Function: CharacterJointEffect::write_datagram
00231 //       Access: Public, Virtual
00232 //  Description: Writes the contents of this object to the datagram
00233 //               for shipping out to a Bam file.
00234 ////////////////////////////////////////////////////////////////////
00235 void CharacterJointEffect::
00236 write_datagram(BamWriter *manager, Datagram &dg) {
00237   RenderEffect::write_datagram(manager, dg);
00238 
00239   if (_character.is_valid_pointer()) {
00240     manager->write_pointer(dg, _character);
00241   } else {
00242     manager->write_pointer(dg, NULL);
00243   }
00244 }
00245 
00246 ////////////////////////////////////////////////////////////////////
00247 //     Function: CharacterJointEffect::complete_pointers
00248 //       Access: Public, Virtual
00249 //  Description: Receives an array of pointers, one for each time
00250 //               manager->read_pointer() was called in fillin().
00251 //               Returns the number of pointers processed.
00252 ////////////////////////////////////////////////////////////////////
00253 int CharacterJointEffect::
00254 complete_pointers(TypedWritable **p_list, BamReader *manager) {
00255   int pi = RenderEffect::complete_pointers(p_list, manager);
00256 
00257   _character = DCAST(Character, p_list[pi++]);
00258 
00259   return pi;
00260 }
00261 
00262 ////////////////////////////////////////////////////////////////////
00263 //     Function: CharacterJointEffect::make_from_bam
00264 //       Access: Protected, Static
00265 //  Description: This function is called by the BamReader's factory
00266 //               when a new object of type CharacterJointEffect is encountered
00267 //               in the Bam file.  It should create the CharacterJointEffect
00268 //               and extract its information from the file.
00269 ////////////////////////////////////////////////////////////////////
00270 TypedWritable *CharacterJointEffect::
00271 make_from_bam(const FactoryParams &params) {
00272   CharacterJointEffect *effect = new CharacterJointEffect;
00273   DatagramIterator scan;
00274   BamReader *manager;
00275 
00276   parse_params(params, scan, manager);
00277   effect->fillin(scan, manager);
00278 
00279   return effect;
00280 }
00281 
00282 ////////////////////////////////////////////////////////////////////
00283 //     Function: CharacterJointEffect::fillin
00284 //       Access: Protected
00285 //  Description: This internal function is called by make_from_bam to
00286 //               read in all of the relevant data from the BamFile for
00287 //               the new CharacterJointEffect.
00288 ////////////////////////////////////////////////////////////////////
00289 void CharacterJointEffect::
00290 fillin(DatagramIterator &scan, BamReader *manager) {
00291   RenderEffect::fillin(scan, manager);
00292 
00293   manager->read_pointer(scan);
00294 }
 All Classes Functions Variables Enumerations