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