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