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(LMatrix4 &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(LMatrix4 &result, const LMatrix4 &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(LMatrix4 &accum, PN_stdfloat weight) const {
00101   check_matrix();
00102 
00103   accum.accumulate(_matrix, weight);
00104 }
00105 
00106 ////////////////////////////////////////////////////////////////////
00107 //     Function: JointVertexTransform::output
00108 //       Access: Published, Virtual
00109 //  Description: 
00110 ////////////////////////////////////////////////////////////////////
00111 void JointVertexTransform::
00112 output(ostream &out) const {
00113   out << _joint->get_name();
00114 }
00115 
00116 ////////////////////////////////////////////////////////////////////
00117 //     Function: JointVertexTransform::compute_matrix
00118 //       Access: Private
00119 //  Description: Recomputes _matrix if it needs it.  Uses locking.
00120 ////////////////////////////////////////////////////////////////////
00121 void JointVertexTransform::
00122 compute_matrix() {
00123   LightMutexHolder holder(_lock);
00124   if (_matrix_stale) {
00125     _matrix = _joint->_initial_net_transform_inverse * _joint->_net_transform;
00126     _matrix_stale = false;
00127   }
00128 }
00129 
00130 
00131 ////////////////////////////////////////////////////////////////////
00132 //     Function: JointVertexTransform::register_with_read_factory
00133 //       Access: Public, Static
00134 //  Description: Tells the BamReader how to create objects of type
00135 //               JointVertexTransform.
00136 ////////////////////////////////////////////////////////////////////
00137 void JointVertexTransform::
00138 register_with_read_factory() {
00139   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00140 }
00141 
00142 ////////////////////////////////////////////////////////////////////
00143 //     Function: JointVertexTransform::write_datagram
00144 //       Access: Public, Virtual
00145 //  Description: Writes the contents of this object to the datagram
00146 //               for shipping out to a Bam file.
00147 ////////////////////////////////////////////////////////////////////
00148 void JointVertexTransform::
00149 write_datagram(BamWriter *manager, Datagram &dg) {
00150   VertexTransform::write_datagram(manager, dg);
00151 
00152   manager->write_pointer(dg, _joint);
00153 }
00154 
00155 ////////////////////////////////////////////////////////////////////
00156 //     Function: JointVertexTransform::complete_pointers
00157 //       Access: Public, Virtual
00158 //  Description: Receives an array of pointers, one for each time
00159 //               manager->read_pointer() was called in fillin().
00160 //               Returns the number of pointers processed.
00161 ////////////////////////////////////////////////////////////////////
00162 int JointVertexTransform::
00163 complete_pointers(TypedWritable **p_list, BamReader *manager) {
00164   int pi = VertexTransform::complete_pointers(p_list, manager);
00165 
00166   _joint = DCAST(CharacterJoint, p_list[pi++]);    
00167   _joint->_vertex_transforms.insert(this);
00168 
00169   return pi;
00170 }
00171 
00172 ////////////////////////////////////////////////////////////////////
00173 //     Function: JointVertexTransform::make_from_bam
00174 //       Access: Protected, Static
00175 //  Description: This function is called by the BamReader's factory
00176 //               when a new object of type JointVertexTransform is encountered
00177 //               in the Bam file.  It should create the JointVertexTransform
00178 //               and extract its information from the file.
00179 ////////////////////////////////////////////////////////////////////
00180 TypedWritable *JointVertexTransform::
00181 make_from_bam(const FactoryParams &params) {
00182   JointVertexTransform *object = new JointVertexTransform;
00183   DatagramIterator scan;
00184   BamReader *manager;
00185 
00186   parse_params(params, scan, manager);
00187   object->fillin(scan, manager);
00188 
00189   return object;
00190 }
00191 
00192 ////////////////////////////////////////////////////////////////////
00193 //     Function: JointVertexTransform::fillin
00194 //       Access: Protected
00195 //  Description: This internal function is called by make_from_bam to
00196 //               read in all of the relevant data from the BamFile for
00197 //               the new JointVertexTransform.
00198 ////////////////////////////////////////////////////////////////////
00199 void JointVertexTransform::
00200 fillin(DatagramIterator &scan, BamReader *manager) {
00201   VertexTransform::fillin(scan, manager);
00202 
00203   manager->read_pointer(scan);
00204   _matrix_stale = true;
00205   mark_modified(Thread::get_current_thread());
00206 }
 All Classes Functions Variables Enumerations