home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /****************************************************************************/
- /* Renderman (.rib) to raw xyz values conversion (with assigned textures) */
- /* Version 1.0 By Richard Newell. Written 07/30/92 */
- /* This program is released to the public domain. */
- /****************************************************************************/
- /* Description: */
- /* */
- /* This program reads a .rib file produced by DesignCAD 3D v4.0 and */
- /* converts the polygons into triangle data that can be read by program */
- /* RAW2POV, written by Steve Anger. It also reads the color attributes */
- /* from the .rib file and assigns a texture to each triangle. For example */
- /* the color [1.0 1.0 1.0] will be converted to texture T1o0_1o0_1o0. */
- /* Using a text editor you change the texture names to something useable. */
- /* */
- /* Of course the .rib file has to have some valid data in it to avoid the */
- /* dreaded "degenerate" triangle. I've found that by converting the */
- /* .dw3 file to the .iges format, and then to the .rib format, that I get */
- /* valid triangles. When I would convert .dw3 to .rib directly, I'd get */
- /* some nasty holes in my surfaces. */
- /* */
- /****************************************************************************/
- #include <stdio.h>
- #include <io.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <memory.h>
- #include <ctype.h>
-
- /* Constants ---------------------------------------------------------------*/
- #define NUM_CHARS 32 /* Total number of control characters */
- #define LF 0x0A /* Linefeed character */
- #define CR 0x0D /* Carriage-return character */
- #define PS 0x5B /* [ */
- #define PE 0x5D /* ] */
- #define SP 0x20 /* space */
- #define Period 0x2E /* Period */
- #define Undr 0x5F /* Underscore */
- #define LilO 0x6F /* Little o */
- #define CapC 0x43 /* Capital C */
- #define CapT 0x54 /* Capital T */
- #define PROMPT -1 /* Get translation from prompts */
- /*--------------------------------------------------------------------------*/
-
- /* Global variables. -------------------------------------------------------*/
- int mapping[NUM_CHARS];
- int global_trans = PROMPT;
- /*--------------------------------------------------------------------------*/
-
- /* Function prototypes -----------------------------------------------------*/
- void translate_file( FILE *fdIn, FILE *fdOut );
- int translate_char( int line, int c );
- /*--------------------------------------------------------------------------*/
-
- /* main - opens input and output files and translates them. ----------------*/
- /* */
- /* Params: argc - count of arguments */
- /* argv - array of argument strings */
- /* */
- /* Return: None ------------------------------------------------------------*/
-
- void main( int argc, char *argv[] )
- {
- FILE *fdInput;
- FILE *fdOutput;
- int c;
-
- /* Check command line arguments for validity and print syntax
- * prompt if invalid.
- */
- if( (argc != 2) && (argc != 3) )
- {
- fprintf( stderr, "SYNTAX: rib2xyz <infile> <outfile> \n" );
- exit( 1 );
- }
-
-
- /* Try to open the input and output files in binary mode. */
- if( (fdInput = fopen( argv[1], "rb" )) == NULL )
- {
- fprintf( stderr, "rib2xyz: fatal error: can't open '%s'\n", argv[1] );
- exit( 1 );
- }
-
- if( !access( argv[2], 0 ) )
- {
- printf( "File exists. Overwrite? " );
- c = getch();
- if( c != 'y' && c != 'Y' )
- exit( 1 );
- printf( "\n" );
- }
- if( (fdOutput = fopen( argv[2], "wb" )) == NULL )
- {
- fclose( fdInput );
- fprintf( stderr, "rib2xyz: fatal error: can't open '%s'\n", argv[2] );
- exit( 1 );
- }
-
- /* Translate the input file to the output file. */
- translate_file( fdInput, fdOutput );
-
- /* Close the files and exit. */
- fclose( fdInput );
- fclose( fdOutput );
- exit( 0 );
- }
-
-
- /* translate_file - translates Renderman Polygons to triangular xyz values--*/
- /* */
- /* Params: fdIn - input file handle, as obtained from fopen() */
- /* fdOut - output file handle, as obtained from fopen() */
- /* */
- /* Return: None */
- /*--------------------------------------------------------------------------*/
- void translate_file( FILE *fdIn, FILE *fdOut )
- {
- int c;
- int i;
- int line = 1;
- int poly,vertcnt,chrcnt,twoeq3,oneeq4,oc,endoln;
- int vertray[50][100], cntray[100];
- int tricnt,numvert,vp1,vp2,vp3;
- int texray[80], colrcnt;
-
- /* Loop until the end of the file, reading each character from
- * the input file and writing x, y, and z values to the output file.
- */
- while( ((c = getc( fdIn )) != EOF) || (!feof( fdIn )) )
- {
- /* Convert color attribute to texture */
- if (c == CapC)
- {
- c = getc(fdIn);
- if ( c == LilO)
- {
- c = getc(fdIn);
- c = getc(fdIn);
- c = getc(fdIn);
- c = getc(fdIn);
- c = getc(fdIn);
- colrcnt =0;
- texray[0]=CapT;
- colrcnt++;
- while ( (c = getc(fdIn)) != PE)
- {
- if( c == Period)
- {
- texray[colrcnt] = LilO;
- }
- else if( c == SP )
- {
- texray[colrcnt] = Undr;
- }
- else
- {
- texray[colrcnt] = c;
- }
- colrcnt++;
- }
- texray[colrcnt] = CR;
- colrcnt++;
- texray[colrcnt] = LF;
- colrcnt++;
- }
- }
-
- /* Look for polygons */
- if (c == PS)
- {
- c = getc( fdIn );
- if (c == CR)
- {
- c = getc( fdIn );
- poly = 1;
- vertcnt = 0;
- chrcnt = 0;
- twoeq3 = 1;
- oneeq4 =1;
-
- /* When a polygon is found */
- while ( poly == 1)
- {
- c = getc( fdIn );
- vertray[chrcnt][vertcnt] = c;
- chrcnt++;
- if((c != CR) && (c != LF))
- {
-
- /*See if it's a triangle in disguise */
- if ( vertcnt == 2)
- {
- if(vertray[chrcnt-1][vertcnt] != vertray[chrcnt-1][vertcnt-1])
- {
- twoeq3 = 0;
- }
- }
- if ( vertcnt == 3)
- {
- if(vertray[chrcnt-1][vertcnt] != vertray[chrcnt-1][vertcnt-3])
- {
- oneeq4 = 0;
- }
- }
- }
-
- /* Bump the vertex counter after each vertex */
- if ( c == LF )
- {
- /* Keep track of the character count for each vertex */
- cntray[vertcnt]=chrcnt-1;
- vertcnt++;
- chrcnt = 0;
- }
-
- /* When a polygon has been read */
- if ( c == PE )
- {
- poly = 0;
-
- /* Do what's needed if it's a normal polygon */
- /* Ex. A 5 sided polygon with vertices 1 2 3 4 5 */
- /* would be split into 3 triangles with vertices */
- /* 1 2 3, 1 3 4, and 1 4 5. */
- if ( (twoeq3 == 0) && (oneeq4 == 0))
- {
- numvert=vertcnt - 3;
- for (tricnt = 0; tricnt < numvert ; tricnt++)
- {
- vp1 = 0;
- vp2 = tricnt + 1;
- vp3 = tricnt + 2;
-
- /* Vertex 1 */
- endoln = cntray[vp1] - 1;
- for (chrcnt =0; chrcnt < endoln; chrcnt++)
- {
- oc=vertray[chrcnt][vp1];
- putc(oc,fdOut);
- }
- putc(SP,fdOut);
-
- /* Vertex 2 */
- endoln = cntray[vp2] - 1;
- for (chrcnt =0; chrcnt < endoln; chrcnt++)
- {
- oc=vertray[chrcnt][vp2];
- putc(oc,fdOut);
- }
- putc(SP,fdOut);
-
- /* Vertex 3 */
- endoln = cntray[vp3] - 1;
- for (chrcnt =0; chrcnt < endoln; chrcnt++)
- {
- oc=vertray[chrcnt][vp3];
- putc(oc,fdOut);
- }
- putc(SP,fdOut);
-
- /* Texture, CR and LF */
- for ( chrcnt = 0; chrcnt < colrcnt; chrcnt++)
- {
- oc=texray[chrcnt];
- putc(oc,fdOut);
- }
- }
-
- }
-
- /* If the 2nd vertex is the same as the 3rd.
- * i.e. triangle in disguise. Do what's needed */
- else if ( twoeq3 == 1)
- {
-
- /* Vertex 1 */
- endoln = cntray[0] - 1;
- for (chrcnt =0; chrcnt < endoln; chrcnt++)
- {
- oc=vertray[chrcnt][0];
- putc(oc,fdOut);
- }
- putc(SP,fdOut);
-
- /* Vertex 2 */
- endoln = cntray[1] - 1;
- for (chrcnt =0; chrcnt < endoln; chrcnt++)
- {
- oc=vertray[chrcnt][1];
- putc(oc,fdOut);
- }
- putc(SP,fdOut);
-
- /* Vertex 3 */
- endoln = cntray[3] - 1;
- for (chrcnt =0; chrcnt < endoln; chrcnt++)
- {
- oc=vertray[chrcnt][3];
- putc(oc,fdOut);
- }
-
- /* Texture, CR and LF */
- putc(SP,fdOut);
- for ( chrcnt = 0; chrcnt < colrcnt; chrcnt++)
- {
- oc=texray[chrcnt];
- putc(oc,fdOut);
- }
- }
-
- /* If the 4th vertex is the same as the 1st.
- * i.e. triangle in disguise. Do what's needed */
- else if ( oneeq4 == 1)
- {
-
- /* Vertex 1 */
- endoln = cntray[0] - 1;
- for (chrcnt =0; chrcnt < endoln; chrcnt++)
- {
- oc=vertray[chrcnt][0];
- putc(oc,fdOut);
- }
- putc(SP,fdOut);
-
- /* Vertex 2 */
- endoln = cntray[1] - 1;
- for (chrcnt =0; chrcnt < endoln; chrcnt++)
- {
- oc=vertray[chrcnt][1];
- putc(oc,fdOut);
- }
- putc(SP,fdOut);
-
- /* Vertex 3 */
- endoln = cntray[2] - 1;
- for (chrcnt =0; chrcnt < endoln; chrcnt++)
- {
- oc=vertray[chrcnt][2];
- putc(oc,fdOut);
- }
-
- /* Texture, CR and LF */
- putc(SP,fdOut);
- for ( chrcnt = 0; chrcnt < colrcnt; chrcnt++)
- {
- oc=texray[chrcnt];
- putc(oc,fdOut);
- }
- }
- vertcnt = 0;
- }
- }
- }
- }
- }
- }
-
-
-
-
-