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