Syncing Orange branch with bf-blender
[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 CValue* BL_ArmatureObject::GetReplica()
52 {
53         BL_ArmatureObject* replica = new BL_ArmatureObject(*this);
54         
55         // this will copy properties and so on...
56         CValue::AddDataToReplica(replica);
57
58         ProcessReplica(replica);
59         return replica;
60 }
61
62 void BL_ArmatureObject::ProcessReplica(BL_ArmatureObject *replica)
63 {
64         KX_GameObject::ProcessReplica(replica);
65
66 }
67
68 BL_ArmatureObject::~BL_ArmatureObject()
69 {
70         if (m_mrdPose){
71 //              clear_pose(m_mrdPose);
72                 MEM_freeN(m_mrdPose);
73         }
74 }
75
76 void BL_ArmatureObject::ApplyPose()
77 {
78         if (m_pose){
79 //              apply_pose_armature(GetArmature(), m_pose, 1);
80                 if (!m_mrdPose)
81                         copy_pose (&m_mrdPose, m_pose, 0);
82                 else
83                         extract_pose_from_pose(m_mrdPose, m_pose);
84         }
85 }
86
87 void BL_ArmatureObject::SetPose(bPose *pose)
88 {
89         m_pose = pose;
90 }
91
92 bool BL_ArmatureObject::SetActiveAction(BL_ActionActuator *act, short priority, double curtime)
93 {
94         if (curtime != m_lastframe){
95                 m_activePriority = 9999;
96                 m_lastframe= curtime;
97                 m_activeAct = NULL;
98         }
99
100         if (priority<=m_activePriority)
101         {
102                 if (m_activeAct && (m_activeAct!=act))
103                         m_activeAct->SetBlendTime(0.0); /* Reset the blend timer */
104                 m_activeAct = act;
105                 m_activePriority = priority;
106                 m_lastframe = curtime;
107         
108                 return true;
109         }
110         else{
111                 act->SetBlendTime(0.0);
112                 return false;
113         }
114         
115 }
116
117 BL_ActionActuator * BL_ArmatureObject::GetActiveAction()
118 {
119         return m_activeAct;
120 }
121
122 void BL_ArmatureObject::GetPose(bPose **pose)
123 {
124         /* If the caller supplies a null pose, create a new one. */
125         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
126         if (!*pose)
127                 copy_pose(pose, m_pose, 0);
128         else
129                 extract_pose_from_pose(*pose, m_pose);
130
131 }
132
133 void BL_ArmatureObject::GetMRDPose(bPose **pose)
134 {
135         /* If the caller supplies a null pose, create a new one. */
136         /* Otherwise, copy the armature's pose channels into the caller-supplied pose */
137
138         if (!m_mrdPose){
139                 copy_pose (&m_mrdPose, m_pose, 0);
140         }
141
142         if (!*pose)
143                 copy_pose(pose, m_mrdPose, 0);
144         else
145                 extract_pose_from_pose(*pose, m_mrdPose);
146
147 }
148
149 short BL_ArmatureObject::GetActivePriority()
150 {
151         return m_activePriority;
152 }
153
154 double BL_ArmatureObject::GetLastFrame()
155 {
156         return m_lastframe;
157 }
158
159 bool BL_ArmatureObject::GetBoneMatrix(Bone* bone, MT_Matrix4x4& matrix) const
160 {
161         // ton hack
162         bPoseChannel *pchan= get_pose_channel(m_pose, bone->name);
163         
164 //      MT_assert(verify_boneptr((bArmature*) GetArmature(), bone) && "Bone is not part of this armature.");
165         
166         matrix.setValue(&pchan->pose_mat[0][0]);
167         
168         return true;
169 }
170
171 float BL_ArmatureObject::GetBoneLength(Bone* bone) const
172 {
173 //      MT_assert(verify_boneptr((bArmature*) GetArmature(), bone) && "Bone is not part of this armature.");
174         
175         return (MT_Point3(bone->head) - MT_Point3(bone->tail)).length();
176 }
177
178