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