MERGING TRUNK
[blender-staging.git] / source / gameengine / Converter / BL_ArmatureObject.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "BL_ArmatureObject.h"
31 #include "BL_ActionActuator.h"
32 #include "BLI_blenlib.h"
33 #include "BKE_action.h"
34 #include "BKE_armature.h"
35 #include "GEN_Map.h"
36 #include "GEN_HashedPtr.h"
37 #include "MEM_guardedalloc.h"
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_object_types.h"
41
42 #include "MT_Matrix4x4.h"
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48
49 BL_ArmatureObject::BL_ArmatureObject(
50                                 void* sgReplicationInfo, 
51                                 SG_Callbacks callbacks, 
52                                 Object *armature )
53
54 :       KX_GameObject(sgReplicationInfo,callbacks),
55         m_objArma(armature),
56         m_mrdPose(NULL),
57         m_lastframe(0.),
58         m_activeAct(NULL),
59         m_activePriority(999)
60 {
61         m_armature = get_armature(m_objArma);
62         m_pose = m_objArma->pose;
63 }
64
65
66 CValue* BL_ArmatureObject::GetReplica()
67 {
68         BL_ArmatureObject* replica = new BL_ArmatureObject(*this);
69         
70         // this will copy properties and so on...
71         CValue::AddDataToReplica(replica);
72
73         ProcessReplica(replica);
74         return replica;
75 }
76
77 void BL_ArmatureObject::ProcessReplica(BL_ArmatureObject *replica)
78 {
79         KX_GameObject::ProcessReplica(replica);
80
81 }
82
83 BL_ArmatureObject::~BL_ArmatureObject()
84 {
85         if (m_mrdPose){
86                 free_pose_channels(m_mrdPose);
87                 MEM_freeN(m_mrdPose);
88         }
89 }
90
91 /* note, you can only call this for exisiting Armature objects, and not mix it with other Armatures */
92 /* there is only 1 unique Pose per Armature */
93 void BL_ArmatureObject::ApplyPose()
94 {
95         if (m_pose) {
96                 // copy to armature object
97                 if (m_objArma->pose != m_pose)/* This should never happen but it does - Campbell */
98                         extract_pose_from_pose(m_objArma->pose, m_pose);
99                 
100                 // is this needed anymore?
101                 //if (!m_mrdPose)
102                 //      copy_pose (&m_mrdPose, m_pose, 0);
103                 //else
104                 //      extract_pose_from_pose(m_mrdPose, m_pose);
105         }
106 }
107
108 void BL_ArmatureObject::SetPose(bPose *pose)
109 {
110         m_pose = pose;
111 }
112
113 bool BL_ArmatureObject::SetActiveAction(BL_ActionActuator *act, short priority, double curtime)
114 {
115         if (curtime != m_lastframe){
116                 m_activePriority = 9999;
117                 m_lastframe= curtime;
118                 m_activeAct = NULL;
119         }
120
121         if (priority<=m_activePriority)
122         {
123                 if (m_activeAct && (m_activeAct!=act))
124                         m_activeAct->SetBlendTime(0.0); /* Reset the blend timer */
125                 m_activeAct = act;
126                 m_activePriority = priority;
127                 m_lastframe = curtime;
128         
129                 return true;
130         }
131         else{
132                 act->SetBlendTime(0.0);
133                 return false;
134         }
135         
136 }
137
138 BL_ActionActuator * BL_ArmatureObject::GetActiveAction()
139 {
140         return m_activeAct;
141 }
142
143 void BL_ArmatureObject::GetPose(bPose **pose)
144 {
145         /* If the caller supplies a null pose, create a new one. */
146         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
147                 
148         if (!*pose) {
149                 /*      probably not to good of an idea to
150                         duplicate everying, but it clears up 
151                         a crash and memory leakage when 
152                         &BL_ActionActuator::m_pose is freed
153                 */
154                 int copy_constraint_channels_hack = 1;
155                 copy_pose(pose, m_pose, copy_constraint_channels_hack);
156         }
157         else {
158                 if (*pose == m_pose)
159                         // no need to copy if the pointers are the same
160                         return;
161                 extract_pose_from_pose(*pose, m_pose);
162         }
163 }
164
165 void BL_ArmatureObject::GetMRDPose(bPose **pose)
166 {
167         /* If the caller supplies a null pose, create a new one. */
168         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
169
170         // is this needed anymore?
171         //if (!m_mrdPose){
172         //      copy_pose (&m_mrdPose, m_pose, 0);
173         //}
174
175         if (!*pose)
176                 // must duplicate the constraints too otherwise we have corruption in free_pose_channels()
177                 // because it will free the blender constraints. 
178                 // Ideally, blender should rememeber that the constraints were not copied so that
179                 // free_pose_channels() would not free them.
180                 copy_pose(pose, m_objArma->pose, 1);
181         else
182                 extract_pose_from_pose(*pose, m_objArma->pose);
183
184 }
185
186 short BL_ArmatureObject::GetActivePriority()
187 {
188         return m_activePriority;
189 }
190
191 double BL_ArmatureObject::GetLastFrame()
192 {
193         return m_lastframe;
194 }
195
196 bool BL_ArmatureObject::GetBoneMatrix(Bone* bone, MT_Matrix4x4& matrix) const
197 {
198         Object* par_arma = m_objArma;
199         where_is_pose(par_arma);
200         bPoseChannel *pchan= get_pose_channel(par_arma->pose, bone->name);
201
202         if(pchan) {
203                 matrix.setValue(&pchan->pose_mat[0][0]);
204                 return true;
205         }
206         return false;
207 }
208
209 float BL_ArmatureObject::GetBoneLength(Bone* bone) const
210 {
211         return (float)(MT_Point3(bone->head) - MT_Point3(bone->tail)).length();
212 }