svn merge -r 16351:16368 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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_framePose(NULL),
57         m_lastframe(0.0),
58         m_activeAct(NULL),
59         m_activePriority(999),
60         m_lastapplyframe(0.0)
61 {
62         m_armature = get_armature(m_objArma);
63
64         /* we make a copy of blender object's pose, and then always swap it with
65          * the original pose before calling into blender functions, to deal with
66          * replica's or other objects using the same blender object */
67         m_pose = NULL;
68         copy_pose(&m_pose, m_objArma->pose, 1 /* copy_constraint_channels_hack */);
69 }
70
71 CValue* BL_ArmatureObject::GetReplica()
72 {
73         BL_ArmatureObject* replica = new BL_ArmatureObject(*this);
74         
75         // this will copy properties and so on...
76         CValue::AddDataToReplica(replica);
77
78         ProcessReplica(replica);
79         return replica;
80 }
81
82 void BL_ArmatureObject::ProcessReplica(BL_ArmatureObject *replica)
83 {
84         KX_GameObject::ProcessReplica(replica);
85
86         replica->m_pose = NULL;
87         copy_pose(&replica->m_pose, m_pose, 1 /* copy_constraint_channels_hack */);
88 }
89
90 BL_ArmatureObject::~BL_ArmatureObject()
91 {
92         if (m_pose)
93                 free_pose(m_pose);
94 }
95
96 bool BL_ArmatureObject::VerifyPose()
97 {
98         if(m_lastapplyframe != m_lastframe) {
99                 extract_pose_from_pose(m_objArma->pose, m_pose);
100                 where_is_pose(m_objArma);
101                 m_lastapplyframe = m_lastframe;
102                 extract_pose_from_pose(m_pose, m_objArma->pose);
103                 return false;
104         }
105         else
106                 return true;
107 }
108
109 void BL_ArmatureObject::ApplyPose()
110 {
111         if(VerifyPose())
112                 extract_pose_from_pose(m_objArma->pose, m_pose);
113 }
114
115 void BL_ArmatureObject::SetPose(bPose *pose)
116 {
117         extract_pose_from_pose(m_pose, pose);
118         m_lastapplyframe = -1.0;
119 }
120
121 bool BL_ArmatureObject::SetActiveAction(BL_ActionActuator *act, short priority, double curtime)
122 {
123         if (curtime != m_lastframe){
124                 m_activePriority = 9999;
125                 m_lastframe= curtime;
126                 m_activeAct = NULL;
127                 // remember the pose at the start of the frame
128                 m_framePose = m_pose;
129         }
130
131         if (priority<=m_activePriority)
132         {
133                 if (priority<m_activePriority)
134                         // this action overwrites the previous ones, start from initial pose to cancel their effects
135                         m_pose = m_framePose;
136                 if (m_activeAct && (m_activeAct!=act))
137                         m_activeAct->SetBlendTime(0.0); /* Reset the blend timer */
138                 m_activeAct = act;
139                 m_activePriority = priority;
140                 m_lastframe = curtime;
141         
142                 return true;
143         }
144         else{
145                 act->SetBlendTime(0.0);
146                 return false;
147         }
148         
149 }
150
151 BL_ActionActuator * BL_ArmatureObject::GetActiveAction()
152 {
153         return m_activeAct;
154 }
155
156 void BL_ArmatureObject::GetPose(bPose **pose)
157 {
158         /* If the caller supplies a null pose, create a new one. */
159         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
160                 
161         if (!*pose) {
162                 /*      probably not to good of an idea to
163                         duplicate everying, but it clears up 
164                         a crash and memory leakage when 
165                         &BL_ActionActuator::m_pose is freed
166                 */
167                 int copy_constraint_channels_hack = 1;
168                 copy_pose(pose, m_pose, copy_constraint_channels_hack);
169         }
170         else {
171                 if (*pose == m_pose)
172                         // no need to copy if the pointers are the same
173                         return;
174
175                 extract_pose_from_pose(*pose, m_pose);
176         }
177 }
178
179 void BL_ArmatureObject::GetMRDPose(bPose **pose)
180 {
181         /* If the caller supplies a null pose, create a new one. */
182         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
183
184         if (!*pose) {
185                 // must duplicate the constraints too otherwise we have corruption in free_pose_channels()
186                 // because it will free the blender constraints. 
187                 // Ideally, blender should rememeber that the constraints were not copied so that
188                 // free_pose_channels() would not free them.
189                 copy_pose(pose, m_pose, 1);
190         }
191         else {
192                 extract_pose_from_pose(*pose, m_pose);
193         }
194
195 }
196
197 short BL_ArmatureObject::GetActivePriority()
198 {
199         return m_activePriority;
200 }
201
202 double BL_ArmatureObject::GetLastFrame()
203 {
204         return m_lastframe;
205 }
206
207 bool BL_ArmatureObject::GetBoneMatrix(Bone* bone, MT_Matrix4x4& matrix)
208 {
209         bPoseChannel *pchan;
210
211         ApplyPose();
212         pchan = get_pose_channel(m_objArma->pose, bone->name);
213
214         if(pchan) {
215                 matrix.setValue(&pchan->pose_mat[0][0]);
216                 return true;
217         }
218
219         return false;
220 }
221
222 float BL_ArmatureObject::GetBoneLength(Bone* bone) const
223 {
224         return (float)(MT_Point3(bone->head) - MT_Point3(bone->tail)).length();
225 }