]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rmain.c
Add TEXF_PERSISTENT and R_PurgeTexture which only frees textures if that flag isn...
[divverent/darkplaces.git] / gl_rmain.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_main.c
21
22 #include "quakedef.h"
23 #include "cl_dyntexture.h"
24 #include "r_shadow.h"
25 #include "polygon.h"
26 #include "image.h"
27
28 mempool_t *r_main_mempool;
29 rtexturepool_t *r_main_texturepool;
30
31 //
32 // screen size info
33 //
34 r_refdef_t r_refdef;
35 r_view_t r_view;
36 r_viewcache_t r_viewcache;
37
38 cvar_t r_depthfirst = {CVAR_SAVE, "r_depthfirst", "1", "renders a depth-only version of the scene before normal rendering begins to eliminate overdraw, values: 0 = off, 1 = world depth, 2 = world and model depth"};
39 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
40 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%,  10 = 100%)"};
41 cvar_t r_showsurfaces = {0, "r_showsurfaces", "0", "1 shows surfaces as different colors, or a value of 2 shows triangle draw order (for analyzing whether meshes are optimized for vertex cache)"};
42 cvar_t r_showtris = {0, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
43 cvar_t r_shownormals = {0, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
44 cvar_t r_showlighting = {0, "r_showlighting", "0", "shows areas lit by lights, useful for finding out why some areas of a map render slowly (bright orange = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
45 cvar_t r_showshadowvolumes = {0, "r_showshadowvolumes", "0", "shows areas shadowed by lights, useful for finding out why some areas of a map render slowly (bright blue = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
46 cvar_t r_showcollisionbrushes = {0, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
47 cvar_t r_showcollisionbrushes_polygonfactor = {0, "r_showcollisionbrushes_polygonfactor", "-1", "expands outward the brush polygons a little bit, used to make collision brushes appear infront of walls"};
48 cvar_t r_showcollisionbrushes_polygonoffset = {0, "r_showcollisionbrushes_polygonoffset", "0", "nudges brush polygon depth in hardware depth units, used to make collision brushes appear infront of walls"};
49 cvar_t r_showdisabledepthtest = {0, "r_showdisabledepthtest", "0", "disables depth testing on r_show* cvars, allowing you to see what hidden geometry the graphics card is processing"};
50 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
51 cvar_t r_drawentities = {0, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
52 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1", "draw your weapon model"};
53 cvar_t r_cullentities_trace = {0, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
54 cvar_t r_cullentities_trace_samples = {0, "r_cullentities_trace_samples", "2", "number of samples to test for entity culling"};
55 cvar_t r_cullentities_trace_enlarge = {0, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
56 cvar_t r_cullentities_trace_delay = {0, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
57 cvar_t r_speeds = {0, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
58 cvar_t r_fullbright = {0, "r_fullbright","0", "makes map very bright and renders faster"};
59 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1", "opacity of water polygons"};
60 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
61 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
62 cvar_t r_shadows = {CVAR_SAVE, "r_shadows", "0", "casts fake stencil shadows from models onto the world (rtlights are unaffected by this)"};
63 cvar_t r_shadows_throwdistance = {CVAR_SAVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
64 cvar_t r_q1bsp_skymasking = {0, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
65 cvar_t r_polygonoffset_submodel_factor = {0, "r_polygonoffset_submodel_factor", "0", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
66 cvar_t r_polygonoffset_submodel_offset = {0, "r_polygonoffset_submodel_offset", "2", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
67
68 cvar_t gl_fogenable = {0, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
69 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
70 cvar_t gl_fogred = {0, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
71 cvar_t gl_foggreen = {0, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
72 cvar_t gl_fogblue = {0, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
73 cvar_t gl_fogstart = {0, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
74 cvar_t gl_fogend = {0, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
75
76 cvar_t r_textureunits = {0, "r_textureunits", "32", "number of hardware texture units reported by driver (note: setting this to 1 turns off gl_combine)"};
77
78 cvar_t r_glsl = {CVAR_SAVE, "r_glsl", "1", "enables use of OpenGL 2.0 pixel shaders for lighting"};
79 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
80 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
81 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
82 cvar_t r_glsl_deluxemapping = {CVAR_SAVE, "r_glsl_deluxemapping", "1", "use per pixel lighting on deluxemap-compiled q3bsp maps (or a value of 2 forces deluxemap shading even without deluxemaps)"};
83 cvar_t r_glsl_contrastboost = {CVAR_SAVE, "r_glsl_contrastboost", "1", "by how much to multiply the contrast in dark areas (1 is no change)"};
84
85 cvar_t r_water = {CVAR_SAVE, "r_water", "0", "whether to use reflections and refraction on water surfaces (note: r_wateralpha must be set below 1)"};
86 cvar_t r_water_clippingplanebias = {CVAR_SAVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
87 cvar_t r_water_resolutionmultiplier = {CVAR_SAVE, "r_water_resolutionmultiplier", "0.5", "multiplier for screen resolution when rendering refracted/reflected scenes, 1 is full quality, lower values are faster"};
88 cvar_t r_water_refractdistort = {CVAR_SAVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
89 cvar_t r_water_reflectdistort = {CVAR_SAVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
90
91 cvar_t r_lerpsprites = {CVAR_SAVE, "r_lerpsprites", "1", "enables animation smoothing on sprites (requires r_lerpmodels 1)"};
92 cvar_t r_lerpmodels = {CVAR_SAVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
93 cvar_t r_waterscroll = {CVAR_SAVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
94
95 cvar_t r_bloom = {CVAR_SAVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
96 cvar_t r_bloom_colorscale = {CVAR_SAVE, "r_bloom_colorscale", "1", "how bright the glow is"};
97 cvar_t r_bloom_brighten = {CVAR_SAVE, "r_bloom_brighten", "2", "how bright the glow is, after subtract/power"};
98 cvar_t r_bloom_blur = {CVAR_SAVE, "r_bloom_blur", "4", "how large the glow is"};
99 cvar_t r_bloom_resolution = {CVAR_SAVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
100 cvar_t r_bloom_colorexponent = {CVAR_SAVE, "r_bloom_colorexponent", "1", "how exagerated the glow is"};
101 cvar_t r_bloom_colorsubtract = {CVAR_SAVE, "r_bloom_colorsubtract", "0.125", "reduces bloom colors by a certain amount"};
102
103 cvar_t r_hdr = {CVAR_SAVE, "r_hdr", "0", "enables High Dynamic Range bloom effect (higher quality version of r_bloom)"};
104 cvar_t r_hdr_scenebrightness = {CVAR_SAVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
105 cvar_t r_hdr_glowintensity = {CVAR_SAVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
106 cvar_t r_hdr_range = {CVAR_SAVE, "r_hdr_range", "4", "how much dynamic range to render bloom with (equivilant to multiplying r_bloom_brighten by this value and dividing r_bloom_colorscale by this value)"};
107
108 cvar_t r_smoothnormals_areaweighting = {0, "r_smoothnormals_areaweighting", "1", "uses significantly faster (and supposedly higher quality) area-weighted vertex normals and tangent vectors rather than summing normalized triangle normals and tangents"};
109
110 cvar_t developer_texturelogging = {0, "developer_texturelogging", "0", "produces a textures.log file containing names of skins and map textures the engine tried to load"};
111
112 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers)"};
113
114 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
115 cvar_t r_batchmode = {0, "r_batchmode", "1", "selects method of rendering multiple surfaces with one driver call (values are 0, 1, 2, etc...)"};
116 cvar_t r_track_sprites = {CVAR_SAVE, "r_track_sprites", "1", "track SPR_LABEL* sprites by putting them as indicator at the screen border to rotate to"};
117 cvar_t r_track_sprites_flags = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accodringly, 2: Make it a continuous rotation"};
118 cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
119 cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
120
121 extern qboolean v_flipped_state;
122
123 typedef struct r_glsl_bloomshader_s
124 {
125         int program;
126         int loc_Texture_Bloom;
127 }
128 r_glsl_bloomshader_t;
129
130 static struct r_bloomstate_s
131 {
132         qboolean enabled;
133         qboolean hdr;
134
135         int bloomwidth, bloomheight;
136
137         int screentexturewidth, screentextureheight;
138         rtexture_t *texture_screen;
139
140         int bloomtexturewidth, bloomtextureheight;
141         rtexture_t *texture_bloom;
142
143         r_glsl_bloomshader_t *shader;
144
145         // arrays for rendering the screen passes
146         float screentexcoord2f[8];
147         float bloomtexcoord2f[8];
148         float offsettexcoord2f[8];
149 }
150 r_bloomstate;
151
152 typedef struct r_waterstate_waterplane_s
153 {
154         rtexture_t *texture_refraction;
155         rtexture_t *texture_reflection;
156         mplane_t plane;
157         int materialflags; // combined flags of all water surfaces on this plane
158         unsigned char pvsbits[(32768+7)>>3]; // FIXME: buffer overflow on huge maps
159         qboolean pvsvalid;
160 }
161 r_waterstate_waterplane_t;
162
163 #define MAX_WATERPLANES 16
164
165 static struct r_waterstate_s
166 {
167         qboolean enabled;
168
169         qboolean renderingscene; // true while rendering a refraction or reflection texture, disables water surfaces
170
171         int waterwidth, waterheight;
172         int texturewidth, textureheight;
173
174         int maxwaterplanes; // same as MAX_WATERPLANES
175         int numwaterplanes;
176         r_waterstate_waterplane_t waterplanes[MAX_WATERPLANES];
177
178         float screenscale[2];
179         float screencenter[2];
180 }
181 r_waterstate;
182
183 // shadow volume bsp struct with automatically growing nodes buffer
184 svbsp_t r_svbsp;
185
186 rtexture_t *r_texture_blanknormalmap;
187 rtexture_t *r_texture_white;
188 rtexture_t *r_texture_grey128;
189 rtexture_t *r_texture_black;
190 rtexture_t *r_texture_notexture;
191 rtexture_t *r_texture_whitecube;
192 rtexture_t *r_texture_normalizationcube;
193 rtexture_t *r_texture_fogattenuation;
194 //rtexture_t *r_texture_fogintensity;
195
196 char r_qwskincache[MAX_SCOREBOARD][MAX_QPATH];
197 skinframe_t *r_qwskincache_skinframe[MAX_SCOREBOARD];
198
199 // vertex coordinates for a quad that covers the screen exactly
200 const static float r_screenvertex3f[12] =
201 {
202         0, 0, 0,
203         1, 0, 0,
204         1, 1, 0,
205         0, 1, 0
206 };
207
208 extern void R_DrawModelShadows(void);
209
210 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
211 {
212         int i;
213         for (i = 0;i < verts;i++)
214         {
215                 out[0] = in[0] * r;
216                 out[1] = in[1] * g;
217                 out[2] = in[2] * b;
218                 out[3] = in[3];
219                 in += 4;
220                 out += 4;
221         }
222 }
223
224 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
225 {
226         int i;
227         for (i = 0;i < verts;i++)
228         {
229                 out[0] = r;
230                 out[1] = g;
231                 out[2] = b;
232                 out[3] = a;
233                 out += 4;
234         }
235 }
236
237 // FIXME: move this to client?
238 void FOG_clear(void)
239 {
240         if (gamemode == GAME_NEHAHRA)
241         {
242                 Cvar_Set("gl_fogenable", "0");
243                 Cvar_Set("gl_fogdensity", "0.2");
244                 Cvar_Set("gl_fogred", "0.3");
245                 Cvar_Set("gl_foggreen", "0.3");
246                 Cvar_Set("gl_fogblue", "0.3");
247         }
248         r_refdef.fog_density = r_refdef.fog_red = r_refdef.fog_green = r_refdef.fog_blue = 0.0f;
249 }
250
251 float FogPoint_World(const vec3_t p)
252 {
253         unsigned int fogmasktableindex = (unsigned int)(VectorDistance((p), r_view.origin) * r_refdef.fogmasktabledistmultiplier);
254         return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
255 }
256
257 float FogPoint_Model(const vec3_t p)
258 {
259         unsigned int fogmasktableindex = (unsigned int)(VectorDistance((p), rsurface.modelorg) * r_refdef.fogmasktabledistmultiplier);
260         return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
261 }
262
263 static void R_BuildBlankTextures(void)
264 {
265         unsigned char data[4];
266         data[2] = 128; // normal X
267         data[1] = 128; // normal Y
268         data[0] = 255; // normal Z
269         data[3] = 128; // height
270         r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
271         data[0] = 255;
272         data[1] = 255;
273         data[2] = 255;
274         data[3] = 255;
275         r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
276         data[0] = 128;
277         data[1] = 128;
278         data[2] = 128;
279         data[3] = 255;
280         r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
281         data[0] = 0;
282         data[1] = 0;
283         data[2] = 0;
284         data[3] = 255;
285         r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
286 }
287
288 static void R_BuildNoTexture(void)
289 {
290         int x, y;
291         unsigned char pix[16][16][4];
292         // this makes a light grey/dark grey checkerboard texture
293         for (y = 0;y < 16;y++)
294         {
295                 for (x = 0;x < 16;x++)
296                 {
297                         if ((y < 8) ^ (x < 8))
298                         {
299                                 pix[y][x][0] = 128;
300                                 pix[y][x][1] = 128;
301                                 pix[y][x][2] = 128;
302                                 pix[y][x][3] = 255;
303                         }
304                         else
305                         {
306                                 pix[y][x][0] = 64;
307                                 pix[y][x][1] = 64;
308                                 pix[y][x][2] = 64;
309                                 pix[y][x][3] = 255;
310                         }
311                 }
312         }
313         r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, NULL);
314 }
315
316 static void R_BuildWhiteCube(void)
317 {
318         unsigned char data[6*1*1*4];
319         memset(data, 255, sizeof(data));
320         r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
321 }
322
323 static void R_BuildNormalizationCube(void)
324 {
325         int x, y, side;
326         vec3_t v;
327         vec_t s, t, intensity;
328 #define NORMSIZE 64
329         unsigned char data[6][NORMSIZE][NORMSIZE][4];
330         for (side = 0;side < 6;side++)
331         {
332                 for (y = 0;y < NORMSIZE;y++)
333                 {
334                         for (x = 0;x < NORMSIZE;x++)
335                         {
336                                 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
337                                 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
338                                 switch(side)
339                                 {
340                                 default:
341                                 case 0:
342                                         v[0] = 1;
343                                         v[1] = -t;
344                                         v[2] = -s;
345                                         break;
346                                 case 1:
347                                         v[0] = -1;
348                                         v[1] = -t;
349                                         v[2] = s;
350                                         break;
351                                 case 2:
352                                         v[0] = s;
353                                         v[1] = 1;
354                                         v[2] = t;
355                                         break;
356                                 case 3:
357                                         v[0] = s;
358                                         v[1] = -1;
359                                         v[2] = -t;
360                                         break;
361                                 case 4:
362                                         v[0] = s;
363                                         v[1] = -t;
364                                         v[2] = 1;
365                                         break;
366                                 case 5:
367                                         v[0] = -s;
368                                         v[1] = -t;
369                                         v[2] = -1;
370                                         break;
371                                 }
372                                 intensity = 127.0f / sqrt(DotProduct(v, v));
373                                 data[side][y][x][2] = (unsigned char)(128.0f + intensity * v[0]);
374                                 data[side][y][x][1] = (unsigned char)(128.0f + intensity * v[1]);
375                                 data[side][y][x][0] = (unsigned char)(128.0f + intensity * v[2]);
376                                 data[side][y][x][3] = 255;
377                         }
378                 }
379         }
380         r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, &data[0][0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
381 }
382
383 static void R_BuildFogTexture(void)
384 {
385         int x, b;
386 #define FOGWIDTH 64
387         unsigned char data1[FOGWIDTH][4];
388         //unsigned char data2[FOGWIDTH][4];
389         for (x = 0;x < FOGWIDTH;x++)
390         {
391                 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
392                 data1[x][0] = b;
393                 data1[x][1] = b;
394                 data1[x][2] = b;
395                 data1[x][3] = 255;
396                 //data2[x][0] = 255 - b;
397                 //data2[x][1] = 255 - b;
398                 //data2[x][2] = 255 - b;
399                 //data2[x][3] = 255;
400         }
401         r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
402         //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
403 }
404
405 static const char *builtinshaderstring =
406 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
407 "// written by Forest 'LordHavoc' Hale\n"
408 "\n"
409 "// common definitions between vertex shader and fragment shader:\n"
410 "\n"
411 "#ifdef __GLSL_CG_DATA_TYPES\n"
412 "# define myhalf half\n"
413 "# define myhvec2 hvec2\n"
414 "# define myhvec3 hvec3\n"
415 "# define myhvec4 hvec4\n"
416 "#else\n"
417 "# define myhalf float\n"
418 "# define myhvec2 vec2\n"
419 "# define myhvec3 vec3\n"
420 "# define myhvec4 vec4\n"
421 "#endif\n"
422 "\n"
423 "varying vec2 TexCoord;\n"
424 "varying vec2 TexCoordLightmap;\n"
425 "\n"
426 "//#ifdef MODE_LIGHTSOURCE\n"
427 "varying vec3 CubeVector;\n"
428 "//#endif\n"
429 "\n"
430 "//#ifdef MODE_LIGHTSOURCE\n"
431 "varying vec3 LightVector;\n"
432 "//#else\n"
433 "//# ifdef MODE_LIGHTDIRECTION\n"
434 "//varying vec3 LightVector;\n"
435 "//# endif\n"
436 "//#endif\n"
437 "\n"
438 "varying vec3 EyeVector;\n"
439 "//#ifdef USEFOG\n"
440 "varying vec3 EyeVectorModelSpace;\n"
441 "//#endif\n"
442 "\n"
443 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
444 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
445 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
446 "\n"
447 "//#ifdef MODE_WATER\n"
448 "varying vec4 ModelViewProjectionPosition;\n"
449 "//#else\n"
450 "//# ifdef MODE_REFRACTION\n"
451 "//varying vec4 ModelViewProjectionPosition;\n"
452 "//# else\n"
453 "//#  ifdef USEREFLECTION\n"
454 "//varying vec4 ModelViewProjectionPosition;\n"
455 "//#  endif\n"
456 "//# endif\n"
457 "//#endif\n"
458 "\n"
459 "\n"
460 "\n"
461 "\n"
462 "\n"
463 "// vertex shader specific:\n"
464 "#ifdef VERTEX_SHADER\n"
465 "\n"
466 "uniform vec3 LightPosition;\n"
467 "uniform vec3 EyePosition;\n"
468 "uniform vec3 LightDir;\n"
469 "\n"
470 "// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3)\n"
471 "\n"
472 "void main(void)\n"
473 "{\n"
474 "       gl_FrontColor = gl_Color;\n"
475 "       // copy the surface texcoord\n"
476 "       TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);\n"
477 "#ifndef MODE_LIGHTSOURCE\n"
478 "# ifndef MODE_LIGHTDIRECTION\n"
479 "       TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
480 "# endif\n"
481 "#endif\n"
482 "\n"
483 "#ifdef MODE_LIGHTSOURCE\n"
484 "       // transform vertex position into light attenuation/cubemap space\n"
485 "       // (-1 to +1 across the light box)\n"
486 "       CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex);\n"
487 "\n"
488 "       // transform unnormalized light direction into tangent space\n"
489 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
490 "       //  normalize it per pixel)\n"
491 "       vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
492 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
493 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
494 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
495 "#endif\n"
496 "\n"
497 "#ifdef MODE_LIGHTDIRECTION\n"
498 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
499 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
500 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
501 "#endif\n"
502 "\n"
503 "       // transform unnormalized eye direction into tangent space\n"
504 "#ifndef USEFOG\n"
505 "       vec3 EyeVectorModelSpace;\n"
506 "#endif\n"
507 "       EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
508 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
509 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
510 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
511 "\n"
512 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
513 "       VectorS = gl_MultiTexCoord1.xyz;\n"
514 "       VectorT = gl_MultiTexCoord2.xyz;\n"
515 "       VectorR = gl_MultiTexCoord3.xyz;\n"
516 "#endif\n"
517 "\n"
518 "//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION)\n"
519 "//     ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix;\n"
520 "//     //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
521 "//     //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
522 "//#endif\n"
523 "\n"
524 "// transform vertex to camera space, using ftransform to match non-VS\n"
525 "       // rendering\n"
526 "       gl_Position = ftransform();\n"
527 "\n"
528 "#ifdef MODE_WATER\n"
529 "       ModelViewProjectionPosition = gl_Position;\n"
530 "#endif\n"
531 "#ifdef MODE_REFRACTION\n"
532 "       ModelViewProjectionPosition = gl_Position;\n"
533 "#endif\n"
534 "#ifdef USEREFLECTION\n"
535 "       ModelViewProjectionPosition = gl_Position;\n"
536 "#endif\n"
537 "}\n"
538 "\n"
539 "#endif // VERTEX_SHADER\n"
540 "\n"
541 "\n"
542 "\n"
543 "\n"
544 "// fragment shader specific:\n"
545 "#ifdef FRAGMENT_SHADER\n"
546 "\n"
547 "// 13 textures, we can only use up to 16 on DX9-class hardware\n"
548 "uniform sampler2D Texture_Normal;\n"
549 "uniform sampler2D Texture_Color;\n"
550 "uniform sampler2D Texture_Gloss;\n"
551 "uniform samplerCube Texture_Cube;\n"
552 "uniform sampler2D Texture_Attenuation;\n"
553 "uniform sampler2D Texture_FogMask;\n"
554 "uniform sampler2D Texture_Pants;\n"
555 "uniform sampler2D Texture_Shirt;\n"
556 "uniform sampler2D Texture_Lightmap;\n"
557 "uniform sampler2D Texture_Deluxemap;\n"
558 "uniform sampler2D Texture_Glow;\n"
559 "uniform sampler2D Texture_Reflection;\n"
560 "uniform sampler2D Texture_Refraction;\n"
561 "\n"
562 "uniform myhvec3 LightColor;\n"
563 "uniform myhvec3 AmbientColor;\n"
564 "uniform myhvec3 DiffuseColor;\n"
565 "uniform myhvec3 SpecularColor;\n"
566 "uniform myhvec3 Color_Pants;\n"
567 "uniform myhvec3 Color_Shirt;\n"
568 "uniform myhvec3 FogColor;\n"
569 "\n"
570 "//#ifdef MODE_WATER\n"
571 "uniform vec4 DistortScaleRefractReflect;\n"
572 "uniform vec4 ScreenScaleRefractReflect;\n"
573 "uniform vec4 ScreenCenterRefractReflect;\n"
574 "uniform myhvec4 RefractColor;\n"
575 "uniform myhvec4 ReflectColor;\n"
576 "uniform myhalf ReflectFactor;\n"
577 "uniform myhalf ReflectOffset;\n"
578 "//#else\n"
579 "//# ifdef MODE_REFRACTION\n"
580 "//uniform vec4 DistortScaleRefractReflect;\n"
581 "//uniform vec4 ScreenScaleRefractReflect;\n"
582 "//uniform vec4 ScreenCenterRefractReflect;\n"
583 "//uniform myhvec4 RefractColor;\n"
584 "//#  ifdef USEREFLECTION\n"
585 "//uniform myhvec4 ReflectColor;\n"
586 "//#  endif\n"
587 "//# else\n"
588 "//#  ifdef USEREFLECTION\n"
589 "//uniform vec4 DistortScaleRefractReflect;\n"
590 "//uniform vec4 ScreenScaleRefractReflect;\n"
591 "//uniform vec4 ScreenCenterRefractReflect;\n"
592 "//uniform myhvec4 ReflectColor;\n"
593 "//#  endif\n"
594 "//# endif\n"
595 "//#endif\n"
596 "\n"
597 "uniform myhalf GlowScale;\n"
598 "uniform myhalf SceneBrightness;\n"
599 "#ifdef USECONTRASTBOOST\n"
600 "uniform myhalf ContrastBoostCoeff;\n"
601 "#endif\n"
602 "\n"
603 "uniform float OffsetMapping_Scale;\n"
604 "uniform float OffsetMapping_Bias;\n"
605 "uniform float FogRangeRecip;\n"
606 "\n"
607 "uniform myhalf AmbientScale;\n"
608 "uniform myhalf DiffuseScale;\n"
609 "uniform myhalf SpecularScale;\n"
610 "uniform myhalf SpecularPower;\n"
611 "\n"
612 "#ifdef USEOFFSETMAPPING\n"
613 "vec2 OffsetMapping(vec2 TexCoord)\n"
614 "{\n"
615 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
616 "       // 14 sample relief mapping: linear search and then binary search\n"
617 "       // this basically steps forward a small amount repeatedly until it finds\n"
618 "       // itself inside solid, then jitters forward and back using decreasing\n"
619 "       // amounts to find the impact\n"
620 "       //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
621 "       //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
622 "       vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
623 "       vec3 RT = vec3(TexCoord, 1);\n"
624 "       OffsetVector *= 0.1;\n"
625 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
626 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
627 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
628 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
629 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
630 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
631 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
632 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
633 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
634 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
635 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
636 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
637 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
638 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
639 "       return RT.xy;\n"
640 "#else\n"
641 "       // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
642 "       // this basically moves forward the full distance, and then backs up based\n"
643 "       // on height of samples\n"
644 "       //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
645 "       //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
646 "       vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
647 "       TexCoord += OffsetVector;\n"
648 "       OffsetVector *= 0.333;\n"
649 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
650 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
651 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
652 "       return TexCoord;\n"
653 "#endif\n"
654 "}\n"
655 "#endif // USEOFFSETMAPPING\n"
656 "\n"
657 "#ifdef MODE_WATER\n"
658 "\n"
659 "// water pass\n"
660 "void main(void)\n"
661 "{\n"
662 "#ifdef USEOFFSETMAPPING\n"
663 "       // apply offsetmapping\n"
664 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
665 "#define TexCoord TexCoordOffset\n"
666 "#endif\n"
667 "\n"
668 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
669 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
670 "       vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec2(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
671 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 5.0) * ReflectFactor + ReflectOffset;\n"
672 "       gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel);\n"
673 "}\n"
674 "\n"
675 "#else // MODE_WATER\n"
676 "#ifdef MODE_REFRACTION\n"
677 "\n"
678 "// refraction pass\n"
679 "void main(void)\n"
680 "{\n"
681 "#ifdef USEOFFSETMAPPING\n"
682 "       // apply offsetmapping\n"
683 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
684 "#define TexCoord TexCoordOffset\n"
685 "#endif\n"
686 "\n"
687 "       vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
688 "       //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
689 "       vec2 ScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy + vec2(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
690 "       gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor;\n"
691 "}\n"
692 "\n"
693 "#else // MODE_REFRACTION\n"
694 "void main(void)\n"
695 "{\n"
696 "#ifdef USEOFFSETMAPPING\n"
697 "       // apply offsetmapping\n"
698 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
699 "#define TexCoord TexCoordOffset\n"
700 "#endif\n"
701 "\n"
702 "       // combine the diffuse textures (base, pants, shirt)\n"
703 "       myhvec4 color = myhvec4(texture2D(Texture_Color, TexCoord));\n"
704 "#ifdef USECOLORMAPPING\n"
705 "       color.rgb += myhvec3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhvec3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
706 "#endif\n"
707 "\n"
708 "\n"
709 "\n"
710 "\n"
711 "#ifdef MODE_LIGHTSOURCE\n"
712 "       // light source\n"
713 "\n"
714 "       // calculate surface normal, light normal, and specular normal\n"
715 "       // compute color intensity for the two textures (colormap and glossmap)\n"
716 "       // scale by light color and attenuation as efficiently as possible\n"
717 "       // (do as much scalar math as possible rather than vector math)\n"
718 "# ifdef USESPECULAR\n"
719 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
720 "       myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
721 "       myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
722 "\n"
723 "       // calculate directional shading\n"
724 "       color.rgb = LightColor * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * myhvec3(texture2D(Texture_Gloss, TexCoord)));\n"
725 "# else\n"
726 "#  ifdef USEDIFFUSE\n"
727 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
728 "       myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
729 "\n"
730 "       // calculate directional shading\n"
731 "       color.rgb = color.rgb * LightColor * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))));\n"
732 "#  else\n"
733 "       // calculate directionless shading\n"
734 "       color.rgb = color.rgb * LightColor * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
735 "#  endif\n"
736 "# endif\n"
737 "\n"
738 "# ifdef USECUBEFILTER\n"
739 "       // apply light cubemap filter\n"
740 "       //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector));\n"
741 "       color.rgb *= myhvec3(textureCube(Texture_Cube, CubeVector));\n"
742 "# endif\n"
743 "       color *= myhvec4(gl_Color);\n"
744 "#endif // MODE_LIGHTSOURCE\n"
745 "\n"
746 "\n"
747 "\n"
748 "\n"
749 "#ifdef MODE_LIGHTDIRECTION\n"
750 "       // directional model lighting\n"
751 "# ifdef USESPECULAR\n"
752 "       // get the surface normal and light normal\n"
753 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
754 "       myhvec3 diffusenormal = myhvec3(LightVector);\n"
755 "\n"
756 "       // calculate directional shading\n"
757 "       color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
758 "       myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
759 "       color.rgb += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
760 "# else\n"
761 "#  ifdef USEDIFFUSE\n"
762 "       // get the surface normal and light normal\n"
763 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
764 "       myhvec3 diffusenormal = myhvec3(LightVector);\n"
765 "\n"
766 "       // calculate directional shading\n"
767 "       color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
768 "#  else\n"
769 "       color.rgb *= AmbientColor;\n"
770 "#  endif\n"
771 "# endif\n"
772 "\n"
773 "       color *= myhvec4(gl_Color);\n"
774 "#endif // MODE_LIGHTDIRECTION\n"
775 "\n"
776 "\n"
777 "\n"
778 "\n"
779 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
780 "       // deluxemap lightmapping using light vectors in modelspace (evil q3map2)\n"
781 "\n"
782 "       // get the surface normal and light normal\n"
783 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
784 "\n"
785 "       myhvec3 diffusenormal_modelspace = myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5);\n"
786 "       myhvec3 diffusenormal = normalize(myhvec3(dot(diffusenormal_modelspace, myhvec3(VectorS)), dot(diffusenormal_modelspace, myhvec3(VectorT)), dot(diffusenormal_modelspace, myhvec3(VectorR))));\n"
787 "       // calculate directional shading\n"
788 "       myhvec3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)));\n"
789 "# ifdef USESPECULAR\n"
790 "       myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
791 "       tempcolor += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
792 "# endif\n"
793 "\n"
794 "       // apply lightmap color\n"
795 "       color.rgb = myhvec4(tempcolor,1) * myhvec4(texture2D(Texture_Lightmap, TexCoordLightmap)) * myhvec4(gl_Color) + myhvec4(color.rgb * AmbientScale, 0);\n"
796 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
797 "\n"
798 "\n"
799 "\n"
800 "\n"
801 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
802 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
803 "\n"
804 "       // get the surface normal and light normal\n"
805 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
806 "\n"
807 "       myhvec3 diffusenormal = normalize(myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5));\n"
808 "       // calculate directional shading\n"
809 "       myhvec3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)));\n"
810 "# ifdef USESPECULAR\n"
811 "       myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
812 "       tempcolor += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
813 "# endif\n"
814 "\n"
815 "       // apply lightmap color\n"
816 "       color = myhvec4(tempcolor, 1) * myhvec4(texture2D(Texture_Lightmap, TexCoordLightmap)) * myhvec4(gl_Color) + myhvec4(color.rgb * AmbientScale, 0);\n"
817 "#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
818 "\n"
819 "\n"
820 "\n"
821 "\n"
822 "#ifdef MODE_LIGHTMAP\n"
823 "       // apply lightmap color\n"
824 "       color *= myhvec4(texture2D(Texture_Lightmap, TexCoordLightmap)) * myhvec4(gl_Color) * myhvec4(myhvec3(DiffuseScale), 1) + myhvec4(myhvec3(AmbientScale), 0);\n"
825 "#endif // MODE_LIGHTMAP\n"
826 "\n"
827 "\n"
828 "\n"
829 "\n"
830 "\n"
831 "\n"
832 "\n"
833 "\n"
834 "#ifdef USEGLOW\n"
835 "       color.rgb += myhvec3(texture2D(Texture_Glow, TexCoord)) * GlowScale;\n"
836 "#endif\n"
837 "\n"
838 "#ifndef MODE_LIGHTSOURCE\n"
839 "# ifdef USEREFLECTION\n"
840 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
841 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
842 "       vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec3(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
843 "       color.rgb = mix(color.rgb, myhvec3(texture2D(Texture_Reflection, ScreenTexCoord.zw)) * ReflectColor.rgb, ReflectColor.a);\n"
844 "# endif\n"
845 "#endif\n"
846 "\n"
847 "#ifdef USEFOG\n"
848 "       // apply fog\n"
849 "       color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhvec2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0))));\n"
850 "#endif\n"
851 "\n"
852 "#ifdef USECONTRASTBOOST\n"
853 "       color.rgb = color.rgb / (ContrastBoostCoeff * color.rgb + myhvec3(1, 1, 1));\n"
854 "#endif\n"
855 "\n"
856 "       color.rgb *= SceneBrightness;\n"
857 "\n"
858 "       gl_FragColor = vec4(color);\n"
859 "}\n"
860 "#endif // MODE_REFRACTION\n"
861 "#endif // MODE_WATER\n"
862 "\n"
863 "#endif // FRAGMENT_SHADER\n"
864 ;
865
866 #define SHADERPERMUTATION_COLORMAPPING (1<<0) // indicates this is a colormapped skin
867 #define SHADERPERMUTATION_CONTRASTBOOST (1<<1) // r_glsl_contrastboost boosts the contrast at low color levels (similar to gamma)
868 #define SHADERPERMUTATION_FOG (1<<2) // tint the color by fog color or black if using additive blend mode
869 #define SHADERPERMUTATION_CUBEFILTER (1<<3) // (lightsource) use cubemap light filter
870 #define SHADERPERMUTATION_GLOW (1<<4) // (lightmap) blend in an additive glow texture
871 #define SHADERPERMUTATION_DIFFUSE (1<<5) // (lightsource) whether to use directional shading
872 #define SHADERPERMUTATION_SPECULAR (1<<6) // (lightsource or deluxemapping) render specular effects
873 #define SHADERPERMUTATION_REFLECTION (1<<7) // normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
874 #define SHADERPERMUTATION_OFFSETMAPPING (1<<8) // adjust texcoords to roughly simulate a displacement mapped surface
875 #define SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING (1<<9) // adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
876 #define SHADERPERMUTATION_MODEBASE (1<<10) // multiplier for the SHADERMODE_ values to get a valid index
877
878 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
879 const char *shaderpermutationinfo[][2] =
880 {
881         {"#define USECOLORMAPPING\n", " colormapping"},
882         {"#define USECONTRASTBOOST\n", " contrastboost"},
883         {"#define USEFOG\n", " fog"},
884         {"#define USECUBEFILTER\n", " cubefilter"},
885         {"#define USEGLOW\n", " glow"},
886         {"#define USEDIFFUSE\n", " diffuse"},
887         {"#define USESPECULAR\n", " specular"},
888         {"#define USEREFLECTION\n", " reflection"},
889         {"#define USEOFFSETMAPPING\n", " offsetmapping"},
890         {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
891         {NULL, NULL}
892 };
893
894 // this enum is multiplied by SHADERPERMUTATION_MODEBASE
895 typedef enum shadermode_e
896 {
897         SHADERMODE_LIGHTMAP, // (lightmap) use directional pixel shading from fixed light direction (q3bsp)
898         SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE, // (lightmap) use directional pixel shading from texture containing modelspace light directions (deluxemap)
899         SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE, // (lightmap) use directional pixel shading from texture containing tangentspace light directions (deluxemap)
900         SHADERMODE_LIGHTDIRECTION, // (lightmap) use directional pixel shading from fixed light direction (q3bsp)
901         SHADERMODE_LIGHTSOURCE, // (lightsource) use directional pixel shading from light source (rtlight)
902         SHADERMODE_REFRACTION, // refract background (the material is rendered normally after this pass)
903         SHADERMODE_WATER, // refract background and reflection (the material is rendered normally after this pass)
904         SHADERMODE_COUNT
905 }
906 shadermode_t;
907
908 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
909 const char *shadermodeinfo[][2] =
910 {
911         {"#define MODE_LIGHTMAP\n", " lightmap"},
912         {"#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
913         {"#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
914         {"#define MODE_LIGHTDIRECTION\n", " lightdirection"},
915         {"#define MODE_LIGHTSOURCE\n", " lightsource"},
916         {"#define MODE_REFRACTION\n", " refraction"},
917         {"#define MODE_WATER\n", " water"},
918         {NULL, NULL}
919 };
920
921 #define SHADERPERMUTATION_INDICES (SHADERPERMUTATION_MODEBASE * SHADERMODE_COUNT)
922
923 typedef struct r_glsl_permutation_s
924 {
925         // indicates if we have tried compiling this permutation already
926         qboolean compiled;
927         // 0 if compilation failed
928         int program;
929         // locations of detected uniforms in program object, or -1 if not found
930         int loc_Texture_Normal;
931         int loc_Texture_Color;
932         int loc_Texture_Gloss;
933         int loc_Texture_Cube;
934         int loc_Texture_Attenuation;
935         int loc_Texture_FogMask;
936         int loc_Texture_Pants;
937         int loc_Texture_Shirt;
938         int loc_Texture_Lightmap;
939         int loc_Texture_Deluxemap;
940         int loc_Texture_Glow;
941         int loc_Texture_Refraction;
942         int loc_Texture_Reflection;
943         int loc_FogColor;
944         int loc_LightPosition;
945         int loc_EyePosition;
946         int loc_LightColor;
947         int loc_Color_Pants;
948         int loc_Color_Shirt;
949         int loc_FogRangeRecip;
950         int loc_AmbientScale;
951         int loc_DiffuseScale;
952         int loc_SpecularScale;
953         int loc_SpecularPower;
954         int loc_GlowScale;
955         int loc_SceneBrightness; // or: Scenebrightness * ContrastBoost
956         int loc_OffsetMapping_Scale;
957         int loc_AmbientColor;
958         int loc_DiffuseColor;
959         int loc_SpecularColor;
960         int loc_LightDir;
961         int loc_ContrastBoostCoeff; // 1 - 1/ContrastBoost
962         int loc_DistortScaleRefractReflect;
963         int loc_ScreenScaleRefractReflect;
964         int loc_ScreenCenterRefractReflect;
965         int loc_RefractColor;
966         int loc_ReflectColor;
967         int loc_ReflectFactor;
968         int loc_ReflectOffset;
969 }
970 r_glsl_permutation_t;
971
972 // information about each possible shader permutation
973 r_glsl_permutation_t r_glsl_permutations[SHADERPERMUTATION_INDICES];
974 // currently selected permutation
975 r_glsl_permutation_t *r_glsl_permutation;
976
977 // these are additional flags used only by R_GLSL_CompilePermutation
978 #define SHADERTYPE_USES_VERTEXSHADER (1<<0)
979 #define SHADERTYPE_USES_GEOMETRYSHADER (1<<1)
980 #define SHADERTYPE_USES_FRAGMENTSHADER (1<<2)
981
982 static void R_GLSL_CompilePermutation(const char *filename, int permutation, int shadertype)
983 {
984         int i;
985         qboolean shaderfound;
986         r_glsl_permutation_t *p = r_glsl_permutations + permutation;
987         int vertstrings_count;
988         int geomstrings_count;
989         int fragstrings_count;
990         char *shaderstring;
991         const char *vertstrings_list[32+1];
992         const char *geomstrings_list[32+1];
993         const char *fragstrings_list[32+1];
994         char permutationname[256];
995         if (p->compiled)
996                 return;
997         p->compiled = true;
998         p->program = 0;
999         vertstrings_list[0] = "#define VERTEX_SHADER\n";
1000         geomstrings_list[0] = "#define GEOMETRY_SHADER\n";
1001         fragstrings_list[0] = "#define FRAGMENT_SHADER\n";
1002         vertstrings_count = 1;
1003         geomstrings_count = 1;
1004         fragstrings_count = 1;
1005         permutationname[0] = 0;
1006         i = permutation / SHADERPERMUTATION_MODEBASE;
1007         vertstrings_list[vertstrings_count++] = shadermodeinfo[i][0];
1008         geomstrings_list[geomstrings_count++] = shadermodeinfo[i][0];
1009         fragstrings_list[fragstrings_count++] = shadermodeinfo[i][0];
1010         strlcat(permutationname, shadermodeinfo[i][1], sizeof(permutationname));
1011         for (i = 0;shaderpermutationinfo[i][0];i++)
1012         {
1013                 if (permutation & (1<<i))
1014                 {
1015                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i][0];
1016                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i][0];
1017                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i][0];
1018                         strlcat(permutationname, shaderpermutationinfo[i][1], sizeof(permutationname));
1019                 }
1020                 else
1021                 {
1022                         // keep line numbers correct
1023                         vertstrings_list[vertstrings_count++] = "\n";
1024                         geomstrings_list[geomstrings_count++] = "\n";
1025                         fragstrings_list[fragstrings_count++] = "\n";
1026                 }
1027         }
1028         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
1029         shaderfound = false;
1030         if (shaderstring)
1031         {
1032                 Con_DPrint("from disk... ");
1033                 vertstrings_list[vertstrings_count++] = shaderstring;
1034                 geomstrings_list[geomstrings_count++] = shaderstring;
1035                 fragstrings_list[fragstrings_count++] = shaderstring;
1036                 shaderfound = true;
1037         }
1038         else if (!strcmp(filename, "glsl/default.glsl"))
1039         {
1040                 vertstrings_list[vertstrings_count++] = builtinshaderstring;
1041                 geomstrings_list[geomstrings_count++] = builtinshaderstring;
1042                 fragstrings_list[fragstrings_count++] = builtinshaderstring;
1043                 shaderfound = true;
1044         }
1045         // clear any lists that are not needed by this shader
1046         if (!(shadertype & SHADERTYPE_USES_VERTEXSHADER))
1047                 vertstrings_count = 0;
1048         if (!(shadertype & SHADERTYPE_USES_GEOMETRYSHADER))
1049                 geomstrings_count = 0;
1050         if (!(shadertype & SHADERTYPE_USES_FRAGMENTSHADER))
1051                 fragstrings_count = 0;
1052         // compile the shader program
1053         if (shaderfound && vertstrings_count + geomstrings_count + fragstrings_count)
1054                 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1055         if (p->program)
1056         {
1057                 CHECKGLERROR
1058                 qglUseProgramObjectARB(p->program);CHECKGLERROR
1059                 // look up all the uniform variable names we care about, so we don't
1060                 // have to look them up every time we set them
1061                 p->loc_Texture_Normal      = qglGetUniformLocationARB(p->program, "Texture_Normal");
1062                 p->loc_Texture_Color       = qglGetUniformLocationARB(p->program, "Texture_Color");
1063                 p->loc_Texture_Gloss       = qglGetUniformLocationARB(p->program, "Texture_Gloss");
1064                 p->loc_Texture_Cube        = qglGetUniformLocationARB(p->program, "Texture_Cube");
1065                 p->loc_Texture_Attenuation = qglGetUniformLocationARB(p->program, "Texture_Attenuation");
1066                 p->loc_Texture_FogMask     = qglGetUniformLocationARB(p->program, "Texture_FogMask");
1067                 p->loc_Texture_Pants       = qglGetUniformLocationARB(p->program, "Texture_Pants");
1068                 p->loc_Texture_Shirt       = qglGetUniformLocationARB(p->program, "Texture_Shirt");
1069                 p->loc_Texture_Lightmap    = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
1070                 p->loc_Texture_Deluxemap   = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
1071                 p->loc_Texture_Glow        = qglGetUniformLocationARB(p->program, "Texture_Glow");
1072                 p->loc_Texture_Refraction  = qglGetUniformLocationARB(p->program, "Texture_Refraction");
1073                 p->loc_Texture_Reflection  = qglGetUniformLocationARB(p->program, "Texture_Reflection");
1074                 p->loc_FogColor            = qglGetUniformLocationARB(p->program, "FogColor");
1075                 p->loc_LightPosition       = qglGetUniformLocationARB(p->program, "LightPosition");
1076                 p->loc_EyePosition         = qglGetUniformLocationARB(p->program, "EyePosition");
1077                 p->loc_LightColor          = qglGetUniformLocationARB(p->program, "LightColor");
1078                 p->loc_Color_Pants         = qglGetUniformLocationARB(p->program, "Color_Pants");
1079                 p->loc_Color_Shirt         = qglGetUniformLocationARB(p->program, "Color_Shirt");
1080                 p->loc_FogRangeRecip       = qglGetUniformLocationARB(p->program, "FogRangeRecip");
1081                 p->loc_AmbientScale        = qglGetUniformLocationARB(p->program, "AmbientScale");
1082                 p->loc_DiffuseScale        = qglGetUniformLocationARB(p->program, "DiffuseScale");
1083                 p->loc_SpecularPower       = qglGetUniformLocationARB(p->program, "SpecularPower");
1084                 p->loc_SpecularScale       = qglGetUniformLocationARB(p->program, "SpecularScale");
1085                 p->loc_GlowScale           = qglGetUniformLocationARB(p->program, "GlowScale");
1086                 p->loc_SceneBrightness     = qglGetUniformLocationARB(p->program, "SceneBrightness");
1087                 p->loc_OffsetMapping_Scale = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
1088                 p->loc_AmbientColor        = qglGetUniformLocationARB(p->program, "AmbientColor");
1089                 p->loc_DiffuseColor        = qglGetUniformLocationARB(p->program, "DiffuseColor");
1090                 p->loc_SpecularColor       = qglGetUniformLocationARB(p->program, "SpecularColor");
1091                 p->loc_LightDir            = qglGetUniformLocationARB(p->program, "LightDir");
1092                 p->loc_ContrastBoostCoeff  = qglGetUniformLocationARB(p->program, "ContrastBoostCoeff");
1093                 p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
1094                 p->loc_ScreenScaleRefractReflect = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
1095                 p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
1096                 p->loc_RefractColor        = qglGetUniformLocationARB(p->program, "RefractColor");
1097                 p->loc_ReflectColor        = qglGetUniformLocationARB(p->program, "ReflectColor");
1098                 p->loc_ReflectFactor       = qglGetUniformLocationARB(p->program, "ReflectFactor");
1099                 p->loc_ReflectOffset       = qglGetUniformLocationARB(p->program, "ReflectOffset");
1100                 // initialize the samplers to refer to the texture units we use
1101                 if (p->loc_Texture_Normal >= 0)    qglUniform1iARB(p->loc_Texture_Normal, 0);
1102                 if (p->loc_Texture_Color >= 0)     qglUniform1iARB(p->loc_Texture_Color, 1);
1103                 if (p->loc_Texture_Gloss >= 0)     qglUniform1iARB(p->loc_Texture_Gloss, 2);
1104                 if (p->loc_Texture_Cube >= 0)      qglUniform1iARB(p->loc_Texture_Cube, 3);
1105                 if (p->loc_Texture_FogMask >= 0)   qglUniform1iARB(p->loc_Texture_FogMask, 4);
1106                 if (p->loc_Texture_Pants >= 0)     qglUniform1iARB(p->loc_Texture_Pants, 5);
1107                 if (p->loc_Texture_Shirt >= 0)     qglUniform1iARB(p->loc_Texture_Shirt, 6);
1108                 if (p->loc_Texture_Lightmap >= 0)  qglUniform1iARB(p->loc_Texture_Lightmap, 7);
1109                 if (p->loc_Texture_Deluxemap >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap, 8);
1110                 if (p->loc_Texture_Glow >= 0)      qglUniform1iARB(p->loc_Texture_Glow, 9);
1111                 if (p->loc_Texture_Attenuation >= 0) qglUniform1iARB(p->loc_Texture_Attenuation, 10);
1112                 if (p->loc_Texture_Refraction >= 0) qglUniform1iARB(p->loc_Texture_Refraction, 11);
1113                 if (p->loc_Texture_Reflection >= 0) qglUniform1iARB(p->loc_Texture_Reflection, 12);
1114                 CHECKGLERROR
1115                 qglUseProgramObjectARB(0);CHECKGLERROR
1116                 if (developer.integer)
1117                         Con_Printf("GLSL shader %s :%s compiled.\n", filename, permutationname);
1118         }
1119         else
1120         {
1121                 if (developer.integer)
1122                         Con_Printf("GLSL shader %s :%s failed!  source code line offset for above errors is %i.\n", permutationname, filename, -(vertstrings_count - 1));
1123                 else
1124                         Con_Printf("GLSL shader %s :%s failed!  some features may not work properly.\n", permutationname, filename);
1125         }
1126         if (shaderstring)
1127                 Mem_Free(shaderstring);
1128 }
1129
1130 void R_GLSL_Restart_f(void)
1131 {
1132         int i;
1133         for (i = 0;i < SHADERPERMUTATION_INDICES;i++)
1134                 if (r_glsl_permutations[i].program)
1135                         GL_Backend_FreeProgram(r_glsl_permutations[i].program);
1136         memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
1137 }
1138
1139 void R_GLSL_DumpShader_f(void)
1140 {
1141         int i;
1142
1143         qfile_t *file = FS_Open("glsl/default.glsl", "w", false, false);
1144         if(!file)
1145         {
1146                 Con_Printf("failed to write to glsl/default.glsl\n");
1147                 return;
1148         }
1149
1150         FS_Print(file, "// The engine may define the following macros:\n");
1151         FS_Print(file, "// #define VERTEX_SHADER\n// #define GEOMETRY_SHADER\n// #define FRAGMENT_SHADER\n");
1152         for (i = 0;shadermodeinfo[i][0];i++)
1153                 FS_Printf(file, "// %s", shadermodeinfo[i][0]);
1154         for (i = 0;shaderpermutationinfo[i][0];i++)
1155                 FS_Printf(file, "// %s", shaderpermutationinfo[i][0]);
1156         FS_Print(file, "\n");
1157         FS_Print(file, builtinshaderstring);
1158         FS_Close(file);
1159
1160         Con_Printf("glsl/default.glsl written\n");
1161 }
1162
1163 extern rtexture_t *r_shadow_attenuationgradienttexture;
1164 extern rtexture_t *r_shadow_attenuation2dtexture;
1165 extern rtexture_t *r_shadow_attenuation3dtexture;
1166 int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, float ambientscale, float diffusescale, float specularscale, rsurfacepass_t rsurfacepass)
1167 {
1168         // select a permutation of the lighting shader appropriate to this
1169         // combination of texture, entity, light source, and fogging, only use the
1170         // minimum features necessary to avoid wasting rendering time in the
1171         // fragment shader on features that are not being used
1172         const char *shaderfilename = NULL;
1173         unsigned int permutation = 0;
1174         unsigned int shadertype = 0;
1175         shadermode_t mode = 0;
1176         r_glsl_permutation = NULL;
1177         shaderfilename = "glsl/default.glsl";
1178         shadertype = SHADERTYPE_USES_VERTEXSHADER | SHADERTYPE_USES_FRAGMENTSHADER;
1179         // TODO: implement geometry-shader based shadow volumes someday
1180         if (r_glsl_offsetmapping.integer)
1181         {
1182                 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
1183                 if (r_glsl_offsetmapping_reliefmapping.integer)
1184                         permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
1185         }
1186         if (rsurfacepass == RSURFPASS_BACKGROUND)
1187         {
1188                 // distorted background
1189                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
1190                         mode = SHADERMODE_WATER;
1191                 else
1192                         mode = SHADERMODE_REFRACTION;
1193         }
1194         else if (rsurfacepass == RSURFPASS_RTLIGHT)
1195         {
1196                 // light source
1197                 mode = SHADERMODE_LIGHTSOURCE;
1198                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1199                         permutation |= SHADERPERMUTATION_CUBEFILTER;
1200                 if (diffusescale > 0)
1201                         permutation |= SHADERPERMUTATION_DIFFUSE;
1202                 if (specularscale > 0)
1203                         permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1204                 if (r_refdef.fogenabled)
1205                         permutation |= SHADERPERMUTATION_FOG;
1206                 if (rsurface.texture->colormapping)
1207                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1208                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1209                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1210                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1211                         permutation |= SHADERPERMUTATION_REFLECTION;
1212         }
1213         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
1214         {
1215                 // unshaded geometry (fullbright or ambient model lighting)
1216                 mode = SHADERMODE_LIGHTMAP;
1217                 if (rsurface.texture->currentskinframe->glow)
1218                         permutation |= SHADERPERMUTATION_GLOW;
1219                 if (r_refdef.fogenabled)
1220                         permutation |= SHADERPERMUTATION_FOG;
1221                 if (rsurface.texture->colormapping)
1222                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1223                 if (r_glsl_offsetmapping.integer)
1224                 {
1225                         permutation |= SHADERPERMUTATION_OFFSETMAPPING;
1226                         if (r_glsl_offsetmapping_reliefmapping.integer)
1227                                 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
1228                 }
1229                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1230                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1231                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1232                         permutation |= SHADERPERMUTATION_REFLECTION;
1233         }
1234         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT_DIRECTIONAL)
1235         {
1236                 // directional model lighting
1237                 mode = SHADERMODE_LIGHTDIRECTION;
1238                 if (rsurface.texture->currentskinframe->glow)
1239                         permutation |= SHADERPERMUTATION_GLOW;
1240                 permutation |= SHADERPERMUTATION_DIFFUSE;
1241                 if (specularscale > 0)
1242                         permutation |= SHADERPERMUTATION_SPECULAR;
1243                 if (r_refdef.fogenabled)
1244                         permutation |= SHADERPERMUTATION_FOG;
1245                 if (rsurface.texture->colormapping)
1246                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1247                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1248                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1249                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1250                         permutation |= SHADERPERMUTATION_REFLECTION;
1251         }
1252         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
1253         {
1254                 // ambient model lighting
1255                 mode = SHADERMODE_LIGHTDIRECTION;
1256                 if (rsurface.texture->currentskinframe->glow)
1257                         permutation |= SHADERPERMUTATION_GLOW;
1258                 if (r_refdef.fogenabled)
1259                         permutation |= SHADERPERMUTATION_FOG;
1260                 if (rsurface.texture->colormapping)
1261                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1262                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1263                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1264                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1265                         permutation |= SHADERPERMUTATION_REFLECTION;
1266         }
1267         else
1268         {
1269                 // lightmapped wall
1270                 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping)
1271                 {
1272                         // deluxemapping (light direction texture)
1273                         if (rsurface.uselightmaptexture && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping && r_refdef.worldmodel->brushq3.deluxemapping_modelspace)
1274                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
1275                         else
1276                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1277                         if (specularscale > 0)
1278                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1279                 }
1280                 else if (r_glsl_deluxemapping.integer >= 2)
1281                 {
1282                         // fake deluxemapping (uniform light direction in tangentspace)
1283                         mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1284                         if (specularscale > 0)
1285                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1286                 }
1287                 else
1288                 {
1289                         // ordinary lightmapping
1290                         mode = SHADERMODE_LIGHTMAP;
1291                 }
1292                 if (rsurface.texture->currentskinframe->glow)
1293                         permutation |= SHADERPERMUTATION_GLOW;
1294                 if (r_refdef.fogenabled)
1295                         permutation |= SHADERPERMUTATION_FOG;
1296                 if (rsurface.texture->colormapping)
1297                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1298                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1299                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1300                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1301                         permutation |= SHADERPERMUTATION_REFLECTION;
1302         }
1303         permutation |= mode * SHADERPERMUTATION_MODEBASE;
1304         if (!r_glsl_permutations[permutation].program)
1305         {
1306                 if (!r_glsl_permutations[permutation].compiled)
1307                         R_GLSL_CompilePermutation(shaderfilename, permutation, shadertype);
1308                 if (!r_glsl_permutations[permutation].program)
1309                 {
1310                         // remove features until we find a valid permutation
1311                         unsigned int i;
1312                         for (i = (SHADERPERMUTATION_MODEBASE >> 1);;i>>=1)
1313                         {
1314                                 if (!i)
1315                                 {
1316                                         Con_Printf("OpenGL 2.0 shaders disabled - unable to find a working shader permutation fallback on this driver (set r_glsl 1 if you want to try again)\n");
1317                                         Cvar_SetValueQuick(&r_glsl, 0);
1318                                         return 0; // no bit left to clear
1319                                 }
1320                                 // reduce i more quickly whenever it would not remove any bits
1321                                 if (!(permutation & i))
1322                                         continue;
1323                                 permutation &= ~i;
1324                                 if (!r_glsl_permutations[permutation].compiled)
1325                                         R_GLSL_CompilePermutation(shaderfilename, permutation, shadertype);
1326                                 if (r_glsl_permutations[permutation].program)
1327                                         break;
1328                         }
1329                 }
1330         }
1331         r_glsl_permutation = r_glsl_permutations + permutation;
1332         CHECKGLERROR
1333         qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
1334         if (mode == SHADERMODE_LIGHTSOURCE)
1335         {
1336                 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
1337                 if (permutation & SHADERPERMUTATION_DIFFUSE)
1338                 {
1339                         if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
1340                         if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, ambientscale);
1341                         if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, diffusescale);
1342                         if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, specularscale);
1343                 }
1344                 else
1345                 {
1346                         // ambient only is simpler
1347                         if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0] * ambientscale, lightcolorbase[1] * ambientscale, lightcolorbase[2] * ambientscale);
1348                         if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, 1);
1349                         if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, 0);
1350                         if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, 0);
1351                 }
1352         }
1353         else if (mode == SHADERMODE_LIGHTDIRECTION)
1354         {
1355                 if (r_glsl_permutation->loc_AmbientColor >= 0)
1356                         qglUniform3fARB(r_glsl_permutation->loc_AmbientColor, rsurface.modellight_ambient[0] * ambientscale, rsurface.modellight_ambient[1] * ambientscale, rsurface.modellight_ambient[2] * ambientscale);
1357                 if (r_glsl_permutation->loc_DiffuseColor >= 0)
1358                         qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor, rsurface.modellight_diffuse[0] * diffusescale, rsurface.modellight_diffuse[1] * diffusescale, rsurface.modellight_diffuse[2] * diffusescale);
1359                 if (r_glsl_permutation->loc_SpecularColor >= 0)
1360                         qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface.modellight_diffuse[0] * specularscale, rsurface.modellight_diffuse[1] * specularscale, rsurface.modellight_diffuse[2] * specularscale);
1361                 if (r_glsl_permutation->loc_LightDir >= 0)
1362                         qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
1363         }
1364         else
1365         {
1366                 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, r_ambient.value * 2.0f / 128.0f);
1367                 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, r_refdef.lightmapintensity * 2.0f);
1368                 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, r_refdef.lightmapintensity * specularscale * 2.0f);
1369         }
1370         if (r_glsl_permutation->loc_GlowScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_GlowScale, r_hdr_glowintensity.value);
1371         if (r_glsl_permutation->loc_ContrastBoostCoeff >= 0)
1372         {
1373                 // The formula used is actually:
1374                 //   color.rgb *= SceneBrightness;
1375                 //   color.rgb *= ContrastBoost / ((ContrastBoost - 1) * color.rgb + 1);
1376                 // I simplify that to
1377                 //   color.rgb *= [[SceneBrightness * ContrastBoost]];
1378                 //   color.rgb /= [[(ContrastBoost - 1) / ContrastBoost]] * color.rgb + 1;
1379                 // and Black:
1380                 //   color.rgb = [[SceneBrightness * ContrastBoost]] * color.rgb / ([[(ContrastBoost - 1) * SceneBrightness]] * color.rgb + 1);
1381                 // and do [[calculations]] here in the engine
1382                 qglUniform1fARB(r_glsl_permutation->loc_ContrastBoostCoeff, (r_glsl_contrastboost.value - 1) * r_view.colorscale);
1383                 if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale * r_glsl_contrastboost.value);
1384         }
1385         else
1386                 if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale);
1387         if (r_glsl_permutation->loc_FogColor >= 0)
1388         {
1389                 // additive passes are only darkened by fog, not tinted
1390                 if (rsurface.rtlight || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD))
1391                         qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1392                 else
1393                 /*
1394                 {
1395                         vec3_t fogvec;
1396                         //   color.rgb *= SceneBrightness;
1397                         VectorScale(r_refdef.fogcolor, r_view.colorscale, fogvec);
1398                         if(r_glsl_permutation->loc_ContrastBoostCoeff >= 0) // need to support contrast boost
1399                         {
1400                                 //   color.rgb *= ContrastBoost / ((ContrastBoost - 1) * color.rgb + 1);
1401                                 fogvec[0] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[0] + 1);
1402                                 fogvec[1] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[1] + 1);
1403                                 fogvec[2] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[2] + 1);
1404                         }
1405                         qglUniform3fARB(r_glsl_permutation->loc_FogColor, fogvec[0], fogvec[1], fogvec[2]);
1406                 }
1407                 */
1408                         qglUniform3fARB(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
1409         }
1410         if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, rsurface.modelorg[0], rsurface.modelorg[1], rsurface.modelorg[2]);
1411         if (r_glsl_permutation->loc_Color_Pants >= 0)
1412         {
1413                 if (rsurface.texture->currentskinframe->pants)
1414                         qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]);
1415                 else
1416                         qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
1417         }
1418         if (r_glsl_permutation->loc_Color_Shirt >= 0)
1419         {
1420                 if (rsurface.texture->currentskinframe->shirt)
1421                         qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]);
1422                 else
1423                         qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
1424         }
1425         if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, r_refdef.fograngerecip);
1426         if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower);
1427         if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
1428         if (r_glsl_permutation->loc_DistortScaleRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_DistortScaleRefractReflect, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor);
1429         if (r_glsl_permutation->loc_ScreenScaleRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_ScreenScaleRefractReflect, r_waterstate.screenscale[0], r_waterstate.screenscale[1], r_waterstate.screenscale[0], r_waterstate.screenscale[1]);
1430         if (r_glsl_permutation->loc_ScreenCenterRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_ScreenCenterRefractReflect, r_waterstate.screencenter[0], r_waterstate.screencenter[1], r_waterstate.screencenter[0], r_waterstate.screencenter[1]);
1431         if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_RefractColor, 1, rsurface.texture->refractcolor4f);
1432         if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_ReflectColor, 1, rsurface.texture->reflectcolor4f);
1433         if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
1434         if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectOffset, rsurface.texture->reflectmin);
1435         CHECKGLERROR
1436         return permutation;
1437 }
1438
1439 #define SKINFRAME_HASH 1024
1440
1441 struct
1442 {
1443         int loadsequence; // incremented each level change
1444         memexpandablearray_t array;
1445         skinframe_t *hash[SKINFRAME_HASH];
1446 }
1447 r_skinframe;
1448
1449 void R_SkinFrame_PrepareForPurge(void)
1450 {
1451         r_skinframe.loadsequence++;
1452         // wrap it without hitting zero
1453         if (r_skinframe.loadsequence >= 200)
1454                 r_skinframe.loadsequence = 1;
1455 }
1456
1457 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
1458 {
1459         if (!skinframe)
1460                 return;
1461         // mark the skinframe as used for the purging code
1462         skinframe->loadsequence = r_skinframe.loadsequence;
1463 }
1464
1465 void R_SkinFrame_Purge(void)
1466 {
1467         int i;
1468         skinframe_t *s;
1469         for (i = 0;i < SKINFRAME_HASH;i++)
1470         {
1471                 for (s = r_skinframe.hash[i];s;s = s->next)
1472                 {
1473                         if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
1474                         {
1475                                 if (s->merged == s->base)
1476                                         s->merged = NULL;
1477                                 // FIXME: maybe pass a pointer to the pointer to R_PurgeTexture and reset it to NULL inside? [11/29/2007 Black]
1478                                 R_PurgeTexture(s->stain );s->stain  = NULL;
1479                                 R_PurgeTexture(s->merged);s->merged = NULL;
1480                                 R_PurgeTexture(s->base  );s->base   = NULL;
1481                                 R_PurgeTexture(s->pants );s->pants  = NULL;
1482                                 R_PurgeTexture(s->shirt );s->shirt  = NULL;
1483                                 R_PurgeTexture(s->nmap  );s->nmap   = NULL;
1484                                 R_PurgeTexture(s->gloss );s->gloss  = NULL;
1485                                 R_PurgeTexture(s->glow  );s->glow   = NULL;
1486                                 R_PurgeTexture(s->fog   );s->fog    = NULL;
1487                                 s->loadsequence = 0;
1488                         }
1489                 }
1490         }
1491 }
1492
1493 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
1494         skinframe_t *item;
1495         char basename[MAX_QPATH];
1496
1497         Image_StripImageExtension(name, basename, sizeof(basename));
1498
1499         if( last == NULL ) {
1500                 int hashindex;
1501                 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
1502                 item = r_skinframe.hash[hashindex];
1503         } else {
1504                 item = last->next;
1505         }
1506
1507         // linearly search through the hash bucket
1508         for( ; item ; item = item->next ) {
1509                 if( !strcmp( item->basename, basename ) ) {
1510                         return item;
1511                 }
1512         }
1513         return NULL;
1514 }
1515
1516 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qboolean add)
1517 {
1518         skinframe_t *item;
1519         int hashindex;
1520         char basename[MAX_QPATH];
1521         
1522         Image_StripImageExtension(name, basename, sizeof(basename));
1523
1524         hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
1525         for (item = r_skinframe.hash[hashindex];item;item = item->next)
1526                 if (!strcmp(item->basename, basename) && item->textureflags == textureflags && item->comparewidth == comparewidth && item->compareheight == compareheight && item->comparecrc == comparecrc)
1527                         break;
1528
1529         if (!item) {
1530                 rtexture_t *dyntexture;
1531                 // check whether its a dynamic texture
1532                 dyntexture = CL_GetDynTexture( basename );
1533                 if (!add && !dyntexture)
1534                         return NULL;
1535                 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
1536                 memset(item, 0, sizeof(*item));
1537                 strlcpy(item->basename, basename, sizeof(item->basename));
1538                 item->base = dyntexture; // either NULL or dyntexture handle
1539                 item->textureflags = textureflags;
1540                 item->comparewidth = comparewidth;
1541                 item->compareheight = compareheight;
1542                 item->comparecrc = comparecrc;
1543                 item->next = r_skinframe.hash[hashindex];       
1544                 r_skinframe.hash[hashindex] = item;
1545         }
1546         else if( item->base == NULL )
1547         {
1548                 rtexture_t *dyntexture;
1549                 // check whether its a dynamic texture
1550                 // this only needs to be done because Purge doesnt delete skinframes - only sets the texture pointers to NULL and we need to restore it before returing.. [11/29/2007 Black]
1551                 dyntexture = CL_GetDynTexture( basename );
1552                 item->base = dyntexture; // either NULL or dyntexture handle
1553         }
1554
1555         R_SkinFrame_MarkUsed(item);
1556         return item;
1557 }
1558
1559 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboolean complain)
1560 {
1561         // FIXME: it should be possible to disable loading various layers using
1562         // cvars, to prevent wasted loading time and memory usage if the user does
1563         // not want them
1564         qboolean loadnormalmap = true;
1565         qboolean loadgloss = true;
1566         qboolean loadpantsandshirt = true;
1567         qboolean loadglow = true;
1568         int j;
1569         unsigned char *pixels;
1570         unsigned char *bumppixels;
1571         unsigned char *basepixels = NULL;
1572         int basepixels_width;
1573         int basepixels_height;
1574         skinframe_t *skinframe;
1575
1576         if (cls.state == ca_dedicated)
1577                 return NULL;
1578
1579         // return an existing skinframe if already loaded
1580         // if loading of the first image fails, don't make a new skinframe as it
1581         // would cause all future lookups of this to be missing
1582         skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
1583         if (skinframe && skinframe->base)
1584                 return skinframe;
1585
1586         basepixels = loadimagepixelsbgra(name, complain, true);
1587         if (basepixels == NULL)
1588                 return NULL;
1589
1590         // we've got some pixels to store, so really allocate this new texture now
1591         if (!skinframe)
1592                 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
1593         skinframe->stain = NULL;
1594         skinframe->merged = NULL;
1595         skinframe->base = r_texture_notexture;
1596         skinframe->pants = NULL;
1597         skinframe->shirt = NULL;
1598         skinframe->nmap = r_texture_blanknormalmap;
1599         skinframe->gloss = NULL;
1600         skinframe->glow = NULL;
1601         skinframe->fog = NULL;
1602
1603         basepixels_width = image_width;
1604         basepixels_height = image_height;
1605         skinframe->base = R_LoadTexture2D (r_main_texturepool, skinframe->basename, basepixels_width, basepixels_height, basepixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1606
1607         if (textureflags & TEXF_ALPHA)
1608         {
1609                 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
1610                         if (basepixels[j] < 255)
1611                                 break;
1612                 if (j < basepixels_width * basepixels_height * 4)
1613                 {
1614                         // has transparent pixels
1615                         pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
1616                         for (j = 0;j < image_width * image_height * 4;j += 4)
1617                         {
1618                                 pixels[j+0] = 255;
1619                                 pixels[j+1] = 255;
1620                                 pixels[j+2] = 255;
1621                                 pixels[j+3] = basepixels[j+3];
1622                         }
1623                         skinframe->fog = R_LoadTexture2D (r_main_texturepool, va("%s_mask", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1624                         Mem_Free(pixels);
1625                 }
1626         }
1627
1628         // _norm is the name used by tenebrae and has been adopted as standard
1629         if (loadnormalmap)
1630         {
1631                 if ((pixels = loadimagepixelsbgra(va("%s_norm", skinframe->basename), false, false)) != NULL)
1632                 {
1633                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1634                         Mem_Free(pixels);
1635                         pixels = NULL;
1636                 }
1637                 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va("%s_bump", skinframe->basename), false, false)) != NULL)
1638                 {
1639                         pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
1640                         Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
1641                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1642                         Mem_Free(pixels);
1643                         Mem_Free(bumppixels);
1644                 }
1645                 else if (r_shadow_bumpscale_basetexture.value > 0)
1646                 {
1647                         pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
1648                         Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
1649                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), basepixels_width, basepixels_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1650                         Mem_Free(pixels);
1651                 }
1652         }
1653         // _luma is supported for tenebrae compatibility
1654         // (I think it's a very stupid name, but oh well)
1655         // _glow is the preferred name
1656         if (loadglow          && ((pixels = loadimagepixelsbgra(va("%s_glow", skinframe->basename), false, false)) != NULL || (pixels = loadimagepixelsbgra(va("%s_luma", skinframe->basename), false, false)) != NULL)) {skinframe->glow = R_LoadTexture2D (r_main_texturepool, va("%s_glow", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_glow.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
1657         if (loadgloss         && (pixels = loadimagepixelsbgra(va("%s_gloss", skinframe->basename), false, false)) != NULL) {skinframe->gloss = R_LoadTexture2D (r_main_texturepool, va("%s_gloss", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_gloss.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
1658         if (loadpantsandshirt && (pixels = loadimagepixelsbgra(va("%s_pants", skinframe->basename), false, false)) != NULL) {skinframe->pants = R_LoadTexture2D (r_main_texturepool, va("%s_pants", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
1659         if (loadpantsandshirt && (pixels = loadimagepixelsbgra(va("%s_shirt", skinframe->basename), false, false)) != NULL) {skinframe->shirt = R_LoadTexture2D (r_main_texturepool, va("%s_shirt", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
1660
1661         if (basepixels)
1662                 Mem_Free(basepixels);
1663
1664         return skinframe;
1665 }
1666
1667 static rtexture_t *R_SkinFrame_TextureForSkinLayer(const unsigned char *in, int width, int height, const char *name, const unsigned int *palette, int textureflags, qboolean force)
1668 {
1669         int i;
1670         if (!force)
1671         {
1672                 for (i = 0;i < width*height;i++)
1673                         if (((unsigned char *)&palette[in[i]])[3] > 0)
1674                                 break;
1675                 if (i == width*height)
1676                         return NULL;
1677         }
1678         return R_LoadTexture2D (r_main_texturepool, name, width, height, in, TEXTYPE_PALETTE, textureflags, palette);
1679 }
1680
1681 // this is only used by .spr32 sprites, HL .spr files, HL .bsp files
1682 skinframe_t *R_SkinFrame_LoadInternalBGRA(const char *name, int textureflags, const unsigned char *skindata, int width, int height)
1683 {
1684         int i;
1685         unsigned char *temp1, *temp2;
1686         skinframe_t *skinframe;
1687
1688         if (cls.state == ca_dedicated)
1689                 return NULL;
1690
1691         // if already loaded just return it, otherwise make a new skinframe
1692         skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height*4) : 0, true);
1693         if (skinframe && skinframe->base)
1694                 return skinframe;
1695
1696         skinframe->stain = NULL;
1697         skinframe->merged = NULL;
1698         skinframe->base = r_texture_notexture;
1699         skinframe->pants = NULL;
1700         skinframe->shirt = NULL;
1701         skinframe->nmap = r_texture_blanknormalmap;
1702         skinframe->gloss = NULL;
1703         skinframe->glow = NULL;
1704         skinframe->fog = NULL;
1705
1706         // if no data was provided, then clearly the caller wanted to get a blank skinframe
1707         if (!skindata)
1708                 return NULL;
1709
1710         if (r_shadow_bumpscale_basetexture.value > 0)
1711         {
1712                 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
1713                 temp2 = temp1 + width * height * 4;
1714                 Image_HeightmapToNormalmap_BGRA(skindata, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
1715                 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
1716                 Mem_Free(temp1);
1717         }
1718         skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_BGRA, skinframe->textureflags, NULL);
1719         if (textureflags & TEXF_ALPHA)
1720         {
1721                 for (i = 3;i < width * height * 4;i += 4)
1722                         if (skindata[i] < 255)
1723                                 break;
1724                 if (i < width * height * 4)
1725                 {
1726                         unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
1727                         memcpy(fogpixels, skindata, width * height * 4);
1728                         for (i = 0;i < width * height * 4;i += 4)
1729                                 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
1730                         skinframe->fog = R_LoadTexture2D(r_main_texturepool, va("%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, skinframe->textureflags, NULL);
1731                         Mem_Free(fogpixels);
1732                 }
1733         }
1734
1735         return skinframe;
1736 }
1737
1738 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
1739 {
1740         int i;
1741         unsigned char *temp1, *temp2;
1742         skinframe_t *skinframe;
1743
1744         if (cls.state == ca_dedicated)
1745                 return NULL;
1746
1747         // if already loaded just return it, otherwise make a new skinframe
1748         skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
1749         if (skinframe && skinframe->base)
1750                 return skinframe;
1751
1752         skinframe->stain = NULL;
1753         skinframe->merged = NULL;
1754         skinframe->base = r_texture_notexture;
1755         skinframe->pants = NULL;
1756         skinframe->shirt = NULL;
1757         skinframe->nmap = r_texture_blanknormalmap;
1758         skinframe->gloss = NULL;
1759         skinframe->glow = NULL;
1760         skinframe->fog = NULL;
1761
1762         // if no data was provided, then clearly the caller wanted to get a blank skinframe
1763         if (!skindata)
1764                 return NULL;
1765
1766         if (r_shadow_bumpscale_basetexture.value > 0)
1767         {
1768                 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
1769                 temp2 = temp1 + width * height * 4;
1770                 // use either a custom palette or the quake palette
1771                 Image_Copy8bitBGRA(skindata, temp1, width * height, palette_bgra_complete);
1772                 Image_HeightmapToNormalmap_BGRA(temp1, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
1773                 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
1774                 Mem_Free(temp1);
1775         }
1776         // use either a custom palette, or the quake palette
1777         skinframe->base = skinframe->merged = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_merged", skinframe->basename), (loadglowtexture ? palette_bgra_nofullbrights : ((skinframe->textureflags & TEXF_ALPHA) ? palette_bgra_transparent : palette_bgra_complete)), skinframe->textureflags, true); // all
1778         if (loadglowtexture)
1779                 skinframe->glow = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_glow", skinframe->basename), palette_bgra_onlyfullbrights, skinframe->textureflags, false); // glow
1780         if (loadpantsandshirt)
1781         {
1782                 skinframe->pants = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_pants", skinframe->basename), palette_bgra_pantsaswhite, skinframe->textureflags, false); // pants
1783                 skinframe->shirt = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_shirt", skinframe->basename), palette_bgra_shirtaswhite, skinframe->textureflags, false); // shirt
1784         }
1785         if (skinframe->pants || skinframe->shirt)
1786                 skinframe->base = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_nospecial", skinframe->basename), loadglowtexture ? palette_bgra_nocolormapnofullbrights : palette_bgra_nocolormap, skinframe->textureflags, false); // no special colors
1787         if (textureflags & TEXF_ALPHA)
1788         {
1789                 for (i = 0;i < width * height;i++)
1790                         if (((unsigned char *)palette_bgra_alpha)[skindata[i]*4+3] < 255)
1791                                 break;
1792                 if (i < width * height)
1793                         skinframe->fog = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_fog", skinframe->basename), palette_bgra_alpha, skinframe->textureflags, true); // fog mask
1794         }
1795
1796         return skinframe;
1797 }
1798
1799 skinframe_t *R_SkinFrame_LoadMissing(void)
1800 {
1801         skinframe_t *skinframe;
1802
1803         if (cls.state == ca_dedicated)
1804                 return NULL;
1805
1806         skinframe = R_SkinFrame_Find("missing", TEXF_PRECACHE, 0, 0, 0, true);
1807         skinframe->stain = NULL;
1808         skinframe->merged = NULL;
1809         skinframe->base = r_texture_notexture;
1810         skinframe->pants = NULL;
1811         skinframe->shirt = NULL;
1812         skinframe->nmap = r_texture_blanknormalmap;
1813         skinframe->gloss = NULL;
1814         skinframe->glow = NULL;
1815         skinframe->fog = NULL;
1816
1817         return skinframe;
1818 }
1819
1820 void gl_main_start(void)
1821 {
1822         int x;
1823         double r, alpha;
1824
1825         r = (-1.0/256.0) * (FOGMASKTABLEWIDTH * FOGMASKTABLEWIDTH);
1826         for (x = 0;x < FOGMASKTABLEWIDTH;x++)
1827         {
1828                 alpha = 1 - exp(r / ((double)x*(double)x));
1829                 if (x == FOGMASKTABLEWIDTH - 1)
1830                         alpha = 0;
1831                 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
1832         }
1833
1834         memset(r_qwskincache, 0, sizeof(r_qwskincache));
1835         memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
1836
1837         // set up r_skinframe loading system for textures
1838         memset(&r_skinframe, 0, sizeof(r_skinframe));
1839         r_skinframe.loadsequence = 1;
1840         Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
1841
1842         r_main_texturepool = R_AllocTexturePool();
1843         R_BuildBlankTextures();
1844         R_BuildNoTexture();
1845         if (gl_texturecubemap)
1846         {
1847                 R_BuildWhiteCube();
1848                 R_BuildNormalizationCube();
1849         }
1850         R_BuildFogTexture();
1851         memset(&r_bloomstate, 0, sizeof(r_bloomstate));
1852         memset(&r_waterstate, 0, sizeof(r_waterstate));
1853         memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
1854         memset(&r_svbsp, 0, sizeof (r_svbsp));
1855 }
1856
1857 void gl_main_shutdown(void)
1858 {
1859         memset(r_qwskincache, 0, sizeof(r_qwskincache));
1860         memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
1861
1862         // clear out the r_skinframe state
1863         Mem_ExpandableArray_FreeArray(&r_skinframe.array);
1864         memset(&r_skinframe, 0, sizeof(r_skinframe));
1865
1866         if (r_svbsp.nodes)
1867                 Mem_Free(r_svbsp.nodes);
1868         memset(&r_svbsp, 0, sizeof (r_svbsp));
1869         R_FreeTexturePool(&r_main_texturepool);
1870         r_texture_blanknormalmap = NULL;
1871         r_texture_white = NULL;
1872         r_texture_grey128 = NULL;
1873         r_texture_black = NULL;
1874         r_texture_whitecube = NULL;
1875         r_texture_normalizationcube = NULL;
1876         memset(&r_bloomstate, 0, sizeof(r_bloomstate));
1877         memset(&r_waterstate, 0, sizeof(r_waterstate));
1878         R_GLSL_Restart_f();
1879 }
1880
1881 extern void CL_ParseEntityLump(char *entitystring);
1882 void gl_main_newmap(void)
1883 {
1884         // FIXME: move this code to client
1885         int l;
1886         char *entities, entname[MAX_QPATH];
1887         if (cl.worldmodel)
1888         {
1889                 strlcpy(entname, cl.worldmodel->name, sizeof(entname));
1890                 l = (int)strlen(entname) - 4;
1891                 if (l >= 0 && !strcmp(entname + l, ".bsp"))
1892                 {
1893                         memcpy(entname + l, ".ent", 5);
1894                         if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
1895                         {
1896                                 CL_ParseEntityLump(entities);
1897                                 Mem_Free(entities);
1898                                 return;
1899                         }
1900                 }
1901                 if (cl.worldmodel->brush.entities)
1902                         CL_ParseEntityLump(cl.worldmodel->brush.entities);
1903         }
1904 }
1905
1906 void GL_Main_Init(void)
1907 {
1908         r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
1909
1910         Cmd_AddCommand("r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
1911         Cmd_AddCommand("r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
1912         // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
1913         if (gamemode == GAME_NEHAHRA)
1914         {
1915                 Cvar_RegisterVariable (&gl_fogenable);
1916                 Cvar_RegisterVariable (&gl_fogdensity);
1917                 Cvar_RegisterVariable (&gl_fogred);
1918                 Cvar_RegisterVariable (&gl_foggreen);
1919                 Cvar_RegisterVariable (&gl_fogblue);
1920                 Cvar_RegisterVariable (&gl_fogstart);
1921                 Cvar_RegisterVariable (&gl_fogend);
1922         }
1923         Cvar_RegisterVariable(&r_depthfirst);
1924         Cvar_RegisterVariable(&r_nearclip);
1925         Cvar_RegisterVariable(&r_showbboxes);
1926         Cvar_RegisterVariable(&r_showsurfaces);
1927         Cvar_RegisterVariable(&r_showtris);
1928         Cvar_RegisterVariable(&r_shownormals);
1929         Cvar_RegisterVariable(&r_showlighting);
1930         Cvar_RegisterVariable(&r_showshadowvolumes);
1931         Cvar_RegisterVariable(&r_showcollisionbrushes);
1932         Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
1933         Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
1934         Cvar_RegisterVariable(&r_showdisabledepthtest);
1935         Cvar_RegisterVariable(&r_drawportals);
1936         Cvar_RegisterVariable(&r_drawentities);
1937         Cvar_RegisterVariable(&r_cullentities_trace);
1938         Cvar_RegisterVariable(&r_cullentities_trace_samples);
1939         Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
1940         Cvar_RegisterVariable(&r_cullentities_trace_delay);
1941         Cvar_RegisterVariable(&r_drawviewmodel);
1942         Cvar_RegisterVariable(&r_speeds);
1943         Cvar_RegisterVariable(&r_fullbrights);
1944         Cvar_RegisterVariable(&r_wateralpha);
1945         Cvar_RegisterVariable(&r_dynamic);
1946         Cvar_RegisterVariable(&r_fullbright);
1947         Cvar_RegisterVariable(&r_shadows);
1948         Cvar_RegisterVariable(&r_shadows_throwdistance);
1949         Cvar_RegisterVariable(&r_q1bsp_skymasking);
1950         Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
1951         Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
1952         Cvar_RegisterVariable(&r_textureunits);
1953         Cvar_RegisterVariable(&r_glsl);
1954         Cvar_RegisterVariable(&r_glsl_offsetmapping);
1955         Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
1956         Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
1957         Cvar_RegisterVariable(&r_glsl_deluxemapping);
1958         Cvar_RegisterVariable(&r_water);
1959         Cvar_RegisterVariable(&r_water_resolutionmultiplier);
1960         Cvar_RegisterVariable(&r_water_clippingplanebias);
1961         Cvar_RegisterVariable(&r_water_refractdistort);
1962         Cvar_RegisterVariable(&r_water_reflectdistort);
1963         Cvar_RegisterVariable(&r_lerpsprites);
1964         Cvar_RegisterVariable(&r_lerpmodels);
1965         Cvar_RegisterVariable(&r_waterscroll);
1966         Cvar_RegisterVariable(&r_bloom);
1967         Cvar_RegisterVariable(&r_bloom_colorscale);
1968         Cvar_RegisterVariable(&r_bloom_brighten);
1969         Cvar_RegisterVariable(&r_bloom_blur);
1970         Cvar_RegisterVariable(&r_bloom_resolution);
1971         Cvar_RegisterVariable(&r_bloom_colorexponent);
1972         Cvar_RegisterVariable(&r_bloom_colorsubtract);
1973         Cvar_RegisterVariable(&r_hdr);
1974         Cvar_RegisterVariable(&r_hdr_scenebrightness);
1975         Cvar_RegisterVariable(&r_glsl_contrastboost);
1976         Cvar_RegisterVariable(&r_hdr_glowintensity);
1977         Cvar_RegisterVariable(&r_hdr_range);
1978         Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
1979         Cvar_RegisterVariable(&developer_texturelogging);
1980         Cvar_RegisterVariable(&gl_lightmaps);
1981         Cvar_RegisterVariable(&r_test);
1982         Cvar_RegisterVariable(&r_batchmode);
1983         if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
1984                 Cvar_SetValue("r_fullbrights", 0);
1985         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
1986
1987         Cvar_RegisterVariable(&r_track_sprites);
1988         Cvar_RegisterVariable(&r_track_sprites_flags);
1989         Cvar_RegisterVariable(&r_track_sprites_scalew);
1990         Cvar_RegisterVariable(&r_track_sprites_scaleh);
1991 }
1992
1993 extern void R_Textures_Init(void);
1994 extern void GL_Draw_Init(void);
1995 extern void GL_Main_Init(void);
1996 extern void R_Shadow_Init(void);
1997 extern void R_Sky_Init(void);
1998 extern void GL_Surf_Init(void);
1999 extern void R_Light_Init(void);
2000 extern void R_Particles_Init(void);
2001 extern void R_Explosion_Init(void);
2002 extern void gl_backend_init(void);
2003 extern void Sbar_Init(void);
2004 extern void R_LightningBeams_Init(void);
2005 extern void Mod_RenderInit(void);
2006
2007 void Render_Init(void)
2008 {
2009         gl_backend_init();
2010         R_Textures_Init();
2011         GL_Main_Init();
2012         GL_Draw_Init();
2013         R_Shadow_Init();
2014         R_Sky_Init();
2015         GL_Surf_Init();
2016         Sbar_Init();
2017         R_Light_Init();
2018         R_Particles_Init();
2019         R_Explosion_Init();
2020         R_LightningBeams_Init();
2021         Mod_RenderInit();
2022 }
2023
2024 /*
2025 ===============
2026 GL_Init
2027 ===============
2028 */
2029 extern char *ENGINE_EXTENSIONS;
2030 void GL_Init (void)
2031 {
2032         VID_CheckExtensions();
2033
2034         // LordHavoc: report supported extensions
2035         Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
2036
2037         // clear to black (loading plaque will be seen over this)
2038         CHECKGLERROR
2039         qglClearColor(0,0,0,1);CHECKGLERROR
2040         qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
2041 }
2042
2043 int R_CullBox(const vec3_t mins, const vec3_t maxs)
2044 {
2045         int i;
2046         mplane_t *p;
2047         for (i = 0;i < r_view.numfrustumplanes;i++)
2048         {
2049                 // skip nearclip plane, it often culls portals when you are very close, and is almost never useful
2050                 if (i == 4)
2051                         continue;
2052                 p = r_view.frustum + i;
2053                 switch(p->signbits)
2054                 {
2055                 default:
2056                 case 0:
2057                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2058                                 return true;
2059                         break;
2060                 case 1:
2061                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2062                                 return true;
2063                         break;
2064                 case 2:
2065                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2066                                 return true;
2067                         break;
2068                 case 3:
2069                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2070                                 return true;
2071                         break;
2072                 case 4:
2073                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2074                                 return true;
2075                         break;
2076                 case 5:
2077                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2078                                 return true;
2079                         break;
2080                 case 6:
2081                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2082                                 return true;
2083                         break;
2084                 case 7:
2085                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2086                                 return true;
2087                         break;
2088                 }
2089         }
2090         return false;
2091 }
2092
2093 int R_CullBoxCustomPlanes(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
2094 {
2095         int i;
2096         const mplane_t *p;
2097         for (i = 0;i < numplanes;i++)
2098         {
2099                 p = planes + i;
2100                 switch(p->signbits)
2101                 {
2102                 default:
2103                 case 0:
2104                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2105                                 return true;
2106                         break;
2107                 case 1:
2108                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2109                                 return true;
2110                         break;
2111                 case 2:
2112                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2113                                 return true;
2114                         break;
2115                 case 3:
2116                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2117                                 return true;
2118                         break;
2119                 case 4:
2120                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2121                                 return true;
2122                         break;
2123                 case 5:
2124                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2125                                 return true;
2126                         break;
2127                 case 6:
2128                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2129                                 return true;
2130                         break;
2131                 case 7:
2132                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2133                                 return true;
2134                         break;
2135                 }
2136         }
2137         return false;
2138 }
2139
2140 //==================================================================================
2141
2142 static void R_UpdateEntityLighting(entity_render_t *ent)
2143 {
2144         vec3_t tempdiffusenormal;
2145
2146         // fetch the lighting from the worldmodel data
2147         VectorSet(ent->modellight_ambient, r_ambient.value * (2.0f / 128.0f), r_ambient.value * (2.0f / 128.0f), r_ambient.value * (2.0f / 128.0f));
2148         VectorClear(ent->modellight_diffuse);
2149         VectorClear(tempdiffusenormal);
2150         if ((ent->flags & RENDER_LIGHT) && r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
2151         {
2152                 vec3_t org;
2153                 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
2154                 r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, org, ent->modellight_ambient, ent->modellight_diffuse, tempdiffusenormal);
2155         }
2156         else // highly rare
2157                 VectorSet(ent->modellight_ambient, 1, 1, 1);
2158
2159         // move the light direction into modelspace coordinates for lighting code
2160         Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, ent->modellight_lightdir);
2161         if(VectorLength2(ent->modellight_lightdir) > 0)
2162         {
2163                 VectorNormalize(ent->modellight_lightdir);
2164         }
2165         else
2166         {
2167                 VectorSet(ent->modellight_lightdir, 0, 0, 1); // have to set SOME valid vector here
2168         }
2169
2170         // scale ambient and directional light contributions according to rendering variables
2171         ent->modellight_ambient[0] *= ent->colormod[0] * r_refdef.lightmapintensity;
2172         ent->modellight_ambient[1] *= ent->colormod[1] * r_refdef.lightmapintensity;
2173         ent->modellight_ambient[2] *= ent->colormod[2] * r_refdef.lightmapintensity;
2174         ent->modellight_diffuse[0] *= ent->colormod[0] * r_refdef.lightmapintensity;
2175         ent->modellight_diffuse[1] *= ent->colormod[1] * r_refdef.lightmapintensity;
2176         ent->modellight_diffuse[2] *= ent->colormod[2] * r_refdef.lightmapintensity;
2177 }
2178
2179 static void R_View_UpdateEntityVisible (void)
2180 {
2181         int i, renderimask;
2182         entity_render_t *ent;
2183
2184         if (!r_drawentities.integer)
2185                 return;
2186
2187         renderimask = r_refdef.envmap ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL) : ((chase_active.integer || r_waterstate.renderingscene) ? RENDER_VIEWMODEL : RENDER_EXTERIORMODEL);
2188         if (r_refdef.worldmodel && r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs)
2189         {
2190                 // worldmodel can check visibility
2191                 for (i = 0;i < r_refdef.numentities;i++)
2192                 {
2193                         ent = r_refdef.entities[i];
2194                         r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs)) && ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.worldmodel, r_viewcache.world_leafvisible, ent->mins, ent->maxs));
2195
2196                 }
2197                 if(r_cullentities_trace.integer)
2198                 {
2199                         for (i = 0;i < r_refdef.numentities;i++)
2200                         {
2201                                 ent = r_refdef.entities[i];
2202                                 if(r_viewcache.entityvisible[i] && !(ent->effects & EF_NODEPTHTEST) && !(ent->flags & RENDER_VIEWMODEL) && !(ent->model && (ent->model->name[0] == '*')))
2203                                 {
2204                                         if(Mod_CanSeeBox_Trace(r_cullentities_trace_samples.integer, r_cullentities_trace_enlarge.value, r_refdef.worldmodel, r_view.origin, ent->mins, ent->maxs))
2205                                                 ent->last_trace_visibility = realtime;
2206                                         if(ent->last_trace_visibility < realtime - r_cullentities_trace_delay.value)
2207                                                 r_viewcache.entityvisible[i] = 0;
2208                                 }
2209                         }
2210                 }
2211         }
2212         else
2213         {
2214                 // no worldmodel or it can't check visibility
2215                 for (i = 0;i < r_refdef.numentities;i++)
2216                 {
2217                         ent = r_refdef.entities[i];
2218                         r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs));
2219                 }
2220         }
2221
2222         // update entity lighting (even on hidden entities for r_shadows)
2223         for (i = 0;i < r_refdef.numentities;i++)
2224                 R_UpdateEntityLighting(r_refdef.entities[i]);
2225 }
2226
2227 // only used if skyrendermasked, and normally returns false
2228 int R_DrawBrushModelsSky (void)
2229 {
2230         int i, sky;
2231         entity_render_t *ent;
2232
2233         if (!r_drawentities.integer)
2234                 return false;
2235
2236         sky = false;
2237         for (i = 0;i < r_refdef.numentities;i++)
2238         {
2239                 if (!r_viewcache.entityvisible[i])
2240                         continue;
2241                 ent = r_refdef.entities[i];
2242                 if (!ent->model || !ent->model->DrawSky)
2243                         continue;
2244                 ent->model->DrawSky(ent);
2245                 sky = true;
2246         }
2247         return sky;
2248 }
2249
2250 static void R_DrawNoModel(entity_render_t *ent);
2251 static void R_DrawModels(void)
2252 {
2253         int i;
2254         entity_render_t *ent;
2255
2256         if (!r_drawentities.integer)
2257                 return;
2258
2259         for (i = 0;i < r_refdef.numentities;i++)
2260         {
2261                 if (!r_viewcache.entityvisible[i])
2262                         continue;
2263                 ent = r_refdef.entities[i];
2264                 r_refdef.stats.entities++;
2265                 if (ent->model && ent->model->Draw != NULL)
2266                         ent->model->Draw(ent);
2267                 else
2268                         R_DrawNoModel(ent);
2269         }
2270 }
2271
2272 static void R_DrawModelsDepth(void)
2273 {
2274         int i;
2275         entity_render_t *ent;
2276
2277         if (!r_drawentities.integer)
2278                 return;
2279
2280         for (i = 0;i < r_refdef.numentities;i++)
2281         {
2282                 if (!r_viewcache.entityvisible[i])
2283                         continue;
2284                 ent = r_refdef.entities[i];
2285                 if (ent->model && ent->model->DrawDepth != NULL)
2286                         ent->model->DrawDepth(ent);
2287         }
2288 }
2289
2290 static void R_DrawModelsDebug(void)
2291 {
2292         int i;
2293         entity_render_t *ent;
2294
2295         if (!r_drawentities.integer)
2296                 return;
2297
2298         for (i = 0;i < r_refdef.numentities;i++)
2299         {
2300                 if (!r_viewcache.entityvisible[i])
2301                         continue;
2302                 ent = r_refdef.entities[i];
2303                 if (ent->model && ent->model->DrawDebug != NULL)
2304                         ent->model->DrawDebug(ent);
2305         }
2306 }
2307
2308 static void R_DrawModelsAddWaterPlanes(void)
2309 {
2310         int i;
2311         entity_render_t *ent;
2312
2313         if (!r_drawentities.integer)
2314                 return;
2315
2316         for (i = 0;i < r_refdef.numentities;i++)
2317         {
2318                 if (!r_viewcache.entityvisible[i])
2319                         continue;
2320                 ent = r_refdef.entities[i];
2321                 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
2322                         ent->model->DrawAddWaterPlanes(ent);
2323         }
2324 }
2325
2326 static void R_View_SetFrustum(void)
2327 {
2328         int i;
2329         double slopex, slopey;
2330
2331         // break apart the view matrix into vectors for various purposes
2332         Matrix4x4_ToVectors(&r_view.matrix, r_view.forward, r_view.left, r_view.up, r_view.origin);
2333         VectorNegate(r_view.left, r_view.right);
2334
2335 #if 0
2336         r_view.frustum[0].normal[0] = 0 - 1.0 / r_view.frustum_x;
2337         r_view.frustum[0].normal[1] = 0 - 0;
2338         r_view.frustum[0].normal[2] = -1 - 0;
2339         r_view.frustum[1].normal[0] = 0 + 1.0 / r_view.frustum_x;
2340         r_view.frustum[1].normal[1] = 0 + 0;
2341         r_view.frustum[1].normal[2] = -1 + 0;
2342         r_view.frustum[2].normal[0] = 0 - 0;
2343         r_view.frustum[2].normal[1] = 0 - 1.0 / r_view.frustum_y;
2344         r_view.frustum[2].normal[2] = -1 - 0;
2345         r_view.frustum[3].normal[0] = 0 + 0;
2346         r_view.frustum[3].normal[1] = 0 + 1.0 / r_view.frustum_y;
2347         r_view.frustum[3].normal[2] = -1 + 0;
2348 #endif
2349
2350 #if 0
2351         zNear = r_refdef.nearclip;
2352         nudge = 1.0 - 1.0 / (1<<23);
2353         r_view.frustum[4].normal[0] = 0 - 0;
2354         r_view.frustum[4].normal[1] = 0 - 0;
2355         r_view.frustum[4].normal[2] = -1 - -nudge;
2356         r_view.frustum[4].dist = 0 - -2 * zNear * nudge;
2357         r_view.frustum[5].normal[0] = 0 + 0;
2358         r_view.frustum[5].normal[1] = 0 + 0;
2359         r_view.frustum[5].normal[2] = -1 + -nudge;
2360         r_view.frustum[5].dist = 0 + -2 * zNear * nudge;
2361 #endif
2362
2363
2364
2365 #if 0
2366         r_view.frustum[0].normal[0] = m[3] - m[0];
2367         r_view.frustum[0].normal[1] = m[7] - m[4];
2368         r_view.frustum[0].normal[2] = m[11] - m[8];
2369         r_view.frustum[0].dist = m[15] - m[12];
2370
2371         r_view.frustum[1].normal[0] = m[3] + m[0];
2372         r_view.frustum[1].normal[1] = m[7] + m[4];
2373         r_view.frustum[1].normal[2] = m[11] + m[8];
2374         r_view.frustum[1].dist = m[15] + m[12];
2375
2376         r_view.frustum[2].normal[0] = m[3] - m[1];
2377         r_view.frustum[2].normal[1] = m[7] - m[5];
2378         r_view.frustum[2].normal[2] = m[11] - m[9];
2379         r_view.frustum[2].dist = m[15] - m[13];
2380
2381         r_view.frustum[3].normal[0] = m[3] + m[1];
2382         r_view.frustum[3].normal[1] = m[7] + m[5];
2383         r_view.frustum[3].normal[2] = m[11] + m[9];
2384         r_view.frustum[3].dist = m[15] + m[13];
2385
2386         r_view.frustum[4].normal[0] = m[3] - m[2];
2387         r_view.frustum[4].normal[1] = m[7] - m[6];
2388         r_view.frustum[4].normal[2] = m[11] - m[10];
2389         r_view.frustum[4].dist = m[15] - m[14];
2390
2391         r_view.frustum[5].normal[0] = m[3] + m[2];
2392         r_view.frustum[5].normal[1] = m[7] + m[6];
2393         r_view.frustum[5].normal[2] = m[11] + m[10];
2394         r_view.frustum[5].dist = m[15] + m[14];
2395 #endif
2396
2397         if (r_view.useperspective)
2398         {
2399                 slopex = 1.0 / r_view.frustum_x;
2400                 slopey = 1.0 / r_view.frustum_y;
2401                 VectorMA(r_view.forward, -slopex, r_view.left, r_view.frustum[0].normal);
2402                 VectorMA(r_view.forward,  slopex, r_view.left, r_view.frustum[1].normal);
2403                 VectorMA(r_view.forward, -slopey, r_view.up  , r_view.frustum[2].normal);
2404                 VectorMA(r_view.forward,  slopey, r_view.up  , r_view.frustum[3].normal);
2405                 VectorCopy(r_view.forward, r_view.frustum[4].normal);
2406                 
2407                 // Leaving those out was a mistake, those were in the old code, and they
2408                 // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
2409                 // I couldn't reproduce it after adding those normalizations. --blub
2410                 VectorNormalize(r_view.frustum[0].normal);
2411                 VectorNormalize(r_view.frustum[1].normal);
2412                 VectorNormalize(r_view.frustum[2].normal);
2413                 VectorNormalize(r_view.frustum[3].normal);
2414
2415                 // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
2416                 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[0]);
2417                 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward,  1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[1]);
2418                 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left,  1024 * slopey, r_view.up, r_view.frustumcorner[2]);
2419                 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward,  1024 * slopex, r_view.left,  1024 * slopey, r_view.up, r_view.frustumcorner[3]);
2420
2421                 r_view.frustum[0].dist = DotProduct (r_view.origin, r_view.frustum[0].normal);
2422                 r_view.frustum[1].dist = DotProduct (r_view.origin, r_view.frustum[1].normal);
2423                 r_view.frustum[2].dist = DotProduct (r_view.origin, r_view.frustum[2].normal);
2424                 r_view.frustum[3].dist = DotProduct (r_view.origin, r_view.frustum[3].normal);
2425                 r_view.frustum[4].dist = DotProduct (r_view.origin, r_view.frustum[4].normal) + r_refdef.nearclip;
2426         }
2427         else
2428         {
2429                 VectorScale(r_view.left, -r_view.ortho_x, r_view.frustum[0].normal);
2430                 VectorScale(r_view.left,  r_view.ortho_x, r_view.frustum[1].normal);
2431                 VectorScale(r_view.up, -r_view.ortho_y, r_view.frustum[2].normal);
2432                 VectorScale(r_view.up,  r_view.ortho_y, r_view.frustum[3].normal);
2433                 VectorCopy(r_view.forward, r_view.frustum[4].normal);
2434                 r_view.frustum[0].dist = DotProduct (r_view.origin, r_view.frustum[0].normal) + r_view.ortho_x;
2435                 r_view.frustum[1].dist = DotProduct (r_view.origin, r_view.frustum[1].normal) + r_view.ortho_x;
2436                 r_view.frustum[2].dist = DotProduct (r_view.origin, r_view.frustum[2].normal) + r_view.ortho_y;
2437                 r_view.frustum[3].dist = DotProduct (r_view.origin, r_view.frustum[3].normal) + r_view.ortho_y;
2438                 r_view.frustum[4].dist = DotProduct (r_view.origin, r_view.frustum[4].normal) + r_refdef.nearclip;
2439         }
2440         r_view.numfrustumplanes = 5;
2441
2442         if (r_view.useclipplane)
2443         {
2444                 r_view.numfrustumplanes = 6;
2445                 r_view.frustum[5] = r_view.clipplane;
2446         }
2447
2448         for (i = 0;i < r_view.numfrustumplanes;i++)
2449                 PlaneClassify(r_view.frustum + i);
2450
2451         // LordHavoc: note to all quake engine coders, Quake had a special case
2452         // for 90 degrees which assumed a square view (wrong), so I removed it,
2453         // Quake2 has it disabled as well.
2454
2455         // rotate R_VIEWFORWARD right by FOV_X/2 degrees
2456         //RotatePointAroundVector( r_view.frustum[0].normal, r_view.up, r_view.forward, -(90 - r_refdef.fov_x / 2));
2457         //r_view.frustum[0].dist = DotProduct (r_view.origin, frustum[0].normal);
2458         //PlaneClassify(&frustum[0]);
2459
2460         // rotate R_VIEWFORWARD left by FOV_X/2 degrees
2461         //RotatePointAroundVector( r_view.frustum[1].normal, r_view.up, r_view.forward, (90 - r_refdef.fov_x / 2));
2462         //r_view.frustum[1].dist = DotProduct (r_view.origin, frustum[1].normal);
2463         //PlaneClassify(&frustum[1]);
2464
2465         // rotate R_VIEWFORWARD up by FOV_X/2 degrees
2466         //RotatePointAroundVector( r_view.frustum[2].normal, r_view.left, r_view.forward, -(90 - r_refdef.fov_y / 2));
2467         //r_view.frustum[2].dist = DotProduct (r_view.origin, frustum[2].normal);
2468         //PlaneClassify(&frustum[2]);
2469
2470         // rotate R_VIEWFORWARD down by FOV_X/2 degrees
2471         //RotatePointAroundVector( r_view.frustum[3].normal, r_view.left, r_view.forward, (90 - r_refdef.fov_y / 2));
2472         //r_view.frustum[3].dist = DotProduct (r_view.origin, frustum[3].normal);
2473         //PlaneClassify(&frustum[3]);
2474
2475         // nearclip plane
2476         //VectorCopy(r_view.forward, r_view.frustum[4].normal);
2477         //r_view.frustum[4].dist = DotProduct (r_view.origin, frustum[4].normal) + r_nearclip.value;
2478         //PlaneClassify(&frustum[4]);
2479 }
2480
2481 void R_View_Update(void)
2482 {
2483         R_View_SetFrustum();
2484         R_View_WorldVisibility(r_view.useclipplane);
2485         R_View_UpdateEntityVisible();
2486 }
2487
2488 void R_SetupView(void)
2489 {
2490         if (!r_view.useperspective)
2491                 GL_SetupView_Mode_Ortho(-r_view.ortho_x, -r_view.ortho_y, r_view.ortho_x, r_view.ortho_y, -r_refdef.farclip, r_refdef.farclip);
2492         else if (r_refdef.rtworldshadows || r_refdef.rtdlightshadows)
2493                 GL_SetupView_Mode_PerspectiveInfiniteFarClip(r_view.frustum_x, r_view.frustum_y, r_refdef.nearclip);
2494         else
2495                 GL_SetupView_Mode_Perspective(r_view.frustum_x, r_view.frustum_y, r_refdef.nearclip, r_refdef.farclip);
2496
2497         GL_SetupView_Orientation_FromEntity(&r_view.matrix);
2498
2499         if (r_view.useclipplane)
2500         {
2501                 // LordHavoc: couldn't figure out how to make this approach the
2502                 vec_t dist = r_view.clipplane.dist - r_water_clippingplanebias.value;
2503                 vec_t viewdist = DotProduct(r_view.origin, r_view.clipplane.normal);
2504                 if (viewdist < r_view.clipplane.dist + r_water_clippingplanebias.value)
2505                         dist = r_view.clipplane.dist;
2506                 GL_SetupView_ApplyCustomNearClipPlane(r_view.clipplane.normal[0], r_view.clipplane.normal[1], r_view.clipplane.normal[2], dist);
2507         }
2508 }
2509
2510 void R_ResetViewRendering2D(void)
2511 {
2512         if (gl_support_fragment_shader)
2513         {
2514                 qglUseProgramObjectARB(0);CHECKGLERROR
2515         }
2516
2517         DrawQ_Finish();
2518
2519         // GL is weird because it's bottom to top, r_view.y is top to bottom
2520         qglViewport(r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2521         GL_SetupView_Mode_Ortho(0, 0, 1, 1, -10, 100);
2522         GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
2523         GL_Color(1, 1, 1, 1);
2524         GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
2525         GL_BlendFunc(GL_ONE, GL_ZERO);
2526         GL_AlphaTest(false);
2527         GL_ScissorTest(false);
2528         GL_DepthMask(false);
2529         GL_DepthRange(0, 1);
2530         GL_DepthTest(false);
2531         R_Mesh_Matrix(&identitymatrix);
2532         R_Mesh_ResetTextureState();
2533         GL_PolygonOffset(0, 0);
2534         qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
2535         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2536         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2537         qglStencilMask(~0);CHECKGLERROR
2538         qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
2539         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
2540         GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
2541 }
2542
2543 void R_ResetViewRendering3D(void)
2544 {
2545         if (gl_support_fragment_shader)
2546         {
2547                 qglUseProgramObjectARB(0);CHECKGLERROR
2548         }
2549
2550         DrawQ_Finish();
2551
2552         // GL is weird because it's bottom to top, r_view.y is top to bottom
2553         qglViewport(r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2554         R_SetupView();
2555         GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
2556         GL_Color(1, 1, 1, 1);
2557         GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
2558         GL_BlendFunc(GL_ONE, GL_ZERO);
2559         GL_AlphaTest(false);
2560         GL_ScissorTest(true);
2561         GL_DepthMask(true);
2562         GL_DepthRange(0, 1);
2563         GL_DepthTest(true);
2564         R_Mesh_Matrix(&identitymatrix);
2565         R_Mesh_ResetTextureState();
2566         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
2567         qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
2568         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2569         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2570         qglStencilMask(~0);CHECKGLERROR
2571         qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
2572         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
2573         GL_CullFace(r_view.cullface_back);
2574 }
2575
2576 /*
2577         R_Bloom_SetupShader(
2578 "// bloom shader\n"
2579 "// written by Forest 'LordHavoc' Hale\n"
2580 "\n"
2581 "// common definitions between vertex shader and fragment shader:\n"
2582 "\n"
2583 "#ifdef __GLSL_CG_DATA_TYPES\n"
2584 "#define myhalf half\n"
2585 "#define myhvec2 hvec2\n"
2586 "#define myhvec3 hvec3\n"
2587 "#define myhvec4 hvec4\n"
2588 "#else\n"
2589 "#define myhalf float\n"
2590 "#define myhvec2 vec2\n"
2591 "#define myhvec3 vec3\n"
2592 "#define myhvec4 vec4\n"
2593 "#endif\n"
2594 "\n"
2595 "varying vec2 ScreenTexCoord;\n"
2596 "varying vec2 BloomTexCoord;\n"
2597 "\n"
2598 "\n"
2599 "\n"
2600 "\n"
2601 "// vertex shader specific:\n"
2602 "#ifdef VERTEX_SHADER\n"
2603 "\n"
2604 "void main(void)\n"
2605 "{\n"
2606 "       ScreenTexCoord = vec2(gl_MultiTexCoord0);\n"
2607 "       BloomTexCoord = vec2(gl_MultiTexCoord1);\n"
2608 "       // transform vertex to camera space, using ftransform to match non-VS\n"
2609 "       // rendering\n"
2610 "       gl_Position = ftransform();\n"
2611 "}\n"
2612 "\n"
2613 "#endif // VERTEX_SHADER\n"
2614 "\n"
2615 "\n"
2616 "\n"
2617 "\n"
2618 "// fragment shader specific:\n"
2619 "#ifdef FRAGMENT_SHADER\n"
2620 "\n"
2621 "void main(void)\n"
2622 "{\n"
2623 "       int x, y;
2624 "       myhvec3 color = myhvec3(texture2D(Texture_Screen, ScreenTexCoord));\n"
2625 "       for (x = -BLUR_X;x <= BLUR_X;x++)
2626 "       color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2627 "       color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2628 "       color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2629 "       color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2630
2631 "       gl_FragColor = vec4(color);\n"
2632 "}\n"
2633 "\n"
2634 "#endif // FRAGMENT_SHADER\n"
2635 */
2636
2637 void R_RenderScene(qboolean addwaterplanes);
2638
2639 static void R_Water_StartFrame(void)
2640 {
2641         int i;
2642         int waterwidth, waterheight, texturewidth, textureheight;
2643         r_waterstate_waterplane_t *p;
2644
2645         // set waterwidth and waterheight to the water resolution that will be
2646         // used (often less than the screen resolution for faster rendering)
2647         waterwidth = (int)bound(1, r_view.width * r_water_resolutionmultiplier.value, r_view.width);
2648         waterheight = (int)bound(1, r_view.height * r_water_resolutionmultiplier.value, r_view.height);
2649
2650         // calculate desired texture sizes
2651         // can't use water if the card does not support the texture size
2652         if (!r_water.integer || !r_glsl.integer || !gl_support_fragment_shader || waterwidth > gl_max_texture_size || waterheight > gl_max_texture_size)
2653                 texturewidth = textureheight = waterwidth = waterheight = 0;
2654         else if (gl_support_arb_texture_non_power_of_two)
2655         {
2656                 texturewidth = waterwidth;
2657                 textureheight = waterheight;
2658         }
2659         else
2660         {
2661                 for (texturewidth   = 1;texturewidth   < waterwidth ;texturewidth   *= 2);
2662                 for (textureheight  = 1;textureheight  < waterheight;textureheight  *= 2);
2663         }
2664
2665         // allocate textures as needed
2666         if (r_waterstate.waterwidth != waterwidth || r_waterstate.waterheight != waterheight || r_waterstate.texturewidth != texturewidth || r_waterstate.textureheight != textureheight)
2667         {
2668                 r_waterstate.maxwaterplanes = MAX_WATERPLANES;
2669                 for (i = 0, p = r_waterstate.waterplanes;i < r_waterstate.maxwaterplanes;i++, p++)
2670                 {
2671                         if (p->texture_refraction)
2672                                 R_FreeTexture(p->texture_refraction);
2673                         p->texture_refraction = NULL;
2674                         if (p->texture_reflection)
2675                                 R_FreeTexture(p->texture_reflection);
2676                         p->texture_reflection = NULL;
2677                 }
2678                 memset(&r_waterstate, 0, sizeof(r_waterstate));
2679                 r_waterstate.waterwidth = waterwidth;
2680                 r_waterstate.waterheight = waterheight;
2681                 r_waterstate.texturewidth = texturewidth;
2682                 r_waterstate.textureheight = textureheight;
2683         }
2684
2685         if (r_waterstate.waterwidth)
2686         {
2687                 r_waterstate.enabled = true;
2688
2689                 // set up variables that will be used in shader setup
2690                 r_waterstate.screenscale[0] = 0.5f * (float)waterwidth / (float)texturewidth;
2691                 r_waterstate.screenscale[1] = 0.5f * (float)waterheight / (float)textureheight;
2692                 r_waterstate.screencenter[0] = 0.5f * (float)waterwidth / (float)texturewidth;
2693                 r_waterstate.screencenter[1] = 0.5f * (float)waterheight / (float)textureheight;
2694         }
2695
2696         r_waterstate.maxwaterplanes = MAX_WATERPLANES;
2697         r_waterstate.numwaterplanes = 0;
2698 }
2699
2700 static void R_Water_AddWaterPlane(msurface_t *surface)
2701 {
2702         int triangleindex, planeindex;
2703         const int *e;
2704         vec_t f;
2705         vec3_t vert[3];
2706         vec3_t normal;
2707         vec3_t center;
2708         r_waterstate_waterplane_t *p;
2709         // just use the first triangle with a valid normal for any decisions
2710         VectorClear(normal);
2711         VectorClear(center);
2712         for (triangleindex = 0, e = rsurface.modelelement3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
2713         {
2714                 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[0]*3, vert[0]);
2715                 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[1]*3, vert[1]);
2716                 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[2]*3, vert[2]);
2717                 TriangleNormal(vert[0], vert[1], vert[2], normal);
2718                 if (VectorLength2(normal) >= 0.001)
2719                         break;
2720         }
2721         // now find the center of this surface
2722         for (triangleindex = 0, e = rsurface.modelelement3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles*3;triangleindex++, e++)
2723         {
2724                 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[0]*3, vert[0]);
2725                 VectorAdd(center, vert[0], center);
2726         }
2727         f = 1.0 / surface->num_triangles*3;
2728         VectorScale(center, f, center);
2729
2730         // find a matching plane if there is one
2731         for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2732                 if (fabs(PlaneDiff(vert[0], &p->plane)) < 1 && fabs(PlaneDiff(vert[1], &p->plane)) < 1 && fabs(PlaneDiff(vert[2], &p->plane)) < 1)
2733                         break;
2734         if (planeindex >= r_waterstate.maxwaterplanes)
2735                 return; // nothing we can do, out of planes
2736
2737         // if this triangle does not fit any known plane rendered this frame, add one
2738         if (planeindex >= r_waterstate.numwaterplanes)
2739         {
2740                 // store the new plane
2741                 r_waterstate.numwaterplanes++;
2742                 VectorCopy(normal, p->plane.normal);
2743                 VectorNormalize(p->plane.normal);
2744                 p->plane.dist = DotProduct(vert[0], p->plane.normal);
2745                 PlaneClassify(&p->plane);
2746                 // flip the plane if it does not face the viewer
2747                 if (PlaneDiff(r_view.origin, &p->plane) < 0)
2748                 {
2749                         VectorNegate(p->plane.normal, p->plane.normal);
2750                         p->plane.dist *= -1;
2751                         PlaneClassify(&p->plane);
2752                 }
2753                 // clear materialflags and pvs
2754                 p->materialflags = 0;
2755                 p->pvsvalid = false;
2756         }
2757         // merge this surface's materialflags into the waterplane
2758         p->materialflags |= surface->texture->currentframe->currentmaterialflags;
2759         // merge this surface's PVS into the waterplane
2760         if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) && r_refdef.worldmodel && r_refdef.worldmodel->brush.FatPVS)
2761         {
2762                 r_refdef.worldmodel->brush.FatPVS(r_refdef.worldmodel, r_view.origin, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
2763                 p->pvsvalid = true;
2764         }
2765 }
2766
2767 static void R_Water_ProcessPlanes(void)
2768 {
2769         r_view_t originalview;
2770         int planeindex;
2771         r_waterstate_waterplane_t *p;
2772
2773         originalview = r_view;
2774
2775         // make sure enough textures are allocated
2776         for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2777         {
2778                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
2779                 {
2780                         if (!p->texture_refraction)
2781                                 p->texture_refraction = R_LoadTexture2D(r_main_texturepool, va("waterplane%i_refraction", planeindex), r_waterstate.texturewidth, r_waterstate.textureheight, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
2782                         if (!p->texture_refraction)
2783                                 goto error;
2784                 }
2785
2786                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
2787                 {
2788                         if (!p->texture_reflection)
2789                                 p->texture_reflection = R_LoadTexture2D(r_main_texturepool, va("waterplane%i_reflection", planeindex), r_waterstate.texturewidth, r_waterstate.textureheight, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
2790                         if (!p->texture_reflection)
2791                                 goto error;
2792                 }
2793         }
2794
2795         // render views
2796         for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2797         {
2798                 r_view.showdebug = false;
2799                 r_view.width = r_waterstate.waterwidth;
2800                 r_view.height = r_waterstate.waterheight;
2801                 r_view.useclipplane = true;
2802                 r_waterstate.renderingscene = true;
2803
2804                 // render the normal view scene and copy into texture
2805                 // (except that a clipping plane should be used to hide everything on one side of the water, and the viewer's weapon model should be omitted)
2806                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
2807                 {
2808                         r_view.clipplane = p->plane;
2809                         VectorNegate(r_view.clipplane.normal, r_view.clipplane.normal);
2810                         r_view.clipplane.dist = -r_view.clipplane.dist;
2811                         PlaneClassify(&r_view.clipplane);
2812
2813                         R_RenderScene(false);
2814
2815                         // copy view into the screen texture
2816                         R_Mesh_TexBind(0, R_GetTexture(p->texture_refraction));
2817                         GL_ActiveTexture(0);
2818                         CHECKGLERROR
2819                         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2820                 }
2821
2822                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
2823                 {
2824                         // render reflected scene and copy into texture
2825                         Matrix4x4_Reflect(&r_view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
2826                         r_view.clipplane = p->plane;
2827                         // reverse the cullface settings for this render
2828                         r_view.cullface_front = GL_FRONT;
2829                         r_view.cullface_back = GL_BACK;
2830                         if (r_refdef.worldmodel && r_refdef.worldmodel->brush.num_pvsclusterbytes)
2831                         {
2832                                 r_view.usecustompvs = true;
2833                                 if (p->pvsvalid)
2834                                         memcpy(r_viewcache.world_pvsbits, p->pvsbits, r_refdef.worldmodel->brush.num_pvsclusterbytes);
2835                                 else
2836                                         memset(r_viewcache.world_pvsbits, 0xFF, r_refdef.worldmodel->brush.num_pvsclusterbytes);
2837                         }
2838
2839                         R_ResetViewRendering3D();
2840                         R_ClearScreen();
2841                         if (r_timereport_active)
2842                                 R_TimeReport("viewclear");
2843
2844                         R_RenderScene(false);
2845
2846                         R_Mesh_TexBind(0, R_GetTexture(p->texture_reflection));
2847                         GL_ActiveTexture(0);
2848                         CHECKGLERROR
2849                         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2850
2851                         R_ResetViewRendering3D();
2852                         R_ClearScreen();
2853                         if (r_timereport_active)
2854                                 R_TimeReport("viewclear");
2855                 }
2856
2857                 r_view = originalview;
2858                 r_view.clear = true;
2859                 r_waterstate.renderingscene = false;
2860         }
2861         return;
2862 error:
2863         r_view = originalview;
2864         r_waterstate.renderingscene = false;
2865         Cvar_SetValueQuick(&r_water, 0);
2866         Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed!  Turned off r_water.\n");
2867         return;
2868 }
2869
2870 void R_Bloom_StartFrame(void)
2871 {
2872         int bloomtexturewidth, bloomtextureheight, screentexturewidth, screentextureheight;
2873
2874         // set bloomwidth and bloomheight to the bloom resolution that will be
2875         // used (often less than the screen resolution for faster rendering)
2876         r_bloomstate.bloomwidth = bound(1, r_bloom_resolution.integer, r_view.width);
2877         r_bloomstate.bloomheight = r_bloomstate.bloomwidth * r_view.height / r_view.width;
2878         r_bloomstate.bloomheight = bound(1, r_bloomstate.bloomheight, r_view.height);
2879
2880         // calculate desired texture sizes
2881         if (gl_support_arb_texture_non_power_of_two)
2882         {
2883                 screentexturewidth = r_view.width;
2884                 screentextureheight = r_view.height;
2885                 bloomtexturewidth = r_bloomstate.bloomwidth;
2886                 bloomtextureheight = r_bloomstate.bloomheight;
2887         }
2888         else
2889         {
2890                 for (screentexturewidth  = 1;screentexturewidth  < vid.width               ;screentexturewidth  *= 2);
2891                 for (screentextureheight = 1;screentextureheight < vid.height              ;screentextureheight *= 2);
2892                 for (bloomtexturewidth   = 1;bloomtexturewidth   < r_bloomstate.bloomwidth ;bloomtexturewidth   *= 2);
2893                 for (bloomtextureheight  = 1;bloomtextureheight  < r_bloomstate.bloomheight;bloomtextureheight  *= 2);
2894         }
2895
2896         if (r_hdr.integer)
2897         {
2898                 screentexturewidth = screentextureheight = 0;
2899         }
2900         else if (r_bloom.integer)
2901         {
2902         }
2903         else
2904         {
2905                 screentexturewidth = screentextureheight = 0;
2906                 bloomtexturewidth = bloomtextureheight = 0;
2907         }
2908
2909         if ((!bloomtexturewidth && !bloomtextureheight) || r_bloom_resolution.integer < 4 || r_bloom_blur.value < 1 || r_bloom_blur.value >= 512 || screentexturewidth > gl_max_texture_size || screentextureheight > gl_max_texture_size || bloomtexturewidth > gl_max_texture_size || bloomtextureheight > gl_max_texture_size)
2910         {
2911                 // can't use bloom if the parameters are too weird
2912                 // can't use bloom if the card does not support the texture size
2913                 if (r_bloomstate.texture_screen)
2914                         R_FreeTexture(r_bloomstate.texture_screen);
2915                 if (r_bloomstate.texture_bloom)
2916                         R_FreeTexture(r_bloomstate.texture_bloom);
2917                 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
2918                 return;
2919         }
2920
2921         r_bloomstate.enabled = true;
2922         r_bloomstate.hdr = r_hdr.integer != 0;
2923
2924         // allocate textures as needed
2925         if (r_bloomstate.screentexturewidth != screentexturewidth || r_bloomstate.screentextureheight != screentextureheight)
2926         {
2927                 if (r_bloomstate.texture_screen)
2928                         R_FreeTexture(r_bloomstate.texture_screen);
2929                 r_bloomstate.texture_screen = NULL;
2930                 r_bloomstate.screentexturewidth = screentexturewidth;
2931                 r_bloomstate.screentextureheight = screentextureheight;
2932                 if (r_bloomstate.screentexturewidth && r_bloomstate.screentextureheight)
2933                         r_bloomstate.texture_screen = R_LoadTexture2D(r_main_texturepool, "screen", r_bloomstate.screentexturewidth, r_bloomstate.screentextureheight, NULL, TEXTYPE_BGRA, TEXF_FORCENEAREST | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
2934         }
2935         if (r_bloomstate.bloomtexturewidth != bloomtexturewidth || r_bloomstate.bloomtextureheight != bloomtextureheight)
2936         {
2937                 if (r_bloomstate.texture_bloom)
2938                         R_FreeTexture(r_bloomstate.texture_bloom);
2939                 r_bloomstate.texture_bloom = NULL;
2940                 r_bloomstate.bloomtexturewidth = bloomtexturewidth;
2941                 r_bloomstate.bloomtextureheight = bloomtextureheight;
2942                 if (r_bloomstate.bloomtexturewidth && r_bloomstate.bloomtextureheight)
2943                         r_bloomstate.texture_bloom = R_LoadTexture2D(r_main_texturepool, "bloom", r_bloomstate.bloomtexturewidth, r_bloomstate.bloomtextureheight, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
2944         }
2945
2946         // set up a texcoord array for the full resolution screen image
2947         // (we have to keep this around to copy back during final render)
2948         r_bloomstate.screentexcoord2f[0] = 0;
2949         r_bloomstate.screentexcoord2f[1] = (float)r_view.height / (float)r_bloomstate.screentextureheight;
2950         r_bloomstate.screentexcoord2f[2] = (float)r_view.width / (float)r_bloomstate.screentexturewidth;
2951         r_bloomstate.screentexcoord2f[3] = (float)r_view.height / (float)r_bloomstate.screentextureheight;
2952         r_bloomstate.screentexcoord2f[4] = (float)r_view.width / (float)r_bloomstate.screentexturewidth;
2953         r_bloomstate.screentexcoord2f[5] = 0;
2954         r_bloomstate.screentexcoord2f[6] = 0;
2955         r_bloomstate.screentexcoord2f[7] = 0;
2956
2957         // set up a texcoord array for the reduced resolution bloom image
2958         // (which will be additive blended over the screen image)
2959         r_bloomstate.bloomtexcoord2f[0] = 0;
2960         r_bloomstate.bloomtexcoord2f[1] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
2961         r_bloomstate.bloomtexcoord2f[2] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
2962         r_bloomstate.bloomtexcoord2f[3] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
2963         r_bloomstate.bloomtexcoord2f[4] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
2964         r_bloomstate.bloomtexcoord2f[5] = 0;
2965         r_bloomstate.bloomtexcoord2f[6] = 0;
2966         r_bloomstate.bloomtexcoord2f[7] = 0;
2967 }
2968
2969 void R_Bloom_CopyScreenTexture(float colorscale)
2970 {
2971         r_refdef.stats.bloom++;
2972
2973         R_ResetViewRendering2D();
2974         R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
2975         R_Mesh_ColorPointer(NULL, 0, 0);
2976         R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
2977         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
2978
2979         // copy view into the screen texture
2980         GL_ActiveTexture(0);
2981         CHECKGLERROR
2982         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2983         r_refdef.stats.bloom_copypixels += r_view.width * r_view.height;
2984
2985         // now scale it down to the bloom texture size
2986         CHECKGLERROR
2987         qglViewport(r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
2988         GL_BlendFunc(GL_ONE, GL_ZERO);
2989         GL_Color(colorscale, colorscale, colorscale, 1);
2990         // TODO: optimize with multitexture or GLSL
2991         R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2992         r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
2993
2994         // we now have a bloom image in the framebuffer
2995         // copy it into the bloom image texture for later processing
2996         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
2997         GL_ActiveTexture(0);
2998         CHECKGLERROR
2999         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3000         r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3001 }
3002
3003 void R_Bloom_CopyHDRTexture(void)
3004 {
3005         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3006         GL_ActiveTexture(0);
3007         CHECKGLERROR
3008         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
3009         r_refdef.stats.bloom_copypixels += r_view.width * r_view.height;
3010 }
3011
3012 void R_Bloom_MakeTexture(void)
3013 {
3014         int x, range, dir;
3015         float xoffset, yoffset, r, brighten;
3016
3017         r_refdef.stats.bloom++;
3018
3019         R_ResetViewRendering2D();
3020         R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3021         R_Mesh_ColorPointer(NULL, 0, 0);
3022
3023         // we have a bloom image in the framebuffer
3024         CHECKGLERROR
3025         qglViewport(r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3026
3027         for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
3028         {
3029                 x *= 2;
3030                 r = bound(0, r_bloom_colorexponent.value / x, 1);
3031                 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
3032                 GL_Color(r, r, r, 1);
3033                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3034                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3035                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3036                 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3037
3038                 // copy the vertically blurred bloom view to a texture
3039                 GL_ActiveTexture(0);
3040                 CHECKGLERROR
3041                 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3042                 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3043         }
3044
3045         range = r_bloom_blur.integer * r_bloomstate.bloomwidth / 320;
3046         brighten = r_bloom_brighten.value;
3047         if (r_hdr.integer)
3048                 brighten *= r_hdr_range.value;
3049         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3050         R_Mesh_TexCoordPointer(0, 2, r_bloomstate.offsettexcoord2f, 0, 0);
3051
3052         for (dir = 0;dir < 2;dir++)
3053         {
3054                 // blend on at multiple vertical offsets to achieve a vertical blur
3055                 // TODO: do offset blends using GLSL
3056                 GL_BlendFunc(GL_ONE, GL_ZERO);
3057                 for (x = -range;x <= range;x++)
3058                 {
3059                         if (!dir){xoffset = 0;yoffset = x;}
3060                         else {xoffset = x;yoffset = 0;}
3061                         xoffset /= (float)r_bloomstate.bloomtexturewidth;
3062                         yoffset /= (float)r_bloomstate.bloomtextureheight;
3063                         // compute a texcoord array with the specified x and y offset
3064                         r_bloomstate.offsettexcoord2f[0] = xoffset+0;
3065                         r_bloomstate.offsettexcoord2f[1] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3066                         r_bloomstate.offsettexcoord2f[2] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3067                         r_bloomstate.offsettexcoord2f[3] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3068                         r_bloomstate.offsettexcoord2f[4] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3069                         r_bloomstate.offsettexcoord2f[5] = yoffset+0;
3070                         r_bloomstate.offsettexcoord2f[6] = xoffset+0;
3071                         r_bloomstate.offsettexcoord2f[7] = yoffset+0;
3072                         // this r value looks like a 'dot' particle, fading sharply to
3073                         // black at the edges
3074                         // (probably not realistic but looks good enough)
3075                         //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
3076                         //r = (dir ? 1.0f : brighten)/(range*2+1);
3077                         r = (dir ? 1.0f : brighten)/(range*2+1)*(1 - x*x/(float)(range*range));
3078                         GL_Color(r, r, r, 1);
3079                         R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3080                         r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3081                         GL_BlendFunc(GL_ONE, GL_ONE);
3082                 }
3083
3084                 // copy the vertically blurred bloom view to a texture
3085                 GL_ActiveTexture(0);
3086                 CHECKGLERROR
3087                 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3088                 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3089         }
3090
3091         // apply subtract last
3092         // (just like it would be in a GLSL shader)
3093         if (r_bloom_colorsubtract.value > 0 && gl_support_ext_blend_subtract)
3094         {
3095                 GL_BlendFunc(GL_ONE, GL_ZERO);
3096                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3097                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3098                 GL_Color(1, 1, 1, 1);
3099                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3100                 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3101
3102                 GL_BlendFunc(GL_ONE, GL_ONE);
3103                 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
3104                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
3105                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3106                 GL_Color(r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 1);
3107                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3108                 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3109                 qglBlendEquationEXT(GL_FUNC_ADD_EXT);
3110
3111                 // copy the darkened bloom view to a texture
3112                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3113                 GL_ActiveTexture(0);
3114                 CHECKGLERROR
3115                 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3116                 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3117         }
3118 }
3119
3120 void R_HDR_RenderBloomTexture(void)
3121 {
3122         int oldwidth, oldheight;
3123
3124         oldwidth = r_view.width;
3125         oldheight = r_view.height;
3126         r_view.width = r_bloomstate.bloomwidth;
3127         r_view.height = r_bloomstate.bloomheight;
3128
3129         // TODO: support GL_EXT_framebuffer_object rather than reusing the framebuffer?  it might improve SLI performance.
3130         // TODO: add exposure compensation features
3131         // TODO: add fp16 framebuffer support
3132
3133         r_view.showdebug = false;
3134         r_view.colorscale = r_bloom_colorscale.value * r_hdr_scenebrightness.value;
3135         if (r_hdr.integer)
3136                 r_view.colorscale /= r_hdr_range.value;
3137         r_waterstate.numwaterplanes = 0;
3138         R_RenderScene(r_waterstate.enabled);
3139         r_view.showdebug = true;
3140
3141         R_ResetViewRendering2D();
3142
3143         R_Bloom_CopyHDRTexture();
3144         R_Bloom_MakeTexture();
3145
3146         R_ResetViewRendering3D();
3147
3148         R_ClearScreen();
3149         if (r_timereport_active)
3150                 R_TimeReport("viewclear");
3151
3152         // restore the view settings
3153         r_view.width = oldwidth;
3154         r_view.height = oldheight;
3155 }
3156
3157 static void R_BlendView(void)
3158 {
3159         if (r_bloomstate.enabled && r_bloomstate.hdr)
3160         {
3161                 // render high dynamic range bloom effect
3162                 // the bloom texture was made earlier this render, so we just need to
3163                 // blend it onto the screen...
3164                 R_ResetViewRendering2D();
3165                 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3166                 R_Mesh_ColorPointer(NULL, 0, 0);
3167                 GL_Color(1, 1, 1, 1);
3168                 GL_BlendFunc(GL_ONE, GL_ONE);
3169                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3170                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3171                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3172                 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3173         }
3174         else if (r_bloomstate.enabled)
3175         {
3176                 // render simple bloom effect
3177                 // copy the screen and shrink it and darken it for the bloom process
3178                 R_Bloom_CopyScreenTexture(r_bloom_colorscale.value);
3179                 // make the bloom texture
3180                 R_Bloom_MakeTexture();
3181                 // put the original screen image back in place and blend the bloom
3182                 // texture on it
3183                 R_ResetViewRendering2D();
3184                 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3185                 R_Mesh_ColorPointer(NULL, 0, 0);
3186                 GL_Color(1, 1, 1, 1);
3187                 GL_BlendFunc(GL_ONE, GL_ZERO);
3188                 // do both in one pass if possible
3189                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3190                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3191                 if (r_textureunits.integer >= 2 && gl_combine.integer)
3192                 {
3193                         R_Mesh_TexCombine(1, GL_ADD, GL_ADD, 1, 1);
3194                         R_Mesh_TexBind(1, R_GetTexture(r_bloomstate.texture_screen));
3195                         R_Mesh_TexCoordPointer(1, 2, r_bloomstate.screentexcoord2f, 0, 0);
3196                 }
3197                 else
3198                 {
3199                         R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3200                         r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3201                         // now blend on the bloom texture
3202                         GL_BlendFunc(GL_ONE, GL_ONE);
3203                         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
3204                         R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
3205                 }
3206                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3207                 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3208         }
3209         if (r_refdef.viewblend[3] >= (1.0f / 256.0f))
3210         {
3211                 // apply a color tint to the whole view
3212                 R_ResetViewRendering2D();
3213                 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3214                 R_Mesh_ColorPointer(NULL, 0, 0);
3215                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3216                 GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
3217                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3218         }
3219 }
3220
3221 void R_RenderScene(qboolean addwaterplanes);
3222
3223 matrix4x4_t r_waterscrollmatrix;
3224
3225 void R_UpdateVariables(void)
3226 {
3227         R_Textures_Frame();
3228
3229         r_refdef.farclip = 4096;
3230         if (r_refdef.worldmodel)
3231                 r_refdef.farclip += VectorDistance(r_refdef.worldmodel->normalmins, r_refdef.worldmodel->normalmaxs);
3232         r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
3233
3234         if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
3235                 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
3236         r_refdef.polygonfactor = 0;
3237         r_refdef.polygonoffset = 0;
3238         r_refdef.shadowpolygonfactor = r_refdef.polygonfactor + r_shadow_polygonfactor.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
3239         r_refdef.shadowpolygonoffset = r_refdef.polygonoffset + r_shadow_polygonoffset.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
3240
3241         r_refdef.rtworld = r_shadow_realtime_world.integer;
3242         r_refdef.rtworldshadows = r_shadow_realtime_world_shadows.integer && gl_stencil;
3243         r_refdef.rtdlight = (r_shadow_realtime_world.integer || r_shadow_realtime_dlight.integer) && !gl_flashblend.integer && r_dynamic.integer;
3244         r_refdef.rtdlightshadows = r_refdef.rtdlight && r_shadow_realtime_dlight_shadows.integer && gl_stencil;
3245         r_refdef.lightmapintensity = r_refdef.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
3246         if (r_showsurfaces.integer)
3247         {
3248                 r_refdef.rtworld = false;
3249                 r_refdef.rtworldshadows = false;
3250                 r_refdef.rtdlight = false;
3251                 r_refdef.rtdlightshadows = false;
3252                 r_refdef.lightmapintensity = 0;
3253         }
3254
3255         if (gamemode == GAME_NEHAHRA)
3256         {
3257                 if (gl_fogenable.integer)
3258                 {
3259                         r_refdef.oldgl_fogenable = true;
3260                         r_refdef.fog_density = gl_fogdensity.value;
3261                         r_refdef.fog_red = gl_fogred.value;
3262                         r_refdef.fog_green = gl_foggreen.value;
3263                         r_refdef.fog_blue = gl_fogblue.value;
3264                 }
3265                 else if (r_refdef.oldgl_fogenable)
3266                 {
3267                         r_refdef.oldgl_fogenable = false;
3268                         r_refdef.fog_density = 0;
3269                         r_refdef.fog_red = 0;
3270                         r_refdef.fog_green = 0;
3271                         r_refdef.fog_blue = 0;
3272                 }
3273         }
3274         if (r_refdef.fog_density)
3275         {
3276                 r_refdef.fogcolor[0] = bound(0.0f, r_refdef.fog_red  , 1.0f);
3277                 r_refdef.fogcolor[1] = bound(0.0f, r_refdef.fog_green, 1.0f);
3278                 r_refdef.fogcolor[2] = bound(0.0f, r_refdef.fog_blue , 1.0f);
3279         }
3280         if (r_refdef.fog_density)
3281         {
3282                 r_refdef.fogenabled = true;
3283                 // this is the point where the fog reaches 0.9986 alpha, which we
3284                 // consider a good enough cutoff point for the texture
3285                 // (0.9986 * 256 == 255.6)
3286                 r_refdef.fogrange = 400 / r_refdef.fog_density;
3287                 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
3288                 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
3289                 // fog color was already set
3290         }
3291         else
3292                 r_refdef.fogenabled = false;
3293 }
3294
3295 /*
3296 ================
3297 R_RenderView
3298 ================
3299 */
3300 void R_RenderView(void)
3301 {
3302         if (!r_refdef.entities/* || !r_refdef.worldmodel*/)
3303                 return; //Host_Error ("R_RenderView: NULL worldmodel");
3304
3305         R_Shadow_UpdateWorldLightSelection();
3306
3307         R_Bloom_StartFrame();
3308         R_Water_StartFrame();
3309
3310         CHECKGLERROR
3311         if (r_timereport_active)
3312                 R_TimeReport("viewsetup");
3313
3314         R_ResetViewRendering3D();
3315
3316         if (r_view.clear)
3317         {
3318                 R_ClearScreen();
3319                 if (r_timereport_active)
3320                         R_TimeReport("viewclear");
3321         }
3322         r_view.clear = true;
3323
3324         r_view.showdebug = true;
3325
3326         // this produces a bloom texture to be used in R_BlendView() later
3327         if (r_hdr.integer)
3328                 R_HDR_RenderBloomTexture();
3329
3330         r_view.colorscale = r_hdr_scenebrightness.value;
3331         r_waterstate.numwaterplanes = 0;
3332         R_RenderScene(r_waterstate.enabled);
3333
3334         R_BlendView();
3335         if (r_timereport_active)
3336                 R_TimeReport("blendview");
3337
3338         GL_Scissor(0, 0, vid.width, vid.height);
3339         GL_ScissorTest(false);
3340         CHECKGLERROR
3341 }
3342
3343 extern void R_DrawLightningBeams (void);
3344 extern void VM_CL_AddPolygonsToMeshQueue (void);
3345 extern void R_DrawPortals (void);
3346 extern cvar_t cl_locs_show;
3347 static void R_DrawLocs(void);
3348 static void R_DrawEntityBBoxes(void);
3349 void R_RenderScene(qboolean addwaterplanes)
3350 {
3351         if (addwaterplanes)
3352         {
3353                 R_ResetViewRendering3D();
3354
3355                 R_View_Update();
3356                 if (r_timereport_active)
3357                         R_TimeReport("watervis");
3358
3359                 if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->DrawAddWaterPlanes)
3360                 {
3361                         r_refdef.worldmodel->DrawAddWaterPlanes(r_refdef.worldentity);
3362                         if (r_timereport_active)
3363                                 R_TimeReport("waterworld");
3364                 }
3365
3366                 // don't let sound skip if going slow
3367                 if (r_refdef.extraupdate)
3368                         S_ExtraUpdate ();
3369
3370                 R_DrawModelsAddWaterPlanes();
3371                 if (r_timereport_active)
3372                         R_TimeReport("watermodels");
3373
3374                 R_Water_ProcessPlanes();
3375                 if (r_timereport_active)
3376                         R_TimeReport("waterscenes");
3377         }
3378
3379         R_ResetViewRendering3D();
3380
3381         // don't let sound skip if going slow
3382         if (r_refdef.extraupdate)
3383                 S_ExtraUpdate ();
3384
3385         R_MeshQueue_BeginScene();
3386
3387         R_SkyStartFrame();
3388
3389         R_View_Update();
3390         if (r_timereport_active)
3391                 R_TimeReport("visibility");
3392
3393         Matrix4x4_CreateTranslate(&r_waterscrollmatrix, sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
3394
3395         if (cl.csqc_vidvars.drawworld)
3396         {
3397                 // don't let sound skip if going slow
3398                 if (r_refdef.extraupdate)
3399                         S_ExtraUpdate ();
3400
3401                 if (r_refdef.worldmodel && r_refdef.worldmodel->DrawSky)
3402                 {
3403                         r_refdef.worldmodel->DrawSky(r_refdef.worldentity);
3404                         if (r_timereport_active)
3405                                 R_TimeReport("worldsky");
3406                 }
3407
3408                 if (R_DrawBrushModelsSky() && r_timereport_active)
3409                         R_TimeReport("bmodelsky");
3410         }
3411
3412         if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->DrawDepth)
3413         {
3414                 r_refdef.worldmodel->DrawDepth(r_refdef.worldentity);
3415                 if (r_timereport_active)
3416                         R_TimeReport("worlddepth");
3417         }
3418         if (r_depthfirst.integer >= 2)
3419         {
3420                 R_DrawModelsDepth();
3421                 if (r_timereport_active)
3422                         R_TimeReport("modeldepth");
3423         }
3424
3425         if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->Draw)
3426         {
3427                 r_refdef.worldmodel->Draw(r_refdef.worldentity);
3428                 if (r_timereport_active)
3429                         R_TimeReport("world");
3430         }
3431
3432         // don't let sound skip if going slow
3433         if (r_refdef.extraupdate)
3434                 S_ExtraUpdate ();
3435
3436         R_DrawModels();
3437         if (r_timereport_active)
3438                 R_TimeReport("models");
3439
3440         // don't let sound skip if going slow
3441         if (r_refdef.extraupdate)
3442                 S_ExtraUpdate ();
3443
3444         if (r_shadows.integer > 0 && r_refdef.lightmapintensity > 0)
3445         {
3446                 R_DrawModelShadows();
3447
3448                 R_ResetViewRendering3D();
3449
3450                 // don't let sound skip if going slow
3451                 if (r_refdef.extraupdate)
3452                         S_ExtraUpdate ();
3453         }
3454
3455         R_ShadowVolumeLighting(false);
3456         if (r_timereport_active)
3457                 R_TimeReport("rtlights");
3458
3459         // don't let sound skip if going slow
3460         if (r_refdef.extraupdate)
3461                 S_ExtraUpdate ();
3462
3463         if (cl.csqc_vidvars.drawworld)
3464         {
3465                 R_DrawLightningBeams();
3466                 if (r_timereport_active)
3467                         R_TimeReport("lightning");
3468
3469                 R_DrawDecals();
3470                 if (r_timereport_active)
3471                         R_TimeReport("decals");
3472
3473                 R_DrawParticles();
3474                 if (r_timereport_active)
3475                         R_TimeReport("particles");
3476
3477                 R_DrawExplosions();
3478                 if (r_timereport_active)
3479                         R_TimeReport("explosions");
3480         }
3481
3482         if (gl_support_fragment_shader)
3483         {
3484                 qglUseProgramObjectARB(0);CHECKGLERROR
3485         }
3486         VM_CL_AddPolygonsToMeshQueue();
3487
3488         if (r_view.showdebug)
3489         {
3490                 if (cl_locs_show.integer)
3491                 {
3492                         R_DrawLocs();
3493                         if (r_timereport_active)
3494                                 R_TimeReport("showlocs");
3495                 }
3496
3497                 if (r_drawportals.integer)
3498                 {
3499                         R_DrawPortals();
3500                         if (r_timereport_active)
3501                                 R_TimeReport("portals");
3502                 }
3503
3504                 if (r_showbboxes.value > 0)
3505                 {
3506                         R_DrawEntityBBoxes();
3507                         if (r_timereport_active)
3508                                 R_TimeReport("bboxes");
3509                 }
3510         }
3511
3512         if (gl_support_fragment_shader)
3513         {
3514                 qglUseProgramObjectARB(0);CHECKGLERROR
3515         }
3516         R_MeshQueue_RenderTransparent();
3517         if (r_timereport_active)
3518                 R_TimeReport("drawtrans");
3519
3520         if (gl_support_fragment_shader)
3521         {
3522                 qglUseProgramObjectARB(0);CHECKGLERROR
3523         }
3524
3525         if (r_view.showdebug && r_refdef.worldmodel && r_refdef.worldmodel->DrawDebug && (r_showtris.value > 0 || r_shownormals.value > 0 || r_showcollisionbrushes.value > 0))
3526         {
3527                 r_refdef.worldmodel->DrawDebug(r_refdef.worldentity);
3528                 if (r_timereport_active)
3529                         R_TimeReport("worlddebug");
3530                 R_DrawModelsDebug();
3531                 if (r_timereport_active)
3532                         R_TimeReport("modeldebug");
3533         }
3534
3535         if (gl_support_fragment_shader)
3536         {
3537                 qglUseProgramObjectARB(0);CHECKGLERROR
3538         }
3539
3540         if (cl.csqc_vidvars.drawworld)
3541         {
3542                 R_DrawCoronas();
3543                 if (r_timereport_active)
3544                         R_TimeReport("coronas");
3545         }
3546
3547         // don't let sound skip if going slow
3548         if (r_refdef.extraupdate)
3549                 S_ExtraUpdate ();
3550
3551         R_ResetViewRendering2D();
3552 }
3553
3554 static const int bboxelements[36] =
3555 {
3556         5, 1, 3, 5, 3, 7,
3557         6, 2, 0, 6, 0, 4,
3558         7, 3, 2, 7, 2, 6,
3559         4, 0, 1, 4, 1, 5,
3560         4, 5, 7, 4, 7, 6,
3561         1, 0, 2, 1, 2, 3,
3562 };
3563
3564 void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
3565 {
3566         int i;
3567         float *v, *c, f1, f2, vertex3f[8*3], color4f[8*4];
3568         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3569         GL_DepthMask(false);
3570         GL_DepthRange(0, 1);
3571         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3572         R_Mesh_Matrix(&identitymatrix);
3573         R_Mesh_ResetTextureState();
3574
3575         vertex3f[ 0] = mins[0];vertex3f[ 1] = mins[1];vertex3f[ 2] = mins[2]; //
3576         vertex3f[ 3] = maxs[0];vertex3f[ 4] = mins[1];vertex3f[ 5] = mins[2];
3577         vertex3f[ 6] = mins[0];vertex3f[ 7] = maxs[1];vertex3f[ 8] = mins[2];
3578         vertex3f[ 9] = maxs[0];vertex3f[10] = maxs[1];vertex3f[11] = mins[2];
3579         vertex3f[12] = mins[0];vertex3f[13] = mins[1];vertex3f[14] = maxs[2];
3580         vertex3f[15] = maxs[0];vertex3f[16] = mins[1];vertex3f[17] = maxs[2];
3581         vertex3f[18] = mins[0];vertex3f[19] = maxs[1];vertex3f[20] = maxs[2];
3582         vertex3f[21] = maxs[0];vertex3f[22] = maxs[1];vertex3f[23] = maxs[2];
3583         R_FillColors(color4f, 8, cr, cg, cb, ca);
3584         if (r_refdef.fogenabled)
3585         {
3586                 for (i = 0, v = vertex3f, c = color4f;i < 8;i++, v += 3, c += 4)
3587                 {
3588                         f1 = FogPoint_World(v);
3589                         f2 = 1 - f1;
3590                         c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
3591                         c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
3592                         c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
3593                 }
3594         }
3595         R_Mesh_VertexPointer(vertex3f, 0, 0);
3596         R_Mesh_ColorPointer(color4f, 0, 0);
3597         R_Mesh_ResetTextureState();
3598         R_Mesh_Draw(0, 8, 12, bboxelements, 0, 0);
3599 }
3600
3601 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3602 {
3603         int i;
3604         float color[4];
3605         prvm_edict_t *edict;
3606         // this function draws bounding boxes of server entities
3607         if (!sv.active)
3608                 return;
3609         SV_VM_Begin();
3610         for (i = 0;i < numsurfaces;i++)
3611         {
3612                 edict = PRVM_EDICT_NUM(surfacelist[i]);
3613                 switch ((int)edict->fields.server->solid)
3614                 {
3615                         case SOLID_NOT:      Vector4Set(color, 1, 1, 1, 0.05);break;
3616                         case SOLID_TRIGGER:  Vector4Set(color, 1, 0, 1, 0.10);break;
3617                         case SOLID_BBOX:     Vector4Set(color, 0, 1, 0, 0.10);break;
3618                         case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
3619                         case SOLID_BSP:      Vector4Set(color, 0, 0, 1, 0.05);break;
3620                         default:             Vector4Set(color, 0, 0, 0, 0.50);break;
3621                 }
3622                 color[3] *= r_showbboxes.value;
3623                 color[3] = bound(0, color[3], 1);
3624                 GL_DepthTest(!r_showdisabledepthtest.integer);
3625                 GL_CullFace(r_view.cullface_front);
3626                 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
3627         }
3628         SV_VM_End();
3629 }
3630
3631 static void R_DrawEntityBBoxes(void)
3632 {
3633         int i;
3634         prvm_edict_t *edict;
3635         vec3_t center;
3636         // this function draws bounding boxes of server entities
3637         if (!sv.active)
3638                 return;
3639         SV_VM_Begin();
3640         for (i = 0;i < prog->num_edicts;i++)
3641         {
3642                 edict = PRVM_EDICT_NUM(i);
3643                 if (edict->priv.server->free)
3644                         continue;
3645                 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
3646                 R_MeshQueue_AddTransparent(center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)NULL);
3647         }
3648         SV_VM_End();
3649 }
3650
3651 int nomodelelements[24] =
3652 {
3653         5, 2, 0,
3654         5, 1, 2,
3655         5, 0, 3,
3656         5, 3, 1,
3657         0, 2, 4,
3658         2, 1, 4,
3659         3, 0, 4,
3660         1, 3, 4
3661 };
3662
3663 float nomodelvertex3f[6*3] =
3664 {
3665         -16,   0,   0,
3666          16,   0,   0,
3667           0, -16,   0,
3668           0,  16,   0,
3669           0,   0, -16,
3670           0,   0,  16
3671 };
3672
3673 float nomodelcolor4f[6*4] =
3674 {
3675         0.0f, 0.0f, 0.5f, 1.0f,
3676         0.0f, 0.0f, 0.5f, 1.0f,
3677         0.0f, 0.5f, 0.0f, 1.0f,
3678         0.0f, 0.5f, 0.0f, 1.0f,
3679         0.5f, 0.0f, 0.0f, 1.0f,
3680         0.5f, 0.0f, 0.0f, 1.0f
3681 };
3682
3683 void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3684 {
3685         int i;
3686         float f1, f2, *c;
3687         float color4f[6*4];
3688         // this is only called once per entity so numsurfaces is always 1, and
3689         // surfacelist is always {0}, so this code does not handle batches
3690         R_Mesh_Matrix(&ent->matrix);
3691
3692         if (ent->flags & EF_ADDITIVE)
3693         {
3694                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
3695                 GL_DepthMask(false);
3696         }
3697         else if (ent->alpha < 1)
3698         {
3699                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3700                 GL_DepthMask(false);
3701         }
3702         else
3703         {
3704                 GL_BlendFunc(GL_ONE, GL_ZERO);
3705                 GL_DepthMask(true);
3706         }
3707         GL_DepthRange(0, (ent->flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
3708         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3709         GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
3710         GL_CullFace((ent->effects & EF_DOUBLESIDED) ? GL_NONE : r_view.cullface_back);
3711         R_Mesh_VertexPointer(nomodelvertex3f, 0, 0);
3712         if (r_refdef.fogenabled)
3713         {
3714                 vec3_t org;
3715                 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
3716                 R_Mesh_ColorPointer(color4f, 0, 0);
3717                 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3718                 f1 = FogPoint_World(org);
3719                 f2 = 1 - f1;
3720                 for (i = 0, c = color4f;i < 6;i++, c += 4)
3721                 {
3722                         c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
3723                         c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
3724                         c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
3725                         c[3] *= ent->alpha;
3726                 }
3727         }
3728         else if (ent->alpha != 1)
3729         {
3730                 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
3731                 R_Mesh_ColorPointer(color4f, 0, 0);
3732                 for (i = 0, c = color4f;i < 6;i++, c += 4)
3733                         c[3] *= ent->alpha;
3734         }
3735         else
3736                 R_Mesh_ColorPointer(nomodelcolor4f, 0, 0);
3737         R_Mesh_ResetTextureState();
3738         R_Mesh_Draw(0, 6, 8, nomodelelements, 0, 0);
3739 }
3740
3741 void R_DrawNoModel(entity_render_t *ent)
3742 {
3743         vec3_t org;
3744         Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3745         //if ((ent->effects & EF_ADDITIVE) || (ent->alpha < 1))
3746                 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_view.origin : org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
3747         //else
3748         //      R_DrawNoModelCallback(ent, 0);
3749 }
3750
3751 void R_CalcBeam_Vertex3f (float *vert, const vec3_t org1, const vec3_t org2, float width)
3752 {
3753         vec3_t right1, right2, diff, normal;
3754
3755         VectorSubtract (org2, org1, normal);
3756
3757         // calculate 'right' vector for start
3758         VectorSubtract (r_view.origin, org1, diff);
3759         CrossProduct (normal, diff, right1);
3760         VectorNormalize (right1);
3761
3762         // calculate 'right' vector for end
3763         VectorSubtract (r_view.origin, org2, diff);
3764         CrossProduct (normal, diff, right2);
3765         VectorNormalize (right2);
3766
3767         vert[ 0] = org1[0] + width * right1[0];
3768         vert[ 1] = org1[1] + width * right1[1];
3769         vert[ 2] = org1[2] + width * right1[2];
3770         vert[ 3] = org1[0] - width * right1[0];
3771         vert[ 4] = org1[1] - width * right1[1];
3772         vert[ 5] = org1[2] - width * right1[2];
3773         vert[ 6] = org2[0] - width * right2[0];
3774         vert[ 7] = org2[1] - width * right2[1];
3775         vert[ 8] = org2[2] - width * right2[2];
3776         vert[ 9] = org2[0] + width * right2[0];
3777         vert[10] = org2[1] + width * right2[1];
3778         vert[11] = org2[2] + width * right2[2];
3779 }
3780
3781 float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
3782
3783 void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_t *fogtexture, qboolean depthdisable, qboolean depthshort, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2, float cr, float cg, float cb, float ca)
3784 {
3785         float fog = 1.0f;
3786         float vertex3f[12];
3787
3788         if (r_refdef.fogenabled)
3789                 fog = FogPoint_World(origin);
3790
3791         R_Mesh_Matrix(&identitymatrix);
3792         GL_BlendFunc(blendfunc1, blendfunc2);
3793
3794         if(v_flipped_state)
3795         {
3796                 scalex1 = -scalex1;
3797                 scalex2 = -scalex2;
3798                 GL_CullFace(r_view.cullface_front);
3799         }
3800         else
3801                 GL_CullFace(r_view.cullface_back);
3802
3803         GL_DepthMask(false);
3804         GL_DepthRange(0, depthshort ? 0.0625 : 1);
3805         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3806         GL_DepthTest(!depthdisable);
3807
3808         vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
3809         vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
3810         vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
3811         vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
3812         vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
3813         vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
3814         vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
3815         vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
3816         vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
3817         vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
3818         vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
3819         vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
3820
3821         R_Mesh_VertexPointer(vertex3f, 0, 0);
3822         R_Mesh_ColorPointer(NULL, 0, 0);
3823         R_Mesh_ResetTextureState();
3824         R_Mesh_TexBind(0, R_GetTexture(texture));
3825         R_Mesh_TexCoordPointer(0, 2, spritetexcoord2f, 0, 0);
3826         // FIXME: fixed function path can't properly handle r_view.colorscale > 1
3827         GL_Color(cr * fog * r_view.colorscale, cg * fog * r_view.colorscale, cb * fog * r_view.colorscale, ca);
3828         R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3829
3830         if (blendfunc2 == GL_ONE_MINUS_SRC_ALPHA)
3831         {
3832                 R_Mesh_TexBind(0, R_GetTexture(fogtexture));
3833                 GL_BlendFunc(blendfunc1, GL_ONE);
3834                 fog = 1 - fog;
3835                 GL_Color(r_refdef.fogcolor[0] * fog * r_view.colorscale, r_refdef.fogcolor[1] * fog * r_view.colorscale, r_refdef.fogcolor[2] * fog * r_view.colorscale, ca);
3836                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3837         }
3838 }
3839
3840 int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
3841 {
3842         int i;
3843         float *vertex3f;
3844         float v[3];
3845         VectorSet(v, x, y, z);
3846         for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
3847                 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
3848                         break;
3849         if (i == mesh->numvertices)
3850         {
3851                 if (mesh->numvertices < mesh->maxvertices)
3852                 {
3853                         VectorCopy(v, vertex3f);
3854                         mesh->numvertices++;
3855                 }
3856                 return mesh->numvertices;
3857         }
3858         else
3859                 return i;
3860 }
3861
3862 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
3863 {
3864         int i;
3865         int *e, element[3];
3866         element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
3867         element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
3868         e = mesh->element3i + mesh->numtriangles * 3;
3869         for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
3870         {
3871                 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
3872                 if (mesh->numtriangles < mesh->maxtriangles)
3873                 {
3874                         *e++ = element[0];
3875                         *e++ = element[1];
3876                         *e++ = element[2];
3877                         mesh->numtriangles++;
3878                 }
3879                 element[1] = element[2];
3880         }
3881 }
3882
3883 void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
3884 {
3885         int i;
3886         int *e, element[3];
3887         element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
3888         element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
3889         e = mesh->element3i + mesh->numtriangles * 3;
3890         for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
3891         {
3892                 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
3893                 if (mesh->numtriangles < mesh->maxtriangles)
3894                 {
3895                         *e++ = element[0];
3896                         *e++ = element[1];
3897                         *e++ = element[2];
3898                         mesh->numtriangles++;
3899                 }
3900                 element[1] = element[2];
3901         }
3902 }
3903
3904 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
3905 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
3906 {
3907         int planenum, planenum2;
3908         int w;
3909         int tempnumpoints;
3910         mplane_t *plane, *plane2;
3911         double maxdist;
3912         double temppoints[2][256*3];
3913         // figure out how large a bounding box we need to properly compute this brush
3914         maxdist = 0;
3915         for (w = 0;w < numplanes;w++)
3916                 maxdist = max(maxdist, planes[w].dist);
3917         // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
3918         maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
3919         for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
3920         {
3921                 w = 0;
3922                 tempnumpoints = 4;
3923                 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
3924                 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
3925                 {
3926                         if (planenum2 == planenum)
3927                                 continue;
3928                         PolygonD_Divide(tempnumpoints, temppoints[w], plane2->normal[0], plane2->normal[1], plane2->normal[2], plane2->dist, R_MESH_PLANE_DIST_EPSILON, 0, NULL, NULL, 256, temppoints[!w], &tempnumpoints, NULL);
3929                         w = !w;
3930                 }
3931                 if (tempnumpoints < 3)
3932                         continue;
3933                 // generate elements forming a triangle fan for this polygon
3934                 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
3935         }
3936 }
3937
3938 static void R_Texture_AddLayer(texture_t *t, qboolean depthmask, int blendfunc1, int blendfunc2, texturelayertype_t type, rtexture_t *texture, const matrix4x4_t *matrix, float r, float g, float b, float a)
3939 {
3940         texturelayer_t *layer;
3941         layer = t->currentlayers + t->currentnumlayers++;
3942         layer->type = type;
3943         layer->depthmask = depthmask;
3944         layer->blendfunc1 = blendfunc1;
3945         layer->blendfunc2 = blendfunc2;
3946         layer->texture = texture;
3947         layer->texmatrix = *matrix;
3948         layer->color[0] = r * r_view.colorscale;
3949         layer->color[1] = g * r_view.colorscale;
3950         layer->color[2] = b * r_view.colorscale;
3951         layer->color[3] = a;
3952 }
3953
3954 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
3955 {
3956         double index, f;
3957         index = parms[2] + r_refdef.time * parms[3];
3958         index -= floor(index);
3959         switch (func)
3960         {
3961         default:
3962         case Q3WAVEFUNC_NONE:
3963         case Q3WAVEFUNC_NOISE:
3964         case Q3WAVEFUNC_COUNT:
3965                 f = 0;
3966                 break;
3967         case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
3968         case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
3969         case Q3WAVEFUNC_SAWTOOTH: f = index;break;
3970         case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
3971         case Q3WAVEFUNC_TRIANGLE:
3972                 index *= 4;
3973                 f = index - floor(index);
3974                 if (index < 1)
3975                         f = f;
3976                 else if (index < 2)
3977                         f = 1 - f;
3978                 else if (index < 3)
3979                         f = -f;
3980                 else
3981                         f = -(1 - f);
3982                 break;
3983         }
3984         return (float)(parms[0] + parms[1] * f);
3985 }
3986
3987 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
3988 {
3989         int i;
3990         model_t *model = ent->model;
3991         float f;
3992         float tcmat[12];
3993         q3shaderinfo_layer_tcmod_t *tcmod;
3994
3995         // switch to an alternate material if this is a q1bsp animated material
3996         {
3997                 texture_t *texture = t;
3998                 int s = ent->skinnum;
3999                 if ((unsigned int)s >= (unsigned int)model->numskins)
4000                         s = 0;
4001                 if (model->skinscenes)
4002                 {
4003                         if (model->skinscenes[s].framecount > 1)
4004                                 s = model->skinscenes[s].firstframe + (unsigned int) (r_refdef.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
4005                         else
4006                                 s = model->skinscenes[s].firstframe;
4007                 }
4008                 if (s > 0)
4009                         t = t + s * model->num_surfaces;
4010                 if (t->animated)
4011                 {
4012                         // use an alternate animation if the entity's frame is not 0,
4013                         // and only if the texture has an alternate animation
4014                         if (ent->frame2 != 0 && t->anim_total[1])
4015                                 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[1]) : 0];
4016                         else
4017                                 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[0]) : 0];
4018                 }
4019                 texture->currentframe = t;
4020         }
4021
4022         // update currentskinframe to be a qw skin or animation frame
4023         if ((i = ent->entitynumber - 1) >= 0 && i < cl.maxclients)
4024         {
4025                 if (strcmp(r_qwskincache[i], cl.scores[i].qw_skin))
4026                 {
4027                         strlcpy(r_qwskincache[i], cl.scores[i].qw_skin, sizeof(r_qwskincache[i]));
4028                         Con_DPrintf("loading skins/%s\n", r_qwskincache[i]);
4029                         r_qwskincache_skinframe[i] = R_SkinFrame_LoadExternal(va("skins/%s", r_qwskincache[i]), TEXF_PRECACHE | (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_PICMIP | TEXF_COMPRESS, developer.integer > 0);
4030                 }
4031                 t->currentskinframe = r_qwskincache_skinframe[i];
4032                 if (t->currentskinframe == NULL)
4033                         t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->frame2time)) % t->numskinframes];
4034         }
4035         else if (t->numskinframes >= 2)
4036                 t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->frame2time)) % t->numskinframes];
4037         if (t->backgroundnumskinframes >= 2)
4038                 t->backgroundcurrentskinframe = t->backgroundskinframes[(int)(t->backgroundskinframerate * (cl.time - ent->frame2time)) % t->backgroundnumskinframes];
4039
4040         t->currentmaterialflags = t->basematerialflags;
4041         t->currentalpha = ent->alpha;
4042         if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_novis.integer))
4043         {
4044                 t->currentalpha *= r_wateralpha.value;
4045                 /*
4046                  * FIXME what is this supposed to do?
4047                 // if rendering refraction/reflection, disable transparency
4048                 if (r_waterstate.enabled && (t->currentalpha < 1 || (t->currentmaterialflags & MATERIALFLAG_ALPHA)))
4049                         t->currentmaterialflags |= MATERIALFLAG_WATERSHADER;
4050                 */
4051         }
4052         if(!r_waterstate.enabled)
4053                 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION);
4054         if (!(ent->flags & RENDER_LIGHT))
4055                 t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
4056         else if (rsurface.modeltexcoordlightmap2f == NULL)
4057         {
4058                 // pick a model lighting mode
4059                 if (VectorLength2(ent->modellight_diffuse) >= (1.0f / 256.0f))
4060                         t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT | MATERIALFLAG_MODELLIGHT_DIRECTIONAL;
4061                 else
4062                         t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
4063         }
4064         if (ent->effects & EF_ADDITIVE)
4065                 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
4066         else if (t->currentalpha < 1)
4067                 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
4068         if (ent->effects & EF_DOUBLESIDED)
4069                 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
4070         if (ent->effects & EF_NODEPTHTEST)
4071                 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
4072         if (ent->flags & RENDER_VIEWMODEL)
4073                 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
4074         if (t->backgroundnumskinframes && !(t->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
4075                 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
4076
4077         for (i = 0, tcmod = t->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
4078         {
4079                 matrix4x4_t matrix;
4080                 switch(tcmod->tcmod)
4081                 {
4082                 case Q3TCMOD_COUNT:
4083                 case Q3TCMOD_NONE:
4084                         if (t->currentmaterialflags & MATERIALFLAG_WATER && r_waterscroll.value != 0)
4085                                 matrix = r_waterscrollmatrix;
4086                         else
4087                                 matrix = identitymatrix;
4088                         break;
4089                 case Q3TCMOD_ENTITYTRANSLATE:
4090                         // this is used in Q3 to allow the gamecode to control texcoord
4091                         // scrolling on the entity, which is not supported in darkplaces yet.
4092                         Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
4093                         break;
4094                 case Q3TCMOD_ROTATE:
4095                         Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
4096                         Matrix4x4_ConcatRotate(&matrix, tcmod->parms[0] * r_refdef.time, 0, 0, 1);
4097                         Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
4098                         break;
4099                 case Q3TCMOD_SCALE:
4100                         Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
4101                         break;
4102                 case Q3TCMOD_SCROLL:
4103                         Matrix4x4_CreateTranslate(&matrix, tcmod->parms[0] * r_refdef.time, tcmod->parms[1] * r_refdef.time, 0);
4104                         break;
4105                 case Q3TCMOD_STRETCH:
4106                         f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
4107                         Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
4108                         break;
4109                 case Q3TCMOD_TRANSFORM:
4110                         VectorSet(tcmat +  0, tcmod->parms[0], tcmod->parms[1], 0);
4111                         VectorSet(tcmat +  3, tcmod->parms[2], tcmod->parms[3], 0);
4112                         VectorSet(tcmat +  6, 0                   , 0                , 1);
4113                         VectorSet(tcmat +  9, tcmod->parms[4], tcmod->parms[5], 0);
4114                         Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
4115                         break;
4116                 case Q3TCMOD_TURBULENT:
4117                         // this is handled in the RSurf_PrepareVertices function
4118                         matrix = identitymatrix;
4119                         break;
4120                 }
4121                 // either replace or concatenate the transformation
4122                 if (i < 1)
4123                         t->currenttexmatrix = matrix;
4124                 else
4125                 {
4126                         matrix4x4_t temp = t->currenttexmatrix;
4127                         Matrix4x4_Concat(&t->currenttexmatrix, &matrix, &temp);
4128                 }
4129         }
4130
4131         t->colormapping = VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f);
4132         t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
4133         t->glosstexture = r_texture_black;
4134         t->backgroundbasetexture = t->backgroundnumskinframes ? ((!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base) : r_texture_white;
4135         t->backgroundglosstexture = r_texture_black;
4136         t->specularpower = r_shadow_glossexponent.value;
4137         // TODO: store reference values for these in the texture?
4138         t->specularscale = 0;
4139         if (r_shadow_gloss.integer > 0)
4140         {
4141                 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
4142                 {
4143                         if (r_shadow_glossintensity.value > 0)
4144                         {
4145                                 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
4146                                 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
4147                                 t->specularscale = r_shadow_glossintensity.value;
4148                         }
4149                 }
4150                 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
4151                 {
4152                         t->glosstexture = r_texture_white;
4153                         t->backgroundglosstexture = r_texture_white;
4154                         t->specularscale = r_shadow_gloss2intensity.value;
4155                 }
4156         }
4157
4158         // lightmaps mode looks bad with dlights using actual texturing, so turn
4159         // off the colormap and glossmap, but leave the normalmap on as it still
4160         // accurately represents the shading involved
4161         if (gl_lightmaps.integer && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
4162         {
4163                 t->basetexture = r_texture_white;
4164                 t->specularscale = 0;
4165         }
4166
4167         t->currentpolygonfactor = r_refdef.polygonfactor + t->basepolygonfactor;
4168         t->currentpolygonoffset = r_refdef.polygonoffset + t->basepolygonoffset;
4169         // submodels are biased to avoid z-fighting with world surfaces that they
4170         // may be exactly overlapping (avoids z-fighting artifacts on certain
4171         // doors and things in Quake maps)
4172         if (ent->model->brush.submodel)
4173         {
4174                 t->currentpolygonfactor += r_polygonoffset_submodel_factor.value;
4175                 t->currentpolygonoffset += r_polygonoffset_submodel_offset.value;
4176         }
4177
4178         VectorClear(t->dlightcolor);
4179         t->currentnumlayers = 0;
4180         if (!(t->currentmaterialflags & MATERIALFLAG_NODRAW))
4181         {
4182                 if (!(t->currentmaterialflags & MATERIALFLAG_SKY))
4183                 {
4184                         int blendfunc1, blendfunc2, depthmask;
4185                         if (t->currentmaterialflags & MATERIALFLAG_ADD)
4186                         {
4187                                 blendfunc1 = GL_SRC_ALPHA;
4188                                 blendfunc2 = GL_ONE;
4189                         }
4190                         else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
4191                         {
4192                                 blendfunc1 = GL_SRC_ALPHA;
4193                                 blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
4194                         }
4195                         else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
4196                         {
4197                                 blendfunc1 = t->customblendfunc[0];
4198                                 blendfunc2 = t->customblendfunc[1];
4199                         }
4200                         else
4201                         {
4202                                 blendfunc1 = GL_ONE;
4203                                 blendfunc2 = GL_ZERO;
4204                         }
4205                         depthmask = !(t->currentmaterialflags & MATERIALFLAG_BLENDED);
4206                         if (t->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
4207                         {
4208                                 rtexture_t *currentbasetexture;
4209                                 int layerflags = 0;
4210                                 if (r_refdef.fogenabled && (t->currentmaterialflags & MATERIALFLAG_BLENDED))
4211                                         layerflags |= TEXTURELAYERFLAG_FOGDARKEN;
4212                                 currentbasetexture = (VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) < (1.0f / 1048576.0f) && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
4213                                 if (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
4214                                 {
4215                                         // fullbright is not affected by r_refdef.lightmapintensity
4216                                         R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0], ent->colormod[1], ent->colormod[2], t->currentalpha);
4217                                         if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
4218                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0], ent->colormap_pantscolor[1] * ent->colormod[1], ent->colormap_pantscolor[2] * ent->colormod[2], t->currentalpha);
4219                                         if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
4220                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0], ent->colormap_shirtcolor[1] * ent->colormod[1], ent->colormap_shirtcolor[2] * ent->colormod[2], t->currentalpha);
4221                                 }
4222                                 else
4223                                 {
4224                                         float colorscale;
4225                                         // set the color tint used for lights affecting this surface
4226                                         VectorSet(t->dlightcolor, ent->colormod[0] * t->currentalpha, ent->colormod[1] * t->currentalpha, ent->colormod[2] * t->currentalpha);
4227                                         colorscale = 2;
4228                                         // q3bsp has no lightmap updates, so the lightstylevalue that
4229                                         // would normally be baked into the lightmap must be
4230                                         // applied to the color
4231                                         // FIXME: r_glsl 1 rendering doesn't support overbright lightstyles with this (the default light style is not overbright)
4232                                         if (ent->model->type == mod_brushq3)
4233                                                 colorscale *= r_refdef.lightstylevalue[0] * (1.0f / 256.0f);
4234                                         colorscale *= r_refdef.lightmapintensity;
4235                                         R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LITTEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * colorscale, ent->colormod[1] * colorscale, ent->colormod[2] * colorscale, t->currentalpha);
4236                                         if (r_ambient.value >= (1.0f/64.0f))
4237                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
4238                                         if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
4239                                         {
4240                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0] * colorscale, ent->colormap_pantscolor[1] * ent->colormod[1] * colorscale, ent->colormap_pantscolor[2]  * ent->colormod[2] * colorscale, t->currentalpha);
4241                                                 if (r_ambient.value >= (1.0f/64.0f))
4242                                                         R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[1] * ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[2] * ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
4243                                         }
4244                                         if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
4245                                         {
4246                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0] * colorscale, ent->colormap_shirtcolor[1] * ent->colormod[1] * colorscale, ent->colormap_shirtcolor[2] * ent->colormod[2] * colorscale, t->currentalpha);
4247                                                 if (r_ambient.value >= (1.0f/64.0f))
4248                                                         R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[1] * ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[2] * ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
4249                                         }
4250                                 }
4251                                 if (t->currentskinframe->glow != NULL)
4252                                         R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->glow, &t->currenttexmatrix, r_hdr_glowintensity.value, r_hdr_glowintensity.value, r_hdr_glowintensity.value, t->currentalpha);
4253                                 if (r_refdef.fogenabled && !(t->currentmaterialflags & MATERIALFLAG_ADD))
4254                                 {
4255                                         // if this is opaque use alpha blend which will darken the earlier
4256                                         // passes cheaply.
4257                                         //
4258                                         // if this is an alpha blended material, all the earlier passes
4259                                         // were darkened by fog already, so we only need to add the fog
4260                                         // color ontop through the fog mask texture
4261                                         //
4262                                         // if this is an additive blended material, all the earlier passes
4263                                         // were darkened by fog already, and we should not add fog color
4264                                         // (because the background was not darkened, there is no fog color
4265                                         // that was lost behind it).
4266                                         R_Texture_AddLayer(t, false, GL_SRC_ALPHA, (t->currentmaterialflags & MATERIALFLAG_BLENDED) ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, TEXTURELAYERTYPE_FOG, t->currentskinframe->fog, &identitymatrix, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], t->currentalpha);
4267                                 }
4268                         }
4269                 }
4270         }
4271 }
4272
4273 void R_UpdateAllTextureInfo(entity_render_t *ent)
4274 {
4275         int i;
4276         if (ent->model)
4277                 for (i = 0;i < ent->model->num_texturesperskin;i++)
4278                         R_UpdateTextureInfo(ent, ent->model->data_textures + i);
4279 }
4280
4281 rsurfacestate_t rsurface;
4282
4283 void R_Mesh_ResizeArrays(int newvertices)
4284 {
4285         float *base;
4286         if (rsurface.array_size >= newvertices)
4287                 return;
4288         if (rsurface.array_modelvertex3f)
4289                 Mem_Free(rsurface.array_modelvertex3f);
4290         rsurface.array_size = (newvertices + 1023) & ~1023;
4291         base = (float *)Mem_Alloc(r_main_mempool, rsurface.array_size * sizeof(float[33]));
4292         rsurface.array_modelvertex3f     = base + rsurface.array_size * 0;
4293         rsurface.array_modelsvector3f    = base + rsurface.array_size * 3;
4294         rsurface.array_modeltvector3f    = base + rsurface.array_size * 6;
4295         rsurface.array_modelnormal3f     = base + rsurface.array_size * 9;
4296         rsurface.array_deformedvertex3f  = base + rsurface.array_size * 12;
4297         rsurface.array_deformedsvector3f = base + rsurface.array_size * 15;
4298         rsurface.array_deformedtvector3f = base + rsurface.array_size * 18;
4299         rsurface.array_deformednormal3f  = base + rsurface.array_size * 21;
4300         rsurface.array_texcoord3f        = base + rsurface.array_size * 24;
4301         rsurface.array_color4f           = base + rsurface.array_size * 27;
4302         rsurface.array_generatedtexcoordtexture2f = base + rsurface.array_size * 31;
4303 }
4304
4305 void RSurf_CleanUp(void)
4306 {
4307         CHECKGLERROR
4308         if (rsurface.mode == RSURFMODE_GLSL)
4309         {
4310                 qglUseProgramObjectARB(0);CHECKGLERROR
4311         }
4312         GL_AlphaTest(false);
4313         rsurface.mode = RSURFMODE_NONE;
4314         rsurface.uselightmaptexture = false;
4315         rsurface.texture = NULL;
4316 }
4317
4318 void RSurf_ActiveWorldEntity(void)
4319 {
4320         model_t *model = r_refdef.worldmodel;
4321         RSurf_CleanUp();
4322         if (rsurface.array_size < model->surfmesh.num_vertices)
4323                 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
4324         rsurface.matrix = identitymatrix;
4325         rsurface.inversematrix = identitymatrix;
4326         R_Mesh_Matrix(&identitymatrix);
4327         VectorCopy(r_view.origin, rsurface.modelorg);
4328         VectorSet(rsurface.modellight_ambient, 0, 0, 0);
4329         VectorSet(rsurface.modellight_diffuse, 0, 0, 0);
4330         VectorSet(rsurface.modellight_lightdir, 0, 0, 1);
4331         VectorSet(rsurface.colormap_pantscolor, 0, 0, 0);
4332         VectorSet(rsurface.colormap_shirtcolor, 0, 0, 0);
4333         rsurface.frameblend[0].frame = 0;
4334         rsurface.frameblend[0].lerp = 1;
4335         rsurface.frameblend[1].frame = 0;
4336         rsurface.frameblend[1].lerp = 0;
4337         rsurface.frameblend[2].frame = 0;
4338         rsurface.frameblend[2].lerp = 0;
4339         rsurface.frameblend[3].frame = 0;
4340         rsurface.frameblend[3].lerp = 0;
4341         rsurface.modelvertex3f  = model->surfmesh.data_vertex3f;
4342         rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
4343         rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
4344         rsurface.modelsvector3f = model->surfmesh.data_svector3f;
4345         rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
4346         rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
4347         rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
4348         rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
4349         rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
4350         rsurface.modelnormal3f  = model->surfmesh.data_normal3f;
4351         rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
4352         rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
4353         rsurface.modellightmapcolor4f  = model->surfmesh.data_lightmapcolor4f;
4354         rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
4355         rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
4356         rsurface.modeltexcoordtexture2f  = model->surfmesh.data_texcoordtexture2f;
4357         rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
4358         rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
4359         rsurface.modeltexcoordlightmap2f  = model->surfmesh.data_texcoordlightmap2f;
4360         rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
4361         rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
4362         rsurface.modelelement3i = model->surfmesh.data_element3i;
4363         rsurface.modelelement3i_bufferobject = model->surfmesh.ebo;
4364         rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
4365         rsurface.modelnum_vertices = model->surfmesh.num_vertices;
4366         rsurface.modelnum_triangles = model->surfmesh.num_triangles;
4367         rsurface.modelsurfaces = model->data_surfaces;
4368         rsurface.generatedvertex = false;
4369         rsurface.vertex3f  = rsurface.modelvertex3f;
4370         rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4371         rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4372         rsurface.svector3f = rsurface.modelsvector3f;
4373         rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4374         rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4375         rsurface.tvector3f = rsurface.modeltvector3f;
4376         rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4377         rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4378         rsurface.normal3f  = rsurface.modelnormal3f;
4379         rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4380         rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4381         rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4382 }
4383
4384 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
4385 {
4386         model_t *model = ent->model;
4387         RSurf_CleanUp();
4388         if (rsurface.array_size < model->surfmesh.num_vertices)
4389                 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
4390         rsurface.matrix = ent->matrix;
4391         rsurface.inversematrix = ent->inversematrix;
4392         R_Mesh_Matrix(&rsurface.matrix);
4393         Matrix4x4_Transform(&rsurface.inversematrix, r_view.origin, rsurface.modelorg);
4394         VectorCopy(ent->modellight_ambient, rsurface.modellight_ambient);
4395         VectorCopy(ent->modellight_diffuse, rsurface.modellight_diffuse);
4396         VectorCopy(ent->modellight_lightdir, rsurface.modellight_lightdir);
4397         VectorCopy(ent->colormap_pantscolor, rsurface.colormap_pantscolor);
4398         VectorCopy(ent->colormap_shirtcolor, rsurface.colormap_shirtcolor);
4399         rsurface.frameblend[0] = ent->frameblend[0];
4400         rsurface.frameblend[1] = ent->frameblend[1];
4401         rsurface.frameblend[2] = ent->frameblend[2];
4402         rsurface.frameblend[3] = ent->frameblend[3];
4403         if (model->surfmesh.isanimated && (rsurface.frameblend[0].lerp != 1 || rsurface.frameblend[0].frame != 0))
4404         {
4405                 if (wanttangents)
4406                 {
4407                         rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4408                         rsurface.modelsvector3f = rsurface.array_modelsvector3f;
4409                         rsurface.modeltvector3f = rsurface.array_modeltvector3f;
4410                         rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4411                         Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, rsurface.array_modelsvector3f, rsurface.array_modeltvector3f);
4412                 }
4413                 else if (wantnormals)
4414                 {
4415                         rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4416                         rsurface.modelsvector3f = NULL;
4417                         rsurface.modeltvector3f = NULL;
4418                         rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4419                         Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, NULL, NULL);
4420                 }
4421                 else
4422                 {
4423                         rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4424                         rsurface.modelsvector3f = NULL;
4425                         rsurface.modeltvector3f = NULL;
4426                         rsurface.modelnormal3f = NULL;
4427                         Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, NULL, NULL, NULL);
4428                 }
4429                 rsurface.modelvertex3f_bufferobject = 0;
4430                 rsurface.modelvertex3f_bufferoffset = 0;
4431                 rsurface.modelsvector3f_bufferobject = 0;
4432                 rsurface.modelsvector3f_bufferoffset = 0;
4433                 rsurface.modeltvector3f_bufferobject = 0;
4434                 rsurface.modeltvector3f_bufferoffset = 0;
4435                 rsurface.modelnormal3f_bufferobject = 0;
4436                 rsurface.modelnormal3f_bufferoffset = 0;
4437                 rsurface.generatedvertex = true;
4438         }
4439         else
4440         {
4441                 rsurface.modelvertex3f  = model->surfmesh.data_vertex3f;
4442                 rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
4443                 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
4444                 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
4445                 rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
4446                 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
4447                 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
4448                 rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
4449                 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
4450                 rsurface.modelnormal3f  = model->surfmesh.data_normal3f;
4451                 rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
4452                 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
4453                 rsurface.generatedvertex = false;
4454         }
4455         rsurface.modellightmapcolor4f  = model->surfmesh.data_lightmapcolor4f;
4456         rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
4457         rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
4458         rsurface.modeltexcoordtexture2f  = model->surfmesh.data_texcoordtexture2f;
4459         rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
4460         rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
4461         rsurface.modeltexcoordlightmap2f  = model->surfmesh.data_texcoordlightmap2f;
4462         rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
4463         rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
4464         rsurface.modelelement3i = model->surfmesh.data_element3i;
4465         rsurface.modelelement3i_bufferobject = model->surfmesh.ebo;
4466         rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
4467         rsurface.modelnum_vertices = model->surfmesh.num_vertices;
4468         rsurface.modelnum_triangles = model->surfmesh.num_triangles;
4469         rsurface.modelsurfaces = model->data_surfaces;
4470         rsurface.vertex3f  = rsurface.modelvertex3f;
4471         rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4472         rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4473         rsurface.svector3f = rsurface.modelsvector3f;
4474         rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4475         rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4476         rsurface.tvector3f = rsurface.modeltvector3f;
4477         rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4478         rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4479         rsurface.normal3f  = rsurface.modelnormal3f;
4480         rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4481         rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4482         rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4483 }
4484
4485 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
4486 void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist)
4487 {
4488         int deformindex;
4489         int texturesurfaceindex;
4490         int i, j;
4491         float amplitude;
4492         float animpos;
4493         float scale;
4494         const float *v1, *in_tc;
4495         float *out_tc;
4496         float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
4497         float waveparms[4];
4498         q3shaderinfo_deform_t *deform;
4499         // if vertices are dynamic (animated models), generate them into the temporary rsurface.array_model* arrays and point rsurface.model* at them instead of the static data from the model itself
4500         if (rsurface.generatedvertex)
4501         {
4502                 if (rsurface.texture->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
4503                         generatenormals = true;
4504                 for (i = 0;i < Q3MAXDEFORMS;i++)
4505                 {
4506                         if (rsurface.texture->deforms[i].deform == Q3DEFORM_AUTOSPRITE)
4507                         {
4508                                 generatetangents = true;
4509                                 generatenormals = true;
4510                         }
4511                         if (rsurface.texture->deforms[i].deform != Q3DEFORM_NONE)
4512                                 generatenormals = true;
4513                 }
4514                 if (generatenormals && !rsurface.modelnormal3f)
4515                 {
4516                         rsurface.normal3f = rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4517                         rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject = 0;
4518                         rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset = 0;
4519                         Mod_BuildNormals(0, rsurface.modelnum_vertices, rsurface.modelnum_triangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.array_modelnormal3f, r_smoothnormals_areaweighting.integer);
4520                 }
4521                 if (generatetangents && !rsurface.modelsvector3f)
4522                 {
4523                         rsurface.svector3f = rsurface.modelsvector3f = rsurface.array_modelsvector3f;
4524                         rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject = 0;
4525                         rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset = 0;
4526                         rsurface.tvector3f = rsurface.modeltvector3f = rsurface.array_modeltvector3f;
4527                         rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject = 0;
4528                         rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset = 0;
4529                         Mod_BuildTextureVectorsFromNormals(0, rsurface.modelnum_vertices, rsurface.modelnum_triangles, rsurface.modelvertex3f, rsurface.modeltexcoordtexture2f, rsurface.modelnormal3f, rsurface.modelelement3i, rsurface.array_modelsvector3f, rsurface.array_modeltvector3f, r_smoothnormals_areaweighting.integer);
4530                 }
4531         }
4532         rsurface.vertex3f  = rsurface.modelvertex3f;
4533         rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4534         rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4535         rsurface.svector3f = rsurface.modelsvector3f;
4536         rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4537         rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4538         rsurface.tvector3f = rsurface.modeltvector3f;
4539         rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4540         rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4541         rsurface.normal3f  = rsurface.modelnormal3f;
4542         rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4543         rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4544         // if vertices are deformed (sprite flares and things in maps, possibly
4545         // water waves, bulges and other deformations), generate them into
4546         // rsurface.deform* arrays from whatever the rsurface.* arrays point to
4547         // (may be static model data or generated data for an animated model, or
4548         //  the previous deform pass)
4549         for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform;deformindex++, deform++)
4550         {
4551                 switch (deform->deform)
4552                 {
4553                 default:
4554                 case Q3DEFORM_PROJECTIONSHADOW:
4555                 case Q3DEFORM_TEXT0:
4556                 case Q3DEFORM_TEXT1:
4557                 case Q3DEFORM_TEXT2:
4558                 case Q3DEFORM_TEXT3:
4559                 case Q3DEFORM_TEXT4:
4560                 case Q3DEFORM_TEXT5:
4561                 case Q3DEFORM_TEXT6:
4562                 case Q3DEFORM_TEXT7:
4563                 case Q3DEFORM_NONE:
4564                         break;
4565                 case Q3DEFORM_AUTOSPRITE:
4566                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, newforward);
4567                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.right, newright);
4568                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.up, newup);
4569                         VectorNormalize(newforward);
4570                         VectorNormalize(newright);
4571                         VectorNormalize(newup);
4572                         // make deformed versions of only the model vertices used by the specified surfaces
4573                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4574                         {
4575                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4576                                 // a single autosprite surface can contain multiple sprites...
4577                                 for (j = 0;j < surface->num_vertices - 3;j += 4)
4578                                 {
4579                                         VectorClear(center);
4580                                         for (i = 0;i < 4;i++)
4581                                                 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
4582                                         VectorScale(center, 0.25f, center);
4583                                         VectorCopy((rsurface.normal3f  + 3 * surface->num_firstvertex) + j*3, forward);
4584                                         VectorCopy((rsurface.svector3f + 3 * surface->num_firstvertex) + j*3, right);
4585                                         VectorCopy((rsurface.tvector3f + 3 * surface->num_firstvertex) + j*3, up);
4586                                         for (i = 0;i < 4;i++)
4587                                         {
4588                                                 VectorSubtract((rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i)*3, center, v);
4589                                                 VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
4590                                         }
4591                                 }
4592                                 Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformednormal3f, r_smoothnormals_areaweighting.integer);
4593                                 Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modeltexcoordtexture2f, rsurface.array_deformednormal3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformedsvector3f, rsurface.array_deformedtvector3f, r_smoothnormals_areaweighting.integer);
4594                         }
4595                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4596                         rsurface.vertex3f_bufferobject = 0;
4597                         rsurface.vertex3f_bufferoffset = 0;
4598                         rsurface.svector3f = rsurface.array_deformedsvector3f;
4599                         rsurface.svector3f_bufferobject = 0;
4600                         rsurface.svector3f_bufferoffset = 0;
4601                         rsurface.tvector3f = rsurface.array_deformedtvector3f;
4602                         rsurface.tvector3f_bufferobject = 0;
4603                         rsurface.tvector3f_bufferoffset = 0;
4604                         rsurface.normal3f = rsurface.array_deformednormal3f;
4605                         rsurface.normal3f_bufferobject = 0;
4606                         rsurface.normal3f_bufferoffset = 0;
4607                         break;
4608                 case Q3DEFORM_AUTOSPRITE2:
4609                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, newforward);
4610                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.right, newright);
4611                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.up, newup);
4612                         VectorNormalize(newforward);
4613                         VectorNormalize(newright);
4614                         VectorNormalize(newup);
4615                         // make deformed versions of only the model vertices used by the specified surfaces
4616                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4617                         {
4618                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4619                                 const float *v1, *v2;
4620                                 vec3_t start, end;
4621                                 float f, l;
4622                                 struct
4623                                 {
4624                                         float length2;
4625                                         const float *v1;
4626                                         const float *v2;
4627                                 }
4628                                 shortest[2];
4629                                 memset(shortest, 0, sizeof(shortest));
4630                                 // a single autosprite surface can contain multiple sprites...
4631                                 for (j = 0;j < surface->num_vertices - 3;j += 4)
4632                                 {
4633                                         VectorClear(center);
4634                                         for (i = 0;i < 4;i++)
4635                                                 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
4636                                         VectorScale(center, 0.25f, center);
4637                                         // find the two shortest edges, then use them to define the
4638                                         // axis vectors for rotating around the central axis
4639                                         for (i = 0;i < 6;i++)
4640                                         {
4641                                                 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][0]);
4642                                                 v2 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][1]);
4643 #if 0
4644                                                 Debug_PolygonBegin(NULL, 0, false, 0);
4645                                                 Debug_PolygonVertex(v1[0], v1[1], v1[2], 0, 0, 1, 0, 0, 1);
4646                                                 Debug_PolygonVertex((v1[0] + v2[0]) * 0.5f + rsurface.normal3f[3 * (surface->num_firstvertex + j)+0] * 4, (v1[1] + v2[1]) * 0.5f + rsurface.normal3f[3 * (surface->num_firstvertex + j)+1], (v1[2] + v2[2]) * 0.5f + rsurface.normal3f[3 * (surface->num_firstvertex + j)+2], 0, 0, 1, 1, 0, 1);
4647                                                 Debug_PolygonVertex(v2[0], v2[1], v2[2], 0, 0, 1, 0, 0, 1);
4648                                                 Debug_PolygonEnd();
4649 #endif
4650                                                 l = VectorDistance2(v1, v2);
4651                                                 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
4652                                                 if (v1[2] != v2[2])
4653                                                         l += (1.0f / 1024.0f);
4654                                                 if (shortest[0].length2 > l || i == 0)
4655                                                 {
4656                                                         shortest[1] = shortest[0];
4657                                                         shortest[0].length2 = l;
4658                                                         shortest[0].v1 = v1;
4659                                                         shortest[0].v2 = v2;
4660                                                 }
4661                                                 else if (shortest[1].length2 > l || i == 1)
4662                                                 {
4663                                                         shortest[1].length2 = l;
4664                                                         shortest[1].v1 = v1;
4665                                                         shortest[1].v2 = v2;
4666                                                 }
4667                                         }
4668                                         VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
4669                                         VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
4670 #if 0
4671                                         Debug_PolygonBegin(NULL, 0, false, 0);
4672                                         Debug_PolygonVertex(start[0], start[1], start[2], 0, 0, 1, 1, 0, 1);
4673                                         Debug_PolygonVertex(center[0] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+0] * 4, center[1] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+1] * 4, center[2] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+2] * 4, 0, 0, 0, 1, 0, 1);
4674                                         Debug_PolygonVertex(end[0], end[1], end[2], 0, 0, 0, 1, 1, 1);
4675                                         Debug_PolygonEnd();
4676 #endif
4677                                         // this calculates the right vector from the shortest edge
4678                                         // and the up vector from the edge midpoints
4679                                         VectorSubtract(shortest[0].v1, shortest[0].v2, right);
4680                                         VectorNormalize(right);
4681                                         VectorSubtract(end, start, up);
4682                                         VectorNormalize(up);
4683                                         // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
4684                                         //VectorSubtract(rsurface.modelorg, center, forward);
4685                                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, forward);
4686                                         VectorNegate(forward, forward);
4687                                         VectorReflect(forward, 0, up, forward);
4688                                         VectorNormalize(forward);
4689                                         CrossProduct(up, forward, newright);
4690                                         VectorNormalize(newright);
4691 #if 0
4692                                         Debug_PolygonBegin(NULL, 0, false, 0);
4693                                         Debug_PolygonVertex(center[0] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+0] * 8, center[1] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+1] * 8, center[2] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+2] * 8, 0, 0, 1, 0, 0, 1);
4694                                         Debug_PolygonVertex(center[0] + right[0] * 8, center[1] + right[1] * 8, center[2] + right[2] * 8, 0, 0, 0, 1, 0, 1);
4695                                         Debug_PolygonVertex(center[0] + up   [0] * 8, center[1] + up   [1] * 8, center[2] + up   [2] * 8, 0, 0, 0, 0, 1, 1);
4696                                         Debug_PolygonEnd();
4697 #endif
4698 #if 0
4699                                         Debug_PolygonBegin(NULL, 0, false, 0);
4700                                         Debug_PolygonVertex(center[0] + forward [0] * 8, center[1] + forward [1] * 8, center[2] + forward [2] * 8, 0, 0, 1, 0, 0, 1);
4701                                         Debug_PolygonVertex(center[0] + newright[0] * 8, center[1] + newright[1] * 8, center[2] + newright[2] * 8, 0, 0, 0, 1, 0, 1);
4702                                         Debug_PolygonVertex(center[0] + up      [0] * 8, center[1] + up      [1] * 8, center[2] + up      [2] * 8, 0, 0, 0, 0, 1, 1);
4703                                         Debug_PolygonEnd();
4704 #endif
4705                                         // rotate the quad around the up axis vector, this is made
4706                                         // especially easy by the fact we know the quad is flat,
4707                                         // so we only have to subtract the center position and
4708                                         // measure distance along the right vector, and then
4709                                         // multiply that by the newright vector and add back the
4710                                         // center position
4711                                         // we also need to subtract the old position to undo the
4712                                         // displacement from the center, which we do with a
4713                                         // DotProduct, the subtraction/addition of center is also
4714                                         // optimized into DotProducts here
4715                                         l = DotProduct(right, center);
4716                                         for (i = 0;i < 4;i++)
4717                                         {
4718                                                 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + j + i);
4719                                                 f = DotProduct(right, v1) - l;
4720                                                 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
4721                                         }
4722                                 }
4723                                 Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformednormal3f, r_smoothnormals_areaweighting.integer);
4724                                 Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modeltexcoordtexture2f, rsurface.array_deformednormal3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformedsvector3f, rsurface.array_deformedtvector3f, r_smoothnormals_areaweighting.integer);
4725                         }
4726                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4727                         rsurface.vertex3f_bufferobject = 0;
4728                         rsurface.vertex3f_bufferoffset = 0;
4729                         rsurface.svector3f = rsurface.array_deformedsvector3f;
4730                         rsurface.svector3f_bufferobject = 0;
4731                         rsurface.svector3f_bufferoffset = 0;
4732                         rsurface.tvector3f = rsurface.array_deformedtvector3f;
4733                         rsurface.tvector3f_bufferobject = 0;
4734                         rsurface.tvector3f_bufferoffset = 0;
4735                         rsurface.normal3f = rsurface.array_deformednormal3f;
4736                         rsurface.normal3f_bufferobject = 0;
4737                         rsurface.normal3f_bufferoffset = 0;
4738                         break;
4739                 case Q3DEFORM_NORMAL:
4740                         // deform the normals to make reflections wavey
4741                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4742                         {
4743                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4744                                 for (j = 0;j < surface->num_vertices;j++)
4745                                 {
4746                                         float vertex[3];
4747                                         float *normal = (rsurface.array_deformednormal3f  + 3 * surface->num_firstvertex) + j*3;
4748                                         VectorScale((rsurface.vertex3f  + 3 * surface->num_firstvertex) + j*3, 0.98f, vertex);
4749                                         VectorCopy((rsurface.normal3f  + 3 * surface->num_firstvertex) + j*3, normal);
4750                                         normal[0] += deform->parms[0] * noise4f(      vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4751                                         normal[1] += deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4752                                         normal[2] += deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4753                                         VectorNormalize(normal);
4754                                 }
4755                                 Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modeltexcoordtexture2f, rsurface.array_deformednormal3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformedsvector3f, rsurface.array_deformedtvector3f, r_smoothnormals_areaweighting.integer);
4756                         }
4757                         rsurface.svector3f = rsurface.array_deformedsvector3f;
4758                         rsurface.svector3f_bufferobject = 0;
4759                         rsurface.svector3f_bufferoffset = 0;
4760                         rsurface.tvector3f = rsurface.array_deformedtvector3f;
4761                         rsurface.tvector3f_bufferobject = 0;
4762                         rsurface.tvector3f_bufferoffset = 0;
4763                         rsurface.normal3f = rsurface.array_deformednormal3f;
4764                         rsurface.normal3f_bufferobject = 0;
4765                         rsurface.normal3f_bufferoffset = 0;
4766                         break;
4767                 case Q3DEFORM_WAVE:
4768                         // deform vertex array to make wavey water and flags and such
4769                         waveparms[0] = deform->waveparms[0];
4770                         waveparms[1] = deform->waveparms[1];
4771                         waveparms[2] = deform->waveparms[2];
4772                         waveparms[3] = deform->waveparms[3];
4773                         // this is how a divisor of vertex influence on deformation
4774                         animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
4775                         scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
4776                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4777                         {
4778                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4779                                 for (j = 0;j < surface->num_vertices;j++)
4780                                 {
4781                                         float *vertex = (rsurface.array_deformedvertex3f  + 3 * surface->num_firstvertex) + j*3;
4782                                         VectorCopy((rsurface.vertex3f  + 3 * surface->num_firstvertex) + j*3, vertex);
4783                                         // if the wavefunc depends on time, evaluate it per-vertex
4784                                         if (waveparms[3])
4785                                         {
4786                                                 waveparms[2] = deform->waveparms[2] + (vertex[0] + vertex[1] + vertex[2]) * animpos;
4787                                                 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
4788                                         }
4789                                         VectorMA(vertex, scale, (rsurface.normal3f  + 3 * surface->num_firstvertex) + j*3, vertex);
4790                                 }
4791                         }
4792                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4793                         rsurface.vertex3f_bufferobject = 0;
4794                         rsurface.vertex3f_bufferoffset = 0;
4795                         break;
4796                 case Q3DEFORM_BULGE:
4797                         // deform vertex array to make the surface have moving bulges
4798                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4799                         {
4800                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4801                                 for (j = 0;j < surface->num_vertices;j++)
4802                                 {
4803                                         scale = sin((rsurface.modeltexcoordtexture2f[2 * (surface->num_firstvertex + j)] * deform->parms[0] + r_refdef.time * deform->parms[2])) * deform->parms[1];
4804                                         VectorMA(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), scale, rsurface.normal3f + 3 * (surface->num_firstvertex + j), rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
4805                                 }
4806                         }
4807                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4808                         rsurface.vertex3f_bufferobject = 0;
4809                         rsurface.vertex3f_bufferoffset = 0;
4810                         break;
4811                 case Q3DEFORM_MOVE:
4812                         // deform vertex array
4813                         scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
4814                         VectorScale(deform->parms, scale, waveparms);
4815                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4816                         {
4817                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4818                                 for (j = 0;j < surface->num_vertices;j++)
4819                                         VectorAdd(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), waveparms, rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
4820                         }
4821                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4822                         rsurface.vertex3f_bufferobject = 0;
4823                         rsurface.vertex3f_bufferoffset = 0;
4824                         break;
4825                 }
4826         }
4827         // generate texcoords based on the chosen texcoord source
4828         switch(rsurface.texture->tcgen.tcgen)
4829         {
4830         default:
4831         case Q3TCGEN_TEXTURE:
4832                 rsurface.texcoordtexture2f               = rsurface.modeltexcoordtexture2f;
4833                 rsurface.texcoordtexture2f_bufferobject  = rsurface.modeltexcoordtexture2f_bufferobject;
4834                 rsurface.texcoordtexture2f_bufferoffset  = rsurface.modeltexcoordtexture2f_bufferoffset;
4835                 break;
4836         case Q3TCGEN_LIGHTMAP:
4837                 rsurface.texcoordtexture2f               = rsurface.modeltexcoordlightmap2f;
4838                 rsurface.texcoordtexture2f_bufferobject  = rsurface.modeltexcoordlightmap2f_bufferobject;
4839                 rsurface.texcoordtexture2f_bufferoffset  = rsurface.modeltexcoordlightmap2f_bufferoffset;
4840                 break;
4841         case Q3TCGEN_VECTOR:
4842                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4843                 {
4844                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4845                         for (j = 0, v1 = rsurface.modelvertex3f + 3 * surface->num_firstvertex, out_tc = rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex;j < surface->num_vertices;j++, v1 += 3, out_tc += 2)
4846                         {
4847                                 out_tc[0] = DotProduct(v1, rsurface.texture->tcgen.parms);
4848                                 out_tc[1] = DotProduct(v1, rsurface.texture->tcgen.parms + 3);
4849                         }
4850                 }
4851                 rsurface.texcoordtexture2f               = rsurface.array_generatedtexcoordtexture2f;
4852                 rsurface.texcoordtexture2f_bufferobject  = 0;
4853                 rsurface.texcoordtexture2f_bufferoffset  = 0;
4854                 break;
4855         case Q3TCGEN_ENVIRONMENT:
4856                 // make environment reflections using a spheremap
4857                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4858                 {
4859                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4860                         const float *vertex = rsurface.modelvertex3f + 3 * surface->num_firstvertex;
4861                         const float *normal = rsurface.modelnormal3f + 3 * surface->num_firstvertex;
4862                         float *out_tc = rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex;
4863                         for (j = 0;j < surface->num_vertices;j++, vertex += 3, normal += 3, out_tc += 2)
4864                         {
4865                                 float l, d, eyedir[3];
4866                                 VectorSubtract(rsurface.modelorg, vertex, eyedir);
4867                                 l = 0.5f / VectorLength(eyedir);
4868                                 d = DotProduct(normal, eyedir)*2;
4869                                 out_tc[0] = 0.5f + (normal[1]*d - eyedir[1])*l;
4870                                 out_tc[1] = 0.5f - (normal[2]*d - eyedir[2])*l;
4871                         }
4872                 }
4873                 rsurface.texcoordtexture2f               = rsurface.array_generatedtexcoordtexture2f;
4874                 rsurface.texcoordtexture2f_bufferobject  = 0;
4875                 rsurface.texcoordtexture2f_bufferoffset  = 0;
4876                 break;
4877         }
4878         // the only tcmod that needs software vertex processing is turbulent, so
4879         // check for it here and apply the changes if needed
4880         // and we only support that as the first one
4881         // (handling a mixture of turbulent and other tcmods would be problematic
4882         //  without punting it entirely to a software path)
4883         if (rsurface.texture->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
4884         {
4885                 amplitude = rsurface.texture->tcmods[0].parms[1];
4886                 animpos = rsurface.texture->tcmods[0].parms[2] + r_refdef.time * rsurface.texture->tcmods[0].parms[3];
4887                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4888                 {
4889                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4890                         for (j = 0, v1 = rsurface.modelvertex3f + 3 * surface->num_firstvertex, in_tc = rsurface.texcoordtexture2f + 2 * surface->num_firstvertex, out_tc = rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex;j < surface->num_vertices;j++, v1 += 3, in_tc += 2, out_tc += 2)
4891                         {
4892                                 out_tc[0] = in_tc[0] + amplitude * sin(((v1[0] + v1[2]) * 1.0 / 1024.0f + animpos) * M_PI * 2);
4893                                 out_tc[1] = in_tc[1] + amplitude * sin(((v1[1]        ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
4894                         }
4895                 }
4896                 rsurface.texcoordtexture2f               = rsurface.array_generatedtexcoordtexture2f;
4897                 rsurface.texcoordtexture2f_bufferobject  = 0;
4898                 rsurface.texcoordtexture2f_bufferoffset  = 0;
4899         }
4900         rsurface.texcoordlightmap2f              = rsurface.modeltexcoordlightmap2f;
4901         rsurface.texcoordlightmap2f_bufferobject = rsurface.modeltexcoordlightmap2f_bufferobject;
4902         rsurface.texcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
4903         R_Mesh_VertexPointer(rsurface.vertex3f, rsurface.vertex3f_bufferobject, rsurface.vertex3f_bufferoffset);
4904 }
4905
4906 void RSurf_DrawBatch_Simple(int texturenumsurfaces, msurface_t **texturesurfacelist)
4907 {
4908         int i, j;
4909         const msurface_t *surface = texturesurfacelist[0];
4910         const msurface_t *surface2;
4911         int firstvertex;
4912         int endvertex;
4913         int numvertices;
4914         int numtriangles;
4915         // TODO: lock all array ranges before render, rather than on each surface
4916         if (texturenumsurfaces == 1)
4917         {
4918                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
4919                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4920         }
4921         else if (r_batchmode.integer == 2)
4922         {
4923                 #define MAXBATCHTRIANGLES 4096
4924                 int batchtriangles = 0;
4925                 int batchelements[MAXBATCHTRIANGLES*3];
4926                 for (i = 0;i < texturenumsurfaces;i = j)
4927                 {
4928                         surface = texturesurfacelist[i];
4929                         j = i + 1;
4930                         if (surface->num_triangles > MAXBATCHTRIANGLES)
4931                         {
4932                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4933                                 continue;
4934                         }
4935                         memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
4936                         batchtriangles = surface->num_triangles;
4937                         firstvertex = surface->num_firstvertex;
4938                         endvertex = surface->num_firstvertex + surface->num_vertices;
4939                         for (;j < texturenumsurfaces;j++)
4940                         {
4941                                 surface2 = texturesurfacelist[j];
4942                                 if (batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
4943                                         break;
4944                                 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
4945                                 batchtriangles += surface2->num_triangles;
4946                                 firstvertex = min(firstvertex, surface2->num_firstvertex);
4947                                 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
4948                         }
4949                         surface2 = texturesurfacelist[j-1];
4950                         numvertices = endvertex - firstvertex;
4951                         R_Mesh_Draw(firstvertex, numvertices, batchtriangles, batchelements, 0, 0);
4952                 }
4953         }
4954         else if (r_batchmode.integer == 1)
4955         {
4956                 for (i = 0;i < texturenumsurfaces;i = j)
4957                 {
4958                         surface = texturesurfacelist[i];
4959                         for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
4960                                 if (texturesurfacelist[j] != surface2)
4961                                         break;
4962                         surface2 = texturesurfacelist[j-1];
4963                         numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
4964                         numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
4965                         GL_LockArrays(surface->num_firstvertex, numvertices);
4966                         R_Mesh_Draw(surface->num_firstvertex, numvertices, numtriangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4967                 }
4968         }
4969         else
4970         {
4971                 for (i = 0;i < texturenumsurfaces;i++)
4972                 {
4973                         surface = texturesurfacelist[i];
4974                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
4975                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4976                 }
4977         }
4978 }
4979
4980 static void RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit, int refractiontexunit, int reflectiontexunit)
4981 {
4982         int i, planeindex, vertexindex;
4983         float d, bestd;
4984         vec3_t vert;
4985         const float *v;
4986         r_waterstate_waterplane_t *p, *bestp;
4987         msurface_t *surface;
4988         if (r_waterstate.renderingscene)
4989                 return;
4990         for (i = 0;i < texturenumsurfaces;i++)
4991         {
4992                 surface = texturesurfacelist[i];
4993                 if (lightmaptexunit >= 0)
4994                         R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
4995                 if (deluxemaptexunit >= 0)
4996                         R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
4997                 // pick the closest matching water plane
4998                 bestd = 0;
4999                 bestp = NULL;
5000                 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
5001                 {
5002                         d = 0;
5003                         for (vertexindex = 0, v = rsurface.modelvertex3f + surface->num_firstvertex * 3;vertexindex < surface->num_vertices;vertexindex++, v += 3)
5004                         {
5005                                 Matrix4x4_Transform(&rsurface.matrix, v, vert);
5006                                 d += fabs(PlaneDiff(vert, &p->plane));
5007                         }
5008                         if (bestd > d || !bestp)
5009                         {
5010                                 bestd = d;
5011                                 bestp = p;
5012                         }
5013                 }
5014                 if (bestp)
5015                 {
5016                         if (refractiontexunit >= 0)
5017                                 R_Mesh_TexBind(refractiontexunit, R_GetTexture(bestp->texture_refraction));
5018                         if (reflectiontexunit >= 0)
5019                                 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(bestp->texture_reflection));
5020                 }
5021                 else
5022                 {
5023                         if (refractiontexunit >= 0)
5024                                 R_Mesh_TexBind(refractiontexunit, R_GetTexture(r_texture_black));
5025                         if (reflectiontexunit >= 0)
5026                                 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(r_texture_black));
5027                 }
5028                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5029                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5030         }
5031 }
5032
5033 static void RSurf_DrawBatch_WithLightmapSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit)
5034 {
5035         int i;
5036         int j;
5037         const msurface_t *surface = texturesurfacelist[0];
5038         const msurface_t *surface2;
5039         int firstvertex;
5040         int endvertex;
5041         int numvertices;
5042         int numtriangles;
5043         // TODO: lock all array ranges before render, rather than on each surface
5044         if (texturenumsurfaces == 1)
5045         {
5046                 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5047                 if (deluxemaptexunit >= 0)
5048                         R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5049                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5050                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5051         }
5052         else if (r_batchmode.integer == 2)
5053         {
5054                 #define MAXBATCHTRIANGLES 4096
5055                 int batchtriangles = 0;
5056                 int batchelements[MAXBATCHTRIANGLES*3];
5057                 for (i = 0;i < texturenumsurfaces;i = j)
5058                 {
5059                         surface = texturesurfacelist[i];
5060                         R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5061                         if (deluxemaptexunit >= 0)
5062                                 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5063                         j = i + 1;
5064                         if (surface->num_triangles > MAXBATCHTRIANGLES)
5065                         {
5066                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5067                                 continue;
5068                         }
5069                         memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
5070                         batchtriangles = surface->num_triangles;
5071                         firstvertex = surface->num_firstvertex;
5072                         endvertex = surface->num_firstvertex + surface->num_vertices;
5073                         for (;j < texturenumsurfaces;j++)
5074                         {
5075                                 surface2 = texturesurfacelist[j];
5076                                 if (surface2->lightmaptexture != surface->lightmaptexture || batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
5077                                         break;
5078                                 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
5079                                 batchtriangles += surface2->num_triangles;
5080                                 firstvertex = min(firstvertex, surface2->num_firstvertex);
5081                                 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
5082                         }
5083                         surface2 = texturesurfacelist[j-1];
5084                         numvertices = endvertex - firstvertex;
5085                         R_Mesh_Draw(firstvertex, numvertices, batchtriangles, batchelements, 0, 0);
5086                 }
5087         }
5088         else if (r_batchmode.integer == 1)
5089         {
5090 #if 0
5091                 Con_Printf("%s batch sizes ignoring lightmap:", rsurface.texture->name);
5092                 for (i = 0;i < texturenumsurfaces;i = j)
5093                 {
5094                         surface = texturesurfacelist[i];
5095                         for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
5096                                 if (texturesurfacelist[j] != surface2)
5097                                         break;
5098                         Con_Printf(" %i", j - i);
5099                 }
5100                 Con_Printf("\n");
5101                 Con_Printf("%s batch sizes honoring lightmap:", rsurface.texture->name);
5102 #endif
5103                 for (i = 0;i < texturenumsurfaces;i = j)
5104                 {
5105                         surface = texturesurfacelist[i];
5106                         R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5107                         if (deluxemaptexunit >= 0)
5108                                 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5109                         for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
5110                                 if (texturesurfacelist[j] != surface2 || texturesurfacelist[j]->lightmaptexture != surface->lightmaptexture)
5111                                         break;
5112 #if 0
5113                         Con_Printf(" %i", j - i);
5114 #endif
5115                         surface2 = texturesurfacelist[j-1];
5116                         numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
5117                         numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
5118                         GL_LockArrays(surface->num_firstvertex, numvertices);
5119                         R_Mesh_Draw(surface->num_firstvertex, numvertices, numtriangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5120                 }
5121 #if 0
5122                 Con_Printf("\n");
5123 #endif
5124         }
5125         else
5126         {
5127                 for (i = 0;i < texturenumsurfaces;i++)
5128                 {
5129                         surface = texturesurfacelist[i];
5130                         R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5131                         if (deluxemaptexunit >= 0)
5132                                 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5133                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5134                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5135                 }
5136         }
5137 }
5138
5139 static void RSurf_DrawBatch_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
5140 {
5141         int j;
5142         int texturesurfaceindex;
5143         if (r_showsurfaces.integer == 2)
5144         {
5145                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5146                 {
5147                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5148                         for (j = 0;j < surface->num_triangles;j++)
5149                         {
5150                                 float f = ((j + surface->num_firsttriangle) & 31) * (1.0f / 31.0f) * r_view.colorscale;
5151                                 GL_Color(f, f, f, 1);
5152                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, 1, (rsurface.modelelement3i + 3 * (j + surface->num_firsttriangle)), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * (j + surface->num_firsttriangle)));
5153                         }
5154                 }
5155         }
5156         else
5157         {
5158                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5159                 {
5160                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5161                         int k = (int)(((size_t)surface) / sizeof(msurface_t));
5162                         GL_Color((k & 15) * (1.0f / 16.0f) * r_view.colorscale, ((k >> 4) & 15) * (1.0f / 16.0f) * r_view.colorscale, ((k >> 8) & 15) * (1.0f / 16.0f) * r_view.colorscale, 1);
5163                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5164                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5165                 }
5166         }
5167 }
5168
5169 static void RSurf_DrawBatch_GL11_ApplyFog(int texturenumsurfaces, msurface_t **texturesurfacelist)
5170 {
5171         int texturesurfaceindex;
5172         int i;
5173         float f;
5174         float *v, *c, *c2;
5175         if (rsurface.lightmapcolor4f)
5176         {
5177                 // generate color arrays for the surfaces in this list
5178                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5179                 {
5180                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5181                         for (i = 0, v = (rsurface.vertex3f + 3 * surface->num_firstvertex), c = (rsurface.lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4, c2 += 4)
5182                         {
5183                                 f = FogPoint_Model(v);
5184                                 c2[0] = c[0] * f;
5185                                 c2[1] = c[1] * f;
5186                                 c2[2] = c[2] * f;
5187                                 c2[3] = c[3];
5188                         }
5189                 }
5190         }
5191         else
5192         {
5193                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5194                 {
5195                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5196                         for (i = 0, v = (rsurface.vertex3f + 3 * surface->num_firstvertex), c2 = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c2 += 4)
5197                         {
5198                                 f = FogPoint_Model(v);
5199                                 c2[0] = f;
5200                                 c2[1] = f;
5201                                 c2[2] = f;
5202                                 c2[3] = 1;
5203                         }
5204                 }
5205         }
5206         rsurface.lightmapcolor4f = rsurface.array_color4f;
5207         rsurface.lightmapcolor4f_bufferobject = 0;
5208         rsurface.lightmapcolor4f_bufferoffset = 0;
5209 }
5210
5211 static void RSurf_DrawBatch_GL11_ApplyColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a)
5212 {
5213         int texturesurfaceindex;
5214         int i;
5215         float *c, *c2;
5216         if (!rsurface.lightmapcolor4f)
5217                 return;
5218         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5219         {
5220                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5221                 for (i = 0, c = (rsurface.lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4, c2 += 4)
5222                 {
5223                         c2[0] = c[0] * r;
5224                         c2[1] = c[1] * g;
5225                         c2[2] = c[2] * b;
5226                         c2[3] = c[3] * a;
5227                 }
5228         }
5229         rsurface.lightmapcolor4f = rsurface.array_color4f;
5230         rsurface.lightmapcolor4f_bufferobject = 0;
5231         rsurface.lightmapcolor4f_bufferoffset = 0;
5232 }
5233
5234 static void RSurf_DrawBatch_GL11_Lightmap(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5235 {
5236         // TODO: optimize
5237         rsurface.lightmapcolor4f = NULL;
5238         rsurface.lightmapcolor4f_bufferobject = 0;
5239         rsurface.lightmapcolor4f_bufferoffset = 0;
5240         if (applyfog)   RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5241         if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5242         R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5243         GL_Color(r, g, b, a);
5244         RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 0, -1);
5245 }
5246
5247 static void RSurf_DrawBatch_GL11_Unlit(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5248 {
5249         // TODO: optimize applyfog && applycolor case
5250         // just apply fog if necessary, and tint the fog color array if necessary
5251         rsurface.lightmapcolor4f = NULL;
5252         rsurface.lightmapcolor4f_bufferobject = 0;
5253         rsurface.lightmapcolor4f_bufferoffset = 0;
5254         if (applyfog)   RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5255         if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5256         R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5257         GL_Color(r, g, b, a);
5258         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5259 }
5260
5261 static void RSurf_DrawBatch_GL11_VertexColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5262 {
5263         int texturesurfaceindex;
5264         int i;
5265         float *c;
5266         // TODO: optimize
5267         if (texturesurfacelist[0]->lightmapinfo && texturesurfacelist[0]->lightmapinfo->stainsamples)
5268         {
5269                 // generate color arrays for the surfaces in this list
5270                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5271                 {
5272                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5273                         for (i = 0, c = rsurface.array_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
5274                         {
5275                                 if (surface->lightmapinfo->samples)
5276                                 {
5277                                         const unsigned char *lm = surface->lightmapinfo->samples + (rsurface.modellightmapoffsets + surface->num_firstvertex)[i];
5278                                         float scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
5279                                         VectorScale(lm, scale, c);
5280                                         if (surface->lightmapinfo->styles[1] != 255)
5281                                         {
5282                                                 int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
5283                                                 lm += size3;
5284                                                 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
5285                                                 VectorMA(c, scale, lm, c);
5286                                                 if (surface->lightmapinfo->styles[2] != 255)
5287                                                 {
5288                                                         lm += size3;
5289                                                         scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
5290                                                         VectorMA(c, scale, lm, c);
5291                                                         if (surface->lightmapinfo->styles[3] != 255)
5292                                                         {
5293                                                                 lm += size3;
5294                                                                 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
5295                                                                 VectorMA(c, scale, lm, c);
5296                                                         }
5297                                                 }
5298                                         }
5299                                 }
5300                                 else
5301                                         VectorClear(c);
5302                                 c[3] = 1;
5303                         }
5304                 }
5305                 rsurface.lightmapcolor4f = rsurface.array_color4f;
5306                 rsurface.lightmapcolor4f_bufferobject = 0;
5307                 rsurface.lightmapcolor4f_bufferoffset = 0;
5308         }
5309         else
5310         {
5311                 rsurface.lightmapcolor4f = rsurface.modellightmapcolor4f;
5312                 rsurface.lightmapcolor4f_bufferobject = rsurface.modellightmapcolor4f_bufferobject;
5313                 rsurface.lightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
5314         }
5315         if (applyfog)   RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5316         if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5317         R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5318         GL_Color(r, g, b, a);
5319         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5320 }
5321
5322 static void RSurf_DrawBatch_GL11_VertexShade(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5323 {
5324         int texturesurfaceindex;
5325         int i;
5326         float f;
5327         float *v, *c, *c2;
5328         vec3_t ambientcolor;
5329         vec3_t diffusecolor;
5330         vec3_t lightdir;
5331         // TODO: optimize
5332         // model lighting
5333         VectorCopy(rsurface.modellight_lightdir, lightdir);
5334         ambientcolor[0] = rsurface.modellight_ambient[0] * r * 0.5f;
5335         ambientcolor[1] = rsurface.modellight_ambient[1] * g * 0.5f;
5336         ambientcolor[2] = rsurface.modellight_ambient[2] * b * 0.5f;
5337         diffusecolor[0] = rsurface.modellight_diffuse[0] * r * 0.5f;
5338         diffusecolor[1] = rsurface.modellight_diffuse[1] * g * 0.5f;
5339         diffusecolor[2] = rsurface.modellight_diffuse[2] * b * 0.5f;
5340         if (VectorLength2(diffusecolor) > 0)
5341         {
5342                 // generate color arrays for the surfaces in this list
5343                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5344                 {
5345                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5346                         int numverts = surface->num_vertices;
5347                         v = rsurface.vertex3f + 3 * surface->num_firstvertex;
5348                         c2 = rsurface.normal3f + 3 * surface->num_firstvertex;
5349                         c = rsurface.array_color4f + 4 * surface->num_firstvertex;
5350                         // q3-style directional shading
5351                         for (i = 0;i < numverts;i++, v += 3, c2 += 3, c += 4)
5352                         {
5353                                 if ((f = DotProduct(c2, lightdir)) > 0)
5354                                         VectorMA(ambientcolor, f, diffusecolor, c);
5355                                 else
5356                                         VectorCopy(ambientcolor, c);
5357                                 c[3] = a;
5358                         }
5359                 }
5360                 r = 1;
5361                 g = 1;
5362                 b = 1;
5363                 a = 1;
5364                 applycolor = false;
5365                 rsurface.lightmapcolor4f = rsurface.array_color4f;
5366                 rsurface.lightmapcolor4f_bufferobject = 0;
5367                 rsurface.lightmapcolor4f_bufferoffset = 0;
5368         }
5369         else
5370         {
5371                 r = ambientcolor[0];
5372                 g = ambientcolor[1];
5373                 b = ambientcolor[2];
5374                 rsurface.lightmapcolor4f = NULL;
5375                 rsurface.lightmapcolor4f_bufferobject = 0;
5376                 rsurface.lightmapcolor4f_bufferoffset = 0;
5377         }
5378         if (applyfog)   RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5379         if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5380         R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5381         GL_Color(r, g, b, a);
5382         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5383 }
5384
5385 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
5386 {
5387         GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5388         GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5389         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5390         GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5391         if (rsurface.mode != RSURFMODE_SHOWSURFACES)
5392         {
5393                 rsurface.mode = RSURFMODE_SHOWSURFACES;
5394                 GL_DepthMask(true);
5395                 GL_BlendFunc(GL_ONE, GL_ZERO);
5396                 R_Mesh_ColorPointer(NULL, 0, 0);
5397                 R_Mesh_ResetTextureState();
5398         }
5399         RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5400         RSurf_DrawBatch_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
5401 }
5402
5403 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, msurface_t **texturesurfacelist)
5404 {
5405         // transparent sky would be ridiculous
5406         if ((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
5407                 return;
5408         if (rsurface.mode != RSURFMODE_SKY)
5409         {
5410                 if (rsurface.mode == RSURFMODE_GLSL)
5411                 {
5412                         qglUseProgramObjectARB(0);CHECKGLERROR
5413                 }
5414                 rsurface.mode = RSURFMODE_SKY;
5415         }
5416         if (skyrendernow)
5417         {
5418                 skyrendernow = false;
5419                 R_Sky();
5420                 // restore entity matrix
5421                 R_Mesh_Matrix(&rsurface.matrix);
5422         }
5423         GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5424         GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5425         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5426         GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5427         GL_DepthMask(true);
5428         // LordHavoc: HalfLife maps have freaky skypolys so don't use
5429         // skymasking on them, and Quake3 never did sky masking (unlike
5430         // software Quake and software Quake2), so disable the sky masking
5431         // in Quake3 maps as it causes problems with q3map2 sky tricks,
5432         // and skymasking also looks very bad when noclipping outside the
5433         // level, so don't use it then either.
5434         if (r_refdef.worldmodel && r_refdef.worldmodel->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_viewcache.world_novis)
5435         {
5436                 GL_Color(r_refdef.fogcolor[0] * r_view.colorscale, r_refdef.fogcolor[1] * r_view.colorscale, r_refdef.fogcolor[2] * r_view.colorscale, 1);
5437                 R_Mesh_ColorPointer(NULL, 0, 0);
5438                 R_Mesh_ResetTextureState();
5439                 if (skyrendermasked)
5440                 {
5441                         // depth-only (masking)
5442                         GL_ColorMask(0,0,0,0);
5443                         // just to make sure that braindead drivers don't draw
5444                         // anything despite that colormask...
5445                         GL_BlendFunc(GL_ZERO, GL_ONE);
5446                 }
5447                 else
5448                 {
5449                         // fog sky
5450                         GL_BlendFunc(GL_ONE, GL_ZERO);
5451                 }
5452                 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5453                 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5454                 if (skyrendermasked)
5455                         GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
5456         }
5457 }
5458
5459 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, msurface_t **texturesurfacelist)
5460 {
5461         if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION)))
5462                 return;
5463
5464         if (rsurface.mode != RSURFMODE_GLSL)
5465         {
5466                 rsurface.mode = RSURFMODE_GLSL;
5467                 R_Mesh_ResetTextureState();
5468         }
5469
5470         R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
5471         R_Mesh_TexBind(0, R_GetTexture(rsurface.texture->currentskinframe->nmap));
5472         R_Mesh_TexBind(1, R_GetTexture(rsurface.texture->basetexture));
5473         R_Mesh_TexBind(2, R_GetTexture(rsurface.texture->glosstexture));
5474         R_Mesh_TexBind(4, R_GetTexture(r_texture_fogattenuation));
5475         R_Mesh_TexBind(5, R_GetTexture(rsurface.texture->currentskinframe->pants));
5476         R_Mesh_TexBind(6, R_GetTexture(rsurface.texture->currentskinframe->shirt));
5477         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
5478         {
5479                 R_Mesh_TexBind(7, R_GetTexture(r_texture_grey128));
5480                 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5481                 R_Mesh_ColorPointer(NULL, 0, 0);
5482         }
5483         else if (rsurface.uselightmaptexture)
5484         {
5485                 R_Mesh_TexBind(7, R_GetTexture(texturesurfacelist[0]->lightmaptexture));
5486                 R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
5487                 R_Mesh_ColorPointer(NULL, 0, 0);
5488         }
5489         else
5490         {
5491                 R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
5492                 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5493                 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
5494         }
5495         R_Mesh_TexBind(9, R_GetTexture(rsurface.texture->currentskinframe->glow));
5496         R_Mesh_TexBind(11, R_GetTexture(r_texture_white)); // changed per surface
5497         R_Mesh_TexBind(12, R_GetTexture(r_texture_white)); // changed per surface
5498
5499         if (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
5500         {
5501                 // render background
5502                 GL_BlendFunc(GL_ONE, GL_ZERO);
5503                 GL_DepthMask(true);
5504                 GL_AlphaTest(false);
5505
5506                 GL_Color(1, 1, 1, 1);
5507                 R_Mesh_ColorPointer(NULL, 0, 0);
5508
5509                 R_SetupSurfaceShader(vec3_origin, rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, 1, 1, rsurface.texture->specularscale, RSURFPASS_BACKGROUND);
5510                 if (r_glsl_permutation)
5511                 {
5512                         RSurf_PrepareVerticesForBatch(true, true, texturenumsurfaces, texturesurfacelist);
5513                         R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
5514                         R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
5515                         R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
5516                         R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
5517                         R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
5518                         RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection ? 12 : -1);
5519                 }
5520
5521                 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
5522                 GL_DepthMask(false);
5523                 GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
5524                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
5525                 {
5526                         R_Mesh_TexBind(7, R_GetTexture(r_texture_grey128));
5527                         R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5528                         R_Mesh_ColorPointer(NULL, 0, 0);
5529                 }
5530                 else if (rsurface.uselightmaptexture)
5531                 {
5532                         R_Mesh_TexBind(7, R_GetTexture(texturesurfacelist[0]->lightmaptexture));
5533                         R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
5534                         R_Mesh_ColorPointer(NULL, 0, 0);
5535                 }
5536                 else
5537                 {
5538                         R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
5539                         R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5540                         R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
5541                 }
5542                 R_Mesh_TexBind(11, R_GetTexture(r_texture_white)); // changed per surface
5543                 R_Mesh_TexBind(12, R_GetTexture(r_texture_white)); // changed per surface
5544         }
5545
5546         R_SetupSurfaceShader(vec3_origin, rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, 1, 1, rsurface.texture->specularscale, RSURFPASS_BASE);
5547         if (!r_glsl_permutation)
5548                 return;
5549
5550         RSurf_PrepareVerticesForBatch(r_glsl_permutation->loc_Texture_Normal >= 0 || r_glsl_permutation->loc_LightDir >= 0, r_glsl_permutation->loc_Texture_Normal >= 0, texturenumsurfaces, texturesurfacelist);
5551         R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
5552         R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
5553         R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
5554         R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
5555         R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
5556         GL_Color(rsurface.texture->currentlayers[0].color[0], rsurface.texture->currentlayers[0].color[1], rsurface.texture->currentlayers[0].color[2], rsurface.texture->currentlayers[0].color[3]);
5557
5558         if (r_glsl_permutation->loc_Texture_Refraction >= 0)
5559         {
5560                 GL_BlendFunc(GL_ONE, GL_ZERO);
5561                 GL_DepthMask(true);
5562                 GL_AlphaTest(false);
5563         }
5564
5565         if (rsurface.uselightmaptexture && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
5566         {
5567                 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
5568                         RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? 12 : -1);
5569                 else
5570                         RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1);
5571         }
5572         else
5573         {
5574                 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
5575                         RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? 12 : -1);
5576                 else
5577                         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5578         }
5579         if (rsurface.texture->backgroundnumskinframes && !(rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
5580         {
5581         }
5582 }
5583
5584 static void R_DrawTextureSurfaceList_GL13(int texturenumsurfaces, msurface_t **texturesurfacelist)
5585 {
5586         // OpenGL 1.3 path - anything not completely ancient
5587         int texturesurfaceindex;
5588         qboolean applycolor;
5589         qboolean applyfog;
5590         rmeshstate_t m;
5591         int layerindex;
5592         const texturelayer_t *layer;
5593         if (rsurface.mode != RSURFMODE_MULTIPASS)
5594                 rsurface.mode = RSURFMODE_MULTIPASS;
5595         RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
5596
5597         for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
5598         {
5599                 vec4_t layercolor;
5600                 int layertexrgbscale;
5601                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5602                 {
5603                         if (layerindex == 0)
5604                                 GL_AlphaTest(true);
5605                         else
5606                         {
5607                                 GL_AlphaTest(false);
5608                                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
5609                         }
5610                 }
5611                 GL_DepthMask(layer->depthmask);
5612                 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
5613                 if ((layer->color[0] > 2 || layer->color[1] > 2 || layer->color[2] > 2) && (gl_combine.integer || layer->depthmask))
5614                 {
5615                         layertexrgbscale = 4;
5616                         VectorScale(layer->color, 0.25f, layercolor);
5617                 }
5618                 else if ((layer->color[0] > 1 || layer->color[1] > 1 || layer->color[2] > 1) && (gl_combine.integer || layer->depthmask))
5619                 {
5620                         layertexrgbscale = 2;
5621                         VectorScale(layer->color, 0.5f, layercolor);
5622                 }
5623                 else
5624                 {
5625                         layertexrgbscale = 1;
5626                         VectorScale(layer->color, 1.0f, layercolor);
5627                 }
5628                 layercolor[3] = layer->color[3];
5629                 applycolor = layercolor[0] != 1 || layercolor[1] != 1 || layercolor[2] != 1 || layercolor[3] != 1;
5630                 R_Mesh_ColorPointer(NULL, 0, 0);
5631                 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
5632                 switch (layer->type)
5633                 {
5634                 case TEXTURELAYERTYPE_LITTEXTURE:
5635                         memset(&m, 0, sizeof(m));
5636                         m.tex[0] = R_GetTexture(r_texture_white);
5637                         m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5638                         m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5639                         m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5640                         m.tex[1] = R_GetTexture(layer->texture);
5641                         m.texmatrix[1] = layer->texmatrix;
5642                         m.texrgbscale[1] = layertexrgbscale;
5643                         m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
5644                         m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
5645                         m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
5646                         R_Mesh_TextureState(&m);
5647                         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5648                                 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5649                         else if (rsurface.uselightmaptexture)
5650                                 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5651                         else
5652                                 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5653                         break;
5654                 case TEXTURELAYERTYPE_TEXTURE:
5655                         memset(&m, 0, sizeof(m));
5656                         m.tex[0] = R_GetTexture(layer->texture);
5657                         m.texmatrix[0] = layer->texmatrix;
5658                         m.texrgbscale[0] = layertexrgbscale;
5659                         m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5660                         m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5661                         m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5662                         R_Mesh_TextureState(&m);
5663                         RSurf_DrawBatch_GL11_Unlit(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5664                         break;
5665                 case TEXTURELAYERTYPE_FOG:
5666                         memset(&m, 0, sizeof(m));
5667                         m.texrgbscale[0] = layertexrgbscale;
5668                         if (layer->texture)
5669                         {
5670                                 m.tex[0] = R_GetTexture(layer->texture);
5671                                 m.texmatrix[0] = layer->texmatrix;
5672                                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5673                                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5674                                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5675                         }
5676                         R_Mesh_TextureState(&m);
5677                         // generate a color array for the fog pass
5678                         R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
5679                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5680                         {
5681                                 int i;
5682                                 float f, *v, *c;
5683                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5684                                 for (i = 0, v = (rsurface.vertex3f + 3 * surface->num_firstvertex), c = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
5685                                 {
5686                                         f = 1 - FogPoint_Model(v);
5687                                         c[0] = layercolor[0];
5688                                         c[1] = layercolor[1];
5689                                         c[2] = layercolor[2];
5690                                         c[3] = f * layercolor[3];
5691                                 }
5692                         }
5693                         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5694                         break;
5695                 default:
5696                         Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
5697                 }
5698                 GL_LockArrays(0, 0);
5699         }
5700         CHECKGLERROR
5701         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5702         {
5703                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
5704                 GL_AlphaTest(false);
5705         }
5706 }
5707
5708 static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, msurface_t **texturesurfacelist)
5709 {
5710         // OpenGL 1.1 - crusty old voodoo path
5711         int texturesurfaceindex;
5712         qboolean applyfog;
5713         rmeshstate_t m;
5714         int layerindex;
5715         const texturelayer_t *layer;
5716         if (rsurface.mode != RSURFMODE_MULTIPASS)
5717                 rsurface.mode = RSURFMODE_MULTIPASS;
5718         RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
5719
5720         for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
5721         {
5722                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5723                 {
5724                         if (layerindex == 0)
5725                                 GL_AlphaTest(true);
5726                         else
5727                         {
5728                                 GL_AlphaTest(false);
5729                                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
5730                         }
5731                 }
5732                 GL_DepthMask(layer->depthmask);
5733                 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
5734                 R_Mesh_ColorPointer(NULL, 0, 0);
5735                 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
5736                 switch (layer->type)
5737                 {
5738                 case TEXTURELAYERTYPE_LITTEXTURE:
5739                         if (layer->blendfunc1 == GL_ONE && layer->blendfunc2 == GL_ZERO)
5740                         {
5741                                 // two-pass lit texture with 2x rgbscale
5742                                 // first the lightmap pass
5743                                 memset(&m, 0, sizeof(m));
5744                                 m.tex[0] = R_GetTexture(r_texture_white);
5745                                 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5746                                 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5747                                 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5748                                 R_Mesh_TextureState(&m);
5749                                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5750                                         RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5751                                 else if (rsurface.uselightmaptexture)
5752                                         RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5753                                 else
5754                                         RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5755                                 GL_LockArrays(0, 0);
5756                                 // then apply the texture to it
5757                                 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
5758                                 memset(&m, 0, sizeof(m));
5759                                 m.tex[0] = R_GetTexture(layer->texture);
5760                                 m.texmatrix[0] = layer->texmatrix;
5761                                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5762                                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5763                                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5764                                 R_Mesh_TextureState(&m);
5765                                 RSurf_DrawBatch_GL11_Unlit(texturenumsurfaces, texturesurfacelist, layer->color[0] * 0.5f, layer->color[1] * 0.5f, layer->color[2] * 0.5f, layer->color[3], layer->color[0] != 2 || layer->color[1] != 2 || layer->color[2] != 2 || layer->color[3] != 1, false);
5766                         }
5767                         else
5768                         {
5769                                 // single pass vertex-lighting-only texture with 1x rgbscale and transparency support
5770                                 memset(&m, 0, sizeof(m));
5771                                 m.tex[0] = R_GetTexture(layer->texture);
5772                                 m.texmatrix[0] = layer->texmatrix;
5773                                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5774                                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5775                                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5776                                 R_Mesh_TextureState(&m);
5777                                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5778                                         RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, layer->color[0], layer->color[1], layer->color[2], layer->color[3], layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
5779                                 else
5780                                         RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, layer->color[0], layer->color[1], layer->color[2], layer->color[3], layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
5781                         }
5782                         break;
5783                 case TEXTURELAYERTYPE_TEXTURE:
5784                         // singletexture unlit texture with transparency support
5785                         memset(&m, 0, sizeof(m));
5786                         m.tex[0] = R_GetTexture(layer->texture);
5787                         m.texmatrix[0] = layer->texmatrix;
5788                         m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5789                         m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5790                         m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5791                         R_Mesh_TextureState(&m);
5792                         RSurf_DrawBatch_GL11_Unlit(texturenumsurfaces, texturesurfacelist, layer->color[0], layer->color[1], layer->color[2], layer->color[3], layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
5793                         break;
5794                 case TEXTURELAYERTYPE_FOG:
5795                         // singletexture fogging
5796                         R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
5797                         if (layer->texture)
5798                         {
5799                                 memset(&m, 0, sizeof(m));
5800                                 m.tex[0] = R_GetTexture(layer->texture);
5801                                 m.texmatrix[0] = layer->texmatrix;
5802                                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5803                                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5804                                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5805                                 R_Mesh_TextureState(&m);
5806                         }
5807                         else
5808                                 R_Mesh_ResetTextureState();
5809                         // generate a color array for the fog pass
5810                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5811                         {
5812                                 int i;
5813                                 float f, *v, *c;
5814                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5815                                 for (i = 0, v = (rsurface.vertex3f + 3 * surface->num_firstvertex), c = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
5816                                 {
5817                                         f = 1 - FogPoint_Model(v);
5818                                         c[0] = layer->color[0];
5819                                         c[1] = layer->color[1];
5820                                         c[2] = layer->color[2];
5821                                         c[3] = f * layer->color[3];
5822                                 }
5823                         }
5824                         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5825                         break;
5826                 default:
5827                         Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
5828                 }
5829                 GL_LockArrays(0, 0);
5830         }
5831         CHECKGLERROR
5832         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5833         {
5834                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
5835                 GL_AlphaTest(false);
5836         }
5837 }
5838
5839 static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth, qboolean depthonly)
5840 {
5841         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW)
5842                 return;
5843         rsurface.rtlight = NULL;
5844         CHECKGLERROR
5845         if (depthonly)
5846         {
5847                 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
5848                         return;
5849                 if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
5850                         return;
5851                 if (rsurface.mode != RSURFMODE_MULTIPASS)
5852                         rsurface.mode = RSURFMODE_MULTIPASS;
5853                 if (r_depthfirst.integer == 3)
5854                 {
5855                         int i = (int)(texturesurfacelist[0] - rsurface.modelsurfaces);
5856                         if (!r_view.showdebug)
5857                                 GL_Color(0, 0, 0, 1);
5858                         else
5859                                 GL_Color(((i >> 6) & 7) / 7.0f, ((i >> 3) & 7) / 7.0f, (i & 7) / 7.0f,1);
5860                 }
5861                 else
5862                 {
5863                         GL_ColorMask(0,0,0,0);
5864                         GL_Color(1,1,1,1);
5865                 }
5866                 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5867                 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5868                 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5869                 GL_DepthTest(true);
5870                 GL_BlendFunc(GL_ONE, GL_ZERO);
5871                 GL_DepthMask(true);
5872                 GL_AlphaTest(false);
5873                 R_Mesh_ColorPointer(NULL, 0, 0);
5874                 R_Mesh_ResetTextureState();
5875                 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5876                 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5877                 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
5878         }
5879         else if (r_depthfirst.integer == 3)
5880                 return;
5881         else if (!r_view.showdebug && (r_showsurfaces.integer || gl_lightmaps.integer))
5882         {
5883                 GL_Color(0, 0, 0, 1);
5884                 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5885         }
5886         else if (r_showsurfaces.integer)
5887         {
5888                 if (rsurface.mode != RSURFMODE_MULTIPASS)
5889                         rsurface.mode = RSURFMODE_MULTIPASS;
5890                 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5891                 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5892                 GL_DepthTest(true);
5893                 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5894                 GL_BlendFunc(GL_ONE, GL_ZERO);
5895                 GL_DepthMask(writedepth);
5896                 GL_Color(1,1,1,1);
5897                 GL_AlphaTest(false);
5898                 R_Mesh_ColorPointer(NULL, 0, 0);
5899                 R_Mesh_ResetTextureState();
5900                 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5901                 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
5902         }
5903         else if (gl_lightmaps.integer)
5904         {
5905                 rmeshstate_t m;
5906                 if (rsurface.mode != RSURFMODE_MULTIPASS)
5907                         rsurface.mode = RSURFMODE_MULTIPASS;
5908                 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5909                 GL_DepthTest(true);
5910                 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5911                 GL_BlendFunc(GL_ONE, GL_ZERO);
5912                 GL_DepthMask(writedepth);
5913                 GL_Color(1,1,1,1);
5914                 GL_AlphaTest(false);
5915                 R_Mesh_ColorPointer(NULL, 0, 0);
5916                 memset(&m, 0, sizeof(m));
5917                 m.tex[0] = R_GetTexture(r_texture_white);
5918                 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5919                 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5920                 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5921                 R_Mesh_TextureState(&m);
5922                 RSurf_PrepareVerticesForBatch(rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, false, texturenumsurfaces, texturesurfacelist);
5923                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5924                         RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5925                 else if (rsurface.uselightmaptexture)
5926                         RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5927                 else
5928                         RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5929         }
5930         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY)
5931                 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
5932         else if (rsurface.texture->currentnumlayers)
5933         {
5934                 // write depth for anything we skipped on the depth-only pass earlier
5935                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5936                         writedepth = true;
5937                 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5938                 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5939                 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5940                 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5941                 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
5942                 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
5943                 GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
5944                 if (r_glsl.integer && gl_support_fragment_shader)
5945                         R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist);
5946                 else if (gl_combine.integer && r_textureunits.integer >= 2)
5947                         R_DrawTextureSurfaceList_GL13(texturenumsurfaces, texturesurfacelist);
5948                 else
5949                         R_DrawTextureSurfaceList_GL11(texturenumsurfaces, texturesurfacelist);
5950         }
5951         CHECKGLERROR
5952         GL_LockArrays(0, 0);
5953 }
5954
5955 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
5956 {
5957         int i, j;
5958         int texturenumsurfaces, endsurface;
5959         texture_t *texture;
5960         msurface_t *surface;
5961         msurface_t *texturesurfacelist[1024];
5962
5963         // if the model is static it doesn't matter what value we give for
5964         // wantnormals and wanttangents, so this logic uses only rules applicable
5965         // to a model, knowing that they are meaningless otherwise
5966         if (ent == r_refdef.worldentity)
5967                 RSurf_ActiveWorldEntity();
5968         else if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
5969                 RSurf_ActiveModelEntity(ent, false, false);
5970         else
5971                 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader);
5972
5973         for (i = 0;i < numsurfaces;i = j)
5974         {
5975                 j = i + 1;
5976                 surface = rsurface.modelsurfaces + surfacelist[i];
5977                 texture = surface->texture;
5978                 R_UpdateTextureInfo(ent, texture);
5979                 rsurface.texture = texture->currentframe;
5980                 rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
5981                 // scan ahead until we find a different texture
5982                 endsurface = min(i + 1024, numsurfaces);
5983                 texturenumsurfaces = 0;
5984                 texturesurfacelist[texturenumsurfaces++] = surface;
5985                 for (;j < endsurface;j++)
5986                 {
5987                         surface = rsurface.modelsurfaces + surfacelist[j];
5988                         if (texture != surface->texture || rsurface.uselightmaptexture != (surface->lightmaptexture != NULL))
5989                                 break;
5990                         texturesurfacelist[texturenumsurfaces++] = surface;
5991                 }
5992                 // render the range of surfaces
5993                 R_DrawTextureSurfaceList(texturenumsurfaces, texturesurfacelist, true, false);
5994         }
5995
5996         RSurf_CleanUp();
5997 }
5998
5999 void R_QueueSurfaceList(entity_render_t *ent, int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes)
6000 {
6001         int i, j;
6002         vec3_t tempcenter, center;
6003         texture_t *texture;
6004         // if we're rendering water textures (extra scene renders), use a separate loop to avoid burdening the main one
6005         if (addwaterplanes)
6006         {
6007                 for (i = 0;i < numsurfaces;i++)
6008                         if (surfacelist[i]->texture->currentframe->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION))
6009                                 R_Water_AddWaterPlane(surfacelist[i]);
6010                 return;
6011         }
6012         // break the surface list down into batches by texture and use of lightmapping
6013         for (i = 0;i < numsurfaces;i = j)
6014         {
6015                 j = i + 1;
6016                 // texture is the base texture pointer, rsurface.texture is the
6017                 // current frame/skin the texture is directing us to use (for example
6018                 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
6019                 // use skin 1 instead)
6020                 texture = surfacelist[i]->texture;
6021                 rsurface.texture = texture->currentframe;
6022                 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
6023                 if (!(rsurface.texture->currentmaterialflags & flagsmask))
6024                 {
6025                         // if this texture is not the kind we want, skip ahead to the next one
6026                         for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
6027                                 ;
6028                         continue;
6029                 }
6030                 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
6031                 {
6032                         // transparent surfaces get pushed off into the transparent queue
6033                         const msurface_t *surface = surfacelist[i];
6034                         if (depthonly)
6035                                 continue;
6036                         tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
6037                         tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
6038                         tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
6039                         Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
6040                         R_MeshQueue_AddTransparent(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_view.origin : center, R_DrawSurface_TransparentCallback, ent, surface - rsurface.modelsurfaces, rsurface.rtlight);
6041                 }
6042                 else
6043                 {
6044                         // simply scan ahead until we find a different texture or lightmap state
6045                         for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.uselightmaptexture == (surfacelist[j]->lightmaptexture != NULL);j++)
6046                                 ;
6047                         // render the range of surfaces
6048                         R_DrawTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly);
6049                 }
6050         }
6051 }
6052
6053 float locboxvertex3f[6*4*3] =
6054 {
6055         1,0,1, 1,0,0, 1,1,0, 1,1,1,
6056         0,1,1, 0,1,0, 0,0,0, 0,0,1,
6057         1,1,1, 1,1,0, 0,1,0, 0,1,1,
6058         0,0,1, 0,0,0, 1,0,0, 1,0,1,
6059         0,0,1, 1,0,1, 1,1,1, 0,1,1,
6060         1,0,0, 0,0,0, 0,1,0, 1,1,0
6061 };
6062
6063 int locboxelement3i[6*2*3] =
6064 {
6065          0, 1, 2, 0, 2, 3,
6066          4, 5, 6, 4, 6, 7,
6067          8, 9,10, 8,10,11,
6068         12,13,14, 12,14,15,
6069         16,17,18, 16,18,19,
6070         20,21,22, 20,22,23
6071 };
6072
6073 void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6074 {
6075         int i, j;
6076         cl_locnode_t *loc = (cl_locnode_t *)ent;
6077         vec3_t mins, size;
6078         float vertex3f[6*4*3];
6079         CHECKGLERROR
6080         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6081         GL_DepthMask(false);
6082         GL_DepthRange(0, 1);
6083         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6084         GL_DepthTest(true);
6085         GL_CullFace(GL_NONE);
6086         R_Mesh_Matrix(&identitymatrix);
6087
6088         R_Mesh_VertexPointer(vertex3f, 0, 0);
6089         R_Mesh_ColorPointer(NULL, 0, 0);
6090         R_Mesh_ResetTextureState();
6091
6092         i = surfacelist[0];
6093         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_view.colorscale,
6094                          ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_view.colorscale,
6095                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_view.colorscale,
6096                         surfacelist[0] < 0 ? 0.5f : 0.125f);
6097
6098         if (VectorCompare(loc->mins, loc->maxs))
6099         {
6100                 VectorSet(size, 2, 2, 2);
6101                 VectorMA(loc->mins, -0.5f, size, mins);
6102         }
6103         else
6104         {
6105                 VectorCopy(loc->mins, mins);
6106                 VectorSubtract(loc->maxs, loc->mins, size);
6107         }
6108
6109         for (i = 0;i < 6*4*3;)
6110                 for (j = 0;j < 3;j++, i++)
6111                         vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
6112
6113         R_Mesh_Draw(0, 6*4, 6*2, locboxelement3i, 0, 0);
6114 }
6115
6116 void R_DrawLocs(void)
6117 {
6118         int index;
6119         cl_locnode_t *loc, *nearestloc;
6120         vec3_t center;
6121         nearestloc = CL_Locs_FindNearest(cl.movement_origin);
6122         for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
6123         {
6124                 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
6125                 R_MeshQueue_AddTransparent(center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
6126         }
6127 }
6128
6129 void R_DrawDebugModel(entity_render_t *ent)
6130 {
6131         int i, j, k, l, flagsmask;
6132         const int *elements;
6133         q3mbrush_t *brush;
6134         msurface_t *surface;
6135         model_t *model = ent->model;
6136         vec3_t v;
6137
6138         flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WATER | MATERIALFLAG_WALL;
6139
6140         R_Mesh_ColorPointer(NULL, 0, 0);
6141         R_Mesh_ResetTextureState();
6142         GL_DepthRange(0, 1);
6143         GL_DepthTest(!r_showdisabledepthtest.integer);
6144         GL_DepthMask(false);
6145         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6146
6147         if (r_showcollisionbrushes.value > 0 && model->brush.num_brushes)
6148         {
6149                 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
6150                 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
6151                 {
6152                         if (brush->colbrushf && brush->colbrushf->numtriangles)
6153                         {
6154                                 R_Mesh_VertexPointer(brush->colbrushf->points->v, 0, 0);
6155                                 GL_Color((i & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 5) & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 10) & 31) * (1.0f / 32.0f) * r_view.colorscale, r_showcollisionbrushes.value);
6156                                 R_Mesh_Draw(0, brush->colbrushf->numpoints, brush->colbrushf->numtriangles, brush->colbrushf->elements, 0, 0);
6157                         }
6158                 }
6159                 for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
6160                 {
6161                         if (surface->num_collisiontriangles)
6162                         {
6163                                 R_Mesh_VertexPointer(surface->data_collisionvertex3f, 0, 0);
6164                                 GL_Color((i & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 5) & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 10) & 31) * (1.0f / 32.0f) * r_view.colorscale, r_showcollisionbrushes.value);
6165                                 R_Mesh_Draw(0, surface->num_collisionvertices, surface->num_collisiontriangles, surface->data_collisionelement3i, 0, 0);
6166                         }
6167                 }
6168         }
6169
6170         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6171
6172         if (r_showtris.integer || r_shownormals.integer)
6173         {
6174                 if (r_showdisabledepthtest.integer)
6175                 {
6176                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6177                         GL_DepthMask(false);
6178                 }
6179                 else
6180                 {
6181                         GL_BlendFunc(GL_ONE, GL_ZERO);
6182                         GL_DepthMask(true);
6183                 }
6184                 for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
6185                 {
6186                         if (ent == r_refdef.worldentity && !r_viewcache.world_surfacevisible[j])
6187                                 continue;
6188                         rsurface.texture = surface->texture->currentframe;
6189                         if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
6190                         {
6191                                 RSurf_PrepareVerticesForBatch(true, true, 1, &surface);
6192                                 if (r_showtris.value > 0)
6193                                 {
6194                                         if (!rsurface.texture->currentlayers->depthmask)
6195                                                 GL_Color(r_view.colorscale, 0, 0, r_showtris.value);
6196                                         else if (ent == r_refdef.worldentity)
6197                                                 GL_Color(r_view.colorscale, r_view.colorscale, r_view.colorscale, r_showtris.value);
6198                                         else
6199                                                 GL_Color(0, r_view.colorscale, 0, r_showtris.value);
6200                                         elements = (ent->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
6201                                         CHECKGLERROR
6202                                         qglBegin(GL_LINES);
6203                                         for (k = 0;k < surface->num_triangles;k++, elements += 3)
6204                                         {
6205 #define GLVERTEXELEMENT(n) qglVertex3f(rsurface.vertex3f[elements[n]*3+0], rsurface.vertex3f[elements[n]*3+1], rsurface.vertex3f[elements[n]*3+2])
6206                                                 GLVERTEXELEMENT(0);GLVERTEXELEMENT(1);
6207                                                 GLVERTEXELEMENT(1);GLVERTEXELEMENT(2);
6208                                                 GLVERTEXELEMENT(2);GLVERTEXELEMENT(0);
6209                                         }
6210                                         qglEnd();
6211                                         CHECKGLERROR
6212                                 }
6213                                 if (r_shownormals.value > 0)
6214                                 {
6215                                         GL_Color(r_view.colorscale, 0, 0, r_shownormals.value);
6216                                         qglBegin(GL_LINES);
6217                                         for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6218                                         {
6219                                                 VectorCopy(rsurface.vertex3f + l * 3, v);
6220                                                 qglVertex3f(v[0], v[1], v[2]);
6221                                                 VectorMA(v, 8, rsurface.svector3f + l * 3, v);
6222                                                 qglVertex3f(v[0], v[1], v[2]);
6223                                         }
6224                                         qglEnd();
6225                                         CHECKGLERROR
6226                                         GL_Color(0, 0, r_view.colorscale, r_shownormals.value);
6227                                         qglBegin(GL_LINES);
6228                                         for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6229                                         {
6230                                                 VectorCopy(rsurface.vertex3f + l * 3, v);
6231                                                 qglVertex3f(v[0], v[1], v[2]);
6232                                                 VectorMA(v, 8, rsurface.tvector3f + l * 3, v);
6233                                                 qglVertex3f(v[0], v[1], v[2]);
6234                                         }
6235                                         qglEnd();
6236                                         CHECKGLERROR
6237                                         GL_Color(0, r_view.colorscale, 0, r_shownormals.value);
6238                                         qglBegin(GL_LINES);
6239                                         for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6240                                         {
6241                                                 VectorCopy(rsurface.vertex3f + l * 3, v);
6242                                                 qglVertex3f(v[0], v[1], v[2]);
6243                                                 VectorMA(v, 8, rsurface.normal3f + l * 3, v);
6244                                                 qglVertex3f(v[0], v[1], v[2]);
6245                                         }
6246                                         qglEnd();
6247                                         CHECKGLERROR
6248                                 }
6249                         }
6250                 }
6251                 rsurface.texture = NULL;
6252         }
6253 }
6254
6255 extern void R_BuildLightMap(const entity_render_t *ent, msurface_t *surface);
6256 void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
6257 {
6258         int i, j, endj, f, flagsmask;
6259         msurface_t *surface, **surfacechain;
6260         texture_t *t;
6261         model_t *model = r_refdef.worldmodel;
6262         const int maxsurfacelist = 1024;
6263         int numsurfacelist = 0;
6264         msurface_t *surfacelist[1024];
6265         if (model == NULL)
6266                 return;
6267
6268         RSurf_ActiveWorldEntity();
6269
6270         // update light styles
6271         if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.light_styleupdatechains)
6272         {
6273                 for (i = 0;i < model->brushq1.light_styles;i++)
6274                 {
6275                         if (model->brushq1.light_stylevalue[i] != r_refdef.lightstylevalue[model->brushq1.light_style[i]])
6276                         {
6277                                 model->brushq1.light_stylevalue[i] = r_refdef.lightstylevalue[model->brushq1.light_style[i]];
6278                                 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
6279                                         for (;(surface = *surfacechain);surfacechain++)
6280                                                 surface->cached_dlight = true;
6281                         }
6282                 }
6283         }
6284
6285         R_UpdateAllTextureInfo(r_refdef.worldentity);
6286         flagsmask = addwaterplanes ? (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
6287
6288         if (debug)
6289         {
6290                 R_DrawDebugModel(r_refdef.worldentity);
6291                 return;
6292         }
6293
6294         f = 0;
6295         t = NULL;
6296         rsurface.uselightmaptexture = false;
6297         rsurface.texture = NULL;
6298         numsurfacelist = 0;
6299         j = model->firstmodelsurface;
6300         endj = j + model->nummodelsurfaces;
6301         while (j < endj)
6302         {
6303                 // quickly skip over non-visible surfaces
6304                 for (;j < endj && !r_viewcache.world_surfacevisible[j];j++)
6305                         ;
6306                 // quickly iterate over visible surfaces
6307                 for (;j < endj && r_viewcache.world_surfacevisible[j];j++)
6308                 {
6309                         // process this surface
6310                         surface = model->data_surfaces + j;
6311                         // if this surface fits the criteria, add it to the list
6312                         if (surface->num_triangles)
6313                         {
6314                                 // if lightmap parameters changed, rebuild lightmap texture
6315                                 if (surface->cached_dlight)
6316                                         R_BuildLightMap(r_refdef.worldentity, surface);
6317                                 // add face to draw list
6318                                 surfacelist[numsurfacelist++] = surface;
6319                                 r_refdef.stats.world_triangles += surface->num_triangles;
6320                                 if (numsurfacelist >= maxsurfacelist)
6321                                 {
6322                                         r_refdef.stats.world_surfaces += numsurfacelist;
6323                                         R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6324                                         numsurfacelist = 0;
6325                                 }
6326                         }
6327                 }
6328         }
6329         r_refdef.stats.world_surfaces += numsurfacelist;
6330         if (numsurfacelist)
6331                 R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6332         RSurf_CleanUp();
6333 }
6334
6335 void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
6336 {
6337         int i, f, flagsmask;
6338         msurface_t *surface, *endsurface, **surfacechain;
6339         texture_t *t;
6340         model_t *model = ent->model;
6341         const int maxsurfacelist = 1024;
6342         int numsurfacelist = 0;
6343         msurface_t *surfacelist[1024];
6344         if (model == NULL)
6345                 return;
6346
6347         // if the model is static it doesn't matter what value we give for
6348         // wantnormals and wanttangents, so this logic uses only rules applicable
6349         // to a model, knowing that they are meaningless otherwise
6350         if (ent == r_refdef.worldentity)
6351                 RSurf_ActiveWorldEntity();
6352         else if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
6353                 RSurf_ActiveModelEntity(ent, false, false);
6354         else
6355                 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader && !depthonly);
6356
6357         // update light styles
6358         if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.light_styleupdatechains)
6359         {
6360                 for (i = 0;i < model->brushq1.light_styles;i++)
6361                 {
6362                         if (model->brushq1.light_stylevalue[i] != r_refdef.lightstylevalue[model->brushq1.light_style[i]])
6363                         {
6364                                 model->brushq1.light_stylevalue[i] = r_refdef.lightstylevalue[model->brushq1.light_style[i]];
6365                                 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
6366                                         for (;(surface = *surfacechain);surfacechain++)
6367                                                 surface->cached_dlight = true;
6368                         }
6369                 }
6370         }
6371
6372         R_UpdateAllTextureInfo(ent);
6373         flagsmask = addwaterplanes ? (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
6374
6375         if (debug)
6376         {
6377                 R_DrawDebugModel(ent);
6378                 return;
6379         }
6380
6381         f = 0;
6382         t = NULL;
6383         rsurface.uselightmaptexture = false;
6384         rsurface.texture = NULL;
6385         numsurfacelist = 0;
6386         surface = model->data_surfaces + model->firstmodelsurface;
6387         endsurface = surface + model->nummodelsurfaces;
6388         for (;surface < endsurface;surface++)
6389         {
6390                 // if this surface fits the criteria, add it to the list
6391                 if (surface->num_triangles)
6392                 {
6393                         // if lightmap parameters changed, rebuild lightmap texture
6394                         if (surface->cached_dlight)
6395                                 R_BuildLightMap(ent, surface);
6396                         // add face to draw list
6397                         surfacelist[numsurfacelist++] = surface;
6398                         r_refdef.stats.entities_triangles += surface->num_triangles;
6399                         if (numsurfacelist >= maxsurfacelist)
6400                         {
6401                                 r_refdef.stats.entities_surfaces += numsurfacelist;
6402                                 R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6403                                 numsurfacelist = 0;
6404                         }
6405                 }
6406         }
6407         r_refdef.stats.entities_surfaces += numsurfacelist;
6408         if (numsurfacelist)
6409                 R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6410         RSurf_CleanUp();
6411 }