Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

BL_BlenderDataConversion.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // Convert sca-system from Blender to KX
00004 //
00005 // Version: $Id: BL_BlenderDataConversion_cpp-source.html,v 1.2 2001/02/01 12:24:57 coockie Exp $
00006 //
00007 extern "C" struct Material *give_current_material(struct Object *ob, int act);
00008 extern "C" void where_is_object(struct Object*);
00009 
00010 
00011 
00012 #ifdef WIN32
00013 #pragma warning (disable : 4786)
00014 #endif
00015 
00016 #include "BL_BlenderDataConversion.h"
00017 #include "KX_BlenderGL.h"
00018 
00019 // Expressions
00020 #include "ListValue.h"
00021 #include "IntValue.h"
00022 // Collision & Fuzzics LTD
00023 #include "SM_Object.h"
00024 #include "SM_FhObject.h"
00025 #include "SM_Scene.h"
00026 #include "KX_Scene.h"
00027 #include "KX_GameObject.h"
00028 #include "RAS_MeshObject.h"
00029 
00030 #include "KX_ConvertActuators.h"
00031 #include "KX_ConvertControllers.h"
00032 #include "KX_ConvertSensors.h"
00033 
00034 #include "KX_BlenderClientObject.h"
00035 
00036 #include "KX_GameObject.h"
00037 #include "SCA_LogicManager.h"
00038 #include "SCA_EventManager.h"
00039 #include "SCA_TimeEventManager.h"
00040 #include "KX_Light.h"
00041 #include "KX_Camera.h"
00042 #include "KX_EmptyObject.h"
00043 #include "MT_Point3.h"
00044 #include "MT_Transform.h"
00045 #include "SCA_IInputDevice.h"
00046 #include "RAS_TexMatrix.h"
00047 #include "BL_BlenderOrientation.h"
00048 #include "RAS_MaterialBucket.h"
00049 #include "KX_BlenderPolyMaterial.h"
00050 #include "RAS_Polygon.h"
00051 #include "RAS_TexVert.h"
00052 #include "RAS_BucketManager.h"
00053 #include "blender.h"
00054 #include "game.h"
00055 #include "sound.h"
00056 #include "ipo.h"
00057 #include "KX_BlenderInputDevice.h"
00058 #include "KX_ConvertProperties.h"
00059 #include "KX_HashedPtr.h"
00060 
00061 #include "BL_BlenderIPO.h"
00062 #include "KX_ScalarInterpolator.h"
00063 
00064 #include "KX_IpoConvert.h"
00065 
00066 #include "SG_Node.h"
00067 
00068 GEN_Map<CHashedPtr,SCA_IActuator*> map_blender_to_gameactuator;
00069 GEN_Map<CHashedPtr,KX_GameObject*> map_blender_to_gameobject;
00070 GEN_Map<CHashedPtr,struct Object*> map_gameobject_to_blender;
00071 struct parentChildLink{
00072         struct Object* m_blenderchild;
00073         class SG_Node* m_gamechildnode;
00074 };
00075 
00076 vector<parentChildLink> vec_parent_child;
00077 
00078 GEN_Map<CHashedPtr,RAS_MeshObject*> map_mesh_to_gamemesh;
00079 GEN_Map<CHashedPtr,SCA_IController*> map_blender_to_gamecontroller;
00080 GEN_Map<CHashedPtr,DT_ShapeHandle> map_gamemesh_to_sumoshape;
00081 
00082 
00083 BL_IpoMap g_ipoMap;
00084 
00085 std::vector<RAS_IPolyMaterial*> polymaterialarray;
00086 
00087 void BL_ClearConvert()

00088 {
00089         int i;
00090 
00091         map_blender_to_gameactuator.clear();
00092         map_blender_to_gamecontroller.clear();
00093         map_gamemesh_to_sumoshape.clear();
00094         vec_parent_child.clear();
00095 
00096         // delete the meshes
00097         int nummeshes = map_mesh_to_gamemesh.size();
00098         for (i=0;i<nummeshes;i++)
00099         {
00100                 RAS_MeshObject** meshptr = map_mesh_to_gamemesh.at(i);
00101                 assert(meshptr);
00102                 if (meshptr)
00103                 {
00104                         delete (*meshptr);
00105                 }
00106         }
00107 
00108         for (vector<RAS_IPolyMaterial*>::iterator itp = polymaterialarray.begin();
00109         !(itp == polymaterialarray.end());itp++)
00110         {
00111                 delete (*itp);
00112         }
00113 
00114         polymaterialarray.clear();
00115         
00116         map_mesh_to_gamemesh.clear();
00117 
00118         map_blender_to_gameobject.clear();
00119         map_gameobject_to_blender.clear();
00120 
00121 
00122         BL_IpoMap::iterator it;
00123         for (it = g_ipoMap.begin(); !(it == g_ipoMap.end()); ++it) {
00124                 delete (*it).second;
00125         }
00126         g_ipoMap.clear();
00127 }
00128 
00129 unsigned int KX_rgbaint2uint_new(unsigned int icol)

00130 {
00131         unsigned int temp=0;
00132         unsigned char *cp= (unsigned char *)&temp;
00133         unsigned char *src= (unsigned char *)&icol;
00134         cp[3]= src[0];//alpha
00135         cp[2]= src[1];//blue
00136         cp[1]= src[2];//green
00137         cp[0]= src[3];//red
00138         return temp;
00139 }
00140 
00141 /* Now the real converting starts... */
00142 unsigned int KX_Mcol2uint_new(MCol col)

00143 {
00144         /* color has to be converted without endian sensitivity. So no shifting! */
00145         unsigned int temp=0;
00146         unsigned char *cp= (unsigned char *)&temp;
00147         cp[2]= col.r;
00148         cp[1]= col.g;
00149         cp[0]= col.b;
00150         return temp;
00151 }
00152 
00153 RAS_MeshObject* BL_ConvertMesh(Mesh* mesh,Object* blenderobj)

00154 {
00155         
00156         RAS_MeshObject** meshobjptr = map_mesh_to_gamemesh[CHashedPtr(mesh)];
00157         if (meshobjptr)
00158         {
00159                 return *meshobjptr;
00160         }
00161         
00162         
00163         RAS_MeshObject* meshobj = new RAS_MeshObject();
00164         meshobj->SetName(mesh->id.name);
00165         
00166         
00167         MFace* mface = static_cast<MFace*>(mesh->mface);
00168         TFace* tface = static_cast<TFace*>(mesh->tface);
00169         
00170 
00171         bool hastexcoords = true;
00172         
00173         bool myvalid=false;
00174         
00175         MCol* mmcol = mesh->mcol;
00176         
00177         
00178 
00179         for (int f=0;f<mesh->totface;f++,mface++,tface++)
00180         {
00181                 
00182                 bool collider = true;
00183                 
00184                 // only add valid polygons
00185                 if (mface->v3)
00186                 {
00187                         
00188                         MT_Vector3 no0(0.0,0.0,0.0),no1(0.0,0.0,0.0),no2(0.0,0.0,0.0),no3(0.0,0.0,0.0);
00189                         MT_Point3 pt0,pt1,pt2,pt3;
00190                         MT_Point2 uv0(0.0,0.0),uv1(0.0,0.0),uv2(0.0,0.0),uv3(0.0,0.0);
00191                         unsigned int rgb0,rgb1,rgb2,rgb3;
00192                         pt0 = MT_Point3(        mesh->mvert[mface->v1].co[0],
00193                                                                 mesh->mvert[mface->v1].co[1],
00194                                                                 mesh->mvert[mface->v1].co[2]);
00195                         no0 = MT_Vector3(
00196                                 mesh->mvert[mface->v1].no[0]/32767.0,
00197                                 mesh->mvert[mface->v1].no[1]/32767.0,
00198                                 mesh->mvert[mface->v1].no[2]/32767.0
00199                                 );
00200                                                 
00201                         pt1 = MT_Point3(        mesh->mvert[mface->v2].co[0],
00202                                                                 mesh->mvert[mface->v2].co[1],
00203                                                                 mesh->mvert[mface->v2].co[2]);
00204                         
00205                         no1 = MT_Vector3(
00206                                 mesh->mvert[mface->v2].no[0]/32767.0,
00207                                 mesh->mvert[mface->v2].no[1]/32767.0,
00208                                 mesh->mvert[mface->v2].no[2]/32767.0
00209                                 );
00210                         
00211                         pt2 = MT_Point3(        mesh->mvert[mface->v3].co[0],
00212                                                                 mesh->mvert[mface->v3].co[1],
00213                                                                 mesh->mvert[mface->v3].co[2]);
00214 
00215                         no2 = MT_Vector3(
00216                                 mesh->mvert[mface->v3].no[0]/32767.0,
00217                                 mesh->mvert[mface->v3].no[1]/32767.0,
00218                                 mesh->mvert[mface->v3].no[2]/32767.0
00219                                 );
00220                         
00221                         if (mface->v4)
00222                         {
00223                                 pt3 = MT_Point3(        mesh->mvert[mface->v4].co[0],
00224                                                                         mesh->mvert[mface->v4].co[1],
00225                                                                         mesh->mvert[mface->v4].co[2]);
00226                                 no3 = MT_Vector3(
00227                                         mesh->mvert[mface->v4].no[0]/32767.0,
00228                                         mesh->mvert[mface->v4].no[1]/32767.0,
00229                                         mesh->mvert[mface->v4].no[2]/32767.0
00230                                         );
00231                         }
00232 
00233                         
00234 
00235                         if((!mface->flag & ME_SMOOTH))
00236                         {
00237                                 no0=no1=no2=no3= ((pt1-pt0).cross(pt2-pt0)).safe_normalized();
00238                         }
00239                 
00240                         {
00241 
00242 
00243                                 CCString imastr = 
00244                                         ((mesh->tface && tface) ? 
00245                                         (tface->tpage? ((Image*) tface->tpage)->id.name : "" ) : "" );
00246                 
00247                                 char flag=0, transp=0;
00248                                 short mode=0, tile=0;
00249                                 
00250                                 bool polyvisible = true;
00251                                 //CCString texturename;
00252                                 if (mesh->tface && tface)
00253                                 {
00254                                         //TF_DYNAMIC means the polygon is a collision face
00255                                         collider = (tface->mode & TF_DYNAMIC != 0);
00256                                         transp = tface->transp;
00257                                         tile = tface->tile;
00258                                         mode = tface->mode;
00259                                         
00260                                         polyvisible = !((tface->flag & TF_HIDE)||(tface->mode & TF_INVISIBLE));
00261                                         
00262                                         uv0 = MT_Point2(tface->uv[0]);
00263                                         uv1 = MT_Point2(tface->uv[1]);
00264                                         uv2 = MT_Point2(tface->uv[2]);
00265                                         rgb0 = KX_rgbaint2uint_new(tface->col[0]);
00266                                         rgb1 = KX_rgbaint2uint_new(tface->col[1]);
00267                                         rgb2 = KX_rgbaint2uint_new(tface->col[2]);
00268 
00269                                         if (mface->v4)
00270                                         {
00271                                                 uv3 = MT_Point2(tface->uv[3]);
00272                                                 rgb3 = KX_rgbaint2uint_new(tface->col[3]);
00273                                         }
00274 
00275                                 } else
00276                                 {
00277                                         //
00278                                         if (mmcol)
00279                                         {
00280                                                 rgb0 = KX_Mcol2uint_new(mmcol[0]);
00281                                                 rgb1 = KX_Mcol2uint_new(mmcol[1]);
00282                                                 rgb2 = KX_Mcol2uint_new(mmcol[2]);
00283                                                 
00284                                                 
00285                                                 if (mface->v4)
00286                                                 {
00287                                                         rgb3 = KX_Mcol2uint_new(mmcol[3]);
00288                                                         
00289                                                 }
00290                                         
00291                                                 mmcol += 4;
00292                                         }
00293                                 }
00294                                         
00295                                 
00296                                 Material* ma = give_current_material(blenderobj, 1);
00297                                 const char* matnameptr = (ma ? ma->id.name : "");
00298                                 
00299 
00300                                 bool istriangle = (mface->v4==0);
00301 
00302                                 RAS_IPolyMaterial* polymat = new KX_BlenderPolyMaterial(imastr,false,matnameptr,tile,mode,transp,
00303                                                                                                                                                          blenderobj->lay,istriangle,blenderobj,tface);
00304                                 // this is needed to free up memory afterwards
00305                                 polymaterialarray.push_back(polymat);
00306 
00307                                 RAS_MaterialBucket* bucket = RAS_BucketManagerFindBucket(polymat);
00308                                 if (!(mode&TF_LIGHT))
00309                                         bucket->DisableLights();
00310                                 
00311                                 bucket->SetTriangleMode(istriangle);
00312 
00313                                 int edcode = mface->edcode;
00314                                 if (mface->v4)
00315                                 {
00316                                         int vtxarray = meshobj->FindVertexArray(4,polymat);
00317                                         RAS_Polygon* poly = new RAS_Polygon(bucket,polyvisible,4,vtxarray);
00318                                         poly->SetVertex(0,meshobj->FindOrAddVertex(vtxarray,pt0,uv0,rgb0,no0,polymat));
00319                                         poly->SetVertex(1,meshobj->FindOrAddVertex(vtxarray,pt1,uv1,rgb1,no1,polymat));
00320                                         poly->SetVertex(2,meshobj->FindOrAddVertex(vtxarray,pt2,uv2,rgb2,no2,polymat));
00321                                         poly->SetVertex(3,meshobj->FindOrAddVertex(vtxarray,pt3,uv3,rgb3,no3,polymat));
00322                                         meshobj->AddPolygon(poly);
00323                                         poly->SetVisibleWireframeEdges(edcode);
00324                                         poly->SetCollider(collider);
00325                                 }
00326                                 else
00327                                 {
00328                                         int vtxarray = meshobj->FindVertexArray(3,polymat);
00329                                         RAS_Polygon* poly = new RAS_Polygon(bucket,polyvisible,3,vtxarray);
00330                                         poly->SetVertex(0,meshobj->FindOrAddVertex(vtxarray,pt0,uv0,rgb0,no0,polymat));
00331                                         poly->SetVertex(1,meshobj->FindOrAddVertex(vtxarray,pt1,uv1,rgb1,no1,polymat));
00332                                         poly->SetVertex(2,meshobj->FindOrAddVertex(vtxarray,pt2,uv2,rgb2,no2,polymat));
00333                                         poly->SetCollider(collider);
00334                                         meshobj->AddPolygon(poly);
00335                                         
00336                                         poly->SetVisibleWireframeEdges(edcode);
00337                                 }
00338                                 myvalid=true;
00339                         }
00340                 }
00341         }
00342         meshobj->UpdateMaterialList();
00343         //for each face in the mesh
00344         map_mesh_to_gamemesh.insert(CHashedPtr(mesh),meshobj);
00345         return meshobj;
00346 }               
00347 
00348 SM_ShapeProps g_shapeProps = {
00349         1.0,    // mass
00350         0.4,    // inertia 
00351         0.9,    // linear drag 
00352         0.1,     // angular drag
00353         { 1.0, 1.0, 1.0 }, // friction scaling on the local X, Y, and Z axis.
00354         false,   // do anisotropic fricition?
00355         false,   // do Fh ?
00356         false    // do Fh rot?
00357 };
00358 
00359 SM_MaterialProps g_materialProps = {
00360         1.0,    // restitution
00361         2.0,    // friction 
00362         0.0,    // fh spring constant
00363         0.0,    // fh damping
00364         0.0,    // fh distance
00365         false   // sliding material?
00366 };
00367 
00368 
00369 DT_ShapeHandle CreateShapeFromMesh(RAS_MeshObject* meshobj)

00370 {
00371 
00372         DT_ShapeHandle* shapeptr = map_gamemesh_to_sumoshape[CHashedPtr(meshobj)];
00373         if (shapeptr)
00374         {
00375                 return *shapeptr;
00376         }
00377         
00378         // todo: shared meshes
00379         DT_ShapeHandle shape = DT_NewComplexShape();
00380         int p=0;
00381         int numpolys = meshobj->NumPolygons();
00382         if (!numpolys)
00383         {
00384                 return NULL;
00385         }
00386         int numvalidpolys = 0;
00387 
00388 
00389         for (p=0;p<numpolys ;p++)
00390         {
00391                 RAS_Polygon* poly = meshobj->GetPolygon(p);
00392                 // only add polygons that have the collisionflag set
00393                 if (poly->IsCollider())
00394                 {
00395                         numvalidpolys++;
00396                         DT_Begin();
00397 
00398                         int v=0;
00399                         for (v=0;v<poly->VertexCount();v++)
00400                         {
00401                                 MT_Point3 pt = meshobj->GetVertex(poly->GetVertexIndexBase().m_vtxarray, 
00402                                                                                                   poly->GetVertexIndexBase().m_indexarray[v],
00403                                                                                                   poly->GetMaterial()->GetPolyMaterial())->xyz();
00404                                 DT_Vertex(pt[0],pt[1],pt[2]);
00405                         }
00406                         DT_End();
00407                 }
00408         }
00409 
00410         DT_EndComplexShape();
00411 
00412         if (numvalidpolys > 0)
00413         {
00414                 map_gamemesh_to_sumoshape.insert(CHashedPtr(meshobj),shape);
00415                 return shape;
00416         }
00417 
00418         // memleak... todo: delete shape
00419         return NULL;
00420 }
00421 
00422 
00423 SM_MaterialProps *CreateMaterialFromBlenderObject(struct Object* blenderobject,
00424                                                                                                   KX_Scene *kxscene)

00425 {
00426         SM_MaterialProps *materialProps = new SM_MaterialProps;
00427         
00428         assert(materialProps);
00429 
00430         kxscene->AddMaterialProps(materialProps);
00431 
00432         Material* blendermat = give_current_material(blenderobject, 0);
00433                 
00434         if (blendermat)
00435         {
00436                 assert(0.0f <= blendermat->reflect && blendermat->reflect <= 1.0f);
00437 
00438                 materialProps->m_restitution = blendermat->reflect;
00439                 materialProps->m_friction = blendermat->friction;
00440                 materialProps->m_fh_spring = blendermat->fh;
00441                 materialProps->m_fh_damping = blendermat->xyfrict;
00442                 materialProps->m_fh_distance = blendermat->fhdist;
00443                 materialProps->m_fh_normal = (blendermat->dynamode & MA_FH_NOR) != 0;
00444         }
00445         else {
00446                 *materialProps = g_materialProps;
00447         }
00448         
00449         return materialProps;
00450 }
00451 
00452 SM_ShapeProps *CreateShapePropsFromBlenderObject(struct Object* blenderobject,
00453                                                                                                  KX_Scene *kxscene)

00454 {
00455         SM_ShapeProps *shapeProps = new SM_ShapeProps;
00456 
00457         assert(shapeProps);
00458         
00459         kxscene->AddShapeProps(shapeProps);
00460 
00461         shapeProps->m_mass = blenderobject->mass;
00462 //      shapeProps->m_inertia = blenderobject->inertia; 
00463 //  This needs to be fixed in blender. For now, we use:
00464         shapeProps->m_inertia = 0.4 * blenderobject->mass;
00465 
00466         assert(0.0f <= blenderobject->damping && blenderobject->damping <= 1.0f);
00467         assert(0.0f <= blenderobject->rdamping && blenderobject->rdamping <= 1.0f);
00468         
00469         shapeProps->m_lin_drag = 1.0 - blenderobject->damping;
00470         shapeProps->m_ang_drag = 1.0 - blenderobject->rdamping;
00471 
00472         shapeProps->m_friction_scaling[0] = blenderobject->anisotropicFriction[0]; 
00473         shapeProps->m_friction_scaling[1] = blenderobject->anisotropicFriction[1];
00474         shapeProps->m_friction_scaling[2] = blenderobject->anisotropicFriction[2];
00475         shapeProps->m_do_anisotropic = ((blenderobject->gameflag & OB_ANISOTROPIC_FRICTION) != 0);
00476 
00477         shapeProps->m_do_fh     = (blenderobject->gameflag & OB_DO_FH) != 0; 
00478         shapeProps->m_do_rot_fh = (blenderobject->gameflag & OB_ROT_FH) != 0;
00479 
00480         return shapeProps;
00481 }
00482 
00483 void BL_CreateSumoObjectNew(KX_GameObject* gameobj,
00484                                                  struct Object* blenderobject,
00485                                                  RAS_MeshObject* meshobj,
00486                                                  KX_Scene* kxscene,
00487                                                  struct Global* glob)

00488                                                  

00489 {
00490         SM_Scene*       sceneptr = kxscene->GetSumoScene();
00491         bool bInLayer = (blenderobject->lay & glob->scene->lay) != 0;
00492 
00493         // Collision & Fuzzics stuff
00494         SM_Object* sumoObj=NULL;
00495         SM_MaterialProps* smmaterial = 
00496                 CreateMaterialFromBlenderObject(blenderobject, kxscene);
00497         
00498         if (BL_IsDynamicObject(blenderobject))
00499         {
00500                 // Dynamic objects have no parent !?!
00501                 //assert(!BL_HasParent(blenderobject));
00502                 
00503                 SM_ShapeProps* shapeprops =
00504                         CreateShapePropsFromBlenderObject(blenderobject, 
00505                         kxscene);
00506                 DT_ShapeHandle shape    =       DT_Sphere(0.0);
00507                 sumoObj                                 =       new SM_Object(shape,smmaterial,shapeprops,NULL);        
00508                 
00509                 
00510                 //lf->axsize
00511                 //mmm, for now, taks this for the size of the dynamicobject
00512                 double radius = BL_GetDynamicObjectSize(blenderobject);
00513                 
00514                 MT_Scalar margin = radius;//0.5;
00515                 sumoObj->setMargin(margin);
00516                 
00517                 if (bInLayer)
00518                 {
00519                         DT_AddObject(kxscene->GetSolidScene(),
00520                                 sumoObj->getObjectHandle());
00521                 }
00522                 
00523                 bool isDynamic = true;
00524                 bool isActor = true;
00525                 gameobj->RegisterSumoObject(sceneptr,sumoObj,NULL,isDynamic,isActor,bInLayer);
00526                 
00527         } 
00528         else {
00529                 // non physics object
00530                 if (meshobj)
00531                 {
00532                         int numpolys = meshobj->NumPolygons();
00533                         if (numpolys>0)
00534                         {
00535                                 DT_ShapeHandle complexshape     = 
00536                                         CreateShapeFromMesh(meshobj);
00537                                 
00538                                 if (complexshape)
00539                                 {
00540                                         SM_Object *dynamicParent = NULL;
00541                                         struct Object *parent = BL_GetRootParent(blenderobject);
00542                                         
00543                                         if (parent && BL_IsDynamicObject(parent)) {
00544                                                 
00545                                                 KX_GameObject *parentgameobject = 
00546                                                         *map_blender_to_gameobject[CHashedPtr(parent)];
00547                                                 if (parentgameobject)
00548                                                 {
00549                                                         // problem is how to find the dynamic parent
00550                                                         // in the scenegraph
00551 
00552                                                         //dynamicParent  = parentgameobject->GetRootSGParent()
00553                                                         //      ->GetSumoObject();
00554                                                         //todo: get rootobject's sumoobject
00555                                                         dynamicParent = parentgameobject->GetSumoObject();
00556                                                         assert(dynamicParent);
00557                                                 }
00558                                         }
00559                                         
00560                                         
00561                                         sumoObj = new SM_Object(complexshape,smmaterial,NULL, dynamicParent);   
00562                                         if (bInLayer)
00563                                         {
00564                                                 DT_AddObject(kxscene->GetSolidScene(),
00565                                                         sumoObj->getObjectHandle());
00566                                         }
00567                                         
00568                                         
00569                                         bool isDynamic = false;
00570                                         bool isActor = BL_IsActor(blenderobject);
00571                                         const char* matnameptr = NULL;
00572                                         if (meshobj->NumMaterials() > 0)
00573                                         {
00574                                                 const CCString& matname = meshobj->GetMaterialName(0);
00575                                                 matnameptr = matname.ReadPtr();
00576                                         }
00577                                         gameobj->RegisterSumoObject(sceneptr,sumoObj,matnameptr,isDynamic,isActor,bInLayer);
00578 
00579                                 }
00580                         }
00581                 }
00582         }
00583 
00584         // physics object get updated here !
00585         
00586         gameobj->UpdateTransform();
00587 
00588         if (bInLayer && sumoObj)
00589         {
00590                 sceneptr->add(*sumoObj);
00591         }
00592 
00593 }
00594 
00595 
00596 
00597 // convert blender objects into ketsji gameobjects
00598 void BL_ConvertBlenderObjects(struct Global* glob,
00599                                                           KX_Scene* kxscene,
00600                                                           PyObject* pythondictionary,
00601                                                           KX_BlenderKeyboardDevice* keydev
00602                                                           )

00603 {
00604         
00605         Main* maggie = glob->main;
00606         Base *base = static_cast<Base*>(glob->scene->base.first);
00607         int numobjects=0;
00608         int numlights=0;
00609         int numempty=0;
00610         int numunknown=0;
00611         int nummeshes=0,numerrormeshes=0;
00612         int numcurves=0;
00613         int numsurf=0;
00614         int nummball=0;
00615         int numcameras=0;
00616         int numlife=0;
00617         int numsector=0;
00618 
00619         BL_BlenderWorldInfo worldinfo = BL_GetBlenderWorldInfo(glob);
00620         kxscene->GetSumoScene()->setForceField(MT_Vector3(0,0,worldinfo.gravity));
00621         
00622         // templist to find Root Parents (object with no parents)
00623         CListValue* templist = new CListValue();
00624         CListValue*     sumolist = new CListValue();
00625 
00626         CListValue* objectlist = kxscene->GetObjectList();
00627         CListValue* parentlist = kxscene->GetRootParentList();
00628 
00629         CListValue* lightlist = kxscene->GetLightList();
00630         CListValue* cameralist = kxscene->GetCameraList();
00631         SCA_LogicManager* logicmgr = kxscene->GetLogicManager();
00632         SCA_TimeEventManager* timemgr = kxscene->GetTimeEventManager();
00633         
00634 
00635         CListValue* logicbrick_conversionlist = new CListValue();
00636 
00637         while(base)
00638         {
00639                 Object* blenderobject = base->object;
00640                 KX_GameObject* gameobj=NULL;
00641                         
00642                 switch(blenderobject->type)
00643                 {
00644                 case OB_LAMP:
00645                 {
00646                         numlights++;
00647                         Lamp* light = static_cast<Lamp*>(blenderobject->data);
00648                         if (light)
00649                         {
00650                                 gameobj = new KX_LightObject(kxscene,KX_Scene::m_callbacks);
00651                                 // set transformation
00652                                 MT_Point3 pos = BL_GetBlenderPosition(blenderobject);
00653                                 MT_Vector3 eulxyz = BL_GetBlenderRotation(blenderobject);
00654                                 MT_Vector3 scale = BL_GetBlenderScaling(blenderobject);
00655                                 gameobj->NodeSetLocalPosition(pos);
00656                                 gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
00657                                 gameobj->NodeSetLocalScale(scale);
00658                                 gameobj->NodeUpdateGS(0,true); // worldcoords
00659 
00660                                 BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene);
00661                                 lightlist->Add(gameobj->AddRef());
00662                         }
00663 
00664                         break;
00665                 };
00666                 case OB_CAMERA:
00667                 {
00668                         numcameras++;
00669                         Camera *ca= static_cast<Camera*>(blenderobject->data);
00670                         // now do something with this camera
00671                         KX_Camera* gamecamera = new KX_Camera(kxscene,KX_Scene::m_callbacks);
00672                         gameobj =  gamecamera;
00673                         // set transformation
00674                         MT_Point3 pos = BL_GetBlenderPosition(blenderobject);
00675                         MT_Vector3 eulxyz = BL_GetBlenderRotation(blenderobject);
00676                         MT_Vector3 scale = BL_GetBlenderScaling(blenderobject);
00677                         gameobj->NodeSetLocalPosition(pos);
00678                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
00679                         gameobj->NodeSetLocalScale(scale);
00680                         gameobj->NodeUpdateGS(0,true); // worldcoords
00681 
00682                         cameralist->Add(gameobj->AddRef());
00683                         BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene);
00684                         if (!BL_ExternalCameraPerspective() && (BL_GetCameraObject(glob) == blenderobject))
00685                         {
00686                                 kxscene->SetActiveCamera(gamecamera);
00687                         }
00688 
00689                         break;
00690                 }
00691 
00692                 case OB_MBALL:
00693                 {
00694                         nummball++;
00695                         break;
00696                 }
00697                 case OB_SURF:
00698                 {
00699                         numsurf++;
00700                         break;
00701                 }
00702                 case OB_MESH:
00703                 {
00704                         Mesh* mesh = static_cast<Mesh*>(blenderobject->data);
00705                         RAS_MeshObject* meshobj = BL_ConvertMesh(mesh,blenderobject);
00706                         
00707                         if (meshobj)
00708                         {
00709                                 // needed for python scripting
00710                                 logicmgr->RegisterMeshName(meshobj->GetName(),meshobj);
00711 
00712                                 gameobj = new KX_GameObject(kxscene,KX_Scene::m_callbacks);
00713                                 // set transformation
00714                                 MT_Point3 pos = BL_GetBlenderPosition(blenderobject);
00715                                 MT_Vector3 eulxyz = BL_GetBlenderRotation(blenderobject);
00716                                 MT_Vector3 scale = BL_GetBlenderScaling(blenderobject);
00717                                 
00718                                 gameobj->NodeSetLocalPosition(pos);
00719                                 gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
00720                                 gameobj->NodeSetLocalScale(scale);
00721                                 gameobj->NodeUpdateGS(0,true); // worldcoords
00722                                 
00723                                 gameobj->AddMesh(meshobj);
00724                                 
00725                                 // todo: this ipo has to convert into an ipo-controller
00726                                 // that can be plugged into the scenegraph
00727                                 // just like the physics can be plugged
00728                                 BL_ConvertIpos(blenderobject,gameobj);
00729 
00730                                 sumolist->Add(gameobj->AddRef());
00731 
00732                                 
00733                                 BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene);
00734                                 // register collision object
00735                                 
00736                                 nummeshes++;
00737                         } else
00738                         {
00739                                 numerrormeshes++;
00740                         }
00741                                         
00742                         break;
00743                 };
00744                                 
00745                                 
00746                                 
00747                                 
00748                 case OB_CURVE:
00749                 {
00750                         numcurves++;
00751                         break;
00752                 }
00753                 case OB_EMPTY:
00754                 {
00755                         numempty++;
00756                                         
00757                         gameobj = new KX_EmptyObject(kxscene,KX_Scene::m_callbacks);
00758                         
00759                         // set transformation
00760                         MT_Point3 pos = BL_GetBlenderPosition(blenderobject);
00761                         MT_Vector3 eulxyz = BL_GetBlenderRotation(blenderobject);
00762                         MT_Vector3 scale = BL_GetBlenderScaling(blenderobject);
00763                         gameobj->NodeSetLocalPosition(pos);
00764                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
00765                         gameobj->NodeSetLocalScale(scale);
00766                         gameobj->NodeUpdateGS(0,true);
00767 
00768                         
00769                         sumolist->Add(gameobj->AddRef());//BL_CreateSumoObject(gameobj,blenderobject,NULL,kxscene,glob);
00770                         BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene);
00771                         
00772 
00773                         break;
00774                                         
00775                 }
00776                 default:
00777                 {
00778                         numunknown++;
00779                                         
00780                 }
00781                 }
00782                 if (gameobj)
00783                 {
00784 
00785                         gameobj->SetName(blenderobject->id.name);
00786         
00787                         // templist to find Root Parents (object with no parents)
00788                         templist->Add(gameobj->AddRef());
00789                         
00790                         // update children/parent hierarchy
00791                         if (BL_HasParent(blenderobject))
00792                         {
00793                                 where_is_object(blenderobject);
00794                                 //KX_GameObject* parentobj = *map_blender_to_gameobject[CHashedPtr(blenderparent)];
00795 
00796                                 // blender has an additional 'parentinverse' offset in each object
00797                                 SG_Node* parentinversenode = new SG_Node(NULL,NULL,SG_IObject::m_nocallbacks);
00798                                 //map_blenderparent_to_childnode.insert(CHashedPtr(blenderparent),parentinversenode);
00799                                 parentChildLink pclink;
00800                                 pclink.m_blenderchild = blenderobject;
00801                                 pclink.m_gamechildnode = parentinversenode;
00802                                 vec_parent_child.push_back(pclink);
00803 
00804                                 float* fl = BL_GetParentInverse(blenderobject);
00805                                 MT_Transform parinvtrans(fl);
00806                                 parentinversenode->SetLocalPosition(parinvtrans.getOrigin());
00807                                 parentinversenode->SetLocalOrientation(parinvtrans.getBasis());
00808                                 //parentobj->NodeAddChild(parentinversenode);
00809                                 parentinversenode->AddChild(gameobj->GetSGNode());
00810                         }
00811                         
00812                         // needed for python scripting
00813                         logicmgr->RegisterGameObjectName(gameobj->GetName(),gameobj);
00814 
00815                         map_blender_to_gameobject.insert(CHashedPtr(blenderobject),gameobj);
00816                         map_gameobject_to_blender.insert(CHashedPtr(gameobj),blenderobject);
00817 
00818                         // this was put in rapidly, needs to be looked at more closely
00819                         // only draw/use objects in active 'blender' layers
00820 
00821                         logicbrick_conversionlist->Add(gameobj->AddRef());
00822                         
00823                         if ((blenderobject->lay & glob->scene->lay))
00824                         {
00825                                 objectlist->Add(gameobj->AddRef());
00826                                 
00827                                 gameobj->NodeUpdateGS(0,true);
00828                                 gameobj->Bucketize();
00829                                 
00830                         } else
00831                         {
00832                                 int i=0;
00833                         }
00834 
00835                         
00836 
00837                         
00838 
00839                 }
00840                 numobjects++;
00841                         
00842                 base = base->next;
00843         }
00844                 
00845         vector<CCString> imagenames;
00846         
00847         // create hierarchy information
00848         int i;
00849         vector<parentChildLink>::iterator pcit;
00850 
00851         for (pcit = vec_parent_child.begin();!(pcit==vec_parent_child.end());++pcit)
00852         {
00853         
00854                 struct Object* blenderchild = pcit->m_blenderchild;
00855                 if (blenderchild->partype & PARVERT1)
00856                 {
00857                         //KX_GameObject* childobj = *map_blender_to_gameobject[CHashedPtr(blenderchild)];
00858                         //childobj->NodeSetVertexParent(true);
00859                         pcit->m_gamechildnode->SetVertexParent(true);
00860                 }
00861                 struct Object* blenderparent = BL_GetParent(blenderchild);
00862                 KX_GameObject* parentobj = *map_blender_to_gameobject[CHashedPtr(blenderparent)];
00863                 parentobj->NodeAddChild(pcit->m_gamechildnode);
00864         }
00865         
00866 
00867         // find 'root' parents (object that has not parents in SceneGraph)
00868         for (i=0;i<templist->GetCount();++i)
00869         {
00870                 KX_GameObject* gameobj = (KX_GameObject*) templist->GetValue(i);
00871                 if (gameobj->GetSGNode()->GetSGParent() == 0)
00872                 {
00873                         parentlist->Add(gameobj->AddRef());
00874                         gameobj->NodeUpdateGS(0,true);
00875                 }
00876         }
00877 
00878 
00879         // create physics information
00880         for (i=0;i<sumolist->GetCount();i++)
00881         {
00882                 KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
00883                 struct Object* blenderobject = *map_gameobject_to_blender[CHashedPtr(gameobj)];
00884                 int nummeshes = gameobj->GetMeshCount();
00885                 if (nummeshes > 0)
00886                 {
00887                         RAS_MeshObject* meshobj = gameobj->GetMesh(0);
00888                         BL_CreateSumoObjectNew(gameobj,blenderobject,meshobj,kxscene,glob);
00889                 } else
00890                 {
00891                         BL_CreateSumoObjectNew(gameobj,blenderobject,NULL,kxscene,glob);
00892                 }
00893                 
00894         }
00895 
00896 
00897         templist->Release();
00898         sumolist->Release();
00899 
00900         int executePriority=0;
00901         
00902         // convert logic bricks, sensors, controllers and actuators
00903         for (i=0;i<logicbrick_conversionlist->GetCount();i++)
00904         {
00905                 KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
00906                 struct Object* blenderobj = *map_gameobject_to_blender[CHashedPtr(gameobj)];
00907                 BL_ConvertActuators(blenderobj,gameobj,logicmgr,kxscene,executePriority);
00908         }
00909         for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
00910         {
00911                 KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
00912                 struct Object* blenderobj = *map_gameobject_to_blender[CHashedPtr(gameobj)];
00913                 BL_ConvertControllers(blenderobj,gameobj,logicmgr,pythondictionary,executePriority);
00914         }
00915         for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
00916         {
00917                 KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
00918                 struct Object* blenderobj = *map_gameobject_to_blender[CHashedPtr(gameobj)];
00919                 BL_ConvertSensors(blenderobj,gameobj,logicmgr,kxscene,keydev,           executePriority);
00920         }
00921         logicbrick_conversionlist->Release();
00922                 
00923 }
00924 
00925 
00926 

Generated at Thu Feb 1 13:03:01 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000