00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "qbsp.h"
00024 #include "l_bsp_q2.h"
00025
00026 int nummiptex;
00027 textureref_t textureref[MAX_MAP_TEXTURES];
00028
00029
00030
00031
00032
00033
00034
00035 int FindMiptex (char *name)
00036 {
00037 int i;
00038 char path[1024];
00039 miptex_t *mt;
00040
00041 for (i = 0; i < nummiptex; i++)
00042 {
00043 if (!strcmp (name, textureref[i].name))
00044 {
00045 return i;
00046 }
00047 }
00048 if (nummiptex == MAX_MAP_TEXTURES)
00049 Error ("MAX_MAP_TEXTURES");
00050 strcpy (textureref[i].name, name);
00051
00052
00053 sprintf (path, "%stextures/%s.wal", gamedir, name);
00054 if (TryLoadFile (path, (void **)&mt) != -1)
00055 {
00056 textureref[i].value = LittleLong (mt->value);
00057 textureref[i].flags = LittleLong (mt->flags);
00058 textureref[i].contents = LittleLong (mt->contents);
00059 strcpy (textureref[i].animname, mt->animname);
00060 FreeMemory(mt);
00061 }
00062 nummiptex++;
00063
00064 if (textureref[i].animname[0])
00065 FindMiptex (textureref[i].animname);
00066
00067 return i;
00068 }
00069
00070
00071
00072
00073
00074
00075 vec3_t baseaxis[18] =
00076 {
00077 {0,0,1}, {1,0,0}, {0,-1,0},
00078 {0,0,-1}, {1,0,0}, {0,-1,0},
00079 {1,0,0}, {0,1,0}, {0,0,-1},
00080 {-1,0,0}, {0,1,0}, {0,0,-1},
00081 {0,1,0}, {1,0,0}, {0,0,-1},
00082 {0,-1,0}, {1,0,0}, {0,0,-1}
00083 };
00084
00085 void TextureAxisFromPlane(plane_t *pln, vec3_t xv, vec3_t yv)
00086 {
00087 int bestaxis;
00088 vec_t dot,best;
00089 int i;
00090
00091 best = 0;
00092 bestaxis = 0;
00093
00094 for (i=0 ; i<6 ; i++)
00095 {
00096 dot = DotProduct (pln->normal, baseaxis[i*3]);
00097 if (dot > best)
00098 {
00099 best = dot;
00100 bestaxis = i;
00101 }
00102 }
00103
00104 VectorCopy (baseaxis[bestaxis*3+1], xv);
00105 VectorCopy (baseaxis[bestaxis*3+2], yv);
00106 }
00107
00108
00109
00110
00111
00112
00113 int TexinfoForBrushTexture(plane_t *plane, brush_texture_t *bt, vec3_t origin)
00114 {
00115 vec3_t vecs[2];
00116 int sv, tv;
00117 vec_t ang, sinv, cosv;
00118 vec_t ns, nt;
00119 texinfo_t tx, *tc;
00120 int i, j, k;
00121 float shift[2];
00122 brush_texture_t anim;
00123 int mt;
00124
00125 if (!bt->name[0])
00126 return 0;
00127
00128 memset (&tx, 0, sizeof(tx));
00129 strcpy (tx.texture, bt->name);
00130
00131 TextureAxisFromPlane(plane, vecs[0], vecs[1]);
00132
00133 shift[0] = DotProduct (origin, vecs[0]);
00134 shift[1] = DotProduct (origin, vecs[1]);
00135
00136 if (!bt->scale[0])
00137 bt->scale[0] = 1;
00138 if (!bt->scale[1])
00139 bt->scale[1] = 1;
00140
00141
00142
00143 if (bt->rotate == 0)
00144 { sinv = 0 ; cosv = 1; }
00145 else if (bt->rotate == 90)
00146 { sinv = 1 ; cosv = 0; }
00147 else if (bt->rotate == 180)
00148 { sinv = 0 ; cosv = -1; }
00149 else if (bt->rotate == 270)
00150 { sinv = -1 ; cosv = 0; }
00151 else
00152 {
00153 ang = bt->rotate / 180 * Q_PI;
00154 sinv = sin(ang);
00155 cosv = cos(ang);
00156 }
00157
00158 if (vecs[0][0])
00159 sv = 0;
00160 else if (vecs[0][1])
00161 sv = 1;
00162 else
00163 sv = 2;
00164
00165 if (vecs[1][0])
00166 tv = 0;
00167 else if (vecs[1][1])
00168 tv = 1;
00169 else
00170 tv = 2;
00171
00172 for (i=0 ; i<2 ; i++)
00173 {
00174 ns = cosv * vecs[i][sv] - sinv * vecs[i][tv];
00175 nt = sinv * vecs[i][sv] + cosv * vecs[i][tv];
00176 vecs[i][sv] = ns;
00177 vecs[i][tv] = nt;
00178 }
00179
00180 for (i=0 ; i<2 ; i++)
00181 for (j=0 ; j<3 ; j++)
00182 tx.vecs[i][j] = vecs[i][j] / bt->scale[i];
00183
00184 tx.vecs[0][3] = bt->shift[0] + shift[0];
00185 tx.vecs[1][3] = bt->shift[1] + shift[1];
00186 tx.flags = bt->flags;
00187 tx.value = bt->value;
00188
00189
00190
00191
00192 tc = texinfo;
00193 for (i=0 ; i<numtexinfo ; i++, tc++)
00194 {
00195 if (tc->flags != tx.flags)
00196 continue;
00197 if (tc->value != tx.value)
00198 continue;
00199 for (j=0 ; j<2 ; j++)
00200 {
00201 if (strcmp (tc->texture, tx.texture))
00202 goto skip;
00203 for (k=0 ; k<4 ; k++)
00204 {
00205 if (tc->vecs[j][k] != tx.vecs[j][k])
00206 goto skip;
00207 }
00208 }
00209 return i;
00210 skip:;
00211 }
00212 *tc = tx;
00213 numtexinfo++;
00214
00215
00216 mt = FindMiptex (bt->name);
00217 if (textureref[mt].animname[0])
00218 {
00219 anim = *bt;
00220 strcpy (anim.name, textureref[mt].animname);
00221 tc->nexttexinfo = TexinfoForBrushTexture (plane, &anim, origin);
00222 }
00223 else
00224 tc->nexttexinfo = -1;
00225
00226
00227 return i;
00228 }