33e4e179071385ba557f711f5d07692acc74d929
[blender.git] / source / gameengine / Ketsji / BL_Action.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <cstdlib>
31
32 #include "BL_Action.h"
33 #include "BL_ArmatureObject.h"
34 #include "BL_DeformableGameObject.h"
35 #include "BL_ShapeDeformer.h"
36 #include "KX_IpoConvert.h"
37 #include "KX_GameObject.h"
38
39 // These three are for getting the action from the logic manager
40 #include "KX_Scene.h"
41 #include "KX_PythonInit.h"
42 #include "SCA_LogicManager.h"
43
44 extern "C" {
45 #include "BKE_animsys.h"
46 #include "BKE_action.h"
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49 #include "DNA_nla_types.h"
50 }
51
52 BL_Action::BL_Action(class KX_GameObject* gameobj)
53 :
54         m_obj(gameobj),
55         m_startframe(0.f),
56         m_endframe(0.f),
57         m_blendin(0.f),
58         m_playmode(0),
59         m_endtime(0.f),
60         m_localtime(0.f),
61         m_blendframe(0.f),
62         m_blendstart(0.f),
63         m_speed(0.f),
64         m_priority(0),
65         m_ipo_flags(0),
66         m_pose(NULL),
67         m_blendpose(NULL),
68         m_sg_contr(NULL),
69         m_ptrrna(NULL),
70         m_done(true),
71         m_bcalc_local_time(true)
72 {
73         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
74         {
75                 BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
76
77                 m_ptrrna = new PointerRNA();
78                 RNA_id_pointer_create(&obj->GetArmatureObject()->id, m_ptrrna);
79         }
80         else
81         {
82                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
83                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
84
85                 if (shape_deformer)
86                 {
87                         m_ptrrna = new PointerRNA();
88                         RNA_id_pointer_create(&shape_deformer->GetKey()->id, m_ptrrna);
89                 }
90         }
91 }
92
93 BL_Action::~BL_Action()
94 {
95         if (m_pose)
96                 game_free_pose(m_pose);
97         if (m_blendpose)
98                 game_free_pose(m_blendpose);
99         if (m_sg_contr)
100         {
101                 m_obj->GetSGNode()->RemoveSGController(m_sg_contr);
102                 delete m_sg_contr;
103         }
104         if (m_ptrrna)
105                 delete m_ptrrna;
106 }
107
108 bool BL_Action::Play(const char* name,
109                                         float start,
110                                         float end,
111                                         short priority,
112                                         float blendin,
113                                         short play_mode,
114                                         short blend_mode,
115                                         short ipo_flags,
116                                         float playback_speed)
117 {
118
119         // Only start playing a new action if we're done, or if
120         // the new action has a higher priority
121         if (priority != 0 && !IsDone() && priority >= m_priority)
122                 return false;
123         m_priority = priority;
124         bAction* prev_action = m_action;
125
126         // First try to load the action
127         m_action = (bAction*)KX_GetActiveScene()->GetLogicManager()->GetActionByName(name);
128         if (!m_action)
129         {
130                 printf("Failed to load action: %s\n", name);
131                 m_done = true;
132                 return false;
133         }
134
135         if (prev_action != m_action)
136         {
137                 // Create an SG_Controller
138                 m_sg_contr = BL_CreateIPO(m_action, m_obj, KX_GetActiveScene()->GetSceneConverter());
139                 m_obj->GetSGNode()->AddSGController(m_sg_contr);
140                 m_sg_contr->SetObject(m_obj->GetSGNode());
141         }
142         
143         m_ipo_flags = ipo_flags;
144         InitIPO();
145
146         // Setup blendin shapes/poses
147         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
148         {
149                 if (!m_blendpose)
150                 {
151                         BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
152                         obj->GetMRDPose(&m_blendpose);
153                 }
154         }
155         else
156         {
157                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
158                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
159                 
160                 obj->GetShape(m_blendshape);
161
162                 // Now that we have the previous blend shape saved, we can clear out the key to avoid any
163                 // further interference.
164                 KeyBlock *kb;
165                 for (kb=(KeyBlock*)shape_deformer->GetKey()->block.first; kb; kb=(KeyBlock*)kb->next)
166                         kb->curval = 0.f;
167
168         }
169
170         // Now that we have an action, we have something we can play
171         m_starttime = KX_GetActiveEngine()->GetFrameTime();
172         m_startframe = m_localtime = start;
173         m_endframe = end;
174         m_blendin = blendin;
175         m_playmode = play_mode;
176         m_blendmode = blend_mode;
177         m_endtime = 0.f;
178         m_blendframe = 0.f;
179         m_blendstart = 0.f;
180         m_speed = playback_speed;
181         
182         m_done = false;
183
184         return true;
185 }
186
187 void BL_Action::Stop()
188 {
189         m_done = true;
190 }
191
192 void BL_Action::InitIPO()
193 {
194                 // Initialize the IPO
195                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
196                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, m_ipo_flags & ACT_IPOFLAG_FORCE);
197                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, m_ipo_flags & ACT_IPOFLAG_ADD);
198                 m_sg_contr->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, m_ipo_flags & ACT_IPOFLAG_LOCAL);
199 }
200
201 float BL_Action::GetFrame()
202 {
203         return m_localtime;
204 }
205
206 void BL_Action::SetFrame(float frame)
207 {
208         // Clamp the frame to the start and end frame
209         if (frame < min(m_startframe, m_endframe))
210                 frame = min(m_startframe, m_endframe);
211         else if (frame > max(m_startframe, m_endframe))
212                 frame = max(m_startframe, m_endframe);
213
214         m_localtime = frame;
215         m_bcalc_local_time = false;
216 }
217
218 void BL_Action::SetLocalTime(float curtime)
219 {
220         float dt = (curtime-m_starttime)*KX_KetsjiEngine::GetAnimFrameRate()*m_speed;
221
222         if (m_endframe < m_startframe)
223                 dt = -dt;
224
225         m_localtime = m_startframe + dt;
226 }
227
228 void BL_Action::IncrementBlending(float curtime)
229 {
230         // Setup m_blendstart if we need to
231         if (m_blendstart == 0.f)
232                 m_blendstart = curtime;
233         
234         // Bump the blend frame
235         m_blendframe = (curtime - m_blendstart)*KX_KetsjiEngine::GetAnimFrameRate();
236
237         // Clamp
238         if (m_blendframe>m_blendin)
239                 m_blendframe = m_blendin;
240 }
241
242
243 void BL_Action::BlendShape(Key* key, float srcweight)
244 {
245         vector<float>::const_iterator it;
246         float dstweight;
247         KeyBlock *kb;
248         
249         dstweight = 1.0F - srcweight;
250         //printf("Dst: %f\tSrc: %f\n", srcweight, dstweight);
251         for (it=m_blendshape.begin(), kb = (KeyBlock*)key->block.first; 
252                  kb && it != m_blendshape.end(); 
253                  kb = (KeyBlock*)kb->next, it++) {
254                 //printf("OirgKeys: %f\t%f\n", kb->curval, (*it));
255                 kb->curval = kb->curval * dstweight + (*it) * srcweight;
256                 //printf("NewKey: %f\n", kb->curval);
257         }
258         //printf("\n");
259 }
260
261 void BL_Action::Update(float curtime)
262 {
263         // Don't bother if we're done with the animation
264         if (m_done)
265                 return;
266
267         // We only want to calculate the current time if we weren't given a frame (e.g., from SetFrame())
268         if (m_bcalc_local_time)
269         {
270                 curtime -= KX_KetsjiEngine::GetSuspendedDelta();
271                 SetLocalTime(curtime);
272         }
273         else
274         {
275                 m_bcalc_local_time = true;
276         }
277
278         // Handle wrap around
279         if (m_localtime < min(m_startframe, m_endframe) || m_localtime > max(m_startframe, m_endframe))
280         {
281                 switch(m_playmode)
282                 {
283                 case ACT_MODE_PLAY:
284                         // Clamp
285                         m_localtime = m_endframe;
286                         m_done = true;
287                         break;
288                 case ACT_MODE_LOOP:
289                         // Put the time back to the beginning
290                         m_localtime = m_startframe;
291                         m_starttime = curtime;
292                         break;
293                 case ACT_MODE_PING_PONG:
294                         // Swap the start and end frames
295                         float temp = m_startframe;
296                         m_startframe = m_endframe;
297                         m_endframe = temp;
298
299                         m_starttime = curtime;
300
301                         break;
302                 }
303
304                 if (!m_done && m_sg_contr)
305                         InitIPO();
306         }
307
308         if (m_obj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE)
309         {
310                 BL_ArmatureObject *obj = (BL_ArmatureObject*)m_obj;
311                 obj->GetPose(&m_pose);
312
313                 // Extract the pose from the action
314                 {
315                         Object *arm = obj->GetArmatureObject();
316                         bPose *temp = arm->pose;
317
318                         arm->pose = m_pose;
319                         animsys_evaluate_action(m_ptrrna, m_action, NULL, m_localtime);
320
321                         arm->pose = temp;
322                 }
323
324                 // Handle blending between armature actions
325                 if (m_blendin && m_blendframe<m_blendin)
326                 {
327                         IncrementBlending(curtime);
328
329                         // Calculate weight
330                         float weight = 1.f - (m_blendframe/m_blendin);
331
332                         // Blend the poses
333                         game_blend_poses(m_pose, m_blendpose, weight, ACTSTRIPMODE_BLEND);
334                 }
335
336
337                 // Handle layer blending
338                 if (m_blendmode != ACT_BLEND_NONE)
339                 {
340                         obj->GetMRDPose(&m_blendpose);
341                         game_blend_poses(m_pose, m_blendpose, 1.f, ACTSTRIPMODE_ADD);
342                 }
343
344                 obj->SetPose(m_pose);
345
346                 obj->SetActiveAction(NULL, 0, curtime);
347         }
348         else
349         {
350                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)m_obj;
351                 BL_ShapeDeformer *shape_deformer = dynamic_cast<BL_ShapeDeformer*>(obj->GetDeformer());
352
353                 // Handle shape actions if we have any
354                 if (shape_deformer)
355                 {
356                         Key *key = shape_deformer->GetKey();
357
358
359                         animsys_evaluate_action(m_ptrrna, m_action, NULL, m_localtime);
360
361                         // Handle blending between shape actions
362                         if (m_blendin && m_blendframe < m_blendin)
363                         {
364                                 IncrementBlending(curtime);
365
366                                 float weight = 1.f - (m_blendframe/m_blendin);
367
368                                 // We go through and clear out the keyblocks so there isn't any interference
369                                 // from other shape actions
370                                 KeyBlock *kb;
371                                 for (kb=(KeyBlock*)key->block.first; kb; kb=(KeyBlock*)kb->next)
372                                         kb->curval = 0.f;
373
374                                 // Now blend the shape
375                                 BlendShape(key, weight);
376                         }
377
378                         obj->SetActiveAction(NULL, 0, m_localtime);
379                 }
380
381
382                 InitIPO();
383                 m_obj->UpdateIPO(m_localtime, m_ipo_flags & ACT_IPOFLAG_CHILD);
384         }
385 }