Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/chan/movingPartMatrix.cxx

Go to the documentation of this file.
00001 // Filename: movingPartMatrix.cxx
00002 // Created by:  drose (23Feb99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 
00020 #include "movingPartMatrix.h"
00021 
00022 #include <compose_matrix.h>
00023 #include <datagram.h>
00024 #include <datagramIterator.h>
00025 #include <bamReader.h>
00026 #include <bamWriter.h>
00027 
00028 // Tell GCC that we'll take care of the instantiation explicitly here.
00029 #ifdef __GNUC__
00030 #pragma implementation
00031 #endif
00032 
00033 TypeHandle MovingPartMatrix::_type_handle;
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 //     Function: MovingPartMatrix::get_blend_value
00037 //       Access: Public
00038 //  Description: Attempts to blend the various matrix values
00039 //               indicated, and sets the _value member to the
00040 //               resulting matrix.
00041 ////////////////////////////////////////////////////////////////////
00042 void MovingPartMatrix::
00043 get_blend_value(const PartBundle *root) {
00044   const PartBundle::ChannelBlend &blend = root->get_blend_map();
00045 
00046   if (blend.empty()) {
00047     // No channel is bound; supply the default value.
00048     _value = _initial_value;
00049 
00050   } else if (blend.size() == 1) {
00051     // A single value, the normal case.
00052     AnimControl *control = (*blend.begin()).first;
00053 
00054     int channel_index = control->get_channel_index();
00055     nassertv(channel_index >= 0 && channel_index < (int)_channels.size());
00056     ChannelType *channel = DCAST(ChannelType, _channels[channel_index]);
00057     nassertv(channel != NULL);
00058 
00059     channel->get_value(control->get_frame(), _value);
00060 
00061   } else {
00062     // A blend of two or more values.
00063 
00064     if (root->get_blend_type() == PartBundle::BT_linear) {
00065       // An ordinary, linear blend.
00066       _value = 0.0f;
00067       float net = 0.0f;
00068 
00069       PartBundle::ChannelBlend::const_iterator cbi;
00070       for (cbi = blend.begin(); cbi != blend.end(); ++cbi) {
00071         AnimControl *control = (*cbi).first;
00072         float effect = (*cbi).second;
00073         nassertv(effect != 0.0f);
00074 
00075         int channel_index = control->get_channel_index();
00076         nassertv(channel_index >= 0 && channel_index < (int)_channels.size());
00077         ChannelType *channel = DCAST(ChannelType, _channels[channel_index]);
00078         nassertv(channel != NULL);
00079 
00080         ValueType v;
00081         channel->get_value(control->get_frame(), v);
00082 
00083         _value += v * effect;
00084         net += effect;
00085       }
00086 
00087       nassertv(net != 0.0f);
00088       _value /= net;
00089 
00090     } else if (root->get_blend_type() == PartBundle::BT_normalized_linear) {
00091       // A normalized linear blend.  This means we do a linear blend
00092       // without scales, normalize the scale components of the
00093       // resulting matrix to eliminate artificially-introduced scales,
00094       // and then reapply the scales.
00095 
00096       _value = 0.0f;
00097       LVector3f scale(0.0f, 0.0f, 0.0f);
00098       float net = 0.0f;
00099 
00100       PartBundle::ChannelBlend::const_iterator cbi;
00101       for (cbi = blend.begin(); cbi != blend.end(); ++cbi) {
00102         AnimControl *control = (*cbi).first;
00103         float effect = (*cbi).second;
00104         nassertv(effect != 0.0f);
00105 
00106         int channel_index = control->get_channel_index();
00107         nassertv(channel_index >= 0 && channel_index < (int)_channels.size());
00108         ChannelType *channel = DCAST(ChannelType, _channels[channel_index]);
00109         nassertv(channel != NULL);
00110 
00111         ValueType v;
00112         channel->get_value_no_scale(control->get_frame(), v);
00113         LVector3f s;
00114         channel->get_scale(control->get_frame(), &s[0]);
00115 
00116         _value += v * effect;
00117         scale += s * effect;
00118         net += effect;
00119       }
00120 
00121       nassertv(net != 0.0f);
00122       _value /= net;
00123       scale /= net;
00124 
00125       // Now rebuild the matrix with the correct scale values.
00126 
00127       LVector3f false_scale, hpr, translate;
00128       decompose_matrix(_value, false_scale, hpr, translate);
00129       compose_matrix(_value, scale, hpr, translate);
00130     }
00131   }
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: MovingPartMatrix::make_MovingPartMatrix
00136 //       Access: Protected
00137 //  Description: Factory method to generate a MovingPartMatrix object
00138 ////////////////////////////////////////////////////////////////////
00139 TypedWritable* MovingPartMatrix::
00140 make_MovingPartMatrix(const FactoryParams &params)
00141 {
00142   MovingPartMatrix *me = new MovingPartMatrix;
00143   DatagramIterator scan;
00144   BamReader *manager;
00145 
00146   parse_params(params, scan, manager);
00147   me->fillin(scan, manager);
00148   return me;
00149 }
00150 
00151 ////////////////////////////////////////////////////////////////////
00152 //     Function: MovingPartMatrix::register_with_factory
00153 //       Access: Public, Static
00154 //  Description: Factory method to generate a MovingPartMatrix object
00155 ////////////////////////////////////////////////////////////////////
00156 void MovingPartMatrix::
00157 register_with_read_factory(void)
00158 {
00159   BamReader::get_factory()->register_factory(get_class_type(), make_MovingPartMatrix);
00160 }

Generated on Fri May 2 00:35:09 2003 for Panda by doxygen1.3