svn merge -r 16351:16368 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / gameengine / Converter / KX_BlenderSceneConverter.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #ifdef WIN32
30         #pragma warning (disable:4786) // suppress stl-MSVC debug info warning
31 #endif
32
33 #include "KX_Scene.h"
34 #include "KX_GameObject.h"
35 #include "KX_BlenderSceneConverter.h"
36 #include "KX_IpoConvert.h"
37 #include "RAS_MeshObject.h"
38 #include "KX_PhysicsEngineEnums.h"
39 #include "PHY_IPhysicsEnvironment.h"
40 #include "KX_KetsjiEngine.h"
41 #include "KX_IPhysicsController.h"
42 #include "BL_Material.h"
43 #include "SYS_System.h"
44
45 #include "DummyPhysicsEnvironment.h"
46
47 //to decide to use sumo/ode or dummy physics - defines USE_ODE
48 #include "KX_ConvertPhysicsObject.h"
49
50 #ifdef USE_BULLET
51 #include "CcdPhysicsEnvironment.h"
52 #endif
53
54 #ifdef USE_ODE
55 #include "OdePhysicsEnvironment.h"
56 #endif //USE_ODE
57
58 #ifdef USE_SUMO_SOLID
59 #include "SumoPhysicsEnvironment.h"
60 #endif
61
62 #include "KX_BlenderSceneConverter.h"
63 #include "KX_BlenderScalarInterpolator.h"
64 #include "BL_BlenderDataConversion.h"
65 #include "BlenderWorldInfo.h"
66 #include "KX_Scene.h"
67
68 /* This little block needed for linking to Blender... */
69 #ifdef WIN32
70 #include "BLI_winstuff.h"
71 #endif
72
73 /* This list includes only data type definitions */
74 #include "DNA_scene_types.h"
75 #include "DNA_world_types.h"
76 #include "BKE_main.h"
77
78 #include "BLI_arithb.h"
79
80 extern "C"
81 {
82 #include "DNA_object_types.h"
83 #include "DNA_curve_types.h"
84 #include "BLI_blenlib.h"
85 #include "MEM_guardedalloc.h"
86 #include "BSE_editipo.h"
87 #include "BSE_editipo_types.h"
88 #include "DNA_ipo_types.h"
89 #include "BKE_global.h"
90 #include "DNA_space_types.h"
91 }
92
93
94 KX_BlenderSceneConverter::KX_BlenderSceneConverter(
95                                                         struct Main* maggie,
96                                                         struct SpaceIpo*        sipo,
97                                                         class KX_KetsjiEngine* engine
98                                                         )
99                                                         : m_maggie(maggie),
100                                                         m_sipo(sipo),
101                                                         m_ketsjiEngine(engine),
102                                                         m_alwaysUseExpandFraming(false),
103                                                         m_usemat(false),
104                                                         m_useglslmat(false)
105 {
106         m_newfilename = "";
107 }
108
109
110 KX_BlenderSceneConverter::~KX_BlenderSceneConverter()
111 {
112         // clears meshes, and hashmaps from blender to gameengine data
113         int i;
114         // delete sumoshapes
115         
116
117         int numipolists = m_map_blender_to_gameipolist.size();
118         for (i=0; i<numipolists; i++) {
119                 BL_InterpolatorList *ipoList= *m_map_blender_to_gameipolist.at(i);
120
121                 delete (ipoList);
122         }
123
124         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator itw = m_worldinfos.begin();
125         while (itw != m_worldinfos.end()) {
126                 delete (*itw).second;
127                 itw++;
128         }
129
130         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator itp = m_polymaterials.begin();
131         while (itp != m_polymaterials.end()) {
132                 delete (*itp).second;
133                 itp++;
134         }
135
136         // delete after RAS_IPolyMaterial
137         vector<pair<KX_Scene*,BL_Material *> >::iterator itmat = m_materials.begin();
138         while (itmat != m_materials.end()) {
139                 delete (*itmat).second;
140                 itmat++;
141         }       
142
143
144         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator itm = m_meshobjects.begin();
145         while (itm != m_meshobjects.end()) {
146                 delete (*itm).second;
147                 itm++;
148         }
149         
150 #ifdef USE_SUMO_SOLID
151         KX_ClearSumoSharedShapes();
152 #endif
153
154 #ifdef USE_BULLET
155         KX_ClearBulletSharedShapes();
156 #endif
157
158 }
159
160
161
162 void KX_BlenderSceneConverter::SetNewFileName(const STR_String& filename)
163 {
164         m_newfilename = filename;
165 }
166
167
168
169 bool KX_BlenderSceneConverter::TryAndLoadNewFile()
170 {
171         bool result = false;
172
173         // find the file
174 /*      if ()
175         {
176                 result = true;
177         }
178         // if not, clear the newfilename
179         else
180         {
181                 m_newfilename = "";     
182         }
183 */
184         return result;
185 }
186
187 Scene *KX_BlenderSceneConverter::GetBlenderSceneForName(const STR_String& name)
188 {
189         Scene *sce;
190
191         /**
192          * Find the specified scene by name, or the first
193          * scene if nothing matches (shouldn't happen).
194          */
195
196         for (sce= (Scene*) m_maggie->scene.first; sce; sce= (Scene*) sce->id.next)
197                 if (name == (sce->id.name+2))
198                         return sce;
199
200         return (Scene*)m_maggie->scene.first;
201
202 }
203 #include "KX_PythonInit.h"
204
205 #ifdef USE_BULLET
206
207 #include "LinearMath/btIDebugDraw.h"
208
209
210 struct  BlenderDebugDraw : public btIDebugDraw
211 {
212         BlenderDebugDraw () :
213                 m_debugMode(0) 
214         {
215         }
216         
217         int m_debugMode;
218
219         virtual void    drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
220         {
221                 if (m_debugMode >0)
222                 {
223                         MT_Vector3 kxfrom(from[0],from[1],from[2]);
224                         MT_Vector3 kxto(to[0],to[1],to[2]);
225                         MT_Vector3 kxcolor(color[0],color[1],color[2]);
226
227                         KX_RasterizerDrawDebugLine(kxfrom,kxto,kxcolor);
228                 }
229         }
230         
231         virtual void    reportErrorWarning(const char* warningString)
232         {
233
234         }
235
236         virtual void    drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,float distance,int lifeTime,const btVector3& color)
237         {
238                 //not yet
239         }
240
241         virtual void    setDebugMode(int debugMode)
242         {
243                 m_debugMode = debugMode;
244         }
245         virtual int             getDebugMode() const
246         {
247                 return m_debugMode;
248         }
249         ///todo: find out if Blender can do this
250         virtual void    draw3dText(const btVector3& location,const char* textString)
251         {
252
253         }
254                 
255 };
256
257 #endif
258
259 void KX_BlenderSceneConverter::ConvertScene(const STR_String& scenename,
260                                                                                         class KX_Scene* destinationscene,
261                                                                                         PyObject* dictobj,
262                                                                                         class SCA_IInputDevice* keyinputdev,
263                                                                                         class RAS_IRenderTools* rendertools,
264                                                                                         class RAS_ICanvas* canvas)
265 {
266         //find out which physics engine
267         Scene *blenderscene = GetBlenderSceneForName(scenename);
268
269         e_PhysicsEngine physics_engine = UseBullet;
270         // hook for registration function during conversion.
271         m_currentScene = destinationscene;
272         destinationscene->SetSceneConverter(this);
273
274         if (blenderscene)
275         {
276         
277                 if (blenderscene->world)
278                 {
279                         switch (blenderscene->world->physicsEngine)
280                         {
281                         case WOPHY_BULLET:
282                                 {
283                                         physics_engine = UseBullet;
284                                         break;
285                                 }
286                                 
287                                 case WOPHY_ODE:
288                                 {
289                                         physics_engine = UseODE;
290                                         break;
291                                 }
292                                 case WOPHY_DYNAMO:
293                                 {
294                                         physics_engine = UseDynamo;
295                                         break;
296                                 }
297                                 case WOPHY_SUMO:
298                                 {
299                                         physics_engine = UseSumo; 
300                                         break;
301                                 }
302                                 case WOPHY_NONE:
303                                 {
304                                         physics_engine = UseNone;
305                                 }
306                         }
307                   
308                 }
309         }
310
311         switch (physics_engine)
312         {
313 #ifdef USE_BULLET
314                 case UseBullet:
315                         {
316                                 CcdPhysicsEnvironment* ccdPhysEnv = new CcdPhysicsEnvironment();
317                                 ccdPhysEnv->setDebugDrawer(new BlenderDebugDraw());
318                                 ccdPhysEnv->setDeactivationLinearTreshold(0.8f); // default, can be overridden by Python
319                                 ccdPhysEnv->setDeactivationAngularTreshold(1.0f); // default, can be overridden by Python
320
321                                 SYS_SystemHandle syshandle = SYS_GetSystem(); /*unused*/
322                                 int visualizePhysics = SYS_GetCommandLineInt(syshandle,"show_physics",0);
323                                 if (visualizePhysics)
324                                         ccdPhysEnv->setDebugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb|btIDebugDraw::DBG_DrawContactPoints|btIDebugDraw::DBG_DrawText);
325                 
326                                 //todo: get a button in blender ?
327                                 //disable / enable debug drawing (contact points, aabb's etc)   
328                                 //ccdPhysEnv->setDebugMode(1);
329                                 destinationscene->SetPhysicsEnvironment(ccdPhysEnv);
330                                 break;
331                         }
332 #endif
333
334 #ifdef USE_SUMO_SOLID
335                 case UseSumo:
336                         destinationscene ->SetPhysicsEnvironment(new SumoPhysicsEnvironment());
337                         break;
338 #endif
339 #ifdef USE_ODE
340
341                 case UseODE:
342                         destinationscene ->SetPhysicsEnvironment(new ODEPhysicsEnvironment());
343                         break;
344 #endif //USE_ODE
345         
346                 case UseDynamo:
347                 {
348                 }
349                 
350                 default:
351                 case UseNone:
352                         physics_engine = UseNone;
353                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
354                         break;
355         }
356
357         BL_ConvertBlenderObjects(m_maggie,
358                 scenename,
359                 destinationscene,
360                 m_ketsjiEngine,
361                 physics_engine,
362                 dictobj,
363                 keyinputdev,
364                 rendertools,
365                 canvas,
366                 this,
367                 m_alwaysUseExpandFraming
368                 );
369
370         //These lookup are not needed during game
371         m_map_blender_to_gameactuator.clear();
372         m_map_blender_to_gamecontroller.clear();
373         m_map_blender_to_gameobject.clear();
374
375         //Clearing this lookup table has the effect of disabling the cache of meshes
376         //between scenes, even if they are shared in the blend file.
377         //This cache mecanism is buggy so I leave it disable and the memory leak
378         //that would result from this is fixed in RemoveScene()
379         m_map_mesh_to_gamemesh.clear();
380         //Don't clear this lookup, it is needed for the baking physics into ipo animation
381         //To avoid it's infinite grows, object will be unregister when they are deleted 
382         //see KX_Scene::NewRemoveObject
383         //m_map_gameobject_to_blender.clear();
384 }
385
386 // This function removes all entities stored in the converter for that scene
387 // It should be used instead of direct delete scene
388 // Note that there was some provision for sharing entities (meshes...) between
389 // scenes but that is now disabled so all scene will have their own copy
390 // and we can delete them here. If the sharing is reactivated, change this code too..
391 // (see KX_BlenderSceneConverter::ConvertScene)
392 void KX_BlenderSceneConverter::RemoveScene(KX_Scene *scene)
393 {
394         int i, size;
395         // delete the scene first as it will stop the use of entities
396         delete scene;
397         // delete the entities of this scene
398         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator worldit;
399         size = m_worldinfos.size();
400         for (i=0, worldit=m_worldinfos.begin(); i<size; ) {
401                 if ((*worldit).first == scene) {
402                         delete (*worldit).second;
403                         *worldit = m_worldinfos.back();
404                         m_worldinfos.pop_back();
405                         size--;
406                 } else {
407                         i++;
408                         worldit++;
409                 }
410         }
411
412         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator polymit;
413         size = m_polymaterials.size();
414         for (i=0, polymit=m_polymaterials.begin(); i<size; ) {
415                 if ((*polymit).first == scene) {
416                         delete (*polymit).second;
417                         *polymit = m_polymaterials.back();
418                         m_polymaterials.pop_back();
419                         size--;
420                 } else {
421                         i++;
422                         polymit++;
423                 }
424         }
425
426         vector<pair<KX_Scene*,BL_Material*> >::iterator matit;
427         size = m_materials.size();
428         for (i=0, matit=m_materials.begin(); i<size; ) {
429                 if ((*matit).first == scene) {
430                         delete (*matit).second;
431                         *matit = m_materials.back();
432                         m_materials.pop_back();
433                         size--;
434                 } else {
435                         i++;
436                         matit++;
437                 }
438         }
439
440         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator meshit;
441         size = m_meshobjects.size();
442         for (i=0, meshit=m_meshobjects.begin(); i<size; ) {
443                 if ((*meshit).first == scene) {
444                         delete (*meshit).second;
445                         *meshit = m_meshobjects.back();
446                         m_meshobjects.pop_back();
447                         size--;
448                 } else {
449                         i++;
450                         meshit++;
451                 }
452         }
453 }
454
455 // use blender materials
456 void KX_BlenderSceneConverter::SetMaterials(bool val)
457 {
458         m_usemat = val;
459         m_useglslmat = false;
460 }
461
462 void KX_BlenderSceneConverter::SetGLSLMaterials(bool val)
463 {
464         m_usemat = val;
465         m_useglslmat = val;
466 }
467
468 bool KX_BlenderSceneConverter::GetMaterials()
469 {
470         return m_usemat;
471 }
472
473 bool KX_BlenderSceneConverter::GetGLSLMaterials()
474 {
475         return m_useglslmat;
476 }
477
478 void KX_BlenderSceneConverter::RegisterBlenderMaterial(BL_Material *mat)
479 {
480         m_materials.push_back(pair<KX_Scene*,BL_Material *>(m_currentScene,mat));
481 }
482
483
484
485 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
486         bool to_what)
487 {
488         m_alwaysUseExpandFraming= to_what;
489 }
490
491         
492
493 void KX_BlenderSceneConverter::RegisterGameObject(
494                                                                         KX_GameObject *gameobject, 
495                                                                         struct Object *for_blenderobject) 
496 {
497         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
498         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
499 }
500
501 void KX_BlenderSceneConverter::UnregisterGameObject(
502                                                                         KX_GameObject *gameobject) 
503 {
504         m_map_gameobject_to_blender.remove(CHashedPtr(gameobject));
505 }
506
507
508 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
509                                                                         struct Object *for_blenderobject) 
510 {
511         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
512         
513         return obp?*obp:NULL;
514 }
515
516
517
518 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
519                                                                         KX_GameObject *for_gameobject) 
520 {
521         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
522         
523         return obp?*obp:NULL;
524 }
525
526         
527
528 void KX_BlenderSceneConverter::RegisterGameMesh(
529                                                                         RAS_MeshObject *gamemesh,
530                                                                         struct Mesh *for_blendermesh)
531 {
532         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
533         m_meshobjects.push_back(pair<KX_Scene*,RAS_MeshObject*>(m_currentScene,gamemesh));
534 }
535
536
537
538 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
539                                                                         struct Mesh *for_blendermesh,
540                                                                         unsigned int onlayer)
541 {
542         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
543         
544         if (meshp && onlayer==(*meshp)->GetLightLayer()) {
545                 return *meshp;
546         } else {
547                 return NULL;
548         }
549 }
550
551         
552
553
554         
555
556 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
557 {
558         m_polymaterials.push_back(pair<KX_Scene*,RAS_IPolyMaterial*>(m_currentScene,polymat));
559 }
560
561
562
563 void KX_BlenderSceneConverter::RegisterInterpolatorList(
564                                                                         BL_InterpolatorList *ipoList,
565                                                                         struct Ipo *for_ipo)
566 {
567         m_map_blender_to_gameipolist.insert(CHashedPtr(for_ipo), ipoList);
568 }
569
570
571
572 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
573                                                                         struct Ipo *for_ipo)
574 {
575         BL_InterpolatorList **listp = m_map_blender_to_gameipolist[CHashedPtr(for_ipo)];
576                 
577         return listp?*listp:NULL;
578 }
579
580
581
582 void KX_BlenderSceneConverter::RegisterGameActuator(
583                                                                         SCA_IActuator *act,
584                                                                         struct bActuator *for_actuator)
585 {
586         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
587 }
588
589
590
591 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
592                                                                         struct bActuator *for_actuator)
593 {
594         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
595         
596         return actp?*actp:NULL;
597 }
598
599
600
601 void KX_BlenderSceneConverter::RegisterGameController(
602                                                                         SCA_IController *cont,
603                                                                         struct bController *for_controller)
604 {
605         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
606 }
607
608
609
610 SCA_IController *KX_BlenderSceneConverter::FindGameController(
611                                                                         struct bController *for_controller)
612 {
613         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
614         
615         return contp?*contp:NULL;
616 }
617
618
619
620 void KX_BlenderSceneConverter::RegisterWorldInfo(
621                                                                         KX_WorldInfo *worldinfo)
622 {
623         m_worldinfos.push_back(pair<KX_Scene*,KX_WorldInfo*>(m_currentScene,worldinfo));
624 }
625
626 /*
627  * When deleting an IPO curve from Python, check if the IPO is being
628  * edited and if so clear the pointer to the old curve.
629  */
630 void KX_BlenderSceneConverter::localDel_ipoCurve ( IpoCurve * icu ,struct SpaceIpo*     sipo)
631 {
632         if (!sipo)
633                 return;
634
635         int i;
636         EditIpo *ei= (EditIpo *)sipo->editipo;
637         if (!ei) return;
638
639         for(i=0; i<G.sipo->totipo; i++, ei++) {
640                 if ( ei->icu == icu ) {
641                         ei->flag &= ~(IPO_SELECT | IPO_EDIT);
642                         ei->icu= 0;
643                         return;
644                 }
645         }
646 }
647
648 //quick hack
649 extern "C"
650 {
651         Ipo *add_ipo( char *name, int idcode );
652         char *getIpoCurveName( IpoCurve * icu );
653         struct IpoCurve *verify_ipocurve(struct ID *, short, char *, char *, char *, int);
654         void testhandles_ipocurve(struct IpoCurve *icu);
655         void Mat3ToEul(float tmat[][3], float *eul);
656
657 }
658
659 IpoCurve* findIpoCurve(IpoCurve* first,char* searchName)
660 {
661         IpoCurve* icu1;
662         for( icu1 = first; icu1; icu1 = icu1->next ) 
663         {
664                 char* curveName = getIpoCurveName( icu1 );
665                 if( !strcmp( curveName, searchName) )
666                 {
667                         return icu1;
668                 }
669         }
670         return 0;
671 }
672
673 // this is not longer necesary //rcruiz
674 /*Ipo* KX_BlenderSceneConverter::findIpoForName(char* objName)
675 {
676         Ipo* ipo_iter = (Ipo*)m_maggie->ipo.first;
677
678         while( ipo_iter )
679         {
680                 if( strcmp( objName, ipo_iter->id.name + 2 ) == 0 ) 
681                 {
682                         return ipo_iter;
683                 }
684                 ipo_iter = (Ipo*)ipo_iter->id.next;
685         }
686         return 0;
687 }
688 */
689
690 void    KX_BlenderSceneConverter::ResetPhysicsObjectsAnimationIpo(bool clearIpo)
691 {
692
693         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
694         int numScenes = scenes->size();
695         int i;
696         for (i=0;i<numScenes;i++)
697         {
698                 KX_Scene* scene = scenes->at(i);
699                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
700                 CListValue* parentList = scene->GetRootParentList();
701                 int numObjects = parentList->GetCount();
702                 int g;
703                 for (g=0;g<numObjects;g++)
704                 {
705                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
706                         if (gameObj->IsDynamic())
707                         {
708                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
709                                 
710                                 Object* blenderObject = FindBlenderObject(gameObj);
711                                 if (blenderObject)
712                                 {
713                                         //erase existing ipo's
714                                         Ipo* ipo = blenderObject->ipo;//findIpoForName(blenderObject->id.name+2);
715                                         if (ipo)
716                                         {       //clear the curve data
717                                                 if (clearIpo){//rcruiz
718                                                         IpoCurve *icu1;
719                                                                                                                 
720                                                         int numCurves = 0;
721                                                         for( icu1 = (IpoCurve*)ipo->curve.first; icu1;  ) {
722                                                         
723                                                                 IpoCurve* tmpicu = icu1;
724                                                                 
725                                                                 /*int i;
726                                                                 BezTriple *bezt;
727                                                                 for( bezt = tmpicu->bezt, i = 0;        i < tmpicu->totvert; i++, bezt++){
728                                                                         printf("(%f,%f,%f),(%f,%f,%f),(%f,%f,%f)\n",bezt->vec[0][0],bezt->vec[0][1],bezt->vec[0][2],bezt->vec[1][0],bezt->vec[1][1],bezt->vec[1][2],bezt->vec[2][0],bezt->vec[2][1],bezt->vec[2][2]);
729                                                                 }*/
730                                                                 
731                                                                 icu1 = icu1->next;
732                                                                 numCurves++;
733                         
734                                                                 BLI_remlink( &( blenderObject->ipo->curve ), tmpicu );
735                                                                 if( tmpicu->bezt )
736                                                                         MEM_freeN( tmpicu->bezt );
737                                                                 MEM_freeN( tmpicu );
738                                                                 localDel_ipoCurve( tmpicu ,m_sipo);
739                                                         }
740                                                 }
741                                         } else
742                                         {       ipo = add_ipo(blenderObject->id.name+2, ID_OB);
743                                                 blenderObject->ipo = ipo;
744
745                                         }
746                                 
747                                         
748
749                                         
750
751                                 }
752                         }
753
754                 }
755                 
756         
757         }
758
759
760
761 }
762
763 void    KX_BlenderSceneConverter::resetNoneDynamicObjectToIpo(){
764         
765         if (addInitFromFrame){          
766                 KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
767                 int numScenes = scenes->size();
768                 if (numScenes>=0){
769                         KX_Scene* scene = scenes->at(0);
770                         CListValue* parentList = scene->GetRootParentList();
771                         for (int ix=0;ix<parentList->GetCount();ix++){
772                                 KX_GameObject* gameobj = (KX_GameObject*)parentList->GetValue(ix);
773                                 if (!gameobj->IsDynamic()){
774                                         Object* blenderobject = FindBlenderObject(gameobj);
775                                         if (!blenderobject)
776                                                 continue;
777                                         if (blenderobject->type==OB_ARMATURE)
778                                                 continue;
779                                         float eu[3];
780                                         Mat4ToEul(blenderobject->obmat,eu);                                     
781                                         MT_Point3 pos = MT_Point3(
782                                                 blenderobject->obmat[3][0],
783                                                 blenderobject->obmat[3][1],
784                                                 blenderobject->obmat[3][2]
785                                         );
786                                         MT_Vector3 eulxyz = MT_Vector3(
787                                                 eu[0],
788                                                 eu[1],
789                                                 eu[2]
790                                         );
791                                         MT_Vector3 scale = MT_Vector3(
792                                                 blenderobject->size[0],
793                                                 blenderobject->size[1],
794                                                 blenderobject->size[2]
795                                         );
796                                         gameobj->NodeSetLocalPosition(pos);
797                                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
798                                         gameobj->NodeSetLocalScale(scale);
799                                         gameobj->NodeUpdateGS(0,true);
800                                 }
801                         }
802                 }
803         }
804 }
805
806 #define TEST_HANDLES_GAME2IPO 0
807
808         ///this generates ipo curves for position, rotation, allowing to use game physics in animation
809 void    KX_BlenderSceneConverter::WritePhysicsObjectToAnimationIpo(int frameNumber)
810 {
811
812         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
813         int numScenes = scenes->size();
814         int i;
815         for (i=0;i<numScenes;i++)
816         {
817                 KX_Scene* scene = scenes->at(i);
818                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
819                 CListValue* parentList = scene->GetRootParentList();
820                 int numObjects = parentList->GetCount();
821                 int g;
822                 for (g=0;g<numObjects;g++)
823                 {
824                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
825                         if (gameObj->IsDynamic())
826                         {
827                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
828                                 
829                                 Object* blenderObject = FindBlenderObject(gameObj);
830                                 if (blenderObject)
831                                 {
832
833                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
834                                         float eulerAngles[3];   
835                                         float tmat[3][3];
836                                         for (int r=0;r<3;r++)
837                                         {
838                                                 for (int c=0;c<3;c++)
839                                                 {
840                                                         tmat[r][c] = orn[c][r];
841                                                 }
842                                         }
843                                         Mat3ToEul(tmat, eulerAngles);
844                                         
845                                         for(int x = 0; x < 3; x++) {
846                                                 eulerAngles[x] *= (float) (180 / 3.14159265f);
847                                         }
848
849                                         eulerAngles[0]/=10.f;
850                                         eulerAngles[1]/=10.f;
851                                         eulerAngles[2]/=10.f;
852
853
854
855                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
856                                         const MT_Point3& position = gameObj->NodeGetWorldPosition();
857                                         
858                                         Ipo* ipo = blenderObject->ipo;
859                                         if (ipo)
860                                         {
861
862                                                 //create the curves, if not existing
863
864                                         IpoCurve *icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
865                                         if (!icu1)
866                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X);
867                                         
868                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
869                                         if (!icu1)
870                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y);
871                                         
872                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
873                                         if (!icu1)
874                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z);
875
876                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
877                                         if (!icu1)
878                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X);
879
880                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
881                                         if (!icu1)
882                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y);
883
884                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
885                                         if (!icu1)
886                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z);
887
888
889
890                                         //fill the curves with data
891
892                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
893                                                 if (icu1)
894                                                 {
895                                                         float curVal = position.x();
896                                                         insert_vert_icu(icu1, frameNumber, curVal, 0);
897 #ifdef TEST_HANDLES_GAME2IPO
898                                                         testhandles_ipocurve(icu1);
899 #endif
900                                                 }
901                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
902                                                 if (icu1)
903                                                 {
904                                                         float curVal = position.y();
905                                                         insert_vert_icu(icu1, frameNumber, curVal, 0);
906 #ifdef TEST_HANDLES_GAME2IPO
907
908                                                         testhandles_ipocurve(icu1);
909 #endif
910                                                 }
911                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
912                                                 if (icu1)
913                                                 {
914                                                         float curVal = position.z();
915                                                         insert_vert_icu(icu1, frameNumber, curVal, 0);
916 #ifdef TEST_HANDLES_GAME2IPO
917                                                         testhandles_ipocurve(icu1);
918 #endif
919                                                 }
920                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
921                                                 if (icu1)
922                                                 {
923                                                         float curVal = eulerAngles[0];
924                                                         insert_vert_icu(icu1, frameNumber, curVal, 0);
925 #ifdef TEST_HANDLES_GAME2IPO
926
927                                                         testhandles_ipocurve(icu1);
928 #endif
929                                                 }
930                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
931                                                 if (icu1)
932                                                 {
933                                                         float curVal = eulerAngles[1];
934                                                         insert_vert_icu(icu1, frameNumber, curVal, 0);
935 #ifdef TEST_HANDLES_GAME2IPO
936
937                                                         testhandles_ipocurve(icu1);
938 #endif
939                                                 }
940                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
941                                                 if (icu1)
942                                                 {
943                                                         float curVal = eulerAngles[2];
944                                                         insert_vert_icu(icu1, frameNumber, curVal, 0);
945 #ifdef TEST_HANDLES_GAME2IPO
946                                                         
947                                                         testhandles_ipocurve(icu1);
948 #endif
949
950                                                 }
951
952                                         }
953                                 }
954                         }
955
956                 }
957                 
958         
959         }       
960         
961
962 }
963
964
965 void    KX_BlenderSceneConverter::TestHandlesPhysicsObjectToAnimationIpo()
966 {
967
968         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
969         int numScenes = scenes->size();
970         int i;
971         for (i=0;i<numScenes;i++)
972         {
973                 KX_Scene* scene = scenes->at(i);
974                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
975                 CListValue* parentList = scene->GetRootParentList();
976                 int numObjects = parentList->GetCount();
977                 int g;
978                 for (g=0;g<numObjects;g++)
979                 {
980                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
981                         if (gameObj->IsDynamic())
982                         {
983                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
984                                 
985                                 Object* blenderObject = FindBlenderObject(gameObj);
986                                 if (blenderObject)
987                                 {
988
989                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
990                                         float eulerAngles[3];   
991                                         float tmat[3][3];
992                                         for (int r=0;r<3;r++)
993                                         {
994                                                 for (int c=0;c<3;c++)
995                                                 {
996                                                         tmat[r][c] = orn[c][r];
997                                                 }
998                                         }
999                                         Mat3ToEul(tmat, eulerAngles);
1000                                         
1001                                         for(int x = 0; x < 3; x++) {
1002                                                 eulerAngles[x] *= (float) (180 / 3.14159265f);
1003                                         }
1004
1005                                         eulerAngles[0]/=10.f;
1006                                         eulerAngles[1]/=10.f;
1007                                         eulerAngles[2]/=10.f;
1008
1009
1010
1011                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
1012                                         //const MT_Point3& position = gameObj->NodeGetWorldPosition();
1013                                         
1014                                         Ipo* ipo = blenderObject->ipo;
1015                                         if (ipo)
1016                                         {
1017
1018                                                 //create the curves, if not existing
1019
1020                                         IpoCurve *icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
1021                                         if (!icu1)
1022                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X);
1023                                         
1024                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
1025                                         if (!icu1)
1026                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y);
1027                                         
1028                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
1029                                         if (!icu1)
1030                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z);
1031
1032                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
1033                                         if (!icu1)
1034                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X);
1035
1036                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
1037                                         if (!icu1)
1038                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y);
1039
1040                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
1041                                         if (!icu1)
1042                                                 icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z);
1043
1044
1045
1046                                         //fill the curves with data
1047
1048                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
1049                                                 if (icu1)
1050                                                 {
1051                                                         testhandles_ipocurve(icu1);
1052                                                 }
1053                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
1054                                                 if (icu1)
1055                                                 {
1056                                                         testhandles_ipocurve(icu1);
1057                                                 }
1058                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
1059                                                 if (icu1)
1060                                                 {
1061                                                         testhandles_ipocurve(icu1);
1062                                                 }
1063                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
1064                                                 if (icu1)
1065                                                 {
1066                                                         testhandles_ipocurve(icu1);
1067                                                 }
1068                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
1069                                                 if (icu1)
1070                                                 {
1071                                                         testhandles_ipocurve(icu1);
1072                                                 }
1073                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
1074                                                 if (icu1)
1075                                                 {
1076                                                         testhandles_ipocurve(icu1);
1077                                                 }
1078
1079                                         }
1080                                 }
1081                         }
1082
1083                 }
1084                 
1085         
1086         }
1087
1088
1089
1090 }