Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

textures.c (5327B)


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