UFO: Alien Invasion
Loading...
Searching...
No Matches
textures.cpp
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 1997-2001 Id Software, Inc.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23*/
24
25
26#include "bsp.h"
27#include "textures.h"
28
29static int nummiptex = 0;
31
38int FindMiptex (const char* name)
39{
40 int i;
41
42 /* search through textures that have already been loaded. */
43 for (i = 0; i < nummiptex; i++)
44 if (Q_streq(name, textureref[i].name)) {
45 return i;
46 }
48 Sys_Error("MAX_MAP_TEXTURES");
50
51 return i;
52}
53
54
55static const 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
65static void TextureAxisFromPlane (plane_t* pln, vec3_t xv, vec3_t yv, bool isTerrain)
66{
67 /* Knightmare- terrain support, use floor/ceiling axis only */
68 int numaxis = (isTerrain) ? 2 : 6;
69
70 vec_t best = 0;
71 int bestaxis = 0;
72
73 for (int i = 0; i < numaxis; i++) {
74 const vec_t dot = DotProduct(pln->normal, baseaxis[i * 3]);
75 if (dot > best) {
76 best = dot;
77 bestaxis = i;
78 }
79 }
80
81 VectorCopy(baseaxis[bestaxis * 3 + 1], xv);
82 VectorCopy(baseaxis[bestaxis * 3 + 2], yv);
83}
84
85
89int TexinfoForBrushTexture (plane_t* plane, brush_texture_t* bt, const vec3_t origin, bool isTerrain)
90{
91 vec3_t vecs[2];
92 int sv, tv;
93 vec_t ang, sinv, cosv;
94 dBspTexinfo_t tx, *tc;
95 int i, j;
96 float shift[2];
97 vec3_t scaledOrigin;
98
99 if (!bt->name[0])
100 return 0;
101
102 OBJZERO(tx);
103 Q_strncpyz(tx.texture, bt->name, sizeof(tx.texture));
104
105 TextureAxisFromPlane(plane, vecs[0], vecs[1], isTerrain);
106
107 /* dot product of a vertex location with the [4] part will produce a
108 * texcoord (s or t depending on the first index) */
109 VectorScale(origin, 1.0 / bt->scale[0], scaledOrigin);
110 shift[0] = DotProduct(scaledOrigin, vecs[0]);
111 VectorScale(origin, 1.0 / bt->scale[1], scaledOrigin);
112 shift[1] = DotProduct(scaledOrigin, vecs[1]);
113
114 if (!bt->scale[0])
115 bt->scale[0] = 1;
116 if (!bt->scale[1])
117 bt->scale[1] = 1;
118
119 /* rotate axis */
120 if (bt->rotate == 0) {
121 sinv = 0;
122 cosv = 1;
123 } else if (bt->rotate == 90) {
124 sinv = 1;
125 cosv = 0;
126 } else if (bt->rotate == 180) {
127 sinv = 0;
128 cosv = -1;
129 } else if (bt->rotate == 270) {
130 sinv = -1;
131 cosv = 0;
132 } else {
133 ang = bt->rotate * torad;
134 sinv = sin(ang);
135 cosv = cos(ang);
136 }
137
138 shift[0] = cosv * shift[0] - sinv * shift[1];
139 shift[1] = sinv * shift[0] + cosv * shift[1];
140
141 if (vecs[0][0])
142 sv = 0;
143 else if (vecs[0][1])
144 sv = 1;
145 else
146 sv = 2;
147
148 if (vecs[1][0])
149 tv = 0;
150 else if (vecs[1][1])
151 tv = 1;
152 else
153 tv = 2;
154
155 for (i = 0; i < 2; i++) {
156 const vec_t ns = cosv * vecs[i][sv] - sinv * vecs[i][tv];
157 const vec_t nt = sinv * vecs[i][sv] + cosv * vecs[i][tv];
158 vecs[i][sv] = ns;
159 vecs[i][tv] = nt;
160 }
161
162 for (i = 0; i < 2; i++)
163 for (j = 0; j < 3; j++)
164 tx.vecs[i][j] = vecs[i][j] / bt->scale[i];
165
166 /* texture offsets */
167 tx.vecs[0][3] = bt->shift[0] + shift[0];
168 tx.vecs[1][3] = bt->shift[1] + shift[1];
169
170 tx.surfaceFlags = bt->surfaceFlags;
171 tx.value = bt->value;
172
173 /* find the texinfo */
174 tc = curTile->texinfo;
175 for (i = 0; i < curTile->numtexinfo; i++, tc++) {
176 if (tc->surfaceFlags != tx.surfaceFlags)
177 continue;
178 if (tc->value != tx.value)
179 continue;
180 if (!Q_streq(tc->texture, tx.texture))
181 continue;
182 for (j = 0; j < 2; j++) {
183 for (int k = 0; k < 4; k++) {
184 if (tc->vecs[j][k] != tx.vecs[j][k])
185 goto skip;
186 }
187 }
188 return i;
189skip:;
190 }
191 if (curTile->numtexinfo >= MAX_MAP_TEXINFO)
192 Sys_Error("MAX_MAP_TEXINFO overflow");
193 *tc = tx;
194 curTile->numtexinfo++;
195
196 return i;
197}
#define MAX_MAP_TEXINFO
Definition defines.h:138
#define MAX_MAP_TEXTURES
Definition defines.h:384
void Sys_Error(const char *error,...)
Definition g_main.cpp:421
voidpf uLong int origin
Definition ioapi.h:45
#define torad
Definition mathlib.h:50
QGL_EXTERN GLint i
Definition r_gl.h:113
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
static ipos3_t shift
The shift array is used for random map assemblies (RMA) to shift the mins/maxs and stuff like that.
serverInstanceGame_t * sv
Definition sv_init.cpp:36
#define Q_streq(a, b)
Definition shared.h:136
#define OBJZERO(obj)
Definition shared.h:178
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition shared.cpp:457
vec2_t shift
Definition map.h:34
vec2_t scale
Definition map.h:36
char name[MAX_TEXPATH]
Definition map.h:37
int value
Definition map.h:39
vec_t rotate
Definition map.h:35
uint32_t surfaceFlags
Definition map.h:38
char texture[32]
Definition typedefs.h:392
uint32_t surfaceFlags
Definition typedefs.h:390
float vecs[2][4]
Definition typedefs.h:389
uint32_t value
Definition typedefs.h:391
Definition map.h:98
vec3_t normal
Definition map.h:99
static void TextureAxisFromPlane(plane_t *pln, vec3_t xv, vec3_t yv, bool isTerrain)
Definition textures.cpp:65
textureref_t textureref[MAX_MAP_TEXTURES]
Definition textures.cpp:30
int TexinfoForBrushTexture(plane_t *plane, brush_texture_t *bt, const vec3_t origin, bool isTerrain)
Definition textures.cpp:89
static int nummiptex
Definition textures.cpp:29
int FindMiptex(const char *name)
Definition textures.cpp:38
static const vec3_t baseaxis[18]
Definition textures.cpp:55
dMapTile_t * curTile
Definition bsp.cpp:32
float vec_t
Definition ufotypes.h:37
vec_t vec3_t[3]
Definition ufotypes.h:39
#define VectorCopy(src, dest)
Definition vector.h:51
#define DotProduct(x, y)
Returns the distance between two 3-dimensional vectors.
Definition vector.h:44
#define VectorScale(in, scale, out)
Definition vector.h:79