home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / t / triq.zip / PARSE.C < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  332 lines

  1. /* ---------------------------------------------------- */
  2. /*              INCLUDE FILE DEFINITIONS                */
  3. /* ---------------------------------------------------- */
  4.  
  5. /*
  6.      (C) Copyright Microsoft Corp. 1991.  All rights reserved.
  7.  
  8.      You have a royalty-free right to use, modify, reproduce and 
  9.      distribute the Sample Files (and/or any modified version) in 
  10.      any way you find useful, provided that you agree that 
  11.      Microsoft has no warranty obligations or liability for any 
  12.      Sample Application Files which are modified. 
  13.  */
  14.  
  15. #include <windows.h>
  16. #include <stdlib.h>
  17. #include "4d/4d.h"
  18. #include "view.h"
  19.  
  20.  
  21. #define isspace(c) ((c) == ' ' || (c) == ',' || (c) == '\t')
  22. #define iseol(c)   ((c) == '\n' || (c) == '\r')
  23.  
  24. #define ungetc(c,pqch) ((*(pqch))--)
  25. #define getc(pqch)     (**(pqch) ?  *(*(pqch))++ :  0)
  26. #define feof(pqch)     (**(pqch) == 0)
  27. #define strcmpi        lstrcmp
  28.  
  29. NewEdge( int p1, int p2);
  30. char    *GetTok (LPSTR *fh);
  31. DWORD PolyShade(LPPOLY pPoly, POINTFX4D color);
  32.  
  33.  
  34.  
  35. DWORD PolyShade(LPPOLY pPoly, POINTFX4D color)
  36. {
  37.     VECTORFX  n,v;
  38.     FIXED4D     nv;
  39.     int       r,g,b;
  40.  
  41.     n = Normalize(pPoly->nvec);
  42.     v = Normalize(Pdiff(light,POINTSQ[pPoly->ipt[0]]));
  43.     nv = Dot4D(n,v);
  44.  
  45.     if (nv < ZERO)
  46.     nv = ZERO;
  47.  
  48.     nv += MAKEFX(0,4000);
  49.  
  50.     r = FXINT(FXMUL(color.x,FXMUL(FX(255),nv)));
  51.     g = FXINT(FXMUL(color.y,FXMUL(FX(255),nv)));
  52.     b = FXINT(FXMUL(color.z,FXMUL(FX(255),nv)));
  53.  
  54.     if (r > 255) r = 255;
  55.     if (g > 255) g = 255;
  56.     if (b > 255) b = 255;
  57.  
  58.     return RGB(r,g,b);
  59. }
  60.  
  61.  
  62.  
  63. char    *GetTok (LPSTR *fh)
  64. {
  65.     static char    tok[81];
  66.     char    c;
  67.     int    i;
  68.  
  69. loop:
  70.     c = (char)getc(fh);
  71.  
  72.     while (isspace(c))            /* eat all spaces and commas */
  73.         c = (char)getc(fh);
  74.  
  75.     if ((c == '!') || (c == ';'))    /* If comment char eat char until EOL */
  76.     {
  77.         while (!feof(fh) && (c = (char)getc(fh)) && !iseol(c))
  78.             ;
  79.         goto loop;
  80. /* return NULL; */
  81.     }
  82.  
  83.     if (feof(fh))
  84.         return NULL;
  85.  
  86.     if (iseol(c))
  87.     {
  88.         while (iseol(c))
  89.             c = getc(fh);
  90.         ungetc(c, fh);
  91.         return NULL;
  92.     }
  93.  
  94.     i = 0;
  95.     while (!isspace(c) && !feof(fh) && !iseol(c))
  96.     {
  97.         tok[i++] = c;
  98.         c = (char)getc(fh);
  99.     }
  100.     tok[i] = 0;
  101.     AnsiUpper(tok);
  102.     ungetc(c, fh);
  103.     return tok;
  104. }
  105.  
  106.  
  107. NewEdge( int p1, int p2)
  108. {
  109.     int    i;
  110.     for (i = 0; i < macEdge; i++)
  111.     {
  112.         if ( (EDGES[i].p1 == p1 && EDGES[i].p2 == p2) || 
  113.             (EDGES[i].p1 == p2 && EDGES[i].p2 == p1) )
  114.             return i;
  115.     }
  116.  
  117.     EDGES[macEdge].p1 = p1;
  118.     EDGES[macEdge].p2 = p2;
  119.     EDGES[macEdge].f  = 0;
  120.  
  121.     if (macEdge < MAXEDGE)
  122.         return macEdge++;
  123.     else
  124.     {
  125. //        WinPrintf("%AEdge Table overflow");
  126.         return macEdge;
  127.     }
  128. }
  129.  
  130.  
  131. /*****************************************************************************
  132.  ParseFile - Reads a text file in reconizing two syntax statements:
  133.             PNT  N, x, y, z
  134.             POLY p1,p2,p3,...pn
  135.  
  136.             building a table of points and a table of polygons.
  137. *****************************************************************************/
  138.  
  139. ParseFile ( LPSTR qch)
  140. {
  141.     int    PntOffset = 0;
  142.     char    *tok;
  143.     int    FirstPNT = 1;
  144.     int    i, n, last, first, nPnt;
  145.     POINTFX4D   translate;
  146.     LPSTR    * fh = &qch;
  147.     SURFACE   surface;
  148.     PSTR      szTexture;
  149.     HBITMAP   hbmTexture;
  150.  
  151.     if (POINTSQ == NULL)
  152.     {
  153.         PNTS    = (VOID FAR * )GlobalLock(GlobalAlloc(GHND, (LONG)MAXPTS  * sizeof(POINT)));
  154.         POINTSQ  = (VOID FAR * )GlobalLock(GlobalAlloc(GHND, (LONG)MAXPTS  * sizeof(PNT) ));
  155.         CPOINTSQ = (VOID FAR * )GlobalLock(GlobalAlloc(GHND, (LONG)MAXPTS  * sizeof(PNT) ));
  156.         EDGES   = (VOID FAR * )GlobalLock(GlobalAlloc(GHND, (LONG)MAXEDGE * sizeof(EDGE)));
  157.         POLYS   = (VOID FAR * )GlobalLock(GlobalAlloc(GHND, (LONG)MAXPOLY * sizeof(POLY)));
  158.  
  159.     }
  160.  
  161.  
  162.     if (!qch)
  163.         return FALSE;
  164.  
  165.     macPoint = 0;
  166.     macPoly  = 0;
  167.     macEdge  = 0;
  168.  
  169.     translate.x = FX(0);
  170.     translate.y = FX(0);
  171.     translate.z = FX(0);
  172.  
  173.     light.x = FX(0);
  174.     light.y = FX(0);
  175.     light.z = FX(0);
  176.  
  177.     color.x = FX(0);
  178.     color.y = FX(0);
  179.     color.z = FX(0);
  180.  
  181.     rgbBackground = RGB(0, 0, 255);
  182.  
  183.     hbmTexture = NULL;
  184.  
  185.     while (!feof(fh)) 
  186.     {
  187.         tok = GetTok (fh);
  188.         if (tok == NULL) 
  189.             continue;
  190. /*
  191.      * Test for a PNT definition
  192.      */
  193.         if (strcmpi(tok, "PNT") == 0) 
  194.         {
  195.             i = atoi (GetTok(fh));
  196.             if (FirstPNT) 
  197.             {
  198.                 FirstPNT = FALSE;
  199.                 PntOffset = macPoint - 1;
  200.                 nPnt = 1;
  201.             }
  202.  
  203.             if (i == 0)
  204.                 i = nPnt;
  205.  
  206.             i += PntOffset;
  207.  
  208.             POINTSQ[i].x = translate.x + atofx(GetTok(fh));
  209.             POINTSQ[i].y = translate.y + atofx(GetTok(fh));
  210.             POINTSQ[i].z = translate.z + atofx(GetTok(fh));
  211.             POINTSQ[i].w = ONE;
  212.             if (macPoint < MAXPTS)
  213.                 macPoint++;
  214.             else
  215.         ;
  216. //                WinPrintf("%APoint Table overflow");
  217.  
  218.             nPnt++;
  219.         }
  220. /*
  221.      * Test for a POLY definition
  222.      */
  223.         else if (strcmpi(tok, "POLY") == 0) 
  224.         {
  225.             FirstPNT = TRUE;
  226.             n     = 0;
  227.             i     = -1;
  228.             tok   = GetTok(fh);
  229.             while (tok != NULL)
  230.             {
  231.                 last = i;
  232.                 i = PntOffset + atoi(tok);
  233. #if 0
  234.                 if (last >= 0)
  235.                 {
  236.                     POLYS[macPoly].ied[n++] = (BYTE)NewEdge(last, i);
  237.                 }
  238.                 else
  239.                 {
  240.                     first = i;
  241.                 }
  242. #endif        
  243.                 POLYS[macPoly].ipt[n]     = (BYTE)i;
  244.                 tok = GetTok(fh);
  245. #if 1        
  246.         n++;
  247. #endif        
  248. #if 0
  249.                 if (n == MAXPT)
  250.                     WinPrintf("%AToo many points in a Polygon");
  251. #endif        
  252.             }
  253.  
  254. //            POLYS[macPoly].ied[n++]   = NewEdge(i, first);
  255. //            POLYS[macPoly].ipt[n]     = (BYTE)i;
  256.             POLYS[macPoly].len        = n;
  257.        POLYS[macPoly].nvec       = PolyNormal(&POLYS[macPoly],POINTSQ);
  258.        POLYS[macPoly].rgb        = PolyShade(&POLYS[macPoly], color);
  259. //      POLYS[macPoly].hbmTexture = hbmTexture;
  260. ////////POLYS[macPoly].rgb        = RGB(FXINT(color.x), FXINT(color.y), FXINT(color.z));
  261.       
  262.  
  263.             if (macPoly < MAXPOLY)
  264.                 macPoly++;
  265. #if 0        
  266.             else
  267.                 WinPrintf("%APoly Table overflow");
  268. #endif
  269.         }
  270.         else if (strcmpi(tok, "TRANSLATE") == 0) 
  271.         {
  272.             translate.x = atofx(GetTok(fh));
  273.             translate.y = atofx(GetTok(fh));
  274.             translate.z = atofx(GetTok(fh));
  275.         }
  276.         else if (strcmpi(tok, "COLOR") == 0) 
  277.         {
  278.             color.x = atofx(GetTok(fh));
  279.             color.y = atofx(GetTok(fh));
  280.             color.z = atofx(GetTok(fh));
  281.         }
  282.         else if (strcmpi(tok, "LIGHT") == 0) 
  283.         {
  284.             light.x = atofx(GetTok(fh));
  285.             light.y = atofx(GetTok(fh));
  286.             light.z = atofx(GetTok(fh));
  287.         }
  288.         else if (strcmpi(tok, "SURFACE") == 0) 
  289.         {
  290.             szTexture = GetTok(fh);
  291. //            hbmTexture = bmOpenBitmap(szTexture, BM_DEFAULT);
  292.         }
  293.         else if (strcmpi(tok, "BACKGROUND") == 0) 
  294.         {
  295.             int    r, g, b;
  296.  
  297.             background.x = atofx(GetTok(fh));
  298.             background.y = atofx(GetTok(fh));
  299.             background.z = atofx(GetTok(fh));
  300.  
  301.             r = FXINT(background.x);
  302.             g = FXINT(background.y);
  303.             b = FXINT(background.z);
  304.  
  305.             if (r > 255) 
  306.                 r = 255;
  307.             if (g > 255) 
  308.                 g = 255;
  309.             if (b > 255) 
  310.                 b = 255;
  311.  
  312.             rgbBackground = RGB(r, g, b);
  313.         }
  314. /*
  315.      *  Otherwise it is a error
  316.      */
  317.         else 
  318.         {
  319. /* ignore it .... */
  320. /* return 0; */
  321.         }
  322.     }
  323.  
  324.  
  325. #if 0
  326.     WinPrintf("%MPoints= %d\nEdges= %d\nPolys=%d", macPoint, macEdge, macPoly);
  327. #endif
  328.     return 1;
  329.  
  330. }
  331.  
  332.