Panda3D

partBundle.h

00001 // Filename: partBundle.h
00002 // Created by:  drose (22Feb99)
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 #ifndef PARTBUNDLE_H
00016 #define PARTBUNDLE_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "partGroup.h"
00021 #include "animControl.h"
00022 #include "partSubset.h"
00023 #include "animPreloadTable.h"
00024 #include "pointerTo.h"
00025 #include "thread.h"
00026 #include "cycleData.h"
00027 #include "cycleDataLockedReader.h"
00028 #include "cycleDataReader.h"
00029 #include "cycleDataWriter.h"
00030 #include "luse.h"
00031 #include "pvector.h"
00032 #include "transformState.h"
00033 #include "weakPointerTo.h"
00034 #include "copyOnWritePointer.h"
00035 
00036 class Loader;
00037 class AnimBundle;
00038 class PartBundleNode;
00039 class PartBundleNode;
00040 class TransformState;
00041 class AnimPreloadTable;
00042 
00043 ////////////////////////////////////////////////////////////////////
00044 //       Class : PartBundle
00045 // Description : This is the root of a MovingPart hierarchy.  It
00046 //               defines the hierarchy of moving parts that make up an
00047 //               animatable object.
00048 ////////////////////////////////////////////////////////////////////
00049 class EXPCL_PANDA_CHAN PartBundle : public PartGroup {
00050 public:
00051 
00052   // This is passed down through the MovingParts during the
00053   // do_update() call to specify the channels that are in effect.
00054   typedef pmap<AnimControl *, PN_stdfloat> ChannelBlend;
00055 
00056 protected:
00057   // The copy constructor is protected; use make_copy() or copy_subgraph().
00058   PartBundle(const PartBundle &copy);
00059 
00060 PUBLISHED:
00061   PartBundle(const string &name = "");
00062   virtual PartGroup *make_copy() const;
00063 
00064   INLINE CPT(AnimPreloadTable) get_anim_preload() const;
00065   INLINE PT(AnimPreloadTable) modify_anim_preload();
00066   INLINE void set_anim_preload(AnimPreloadTable *table);
00067   INLINE void clear_anim_preload();
00068   void merge_anim_preloads(const PartBundle *other);
00069 
00070   // This is the parameter to set_blend_type() and specifies the kind
00071   // of blending operation to be performed when multiple controls are
00072   // in effect simultaneously (see set_control_effect()) or between
00073   // sequential frames of the animation.
00074   enum BlendType {
00075     // BT_linear does a componentwise average of all blended matrices,
00076     // which is a linear blend.  The result of this is that if a
00077     // particular vertex would have been at point P in one animation
00078     // and point Q in another one, it will end up on the line in
00079     // between them in the resulting blend animation.  However, this
00080     // tends to stretch and squash limbs in strange and disturbing
00081     // ways.
00082     BT_linear,
00083 
00084     // BT_normalized_linear is a compromise on BT_linear.  The matrix
00085     // is blended linearly without the scale and shear components, and
00086     // the blended scale and shear components are applied separately.
00087     // This keeps all of the character's body parts in the correct
00088     // size and shape.  However, if the hierarchy is disconnected,
00089     // body parts can fly off.  It's essential the skeleton hierarchy
00090     // be completely connected to use this blend mode successully.
00091     BT_normalized_linear,
00092 
00093     // BT_componentwise linearly blends all components separately,
00094     // including H, P, and R, and recomposes the matrix.
00095     BT_componentwise,
00096 
00097     // BT_componentwise_quat linearly blends all components
00098     // separately, except for rotation which is blended as a
00099     // quaternion.
00100     BT_componentwise_quat,
00101   };
00102 
00103   INLINE void set_blend_type(BlendType bt);
00104   INLINE BlendType get_blend_type() const;
00105 
00106   void set_anim_blend_flag(bool anim_blend_flag);
00107   INLINE bool get_anim_blend_flag() const;
00108 
00109   INLINE void set_frame_blend_flag(bool frame_blend_flag);
00110   INLINE bool get_frame_blend_flag() const;
00111 
00112   INLINE void set_root_xform(const LMatrix4 &root_xform);
00113   INLINE void xform(const LMatrix4 &mat);
00114   INLINE const LMatrix4 &get_root_xform() const;
00115   PT(PartBundle) apply_transform(const TransformState *transform);
00116 
00117   INLINE int get_num_nodes() const;
00118   INLINE PartBundleNode *get_node(int n) const;
00119   MAKE_SEQ(get_nodes, get_num_nodes, get_node);
00120 
00121   void clear_control_effects();
00122   INLINE void set_control_effect(AnimControl *control, PN_stdfloat effect);
00123   INLINE PN_stdfloat get_control_effect(AnimControl *control) const;
00124 
00125   virtual void output(ostream &out) const;
00126   virtual void write(ostream &out, int indent_level) const;
00127 
00128   PT(AnimControl) bind_anim(AnimBundle *anim,
00129                             int hierarchy_match_flags = 0, 
00130                             const PartSubset &subset = PartSubset());
00131   PT(AnimControl) load_bind_anim(Loader *loader,
00132                                  const Filename &filename,
00133                                  int hierarchy_match_flags,
00134                                  const PartSubset &subset,
00135                                  bool allow_async);
00136   void wait_pending();
00137 
00138   bool freeze_joint(const string &joint_name, const TransformState *transform);
00139   bool freeze_joint(const string &joint_name, const LVecBase3 &pos, const LVecBase3 &hpr, const LVecBase3 &scale);
00140   bool freeze_joint(const string &joint_name, PN_stdfloat value);
00141   bool control_joint(const string &joint_name, PandaNode *node);
00142   bool release_joint(const string &joint_name);
00143 
00144   bool update();
00145   bool force_update();
00146   
00147 public:
00148   // The following functions aren't really part of the public
00149   // interface; they're just public so we don't have to declare a
00150   // bunch of friends.
00151   virtual void control_activated(AnimControl *control);
00152   INLINE void set_update_delay(double delay);
00153 
00154   bool do_bind_anim(AnimControl *control, AnimBundle *anim,
00155                     int hierarchy_match_flags, const PartSubset &subset);
00156 
00157 protected:
00158   virtual void add_node(PartBundleNode *node);
00159   virtual void remove_node(PartBundleNode *node);
00160 
00161 private:
00162   class CData;
00163 
00164   void do_set_control_effect(AnimControl *control, PN_stdfloat effect, CData *cdata);
00165   PN_stdfloat do_get_control_effect(AnimControl *control, const CData *cdata) const;
00166   void recompute_net_blend(CData *cdata);
00167   void clear_and_stop_intersecting(AnimControl *control, CData *cdata);
00168 
00169   COWPT(AnimPreloadTable) _anim_preload;
00170 
00171   typedef pvector<PartBundleNode *> Nodes;
00172   Nodes _nodes;
00173 
00174   typedef pmap<WCPT(TransformState), WPT(PartBundle) > AppliedTransforms;
00175   AppliedTransforms _applied_transforms;
00176 
00177   double _update_delay;
00178 
00179   // This is the data that must be cycled between pipeline stages.
00180   class CData : public CycleData {
00181   public:
00182     CData();
00183     CData(const CData &copy);
00184 
00185     virtual CycleData *make_copy() const;
00186     virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
00187     virtual void fillin(DatagramIterator &scan, BamReader *manager);
00188     virtual TypeHandle get_parent_type() const {
00189       return PartBundle::get_class_type();
00190     }
00191 
00192     BlendType _blend_type;
00193     bool _anim_blend_flag;
00194     bool _frame_blend_flag;
00195     LMatrix4 _root_xform;
00196     AnimControl *_last_control_set;
00197     ChannelBlend _blend;
00198     PN_stdfloat _net_blend;
00199     bool _anim_changed;
00200     double _last_update;
00201   };
00202 
00203   PipelineCycler<CData> _cycler;
00204   typedef CycleDataLockedReader<CData> CDLockedReader;
00205   typedef CycleDataReader<CData> CDReader;
00206   typedef CycleDataWriter<CData> CDWriter;
00207 
00208 public:
00209   static void register_with_read_factory();
00210   virtual void finalize(BamReader *manager);
00211   virtual void write_datagram(BamWriter *manager, Datagram &dg);
00212   virtual int complete_pointers(TypedWritable **p_list,
00213                                 BamReader *manager);
00214 
00215 protected:
00216   static TypedWritable *make_from_bam(const FactoryParams &params);
00217   void fillin(DatagramIterator &scan, BamReader *manager);
00218 
00219 public:
00220 
00221   virtual TypeHandle get_type() const {
00222     return get_class_type();
00223   }
00224   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00225   static TypeHandle get_class_type() {
00226     return _type_handle;
00227   }
00228   static void init_type() {
00229     PartGroup::init_type();
00230     register_type(_type_handle, "PartBundle",
00231                   PartGroup::get_class_type());
00232   }
00233 
00234 private:
00235   static TypeHandle _type_handle;
00236 
00237   friend class PartBundleNode;
00238   friend class Character;
00239   friend class MovingPartBase;
00240   friend class MovingPartMatrix;
00241   friend class MovingPartScalar;
00242 };
00243 
00244 inline ostream &operator <<(ostream &out, const PartBundle &bundle) {
00245   bundle.output(out);
00246   return out;
00247 }
00248 
00249 ostream &operator <<(ostream &out, PartBundle::BlendType blend_type);
00250 istream &operator >>(istream &in, PartBundle::BlendType &blend_type);
00251 
00252 #include "partBundle.I"
00253 
00254 #endif
 All Classes Functions Variables Enumerations