Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

pcggui.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002  *<
00003         FILE: pcggui.cpp
00004 
00005         DESCRIPTION:    Appwizard generated plugin
00006 
00007         CREATED BY: 
00008 
00009         HISTORY: 
00010 
00011  *>     Copyright (c) 2003, All Rights Reserved.
00012  **********************************************************************/
00013 
00014 #include "pcggui.h"
00015 #include <map>
00016 
00017 #define pcggui_CLASS_ID Class_ID(0x67910a43, 0xc56b6dbc)
00018 
00019 static pcggui thepcggui;
00020 static FILE     *file;
00021 
00022 #define SKIN_INTERFACE 0x00010000
00023 #define SKIN_CLASS_ID Class_ID(9815843,87654)
00024 #define PHYSIQUE_CLASS_ID Class_ID(PHYSIQUE_CLASS_ID_A, PHYSIQUE_CLASS_ID_B)
00025 
00026 typedef signed int sint32;
00027 typedef std::map<INode*, uint> TInodePtrInt;
00028 
00029 
00030 
00031 
00032 class pcgguiClassDesc : public ClassDesc2 {
00033 public:
00034         int                     IsPublic() { return TRUE; }
00035         void *                  Create(BOOL loading = FALSE) { return &thepcggui; }
00036         const TCHAR *   ClassName() { return GetString(IDS_CLASS_NAME); }
00037         SClass_ID               SuperClassID() { return UTILITY_CLASS_ID; }
00038         Class_ID                ClassID() { return pcggui_CLASS_ID; }
00039         const TCHAR*    Category() { return GetString(IDS_CATEGORY); }
00040 
00041         const TCHAR*    InternalName() { return _T("pcggui"); } // returns fixed parsable name (scripter-visible name)
00042         HINSTANCE               HInstance() { return hInstance; }                                       // returns owning module handle
00043 
00044 
00045 };
00046 
00047 static pcgguiClassDesc pcgguiDesc;
00048 ClassDesc2* GetpcgguiDesc() { return &pcgguiDesc; }
00049 
00050 
00051 
00052 
00053 static BOOL CALLBACK pcgguiDlgProc(
00054                 HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
00055 {
00056         switch (msg) {
00057                 case WM_INITDIALOG:
00058                         thepcggui.Init(hWnd);
00059                         break;
00060 
00061                 case WM_DESTROY:
00062                         thepcggui.Destroy(hWnd);
00063                         break;
00064 
00065                 case WM_COMMAND:
00066                         switch(LOWORD(wParam)) { //switch on ID
00067                                 case IDC_BUTTON_CONVERT: // A specific button's ID.
00068                                         thepcggui.CovertScene();
00069                                         break;
00070 
00071                                 case IDC_REFERENCE_MODEL:
00072                                         thepcggui.SetReferenceModelMode(IsDlgButtonChecked(hWnd, IDC_REFERENCE_MODEL));
00073                                         break;
00074 
00075                                 case IDC_BUTTON_EXPORT:
00076                                         thepcggui.ExportAll();
00077                                         break;
00078 
00079                                 case IDC_BUTTON_SAVE:
00080                                         thepcggui.SaveAll();
00081                                         break;
00082 
00083                                 case IDC_BUTTON_LOAD:
00084                                         thepcggui.LoadAll();
00085                                         break;
00086 
00087                                 case IDC_BUTTON_SAVE_ANALYZER:
00088                                         thepcggui.SaveAnalyzer();
00089                                         break;
00090 
00091                                 case IDC_BUTTON_LOAD_ANALYZER:
00092                                         thepcggui.LoadAnalyzer();
00093                                         break;
00094 
00095                                 case IDC_BUTTON_ANALYZE:
00096                                         thepcggui.Analyze();
00097                                         break;
00098 
00099                                 case IDC_BUTTON_RENDER:
00100                                         thepcggui.Render();
00101                                         break;
00102 
00103                                 default:
00104                                         break;
00105                         };
00106                         break;
00107 
00108 
00109                 case WM_LBUTTONDOWN:
00110                 case WM_LBUTTONUP:
00111                 case WM_MOUSEMOVE:
00112                         thepcggui.ip->RollupMouseMessage(hWnd,msg,wParam,lParam); 
00113                         break;
00114 
00115                 default:
00116                         return FALSE;
00117         }
00118         return TRUE;
00119 }
00120 
00121 
00122 class NullView: public View {
00123 public:
00124         Point2 ViewToScreen(Point3 p) { return Point2(p.x,p.y); }
00125         NullView() { worldToView.IdentityMatrix(); screenW=640.0f; screenH = 480.0f; }
00126 };
00127 
00128 
00129 //--- pcggui -------------------------------------------------------
00130 pcggui::pcggui()
00131 {
00132         iu = NULL;
00133         ip = NULL;      
00134         hPanel = NULL;
00135 }
00136 
00137 pcggui::~pcggui()
00138 {
00139 
00140 }
00141 
00142 void pcggui::BeginEditParams(Interface *ip,IUtil *iu) 
00143 {
00144         fopen_s(&file,"c:\\testdebug.txt","w");
00145         fprintf(file,"");
00146         fclose(file);
00147         fopen_s(&file,"c:\\testdebug.txt","a");
00148         fprintf(file,"\nFile Opened\n");
00149         this->iu = iu;
00150         this->ip = ip;
00151         hPanel = ip->AddRollupPage(
00152                 hInstance,
00153                 MAKEINTRESOURCE(IDD_PANEL),
00154                 pcgguiDlgProc,
00155                 GetString(IDS_PARAMS),
00156                 0);
00157 }
00158         
00159 void pcggui::EndEditParams(Interface *ip,IUtil *iu) 
00160 {
00161         this->iu = NULL;
00162         this->ip = NULL;
00163         ip->DeleteRollupPage(hPanel);
00164         hPanel = NULL;
00165         fclose(file);
00166 }
00167 
00168 void pcggui::Init(HWND hWnd)
00169 {
00170         fprintf(file,"Init\n");
00171 }
00172 
00173 void pcggui::Destroy(HWND hWnd)
00174 {
00175         fprintf(file,"Destroy\n");
00176 }
00177 
00178 void pcggui::CovertScene() {
00179         model::ExampleModel* em = new model::ExampleModel();
00180         model::ReferenceModel* rm = new model::ReferenceModel();
00181         TimeValue currtime = thepcggui.ip->GetTime();
00182         INode* node;
00183         BOOL needDel;
00184         NullView nullView;
00185         unsigned long offset = 0;
00186         unsigned long tmpOffset = 0;
00187 
00188         if(thepcggui.ip->GetRootNode()->NumberOfChildren() > 0) {
00189                 for(int n=0;n<thepcggui.ip->GetRootNode()->NumberOfChildren();n++) {
00190 
00191                         node = thepcggui.ip->GetRootNode()->GetChildNode(n);
00192                         ObjectState os = node->EvalWorldState(currtime);
00193 
00195                         // find and export modifier data if
00196                         // exists for this node
00197 
00198                         Object* pObj = node->GetObjectRef();
00199                         // make sure we have a derived obj, which implies
00200                         // modifiers are avail, and not a base obj
00201                         while (pObj && pObj->SuperClassID() == GEN_DERIVOB_CLASS_ID) {
00202                                 IDerivedObject* pDerObj = (IDerivedObject*) pObj;
00203                                 // Iterate over all entries of the modifier stack.
00204                                 for (int Idx = 0; Idx < pDerObj->NumModifiers(); Idx++) {
00205                                         Modifier* mod = pDerObj->GetModifier(Idx);
00206                                         DebugPrint("Mod Name: %s\n",mod->GetName());
00207                                         DebugPrint("Mod class ID: 0x%x - %d\n",mod->ClassID(),mod->ClassID());
00208                                         /*if(mod->ClassID() == 9845843) {
00209                                                 DebugPrint("SKIN\n");
00210                                         }*/
00211                                         
00212                                         //if (mod->ClassID() == Class_ID(BENDOSM_CLASS_ID,0)) {
00213                                 }
00214                                 // get next object in stack -- could be base obj,
00215                                 // or another derived obj
00216                                 pObj = pDerObj->GetObjRef();
00217                         }
00218 
00219 
00220                         if(os.obj->SuperClassID()==GEOMOBJECT_CLASS_ID) {
00221                                 if(node->GetBoneNodeOnOff()) {
00222                                         if(refModel) {
00223                                                 ConvertBone(node,rm);
00224                                         }
00225                                 } else {
00226                                         Mesh *mesh = ((GeomObject*)os.obj)->GetRenderMesh(thepcggui.ip->GetTime(),node,nullView,needDel);
00227                                         if (!mesh) return;
00228 
00229                                         mesh->buildNormals();
00230                                         Matrix3 tm = node->GetObjTMAfterWSM(thepcggui.ip->GetTime());
00231                                         for (int i=0; i<mesh->getNumVerts(); i++) {
00232                                                 Point3 v = (tm * mesh->verts[i]);
00233                                                 model::Vertex* vertex = new model::Vertex();
00234                                                 vertex->setCoordinates(v.x, v.y, v.z);
00235                                                 vertex->setNormal(mesh->getNormal(i).x,mesh->getNormal(i).y,mesh->getNormal(i).z);
00236                                                 if(refModel) {
00237                                                         rm->addVertex(vertex);
00238                                                 } else {
00239                                                         em->addVertex(vertex);
00240                                                 }
00241                                                 tmpOffset = i;
00242                                         }
00243                                         for (int i=0; i<mesh->getNumFaces(); i++) {
00244                                                 model::Face* face = new model::Face();
00245                                                 if(refModel) {
00246                                                         face->setVertices(
00247                                                                 rm->getVertex(offset + (int)mesh->faces[i].v[0]),
00248                                                                 rm->getVertex(offset + (int)mesh->faces[i].v[1]),
00249                                                                 rm->getVertex(offset + (int)mesh->faces[i].v[2]));
00250                                                         rm->addFace(face);
00251                                                 } else {
00252                                                         face->setVertices(
00253                                                                 em->getVertex(offset + (int)mesh->faces[i].v[0]),
00254                                                                 em->getVertex(offset + (int)mesh->faces[i].v[1]),
00255                                                                 em->getVertex(offset + (int)mesh->faces[i].v[2]));
00256                                                         em->addFace(face);
00257                                                 }
00258                                         }
00259                                         offset = tmpOffset;
00260                                 }
00261                         }
00262                 }
00263                 for(int n=0;n<thepcggui.ip->GetRootNode()->NumberOfChildren();n++) {
00264                         node = thepcggui.ip->GetRootNode()->GetChildNode(n);
00265                         ObjectState os = node->EvalWorldState(currtime);
00266                         if(os.obj->SuperClassID()==GEOMOBJECT_CLASS_ID) {
00267                                 if(!node->GetBoneNodeOnOff()) {
00268                                         if(refModel) {
00269                                                 DebugPrint("Error Code: %d\n",ConvertSkin(node,rm));
00270                                         }
00271                                 }
00272                         }
00273                 }
00274                 if(refModel) {
00275                         referenceModels.push_back(rm);
00276                 } else {
00277                         exampleModels.push_back(em);
00278                 }
00279         }
00280 }
00281 
00282 void pcggui::ConvertBone(INode* pParent, model::ReferenceModel* rm) {
00283         DebugPrint("Sub Named Object: \"%s\"\n",pParent->GetName());
00284 
00285         rm->getSkeleton()->addBone(NULL,NULL,10,pParent->GetName());
00286 
00287         for(int q=0;q<pParent->NumberOfChildren();q++) {
00288                 ConvertBone(pParent->GetChildNode(q),rm);
00289         }
00290 }
00291 
00292 void pcggui::ExportAll() {
00293         functions::Exporter ex = functions::Exporter();
00294         char tmp[100];
00295 
00296         for(int i=0; i<exampleModels.size(); i++) {
00297                 sprintf_s(tmp,"c:\\exampleModel%d.x3d",i);
00298                 ex.exportModel(exampleModels[i],tmp);
00299         }
00300 
00301         for(int i=0; i<referenceModels.size(); i++) {
00302                 sprintf_s(tmp,"c:\\referenceModel%d.x3d",i);
00303                 ex.exportModel(referenceModels[i],tmp);
00304         }
00305 
00306 }
00307 
00308 void pcggui::SaveAll() {
00309         char tmp[100];
00310 
00311         for(int i=0; i<exampleModels.size(); i++) {
00312                 sprintf_s(tmp,"c:\\savedExampleModel%d.pcg",i);
00313                 exampleModels[i]->save(tmp);
00314         }
00315 
00316         for(int i=0; i<referenceModels.size(); i++) {
00317                 sprintf_s(tmp,"c:\\savedReferenceModel%d.pcg",i);
00318                 referenceModels[i]->save(tmp);
00319         }
00320 }
00321 
00322 void pcggui::LoadAll() {
00323         char tmp[100];
00324         int number = 10;
00325 
00326         exampleModels.clear();
00327         referenceModels.clear();
00328 
00329         for(int i=0; i<number; i++) {
00330                 sprintf_s(tmp,"c:\\savedExampleModel%d.pcg",i);
00331                 model::ExampleModel* em = new model::ExampleModel();
00332                 if(em->load(tmp)) {
00333                         exampleModels.push_back(em);
00334                 }
00335         }
00336 
00337         for(int i=0; i<number; i++) {
00338                 sprintf_s(tmp,"c:\\savedReferenceModel%d.pcg",i);
00339                 model::ReferenceModel* rm = new model::ReferenceModel();
00340                 if(rm->load(tmp)) {
00341                         referenceModels.push_back(rm);
00342                 }
00343         }
00344 }
00345 
00346 void pcggui::SaveAnalyzer() {
00347         pca.save("c:\\PCG-ANALYZER");
00348 }
00349 
00350 void pcggui::LoadAnalyzer() {
00351         pca.load("c:\\PCG-ANALYZER");
00352 }
00353 
00354 void pcggui::Analyze() {
00355         DebugPrint("*************Starting to Analyze*************\n");
00356         pca.mergeData(*referenceModels[0],exampleModels);
00357         DebugPrint("Calculating mean\n");
00358         pca.mean();
00359         DebugPrint("Adjusting data\n");
00360         pca.adjust();
00361         DebugPrint("Finding Covariance\n");
00362         pca.covariance();
00363         DebugPrint("Begin EigenVector calculations\n");
00364         DebugPrint("Timer Started\n");
00365         clock_t begin = clock();
00366         pca.findComponents(model::Analyzer::HOUSEHOLDER);
00367         clock_t end = clock();
00368         DebugPrint("Timer Stopped\n");
00369         DebugPrint("Time elapsed in seconds: %d\n", difftime(end, begin));
00370         DebugPrint("End EigenVector calculations\n");
00371         pca.computeEnergy();
00372         pca.selectComponents();
00373         pca.transformation();
00374 }
00375 
00376 void pcggui::Render() {
00377         Mesh mesh;
00378         model::ExampleModel* em = exampleModels[0];
00379         mesh.setNumVerts(em->getVertexs().size());
00380         mesh.setNumFaces(em->getFaces().size());
00381         for(int i=0;i<em->getVertexs().size();i++) {
00382                 Vertex* v = em->getVertex(i);
00383                 mesh.setVert(i,Point3(v->getCoordinates()->x,v->getCoordinates()->y,v->getCoordinates()->z));
00384                 mesh.setNormal(i,Point3(v->getNormal()->x,v->getNormal()->y,v->getNormal()->z));
00385         }
00386         for(int i=0;i<em->getFaces().size();i++) {
00387                 model::Face* f = em->getFace(i);
00388                 mesh.faces[i].setVerts(f->getVertex(0)->getIndex(),f->getVertex(1)->getIndex(),f->getVertex(2)->getIndex());
00389         }
00390 
00391         //TriObject *obj = CreateNewTriObject();
00392         //RegisterEditTriObjDesc( GetEditTriObjDesc() );
00393 
00394         // Create a new object through the CreateInstance() API
00395         GeomObject *obj = (GeomObject*)ip->CreateInstance(GEOMOBJECT_CLASS_ID, Class_ID(BOXOBJ_CLASS_ID ,0));
00396         assert(obj);
00397         
00398         
00399         //((TriObject*)obj)->mesh = mesh;
00400         //RegisterEditTriObjDesc( GetTriObjDescriptor() );
00401 
00402 
00403 
00404         // Create a derived object that references the cylinder
00405         IDerivedObject *dobj = CreateDerivedObject(obj);
00406 
00407         // Create a node in the scene that references the derived object
00408         INode *node = ip->CreateObjectNode(dobj);
00409 
00410         // Name the node and make the name unique.
00411         TSTR name(_T("MyNode"));
00412         ip->MakeNameUnique(name);
00413         node->SetName(name);
00414 
00415         // Eval the node's object (exclude WSMs)
00416         Object *oldObj = node->GetObjectRef();
00417         ObjectState os = oldObj->Eval(ip->GetTime());
00418 
00419         // Set the result to be the node's new object (make a clone)
00420         Object *newobj = (Object*)os.obj->Clone();
00421         newobj->SetSubSelState(0); // Reset the selection level to object level
00422         oldObj->SetAFlag(A_LOCK_TARGET);
00423         node->SetObjectRef(newobj);
00424         node->NotifyDependents(FOREVER,0,REFMSG_SUBANIM_STRUCTURE_CHANGED);
00425         oldObj->ClearAFlag(A_LOCK_TARGET);
00426         oldObj->MaybeAutoDelete();
00427 
00428 
00429         // Redraw the viewports
00430         ip->RedrawViews(ip->GetTime());
00431 
00432 }
00433 
00434 /****************************************************************************/
00435 
00436 // Get the first modifier in the pipeline of a node by its class identifier
00437 Modifier* pcggui::getModifier (INode* pNode, Class_ID modCID)
00438 {
00439         Object* pObj = pNode->GetObjectRef();
00440 
00441         if (!pObj)
00442                 return NULL;
00443 
00444         ObjectState os = pNode->EvalWorldState(0);
00445         if (os.obj && (os.obj->SuperClassID() != GEOMOBJECT_CLASS_ID) && (os.obj->SuperClassID() != LIGHT_CLASS_ID) )
00446         {
00447                 return NULL;
00448         }
00449 
00450         // For all derived objects (can be > 1)
00451         while (pObj && (pObj->SuperClassID() == GEN_DERIVOB_CLASS_ID))
00452         {
00453                 IDerivedObject* pDObj = (IDerivedObject*)pObj;
00454                 int m;
00455                 int nNumMods = pDObj->NumModifiers();
00456                 // Step through all modififers and verify the class id
00457                 for (m=0; m<nNumMods; ++m) 
00458                 {
00459                         Modifier* pMod = pDObj->GetModifier(m);
00460                         if (pMod) 
00461                         {
00462                                 if (pMod->ClassID() == modCID) 
00463                                 {
00464                                         // Match! Return it
00465                                         return pMod;
00466                                 }
00467                         }
00468                 }
00469                 pObj = pDObj->GetObjRef();
00470         }
00471 
00472         return NULL;
00473 }
00474 
00475 uint pcggui::ConvertSkin(INode* node, model::ReferenceModel* rm) {
00476         // Return success
00477         uint ok=NoError;
00478 
00479         // Get the skin modifier
00480         Modifier* skin=getModifier (node, SKIN_CLASS_ID);
00481 
00482         // Found it ?
00483         if (skin)
00484         {
00485                 // **********    COMSKIN EXPORT    **********
00486 
00487                 // Get a com_skin2 interface
00488                 ISkin *comSkinInterface=(ISkin*)skin->GetInterface (SKIN_INTERFACE);
00489 
00490                 // Should been controled with isSkin before.
00491                 assert (comSkinInterface);
00492 
00493                 // Found com_skin2 ?
00494                 if (comSkinInterface)
00495                 {
00496                         // Get local data
00497                         ISkinContextData *localData=comSkinInterface->GetContextInterface(node);
00498 
00499                         // Should been controled with isSkin before.
00500                         assert (localData);
00501 
00502                         // Found ?
00503                         if (localData)
00504                         {
00505                                 // Check same vertices count
00506                                 uint vertCount=localData->GetNumPoints();
00507 
00508 
00509                                 // For each vertex
00510                                 for (uint vert=0; vert<vertCount; vert++)
00511                                 {
00512                                         // Get bones count for this vertex
00513                                         uint boneCount=localData->GetNumAssignedBones (vert);
00514 
00515                                         // No bones, can't export
00516                                         if (boneCount==0)
00517                                         {
00518                                                 // Error
00519                                                 ok=VertexWithoutWeight;
00520                                                 break;
00521                                         }
00522 
00523                                         // A map of float / string
00524                                         //std::multimap<float, uint> weightMap;
00525                                         std::map<float, uint> weightMap;
00526 
00527                                         // For each bones
00528                                         for (uint bone=0; bone<boneCount; bone++)
00529                                         {
00530                                                 // Get the bone weight
00531                                                 float weight=localData->GetBoneWeight (vert, bone);
00532 
00533                                                 // Get bone number
00534                                                 uint boneId=localData->GetAssignedBone (vert, bone);
00535 
00536                                                 // Insert in the map
00537                                                 weightMap.insert (std::map<float, uint>::value_type (weight, boneId));
00538                                         }
00539 
00540                                         // Sum the NL3D_MESH_SKINNING_MAX_MATRIX highest bones
00541                                         float sum=0.f;
00542                                         std::map<float, uint>::iterator ite=weightMap.begin();
00543                                         while (ite!=weightMap.end())
00544                                         {
00545                                                 // Add to the sum
00546                                                 sum+=ite->first;
00547 
00548                                                 // Next value
00549                                                 ite++;
00550                                         }
00551 
00552                                         // For each bones in the list, build the skin information
00553                                         uint id=0;
00554                                         ite=weightMap.end();
00555                                         while (ite!=weightMap.begin())
00556                                         {
00557                                                 // Previous value
00558                                                 ite--;
00559 
00560                                                 // Get the bones ID
00561                                                 Bone* bone = rm->getSkeleton()->getBone(comSkinInterface->GetBone(ite->second)->GetName());
00562 
00563                                                 // Set the weight
00564                                                 //DebugPrint("Bone: %s - Vertex: %d - %f\n",bone->getName().c_str(),vert,ite->first/sum);
00565                                                 bone->addMapping(new model::Mapping(vert,ite->first/sum));
00566 
00567                                                 // Next Id
00568                                                 id++;
00569                                         }
00570                                         // Breaked ?
00571                                         if (ite!=weightMap.begin())
00572                                         {
00573                                                 // break again to exit
00574                                                 break;
00575                                         }
00576                                 }
00577                         }
00578                 }
00579 
00580                 // Release the interface
00581                 skin->ReleaseInterface (SKIN_INTERFACE, comSkinInterface);
00582         }
00583         /*else
00584         {
00585                 // **********    PHYSIQUE EXPORT    **********
00586 
00587                 // Physique mode
00588                 Modifier* skin=getModifier (node, PHYSIQUE_CLASS_ID);
00589 
00590                 // Must exist
00591                 assert (skin);
00592                 
00593                 // Get a com_skin2 interface
00594                 IPhysiqueExport *physiqueInterface=(IPhysiqueExport *)skin->GetInterface (I_PHYINTERFACE);
00595 
00596                 // Should been controled with isSkin before.
00597                 assert (physiqueInterface);
00598 
00599                 // Found com_skin2 ?
00600                 if (physiqueInterface)
00601                 {
00602                         // Get local data
00603                         IPhyContextExport *localData=physiqueInterface->GetContextInterface(&node);
00604 
00605                         // Should been controled with isSkin before.
00606                         assert (localData);
00607 
00608                         // Found ?
00609                         if (localData)
00610                         {
00611                                 // Use rigid export
00612                                 localData->ConvertToRigid (TRUE);
00613 
00614                                 // Allow blending
00615                                 localData->AllowBlending (TRUE);
00616 
00617                                 // Check same vertices count
00618                                 uint vertCount=localData->GetNumberVertices();
00619 
00620                                 // Ctrl we have the same number of vertices in the mesh and in the modifier.
00621                                 if (rm->getVertexs().size()!=vertCount)
00622                                 {
00623                                         ok=InvalidSkeleton;
00624                                 }
00625                                 else
00626                                 {
00627                                         // If not the same count, return false (perhaps, the modifier is not in the good place in the stack..)
00628                                         if (rm->getVertexs().size()==vertCount)
00629                                         {
00630                                                 // Rebuild the array
00631                                                 buildMesh.SkinWeights.resize (vertCount);
00632 
00633                                                 // For each vertex
00634                                                 for (uint vert=0; vert<vertCount; vert++)
00635                                                 {
00636                                                         // Get a vertex interface
00637                                                         IPhyVertexExport *vertexInterface=localData->GetVertexInterface (vert);
00638 
00639                                                         // Check if it is a rigid vertex or a blended vertex
00640                                                         IPhyRigidVertex                 *rigidInterface=NULL;
00641                                                         IPhyBlendedRigidVertex  *blendedInterface=NULL;
00642                                                         int type=vertexInterface->GetVertexType ();
00643                                                         if (type==RIGID_TYPE)
00644                                                         {
00645                                                                 // this is a rigid vertex
00646                                                                 rigidInterface=(IPhyRigidVertex*)vertexInterface;
00647                                                         }
00648                                                         else
00649                                                         {
00650                                                                 // It must be a blendable vertex
00651                                                                 assert (type==RIGID_BLENDED_TYPE);
00652                                                                 blendedInterface=(IPhyBlendedRigidVertex*)vertexInterface;
00653                                                         }
00654 
00655                                                         // Get bones count for this vertex
00656                                                         uint boneCount;
00657                                                         if (blendedInterface)
00658                                                         {
00659                                                                 // If blenvertex, only one bone
00660                                                                 boneCount=blendedInterface->GetNumberNodes();
00661                                                         }
00662                                                         else
00663                                                         {
00664                                                                 // If rigid vertex, only one bone
00665                                                                 boneCount=1;
00666                                                         }
00667 
00668                                                         // No bones, can't export
00669                                                         if (boneCount==0)
00670                                                         {
00671                                                                 // Error
00672                                                                 ok=VertexWithoutWeight;
00673                                                                 break;
00674                                                         }
00675 
00676                                                         // A map of float / string
00677                                                         //std::multimap<float, INode*> weightMap;
00678                                                         std::map<float, INode*> weightMap;
00679 
00680                                                         // For each bones
00681                                                         for (uint bone=0; bone<boneCount; bone++)
00682                                                         {
00683                                                                 if (blendedInterface)
00684                                                                 {
00685                                                                         // Get the bone weight
00686                                                                         float weight=blendedInterface->GetWeight(bone);
00687 
00688                                                                         // Get node
00689                                                                         INode *node=blendedInterface->GetNode(bone);
00690                                                                         assert (node);
00691 
00692                                                                         // Insert in the map
00693                                                                         weightMap.insert (std::map<float, INode*>::value_type (weight, node));
00694                                                                 }
00695                                                                 else
00696                                                                 {
00697                                                                         // Get node
00698                                                                         INode *node=rigidInterface->GetNode();
00699                                                                         assert (node);
00700 
00701                                                                         // Insert in the map
00702                                                                         weightMap.insert (std::map<float, INode*>::value_type (1, node));
00703                                                                 }
00704                                                         }
00705 
00706                                                         // Sum the NL3D_MESH_SKINNING_MAX_MATRIX highest bones
00707                                                         float sum=0.f;
00708                                                         std::map<float, INode*>::iterator ite=weightMap.begin();
00709                                                         while (ite!=weightMap.end())
00710                                                         {
00711                                                                 // Add to the sum
00712                                                                 sum+=ite->first;
00713                                                                 
00714                                                                 // Next value
00715                                                                 ite++;
00716                                                         }
00717 
00718                                                         // For each bones in the list, build the skin information
00719                                                         uint id=0;
00720                                                         ite=weightMap.end();
00721                                                         while (ite!=weightMap.begin())
00722                                                         {
00723                                                                 // Previous value
00724                                                                 ite--;
00725 
00726                                                                 // Get the bones ID
00727                                                                 sint32 matrixId=-1;
00728                                                                 TInodePtrInt::const_iterator itId=skeletonShape.find (ite->second);
00729                                                                 if (itId!=skeletonShape.end())
00730                                                                         matrixId=itId->second;
00731 
00732                                                                 // Find the bone ?
00733                                                                 if (matrixId==-1)
00734                                                                 {
00735                                                                         // no, error, wrong skeleton
00736                                                                         ok=InvalidSkeleton;
00737                                                                         break;
00738                                                                 }
00739 
00740                                                                 // Set the weight
00741                                                                 buildMesh.SkinWeights[vert].MatrixId[id]=matrixId;
00742                                                                 buildMesh.SkinWeights[vert].Weights[id]=ite->first/sum;
00743                                                                 
00744                                                                 // Next Id
00745                                                                 id++;
00746                                                         }
00747                                                         
00748                                                         // Breaked ?
00749                                                         if (ite!=weightMap.begin())
00750                                                         {
00751                                                                 // break again to exit
00752                                                                 break;
00753                                                         }
00754 
00755                                                         // Release vertex interfaces
00756                                                         localData->ReleaseVertexInterface (vertexInterface);
00757                                                 }
00758                                         }
00759                                 }
00760 
00761                                 // Release locaData interface
00762                                 physiqueInterface->ReleaseContextInterface (localData);
00763                         }
00764 
00765                 }
00766 
00767                 // Release the interface
00768                 skin->ReleaseInterface (I_PHYINTERFACE, physiqueInterface);
00769         }*/
00770 
00771         return ok;
00772 }

Generated on Tue Apr 17 09:39:11 2007 for PCG Library by  doxygen 1.3.9.1