Set the sound system listener properties.
[blender.git] / source / gameengine / Converter / BL_BlenderDataConversion.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Convert blender data to ketsji
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef WIN32
39 #pragma warning (disable : 4786)
40 #endif
41
42 #include <math.h>
43
44 #include "BL_BlenderDataConversion.h"
45 #include "KX_BlenderGL.h"
46 #include "KX_BlenderScalarInterpolator.h"
47
48 #include "RAS_IPolygonMaterial.h"
49
50 // Expressions
51 #include "ListValue.h"
52 #include "IntValue.h"
53 // Collision & Fuzzics LTD
54
55 #include "PHY_Pro.h"
56
57
58 #include "KX_Scene.h"
59 #include "KX_GameObject.h"
60 #include "RAS_FramingManager.h"
61 #include "RAS_MeshObject.h"
62
63 #include "KX_ConvertActuators.h"
64 #include "KX_ConvertControllers.h"
65 #include "KX_ConvertSensors.h"
66
67 #include "SCA_LogicManager.h"
68 #include "SCA_EventManager.h"
69 #include "SCA_TimeEventManager.h"
70 #include "KX_Light.h"
71 #include "KX_Camera.h"
72 #include "KX_EmptyObject.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
86 #include "DNA_action_types.h"
87 #include "BKE_main.h"
88 #include "BKE_global.h"
89 #include "BL_SkinMeshObject.h"
90 #include "BL_SkinDeformer.h"
91 #include "BL_MeshDeformer.h"
92 //#include "BL_ArmatureController.h"
93
94 #include "BlenderWorldInfo.h"
95
96 #include "KX_KetsjiEngine.h"
97 #include "KX_BlenderSceneConverter.h"
98
99 #include"SND_Scene.h"
100 #include "SND_SoundListener.h"
101
102 /* This little block needed for linking to Blender... */
103 #ifdef WIN32
104 #include "BLI_winstuff.h"
105 #endif
106
107 /* This list includes only data type definitions */
108 #include "DNA_object_types.h"
109 #include "DNA_material_types.h"
110 #include "DNA_image_types.h"
111 #include "DNA_lamp_types.h"
112 #include "DNA_group_types.h"
113 #include "DNA_scene_types.h"
114 #include "DNA_camera_types.h"
115 #include "DNA_property_types.h"
116 #include "DNA_text_types.h"
117 #include "DNA_sensor_types.h"
118 #include "DNA_controller_types.h"
119 #include "DNA_actuator_types.h"
120 #include "DNA_mesh_types.h"
121 #include "DNA_meshdata_types.h"
122 #include "DNA_view3d_types.h"
123 #include "DNA_world_types.h"
124 #include "DNA_sound_types.h"
125 #include "DNA_key_types.h"
126
127
128 #include "MEM_guardedalloc.h"
129 #include "BKE_utildefines.h"
130 #include "BKE_key.h"
131 #include "BKE_mesh.h"
132 #include "MT_Point3.h"
133
134
135 #include "BKE_material.h" /* give_current_material */
136 /* end of blender include block */
137
138 #include "KX_BlenderInputDevice.h"
139 #include "KX_ConvertProperties.h"
140 #include "KX_HashedPtr.h"
141
142
143 #include "KX_ScalarInterpolator.h"
144
145 #include "KX_IpoConvert.h"
146 #include "SYS_System.h"
147
148 #include "SG_Node.h"
149 #include "SG_BBox.h"
150 #include "SG_Tree.h"
151
152 // defines USE_ODE to choose physics engine
153 #include "KX_ConvertPhysicsObject.h"
154
155
156 // This file defines relationships between parents and children
157 // in the game engine.
158
159 #include "KX_SG_NodeRelationships.h"
160
161 #include "BL_ArmatureObject.h"
162 #include "BL_DeformableGameObject.h"
163
164 static int default_face_mode = TF_DYNAMIC;
165
166 static unsigned int KX_rgbaint2uint_new(unsigned int icol)
167 {
168         union
169         {
170                 unsigned int integer;
171                 unsigned char cp[4];
172         } out_colour, in_colour;
173         
174         in_colour.integer = icol;
175         out_colour.cp[0] = in_colour.cp[3]; // red
176         out_colour.cp[1] = in_colour.cp[2]; // green
177         out_colour.cp[2] = in_colour.cp[1]; // blue
178         out_colour.cp[3] = in_colour.cp[0]; // alpha
179         
180         return out_colour.integer;
181 }
182
183 /* Now the real converting starts... */
184 static unsigned int KX_Mcol2uint_new(MCol col)
185 {
186         /* color has to be converted without endian sensitivity. So no shifting! */
187         union
188         {
189                 MCol col;
190                 unsigned int integer;
191                 unsigned char cp[4];
192         } out_colour, in_colour;
193
194         in_colour.col = col;
195         out_colour.cp[0] = in_colour.cp[3]; // red
196         out_colour.cp[1] = in_colour.cp[2]; // green
197         out_colour.cp[2] = in_colour.cp[1]; // blue
198         out_colour.cp[3] = in_colour.cp[0]; // alpha
199         
200         return out_colour.integer;
201 }
202
203 static void SetDefaultFaceType(Scene* scene)
204 {
205         default_face_mode = TF_DYNAMIC;
206         Base *base = static_cast<Base*>(scene->base.first);
207         while(base)
208         {
209                 if (base->object->type == OB_LAMP)
210                 {
211                         default_face_mode = TF_DYNAMIC|TF_LIGHT;
212                         return;
213                 }
214                 base = base->next;
215         }
216 }
217
218 RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools* rendertools, KX_Scene* scene, KX_BlenderSceneConverter *converter)
219 {
220         RAS_MeshObject *meshobj;
221         bool    skinMesh = false;
222         
223         int lightlayer = blenderobj->lay;
224         
225         // Determine if we need to make a skinned mesh
226         if (mesh->dvert){
227                 meshobj = new BL_SkinMeshObject(lightlayer);
228                 skinMesh = true;
229         }
230         else {
231                 meshobj = new RAS_MeshObject(lightlayer);
232         }
233         
234         meshobj->SetName(mesh->id.name);
235         
236         MFace* mface = static_cast<MFace*>(mesh->mface);
237         TFace* tface = static_cast<TFace*>(mesh->tface);
238         assert(mface);
239         MCol* mmcol = mesh->mcol;
240         
241         meshobj->m_xyz_index_to_vertex_index_mapping.resize(mesh->totvert);
242         
243         for (int f=0;f<mesh->totface;f++,mface++)
244         {
245                 
246                 bool collider = true;
247                 
248                 // only add valid polygons
249                 if (mface->v3)
250                 {
251                         MT_Point2 uv0(0.0,0.0),uv1(0.0,0.0),uv2(0.0,0.0),uv3(0.0,0.0);
252                         // rgb3 is set from the adjoint face in a square
253                         unsigned int rgb0,rgb1,rgb2,rgb3 = 0;
254                         MT_Vector3      no0(mesh->mvert[mface->v1].no[0], mesh->mvert[mface->v1].no[1], mesh->mvert[mface->v1].no[2]),
255                                         no1(mesh->mvert[mface->v2].no[0], mesh->mvert[mface->v2].no[1], mesh->mvert[mface->v2].no[2]),
256                                         no2(mesh->mvert[mface->v3].no[0], mesh->mvert[mface->v3].no[1], mesh->mvert[mface->v3].no[2]),
257                                         no3(0.0, 0.0, 0.0);
258                         MT_Point3       pt0(mesh->mvert[mface->v1].co),
259                                         pt1(mesh->mvert[mface->v2].co),
260                                         pt2(mesh->mvert[mface->v3].co),
261                                         pt3(0.0, 0.0, 0.0);
262                         
263                         no0 /= 32767.0;
264                         no1 /= 32767.0;
265                         no2 /= 32767.0;
266                         if (mface->v4)
267                         {
268                                 pt3 = MT_Point3(mesh->mvert[mface->v4].co);
269                                 no3 = MT_Vector3(mesh->mvert[mface->v4].no[0], mesh->mvert[mface->v4].no[1], mesh->mvert[mface->v4].no[2]);
270                                 no3 /= 32767.0;
271                         }
272         
273                         if(!(mface->flag & ME_SMOOTH))
274                         {
275                                 MT_Vector3 norm = ((pt1-pt0).cross(pt2-pt0)).safe_normalized();
276                                 norm[0] = ((int) (10*norm[0]))/10.0;
277                                 norm[1] = ((int) (10*norm[1]))/10.0;
278                                 norm[2] = ((int) (10*norm[2]))/10.0;
279                                 no0=no1=no2=no3= norm;
280         
281                         }
282                 
283                         {
284                                 Image* bima = ((mesh->tface && tface) ? (Image*) tface->tpage : NULL);
285         
286                                 STR_String imastr = 
287                                         ((mesh->tface && tface) ? 
288                                         (bima? (bima)->id.name : "" ) : "" );
289                 
290                                 char transp=0;
291                                 short mode=0, tile=0;
292                                 int     tilexrep=4,tileyrep = 4;
293                                 
294                                 if (bima)
295                                 {
296                                         tilexrep = bima->xrep;
297                                         tileyrep = bima->yrep;
298                         
299                                 }
300         
301                                 Material* ma = give_current_material(blenderobj, 1 /* mface->mat_nr */);
302                                 const char* matnameptr = (ma ? ma->id.name : "");
303                                 
304                                 bool polyvisible = true;
305                                 if (mesh->tface && tface)
306                                 {
307                                         // Use texface colors if available
308                                         //TF_DYNAMIC means the polygon is a collision face
309                                         collider = (tface->mode & TF_DYNAMIC != 0);
310                                         transp = tface->transp;
311                                         tile = tface->tile;
312                                         mode = tface->mode;
313                                         
314                                         polyvisible = !((tface->flag & TF_HIDE)||(tface->mode & TF_INVISIBLE));
315                                         
316                                         uv0 = MT_Point2(tface->uv[0]);
317                                         uv1 = MT_Point2(tface->uv[1]);
318                                         uv2 = MT_Point2(tface->uv[2]);
319                                         rgb0 = KX_rgbaint2uint_new(tface->col[0]);
320                                         rgb1 = KX_rgbaint2uint_new(tface->col[1]);
321                                         rgb2 = KX_rgbaint2uint_new(tface->col[2]);
322         
323                                         if (mface->v4)
324                                         {
325                                                 uv3 = MT_Point2(tface->uv[3]);
326                                                 rgb3 = KX_rgbaint2uint_new(tface->col[3]);
327                                         } 
328                                 } 
329                                 else
330                                 {
331                                         //
332                                         if (mmcol)
333                                         {
334                                                 // Use vertex colours
335                                                 rgb0 = KX_Mcol2uint_new(mmcol[0]);
336                                                 rgb1 = KX_Mcol2uint_new(mmcol[1]);
337                                                 rgb2 = KX_Mcol2uint_new(mmcol[2]);
338                                                 
339                                                 
340                                                 if (mface->v4)
341                                                 {
342                                                         rgb3 = KX_Mcol2uint_new(mmcol[3]);
343                                                         
344                                                 }
345                                         
346                                                 mmcol += 4;
347                                         }
348                                         else
349                                         {
350                                                 // If there are no vertex colors OR texfaces,
351                                                 // Initialize face to white and set COLLSION true and everything else FALSE
352                                                 unsigned int colour = 0xFFFFFFFFL;
353                                                 mode = default_face_mode;       
354                                                 transp = TF_SOLID;
355                                                 tile = 0;
356                                                 if (ma)
357                                                 {
358                                                         // If we have a material, take the default colour from the material.
359                                                         union
360                                                         {
361                                                                 unsigned char cp[4];
362                                                                 unsigned int integer;
363                                                         } col_converter;
364                                                         
365                                                         col_converter.cp[3] = (unsigned char) (ma->r*255.0);
366                                                         col_converter.cp[2] = (unsigned char) (ma->g*255.0);
367                                                         col_converter.cp[1] = (unsigned char) (ma->b*255.0);
368                                                         col_converter.cp[0] = (unsigned char) (ma->alpha*255.0);
369                                                         
370                                                         colour = col_converter.integer;
371                                                 }
372                                                 
373                                                 rgb0 = KX_rgbaint2uint_new(colour);
374                                                 rgb1 = KX_rgbaint2uint_new(colour);
375                                                 rgb2 = KX_rgbaint2uint_new(colour);     
376                                                 
377                                                 if (mface->v4)
378                                                         rgb3 = KX_rgbaint2uint_new(colour);
379                                         }
380                                 }
381                                         
382                                 
383                                 bool istriangle = (mface->v4==0);
384                                 bool zsort = ma?(ma->mode & MA_ZTRA) != 0:false;
385                                 
386                                 RAS_IPolyMaterial* polymat = rendertools->CreateBlenderPolyMaterial(imastr, false, matnameptr,
387                                         tile, tilexrep, tileyrep, 
388                                         mode, transp, zsort, lightlayer, istriangle, blenderobj, tface);
389         
390                                 if (ma)
391                                 {
392                                         polymat->m_specular = MT_Vector3(ma->specr, ma->specg, ma->specb)*ma->spec;
393                                         polymat->m_shininess = (float)ma->har/4.0; // 0 < ma->har <= 512
394                                         polymat->m_diffuse = MT_Vector3(ma->r, ma->g, ma->b)*(ma->emit + ma->ref);
395
396                                 } else
397                                 {
398                                         polymat->m_specular = MT_Vector3(0.0f,0.0f,0.0f);
399                                         polymat->m_shininess = 35.0;
400                                 }
401
402                         
403                                 // this is needed to free up memory afterwards
404                                 converter->RegisterPolyMaterial(polymat);
405         
406                                 RAS_MaterialBucket* bucket = scene->FindBucket(polymat);
407                                                          
408                                 int nverts = mface->v4?4:3;
409                                 int vtxarray = meshobj->FindVertexArray(nverts,polymat);
410                                 RAS_Polygon* poly = new RAS_Polygon(bucket,polyvisible,nverts,vtxarray);
411                                 if (skinMesh) {
412                                         int d1, d2, d3, d4;
413                                         bool flat;
414
415                                         /* If the face is set to solid, all fnors are the same */
416                                         if (mface->flag & ME_SMOOTH)
417                                                 flat = false;
418                                         else
419                                                 flat = true;
420                                         
421                                         d1=((BL_SkinMeshObject*)meshobj)->FindOrAddDeform(vtxarray, mface->v1, &mesh->dvert[mface->v1], polymat);
422                                         d2=((BL_SkinMeshObject*)meshobj)->FindOrAddDeform(vtxarray, mface->v2, &mesh->dvert[mface->v2], polymat);
423                                         d3=((BL_SkinMeshObject*)meshobj)->FindOrAddDeform(vtxarray, mface->v3, &mesh->dvert[mface->v3], polymat);
424                                         if (nverts==4)
425                                                 d4=((BL_SkinMeshObject*)meshobj)->FindOrAddDeform(vtxarray, mface->v4, &mesh->dvert[mface->v4], polymat);
426                                         poly->SetVertex(0,((BL_SkinMeshObject*)meshobj)->FindOrAddVertex(vtxarray,pt0,uv0,rgb0,no0,d1,flat, polymat));
427                                         poly->SetVertex(1,((BL_SkinMeshObject*)meshobj)->FindOrAddVertex(vtxarray,pt1,uv1,rgb1,no1,d2,flat, polymat));
428                                         poly->SetVertex(2,((BL_SkinMeshObject*)meshobj)->FindOrAddVertex(vtxarray,pt2,uv2,rgb2,no2,d3,flat, polymat));
429                                         if (nverts==4)
430                                                 poly->SetVertex(3,((BL_SkinMeshObject*)meshobj)->FindOrAddVertex(vtxarray,pt3,uv3,rgb3,no3,d4, flat,polymat));
431                                 }
432                                 else
433                                 {
434                                         poly->SetVertex(0,meshobj->FindOrAddVertex(vtxarray,pt0,uv0,rgb0,no0,polymat,mface->v1));
435                                         poly->SetVertex(1,meshobj->FindOrAddVertex(vtxarray,pt1,uv1,rgb1,no1,polymat,mface->v2));
436                                         poly->SetVertex(2,meshobj->FindOrAddVertex(vtxarray,pt2,uv2,rgb2,no2,polymat,mface->v3));
437                                         if (nverts==4)
438                                                 poly->SetVertex(3,meshobj->FindOrAddVertex(vtxarray,pt3,uv3,rgb3,no3,polymat,mface->v4));
439                                 }
440                                 meshobj->AddPolygon(poly);
441                                 if (poly->IsCollider())
442                                 {
443                                         RAS_TriangleIndex idx;
444                                         idx.m_index[0] = mface->v1;
445                                         idx.m_index[1] = mface->v2;
446                                         idx.m_index[2] = mface->v3;
447                                         idx.m_collider = collider;
448                                         meshobj->m_triangle_indices.push_back(idx);
449                                         if (nverts==4)
450                                         {
451                                         idx.m_index[0] = mface->v1;
452                                         idx.m_index[1] = mface->v3;
453                                         idx.m_index[2] = mface->v4;
454                                         idx.m_collider = collider;
455                                         meshobj->m_triangle_indices.push_back(idx);
456                                         }
457                                 }
458                                 
459                                 poly->SetVisibleWireframeEdges(mface->edcode);
460                                 poly->SetCollider(collider);
461                         }
462                 }
463                 if (tface) 
464                         tface++;
465         }
466         meshobj->UpdateMaterialList();
467         
468         return meshobj;
469 }
470
471 static PHY_MaterialProps g_materialProps = {
472         1.0,    // restitution
473         2.0,    // friction 
474         0.0,    // fh spring constant
475         0.0,    // fh damping
476         0.0,    // fh distance
477         false   // sliding material?
478 };
479
480         
481         
482 static PHY_MaterialProps *CreateMaterialFromBlenderObject(struct Object* blenderobject,
483                                                                                                   KX_Scene *kxscene)
484 {
485         PHY_MaterialProps *materialProps = new PHY_MaterialProps;
486         
487         assert(materialProps);
488                 
489         Material* blendermat = give_current_material(blenderobject, 0);
490                 
491         if (blendermat)
492         {
493                 assert(0.0f <= blendermat->reflect && blendermat->reflect <= 1.0f);
494         
495                 materialProps->m_restitution = blendermat->reflect;
496                 materialProps->m_friction = blendermat->friction;
497                 materialProps->m_fh_spring = blendermat->fh;
498                 materialProps->m_fh_damping = blendermat->xyfrict;
499                 materialProps->m_fh_distance = blendermat->fhdist;
500                 materialProps->m_fh_normal = (blendermat->dynamode & MA_FH_NOR) != 0;
501         }
502         else {
503                 *materialProps = g_materialProps;
504         }
505         
506         return materialProps;
507 }
508
509 static PHY_ShapeProps *CreateShapePropsFromBlenderObject(struct Object* blenderobject,
510                                                                                                  KX_Scene *kxscene)
511 {
512         PHY_ShapeProps *shapeProps = new PHY_ShapeProps;
513         
514         assert(shapeProps);
515                 
516         shapeProps->m_mass = blenderobject->mass;
517         
518 //  This needs to be fixed in blender. For now, we use:
519         
520 // in Blender, inertia stands for the size value which is equivalent to
521 // the sphere radius
522         shapeProps->m_inertia = blenderobject->formfactor;
523         
524         assert(0.0f <= blenderobject->damping && blenderobject->damping <= 1.0f);
525         assert(0.0f <= blenderobject->rdamping && blenderobject->rdamping <= 1.0f);
526         
527         shapeProps->m_lin_drag = 1.0 - blenderobject->damping;
528         shapeProps->m_ang_drag = 1.0 - blenderobject->rdamping;
529         
530         shapeProps->m_friction_scaling[0] = blenderobject->anisotropicFriction[0]; 
531         shapeProps->m_friction_scaling[1] = blenderobject->anisotropicFriction[1];
532         shapeProps->m_friction_scaling[2] = blenderobject->anisotropicFriction[2];
533         shapeProps->m_do_anisotropic = ((blenderobject->gameflag & OB_ANISOTROPIC_FRICTION) != 0);
534         
535         shapeProps->m_do_fh     = (blenderobject->gameflag & OB_DO_FH) != 0; 
536         shapeProps->m_do_rot_fh = (blenderobject->gameflag & OB_ROT_FH) != 0;
537         
538         return shapeProps;
539 }
540
541         
542         
543         
544                 
545 //////////////////////////////////////////////////////////
546         
547
548
549 static float my_boundbox_mesh(Mesh *me, float *loc, float *size)
550 {
551         MVert *mvert;
552         BoundBox *bb;
553         MT_Point3 min, max;
554         float mloc[3], msize[3];
555         int a;
556         
557         if(me->bb==0) me->bb= (struct BoundBox *)MEM_callocN(sizeof(BoundBox), "boundbox");
558         bb= me->bb;
559         
560         INIT_MINMAX(min, max);
561
562         if (!loc) loc= mloc;
563         if (!size) size= msize;
564         
565         mvert= me->mvert;
566         for(a=0; a<me->totvert; a++, mvert++) {
567                 DO_MINMAX(mvert->co, min, max);
568         }
569                 
570         if(me->totvert) {
571                 loc[0]= (min[0]+max[0])/2.0;
572                 loc[1]= (min[1]+max[1])/2.0;
573                 loc[2]= (min[2]+max[2])/2.0;
574                 
575                 size[0]= (max[0]-min[0])/2.0;
576                 size[1]= (max[1]-min[1])/2.0;
577                 size[2]= (max[2]-min[2])/2.0;
578         }
579         else {
580                 loc[0]= loc[1]= loc[2]= 0.0;
581                 size[0]= size[1]= size[2]= 0.0;
582         }
583                 
584         bb->vec[0][0]=bb->vec[1][0]=bb->vec[2][0]=bb->vec[3][0]= loc[0]-size[0];
585         bb->vec[4][0]=bb->vec[5][0]=bb->vec[6][0]=bb->vec[7][0]= loc[0]+size[0];
586                 
587         bb->vec[0][1]=bb->vec[1][1]=bb->vec[4][1]=bb->vec[5][1]= loc[1]-size[1];
588         bb->vec[2][1]=bb->vec[3][1]=bb->vec[6][1]=bb->vec[7][1]= loc[1]+size[1];
589
590         bb->vec[0][2]=bb->vec[3][2]=bb->vec[4][2]=bb->vec[7][2]= loc[2]-size[2];
591         bb->vec[1][2]=bb->vec[2][2]=bb->vec[5][2]=bb->vec[6][2]= loc[2]+size[2];
592
593         float radius = 0;
594         for (a=0, mvert = me->mvert; a < me->totvert; a++, mvert++)
595         {
596                 float vert_radius = MT_Vector3(mvert->co).length2();
597                 if (vert_radius > radius)
598                         radius = vert_radius;
599         } 
600         return sqrt(radius);
601 }
602                 
603
604
605
606 static void my_tex_space_mesh(Mesh *me)
607                 {
608         KeyBlock *kb;
609         float *fp, loc[3], size[3], min[3], max[3];
610         int a;
611
612         my_boundbox_mesh(me, loc, size);
613         
614         if(me->texflag & AUTOSPACE) {
615                 if(me->key) {
616                         kb= me->key->refkey;
617                         if (kb) {
618         
619                                 INIT_MINMAX(min, max);
620                 
621                                 fp= (float *)kb->data;
622                                 for(a=0; a<kb->totelem; a++, fp+=3) {   
623                                         DO_MINMAX(fp, min, max);
624                                 }
625                                 if(kb->totelem) {
626                                         loc[0]= (min[0]+max[0])/2.0; loc[1]= (min[1]+max[1])/2.0; loc[2]= (min[2]+max[2])/2.0;
627                                         size[0]= (max[0]-min[0])/2.0; size[1]= (max[1]-min[1])/2.0; size[2]= (max[2]-min[2])/2.0;
628         } 
629         else {
630                                         loc[0]= loc[1]= loc[2]= 0.0;
631                                         size[0]= size[1]= size[2]= 0.0;
632                                 }
633                                 
634                         }
635                                 }
636         
637                 VECCOPY(me->loc, loc);
638                 VECCOPY(me->size, size);
639                 me->rot[0]= me->rot[1]= me->rot[2]= 0.0;
640         
641                 if(me->size[0]==0.0) me->size[0]= 1.0;
642                 else if(me->size[0]>0.0 && me->size[0]<0.00001) me->size[0]= 0.00001;
643                 else if(me->size[0]<0.0 && me->size[0]> -0.00001) me->size[0]= -0.00001;
644         
645                 if(me->size[1]==0.0) me->size[1]= 1.0;
646                 else if(me->size[1]>0.0 && me->size[1]<0.00001) me->size[1]= 0.00001;
647                 else if(me->size[1]<0.0 && me->size[1]> -0.00001) me->size[1]= -0.00001;
648                                                 
649                 if(me->size[2]==0.0) me->size[2]= 1.0;
650                 else if(me->size[2]>0.0 && me->size[2]<0.00001) me->size[2]= 0.00001;
651                 else if(me->size[2]<0.0 && me->size[2]> -0.00001) me->size[2]= -0.00001;
652         }
653         
654 }
655
656 static void my_get_local_bounds(Object *ob, float *centre, float *size)
657 {
658         BoundBox *bb= NULL;
659         /* uses boundbox, function used by Ketsji */
660         switch (ob->type)
661         {
662                 case OB_MESH:
663                         bb= ( (Mesh *)ob->data )->bb;
664                         if(bb==0) 
665                         {
666                                 my_tex_space_mesh((struct Mesh *)ob->data);
667                                 bb= ( (Mesh *)ob->data )->bb;
668                         }
669                         break;
670                 case OB_CURVE:
671                 case OB_SURF:
672                 case OB_FONT:
673                         centre[0]= centre[1]= centre[2]= 0.0;
674                         size[0]  = size[1]=size[2]=0.0;
675                         break;
676                 case OB_MBALL:
677                         bb= ob->bb;
678                         break;
679         }
680         
681         if(bb==NULL) 
682         {
683                 centre[0]= centre[1]= centre[2]= 0.0;
684                 size[0] = size[1]=size[2]=1.0;
685         }
686         else 
687         {
688                 size[0]= 0.5*fabs(bb->vec[0][0] - bb->vec[4][0]);
689                 size[1]= 0.5*fabs(bb->vec[0][1] - bb->vec[2][1]);
690                 size[2]= 0.5*fabs(bb->vec[0][2] - bb->vec[1][2]);
691                                         
692                 centre[0]= 0.5*(bb->vec[0][0] + bb->vec[4][0]);
693                 centre[1]= 0.5*(bb->vec[0][1] + bb->vec[2][1]);
694                 centre[2]= 0.5*(bb->vec[0][2] + bb->vec[1][2]);
695         }
696 }
697         
698
699
700
701 //////////////////////////////////////////////////////
702
703
704
705
706
707 void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
708                                                  struct Object* blenderobject,
709                                                  RAS_MeshObject* meshobj,
710                                                  KX_Scene* kxscene,
711                                                  int activeLayerBitInfo,
712                                                  e_PhysicsEngine        physics_engine,
713                                                  KX_BlenderSceneConverter *converter
714                                                  )
715                                         
716 {
717         SYS_SystemHandle syshandle = SYS_GetSystem();
718         //int userigidbody = SYS_GetCommandLineInt(syshandle,"norigidbody",0);
719         //bool bRigidBody = (userigidbody == 0);
720
721         PHY_ShapeProps* shapeprops =
722                         CreateShapePropsFromBlenderObject(blenderobject, 
723                         kxscene);
724
725         
726         PHY_MaterialProps* smmaterial = 
727                 CreateMaterialFromBlenderObject(blenderobject, kxscene);
728                                         
729         KX_ObjectProperties objprop;
730         if ((objprop.m_isactor = (blenderobject->gameflag & OB_ACTOR)!=0))
731         {
732                 objprop.m_dyna = (blenderobject->gameflag & OB_DYNAMIC) != 0;
733                 objprop.m_angular_rigidbody = (blenderobject->gameflag & OB_RIGID_BODY) != 0;
734                 objprop.m_ghost = (blenderobject->gameflag & OB_GHOST) != 0;
735         } else {
736                 objprop.m_dyna = false;
737                 objprop.m_angular_rigidbody = false;
738                 objprop.m_ghost = false;
739         }
740         //mmm, for now, taks this for the size of the dynamicobject
741         // Blender uses inertia for radius of dynamic object
742         objprop.m_radius = blenderobject->inertia;
743         objprop.m_in_active_layer = (blenderobject->lay & activeLayerBitInfo) != 0;
744         objprop.m_dynamic_parent=NULL;
745         objprop.m_isdeformable = ((blenderobject->gameflag2 & 2)) != 0;
746         objprop.m_boundclass = objprop.m_dyna?KX_BOUNDSPHERE:KX_BOUNDMESH;
747         
748         KX_BoxBounds bb;
749         my_get_local_bounds(blenderobject,objprop.m_boundobject.box.m_center,bb.m_extends);
750         if (blenderobject->gameflag & OB_BOUNDS)
751         {
752                 switch (blenderobject->boundtype)
753                 {
754                         case OB_BOUND_BOX:
755                                 objprop.m_boundclass = KX_BOUNDBOX;
756                                 //mmm, has to be divided by 2 to be proper extends
757                                 objprop.m_boundobject.box.m_extends[0]=2.f*bb.m_extends[0];
758                                 objprop.m_boundobject.box.m_extends[1]=2.f*bb.m_extends[1];
759                                 objprop.m_boundobject.box.m_extends[2]=2.f*bb.m_extends[2];
760                                 break;
761                         case OB_BOUND_POLYT:
762                                 if (blenderobject->type == OB_MESH)
763                                 {
764                                         objprop.m_boundclass = KX_BOUNDPOLYTOPE;
765                                         break;
766                                 }
767                                 // Object is not a mesh... fall through OB_BOUND_POLYH to 
768                                 // OB_BOUND_SPHERE
769                         case OB_BOUND_POLYH:
770                                 if (blenderobject->type == OB_MESH)
771                                 {
772                                         objprop.m_boundclass = KX_BOUNDMESH;
773                                         break;
774                                 }
775                                 // Object is not a mesh... can't use polyheder. 
776                                 // Fall through and become a sphere.
777                         case OB_BOUND_SPHERE:
778                         {
779                                 objprop.m_boundclass = KX_BOUNDSPHERE;
780                                 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], MT_max(bb.m_extends[1], bb.m_extends[2]));
781                                 break;
782                         }
783                         case OB_BOUND_CYLINDER:
784                         {
785                                 objprop.m_boundclass = KX_BOUNDCYLINDER;
786                                 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], bb.m_extends[1]);
787                                 objprop.m_boundobject.c.m_height = 2.f*bb.m_extends[2];
788                                 break;
789                         }
790                         case OB_BOUND_CONE:
791                         {
792                                 objprop.m_boundclass = KX_BOUNDCONE;
793                                 objprop.m_boundobject.c.m_radius = MT_max(bb.m_extends[0], bb.m_extends[1]);
794                                 objprop.m_boundobject.c.m_height = 2.f*bb.m_extends[2];
795                                 break;
796                         }
797                 }
798         }
799
800         // get Root Parent of blenderobject
801         struct Object* parent= blenderobject->parent;
802         while(parent && parent->parent) {
803                 parent= parent->parent;
804         }
805
806         if (parent && (parent->gameflag & OB_DYNAMIC)) {
807                 
808                 KX_GameObject *parentgameobject = converter->FindGameObject(parent);
809                 objprop.m_dynamic_parent = parentgameobject;
810
811         }
812
813         objprop.m_concave = (blenderobject->boundtype & 4) != 0;
814         
815         switch (physics_engine)
816         {
817 #ifdef USE_SUMO_SOLID
818                 case UseSumo:
819                         KX_ConvertSumoObject(gameobj, meshobj, kxscene, shapeprops, smmaterial, &objprop);
820                         break;
821 #endif
822                         
823 #ifdef USE_ODE
824                 case UseODE:
825                         KX_ConvertODEEngineObject(gameobj, meshobj, kxscene, shapeprops, smmaterial, &objprop);
826                         break;
827 #endif //USE_ODE
828
829                 case UseDynamo:
830                         //KX_ConvertDynamoObject(gameobj,meshobj,kxscene,shapeprops,    smmaterial,     &objprop);
831                         break;
832                         
833                 case UseNone:
834                 default:
835                         break;
836         }
837
838 }
839
840
841
842
843
844 static KX_LightObject *gamelight_from_blamp(Lamp *la, unsigned int layerflag, KX_Scene *kxscene, RAS_IRenderTools *rendertools, KX_BlenderSceneConverter *converter) {
845         RAS_LightObject lightobj;
846         KX_LightObject *gamelight;
847         
848         lightobj.m_att1 = la->att1;
849         lightobj.m_att2 = (la->mode & LA_QUAD)?la->att2:0.0;
850         lightobj.m_red = la->r;
851         lightobj.m_green = la->g;
852         lightobj.m_blue = la->b;
853         lightobj.m_distance = la->dist;
854         lightobj.m_energy = la->energy;
855         lightobj.m_layer = layerflag;
856         lightobj.m_spotblend = la->spotblend;
857         lightobj.m_spotsize = la->spotsize;
858         
859         lightobj.m_nodiffuse = (la->mode & LA_NO_DIFF) != 0;
860         lightobj.m_nospecular = (la->mode & LA_NO_SPEC) != 0;
861         
862         if (la->mode & LA_NEG)
863         {
864                 lightobj.m_red = -lightobj.m_red;
865                 lightobj.m_green = -lightobj.m_green;
866                 lightobj.m_blue = -lightobj.m_blue;
867         }
868                 
869         if (la->type==LA_SUN) {
870                 lightobj.m_type = RAS_LightObject::LIGHT_SUN;
871         } else if (la->type==LA_SPOT) {
872                 lightobj.m_type = RAS_LightObject::LIGHT_SPOT;
873         } else {
874                 lightobj.m_type = RAS_LightObject::LIGHT_NORMAL;
875         }
876         
877         gamelight = new KX_LightObject(kxscene, KX_Scene::m_callbacks, rendertools, lightobj);
878         BL_ConvertLampIpos(la, gamelight, converter);
879         
880         return gamelight;
881 }
882
883 static KX_Camera *gamecamera_from_bcamera(Camera *ca, KX_Scene *kxscene, KX_BlenderSceneConverter *converter) {
884         RAS_CameraData camdata(ca->lens, ca->clipsta, ca->clipend, ca->type == CAM_PERSP);
885         KX_Camera *gamecamera;
886         
887         gamecamera= new KX_Camera(kxscene, KX_Scene::m_callbacks, camdata);
888         gamecamera->SetName(ca->id.name + 2);
889         
890         BL_ConvertCameraIpos(ca, gamecamera, converter);
891         
892         return gamecamera;
893 }
894
895 static KX_GameObject *gameobject_from_blenderobject(
896                                                                 Object *ob, 
897                                                                 KX_Scene *kxscene, 
898                                                                 RAS_IRenderTools *rendertools, 
899                                                                 KX_BlenderSceneConverter *converter,
900                                                                 Scene *blenderscene) 
901 {
902         KX_GameObject *gameobj = NULL;
903         
904         switch(ob->type)
905         {
906         case OB_LAMP:
907         {
908                 KX_LightObject* gamelight= gamelight_from_blamp(static_cast<Lamp*>(ob->data), ob->lay, kxscene, rendertools, converter);
909                 gameobj = gamelight;
910                 
911                 gamelight->AddRef();
912                 kxscene->GetLightList()->Add(gamelight);
913                 
914                 break;
915         }
916         
917         case OB_CAMERA:
918         {
919                 KX_Camera* gamecamera = gamecamera_from_bcamera(static_cast<Camera*>(ob->data), kxscene, converter);
920                 gameobj = gamecamera;
921                 
922                 gamecamera->AddRef();
923                 kxscene->AddCamera(gamecamera);
924                 
925                 break;
926         }
927         
928         case OB_MESH:
929         {
930                 Mesh* mesh = static_cast<Mesh*>(ob->data);
931                 RAS_MeshObject* meshobj = converter->FindGameMesh(mesh, ob->lay);
932                 float centre[3], extents[3];
933                 float radius = my_boundbox_mesh((Mesh*) ob->data, centre, extents);
934                 
935                 if (!meshobj) {
936                         meshobj = BL_ConvertMesh(mesh,ob,rendertools,kxscene,converter);
937                         converter->RegisterGameMesh(meshobj, mesh);
938                 }
939                 
940                 // needed for python scripting
941                 kxscene->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj);
942         
943                 gameobj = new BL_DeformableGameObject(kxscene,KX_Scene::m_callbacks);
944         
945                 // set transformation
946                 gameobj->AddMesh(meshobj);
947         
948                 // for all objects: check whether they want to
949                 // respond to updates
950                 bool ignoreActivityCulling =  
951                         ((ob->gameflag2 & OB_NEVER_DO_ACTIVITY_CULLING)!=0);
952                 gameobj->SetIgnoreActivityCulling(ignoreActivityCulling);
953         
954                 //      If this is a skin object, make Skin Controller
955                 if (ob->parent && ob->parent->type == OB_ARMATURE && ob->partype==PARSKEL && ((Mesh*)ob->data)->dvert){
956                         BL_SkinDeformer *dcont = new BL_SkinDeformer(ob, (BL_SkinMeshObject*)meshobj);                          
957                         ((BL_DeformableGameObject*)gameobj)->m_pDeformer = dcont;
958                 }
959                 else if (((Mesh*)ob->data)->dvert){
960                         BL_MeshDeformer *dcont = new BL_MeshDeformer(ob, (BL_SkinMeshObject*)meshobj);
961                         ((BL_DeformableGameObject*)gameobj)->m_pDeformer = dcont;
962                 }
963                 
964                 MT_Point3 min = MT_Point3(centre) - MT_Vector3(extents);
965                 MT_Point3 max = MT_Point3(centre) + MT_Vector3(extents);
966                 SG_BBox bbox = SG_BBox(min, max);
967                 gameobj->GetSGNode()->SetBBox(bbox);
968                 gameobj->GetSGNode()->SetRadius(radius);
969         
970                 break;
971         }
972         
973         case OB_ARMATURE:
974         {
975                 gameobj = new BL_ArmatureObject (kxscene, KX_Scene::m_callbacks,
976                         (bArmature*)ob->data,
977                         ob->pose);
978         
979                 /* Get the current pose from the armature object and apply it as the rest pose */
980                 break;
981         }
982         
983         case OB_EMPTY:
984         {
985                 gameobj = new KX_EmptyObject(kxscene,KX_Scene::m_callbacks);
986                 // set transformation
987                 break;
988         }
989         }
990         
991         return gameobj;
992 }
993
994 struct parentChildLink {
995         struct Object* m_blenderchild;
996         SG_Node* m_gamechildnode;
997 };
998
999         /**
1000          * Find the specified scene by name, or the first
1001          * scene if nothing matches (shouldn't happen).
1002          */
1003 static struct Scene *GetSceneForName(struct Main *maggie, const STR_String& scenename) {
1004         Scene *sce;
1005
1006         for (sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
1007                 if (scenename == (sce->id.name+2))
1008                         return sce;
1009
1010         return (Scene*) maggie->scene.first;
1011 }
1012
1013 // convert blender objects into ketsji gameobjects
1014 void BL_ConvertBlenderObjects(struct Main* maggie,
1015                                                           const STR_String& scenename,
1016                                                           KX_Scene* kxscene,
1017                                                           KX_KetsjiEngine* ketsjiEngine,
1018                                                           e_PhysicsEngine       physics_engine,
1019                                                           PyObject* pythondictionary,
1020                                                           SCA_IInputDevice* keydev,
1021                                                           RAS_IRenderTools* rendertools,
1022                                                           RAS_ICanvas* canvas,
1023                                                           KX_BlenderSceneConverter* converter,
1024                                                           bool alwaysUseExpandFraming
1025                                                           )
1026 {       
1027         Scene *blenderscene = GetSceneForName(maggie, scenename);
1028
1029         // Get the frame settings of the canvas.
1030         // Get the aspect ratio of the canvas as designed by the user.
1031
1032         RAS_FrameSettings::RAS_FrameType frame_type;
1033         int aspect_width;
1034         int aspect_height;
1035         
1036         if (alwaysUseExpandFraming) {
1037                 frame_type = RAS_FrameSettings::e_frame_extend;
1038                 aspect_width = canvas->GetWidth();
1039                 aspect_height = canvas->GetHeight();
1040         } else {
1041                 if (blenderscene->framing.type == SCE_GAMEFRAMING_BARS) {
1042                         frame_type = RAS_FrameSettings::e_frame_bars;
1043                 } else if (blenderscene->framing.type == SCE_GAMEFRAMING_EXTEND) {
1044                         frame_type = RAS_FrameSettings::e_frame_extend;
1045                 } else {
1046                         frame_type = RAS_FrameSettings::e_frame_scale;
1047                 }
1048                 
1049                 aspect_width = blenderscene->r.xsch;
1050                 aspect_height = blenderscene->r.ysch;
1051         }
1052         
1053         RAS_FrameSettings frame_settings(
1054                 frame_type,
1055                 blenderscene->framing.col[0],
1056                 blenderscene->framing.col[1],
1057                 blenderscene->framing.col[2],
1058                 aspect_width,
1059                 aspect_height
1060         );
1061         kxscene->SetFramingType(frame_settings);
1062
1063         kxscene->SetGravity(MT_Vector3(0,0,(blenderscene->world != NULL) ? -blenderscene->world->gravity : -9.8));
1064         
1065         /* set activity culling parameters */
1066         if (blenderscene->world) {
1067                 kxscene->SetActivityCulling( (blenderscene->world->mode & WO_ACTIVITY_CULLING) != 0);
1068                 kxscene->SetActivityCullingRadius(blenderscene->world->activityBoxRadius);
1069         } else {
1070                 kxscene->SetActivityCulling(false);
1071         }
1072         
1073         int activeLayerBitInfo = blenderscene->lay;
1074         
1075         // templist to find Root Parents (object with no parents)
1076         CListValue* templist = new CListValue();
1077         CListValue*     sumolist = new CListValue();
1078         
1079         vector<parentChildLink> vec_parent_child;
1080         
1081         CListValue* objectlist = kxscene->GetObjectList();
1082         CListValue* parentlist = kxscene->GetRootParentList();
1083         
1084         SCA_LogicManager* logicmgr = kxscene->GetLogicManager();
1085         SCA_TimeEventManager* timemgr = kxscene->GetTimeEventManager();
1086         
1087         CListValue* logicbrick_conversionlist = new CListValue();
1088         
1089         SG_TreeFactory tf;
1090         
1091         // Convert actions to actionmap
1092         bAction *curAct;
1093         for (curAct = (bAction*)maggie->action.first; curAct; curAct=(bAction*)curAct->id.next)
1094         {
1095                 logicmgr->RegisterActionName(curAct->id.name, curAct);
1096         }
1097         
1098         SetDefaultFaceType(blenderscene);
1099         
1100         Base *base = static_cast<Base*>(blenderscene->base.first);
1101         while(base)
1102         {
1103                 Object* blenderobject = base->object;
1104                 KX_GameObject* gameobj = gameobject_from_blenderobject(
1105                                                                                 base->object, 
1106                                                                                 kxscene, 
1107                                                                                 rendertools, 
1108                                                                                 converter,
1109                                                                                 blenderscene);
1110                                                                                         
1111                 if (gameobj)
1112                 {
1113                         MT_Point3 pos = MT_Point3(
1114                                 blenderobject->loc[0]+blenderobject->dloc[0],
1115                                 blenderobject->loc[1]+blenderobject->dloc[1],
1116                                 blenderobject->loc[2]+blenderobject->dloc[2]
1117                         );
1118                         MT_Vector3 eulxyz = MT_Vector3(
1119                                 blenderobject->rot[0],
1120                                 blenderobject->rot[1],
1121                                 blenderobject->rot[2]
1122                         );
1123                         MT_Vector3 scale = MT_Vector3(
1124                                 blenderobject->size[0],
1125                                 blenderobject->size[1],
1126                                 blenderobject->size[2]
1127                         );
1128                         
1129                         gameobj->NodeSetLocalPosition(pos);
1130                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
1131                         gameobj->NodeSetLocalScale(scale);
1132                         gameobj->NodeUpdateGS(0,true);
1133                         
1134                         BL_ConvertIpos(blenderobject,gameobj,converter);
1135         
1136                         bool isInActiveLayer = (blenderobject->lay & activeLayerBitInfo) !=0;
1137         
1138                         sumolist->Add(gameobj->AddRef());
1139                         
1140                         BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene,isInActiveLayer);
1141                         
1142         
1143                         gameobj->SetName(blenderobject->id.name);
1144         
1145                         // templist to find Root Parents (object with no parents)
1146                         templist->Add(gameobj->AddRef());
1147                         
1148                         // update children/parent hierarchy
1149                         if (blenderobject->parent != 0)
1150                         {
1151                                 // blender has an additional 'parentinverse' offset in each object
1152                                 SG_Node* parentinversenode = new SG_Node(NULL,NULL,SG_Callbacks());
1153                         
1154                                 // define a normal parent relationship for this node.
1155                                 KX_NormalParentRelation * parent_relation = KX_NormalParentRelation::New();
1156                                 parentinversenode->SetParentRelation(parent_relation);
1157         
1158                                 parentChildLink pclink;
1159                                 pclink.m_blenderchild = blenderobject;
1160                                 pclink.m_gamechildnode = parentinversenode;
1161                                 vec_parent_child.push_back(pclink);
1162         
1163                                 float* fl = (float*) blenderobject->parentinv;
1164                                 MT_Transform parinvtrans(fl);
1165                                 parentinversenode->SetLocalPosition(parinvtrans.getOrigin());
1166                                 parentinversenode->SetLocalOrientation(parinvtrans.getBasis());
1167                                 parentinversenode->AddChild(gameobj->GetSGNode());
1168                         }
1169                         
1170                         // needed for python scripting
1171                         logicmgr->RegisterGameObjectName(gameobj->GetName(),gameobj);
1172         
1173                         converter->RegisterGameObject(gameobj, blenderobject);  
1174                         
1175                         // this was put in rapidly, needs to be looked at more closely
1176                         // only draw/use objects in active 'blender' layers
1177         
1178                         logicbrick_conversionlist->Add(gameobj->AddRef());
1179                         
1180                         if (isInActiveLayer)
1181                         {
1182                                 objectlist->Add(gameobj->AddRef());
1183                                 tf.Add(gameobj->GetSGNode());
1184                                 
1185                                 gameobj->NodeUpdateGS(0,true);
1186                                 gameobj->Bucketize();
1187                                 
1188                         }
1189                         
1190                 }
1191                         
1192                 base = base->next;
1193         }
1194
1195         if (blenderscene->camera) {
1196                 KX_Camera *gamecamera= (KX_Camera*) converter->FindGameObject(blenderscene->camera);
1197                 
1198                 kxscene->SetActiveCamera(gamecamera);
1199         }
1200
1201         //      Set up armatures
1202         for (base = static_cast<Base*>(blenderscene->base.first); base; base=base->next){
1203                 if (base->object->type==OB_MESH){
1204                         Mesh *me = (Mesh*)base->object->data;
1205         
1206                         if (me->dvert){
1207                                 KX_GameObject *obj = converter->FindGameObject(base->object);
1208         
1209                                 if (base->object->parent && base->object->parent->type==OB_ARMATURE && base->object->partype==PARSKEL){
1210                                         KX_GameObject *par = converter->FindGameObject(base->object->parent);
1211                                         if (par)
1212                                                 ((BL_SkinDeformer*)(((BL_DeformableGameObject*)obj)->m_pDeformer))->SetArmature((BL_ArmatureObject*) par);
1213                                 }
1214                         }
1215                 }
1216         }
1217         
1218         // create hierarchy information
1219         int i;
1220         vector<parentChildLink>::iterator pcit;
1221         
1222         for (pcit = vec_parent_child.begin();!(pcit==vec_parent_child.end());++pcit)
1223         {
1224         
1225                 struct Object* blenderchild = pcit->m_blenderchild;
1226                 if (blenderchild->partype == PARVERT1)
1227                 {
1228                         // creat a new vertex parent relationship for this node.
1229                         KX_VertexParentRelation * vertex_parent_relation = KX_VertexParentRelation::New();
1230                         pcit->m_gamechildnode->SetParentRelation(vertex_parent_relation);
1231                 } else 
1232                 if (blenderchild->partype == PARSLOW) 
1233                 {
1234                         // creat a new slow parent relationship for this node.
1235                         KX_SlowParentRelation * slow_parent_relation = KX_SlowParentRelation::New(blenderchild->sf);
1236                         pcit->m_gamechildnode->SetParentRelation(slow_parent_relation);
1237                 }       
1238         
1239                 struct Object* blenderparent = blenderchild->parent;
1240                 KX_GameObject* parentobj = converter->FindGameObject(blenderparent);
1241                 if (parentobj)
1242                 {
1243                         parentobj->     GetSGNode()->AddChild(pcit->m_gamechildnode);
1244                 }
1245         }
1246         vec_parent_child.clear();
1247         
1248         // find 'root' parents (object that has not parents in SceneGraph)
1249         for (i=0;i<templist->GetCount();++i)
1250         {
1251                 KX_GameObject* gameobj = (KX_GameObject*) templist->GetValue(i);
1252                 if (gameobj->GetSGNode()->GetSGParent() == 0)
1253                 {
1254                         parentlist->Add(gameobj->AddRef());
1255                         gameobj->NodeUpdateGS(0,true);
1256                 }
1257         }
1258         
1259         // create physics information
1260         for (i=0;i<sumolist->GetCount();i++)
1261         {
1262                 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
1263                 struct Object* blenderobject = converter->FindBlenderObject(gameobj);
1264                 int nummeshes = gameobj->GetMeshCount();
1265                 RAS_MeshObject* meshobj = 0;
1266
1267                 if (nummeshes > 0)
1268                 {
1269                         meshobj = gameobj->GetMesh(0);
1270                 }
1271
1272                 BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,activeLayerBitInfo,physics_engine,converter);
1273
1274         }
1275         
1276         templist->Release();
1277         sumolist->Release();    
1278
1279
1280         int executePriority=0; /* incremented by converter routines */
1281         
1282         // convert global sound stuff
1283
1284         /* XXX, glob is the very very wrong place for this
1285          * to be, re-enable once the listener has been moved into
1286          * the scene. */
1287 #if 1
1288         SND_Scene* soundscene = kxscene->GetSoundScene();
1289         SND_SoundListener* listener = soundscene->GetListener();
1290         if (listener && G.listener)
1291         {
1292                 listener->SetDopplerFactor(G.listener->dopplerfactor);
1293                 listener->SetDopplerVelocity(G.listener->dopplervelocity);
1294                 listener->SetGain(G.listener->gain);
1295         }
1296 #endif
1297
1298         // convert world
1299         KX_WorldInfo* worldinfo = new BlenderWorldInfo(blenderscene->world);
1300         converter->RegisterWorldInfo(worldinfo);
1301         kxscene->SetWorldInfo(worldinfo);
1302         
1303         // convert logic bricks, sensors, controllers and actuators
1304         for (i=0;i<logicbrick_conversionlist->GetCount();i++)
1305         {
1306                 KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
1307                 struct Object* blenderobj = converter->FindBlenderObject(gameobj);
1308                 bool isInActiveLayer = (blenderobj->lay & activeLayerBitInfo)!=0;
1309                 BL_ConvertActuators(maggie->name, blenderobj,gameobj,logicmgr,kxscene,ketsjiEngine,executePriority, activeLayerBitInfo,isInActiveLayer,rendertools,converter);
1310         }
1311         for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
1312         {
1313                 KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
1314                 struct Object* blenderobj = converter->FindBlenderObject(gameobj);
1315                 bool isInActiveLayer = (blenderobj->lay & activeLayerBitInfo)!=0;
1316                 BL_ConvertControllers(blenderobj,gameobj,logicmgr,pythondictionary,executePriority,activeLayerBitInfo,isInActiveLayer,converter);
1317         }
1318         for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
1319         {
1320                 KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
1321                 struct Object* blenderobj = converter->FindBlenderObject(gameobj);
1322                 bool isInActiveLayer = (blenderobj->lay & activeLayerBitInfo)!=0;
1323                 BL_ConvertSensors(blenderobj,gameobj,logicmgr,kxscene,keydev,executePriority,activeLayerBitInfo,isInActiveLayer,canvas,converter);
1324         }
1325         logicbrick_conversionlist->Release();
1326         
1327         // Calculate the scene btree -
1328         // too slow - commented out.
1329         //kxscene->SetNodeTree(tf.MakeTree());
1330 }
1331