home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / MEC2SUF.LZH / MEC2SUF.CPP < prev    next >
C/C++ Source or Header  |  1996-06-18  |  7KB  |  278 lines

  1. #include <owl\owlpch.h>
  2. #pragma hdrstop
  3.  
  4. #include "files.h"
  5. #include "matrix.h"
  6. #include "suflib.h"
  7. #include "token.h"
  8. #include "parts.h"
  9. #include "mec2suf.h"
  10.  
  11. extern TApplication *app;
  12.  
  13. static char str[512];
  14. static char *convatr[256];
  15. static    Vector pos[256];
  16. static    Vector vec[256];
  17. static     int u[256];
  18. static    int v[256];
  19.  
  20. extern "C" extendargs(int *argcp, char **argvp[], int extn, char *ext[]);
  21.  
  22. static void PolyConv(FILE *fp, Matrix& m, Matrix& mt, TokenReader *tokenread, int wflag)
  23. {
  24.     char    token[32];
  25.     Vector p;
  26.     int vflag, uflag;
  27.     int i, points;
  28.  
  29.     tokenread->GetToken( token ) ;
  30.  
  31.     points = 0;
  32.     vflag = uflag = FALSE;
  33.     if ( !strcmpi( token, "poly" ) ) {
  34.     } else if ( !strcmpi( token, "shade" ) ) {
  35.         vflag = TRUE;
  36.     } else if ( !strcmpi( token, "uvpoly" ) ) {
  37.         uflag = TRUE;
  38.     } else if ( !strcmpi( token, "uvshade" ) ) {
  39.         uflag = vflag = TRUE;
  40.     } else {
  41.         return ;
  42.     }
  43.     if (wflag) {
  44.         fprintf(fp, "prim %s (", token);
  45.     }
  46.     if ( tokenread->GetToken( token ), token[0] != '(' ) {
  47.         return;
  48.     }
  49.     while ( tokenread->GetToken( token ), token[0] != ')' )
  50.     {
  51.         app->PumpWaitingMessages();
  52.         pos[points].x = atof( token ) ;
  53.  
  54.         if (tokenread->GetToken( token ), token[0] == ')') return;
  55.         pos[points].y = atof( token ) ;
  56.  
  57.         if (tokenread->GetToken( token ), token[0] == ')') return;
  58.         pos[points].z = atof( token ) ;
  59.  
  60.         if (vflag) {
  61.             if (tokenread->GetToken( token ), token[0] == ')') return;
  62.             vec[points].x = atof(token);
  63.  
  64.             if (tokenread->GetToken( token ), token[0] == ')') return;
  65.             vec[points].y = atof(token);
  66.  
  67.             if (tokenread->GetToken( token ), token[0] == ')') return;
  68.             vec[points].z = atof(token);
  69.         }
  70.         if (uflag) {
  71.             if (tokenread->GetToken( token ), token[0] == ')') return;
  72.              u[points] = atoi(token);
  73.  
  74.             if (tokenread->GetToken( token ), token[0] == ')') return;
  75.              v[points] = atoi(token);
  76.         }
  77.         points++;
  78.     }
  79.     if (!wflag) {
  80.         return;
  81.     }
  82.     if (m.value() > 0) {
  83.         for (i = 0; i < points; i++) {
  84.             p = m * pos[i];
  85.             fprintf(fp, "\n\t%5.0lf %5.0lf %5.0lf", p.x, p.y, p.z);
  86.             if (vflag) {
  87.                 p = mt * vec[i];
  88.                 fprintf(fp, " %5.0lf %5.0lf %5.0lf", p.x, p.y, p.z);
  89.             }
  90.             if (uflag) {
  91.                 fprintf(fp, " %4d %4d", u[i], v[i]);
  92.             }
  93.         }
  94.     } else {
  95.         for (i = points-1; i >= 0; --i) {
  96.             p = m * pos[i];
  97.             fprintf(fp, "\n\t%5.0lf %5.0lf %5.0lf", p.x, p.y, p.z);
  98.             if (vflag) {
  99.                 p = mt * vec[i];
  100.                 fprintf(fp, " %5.0lf %5.0lf %5.0lf", p.x, p.y, p.z);
  101.             }
  102.             if (uflag) {
  103.                 fprintf(fp, " %4d %4d", u[i], v[i]);
  104.             }
  105.         }
  106.     }
  107.  
  108.     fprintf(fp, ")\n");
  109. }
  110.  
  111. static void PartsConv(FILE *fp, char *objname, char *filename, Matrix& m, char *col)
  112. {
  113.     char    token[32];
  114.     int wflag;
  115.     Matrix mt= m.inv().tra();
  116.  
  117.     TokenReader t(filename);
  118.     TokenReader *tokenread = &t;
  119.  
  120.     if (!tokenread->Suceed()) {
  121.         return;
  122.     }
  123.     while (tokenread->GetToken( token ), strcmpi( token, "obj" ) == 0) {
  124.         if ( tokenread->GetToken( token ) ,strcmpi( token, "suf" ) ) {
  125.             break;
  126.         }
  127.         tokenread->GetToken(token);
  128.         if (objname == NULL || objname[0] == '\0' || strcmpi(token, objname) == 0) {
  129.             wflag = TRUE;
  130.         } else {
  131.             wflag = FALSE;
  132.         }
  133.         if ( tokenread->GetToken( token ), token[0] != '{' ) {
  134.             break;
  135.         }
  136.         while ( tokenread->GetToken( token ), token[0] != '}' )
  137.         {
  138.             if ( !strcmpi( token, "atr" ) ) {
  139.                 tokenread->GetToken( token ) ;
  140.                 if (col == NULL || col[0] == '\0') {
  141.                     fprintf(fp, "atr %s\n", token);
  142.                 } else {
  143.                     int i;
  144.                     for (i = 0; convatr[i] != NULL; i++) {
  145.                         if (strcmpi(convatr[i], token) == 0) {
  146.                             break;
  147.                         }
  148.                     }
  149.                     if (convatr[i] == NULL) {
  150.                         fprintf(fp, "atr %s\n", token);
  151.                     } else {
  152.                         fprintf(fp, "atr %s_%s\n", token, col);
  153.                     }
  154.                 }
  155.             } else if ( !strcmpi( token, "prim" ) ) {
  156.                 PolyConv(fp, m, mt, tokenread, wflag) ;
  157.             }
  158.         }
  159.     }
  160. }
  161.  
  162. #define MAXOBJS 256
  163. #define MAXSTRING 4096
  164. #define STRING 256
  165.  
  166. void Mec2Suf(TDC& dc, int argc, char *argv[])
  167. {
  168.     char oname[STRING];
  169.     char mess[STRING];
  170.     char argbuf[MAXSTRING];
  171.     char *filename[MAXOBJS];
  172.     char *colorname[MAXOBJS];
  173.     char *objname[STRING];
  174.     char *p, *parg;
  175.     char *output = NULL;
  176.     char *ext[] = {"FSC"};
  177.  
  178.     int filenames, i, j;
  179.     filenames = 0;
  180.     FILE *fp;
  181.  
  182.     p = str;
  183.     i = 0;
  184.     strcpy(p, colorchangeatr.c_str());
  185.     convatr[i++] = p;
  186.     while ((p = strchr(p, ',')) != NULL) {
  187.         *p = '\0';
  188.         convatr[i++] = p+1;
  189.         p = p+1;
  190.     }
  191.     convatr[i] = NULL;
  192.  
  193.     extendargs(&argc, &argv, 1, ext);
  194.  
  195.     parg = argbuf;
  196.     for (i = 1; i < argc; i++) {
  197.         if (argv[i][0] == '-') {
  198.             if (tolower(argv[i][1]) == 'o') {
  199.                 output = argv[i]+2;
  200.             }
  201.         } else {
  202.             filename[filenames] = parg;
  203.             strcpy(filename[filenames], argv[i]);
  204.             parg += strlen(parg) + 1;
  205.             if ((p = strchr(filename[filenames], ',')) == NULL) {
  206.                 colorname[filenames] = NULL;
  207.             } else {
  208.                 *p = '\0';
  209.                 colorname[filenames] = p+1;
  210.                 if ((p = strchr(colorname[filenames], ',')) == NULL) {
  211.                     objname[filenames] = NULL;
  212.                 } else {
  213.                     *p = '\0';
  214.                     objname[filenames] = p+1;
  215.                 }
  216.             }
  217.             filenames++;
  218.         }
  219.     }
  220.     if (output == NULL) {
  221.         return;
  222.     }
  223.     fp = fopen(output, "w");
  224.     if (fp == NULL) {
  225.         return;
  226.     }
  227.     for (i = 0; i < filenames; i++) {
  228.         Parts *parts, *next;
  229.         CameraParts *camera;
  230.         if (objname[i] == NULL || objname[i][0] == '\0') {
  231.             char *cp;
  232.             if ((cp = strrchr(filename[i], '\\')) == NULL) {
  233.                 cp = filename[i];
  234.             } else {
  235.                 cp = cp+1;
  236.             }
  237.             strcpy(oname, cp);
  238.             if ((cp = strrchr(oname, '.')) != NULL) {
  239.                 *cp = '\0';
  240.             }
  241.         } else {
  242.             strcpy(oname, objname[i]);
  243.         }
  244.         for (j = 0; oname[j]; j++) {
  245.             oname[j] = toupper(oname[j]);
  246.         }
  247.         fprintf(fp, "obj suf %s {\n", oname);
  248.         sprintf(mess, "Read Mecha: %s", oname);
  249.         dc.TextOut(0, 0, mess);
  250. //fclose(fp);return;
  251.  
  252.         if (Parts::FileRead(parts, camera, filename[i], NULL) != FALSE) {
  253.             for (Parts *p = parts; p != NULL; p = next) {
  254.                 if (p->object != NULL) {
  255.                     sprintf(mess, "Read Parts: %s", p->object->name);
  256.                     dc.TextOut(0, 16, mess);
  257.                     fprintf(fp, "/* %s */\n", p->object->name);
  258.  
  259.                     char fname[256];
  260.                     char *f = p->object->filename;
  261.                     if (strncmpi(f, simplepartsdir.c_str(), simplepartsdir.length()) == 0) {
  262.                         sprintf(fname, "%s%s", partsdir.c_str(), f+simplepartsdir.length());
  263.                     } else {
  264.                         strcpy(fname, f);
  265.                     }
  266.                     PartsConv(fp, p->object->name, fname, p->GetMatrix(), colorname[i]);
  267.                 }
  268.                 next = p->next;
  269.                 delete p;
  270.             }
  271.             delete camera;
  272.         }
  273.         fprintf(fp, "}\n");
  274.     }
  275.     fclose(fp);
  276. }
  277.  
  278.