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