Merge from 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                 extract_pose_from_pose(m_objArma->pose, m_pose);
98                 
99                 // is this needed anymore?
100                 //if (!m_mrdPose)
101                 //      copy_pose (&m_mrdPose, m_pose, 0);
102                 //else
103                 //      extract_pose_from_pose(m_mrdPose, m_pose);
104         }
105 }
106
107 void BL_ArmatureObject::SetPose(bPose *pose)
108 {
109         m_pose = pose;
110 }
111
112 bool BL_ArmatureObject::SetActiveAction(BL_ActionActuator *act, short priority, double curtime)
113 {
114         if (curtime != m_lastframe){
115                 m_activePriority = 9999;
116                 m_lastframe= curtime;
117                 m_activeAct = NULL;
118         }
119
120         if (priority<=m_activePriority)
121         {
122                 if (m_activeAct && (m_activeAct!=act))
123                         m_activeAct->SetBlendTime(0.0); /* Reset the blend timer */
124                 m_activeAct = act;
125                 m_activePriority = priority;
126                 m_lastframe = curtime;
127         
128                 return true;
129         }
130         else{
131                 act->SetBlendTime(0.0);
132                 return false;
133         }
134         
135 }
136
137 BL_ActionActuator * BL_ArmatureObject::GetActiveAction()
138 {
139         return m_activeAct;
140 }
141
142 void BL_ArmatureObject::GetPose(bPose **pose)
143 {
144         /* If the caller supplies a null pose, create a new one. */
145         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
146
147         if (!*pose) {
148                 /*      probably not to good of an idea to
149                         duplicate everying, but it clears up 
150                         a crash and memory leakage when 
151                         &BL_ActionActuator::m_pose is freed
152                 */
153                 int copy_constraint_channels_hack = 1;
154                 copy_pose(pose, m_pose, copy_constraint_channels_hack);
155         }
156         else {
157                 if (*pose == m_pose)
158                         // no need to copy if the pointers are the same
159                         return;
160                 extract_pose_from_pose(*pose, m_pose);
161         }
162 }
163
164 void BL_ArmatureObject::GetMRDPose(bPose **pose)
165 {
166         /* If the caller supplies a null pose, create a new one. */
167         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
168
169         // is this needed anymore?
170         //if (!m_mrdPose){
171         //      copy_pose (&m_mrdPose, m_pose, 0);
172         //}
173
174         if (!*pose)
175                 // must duplicate the constraints too otherwise we have corruption in free_pose_channels()
176                 // because it will free the blender constraints. 
177                 // Ideally, blender should rememeber that the constraints were not copied so that
178                 // free_pose_channels() would not free them.
179                 copy_pose(pose, m_objArma->pose, 1);
180         else
181                 extract_pose_from_pose(*pose, m_objArma->pose);
182
183 }
184
185 short BL_ArmatureObject::GetActivePriority()
186 {
187         return m_activePriority;
188 }
189
190 double BL_ArmatureObject::GetLastFrame()
191 {
192         return m_lastframe;
193 }
194
195 bool BL_ArmatureObject::GetBoneMatrix(Bone* bone, MT_Matrix4x4& matrix) const
196 {
197         Object* par_arma = m_objArma;
198         where_is_pose(par_arma);
199         bPoseChannel *pchan= get_pose_channel(par_arma->pose, bone->name);
200
201         if(pchan) {
202                 matrix.setValue(&pchan->pose_mat[0][0]);
203                 return true;
204         }
205         return false;
206 }
207
208 float BL_ArmatureObject::GetBoneLength(Bone* bone) const
209 {
210         return (float)(MT_Point3(bone->head) - MT_Point3(bone->tail)).length();
211 }