Ignore:
Timestamp:
05/24/06 10:47:49 (18 years ago)
Author:
gumbau
Message:
 
Location:
GTP/trunk/Lib/Geom/shared/GeoTool/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshLoader.cpp

    r891 r980  
    1 /*      ========================================================================== 
     1/*========================================================================== 
    22 *      (C) 2005 Universitat Jaume I 
    3  *      ========================================================================== 
     3 *========================================================================== 
    44 *      PROYECT:        GAME TOOLS 
    5  *      ==========================================================================*/ 
    6 /**     CONTENT:         
    7         * 
    8         * 
    9         *       @file   GeoMeshLoader.cpp 
    10 /*===========================================================================*/ 
     5 *==========================================================================*/ 
     6/*      CONTENT:         
     7 * 
     8 * 
     9 *      @file   GeoMeshLoader.cpp 
     10 *==========================================================================*/ 
     11 
    1112#include        "GeoMeshLoader.h" 
    1213 
     
    5556        vType   =       static_cast<VertexElementType>(aux.type); 
    5657 
     58        /* 
    5759        if (vType == VET_FLOAT3) 
    5860        { 
     
    6971                printf("%u es float2 ",aux.type); 
    7072        } 
     73        */ 
    7174 
    7275        // unsigned short semantic; // VertexElementSemantic 
     
    115118                list.push_back(aux2); 
    116119        } 
    117          
    118         //      Debug. 
    119         cout    <<      "Finish Vertex Element" <<      endl; 
    120120} 
    121121 
     
    134134      { 
    135135                                case M_GEOMETRY_VERTEX_ELEMENT: 
    136                                         cout    <<      "M_GEOMETRY_VERTEX_ELEMENT"     <<      endl; 
     136 
     137                                        //      Debug. 
     138                                        cout    <<      "               M_GEOMETRY_VERTEX_ELEMENT" 
     139                                                                <<      endl; 
     140                                         
    137141          readGeometryVertexElement(f, geoMesh); 
     142 
    138143          break; 
    139144      } 
     
    153158 
    154159  }//   End if (!feof(f)) 
    155 } 
     160}//     End readGeometryVertexDeclaration. 
    156161 
    157162//      Read geometry vertex buffer. 
     
    172177        k                                               =       0; 
    173178        encontrado      =       false; 
    174  
    175179         
    176180        if (option == GEOMESH_BUILD) 
     
    196200        fread(&vertexSize,sizeof(unsigned short),1,f); 
    197201 
    198         //      Debug. 
    199         cout    <<      "Per-vertex size: " 
    200                                 <<      vertexSize 
    201                                 <<      endl; 
    202          
    203202        // Check for vertex data header 
    204203        headerID        =       readChunk(f); 
     
    211210        else 
    212211        { 
    213                 cout    <<      "M_GEOMETRY_VERTEX_BUFFER_DATA" <<      endl; 
     212                //      Debug. 
     213                cout    <<      "               M_GEOMETRY_VERTEX_BUFFER_DATA" 
     214                                        <<      endl; 
    214215        } 
    215216 
     
    226227        } 
    227228 
    228         //      Considero q los van por grupitos de vertices+normales+... 
    229         //      para cada uno de los vertices 
    230229        if (encontrado) 
    231230        { 
    232                 FILE * salida; 
    233                 salida=fopen(nom_salida,"a"); 
    234                 cout    <<      "----->Leo los vertices" 
    235                                         <<      endl; 
    236  
    237                 for (k=0;k<numVertices;k++) 
    238                 { 
    239                         //Para cada uno de los elementos de ese source. 
    240                         for(int j=0;j<list[i].list.size();j++) 
    241                         { 
    242                                 //      Son posiciones, vertices 
    243                                 if (list[i].list[j].semantic==1) 
     231                for (k  =       0;      k < numVertices;        k++) 
     232                { 
     233                        //      For each element in this source. 
     234                        for(int j       =       0;      j < list[i].list.size();        j++) 
     235                        { 
     236                                //      Positions. 
     237                                if (list[i].list[j].semantic == 1) 
    244238                                { 
    245239                                        fread(&x,sizeof(float),1,f); 
     
    255249                                        } 
    256250                                } 
    257                                 //      si no son vertices no lo quiero 
     251                                //      If are not vertices. 
    258252                                else  
    259253                                { 
    260                                         //Se tendría que hacer con un fseek 
    261                                         if (list[i].list[j].type==2) //Son 3 floats 
     254                                        if (list[i].list[j].type == 2) 
    262255                                        { 
    263256                                                fread(&x,sizeof(float),1,f); 
     
    273266                                                } 
    274267                                        } 
    275                                         if (list[i].list[j].type==1) //Son 2 floats 
     268                                        if (list[i].list[j].type == 1) 
    276269                                        { 
    277270                                                fread(&x,sizeof(float),1,f); 
     
    288281                        } 
    289282                } 
    290                 fclose(salida); 
    291283        } 
    292284        else 
    293285        { 
    294                 cout    <<      "Error" 
     286                //      Debug. 
     287                cout    <<      "Error: Source not found." 
    295288                                        <<      endl; 
    296289        } 
     
    298291} 
    299292 
    300 //      Read geometry. 
     293//      Read Geometry. 
    301294void GeoMeshLoader::readGeometry(FILE   *f, Mesh        *geoMesh, int option) 
    302295{ 
     
    350343                        { 
    351344                                case M_GEOMETRY_VERTEX_DECLARATION: 
    352                                         cout<<"M_GEOMETRY_VERTEX_DECLARATION\n"; 
     345 
     346                                        //      Debug. 
     347                                        cout    <<      "               M_GEOMETRY_VERTEX_DECLARATION" 
     348                                                                <<      endl; 
     349                                         
    353350                                        readGeometryVertexDeclaration(f, geoMesh); 
     351                                         
    354352                                        break; 
     353                                         
    355354                                case M_GEOMETRY_VERTEX_BUFFER: 
    356                                         cout<<"M_GEOMETRY_VERTEX_BUFFER\n"; 
     355                                         
     356                                        //      Debug. 
     357                                        cout    <<      "               M_GEOMETRY_VERTEX_BUFFER" 
     358                                                                <<      endl; 
     359                                         
    357360                                        readGeometryVertexBuffer(f, geoMesh,option); 
     361                                         
    358362                                        break; 
    359363                        } 
     
    365369                        } 
    366370                } 
     371 
    367372                if (!feof(f)) 
    368373                { 
     
    379384{ 
    380385        unsigned        short   opType; 
     386         
    381387        fread(&opType,sizeof(unsigned short),1,f); 
    382         printf("\tLa render operation es %d\n",opType); 
    383388 
    384389        if (option == GEOMESH_BUILD) 
     
    404409        unsigned        int             numIndices; 
    405410        bool                                            idx32bit; 
    406         FILE                                            *salida; 
    407411 
    408412        switch  (option) 
     
    413417                case    GEOMESH_BUILD: 
    414418 
    415                         //      Debug. 
    416                         cout    <<      "Build GEOMESH" <<      endl; 
    417  
    418419                        //      Initialize the new subMesh. 
    419420                        geoSubMesh      =       &geoMesh->mSubMesh[currentSubMesh]; 
    420  
    421                         cout    <<      "Current SubMesh: " 
    422                                                 <<      currentSubMesh 
    423                                                 <<      endl; 
    424         } 
    425          
    426         //La vacio antes de leer el mesh 
     421        } 
     422         
    427423        list.clear();  
    428424 
     425        //      Gets material name. 
    429426        fgets(materialName,255,f); 
    430         printf("\tmaterialName: %s",materialName); 
    431427 
    432428        fread(&useSharedVertices,sizeof(bool),1,f); 
     
    434430        if (option      ==      GEOMESH_BUILD) 
    435431        { 
     432                //      Obtain the material name. 
     433                strcpy(geoSubMesh->mMaterialName,materialName); 
     434                 
    436435                //      Obtain the flag of vertex shared. 
    437436                geoSubMesh->mSharedVertexBuffer =       useSharedVertices; 
     
    441440                        //      The VB of the submesh points to the VB of the mesh. 
    442441                        geoSubMesh->mVertexBuffer       =       geoMesh->mVertexBuffer; 
    443                          
    444                         cout    <<      "\tUse Shared Vertices" 
    445                                                 <<      endl; 
    446  
    447442                } 
    448443        } 
    449444 
    450445        fread(&numIndices,sizeof(unsigned int),1,f); 
    451         printf("\tnumIndices: %u\n",numIndices); 
    452         printf("\tnumTriangles: %u\n",numIndices/3); 
    453446        fread(&idx32bit,sizeof(bool),1,f); 
    454447         
     
    462455        } 
    463456         
     457        /* 
    464458        if (idx32bit) 
    465459        { 
     
    470464                printf("\tSon 16 bits por indice\n"); 
    471465        } 
    472  
    473         salida  =       fopen(nom_salida,"a"); 
    474  
    475         fprintf(salida,"\n#----->Leo un subMesh:"); 
    476          
    477         if (useSharedVertices) 
    478         { 
    479                 fprintf(salida," shared vertices"); 
    480         } 
    481         else 
    482         { 
    483                 fprintf(salida, 
    484                                                 " no shared vertices %d triangles", 
    485                                                 numIndices/3); 
    486         } 
    487  
    488         fprintf(salida,"\n# Indexes:"); 
    489          
     466        */ 
     467 
    490468        //Sumo uno a los vertices porque en el obj no puede hacer un vertice 0 
    491469        if (idx32bit) 
     
    518496        } 
    519497 
    520         fclose(salida); 
    521  
    522498  // M_GEOMETRY chunk  
    523499        // (Optional: present only if useSharedVertices = false) 
     
    528504                if (chunkID != M_GEOMETRY) 
    529505                { 
    530                         printf("Missing geometry data in mesh file\n"); //Error 
     506                        //      Debug. 
     507                        cout    <<      "Error: Missing mesh geometry." 
     508                                                <<      endl; 
    531509                } 
    532510                 
     
    546524                                case M_SUBMESH_OPERATION: 
    547525                                                                 
    548                                         cout<<"M_SUBMESH_OPERATION\n"; 
     526                                        //      Debug. 
     527                                        cout    <<      "               M_SUBMESH_OPERATION" 
     528                                                                <<      endl; 
    549529 
    550530                                        readSubMeshOperation(f, geoMesh, option); 
    551531                                         
    552532                                        break; 
     533                                         
    553534                                case M_SUBMESH_BONE_ASSIGNMENT: 
    554                                         jumpChunk(f); 
     535                                         
     536                                        //      Debug. 
     537                                        //cout  <<      "               M_SUBMESH_BONE_ASSIGNMENT" 
     538                                        //                      <<      endl; 
     539                                         
     540                                        if (option      ==      GEOMESH_BUILD) 
     541                                        { 
     542                                                readSubMeshBoneAssignment(f, geoSubMesh, option); 
     543                                        } 
     544                                        else 
     545                                        { 
     546                                                jumpChunk(f); 
     547                                        } 
     548                                         
    555549                                        break; 
    556550                        } 
     
    564558                if (!feof(f)) 
    565559                { 
    566                         // Backpedal back to start of chunk 
     560                        // Backpedal back to start of chunk. 
    567561                        fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1); 
    568562                } 
     
    591585                if (chunkID != M_MESH_LOD_USAGE) 
    592586                { 
    593                         printf("Missing M_MESH_LOD_USAGE chunk\n"); //Error 
    594                 } 
    595                 else printf("\tM_MESH_LOD_USAGE (%d bytes) ",long_actual); 
     587                        //      Debug. 
     588                        cout    <<      "Error: Missing M_MESH_LOD_USAGE chunk" 
     589                                                <<      endl; 
     590                } 
     591                else 
     592                { 
     593                        cout    <<      "               M_MESH_LOD_USAGE" 
     594                                                <<      endl; 
     595                } 
     596                 
    596597                jumpChunk(f); 
    597598 
     
    612613                        while   (!feof(f) && (chunkID == M_SUBMESH_NAME_TABLE_ELEMENT )) 
    613614                        { 
    614                                 printf("M_SUBMESH_NAME_TABLE_ELEMENT %d bytes\n",long_actual); 
     615                                //      Debug. 
     616                                cout    <<      "               M_SUBMESH_NAME_TABLE_ELEMENT" 
     617                                                        <<      endl; 
    615618                                 
    616619                                // Read in the index of the submesh. 
     
    619622                                // Read in the String and map it to its index. 
    620623                                fgets(string,255,f); 
    621                                 printf("index %u name %s\n",subMeshIndex,string); 
    622  
    623                                 // If we're not end of file get the next chunk ID 
     624 
     625                                // If we're not end of file get the next chunk ID. 
    624626                                if (!feof(f)) 
    625627                                { 
     
    629631                        if (!feof(f)) 
    630632                        { 
    631                                 // Backpedal back to start of chunk 
     633                                // Backpedal back to start of chunk. 
    632634                                fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1); 
    633635                        } 
     
    641643        bool                                            nada; 
    642644 
    643         //      Debug. 
    644         cout    <<      "Read Mesh" 
    645                                 <<      endl; 
    646          
    647645        //      Create the VertexBuffer of the mesh. 
    648646        geoMesh->mVertexBuffer  =       new     VertexBuffer(); 
     
    675673 
    676674                                        //      Debug. 
    677                                         cout    <<      "Geometry"      <<      endl; 
     675                                        cout    <<      "               M_GEOMETRY" 
     676                                                                <<      endl; 
    678677 
    679678                                        readGeometry(f, geoMesh, option); 
     
    682681                                case M_SUBMESH: 
    683682 
    684                                         cout    <<      "SubMesh"       <<      endl; 
     683                                        //      Debug. 
     684                                        cout    <<      "               M_SUBMESH" 
     685                                                                <<      endl; 
    685686 
    686687                                        if (option == GEOMESH_BUILD) 
     
    696697                                case M_MESH_SKELETON_LINK: 
    697698 
    698                                         cout    <<      "M_MESH_SKELETON_LINK  " 
    699                                                 <<      long_actual 
    700                                                 <<      " bytes\n"; 
     699                                        //      Debug. 
     700                                        cout    <<      "               M_MESH_SKELETON_LINK  " 
     701                                                                <<      endl; 
     702                                                                 
     703 
     704                                        readSkeletonLink(f, geoMesh, option); 
     705 
     706                                        break; 
     707 
     708                                case M_MESH_BONE_ASSIGNMENT: 
     709 
     710                                        //      Debug. 
     711                                        //cout  <<      "               M_MESH_BONE_ASSIGNMENT  " 
     712                                        //                      <<      endl; 
     713 
     714                                        //OSCAR 
     715                                        readMeshBoneAssignment(f, geoMesh, option); 
     716 
     717                                        break; 
     718 
     719                                case M_MESH_LOD: 
     720 
     721                                        //      Debug. 
     722                                        cout    <<      "               M_MESH_LOD" 
     723                                                                <<      endl; 
     724                                         
     725                                        readMeshLodInfo(f, geoMesh); 
     726                                        break; 
     727 
     728                                case M_MESH_BOUNDS: 
     729 
     730                                        //      Debug. 
     731                                        cout    <<      "               M_MESH_BOUNDS " 
     732                                                                <<      endl; 
     733 
     734                                        //jumpChunk(f); 
     735                                        readMeshBounds(f, geoMesh, option); 
     736                                        break; 
     737 
     738                                case M_SUBMESH_NAME_TABLE: 
     739 
     740                                        //      Debug. 
     741                                        cout    <<      "               M_SUBMESH_NAME_TABLE " 
     742                                                                <<      endl; 
     743 
     744                                        readSubMeshNameTable(f, geoMesh); 
     745                                        break; 
     746 
     747                                case M_EDGE_LISTS: 
     748 
     749                                        //      Debug. 
     750                                        cout    <<      "               M_EDGE_LISTS " 
     751                                                                <<      endl; 
    701752 
    702753                                        jumpChunk(f); 
    703754                                        break; 
    704  
    705                                 case M_MESH_BONE_ASSIGNMENT: 
    706  
    707                                         cout    <<      "M_MESH_BONE_ASSIGNMENT  " 
    708                                                 <<      long_actual 
    709                                                 <<      " bytes\n"; 
    710  
    711                                         break; 
    712  
    713                                 case M_MESH_LOD: 
    714  
    715                                         cout    <<      "M_MESH_LOD\n"; 
    716                                         readMeshLodInfo(f, geoMesh); 
    717                                         break; 
    718  
    719                                 case M_MESH_BOUNDS: 
    720  
    721                                         cout    <<      "M_MESH_BOUNDS " 
    722                                                 <<      long_actual 
    723                                                 <<      " bytes\n"; 
    724  
    725                                         jumpChunk(f); 
    726                                         break; 
    727  
    728                                 case M_SUBMESH_NAME_TABLE: 
    729  
    730                                         cout    <<      "M_SUBMESH_NAME_TABLE " 
    731                                                 <<      long_actual 
    732                                                 <<      " bytes\n"; 
    733  
    734                                         readSubMeshNameTable(f, geoMesh); 
    735                                         break; 
    736  
    737                                 case M_EDGE_LISTS: 
    738  
    739                                         cout    <<      "M_EDGE_LISTS " 
    740                                                 <<      long_actual 
    741                                                 <<      " bytes\n"; 
    742  
    743                                         jumpChunk(f); 
    744                                         break; 
    745755                        } 
    746756 
     
    756766                        // Backpedal back to start of chunk 
    757767                        fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1); 
    758                         printf("Malo\n"); 
     768 
     769                        //      Debug. 
     770                        cout    <<      "Error: Reading failure." 
     771                                                <<      endl; 
    759772                } 
    760773        } 
     
    974987        char                                            version[255]; 
    975988        char                                            kk; 
    976         FILE                                            *salida; 
    977989        FILE                                            *pFile; 
    978990        SubMesh                                 *geosubmesh; 
     
    981993        currentSubMesh  =       -1; 
    982994         
    983         std::cout       <<      "Load a new Mesh" 
    984                                                 <<      std::endl; 
     995        //      Debug. 
     996        cout    <<      "---------------------------------" 
     997                                <<      endl 
     998                                <<      "\t LOAD MESH" 
     999                                <<      endl 
     1000                                <<      "---------------------------------" 
     1001                                <<      endl; 
    9851002 
    9861003        //      Initialize the return value. 
     
    9921009  if (!pFile) 
    9931010  { 
    994                 printf("No he encontrado el fichero\n"); 
     1011                //      Debug. 
     1012                cout    <<      "Error: File " 
     1013                                        <<      nameFileMesh 
     1014                                        <<      " not found." 
     1015                                        <<      endl; 
     1016                 
    9951017                exit(0); 
    9961018  } 
     
    10041026                if (uno != M_HEADER) 
    10051027                { 
    1006                         cout    <<      "no se ha encontrado el header" 
     1028                        //      Debug. 
     1029                        cout    <<      "Error: Header not found." 
    10071030                                                <<      endl; 
    10081031                } 
     
    10151038                if (strcmp(version,"[MeshSerializer_v1.30]\n")) 
    10161039                { 
    1017                         cout    << 
    1018                                                 "No se puede trabajar con versiones de" 
    1019                                                 << 
    1020                                                 " .mesh diferentes de la 1.3" 
    1021                                                 << 
    1022                                                 endl; 
    1023                 } 
    1024  
    1025                 salida  =       fopen(nom_salida,"w"); 
    1026  
    1027                 fprintf(salida,"# Empiezo el fichero:"); 
    1028  
    1029                 fclose(salida); 
     1040                        //      Debug. 
     1041                        cout    <<      "Error: Wrong mesh version." 
     1042                                                <<      endl 
     1043                                                <<      "Only version 1.3 or older allowed." 
     1044                                                <<      endl; 
     1045                } 
    10301046 
    10311047                while(!feof(pFile)) 
     
    10441060                if (option == SUBMESH_COUNT) 
    10451061                {                
    1046                         cout    <<      "---------------" 
    1047                                                 <<      endl; 
    1048  
    1049                         cout    <<      "SubMesh Count: " 
    1050                                                 <<      geoMesh->mSubMeshCount 
    1051                                                 <<      endl; 
    1052  
    10531062                        geoMesh->mSubMesh       = new SubMesh[geoMesh->mSubMeshCount]; 
    10541063                } 
    10551064                                                         
    1056                 if (feof(pFile)) 
    1057                 { 
    1058                         cout    <<      "------->Hemos llegado al final del fichero" 
    1059                                                 <<      endl; 
    1060                 } 
    1061  
    1062                 //cout  <<      "Press enter to exit\n"; 
    1063  
    10641065                //      Move the curso to the begining of the file. 
    10651066                fseek(pFile,0,SEEK_SET); 
     
    10871088        } 
    10881089         
     1090        //      Sets coods between -1 and 1. 
     1091        normalizeModel(geoMesh); 
     1092 
    10891093        return  geoMesh; 
     1094} 
     1095 
     1096//      Sets coords between -1 and 1. 
     1097void    GeoMeshLoader::normalizeModel(Mesh      *geoMesh) 
     1098{ 
     1099        unsigned        int     i; 
     1100        float                                   maxx, minx, maxy, miny, maxz, minz; 
     1101        float                                   cx, cy, cz, w, h, d, max; 
     1102        float                                   scale; 
     1103        VertexBuffer    *vertex_buffer; 
     1104 
     1105        //      Gets vertex buffer. 
     1106        vertex_buffer   =       geoMesh->mSubMesh[0].mVertexBuffer; 
     1107         
     1108        //      Get the max/mins. 
     1109        maxx = minx = vertex_buffer->mPosition[0].x; 
     1110        maxy = miny = vertex_buffer->mPosition[0].y; 
     1111        maxz = minz = vertex_buffer->mPosition[0].z; 
     1112         
     1113        //      For each submesh. 
     1114        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) 
     1115        { 
     1116                //      Gets the actual submesh. 
     1117                vertex_buffer   =       geoMesh->mSubMesh[submesh].mVertexBuffer; 
     1118 
     1119                //      For each index of the strip. 
     1120                for (int i = 0; i < vertex_buffer->mVertexCount; i++) 
     1121                { 
     1122                        if (maxx < vertex_buffer->mPosition[i].x) 
     1123                        { 
     1124                                maxx = vertex_buffer->mPosition[i].x; 
     1125                        } 
     1126 
     1127                        if (minx > vertex_buffer->mPosition[i].x) 
     1128                        { 
     1129                                minx = vertex_buffer->mPosition[i].x; 
     1130                        } 
     1131 
     1132                        if (maxy < vertex_buffer->mPosition[i].y) 
     1133                        { 
     1134                                maxy = vertex_buffer->mPosition[i].y; 
     1135                        } 
     1136 
     1137                        if (miny > vertex_buffer->mPosition[i].y) 
     1138                        { 
     1139                                miny = vertex_buffer->mPosition[i].y; 
     1140                        } 
     1141 
     1142                        if (maxz < vertex_buffer->mPosition[i].z) 
     1143                        { 
     1144                                maxz = vertex_buffer->mPosition[i].z; 
     1145                        } 
     1146 
     1147                        if (minz > vertex_buffer->mPosition[i].z) 
     1148                        { 
     1149                                minz = vertex_buffer->mPosition[i].z; 
     1150                        } 
     1151                } 
     1152 
     1153                //      If is a shared vertex Buffer. 
     1154                if (geoMesh->mSubMesh[submesh].mSharedVertexBuffer) 
     1155                { 
     1156                        break; 
     1157                } 
     1158        } 
     1159         
     1160        //      Calculate model width, height, and depth. 
     1161        w = fabs(maxx) + fabs(minx); 
     1162        h = fabs(maxy) + fabs(miny); 
     1163        d = fabs(maxz) + fabs(minz); 
     1164 
     1165        //      Calculate center of the model. 
     1166        cx = (maxx + minx) / 2.0; 
     1167        cy = (maxy + miny) / 2.0; 
     1168        cz = (maxz + minz) / 2.0; 
     1169 
     1170 
     1171        //      Calculate max dimension. 
     1172        if (w > h) 
     1173        { 
     1174                max     =       w; 
     1175        } 
     1176        else 
     1177        { 
     1178                max     =       h; 
     1179        } 
     1180 
     1181        if (d > max) 
     1182        { 
     1183                max     =       d; 
     1184        } 
     1185         
     1186        //      Calculate unitizing scale factor. 
     1187        scale = 1.0 / max; 
     1188 
     1189        geoMesh->mMeshBounds.scaleFactor                =       scale; 
     1190 
     1191        //      Translate around center then scale. 
     1192        //      For each submesh. 
     1193        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) 
     1194        { 
     1195                //      Gets the actual submesh. 
     1196                vertex_buffer   =       geoMesh->mSubMesh[submesh].mVertexBuffer; 
     1197 
     1198                //      For each index of the strip. 
     1199                for (int i = 0; i < vertex_buffer->mVertexCount; i++) 
     1200                { 
     1201                        vertex_buffer->mPosition[i].x -= cx; 
     1202                        vertex_buffer->mPosition[i].y -= cy; 
     1203                        vertex_buffer->mPosition[i].z -= cz; 
     1204                        vertex_buffer->mPosition[i].x *= scale; 
     1205                        vertex_buffer->mPosition[i].y *= scale; 
     1206                        vertex_buffer->mPosition[i].z *= scale; 
     1207                } 
     1208 
     1209                //      If is a shared vertex Buffer. 
     1210                if (geoMesh->mSubMesh[submesh].mSharedVertexBuffer) 
     1211                { 
     1212                        break; 
     1213                } 
     1214        } 
     1215 
    10901216} 
    10911217 
     
    10991225GeoMeshLoader::GeoMeshLoader() 
    11001226{ 
    1101         nom_salida      =       "salida22.obj"; 
    11021227        geoMesh                 =       NULL; 
    11031228        mFileSize               =       0; 
     
    11101235} 
    11111236 
     1237//OSCAR 
     1238void GeoMeshLoader::readSubMeshBoneAssignment(FILE* f, SubMesh* geoSubMesh,int option) 
     1239{ 
     1240        if (option == GEOMESH_BUILD) 
     1241        { 
     1242                VertexBoneAssignment assign; 
     1243 
     1244                // unsigned int vertexIndex; 
     1245                fread(&(assign.vertexIndex),sizeof(unsigned int),1,f); 
     1246                // unsigned short boneIndex; 
     1247                fread(&(assign.boneIndex),sizeof(unsigned short),1,f); 
     1248                // float weight 
     1249                fread(&(assign.weight),sizeof(float),1,f); 
     1250 
     1251                geoSubMesh->mBones.push_back(assign); 
     1252        } 
     1253} 
     1254 
     1255//OSCAR 
     1256void GeoMeshLoader::readMeshBoneAssignment(FILE* f, Mesh* geoMesh,int option) 
     1257{ 
     1258        if (option == GEOMESH_BUILD) 
     1259        { 
     1260                VertexBoneAssignment assign; 
     1261 
     1262                // unsigned int vertexIndex; 
     1263                fread(&(assign.vertexIndex),sizeof(unsigned int),1,f); 
     1264                // unsigned short boneIndex; 
     1265                fread(&(assign.boneIndex),sizeof(unsigned short),1,f); 
     1266                // float weight 
     1267                fread(&(assign.weight),sizeof(float),1,f); 
     1268 
     1269 
     1270                geoMesh->mBones.push_back(assign); 
     1271        } 
     1272} 
     1273 
     1274//OSCAR 
     1275void GeoMeshLoader::readSkeletonLink(FILE* f, Mesh* geoMesh,int option) 
     1276{ 
     1277        if (option == GEOMESH_BUILD) 
     1278        { 
     1279                fgets(geoMesh->mSkeletonName,255,f); 
     1280                 
     1281                geoMesh->hasSkeleton    =       true; 
     1282 
     1283                //      Debug. 
     1284                cout    <<      "Skeleton Name: " 
     1285                                        <<      geoMesh->mSkeletonName 
     1286                                        <<      endl; 
     1287        } 
     1288} 
     1289 
     1290//      Read bounding box settings. 
     1291void    GeoMeshLoader::readMeshBounds(FILE      *f, Mesh        *geoMesh,       int     option) 
     1292{ 
     1293        if (option == GEOMESH_BUILD) 
     1294        { 
     1295                fread(&(geoMesh->mMeshBounds.minX),sizeof(float),1,f); 
     1296                fread(&(geoMesh->mMeshBounds.minY),sizeof(float),1,f); 
     1297                fread(&(geoMesh->mMeshBounds.minZ),sizeof(float),1,f); 
     1298                fread(&(geoMesh->mMeshBounds.maxX),sizeof(float),1,f); 
     1299                fread(&(geoMesh->mMeshBounds.maxY),sizeof(float),1,f); 
     1300                fread(&(geoMesh->mMeshBounds.maxZ),sizeof(float),1,f); 
     1301                fread(&(geoMesh->mMeshBounds.radius),sizeof(float),1,f); 
     1302        } 
     1303} 
     1304 
  • GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshSaver.cpp

    r891 r980  
    2323GeoMeshSaver::GeoMeshSaver() 
    2424{ 
    25         mMaterialName                           =       ""; 
    26         mSkeletallyAnimated     =       false; 
    27  
    28         //      Initializa the mesh bounds. 
    29         mMeshBounds.minx                        =       0.0; 
    30         mMeshBounds.miny                        =       0.0; 
    31         mMeshBounds.minz                        =       0.0; 
    32         mMeshBounds.maxx                        =       0.0; 
    33         mMeshBounds.maxy                        =       0.0; 
    34         mMeshBounds.maxz                        =       0.0; 
    35         mMeshBounds.radius              =       0.0; 
    36 } 
    37  
    38 GeoMeshSaver::GeoMeshSaver(GeometryBounds bounds) 
    39 { 
    40         mMaterialName                           =       ""; 
    41         mSkeletallyAnimated     =       false; 
    42  
    43         //      Initializa the mesh bounds. 
    44         mMeshBounds.minx                        =       bounds.minx; 
    45         mMeshBounds.miny                        =       bounds.miny; 
    46         mMeshBounds.minz                        =       bounds.minz; 
    47         mMeshBounds.maxx                        =       bounds.maxx; 
    48         mMeshBounds.maxy                        =       bounds.maxy; 
    49         mMeshBounds.maxz                        =       bounds.maxz; 
    50         mMeshBounds.radius              =       bounds.radius; 
    5125} 
    5226 
     
    5630GeoMeshSaver::~GeoMeshSaver() 
    5731{ 
     32        delete  mGeoMesh; 
    5833} 
    5934 
     
    6338        int                     size; 
    6439        String  name(fileNameMesh); 
    65          
     40 
     41        mGeoMesh        =       new Mesh(); 
     42 
     43        //      Set the mesh. 
     44        *mGeoMesh       =       *geoMesh; 
     45 
     46        //      Debug. 
     47        cout    <<      endl 
     48                                <<      endl 
     49                                <<      "-----------------------------------" 
     50                                <<      endl 
     51                                <<      "\t SAVE MESH" 
     52                                <<      endl 
     53                                <<      "-----------------------------------" 
     54                                <<      endl 
     55                                <<      endl; 
     56 
     57        //      Unnormalize geometry model. 
     58        unnormalizeModel(mGeoMesh); 
     59 
    6660        //      Open the file. 
    6761        mSerializer     =       new     Serializer(     name, 
    6862                                                                                                                                Serializer::WRITE); 
    6963 
    70         //      Set the mesh. 
    71         mGeoMesh        =       geoMesh; 
    7264 
    7365        //      Write the file header. 
     
    7567 
    7668        //      Write the mesh data. 
    77         writeMesh(geoMesh); 
    78  
    79         //      Debug. 
    80         cout    <<      "Write mesh finish" 
    81                                 <<      endl; 
    82          
     69        writeMesh(mGeoMesh); 
     70 
    8371        size    =       mSerializer->GetSize(); 
    8472         
     
    9785void    GeoMeshSaver::writeMesh(Mesh    *geoMesh) 
    9886{ 
    99         //      Debug. 
    100         cout    <<      "Write the chunk header" 
    101                                 <<      endl; 
    102          
    10387        // Header 
    10488        writeChunkHeader(M_MESH, calcMeshSize(geoMesh)); 
    10589 
     90        //      Debug. 
     91        cout    <<      "               M_MESH" 
     92                                <<      endl; 
     93 
    10694        //      Write the skeletally animated flag. 
    107         writeBools(mSkeletallyAnimated,1); 
    108          
    109         cout    <<      "1 - VertexCount: " 
    110                                 <<      geoMesh->mVertexBuffer->mVertexCount 
    111                                 <<      endl; 
    112  
     95        writeBools(geoMesh->hasSkeleton,1); 
     96         
    11397        //      Write shared geometry. 
    11498        if (geoMesh->mVertexBuffer->mVertexCount > 0) 
    11599        { 
    116                 //      Debug. 
    117                 cout    <<      "Write the shared vertex buffer" 
    118                                         <<      endl; 
    119                  
    120100                writeGeometry(geoMesh->mVertexBuffer); 
    121101        } 
     
    124104        for (int i = 0; i < geoMesh->mSubMeshCount;     i++) 
    125105        { 
    126                 //      Debug. 
    127                 cout    <<      "Write the submesh " 
    128                                         <<      i 
    129                                         <<      endl; 
    130                  
    131106                writeSubMesh(&geoMesh->mSubMesh[i]); 
    132107        } 
    133108 
     109        //OSCAR 
     110        // Write skeleton info if required 
     111        if (geoMesh->hasSkeleton) 
     112        { 
     113                // Write skeleton link 
     114                writeSkeletonLink(geoMesh->mSkeletonName); 
     115 
     116                // Write bone assignments 
     117                if (!geoMesh->mBones.empty()) 
     118                { 
     119 
     120                        for (int i=0; i< geoMesh->mBones.size(); i++) 
     121                        { 
     122                                writeMeshBoneAssignment(geoMesh->mBones[i]); 
     123                        } 
     124 
     125                } 
     126        } 
     127 
    134128        //      Write the mesh bounds. 
    135         writeMeshBounds(mMeshBounds); 
    136          
     129        writeMeshBounds(geoMesh); 
     130 
    137131        /* 
    138         // Write skeleton info if required 
    139         if (pMesh->hasSkeleton()) 
    140         { 
    141         LogManager::getSingleton().logMessage("Exporting skeleton link..."); 
    142         // Write skeleton link 
    143         writeSkeletonLink(pMesh->getSkeletonName()); 
    144         LogManager::getSingleton().logMessage("Skeleton link exported."); 
    145  
    146         // Write bone assignments 
    147         if (!pMesh->mBoneAssignments.empty()) 
    148         { 
    149         LogManager::getSingleton().logMessage("Exporting shared geometry bone assignments..."); 
    150  
    151         Mesh::VertexBoneAssignmentList::const_iterator vi; 
    152         for (vi = pMesh->mBoneAssignments.begin();  
    153         vi != pMesh->mBoneAssignments.end(); ++vi) 
    154         { 
    155         writeMeshBoneAssignment(vi->second); 
    156         } 
    157  
    158         LogManager::getSingleton().logMessage("Shared geometry bone assignments exported."); 
    159         } 
    160         } 
    161  
    162132        // Write LOD data if any 
    163133        if (pMesh->getNumLodLevels() > 1) 
     
    186156        } 
    187157        */ 
    188 } 
     158}//     End write mesh. 
    189159 
    190160//      Write a submesh. 
     
    193163        bool                                            idx32bit; 
    194164        unsigned        long    indexCount; 
    195         String                                  materialName; 
    196165        Index                                           *index; 
    197166        Index                                           *indexBegin; 
    198167        Index                                           *indexEnd; 
    199168         
    200         //      Debug. 
    201         cout    <<      "Write the submesh chunk header" 
    202                                 <<      endl; 
    203          
    204169        // Header. 
    205170        writeChunkHeader(M_SUBMESH, calcSubMeshSize(geoSubMesh)); 
    206171 
    207172        //      Debug. 
    208         cout    <<      "Write the material name" 
     173        cout    <<      "               M_SUBMESH" 
    209174                                <<      endl; 
    210175 
    211176        // Material Name. 
    212         materialName    =       getMaterialName(); 
    213         writeString(materialName); 
    214  
    215         //      Debug. 
    216         cout    <<      "Write the shared vertex buffer flag: " 
    217                                 <<      endl; 
     177        mSerializer->WriteData(geoSubMesh->mMaterialName); 
    218178 
    219179        // bool useSharedVertices 
     
    228188        } 
    229189         
     190        //      Write index count. 
    230191        writeInts(indexCount, 1); 
    231192 
     
    234195        writeBools(idx32bit, 1); 
    235196 
    236         //      Debug. 
    237         cout    <<      "Write the mIndex array" 
    238                                 <<      endl; 
    239  
    240197        //      If the submesh is in triangle strips. 
    241198        if (geoSubMesh->mType == GEO_TRIANGLE_STRIPS) 
    242199        { 
    243                 //      Debug. 
    244                 cout    <<      "Write triangle strips" 
    245                                         <<      endl; 
    246                  
    247200                //      For each one of the strips. 
    248201                for (int strip = 0; strip < geoSubMesh->mStripCount; strip++) 
     
    300253        if (!geoSubMesh->mSharedVertexBuffer) 
    301254        { 
    302                 //      Debug. 
    303                 cout    <<      "Write the geometry of the submesh" 
    304                                         <<      endl; 
    305  
    306255                writeGeometry(geoSubMesh->mVertexBuffer); 
    307256        } 
    308  
    309         //      Debug. 
    310         cout    <<      "mIndex array written" 
    311                                 <<      endl; 
    312257 
    313258        // Operation type 
    314259        writeSubMeshOperation(geoSubMesh); 
    315260 
    316         //TODO:Bones. 
    317         /* 
    318261        // Bone assignments 
    319         if (!s->mBoneAssignments.empty()) 
    320         { 
    321                 LogManager::getSingleton().logMessage("Exporting dedicated geometry bone assignments..."); 
    322  
    323                 SubMesh::VertexBoneAssignmentList::const_iterator vi; 
    324  
    325                 for (vi = s->mBoneAssignments.begin();  
    326                                 vi != s->mBoneAssignments.end(); ++vi) 
     262        if (!geoSubMesh->mBones.empty()) 
     263        { 
     264                for (int i = 0; i < geoSubMesh->mBones.size(); i++) 
    327265                { 
    328                         writeSubMeshBoneAssignment(vi->second); 
     266                        writeSubMeshBoneAssignment(geoSubMesh->mBones[i]); 
    329267                } 
    330  
    331                 LogManager::getSingleton().logMessage("Dedicated geometry bone assignments exported."); 
    332         } 
    333         */ 
     268        } 
    334269} 
    335270 
     
    340275        unsigned        long    size; 
    341276 
    342         //      Debug. 
    343         cout    <<      "Write submesh operation" 
    344                                 <<      endl; 
    345          
    346277        size    =       CHUNK_OVERHEAD_SIZE     +       sizeof(unsigned short); 
    347278         
     
    349280        writeChunkHeader(M_SUBMESH_OPERATION, size); 
    350281 
     282        //      Debug. 
     283        cout    <<      "               M_SUBMESH_OPERATION" 
     284                                <<      endl; 
     285 
    351286        //      If the mesh is in triangle strips. 
    352287        if (geoSubMesh->mType   ==      GEO_TRIANGLE_STRIPS) 
     
    361296         
    362297        writeShorts(opType, 1); 
    363  
    364         //      Debug. 
    365         cout    <<      "Submesh operation Written" 
    366                                 <<      endl; 
    367298} 
    368299 
     
    385316        writeChunkHeader(M_GEOMETRY, size); 
    386317 
     318        //      Debug. 
     319        cout    <<      "               M_GEOMETRY" 
     320                                <<      endl; 
     321 
    387322        writeInts(vertexBuffer->mVertexCount,1); 
    388323 
     
    398333        writeChunkHeader(       M_GEOMETRY_VERTEX_DECLARATION,size); 
    399334 
     335        //      Debug. 
     336        cout    <<      "               M_GEOMETRY_VERTEX_DECLARATION" 
     337                                <<      endl; 
     338 
    400339        //      Obtain the size of the vertex element chunk. 
    401340        size    =       CHUNK_OVERHEAD_SIZE     +       (sizeof(unsigned short) *       5); 
     
    405344        writeChunkHeader(       M_GEOMETRY_VERTEX_ELEMENT,size); 
    406345 
     346        //      Debug. 
     347        cout    <<      "               M_GEOMETRY_VERTEX_ELEMENT" 
     348                                <<      endl; 
     349 
    407350        element =       0; 
    408351        writeShorts(element,1); 
     
    424367        writeChunkHeader(       M_GEOMETRY_VERTEX_ELEMENT,size); 
    425368         
     369        //      Debug. 
     370        cout    <<      "               M_GEOMETRY_VERTEX_ELEMENT" 
     371                                <<      endl; 
     372 
    426373        element =       1; 
    427374        writeShorts(element,1); 
     
    443390        writeChunkHeader(M_GEOMETRY_VERTEX_ELEMENT,size); 
    444391         
     392        //      Debug. 
     393        cout    <<      "               M_GEOMETRY_VERTEX_ELEMENT" 
     394                                <<      endl; 
     395 
    445396        element =       2; 
    446397        writeShorts(element,1); 
     
    463414        //      Write the vertex buffer header for positions. 
    464415        writeChunkHeader(M_GEOMETRY_VERTEX_BUFFER,      size); 
     416 
     417        //      Debug. 
     418        cout    <<      "               M_GEOMETRY_VERTEX_BUFFER" 
     419                                <<      endl; 
    465420 
    466421        element =       0; 
     
    478433        writeChunkHeader(M_GEOMETRY_VERTEX_BUFFER_DATA, size); 
    479434 
     435        //      Debug. 
     436        cout    <<      "               M_GEOMETRY_VERTEX_BUFFER_DATA" 
     437                                <<      endl; 
     438 
    480439        //      Write all the positions coords. 
    481440        mSerializer->WriteArray(vertexBuffer->mPosition, 
     
    487446        //      Write the vertex buffer header. 
    488447        writeChunkHeader(       M_GEOMETRY_VERTEX_BUFFER,       size); 
     448 
     449        //      Debug. 
     450        cout    <<      "               M_GEOMETRY_VERTEX_BUFFER" 
     451                                <<      endl; 
    489452 
    490453        element =       1; 
     
    502465        writeChunkHeader(M_GEOMETRY_VERTEX_BUFFER_DATA, size); 
    503466 
     467        //      Debug. 
     468        cout    <<      "               M_GEOMETRY_VERTEX_BUFFER_DATA" 
     469                                <<      endl; 
     470 
    504471        //      Write all the normals coords. 
    505472        mSerializer->WriteArray(vertexBuffer->mNormal, 
     
    511478        //      Write the vertex buffer header for textures. 
    512479        writeChunkHeader(M_GEOMETRY_VERTEX_BUFFER,      size); 
     480 
     481        //      Debug. 
     482        cout    <<      "               M_GEOMETRY_VERTEX_BUFFER" 
     483                                <<      endl; 
    513484 
    514485        element =       2; 
     
    527498 
    528499        //      Debug. 
    529         cout    <<      "-7-"   <<      endl; 
     500        cout    <<      "               M_GEOMETRY_VERTEX_BUFFER_DATA" 
     501                                <<      endl; 
    530502 
    531503        //      Write all the texture coords. 
    532504        mSerializer->WriteArray(vertexBuffer->mTexCoords, 
    533505                                                                                                        vertexBuffer->mVertexCount); 
    534  
    535         //      Debug. 
    536         cout    <<      "-8-"   <<      endl; 
    537  
    538 } 
    539  
    540 //      TODO: Write Mesh Bounds. 
    541 void    GeoMeshSaver::writeMeshBounds(GeometryBounds bounds) 
     506} 
     507 
     508//      Write Mesh Bounds. 
     509void    GeoMeshSaver::writeMeshBounds(Mesh      *geoMesh) 
    542510{ 
    543511        size_t  size; 
     
    549517        writeChunkHeader(M_MESH_BOUNDS, size); 
    550518 
    551         writeFloats(bounds.minx,1); 
    552         writeFloats(bounds.miny,1); 
    553         writeFloats(bounds.minz,1); 
    554         writeFloats(bounds.maxx,1); 
    555         writeFloats(bounds.maxy,1); 
    556         writeFloats(bounds.maxz,1); 
    557         writeFloats(bounds.radius,1); 
     519        //      Debug. 
     520        cout    <<      "               M_MESH_BOUNDS" 
     521                                <<      endl; 
     522 
     523        writeFloats(geoMesh->mMeshBounds.minX,1); 
     524        writeFloats(geoMesh->mMeshBounds.minY,1); 
     525        writeFloats(geoMesh->mMeshBounds.minZ,1); 
     526        writeFloats(geoMesh->mMeshBounds.maxX,1); 
     527        writeFloats(geoMesh->mMeshBounds.maxY,1); 
     528        writeFloats(geoMesh->mMeshBounds.maxZ,1); 
     529        writeFloats(geoMesh->mMeshBounds.radius,1); 
    558530} 
    559531 
     
    563535        size_t size = CHUNK_OVERHEAD_SIZE; 
    564536 
    565         //      Debug. 
    566         cout    <<      "Calc the size of the mesh" 
    567                                 <<      endl; 
    568          
    569537        // Num shared vertices 
    570538        size += sizeof(uint32); 
    571539 
    572         //      Debug. 
    573         cout    <<      "VertexCount: " 
    574                                 <<      geoMesh->mVertexBuffer->mVertexCount 
    575                                 <<      endl; 
    576  
    577540        // Geometry 
    578541        if (geoMesh->mVertexBuffer->mVertexCount > 0) 
    579542        { 
    580                 //      Debug. 
    581                 cout    <<      "Calc the size of the shared vertex buffer" 
    582                                         <<      endl; 
    583                  
    584543                size += calcGeometrySize(geoMesh->mVertexBuffer); 
    585544        } 
     
    588547        for (unsigned short i = 0; i < geoMesh->mSubMeshCount; ++i) 
    589548        { 
    590                 //      Debug. 
    591                 cout    <<      "Calc the size of the submesh " 
    592                                         <<      i 
    593                                         <<      endl; 
    594  
    595549                size += calcSubMeshSize(&geoMesh->mSubMesh[i]); 
    596550        } 
     
    601555                                                (sizeof(float)  *       7); 
    602556 
    603         /* 
    604557        // Skeleton link 
    605         if (geoMesh->hasSkeleton()) 
    606         { 
    607                 size += calcSkeletonLinkSize(geoMesh->getSkeletonName()); 
    608         } 
    609         */ 
     558        if (geoMesh->hasSkeleton) 
     559        { 
     560                size += calcSkeletonLinkSize(geoMesh); 
     561        } 
    610562 
    611563        /* 
     
    629581 
    630582        // Material name 
    631         size += getMaterialName().length() + 1; 
     583        size += strlen(geoSubMesh->mMaterialName); 
    632584 
    633585        // bool useSharedVertices 
     
    682634} 
    683635 
     636//      Calculate the skeleton link size in bytes. 
     637size_t  GeoMeshSaver::calcSkeletonLinkSize(const Mesh   *geoMesh) 
     638{ 
     639        size_t  size    =               CHUNK_OVERHEAD_SIZE; 
     640        size                                    +=      strlen(geoMesh->mSkeletonName); 
     641 
     642        //      Debug. 
     643        cout    <<      "Length Skeleton Link: " 
     644                                <<      strlen(geoMesh->mSkeletonName) 
     645                                <<      endl; 
     646 
     647        return  size; 
     648} 
     649 
    684650//      Write the file header. 
    685651void GeoMeshSaver::writeFileHeader(void) 
    686652{ 
    687         String  mesh_version("[MeshSerializer_v1.30]"); 
     653        String  mesh_version("[MeshSerializer_v1.30]\n"); 
    688654         
    689655        writeShorts(M_HEADER, 1); 
     
    735701} 
    736702 
    737 //      Sets the material of the mesh. 
    738 void    GeoMeshSaver::setMaterialName(String    materialName) 
    739 { 
    740         mMaterialName   =       materialName; 
    741 } 
    742  
    743 //      Gets the material of the mesh. 
    744 String  GeoMeshSaver::getMaterialName() 
    745 { 
    746         return  mMaterialName; 
    747 } 
    748  
    749 //      Set Mesh Bounds. 
    750 void    GeoMeshSaver::setMeshBounds(GeometryBounds      bounds) 
    751 { 
    752         mMeshBounds.minx                =       bounds.minx; 
    753         mMeshBounds.miny                =       bounds.miny; 
    754         mMeshBounds.minz                =       bounds.minz; 
    755         mMeshBounds.maxx                =       bounds.maxx; 
    756         mMeshBounds.maxy                =       bounds.maxy; 
    757         mMeshBounds.maxz                =       bounds.maxz; 
    758         mMeshBounds.radius      =       bounds.radius; 
    759 } 
    760  
     703void GeoMeshSaver::writeSkeletonLink(const String& skelName) 
     704{        
     705        writeChunkHeader(M_MESH_SKELETON_LINK, calcSkeletonLinkSize(mGeoMesh)); 
     706 
     707        //      Debug. 
     708        cout    <<      "               M_MESH_SKELETON_LINK" 
     709                                <<      endl; 
     710 
     711    writeString(skelName); 
     712} 
     713 
     714void GeoMeshSaver::writeMeshBoneAssignment(const VertexBoneAssignment& assign) 
     715{ 
     716        size_t size = CHUNK_OVERHEAD_SIZE + sizeof(unsigned int) + sizeof(unsigned short)+ sizeof(float); 
     717 
     718        writeChunkHeader(M_MESH_BONE_ASSIGNMENT, size); 
     719 
     720        // unsigned int vertexIndex; 
     721        writeInts(assign.vertexIndex, 1); 
     722        // unsigned short boneIndex; 
     723        writeShorts(assign.boneIndex, 1); 
     724        // float weight; 
     725        writeFloats(assign.weight, 1); 
     726} 
     727 
     728void GeoMeshSaver::writeSubMeshBoneAssignment(const VertexBoneAssignment& assign) 
     729{ 
     730        size_t size = CHUNK_OVERHEAD_SIZE + sizeof(unsigned int) + sizeof(unsigned short)+ sizeof(float); 
     731 
     732        writeChunkHeader(M_SUBMESH_BONE_ASSIGNMENT, size); 
     733 
     734        // unsigned int vertexIndex; 
     735        writeInts(assign.vertexIndex, 1); 
     736        // unsigned short boneIndex; 
     737        writeShorts(assign.boneIndex, 1); 
     738        // float weight; 
     739        writeFloats(assign.weight, 1); 
     740} 
     741 
     742//      unnormalize geometry model. 
     743void    GeoMeshSaver::unnormalizeModel(Mesh     *geoMesh) 
     744{ 
     745        float   maxx; 
     746        float   maxy; 
     747        float   maxz; 
     748        float   minx; 
     749        float   miny; 
     750        float   minz; 
     751        float   cx; 
     752        float   cy; 
     753        float   cz; 
     754        float   scale; 
     755         
     756        VertexBuffer    *vertex_buffer; 
     757 
     758        maxx    =       geoMesh->mMeshBounds.maxX; 
     759        maxy    =       geoMesh->mMeshBounds.maxY; 
     760        maxz    =       geoMesh->mMeshBounds.maxZ; 
     761        minx    =       geoMesh->mMeshBounds.minX; 
     762        miny    =       geoMesh->mMeshBounds.minY; 
     763        minz    =       geoMesh->mMeshBounds.minZ; 
     764        scale   =       geoMesh->mMeshBounds.scaleFactor; 
     765         
     766        //      Calculate center of the model. 
     767        cx = (maxx + minx) / 2.0; 
     768        cy = (maxy + miny) / 2.0; 
     769        cz = (maxz + minz) / 2.0; 
     770 
     771        //      Translate around center then scale. 
     772        //      For each submesh. 
     773        for (int submesh = 0; submesh < geoMesh->mSubMeshCount; submesh++) 
     774        { 
     775                //      Gets the actual submesh. 
     776                vertex_buffer   =       geoMesh->mSubMesh[submesh].mVertexBuffer; 
     777 
     778                //      For each index of the strip. 
     779                for (int i = 0; i < vertex_buffer->mVertexCount; i++) 
     780                { 
     781                        vertex_buffer->mPosition[i].x /= scale; 
     782                        vertex_buffer->mPosition[i].y /= scale; 
     783                        vertex_buffer->mPosition[i].z /= scale; 
     784                        vertex_buffer->mPosition[i].x += cx; 
     785                        vertex_buffer->mPosition[i].y += cy; 
     786                        vertex_buffer->mPosition[i].z += cz; 
     787                } 
     788 
     789                //      If is a shared vertex Buffer. 
     790                if (geoMesh->mSubMesh[submesh].mSharedVertexBuffer) 
     791                { 
     792                        break; 
     793                } 
     794        } 
     795} 
     796 
  • GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshView.cpp

    r895 r980  
    200200//      Set the color to the submesh sumbmeshIndex 
    201201//--------------------------------------------------------------------------- 
    202 void    GeoMeshView::setColorSubmeshi(int submeshIndex) 
    203 { 
    204         colorsubmeshi   =       submeshIndex; 
     202void    GeoMeshView::setSubMeshSelected(int submeshIndex) 
     203{ 
     204        mSubMeshSelected        =       submeshIndex; 
    205205} 
    206206 
     
    208208//      Sets the leaves submesh index. 
    209209//--------------------------------------------------------------------------- 
    210 void    GeoMeshView::setLeavesSubmesh(int submeshIndex) 
    211 { 
    212         leavesSubmesh   =       submeshIndex; 
     210void    GeoMeshView::setLeavesSubMesh(int submeshIndex) 
     211{ 
     212        leavesSubMesh   =       submeshIndex; 
    213213} 
    214214 
     
    216216//      Gets the leaves submesh index. 
    217217//--------------------------------------------------------------------------- 
    218 int     GeoMeshView::getLeavesSubmesh() 
    219 { 
    220         return  leavesSubmesh; 
     218int     GeoMeshView::getLeavesSubMesh() 
     219{ 
     220        return  leavesSubMesh; 
    221221} 
    222222 
     
    277277} 
    278278 
     279/* 
    279280//--------------------------------------------------------------------------- 
    280281//      Get the bounding box of the object. 
     
    386387        return  bounds; 
    387388} 
     389*/ 
    388390 
    389391//--------------------------------------------------------------------------- 
     
    446448        GLfloat color_blanco[]  =       {1.0f,1.0f,1.0f,1.0f}; 
    447449 
    448         this->geoMesh   =       geomesh; 
    449         colorsubmeshi   =       -1; 
    450         leavesSubmesh   =       -1; 
     450        this->geoMesh                   =       geomesh; 
     451        mSubMeshSelected        =       -1; 
     452        leavesSubMesh                   =       -1; 
     453        mScaleFactor                    =       geomesh->mMeshBounds.scaleFactor; 
    451454         
    452455        //      Refresh vertices to the vertex array. 
     
    577580//      Repaint the Mesh. 
    578581//--------------------------------------------------------------------------- 
    579 void GeoMeshView::drawGeoMesh(int selectedMesh) 
     582void GeoMeshView::drawGeoMesh(bool      paintWire) 
    580583{ 
    581584        SubMesh *geosubmesh; 
     
    601604                                if (geosubmesh->mType   ==      GEO_TRIANGLE_LIST) 
    602605                                { 
    603                                         if (selectedMesh == submesh) 
     606                                        if (mSubMeshSelected == submesh) 
    604607                                        { 
    605608                                                submesh_selected        = true; 
     
    611614                                         
    612615                                        //      Draw the actual submesh in triangle list. 
    613                                         drawTriangleList(submesh,submesh_selected); 
     616                                        drawTriangleList(submesh,submesh_selected,paintWire); 
    614617                                } 
    615618                                else 
     
    618621                                        drawTriangleStrip(submesh); 
    619622                                } 
    620                         }                
    621                 } 
    622                 /* 
    623                 //      Choose the render operation. 
    624                 else if (geoMesh->mType == GEO_TRIANGLE_LIST) 
    625                 { 
    626                         //      Paint vertices. 
    627                         drawTriangleList(selectedMesh); 
    628                 } 
    629                 else 
    630                 { 
    631                         //      Paint vertices. 
    632                         drawTriangleStrip(); 
    633                 } 
    634                 */ 
     623                        } 
     624                } 
    635625        } 
    636626}//     End drawGeoMesh. 
     
    743733//--------------------------------------------------------------------------- 
    744734void GeoMeshView::drawTriangleList(     int                     submesh, 
    745                                                                                                                                                 bool            selectedSubMesh) 
     735                                                                                                                                                bool            selectedSubMesh, 
     736                                                                                                                                                bool            paintWire) 
    746737{ 
    747738        SubMesh *geosubmesh; 
     
    761752        geosubmesh      =       &geoMesh->mSubMesh[submesh]; 
    762753         
    763         //      If a mesh is selected. 
    764         if (selectedSubMesh) 
    765         { 
    766                 glColor3d(1.0,0.0,0.0); 
    767                 GLfloat red[]   =       {1.0f,0.0f,0.0f,1.0f}; 
    768                 glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,red); 
    769         } 
    770         //      If is a tree. 
    771         else if (leavesSubmesh >= 0) 
    772         { 
    773                 glColor3d(1.0, 0.5, 0.5); 
    774                 GLfloat brown[] =       {1.0f,0.5f,0.5f,1.0f}; 
    775                 glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,brown); 
    776  
    777                 //      For each index of the strip. 
    778                 if (submesh == leavesSubmesh) 
    779                 { 
    780                         glColor3d(0.0,1.0,0.0); 
    781                         GLfloat green[] =       {0.0f,1.0f,0.0f,1.0f}; 
    782                         glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,green); 
    783                 } 
    784         } 
    785         //      If is NOT a tree. 
    786         else 
    787         { 
    788                 //      Set white color to the object. 
    789                 glColor3d(1.0, 1.0, 1.0); 
    790                 GLfloat white[] =       {1.0f,1.0f,1.0f,1.0f}; 
    791                 glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white); 
    792         } 
    793  
    794  
     754        //      If wire is not selected. 
     755        if (!paintWire) 
     756        { 
     757                //      If a mesh is selected. 
     758                if (selectedSubMesh) 
     759                { 
     760                        glColor3d(1.0,0.0,0.0); 
     761                        GLfloat red[]   =       {1.0f,0.0f,0.0f,1.0f}; 
     762                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,red); 
     763                } 
     764                //      If is a tree. 
     765                else if (leavesSubMesh >= 0) 
     766                { 
     767                        glColor3d(1.0, 0.5, 0.5); 
     768                        GLfloat brown[] =       {1.0f,0.5f,0.5f,1.0f}; 
     769                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,brown); 
     770 
     771                        //      For each index of the strip. 
     772                        if (submesh == leavesSubMesh) 
     773                        { 
     774                                glColor3d(0.0,1.0,0.0); 
     775                                GLfloat green[] =       {0.0f,1.0f,0.0f,1.0f}; 
     776                                glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,green); 
     777                        } 
     778                } 
     779                //      If is NOT a tree. 
     780                else 
     781                { 
     782                        //      Set white color to the object. 
     783                        glColor3d(1.0, 1.0, 1.0); 
     784                        GLfloat white[] =       {1.0f,1.0f,1.0f,1.0f}; 
     785                        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white); 
     786                } 
     787        } 
     788         
    795789        //      Enable arrays. 
    796790        glEnableClientState(GL_VERTEX_ARRAY); 
     
    10561050        //      Frustrum. 
    10571051        glViewport(0,0,w(),h()); 
    1058         gluPerspective(60,1,0.1,1000); 
     1052        gluPerspective(60,(float)w()/(float)h(),0.1,1000); 
    10591053 
    10601054        glMatrixMode(GL_MODELVIEW); 
     
    10821076        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    10831077 
    1084         gluLookAt(0,0,mScaleFactor*2+size, 0,0,0, 0,1,0); 
     1078        gluLookAt(0,0,2/*mScaleFactor*/ + size * 0.25, 0,0,0, 0,1,0); 
    10851079 
    10861080        glTranslatef(xshift,yshift,0); 
    10871081        glRotatef(hAng,0,1,0); 
    10881082        glRotatef(vAng,1,0,0); 
    1089         glTranslatef(-xMed,-yMed,-zMed); 
     1083        //glTranslatef(-xMed,-yMed,-zMed); 
    10901084 
    10911085        //      Set white color to the object. 
     
    10941088        glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,color_blanco); 
    10951089 
     1090        glClearColor(0,0,0,0); 
     1091         
    10961092        if (mCW) 
    10971093        { 
     
    11171113                glDisable(GL_LIGHTING); 
    11181114                glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); 
    1119                 drawGeoMesh(colorsubmeshi); 
     1115                drawGeoMesh(false); 
    11201116        } 
    11211117        else 
     
    11301126                glColor3d(0.0, 0.0, 1.0); 
    11311127                glPolygonMode (GL_FRONT_AND_BACK, GL_LINE); 
    1132                 drawGeoMesh(-1); 
     1128                drawGeoMesh(true); 
    11331129        } 
    11341130        else 
     
    11391135                        glEnable(GL_LIGHTING); 
    11401136                        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
    1141                         drawGeoMesh(colorsubmeshi); 
     1137                        drawGeoMesh(false); 
    11421138                } 
    11431139        } 
  • GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshViewUI.cpp

    r895 r980  
    6363        { 
    6464                //      Create a mesh saver with mesh bounds. 
    65                 mesh_saver      =       new     GeoMeshSaver(mMeshBounds); 
     65                mesh_saver      =       new     GeoMeshSaver(); 
    6666 
    6767                //      Save the file mesh. 
     
    9999                if (fcho->value()) 
    100100                { 
    101                         mesh_saver      =       new     GeoMeshSaver(mMeshBounds); 
     101                        mesh_saver      =       new     GeoMeshSaver(); 
    102102                        mesh_saver->save(mGeoMesh,filename_name(fcho->value())); 
    103103                        delete  mesh_saver; 
     
    953953                                        //      Simplify the mesh object. 
    954954                                        simplifyEdgeCollapse(); 
     955                                         
     956                                        delete  mMeshSimplifier; 
     957                                        geoMeshView->restoreContext(); 
    955958                                } 
    956959                                break; 
     
    10431046                                        //      Create the qslim simplification sequence. 
    10441047                                        createQslimSequence(); 
     1048                                         
     1049                                        delete  mMeshSimplifier; 
     1050                                        geoMeshView->restoreContext(); 
    10451051                                } 
    10461052 
     
    10531059 
    10541060                                //      Set the leaves submesh. 
    1055                                 geoMeshView->setLeavesSubmesh(idMeshLeaves); 
     1061                                geoMeshView->setLeavesSubMesh(idMeshLeaves); 
    10561062 
    10571063                                //      Refresh mesh info. 
     
    11931199                                                geoMeshView->setMesh(mGeoMesh); 
    11941200 
    1195                                                 mesh_saver      =       new     GeoMeshSaver(mMeshBounds); 
     1201                                                mesh_saver      =       new     GeoMeshSaver(); 
    11961202                                                file_name[strlen(file_name) - 4]        =       '\0'; 
    11971203                                                mesh_saver->save(mGeoMesh, strcat(file_name,".mesh")); 
     
    13541360        mGeometryBased->set(); 
    13551361 
    1356         mImageBased->set_visible(); 
    1357         mImageBased->activate(); 
     1362        mViewPointDriven->set_visible(); 
     1363        mViewPointDriven->activate(); 
    13581364 
    13591365        /* 
     
    14011407        mGeometryBased->deactivate(); 
    14021408 
    1403         mImageBased->hide(); 
    1404         mImageBased->deactivate(); 
     1409        mViewPointDriven->hide(); 
     1410        mViewPointDriven->deactivate(); 
    14051411 
    14061412        /* 
     
    14431449        mGeometryBased->set(); 
    14441450 
    1445         mImageBased->set_visible(); 
    1446         mImageBased->activate(); 
     1451        mViewPointDriven->set_visible(); 
     1452        mViewPointDriven->activate(); 
    14471453 
    14481454        /* 
     
    14861492        mGeometryBased->deactivate(); 
    14871493 
    1488         mImageBased->hide(); 
    1489         mImageBased->deactivate(); 
     1494        mViewPointDriven->hide(); 
     1495        mViewPointDriven->deactivate(); 
    14901496 
    14911497        /* 
     
    16621668                        char *cadena=new char[12]; 
    16631669 
    1664                         if (geoMeshView->getLeavesSubmesh() >= 0) 
     1670                        if (geoMeshView->getLeavesSubMesh() >= 0) 
    16651671                        { 
    1666                                 if (geoMeshView->getLeavesSubmesh() == i) 
     1672                                if (geoMeshView->getLeavesSubMesh() == i) 
    16671673                                { 
    16681674                                        strcpy(type,"(Leaves)"); 
     
    16931699                        } 
    16941700 
    1695                         char *cadena2=new char[30]; 
    1696                         sprintf(cadena2, "Number of indexes: %d",mGeoMesh->mSubMesh[i].mIndexCount); 
    1697                         fltk::Item *numberIndex = new fltk::Item(cadena2); 
     1701                        //      Adds material name. 
     1702                        char *cadena2=new char[100]; 
     1703                        sprintf(cadena2, "Material: %s",mGeoMesh->mSubMesh[i].mMaterialName); 
     1704                         
     1705                        fltk::Item      *geomaterial    =       new fltk::Item(cadena2); 
     1706                         
     1707                        char *cadena3=new char[100]; 
     1708                         
     1709                        sprintf(cadena3, "Number of indexes: %d",mGeoMesh->mSubMesh[i].mIndexCount); 
     1710                        fltk::Item *numberIndex = new fltk::Item(cadena3); 
    16981711                        if (mGeoMesh->mSubMesh[i].mType==Geometry::GEO_TRIANGLE_LIST) 
    16991712                        { 
     
    18841897        } 
    18851898         
    1886         geoMeshView->setColorSubmeshi(meshNumber); 
     1899        geoMeshView->setSubMeshSelected(meshNumber); 
    18871900        geoMeshView->redraw(); 
    18881901         
     
    18971910        Real    percent; 
    18981911 
    1899         simplificacionState     =       QSLIM; 
    1900  
    1901         mMeshSimplifier = new GeometryBasedSimplifier(mGeoMesh, progress_function); 
     1912        //----------------- 
     1913        geoMeshView->saveContext(); 
     1914 
     1915        //      Gets simplify option. 
     1916        if (mGeometryBased->value()) 
     1917        { 
     1918                simplificationState     =       QSLIM; 
     1919                mMeshSimplifier                 = new GeometryBasedSimplifier(mGeoMesh, 
     1920                                                                                                                                                                                                                        progress_function); 
     1921        } 
     1922        else 
     1923        { 
     1924                simplificationState     =       VIEWPOINTDRIVEN; 
     1925                mMeshSimplifier                 = new ViewPointDrivenSimplifier(mGeoMesh, 
     1926                                                                                                                                                                                                                                progress_function); 
     1927        } 
    19021928 
    19031929        mMeshSimplifier->setMeshLeaves(idMeshLeaves); 
    19041930 
    1905         if (mPercent->value() == true) 
     1931        if (mPercent->value()) 
    19061932        { 
    19071933                // Simplificación por porcentaje 
     
    19101936                        percent = mMeshReduction->fvalue(); 
    19111937                        percent =       percent / 100.0; 
     1938                         
    19121939 
    19131940                        // Simplifica el geomesh -> Parámetro es un factor LOD [0,1]. 
     
    19281955                        //      Visualize mesh. 
    19291956                        geoMeshView->setMesh(mGeoMesh); 
     1957 
    19301958                } 
    19311959                else 
     
    19701998        //      Initialize      error flag. 
    19711999        error   =       false; 
    1972          
     2000 
    19732001        //      Check submeshes for triangles strips. 
    19742002        for (int i = 0; i < mGeoMesh->mSubMeshCount; i++) 
     
    19802008                } 
    19812009        } 
    1982                                  
     2010 
    19832011        //      if the mesh is stripified. 
    19842012        if (error) 
     
    19882016        //      if the mesh is in triangle list. 
    19892017        else 
    1990         {                        
    1991                 simplificacionState     =       HOJAS; 
     2018        { 
     2019                simplificationState     =       HOJAS; 
    19922020                 
    19932021                mTreeSimplifier =       new TreeSimplifier(mGeoMesh, progress_function); 
     
    23082336                geoMeshView->setMesh(mGeoMesh); 
    23092337 
    2310                 //      Get the mesh bounds. 
    2311                 mMeshBounds     =       geoMeshView->getBoundingBox(); 
    2312  
    23132338                //      Hide right panel. 
    23142339                hideRightPanel(); 
     
    24002425                        { 
    24012426                                fltk::Group* o; 
    2402                                 o = MainView = new fltk::Group(0, 0, 600, 500); 
     2427                                o = MainView = new fltk::Group(0, 0, 500, 530); 
    24032428                                o->begin(); 
    24042429 
    24052430                                { 
    24062431                                        fltk::InvisibleBox* o; 
    2407                                         o = cframe = new fltk::InvisibleBox(0, 0, 600, 500); 
     2432                                        o = cframe = new fltk::InvisibleBox(0, 0, 500, 530); 
    24082433                                        o->box(fltk::FLAT_BOX); 
    24092434                                        o->color((fltk::Color)0xffffff00); 
     
    24132438                                { 
    24142439                                        GeoMeshView* o; 
    2415                                         o = geoMeshView = new GeoMeshView(      0,0,600,500,0,this); 
     2440                                        o = geoMeshView = new GeoMeshView(      0,0,500,530,0,this); 
    24162441                                } 
    24172442 
     
    24242449                        { 
    24252450                                fltk::Group* o; 
    2426                                 o = MainView = new fltk::Group(600, 0, 200, 500); 
     2451                                o = MainView = new fltk::Group(500, 0, 300, 530); 
    24272452                                o->begin(); 
    24282453 
     
    25092534                                        { 
    25102535                                                mGeometryBased = new fltk::RadioButton(0, 8, 25, 25, "Geometry based"); 
    2511                                                 mImageBased = new fltk::RadioButton(0, 23, 25, 25, "Image based"); 
     2536                                                mViewPointDriven = new fltk::RadioButton(0, 23, 25, 25, "Viewpoint driven"); 
    25122537                                        } 
    25132538                                        o->end(); 
     
    25172542 
    25182543                                { 
    2519                                         mProcessTitle           = new fltk::Widget(0, 0, 200, 25, ""); 
     2544                                        mProcessTitle           = new fltk::Widget(0, 0, 300, 25, ""); 
    25202545                                        fltk::Widget* o =       mProcessTitle; 
    25212546                                        o->set_vertical(); 
     
    25822607 
    25832608                                { 
    2584                                         fltk::Browser* o = mMeshInfo = new fltk::Browser(0, 20, 200, 380); 
     2609                                        fltk::Browser* o = mMeshInfo = new fltk::Browser(0, 20, 300, 380); 
    25852610                                        o->set_vertical(); 
    25862611                                        o->callback((fltk::Callback*)cb_mMeshInfo); 
     
    26182643                                } 
    26192644 
    2620                                 //{ 
    2621                                 //      fltk::Item* o = menuMeshExportOBJ = new fltk::Item("Export to OBJ"); 
    2622                                 //      o->callback((fltk::Callback*)cb_menuMeshExportOBJ); 
    2623                                 //} 
     2645                                { 
     2646                                        fltk::Item* o = menuMeshExportOBJ = new fltk::Item("Export to OBJ"); 
     2647                                        o->callback((fltk::Callback*)cb_menuMeshExportOBJ); 
     2648                                } 
    26242649 
    26252650                                //{ 
  • GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoTool.cpp

    r774 r980  
     1#include "GL/glew.h" 
     2#ifdef _WIN32 
     3#include "GL/wglew.h" 
     4#endif 
     5 
    16#include "GeoMeshViewUI.h" 
    27 
Note: See TracChangeset for help on using the changeset viewer.