home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / sharewar / quake106 / utils / texmake / texmake.c next >
C/C++ Source or Header  |  1996-09-12  |  3KB  |  223 lines

  1. #include "cmdlib.h"
  2. #include "mathlib.h"
  3. #include "lbmlib.h"
  4. #include "trilib.h"
  5.  
  6.  
  7. triangle_t    *faces;
  8. int        numfaces;
  9.  
  10. byte    pic[64000];
  11. byte    *palette;
  12.  
  13. int        width, height;
  14. int        iwidth, iheight;
  15.  
  16. float    scale;
  17.  
  18. char    texname[20];
  19.  
  20.  
  21. /*
  22. ================
  23. BoundFaces
  24. ================
  25. */
  26. vec3_t    mins, maxs;
  27.  
  28. void BoundFaces (void)
  29. {
  30.     int        i,j,k;
  31.     triangle_t    *pol;
  32.     float    v;
  33.  
  34.     for (i=0 ; i<numfaces ; i++)
  35.     {
  36.         pol = &faces[i];
  37.         for (j=0 ; j<3 ; j++)
  38.             for (k=0 ; k<3 ; k++)
  39.             {
  40.                 v = pol->verts[j][k];
  41.                 if (v<mins[k])
  42.                     mins[k] = v;
  43.                 if (v>maxs[k])
  44.                     maxs[k] = v;
  45.             }    
  46.     }
  47.     
  48.     for (i=0 ; i<3 ; i++)
  49.     {
  50.         mins[i] = floor(mins[i]);
  51.         maxs[i] = ceil(maxs[i]);
  52.     }
  53.     
  54.     width = maxs[0] - mins[0];
  55.     height = maxs[2] - mins[2];
  56.     
  57.     printf ("width: %i  height: %i\n",width, height);
  58.  
  59.     scale = 8;
  60.     if (width*scale >= 150)
  61.         scale = 150.0 / width;    
  62.     if (height*scale >= 190)
  63.         scale = 190.0 / height;
  64.     iwidth = ceil(width*scale) + 4;
  65.     iheight = ceil(height*scale) + 4;
  66.     
  67.     printf ("scale: %f\n",scale);
  68.     printf ("iwidth: %i  iheight: %i\n",iwidth, iheight);
  69. }
  70.  
  71.  
  72. /*
  73. ============
  74. DrawLine
  75.  
  76. Draw a fat line
  77. ============
  78. */
  79. void DrawLine (int x1, int y1, int x2, int y2)
  80. {
  81.     int        dx, dy;
  82.     int        adx, ady;
  83.     int        count;
  84.     float    xfrac, yfrac, xstep, ystep;
  85.     unsigned        sx, sy;
  86.     float        u, v;
  87.     
  88.     dx = x2 - x1;
  89.     dy = y2 - y1;
  90.     adx = abs(dx);
  91.     ady = abs(dy);
  92.     
  93.     count = adx > ady ? adx : ady;
  94.     count ++;
  95.     
  96.     if (count > 300)
  97.         return;        // don't ever hang up on bad data
  98.         
  99.     xfrac = x1;
  100.     yfrac = y1;
  101.     
  102.     xstep = (float)dx / count;
  103.     ystep = (float)dy / count;
  104.     
  105.     do
  106.     {
  107.         for (u=-0.1 ; u<=0.9 ; u+=0.999)
  108.             for (v=-0.1 ; v<=0.9 ; v+=0.999)
  109.             {
  110.                 sx = xfrac+u;
  111.                 sy = yfrac+v;
  112.                 if (sx < 320 && sy < 200)
  113.                     pic[sy*320+sx] = 255;
  114.             }
  115.             
  116.         xfrac += xstep;
  117.         yfrac += ystep;
  118.         count--;
  119.     } while (count > 0);
  120. }
  121.  
  122.  
  123. /*
  124. ============
  125. AddFace
  126. ============
  127. */
  128. void AddFace (triangle_t *f)
  129. {
  130.     vec3_t        v1, v2, normal;
  131.     int        basex, basey;
  132.     int            i, j;
  133.     int        coords[3][2];
  134.  
  135. //
  136. // determine which side to map the teture to
  137. //
  138.     VectorSubtract (f->verts[0], f->verts[1], v1);
  139.     VectorSubtract (f->verts[2], f->verts[1], v2);
  140.     CrossProduct (v1, v2, normal);
  141.     
  142.     if (normal[1] > 0)
  143.         basex = iwidth + 2;
  144.     else
  145.         basex = 2;
  146.     basey = 2;
  147.  
  148.     for (i=0 ; i<3 ; i++)
  149.     {
  150.         coords[i][0] = Q_rint((f->verts[i][0] - mins[0])*scale + basex);
  151.         coords[i][1] = Q_rint( (maxs[2] - f->verts[i][2])*scale + basey);
  152.     }
  153.     
  154. //
  155. // draw lines
  156. //
  157.     for (i=0 ; i<3 ; i++)
  158.     {
  159.         j = (i+1)%3;
  160.         DrawLine (coords[i][0], coords[i][1],
  161.         coords[j][0], coords[j][1]);
  162.     }
  163. }
  164.  
  165.  
  166. /*
  167. ============
  168. CalcPalette
  169. ============
  170. */
  171. void CalcPalette (void)
  172. {
  173.     byte *picture;
  174.     LoadLBM (ExpandPath("id1/gfx/gamepal.lbm"), &picture, &palette);
  175. }
  176.  
  177.  
  178.  
  179. /*
  180. ============
  181. main
  182. ============
  183. */
  184. void main (int argc, char **argv)
  185. {
  186.     int        i;
  187.     char    filename[1024];
  188.                 
  189.     if (argc == 1)
  190.         Error ("texmake polfile[.idpol]\nGenerates polfile.lbm and polfile_t.idpol\n");
  191.     
  192. //
  193. // read the polfile
  194. //    
  195.     strcpy (filename, argv[1]);
  196.     DefaultExtension (filename, ".tri");
  197.     SetQdirFromPath (filename);
  198.     LoadTriangleList (filename, &faces, &numfaces);
  199.     printf ("numfaces: %i\n",numfaces);
  200.     
  201. //
  202. // generate the texture coordinates
  203. //
  204.     BoundFaces ();
  205.     
  206. //
  207. // generate the lbm
  208. //
  209.     for (i=0 ; i<numfaces ; i++)
  210.         AddFace (&faces[i]);
  211.         
  212. //
  213. // save the lbm
  214. //
  215.     strcpy (filename, argv[1]);
  216.     StripExtension (filename);
  217.     strcat (filename, ".lbm");
  218.  
  219.     printf ("output file: %s\n",filename);
  220.     CalcPalette ();
  221.     WriteLBMfile (filename, pic, 320, 200, palette);    
  222. }
  223.