Panda3D
 All Classes Functions Variables Enumerations
jointVertexTransform.cxx
1 // Filename: jointVertexTransform.cxx
2 // Created by: drose (24Mar05)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "jointVertexTransform.h"
16 #include "datagram.h"
17 #include "datagramIterator.h"
18 #include "bamReader.h"
19 #include "bamWriter.h"
20 #include "lightMutexHolder.h"
21 
22 TypeHandle JointVertexTransform::_type_handle;
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: JointVertexTransform::Default Constructor
26 // Access: Private
27 // Description: Constructs an invalid object; used only by the bam
28 // loader.
29 ////////////////////////////////////////////////////////////////////
30 JointVertexTransform::
31 JointVertexTransform() :
32  _matrix_stale(true)
33 {
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: JointVertexTransform::Constructor
38 // Access: Published
39 // Description: Constructs a new object that converts vertices from
40 // the indicated joint's coordinate space, into the
41 // other indicated joint's space.
42 ////////////////////////////////////////////////////////////////////
43 JointVertexTransform::
44 JointVertexTransform(CharacterJoint *joint) :
45  _joint(joint),
46  _matrix_stale(true)
47 {
48  // Tell the joint that we need to be informed when it moves.
49  _joint->_vertex_transforms.insert(this);
50  mark_modified(Thread::get_current_thread());
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: JointVertexTransform::Destructor
55 // Access: Published, Virtual
56 // Description:
57 ////////////////////////////////////////////////////////////////////
58 JointVertexTransform::
59 ~JointVertexTransform() {
60  // Tell the joint to stop informing us about its motion.
61  _joint->_vertex_transforms.erase(this);
62 }
63 
64 ////////////////////////////////////////////////////////////////////
65 // Function: JointVertexTransform::get_matrix
66 // Access: Published, Virtual
67 // Description: Stores the transform's matrix in the indicated object.
68 ////////////////////////////////////////////////////////////////////
70 get_matrix(LMatrix4 &matrix) const {
71  check_matrix();
72  matrix = _matrix;
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: JointVertexTransform::mult_matrix
77 // Access: Published, Virtual
78 // Description: Premultiplies this transform's matrix with the
79 // indicated previous matrix, so that the result is the
80 // net composition of the given transform with this
81 // transform. The result is stored in the parameter
82 // "result", which should not be the same matrix as
83 // previous.
84 ////////////////////////////////////////////////////////////////////
86 mult_matrix(LMatrix4 &result, const LMatrix4 &previous) const {
87  check_matrix();
88  result.multiply(_matrix, previous);
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: JointVertexTransform::accumulate_matrix
93 // Access: Published, Virtual
94 // Description: Adds the value of this transform's matrix, modified
95 // by the indicated weight, into the indicated
96 // accumulation matrix. This is used to compute the
97 // result of several blended transforms.
98 ////////////////////////////////////////////////////////////////////
100 accumulate_matrix(LMatrix4 &accum, PN_stdfloat weight) const {
101  check_matrix();
102 
103  accum.accumulate(_matrix, weight);
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: JointVertexTransform::output
108 // Access: Published, Virtual
109 // Description:
110 ////////////////////////////////////////////////////////////////////
111 void JointVertexTransform::
112 output(ostream &out) const {
113  out << _joint->get_name();
114 }
115 
116 ////////////////////////////////////////////////////////////////////
117 // Function: JointVertexTransform::compute_matrix
118 // Access: Private
119 // Description: Recomputes _matrix if it needs it. Uses locking.
120 ////////////////////////////////////////////////////////////////////
121 void JointVertexTransform::
122 compute_matrix() {
123  LightMutexHolder holder(_lock);
124  if (_matrix_stale) {
125  _matrix = _joint->_initial_net_transform_inverse * _joint->_net_transform;
126  _matrix_stale = false;
127  }
128 }
129 
130 
131 ////////////////////////////////////////////////////////////////////
132 // Function: JointVertexTransform::register_with_read_factory
133 // Access: Public, Static
134 // Description: Tells the BamReader how to create objects of type
135 // JointVertexTransform.
136 ////////////////////////////////////////////////////////////////////
139  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: JointVertexTransform::write_datagram
144 // Access: Public, Virtual
145 // Description: Writes the contents of this object to the datagram
146 // for shipping out to a Bam file.
147 ////////////////////////////////////////////////////////////////////
150  VertexTransform::write_datagram(manager, dg);
151 
152  manager->write_pointer(dg, _joint);
153 }
154 
155 ////////////////////////////////////////////////////////////////////
156 // Function: JointVertexTransform::complete_pointers
157 // Access: Public, Virtual
158 // Description: Receives an array of pointers, one for each time
159 // manager->read_pointer() was called in fillin().
160 // Returns the number of pointers processed.
161 ////////////////////////////////////////////////////////////////////
164  int pi = VertexTransform::complete_pointers(p_list, manager);
165 
166  _joint = DCAST(CharacterJoint, p_list[pi++]);
167  _joint->_vertex_transforms.insert(this);
168 
169  return pi;
170 }
171 
172 ////////////////////////////////////////////////////////////////////
173 // Function: JointVertexTransform::make_from_bam
174 // Access: Protected, Static
175 // Description: This function is called by the BamReader's factory
176 // when a new object of type JointVertexTransform is encountered
177 // in the Bam file. It should create the JointVertexTransform
178 // and extract its information from the file.
179 ////////////////////////////////////////////////////////////////////
180 TypedWritable *JointVertexTransform::
181 make_from_bam(const FactoryParams &params) {
183  DatagramIterator scan;
184  BamReader *manager;
185 
186  parse_params(params, scan, manager);
187  object->fillin(scan, manager);
188 
189  return object;
190 }
191 
192 ////////////////////////////////////////////////////////////////////
193 // Function: JointVertexTransform::fillin
194 // Access: Protected
195 // Description: This internal function is called by make_from_bam to
196 // read in all of the relevant data from the BamFile for
197 // the new JointVertexTransform.
198 ////////////////////////////////////////////////////////////////////
199 void JointVertexTransform::
200 fillin(DatagramIterator &scan, BamReader *manager) {
201  VertexTransform::fillin(scan, manager);
202 
203  manager->read_pointer(scan);
204  _matrix_stale = true;
205  mark_modified(Thread::get_current_thread());
206 }
virtual void accumulate_matrix(LMatrix4 &accum, PN_stdfloat weight) const
Adds the value of this transform&#39;s matrix, modified by the indicated weight, into the indicated accum...
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
virtual void mult_matrix(LMatrix4 &result, const LMatrix4 &previous) const
Premultiplies this transform&#39;s matrix with the indicated previous matrix, so that the result is the n...
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
static Thread * get_current_thread()
Returns a pointer to the currently-executing Thread object.
Definition: thread.I:145
virtual void get_matrix(LMatrix4 &matrix) const
Stores the transform&#39;s matrix in the indicated object.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager-&gt;read_pointer() was called in fillin()...
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
Similar to MutexHolder, but for a light mutex.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
void accumulate(const LMatrix4f &other, float weight)
Computes (*this) += other * weight.
Definition: lmatrix.h:2300
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager-&gt;read_pointer() was called in fillin()...
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
This is a specialization on VertexTransform that returns the transform necessary to move vertices as ...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
This represents one joint of the character&#39;s animation, containing an animating transform matrix...
static void register_with_read_factory()
Tells the BamReader how to create objects of type JointVertexTransform.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:279
void read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:652