home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Meshwriter_lib / Library / samples / geo2any.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  6.0 KB  |  190 lines

  1. /*
  2. **      $VER: geo2any.c 1.00 (28.3.99)
  3. **
  4. **      Sample converter program which convertes VideoScape ascii (GEO)
  5. **      files with the help of the MeshWriter library.
  6. **
  7. **      Based on the idea from Stephan Bodmer which made not only Geo2Vrml,
  8. **      but the VrmlViewer and VrmlEditor too. Go and get them !!
  9. **
  10. **      Stephan Bielmann
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16.  
  17. #include <meshwriter/meshwriter.h>
  18. #include <pragma/meshwriter_lib.h>
  19. #include <clib/exec_protos.h>
  20. struct MeshWriterBase *MeshWriterBase = NULL;
  21.  
  22. FILE *fp=NULL;
  23. unsigned long *indexlist=NULL;
  24. ULONG mesh;
  25. TOCLColor colorTbl[] = {
  26. {  0,  0,  0},{  0,  0,178},{  0,178,  0},{  0,178,178},{178,  0,  0},{255,127,255},
  27. {204,153,102},{127,127,127},{  0,  0,  0},{102,102,255},{102,255,102},{  0,255,100},
  28. {255,102,102},{255,204,255},{255,255,  0},{255,255,255}
  29. };
  30. ULONG materials[16];
  31.  
  32. void finish() {
  33.         free(indexlist);
  34.         fclose(fp);
  35.         MWLMeshDelete(mesh);
  36.         CloseLibrary((APTR) MeshWriterBase);
  37.  
  38.         exit(0);
  39. }
  40.  
  41. void initmaterials() {
  42.         long i;
  43.         
  44.                 for(i=0;i<16;i++) {
  45.                                 MWLMeshMaterialAdd(mesh,&(materials[i]));
  46.                                 MWLMeshMaterialDiffuseColorSet(mesh,materials[i],&(colorTbl[i]));
  47.         }
  48. }
  49.  
  50. void main(int argc, char *argv[]) {
  51.         char header[10];
  52.         unsigned long vertices=0,polygons=0,material=0;
  53.         unsigned long linecounter=0,ret;
  54.         unsigned long points[1000];
  55.         long i;
  56.         STRPTR *fileformats=NULL;
  57.         TOCLVertex v;
  58.  
  59.         if (!(MeshWriterBase = (APTR) OpenLibrary("meshwriter.library", 0)))
  60.         {
  61.                 printf("Could not open the meshwriter.library\n");
  62.                 exit(30);
  63.         }
  64.     
  65.         printf("\nThis tool is using MeshWriter.library by Stephan Bielmann.\n\n");
  66.  
  67.         if(argc!=4) {
  68.                 printf("Usage %s inputfile outputfile formatid\n\n",argv[0]);
  69.  
  70.                 fileformats=MWL3DFileFormatNamesGet();
  71.                 printf("Valid format id's are :\n");
  72.                 i=0;
  73.                 while(fileformats!=NULL && fileformats[i]!=NULL) {
  74.                         printf("%2ld : %s\n",MWL3DFileFormatIDGet(fileformats[i]),fileformats[i]);
  75.                         i++;
  76.                 }
  77.                 printf("\n");
  78.  
  79.                 exit(0);
  80.         }
  81.  
  82.         mesh=MWLMeshNew();
  83.         if(mesh==0) {
  84.                 printf("Could not creat a new mesh.\n");
  85.                 exit(0);
  86.         }
  87.  
  88.         initmaterials();
  89.  
  90.         fp=fopen(argv[1],"r");
  91.         if(fp==NULL) {
  92.                 printf("Could not open the input file.\n");
  93.                 exit(0);
  94.         }
  95.  
  96.         fscanf(fp,"%4s",header);
  97.         if(strncmp(header,"3DG1",4)!=0) {
  98.                 printf("Inputfile is not a GEO ascii file.\n");
  99.                 finish();
  100.         }
  101.         
  102.         linecounter++;
  103.         printf("Inputfile is a GEO ascii file.\n");
  104.  
  105.         fscanf(fp,"%ld",&vertices);
  106.         if(vertices==0) {
  107.                 printf("No vertices were found in the inputfile.\n");
  108.                 finish();
  109.         }
  110.         
  111.         linecounter++;
  112.         printf("Found %ld vertices.\nProcessing the vertices.\n",vertices);
  113.  
  114.         indexlist=malloc(sizeof(unsigned long)*vertices);
  115.         if(indexlist==NULL) {
  116.                 printf("Not enough memory to allocate the indexlist.\n");
  117.                 finish();
  118.         }
  119.  
  120.         for(i=0;i<vertices;i++,linecounter++) {
  121.                 // GEO z equals to the libraries y
  122.                 if(fscanf(fp,"%f %f %f",&v.x,&v.z,&v.y)==EOF) {
  123.                         printf("Error while reading the vertices, at line %ld.\n",i+1);
  124.                         finish();
  125.                 }
  126.  
  127.         ret=MWLMeshVertexAdd(mesh,&v,&(indexlist[i]));
  128.                 if(ret!=RCNOERROR) {
  129.                         printf("Could not add a new vertex %ld.\n",ret);
  130.                         finish();
  131.                 }
  132.         }
  133.  
  134.         printf("Processing the polygons.\n");
  135.  
  136.         while(fscanf(fp,"%ld",&polygons)!=EOF) {
  137.                 MWLMeshPolygonAdd(mesh,0);
  138.                 // we limit us to 200 points per polygon, as in the library itself
  139.                 for(i=0;i<polygons&&i<200;i++) {
  140.                         if(fscanf(fp,"%ld",&(points[i]))==EOF) {
  141.                                 printf("Error while reading the polygon information, at line %ld.\n",linecounter);
  142.                                 finish();
  143.                         } else {
  144.                                 if(points[i]>=vertices) {
  145.                                         printf("A polygon its vertex index is not valid %ld at line %ld.\n",points[i],linecounter);
  146.                                         finish();
  147.                                 }
  148.                         }
  149.                 }
  150.  
  151.                 // remember, GEO is clockwise, the library counterclockwise
  152.                 for(i=polygons-1;i>=0;i--) {
  153.                         ret=MWLMeshPolygonVertexAssign(mesh,indexlist[points[i]]);
  154.                         if(ret!=RCNOERROR) {
  155.                                 printf("Could not assign a vertex to the polygo %ld.\n",ret);
  156.                                 finish();
  157.                         }
  158.                 }
  159.  
  160.                 if(fscanf(fp,"%ld",&material)==EOF) {
  161.                         printf("Error while reading the material information, at line %ld.\n",linecounter);
  162.                         finish();
  163.                 } else {
  164.                         // we are using only 16 out of 256 colors.
  165.                         MWLMeshPolygonMaterialSet(mesh,materials[material%16]);
  166.                 }
  167.  
  168.                 linecounter++;
  169.         }
  170.  
  171.         printf("Found %ld polygons.\n",linecounter-2-vertices);
  172.  
  173.         printf("Inputfile processed.\n");
  174.  
  175.         printf("Writing the outputfile.\n");
  176.         ret=MWLMeshSave3D(mesh,atoi(argv[3]),argv[2],NULL);
  177.         if(ret!=RCNOERROR) {
  178.                 printf("Could not save the outputfile %ld.\n",ret);
  179.         }
  180.  
  181.         printf("Cleaning up.\n");
  182.         MWLMeshDelete(mesh);
  183.         free(indexlist);
  184.         fclose(fp);
  185.  
  186.         printf("Finished.\n\n");
  187.  
  188.         CloseLibrary((APTR) MeshWriterBase);
  189. }  
  190.