home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / MODEL / FRAME.C < prev    next >
C/C++ Source or Header  |  1996-07-31  |  8KB  |  377 lines

  1. /*
  2.  *        フレームファイル入力
  3.  *
  4.  *        Copyright    T.Kobayashi        1994.7.17
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <math.h>
  10. #include <io.h>
  11. #include <assert.h>
  12.  
  13. #include "lib.h"
  14. #include "reader.h"
  15. #include "poly.h"
  16. #include "view.h"
  17. #include "vector.h"
  18.  
  19. #define MAXOBJECT 64
  20.  
  21. static char objname[MAXOBJECT][F_NAME_LEN];
  22. static Matrix objmat[MAXOBJECT];
  23.  
  24. static char *initialdir;
  25. static char searchdir[F_NAME_LEN];
  26. extern char ExecPath[];
  27.  
  28. static int SearchFile(char *path, char *file)
  29. {
  30.     char *p;
  31.     if (access(file, 0) == 0) {
  32.         strcpy(path, file);
  33.         return TRUE;
  34.     }
  35.     strcpy(path, ExecPath);
  36.     strcat(path, file);
  37.     if (access(path, 0) == 0) {
  38.         return TRUE;
  39.     }
  40.     if (strlen(file) > 2) {
  41.         path[0] = file[0];
  42.         path[1] = file[1];
  43.         path[2] = '\\';
  44.         strcpy(path+3, file);
  45.         if (access(path, 0) == 0) {
  46.             return TRUE;
  47.         }
  48.  
  49.         strcpy(path, ExecPath);
  50.         p = path + strlen(path);
  51.         p[0] = file[0];
  52.         p[1] = file[1];
  53.         p[2] = '\\';
  54.         strcpy(p+3, file);
  55.         if (access(path, 0) == 0) {
  56.             return TRUE;
  57.         }
  58.  
  59.         strcpy(path, "parts\\");
  60.         p = path + strlen(path);
  61.         p[0] = file[0];
  62.         p[1] = file[1];
  63.         p[2] = '\\';
  64.         strcpy(p+3, file);
  65.         if (access(path, 0) == 0) {
  66.             return TRUE;
  67.         }
  68.  
  69.         strcpy(path, ExecPath);
  70.         strcat(path, "parts\\");
  71.         p = path + strlen(path);
  72.         p[0] = file[0];
  73.         p[1] = file[1];
  74.         p[2] = '\\';
  75.         strcpy(p+3, file);
  76.         if (access(path, 0) == 0) {
  77.             return TRUE;
  78.         }
  79.  
  80.         strcpy(path, "..\\");
  81.         strcat(path, "parts\\");
  82.         p = path + strlen(path);
  83.         p[0] = file[0];
  84.         p[1] = file[1];
  85.         p[2] = '\\';
  86.         strcpy(p+3, file);
  87.         if (access(path, 0) == 0) {
  88.             return TRUE;
  89.         }
  90.  
  91.         strcpy(path, "..\\..\\");
  92.         strcat(path, "parts\\");
  93.         p = path + strlen(path);
  94.         p[0] = file[0];
  95.         p[1] = file[1];
  96.         p[2] = '\\';
  97.         strcpy(p+3, file);
  98.         if (access(path, 0) == 0) {
  99.             return TRUE;
  100.         }
  101.         strcpy(path, "..\\..\\..\\");
  102.         strcat(path, "parts\\");
  103.         p = path + strlen(path);
  104.         p[0] = file[0];
  105.         p[1] = file[1];
  106.         p[2] = '\\';
  107.         strcpy(p+3, file);
  108.         if (access(path, 0) == 0) {
  109.             return TRUE;
  110.         }
  111.     }
  112.     if (searchdir[0] != '\0') {
  113.         strcpy(path, searchdir);
  114.         p = path + strlen(path);
  115.         strcat(path, file);
  116.         if (access(path, 0) == 0) {
  117.             return TRUE;
  118.         }
  119.  
  120.         p[0] = file[0];
  121.         p[1] = file[1];
  122.         p[2] = '\\';
  123.         strcpy(p+3, file);
  124.         if (access(path, 0) == 0) {
  125.             return TRUE;
  126.         }
  127.  
  128.         strcpy(p, "parts\\");
  129.         p = path + strlen(path);
  130.         p[0] = file[0];
  131.         p[1] = file[1];
  132.         p[2] = '\\';
  133.         strcpy(p+3, file);
  134.         if (access(path, 0) == 0) {
  135.             return TRUE;
  136.         }
  137.     }
  138.  
  139.     strcpy(path, file);
  140.     return FALSE;
  141. }
  142.  
  143. int        FrmReadFile( char *file, char *dir, int mode )
  144. {
  145.     int        i, j, objs, atrno, objno, type ;
  146.     int rdata[8];
  147.     int    x,y,z;
  148.     int pvx=0, pvy=0, pvz=0;
  149.     int coe[3];
  150.     int reverseflag;
  151.     char    atrname[64], frmname[64], *p, *q ;
  152.     char    filename[F_NAME_LEN];
  153.  
  154.     Matrix matit;
  155.     int        imat[5][3], imatit[5][3] ;
  156.  
  157.     if (dir != NULL) {
  158.         strcpy(searchdir, dir);
  159.         i = strlen(searchdir);
  160.         if (i > 0 && searchdir[i-1] != '\\') {
  161.             searchdir[i] = '\\';
  162.             searchdir[i+1] = '\0';
  163.         }
  164.     } else {
  165.         strcpy(searchdir, file);
  166.         if ((p = strrchr(searchdir, '\\')) == NULL) {
  167.             p = searchdir;
  168.         }
  169.         if ((q = strrchr(p, '/')) == NULL) {
  170.             q = p;
  171.         }
  172.         if (*q == '\\' || *q == '/') {
  173.             p[1] = '\0';
  174.         }
  175.     }
  176.  
  177.     if (FrmReadOpen( file ) == FALSE) {
  178.         return FALSE;
  179.     }
  180.     if ((p = strrchr(file, '\\')) == NULL) {
  181.         p = file;
  182.     }
  183.     if ((q = strrchr(p, '/')) == NULL) {
  184.         q = p;
  185.     }
  186.     if (*q == '\\' || *q == '/') {
  187.         q++;
  188.     }
  189.     strcpy(frmname, q);
  190.     if ((p = strchr(frmname, '.')) != NULL) {
  191.         *p = '\0';
  192.     }
  193.     if (mode != FILE_APPEND) {
  194.         objno = ObjAppend( frmname );
  195.     } else {
  196.         objno = ObjSearchOrAppend( frmname );
  197.     }
  198.     strcpy( ObjData[objno].fname, file);
  199.     i = strlen(file);
  200.     if (i > 4 && file[i-4] == '.') {
  201.         strcpy( ObjData[objno].fname + i-4, ".suf");
  202.     }
  203.  
  204.     objs = 0;
  205.     while ( FrmReadObj(objname[objs], objmat[objs])) {
  206.         strcat(objname[objs], ".suf");
  207.         if (objs < MAXOBJECT-1) {
  208.             objs++;
  209.         }
  210.     }
  211.     FrmReadClose();
  212.  
  213.     for (j = 0; j < objs; j++) {
  214.         float delta, r;
  215.         SearchFile(filename, objname[j]);
  216.         if (ObjReadOpen(filename) == FALSE) {
  217.             continue;
  218.         }
  219.  
  220.         MatCopy(matit, objmat[j]);
  221.         MatInv(matit);
  222.         MatTra(matit);
  223.  
  224.         MatToInt( imat, objmat[j]);
  225.         MatToInt( imatit, matit );
  226.  
  227.         delta = (float)0.0 ;
  228.         for( i = 0 ; i < 3 ; i ++ )
  229.         {
  230.             delta = delta
  231.                 + objmat[j][0+i][0] * objmat[j][(1+i)%3][1] * objmat[j][(2+i)%3][2]
  232.                 - objmat[j][0+i][2] * objmat[j][(1+i)%3][1] * objmat[j][(2+i)%3][0] ;
  233.         }
  234.  
  235.         reverseflag = (delta < 0.0);
  236.  
  237.         while( ObjReadName( filename ) && !ReaderError())
  238.         {
  239.             CurrentPoly->obj = (short)objno ;
  240.  
  241.             while( ObjReadPoly( atrname, &type ) && !ReaderError())
  242.             {
  243.                 atrno = AttrSearchOrAppend( atrname );
  244.                 if (atrno < 0) return -1;
  245.  
  246.                 CurrentPoly->type = (short)type ;
  247.                 CurrentPoly->atr = (short)atrno ;
  248.                 for( i = 0 ; ObjReadVertexInt( rdata ) && !ReaderError(); i++ )
  249.                 {
  250.                     x = rdata[0] * imat[0][0]
  251.                       + rdata[1] * imat[1][0]
  252.                       + rdata[2] * imat[2][0]
  253.                       +            imat[4][0] ;
  254.                     y = rdata[0] * imat[0][1]
  255.                       + rdata[1] * imat[1][1]
  256.                       + rdata[2] * imat[2][1]
  257.                       +            imat[4][1] ;
  258.                     z = rdata[0] * imat[0][2]
  259.                       + rdata[1] * imat[1][2]
  260.                       + rdata[2] * imat[2][2]
  261.                       +            imat[4][2] ;
  262.                     if (x > 0) {
  263.                         x = imat[3][0] + ( ( x + 32767) >> 16 );
  264.                     } else {
  265.                         x = imat[3][0] - ( (-x + 32767) >> 16 );
  266.                     }
  267.                     if (y > 0) {
  268.                         y = imat[3][1] + ( ( y + 32767) >> 16 );
  269.                     } else {
  270.                         y = imat[3][1] - ( (-y + 32767) >> 16 );
  271.                     }
  272.                     if (z > 0) {
  273.                         z = imat[3][2] + ( ( z + 32767) >> 16 );
  274.                     } else {
  275.                         z = imat[3][2] - ( (-z + 32767) >> 16 );
  276.                     }
  277.  
  278.                     CurrentPoly->ver[i].x = (short)x;
  279.                     CurrentPoly->ver[i].y = (short)y;
  280.                     CurrentPoly->ver[i].z = (short)z;
  281.  
  282.                     if ( type & POLY_SHADE )
  283.                     {
  284.                         if ( i > 0 )
  285.                         {
  286.                             if ( pvx * rdata[3] + pvy * rdata[4] + pvz * rdata[5] < 0 )
  287.                             {
  288.                                 rdata[3] = -rdata[3];
  289.                                 rdata[4] = -rdata[4];
  290.                                 rdata[5] = -rdata[5];
  291.                             }
  292.                         }
  293.                         x = rdata[3] * imatit[0][0]
  294.                           + rdata[4] * imatit[1][0]
  295.                           + rdata[5] * imatit[2][0];
  296.                         y = rdata[3] * imatit[0][1]
  297.                           + rdata[4] * imatit[1][1]
  298.                           + rdata[5] * imatit[2][1];
  299.                         z = rdata[3] * imatit[0][2]
  300.                           + rdata[4] * imatit[1][2]
  301.                           + rdata[5] * imatit[2][2];
  302.  
  303.                         if ( i > 0 )
  304.                         {
  305.                             if ( pvx * (x/256) + pvy * (y/256) + pvz * (z/256) < 0 )
  306.                             {
  307.                                 x = -x;
  308.                                 y = -y;
  309.                                 z = -z;
  310.                             }
  311.                         }
  312.  
  313.                         r = sqrt( (float)x*(float)x + (float)y*(float)y + (float)z*(float)z );
  314.                         if (r > 0.0) r = 256.0 / r;
  315.  
  316.                         pvx = CurrentPoly->ver[i].vx = (short)(r * x);
  317.                         pvy = CurrentPoly->ver[i].vy = (short)(r * y);
  318.                         pvz = CurrentPoly->ver[i].vz = (short)(r * z);
  319.                     }
  320.  
  321.                     CurrentPoly->ver[i].u = (short)rdata[6] ;
  322.                     CurrentPoly->ver[i].v = (short)rdata[7] ;
  323.                 }
  324.                 CurrentPoly->vers = (short)i ;
  325.                 CurrentPoly->select = TRUE ;
  326.  
  327.                 Coefficent(coe, CurrentPoly->ver, CurrentPoly->vers);
  328.                 if ( coe[0] * CurrentPoly->ver[0].vx
  329.                    + coe[1] * CurrentPoly->ver[0].vy
  330.                    + coe[2] * CurrentPoly->ver[0].vz < 0) {
  331.                     for( i = 1 ; i < CurrentPoly->vers; i++) {
  332.                         Vertex *ver;
  333.                         ver = &(CurrentPoly->ver[i]);
  334.                         if ( coe[0] * ver->vx + coe[1] * ver->vy + coe[2] * ver->vz > 0) {
  335.                             ver->vx = -ver->vx;
  336.                             ver->vy = -ver->vy;
  337.                             ver->vz = -ver->vz;
  338.                         }
  339.                     }
  340.                 } else {
  341.                     for( i = 1 ; i < CurrentPoly->vers; i++) {
  342.                         Vertex *ver;
  343.                         ver = &(CurrentPoly->ver[i]);
  344.                         if ( coe[0] * ver->vx + coe[1] * ver->vy + coe[2] * ver->vz < 0) {
  345.                             ver->vx = -ver->vx;
  346.                             ver->vy = -ver->vy;
  347.                             ver->vz = -ver->vz;
  348.                         }
  349.                     }
  350.                 }
  351.  
  352. /*                Selects ++ ;*/
  353.                 if (reverseflag) {
  354.                     PolyInvVertex();
  355. #if 0
  356.                     for (i = 0; i < CurrentPoly->vers; i++) {
  357.                         CurrentPoly->ver[i].vx = -CurrentPoly->ver[i].vx;
  358.                         CurrentPoly->ver[i].vy = -CurrentPoly->ver[i].vy;
  359.                         CurrentPoly->ver[i].vz = -CurrentPoly->ver[i].vz;
  360.                     }
  361. #endif
  362.                 }
  363.  
  364.                 PolyAppend();
  365.             }
  366.             ObjData[objno].edit = FALSE ;
  367.         }
  368.         ObjReadClose();
  369.         if (ReaderError()) {
  370.             return -1;
  371.         }
  372.     }
  373.     return objno ;
  374. }
  375.  
  376.  
  377.