f0f5e1d4f4121a9f7e2ffbec65f4ec4cf13142e8
[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 #ifndef DISABLE_PYTHON
247                                                                                         PyObject* dictobj,
248 #endif
249                                                                                         class RAS_IRenderTools* rendertools,
250                                                                                         class RAS_ICanvas* canvas)
251 {
252         //find out which physics engine
253         Scene *blenderscene = destinationscene->GetBlenderScene();
254
255         e_PhysicsEngine physics_engine = UseBullet;
256         bool useDbvtCulling = false;
257         // hook for registration function during conversion.
258         m_currentScene = destinationscene;
259         destinationscene->SetSceneConverter(this);
260         SG_SetActiveStage(SG_STAGE_CONVERTER);
261
262         if (blenderscene)
263         {
264         
265                 switch (blenderscene->gm.physicsEngine)
266                 {
267                 case WOPHY_BULLET:
268                         {
269                                 physics_engine = UseBullet;
270                                 useDbvtCulling = (blenderscene->gm.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         switch (physics_engine)
297         {
298 #ifdef USE_BULLET
299                 case UseBullet:
300                         {
301                                 CcdPhysicsEnvironment* ccdPhysEnv = new CcdPhysicsEnvironment(useDbvtCulling);
302                                 ccdPhysEnv->setDebugDrawer(new BlenderDebugDraw());
303                                 ccdPhysEnv->setDeactivationLinearTreshold(0.8f); // default, can be overridden by Python
304                                 ccdPhysEnv->setDeactivationAngularTreshold(1.0f); // default, can be overridden by Python
305
306                                 SYS_SystemHandle syshandle = SYS_GetSystem(); /*unused*/
307                                 int visualizePhysics = SYS_GetCommandLineInt(syshandle,"show_physics",0);
308                                 if (visualizePhysics)
309                                         ccdPhysEnv->setDebugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb|btIDebugDraw::DBG_DrawContactPoints|btIDebugDraw::DBG_DrawText|btIDebugDraw::DBG_DrawConstraintLimits|btIDebugDraw::DBG_DrawConstraints);
310                 
311                                 //todo: get a button in blender ?
312                                 //disable / enable debug drawing (contact points, aabb's etc)   
313                                 //ccdPhysEnv->setDebugMode(1);
314                                 destinationscene->SetPhysicsEnvironment(ccdPhysEnv);
315                                 break;
316                         }
317 #endif  
318                 case UseDynamo:
319                 {
320                 }
321                 
322                 default:
323                 case UseNone:
324                         physics_engine = UseNone;
325                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
326                         break;
327         }
328
329         BL_ConvertBlenderObjects(m_maggie,
330                 destinationscene,
331                 m_ketsjiEngine,
332                 physics_engine,
333 #ifndef DISABLE_PYTHON
334                 dictobj,
335 #endif
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 }
353
354 // This function removes all entities stored in the converter for that scene
355 // It should be used instead of direct delete scene
356 // Note that there was some provision for sharing entities (meshes...) between
357 // scenes but that is now disabled so all scene will have their own copy
358 // and we can delete them here. If the sharing is reactivated, change this code too..
359 // (see KX_BlenderSceneConverter::ConvertScene)
360 void KX_BlenderSceneConverter::RemoveScene(KX_Scene *scene)
361 {
362         int i, size;
363         // delete the scene first as it will stop the use of entities
364         delete scene;
365         // delete the entities of this scene
366         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator worldit;
367         size = m_worldinfos.size();
368         for (i=0, worldit=m_worldinfos.begin(); i<size; ) {
369                 if ((*worldit).first == scene) {
370                         delete (*worldit).second;
371                         *worldit = m_worldinfos.back();
372                         m_worldinfos.pop_back();
373                         size--;
374                 } else {
375                         i++;
376                         worldit++;
377                 }
378         }
379
380         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator polymit;
381         size = m_polymaterials.size();
382         for (i=0, polymit=m_polymaterials.begin(); i<size; ) {
383                 if ((*polymit).first == scene) {
384                         delete (*polymit).second;
385                         *polymit = m_polymaterials.back();
386                         m_polymaterials.pop_back();
387                         size--;
388                 } else {
389                         i++;
390                         polymit++;
391                 }
392         }
393
394         vector<pair<KX_Scene*,BL_Material*> >::iterator matit;
395         size = m_materials.size();
396         for (i=0, matit=m_materials.begin(); i<size; ) {
397                 if ((*matit).first == scene) {
398                         delete (*matit).second;
399                         *matit = m_materials.back();
400                         m_materials.pop_back();
401                         size--;
402                 } else {
403                         i++;
404                         matit++;
405                 }
406         }
407
408         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator meshit;
409         size = m_meshobjects.size();
410         for (i=0, meshit=m_meshobjects.begin(); i<size; ) {
411                 if ((*meshit).first == scene) {
412                         delete (*meshit).second;
413                         *meshit = m_meshobjects.back();
414                         m_meshobjects.pop_back();
415                         size--;
416                 } else {
417                         i++;
418                         meshit++;
419                 }
420         }
421 }
422
423 // use blender materials
424 void KX_BlenderSceneConverter::SetMaterials(bool val)
425 {
426         m_usemat = val;
427         m_useglslmat = false;
428 }
429
430 void KX_BlenderSceneConverter::SetGLSLMaterials(bool val)
431 {
432         m_usemat = val;
433         m_useglslmat = val;
434 }
435
436 bool KX_BlenderSceneConverter::GetMaterials()
437 {
438         return m_usemat;
439 }
440
441 bool KX_BlenderSceneConverter::GetGLSLMaterials()
442 {
443         return m_useglslmat;
444 }
445
446 void KX_BlenderSceneConverter::RegisterBlenderMaterial(BL_Material *mat)
447 {
448         m_materials.push_back(pair<KX_Scene*,BL_Material *>(m_currentScene,mat));
449 }
450
451
452
453 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
454         bool to_what)
455 {
456         m_alwaysUseExpandFraming= to_what;
457 }
458
459         
460
461 void KX_BlenderSceneConverter::RegisterGameObject(
462                                                                         KX_GameObject *gameobject, 
463                                                                         struct Object *for_blenderobject) 
464 {
465         /* only maintained while converting, freed during game runtime */
466         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
467 }
468
469 /* only need to run this during conversion since
470  * m_map_blender_to_gameobject is freed after conversion */
471 void KX_BlenderSceneConverter::UnregisterGameObject(
472                                                                         KX_GameObject *gameobject) 
473 {
474         struct Object *bobp= gameobject->GetBlenderObject();
475         if (bobp) {
476                 CHashedPtr bptr(bobp);
477                 KX_GameObject **gobp= m_map_blender_to_gameobject[bptr];
478                 if (gobp && *gobp == gameobject)
479                 {
480                         // also maintain m_map_blender_to_gameobject if the gameobject
481                         // being removed is matching the blender object
482                         m_map_blender_to_gameobject.remove(bptr);
483                 }
484         }
485 }
486
487 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
488                                                                         struct Object *for_blenderobject) 
489 {
490         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
491         
492         return obp?*obp:NULL;
493 }
494
495 void KX_BlenderSceneConverter::RegisterGameMesh(
496                                                                         RAS_MeshObject *gamemesh,
497                                                                         struct Mesh *for_blendermesh)
498 {
499         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
500         m_meshobjects.push_back(pair<KX_Scene*,RAS_MeshObject*>(m_currentScene,gamemesh));
501 }
502
503
504
505 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
506                                                                         struct Mesh *for_blendermesh/*,
507                                                                         unsigned int onlayer*/)
508 {
509         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
510         
511         if (meshp/* && onlayer==(*meshp)->GetLightLayer()*/) {
512                 return *meshp;
513         } else {
514                 return NULL;
515         }
516 }
517
518         
519
520
521         
522
523 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
524 {
525         m_polymaterials.push_back(pair<KX_Scene*,RAS_IPolyMaterial*>(m_currentScene,polymat));
526 }
527
528
529
530 void KX_BlenderSceneConverter::RegisterInterpolatorList(
531                                                                         BL_InterpolatorList *adtList,
532                                                                         struct AnimData *for_adt)
533 {
534         m_map_blender_to_gameAdtList.insert(CHashedPtr(for_adt), adtList);
535 }
536
537
538
539 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
540                                                                         struct AnimData *for_adt)
541 {
542         BL_InterpolatorList **listp = m_map_blender_to_gameAdtList[CHashedPtr(for_adt)];
543                 
544         return listp?*listp:NULL;
545 }
546
547
548
549 void KX_BlenderSceneConverter::RegisterGameActuator(
550                                                                         SCA_IActuator *act,
551                                                                         struct bActuator *for_actuator)
552 {
553         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
554 }
555
556
557
558 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
559                                                                         struct bActuator *for_actuator)
560 {
561         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
562         
563         return actp?*actp:NULL;
564 }
565
566
567
568 void KX_BlenderSceneConverter::RegisterGameController(
569                                                                         SCA_IController *cont,
570                                                                         struct bController *for_controller)
571 {
572         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
573 }
574
575
576
577 SCA_IController *KX_BlenderSceneConverter::FindGameController(
578                                                                         struct bController *for_controller)
579 {
580         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
581         
582         return contp?*contp:NULL;
583 }
584
585
586
587 void KX_BlenderSceneConverter::RegisterWorldInfo(
588                                                                         KX_WorldInfo *worldinfo)
589 {
590         m_worldinfos.push_back(pair<KX_Scene*,KX_WorldInfo*>(m_currentScene,worldinfo));
591 }
592
593 /*
594  * When deleting an IPO curve from Python, check if the IPO is being
595  * edited and if so clear the pointer to the old curve.
596  */
597 void KX_BlenderSceneConverter::localDel_ipoCurve ( IpoCurve * icu )
598 {
599 #if 0 //XXX
600         if (!G.sipo)
601                 return;
602
603         int i;
604         EditIpo *ei= (EditIpo *)G.sipo->editipo;
605         if (!ei) return;
606
607         for(i=0; i<G.sipo->totipo; i++, ei++) {
608                 if ( ei->icu == icu ) {
609                         ei->flag &= ~(IPO_SELECT | IPO_EDIT);
610                         ei->icu= 0;
611                         return;
612                 }
613         }
614 #endif
615 }
616
617 //quick hack
618 extern "C"
619 {
620         Ipo *add_ipo( char *name, int idcode );
621         //XXX char *getIpoCurveName( IpoCurve * icu );
622         //XXX struct IpoCurve *verify_ipocurve(struct ID *, short, char *, char *, char *, int);
623         //XXX void testhandles_ipocurve(struct IpoCurve *icu);
624         void insert_vert_icu(struct IpoCurve *, float, float, short);
625         float eval_icu(struct IpoCurve *icu, float ipotime);
626         //void Mat3ToEul(float tmat[][3], float *eul);
627         void Mat3ToCompatibleEul(float mat[][3], float *eul, float *oldrot);
628 }
629
630 IpoCurve* findIpoCurve(IpoCurve* first, const char* searchName)
631 {
632         IpoCurve* icu1;
633         for( icu1 = first; icu1; icu1 = icu1->next ) 
634         {
635                 /*XXX char* curveName = getIpoCurveName( icu1 );
636                 if( !strcmp( curveName, searchName) )
637                 {
638                         return icu1;
639                 }*/
640         }
641         return 0;
642 }
643
644 // this is not longer necesary //rcruiz
645 /*Ipo* KX_BlenderSceneConverter::findIpoForName(char* objName)
646 {
647         Ipo* ipo_iter = (Ipo*)m_maggie->ipo.first;
648
649         while( ipo_iter )
650         {
651                 if( strcmp( objName, ipo_iter->id.name + 2 ) == 0 ) 
652                 {
653                         return ipo_iter;
654                 }
655                 ipo_iter = (Ipo*)ipo_iter->id.next;
656         }
657         return 0;
658 }
659 */
660
661 void    KX_BlenderSceneConverter::ResetPhysicsObjectsAnimationIpo(bool clearIpo)
662 {
663
664         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
665         int numScenes = scenes->size();
666         int i;
667         for (i=0;i<numScenes;i++)
668         {
669                 KX_Scene* scene = scenes->at(i);
670                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
671                 CListValue* parentList = scene->GetRootParentList();
672                 int numObjects = parentList->GetCount();
673                 int g;
674                 for (g=0;g<numObjects;g++)
675                 {
676                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
677                         if (gameObj->IsDynamic())
678                         {
679                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
680                                 
681                                 Object* blenderObject = gameObj->GetBlenderObject();
682                                 if (blenderObject)
683                                 {
684                                         //erase existing ipo's
685                                         Ipo* ipo = blenderObject->ipo;//findIpoForName(blenderObject->id.name+2);
686                                         if (ipo)
687                                         {       //clear the curve data
688                                                 if (clearIpo){//rcruiz
689                                                         IpoCurve *icu1;
690                                                                                                                 
691                                                         int numCurves = 0;
692                                                         for( icu1 = (IpoCurve*)ipo->curve.first; icu1;  ) {
693                                                         
694                                                                 IpoCurve* tmpicu = icu1;
695                                                                 
696                                                                 /*int i;
697                                                                 BezTriple *bezt;
698                                                                 for( bezt = tmpicu->bezt, i = 0;        i < tmpicu->totvert; i++, bezt++){
699                                                                         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]);
700                                                                 }*/
701                                                                 
702                                                                 icu1 = icu1->next;
703                                                                 numCurves++;
704                         
705                                                                 BLI_remlink( &( blenderObject->ipo->curve ), tmpicu );
706                                                                 if( tmpicu->bezt )
707                                                                         MEM_freeN( tmpicu->bezt );
708                                                                 MEM_freeN( tmpicu );
709                                                                 localDel_ipoCurve( tmpicu );
710                                                         }
711                                                 }
712                                         } else
713                                         {       ipo = NULL; // XXX add_ipo(blenderObject->id.name+2, ID_OB);
714                                                 blenderObject->ipo = ipo;
715
716                                         }
717                                 
718                                         
719
720                                         
721
722                                 }
723                         }
724
725                 }
726                 
727         
728         }
729
730
731
732 }
733
734 void    KX_BlenderSceneConverter::resetNoneDynamicObjectToIpo(){
735         
736         if (addInitFromFrame){          
737                 KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
738                 int numScenes = scenes->size();
739                 if (numScenes>=0){
740                         KX_Scene* scene = scenes->at(0);
741                         CListValue* parentList = scene->GetRootParentList();
742                         for (int ix=0;ix<parentList->GetCount();ix++){
743                                 KX_GameObject* gameobj = (KX_GameObject*)parentList->GetValue(ix);
744                                 if (!gameobj->IsDynamic()){
745                                         Object* blenderobject = gameobj->GetBlenderObject();
746                                         if (!blenderobject)
747                                                 continue;
748                                         if (blenderobject->type==OB_ARMATURE)
749                                                 continue;
750                                         float eu[3];
751                                         Mat4ToEul(blenderobject->obmat,eu);                                     
752                                         MT_Point3 pos = MT_Point3(
753                                                 blenderobject->obmat[3][0],
754                                                 blenderobject->obmat[3][1],
755                                                 blenderobject->obmat[3][2]
756                                         );
757                                         MT_Vector3 eulxyz = MT_Vector3(
758                                                 eu[0],
759                                                 eu[1],
760                                                 eu[2]
761                                         );
762                                         MT_Vector3 scale = MT_Vector3(
763                                                 blenderobject->size[0],
764                                                 blenderobject->size[1],
765                                                 blenderobject->size[2]
766                                         );
767                                         gameobj->NodeSetLocalPosition(pos);
768                                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
769                                         gameobj->NodeSetLocalScale(scale);
770                                         gameobj->NodeUpdateGS(0);
771                                 }
772                         }
773                 }
774         }
775 }
776
777
778         ///this generates ipo curves for position, rotation, allowing to use game physics in animation
779 void    KX_BlenderSceneConverter::WritePhysicsObjectToAnimationIpo(int frameNumber)
780 {
781
782         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
783         int numScenes = scenes->size();
784         int i;
785         for (i=0;i<numScenes;i++)
786         {
787                 KX_Scene* scene = scenes->at(i);
788                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
789                 CListValue* parentList = scene->GetRootParentList();
790                 int numObjects = parentList->GetCount();
791                 int g;
792                 for (g=0;g<numObjects;g++)
793                 {
794                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
795                         if (gameObj->IsDynamic())
796                         {
797                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
798                                 
799                                 Object* blenderObject = gameObj->GetBlenderObject();
800                                 if (blenderObject && blenderObject->ipo)
801                                 {
802 #if 0
803                                         const MT_Point3& position = gameObj->NodeGetWorldPosition();
804                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
805                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
806                                         
807                                         float eulerAngles[3];   
808                                         float eulerAnglesOld[3] = {0.0f, 0.0f, 0.0f};                                           
809                                         float tmat[3][3];
810                                         
811                                         // XXX animato
812                                         Ipo* ipo = blenderObject->ipo;
813
814                                         //create the curves, if not existing, set linear if new
815
816                                         IpoCurve *icu_lx = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
817                                         if (!icu_lx) {
818                                                 icu_lx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X, 1);
819                                                 if(icu_lx) icu_lx->ipo = IPO_LIN;
820                                         }
821                                         IpoCurve *icu_ly = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
822                                         if (!icu_ly) {
823                                                 icu_ly = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y, 1);
824                                                 if(icu_ly) icu_ly->ipo = IPO_LIN;
825                                         }
826                                         IpoCurve *icu_lz = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
827                                         if (!icu_lz) {
828                                                 icu_lz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z, 1);
829                                                 if(icu_lz) icu_lz->ipo = IPO_LIN;
830                                         }
831                                         IpoCurve *icu_rx = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
832                                         if (!icu_rx) {
833                                                 icu_rx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X, 1);
834                                                 if(icu_rx) icu_rx->ipo = IPO_LIN;
835                                         }
836                                         IpoCurve *icu_ry = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
837                                         if (!icu_ry) {
838                                                 icu_ry = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y, 1);
839                                                 if(icu_ry) icu_ry->ipo = IPO_LIN;
840                                         }
841                                         IpoCurve *icu_rz = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
842                                         if (!icu_rz) {
843                                                 icu_rz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z, 1);
844                                                 if(icu_rz) icu_rz->ipo = IPO_LIN;
845                                         }
846                                         
847                                         if(icu_rx) eulerAnglesOld[0]= eval_icu( icu_rx, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
848                                         if(icu_ry) eulerAnglesOld[1]= eval_icu( icu_ry, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
849                                         if(icu_rz) eulerAnglesOld[2]= eval_icu( icu_rz, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
850                                         
851                                         // orn.getValue((float *)tmat); // uses the wrong ordering, cant use this
852                                         for (int r=0;r<3;r++)
853                                                 for (int c=0;c<3;c++)
854                                                         tmat[r][c] = orn[c][r];
855                                         
856                                         // Mat3ToEul(tmat, eulerAngles); // better to use Mat3ToCompatibleEul
857                                         Mat3ToCompatibleEul(tmat, eulerAngles, eulerAnglesOld);
858                                         
859                                         //eval_icu
860                                         for(int x = 0; x < 3; x++)
861                                                 eulerAngles[x] *= (float) ((180 / 3.14159265f) / 10.0);
862                                         
863                                         //fill the curves with data
864                                         if (icu_lx) insert_vert_icu(icu_lx, frameNumber, position.x(), 1);
865                                         if (icu_ly) insert_vert_icu(icu_ly, frameNumber, position.y(), 1);
866                                         if (icu_lz) insert_vert_icu(icu_lz, frameNumber, position.z(), 1);
867                                         if (icu_rx) insert_vert_icu(icu_rx, frameNumber, eulerAngles[0], 1);
868                                         if (icu_ry) insert_vert_icu(icu_ry, frameNumber, eulerAngles[1], 1);
869                                         if (icu_rz) insert_vert_icu(icu_rz, frameNumber, eulerAngles[2], 1);
870                                         
871                                         // Handles are corrected at the end, testhandles_ipocurve isnt needed yet
872 #endif
873                                 }
874                         }
875                 }
876         }
877 }
878
879
880 void    KX_BlenderSceneConverter::TestHandlesPhysicsObjectToAnimationIpo()
881 {
882
883         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
884         int numScenes = scenes->size();
885         int i;
886         for (i=0;i<numScenes;i++)
887         {
888                 KX_Scene* scene = scenes->at(i);
889                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
890                 CListValue* parentList = scene->GetRootParentList();
891                 int numObjects = parentList->GetCount();
892                 int g;
893                 for (g=0;g<numObjects;g++)
894                 {
895                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
896                         if (gameObj->IsDynamic())
897                         {
898                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
899                                 
900                                 Object* blenderObject = gameObj->GetBlenderObject();
901                                 if (blenderObject && blenderObject->ipo)
902                                 {
903                                         // XXX animato
904 #if 0
905                                         Ipo* ipo = blenderObject->ipo;
906                                         
907                                         //create the curves, if not existing
908                                         //testhandles_ipocurve checks for NULL
909                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocX"));
910                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocY"));
911                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ"));
912                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotX"));
913                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotY"));
914                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ"));
915 #endif
916                                 }
917                         }
918
919                 }
920                 
921         
922         }
923
924
925
926 }