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

Revision 826, 20.4 KB checked in by gumbau, 18 years ago (diff)

Modification Stripify for mesh discrimination.
Adds submesh operation type.

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,
377                                                                                                                                                                        Mesh    *geoMesh,
378                                                                                                                                                                        int     option)
379{
380        unsigned        short   opType;
381        fread(&opType,sizeof(unsigned short),1,f);
382        printf("\tLa render operation es %d\n",opType);
383
384        if (option == GEOMESH_BUILD)
385        {
386                if (opType == 5)
387                {
388                        geoMesh->mSubMesh[currentSubMesh].mType =       GEO_TRIANGLE_STRIPS;
389                }
390                else
391                {
392                        geoMesh->mSubMesh[currentSubMesh].mType =       GEO_TRIANGLE_LIST;
393                }
394        }
395}
396
397//      Read a submesh.
398void GeoMeshLoader::readSubMesh(FILE    *f, Mesh        *geoMesh, int option)
399{
400        SubMesh                                 *geoSubMesh;
401        unsigned        short   chunkID;
402        char                                            materialName[255];
403        bool                                            useSharedVertices;
404        unsigned        int             numIndices;
405        bool                                            idx32bit;
406        FILE                                            *salida;
407
408        switch  (option)
409        {
410                case    SUBMESH_COUNT:
411                        geoMesh->mSubMeshCount++;
412                        break;
413                case    GEOMESH_BUILD:
414
415                        //      Debug.
416                        cout    <<      "Build GEOMESH" <<      endl;
417
418                        //      Initialize the new subMesh.
419                        geoSubMesh      =       &geoMesh->mSubMesh[currentSubMesh];
420
421                        cout    <<      "Current SubMesh: "
422                                                <<      currentSubMesh
423                                                <<      endl;
424        }
425       
426        //La vacio antes de leer el mesh
427        list.clear();
428
429        fgets(materialName,255,f);
430        printf("\tmaterialName: %s",materialName);
431
432        fread(&useSharedVertices,sizeof(bool),1,f);
433       
434        if (option      ==      GEOMESH_BUILD)
435        {
436                //      Obtain the flag of vertex shared.
437                geoSubMesh->mSharedVertexBuffer =       useSharedVertices;
438               
439                if (useSharedVertices)
440                {
441                        //      The VB of the submesh points to the VB of the mesh.
442                        geoSubMesh->mVertexBuffer       =       geoMesh->mVertexBuffer;
443                       
444                        cout    <<      "\tUse Shared Vertices"
445                                                <<      endl;
446
447                }
448        }
449
450        fread(&numIndices,sizeof(unsigned int),1,f);
451        printf("\tnumIndices: %u\n",numIndices);
452        printf("\tnumTriangles: %u\n",numIndices/3);
453        fread(&idx32bit,sizeof(bool),1,f);
454       
455        if (option == GEOMESH_BUILD)
456        {
457                        //      Sets the index count of the submesh.
458                        geoSubMesh->mIndexCount =       numIndices;
459       
460                        //      Create the index arrar for this submesh.
461                        geoSubMesh->mIndex      =       new Index[numIndices];
462        }
463       
464        if (idx32bit)
465        {
466                printf("\tSon 32 bits por indice\n");
467        }
468        else
469        {
470                printf("\tSon 16 bits por indice\n");
471        }
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       
490        //Sumo uno a los vertices porque en el obj no puede hacer un vertice 0
491        if (idx32bit)
492        {
493                unsigned int aux;
494
495                for (int i = 0; i < numIndices; i++)
496                {
497                        fread(&aux,sizeof(unsigned int),1,f);
498
499                        if (option == GEOMESH_BUILD)
500                        {
501                                        geoSubMesh->mIndex[i]   =       aux;
502                        }
503                }
504        }
505        else
506        {
507                unsigned short aux;
508
509                for (int i = 0; i < numIndices; i++)
510                {
511                        fread(&aux,sizeof(unsigned short),1,f);
512                       
513                        if (option == GEOMESH_BUILD)
514                        {
515                                geoSubMesh->mIndex[i]   =       aux;
516                        }
517                }
518        }
519
520        fclose(salida);
521
522  // M_GEOMETRY chunk
523        // (Optional: present only if useSharedVertices = false)
524  if (!useSharedVertices)
525        {
526                chunkID = readChunk(f);
527               
528                if (chunkID != M_GEOMETRY)
529                {
530                        printf("Missing geometry data in mesh file\n"); //Error
531                }
532               
533                readGeometry(f, geoMesh, option);
534        }
535
536        // Find all bone assignments (if present)
537        if (!feof(f))
538        {
539                chunkID = readChunk(f);
540                while(!feof(f) &&
541                                        (chunkID == M_SUBMESH_BONE_ASSIGNMENT ||
542                                        chunkID == M_SUBMESH_OPERATION))
543                {
544                        switch(chunkID)
545                        {
546                                case M_SUBMESH_OPERATION:
547                                                               
548                                        cout<<"M_SUBMESH_OPERATION\n";
549
550                                        readSubMeshOperation(f, geoMesh, option);
551                                       
552                                        break;
553                                case M_SUBMESH_BONE_ASSIGNMENT:
554                                        jumpChunk(f);
555                                        break;
556                        }
557
558                        if (!feof(f))
559                        {
560                                chunkID =       readChunk(f);
561                        }
562
563                }
564                if (!feof(f))
565                {
566                        // Backpedal back to start of chunk
567                        fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1);
568                }
569        }
570
571}
572
573//      Read a mesh lod information.
574void GeoMeshLoader::readMeshLodInfo(FILE        *f, Mesh        *geoMesh)
575{
576        unsigned        short   chunkID;
577        unsigned        short   i;
578        unsigned        short   aux;
579        bool                                            aux2;
580
581  // unsigned short numLevels;
582        fread(&aux,sizeof(unsigned short),1,f);
583        // bool manual;
584        // (true for manual alternate meshes, false for generated)
585        fread(&aux2,sizeof(bool),1,f);
586
587        // Loop from 1 rather than 0 (full detail index is not in file)
588        for (i = 1; i < aux; ++i)
589        {
590                chunkID = readChunk(f);
591                if (chunkID != M_MESH_LOD_USAGE)
592                {
593                        printf("Missing M_MESH_LOD_USAGE chunk\n"); //Error
594                }
595                else printf("\tM_MESH_LOD_USAGE (%d bytes) ",long_actual);
596                jumpChunk(f);
597
598        }
599}
600
601//      Read a submesh name table.
602void GeoMeshLoader::readSubMeshNameTable(FILE   *f, Mesh        *geoMesh)
603{
604                unsigned        short   chunkID;
605                unsigned        short   subMeshIndex;
606                char                                            string[255];
607
608                if (!feof(f))
609                {
610                        chunkID =       readChunk(f);
611                       
612                        while   (!feof(f) && (chunkID == M_SUBMESH_NAME_TABLE_ELEMENT ))
613                        {
614                                printf("M_SUBMESH_NAME_TABLE_ELEMENT %d bytes\n",long_actual);
615                               
616                                // Read in the index of the submesh.
617                                fread(&subMeshIndex,sizeof(unsigned short),1,f);
618                               
619                                // Read in the String and map it to its index.
620                                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                                if (!feof(f))
625                                {
626                                        chunkID = readChunk(f);
627                                }
628                        }
629                        if (!feof(f))
630                        {
631                                // Backpedal back to start of chunk
632                                fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1);
633                        }
634                }
635}
636
637//      Read a mesh file.
638void GeoMeshLoader::readMesh(FILE       *f, Mesh        *geoMesh, int option)
639{
640        unsigned        short   chunkID;
641        bool                                            nada;
642
643        //      Debug.
644        cout    <<      "Read Mesh"
645                                <<      endl;
646       
647        //      Create the VertexBuffer of the mesh.
648        geoMesh->mVertexBuffer  =       new     VertexBuffer();
649        geoMesh->mVertexBuffer->mVertexCount    =       0;
650
651        //La vacio antes de leer el mesh
652        list.clear();
653
654        // bool skeletallyAnimated
655        fread(&nada,sizeof(bool),1,f);
656
657  // Find all subchunks
658        if (!feof(f))
659        {
660                chunkID =       readChunk(f);
661
662                while(!feof(f) &&
663                                (chunkID == M_GEOMETRY ||
664                                 chunkID == M_SUBMESH ||
665                                 chunkID == M_MESH_SKELETON_LINK ||
666                                 chunkID == M_MESH_BONE_ASSIGNMENT ||
667                                 chunkID == M_MESH_LOD ||
668                                 chunkID == M_MESH_BOUNDS ||
669                                 chunkID == M_SUBMESH_NAME_TABLE ||
670                                 chunkID == M_EDGE_LISTS))
671                {
672                        switch(chunkID)
673                        {
674                                case M_GEOMETRY:
675
676                                        //      Debug.
677                                        cout    <<      "Geometry"      <<      endl;
678
679                                        readGeometry(f, geoMesh, option);
680                                        break;
681
682                                case M_SUBMESH:
683
684                                        cout    <<      "SubMesh"       <<      endl;
685
686                                        if (option == GEOMESH_BUILD)
687                                        {
688                                                //      Increments the current submesh.
689                                                currentSubMesh++;
690                                        }
691
692                                        readSubMesh(f, geoMesh, option);
693
694                                        break;
695
696                                case M_MESH_SKELETON_LINK:
697
698                                        cout    <<      "M_MESH_SKELETON_LINK  "
699                                                <<      long_actual
700                                                <<      " bytes\n";
701
702                                        jumpChunk(f);
703                                        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;
745                        }
746
747                        if (!feof(f))
748                        {
749                                chunkID =       readChunk(f);
750                        }
751
752                }
753
754                if (!feof(f))
755                {
756                        // Backpedal back to start of chunk
757                        fseek(f,-(long)CHUNK_OVERHEAD_SIZE,1);
758                        printf("Malo\n");
759                }
760        }
761
762}
763
764/*
765                Allocate memory to the array of strips
766*/
767SubMesh* GeoMeshLoader::BuildStripsGeoSubMesh(SubMesh* geoSubMesh)
768{
769        bool            head_found;
770        bool            tail_found;
771        int                     strip_count;
772        int                     tailIndex;
773        size_t  tail;
774
775
776        head_found                                                      =       false;
777        tail_found                                                      =       false;
778        geoSubMesh->mStripCount =       1;
779
780        //      Foreachone of the indices.
781        //      Obtains the number of triangle strips.
782        for (int i = 0; i < (geoSubMesh->mIndexCount - 1); i++)
783        {
784                if (geoSubMesh->mIndex[i] == geoSubMesh->mIndex[i+1])
785                {
786                        if (head_found)
787                        {
788                                tail_found      =       true;
789                        }
790                        else
791                        {
792                                head_found      =       true;
793                        }
794
795                        //      Jump one index.
796                        i++;
797                }
798                else
799                {
800                        //      Increments the number of triangle strips.
801                        if (tail_found)
802                        {
803                                geoSubMesh->mStripCount++;
804                        }
805
806                        head_found      =       false;
807                        tail_found      =       false;
808                }
809        }
810
811        geoSubMesh->mStrip              =       (Index**) malloc(       sizeof(Index*)
812                        *
813                        geoSubMesh->mStripCount);
814
815        //      Number of strips. Initially there is one triangle strip.
816        strip_count     =       1;
817
818        head_found                                              =       false;
819        tail_found                                              =       false;
820
821        //      Initialize the fist triangle strip.
822        geoSubMesh->mStrip[0]   =       &geoSubMesh->mIndex[0];
823
824        //      Foreachone of the indices.
825        //      Assigns the beginning of the triangle strips.
826        for(int i = 0;i < (geoSubMesh->mIndexCount - 1); i++)
827        {
828                if(geoSubMesh->mIndex[i] == geoSubMesh->mIndex[i+1])
829                {
830                        if(head_found)
831                        {
832                                if (!tail_found)
833                                {
834                                        tailIndex               =       i;
835                                }
836
837                                tail_found      =       true;
838                        }
839                        else
840                        {
841                                head_found      =       true;
842                        }
843
844                        //      Jump one index.
845                        i++;
846                }
847                else
848                {
849                        //      Increments the number of triangle strips.
850                        if(tail_found)
851                        {
852                                geoSubMesh->mStrip[strip_count++]       =       &geoSubMesh->mIndex[tailIndex];
853                        }
854
855                        head_found      =       false;
856                        tail_found      =       false;
857                }
858        }
859
860        return  geoSubMesh;
861}
862
863//      Loads a mesh.
864Mesh* GeoMeshLoader::load(char  *nameFileMesh)
865{
866        unsigned        short   uno;
867  unsigned      short   chunkID;
868        char                                            version[255];
869        char                                            kk;
870        FILE                                            *salida;
871        FILE                                            *pFile;
872        SubMesh                                 *geosubmesh;
873
874        //      Initialize the current submesh;
875        currentSubMesh  =       -1;
876       
877        std::cout       <<      "Load a new Mesh"
878                                                <<      std::endl;
879
880        //      Initialize the return value.
881        geoMesh =       new     Mesh();
882
883        //      Open the mesh file.
884        pFile   =       fopen( nameFileMesh, "rb" );
885
886  if (!pFile)
887  {
888                printf("No he encontrado el fichero\n");
889                exit(0);
890  }
891
892        //      Count the submeshes
893        //      and next build the geomesh.
894        for (int option = 0; option < 2;option++)
895        {
896                fread(&uno,sizeof(unsigned short),1,pFile);
897
898                if (uno != M_HEADER)
899                {
900                        cout    <<      "no se ha encontrado el header"
901                                                <<      endl;
902                }
903
904                // Read version.
905                fgets(version,255,pFile);
906
907                cout    <<      version <<      endl;
908
909                if (strcmp(version,"[MeshSerializer_v1.30]\n"))
910                {
911                        cout    <<
912                                                "No se puede trabajar con versiones de"
913                                                <<
914                                                " .mesh diferentes de la 1.3"
915                                                <<
916                                                endl;
917                }
918
919                salida  =       fopen(nom_salida,"w");
920
921                fprintf(salida,"# Empiezo el fichero:");
922
923                fclose(salida);
924
925                while(!feof(pFile))
926                {
927                        chunkID = readChunk(pFile);
928
929                        switch (chunkID)
930                        {
931                                case M_MESH:
932                                        readMesh(pFile, geoMesh, option);
933                                        break;
934                        }
935                }
936
937                //      Create the submesh array
938                if (option == SUBMESH_COUNT)
939                {               
940                        cout    <<      "---------------"
941                                                <<      endl;
942
943                        cout    <<      "SubMesh Count: "
944                                                <<      geoMesh->mSubMeshCount
945                                                <<      endl;
946
947                        geoMesh->mSubMesh       = new SubMesh[geoMesh->mSubMeshCount];
948                }
949                                                       
950                if (feof(pFile))
951                {
952                        cout    <<      "------->Hemos llegado al final del fichero"
953                                                <<      endl;
954                }
955
956                //cout  <<      "Press enter to exit\n";
957
958                //      Move the curso to the begining of the file.
959                fseek(pFile,0,SEEK_SET);
960        }
961
962        //      Goes to the end of the file.
963        fseek(pFile,0,SEEK_END);
964
965        //      Gets the size of the file.
966        mFileSize       =       ftell(pFile);
967       
968        // Close the mesh file.
969        fclose(pFile);
970       
971        for (int submesh = 0;   submesh < geoMesh->mSubMeshCount; submesh++)
972        {
973                //      Gets the actual submesh.
974                geosubmesh      =       &geoMesh->mSubMesh[submesh];
975               
976                if (geosubmesh->mType == Geometry::GEO_TRIANGLE_STRIPS)
977                {
978                        //      Fill the strips list.
979                        geosubmesh      =       BuildStripsGeoSubMesh(geosubmesh);
980                }
981        }
982       
983        return  geoMesh;
984}
985
986//      Get the size in bytes of the file.
987size_t  GeoMeshLoader::getFileSize()
988{
989        return  mFileSize;
990}
991
992//      Constructor
993GeoMeshLoader::GeoMeshLoader()
994{
995        nom_salida      =       "salida22.obj";
996        geoMesh                 =       NULL;
997        mFileSize               =       0;
998}
999
1000//      Destroyer.
1001GeoMeshLoader::~GeoMeshLoader()
1002{
1003        delete  geoMesh;
1004}
1005
Note: See TracBrowser for help on using the repository browser.