Revert last 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(m_mrdPose);
87 }
88
89 /* note, you can only call this for exisiting Armature objects, and not mix it with other Armatures */
90 /* there is only 1 unique Pose per Armature */
91 void BL_ArmatureObject::ApplyPose()
92 {
93         if (m_pose) {
94                 // copy to armature object
95                 if (m_objArma->pose != m_pose)/* This should never happen but it does - Campbell */
96                         extract_pose_from_pose(m_objArma->pose, m_pose);
97                 
98                 // is this needed anymore?
99                 //if (!m_mrdPose)
100                 //      copy_pose (&m_mrdPose, m_pose, 0);
101                 //else
102                 //      extract_pose_from_pose(m_mrdPose, m_pose);
103         }
104 }
105
106 void BL_ArmatureObject::SetPose(bPose *pose)
107 {
108         m_pose = pose;
109 }
110
111 bool BL_ArmatureObject::SetActiveAction(BL_ActionActuator *act, short priority, double curtime)
112 {
113         if (curtime != m_lastframe){
114                 m_activePriority = 9999;
115                 m_lastframe= curtime;
116                 m_activeAct = NULL;
117         }
118
119         if (priority<=m_activePriority)
120         {
121                 if (m_activeAct && (m_activeAct!=act))
122                         m_activeAct->SetBlendTime(0.0); /* Reset the blend timer */
123                 m_activeAct = act;
124                 m_activePriority = priority;
125                 m_lastframe = curtime;
126         
127                 return true;
128         }
129         else{
130                 act->SetBlendTime(0.0);
131                 return false;
132         }
133         
134 }
135
136 BL_ActionActuator * BL_ArmatureObject::GetActiveAction()
137 {
138         return m_activeAct;
139 }
140
141 void BL_ArmatureObject::GetPose(bPose **pose)
142 {
143         /* If the caller supplies a null pose, create a new one. */
144         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
145                 
146         if (!*pose) {
147                 /*      probably not to good of an idea to
148                         duplicate everying, but it clears up 
149                         a crash and memory leakage when 
150                         &BL_ActionActuator::m_pose is freed
151                 */
152                 int copy_constraint_channels_hack = 1;
153                 copy_pose(pose, m_pose, copy_constraint_channels_hack);
154         }
155         else {
156                 if (*pose == m_pose)
157                         // no need to copy if the pointers are the same
158                         return;
159                 extract_pose_from_pose(*pose, m_pose);
160         }
161 }
162
163 void BL_ArmatureObject::GetMRDPose(bPose **pose)
164 {
165         /* If the caller supplies a null pose, create a new one. */
166         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
167
168         // is this needed anymore?
169         //if (!m_mrdPose){
170         //      copy_pose (&m_mrdPose, m_pose, 0);
171         //}
172
173         if (!*pose) {
174                 // must duplicate the constraints too otherwise we have corruption in free_pose_channels()
175                 // because it will free the blender constraints. 
176                 // Ideally, blender should rememeber that the constraints were not copied so that
177                 // free_pose_channels() would not free them.
178                 copy_pose(pose, m_objArma->pose, 1);
179         }
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 }