home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / bsp / qbsp3 / textures.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-30  |  3.8 KB  |  201 lines

  1.  
  2. #include "qbsp.h"
  3.  
  4. int        nummiptex;
  5. textureref_t    textureref[MAX_MAP_TEXTURES];
  6.  
  7. //==========================================================================
  8.  
  9.  
  10. int    FindMiptex (char *name)
  11. {
  12.     int        i;
  13.     char    path[1024];
  14.     miptex_t    *mt;
  15.  
  16.     for (i=0 ; i<nummiptex ; i++)
  17.         if (!strcmp (name, textureref[i].name))
  18.         {
  19.             return i;
  20.         }
  21.     if (nummiptex == MAX_MAP_TEXTURES)
  22.         Error ("MAX_MAP_TEXTURES");
  23.     strcpy (textureref[i].name, name);
  24.  
  25.     // load the miptex to get the flags and values
  26.     sprintf (path, "%stextures/%s.wal", gamedir, name);
  27.     if (TryLoadFile (path, (void **)&mt) != -1)
  28.     {
  29.         textureref[i].value = LittleLong (mt->value);
  30.         textureref[i].flags = LittleLong (mt->flags);
  31.         textureref[i].contents = LittleLong (mt->contents);
  32.         strcpy (textureref[i].animname, mt->animname);
  33.         free (mt);
  34.     }
  35.     nummiptex++;
  36.  
  37.     if (textureref[i].animname[0])
  38.         FindMiptex (textureref[i].animname);
  39.  
  40.     return i;
  41. }
  42.  
  43.  
  44. /*
  45. ==================
  46. textureAxisFromPlane
  47. ==================
  48. */
  49. vec3_t    baseaxis[18] =
  50. {
  51. {0,0,1}, {1,0,0}, {0,-1,0},            // floor
  52. {0,0,-1}, {1,0,0}, {0,-1,0},        // ceiling
  53. {1,0,0}, {0,1,0}, {0,0,-1},            // west wall
  54. {-1,0,0}, {0,1,0}, {0,0,-1},        // east wall
  55. {0,1,0}, {1,0,0}, {0,0,-1},            // south wall
  56. {0,-1,0}, {1,0,0}, {0,0,-1}            // north wall
  57. };
  58.  
  59. void TextureAxisFromPlane(plane_t *pln, vec3_t xv, vec3_t yv)
  60. {
  61.     int        bestaxis;
  62.     vec_t    dot,best;
  63.     int        i;
  64.     
  65.     best = 0;
  66.     bestaxis = 0;
  67.     
  68.     for (i=0 ; i<6 ; i++)
  69.     {
  70.         dot = DotProduct (pln->normal, baseaxis[i*3]);
  71.         if (dot > best)
  72.         {
  73.             best = dot;
  74.             bestaxis = i;
  75.         }
  76.     }
  77.     
  78.     VectorCopy (baseaxis[bestaxis*3+1], xv);
  79.     VectorCopy (baseaxis[bestaxis*3+2], yv);
  80. }
  81.  
  82.  
  83.  
  84.  
  85. int TexinfoForBrushTexture (plane_t *plane, brush_texture_t *bt, vec3_t origin)
  86. {
  87.     vec3_t    vecs[2];
  88.     int        sv, tv;
  89.     vec_t    ang, sinv, cosv;
  90.     vec_t    ns, nt;
  91.     texinfo_t    tx, *tc;
  92.     int        i, j, k;
  93.     float    shift[2];
  94.     brush_texture_t        anim;
  95.     int                mt;
  96.  
  97.     if (!bt->name[0])
  98.         return 0;
  99.  
  100.     memset (&tx, 0, sizeof(tx));
  101.     strcpy (tx.texture, bt->name);
  102.  
  103.     TextureAxisFromPlane(plane, vecs[0], vecs[1]);
  104.  
  105.     shift[0] = DotProduct (origin, vecs[0]);
  106.     shift[1] = DotProduct (origin, vecs[1]);
  107.  
  108.     if (!bt->scale[0])
  109.         bt->scale[0] = 1;
  110.     if (!bt->scale[1])
  111.         bt->scale[1] = 1;
  112.  
  113.  
  114. // rotate axis
  115.     if (bt->rotate == 0)
  116.         { sinv = 0 ; cosv = 1; }
  117.     else if (bt->rotate == 90)
  118.         { sinv = 1 ; cosv = 0; }
  119.     else if (bt->rotate == 180)
  120.         { sinv = 0 ; cosv = -1; }
  121.     else if (bt->rotate == 270)
  122.         { sinv = -1 ; cosv = 0; }
  123.     else
  124.     {    
  125.         ang = bt->rotate / 180 * Q_PI;
  126.         sinv = sin(ang);
  127.         cosv = cos(ang);
  128.     }
  129.  
  130.     if (vecs[0][0])
  131.         sv = 0;
  132.     else if (vecs[0][1])
  133.         sv = 1;
  134.     else
  135.         sv = 2;
  136.                 
  137.     if (vecs[1][0])
  138.         tv = 0;
  139.     else if (vecs[1][1])
  140.         tv = 1;
  141.     else
  142.         tv = 2;
  143.                     
  144.     for (i=0 ; i<2 ; i++)
  145.     {
  146.         ns = cosv * vecs[i][sv] - sinv * vecs[i][tv];
  147.         nt = sinv * vecs[i][sv] +  cosv * vecs[i][tv];
  148.         vecs[i][sv] = ns;
  149.         vecs[i][tv] = nt;
  150.     }
  151.  
  152.     for (i=0 ; i<2 ; i++)
  153.         for (j=0 ; j<3 ; j++)
  154.             tx.vecs[i][j] = vecs[i][j] / bt->scale[i];
  155.  
  156.     tx.vecs[0][3] = bt->shift[0] + shift[0];
  157.     tx.vecs[1][3] = bt->shift[1] + shift[1];
  158.     tx.flags = bt->flags;
  159.     tx.value = bt->value;
  160.  
  161.     //
  162.     // find the texinfo
  163.     //
  164.     tc = texinfo;
  165.     for (i=0 ; i<numtexinfo ; i++, tc++)
  166.     {
  167.         if (tc->flags != tx.flags)
  168.             continue;
  169.         if (tc->value != tx.value)
  170.             continue;
  171.         for (j=0 ; j<2 ; j++)
  172.         {
  173.             if (strcmp (tc->texture, tx.texture))
  174.                 goto skip;
  175.             for (k=0 ; k<4 ; k++)
  176.             {
  177.                 if (tc->vecs[j][k] != tx.vecs[j][k])
  178.                     goto skip;
  179.             }
  180.         }
  181.         return i;
  182. skip:;
  183.     }
  184.     *tc = tx;
  185.     numtexinfo++;
  186.  
  187.     // load the next animation
  188.     mt = FindMiptex (bt->name);
  189.     if (textureref[mt].animname[0])
  190.     {
  191.         anim = *bt;
  192.         strcpy (anim.name, textureref[mt].animname);
  193.         tc->nexttexinfo = TexinfoForBrushTexture (plane, &anim, origin);
  194.     }
  195.     else
  196.         tc->nexttexinfo = -1;
  197.  
  198.  
  199.     return i;
  200. }
  201.