svn merge -r 14236:14505 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_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                 extract_pose_from_pose(*pose, m_pose);
158
159 }
160
161 void BL_ArmatureObject::GetMRDPose(bPose **pose)
162 {
163         /* If the caller supplies a null pose, create a new one. */
164         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
165
166         // is this needed anymore?
167         //if (!m_mrdPose){
168         //      copy_pose (&m_mrdPose, m_pose, 0);
169         //}
170
171         if (!*pose)
172                 copy_pose(pose, m_objArma->pose, 0);
173         else
174                 extract_pose_from_pose(*pose, m_objArma->pose);
175
176 }
177
178 short BL_ArmatureObject::GetActivePriority()
179 {
180         return m_activePriority;
181 }
182
183 double BL_ArmatureObject::GetLastFrame()
184 {
185         return m_lastframe;
186 }
187
188 bool BL_ArmatureObject::GetBoneMatrix(Bone* bone, MT_Matrix4x4& matrix) const
189 {
190         Object* par_arma = m_objArma;
191         where_is_pose(par_arma);
192         bPoseChannel *pchan= get_pose_channel(par_arma->pose, bone->name);
193
194         if(pchan) {
195                 matrix.setValue(&pchan->pose_mat[0][0]);
196                 return true;
197         }
198         return false;
199 }
200
201 float BL_ArmatureObject::GetBoneLength(Bone* bone) const
202 {
203         return (float)(MT_Point3(bone->head) - MT_Point3(bone->tail)).length();
204 }