svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22075:22099
[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 #include "KX_ConvertPhysicsObject.h"
48
49 #ifdef USE_BULLET
50 #include "CcdPhysicsEnvironment.h"
51 #endif
52
53 #include "KX_BlenderSceneConverter.h"
54 #include "KX_BlenderScalarInterpolator.h"
55 #include "BL_BlenderDataConversion.h"
56 #include "BlenderWorldInfo.h"
57 #include "KX_Scene.h"
58
59 /* This little block needed for linking to Blender... */
60 #ifdef WIN32
61 #include "BLI_winstuff.h"
62 #endif
63
64 /* This list includes only data type definitions */
65 #include "DNA_scene_types.h"
66 #include "DNA_world_types.h"
67 #include "BKE_main.h"
68
69 #include "BLI_arithb.h"
70
71 extern "C"
72 {
73 #include "DNA_object_types.h"
74 #include "DNA_curve_types.h"
75 #include "BLI_blenlib.h"
76 #include "MEM_guardedalloc.h"
77 //XXX #include "BSE_editipo.h"
78 //XXX #include "BSE_editipo_types.h"
79 #include "DNA_ipo_types.h"
80 #include "BKE_global.h"
81 #include "BKE_ipo.h" // eval_icu
82 #include "DNA_space_types.h"
83 }
84
85
86 KX_BlenderSceneConverter::KX_BlenderSceneConverter(
87                                                         struct Main* maggie,
88                                                         class KX_KetsjiEngine* engine
89                                                         )
90                                                         : m_maggie(maggie),
91                                                         m_ketsjiEngine(engine),
92                                                         m_alwaysUseExpandFraming(false),
93                                                         m_usemat(false),
94                                                         m_useglslmat(false)
95 {
96         m_newfilename = "";
97 }
98
99
100 KX_BlenderSceneConverter::~KX_BlenderSceneConverter()
101 {
102         // clears meshes, and hashmaps from blender to gameengine data
103         int i;
104         // delete sumoshapes
105         
106
107         int numAdtLists = m_map_blender_to_gameAdtList.size();
108         for (i=0; i<numAdtLists; i++) {
109                 BL_InterpolatorList *adtList= *m_map_blender_to_gameAdtList.at(i);
110
111                 delete (adtList);
112         }
113
114         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator itw = m_worldinfos.begin();
115         while (itw != m_worldinfos.end()) {
116                 delete (*itw).second;
117                 itw++;
118         }
119
120         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator itp = m_polymaterials.begin();
121         while (itp != m_polymaterials.end()) {
122                 delete (*itp).second;
123                 itp++;
124         }
125
126         // delete after RAS_IPolyMaterial
127         vector<pair<KX_Scene*,BL_Material *> >::iterator itmat = m_materials.begin();
128         while (itmat != m_materials.end()) {
129                 delete (*itmat).second;
130                 itmat++;
131         }       
132
133
134         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator itm = m_meshobjects.begin();
135         while (itm != m_meshobjects.end()) {
136                 delete (*itm).second;
137                 itm++;
138         }
139
140 #ifdef USE_BULLET
141         KX_ClearBulletSharedShapes();
142 #endif
143
144 }
145
146
147
148 void KX_BlenderSceneConverter::SetNewFileName(const STR_String& filename)
149 {
150         m_newfilename = filename;
151 }
152
153
154
155 bool KX_BlenderSceneConverter::TryAndLoadNewFile()
156 {
157         bool result = false;
158
159         // find the file
160 /*      if ()
161         {
162                 result = true;
163         }
164         // if not, clear the newfilename
165         else
166         {
167                 m_newfilename = "";     
168         }
169 */
170         return result;
171 }
172
173 Scene *KX_BlenderSceneConverter::GetBlenderSceneForName(const STR_String& name)
174 {
175         Scene *sce;
176
177         /**
178          * Find the specified scene by name, or the first
179          * scene if nothing matches (shouldn't happen).
180          */
181
182         for (sce= (Scene*) m_maggie->scene.first; sce; sce= (Scene*) sce->id.next)
183                 if (name == (sce->id.name+2))
184                         return sce;
185
186         return (Scene*)m_maggie->scene.first;
187
188 }
189 #include "KX_PythonInit.h"
190
191 #ifdef USE_BULLET
192
193 #include "LinearMath/btIDebugDraw.h"
194
195
196 struct  BlenderDebugDraw : public btIDebugDraw
197 {
198         BlenderDebugDraw () :
199                 m_debugMode(0) 
200         {
201         }
202         
203         int m_debugMode;
204
205         virtual void    drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
206         {
207                 if (m_debugMode >0)
208                 {
209                         MT_Vector3 kxfrom(from[0],from[1],from[2]);
210                         MT_Vector3 kxto(to[0],to[1],to[2]);
211                         MT_Vector3 kxcolor(color[0],color[1],color[2]);
212
213                         KX_RasterizerDrawDebugLine(kxfrom,kxto,kxcolor);
214                 }
215         }
216         
217         virtual void    reportErrorWarning(const char* warningString)
218         {
219
220         }
221
222         virtual void    drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,float distance,int lifeTime,const btVector3& color)
223         {
224                 //not yet
225         }
226
227         virtual void    setDebugMode(int debugMode)
228         {
229                 m_debugMode = debugMode;
230         }
231         virtual int             getDebugMode() const
232         {
233                 return m_debugMode;
234         }
235         ///todo: find out if Blender can do this
236         virtual void    draw3dText(const btVector3& location,const char* textString)
237         {
238
239         }
240                 
241 };
242
243 #endif
244
245 void KX_BlenderSceneConverter::ConvertScene(class KX_Scene* destinationscene,
246                                                                                         PyObject* dictobj,
247                                                                                         class RAS_IRenderTools* rendertools,
248                                                                                         class RAS_ICanvas* canvas)
249 {
250         //find out which physics engine
251         Scene *blenderscene = destinationscene->GetBlenderScene();
252
253         e_PhysicsEngine physics_engine = UseBullet;
254         bool useDbvtCulling = false;
255         // hook for registration function during conversion.
256         m_currentScene = destinationscene;
257         destinationscene->SetSceneConverter(this);
258         SG_SetActiveStage(SG_STAGE_CONVERTER);
259
260         if (blenderscene)
261         {
262         
263                 if (blenderscene->world)
264                 {
265                         switch (blenderscene->world->physicsEngine)
266                         {
267                         case WOPHY_BULLET:
268                                 {
269                                         physics_engine = UseBullet;
270                                         useDbvtCulling = (blenderscene->world->mode & WO_DBVT_CULLING) != 0;
271                                         break;
272                                 }
273                                 
274                                 case WOPHY_ODE:
275                                 {
276                                         physics_engine = UseODE;
277                                         break;
278                                 }
279                                 case WOPHY_DYNAMO:
280                                 {
281                                         physics_engine = UseDynamo;
282                                         break;
283                                 }
284                                 case WOPHY_SUMO:
285                                 {
286                                         physics_engine = UseSumo; 
287                                         break;
288                                 }
289                                 case WOPHY_NONE:
290                                 {
291                                         physics_engine = UseNone;
292                                 }
293                         }
294                   
295                 }
296         }
297
298         switch (physics_engine)
299         {
300 #ifdef USE_BULLET
301                 case UseBullet:
302                         {
303                                 CcdPhysicsEnvironment* ccdPhysEnv = new CcdPhysicsEnvironment(useDbvtCulling);
304                                 ccdPhysEnv->setDebugDrawer(new BlenderDebugDraw());
305                                 ccdPhysEnv->setDeactivationLinearTreshold(0.8f); // default, can be overridden by Python
306                                 ccdPhysEnv->setDeactivationAngularTreshold(1.0f); // default, can be overridden by Python
307
308                                 SYS_SystemHandle syshandle = SYS_GetSystem(); /*unused*/
309                                 int visualizePhysics = SYS_GetCommandLineInt(syshandle,"show_physics",0);
310                                 if (visualizePhysics)
311                                         ccdPhysEnv->setDebugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb|btIDebugDraw::DBG_DrawContactPoints|btIDebugDraw::DBG_DrawText|btIDebugDraw::DBG_DrawConstraintLimits|btIDebugDraw::DBG_DrawConstraints);
312                 
313                                 //todo: get a button in blender ?
314                                 //disable / enable debug drawing (contact points, aabb's etc)   
315                                 //ccdPhysEnv->setDebugMode(1);
316                                 destinationscene->SetPhysicsEnvironment(ccdPhysEnv);
317                                 break;
318                         }
319 #endif  
320                 case UseDynamo:
321                 {
322                 }
323                 
324                 default:
325                 case UseNone:
326                         physics_engine = UseNone;
327                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
328                         break;
329         }
330
331         BL_ConvertBlenderObjects(m_maggie,
332                 destinationscene,
333                 m_ketsjiEngine,
334                 physics_engine,
335                 dictobj,
336                 rendertools,
337                 canvas,
338                 this,
339                 m_alwaysUseExpandFraming
340                 );
341
342         //These lookup are not needed during game
343         m_map_blender_to_gameactuator.clear();
344         m_map_blender_to_gamecontroller.clear();
345         m_map_blender_to_gameobject.clear();
346
347         //Clearing this lookup table has the effect of disabling the cache of meshes
348         //between scenes, even if they are shared in the blend file.
349         //This cache mecanism is buggy so I leave it disable and the memory leak
350         //that would result from this is fixed in RemoveScene()
351         m_map_mesh_to_gamemesh.clear();
352         //Don't clear this lookup, it is needed for the baking physics into ipo animation
353         //To avoid it's infinite grows, object will be unregister when they are deleted 
354         //see KX_Scene::NewRemoveObject
355         //m_map_gameobject_to_blender.clear();
356 }
357
358 // This function removes all entities stored in the converter for that scene
359 // It should be used instead of direct delete scene
360 // Note that there was some provision for sharing entities (meshes...) between
361 // scenes but that is now disabled so all scene will have their own copy
362 // and we can delete them here. If the sharing is reactivated, change this code too..
363 // (see KX_BlenderSceneConverter::ConvertScene)
364 void KX_BlenderSceneConverter::RemoveScene(KX_Scene *scene)
365 {
366         int i, size;
367         // delete the scene first as it will stop the use of entities
368         delete scene;
369         // delete the entities of this scene
370         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator worldit;
371         size = m_worldinfos.size();
372         for (i=0, worldit=m_worldinfos.begin(); i<size; ) {
373                 if ((*worldit).first == scene) {
374                         delete (*worldit).second;
375                         *worldit = m_worldinfos.back();
376                         m_worldinfos.pop_back();
377                         size--;
378                 } else {
379                         i++;
380                         worldit++;
381                 }
382         }
383
384         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator polymit;
385         size = m_polymaterials.size();
386         for (i=0, polymit=m_polymaterials.begin(); i<size; ) {
387                 if ((*polymit).first == scene) {
388                         delete (*polymit).second;
389                         *polymit = m_polymaterials.back();
390                         m_polymaterials.pop_back();
391                         size--;
392                 } else {
393                         i++;
394                         polymit++;
395                 }
396         }
397
398         vector<pair<KX_Scene*,BL_Material*> >::iterator matit;
399         size = m_materials.size();
400         for (i=0, matit=m_materials.begin(); i<size; ) {
401                 if ((*matit).first == scene) {
402                         delete (*matit).second;
403                         *matit = m_materials.back();
404                         m_materials.pop_back();
405                         size--;
406                 } else {
407                         i++;
408                         matit++;
409                 }
410         }
411
412         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator meshit;
413         size = m_meshobjects.size();
414         for (i=0, meshit=m_meshobjects.begin(); i<size; ) {
415                 if ((*meshit).first == scene) {
416                         delete (*meshit).second;
417                         *meshit = m_meshobjects.back();
418                         m_meshobjects.pop_back();
419                         size--;
420                 } else {
421                         i++;
422                         meshit++;
423                 }
424         }
425 }
426
427 // use blender materials
428 void KX_BlenderSceneConverter::SetMaterials(bool val)
429 {
430         m_usemat = val;
431         m_useglslmat = false;
432 }
433
434 void KX_BlenderSceneConverter::SetGLSLMaterials(bool val)
435 {
436         m_usemat = val;
437         m_useglslmat = val;
438 }
439
440 bool KX_BlenderSceneConverter::GetMaterials()
441 {
442         return m_usemat;
443 }
444
445 bool KX_BlenderSceneConverter::GetGLSLMaterials()
446 {
447         return m_useglslmat;
448 }
449
450 void KX_BlenderSceneConverter::RegisterBlenderMaterial(BL_Material *mat)
451 {
452         m_materials.push_back(pair<KX_Scene*,BL_Material *>(m_currentScene,mat));
453 }
454
455
456
457 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
458         bool to_what)
459 {
460         m_alwaysUseExpandFraming= to_what;
461 }
462
463         
464
465 void KX_BlenderSceneConverter::RegisterGameObject(
466                                                                         KX_GameObject *gameobject, 
467                                                                         struct Object *for_blenderobject) 
468 {
469         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
470         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
471 }
472
473 void KX_BlenderSceneConverter::UnregisterGameObject(
474                                                                         KX_GameObject *gameobject) 
475 {
476         CHashedPtr gptr(gameobject);
477         struct Object **bobp= m_map_gameobject_to_blender[gptr];
478         if (bobp) {
479                 CHashedPtr bptr(*bobp);
480                 KX_GameObject **gobp= m_map_blender_to_gameobject[bptr];
481                 if (gobp && *gobp == gameobject)
482                         // also maintain m_map_blender_to_gameobject if the gameobject
483                         // being removed is matching the blender object
484                         m_map_blender_to_gameobject.remove(bptr);
485                 m_map_gameobject_to_blender.remove(gptr);
486         }
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 *adtList,
547                                                                         struct AnimData *for_adt)
548 {
549         m_map_blender_to_gameAdtList.insert(CHashedPtr(for_adt), adtList);
550 }
551
552
553
554 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
555                                                                         struct AnimData *for_adt)
556 {
557         BL_InterpolatorList **listp = m_map_blender_to_gameAdtList[CHashedPtr(for_adt)];
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 )
613 {
614 #if 0 //XXX
615         if (!G.sipo)
616                 return;
617
618         int i;
619         EditIpo *ei= (EditIpo *)G.sipo->editipo;
620         if (!ei) return;
621
622         for(i=0; i<G.sipo->totipo; i++, ei++) {
623                 if ( ei->icu == icu ) {
624                         ei->flag &= ~(IPO_SELECT | IPO_EDIT);
625                         ei->icu= 0;
626                         return;
627                 }
628         }
629 #endif
630 }
631
632 //quick hack
633 extern "C"
634 {
635         Ipo *add_ipo( char *name, int idcode );
636         //XXX char *getIpoCurveName( IpoCurve * icu );
637         //XXX struct IpoCurve *verify_ipocurve(struct ID *, short, char *, char *, char *, int);
638         //XXX void testhandles_ipocurve(struct IpoCurve *icu);
639         void insert_vert_icu(struct IpoCurve *, float, float, short);
640         float eval_icu(struct IpoCurve *icu, float ipotime);
641         //void Mat3ToEul(float tmat[][3], float *eul);
642         void Mat3ToCompatibleEul(float mat[][3], float *eul, float *oldrot);
643 }
644
645 IpoCurve* findIpoCurve(IpoCurve* first, const char* searchName)
646 {
647         IpoCurve* icu1;
648         for( icu1 = first; icu1; icu1 = icu1->next ) 
649         {
650                 /*XXX char* curveName = getIpoCurveName( icu1 );
651                 if( !strcmp( curveName, searchName) )
652                 {
653                         return icu1;
654                 }*/
655         }
656         return 0;
657 }
658
659 // this is not longer necesary //rcruiz
660 /*Ipo* KX_BlenderSceneConverter::findIpoForName(char* objName)
661 {
662         Ipo* ipo_iter = (Ipo*)m_maggie->ipo.first;
663
664         while( ipo_iter )
665         {
666                 if( strcmp( objName, ipo_iter->id.name + 2 ) == 0 ) 
667                 {
668                         return ipo_iter;
669                 }
670                 ipo_iter = (Ipo*)ipo_iter->id.next;
671         }
672         return 0;
673 }
674 */
675
676 void    KX_BlenderSceneConverter::ResetPhysicsObjectsAnimationIpo(bool clearIpo)
677 {
678
679         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
680         int numScenes = scenes->size();
681         int i;
682         for (i=0;i<numScenes;i++)
683         {
684                 KX_Scene* scene = scenes->at(i);
685                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
686                 CListValue* parentList = scene->GetRootParentList();
687                 int numObjects = parentList->GetCount();
688                 int g;
689                 for (g=0;g<numObjects;g++)
690                 {
691                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
692                         if (gameObj->IsDynamic())
693                         {
694                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
695                                 
696                                 Object* blenderObject = FindBlenderObject(gameObj);
697                                 if (blenderObject)
698                                 {
699                                         //erase existing ipo's
700                                         Ipo* ipo = blenderObject->ipo;//findIpoForName(blenderObject->id.name+2);
701                                         if (ipo)
702                                         {       //clear the curve data
703                                                 if (clearIpo){//rcruiz
704                                                         IpoCurve *icu1;
705                                                                                                                 
706                                                         int numCurves = 0;
707                                                         for( icu1 = (IpoCurve*)ipo->curve.first; icu1;  ) {
708                                                         
709                                                                 IpoCurve* tmpicu = icu1;
710                                                                 
711                                                                 /*int i;
712                                                                 BezTriple *bezt;
713                                                                 for( bezt = tmpicu->bezt, i = 0;        i < tmpicu->totvert; i++, bezt++){
714                                                                         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]);
715                                                                 }*/
716                                                                 
717                                                                 icu1 = icu1->next;
718                                                                 numCurves++;
719                         
720                                                                 BLI_remlink( &( blenderObject->ipo->curve ), tmpicu );
721                                                                 if( tmpicu->bezt )
722                                                                         MEM_freeN( tmpicu->bezt );
723                                                                 MEM_freeN( tmpicu );
724                                                                 localDel_ipoCurve( tmpicu );
725                                                         }
726                                                 }
727                                         } else
728                                         {       ipo = NULL; // XXX add_ipo(blenderObject->id.name+2, ID_OB);
729                                                 blenderObject->ipo = ipo;
730
731                                         }
732                                 
733                                         
734
735                                         
736
737                                 }
738                         }
739
740                 }
741                 
742         
743         }
744
745
746
747 }
748
749 void    KX_BlenderSceneConverter::resetNoneDynamicObjectToIpo(){
750         
751         if (addInitFromFrame){          
752                 KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
753                 int numScenes = scenes->size();
754                 if (numScenes>=0){
755                         KX_Scene* scene = scenes->at(0);
756                         CListValue* parentList = scene->GetRootParentList();
757                         for (int ix=0;ix<parentList->GetCount();ix++){
758                                 KX_GameObject* gameobj = (KX_GameObject*)parentList->GetValue(ix);
759                                 if (!gameobj->IsDynamic()){
760                                         Object* blenderobject = FindBlenderObject(gameobj);
761                                         if (!blenderobject)
762                                                 continue;
763                                         if (blenderobject->type==OB_ARMATURE)
764                                                 continue;
765                                         float eu[3];
766                                         Mat4ToEul(blenderobject->obmat,eu);                                     
767                                         MT_Point3 pos = MT_Point3(
768                                                 blenderobject->obmat[3][0],
769                                                 blenderobject->obmat[3][1],
770                                                 blenderobject->obmat[3][2]
771                                         );
772                                         MT_Vector3 eulxyz = MT_Vector3(
773                                                 eu[0],
774                                                 eu[1],
775                                                 eu[2]
776                                         );
777                                         MT_Vector3 scale = MT_Vector3(
778                                                 blenderobject->size[0],
779                                                 blenderobject->size[1],
780                                                 blenderobject->size[2]
781                                         );
782                                         gameobj->NodeSetLocalPosition(pos);
783                                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
784                                         gameobj->NodeSetLocalScale(scale);
785                                         gameobj->NodeUpdateGS(0);
786                                 }
787                         }
788                 }
789         }
790 }
791
792
793         ///this generates ipo curves for position, rotation, allowing to use game physics in animation
794 void    KX_BlenderSceneConverter::WritePhysicsObjectToAnimationIpo(int frameNumber)
795 {
796
797         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
798         int numScenes = scenes->size();
799         int i;
800         for (i=0;i<numScenes;i++)
801         {
802                 KX_Scene* scene = scenes->at(i);
803                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
804                 CListValue* parentList = scene->GetRootParentList();
805                 int numObjects = parentList->GetCount();
806                 int g;
807                 for (g=0;g<numObjects;g++)
808                 {
809                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
810                         if (gameObj->IsDynamic())
811                         {
812                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
813                                 
814                                 Object* blenderObject = FindBlenderObject(gameObj);
815                                 if (blenderObject && blenderObject->ipo)
816                                 {
817                                         const MT_Point3& position = gameObj->NodeGetWorldPosition();
818                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
819                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
820                                         
821                                         float eulerAngles[3];   
822                                         float eulerAnglesOld[3] = {0.0f, 0.0f, 0.0f};                                           
823                                         float tmat[3][3];
824                                         
825                                         // XXX animato
826 #if 0
827                                         Ipo* ipo = blenderObject->ipo;
828
829                                         //create the curves, if not existing, set linear if new
830
831                                         IpoCurve *icu_lx = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
832                                         if (!icu_lx) {
833                                                 icu_lx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X, 1);
834                                                 if(icu_lx) icu_lx->ipo = IPO_LIN;
835                                         }
836                                         IpoCurve *icu_ly = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
837                                         if (!icu_ly) {
838                                                 icu_ly = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y, 1);
839                                                 if(icu_ly) icu_ly->ipo = IPO_LIN;
840                                         }
841                                         IpoCurve *icu_lz = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
842                                         if (!icu_lz) {
843                                                 icu_lz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z, 1);
844                                                 if(icu_lz) icu_lz->ipo = IPO_LIN;
845                                         }
846                                         IpoCurve *icu_rx = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
847                                         if (!icu_rx) {
848                                                 icu_rx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X, 1);
849                                                 if(icu_rx) icu_rx->ipo = IPO_LIN;
850                                         }
851                                         IpoCurve *icu_ry = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
852                                         if (!icu_ry) {
853                                                 icu_ry = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y, 1);
854                                                 if(icu_ry) icu_ry->ipo = IPO_LIN;
855                                         }
856                                         IpoCurve *icu_rz = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
857                                         if (!icu_rz) {
858                                                 icu_rz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z, 1);
859                                                 if(icu_rz) icu_rz->ipo = IPO_LIN;
860                                         }
861                                         
862                                         if(icu_rx) eulerAnglesOld[0]= eval_icu( icu_rx, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
863                                         if(icu_ry) eulerAnglesOld[1]= eval_icu( icu_ry, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
864                                         if(icu_rz) eulerAnglesOld[2]= eval_icu( icu_rz, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
865                                         
866                                         // orn.getValue((float *)tmat); // uses the wrong ordering, cant use this
867                                         for (int r=0;r<3;r++)
868                                                 for (int c=0;c<3;c++)
869                                                         tmat[r][c] = orn[c][r];
870                                         
871                                         // Mat3ToEul(tmat, eulerAngles); // better to use Mat3ToCompatibleEul
872                                         Mat3ToCompatibleEul(tmat, eulerAngles, eulerAnglesOld);
873                                         
874                                         //eval_icu
875                                         for(int x = 0; x < 3; x++)
876                                                 eulerAngles[x] *= (float) ((180 / 3.14159265f) / 10.0);
877                                         
878                                         //fill the curves with data
879                                         if (icu_lx) insert_vert_icu(icu_lx, frameNumber, position.x(), 1);
880                                         if (icu_ly) insert_vert_icu(icu_ly, frameNumber, position.y(), 1);
881                                         if (icu_lz) insert_vert_icu(icu_lz, frameNumber, position.z(), 1);
882                                         if (icu_rx) insert_vert_icu(icu_rx, frameNumber, eulerAngles[0], 1);
883                                         if (icu_ry) insert_vert_icu(icu_ry, frameNumber, eulerAngles[1], 1);
884                                         if (icu_rz) insert_vert_icu(icu_rz, frameNumber, eulerAngles[2], 1);
885                                         
886                                         // Handles are corrected at the end, testhandles_ipocurve isnt needed yet
887 #endif
888                                 }
889                         }
890                 }
891         }
892 }
893
894
895 void    KX_BlenderSceneConverter::TestHandlesPhysicsObjectToAnimationIpo()
896 {
897
898         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
899         int numScenes = scenes->size();
900         int i;
901         for (i=0;i<numScenes;i++)
902         {
903                 KX_Scene* scene = scenes->at(i);
904                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
905                 CListValue* parentList = scene->GetRootParentList();
906                 int numObjects = parentList->GetCount();
907                 int g;
908                 for (g=0;g<numObjects;g++)
909                 {
910                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
911                         if (gameObj->IsDynamic())
912                         {
913                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
914                                 
915                                 Object* blenderObject = FindBlenderObject(gameObj);
916                                 if (blenderObject && blenderObject->ipo)
917                                 {
918                                         // XXX animato
919 #if 0
920                                         Ipo* ipo = blenderObject->ipo;
921                                         
922                                         //create the curves, if not existing
923                                         //testhandles_ipocurve checks for NULL
924                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocX"));
925                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocY"));
926                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ"));
927                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotX"));
928                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotY"));
929                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ"));
930 #endif
931                                 }
932                         }
933
934                 }
935                 
936         
937         }
938
939
940
941 }