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