Text Editor (GSOC 2008)
[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_framePose(NULL),
58         m_lastframe(0.),
59         m_activeAct(NULL),
60         m_activePriority(999)
61 {
62         m_armature = get_armature(m_objArma);
63         m_pose = m_objArma->pose;
64 }
65
66
67 CValue* BL_ArmatureObject::GetReplica()
68 {
69         BL_ArmatureObject* replica = new BL_ArmatureObject(*this);
70         
71         // this will copy properties and so on...
72         CValue::AddDataToReplica(replica);
73
74         ProcessReplica(replica);
75         return replica;
76 }
77
78 void BL_ArmatureObject::ProcessReplica(BL_ArmatureObject *replica)
79 {
80         KX_GameObject::ProcessReplica(replica);
81
82 }
83
84 BL_ArmatureObject::~BL_ArmatureObject()
85 {
86         if (m_mrdPose)
87                 free_pose(m_mrdPose);
88 }
89
90 /* note, you can only call this for exisiting Armature objects, and not mix it with other Armatures */
91 /* there is only 1 unique Pose per Armature */
92 void BL_ArmatureObject::ApplyPose()
93 {
94         if (m_pose) {
95                 // copy to armature object
96                 if (m_objArma->pose != m_pose)/* This should never happen but it does - Campbell */
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                 // remember the pose at the start of the frame
119                 m_framePose = m_pose;
120         }
121
122         if (priority<=m_activePriority)
123         {
124                 if (priority<m_activePriority)
125                         // this action overwrites the previous ones, start from initial pose to cancel their effects
126                         m_pose = m_framePose;
127                 if (m_activeAct && (m_activeAct!=act))
128                         m_activeAct->SetBlendTime(0.0); /* Reset the blend timer */
129                 m_activeAct = act;
130                 m_activePriority = priority;
131                 m_lastframe = curtime;
132         
133                 return true;
134         }
135         else{
136                 act->SetBlendTime(0.0);
137                 return false;
138         }
139         
140 }
141
142 BL_ActionActuator * BL_ArmatureObject::GetActiveAction()
143 {
144         return m_activeAct;
145 }
146
147 void BL_ArmatureObject::GetPose(bPose **pose)
148 {
149         /* If the caller supplies a null pose, create a new one. */
150         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
151                 
152         if (!*pose) {
153                 /*      probably not to good of an idea to
154                         duplicate everying, but it clears up 
155                         a crash and memory leakage when 
156                         &BL_ActionActuator::m_pose is freed
157                 */
158                 int copy_constraint_channels_hack = 1;
159                 copy_pose(pose, m_pose, copy_constraint_channels_hack);
160         }
161         else {
162                 if (*pose == m_pose)
163                         // no need to copy if the pointers are the same
164                         return;
165                 extract_pose_from_pose(*pose, m_pose);
166         }
167 }
168
169 void BL_ArmatureObject::GetMRDPose(bPose **pose)
170 {
171         /* If the caller supplies a null pose, create a new one. */
172         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
173
174         // is this needed anymore?
175         //if (!m_mrdPose){
176         //      copy_pose (&m_mrdPose, m_pose, 0);
177         //}
178
179         if (!*pose) {
180                 // must duplicate the constraints too otherwise we have corruption in free_pose_channels()
181                 // because it will free the blender constraints. 
182                 // Ideally, blender should rememeber that the constraints were not copied so that
183                 // free_pose_channels() would not free them.
184                 copy_pose(pose, m_objArma->pose, 1);
185         }
186         else
187                 extract_pose_from_pose(*pose, m_objArma->pose);
188
189 }
190
191 short BL_ArmatureObject::GetActivePriority()
192 {
193         return m_activePriority;
194 }
195
196 double BL_ArmatureObject::GetLastFrame()
197 {
198         return m_lastframe;
199 }
200
201 bool BL_ArmatureObject::GetBoneMatrix(Bone* bone, MT_Matrix4x4& matrix) const
202 {
203         Object* par_arma = m_objArma;
204         where_is_pose(par_arma);
205         bPoseChannel *pchan= get_pose_channel(par_arma->pose, bone->name);
206
207         if(pchan) {
208                 matrix.setValue(&pchan->pose_mat[0][0]);
209                 return true;
210         }
211         return false;
212 }
213
214 float BL_ArmatureObject::GetBoneLength(Bone* bone) const
215 {
216         return (float)(MT_Point3(bone->head) - MT_Point3(bone->tail)).length();
217 }