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