changes to the sequencer in trunk between 20855:20928, (excluding UI changes)
[blender.git] / source / gameengine / Converter / BL_BlenderDataConversion.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Convert blender data to ketsji
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #ifdef WIN32
36 #pragma warning (disable : 4786)
37 #endif
38
39 #include <math.h>
40
41 #include "BL_BlenderDataConversion.h"
42 #include "KX_BlenderGL.h"
43 #include "KX_BlenderScalarInterpolator.h"
44
45 #include "RAS_IPolygonMaterial.h"
46 #include "KX_PolygonMaterial.h"
47
48 // Expressions
49 #include "ListValue.h"
50 #include "IntValue.h"
51 // Collision & Fuzzics LTD
52
53 #include "PHY_Pro.h"
54
55
56 #include "KX_Scene.h"
57 #include "KX_GameObject.h"
58 #include "RAS_FramingManager.h"
59 #include "RAS_MeshObject.h"
60
61 #include "KX_ConvertActuators.h"
62 #include "KX_ConvertControllers.h"
63 #include "KX_ConvertSensors.h"
64
65 #include "SCA_LogicManager.h"
66 #include "SCA_EventManager.h"
67 #include "SCA_TimeEventManager.h"
68 #include "KX_Light.h"
69 #include "KX_Camera.h"
70 #include "KX_EmptyObject.h"
71 #include "MT_Point3.h"
72 #include "MT_Transform.h"
73 #include "MT_MinMax.h"
74 #include "SCA_IInputDevice.h"
75 #include "RAS_TexMatrix.h"
76 #include "RAS_ICanvas.h"
77 #include "RAS_MaterialBucket.h"
78 //#include "KX_BlenderPolyMaterial.h"
79 #include "RAS_Polygon.h"
80 #include "RAS_TexVert.h"
81 #include "RAS_BucketManager.h"
82 #include "RAS_IRenderTools.h"
83 #include "BL_Material.h"
84 #include "KX_BlenderMaterial.h"
85 #include "BL_Texture.h"
86
87 #include "DNA_action_types.h"
88 #include "BKE_main.h"
89 #include "BKE_global.h"
90 #include "BKE_object.h"
91 #include "BKE_scene.h"
92 #include "BL_SkinMeshObject.h"
93 #include "BL_ModifierDeformer.h"
94 #include "BL_ShapeDeformer.h"
95 #include "BL_SkinDeformer.h"
96 #include "BL_MeshDeformer.h"
97 //#include "BL_ArmatureController.h"
98
99 #include "BlenderWorldInfo.h"
100
101 #include "KX_KetsjiEngine.h"
102 #include "KX_BlenderSceneConverter.h"
103
104 #include"SND_Scene.h"
105 #include "SND_SoundListener.h"
106
107 /* This little block needed for linking to Blender... */
108 #ifdef WIN32
109 #include "BLI_winstuff.h"
110 #endif
111
112 /* This list includes only data type definitions */
113 #include "DNA_object_types.h"
114 #include "DNA_material_types.h"
115 #include "DNA_texture_types.h"
116 #include "DNA_image_types.h"
117 #include "DNA_lamp_types.h"
118 #include "DNA_group_types.h"
119 #include "DNA_scene_types.h"
120 #include "DNA_camera_types.h"
121 #include "DNA_property_types.h"
122 #include "DNA_text_types.h"
123 #include "DNA_sensor_types.h"
124 #include "DNA_controller_types.h"
125 #include "DNA_actuator_types.h"
126 #include "DNA_mesh_types.h"
127 #include "DNA_meshdata_types.h"
128 #include "DNA_view3d_types.h"
129 #include "DNA_world_types.h"
130 #include "DNA_sound_types.h"
131 #include "DNA_key_types.h"
132 #include "DNA_armature_types.h"
133 #include "DNA_object_force.h"
134
135 #include "MEM_guardedalloc.h"
136 #include "BKE_utildefines.h"
137 #include "BKE_key.h"
138 #include "BKE_mesh.h"
139 #include "MT_Point3.h"
140
141 #include "BLI_arithb.h"
142
143 extern "C" {
144 #include "BKE_customdata.h"
145 #include "BKE_cdderivedmesh.h"
146 #include "BKE_DerivedMesh.h"
147 }
148
149 #include "BKE_material.h" /* give_current_material */
150 /* end of blender include block */
151
152 #include "KX_BlenderInputDevice.h"
153 #include "KX_ConvertProperties.h"
154 #include "KX_HashedPtr.h"
155
156
157 #include "KX_ScalarInterpolator.h"
158
159 #include "KX_IpoConvert.h"
160 #include "SYS_System.h"
161
162 #include "SG_Node.h"
163 #include "SG_BBox.h"
164 #include "SG_Tree.h"
165
166 // defines USE_ODE to choose physics engine
167 #include "KX_ConvertPhysicsObject.h"
168 #ifdef USE_BULLET
169 #include "CcdPhysicsEnvironment.h"
170 #include "CcdGraphicController.h"
171 #endif
172 #include "KX_MotionState.h"
173
174 // This file defines relationships between parents and children
175 // in the game engine.
176
177 #include "KX_SG_NodeRelationships.h"
178 #include "KX_SG_BoneParentNodeRelationship.h"
179
180 #include "BL_ArmatureObject.h"
181 #include "BL_DeformableGameObject.h"
182
183 #ifdef __cplusplus
184 extern "C" {
185 #endif
186 //XXX #include "BSE_headerbuttons.h"
187 //XXX void update_for_newframe();
188 //void scene_update_for_newframe(struct Scene *sce, unsigned int lay);
189 //#include "BKE_ipo.h"
190 //void do_all_data_ipos(void);
191 #ifdef __cplusplus
192 }
193 #endif
194
195 static int default_face_mode = TF_DYNAMIC;
196
197 static unsigned int KX_rgbaint2uint_new(unsigned int icol)
198 {
199         union
200         {
201                 unsigned int integer;
202                 unsigned char cp[4];
203         } out_color, in_color;
204         
205         in_color.integer = icol;
206         out_color.cp[0] = in_color.cp[3]; // red
207         out_color.cp[1] = in_color.cp[2]; // green
208         out_color.cp[2] = in_color.cp[1]; // blue
209         out_color.cp[3] = in_color.cp[0]; // alpha
210         
211         return out_color.integer;
212 }
213
214 /* Now the real converting starts... */
215 static unsigned int KX_Mcol2uint_new(MCol col)
216 {
217         /* color has to be converted without endian sensitivity. So no shifting! */
218         union
219         {
220                 MCol col;
221                 unsigned int integer;
222                 unsigned char cp[4];
223         } out_color, in_color;
224
225         in_color.col = col;
226         out_color.cp[0] = in_color.cp[3]; // red
227         out_color.cp[1] = in_color.cp[2]; // green
228         out_color.cp[2] = in_color.cp[1]; // blue
229         out_color.cp[3] = in_color.cp[0]; // alpha
230         
231         return out_color.integer;
232 }
233
234 static void SetDefaultFaceType(Scene* scene)
235 {
236         default_face_mode = TF_DYNAMIC;
237         Scene *sce;
238         Base *base;
239
240         for(SETLOOPER(scene,base))
241         {
242                 if (base->object->type == OB_LAMP)
243                 {
244                         default_face_mode = TF_DYNAMIC|TF_LIGHT;
245                         return;
246                 }
247         }
248 }
249
250
251 // --
252 static void GetRGB(short type,
253         MFace* mface,
254         MCol* mmcol,
255         Material *mat,
256         unsigned int &c0, 
257         unsigned int &c1, 
258         unsigned int &c2, 
259         unsigned int &c3)
260 {
261         unsigned int color = 0xFFFFFFFFL;
262         switch(type)
263         {
264                 case 0: // vertex colors
265                 {
266                         if(mmcol) {
267                                 c0 = KX_Mcol2uint_new(mmcol[0]);
268                                 c1 = KX_Mcol2uint_new(mmcol[1]);
269                                 c2 = KX_Mcol2uint_new(mmcol[2]);
270                                 if (mface->v4)
271                                         c3 = KX_Mcol2uint_new(mmcol[3]);
272                         }else // backup white
273                         {
274                                 c0 = KX_rgbaint2uint_new(color);
275                                 c1 = KX_rgbaint2uint_new(color);
276                                 c2 = KX_rgbaint2uint_new(color);        
277                                 if (mface->v4)
278                                         c3 = KX_rgbaint2uint_new( color );
279                         }
280                 } break;
281                 
282         
283                 case 1: // material rgba
284                 {
285                         if (mat) {
286                                 union {
287                                         unsigned char cp[4];
288                                         unsigned int integer;
289                                 } col_converter;
290                                 col_converter.cp[3] = (unsigned char) (mat->r*255.0);
291                                 col_converter.cp[2] = (unsigned char) (mat->g*255.0);
292                                 col_converter.cp[1] = (unsigned char) (mat->b*255.0);
293                                 col_converter.cp[0] = (unsigned char) (mat->alpha*255.0);
294                                 color = col_converter.integer;
295                         }
296                         c0 = KX_rgbaint2uint_new(color);
297                         c1 = KX_rgbaint2uint_new(color);
298                         c2 = KX_rgbaint2uint_new(color);        
299                         if (mface->v4)
300                                 c3 = KX_rgbaint2uint_new(color);
301                 } break;
302                 
303                 default: // white
304                 {
305                         c0 = KX_rgbaint2uint_new(color);
306                         c1 = KX_rgbaint2uint_new(color);
307                         c2 = KX_rgbaint2uint_new(color);        
308                         if (mface->v4)
309                                 c3 = KX_rgbaint2uint_new(color);
310                 } break;
311         }
312 }
313
314 typedef struct MTF_localLayer
315 {
316         MTFace *face;
317         const char *name;
318 }MTF_localLayer;
319
320 // ------------------------------------
321 bool ConvertMaterial(
322         BL_Material *material,
323         Material *mat, 
324         MTFace* tface,  
325         const char *tfaceName,
326         MFace* mface, 
327         MCol* mmcol, 
328         int lightlayer, 
329         Object* blenderobj,
330         MTF_localLayer *layers,
331         bool glslmat)
332 {
333         material->Initialize();
334         int numchan =   -1, texalpha = 0;
335         bool validmat   = (mat!=0);
336         bool validface  = (tface!=0);
337         
338         short type = 0;
339         if( validmat )
340                 type = 1; // material color 
341         
342         material->IdMode = DEFAULT_BLENDER;
343         material->glslmat = (validmat)? glslmat: false;
344         material->materialindex = mface->mat_nr;
345
346         // --------------------------------
347         if(validmat) {
348
349                 // use vertex colors by explicitly setting
350                 if(mat->mode &MA_VERTEXCOLP || glslmat)
351                         type = 0;
352
353                 // use lighting?
354                 material->ras_mode |= ( mat->mode & MA_SHLESS )?0:USE_LIGHT;
355                 MTex *mttmp = 0;
356                 numchan = getNumTexChannels(mat);
357                 int valid_index = 0;
358                 
359                 // use the face texture if
360                 // 1) it is set in the buttons
361                 // 2) we have a face texture and a material but no valid texture in slot 1
362                 bool facetex = false;
363                 if(validface && mat->mode &MA_FACETEXTURE) 
364                         facetex = true;
365                 if(validface && !mat->mtex[0])
366                         facetex = true;
367                 if(validface && mat->mtex[0]) {
368                         MTex *tmp = mat->mtex[0];
369                         if(!tmp->tex || (tmp->tex && !tmp->tex->ima))
370                                 facetex = true;
371                 }
372                 numchan = numchan>MAXTEX?MAXTEX:numchan;
373         
374                 // foreach MTex
375                 for(int i=0; i<numchan; i++) {
376                         // use face tex
377
378                         if(i==0 && facetex ) {
379                                 Image*tmp = (Image*)(tface->tpage);
380
381                                 if(tmp) {
382                                         material->img[i] = tmp;
383                                         material->texname[i] = material->img[i]->id.name;
384                                         material->flag[i] |= ( tface->transp  &TF_ALPHA )?USEALPHA:0;
385                                         material->flag[i] |= ( tface->transp  &TF_ADD   )?CALCALPHA:0;
386                                         material->flag[i] |= MIPMAP;
387
388                                         if(material->img[i]->flag & IMA_REFLECT)
389                                                 material->mapping[i].mapping |= USEREFL;
390                                         else
391                                         {
392                                                 mttmp = getImageFromMaterial( mat, i );
393                                                 if(mttmp && mttmp->texco &TEXCO_UV)
394                                                 {
395                                                         STR_String uvName = mttmp->uvname;
396
397                                                         if (!uvName.IsEmpty())
398                                                                 material->mapping[i].uvCoName = mttmp->uvname;
399                                                         else
400                                                                 material->mapping[i].uvCoName = "";
401                                                 }
402                                                 material->mapping[i].mapping |= USEUV;
403                                         }
404
405                                         if(material->ras_mode & USE_LIGHT)
406                                                 material->ras_mode &= ~USE_LIGHT;
407                                         if(tface->mode & TF_LIGHT)
408                                                 material->ras_mode |= USE_LIGHT;
409
410                                         valid_index++;
411                                 }
412                                 else {
413                                         material->img[i] = 0;
414                                         material->texname[i] = "";
415                                 }
416                                 continue;
417                         }
418
419                         mttmp = getImageFromMaterial( mat, i );
420                         if( mttmp ) {
421                                 if( mttmp->tex ) {
422                                         if( mttmp->tex->type == TEX_IMAGE ) {
423                                                 material->mtexname[i] = mttmp->tex->id.name;
424                                                 material->img[i] = mttmp->tex->ima;
425                                                 if( material->img[i] ) {
426
427                                                         material->texname[i] = material->img[i]->id.name;
428                                                         material->flag[i] |= ( mttmp->tex->imaflag &TEX_MIPMAP )?MIPMAP:0;
429                                                         // -----------------------
430                                                         if( mttmp->tex->imaflag &TEX_USEALPHA ) {
431                                                                 material->flag[i]       |= USEALPHA;
432                                                         }
433                                                         // -----------------------
434                                                         else if( mttmp->tex->imaflag &TEX_CALCALPHA ) {
435                                                                 material->flag[i]       |= CALCALPHA;
436                                                         }
437                                                         else if(mttmp->tex->flag &TEX_NEGALPHA) {
438                                                                 material->flag[i]       |= USENEGALPHA;
439                                                         }
440
441                                                         material->color_blend[i] = mttmp->colfac;
442                                                         material->flag[i] |= ( mttmp->mapto  & MAP_ALPHA                )?TEXALPHA:0;
443                                                         material->flag[i] |= ( mttmp->texflag& MTEX_NEGATIVE    )?TEXNEG:0;
444
445                                                         if(!glslmat && (material->flag[i] & TEXALPHA))
446                                                                 texalpha = 1;
447                                                 }
448                                         }
449                                         else if(mttmp->tex->type == TEX_ENVMAP) {
450                                                 if( mttmp->tex->env->stype == ENV_LOAD ) {
451                                         
452                                                         material->mtexname[i]     = mttmp->tex->id.name;
453                                                         EnvMap *env = mttmp->tex->env;
454                                                         env->ima = mttmp->tex->ima;
455                                                         material->cubemap[i] = env;
456
457                                                         if (material->cubemap[i])
458                                                         {
459                                                                 if (!material->cubemap[i]->cube[0])
460                                                                         BL_Texture::SplitEnvMap(material->cubemap[i]);
461
462                                                                 material->texname[i]= material->cubemap[i]->ima->id.name;
463                                                                 material->mapping[i].mapping |= USEENV;
464                                                         }
465                                                 }
466                                         }
467                                         material->flag[i] |= (mat->ipo!=0)?HASIPO:0;
468                                         /// --------------------------------
469                                         // mapping methods
470                                         material->mapping[i].mapping |= ( mttmp->texco  & TEXCO_REFL    )?USEREFL:0;
471                                         
472                                         if(mttmp->texco & TEXCO_OBJECT) {
473                                                 material->mapping[i].mapping |= USEOBJ;
474                                                 if(mttmp->object)
475                                                         material->mapping[i].objconame = mttmp->object->id.name;
476                                         }
477                                         else if(mttmp->texco &TEXCO_REFL)
478                                                 material->mapping[i].mapping |= USEREFL;
479                                         else if(mttmp->texco &(TEXCO_ORCO|TEXCO_GLOB))
480                                                 material->mapping[i].mapping |= USEORCO;
481                                         else if(mttmp->texco &TEXCO_UV)
482                                         {
483                                                 STR_String uvName = mttmp->uvname;
484
485                                                 if (!uvName.IsEmpty())
486                                                         material->mapping[i].uvCoName = mttmp->uvname;
487                                                 else
488                                                         material->mapping[i].uvCoName = "";
489                                                 material->mapping[i].mapping |= USEUV;
490                                         }
491                                         else if(mttmp->texco &TEXCO_NORM)
492                                                 material->mapping[i].mapping |= USENORM;
493                                         else if(mttmp->texco &TEXCO_TANGENT)
494                                                 material->mapping[i].mapping |= USETANG;
495                                         else
496                                                 material->mapping[i].mapping |= DISABLE;
497                                         
498                                         material->mapping[i].scale[0] = mttmp->size[0];
499                                         material->mapping[i].scale[1] = mttmp->size[1];
500                                         material->mapping[i].scale[2] = mttmp->size[2];
501                                         material->mapping[i].offsets[0] = mttmp->ofs[0];
502                                         material->mapping[i].offsets[1] = mttmp->ofs[1];
503                                         material->mapping[i].offsets[2] = mttmp->ofs[2];
504
505                                         material->mapping[i].projplane[0] = mttmp->projx;
506                                         material->mapping[i].projplane[1] = mttmp->projy;
507                                         material->mapping[i].projplane[2] = mttmp->projz;
508                                         /// --------------------------------
509                                         
510                                         switch( mttmp->blendtype ) {
511                                         case MTEX_BLEND:
512                                                 material->blend_mode[i] = BLEND_MIX;
513                                                 break;
514                                         case MTEX_MUL:
515                                                 material->blend_mode[i] = BLEND_MUL;
516                                                 break;
517                                         case MTEX_ADD:
518                                                 material->blend_mode[i] = BLEND_ADD;
519                                                 break;
520                                         case MTEX_SUB:
521                                                 material->blend_mode[i] = BLEND_SUB;
522                                                 break;
523                                         case MTEX_SCREEN:
524                                                 material->blend_mode[i] = BLEND_SCR;
525                                                 break;
526                                         }
527                                         valid_index++;
528                                 }
529                         }
530                 }
531
532                 // above one tex the switches here
533                 // are not used
534                 switch(valid_index) {
535                 case 0:
536                         material->IdMode = DEFAULT_BLENDER;
537                         break;
538                 case 1:
539                         material->IdMode = ONETEX;
540                         break;
541                 default:
542                         material->IdMode = GREATERTHAN2;
543                         break;
544                 }
545                 material->SetUsers(mat->id.us);
546
547                 material->num_enabled = valid_index;
548
549                 material->speccolor[0]  = mat->specr;
550                 material->speccolor[1]  = mat->specg;
551                 material->speccolor[2]  = mat->specb;
552                 material->hard                  = (float)mat->har/4.0f;
553                 material->matcolor[0]   = mat->r;
554                 material->matcolor[1]   = mat->g;
555                 material->matcolor[2]   = mat->b;
556                 material->matcolor[3]   = mat->alpha;
557                 material->alpha                 = mat->alpha;
558                 material->emit                  = mat->emit;
559                 material->spec_f                = mat->spec;
560                 material->ref                   = mat->ref;
561                 material->amb                   = mat->amb;
562
563                 material->ras_mode |= (mat->mode & MA_WIRE)? WIRE: 0;
564         }
565         else {
566                 int valid = 0;
567
568                 // check for tface tex to fallback on
569                 if( validface ){
570
571                         // no light bugfix
572                         if(tface->mode) material->ras_mode |= USE_LIGHT;
573
574                         material->img[0] = (Image*)(tface->tpage);
575                         // ------------------------
576                         if(material->img[0]) {
577                                 material->texname[0] = material->img[0]->id.name;
578                                 material->mapping[0].mapping |= ( (material->img[0]->flag & IMA_REFLECT)!=0 )?USEREFL:0;
579                                 material->flag[0] |= ( tface->transp  &TF_ALPHA )?USEALPHA:0;
580                                 material->flag[0] |= ( tface->transp  &TF_ADD   )?CALCALPHA:0;
581                                 valid++;
582                         }
583                 }
584                 material->SetUsers(-1);
585                 material->num_enabled   = valid;
586                 material->IdMode                = TEXFACE;
587                 material->speccolor[0]  = 1.f;
588                 material->speccolor[1]  = 1.f;
589                 material->speccolor[2]  = 1.f;
590                 material->hard                  = 35.f;
591                 material->matcolor[0]   = 0.5f;
592                 material->matcolor[1]   = 0.5f;
593                 material->matcolor[2]   = 0.5f;
594                 material->spec_f                = 0.5f;
595                 material->ref                   = 0.8f;
596         }
597         MT_Point2 uv[4];
598         MT_Point2 uv2[4];
599         const char *uvName = "", *uv2Name = "";
600
601         
602         uv2[0]= uv2[1]= uv2[2]= uv2[3]= MT_Point2(0.0f, 0.0f);
603
604         if( validface ) {
605
606                 material->ras_mode |= (tface->mode & TF_INVISIBLE)?0:POLY_VIS;
607
608                 material->transp = tface->transp;
609                 material->tile  = tface->tile;
610                 material->mode  = tface->mode;
611                         
612                 uv[0].setValue(tface->uv[0]);
613                 uv[1].setValue(tface->uv[1]);
614                 uv[2].setValue(tface->uv[2]);
615
616                 if (mface->v4) 
617                         uv[3].setValue(tface->uv[3]);
618
619                 uvName = tfaceName;
620         } 
621         else {
622                 // nothing at all
623                 material->ras_mode |= (POLY_VIS| (validmat?0:USE_LIGHT));
624                 material->mode          = default_face_mode;    
625                 material->transp        = TF_SOLID;
626                 material->tile          = 0;
627                 
628                 uv[0]= uv[1]= uv[2]= uv[3]= MT_Point2(0.0f, 0.0f);
629         }
630
631         // with ztransp enabled, enforce alpha blending mode
632         if(validmat && (mat->mode & MA_ZTRA) && (material->transp == TF_SOLID))
633                 material->transp = TF_ALPHA;
634
635         // always zsort alpha + add
636         if((material->transp == TF_ALPHA || material->transp == TF_ADD || texalpha) && (material->transp != TF_CLIP)) {
637                 material->ras_mode |= ALPHA;
638                 material->ras_mode |= (material->mode & TF_ALPHASORT)? ZSORT: 0;
639         }
640
641         // collider or not?
642         material->ras_mode |= (material->mode & TF_DYNAMIC)? COLLIDER: 0;
643
644         // these flags are irrelevant at this point, remove so they
645         // don't hurt material bucketing 
646         material->mode &= ~(TF_DYNAMIC|TF_ALPHASORT|TF_TEX);
647
648         // get uv sets
649         if(validmat) 
650         {
651                 bool isFirstSet = true;
652
653                 // only two sets implemented, but any of the eight 
654                 // sets can make up the two layers
655                 for (int vind = 0; vind<material->num_enabled; vind++)
656                 {
657                         BL_Mapping &map = material->mapping[vind];
658
659                         if (map.uvCoName.IsEmpty())
660                                 isFirstSet = false;
661                         else
662                         {
663                                 for (int lay=0; lay<MAX_MTFACE; lay++)
664                                 {
665                                         MTF_localLayer& layer = layers[lay];
666                                         if (layer.face == 0) break;
667
668                                         if (strcmp(map.uvCoName.ReadPtr(), layer.name)==0)
669                                         {
670                                                 MT_Point2 uvSet[4];
671
672                                                 uvSet[0].setValue(layer.face->uv[0]);
673                                                 uvSet[1].setValue(layer.face->uv[1]);
674                                                 uvSet[2].setValue(layer.face->uv[2]);
675
676                                                 if (mface->v4) 
677                                                         uvSet[3].setValue(layer.face->uv[3]);
678                                                 else
679                                                         uvSet[3].setValue(0.0f, 0.0f);
680
681                                                 if (isFirstSet)
682                                                 {
683                                                         uv[0] = uvSet[0]; uv[1] = uvSet[1];
684                                                         uv[2] = uvSet[2]; uv[3] = uvSet[3];
685                                                         isFirstSet = false;
686                                                         uvName = layer.name;
687                                                 }
688                                                 else if(strcmp(layer.name, uvName) != 0)
689                                                 {
690                                                         uv2[0] = uvSet[0]; uv2[1] = uvSet[1];
691                                                         uv2[2] = uvSet[2]; uv2[3] = uvSet[3];
692                                                         map.mapping |= USECUSTOMUV;
693                                                         uv2Name = layer.name;
694                                                 }
695                                         }
696                                 }
697                         }
698                 }
699         }
700
701         unsigned int rgb[4];
702         GetRGB(type,mface,mmcol,mat,rgb[0],rgb[1],rgb[2], rgb[3]);
703
704         // swap the material color, so MCol on TF_BMFONT works
705         if (validmat && type==1 && (tface && tface->mode & TF_BMFONT))
706         {
707                 rgb[0] = KX_rgbaint2uint_new(rgb[0]);
708                 rgb[1] = KX_rgbaint2uint_new(rgb[1]);
709                 rgb[2] = KX_rgbaint2uint_new(rgb[2]);
710                 rgb[3] = KX_rgbaint2uint_new(rgb[3]);
711         }
712
713         material->SetConversionRGB(rgb);
714         material->SetConversionUV(uvName, uv);
715         material->SetConversionUV2(uv2Name, uv2);
716
717         if(validmat)
718                 material->matname       =(mat->id.name);
719
720         material->tface         = tface;
721         material->material      = mat;
722         return true;
723 }
724
725
726 RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools* rendertools, KX_Scene* scene, KX_BlenderSceneConverter *converter)
727 {
728         RAS_MeshObject *meshobj;
729         bool skinMesh = false;
730         int lightlayer = blenderobj->lay;
731
732         if ((meshobj = converter->FindGameMesh(mesh/*, ob->lay*/)) != NULL)
733                 return meshobj;
734         // Get DerivedMesh data
735         DerivedMesh *dm = CDDM_from_mesh(mesh, blenderobj);
736
737         MVert *mvert = dm->getVertArray(dm);
738         int totvert = dm->getNumVerts(dm);
739
740         MFace *mface = dm->getFaceArray(dm);
741         MTFace *tface = static_cast<MTFace*>(dm->getFaceDataArray(dm, CD_MTFACE));
742         MCol *mcol = static_cast<MCol*>(dm->getFaceDataArray(dm, CD_MCOL));
743         float (*tangent)[3] = NULL;
744         int totface = dm->getNumFaces(dm);
745         const char *tfaceName = "";
746
747         if(tface) {
748                 DM_add_tangent_layer(dm);
749                 tangent = (float(*)[3])dm->getFaceDataArray(dm, CD_TANGENT);
750         }
751
752         // Determine if we need to make a skinned mesh
753         if (mesh->dvert || mesh->key || ((blenderobj->gameflag & OB_SOFT_BODY) != 0) || BL_ModifierDeformer::HasCompatibleDeformer(blenderobj))
754         {
755                 meshobj = new BL_SkinMeshObject(mesh, lightlayer);
756                 skinMesh = true;
757         }
758         else
759                 meshobj = new RAS_MeshObject(mesh, lightlayer);
760
761         // Extract avaiable layers
762         MTF_localLayer *layers =  new MTF_localLayer[MAX_MTFACE];
763         for (int lay=0; lay<MAX_MTFACE; lay++) {
764                 layers[lay].face = 0;
765                 layers[lay].name = "";
766         }
767
768         int validLayers = 0;
769         for (int i=0; i<dm->faceData.totlayer; i++)
770         {
771                 if (dm->faceData.layers[i].type == CD_MTFACE)
772                 {
773                         assert(validLayers <= 8);
774
775                         layers[validLayers].face = (MTFace*)(dm->faceData.layers[i].data);
776                         layers[validLayers].name = dm->faceData.layers[i].name;
777                         if(tface == layers[validLayers].face)
778                                 tfaceName = layers[validLayers].name;
779                         validLayers++;
780                 }
781         }
782
783         meshobj->SetName(mesh->id.name);
784         meshobj->m_sharedvertex_map.resize(totvert);
785         RAS_IPolyMaterial* polymat = NULL;
786         STR_String imastr;
787         // These pointers will hold persistent material structure during the conversion
788         // to avoid countless allocation/deallocation of memory.
789         BL_Material* bl_mat = NULL;
790         KX_BlenderMaterial* kx_blmat = NULL;
791         KX_PolygonMaterial* kx_polymat = NULL;
792
793         for (int f=0;f<totface;f++,mface++)
794         {
795                 Material* ma = 0;
796                 bool collider = true;
797                 MT_Point2 uv0(0.0,0.0),uv1(0.0,0.0),uv2(0.0,0.0),uv3(0.0,0.0);
798                 MT_Point2 uv20(0.0,0.0),uv21(0.0,0.0),uv22(0.0,0.0),uv23(0.0,0.0);
799                 unsigned int rgb0,rgb1,rgb2,rgb3 = 0;
800
801                 MT_Point3 pt0, pt1, pt2, pt3;
802                 MT_Vector3 no0(0,0,0), no1(0,0,0), no2(0,0,0), no3(0,0,0);
803                 MT_Vector4 tan0(0,0,0,0), tan1(0,0,0,0), tan2(0,0,0,0), tan3(0,0,0,0);
804
805                 /* get coordinates, normals and tangents */
806                 pt0.setValue(mvert[mface->v1].co);
807                 pt1.setValue(mvert[mface->v2].co);
808                 pt2.setValue(mvert[mface->v3].co);
809                 if (mface->v4) pt3.setValue(mvert[mface->v4].co);
810
811                 if(mface->flag & ME_SMOOTH) {
812                         float n0[3], n1[3], n2[3], n3[3];
813
814                         NormalShortToFloat(n0, mvert[mface->v1].no);
815                         NormalShortToFloat(n1, mvert[mface->v2].no);
816                         NormalShortToFloat(n2, mvert[mface->v3].no);
817                         no0 = n0;
818                         no1 = n1;
819                         no2 = n2;
820
821                         if(mface->v4) {
822                                 NormalShortToFloat(n3, mvert[mface->v4].no);
823                                 no3 = n3;
824                         }
825                 }
826                 else {
827                         float fno[3];
828
829                         if(mface->v4)
830                                 CalcNormFloat4(mvert[mface->v1].co, mvert[mface->v2].co,
831                                         mvert[mface->v3].co, mvert[mface->v4].co, fno);
832                         else
833                                 CalcNormFloat(mvert[mface->v1].co, mvert[mface->v2].co,
834                                         mvert[mface->v3].co, fno);
835
836                         no0 = no1 = no2 = no3 = MT_Vector3(fno);
837                 }
838
839                 if(tangent) {
840                         tan0 = tangent[f*4 + 0];
841                         tan1 = tangent[f*4 + 1];
842                         tan2 = tangent[f*4 + 2];
843
844                         if (mface->v4)
845                                 tan3 = tangent[f*4 + 3];
846                 }
847
848                 /* get material */
849                 ma = give_current_material(blenderobj, mface->mat_nr+1);
850         
851                 {
852                         bool visible = true;
853                         bool twoside = false;
854
855                         if(converter->GetMaterials()) {
856                                 /* do Blender Multitexture and Blender GLSL materials */
857                                 unsigned int rgb[4];
858                                 MT_Point2 uv[4];
859
860                                 /* first is the BL_Material */
861                                 if (!bl_mat)
862                                         bl_mat = new BL_Material();
863                                 ConvertMaterial(bl_mat, ma, tface, tfaceName, mface, mcol,
864                                         lightlayer, blenderobj, layers, converter->GetGLSLMaterials());
865
866                                 visible = ((bl_mat->ras_mode & POLY_VIS)!=0);
867                                 collider = ((bl_mat->ras_mode & COLLIDER)!=0);
868                                 twoside = ((bl_mat->mode & TF_TWOSIDE)!=0);
869
870                                 /* vertex colors and uv's were stored in bl_mat temporarily */
871                                 bl_mat->GetConversionRGB(rgb);
872                                 rgb0 = rgb[0]; rgb1 = rgb[1];
873                                 rgb2 = rgb[2]; rgb3 = rgb[3];
874
875                                 bl_mat->GetConversionUV(uv);
876                                 uv0 = uv[0]; uv1 = uv[1];
877                                 uv2 = uv[2]; uv3 = uv[3];
878
879                                 bl_mat->GetConversionUV2(uv);
880                                 uv20 = uv[0]; uv21 = uv[1];
881                                 uv22 = uv[2]; uv23 = uv[3];
882                                 
883                                 /* then the KX_BlenderMaterial */
884                                 if (kx_blmat == NULL)
885                                         kx_blmat = new KX_BlenderMaterial();
886
887                                 kx_blmat->Initialize(scene, bl_mat, skinMesh, lightlayer);
888                                 polymat = static_cast<RAS_IPolyMaterial*>(kx_blmat);
889                         }
890                         else {
891                                 /* do Texture Face materials */
892                                 Image* bima = (tface)? (Image*)tface->tpage: NULL;
893                                 imastr =  (tface)? (bima? (bima)->id.name : "" ) : "";
894                 
895                                 char transp=0;
896                                 short mode=0, tile=0;
897                                 int     tilexrep=4,tileyrep = 4;
898                                 
899                                 if (bima) {
900                                         tilexrep = bima->xrep;
901                                         tileyrep = bima->yrep;
902                                 }
903
904                                 /* get tface properties if available */
905                                 if(tface) {
906                                         /* TF_DYNAMIC means the polygon is a collision face */
907                                         collider = ((tface->mode & TF_DYNAMIC) != 0);
908                                         transp = tface->transp;
909                                         tile = tface->tile;
910                                         mode = tface->mode;
911                                         
912                                         visible = !(tface->mode & TF_INVISIBLE);
913                                         twoside = ((tface->mode & TF_TWOSIDE)!=0);
914                                         
915                                         uv0.setValue(tface->uv[0]);
916                                         uv1.setValue(tface->uv[1]);
917                                         uv2.setValue(tface->uv[2]);
918         
919                                         if (mface->v4)
920                                                 uv3.setValue(tface->uv[3]);
921                                 } 
922                                 else {
923                                         /* no texfaces, set COLLSION true and everything else FALSE */
924                                         mode = default_face_mode;       
925                                         transp = TF_SOLID;
926                                         tile = 0;
927                                 }
928
929                                 /* get vertex colors */
930                                 if (mcol) {
931                                         /* we have vertex colors */
932                                         rgb0 = KX_Mcol2uint_new(mcol[0]);
933                                         rgb1 = KX_Mcol2uint_new(mcol[1]);
934                                         rgb2 = KX_Mcol2uint_new(mcol[2]);
935                                         
936                                         if (mface->v4)
937                                                 rgb3 = KX_Mcol2uint_new(mcol[3]);
938                                 }
939                                 else {
940                                         /* no vertex colors, take from material, otherwise white */
941                                         unsigned int color = 0xFFFFFFFFL;
942
943                                         if (ma)
944                                         {
945                                                 union
946                                                 {
947                                                         unsigned char cp[4];
948                                                         unsigned int integer;
949                                                 } col_converter;
950                                                 
951                                                 col_converter.cp[3] = (unsigned char) (ma->r*255.0);
952                                                 col_converter.cp[2] = (unsigned char) (ma->g*255.0);
953                                                 col_converter.cp[1] = (unsigned char) (ma->b*255.0);
954                                                 col_converter.cp[0] = (unsigned char) (ma->alpha*255.0);
955                                                 
956                                                 color = col_converter.integer;
957                                         }
958
959                                         rgb0 = KX_rgbaint2uint_new(color);
960                                         rgb1 = KX_rgbaint2uint_new(color);
961                                         rgb2 = KX_rgbaint2uint_new(color);      
962                                         
963                                         if (mface->v4)
964                                                 rgb3 = KX_rgbaint2uint_new(color);
965                                 }
966                                 
967                                 // only zsort alpha + add
968                                 bool alpha = (transp == TF_ALPHA || transp == TF_ADD);
969                                 bool zsort = (mode & TF_ALPHASORT)? alpha: 0;
970
971                                 if (kx_polymat == NULL)
972                                         kx_polymat = new KX_PolygonMaterial();
973                                 kx_polymat->Initialize(imastr, ma, (int)mface->mat_nr,
974                                         tile, tilexrep, tileyrep, 
975                                         mode, transp, alpha, zsort, lightlayer, tface, (unsigned int*)mcol);
976                                 polymat = static_cast<RAS_IPolyMaterial*>(kx_polymat);
977         
978                                 if (ma) {
979                                         polymat->m_specular = MT_Vector3(ma->specr, ma->specg, ma->specb)*ma->spec;
980                                         polymat->m_shininess = (float)ma->har/4.0; // 0 < ma->har <= 512
981                                         polymat->m_diffuse = MT_Vector3(ma->r, ma->g, ma->b)*(ma->emit + ma->ref);
982                                 }
983                                 else {
984                                         polymat->m_specular.setValue(0.0f,0.0f,0.0f);
985                                         polymat->m_shininess = 35.0;
986                                 }
987                         }
988
989                         /* mark face as flat, so vertices are split */
990                         bool flat = (mface->flag & ME_SMOOTH) == 0;
991
992                         // see if a bucket was reused or a new one was created
993                         // this way only one KX_BlenderMaterial object has to exist per bucket
994                         bool bucketCreated; 
995                         RAS_MaterialBucket* bucket = scene->FindBucket(polymat, bucketCreated);
996                         if (bucketCreated) {
997                                 // this is needed to free up memory afterwards
998                                 converter->RegisterPolyMaterial(polymat);
999                                 if(converter->GetMaterials()) {
1000                                         converter->RegisterBlenderMaterial(bl_mat);
1001                                         // the poly material has been stored in the bucket, next time we must create a new one
1002                                         bl_mat = NULL;
1003                                         kx_blmat = NULL;
1004                                 } else {
1005                                         // the poly material has been stored in the bucket, next time we must create a new one
1006                                         kx_polymat = NULL;
1007                                 }
1008                         } else {
1009                                 // from now on, use the polygon material from the material bucket
1010                                 polymat = bucket->GetPolyMaterial();
1011                                 // keep the material pointers, they will be reused for next face
1012                         }
1013                                                  
1014                         int nverts = (mface->v4)? 4: 3;
1015                         RAS_Polygon *poly = meshobj->AddPolygon(bucket, nverts);
1016
1017                         poly->SetVisible(visible);
1018                         poly->SetCollider(collider);
1019                         poly->SetTwoside(twoside);
1020                         //poly->SetEdgeCode(mface->edcode);
1021
1022                         meshobj->AddVertex(poly,0,pt0,uv0,uv20,tan0,rgb0,no0,flat,mface->v1);
1023                         meshobj->AddVertex(poly,1,pt1,uv1,uv21,tan1,rgb1,no1,flat,mface->v2);
1024                         meshobj->AddVertex(poly,2,pt2,uv2,uv22,tan2,rgb2,no2,flat,mface->v3);
1025
1026                         if (nverts==4)
1027                                 meshobj->AddVertex(poly,3,pt3,uv3,uv23,tan3,rgb3,no3,flat,mface->v4);
1028                 }
1029
1030                 if (tface) 
1031                         tface++;
1032                 if (mcol)
1033                         mcol+=4;
1034
1035                 for (int lay=0; lay<MAX_MTFACE; lay++)
1036                 {
1037                         MTF_localLayer &layer = layers[lay];
1038                         if (layer.face == 0) break;
1039
1040                         layer.face++;
1041                 }
1042         }
1043         meshobj->m_sharedvertex_map.clear();
1044
1045         // pre calculate texture generation
1046         for(list<RAS_MeshMaterial>::iterator mit = meshobj->GetFirstMaterial();
1047                 mit != meshobj->GetLastMaterial(); ++ mit) {
1048                 mit->m_bucket->GetPolyMaterial()->OnConstruction(lightlayer);
1049         }
1050
1051         if (layers)
1052                 delete []layers;
1053         
1054         dm->release(dm);
1055         // cleanup material
1056         if (bl_mat)
1057                 delete bl_mat;
1058         if (kx_blmat)
1059                 delete kx_blmat;
1060         if (kx_polymat)
1061                 delete kx_polymat;
1062         converter->RegisterGameMesh(meshobj, mesh);
1063         return meshobj;
1064 }
1065
1066         
1067         
1068 static PHY_MaterialProps *CreateMaterialFromBlenderObject(struct Object* blenderobject,
1069                                                                                                   KX_Scene *kxscene)
1070 {
1071         PHY_MaterialProps *materialProps = new PHY_MaterialProps;
1072         
1073         MT_assert(materialProps && "Create physics material properties failed");
1074                 
1075         Material* blendermat = give_current_material(blenderobject, 0);
1076                 
1077         if (blendermat)
1078         {
1079                 MT_assert(0.0f <= blendermat->reflect && blendermat->reflect <= 1.0f);
1080         
1081                 materialProps->m_restitution = blendermat->reflect;
1082                 materialProps->m_friction = blendermat->friction;
1083                 materialProps->m_fh_spring = blendermat->fh;
1084                 materialProps->m_fh_damping = blendermat->xyfrict;
1085                 materialProps->m_fh_distance = blendermat->fhdist;
1086                 materialProps->m_fh_normal = (blendermat->dynamode & MA_FH_NOR) != 0;
1087         }
1088         else {
1089                 //give some defaults
1090                 materialProps->m_restitution = 0.f;
1091                 materialProps->m_friction = 0.5;
1092                 materialProps->m_fh_spring = 0.f;
1093                 materialProps->m_fh_damping = 0.f;
1094                 materialProps->m_fh_distance = 0.f;
1095                 materialProps->m_fh_normal = false;
1096
1097         }
1098         
1099         return materialProps;
1100 }
1101
1102 static PHY_ShapeProps *CreateShapePropsFromBlenderObject(struct Object* blenderobject,
1103                                                                                                  KX_Scene *kxscene)
1104 {
1105         PHY_ShapeProps *shapeProps = new PHY_ShapeProps;
1106         
1107         MT_assert(shapeProps);
1108                 
1109         shapeProps->m_mass = blenderobject->mass;
1110         
1111 //  This needs to be fixed in blender. For now, we use:
1112         
1113 // in Blender, inertia stands for the size value which is equivalent to
1114 // the sphere radius
1115         shapeProps->m_inertia = blenderobject->formfactor;
1116         
1117         MT_assert(0.0f <= blenderobject->damping && blenderobject->damping <= 1.0f);
1118         MT_assert(0.0f <= blenderobject->rdamping && blenderobject->rdamping <= 1.0f);
1119         
1120         shapeProps->m_lin_drag = 1.0 - blenderobject->damping;
1121         shapeProps->m_ang_drag = 1.0 - blenderobject->rdamping;
1122         
1123         shapeProps->m_friction_scaling[0] = blenderobject->anisotropicFriction[0]; 
1124         shapeProps->m_friction_scaling[1] = blenderobject->anisotropicFriction[1];
1125         shapeProps->m_friction_scaling[2] = blenderobject->anisotropicFriction[2];
1126         shapeProps->m_do_anisotropic = ((blenderobject->gameflag & OB_ANISOTROPIC_FRICTION) != 0);
1127         
1128         shapeProps->m_do_fh     = (blenderobject->gameflag & OB_DO_FH) != 0; 
1129         shapeProps->m_do_rot_fh = (blenderobject->gameflag & OB_ROT_FH) != 0;
1130         
1131 //      velocity clamping XXX
1132         shapeProps->m_clamp_vel_min = blenderobject->min_vel;
1133         shapeProps->m_clamp_vel_max = blenderobject->max_vel;
1134         
1135         return shapeProps;
1136 }
1137
1138         
1139         
1140         
1141                 
1142 //////////////////////////////////////////////////////////
1143         
1144
1145
1146 static float my_boundbox_mesh(Mesh *me, float *loc, float *size)
1147 {
1148         MVert *mvert;
1149         BoundBox *bb;
1150         MT_Point3 min, max;
1151         float mloc[3], msize[3];
1152         float radius=0.0f, vert_radius, *co;
1153         int a;
1154         
1155         if(me->bb==0) me->bb= (struct BoundBox *)MEM_callocN(sizeof(BoundBox), "boundbox");
1156         bb= me->bb;
1157         
1158         INIT_MINMAX(min, max);
1159
1160         if (!loc) loc= mloc;
1161         if (!size) size= msize;
1162         
1163         mvert= me->mvert;
1164         for(a=0; a<me->totvert; a++, mvert++) {
1165                 co= mvert->co;
1166                 
1167                 /* bounds */
1168                 DO_MINMAX(co, min, max);
1169                 
1170                 /* radius */
1171                 vert_radius= co[0]*co[0] + co[1]*co[1] + co[2]*co[2];
1172                 if (vert_radius > radius)
1173                         radius= vert_radius;
1174         }
1175                 
1176         if(me->totvert) {
1177                 loc[0]= (min[0]+max[0])/2.0;
1178                 loc[1]= (min[1]+max[1])/2.0;
1179                 loc[2]= (min[2]+max[2])/2.0;
1180                 
1181                 size[0]= (max[0]-min[0])/2.0;
1182                 size[1]= (max[1]-min[1])/2.0;
1183                 size[2]= (max[2]-min[2])/2.0;
1184         }
1185         else {
1186                 loc[0]= loc[1]= loc[2]= 0.0;
1187                 size[0]= size[1]= size[2]= 0.0;
1188         }
1189                 
1190         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= loc[0]-size[0];
1191         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= loc[0]+size[0];
1192                 
1193         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= loc[1]-size[1];
1194         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= loc[1]+size[1];
1195
1196         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= loc[2]-size[2];
1197         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= loc[2]+size[2];
1198
1199         return sqrt(radius);
1200 }
1201                 
1202
1203
1204
1205 static void my_tex_space_mesh(Mesh *me)
1206                 {
1207         KeyBlock *kb;
1208         float *fp, loc[3], size[3], min[3], max[3];
1209         int a;
1210
1211         my_boundbox_mesh(me, loc, size);
1212         
1213         if(me->texflag & AUTOSPACE) {
1214                 if(me->key) {
1215                         kb= me->key->refkey;
1216                         if (kb) {
1217         
1218                                 INIT_MINMAX(min, max);
1219                 
1220                                 fp= (float *)kb->data;
1221                                 for(a=0; a<kb->totelem; a++, fp+=3) {   
1222                                         DO_MINMAX(fp, min, max);
1223                                 }
1224                                 if(kb->totelem) {
1225                                         loc[0]= (min[0]+max[0])/2.0; loc[1]= (min[1]+max[1])/2.0; loc[2]= (min[2]+max[2])/2.0;
1226                                         size[0]= (max[0]-min[0])/2.0; size[1]= (max[1]-min[1])/2.0; size[2]= (max[2]-min[2])/2.0;
1227         } 
1228         else {
1229                                         loc[0]= loc[1]= loc[2]= 0.0;
1230                                         size[0]= size[1]= size[2]= 0.0;
1231                                 }
1232                                 
1233                         }
1234                                 }
1235         
1236                 VECCOPY(me->loc, loc);
1237                 VECCOPY(me->size, size);
1238                 me->rot[0]= me->rot[1]= me->rot[2]= 0.0;
1239         
1240                 if(me->size[0]==0.0) me->size[0]= 1.0;
1241                 else if(me->size[0]>0.0 && me->size[0]<0.00001) me->size[0]= 0.00001;
1242                 else if(me->size[0]<0.0 && me->size[0]> -0.00001) me->size[0]= -0.00001;
1243         
1244                 if(me->size[1]==0.0) me->size[1]= 1.0;
1245                 else if(me->size[1]>0.0 && me->size[1]<0.00001) me->size[1]= 0.00001;
1246                 else if(me->size[1]<0.0 && me->size[1]> -0.00001) me->size[1]= -0.00001;
1247                                                 
1248                 if(me->size[2]==0.0) me->size[2]= 1.0;
1249                 else if(me->size[2]>0.0 && me->size[2]<0.00001) me->size[2]= 0.00001;
1250                 else if(me->size[2]<0.0 && me->size[2]> -0.00001) me->size[2]= -0.00001;
1251         }
1252         
1253 }
1254
1255 static void my_get_local_bounds(Object *ob, DerivedMesh *dm, float *center, float *size)
1256 {
1257         BoundBox *bb= NULL;
1258         /* uses boundbox, function used by Ketsji */
1259         switch (ob->type)
1260         {
1261                 case OB_MESH:
1262                         if (dm)
1263                         {
1264                                 float min_r[3], max_r[3];
1265                                 INIT_MINMAX(min_r, max_r);
1266                                 dm->getMinMax(dm, min_r, max_r);
1267                                 size[0]= 0.5*fabs(max_r[0] - min_r[0]);
1268                                 size[1]= 0.5*fabs(max_r[1] - min_r[1]);
1269                                 size[2]= 0.5*fabs(max_r[2] - min_r[2]);
1270                                         
1271                                 center[0]= 0.5*(max_r[0] + min_r[0]);
1272                                 center[1]= 0.5*(max_r[1] + min_r[1]);
1273                                 center[2]= 0.5*(max_r[2] + min_r[2]);
1274                                 return;
1275                         } else
1276                         {
1277                                 bb= ( (Mesh *)ob->data )->bb;
1278                                 if(bb==0) 
1279                                 {
1280                                         my_tex_space_mesh((struct Mesh *)ob->data);
1281                                         bb= ( (Mesh *)ob->data )->bb;
1282                                 }
1283                         }
1284                         break;
1285                 case OB_CURVE:
1286                 case OB_SURF:
1287                 case OB_FONT:
1288                         center[0]= center[1]= center[2]= 0.0;
1289                         size[0]  = size[1]=size[2]=0.0;
1290                         break;
1291                 case OB_MBALL:
1292                         bb= ob->bb;
1293                         break;
1294         }
1295         
1296         if(bb==NULL) 
1297         {
1298                 center[0]= center[1]= center[2]= 0.0;
1299                 size[0] = size[1]=size[2]=1.0;
1300         }
1301         else 
1302         {
1303                 size[0]= 0.5*fabs(bb->vec[0][0] - bb->vec[4][0]);
1304                 size[1]= 0.5*fabs(bb->vec[0][1] - bb->vec[2][1]);
1305                 size[2]= 0.5*fabs(bb->vec[0][2] - bb->vec[1][2]);
1306                                         
1307                 center[0]= 0.5*(bb->vec[0][0] + bb->vec[4][0]);
1308                 center[1]= 0.5*(bb->vec[0][1] + bb->vec[2][1]);
1309                 center[2]= 0.5*(bb->vec[0][2] + bb->vec[1][2]);
1310         }
1311 }
1312         
1313
1314
1315
1316 //////////////////////////////////////////////////////
1317
1318
1319 void BL_CreateGraphicObjectNew(KX_GameObject* gameobj,
1320                                                            const MT_Point3& localAabbMin,
1321                                                            const MT_Point3& localAabbMax,
1322                                                            KX_Scene* kxscene,
1323                                                            bool isActive,
1324                                                            e_PhysicsEngine physics_engine)
1325 {
1326         if (gameobj->GetMeshCount() > 0) 
1327         {
1328                 switch (physics_engine)
1329                 {
1330 #ifdef USE_BULLET
1331                 case UseBullet:
1332                         {
1333                                 CcdPhysicsEnvironment* env = (CcdPhysicsEnvironment*)kxscene->GetPhysicsEnvironment();
1334                                 assert(env);
1335                                 PHY_IMotionState* motionstate = new KX_MotionState(gameobj->GetSGNode());
1336                                 CcdGraphicController* ctrl = new CcdGraphicController(env, motionstate);
1337                                 gameobj->SetGraphicController(ctrl);
1338                                 ctrl->setNewClientInfo(gameobj->getClientInfo());
1339                                 ctrl->setLocalAabb(localAabbMin, localAabbMax);
1340                                 if (isActive) {
1341                                         // add first, this will create the proxy handle, only if the object is visible
1342                                         if (gameobj->GetVisible())
1343                                                 env->addCcdGraphicController(ctrl);
1344                                         // update the mesh if there is a deformer, this will also update the bounding box for modifiers
1345                                         RAS_Deformer* deformer = gameobj->GetDeformer();
1346                                         if (deformer)
1347                                                 deformer->UpdateBuckets();
1348                                 }
1349                         }
1350                         break;
1351 #endif
1352                 default:
1353                         break;
1354                 }
1355         }
1356 }
1357
1358 void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
1359                                                  struct Object* blenderobject,
1360                                                  RAS_MeshObject* meshobj,
1361                                                  KX_Scene* kxscene,
1362                                                  int activeLayerBitInfo,
1363                                                  e_PhysicsEngine        physics_engine,
1364                                                  KX_BlenderSceneConverter *converter,
1365                                                  bool processCompoundChildren
1366                                                  )
1367                                         
1368 {
1369         //SYS_SystemHandle syshandle = SYS_GetSystem(); /*unused*/
1370         //int userigidbody = SYS_GetCommandLineInt(syshandle,"norigidbody",0);
1371         //bool bRigidBody = (userigidbody == 0);
1372
1373         // object has physics representation?
1374         if (!(blenderobject->gameflag & OB_COLLISION))
1375                 return;
1376
1377         // get Root Parent of blenderobject
1378         struct Object* parent= blenderobject->parent;
1379         while(parent && parent->parent) {
1380                 parent= parent->parent;
1381         }
1382
1383         bool isCompoundChild = false;
1384         bool hasCompoundChildren = !parent && (blenderobject->gameflag & OB_CHILD);
1385
1386         if (parent/* && (parent->gameflag & OB_DYNAMIC)*/) {
1387                 
1388                 if ((parent->gameflag & OB_CHILD) != 0 && (blenderobject->gameflag & OB_CHILD))
1389                 {
1390                         isCompoundChild = true;
1391                 } 
1392         }
1393         if (processCompoundChildren != isCompoundChild)
1394                 return;
1395
1396
1397         PHY_ShapeProps* shapeprops =
1398                         CreateShapePropsFromBlenderObject(blenderobject, 
1399                         kxscene);
1400
1401         
1402         PHY_MaterialProps* smmaterial = 
1403                 CreateMaterialFromBlenderObject(blenderobject, kxscene);
1404                                         
1405         KX_ObjectProperties objprop;
1406         objprop.m_lockXaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_X_AXIS) !=0;
1407         objprop.m_lockYaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Y_AXIS) !=0;
1408         objprop.m_lockZaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Z_AXIS) !=0;
1409         objprop.m_lockXRotaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_X_ROT_AXIS) !=0;
1410         objprop.m_lockYRotaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Y_ROT_AXIS) !=0;
1411         objprop.m_lockZRotaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Z_ROT_AXIS) !=0;
1412
1413         objprop.m_isCompoundChild = isCompoundChild;
1414         objprop.m_hasCompoundChildren = hasCompoundChildren;
1415         objprop.m_margin = blenderobject->margin;
1416         
1417         // ACTOR is now a separate feature
1418         objprop.m_isactor = (blenderobject->gameflag & OB_ACTOR)!=0;
1419         objprop.m_dyna = (blenderobject->gameflag & OB_DYNAMIC) != 0;
1420         objprop.m_softbody = (blenderobject->gameflag & OB_SOFT_BODY) != 0;
1421         objprop.m_angular_rigidbody = (blenderobject->gameflag & OB_RIGID_BODY) != 0;
1422         
1423         ///contact processing threshold is only for rigid bodies and static geometry, not 'dynamic'
1424         if (objprop.m_angular_rigidbody || !objprop.m_dyna )
1425         {
1426                 objprop.m_contactProcessingThreshold = blenderobject->m_contactProcessingThreshold;
1427         } else
1428         {
1429                 objprop.m_contactProcessingThreshold = 0.f;
1430         }
1431
1432         objprop.m_sensor = (blenderobject->gameflag & OB_SENSOR) != 0;
1433         
1434         if (objprop.m_softbody)
1435         {
1436                 ///for game soft bodies
1437                 if (blenderobject->bsoft)
1438                 {
1439                         objprop.m_gamesoftFlag = blenderobject->bsoft->flag;
1440                                         ///////////////////
1441                         objprop.m_soft_linStiff = blenderobject->bsoft->linStiff;
1442                         objprop.m_soft_angStiff = blenderobject->bsoft->angStiff;               /* angular stiffness 0..1 */
1443                         objprop.m_soft_volume= blenderobject->bsoft->volume;                    /* volume preservation 0..1 */
1444
1445                         objprop.m_soft_viterations= blenderobject->bsoft->viterations;          /* Velocities solver iterations */
1446                         objprop.m_soft_piterations= blenderobject->bsoft->piterations;          /* Positions solver iterations */
1447                         objprop.m_soft_diterations= blenderobject->bsoft->diterations;          /* Drift solver iterations */
1448                         objprop.m_soft_citerations= blenderobject->bsoft->citerations;          /* Cluster solver iterations */
1449
1450                         objprop.m_soft_kSRHR_CL= blenderobject->bsoft->kSRHR_CL;                /* Soft vs rigid hardness [0,1] (cluster only) */
1451                         objprop.m_soft_kSKHR_CL= blenderobject->bsoft->kSKHR_CL;                /* Soft vs kinetic hardness [0,1] (cluster only) */
1452                         objprop.m_soft_kSSHR_CL= blenderobject->bsoft->kSSHR_CL;                /* Soft vs soft hardness [0,1] (cluster only) */
1453                         objprop.m_soft_kSR_SPLT_CL= blenderobject->bsoft->kSR_SPLT_CL;  /* Soft vs rigid impulse split [0,1] (cluster only) */
1454
1455                         objprop.m_soft_kSK_SPLT_CL= blenderobject->bsoft->kSK_SPLT_CL;  /* Soft vs rigid impulse split [0,1] (cluster only) */
1456                         objprop.m_soft_kSS_SPLT_CL= blenderobject->bsoft->kSS_SPLT_CL;  /* Soft vs rigid impulse split [0,1] (cluster only) */
1457                         objprop.m_soft_kVCF= blenderobject->bsoft->kVCF;                        /* Velocities correction factor (Baumgarte) */
1458                         objprop.m_soft_kDP= blenderobject->bsoft->kDP;                  /* Damping coefficient [0,1] */
1459
1460                         objprop.m_soft_kDG= blenderobject->bsoft->kDG;                  /* Drag coefficient [0,+inf] */
1461                         objprop.m_soft_kLF= blenderobject->bsoft->kLF;                  /* Lift coefficient [0,+inf] */
1462                         objprop.m_soft_kPR= blenderobject->bsoft->kPR;                  /* Pressure coefficient [-inf,+inf] */
1463                         objprop.m_soft_kVC= blenderobject->bsoft->kVC;                  /* Volume conversation coefficient [0,+inf] */
1464
1465                         objprop.m_soft_kDF= blenderobject->bsoft->kDF;                  /* Dynamic friction coefficient [0,1] */
1466                         objprop.m_soft_kMT= blenderobject->bsoft->kMT;                  /* Pose matching coefficient [0,1] */
1467                         objprop.m_soft_kCHR= blenderobject->bsoft->kCHR;                        /* Rigid contacts hardness [0,1] */
1468                         objprop.m_soft_kKHR= blenderobject->bsoft->kKHR;                        /* Kinetic contacts hardness [0,1] */
1469
1470                         objprop.m_soft_kSHR= blenderobject->bsoft->kSHR;                        /* Soft contacts hardness [0,1] */
1471                         objprop.m_soft_kAHR= blenderobject->bsoft->kAHR;                        /* Anchors hardness [0,1] */
1472                         objprop.m_soft_collisionflags= blenderobject->bsoft->collisionflags;    /* Vertex/Face or Signed Distance Field(SDF) or Clusters, Soft versus Soft or Rigid */
1473                         objprop.m_soft_numclusteriterations= blenderobject->bsoft->numclusteriterations;        /* number of iterations to refine collision clusters*/
1474                         objprop.m_soft_welding = blenderobject->bsoft->welding;         /* welding */
1475                         objprop.m_margin = blenderobject->bsoft->margin;
1476                         objprop.m_contactProcessingThreshold = 0.f;
1477                 } else
1478                 {
1479                         objprop.m_gamesoftFlag = OB_BSB_BENDING_CONSTRAINTS | OB_BSB_SHAPE_MATCHING | OB_BSB_AERO_VPOINT;
1480                         
1481                         objprop.m_soft_linStiff = 0.5;;
1482                         objprop.m_soft_angStiff = 1.f;          /* angular stiffness 0..1 */
1483                         objprop.m_soft_volume= 1.f;                     /* volume preservation 0..1 */
1484
1485
1486                         objprop.m_soft_viterations= 0;
1487                         objprop.m_soft_piterations= 1;
1488                         objprop.m_soft_diterations= 0;
1489                         objprop.m_soft_citerations= 4;
1490
1491                         objprop.m_soft_kSRHR_CL= 0.1f;
1492                         objprop.m_soft_kSKHR_CL= 1.f;
1493                         objprop.m_soft_kSSHR_CL= 0.5;
1494                         objprop.m_soft_kSR_SPLT_CL= 0.5f;
1495
1496                         objprop.m_soft_kSK_SPLT_CL= 0.5f;
1497                         objprop.m_soft_kSS_SPLT_CL= 0.5f;
1498                         objprop.m_soft_kVCF=  1;
1499                         objprop.m_soft_kDP= 0;
1500
1501                         objprop.m_soft_kDG= 0;
1502                         objprop.m_soft_kLF= 0;
1503                         objprop.m_soft_kPR= 0;
1504                         objprop.m_soft_kVC= 0;
1505
1506                         objprop.m_soft_kDF= 0.2f;
1507                         objprop.m_soft_kMT= 0.05f;
1508                         objprop.m_soft_kCHR= 1.0f;
1509                         objprop.m_soft_kKHR= 0.1f;
1510
1511                         objprop.m_soft_kSHR= 1.f;
1512                         objprop.m_soft_kAHR= 0.7f;
1513                         objprop.m_soft_collisionflags= OB_BSB_COL_SDF_RS + OB_BSB_COL_VF_SS;
1514                         objprop.m_soft_numclusteriterations= 16;
1515                         objprop.m_soft_welding = 0.f;
1516                         objprop.m_margin = 0.f;
1517                         objprop.m_contactProcessingThreshold = 0.f;
1518                 }
1519         }
1520
1521         objprop.m_ghost = (blenderobject->gameflag & OB_GHOST) != 0;
1522         objprop.m_disableSleeping = (blenderobject->gameflag & OB_COLLISION_RESPONSE) != 0;//abuse the OB_COLLISION_RESPONSE flag
1523         //mmm, for now, taks this for the size of the dynamicobject
1524         // Blender uses inertia for radius of dynamic object
1525         objprop.m_radius = blenderobject->inertia;
1526         objprop.m_in_active_layer = (blenderobject->lay & activeLayerBitInfo) != 0;
1527         objprop.m_dynamic_parent=NULL;
1528         objprop.m_isdeformable = ((blenderobject->gameflag2 & 2)) != 0;
1529         objprop.m_boundclass = objprop.m_dyna?KX_BOUNDSPHERE:KX_BOUNDMESH;
1530         
1531         if ((blenderobject->gameflag & OB_SOFT_BODY) && !(blenderobject->gameflag & OB_BOUNDS))
1532         {
1533                 objprop.m_boundclass = KX_BOUNDMESH;
1534         }
1535
1536         KX_BoxBounds bb;
1537         DerivedMesh* dm = NULL;
1538         if (gameobj->GetDeformer())
1539                 dm = gameobj->GetDeformer()->GetFinalMesh();
1540         my_get_local_bounds(blenderobject,dm,objprop.m_boundobject.box.m_center,bb.m_extends);
1541         if (blenderobject->gameflag & OB_BOUNDS)
1542         {
1543                 switch (blenderobject->boundtype)
1544                 {
1545                         case OB_BOUND_BOX:
1546                                 objprop.m_boundclass = KX_BOUNDBOX;
1547                                 //mmm, has to be divided by 2 to be proper extends
1548                                 objprop.m_boundobject.box.m_extends[0]=2.f*bb.m_extends[0];
1549                                 objprop.m_boundobject.box.m_extends[1]=2.f*bb.m_extends[1];
1550                                 objprop.m_boundobject.box.m_extends[2]=2.f*bb.m_extends[2];
1551                                 break;
1552                         case OB_BOUND_POLYT:
1553                                 if (blenderobject->type == OB_MESH)
1554                                 {
1555                                         objprop.m_boundclass = KX_BOUNDPOLYTOPE;
1556                                         break;
1557                                 }
1558                                 // Object is not a mesh... fall through OB_BOUND_POLYH to 
1559                                 // OB_BOUND_SPHERE
1560                         case OB_BOUND_POLYH:
1561                                 if (blenderobject->type == OB_MESH)
1562                                 {
1563                                         objprop.m_boundclass = KX_BOUNDMESH;
1564                                         break;
1565                                 }
1566                                 // Object is not a mesh... can't use polyheder. 
1567                                 // Fall through and become a sphere.
1568                         case OB_BOUND_SPHERE:
1569                         {
1570                                 objprop.m_boundclass = KX_BOUNDSPHERE;
1571                                 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], MT_max(bb.m_extends[1], bb.m_extends[2]));
1572                                 break;
1573                         }
1574                         case OB_BOUND_CYLINDER:
1575                         {
1576                                 objprop.m_boundclass = KX_BOUNDCYLINDER;
1577                                 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], bb.m_extends[1]);
1578                                 objprop.m_boundobject.c.m_height = 2.f*bb.m_extends[2];
1579                                 break;
1580                         }
1581                         case OB_BOUND_CONE:
1582                         {
1583                                 objprop.m_boundclass = KX_BOUNDCONE;
1584                                 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], bb.m_extends[1]);
1585                                 objprop.m_boundobject.c.m_height = 2.f*bb.m_extends[2];
1586                                 break;
1587                         }
1588                 }
1589         }
1590
1591         
1592         if (parent/* && (parent->gameflag & OB_DYNAMIC)*/) {
1593                 // parented object cannot be dynamic
1594                 KX_GameObject *parentgameobject = converter->FindGameObject(parent);
1595                 objprop.m_dynamic_parent = parentgameobject;
1596                 //cannot be dynamic:
1597                 objprop.m_dyna = false;
1598                 objprop.m_softbody = false;
1599                 shapeprops->m_mass = 0.f;
1600         }
1601
1602         
1603         objprop.m_concave = (blenderobject->boundtype & 4) != 0;
1604         
1605         switch (physics_engine)
1606         {
1607 #ifdef USE_BULLET
1608                 case UseBullet:
1609                         KX_ConvertBulletObject(gameobj, meshobj, dm, kxscene, shapeprops, smmaterial, &objprop);
1610                         break;
1611
1612 #endif
1613 #ifdef USE_SUMO_SOLID
1614                 case UseSumo:
1615                         KX_ConvertSumoObject(gameobj, meshobj, kxscene, shapeprops, smmaterial, &objprop);
1616                         break;
1617 #endif
1618                         
1619 #ifdef USE_ODE
1620                 case UseODE:
1621                         KX_ConvertODEEngineObject(gameobj, meshobj, kxscene, shapeprops, smmaterial, &objprop);
1622                         break;
1623 #endif //USE_ODE
1624
1625                 case UseDynamo:
1626                         //KX_ConvertDynamoObject(gameobj,meshobj,kxscene,shapeprops,    smmaterial,     &objprop);
1627                         break;
1628                         
1629                 case UseNone:
1630                 default:
1631                         break;
1632         }
1633         delete shapeprops;
1634         delete smmaterial;
1635 }
1636
1637
1638
1639
1640
1641 static KX_LightObject *gamelight_from_blamp(Object *ob, Lamp *la, unsigned int layerflag, KX_Scene *kxscene, RAS_IRenderTools *rendertools, KX_BlenderSceneConverter *converter) {
1642         RAS_LightObject lightobj;
1643         KX_LightObject *gamelight;
1644         
1645         lightobj.m_att1 = la->att1;
1646         lightobj.m_att2 = (la->mode & LA_QUAD)?la->att2:0.0;
1647         lightobj.m_red = la->r;
1648         lightobj.m_green = la->g;
1649         lightobj.m_blue = la->b;
1650         lightobj.m_distance = la->dist;
1651         lightobj.m_energy = la->energy;
1652         lightobj.m_layer = layerflag;
1653         lightobj.m_spotblend = la->spotblend;
1654         lightobj.m_spotsize = la->spotsize;
1655         
1656         lightobj.m_nodiffuse = (la->mode & LA_NO_DIFF) != 0;
1657         lightobj.m_nospecular = (la->mode & LA_NO_SPEC) != 0;
1658         
1659         if (la->mode & LA_NEG)
1660         {
1661                 lightobj.m_red = -lightobj.m_red;
1662                 lightobj.m_green = -lightobj.m_green;
1663                 lightobj.m_blue = -lightobj.m_blue;
1664         }
1665                 
1666         if (la->type==LA_SUN) {
1667                 lightobj.m_type = RAS_LightObject::LIGHT_SUN;
1668         } else if (la->type==LA_SPOT) {
1669                 lightobj.m_type = RAS_LightObject::LIGHT_SPOT;
1670         } else {
1671                 lightobj.m_type = RAS_LightObject::LIGHT_NORMAL;
1672         }
1673
1674         gamelight = new KX_LightObject(kxscene, KX_Scene::m_callbacks, rendertools,
1675                 lightobj, converter->GetGLSLMaterials());
1676
1677         BL_ConvertLampIpos(la, gamelight, converter);
1678         
1679         return gamelight;
1680 }
1681
1682 static KX_Camera *gamecamera_from_bcamera(Object *ob, KX_Scene *kxscene, KX_BlenderSceneConverter *converter) {
1683         Camera* ca = static_cast<Camera*>(ob->data);
1684         RAS_CameraData camdata(ca->lens, ca->ortho_scale, ca->clipsta, ca->clipend, ca->type == CAM_PERSP, dof_camera(ob));
1685         KX_Camera *gamecamera;
1686         
1687         gamecamera= new KX_Camera(kxscene, KX_Scene::m_callbacks, camdata);
1688         gamecamera->SetName(ca->id.name + 2);
1689         
1690         BL_ConvertCameraIpos(ca, gamecamera, converter);
1691         
1692         return gamecamera;
1693 }
1694
1695 static KX_GameObject *gameobject_from_blenderobject(
1696                                                                 Object *ob, 
1697                                                                 KX_Scene *kxscene, 
1698                                                                 RAS_IRenderTools *rendertools, 
1699                                                                 KX_BlenderSceneConverter *converter,
1700                                                                 Scene *blenderscene) 
1701 {
1702         KX_GameObject *gameobj = NULL;
1703         
1704         switch(ob->type)
1705         {
1706         case OB_LAMP:
1707         {
1708                 KX_LightObject* gamelight= gamelight_from_blamp(ob, static_cast<Lamp*>(ob->data), ob->lay, kxscene, rendertools, converter);
1709                 gameobj = gamelight;
1710                 
1711                 gamelight->AddRef();
1712                 kxscene->GetLightList()->Add(gamelight);
1713
1714                 break;
1715         }
1716         
1717         case OB_CAMERA:
1718         {
1719                 KX_Camera* gamecamera = gamecamera_from_bcamera(ob, kxscene, converter);
1720                 gameobj = gamecamera;
1721                 
1722                 //don't add a reference: the camera list in kxscene->m_cameras is not released at the end
1723                 //gamecamera->AddRef();
1724                 kxscene->AddCamera(gamecamera);
1725                 
1726                 break;
1727         }
1728         
1729         case OB_MESH:
1730         {
1731                 Mesh* mesh = static_cast<Mesh*>(ob->data);
1732                 float center[3], extents[3];
1733                 float radius = my_boundbox_mesh((Mesh*) ob->data, center, extents);
1734                 RAS_MeshObject* meshobj = BL_ConvertMesh(mesh,ob,rendertools,kxscene,converter);
1735                 
1736                 // needed for python scripting
1737                 kxscene->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj);
1738         
1739                 gameobj = new BL_DeformableGameObject(ob,kxscene,KX_Scene::m_callbacks);
1740         
1741                 // set transformation
1742                 gameobj->AddMesh(meshobj);
1743         
1744                 // for all objects: check whether they want to
1745                 // respond to updates
1746                 bool ignoreActivityCulling =  
1747                         ((ob->gameflag2 & OB_NEVER_DO_ACTIVITY_CULLING)!=0);
1748                 gameobj->SetIgnoreActivityCulling(ignoreActivityCulling);
1749                 gameobj->SetOccluder((ob->gameflag & OB_OCCLUDER) != 0, false);
1750         
1751                 // two options exists for deform: shape keys and armature
1752                 // only support relative shape key
1753                 bool bHasShapeKey = mesh->key != NULL && mesh->key->type==KEY_RELATIVE;
1754                 bool bHasDvert = mesh->dvert != NULL && ob->defbase.first;
1755                 bool bHasArmature = (ob->parent && ob->parent->type == OB_ARMATURE && ob->partype==PARSKEL && bHasDvert);
1756                 bool bHasModifier = BL_ModifierDeformer::HasCompatibleDeformer(ob);
1757
1758                 if (bHasModifier) {
1759                         BL_ModifierDeformer *dcont = new BL_ModifierDeformer((BL_DeformableGameObject *)gameobj,
1760                                                                                                                                 blenderscene, ob,       (BL_SkinMeshObject *)meshobj);
1761                         ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
1762                         if (bHasShapeKey && bHasArmature)
1763                                 dcont->LoadShapeDrivers(ob->parent);
1764                 } else if (bHasShapeKey) {
1765                         // not that we can have shape keys without dvert! 
1766                         BL_ShapeDeformer *dcont = new BL_ShapeDeformer((BL_DeformableGameObject*)gameobj, 
1767                                                                                                                         ob, (BL_SkinMeshObject*)meshobj);
1768                         ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
1769                         if (bHasArmature)
1770                                 dcont->LoadShapeDrivers(ob->parent);
1771                 } else if (bHasArmature) {
1772                         BL_SkinDeformer *dcont = new BL_SkinDeformer((BL_DeformableGameObject*)gameobj,
1773                                                                                                                         ob, (BL_SkinMeshObject*)meshobj);
1774                         ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
1775                 } else if (bHasDvert) {
1776                         // this case correspond to a mesh that can potentially deform but not with the
1777                         // object to which it is attached for the moment. A skin mesh was created in
1778                         // BL_ConvertMesh() so must create a deformer too!
1779                         BL_MeshDeformer *dcont = new BL_MeshDeformer((BL_DeformableGameObject*)gameobj,
1780                                                                                                                   ob, (BL_SkinMeshObject*)meshobj);
1781                         ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
1782                 }
1783                 
1784                 MT_Point3 min = MT_Point3(center) - MT_Vector3(extents);
1785                 MT_Point3 max = MT_Point3(center) + MT_Vector3(extents);
1786                 SG_BBox bbox = SG_BBox(min, max);
1787                 gameobj->GetSGNode()->SetBBox(bbox);
1788                 gameobj->GetSGNode()->SetRadius(radius);
1789         
1790                 break;
1791         }
1792         
1793         case OB_ARMATURE:
1794         {
1795                 gameobj = new BL_ArmatureObject(
1796                         kxscene,
1797                         KX_Scene::m_callbacks,
1798                         ob // handle
1799                 );
1800                 /* Get the current pose from the armature object and apply it as the rest pose */
1801                 break;
1802         }
1803         
1804         case OB_EMPTY:
1805         {
1806                 gameobj = new KX_EmptyObject(kxscene,KX_Scene::m_callbacks);
1807                 // set transformation
1808                 break;
1809         }
1810         }
1811         if (gameobj) 
1812         {
1813                 gameobj->SetPhysicsEnvironment(kxscene->GetPhysicsEnvironment());
1814                 gameobj->SetLayer(ob->lay);
1815                 gameobj->SetBlenderObject(ob);
1816                 /* set the visibility state based on the objects render option in the outliner */
1817                 if(ob->restrictflag & OB_RESTRICT_RENDER) gameobj->SetVisible(0, 0);
1818         }
1819         return gameobj;
1820 }
1821
1822 struct parentChildLink {
1823         struct Object* m_blenderchild;
1824         SG_Node* m_gamechildnode;
1825 };
1826
1827 #include "DNA_constraint_types.h"
1828 //XXX #include "BIF_editconstraint.h"
1829
1830 bPoseChannel *get_active_posechannel2 (Object *ob)
1831 {
1832         bArmature *arm= (bArmature*)ob->data;
1833         bPoseChannel *pchan;
1834         
1835         /* find active */
1836         for(pchan= (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1837                 if(pchan->bone && (pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer))
1838                         return pchan;
1839         }
1840         
1841         return NULL;
1842 }
1843
1844 ListBase *get_active_constraints2(Object *ob)
1845 {
1846         if (!ob)
1847                 return NULL;
1848
1849         if (ob->flag & OB_POSEMODE) {
1850                 bPoseChannel *pchan;
1851
1852                 pchan = get_active_posechannel2(ob);
1853                 if (pchan)
1854                         return &pchan->constraints;
1855         }
1856         else 
1857                 return &ob->constraints;
1858
1859         return NULL;
1860 }
1861
1862
1863 void RBJconstraints(Object *ob)//not used
1864 {
1865         ListBase *conlist;
1866         bConstraint *curcon;
1867
1868         conlist = get_active_constraints2(ob);
1869
1870         if (conlist) {
1871                 for (curcon = (bConstraint *)conlist->first; curcon; curcon=(bConstraint *)curcon->next) {
1872
1873                         printf("%i\n",curcon->type);
1874                 }
1875
1876
1877         }
1878 }
1879
1880 #include "PHY_IPhysicsEnvironment.h"
1881 #include "KX_IPhysicsController.h"
1882 #include "PHY_DynamicTypes.h"
1883
1884 KX_IPhysicsController* getPhId(CListValue* sumolist,STR_String busc){//not used
1885
1886     for (int j=0;j<sumolist->GetCount();j++)
1887         {
1888             KX_GameObject* gameobje = (KX_GameObject*) sumolist->GetValue(j);
1889             if (gameobje->GetName()==busc)
1890             return gameobje->GetPhysicsController();
1891         }
1892
1893         return 0;
1894
1895 }
1896
1897 KX_GameObject* getGameOb(STR_String busc,CListValue* sumolist){
1898
1899     for (int j=0;j<sumolist->GetCount();j++)
1900         {
1901             KX_GameObject* gameobje = (KX_GameObject*) sumolist->GetValue(j);
1902             if (gameobje->GetName()==busc)
1903             return gameobje;
1904         }
1905         
1906         return 0;
1907
1908 }
1909
1910 // convert blender objects into ketsji gameobjects
1911 void BL_ConvertBlenderObjects(struct Main* maggie,
1912                                                           const STR_String& scenename,
1913                                                           KX_Scene* kxscene,
1914                                                           KX_KetsjiEngine* ketsjiEngine,
1915                                                           e_PhysicsEngine       physics_engine,
1916                                                           PyObject* pythondictionary,
1917                                                           SCA_IInputDevice* keydev,
1918                                                           RAS_IRenderTools* rendertools,
1919                                                           RAS_ICanvas* canvas,
1920                                                           KX_BlenderSceneConverter* converter,
1921                                                           bool alwaysUseExpandFraming
1922                                                           )
1923 {       
1924
1925         Scene *blenderscene = converter->GetBlenderSceneForName(scenename);
1926         // for SETLOOPER
1927         Scene *sce;
1928         Base *base;
1929
1930         // Get the frame settings of the canvas.
1931         // Get the aspect ratio of the canvas as designed by the user.
1932
1933         RAS_FrameSettings::RAS_FrameType frame_type;
1934         int aspect_width;
1935         int aspect_height;
1936         vector<MT_Vector3> inivel,iniang;
1937         set<Group*> grouplist;  // list of groups to be converted
1938         set<Object*> allblobj;  // all objects converted
1939         set<Object*> groupobj;  // objects from groups (never in active layer)
1940
1941         if (alwaysUseExpandFraming) {
1942                 frame_type = RAS_FrameSettings::e_frame_extend;
1943                 aspect_width = canvas->GetWidth();
1944                 aspect_height = canvas->GetHeight();
1945         } else {
1946                 if (blenderscene->framing.type == SCE_GAMEFRAMING_BARS) {
1947                         frame_type = RAS_FrameSettings::e_frame_bars;
1948                 } else if (blenderscene->framing.type == SCE_GAMEFRAMING_EXTEND) {
1949                         frame_type = RAS_FrameSettings::e_frame_extend;
1950                 } else {
1951                         frame_type = RAS_FrameSettings::e_frame_scale;
1952                 }
1953                 
1954                 aspect_width = blenderscene->r.xsch;
1955                 aspect_height = blenderscene->r.ysch;
1956         }
1957         
1958         RAS_FrameSettings frame_settings(
1959                 frame_type,
1960                 blenderscene->framing.col[0],
1961                 blenderscene->framing.col[1],
1962                 blenderscene->framing.col[2],
1963                 aspect_width,
1964                 aspect_height
1965         );
1966         kxscene->SetFramingType(frame_settings);
1967
1968         kxscene->SetGravity(MT_Vector3(0,0,(blenderscene->world != NULL) ? -blenderscene->world->gravity : -9.8));
1969         
1970         /* set activity culling parameters */
1971         if (blenderscene->world) {
1972                 kxscene->SetActivityCulling( (blenderscene->world->mode & WO_ACTIVITY_CULLING) != 0);
1973                 kxscene->SetActivityCullingRadius(blenderscene->world->activityBoxRadius);
1974                 kxscene->SetDbvtCulling((blenderscene->world->mode & WO_DBVT_CULLING) != 0);
1975         } else {
1976                 kxscene->SetActivityCulling(false);
1977                 kxscene->SetDbvtCulling(false);
1978         }
1979         // no occlusion culling by default
1980         kxscene->SetDbvtOcclusionRes(0);
1981
1982         int activeLayerBitInfo = blenderscene->lay;
1983         
1984         // list of all object converted, active and inactive
1985         CListValue*     sumolist = new CListValue();
1986         
1987         vector<parentChildLink> vec_parent_child;
1988         
1989         CListValue* objectlist = kxscene->GetObjectList();
1990         CListValue* inactivelist = kxscene->GetInactiveList();
1991         CListValue* parentlist = kxscene->GetRootParentList();
1992         
1993         SCA_LogicManager* logicmgr = kxscene->GetLogicManager();
1994         SCA_TimeEventManager* timemgr = kxscene->GetTimeEventManager();
1995         
1996         CListValue* logicbrick_conversionlist = new CListValue();
1997         
1998         //SG_TreeFactory tf;
1999         
2000         // Convert actions to actionmap
2001         bAction *curAct;
2002         for (curAct = (bAction*)maggie->action.first; curAct; curAct=(bAction*)curAct->id.next)
2003         {
2004                 logicmgr->RegisterActionName(curAct->id.name, curAct);
2005         }
2006
2007         SetDefaultFaceType(blenderscene);
2008         // Let's support scene set.
2009         // Beware of name conflict in linked data, it will not crash but will create confusion
2010         // in Python scripting and in certain actuators (replace mesh). Linked scene *should* have
2011         // no conflicting name for Object, Object data and Action.
2012         for (SETLOOPER(blenderscene, base))
2013         {
2014                 Object* blenderobject = base->object;
2015                 allblobj.insert(blenderobject);
2016
2017                 KX_GameObject* gameobj = gameobject_from_blenderobject(
2018                                                                                 base->object, 
2019                                                                                 kxscene, 
2020                                                                                 rendertools, 
2021                                                                                 converter,
2022                                                                                 blenderscene);
2023                                                                                 
2024                 bool isInActiveLayer = (blenderobject->lay & activeLayerBitInfo) !=0;
2025                 bool addobj=true;
2026                 
2027                 if (converter->addInitFromFrame)
2028                         if (!isInActiveLayer)
2029                                 addobj=false;
2030
2031                 if (gameobj&&addobj)
2032                 {
2033                         MT_Point3 posPrev;                      
2034                         MT_Matrix3x3 angor;                     
2035                         if (converter->addInitFromFrame) blenderscene->r.cfra=blenderscene->r.sfra;
2036                         
2037                         MT_Point3 pos;
2038                         pos.setValue(
2039                                 blenderobject->loc[0]+blenderobject->dloc[0],
2040                                 blenderobject->loc[1]+blenderobject->dloc[1],
2041                                 blenderobject->loc[2]+blenderobject->dloc[2]
2042                         );
2043                         MT_Vector3 eulxyz(blenderobject->rot);
2044                         MT_Vector3 scale(blenderobject->size);
2045                         if (converter->addInitFromFrame){//rcruiz
2046                                 float eulxyzPrev[3];
2047                                 blenderscene->r.cfra=blenderscene->r.sfra-1;
2048                                 //XXX update_for_newframe();
2049                                 MT_Vector3 tmp=pos-MT_Point3(blenderobject->loc[0]+blenderobject->dloc[0],
2050                                                                                         blenderobject->loc[1]+blenderobject->dloc[1],
2051                                                                                         blenderobject->loc[2]+blenderobject->dloc[2]
2052                                                                         );
2053                                 eulxyzPrev[0]=blenderobject->rot[0];
2054                                 eulxyzPrev[1]=blenderobject->rot[1];
2055                                 eulxyzPrev[2]=blenderobject->rot[2];
2056
2057                                 double fps = (double) blenderscene->r.frs_sec/
2058                                         (double) blenderscene->r.frs_sec_base;
2059
2060                                 tmp.scale(fps, fps, fps);
2061                                 inivel.push_back(tmp);
2062                                 tmp=eulxyz-eulxyzPrev;
2063                                 tmp.scale(fps, fps, fps);
2064                                 iniang.push_back(tmp);
2065                                 blenderscene->r.cfra=blenderscene->r.sfra;
2066                                 //XXX update_for_newframe();
2067                         }               
2068                                                 
2069                         gameobj->NodeSetLocalPosition(pos);
2070                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
2071                         gameobj->NodeSetLocalScale(scale);
2072                         gameobj->NodeUpdateGS(0);
2073                         
2074                         BL_ConvertIpos(blenderobject,gameobj,converter);
2075                         BL_ConvertMaterialIpos(blenderobject, gameobj, converter);
2076                         
2077                         sumolist->Add(gameobj->AddRef());
2078                         
2079                         BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene,isInActiveLayer);
2080                         
2081         
2082                         gameobj->SetName(blenderobject->id.name);
2083         
2084                         // update children/parent hierarchy
2085                         if ((blenderobject->parent != 0)&&(!converter->addInitFromFrame))
2086                         {
2087                                 // blender has an additional 'parentinverse' offset in each object
2088                                 SG_Callbacks callback(NULL,NULL,NULL,KX_Scene::KX_ScenegraphUpdateFunc,KX_Scene::KX_ScenegraphRescheduleFunc);
2089                                 SG_Node* parentinversenode = new SG_Node(NULL,kxscene,callback);
2090                         
2091                                 // define a normal parent relationship for this node.
2092                                 KX_NormalParentRelation * parent_relation = KX_NormalParentRelation::New();
2093                                 parentinversenode->SetParentRelation(parent_relation);
2094         
2095                                 parentChildLink pclink;
2096                                 pclink.m_blenderchild = blenderobject;
2097                                 pclink.m_gamechildnode = parentinversenode;
2098                                 vec_parent_child.push_back(pclink);
2099
2100                                 float* fl = (float*) blenderobject->parentinv;
2101                                 MT_Transform parinvtrans(fl);
2102                                 parentinversenode->SetLocalPosition(parinvtrans.getOrigin());
2103                                 // problem here: the parent inverse transform combines scaling and rotation 
2104                                 // in the basis but the scenegraph needs separate rotation and scaling.
2105                                 // This is not important for OpenGL (it uses 4x4 matrix) but it is important
2106                                 // for the physic engine that needs a separate scaling
2107                                 //parentinversenode->SetLocalOrientation(parinvtrans.getBasis());
2108
2109                                 // Extract the rotation and the scaling from the basis
2110                                 MT_Matrix3x3 ori(parinvtrans.getBasis());
2111                                 MT_Vector3 x(ori.getColumn(0));
2112                                 MT_Vector3 y(ori.getColumn(1));
2113                                 MT_Vector3 z(ori.getColumn(2));
2114                                 MT_Vector3 parscale(x.length(), y.length(), z.length());
2115                                 if (!MT_fuzzyZero(parscale[0]))
2116                                         x /= parscale[0];
2117                                 if (!MT_fuzzyZero(parscale[1]))
2118                                         y /= parscale[1];
2119                                 if (!MT_fuzzyZero(parscale[2]))
2120                                         z /= parscale[2];
2121                                 ori.setColumn(0, x);                                                            
2122                                 ori.setColumn(1, y);                                                            
2123                                 ori.setColumn(2, z);                                                            
2124                                 parentinversenode->SetLocalOrientation(ori);
2125                                 parentinversenode->SetLocalScale(parscale);
2126                                 
2127                                 parentinversenode->AddChild(gameobj->GetSGNode());
2128                         }
2129                         
2130                         // needed for python scripting
2131                         logicmgr->RegisterGameObjectName(gameobj->GetName(),gameobj);
2132
2133                         // needed for group duplication
2134                         logicmgr->RegisterGameObj(blenderobject, gameobj);
2135                         for (int i = 0; i < gameobj->GetMeshCount(); i++)
2136                                 logicmgr->RegisterGameMeshName(gameobj->GetMesh(i)->GetName(), blenderobject);
2137         
2138                         converter->RegisterGameObject(gameobj, blenderobject);  
2139                         // this was put in rapidly, needs to be looked at more closely
2140                         // only draw/use objects in active 'blender' layers
2141         
2142                         logicbrick_conversionlist->Add(gameobj->AddRef());
2143                         
2144                         if (converter->addInitFromFrame){
2145                                 posPrev=gameobj->NodeGetWorldPosition();
2146                                 angor=gameobj->NodeGetWorldOrientation();
2147                         }
2148                         if (isInActiveLayer)
2149                         {
2150                                 objectlist->Add(gameobj->AddRef());
2151                                 //tf.Add(gameobj->GetSGNode());
2152                                 
2153                                 gameobj->NodeUpdateGS(0);
2154                                 gameobj->AddMeshUser();
2155                 
2156                         }
2157                         else
2158                         {
2159                                 //we must store this object otherwise it will be deleted 
2160                                 //at the end of this function if it is not a root object
2161                                 inactivelist->Add(gameobj->AddRef());
2162                         }
2163                         if (gameobj->IsDupliGroup())
2164                                 grouplist.insert(blenderobject->dup_group);
2165                         if (converter->addInitFromFrame){
2166                                 gameobj->NodeSetLocalPosition(posPrev);
2167                                 gameobj->NodeSetLocalOrientation(angor);
2168                         }
2169                                                 
2170                 }
2171                 /* Note about memory leak issues:
2172                    When a CValue derived class is created, m_refcount is initialized to 1
2173                    so the class must be released after being used to make sure that it won't 
2174                    hang in memory. If the object needs to be stored for a long time, 
2175                    use AddRef() so that this Release() does not free the object.
2176                    Make sure that for any AddRef() there is a Release()!!!! 
2177                    Do the same for any object derived from CValue, CExpression and NG_NetworkMessage
2178                  */
2179                 if (gameobj)
2180                         gameobj->Release();
2181
2182         }
2183
2184         if (!grouplist.empty())
2185         {
2186                 // now convert the group referenced by dupli group object
2187                 // keep track of all groups already converted
2188                 set<Group*> allgrouplist = grouplist;
2189                 set<Group*> tempglist;
2190                 // recurse
2191                 while (!grouplist.empty())
2192                 {
2193                         set<Group*>::iterator git;
2194                         tempglist.clear();
2195                         tempglist.swap(grouplist);
2196                         for (git=tempglist.begin(); git!=tempglist.end(); git++)
2197                         {
2198                                 Group* group = *git;
2199                                 GroupObject* go;
2200                                 for(go=(GroupObject*)group->gobject.first; go; go=(GroupObject*)go->next) 
2201                                 {
2202                                         Object* blenderobject = go->ob;
2203                                         if (converter->FindGameObject(blenderobject) == NULL)
2204                                         {
2205                                                 allblobj.insert(blenderobject);
2206                                                 groupobj.insert(blenderobject);
2207                                                 KX_GameObject* gameobj = gameobject_from_blenderobject(
2208                                                                                                                 blenderobject, 
2209                                                                                                                 kxscene, 
2210                                                                                                                 rendertools, 
2211                                                                                                                 converter,
2212                                                                                                                 blenderscene);
2213                                                                                 
2214                                                 // this code is copied from above except that
2215                                                 // object from groups are never in active layer
2216                                                 bool isInActiveLayer = false;
2217                                                 bool addobj=true;
2218                                                 
2219                                                 if (converter->addInitFromFrame)
2220                                                         if (!isInActiveLayer)
2221                                                                 addobj=false;
2222                                                                                                                 
2223                                                 if (gameobj&&addobj)
2224                                                 {
2225                                                         MT_Point3 posPrev;                      
2226                                                         MT_Matrix3x3 angor;                     
2227                                                         if (converter->addInitFromFrame) 
2228                                                                 blenderscene->r.cfra=blenderscene->r.sfra;
2229                                                         
2230                                                         MT_Point3 pos(
2231                                                                 blenderobject->loc[0]+blenderobject->dloc[0],
2232                                                                 blenderobject->loc[1]+blenderobject->dloc[1],
2233                                                                 blenderobject->loc[2]+blenderobject->dloc[2]
2234                                                         );
2235                                                         MT_Vector3 eulxyz(blenderobject->rot);
2236                                                         MT_Vector3 scale(blenderobject->size);
2237                                                         if (converter->addInitFromFrame){//rcruiz
2238                                                                 float eulxyzPrev[3];
2239                                                                 blenderscene->r.cfra=blenderscene->r.sfra-1;
2240                                                                 //XXX update_for_newframe();
2241                                                                 MT_Vector3 tmp=pos-MT_Point3(blenderobject->loc[0]+blenderobject->dloc[0],
2242                                                                                                                         blenderobject->loc[1]+blenderobject->dloc[1],
2243                                                                                                                         blenderobject->loc[2]+blenderobject->dloc[2]
2244                                                                                                         );
2245                                                                 eulxyzPrev[0]=blenderobject->rot[0];
2246                                                                 eulxyzPrev[1]=blenderobject->rot[1];
2247                                                                 eulxyzPrev[2]=blenderobject->rot[2];
2248
2249                                                                 double fps = (double) blenderscene->r.frs_sec/
2250                                                                         (double) blenderscene->r.frs_sec_base;
2251
2252                                                                 tmp.scale(fps, fps, fps);
2253                                                                 inivel.push_back(tmp);
2254                                                                 tmp=eulxyz-eulxyzPrev;
2255                                                                 tmp.scale(fps, fps, fps);
2256                                                                 iniang.push_back(tmp);
2257                                                                 blenderscene->r.cfra=blenderscene->r.sfra;
2258                                                                 //XXX update_for_newframe();
2259                                                         }               
2260                                                                                 
2261                                                         gameobj->NodeSetLocalPosition(pos);
2262                                                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
2263                                                         gameobj->NodeSetLocalScale(scale);
2264                                                         gameobj->NodeUpdateGS(0);
2265                                                         
2266                                                         BL_ConvertIpos(blenderobject,gameobj,converter);
2267                                                         BL_ConvertMaterialIpos(blenderobject,gameobj, converter);       
2268                                         
2269                                                         sumolist->Add(gameobj->AddRef());
2270                                                         
2271                                                         BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene,isInActiveLayer);
2272                                                         
2273                                         
2274                                                         gameobj->SetName(blenderobject->id.name);
2275                                         
2276                                                         // update children/parent hierarchy
2277                                                         if ((blenderobject->parent != 0)&&(!converter->addInitFromFrame))
2278                                                         {
2279                                                                 // blender has an additional 'parentinverse' offset in each object
2280                                                                 SG_Callbacks callback(NULL,NULL,NULL,KX_Scene::KX_ScenegraphUpdateFunc,KX_Scene::KX_ScenegraphRescheduleFunc);
2281                                                                 SG_Node* parentinversenode = new SG_Node(NULL,kxscene,callback);
2282                                                         
2283                                                                 // define a normal parent relationship for this node.
2284                                                                 KX_NormalParentRelation * parent_relation = KX_NormalParentRelation::New();
2285                                                                 parentinversenode->SetParentRelation(parent_relation);
2286                                         
2287                                                                 parentChildLink pclink;
2288                                                                 pclink.m_blenderchild = blenderobject;
2289                                                                 pclink.m_gamechildnode = parentinversenode;
2290                                                                 vec_parent_child.push_back(pclink);
2291
2292                                                                 float* fl = (float*) blenderobject->parentinv;
2293                                                                 MT_Transform parinvtrans(fl);
2294                                                                 parentinversenode->SetLocalPosition(parinvtrans.getOrigin());
2295
2296                                                                 // Extract the rotation and the scaling from the basis
2297                                                                 MT_Matrix3x3 ori(parinvtrans.getBasis());
2298                                                                 MT_Vector3 x(ori.getColumn(0));
2299                                                                 MT_Vector3 y(ori.getColumn(1));
2300                                                                 MT_Vector3 z(ori.getColumn(2));
2301                                                                 MT_Vector3 localscale(x.length(), y.length(), z.length());
2302                                                                 if (!MT_fuzzyZero(localscale[0]))
2303                                                                         x /= localscale[0];
2304                                                                 if (!MT_fuzzyZero(localscale[1]))
2305                                                                         y /= localscale[1];
2306                                                                 if (!MT_fuzzyZero(localscale[2]))
2307                                                                         z /= localscale[2];
2308                                                                 ori.setColumn(0, x);                                                            
2309                                                                 ori.setColumn(1, y);                                                            
2310                                                                 ori.setColumn(2, z);                                                            
2311                                                                 parentinversenode->SetLocalOrientation(ori);
2312                                                                 parentinversenode->SetLocalScale(localscale);
2313                                                                 
2314                                                                 parentinversenode->AddChild(gameobj->GetSGNode());
2315                                                         }
2316                                                         
2317                                                         // needed for python scripting
2318                                                         logicmgr->RegisterGameObjectName(gameobj->GetName(),gameobj);
2319
2320                                                         // needed for group duplication
2321                                                         logicmgr->RegisterGameObj(blenderobject, gameobj);
2322                                                         for (int i = 0; i < gameobj->GetMeshCount(); i++)
2323                                                                 logicmgr->RegisterGameMeshName(gameobj->GetMesh(i)->GetName(), blenderobject);
2324                                         
2325                                                         converter->RegisterGameObject(gameobj, blenderobject);  
2326                                                         // this was put in rapidly, needs to be looked at more closely
2327                                                         // only draw/use objects in active 'blender' layers
2328                                         
2329                                                         logicbrick_conversionlist->Add(gameobj->AddRef());
2330                                                         
2331                                                         if (converter->addInitFromFrame){
2332                                                                 posPrev=gameobj->NodeGetWorldPosition();
2333                                                                 angor=gameobj->NodeGetWorldOrientation();
2334                                                         }
2335                                                         if (isInActiveLayer)
2336                                                         {
2337                                                                 objectlist->Add(gameobj->AddRef());
2338                                                                 //tf.Add(gameobj->GetSGNode());
2339                                                                 
2340                                                                 gameobj->NodeUpdateGS(0);
2341                                                                 gameobj->AddMeshUser();
2342                                                         }
2343                                                         else
2344                                                         {
2345                                                                 //we must store this object otherwise it will be deleted 
2346                                                                 //at the end of this function if it is not a root object
2347                                                                 inactivelist->Add(gameobj->AddRef());
2348
2349                                                         }
2350                                                         if (gameobj->IsDupliGroup())
2351                                                         {
2352                                                                 // check that the group is not already converted
2353                                                                 if (allgrouplist.insert(blenderobject->dup_group).second)
2354                                                                         grouplist.insert(blenderobject->dup_group);
2355                                                         }
2356                                                         if (converter->addInitFromFrame){
2357                                                                 gameobj->NodeSetLocalPosition(posPrev);
2358                                                                 gameobj->NodeSetLocalOrientation(angor);
2359                                                         }
2360                                                                                 
2361                                                 }
2362                                                 if (gameobj)
2363                                                         gameobj->Release();
2364                                         }
2365                                 }
2366                         }
2367                 }
2368         }
2369
2370         // non-camera objects not supported as camera currently
2371         if (blenderscene->camera && blenderscene->camera->type == OB_CAMERA) {
2372                 KX_Camera *gamecamera= (KX_Camera*) converter->FindGameObject(blenderscene->camera);
2373                 
2374                 if(gamecamera)
2375                         kxscene->SetActiveCamera(gamecamera);
2376         }
2377
2378         //      Set up armatures
2379         set<Object*>::iterator oit;
2380         for(oit=allblobj.begin(); oit!=allblobj.end(); oit++)
2381         {
2382                 Object* blenderobj = *oit;
2383                 if (blenderobj->type==OB_MESH) {
2384                         Mesh *me = (Mesh*)blenderobj->data;
2385         
2386                         if (me->dvert){
2387                                 BL_DeformableGameObject *obj = (BL_DeformableGameObject*)converter->FindGameObject(blenderobj);
2388         
2389                                 if (obj && blenderobj->parent && blenderobj->parent->type==OB_ARMATURE && blenderobj->partype==PARSKEL){
2390                                         KX_GameObject *par = converter->FindGameObject(blenderobj->parent);
2391                                         if (par && obj->GetDeformer())
2392                                                 ((BL_SkinDeformer*)obj->GetDeformer())->SetArmature((BL_ArmatureObject*) par);
2393                                 }
2394                         }
2395                 }
2396         }
2397         
2398         // create hierarchy information
2399         int i;
2400         vector<parentChildLink>::iterator pcit;
2401         
2402         for (pcit = vec_parent_child.begin();!(pcit==vec_parent_child.end());++pcit)
2403         {
2404         
2405                 struct Object* blenderchild = pcit->m_blenderchild;
2406                 struct Object* blenderparent = blenderchild->parent;
2407                 KX_GameObject* parentobj = converter->FindGameObject(blenderparent);
2408                 KX_GameObject* childobj = converter->FindGameObject(blenderchild);
2409
2410                 assert(childobj);
2411
2412                 if (!parentobj || objectlist->SearchValue(childobj) != objectlist->SearchValue(parentobj))
2413                 {
2414                         // special case: the parent and child object are not in the same layer. 
2415                         // This weird situation is used in Apricot for test purposes.
2416                         // Resolve it by not converting the child
2417                         childobj->GetSGNode()->DisconnectFromParent();
2418                         delete pcit->m_gamechildnode;
2419                         // Now destroy the child object but also all its descendent that may already be linked
2420                         // Remove the child reference in the local list!
2421                         // Note: there may be descendents already if the children of the child were processed
2422                         //       by this loop before the child. In that case, we must remove the children also
2423                         CListValue* childrenlist = childobj->GetChildrenRecursive();
2424                         childrenlist->Add(childobj->AddRef());
2425                         for ( i=0;i<childrenlist->GetCount();i++)
2426                         {
2427                                 KX_GameObject* obj = static_cast<KX_GameObject*>(childrenlist->GetValue(i));
2428                                 if (sumolist->RemoveValue(obj))
2429                                         obj->Release();
2430                                 if (logicbrick_conversionlist->RemoveValue(obj))
2431                                         obj->Release();
2432                         }
2433                         childrenlist->Release();
2434                         // now destroy recursively
2435                         kxscene->RemoveObject(childobj);
2436                         continue;
2437                 }
2438
2439                 switch (blenderchild->partype)
2440                 {
2441                         case PARVERT1:
2442                         {
2443                                 // creat a new vertex parent relationship for this node.
2444                                 KX_VertexParentRelation * vertex_parent_relation = KX_VertexParentRelation::New();
2445                                 pcit->m_gamechildnode->SetParentRelation(vertex_parent_relation);
2446                                 break;
2447                         }
2448                         case PARSLOW:
2449                         {
2450                                 // creat a new slow parent relationship for this node.
2451                                 KX_SlowParentRelation * slow_parent_relation = KX_SlowParentRelation::New(blenderchild->sf);
2452                                 pcit->m_gamechildnode->SetParentRelation(slow_parent_relation);
2453                                 break;
2454                         }       
2455                         case PARBONE:
2456                         {
2457                                 // parent this to a bone
2458                                 Bone *parent_bone = get_named_bone( (bArmature *)(blenderchild->parent)->data, blenderchild->parsubstr);
2459
2460                                 if(parent_bone) {
2461                                         KX_BoneParentRelation *bone_parent_relation = KX_BoneParentRelation::New(parent_bone);
2462                                         pcit->m_gamechildnode->SetParentRelation(bone_parent_relation);
2463                                 }
2464                         
2465                                 break;
2466                         }
2467                         case PARSKEL: // skinned - ignore
2468                                 break;
2469                         case PAROBJECT:
2470                         case PARCURVE:
2471                         case PARKEY:
2472                         case PARVERT3:
2473                         default:
2474                                 // unhandled
2475                                 break;
2476                 }
2477         
2478                 parentobj->     GetSGNode()->AddChild(pcit->m_gamechildnode);
2479         }
2480         vec_parent_child.clear();
2481         
2482         // find 'root' parents (object that has not parents in SceneGraph)
2483         for (i=0;i<sumolist->GetCount();++i)
2484         {
2485                 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2486                 if (gameobj->GetSGNode()->GetSGParent() == 0)
2487                 {
2488                         parentlist->Add(gameobj->AddRef());
2489                         gameobj->NodeUpdateGS(0);
2490                 }
2491         }
2492
2493         // create graphic controller for culling
2494         if (kxscene->GetDbvtCulling())
2495         {
2496                 bool occlusion = false;
2497                 for (i=0; i<sumolist->GetCount();i++)
2498                 {
2499                         KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2500                         if (gameobj->GetMeshCount() > 0) 
2501                         {
2502                                 MT_Point3 box[2];
2503                                 gameobj->GetSGNode()->BBox().getmm(box, MT_Transform::Identity());
2504                                 // box[0] is the min, box[1] is the max
2505                                 bool isactive = objectlist->SearchValue(gameobj);
2506                                 BL_CreateGraphicObjectNew(gameobj,box[0],box[1],kxscene,isactive,physics_engine);
2507                                 if (gameobj->GetOccluder())
2508                                         occlusion = true;
2509                         }
2510                 }
2511                 if (occlusion)
2512                         kxscene->SetDbvtOcclusionRes(blenderscene->world->occlusionRes);
2513         }
2514         if (blenderscene->world)
2515                 kxscene->GetPhysicsEnvironment()->setNumTimeSubSteps(blenderscene->world->physubstep);
2516
2517         // now that the scenegraph is complete, let's instantiate the deformers.
2518         // We need that to create reusable derived mesh and physic shapes
2519         for (i=0;i<sumolist->GetCount();++i)
2520         {
2521                 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2522                 if (gameobj->GetDeformer())
2523                         gameobj->GetDeformer()->UpdateBuckets();
2524         }
2525
2526         bool processCompoundChildren = false;
2527
2528         // create physics information
2529         for (i=0;i<sumolist->GetCount();i++)
2530         {
2531                 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2532                 struct Object* blenderobject = converter->FindBlenderObject(gameobj);
2533                 int nummeshes = gameobj->GetMeshCount();
2534                 RAS_MeshObject* meshobj = 0;
2535                 if (nummeshes > 0)
2536                 {
2537                         meshobj = gameobj->GetMesh(0);
2538                 }
2539                 int layerMask = (groupobj.find(blenderobject) == groupobj.end()) ? activeLayerBitInfo : 0;
2540                 BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,layerMask,physics_engine,converter,processCompoundChildren);
2541         }
2542
2543         processCompoundChildren = true;
2544         // create physics information
2545         for (i=0;i<sumolist->GetCount();i++)
2546         {
2547                 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2548                 struct Object* blenderobject = converter->FindBlenderObject(gameobj);
2549                 int nummeshes = gameobj->GetMeshCount();
2550                 RAS_MeshObject* meshobj = 0;
2551                 if (nummeshes > 0)
2552                 {
2553                         meshobj = gameobj->GetMesh(0);
2554                 }
2555                 int layerMask = (groupobj.find(blenderobject) == groupobj.end()) ? activeLayerBitInfo : 0;
2556                 BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,layerMask,physics_engine,converter,processCompoundChildren);
2557         }
2558         
2559         //set ini linearVel and int angularVel //rcruiz
2560         if (converter->addInitFromFrame)
2561                 for (i=0;i<sumolist->GetCount();i++)
2562                 {
2563                         KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2564                         if (gameobj->IsDynamic()){
2565                                 gameobj->setLinearVelocity(inivel[i],false);
2566                                 gameobj->setAngularVelocity(iniang[i],false);
2567                         }
2568                 
2569                 
2570                 }       
2571
2572                 // create physics joints
2573         for (i=0;i<sumolist->GetCount();i++)
2574         {
2575                 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
2576                 struct Object* blenderobject = converter->FindBlenderObject(gameobj);
2577                 ListBase *conlist;
2578                 bConstraint *curcon;
2579                 conlist = get_active_constraints2(blenderobject);
2580
2581                 if (conlist) {
2582                         for (curcon = (bConstraint *)conlist->first; curcon; curcon=(bConstraint *)curcon->next) {
2583                                 if (curcon->type==CONSTRAINT_TYPE_RIGIDBODYJOINT){
2584
2585                                         bRigidBodyJointConstraint *dat=(bRigidBodyJointConstraint *)curcon->data;
2586
2587                                         if (!dat->child){
2588
2589                                                 PHY_IPhysicsController* physctr2 = 0;
2590
2591                                                 if (dat->tar)
2592                                                 {
2593                                                         KX_GameObject *gotar=getGameOb(dat->tar->id.name,sumolist);
2594                                                         if (gotar && gotar->GetPhysicsController())
2595                                                                 physctr2 = (PHY_IPhysicsController*) gotar->GetPhysicsController()->GetUserData();
2596                                                 }
2597
2598                                                 if (gameobj->GetPhysicsController())
2599                                                 {
2600                                                         float radsPerDeg = 6.283185307179586232f / 360.f;
2601
2602                                                         PHY_IPhysicsController* physctrl = (PHY_IPhysicsController*) gameobj->GetPhysicsController()->GetUserData();
2603                                                         //we need to pass a full constraint frame, not just axis
2604                                     
2605                                                         //localConstraintFrameBasis
2606                                                         MT_Matrix3x3 localCFrame(MT_Vector3(radsPerDeg*dat->axX,radsPerDeg*dat->axY,radsPerDeg*dat->axZ));
2607                                                         MT_Vector3 axis0 = localCFrame.getColumn(0);
2608                                                         MT_Vector3 axis1 = localCFrame.getColumn(1);
2609                                                         MT_Vector3 axis2 = localCFrame.getColumn(2);
2610                                                                 
2611                                                         int constraintId = kxscene->GetPhysicsEnvironment()->createConstraint(physctrl,physctr2,(PHY_ConstraintType)dat->type,(float)dat->pivX,
2612                                                                 (float)dat->pivY,(float)dat->pivZ,
2613                                                                 (float)axis0.x(),(float)axis0.y(),(float)axis0.z(),
2614                                                                 (float)axis1.x(),(float)axis1.y(),(float)axis1.z(),
2615                                                                 (float)axis2.x(),(float)axis2.y(),(float)axis2.z(),dat->flag);
2616                                                         if (constraintId)
2617                                                         {
2618                                                                 //if it is a generic 6DOF constraint, set all the limits accordingly
2619