svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22717:22875
[blender.git] / source / gameengine / Ketsji / KX_IPO_SGController.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  * Scenegraph controller for ipos.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #if defined(_WIN64)
36 typedef unsigned __int64 uint_ptr;
37 #else
38 typedef unsigned long uint_ptr;
39 #endif
40
41 #ifdef WIN32
42 // This warning tells us about truncation of __long__ stl-generated names.
43 // It can occasionally cause DevStudio to have internal compiler warnings.
44 #pragma warning( disable : 4786 )     
45 #endif
46
47 #include "KX_IPO_SGController.h"
48 #include "KX_ScalarInterpolator.h"
49 #include "KX_GameObject.h"
50 #include "KX_IPhysicsController.h"
51 #include "DNA_ipo_types.h"
52 #include "BLI_arithb.h"
53
54 // All objects should start on frame 1! Will we ever need an object to 
55 // start on another frame, the 1.0 should change.
56 KX_IpoSGController::KX_IpoSGController() 
57 : m_ipo_as_force(false),
58   m_ipo_add(false),
59   m_ipo_local(false),
60   m_modified(true),
61   m_ipotime(1.0),
62   m_ipo_start_initialized(false),
63   m_ipo_start_euler(0.0,0.0,0.0),
64   m_ipo_euler_initialized(false)
65 {
66         m_game_object = NULL;
67         for (int i=0; i < KX_MAX_IPO_CHANNELS; i++)
68                 m_ipo_channels_active[i] = false;
69 }
70
71
72 void KX_IpoSGController::SetOption(
73         int option,
74         int value)
75 {
76         switch (option) {
77         case SG_CONTR_IPO_IPO_AS_FORCE:
78                 m_ipo_as_force = (value != 0);
79                 m_modified = true;
80                 break;
81         case SG_CONTR_IPO_IPO_ADD:
82                 m_ipo_add = (value != 0);
83                 m_modified = true;
84                 break;
85         case SG_CONTR_IPO_RESET:
86                 if (m_ipo_start_initialized && value) {
87                         m_ipo_start_initialized = false;
88                         m_modified = true;
89                 }
90                 break;
91         case SG_CONTR_IPO_LOCAL:
92                 if (value/* && ((SG_Node*)m_pObject)->GetSGParent() == NULL*/) {
93                         // only accept local Ipo if the object has no parent
94                         m_ipo_local = true;
95                 } else {
96                         m_ipo_local = false;
97                 }
98                 m_modified = true;
99                 break;
100         default:
101                 ; /* just ignore the rest */
102         }
103 }
104
105         void 
106 KX_IpoSGController::UpdateSumoReference(
107         )
108 {
109         if (m_game_object) {
110
111         }
112 }
113
114         void 
115 KX_IpoSGController::SetGameObject(
116         KX_GameObject* go
117         )
118 {
119         m_game_object = go;
120 }
121
122
123
124 bool KX_IpoSGController::Update(double currentTime)
125 {
126         if (m_modified)
127         {
128                 T_InterpolatorList::iterator i;
129                 for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
130                         (*i)->Execute(m_ipotime);//currentTime);
131                 }
132                 
133                 SG_Spatial* ob = (SG_Spatial*)m_pObject;
134
135                 //initialization on the first frame of the IPO
136                 if (! m_ipo_start_initialized && currentTime > 0.0) {
137                         m_ipo_start_point = ob->GetLocalPosition();
138                         m_ipo_start_orient = ob->GetLocalOrientation();
139                         m_ipo_start_scale = ob->GetLocalScale();
140                         m_ipo_start_initialized = true;
141                         if (!m_ipo_euler_initialized) {
142                                 // do it only once to avoid angle discontinuities
143                                 m_ipo_start_orient.getEuler(m_ipo_start_euler[0], m_ipo_start_euler[1], m_ipo_start_euler[2]);
144                                 m_ipo_euler_initialized = true;
145                         }
146                 }
147
148                 //modifies position?
149                 if (m_ipo_channels_active[OB_LOC_X] || m_ipo_channels_active[OB_LOC_Y] || m_ipo_channels_active[OB_LOC_Z] || m_ipo_channels_active[OB_DLOC_X] || m_ipo_channels_active[OB_DLOC_Y] || m_ipo_channels_active[OB_DLOC_Z])
150                 {
151                         if (m_ipo_as_force == true) 
152                         {
153                                 if (m_game_object && ob && m_game_object->GetPhysicsController()) 
154                                 {
155                                         m_game_object->GetPhysicsController()->ApplyForce(m_ipo_local ?
156                                                 ob->GetWorldOrientation() * m_ipo_xform.GetPosition() :
157                                                 m_ipo_xform.GetPosition(), false);
158                                 }
159                         } 
160                         else
161                         {
162                                 // Local ipo should be defined with the object position at (0,0,0)
163                                 // Local transform is applied to the object based on initial position
164                                 MT_Point3 newPosition(0.0,0.0,0.0);
165                                 
166                                 if (!m_ipo_add)
167                                         newPosition = ob->GetLocalPosition();
168                                 //apply separate IPO channels if there is any data in them
169                                 //Loc and dLoc act by themselves or are additive
170                                 //LocX and dLocX
171                                 if (m_ipo_channels_active[OB_LOC_X]) {
172                                         newPosition[0] = (m_ipo_channels_active[OB_DLOC_X] ? m_ipo_xform.GetPosition()[0] + m_ipo_xform.GetDeltaPosition()[0] : m_ipo_xform.GetPosition()[0]);
173                                 }
174                                 else if (m_ipo_channels_active[OB_DLOC_X] && m_ipo_start_initialized) {
175                                         newPosition[0] = (((!m_ipo_add)?m_ipo_start_point[0]:0.0) + m_ipo_xform.GetDeltaPosition()[0]);
176                                 }
177                                 //LocY and dLocY
178                                 if (m_ipo_channels_active[OB_LOC_Y]) {
179                                         newPosition[1] = (m_ipo_channels_active[OB_DLOC_Y] ? m_ipo_xform.GetPosition()[1] + m_ipo_xform.GetDeltaPosition()[1] : m_ipo_xform.GetPosition()[1]);
180                                 }
181                                 else if (m_ipo_channels_active[OB_DLOC_Y] && m_ipo_start_initialized) {
182                                         newPosition[1] = (((!m_ipo_add)?m_ipo_start_point[1]:0.0) + m_ipo_xform.GetDeltaPosition()[1]);
183                                 }
184                                 //LocZ and dLocZ
185                                 if (m_ipo_channels_active[OB_LOC_Z]) {
186                                         newPosition[2] = (m_ipo_channels_active[OB_DLOC_Z] ? m_ipo_xform.GetPosition()[2] + m_ipo_xform.GetDeltaPosition()[2] : m_ipo_xform.GetPosition()[2]);
187                                 }
188                                 else if (m_ipo_channels_active[OB_DLOC_Z] && m_ipo_start_initialized) {
189                                         newPosition[2] = (((!m_ipo_add)?m_ipo_start_point[2]:0.0) + m_ipo_xform.GetDeltaPosition()[2]);
190                                 }
191                                 if (m_ipo_add) {
192                                         if (m_ipo_local)
193                                                 newPosition = m_ipo_start_point + m_ipo_start_scale*(m_ipo_start_orient*newPosition);
194                                         else
195                                                 newPosition = m_ipo_start_point + newPosition;
196                                 }
197                                 if (m_game_object)
198                                         m_game_object->NodeSetLocalPosition(newPosition);
199                         }
200                 }
201                 //modifies orientation?
202                 if (m_ipo_channels_active[OB_ROT_X] || m_ipo_channels_active[OB_ROT_Y] || m_ipo_channels_active[OB_ROT_Z] || m_ipo_channels_active[OB_DROT_X] || m_ipo_channels_active[OB_DROT_Y] || m_ipo_channels_active[OB_DROT_Z]) {
203                         if (m_ipo_as_force) {
204                                 
205                                 if (m_game_object && ob) {
206                                         m_game_object->ApplyTorque(m_ipo_local ?
207                                                 ob->GetWorldOrientation() * m_ipo_xform.GetEulerAngles() :
208                                                 m_ipo_xform.GetEulerAngles(), false);
209                                 }
210                         } else if (m_ipo_add) {
211                                 if (m_ipo_start_initialized) {
212                                         double yaw=0, pitch=0,  roll=0; //delta Euler angles
213
214                                         //RotX and dRotX
215                                         if (m_ipo_channels_active[OB_ROT_X])
216                                                 yaw += m_ipo_xform.GetEulerAngles()[0];
217                                         if (m_ipo_channels_active[OB_DROT_X])
218                                                 yaw += m_ipo_xform.GetDeltaEulerAngles()[0];
219
220                                         //RotY dRotY
221                                         if (m_ipo_channels_active[OB_ROT_Y])
222                                                 pitch += m_ipo_xform.GetEulerAngles()[1];
223                                         if (m_ipo_channels_active[OB_DROT_Y])
224                                                 pitch += m_ipo_xform.GetDeltaEulerAngles()[1];
225                                         
226                                         //RotZ and dRotZ
227                                         if (m_ipo_channels_active[OB_ROT_Z])
228                                                 roll += m_ipo_xform.GetEulerAngles()[2];
229                                         if (m_ipo_channels_active[OB_DROT_Z])
230                                                 roll += m_ipo_xform.GetDeltaEulerAngles()[2];
231
232                                         MT_Matrix3x3 rotation(MT_Vector3(yaw, pitch, roll));
233                                         if (m_ipo_local)
234                                                 rotation = m_ipo_start_orient * rotation;
235                                         else
236                                                 rotation = rotation * m_ipo_start_orient;
237                                         if (m_game_object)
238                                                 m_game_object->NodeSetLocalOrientation(rotation);
239                                 }
240                         } else if (m_ipo_channels_active[OB_ROT_X] || m_ipo_channels_active[OB_ROT_Y] || m_ipo_channels_active[OB_ROT_Z]) {
241                                 if (m_ipo_euler_initialized) {
242                                         // assume all channel absolute
243                                         // All 3 channels should be specified but if they are not, we will take 
244                                         // the value at the start of the game to avoid angle sign reversal 
245                                         double yaw=m_ipo_start_euler[0], pitch=m_ipo_start_euler[1], roll=m_ipo_start_euler[2];
246
247                                         //RotX and dRotX
248                                         if (m_ipo_channels_active[OB_ROT_X]) {
249                                                 yaw = (m_ipo_channels_active[OB_DROT_X] ? (m_ipo_xform.GetEulerAngles()[0] + m_ipo_xform.GetDeltaEulerAngles()[0]) : m_ipo_xform.GetEulerAngles()[0] );
250                                         }
251                                         else if (m_ipo_channels_active[OB_DROT_X]) {
252                                                 yaw += m_ipo_xform.GetDeltaEulerAngles()[0];
253                                         }
254
255                                         //RotY dRotY
256                                         if (m_ipo_channels_active[OB_ROT_Y]) {
257                                                 pitch = (m_ipo_channels_active[OB_DROT_Y] ? (m_ipo_xform.GetEulerAngles()[1] + m_ipo_xform.GetDeltaEulerAngles()[1]) : m_ipo_xform.GetEulerAngles()[1] );
258                                         }
259                                         else if (m_ipo_channels_active[OB_DROT_Y]) {
260                                                 pitch += m_ipo_xform.GetDeltaEulerAngles()[1];
261                                         }
262                                         
263                                         //RotZ and dRotZ
264                                         if (m_ipo_channels_active[OB_ROT_Z]) {
265                                                 roll = (m_ipo_channels_active[OB_DROT_Z] ? (m_ipo_xform.GetEulerAngles()[2] + m_ipo_xform.GetDeltaEulerAngles()[2]) : m_ipo_xform.GetEulerAngles()[2] );
266                                         }
267                                         else if (m_ipo_channels_active[OB_DROT_Z]) {
268                                                 roll += m_ipo_xform.GetDeltaEulerAngles()[2];
269                                         }
270                                         if (m_game_object)
271                                                 m_game_object->NodeSetLocalOrientation(MT_Vector3(yaw, pitch, roll));
272                                 }
273                         } else if (m_ipo_start_initialized) {
274                                 // only DROT, treat as Add
275                                 double yaw=0, pitch=0,  roll=0; //delta Euler angles
276
277                                 //dRotX
278                                 if (m_ipo_channels_active[OB_DROT_X])
279                                         yaw = m_ipo_xform.GetDeltaEulerAngles()[0];
280
281                                 //dRotY
282                                 if (m_ipo_channels_active[OB_DROT_Y])
283                                         pitch = m_ipo_xform.GetDeltaEulerAngles()[1];
284                                 
285                                 //dRotZ
286                                 if (m_ipo_channels_active[OB_DROT_Z])
287                                         roll = m_ipo_xform.GetDeltaEulerAngles()[2];
288
289                                 // dRot are always local
290                                 MT_Matrix3x3 rotation(MT_Vector3(yaw, pitch, roll));
291                                 rotation = m_ipo_start_orient * rotation;
292                                 if (m_game_object)
293                                         m_game_object->NodeSetLocalOrientation(rotation);
294                         }
295                 }
296                 //modifies scale?
297                 if (m_ipo_channels_active[OB_SIZE_X] || m_ipo_channels_active[OB_SIZE_Y] || m_ipo_channels_active[OB_SIZE_Z] || m_ipo_channels_active[OB_DSIZE_X] || m_ipo_channels_active[OB_DSIZE_Y] || m_ipo_channels_active[OB_DSIZE_Z]) {
298                         //default is no scale change
299                         MT_Vector3 newScale(1.0,1.0,1.0);
300                         if (!m_ipo_add)
301                                 newScale = ob->GetLocalScale();
302
303                         if (m_ipo_channels_active[OB_SIZE_X]) {
304                                 newScale[0] = (m_ipo_channels_active[OB_DSIZE_X] ? (m_ipo_xform.GetScaling()[0] + m_ipo_xform.GetDeltaScaling()[0]) : m_ipo_xform.GetScaling()[0]);
305                         }
306                         else if (m_ipo_channels_active[OB_DSIZE_X] && m_ipo_start_initialized) {
307                                 newScale[0] = (m_ipo_xform.GetDeltaScaling()[0] + ((!m_ipo_add)?m_ipo_start_scale[0]:0.0));
308                         }
309
310                         //RotY dRotY
311                         if (m_ipo_channels_active[OB_SIZE_Y]) {
312                                 newScale[1] = (m_ipo_channels_active[OB_DSIZE_Y] ? (m_ipo_xform.GetScaling()[1] + m_ipo_xform.GetDeltaScaling()[1]): m_ipo_xform.GetScaling()[1]);
313                         }
314                         else if (m_ipo_channels_active[OB_DSIZE_Y] && m_ipo_start_initialized) {
315                                 newScale[1] = (m_ipo_xform.GetDeltaScaling()[1] + ((!m_ipo_add)?m_ipo_start_scale[1]:0.0));
316                         }
317                         
318                         //RotZ and dRotZ
319                         if (m_ipo_channels_active[OB_SIZE_Z]) {
320                                 newScale[2] = (m_ipo_channels_active[OB_DSIZE_Z] ? (m_ipo_xform.GetScaling()[2] + m_ipo_xform.GetDeltaScaling()[2]) : m_ipo_xform.GetScaling()[2]);
321                         }
322                         else if (m_ipo_channels_active[OB_DSIZE_Z] && m_ipo_start_initialized) {
323                                 newScale[2] = (m_ipo_xform.GetDeltaScaling()[2] + ((!m_ipo_add)?m_ipo_start_scale[2]:1.0));
324                         }
325
326                         if (m_ipo_add) {
327                                 newScale = m_ipo_start_scale * newScale;
328                         }
329                         if (m_game_object)
330                                 m_game_object->NodeSetLocalScale(newScale);
331                 }
332
333                 m_modified=false;
334         }
335         return false;
336 }
337
338
339 void KX_IpoSGController::AddInterpolator(KX_IInterpolator* interp)
340 {
341         this->m_interpolators.push_back(interp);
342 }
343
344 SG_Controller*  KX_IpoSGController::GetReplica(class SG_Node* destnode)
345 {
346         KX_IpoSGController* iporeplica = new KX_IpoSGController(*this);
347         // clear object that ipo acts on in the replica.
348         iporeplica->ClearObject();
349         iporeplica->SetGameObject((KX_GameObject*)destnode->GetSGClientObject());
350
351         // dirty hack, ask Gino for a better solution in the ipo implementation
352         // hacken en zagen, in what we call datahiding, not written for replication :(
353
354         T_InterpolatorList oldlist = m_interpolators;
355         iporeplica->m_interpolators.clear();
356
357         T_InterpolatorList::iterator i;
358         for (i = oldlist.begin(); !(i == oldlist.end()); ++i) {
359                 KX_ScalarInterpolator* copyipo = new KX_ScalarInterpolator(*((KX_ScalarInterpolator*)*i));
360                 iporeplica->AddInterpolator(copyipo);
361
362                 MT_Scalar* scaal = ((KX_ScalarInterpolator*)*i)->GetTarget();
363                 uint_ptr orgbase = (uint_ptr)&m_ipo_xform;
364                 uint_ptr orgloc = (uint_ptr)scaal;
365                 uint_ptr offset = orgloc-orgbase;
366                 uint_ptr newaddrbase = (uint_ptr)&iporeplica->m_ipo_xform;
367                 newaddrbase += offset;
368                 MT_Scalar* blaptr = (MT_Scalar*) newaddrbase;
369                 copyipo->SetNewTarget((MT_Scalar*)blaptr);
370         }
371         
372         return iporeplica;
373 }
374
375 KX_IpoSGController::~KX_IpoSGController()
376 {
377
378         T_InterpolatorList::iterator i;
379         for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
380                 delete (*i);
381         }
382         
383 }