Panda3D

animChannelMatrixDynamic.cxx

00001 // Filename: animChannelMatrixDynamic.cxx
00002 // Created by:  drose (20Oct03)
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 "animChannelMatrixDynamic.h"
00016 #include "animBundle.h"
00017 #include "config_chan.h"
00018 
00019 #include "compose_matrix.h"
00020 #include "indent.h"
00021 #include "datagram.h"
00022 #include "datagramIterator.h"
00023 #include "bamReader.h"
00024 #include "bamWriter.h"
00025 
00026 TypeHandle AnimChannelMatrixDynamic::_type_handle;
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: AnimChannelMatrixDynamic::Constructor
00030 //       Access: Protected
00031 //  Description: For use only with the bam reader.
00032 /////////////////////////////////////////////////////////////
00033 AnimChannelMatrixDynamic::
00034 AnimChannelMatrixDynamic() {
00035 }
00036 
00037 ////////////////////////////////////////////////////////////////////
00038 //     Function: AnimChannelMatrixDynamic::Copy Constructor
00039 //       Access: Protected
00040 //  Description: Creates a new AnimChannelMatrixDynamic, just like
00041 //               this one, without copying any children.  The new copy
00042 //               is added to the indicated parent.  Intended to be
00043 //               called by make_copy() only.
00044 ////////////////////////////////////////////////////////////////////
00045 AnimChannelMatrixDynamic::
00046 AnimChannelMatrixDynamic(AnimGroup *parent, const AnimChannelMatrixDynamic &copy) : 
00047   AnimChannelMatrix(parent, copy),
00048   _value_node(copy._value_node),
00049   _value(copy._value),
00050   _last_value(NULL)
00051 {
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //     Function: AnimChannelMatrixDynamic::Constructor
00056 //       Access: Public
00057 //  Description:
00058 ////////////////////////////////////////////////////////////////////
00059 AnimChannelMatrixDynamic::
00060 AnimChannelMatrixDynamic(const string &name)
00061   : AnimChannelMatrix(name) 
00062 {
00063   _value = TransformState::make_identity();
00064   _last_value = NULL;  // This is impossible; thus, has_changed() will
00065                        // always return true the first time.
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: AnimChannelMatrixDynamic::has_changed
00070 //       Access: Public, Virtual
00071 //  Description: Returns true if the value has changed since the last
00072 //               call to has_changed().  last_frame is the frame
00073 //               number of the last call; this_frame is the current
00074 //               frame number.
00075 ////////////////////////////////////////////////////////////////////
00076 bool AnimChannelMatrixDynamic::
00077 has_changed(int, double, int, double) {
00078   if (_value_node != (PandaNode *)NULL) {
00079     _value = _value_node->get_transform();
00080   }
00081   bool has_changed = (_value != _last_value);
00082   _last_value = _value;
00083   return has_changed;
00084 }
00085 
00086 ////////////////////////////////////////////////////////////////////
00087 //     Function: AnimChannelMatrixDynamic::get_value
00088 //       Access: Public, Virtual
00089 //  Description: Gets the value of the channel at the indicated frame.
00090 ////////////////////////////////////////////////////////////////////
00091 void AnimChannelMatrixDynamic::
00092 get_value(int, LMatrix4 &mat) {
00093   if (_value_node != (PandaNode *)NULL) {
00094     _value = _value_node->get_transform();
00095   }
00096   mat = _value->get_mat();
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function: AnimChannelMatrixDynamic::get_value_no_scale_shear
00101 //       Access: Public, Virtual
00102 //  Description: Gets the value of the channel at the indicated frame,
00103 //               without any scale or shear information.
00104 ////////////////////////////////////////////////////////////////////
00105 void AnimChannelMatrixDynamic::
00106 get_value_no_scale_shear(int, LMatrix4 &mat) {
00107   if (_value_node != (PandaNode *)NULL) {
00108     _value = _value_node->get_transform();
00109   }
00110   if (_value->has_scale() || _value->has_shear()) {
00111     compose_matrix(mat, LVecBase3(1.0f, 1.0f, 1.0f),
00112                    _value->get_hpr(), _value->get_pos());
00113   } else {
00114     mat = _value->get_mat();
00115   }
00116 }
00117 
00118 ////////////////////////////////////////////////////////////////////
00119 //     Function: AnimChannelMatrixDynamic::get_scale
00120 //       Access: Public, Virtual
00121 //  Description: Gets the scale value at the indicated frame.
00122 ////////////////////////////////////////////////////////////////////
00123 void AnimChannelMatrixDynamic::
00124 get_scale(int, LVecBase3 &scale) {
00125   if (_value_node != (PandaNode *)NULL) {
00126     _value = _value_node->get_transform();
00127   }
00128   scale = _value->get_scale();
00129 }
00130 
00131 ////////////////////////////////////////////////////////////////////
00132 //     Function: AnimChannelMatrixDynamic::get_hpr
00133 //       Access: Public, Virtual
00134 //  Description: Returns the h, p, and r components associated
00135 //               with the current frame.  As above, this only makes
00136 //               sense for a matrix-type channel.
00137 ////////////////////////////////////////////////////////////////////
00138 void AnimChannelMatrixDynamic::
00139 get_hpr(int, LVecBase3 &hpr) {
00140   if (_value_node != (PandaNode *)NULL) {
00141     _value = _value_node->get_transform();
00142   }
00143   hpr = _value->get_hpr();
00144 }
00145 
00146 ////////////////////////////////////////////////////////////////////
00147 //     Function: AnimChannelMatrixDynamic::get_quat
00148 //       Access: Public, Virtual
00149 //  Description: Returns the rotation component associated with the
00150 //               current frame, expressed as a quaternion.  As above,
00151 //               this only makes sense for a matrix-type channel.
00152 ////////////////////////////////////////////////////////////////////
00153 void AnimChannelMatrixDynamic::
00154 get_quat(int, LQuaternion &quat) {
00155   if (_value_node != (PandaNode *)NULL) {
00156     _value = _value_node->get_transform();
00157   }
00158   quat = _value->get_quat();
00159 }
00160 
00161 ////////////////////////////////////////////////////////////////////
00162 //     Function: AnimChannelMatrixDynamic::get_pos
00163 //       Access: Public, Virtual
00164 //  Description: Returns the x, y, and z translation components
00165 //               associated with the current frame.  As above, this
00166 //               only makes sense for a matrix-type channel.
00167 ////////////////////////////////////////////////////////////////////
00168 void AnimChannelMatrixDynamic::
00169 get_pos(int, LVecBase3 &pos) {
00170   if (_value_node != (PandaNode *)NULL) {
00171     _value = _value_node->get_transform();
00172   }
00173   pos = _value->get_pos();
00174 }
00175 
00176 ////////////////////////////////////////////////////////////////////
00177 //     Function: AnimChannelMatrixDynamic::get_shear
00178 //       Access: Public, Virtual
00179 //  Description: Returns the a, b, and c shear components associated
00180 //               with the current frame.  As above, this only makes
00181 //               sense for a matrix-type channel.
00182 ////////////////////////////////////////////////////////////////////
00183 void AnimChannelMatrixDynamic::
00184 get_shear(int, LVecBase3 &shear) {
00185   if (_value_node != (PandaNode *)NULL) {
00186     _value = _value_node->get_transform();
00187   }
00188   shear = _value->get_shear();
00189 }
00190 
00191 ////////////////////////////////////////////////////////////////////
00192 //     Function: AnimChannelMatrixDynamic::set_value
00193 //       Access: Published
00194 //  Description: Explicitly sets the matrix value.
00195 ////////////////////////////////////////////////////////////////////
00196 void AnimChannelMatrixDynamic::
00197 set_value(const LMatrix4 &value) {
00198   _value = TransformState::make_mat(value);
00199   _value_node.clear();
00200 }
00201 
00202 ////////////////////////////////////////////////////////////////////
00203 //     Function: AnimChannelMatrixDynamic::set_value
00204 //       Access: Published
00205 //  Description: Explicitly sets the matrix value, using the indicated
00206 //               TransformState object as a convenience.
00207 ////////////////////////////////////////////////////////////////////
00208 void AnimChannelMatrixDynamic::
00209 set_value(const TransformState *value) {
00210   _value = value;
00211   _value_node.clear();
00212 }
00213 
00214 ////////////////////////////////////////////////////////////////////
00215 //     Function: AnimChannelMatrixDynamic::set_value_node
00216 //       Access: Published
00217 //  Description: Specifies a node whose transform will be queried each
00218 //               frame to implicitly specify the transform of this
00219 //               joint.
00220 ////////////////////////////////////////////////////////////////////
00221 void AnimChannelMatrixDynamic::
00222 set_value_node(PandaNode *value_node) {
00223   _value_node = value_node;
00224   if (_value_node != (PandaNode *)NULL) {
00225     _value = _value_node->get_transform();
00226   }
00227 }
00228 
00229 ////////////////////////////////////////////////////////////////////
00230 //     Function: AnimChannelMatrixDynamic::make_copy
00231 //       Access: Protected, Virtual
00232 //  Description: Returns a copy of this object, and attaches it to the
00233 //               indicated parent (which may be NULL only if this is
00234 //               an AnimBundle).  Intended to be called by
00235 //               copy_subtree() only.
00236 ////////////////////////////////////////////////////////////////////
00237 AnimGroup *AnimChannelMatrixDynamic::
00238 make_copy(AnimGroup *parent) const {
00239   return new AnimChannelMatrixDynamic(parent, *this);
00240 }
00241 
00242 
00243 ////////////////////////////////////////////////////////////////////
00244 //     Function: AnimChannelMatrixDynamic::write_datagram
00245 //       Access: Public
00246 //  Description: Function to write the important information in
00247 //               the particular object to a Datagram
00248 ////////////////////////////////////////////////////////////////////
00249 void AnimChannelMatrixDynamic::
00250 write_datagram(BamWriter *manager, Datagram &dg) {
00251   AnimChannelMatrix::write_datagram(manager, dg);
00252   manager->write_pointer(dg, _value_node);
00253   manager->write_pointer(dg, _value);
00254 }
00255 
00256 ////////////////////////////////////////////////////////////////////
00257 //     Function: AnimChannelMatrixDynamic::complete_pointers
00258 //       Access: Public, Virtual
00259 //  Description: Receives an array of pointers, one for each time
00260 //               manager->read_pointer() was called in fillin().
00261 //               Returns the number of pointers processed.
00262 ////////////////////////////////////////////////////////////////////
00263 int AnimChannelMatrixDynamic::
00264 complete_pointers(TypedWritable **p_list, BamReader *manager) {
00265   int pi = AnimChannelMatrix::complete_pointers(p_list, manager);
00266 
00267   // Get the _value_node and _value pointers.
00268   _value_node = DCAST(PandaNode, p_list[pi++]);
00269   _value = DCAST(TransformState, p_list[pi++]);
00270 
00271   return pi;
00272 }
00273 
00274 ////////////////////////////////////////////////////////////////////
00275 //     Function: AnimChannelMatrixDynamic::fillin
00276 //       Access: Public
00277 //  Description: Function that reads out of the datagram (or asks
00278 //               manager to read) all of the data that is needed to
00279 //               re-create this object and stores it in the appropiate
00280 //               place
00281 ////////////////////////////////////////////////////////////////////
00282 void AnimChannelMatrixDynamic::
00283 fillin(DatagramIterator &scan, BamReader *manager) {
00284   AnimChannelMatrix::fillin(scan, manager);
00285 
00286   // Read the _value_node and _value pointers.
00287   manager->read_pointer(scan);
00288   manager->read_pointer(scan);
00289 }
00290 
00291 ////////////////////////////////////////////////////////////////////
00292 //     Function: AnimChannelMatrixDynamic::make_AnimChannelMatrixDynamic
00293 //       Access: Public
00294 //  Description: Factory method to generate an
00295 //               AnimChannelMatrixDynamic object.
00296 ////////////////////////////////////////////////////////////////////
00297 TypedWritable *AnimChannelMatrixDynamic::
00298 make_AnimChannelMatrixDynamic(const FactoryParams &params) {
00299   AnimChannelMatrixDynamic *me = new AnimChannelMatrixDynamic;
00300   DatagramIterator scan;
00301   BamReader *manager;
00302 
00303   parse_params(params, scan, manager);
00304   me->fillin(scan, manager);
00305   return me;
00306 }
00307 
00308 ////////////////////////////////////////////////////////////////////
00309 //     Function: AnimChannelMatrixDynamic::register_with_factory
00310 //       Access: Public, Static
00311 //  Description: Factory method to generate an
00312 //               AnimChannelMatrixDynamic object.
00313 ////////////////////////////////////////////////////////////////////
00314 void AnimChannelMatrixDynamic::
00315 register_with_read_factory() {
00316   BamReader::get_factory()->register_factory(get_class_type(), make_AnimChannelMatrixDynamic);
00317 }
00318 
00319 
 All Classes Functions Variables Enumerations