textures.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qbsp.h"
  19. int nummiptex;
  20. textureref_t textureref[MAX_MAP_TEXTURES];
  21. //==========================================================================
  22. int FindMiptex (char *name)
  23. {
  24. int i;
  25. char path[1024];
  26. miptex_t *mt;
  27. for (i=0 ; i<nummiptex ; i++)
  28. if (!strcmp (name, textureref[i].name))
  29. {
  30. return i;
  31. }
  32. if (nummiptex == MAX_MAP_TEXTURES)
  33. Error ("MAX_MAP_TEXTURES");
  34. strcpy (textureref[i].name, name);
  35. // load the miptex to get the flags and values
  36. sprintf (path, "%stextures/%s.wal", gamedir, name);
  37. if (TryLoadFile (path, (void **)&mt) != -1)
  38. {
  39. textureref[i].value = LittleLong (mt->value);
  40. textureref[i].flags = LittleLong (mt->flags);
  41. textureref[i].contents = LittleLong (mt->contents);
  42. strcpy (textureref[i].animname, mt->animname);
  43. free (mt);
  44. }
  45. nummiptex++;
  46. if (textureref[i].animname[0])
  47. FindMiptex (textureref[i].animname);
  48. return i;
  49. }
  50. /*
  51. ==================
  52. textureAxisFromPlane
  53. ==================
  54. */
  55. vec3_t baseaxis[18] =
  56. {
  57. {0,0,1}, {1,0,0}, {0,-1,0}, // floor
  58. {0,0,-1}, {1,0,0}, {0,-1,0}, // ceiling
  59. {1,0,0}, {0,1,0}, {0,0,-1}, // west wall
  60. {-1,0,0}, {0,1,0}, {0,0,-1}, // east wall
  61. {0,1,0}, {1,0,0}, {0,0,-1}, // south wall
  62. {0,-1,0}, {1,0,0}, {0,0,-1} // north wall
  63. };
  64. void TextureAxisFromPlane(plane_t *pln, vec3_t xv, vec3_t yv)
  65. {
  66. int bestaxis;
  67. vec_t dot,best;
  68. int i;
  69. best = 0;
  70. bestaxis = 0;
  71. for (i=0 ; i<6 ; i++)
  72. {
  73. dot = DotProduct (pln->normal, baseaxis[i*3]);
  74. if (dot > best)
  75. {
  76. best = dot;
  77. bestaxis = i;
  78. }
  79. }
  80. VectorCopy (baseaxis[bestaxis*3+1], xv);
  81. VectorCopy (baseaxis[bestaxis*3+2], yv);
  82. }
  83. int TexinfoForBrushTexture (plane_t *plane, brush_texture_t *bt, vec3_t origin)
  84. {
  85. vec3_t vecs[2];
  86. int sv, tv;
  87. vec_t ang, sinv, cosv;
  88. vec_t ns, nt;
  89. texinfo_t tx, *tc;
  90. int i, j, k;
  91. float shift[2];
  92. brush_texture_t anim;
  93. int mt;
  94. if (!bt->name[0])
  95. return 0;
  96. memset (&tx, 0, sizeof(tx));
  97. strcpy (tx.texture, bt->name);
  98. TextureAxisFromPlane(plane, vecs[0], vecs[1]);
  99. shift[0] = DotProduct (origin, vecs[0]);
  100. shift[1] = DotProduct (origin, vecs[1]);
  101. if (!bt->scale[0])
  102. bt->scale[0] = 1;
  103. if (!bt->scale[1])
  104. bt->scale[1] = 1;
  105. // rotate axis
  106. if (bt->rotate == 0)
  107. { sinv = 0 ; cosv = 1; }
  108. else if (bt->rotate == 90)
  109. { sinv = 1 ; cosv = 0; }
  110. else if (bt->rotate == 180)
  111. { sinv = 0 ; cosv = -1; }
  112. else if (bt->rotate == 270)
  113. { sinv = -1 ; cosv = 0; }
  114. else
  115. {
  116. ang = bt->rotate / 180 * Q_PI;
  117. sinv = sin(ang);
  118. cosv = cos(ang);
  119. }
  120. if (vecs[0][0])
  121. sv = 0;
  122. else if (vecs[0][1])
  123. sv = 1;
  124. else
  125. sv = 2;
  126. if (vecs[1][0])
  127. tv = 0;
  128. else if (vecs[1][1])
  129. tv = 1;
  130. else
  131. tv = 2;
  132. for (i=0 ; i<2 ; i++)
  133. {
  134. ns = cosv * vecs[i][sv] - sinv * vecs[i][tv];
  135. nt = sinv * vecs[i][sv] + cosv * vecs[i][tv];
  136. vecs[i][sv] = ns;
  137. vecs[i][tv] = nt;
  138. }
  139. for (i=0 ; i<2 ; i++)
  140. for (j=0 ; j<3 ; j++)
  141. tx.vecs[i][j] = vecs[i][j] / bt->scale[i];
  142. tx.vecs[0][3] = bt->shift[0] + shift[0];
  143. tx.vecs[1][3] = bt->shift[1] + shift[1];
  144. tx.flags = bt->flags;
  145. tx.value = bt->value;
  146. //
  147. // find the texinfo
  148. //
  149. tc = texinfo;
  150. for (i=0 ; i<numtexinfo ; i++, tc++)
  151. {
  152. if (tc->flags != tx.flags)
  153. continue;
  154. if (tc->value != tx.value)
  155. continue;
  156. for (j=0 ; j<2 ; j++)
  157. {
  158. if (strcmp (tc->texture, tx.texture))
  159. goto skip;
  160. for (k=0 ; k<4 ; k++)
  161. {
  162. if (tc->vecs[j][k] != tx.vecs[j][k])
  163. goto skip;
  164. }
  165. }
  166. return i;
  167. skip:;
  168. }
  169. *tc = tx;
  170. numtexinfo++;
  171. // load the next animation
  172. mt = FindMiptex (bt->name);
  173. if (textureref[mt].animname[0])
  174. {
  175. anim = *bt;
  176. strcpy (anim.name, textureref[mt].animname);
  177. tc->nexttexinfo = TexinfoForBrushTexture (plane, &anim, origin);
  178. }
  179. else
  180. tc->nexttexinfo = -1;
  181. return i;
  182. }