Merged 15170:15635 from trunk (no conflicts or even merges)
[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_ipo_start_initialized(false),
62   m_ipotime(1.0)
63 {
64         m_game_object = NULL;
65         for (int i=0; i < KX_MAX_IPO_CHANNELS; i++)
66                 m_ipo_channels_active[i] = false;
67 }
68
69
70 void KX_IpoSGController::SetOption(
71         int option,
72         int value)
73 {
74         switch (option) {
75         case SG_CONTR_IPO_IPO_AS_FORCE:
76                 m_ipo_as_force = (value != 0);
77                 m_modified = true;
78                 break;
79         case SG_CONTR_IPO_IPO_ADD:
80                 m_ipo_add = (value != 0);
81                 m_modified = true;
82                 break;
83         case SG_CONTR_IPO_RESET:
84                 if (m_ipo_start_initialized && value) {
85                         m_ipo_start_initialized = false;
86                         m_modified = true;
87                 }
88                 break;
89         case SG_CONTR_IPO_LOCAL:
90                 if (value/* && ((SG_Node*)m_pObject)->GetSGParent() == NULL*/) {
91                         // only accept local Ipo if the object has no parent
92                         m_ipo_local = true;
93                 } else {
94                         m_ipo_local = false;
95                 }
96                 m_modified = true;
97                 break;
98         default:
99                 ; /* just ignore the rest */
100         }
101 }
102
103         void 
104 KX_IpoSGController::UpdateSumoReference(
105         )
106 {
107         if (m_game_object) {
108
109         }
110 }
111
112         void 
113 KX_IpoSGController::SetGameObject(
114         KX_GameObject* go
115         )
116 {
117         m_game_object = go;
118 }
119
120
121
122 bool KX_IpoSGController::Update(double currentTime)
123 {
124         if (m_modified)
125         {
126                 T_InterpolatorList::iterator i;
127                 for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
128                         (*i)->Execute(m_ipotime);//currentTime);
129                 }
130                 
131                 SG_Spatial* ob = (SG_Spatial*)m_pObject;
132
133                 //initialization on the first frame of the IPO
134                 if (! m_ipo_start_initialized && currentTime > 0.0) {
135                         m_ipo_start_point = ob->GetLocalPosition();
136                         m_ipo_start_orient = ob->GetLocalOrientation();
137                         m_ipo_start_scale = ob->GetLocalScale();
138                         m_ipo_start_initialized = true;
139                 }
140
141                 //modifies position?
142                 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])
143                 {
144                         if (m_ipo_as_force == true) 
145                         {
146                                 if (m_game_object && ob) 
147                                 {
148                                         m_game_object->GetPhysicsController()->ApplyForce(m_ipo_local ?
149                                                 ob->GetWorldOrientation() * m_ipo_xform.GetPosition() :
150                                                 m_ipo_xform.GetPosition(), false);
151                                 }
152                         } 
153                         else
154                         {
155                                 // Local ipo should be defined with the object position at (0,0,0)
156                                 // Local transform is applied to the object based on initial position
157                                 MT_Point3 newPosition(0.0,0.0,0.0);
158                                 
159                                 if (!m_ipo_add)
160                                         newPosition = ob->GetLocalPosition();
161                                 //apply separate IPO channels if there is any data in them
162                                 //Loc and dLoc act by themselves or are additive
163                                 //LocX and dLocX
164                                 if (m_ipo_channels_active[OB_LOC_X]) {
165                                         newPosition[0] = (m_ipo_channels_active[OB_DLOC_X] ? m_ipo_xform.GetPosition()[0] + m_ipo_xform.GetDeltaPosition()[0] : m_ipo_xform.GetPosition()[0]);
166                                 }
167                                 else if (m_ipo_channels_active[OB_DLOC_X] && m_ipo_start_initialized) {
168                                         newPosition[0] = (((!m_ipo_add)?m_ipo_start_point[0]:0.0) + m_ipo_xform.GetDeltaPosition()[0]);
169                                 }
170                                 //LocY and dLocY
171                                 if (m_ipo_channels_active[OB_LOC_Y]) {
172                                         newPosition[1] = (m_ipo_channels_active[OB_DLOC_Y] ? m_ipo_xform.GetPosition()[1] + m_ipo_xform.GetDeltaPosition()[1] : m_ipo_xform.GetPosition()[1]);
173                                 }
174                                 else if (m_ipo_channels_active[OB_DLOC_Y] && m_ipo_start_initialized) {
175                                         newPosition[1] = (((!m_ipo_add)?m_ipo_start_point[1]:0.0) + m_ipo_xform.GetDeltaPosition()[1]);
176                                 }
177                                 //LocZ and dLocZ
178                                 if (m_ipo_channels_active[OB_LOC_Z]) {
179                                         newPosition[2] = (m_ipo_channels_active[OB_DLOC_Z] ? m_ipo_xform.GetPosition()[2] + m_ipo_xform.GetDeltaPosition()[2] : m_ipo_xform.GetPosition()[2]);
180                                 }
181                                 else if (m_ipo_channels_active[OB_DLOC_Z] && m_ipo_start_initialized) {
182                                         newPosition[2] = (((!m_ipo_add)?m_ipo_start_point[2]:0.0) + m_ipo_xform.GetDeltaPosition()[2]);
183                                 }
184                                 if (m_ipo_add) {
185                                         if (m_ipo_local)
186                                                 newPosition = m_ipo_start_point + m_ipo_start_scale*(m_ipo_start_orient*newPosition);
187                                         else
188                                                 newPosition = m_ipo_start_point + newPosition;
189                                 }
190                                 ob->SetLocalPosition(newPosition);
191                         }
192                 }
193                 //modifies orientation?
194                 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]) {
195                         if (m_ipo_as_force) {
196                                 
197                                 if (m_game_object && ob) {
198                                         m_game_object->ApplyTorque(m_ipo_local ?
199                                                 ob->GetWorldOrientation() * m_ipo_xform.GetEulerAngles() :
200                                                 m_ipo_xform.GetEulerAngles(), false);
201                                 }
202                         } else {
203                                 double yaw=0, pitch=0,  roll=0; //final Euler angles
204                                 double tempYaw=0, tempPitch=0, tempRoll=0;      //temp holders
205                                 if (!m_ipo_add)
206                                         ob->GetLocalOrientation().getEuler(yaw, pitch, roll);
207
208                                 //RotX and dRotX
209                                 if (m_ipo_channels_active[OB_ROT_X]) {
210                                         yaw = (m_ipo_channels_active[OB_DROT_X] ? (m_ipo_xform.GetEulerAngles()[0] + m_ipo_xform.GetDeltaEulerAngles()[0]) : m_ipo_xform.GetEulerAngles()[0] );
211                                 }
212                                 else if (m_ipo_channels_active[OB_DROT_X] && m_ipo_start_initialized) {
213                                         if (!m_ipo_add)
214                                                 m_ipo_start_orient.getEuler(tempYaw, tempPitch, tempRoll);
215                                         yaw = tempYaw + m_ipo_xform.GetDeltaEulerAngles()[0];
216                                 }
217
218                                 //RotY dRotY
219                                 if (m_ipo_channels_active[OB_ROT_Y]) {
220                                         pitch = (m_ipo_channels_active[OB_DROT_Y] ? (m_ipo_xform.GetEulerAngles()[1] + m_ipo_xform.GetDeltaEulerAngles()[1]) : m_ipo_xform.GetEulerAngles()[1] );
221                                 }
222                                 else if (m_ipo_channels_active[OB_DROT_Y] && m_ipo_start_initialized) {
223                                         if (!m_ipo_add)
224                                                 m_ipo_start_orient.getEuler(tempYaw, tempPitch, tempRoll);
225                                         pitch = tempPitch + m_ipo_xform.GetDeltaEulerAngles()[1];
226                                 }
227                                 
228                                 //RotZ and dRotZ
229                                 if (m_ipo_channels_active[OB_ROT_Z]) {
230                                         roll = (m_ipo_channels_active[OB_DROT_Z] ? (m_ipo_xform.GetEulerAngles()[2] + m_ipo_xform.GetDeltaEulerAngles()[2]) : m_ipo_xform.GetEulerAngles()[2] );
231                                 }
232                                 else if (m_ipo_channels_active[OB_DROT_Z] && m_ipo_start_initialized) {
233                                         if (!m_ipo_add)
234                                                 m_ipo_start_orient.getEuler(tempYaw, tempPitch, tempRoll);
235                                         roll = tempRoll + m_ipo_xform.GetDeltaEulerAngles()[2];
236                                 }
237                                 if (m_ipo_add) {
238                                         MT_Matrix3x3 rotation(MT_Vector3(yaw, pitch, roll));
239                                         if (m_ipo_local)
240                                                 rotation = m_ipo_start_orient * rotation;
241                                         else
242                                                 rotation = rotation * m_ipo_start_orient;
243                                         ob->SetLocalOrientation(rotation);
244                                 } else {
245                                         ob->SetLocalOrientation(MT_Vector3(yaw, pitch, roll));
246                                 }
247                         }
248                 }
249                 //modifies scale?
250                 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]) {
251                         //default is no scale change
252                         MT_Vector3 newScale(1.0,1.0,1.0);
253                         if (!m_ipo_add)
254                                 newScale = ob->GetLocalScale();
255
256                         if (m_ipo_channels_active[OB_SIZE_X]) {
257                                 newScale[0] = (m_ipo_channels_active[OB_DSIZE_X] ? (m_ipo_xform.GetScaling()[0] + m_ipo_xform.GetDeltaScaling()[0]) : m_ipo_xform.GetScaling()[0]);
258                         }
259                         else if (m_ipo_channels_active[OB_DSIZE_X] && m_ipo_start_initialized) {
260                                 newScale[0] = (m_ipo_xform.GetDeltaScaling()[0] + ((!m_ipo_add)?m_ipo_start_scale[0]:0.0));
261                         }
262
263                         //RotY dRotY
264                         if (m_ipo_channels_active[OB_SIZE_Y]) {
265                                 newScale[1] = (m_ipo_channels_active[OB_DSIZE_Y] ? (m_ipo_xform.GetScaling()[1] + m_ipo_xform.GetDeltaScaling()[1]): m_ipo_xform.GetScaling()[1]);
266                         }
267                         else if (m_ipo_channels_active[OB_DSIZE_Y] && m_ipo_start_initialized) {
268                                 newScale[1] = (m_ipo_xform.GetDeltaScaling()[1] + ((!m_ipo_add)?m_ipo_start_scale[1]:0.0));
269                         }
270                         
271                         //RotZ and dRotZ
272                         if (m_ipo_channels_active[OB_SIZE_Z]) {
273                                 newScale[2] = (m_ipo_channels_active[OB_DSIZE_Z] ? (m_ipo_xform.GetScaling()[2] + m_ipo_xform.GetDeltaScaling()[2]) : m_ipo_xform.GetScaling()[2]);
274                         }
275                         else if (m_ipo_channels_active[OB_DSIZE_Z] && m_ipo_start_initialized) {
276                                 newScale[2] = (m_ipo_xform.GetDeltaScaling()[2] + ((!m_ipo_add)?m_ipo_start_scale[2]:1.0));
277                         }
278
279                         if (m_ipo_add) {
280                                 newScale = m_ipo_start_scale * newScale;
281                         }
282
283                         ob->SetLocalScale(newScale);
284                 }
285
286                 m_modified=false;
287         }
288         return false;
289 }
290
291
292 void KX_IpoSGController::AddInterpolator(KX_IInterpolator* interp)
293 {
294         this->m_interpolators.push_back(interp);
295 }
296
297 SG_Controller*  KX_IpoSGController::GetReplica(class SG_Node* destnode)
298 {
299         KX_IpoSGController* iporeplica = new KX_IpoSGController(*this);
300         // clear object that ipo acts on in the replica.
301         iporeplica->ClearObject();
302
303         // dirty hack, ask Gino for a better solution in the ipo implementation
304         // hacken en zagen, in what we call datahiding, not written for replication :(
305
306         T_InterpolatorList oldlist = m_interpolators;
307         iporeplica->m_interpolators.clear();
308
309         T_InterpolatorList::iterator i;
310         for (i = oldlist.begin(); !(i == oldlist.end()); ++i) {
311                 KX_ScalarInterpolator* copyipo = new KX_ScalarInterpolator(*((KX_ScalarInterpolator*)*i));
312                 iporeplica->AddInterpolator(copyipo);
313
314                 MT_Scalar* scaal = ((KX_ScalarInterpolator*)*i)->GetTarget();
315                 uint_ptr orgbase = (uint_ptr)&m_ipo_xform;
316                 uint_ptr orgloc = (uint_ptr)scaal;
317                 uint_ptr offset = orgloc-orgbase;
318                 uint_ptr newaddrbase = (uint_ptr)&iporeplica->m_ipo_xform;
319                 newaddrbase += offset;
320                 MT_Scalar* blaptr = (MT_Scalar*) newaddrbase;
321                 copyipo->SetNewTarget((MT_Scalar*)blaptr);
322         }
323         
324         return iporeplica;
325 }
326
327 KX_IpoSGController::~KX_IpoSGController()
328 {
329
330         T_InterpolatorList::iterator i;
331         for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
332                 delete (*i);
333         }
334         
335 }