2.5
[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 //XXX #include "BSE_editipo.h"
87 //XXX #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         CHashedPtr gptr(gameobject);
505         struct Object **bobp= m_map_gameobject_to_blender[gptr];
506         if (bobp) {
507                 CHashedPtr bptr(*bobp);
508                 KX_GameObject **gobp= m_map_blender_to_gameobject[bptr];
509                 if (gobp && *gobp == gameobject)
510                         // also maintain m_map_blender_to_gameobject if the gameobject
511                         // being removed is matching the blender object
512                         m_map_blender_to_gameobject.remove(bptr);
513                 m_map_gameobject_to_blender.remove(gptr);
514         }
515 }
516
517
518 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
519                                                                         struct Object *for_blenderobject) 
520 {
521         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
522         
523         return obp?*obp:NULL;
524 }
525
526
527
528 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
529                                                                         KX_GameObject *for_gameobject) 
530 {
531         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
532         
533         return obp?*obp:NULL;
534 }
535
536         
537
538 void KX_BlenderSceneConverter::RegisterGameMesh(
539                                                                         RAS_MeshObject *gamemesh,
540                                                                         struct Mesh *for_blendermesh)
541 {
542         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
543         m_meshobjects.push_back(pair<KX_Scene*,RAS_MeshObject*>(m_currentScene,gamemesh));
544 }
545
546
547
548 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
549                                                                         struct Mesh *for_blendermesh,
550                                                                         unsigned int onlayer)
551 {
552         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
553         
554         if (meshp && onlayer==(*meshp)->GetLightLayer()) {
555                 return *meshp;
556         } else {
557                 return NULL;
558         }
559 }
560
561         
562
563
564         
565
566 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
567 {
568         m_polymaterials.push_back(pair<KX_Scene*,RAS_IPolyMaterial*>(m_currentScene,polymat));
569 }
570
571
572
573 void KX_BlenderSceneConverter::RegisterInterpolatorList(
574                                                                         BL_InterpolatorList *ipoList,
575                                                                         struct Ipo *for_ipo)
576 {
577         m_map_blender_to_gameipolist.insert(CHashedPtr(for_ipo), ipoList);
578 }
579
580
581
582 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
583                                                                         struct Ipo *for_ipo)
584 {
585         BL_InterpolatorList **listp = m_map_blender_to_gameipolist[CHashedPtr(for_ipo)];
586                 
587         return listp?*listp:NULL;
588 }
589
590
591
592 void KX_BlenderSceneConverter::RegisterGameActuator(
593                                                                         SCA_IActuator *act,
594                                                                         struct bActuator *for_actuator)
595 {
596         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
597 }
598
599
600
601 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
602                                                                         struct bActuator *for_actuator)
603 {
604         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
605         
606         return actp?*actp:NULL;
607 }
608
609
610
611 void KX_BlenderSceneConverter::RegisterGameController(
612                                                                         SCA_IController *cont,
613                                                                         struct bController *for_controller)
614 {
615         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
616 }
617
618
619
620 SCA_IController *KX_BlenderSceneConverter::FindGameController(
621                                                                         struct bController *for_controller)
622 {
623         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
624         
625         return contp?*contp:NULL;
626 }
627
628
629
630 void KX_BlenderSceneConverter::RegisterWorldInfo(
631                                                                         KX_WorldInfo *worldinfo)
632 {
633         m_worldinfos.push_back(pair<KX_Scene*,KX_WorldInfo*>(m_currentScene,worldinfo));
634 }
635
636 /*
637  * When deleting an IPO curve from Python, check if the IPO is being
638  * edited and if so clear the pointer to the old curve.
639  */
640 void KX_BlenderSceneConverter::localDel_ipoCurve ( IpoCurve * icu ,struct SpaceIpo*     sipo)
641 {
642         if (!sipo)
643                 return;
644
645         int i;
646 #if 0 //XXX
647         EditIpo *ei= (EditIpo *)sipo->editipo;
648         if (!ei) return;
649
650         for(i=0; i<G.sipo->totipo; i++, ei++) {
651                 if ( ei->icu == icu ) {
652                         ei->flag &= ~(IPO_SELECT | IPO_EDIT);
653                         ei->icu= 0;
654                         return;
655                 }
656         }
657 #endif
658 }
659
660 //quick hack
661 extern "C"
662 {
663         Ipo *add_ipo( char *name, int idcode );
664         //XXX char *getIpoCurveName( IpoCurve * icu );
665         //XXX struct IpoCurve *verify_ipocurve(struct ID *, short, char *, char *, char *, int);
666         void testhandles_ipocurve(struct IpoCurve *icu);
667         void insert_vert_icu(struct IpoCurve *, float, float, short);
668         void Mat3ToEul(float tmat[][3], float *eul);
669 }
670
671 IpoCurve* findIpoCurve(IpoCurve* first, const char* searchName)
672 {
673         IpoCurve* icu1;
674         for( icu1 = first; icu1; icu1 = icu1->next ) 
675         {
676                 /*XXX char* curveName = getIpoCurveName( icu1 );
677                 if( !strcmp( curveName, searchName) )
678                 {
679                         return icu1;
680                 }*/
681         }
682         return 0;
683 }
684
685 // this is not longer necesary //rcruiz
686 /*Ipo* KX_BlenderSceneConverter::findIpoForName(char* objName)
687 {
688         Ipo* ipo_iter = (Ipo*)m_maggie->ipo.first;
689
690         while( ipo_iter )
691         {
692                 if( strcmp( objName, ipo_iter->id.name + 2 ) == 0 ) 
693                 {
694                         return ipo_iter;
695                 }
696                 ipo_iter = (Ipo*)ipo_iter->id.next;
697         }
698         return 0;
699 }
700 */
701
702 void    KX_BlenderSceneConverter::ResetPhysicsObjectsAnimationIpo(bool clearIpo)
703 {
704
705         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
706         int numScenes = scenes->size();
707         int i;
708         for (i=0;i<numScenes;i++)
709         {
710                 KX_Scene* scene = scenes->at(i);
711                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
712                 CListValue* parentList = scene->GetRootParentList();
713                 int numObjects = parentList->GetCount();
714                 int g;
715                 for (g=0;g<numObjects;g++)
716                 {
717                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
718                         if (gameObj->IsDynamic())
719                         {
720                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
721                                 
722                                 Object* blenderObject = FindBlenderObject(gameObj);
723                                 if (blenderObject)
724                                 {
725                                         //erase existing ipo's
726                                         Ipo* ipo = blenderObject->ipo;//findIpoForName(blenderObject->id.name+2);
727                                         if (ipo)
728                                         {       //clear the curve data
729                                                 if (clearIpo){//rcruiz
730                                                         IpoCurve *icu1;
731                                                                                                                 
732                                                         int numCurves = 0;
733                                                         for( icu1 = (IpoCurve*)ipo->curve.first; icu1;  ) {
734                                                         
735                                                                 IpoCurve* tmpicu = icu1;
736                                                                 
737                                                                 /*int i;
738                                                                 BezTriple *bezt;
739                                                                 for( bezt = tmpicu->bezt, i = 0;        i < tmpicu->totvert; i++, bezt++){
740                                                                         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]);
741                                                                 }*/
742                                                                 
743                                                                 icu1 = icu1->next;
744                                                                 numCurves++;
745                         
746                                                                 BLI_remlink( &( blenderObject->ipo->curve ), tmpicu );
747                                                                 if( tmpicu->bezt )
748                                                                         MEM_freeN( tmpicu->bezt );
749                                                                 MEM_freeN( tmpicu );
750                                                                 localDel_ipoCurve( tmpicu ,m_sipo);
751                                                         }
752                                                 }
753                                         } else
754                                         {       ipo = add_ipo(blenderObject->id.name+2, ID_OB);
755                                                 blenderObject->ipo = ipo;
756
757                                         }
758                                 
759                                         
760
761                                         
762
763                                 }
764                         }
765
766                 }
767                 
768         
769         }
770
771
772
773 }
774
775 void    KX_BlenderSceneConverter::resetNoneDynamicObjectToIpo(){
776         
777         if (addInitFromFrame){          
778                 KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
779                 int numScenes = scenes->size();
780                 if (numScenes>=0){
781                         KX_Scene* scene = scenes->at(0);
782                         CListValue* parentList = scene->GetRootParentList();
783                         for (int ix=0;ix<parentList->GetCount();ix++){
784                                 KX_GameObject* gameobj = (KX_GameObject*)parentList->GetValue(ix);
785                                 if (!gameobj->IsDynamic()){
786                                         Object* blenderobject = FindBlenderObject(gameobj);
787                                         if (!blenderobject)
788                                                 continue;
789                                         if (blenderobject->type==OB_ARMATURE)
790                                                 continue;
791                                         float eu[3];
792                                         Mat4ToEul(blenderobject->obmat,eu);                                     
793                                         MT_Point3 pos = MT_Point3(
794                                                 blenderobject->obmat[3][0],
795                                                 blenderobject->obmat[3][1],
796                                                 blenderobject->obmat[3][2]
797                                         );
798                                         MT_Vector3 eulxyz = MT_Vector3(
799                                                 eu[0],
800                                                 eu[1],
801                                                 eu[2]
802                                         );
803                                         MT_Vector3 scale = MT_Vector3(
804                                                 blenderobject->size[0],
805                                                 blenderobject->size[1],
806                                                 blenderobject->size[2]
807                                         );
808                                         gameobj->NodeSetLocalPosition(pos);
809                                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
810                                         gameobj->NodeSetLocalScale(scale);
811                                         gameobj->NodeUpdateGS(0,true);
812                                 }
813                         }
814                 }
815         }
816 }
817
818 #define TEST_HANDLES_GAME2IPO 0
819
820         ///this generates ipo curves for position, rotation, allowing to use game physics in animation
821 void    KX_BlenderSceneConverter::WritePhysicsObjectToAnimationIpo(int frameNumber)
822 {
823
824         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
825         int numScenes = scenes->size();
826         int i;
827         for (i=0;i<numScenes;i++)
828         {
829                 KX_Scene* scene = scenes->at(i);
830                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
831                 CListValue* parentList = scene->GetRootParentList();
832                 int numObjects = parentList->GetCount();
833                 int g;
834                 for (g=0;g<numObjects;g++)
835                 {
836                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
837                         if (gameObj->IsDynamic())
838                         {
839                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
840                                 
841                                 Object* blenderObject = FindBlenderObject(gameObj);
842                                 if (blenderObject)
843                                 {
844
845                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
846                                         float eulerAngles[3];   
847                                         float tmat[3][3];
848                                         for (int r=0;r<3;r++)
849                                         {
850                                                 for (int c=0;c<3;c++)
851                                                 {
852                                                         tmat[r][c] = orn[c][r];
853                                                 }
854                                         }
855                                         Mat3ToEul(tmat, eulerAngles);
856                                         
857                                         for(int x = 0; x < 3; x++) {
858                                                 eulerAngles[x] *= (float) (180 / 3.14159265f);
859                                         }
860
861                                         eulerAngles[0]/=10.f;
862                                         eulerAngles[1]/=10.f;
863                                         eulerAngles[2]/=10.f;
864
865
866
867                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
868                                         const MT_Point3& position = gameObj->NodeGetWorldPosition();
869                                         
870                                         Ipo* ipo = blenderObject->ipo;
871                                         if (ipo)
872                                         {
873
874                                                 //create the curves, if not existing
875
876                                         IpoCurve *icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
877                                         //XXX if (!icu1)
878                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X);
879                                         
880                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
881                                         //XXX if (!icu1)
882                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y);
883                                         
884                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
885                                         //XXX if (!icu1)
886                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z);
887
888                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
889                                         //XXX if (!icu1)
890                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X);
891
892                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
893                                         //XXX if (!icu1)
894                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y);
895
896                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
897                                         //XXX if (!icu1)
898                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z);
899
900
901
902                                         //fill the curves with data
903
904                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
905                                                 if (icu1)
906                                                 {
907                                                         float curVal = position.x();
908                                                         //XXX insert_vert_icu(icu1, frameNumber, curVal, 0);
909 #ifdef TEST_HANDLES_GAME2IPO
910                                                         testhandles_ipocurve(icu1);
911 #endif
912                                                 }
913                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
914                                                 if (icu1)
915                                                 {
916                                                         float curVal = position.y();
917                                                         //XXX insert_vert_icu(icu1, frameNumber, curVal, 0);
918 #ifdef TEST_HANDLES_GAME2IPO
919
920                                                         testhandles_ipocurve(icu1);
921 #endif
922                                                 }
923                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
924                                                 if (icu1)
925                                                 {
926                                                         float curVal = position.z();
927                                                         //XXX insert_vert_icu(icu1, frameNumber, curVal, 0);
928 #ifdef TEST_HANDLES_GAME2IPO
929                                                         testhandles_ipocurve(icu1);
930 #endif
931                                                 }
932                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
933                                                 if (icu1)
934                                                 {
935                                                         float curVal = eulerAngles[0];
936                                                         //XXX insert_vert_icu(icu1, frameNumber, curVal, 0);
937 #ifdef TEST_HANDLES_GAME2IPO
938
939                                                         testhandles_ipocurve(icu1);
940 #endif
941                                                 }
942                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
943                                                 if (icu1)
944                                                 {
945                                                         float curVal = eulerAngles[1];
946                                                         //XXX insert_vert_icu(icu1, frameNumber, curVal, 0);
947 #ifdef TEST_HANDLES_GAME2IPO
948
949                                                         testhandles_ipocurve(icu1);
950 #endif
951                                                 }
952                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
953                                                 if (icu1)
954                                                 {
955                                                         float curVal = eulerAngles[2];
956                                                         //XXX insert_vert_icu(icu1, frameNumber, curVal, 0);
957 #ifdef TEST_HANDLES_GAME2IPO
958                                                         
959                                                         testhandles_ipocurve(icu1);
960 #endif
961
962                                                 }
963
964                                         }
965                                 }
966                         }
967
968                 }
969                 
970         
971         }       
972         
973
974 }
975
976
977 void    KX_BlenderSceneConverter::TestHandlesPhysicsObjectToAnimationIpo()
978 {
979
980         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
981         int numScenes = scenes->size();
982         int i;
983         for (i=0;i<numScenes;i++)
984         {
985                 KX_Scene* scene = scenes->at(i);
986                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
987                 CListValue* parentList = scene->GetRootParentList();
988                 int numObjects = parentList->GetCount();
989                 int g;
990                 for (g=0;g<numObjects;g++)
991                 {
992                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
993                         if (gameObj->IsDynamic())
994                         {
995                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
996                                 
997                                 Object* blenderObject = FindBlenderObject(gameObj);
998                                 if (blenderObject)
999                                 {
1000
1001                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
1002                                         float eulerAngles[3];   
1003                                         float tmat[3][3];
1004                                         for (int r=0;r<3;r++)
1005                                         {
1006                                                 for (int c=0;c<3;c++)
1007                                                 {
1008                                                         tmat[r][c] = orn[c][r];
1009                                                 }
1010                                         }
1011                                         Mat3ToEul(tmat, eulerAngles);
1012                                         
1013                                         for(int x = 0; x < 3; x++) {
1014                                                 eulerAngles[x] *= (float) (180 / 3.14159265f);
1015                                         }
1016
1017                                         eulerAngles[0]/=10.f;
1018                                         eulerAngles[1]/=10.f;
1019                                         eulerAngles[2]/=10.f;
1020
1021
1022
1023                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
1024                                         //const MT_Point3& position = gameObj->NodeGetWorldPosition();
1025                                         
1026                                         Ipo* ipo = blenderObject->ipo;
1027                                         if (ipo)
1028                                         {
1029
1030                                                 //create the curves, if not existing
1031
1032                                         IpoCurve *icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
1033                                         //XXX if (!icu1)
1034                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X);
1035                                         
1036                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
1037                                         //XXX if (!icu1)
1038                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y);
1039                                         
1040                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
1041                                         //XXX if (!icu1)
1042                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z);
1043
1044                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
1045                                         //XXX if (!icu1)
1046                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X);
1047
1048                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
1049                                         //XXX if (!icu1)
1050                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y);
1051
1052                                         icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
1053                                         //XXX if (!icu1)
1054                                         //XXX   icu1 = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z);
1055
1056
1057
1058                                         //fill the curves with data
1059
1060                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
1061                                                 if (icu1)
1062                                                 {
1063                                                         testhandles_ipocurve(icu1);
1064                                                 }
1065                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
1066                                                 if (icu1)
1067                                                 {
1068                                                         testhandles_ipocurve(icu1);
1069                                                 }
1070                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
1071                                                 if (icu1)
1072                                                 {
1073                                                         testhandles_ipocurve(icu1);
1074                                                 }
1075                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
1076                                                 if (icu1)
1077                                                 {
1078                                                         testhandles_ipocurve(icu1);
1079                                                 }
1080                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
1081                                                 if (icu1)
1082                                                 {
1083                                                         testhandles_ipocurve(icu1);
1084                                                 }
1085                                                 icu1 = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
1086                                                 if (icu1)
1087                                                 {
1088                                                         testhandles_ipocurve(icu1);
1089                                                 }
1090
1091                                         }
1092                                 }
1093                         }
1094
1095                 }
1096                 
1097         
1098         }
1099
1100
1101
1102 }