source: GTP/trunk/Lib/Geom/shared/GeoTool/src/GeoMeshLoader.cpp @ 774

Revision 774, 20.4 KB checked in by gumbau, 19 years ago (diff)

GTGeometry and GeoTool? initial imports

Line 
1/*      ==========================================================================
2 *      (C) 2005 Universitat Jaume I
3 *      ==========================================================================
4 *      PROYECT:        GAME TOOLS
5 *      ==========================================================================*/
6/**     CONTENT:       
7        *
8        *
9        *       @file   GeoMeshLoader.cpp
10/*===========================================================================*/
11#include        "GeoMeshLoader.h"
12
13using namespace Geometry;
14using   namespace       std;
15
16//      Jump a chunk.
17void GeoMeshLoader::jumpChunk(FILE      *f)
18{
19        fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1);
20
21        //      Return to the begin of the chunk.
22        fseek(f,long_actual,SEEK_CUR);
23}
24
25//      Read a chunk.
26unsigned short GeoMeshLoader::readChunk(FILE    *f)
27{
28        unsigned        short   id;
29
30        fread(&id,sizeof(unsigned short),1,f);
31        fread(&long_actual,sizeof(unsigned long),1,f);
32                                                                       
33        return id;
34}
35
36//      Read geometry vertex element.
37void GeoMeshLoader::readGeometryVertexElement(FILE      *f, Mesh        *geoMesh)
38{
39        unsigned        short           source;
40        GeometryElement         aux;
41        int                                                             i;
42        bool                                                    encontrado;
43        VertexElementType       vType;
44
45        i                                               =       0;
46        encontrado      =       false;
47
48        //              VertexElementSemantic vSemantic;
49        // unsigned short source;       // buffer bind source
50        fread(&source,sizeof(unsigned short),1,f);
51
52        // unsigned short type;         // VertexElementType
53        fread(&aux.type,sizeof(unsigned short),1,f);
54
55        vType   =       static_cast<VertexElementType>(aux.type);
56
57        if (vType == VET_FLOAT3)
58        {
59                printf("%u es float3 ",aux.type);
60        }
61
62        if (vType == VET_FLOAT4)
63        {
64                printf("%u es float4 ",aux.type);
65        }
66
67        if (vType == VET_FLOAT2)
68        {
69                printf("%u es float2 ",aux.type);
70        }
71
72        // unsigned short semantic; // VertexElementSemantic
73        fread(&aux.semantic,sizeof(unsigned short),1,f);
74
75        //      vSemantic = static_cast<VertexElementSemantic>(tmp);
76        //      unsigned short offset;  // start offset in buffer in bytes
77        fread(&aux.offset,sizeof(unsigned short),1,f);
78
79        // unsigned short index;        // index of the semantic
80        /*
81        VES_POSITION  Position, 3 reals per vertex. 
82        VES_BLEND_WEIGHTS  Blending weights. 
83        VES_BLEND_INDICES  Blending indices. 
84        VES_NORMAL  Normal, 3 reals per vertex. 
85        VES_DIFFUSE  Diffuse colours. 
86        VES_SPECULAR  Specular colours. 
87        VES_TEXTURE_COORDINATES  Texture coordinates. 
88        VES_BINORMAL  Binormal (Y axis if normal is Z). 
89        VES_TANGENT  Tangent (X axis if normal is Z). 
90        */
91        fread(&aux.index,sizeof(unsigned short),1,f);
92
93        while ((i < list.size()) && (!encontrado))
94        {
95                if (list[i].source==source)
96                {
97                        encontrado      =       true;
98                }
99                else
100                {
101                        i++;
102                }
103        }
104
105        if (encontrado)
106        {
107                list[i].list.push_back(aux);
108        }
109        else
110        {
111                GT      aux2;
112               
113                aux2.source     =       source;
114                aux2.list.push_back(aux);
115                list.push_back(aux2);
116        }
117       
118        //      Debug.
119        cout    <<      "Finish Vertex Element" <<      endl;
120}
121
122//      Read geometry vertex declaration.
123void GeoMeshLoader::readGeometryVertexDeclaration(FILE  *f, Mesh        *geoMesh)
124{
125        unsigned short chunkID;
126  // Find optional geometry chunks
127  if (!feof(f))
128  {
129    chunkID     =       readChunk(f);
130
131    while(!feof(f) && (chunkID == M_GEOMETRY_VERTEX_ELEMENT ))
132    {
133      switch (chunkID)
134      {
135                                case M_GEOMETRY_VERTEX_ELEMENT:
136                                        cout    <<      "M_GEOMETRY_VERTEX_ELEMENT"     <<      endl;
137          readGeometryVertexElement(f, geoMesh);
138          break;
139      }
140
141      // Get next chunk
142      if (!feof(f))
143      {
144                                chunkID = readChunk(f);
145      }
146    }// End while.
147
148    if (!feof(f))
149    {
150                        // Backpedal back to start of non-submesh chunk
151                        fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1);
152    }
153
154  }//   End if (!feof(f))
155}
156
157//      Read geometry vertex buffer.
158void GeoMeshLoader::readGeometryVertexBuffer(FILE       *f, Mesh        *geoMesh, int option)
159{
160        unsigned        short   bindIndex;
161        unsigned        short   vertexSize;
162        unsigned        short   headerID;
163        int                                                     i;
164        float                                           aux;
165        int                                                     k;
166        bool                                            encontrado;
167        float                                           x,y,z;
168        SubMesh                                 *geoSubMesh;
169        VertexBuffer            *vertex_buffer;
170
171        i                                               =       0;
172        k                                               =       0;
173        encontrado      =       false;
174
175       
176        if (option == GEOMESH_BUILD)
177        {
178                //      If is the main mesh.
179                if (currentSubMesh == -1)
180                {
181                        vertex_buffer   =       geoMesh->mVertexBuffer;
182                }
183                //      If is a submesh.
184                else
185                {
186                        //      Gets the current submesh
187                        geoSubMesh              =       &geoMesh->mSubMesh[currentSubMesh];
188                        vertex_buffer   =       geoSubMesh->mVertexBuffer;
189                }
190        }
191       
192        // Index to bind this buffer to
193        fread(&bindIndex,sizeof(unsigned short),1,f);
194
195        // Per-vertex size, must agree with declaration at this index
196        fread(&vertexSize,sizeof(unsigned short),1,f);
197
198        //      Debug.
199        cout    <<      "Per-vertex size: "
200                                <<      vertexSize
201                                <<      endl;
202       
203        // Check for vertex data header
204        headerID        =       readChunk(f);
205
206        if (headerID != M_GEOMETRY_VERTEX_BUFFER_DATA)
207        {
208                cout    <<      "Can't find vertex buffer data area"
209                                        <<      endl;
210        }
211        else
212        {
213                cout    <<      "M_GEOMETRY_VERTEX_BUFFER_DATA" <<      endl;
214        }
215
216        while ((i<list.size())&&(!encontrado))
217        {
218                if (list[i].source      ==      bindIndex)
219                {
220                        encontrado      =       true;
221                }
222                else
223                {
224                        i++;
225                }
226        }
227
228        //      Considero q los van por grupitos de vertices+normales+...
229        //      para cada uno de los vertices
230        if (encontrado)
231        {
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)
244                                {
245                                        fread(&x,sizeof(float),1,f);
246                                        fread(&y,sizeof(float),1,f);
247                                        fread(&z,sizeof(float),1,f);
248
249                                        if (option == GEOMESH_BUILD)
250                                        {
251                                                vertex_buffer->mVertexInfo              |=      VERTEX_POSITION;
252                                                vertex_buffer->mPosition[k].x   =               x;
253                                                vertex_buffer->mPosition[k].y   =               y;
254                                                vertex_buffer->mPosition[k].z   =               z;
255                                        }
256                                }
257                                //      si no son vertices no lo quiero
258                                else
259                                {
260                                        //Se tendría que hacer con un fseek
261                                        if (list[i].list[j].type==2) //Son 3 floats
262                                        {
263                                                fread(&x,sizeof(float),1,f);
264                                                fread(&y,sizeof(float),1,f);
265                                                fread(&z,sizeof(float),1,f);
266
267                                                if (option == GEOMESH_BUILD)
268                                                {
269                                                        vertex_buffer->mVertexInfo      |=      VERTEX_NORMAL;
270                                                        vertex_buffer->mNormal[k].x     =               x;
271                                                        vertex_buffer->mNormal[k].y     =               y;
272                                                        vertex_buffer->mNormal[k].z     =               z;
273                                                }
274                                        }
275                                        if (list[i].list[j].type==1) //Son 2 floats
276                                        {
277                                                fread(&x,sizeof(float),1,f);
278                                                fread(&y,sizeof(float),1,f);
279                                               
280                                                if (option == GEOMESH_BUILD)
281                                                {
282                                                        vertex_buffer->mVertexInfo                      |=      VERTEX_TEXCOORDS;
283                                                        vertex_buffer->mTexCoords[k].x  =               x;
284                                                        vertex_buffer->mTexCoords[k].y  =               y;
285                                                }
286                                        }
287                                }
288                        }
289                }
290                fclose(salida);
291        }
292        else
293        {
294                cout    <<      "Error"
295                                        <<      endl;
296        }
297
298}
299
300//      Read geometry.
301void GeoMeshLoader::readGeometry(FILE   *f, Mesh        *geoMesh, int option)
302{
303        SubMesh                         *geoSubMesh;
304        VertexBuffer    *vertex_buffer;
305       
306        fread(&numVertices,sizeof(unsigned int),1,f);
307
308        if (option == GEOMESH_BUILD)
309        {
310                if (currentSubMesh == -1)
311                {
312                        //      Get the current vertex buffer.
313                        vertex_buffer   =       geoMesh->mVertexBuffer;
314                }
315                else
316                {
317                        //      Get the current submesh.
318                        geoSubMesh              =       &geoMesh->mSubMesh[currentSubMesh];
319
320                        //      Create the VertexBuffer of the submesh.
321                        geoSubMesh->mVertexBuffer       =       new VertexBuffer();
322
323                        //      Get the current vertex buffer.
324                        vertex_buffer   =       geoSubMesh->mVertexBuffer;
325                }
326
327                //      Initialize the VertexBuffer of the current submesh.
328                vertex_buffer->mVertexCount     =       numVertices;
329
330                //      Initialize the position array.
331                vertex_buffer->mPosition        =       new Vector3[numVertices];
332
333                //      Initialize the normal array.
334                vertex_buffer->mNormal          =       new Vector3[numVertices];
335
336                //      Initialize the texture coords array.
337                vertex_buffer->mTexCoords       =       new Vector2[numVertices];
338        }
339
340        // Find optional geometry chunks
341        if (!feof(f))
342        {
343                unsigned short chunkID = readChunk(f);
344       
345                while(!feof(f) &&
346                                        (chunkID == M_GEOMETRY_VERTEX_DECLARATION ||
347                                        chunkID == M_GEOMETRY_VERTEX_BUFFER ))
348                {
349                        switch (chunkID)
350                        {
351                                case M_GEOMETRY_VERTEX_DECLARATION:
352                                        cout<<"M_GEOMETRY_VERTEX_DECLARATION\n";
353                                        readGeometryVertexDeclaration(f, geoMesh);
354                                        break;
355                                case M_GEOMETRY_VERTEX_BUFFER:
356                                        cout<<"M_GEOMETRY_VERTEX_BUFFER\n";
357                                        readGeometryVertexBuffer(f, geoMesh,option);
358                                        break;
359                        }
360               
361                        // Get next chunk.
362                        if (!feof(f))
363                        {
364                                chunkID = readChunk(f);
365                        }
366                }
367                if (!feof(f))
368                {
369                        // Backpedal back to start of non-submesh chunk.
370                        fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1);
371                }
372        }
373}
374
375//      Read a submesh operation.
376void GeoMeshLoader::readSubMeshOperation(FILE   *f, Mesh        *geoMesh, int option)
377{
378        unsigned        short   opType;
379        fread(&opType,sizeof(unsigned short),1,f);
380        printf("\tLa render operation es %d\n",opType);
381
382        if (option == GEOMESH_BUILD)
383        {
384                if (opType == 5)
385                {
386                        geoMesh->mType  =       GEO_TRIANGLE_STRIPS;
387                }
388                else
389                {
390                        geoMesh->mType  =       GEO_TRIANGLE_LIST;
391                }
392        }
393}
394
395//      Read a submesh.
396void GeoMeshLoader::readSubMesh(FILE    *f, Mesh        *geoMesh, int option)
397{
398        SubMesh                                 *geoSubMesh;
399        unsigned        short   chunkID;
400        char                                            materialName[255];
401        bool                                            useSharedVertices;
402        unsigned        int             numIndices;
403        bool                                            idx32bit;
404        FILE                                            *salida;
405
406        switch  (option)
407        {
408                case    SUBMESH_COUNT:
409                        geoMesh->mSubMeshCount++;
410                        break;
411                case    GEOMESH_BUILD:
412
413                        //      Debug.
414                        cout    <<      "Build GEOMESH" <<      endl;
415
416                        //      Initialize the new subMesh.
417                        geoSubMesh      =       &geoMesh->mSubMesh[currentSubMesh];
418
419                        cout    <<      "Current SubMesh: "
420                                                <<      currentSubMesh
421                                                <<      endl;
422        }
423       
424        //La vacio antes de leer el mesh
425        list.clear();
426
427        fgets(materialName,255,f);
428        printf("\tmaterialName: %s",materialName);
429
430        fread(&useSharedVertices,sizeof(bool),1,f);
431       
432        if (option      ==      GEOMESH_BUILD)
433        {
434                //      Obtain the flag of vertex shared.
435                geoSubMesh->mSharedVertexBuffer =       useSharedVertices;
436               
437                if (useSharedVertices)
438                {
439                        //      The VB of the submesh points to the VB of the mesh.
440                        geoSubMesh->mVertexBuffer       =       geoMesh->mVertexBuffer;
441                       
442                        cout    <<      "\tUse Shared Vertices"
443                                                <<      endl;
444
445                }
446        }
447
448        fread(&numIndices,sizeof(unsigned int),1,f);
449        printf("\tnumIndices: %u\n",numIndices);
450        printf("\tnumTriangles: %u\n",numIndices/3);
451        fread(&idx32bit,sizeof(bool),1,f);
452       
453        if (option == GEOMESH_BUILD)
454        {
455                        //      Sets the index count of the submesh.
456                        geoSubMesh->mIndexCount =       numIndices;
457       
458                        //      Create the index arrar for this submesh.
459                        geoSubMesh->mIndex      =       new Index[numIndices];
460        }
461       
462        if (idx32bit)
463        {
464                printf("\tSon 32 bits por indice\n");
465        }
466        else
467        {
468                printf("\tSon 16 bits por indice\n");
469        }
470
471        salida  =       fopen(nom_salida,"a");
472
473        fprintf(salida,"\n#----->Leo un subMesh:");
474       
475        if (useSharedVertices)
476        {
477                fprintf(salida," shared vertices");
478        }
479        else
480        {
481                fprintf(salida,
482                                                " no shared vertices %d triangles",
483                                                numIndices/3);
484        }
485
486        fprintf(salida,"\n# Indexes:");
487       
488        //Sumo uno a los vertices porque en el obj no puede hacer un vertice 0
489        if (idx32bit)
490        {
491                unsigned int aux;
492
493                for (int i = 0; i < numIndices; i++)
494                {
495                        fread(&aux,sizeof(unsigned int),1,f);
496
497                        if (option == GEOMESH_BUILD)
498                        {
499                                        geoSubMesh->mIndex[i]   =       aux;
500                        }
501                }
502        }
503        else
504        {
505                unsigned short aux;
506
507                for (int i = 0; i < numIndices; i++)
508                {
509                        fread(&aux,sizeof(unsigned short),1,f);
510                       
511                        if (option == GEOMESH_BUILD)
512                        {
513                                geoSubMesh->mIndex[i]   =       aux;
514                        }
515                }
516        }
517
518        fclose(salida);
519
520  // M_GEOMETRY chunk
521        // (Optional: present only if useSharedVertices = false)
522  if (!useSharedVertices)
523        {
524                chunkID = readChunk(f);
525               
526                if (chunkID != M_GEOMETRY)
527                {
528                        printf("Missing geometry data in mesh file\n"); //Error
529                }
530               
531                readGeometry(f, geoMesh, option);
532        }
533
534        // Find all bone assignments (if present)
535        if (!feof(f))
536        {
537                chunkID = readChunk(f);
538                while(!feof(f) &&
539                                        (chunkID == M_SUBMESH_BONE_ASSIGNMENT ||
540                                        chunkID == M_SUBMESH_OPERATION))
541                {
542                        switch(chunkID)
543                        {
544                                case M_SUBMESH_OPERATION:
545                                                               
546                                        cout<<"M_SUBMESH_OPERATION\n";
547                                        readSubMeshOperation(f, geoMesh, option);
548                                        break;
549                                case M_SUBMESH_BONE_ASSIGNMENT:
550                                        jumpChunk(f);
551                                        break;
552                        }
553
554                        if (!feof(f))
555                        {
556                                chunkID =       readChunk(f);
557                        }
558
559                }
560                if (!feof(f))
561                {
562                        // Backpedal back to start of chunk
563                        fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1);
564                }
565        }
566
567}
568
569//      Read a mesh lod information.
570void GeoMeshLoader::readMeshLodInfo(FILE        *f, Mesh        *geoMesh)
571{
572        unsigned        short   chunkID;
573        unsigned        short   i;
574        unsigned        short   aux;
575        bool                                            aux2;
576
577  // unsigned short numLevels;
578        fread(&aux,sizeof(unsigned short),1,f);
579        // bool manual;
580        // (true for manual alternate meshes, false for generated)
581        fread(&aux2,sizeof(bool),1,f);
582
583        // Loop from 1 rather than 0 (full detail index is not in file)
584        for (i = 1; i < aux; ++i)
585        {
586                chunkID = readChunk(f);
587                if (chunkID != M_MESH_LOD_USAGE)
588                {
589                        printf("Missing M_MESH_LOD_USAGE chunk\n"); //Error
590                }
591                else printf("\tM_MESH_LOD_USAGE (%d bytes) ",long_actual);
592                jumpChunk(f);
593
594        }
595}
596
597//      Read a submesh name table.
598void GeoMeshLoader::readSubMeshNameTable(FILE   *f, Mesh        *geoMesh)
599{
600                unsigned        short   chunkID;
601                unsigned        short   subMeshIndex;
602                char                                            string[255];
603
604                if (!feof(f))
605                {
606                        chunkID =       readChunk(f);
607                       
608                        while   (!feof(f) && (chunkID == M_SUBMESH_NAME_TABLE_ELEMENT ))
609                        {
610                                printf("M_SUBMESH_NAME_TABLE_ELEMENT %d bytes\n",long_actual);
611                               
612                                // Read in the index of the submesh.
613                                fread(&subMeshIndex,sizeof(unsigned short),1,f);
614                               
615                                // Read in the String and map it to its index.
616                                fgets(string,255,f);
617                                printf("index %u name %s\n",subMeshIndex,string);
618
619                                // If we're not end of file get the next chunk ID
620                                if (!feof(f))
621                                {
622                                        chunkID = readChunk(f);
623                                }
624                        }
625                        if (!feof(f))
626                        {
627                                // Backpedal back to start of chunk
628                                fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1);
629                        }
630                }
631}
632
633//      Read a mesh file.
634void GeoMeshLoader::readMesh(FILE       *f, Mesh        *geoMesh, int option)
635{
636        unsigned        short   chunkID;
637        bool                                            nada;
638
639        //      Debug.
640        cout    <<      "Read Mesh"
641                                <<      endl;
642       
643        //      Create the VertexBuffer of the mesh.
644        geoMesh->mVertexBuffer  =       new     VertexBuffer();
645        geoMesh->mVertexBuffer->mVertexCount    =       0;
646
647        //La vacio antes de leer el mesh
648        list.clear();
649
650        // bool skeletallyAnimated
651        fread(&nada,sizeof(bool),1,f);
652
653  // Find all subchunks
654        if (!feof(f))
655        {
656                chunkID =       readChunk(f);
657
658                while(!feof(f) &&
659                                (chunkID == M_GEOMETRY ||
660                                 chunkID == M_SUBMESH ||
661                                 chunkID == M_MESH_SKELETON_LINK ||
662                                 chunkID == M_MESH_BONE_ASSIGNMENT ||
663                                 chunkID == M_MESH_LOD ||
664                                 chunkID == M_MESH_BOUNDS ||
665                                 chunkID == M_SUBMESH_NAME_TABLE ||
666                                 chunkID == M_EDGE_LISTS))
667                {
668                        switch(chunkID)
669                        {
670                                case M_GEOMETRY:
671
672                                        //      Debug.
673                                        cout    <<      "Geometry"      <<      endl;
674
675                                        readGeometry(f, geoMesh, option);
676                                        break;
677
678                                case M_SUBMESH:
679
680                                        cout    <<      "SubMesh"       <<      endl;
681
682                                        if (option == GEOMESH_BUILD)
683                                        {
684                                                //      Increments the current submesh.
685                                                currentSubMesh++;
686                                        }
687
688                                        readSubMesh(f, geoMesh, option);
689
690                                        break;
691
692                                case M_MESH_SKELETON_LINK:
693
694                                        cout    <<      "M_MESH_SKELETON_LINK  "
695                                                <<      long_actual
696                                                <<      " bytes\n";
697
698                                        jumpChunk(f);
699                                        break;
700
701                                case M_MESH_BONE_ASSIGNMENT:
702
703                                        cout    <<      "M_MESH_BONE_ASSIGNMENT  "
704                                                <<      long_actual
705                                                <<      " bytes\n";
706
707                                        break;
708
709                                case M_MESH_LOD:
710
711                                        cout    <<      "M_MESH_LOD\n";
712                                        readMeshLodInfo(f, geoMesh);
713                                        break;
714
715                                case M_MESH_BOUNDS:
716
717                                        cout    <<      "M_MESH_BOUNDS "
718                                                <<      long_actual
719                                                <<      " bytes\n";
720
721                                        jumpChunk(f);
722                                        break;
723
724                                case M_SUBMESH_NAME_TABLE:
725
726                                        cout    <<      "M_SUBMESH_NAME_TABLE "
727                                                <<      long_actual
728                                                <<      " bytes\n";
729
730                                        readSubMeshNameTable(f, geoMesh);
731                                        break;
732
733                                case M_EDGE_LISTS:
734
735                                        cout    <<      "M_EDGE_LISTS "
736                                                <<      long_actual
737                                                <<      " bytes\n";
738
739                                        jumpChunk(f);
740                                        break;
741                        }
742
743                        if (!feof(f))
744                        {
745                                chunkID =       readChunk(f);
746                        }
747
748                }
749
750                if (!feof(f))
751                {
752                        // Backpedal back to start of chunk
753                        fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1);
754                        printf("Malo\n");
755                }
756        }
757
758}
759
760/*
761                Allocate memory to the array of strips
762*/
763Mesh* GeoMeshLoader::BuildStripsGeoSubMesh(Mesh* geoMesh)
764{
765        bool            head_found;
766        bool            tail_found;
767        int                     strip_count;
768        int                     tailIndex;
769        size_t  tail;
770        SubMesh *geoSubMesh;
771
772        //      For each submesh.
773        for (int submesh        =       0; submesh < geoMesh->mSubMeshCount; submesh++)
774        {
775                //      Gets the actual submesh.
776                geoSubMesh      =       &geoMesh->mSubMesh[submesh];
777               
778                head_found                                                      =       false;
779                tail_found                                                      =       false;
780                geoSubMesh->mStripCount =       1;
781       
782                //      Foreachone of the indices.
783                //      Obtains the number of triangle strips.
784                for (int i = 0; i < (geoSubMesh->mIndexCount - 1); i++)
785                {
786                        if (geoSubMesh->mIndex[i] == geoSubMesh->mIndex[i+1])
787                        {
788                                if (head_found)
789                                  {
790                                        tail_found      =       true;
791                                }
792                                else
793                                {
794                                        head_found      =       true;
795                                }
796
797                                //      Jump one index.
798                                i++;
799                        }
800                        else
801                        {
802                                //      Increments the number of triangle strips.
803                                if (tail_found)
804                                {
805                                        geoSubMesh->mStripCount++;
806                                }
807
808                                head_found      =       false;
809                                tail_found      =       false;
810                        }
811                }
812
813                geoSubMesh->mStrip              =       (Index**) malloc(       sizeof(Index*)
814                                                                                                                                                                                        *
815                                                                                                                                                                                        geoSubMesh->mStripCount);
816
817                //      Number of strips. Initially there is one triangle strip.
818                strip_count     =       1;
819
820                head_found                                              =       false;
821                tail_found                                              =       false;
822
823                //      Initialize the fist triangle strip.
824                geoSubMesh->mStrip[0]   =       &geoSubMesh->mIndex[0];
825
826                //      Foreachone of the indices.
827                //      Assigns the beginning of the triangle strips.
828                for(int i = 0;i < (geoSubMesh->mIndexCount - 1); i++)
829                {
830                        if(geoSubMesh->mIndex[i] == geoSubMesh->mIndex[i+1])
831                        {
832                                if(head_found)
833                                {
834                                        if (!tail_found)
835                                        {
836                                                tailIndex               =       i;
837                                        }
838
839                                        tail_found      =       true;
840                                }
841                                else
842                                {
843                                        head_found      =       true;
844                                }
845
846                                //      Jump one index.
847                                i++;
848                        }
849                        else
850                        {
851                                //      Increments the number of triangle strips.
852                                if(tail_found)
853                                {
854                                        geoSubMesh->mStrip[strip_count++]       =       &geoSubMesh->mIndex[tailIndex];
855                                }
856
857                                head_found      =       false;
858                                tail_found      =       false;
859                        }
860                }
861        }
862
863        return  geoMesh;
864}
865
866//      Loads a mesh.
867Mesh* GeoMeshLoader::load(char  *nameFileMesh)
868{
869        unsigned        short   uno;
870  unsigned      short   chunkID;
871        char                                            version[255];
872        char                                            kk;
873        FILE                                            *salida;
874        FILE                                            *pFile;
875
876        //      Initialize the current submesh;
877        currentSubMesh  =       -1;
878       
879        std::cout       <<      "Load a new Mesh"
880                                                <<      std::endl;
881
882        //      Initialize the return value.
883        geoMesh =       new     Mesh();
884
885        //      Open the mesh file.
886        pFile   =       fopen( nameFileMesh, "rb" );
887
888  if (!pFile)
889  {
890                printf("No he encontrado el fichero\n");
891                exit(0);
892  }
893
894        //      Count the submeshes
895        //      and next build the geomesh.
896        for (int option = 0; option < 2;option++)
897        {
898                fread(&uno,sizeof(unsigned short),1,pFile);
899
900                if (uno != M_HEADER)
901                {
902                        cout    <<      "no se ha encontrado el header"
903                                                <<      endl;
904                }
905
906                // Read version.
907                fgets(version,255,pFile);
908
909                cout    <<      version <<      endl;
910
911                if (strcmp(version,"[MeshSerializer_v1.30]\n"))
912                {
913                        cout    <<
914                                                "No se puede trabajar con versiones de"
915                                                <<
916                                                " .mesh diferentes de la 1.3"
917                                                <<
918                                                endl;
919                }
920
921                salida  =       fopen(nom_salida,"w");
922
923                fprintf(salida,"# Empiezo el fichero:");
924
925                fclose(salida);
926
927                while(!feof(pFile))
928                {
929                        chunkID = readChunk(pFile);
930
931                        switch (chunkID)
932                        {
933                                case M_MESH:
934                                        readMesh(pFile, geoMesh, option);
935                                        break;
936                        }
937                }
938
939                //      Create the submesh array
940                if (option == SUBMESH_COUNT)
941                {               
942                        cout    <<      "---------------"
943                                                <<      endl;
944
945                        cout    <<      "SubMesh Count: "
946                                                <<      geoMesh->mSubMeshCount
947                                                <<      endl;
948
949                        geoMesh->mSubMesh       = new SubMesh[geoMesh->mSubMeshCount];
950                }
951                                                       
952                if (feof(pFile))
953                {
954                        cout    <<      "------->Hemos llegado al final del fichero"
955                                                <<      endl;
956                }
957
958                //cout  <<      "Press enter to exit\n";
959
960                //      Move the curso to the begining of the file.
961                fseek(pFile,0,SEEK_SET);
962        }
963
964        //      Goes to the end of the file.
965        fseek(pFile,0,SEEK_END);
966
967        //      Gets the size of the file.
968        mFileSize       =       ftell(pFile);
969       
970        // Close the mesh file.
971        fclose(pFile);
972       
973        if (geoMesh->mType == Geometry::GEO_TRIANGLE_STRIPS)
974        {
975                //      Fill the strips list.
976                geoMesh =       BuildStripsGeoSubMesh(geoMesh);
977        }
978       
979        return  geoMesh;
980}
981
982//      Get the size in bytes of the file.
983size_t  GeoMeshLoader::getFileSize()
984{
985        return  mFileSize;
986}
987
988//      Constructor
989GeoMeshLoader::GeoMeshLoader()
990{
991        nom_salida      =       "salida22.obj";
992        geoMesh                 =       NULL;
993        mFileSize               =       0;
994}
995
996//      Destroyer.
997GeoMeshLoader::~GeoMeshLoader()
998{
999        delete  geoMesh;
1000}
1001
Note: See TracBrowser for help on using the repository browser.