home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / utility / dos / ribxyz / rib2xyz.c next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  10.2 KB  |  362 lines

  1. /****************************************************************************/
  2. /****************************************************************************/
  3. /* Renderman (.rib) to raw xyz values conversion (with assigned textures)   */
  4. /* Version 1.0 By Richard Newell.    Written 07/30/92                       */
  5. /* This program is released to the public domain.                           */
  6. /****************************************************************************/
  7. /* Description:                                                             */
  8. /*                                                                          */
  9. /*   This program reads a .rib file produced by DesignCAD 3D v4.0 and       */
  10. /*   converts the polygons into triangle data that can be read by program   */
  11. /*   RAW2POV, written by Steve Anger.  It also reads the color attributes   */
  12. /*   from the .rib file and assigns a texture to each triangle. For example */
  13. /*   the color [1.0 1.0 1.0] will be converted to texture T1o0_1o0_1o0.     */
  14. /*   Using a text editor you change the texture names to something useable. */
  15. /*                                                                          */
  16. /*   Of course the .rib file has to have some valid data in it to avoid the */
  17. /*   dreaded "degenerate" triangle.  I've found that by converting the      */
  18. /*   .dw3 file to the .iges format, and then to the .rib format, that I get */
  19. /*   valid triangles. When I would convert .dw3 to .rib directly, I'd get   */
  20. /*   some nasty holes in my surfaces.                                       */
  21. /*                                                                          */
  22. /****************************************************************************/
  23. #include <stdio.h>
  24. #include <io.h>
  25. #include <conio.h>
  26. #include <stdlib.h>
  27. #include <memory.h>
  28. #include <ctype.h>
  29.  
  30. /* Constants ---------------------------------------------------------------*/
  31. #define NUM_CHARS   32  /* Total number of control characters */
  32. #define LF      0x0A    /* Linefeed character                 */
  33. #define CR      0x0D    /* Carriage-return character          */
  34. #define PS      0x5B    /* [                                  */
  35. #define PE      0x5D    /* ]                                  */
  36. #define SP      0x20    /* space                              */
  37. #define Period  0x2E    /* Period                             */
  38. #define Undr    0x5F    /* Underscore                         */
  39. #define LilO    0x6F    /* Little o                           */
  40. #define CapC    0x43    /* Capital C                          */
  41. #define CapT    0x54    /* Capital T                          */
  42. #define PROMPT  -1      /* Get translation from prompts       */
  43. /*--------------------------------------------------------------------------*/
  44.  
  45. /* Global variables. -------------------------------------------------------*/
  46. int mapping[NUM_CHARS];
  47. int global_trans = PROMPT;
  48. /*--------------------------------------------------------------------------*/
  49.  
  50. /* Function prototypes -----------------------------------------------------*/
  51. void translate_file( FILE *fdIn, FILE *fdOut );
  52. int  translate_char( int line, int c );
  53. /*--------------------------------------------------------------------------*/
  54.  
  55. /* main - opens input and output files and translates them. ----------------*/
  56. /*                                                                          */
  57. /* Params: argc - count of arguments                                        */
  58. /*         argv - array of argument strings                                 */
  59. /*                                                                          */
  60. /* Return: None ------------------------------------------------------------*/
  61.  
  62. void main( int argc, char *argv[] )
  63. {
  64.     FILE *fdInput;
  65.     FILE *fdOutput;
  66.     int  c;
  67.  
  68.     /* Check command line arguments for validity and print syntax
  69.      * prompt if invalid.
  70.      */
  71.     if( (argc != 2) && (argc != 3) )
  72.     {
  73.     fprintf( stderr, "SYNTAX: rib2xyz <infile> <outfile> \n" );
  74.     exit( 1 );
  75.     }
  76.  
  77.  
  78.     /* Try to open the input and output files in binary mode. */
  79.     if( (fdInput = fopen( argv[1], "rb" )) == NULL )
  80.     {
  81.     fprintf( stderr, "rib2xyz: fatal error: can't open '%s'\n", argv[1] );
  82.     exit( 1 );
  83.     }
  84.  
  85.     if( !access( argv[2], 0 ) )
  86.     {
  87.     printf( "File exists. Overwrite? " );
  88.     c = getch();
  89.     if( c != 'y' && c != 'Y' )
  90.         exit( 1 );
  91.     printf( "\n" );
  92.     }
  93.     if( (fdOutput = fopen( argv[2], "wb" )) == NULL )
  94.     {
  95.     fclose( fdInput );
  96.     fprintf( stderr, "rib2xyz: fatal error: can't open '%s'\n", argv[2] );
  97.     exit( 1 );
  98.     }
  99.  
  100.     /* Translate the input file to the output file. */
  101.     translate_file( fdInput, fdOutput );
  102.  
  103.     /* Close the files and exit. */
  104.     fclose( fdInput );
  105.     fclose( fdOutput );
  106.     exit( 0 );
  107. }
  108.  
  109.  
  110. /* translate_file - translates Renderman Polygons to triangular xyz values--*/
  111. /*                                                                          */
  112. /* Params: fdIn - input file handle, as obtained from fopen()               */
  113. /*         fdOut - output file handle, as obtained from fopen()             */
  114. /*                                                                          */
  115. /* Return: None                                                             */
  116. /*--------------------------------------------------------------------------*/
  117. void translate_file( FILE *fdIn, FILE *fdOut )
  118. {
  119.     int     c;
  120.     int     i;
  121.     int     line = 1;
  122.     int     poly,vertcnt,chrcnt,twoeq3,oneeq4,oc,endoln;
  123.     int     vertray[50][100], cntray[100];
  124.     int     tricnt,numvert,vp1,vp2,vp3;
  125.     int     texray[80], colrcnt;
  126.  
  127.     /* Loop until the end of the file, reading each character from
  128.      * the input file and writing x, y, and z values to the output file.
  129.      */
  130.     while( ((c = getc( fdIn )) != EOF) || (!feof( fdIn )) )
  131.     {
  132.     /* Convert color attribute to texture  */
  133.     if (c == CapC)
  134.     {
  135.       c = getc(fdIn);
  136.       if ( c == LilO)
  137.       {
  138.         c = getc(fdIn);
  139.         c = getc(fdIn);
  140.         c = getc(fdIn);
  141.         c = getc(fdIn);
  142.         c = getc(fdIn);
  143.         colrcnt =0;
  144.         texray[0]=CapT;
  145.         colrcnt++;
  146.         while ( (c = getc(fdIn)) != PE)
  147.         {
  148.           if( c == Period)
  149.           {
  150.         texray[colrcnt] = LilO;
  151.           }
  152.           else if( c == SP )
  153.           {
  154.         texray[colrcnt] = Undr;
  155.           }
  156.           else
  157.           {
  158.         texray[colrcnt] = c;
  159.           }
  160.           colrcnt++;
  161.         }
  162.         texray[colrcnt] = CR;
  163.         colrcnt++;
  164.         texray[colrcnt] = LF;
  165.         colrcnt++;
  166.       }
  167.     }
  168.  
  169.     /* Look for polygons */
  170.     if (c == PS)
  171.     {
  172.      c = getc( fdIn );
  173.      if (c == CR)
  174.      {
  175.       c = getc( fdIn );
  176.       poly = 1;
  177.       vertcnt = 0;
  178.       chrcnt = 0;
  179.       twoeq3 = 1;
  180.       oneeq4 =1;
  181.  
  182.       /* When a polygon is found */
  183.       while ( poly == 1)
  184.       {
  185.         c = getc( fdIn );
  186.         vertray[chrcnt][vertcnt] = c;
  187.         chrcnt++;
  188.         if((c != CR) && (c != LF))
  189.         {
  190.  
  191.         /*See if it's a triangle in disguise */
  192.          if ( vertcnt == 2)
  193.          {
  194.            if(vertray[chrcnt-1][vertcnt] != vertray[chrcnt-1][vertcnt-1])
  195.            {
  196.           twoeq3 = 0;
  197.            }
  198.          }
  199.          if ( vertcnt == 3)
  200.          {
  201.            if(vertray[chrcnt-1][vertcnt] != vertray[chrcnt-1][vertcnt-3])
  202.            {
  203.           oneeq4 = 0;
  204.            }
  205.          }
  206.         }
  207.  
  208.         /* Bump the vertex counter after each vertex */
  209.         if ( c == LF )
  210.         {
  211.           /* Keep track of the character count for each vertex */
  212.           cntray[vertcnt]=chrcnt-1;
  213.           vertcnt++;
  214.           chrcnt = 0;
  215.         }
  216.  
  217.         /* When a polygon has been read */
  218.         if ( c == PE )
  219.         {
  220.           poly = 0;
  221.  
  222.           /* Do what's needed if it's a normal polygon       */
  223.           /*  Ex. A 5 sided polygon with vertices 1 2 3 4 5  */
  224.           /*  would be split into 3 triangles with vertices  */
  225.           /*  1 2 3,  1 3 4, and 1 4 5.                      */
  226.           if ( (twoeq3 == 0) && (oneeq4 == 0))
  227.           {
  228.         numvert=vertcnt - 3;
  229.         for (tricnt = 0; tricnt < numvert ; tricnt++)
  230.         {
  231.           vp1 = 0;
  232.           vp2 = tricnt + 1;
  233.           vp3 = tricnt + 2;
  234.  
  235.           /* Vertex 1 */
  236.           endoln = cntray[vp1] - 1;
  237.           for (chrcnt =0; chrcnt < endoln; chrcnt++)
  238.           {
  239.             oc=vertray[chrcnt][vp1];
  240.             putc(oc,fdOut);
  241.           }
  242.           putc(SP,fdOut);
  243.  
  244.           /* Vertex 2 */
  245.           endoln = cntray[vp2] - 1;
  246.           for (chrcnt =0; chrcnt < endoln; chrcnt++)
  247.           {
  248.             oc=vertray[chrcnt][vp2];
  249.             putc(oc,fdOut);
  250.           }
  251.           putc(SP,fdOut);
  252.  
  253.           /* Vertex 3 */
  254.           endoln = cntray[vp3] - 1;
  255.           for (chrcnt =0; chrcnt < endoln; chrcnt++)
  256.           {
  257.             oc=vertray[chrcnt][vp3];
  258.             putc(oc,fdOut);
  259.           }
  260.           putc(SP,fdOut);
  261.  
  262.           /* Texture, CR and LF */
  263.           for ( chrcnt = 0; chrcnt < colrcnt; chrcnt++)
  264.           {
  265.             oc=texray[chrcnt];
  266.             putc(oc,fdOut);
  267.           }
  268.         }
  269.  
  270.           }
  271.  
  272.           /* If the 2nd vertex is the same as the 3rd.
  273.            * i.e. triangle in disguise. Do what's needed */
  274.           else if ( twoeq3 == 1)
  275.           {
  276.  
  277.         /* Vertex 1 */
  278.         endoln = cntray[0] - 1;
  279.         for (chrcnt =0; chrcnt < endoln; chrcnt++)
  280.         {
  281.           oc=vertray[chrcnt][0];
  282.           putc(oc,fdOut);
  283.         }
  284.         putc(SP,fdOut);
  285.  
  286.         /* Vertex 2 */
  287.         endoln = cntray[1] - 1;
  288.         for (chrcnt =0; chrcnt < endoln; chrcnt++)
  289.         {
  290.           oc=vertray[chrcnt][1];
  291.           putc(oc,fdOut);
  292.         }
  293.         putc(SP,fdOut);
  294.  
  295.         /* Vertex 3 */
  296.         endoln = cntray[3] - 1;
  297.         for (chrcnt =0; chrcnt < endoln; chrcnt++)
  298.         {
  299.           oc=vertray[chrcnt][3];
  300.           putc(oc,fdOut);
  301.         }
  302.  
  303.         /* Texture, CR and LF */
  304.         putc(SP,fdOut);
  305.         for ( chrcnt = 0; chrcnt < colrcnt; chrcnt++)
  306.         {
  307.           oc=texray[chrcnt];
  308.           putc(oc,fdOut);
  309.         }
  310.           }
  311.  
  312.           /* If the 4th vertex is the same as the 1st.
  313.            * i.e. triangle in disguise. Do what's needed */
  314.           else if ( oneeq4 == 1)
  315.           {
  316.  
  317.         /* Vertex 1 */
  318.         endoln = cntray[0] - 1;
  319.         for (chrcnt =0; chrcnt < endoln; chrcnt++)
  320.         {
  321.           oc=vertray[chrcnt][0];
  322.           putc(oc,fdOut);
  323.         }
  324.         putc(SP,fdOut);
  325.  
  326.         /* Vertex 2 */
  327.         endoln = cntray[1] - 1;
  328.         for (chrcnt =0; chrcnt < endoln; chrcnt++)
  329.         {
  330.           oc=vertray[chrcnt][1];
  331.           putc(oc,fdOut);
  332.         }
  333.         putc(SP,fdOut);
  334.  
  335.         /* Vertex 3 */
  336.         endoln = cntray[2] - 1;
  337.         for (chrcnt =0; chrcnt < endoln; chrcnt++)
  338.         {
  339.           oc=vertray[chrcnt][2];
  340.           putc(oc,fdOut);
  341.         }
  342.  
  343.         /* Texture, CR and LF */
  344.         putc(SP,fdOut);
  345.         for ( chrcnt = 0; chrcnt < colrcnt; chrcnt++)
  346.         {
  347.           oc=texray[chrcnt];
  348.           putc(oc,fdOut);
  349.         }
  350.           }
  351.           vertcnt = 0;
  352.         }
  353.       }
  354.      }
  355.     }
  356.     }
  357. }
  358.  
  359.  
  360.  
  361.  
  362.