Sorry to break the cvs-closed status, so if you really need to make a new 2.40 build...
[blender-staging.git] / source / gameengine / Converter / BL_ArmatureObject.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include "BL_ArmatureObject.h"
34 #include "BL_ActionActuator.h"
35 #include "BLI_blenlib.h"
36 #include "BKE_action.h"
37 #include "BKE_armature.h"
38 #include "GEN_Map.h"
39 #include "GEN_HashedPtr.h"
40 #include "MEM_guardedalloc.h"
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_object_types.h"
44
45 #include "MT_Matrix4x4.h"
46
47 #ifdef HAVE_CONFIG_H
48 #include <config.h>
49 #endif
50
51
52 BL_ArmatureObject::BL_ArmatureObject(
53                                 void* sgReplicationInfo, 
54                                 SG_Callbacks callbacks, 
55                                 Object *armature )
56
57 :       KX_GameObject(sgReplicationInfo,callbacks),
58         m_objArma(armature),
59         m_mrdPose(NULL),
60         m_lastframe(0.),
61         m_activeAct(NULL),
62         m_activePriority(999)
63 {
64         m_armature = get_armature(m_objArma);
65         m_pose = m_objArma->pose;
66 }
67
68
69 CValue* BL_ArmatureObject::GetReplica()
70 {
71         BL_ArmatureObject* replica = new BL_ArmatureObject(*this);
72         
73         // this will copy properties and so on...
74         CValue::AddDataToReplica(replica);
75
76         ProcessReplica(replica);
77         return replica;
78 }
79
80 void BL_ArmatureObject::ProcessReplica(BL_ArmatureObject *replica)
81 {
82         KX_GameObject::ProcessReplica(replica);
83
84 }
85
86 BL_ArmatureObject::~BL_ArmatureObject()
87 {
88         if (m_mrdPose){
89                 free_pose_channels(m_mrdPose);
90                 MEM_freeN(m_mrdPose);
91         }
92 }
93
94 /* note, you can only call this for exisiting Armature objects, and not mix it with other Armatures */
95 /* there is only 1 unique Pose per Armature */
96 void BL_ArmatureObject::ApplyPose()
97 {
98         if (m_pose){
99                 // copy to armature object
100                 extract_pose_from_pose(m_objArma->pose, m_pose);
101                 
102                 // is this needed anymore?
103                 //if (!m_mrdPose)
104                 //      copy_pose (&m_mrdPose, m_pose, 0);
105                 //else
106                 //      extract_pose_from_pose(m_mrdPose, m_pose);
107         }
108 }
109
110 void BL_ArmatureObject::SetPose(bPose *pose)
111 {
112         m_pose = pose;
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         }
122
123         if (priority<=m_activePriority)
124         {
125                 if (m_activeAct && (m_activeAct!=act))
126                         m_activeAct->SetBlendTime(0.0); /* Reset the blend timer */
127                 m_activeAct = act;
128                 m_activePriority = priority;
129                 m_lastframe = curtime;
130         
131                 return true;
132         }
133         else{
134                 act->SetBlendTime(0.0);
135                 return false;
136         }
137         
138 }
139
140 BL_ActionActuator * BL_ArmatureObject::GetActiveAction()
141 {
142         return m_activeAct;
143 }
144
145 void BL_ArmatureObject::GetPose(bPose **pose)
146 {
147         /* If the caller supplies a null pose, create a new one. */
148         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
149         if (!*pose)
150                 copy_pose(pose, m_pose, 0);
151         else
152                 extract_pose_from_pose(*pose, m_pose);
153
154 }
155
156 void BL_ArmatureObject::GetMRDPose(bPose **pose)
157 {
158         /* If the caller supplies a null pose, create a new one. */
159         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
160
161         // is this needed anymore?
162         //if (!m_mrdPose){
163         //      copy_pose (&m_mrdPose, m_pose, 0);
164         //}
165
166         if (!*pose)
167                 copy_pose(pose, m_objArma->pose, 0);
168         else
169                 extract_pose_from_pose(*pose, m_objArma->pose);
170
171 }
172
173 short BL_ArmatureObject::GetActivePriority()
174 {
175         return m_activePriority;
176 }
177
178 double BL_ArmatureObject::GetLastFrame()
179 {
180         return m_lastframe;
181 }
182
183 bool BL_ArmatureObject::GetBoneMatrix(Bone* bone, MT_Matrix4x4& matrix) const
184 {
185         // ton hack
186         bPoseChannel *pchan= get_pose_channel(m_pose, bone->name);
187
188         if(pchan) {
189                 matrix.setValue(&pchan->pose_mat[0][0]);
190                 return true;
191         }
192         return false;
193 }
194
195 float BL_ArmatureObject::GetBoneLength(Bone* bone) const
196 {
197         return (float)(MT_Point3(bone->head) - MT_Point3(bone->tail)).length();
198 }