Panda3D

jointVertexTransform.cxx

00001 // Filename: jointVertexTransform.cxx
00002 // Created by:  drose (24Mar05)
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 "jointVertexTransform.h"
00016 #include "datagram.h"
00017 #include "datagramIterator.h"
00018 #include "bamReader.h"
00019 #include "bamWriter.h"
00020 #include "lightMutexHolder.h"
00021 
00022 TypeHandle JointVertexTransform::_type_handle;
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //     Function: JointVertexTransform::Default Constructor
00026 //       Access: Private
00027 //  Description: Constructs an invalid object; used only by the bam
00028 //               loader.
00029 ////////////////////////////////////////////////////////////////////
00030 JointVertexTransform::
00031 JointVertexTransform() :
00032   _matrix_stale(true)
00033 {
00034 }
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //     Function: JointVertexTransform::Constructor
00038 //       Access: Published
00039 //  Description: Constructs a new object that converts vertices from
00040 //               the indicated joint's coordinate space, into the
00041 //               other indicated joint's space.
00042 ////////////////////////////////////////////////////////////////////
00043 JointVertexTransform::
00044 JointVertexTransform(CharacterJoint *joint) :
00045   _joint(joint),
00046   _matrix_stale(true)
00047 {
00048   // Tell the joint that we need to be informed when it moves.
00049   _joint->_vertex_transforms.insert(this);
00050   mark_modified(Thread::get_current_thread());
00051 }
00052 
00053 ////////////////////////////////////////////////////////////////////
00054 //     Function: JointVertexTransform::Destructor
00055 //       Access: Published, Virtual
00056 //  Description: 
00057 ////////////////////////////////////////////////////////////////////
00058 JointVertexTransform::
00059 ~JointVertexTransform() {
00060   // Tell the joint to stop informing us about its motion.
00061   _joint->_vertex_transforms.erase(this);
00062 }
00063 
00064 ////////////////////////////////////////////////////////////////////
00065 //     Function: JointVertexTransform::get_matrix
00066 //       Access: Published, Virtual
00067 //  Description: Stores the transform's matrix in the indicated object.
00068 ////////////////////////////////////////////////////////////////////
00069 void JointVertexTransform::
00070 get_matrix(LMatrix4f &matrix) const {
00071   check_matrix();
00072   matrix = _matrix;
00073 }
00074 
00075 ////////////////////////////////////////////////////////////////////
00076 //     Function: JointVertexTransform::mult_matrix
00077 //       Access: Published, Virtual
00078 //  Description: Premultiplies this transform's matrix with the
00079 //               indicated previous matrix, so that the result is the
00080 //               net composition of the given transform with this
00081 //               transform.  The result is stored in the parameter
00082 //               "result", which should not be the same matrix as
00083 //               previous.
00084 ////////////////////////////////////////////////////////////////////
00085 void JointVertexTransform::
00086 mult_matrix(LMatrix4f &result, const LMatrix4f &previous) const {
00087   check_matrix();
00088   result.multiply(_matrix, previous);
00089 }
00090 
00091 ////////////////////////////////////////////////////////////////////
00092 //     Function: JointVertexTransform::accumulate_matrix
00093 //       Access: Published, Virtual
00094 //  Description: Adds the value of this transform's matrix, modified
00095 //               by the indicated weight, into the indicated
00096 //               accumulation matrix.  This is used to compute the
00097 //               result of several blended transforms.
00098 ////////////////////////////////////////////////////////////////////
00099 void JointVertexTransform::
00100 accumulate_matrix(LMatrix4f &accum, float weight) const {
00101   check_matrix();
00102 
00103   accum._m.m._00 += _matrix._m.m._00 * weight;
00104   accum._m.m._01 += _matrix._m.m._01 * weight;
00105   accum._m.m._02 += _matrix._m.m._02 * weight;
00106   accum._m.m._03 += _matrix._m.m._03 * weight;
00107   
00108   accum._m.m._10 += _matrix._m.m._10 * weight;
00109   accum._m.m._11 += _matrix._m.m._11 * weight;
00110   accum._m.m._12 += _matrix._m.m._12 * weight;
00111   accum._m.m._13 += _matrix._m.m._13 * weight;
00112   
00113   accum._m.m._20 += _matrix._m.m._20 * weight;
00114   accum._m.m._21 += _matrix._m.m._21 * weight;
00115   accum._m.m._22 += _matrix._m.m._22 * weight;
00116   accum._m.m._23 += _matrix._m.m._23 * weight;
00117   
00118   accum._m.m._30 += _matrix._m.m._30 * weight;
00119   accum._m.m._31 += _matrix._m.m._31 * weight;
00120   accum._m.m._32 += _matrix._m.m._32 * weight;
00121   accum._m.m._33 += _matrix._m.m._33 * weight;
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: JointVertexTransform::output
00126 //       Access: Published, Virtual
00127 //  Description: 
00128 ////////////////////////////////////////////////////////////////////
00129 void JointVertexTransform::
00130 output(ostream &out) const {
00131   out << _joint->get_name();
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: JointVertexTransform::compute_matrix
00136 //       Access: Private
00137 //  Description: Recomputes _matrix if it needs it.  Uses locking.
00138 ////////////////////////////////////////////////////////////////////
00139 void JointVertexTransform::
00140 compute_matrix() {
00141   LightMutexHolder holder(_lock);
00142   if (_matrix_stale) {
00143     _matrix = _joint->_initial_net_transform_inverse * _joint->_net_transform;
00144     _matrix_stale = false;
00145   }
00146 }
00147 
00148 
00149 ////////////////////////////////////////////////////////////////////
00150 //     Function: JointVertexTransform::register_with_read_factory
00151 //       Access: Public, Static
00152 //  Description: Tells the BamReader how to create objects of type
00153 //               JointVertexTransform.
00154 ////////////////////////////////////////////////////////////////////
00155 void JointVertexTransform::
00156 register_with_read_factory() {
00157   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00158 }
00159 
00160 ////////////////////////////////////////////////////////////////////
00161 //     Function: JointVertexTransform::write_datagram
00162 //       Access: Public, Virtual
00163 //  Description: Writes the contents of this object to the datagram
00164 //               for shipping out to a Bam file.
00165 ////////////////////////////////////////////////////////////////////
00166 void JointVertexTransform::
00167 write_datagram(BamWriter *manager, Datagram &dg) {
00168   VertexTransform::write_datagram(manager, dg);
00169 
00170   manager->write_pointer(dg, _joint);
00171 }
00172 
00173 ////////////////////////////////////////////////////////////////////
00174 //     Function: JointVertexTransform::complete_pointers
00175 //       Access: Public, Virtual
00176 //  Description: Receives an array of pointers, one for each time
00177 //               manager->read_pointer() was called in fillin().
00178 //               Returns the number of pointers processed.
00179 ////////////////////////////////////////////////////////////////////
00180 int JointVertexTransform::
00181 complete_pointers(TypedWritable **p_list, BamReader *manager) {
00182   int pi = VertexTransform::complete_pointers(p_list, manager);
00183 
00184   _joint = DCAST(CharacterJoint, p_list[pi++]);    
00185   _joint->_vertex_transforms.insert(this);
00186 
00187   return pi;
00188 }
00189 
00190 ////////////////////////////////////////////////////////////////////
00191 //     Function: JointVertexTransform::make_from_bam
00192 //       Access: Protected, Static
00193 //  Description: This function is called by the BamReader's factory
00194 //               when a new object of type JointVertexTransform is encountered
00195 //               in the Bam file.  It should create the JointVertexTransform
00196 //               and extract its information from the file.
00197 ////////////////////////////////////////////////////////////////////
00198 TypedWritable *JointVertexTransform::
00199 make_from_bam(const FactoryParams &params) {
00200   JointVertexTransform *object = new JointVertexTransform;
00201   DatagramIterator scan;
00202   BamReader *manager;
00203 
00204   parse_params(params, scan, manager);
00205   object->fillin(scan, manager);
00206 
00207   return object;
00208 }
00209 
00210 ////////////////////////////////////////////////////////////////////
00211 //     Function: JointVertexTransform::fillin
00212 //       Access: Protected
00213 //  Description: This internal function is called by make_from_bam to
00214 //               read in all of the relevant data from the BamFile for
00215 //               the new JointVertexTransform.
00216 ////////////////////////////////////////////////////////////////////
00217 void JointVertexTransform::
00218 fillin(DatagramIterator &scan, BamReader *manager) {
00219   VertexTransform::fillin(scan, manager);
00220 
00221   manager->read_pointer(scan);
00222   _matrix_stale = true;
00223   mark_modified(Thread::get_current_thread());
00224 }
 All Classes Functions Variables Enumerations