2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
23 #include "cl_dyntexture.h"
28 mempool_t *r_main_mempool;
29 rtexturepool_t *r_main_texturepool;
31 static int r_frame = 0; ///< used only by R_GetCurrentTexture
38 cvar_t r_motionblur = {CVAR_SAVE, "r_motionblur", "0", "motionblur value scale - 0.5 recommended"};
39 cvar_t r_damageblur = {CVAR_SAVE, "r_damageblur", "0", "motionblur based on damage"};
40 cvar_t r_motionblur_vmin = {CVAR_SAVE, "r_motionblur_vmin", "300", "minimum influence from velocity"};
41 cvar_t r_motionblur_vmax = {CVAR_SAVE, "r_motionblur_vmax", "600", "maximum influence from velocity"};
42 cvar_t r_motionblur_bmin = {CVAR_SAVE, "r_motionblur_bmin", "0.5", "velocity at which there is no blur yet (may be negative to always have some blur)"};
43 cvar_t r_motionblur_vcoeff = {CVAR_SAVE, "r_motionblur_vcoeff", "0.05", "sliding average reaction time for velocity"};
44 cvar_t r_motionblur_maxblur = {CVAR_SAVE, "r_motionblur_maxblur", "0.88", "cap for motionblur alpha value"};
45 cvar_t r_motionblur_randomize = {CVAR_SAVE, "r_motionblur_randomize", "0.1", "randomizing coefficient to workaround ghosting"};
47 cvar_t r_animcache = {CVAR_SAVE, "r_animcache", "1", "cache animation frames to save CPU usage, primarily optimizes shadows and reflections"};
49 cvar_t r_depthfirst = {CVAR_SAVE, "r_depthfirst", "0", "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"};
50 cvar_t r_useinfinitefarclip = {CVAR_SAVE, "r_useinfinitefarclip", "1", "enables use of a special kind of projection matrix that has an extremely large farclip"};
51 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
52 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%, 10 = 100%)"};
53 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)"};
54 cvar_t r_showtris = {0, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
55 cvar_t r_shownormals = {0, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
56 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"};
57 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"};
58 cvar_t r_showcollisionbrushes = {0, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
59 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"};
60 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"};
61 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"};
62 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
63 cvar_t r_drawentities = {0, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
64 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1", "draw your weapon model"};
65 cvar_t r_cullentities_trace = {0, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
66 cvar_t r_cullentities_trace_samples = {0, "r_cullentities_trace_samples", "2", "number of samples to test for entity culling"};
67 cvar_t r_cullentities_trace_enlarge = {0, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
68 cvar_t r_cullentities_trace_delay = {0, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
69 cvar_t r_speeds = {0, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
70 cvar_t r_fullbright = {0, "r_fullbright","0", "makes map very bright and renders faster"};
71 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1", "opacity of water polygons"};
72 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
73 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
74 cvar_t r_shadows = {CVAR_SAVE, "r_shadows", "0", "casts fake stencil shadows from models onto the world (rtlights are unaffected by this); when set to 2, always cast the shadows in the direction set by r_shadows_throwdirection, otherwise use the model lighting."};
75 cvar_t r_shadows_darken = {CVAR_SAVE, "r_shadows_darken", "0.5", "how much shadowed areas will be darkened"};
76 cvar_t r_shadows_throwdistance = {CVAR_SAVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
77 cvar_t r_shadows_throwdirection = {CVAR_SAVE, "r_shadows_throwdirection", "0 0 -1", "override throwing direction for r_shadows 2"};
78 cvar_t r_shadows_drawafterrtlighting = {CVAR_SAVE, "r_shadows_drawafterrtlighting", "0", "draw fake shadows AFTER realtime lightning is drawn. May be useful for simulating fast sunlight on large outdoor maps with only one noshadow rtlight. The price is less realistic appearance of dynamic light shadows."};
79 cvar_t r_shadows_castfrombmodels = {CVAR_SAVE, "r_shadows_castfrombmodels", "0", "do cast shadows from bmodels"};
80 cvar_t r_q1bsp_skymasking = {0, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
81 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"};
82 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"};
83 cvar_t r_fog_exp2 = {0, "r_fog_exp2", "0", "uses GL_EXP2 fog (as in Nehahra) rather than realistic GL_EXP fog"};
84 cvar_t r_drawfog = {CVAR_SAVE, "r_drawfog", "1", "allows one to disable fog rendering"};
86 cvar_t gl_fogenable = {0, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
87 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
88 cvar_t gl_fogred = {0, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
89 cvar_t gl_foggreen = {0, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
90 cvar_t gl_fogblue = {0, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
91 cvar_t gl_fogstart = {0, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
92 cvar_t gl_fogend = {0, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
93 cvar_t gl_skyclip = {0, "gl_skyclip", "4608", "nehahra farclip distance - the real fog end (for Nehahra compatibility only)"};
95 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)"};
97 cvar_t r_glsl = {CVAR_SAVE, "r_glsl", "1", "enables use of OpenGL 2.0 pixel shaders for lighting"};
98 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)"};
99 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
100 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
101 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
102 cvar_t r_glsl_postprocess = {CVAR_SAVE, "r_glsl_postprocess", "0", "use a GLSL postprocessing shader"};
103 cvar_t r_glsl_postprocess_uservec1 = {CVAR_SAVE, "r_glsl_postprocess_uservec1", "0 0 0 0", "a 4-component vector to pass as uservec1 to the postprocessing shader (only useful if default.glsl has been customized)"};
104 cvar_t r_glsl_postprocess_uservec2 = {CVAR_SAVE, "r_glsl_postprocess_uservec2", "0 0 0 0", "a 4-component vector to pass as uservec2 to the postprocessing shader (only useful if default.glsl has been customized)"};
105 cvar_t r_glsl_postprocess_uservec3 = {CVAR_SAVE, "r_glsl_postprocess_uservec3", "0 0 0 0", "a 4-component vector to pass as uservec3 to the postprocessing shader (only useful if default.glsl has been customized)"};
106 cvar_t r_glsl_postprocess_uservec4 = {CVAR_SAVE, "r_glsl_postprocess_uservec4", "0 0 0 0", "a 4-component vector to pass as uservec4 to the postprocessing shader (only useful if default.glsl has been customized)"};
107 cvar_t r_glsl_usegeneric = {CVAR_SAVE, "r_glsl_usegeneric", "1", "use shaders for rendering simple geometry (rather than conventional fixed-function rendering for this purpose)"};
109 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)"};
110 cvar_t r_water_clippingplanebias = {CVAR_SAVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
111 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"};
112 cvar_t r_water_refractdistort = {CVAR_SAVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
113 cvar_t r_water_reflectdistort = {CVAR_SAVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
115 cvar_t r_lerpsprites = {CVAR_SAVE, "r_lerpsprites", "1", "enables animation smoothing on sprites"};
116 cvar_t r_lerpmodels = {CVAR_SAVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
117 cvar_t r_lerplightstyles = {CVAR_SAVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
118 cvar_t r_waterscroll = {CVAR_SAVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
120 cvar_t r_bloom = {CVAR_SAVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
121 cvar_t r_bloom_colorscale = {CVAR_SAVE, "r_bloom_colorscale", "1", "how bright the glow is"};
122 cvar_t r_bloom_brighten = {CVAR_SAVE, "r_bloom_brighten", "2", "how bright the glow is, after subtract/power"};
123 cvar_t r_bloom_blur = {CVAR_SAVE, "r_bloom_blur", "4", "how large the glow is"};
124 cvar_t r_bloom_resolution = {CVAR_SAVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
125 cvar_t r_bloom_colorexponent = {CVAR_SAVE, "r_bloom_colorexponent", "1", "how exagerated the glow is"};
126 cvar_t r_bloom_colorsubtract = {CVAR_SAVE, "r_bloom_colorsubtract", "0.125", "reduces bloom colors by a certain amount"};
128 cvar_t r_hdr = {CVAR_SAVE, "r_hdr", "0", "enables High Dynamic Range bloom effect (higher quality version of r_bloom)"};
129 cvar_t r_hdr_scenebrightness = {CVAR_SAVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
130 cvar_t r_hdr_glowintensity = {CVAR_SAVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
131 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)"};
133 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"};
135 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"};
137 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers)"};
139 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
140 cvar_t r_batchmode = {0, "r_batchmode", "1", "selects method of rendering multiple surfaces with one driver call (values are 0, 1, 2, etc...)"};
141 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"};
142 cvar_t r_track_sprites_flags = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accodringly, 2: Make it a continuous rotation"};
143 cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
144 cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
145 cvar_t r_glsl_saturation = {CVAR_SAVE, "r_glsl_saturation", "1", "saturation multiplier (only working in glsl!)"};
147 extern cvar_t v_glslgamma;
149 extern qboolean v_flipped_state;
151 static struct r_bloomstate_s
156 int bloomwidth, bloomheight;
158 int screentexturewidth, screentextureheight;
159 rtexture_t *texture_screen; /// \note also used for motion blur if enabled!
161 int bloomtexturewidth, bloomtextureheight;
162 rtexture_t *texture_bloom;
164 // arrays for rendering the screen passes
165 float screentexcoord2f[8];
166 float bloomtexcoord2f[8];
167 float offsettexcoord2f[8];
169 r_viewport_t viewport;
173 r_waterstate_t r_waterstate;
175 /// shadow volume bsp struct with automatically growing nodes buffer
178 rtexture_t *r_texture_blanknormalmap;
179 rtexture_t *r_texture_white;
180 rtexture_t *r_texture_grey128;
181 rtexture_t *r_texture_black;
182 rtexture_t *r_texture_notexture;
183 rtexture_t *r_texture_whitecube;
184 rtexture_t *r_texture_normalizationcube;
185 rtexture_t *r_texture_fogattenuation;
186 rtexture_t *r_texture_gammaramps;
187 unsigned int r_texture_gammaramps_serial;
188 //rtexture_t *r_texture_fogintensity;
190 unsigned int r_queries[R_MAX_OCCLUSION_QUERIES];
191 unsigned int r_numqueries;
192 unsigned int r_maxqueries;
194 char r_qwskincache[MAX_SCOREBOARD][MAX_QPATH];
195 skinframe_t *r_qwskincache_skinframe[MAX_SCOREBOARD];
197 /// vertex coordinates for a quad that covers the screen exactly
198 const float r_screenvertex3f[12] =
206 extern void R_DrawModelShadows(void);
208 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
211 for (i = 0;i < verts;i++)
222 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
225 for (i = 0;i < verts;i++)
235 // FIXME: move this to client?
238 if (gamemode == GAME_NEHAHRA)
240 Cvar_Set("gl_fogenable", "0");
241 Cvar_Set("gl_fogdensity", "0.2");
242 Cvar_Set("gl_fogred", "0.3");
243 Cvar_Set("gl_foggreen", "0.3");
244 Cvar_Set("gl_fogblue", "0.3");
246 r_refdef.fog_density = 0;
247 r_refdef.fog_red = 0;
248 r_refdef.fog_green = 0;
249 r_refdef.fog_blue = 0;
250 r_refdef.fog_alpha = 1;
251 r_refdef.fog_start = 0;
252 r_refdef.fog_end = 0;
255 float FogForDistance(vec_t dist)
257 unsigned int fogmasktableindex = (unsigned int)(dist * r_refdef.fogmasktabledistmultiplier);
258 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
261 float FogPoint_World(const vec3_t p)
263 return FogForDistance(VectorDistance((p), r_refdef.view.origin));
266 float FogPoint_Model(const vec3_t p)
268 return FogForDistance(VectorDistance((p), rsurface.modelorg) * Matrix4x4_ScaleFromMatrix(&rsurface.matrix));
271 static void R_BuildBlankTextures(void)
273 unsigned char data[4];
274 data[2] = 128; // normal X
275 data[1] = 128; // normal Y
276 data[0] = 255; // normal Z
277 data[3] = 128; // height
278 r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
283 r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
288 r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
293 r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
296 static void R_BuildNoTexture(void)
299 unsigned char pix[16][16][4];
300 // this makes a light grey/dark grey checkerboard texture
301 for (y = 0;y < 16;y++)
303 for (x = 0;x < 16;x++)
305 if ((y < 8) ^ (x < 8))
321 r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, NULL);
324 static void R_BuildWhiteCube(void)
326 unsigned char data[6*1*1*4];
327 memset(data, 255, sizeof(data));
328 r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
331 static void R_BuildNormalizationCube(void)
335 vec_t s, t, intensity;
337 unsigned char data[6][NORMSIZE][NORMSIZE][4];
338 for (side = 0;side < 6;side++)
340 for (y = 0;y < NORMSIZE;y++)
342 for (x = 0;x < NORMSIZE;x++)
344 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
345 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
380 intensity = 127.0f / sqrt(DotProduct(v, v));
381 data[side][y][x][2] = (unsigned char)(128.0f + intensity * v[0]);
382 data[side][y][x][1] = (unsigned char)(128.0f + intensity * v[1]);
383 data[side][y][x][0] = (unsigned char)(128.0f + intensity * v[2]);
384 data[side][y][x][3] = 255;
388 r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, &data[0][0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
391 static void R_BuildFogTexture(void)
395 unsigned char data1[FOGWIDTH][4];
396 //unsigned char data2[FOGWIDTH][4];
399 r_refdef.fogmasktable_start = r_refdef.fog_start;
400 r_refdef.fogmasktable_alpha = r_refdef.fog_alpha;
401 r_refdef.fogmasktable_range = r_refdef.fogrange;
402 r_refdef.fogmasktable_density = r_refdef.fog_density;
404 r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
405 for (x = 0;x < FOGMASKTABLEWIDTH;x++)
407 d = (x * r - r_refdef.fogmasktable_start);
408 if(developer.integer >= 100)
409 Con_Printf("%f ", d);
411 if (r_fog_exp2.integer)
412 alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
414 alpha = exp(-r_refdef.fogmasktable_density * 0.004 * d);
415 if(developer.integer >= 100)
416 Con_Printf(" : %f ", alpha);
417 alpha = 1 - (1 - alpha) * r_refdef.fogmasktable_alpha;
418 if(developer.integer >= 100)
419 Con_Printf(" = %f\n", alpha);
420 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
423 for (x = 0;x < FOGWIDTH;x++)
425 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
430 //data2[x][0] = 255 - b;
431 //data2[x][1] = 255 - b;
432 //data2[x][2] = 255 - b;
435 if (r_texture_fogattenuation)
437 R_UpdateTexture(r_texture_fogattenuation, &data1[0][0], 0, 0, FOGWIDTH, 1);
438 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, FOGWIDTH, 1);
442 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);
443 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
447 static const char *builtinshaderstring =
448 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
449 "// written by Forest 'LordHavoc' Hale\n"
451 "// enable various extensions depending on permutation:\n"
453 "#ifdef USESHADOWMAPRECT\n"
454 "# extension GL_ARB_texture_rectangle : enable\n"
457 "#ifdef USESHADOWMAP2D\n"
458 "# ifdef GL_EXT_gpu_shader4\n"
459 "# extension GL_EXT_gpu_shader4 : enable\n"
461 "# ifdef GL_ARB_texture_gather\n"
462 "# extension GL_ARB_texture_gather : enable\n"
464 "# ifdef GL_AMD_texture_texture4\n"
465 "# extension GL_AMD_texture_texture4 : enable\n"
470 "#ifdef USESHADOWMAPCUBE\n"
471 "# extension GL_EXT_gpu_shader4 : enable\n"
474 "#ifdef USESHADOWSAMPLER\n"
475 "# extension GL_ARB_shadow : enable\n"
478 "// common definitions between vertex shader and fragment shader:\n"
480 "//#ifdef __GLSL_CG_DATA_TYPES\n"
481 "//# define myhalf half\n"
482 "//# define myhalf2 half2\n"
483 "//# define myhalf3half3\n"
484 "//# define myhalf4 half4\n"
486 "# define myhalf float\n"
487 "# define myhalf2 vec2\n"
488 "# define myhalf3 vec3\n"
489 "# define myhalf4 vec4\n"
492 "#ifdef MODE_DEPTH_OR_SHADOW\n"
494 "# ifdef VERTEX_SHADER\n"
497 " gl_Position = ftransform();\n"
502 "#ifdef MODE_SHOWDEPTH\n"
503 "# ifdef VERTEX_SHADER\n"
506 " gl_Position = ftransform();\n"
507 " gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
510 "# ifdef FRAGMENT_SHADER\n"
513 " gl_FragColor = gl_Color;\n"
517 "#else // !MODE_SHOWDEPTH\n"
519 "#ifdef MODE_POSTPROCESS\n"
520 "# ifdef VERTEX_SHADER\n"
523 " gl_FrontColor = gl_Color;\n"
524 " gl_Position = ftransform();\n"
525 " gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n"
527 " gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;\n"
531 "# ifdef FRAGMENT_SHADER\n"
533 "uniform sampler2D Texture_First;\n"
535 "uniform sampler2D Texture_Second;\n"
537 "#ifdef USEGAMMARAMPS\n"
538 "uniform sampler2D Texture_GammaRamps;\n"
540 "#ifdef USESATURATION\n"
541 "uniform float Saturation;\n"
543 "#ifdef USEVIEWTINT\n"
544 "uniform vec4 TintColor;\n"
546 "//uncomment these if you want to use them:\n"
547 "uniform vec4 UserVec1;\n"
548 "// uniform vec4 UserVec2;\n"
549 "// uniform vec4 UserVec3;\n"
550 "// uniform vec4 UserVec4;\n"
551 "// uniform float ClientTime;\n"
552 "uniform vec2 PixelSize;\n"
555 " gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy);\n"
557 " gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy);\n"
559 "#ifdef USEVIEWTINT\n"
560 " gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a);\n"
563 "#ifdef USEPOSTPROCESSING\n"
564 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
565 "// this code does a blur with the radius specified in the first component of r_glsl_postprocess_uservec1 and blends it using the second component\n"
566 " gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y;\n"
567 " gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y;\n"
568 " gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y;\n"
569 " gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107, 0.707107)) * UserVec1.y;\n"
570 " gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990, 0.891007)) * UserVec1.y;\n"
571 " gl_FragColor /= (1 + 5 * UserVec1.y);\n"
574 "#ifdef USESATURATION\n"
575 " //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
576 " myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n"
577 " //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation;\n"
578 " gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation);\n"
581 "#ifdef USEGAMMARAMPS\n"
582 " gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r;\n"
583 " gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g;\n"
584 " gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b;\n"
591 "#ifdef MODE_GENERIC\n"
592 "# ifdef VERTEX_SHADER\n"
595 " gl_FrontColor = gl_Color;\n"
596 "# ifdef USEDIFFUSE\n"
597 " gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n"
599 "# ifdef USESPECULAR\n"
600 " gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;\n"
602 " gl_Position = ftransform();\n"
605 "# ifdef FRAGMENT_SHADER\n"
607 "# ifdef USEDIFFUSE\n"
608 "uniform sampler2D Texture_First;\n"
610 "# ifdef USESPECULAR\n"
611 "uniform sampler2D Texture_Second;\n"
616 " gl_FragColor = gl_Color;\n"
617 "# ifdef USEDIFFUSE\n"
618 " gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy);\n"
621 "# ifdef USESPECULAR\n"
622 " vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy);\n"
624 "# ifdef USECOLORMAPPING\n"
625 " gl_FragColor *= tex2;\n"
628 " gl_FragColor += tex2;\n"
630 "# ifdef USEVERTEXTEXTUREBLEND\n"
631 " gl_FragColor = mix(gl_FragColor, tex2, tex2.a);\n"
636 "#else // !MODE_GENERIC\n"
638 "varying vec2 TexCoord;\n"
639 "#ifdef USEVERTEXTEXTUREBLEND\n"
640 "varying vec2 TexCoord2;\n"
642 "varying vec2 TexCoordLightmap;\n"
644 "#ifdef MODE_LIGHTSOURCE\n"
645 "varying vec3 CubeVector;\n"
648 "#ifdef MODE_LIGHTSOURCE\n"
649 "varying vec3 LightVector;\n"
651 "#ifdef MODE_LIGHTDIRECTION\n"
652 "varying vec3 LightVector;\n"
655 "varying vec3 EyeVector;\n"
657 "varying vec3 EyeVectorModelSpace;\n"
660 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
661 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
662 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
664 "#ifdef MODE_WATER\n"
665 "varying vec4 ModelViewProjectionPosition;\n"
667 "#ifdef MODE_REFRACTION\n"
668 "varying vec4 ModelViewProjectionPosition;\n"
670 "#ifdef USEREFLECTION\n"
671 "varying vec4 ModelViewProjectionPosition;\n"
678 "// vertex shader specific:\n"
679 "#ifdef VERTEX_SHADER\n"
681 "uniform vec3 LightPosition;\n"
682 "uniform vec3 EyePosition;\n"
683 "uniform vec3 LightDir;\n"
685 "// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3), this would require sending a 4 component texcoord1 with W as 1 or -1 according to which side the texcoord2 should be on\n"
689 " gl_FrontColor = gl_Color;\n"
690 " // copy the surface texcoord\n"
691 " TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);\n"
692 "#ifdef USEVERTEXTEXTUREBLEND\n"
693 " TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0);\n"
695 "#ifndef MODE_LIGHTSOURCE\n"
696 "# ifndef MODE_LIGHTDIRECTION\n"
697 " TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
701 "#ifdef MODE_LIGHTSOURCE\n"
702 " // transform vertex position into light attenuation/cubemap space\n"
703 " // (-1 to +1 across the light box)\n"
704 " CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex);\n"
706 " // transform unnormalized light direction into tangent space\n"
707 " // (we use unnormalized to ensure that it interpolates correctly and then\n"
708 " // normalize it per pixel)\n"
709 " vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
710 " LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
711 " LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
712 " LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
715 "#ifdef MODE_LIGHTDIRECTION\n"
716 " LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
717 " LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
718 " LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
721 " // transform unnormalized eye direction into tangent space\n"
723 " vec3 EyeVectorModelSpace;\n"
725 " EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
726 " EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
727 " EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
728 " EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
730 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
731 " VectorS = gl_MultiTexCoord1.xyz;\n"
732 " VectorT = gl_MultiTexCoord2.xyz;\n"
733 " VectorR = gl_MultiTexCoord3.xyz;\n"
736 "//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION)\n"
737 "// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix;\n"
738 "// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
739 "// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
742 "// transform vertex to camera space, using ftransform to match non-VS\n"
744 " gl_Position = ftransform();\n"
746 "#ifdef MODE_WATER\n"
747 " ModelViewProjectionPosition = gl_Position;\n"
749 "#ifdef MODE_REFRACTION\n"
750 " ModelViewProjectionPosition = gl_Position;\n"
752 "#ifdef USEREFLECTION\n"
753 " ModelViewProjectionPosition = gl_Position;\n"
757 "#endif // VERTEX_SHADER\n"
762 "// fragment shader specific:\n"
763 "#ifdef FRAGMENT_SHADER\n"
765 "// 13 textures, we can only use up to 16 on DX9-class hardware\n"
766 "uniform sampler2D Texture_Normal;\n"
767 "uniform sampler2D Texture_Color;\n"
768 "uniform sampler2D Texture_Gloss;\n"
769 "uniform sampler2D Texture_Glow;\n"
770 "uniform sampler2D Texture_SecondaryNormal;\n"
771 "uniform sampler2D Texture_SecondaryColor;\n"
772 "uniform sampler2D Texture_SecondaryGloss;\n"
773 "uniform sampler2D Texture_SecondaryGlow;\n"
774 "uniform sampler2D Texture_Pants;\n"
775 "uniform sampler2D Texture_Shirt;\n"
776 "uniform sampler2D Texture_FogMask;\n"
777 "uniform sampler2D Texture_Lightmap;\n"
778 "uniform sampler2D Texture_Deluxemap;\n"
779 "uniform sampler2D Texture_Refraction;\n"
780 "uniform sampler2D Texture_Reflection;\n"
781 "uniform sampler2D Texture_Attenuation;\n"
782 "uniform samplerCube Texture_Cube;\n"
784 "#define showshadowmap 0\n"
786 "#ifdef USESHADOWMAPRECT\n"
787 "# ifdef USESHADOWSAMPLER\n"
788 "uniform sampler2DRectShadow Texture_ShadowMapRect;\n"
790 "uniform sampler2DRect Texture_ShadowMapRect;\n"
794 "#ifdef USESHADOWMAP2D\n"
795 "# ifdef USESHADOWSAMPLER\n"
796 "uniform sampler2DShadow Texture_ShadowMap2D;\n"
798 "uniform sampler2D Texture_ShadowMap2D;\n"
802 "#ifdef USESHADOWMAPVSDCT\n"
803 "uniform samplerCube Texture_CubeProjection;\n"
806 "#ifdef USESHADOWMAPCUBE\n"
807 "# ifdef USESHADOWSAMPLER\n"
808 "uniform samplerCubeShadow Texture_ShadowMapCube;\n"
810 "uniform samplerCube Texture_ShadowMapCube;\n"
814 "uniform myhalf3 LightColor;\n"
815 "uniform myhalf3 AmbientColor;\n"
816 "uniform myhalf3 DiffuseColor;\n"
817 "uniform myhalf3 SpecularColor;\n"
818 "uniform myhalf3 Color_Pants;\n"
819 "uniform myhalf3 Color_Shirt;\n"
820 "uniform myhalf3 FogColor;\n"
822 "uniform myhalf4 TintColor;\n"
825 "//#ifdef MODE_WATER\n"
826 "uniform vec4 DistortScaleRefractReflect;\n"
827 "uniform vec4 ScreenScaleRefractReflect;\n"
828 "uniform vec4 ScreenCenterRefractReflect;\n"
829 "uniform myhalf4 RefractColor;\n"
830 "uniform myhalf4 ReflectColor;\n"
831 "uniform myhalf ReflectFactor;\n"
832 "uniform myhalf ReflectOffset;\n"
834 "//# ifdef MODE_REFRACTION\n"
835 "//uniform vec4 DistortScaleRefractReflect;\n"
836 "//uniform vec4 ScreenScaleRefractReflect;\n"
837 "//uniform vec4 ScreenCenterRefractReflect;\n"
838 "//uniform myhalf4 RefractColor;\n"
839 "//# ifdef USEREFLECTION\n"
840 "//uniform myhalf4 ReflectColor;\n"
843 "//# ifdef USEREFLECTION\n"
844 "//uniform vec4 DistortScaleRefractReflect;\n"
845 "//uniform vec4 ScreenScaleRefractReflect;\n"
846 "//uniform vec4 ScreenCenterRefractReflect;\n"
847 "//uniform myhalf4 ReflectColor;\n"
852 "uniform myhalf3 GlowColor;\n"
853 "uniform myhalf SceneBrightness;\n"
855 "uniform float OffsetMapping_Scale;\n"
856 "uniform float OffsetMapping_Bias;\n"
857 "uniform float FogRangeRecip;\n"
859 "uniform myhalf AmbientScale;\n"
860 "uniform myhalf DiffuseScale;\n"
861 "uniform myhalf SpecularScale;\n"
862 "uniform myhalf SpecularPower;\n"
864 "#ifdef USEOFFSETMAPPING\n"
865 "vec2 OffsetMapping(vec2 TexCoord)\n"
867 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
868 " // 14 sample relief mapping: linear search and then binary search\n"
869 " // this basically steps forward a small amount repeatedly until it finds\n"
870 " // itself inside solid, then jitters forward and back using decreasing\n"
871 " // amounts to find the impact\n"
872 " //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
873 " //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
874 " vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
875 " vec3 RT = vec3(TexCoord, 1);\n"
876 " OffsetVector *= 0.1;\n"
877 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
878 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
879 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
880 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
881 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
882 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
883 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
884 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
885 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
886 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5);\n"
887 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25);\n"
888 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125);\n"
889 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625);\n"
890 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
893 " // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
894 " // this basically moves forward the full distance, and then backs up based\n"
895 " // on height of samples\n"
896 " //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
897 " //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
898 " vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
899 " TexCoord += OffsetVector;\n"
900 " OffsetVector *= 0.333;\n"
901 " TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
902 " TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
903 " TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
904 " return TexCoord;\n"
907 "#endif // USEOFFSETMAPPING\n"
909 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE)\n"
910 "uniform vec2 ShadowMap_TextureScale;\n"
911 "uniform vec4 ShadowMap_Parameters;\n"
914 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
915 "vec3 GetShadowMapTC2D(vec3 dir)\n"
917 " vec3 adir = abs(dir);\n"
918 "# ifndef USESHADOWMAPVSDCT\n"
922 " if (adir.x > adir.y)\n"
924 " if (adir.x > adir.z) // X\n"
928 " offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5);\n"
934 " offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
939 " if (adir.y > adir.z) // Y\n"
943 " offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5);\n"
949 " offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
953 " vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
954 " stc.xy += offset * ShadowMap_Parameters.y;\n"
955 " stc.z += ShadowMap_Parameters.z;\n"
956 "# if showshadowmap\n"
957 " stc.xy *= ShadowMap_TextureScale;\n"
961 " vec4 proj = textureCube(Texture_CubeProjection, dir);\n"
962 " float ma = max(max(adir.x, adir.y), adir.z);\n"
963 " vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
964 " stc.xy += proj.zw * ShadowMap_Parameters.y;\n"
965 " stc.z += ShadowMap_Parameters.z;\n"
966 "# if showshadowmap\n"
967 " stc.xy *= ShadowMap_TextureScale;\n"
972 "#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
974 "#ifdef USESHADOWMAPCUBE\n"
975 "vec4 GetShadowMapTCCube(vec3 dir)\n"
977 " vec3 adir = abs(dir);\n"
978 " return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z));\n"
982 "#if !showshadowmap\n"
983 "# ifdef USESHADOWMAPRECT\n"
984 "float ShadowMapCompare(vec3 dir)\n"
986 " vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
988 "# ifdef USESHADOWSAMPLER\n"
990 "# ifdef USESHADOWMAPPCF\n"
991 "# define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r\n"
992 " f = dot(vec4(0.25), vec4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
994 " f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r;\n"
999 "# ifdef USESHADOWMAPPCF\n"
1000 "# if USESHADOWMAPPCF > 1\n"
1001 "# define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r\n"
1002 " vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1003 " vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
1004 " vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0), texval( 2.0, 0.0)));\n"
1005 " vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0), texval( 2.0, 1.0)));\n"
1006 " vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0, 2.0), texval( 0.0, 2.0), texval( 1.0, 2.0), texval( 2.0, 2.0)));\n"
1007 " vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1008 " f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1010 "# define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r\n"
1011 " vec2 offset = fract(shadowmaptc.xy);\n"
1012 " vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1013 " vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0)));\n"
1014 " vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0)));\n"
1015 " vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1016 " f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1019 " f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r);\n"
1027 "# ifdef USESHADOWMAP2D\n"
1028 "float ShadowMapCompare(vec3 dir)\n"
1030 " vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
1033 "# ifdef USESHADOWSAMPLER\n"
1034 "# ifdef USESHADOWMAPPCF\n"
1035 "# define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r \n"
1036 " vec2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
1037 " f = dot(vec4(0.25), vec4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
1039 " f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r;\n"
1042 "# ifdef USESHADOWMAPPCF\n"
1043 "# if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
1044 "# ifdef GL_ARB_texture_gather\n"
1045 "# define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y))\n"
1047 "# define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale)\n"
1049 " vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1050 " center *= ShadowMap_TextureScale;\n"
1051 " vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
1052 " vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
1053 " vec4 group3 = step(shadowmaptc.z, texval(-1.0, 1.0));\n"
1054 " vec4 group4 = step(shadowmaptc.z, texval( 1.0, 1.0));\n"
1055 " vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +\n"
1056 " mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);\n"
1057 " f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1059 "# ifdef GL_EXT_gpu_shader4\n"
1060 "# define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r\n"
1062 "# define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r \n"
1064 "# if USESHADOWMAPPCF > 1\n"
1065 " vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1066 " center *= ShadowMap_TextureScale;\n"
1067 " vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
1068 " vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0), texval( 2.0, 0.0)));\n"
1069 " vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0), texval( 2.0, 1.0)));\n"
1070 " vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0, 2.0), texval( 0.0, 2.0), texval( 1.0, 2.0), texval( 2.0, 2.0)));\n"
1071 " vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1072 " f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1074 " vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy);\n"
1075 " vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1076 " vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0, 0.0), texval( 0.0, 0.0), texval( 1.0, 0.0)));\n"
1077 " vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0, 1.0), texval( 0.0, 1.0), texval( 1.0, 1.0)));\n"
1078 " vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1079 " f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1083 " f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
1090 "# ifdef USESHADOWMAPCUBE\n"
1091 "float ShadowMapCompare(vec3 dir)\n"
1093 " // apply depth texture cubemap as light filter\n"
1094 " vec4 shadowmaptc = GetShadowMapTCCube(dir);\n"
1096 "# ifdef USESHADOWSAMPLER\n"
1097 " f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r;\n"
1099 " f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r);\n"
1106 "#ifdef MODE_WATER\n"
1111 "#ifdef USEOFFSETMAPPING\n"
1112 " // apply offsetmapping\n"
1113 " vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1114 "#define TexCoord TexCoordOffset\n"
1117 " vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1118 " //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1119 " vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1120 " vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect;\n"
1121 " // FIXME temporary hack to detect the case that the reflection\n"
1122 " // gets blackened at edges due to leaving the area that contains actual\n"
1124 " // Remove this 'ack once we have a better way to stop this thing from\n"
1126 " float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1127 " f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1128 " f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1129 " f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1130 " ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
1131 " f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1132 " f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1133 " f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1134 " f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1135 " ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
1136 " float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
1137 " gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel);\n"
1140 "#else // !MODE_WATER\n"
1141 "#ifdef MODE_REFRACTION\n"
1143 "// refraction pass\n"
1146 "#ifdef USEOFFSETMAPPING\n"
1147 " // apply offsetmapping\n"
1148 " vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1149 "#define TexCoord TexCoordOffset\n"
1152 " vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
1153 " //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
1154 " vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
1155 " vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
1156 " // FIXME temporary hack to detect the case that the reflection\n"
1157 " // gets blackened at edges due to leaving the area that contains actual\n"
1159 " // Remove this 'ack once we have a better way to stop this thing from\n"
1161 " float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1162 " f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1163 " f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1164 " f *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1165 " ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1166 " gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor;\n"
1169 "#else // !MODE_REFRACTION\n"
1172 "#ifdef USEOFFSETMAPPING\n"
1173 " // apply offsetmapping\n"
1174 " vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1175 "#define TexCoord TexCoordOffset\n"
1178 " // combine the diffuse textures (base, pants, shirt)\n"
1179 " myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord));\n"
1180 "#ifdef USECOLORMAPPING\n"
1181 " color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
1183 "#ifdef USEVERTEXTEXTUREBLEND\n"
1184 " myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0));\n"
1185 " //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0));\n"
1186 " //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5;\n"
1187 " color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);\n"
1189 " //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend);\n"
1192 "#ifdef USEDIFFUSE\n"
1193 " // get the surface normal and the gloss color\n"
1194 "# ifdef USEVERTEXTEXTUREBLEND\n"
1195 " myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5));\n"
1196 "# ifdef USESPECULAR\n"
1197 " myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend);\n"
1200 " myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5));\n"
1201 "# ifdef USESPECULAR\n"
1202 " myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord));\n"
1209 "#ifdef MODE_LIGHTSOURCE\n"
1210 " // light source\n"
1212 " // calculate surface normal, light normal, and specular normal\n"
1213 " // compute color intensity for the two textures (colormap and glossmap)\n"
1214 " // scale by light color and attenuation as efficiently as possible\n"
1215 " // (do as much scalar math as possible rather than vector math)\n"
1216 "# ifdef USEDIFFUSE\n"
1217 " // get the light normal\n"
1218 " myhalf3 diffusenormal = myhalf3(normalize(LightVector));\n"
1220 "# ifdef USESPECULAR\n"
1221 "# ifndef USEEXACTSPECULARMATH\n"
1222 " myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector)));\n"
1225 " // calculate directional shading\n"
1226 "# ifdef USEEXACTSPECULARMATH\n"
1227 " color.rgb = 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(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor);\n"
1229 " color.rgb = 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)) * glosscolor);\n"
1232 "# ifdef USEDIFFUSE\n"
1233 " // calculate directional shading\n"
1234 " color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))));\n"
1236 " // calculate directionless shading\n"
1237 " color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1241 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D)\n"
1242 "#if !showshadowmap\n"
1243 " color.rgb *= ShadowMapCompare(CubeVector);\n"
1247 "# ifdef USECUBEFILTER\n"
1248 " // apply light cubemap filter\n"
1249 " //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector));\n"
1250 " color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector));\n"
1252 "#endif // MODE_LIGHTSOURCE\n"
1257 "#ifdef MODE_LIGHTDIRECTION\n"
1258 " // directional model lighting\n"
1259 "# ifdef USEDIFFUSE\n"
1260 " // get the light normal\n"
1261 " myhalf3 diffusenormal = myhalf3(normalize(LightVector));\n"
1263 "# ifdef USESPECULAR\n"
1264 " // calculate directional shading\n"
1265 " color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
1266 "# ifdef USEEXACTSPECULARMATH\n"
1267 " color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1269 " myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector)));\n"
1270 " color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1273 "# ifdef USEDIFFUSE\n"
1275 " // calculate directional shading\n"
1276 " color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
1278 " color.rgb *= AmbientColor;\n"
1281 "#endif // MODE_LIGHTDIRECTION\n"
1286 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1287 " // deluxemap lightmapping using light vectors in modelspace (evil q3map2)\n"
1289 " // get the light normal\n"
1290 " myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1291 " myhalf3 diffusenormal;\n"
1292 " diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS));\n"
1293 " diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT));\n"
1294 " diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR));\n"
1295 " // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1296 " // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
1297 " // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
1298 " // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
1299 " // to map the luxels to coordinates on the draw surfaces), which also causes\n"
1300 " // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
1301 " // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
1302 " // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
1303 " // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
1304 " myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0)));\n"
1305 " // 0.25 supports up to 75.5 degrees normal/deluxe angle\n"
1306 "# ifdef USESPECULAR\n"
1307 "# ifdef USEEXACTSPECULARMATH\n"
1308 " tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1310 " myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector))));\n"
1311 " tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1315 " // apply lightmap color\n"
1316 " color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1317 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1322 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1323 " // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
1325 " // get the light normal\n"
1326 " myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1327 " // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1328 " myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0)));\n"
1329 "# ifdef USESPECULAR\n"
1330 "# ifdef USEEXACTSPECULARMATH\n"
1331 " tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1333 " myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector))));\n"
1334 " tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1338 " // apply lightmap color\n"
1339 " color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1340 "#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1345 "#ifdef MODE_LIGHTMAP\n"
1346 " // apply lightmap color\n"
1347 " color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale;\n"
1348 "#endif // MODE_LIGHTMAP\n"
1353 "#ifdef MODE_VERTEXCOLOR\n"
1354 " // apply lightmap color\n"
1355 " color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale;\n"
1356 "#endif // MODE_VERTEXCOLOR\n"
1361 "#ifdef MODE_FLATCOLOR\n"
1362 "#endif // MODE_FLATCOLOR\n"
1370 " color *= TintColor;\n"
1373 "#ifdef USEVERTEXTEXTUREBLEND\n"
1374 " color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend);\n"
1376 " color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowColor;\n"
1380 " color.rgb *= SceneBrightness;\n"
1382 " // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately\n"
1384 " color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0))));\n"
1387 " // reflection must come last because it already contains exactly the correct fog (the reflection render preserves camera distance from the plane, it only flips the side) and ContrastBoost/SceneBrightness\n"
1388 "#ifdef USEREFLECTION\n"
1389 " vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1390 " //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1391 " vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
1392 " vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;\n"
1393 " // FIXME temporary hack to detect the case that the reflection\n"
1394 " // gets blackened at edges due to leaving the area that contains actual\n"
1396 " // Remove this 'ack once we have a better way to stop this thing from\n"
1398 " float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1399 " f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1400 " f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1401 " f *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1402 " ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1403 " color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);\n"
1406 " gl_FragColor = vec4(color);\n"
1408 "#if showshadowmap\n"
1409 "# ifdef USESHADOWMAPRECT\n"
1410 "# ifdef USESHADOWSAMPLER\n"
1411 " gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz);\n"
1413 " gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy);\n"
1416 "# ifdef USESHADOWMAP2D\n"
1417 "# ifdef USESHADOWSAMPLER\n"
1418 " gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz);\n"
1420 " gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy);\n"
1424 "# ifdef USESHADOWMAPCUBE\n"
1425 "# ifdef USESHADOWSAMPLER\n"
1426 " gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector));\n"
1428 " gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz);\n"
1433 "#endif // !MODE_REFRACTION\n"
1434 "#endif // !MODE_WATER\n"
1436 "#endif // FRAGMENT_SHADER\n"
1438 "#endif // !MODE_GENERIC\n"
1439 "#endif // !MODE_POSTPROCESS\n"
1440 "#endif // !MODE_SHOWDEPTH\n"
1441 "#endif // !MODE_DEPTH_OR_SHADOW\n"
1444 typedef struct shaderpermutationinfo_s
1446 const char *pretext;
1449 shaderpermutationinfo_t;
1451 typedef struct shadermodeinfo_s
1453 const char *vertexfilename;
1454 const char *geometryfilename;
1455 const char *fragmentfilename;
1456 const char *pretext;
1461 typedef enum shaderpermutation_e
1463 SHADERPERMUTATION_DIFFUSE = 1<<0, ///< (lightsource) whether to use directional shading
1464 SHADERPERMUTATION_VERTEXTEXTUREBLEND = 1<<1, ///< indicates this is a two-layer material blend based on vertex alpha (q3bsp)
1465 SHADERPERMUTATION_VIEWTINT = 1<<1, ///< view tint (postprocessing only)
1466 SHADERPERMUTATION_COLORMAPPING = 1<<2, ///< indicates this is a colormapped skin
1467 SHADERPERMUTATION_SATURATION = 1<<2, ///< saturation (postprocessing only)
1468 SHADERPERMUTATION_FOG = 1<<3, ///< tint the color by fog color or black if using additive blend mode
1469 SHADERPERMUTATION_GAMMARAMPS = 1<<3, ///< gamma (postprocessing only)
1470 SHADERPERMUTATION_CUBEFILTER = 1<<4, ///< (lightsource) use cubemap light filter
1471 SHADERPERMUTATION_GLOW = 1<<5, ///< (lightmap) blend in an additive glow texture
1472 SHADERPERMUTATION_BLOOM = 1<<5, ///< bloom (postprocessing only)
1473 SHADERPERMUTATION_SPECULAR = 1<<6, ///< (lightsource or deluxemapping) render specular effects
1474 SHADERPERMUTATION_POSTPROCESSING = 1<<6, ///< user defined postprocessing (postprocessing only)
1475 SHADERPERMUTATION_EXACTSPECULARMATH = 1<<7, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
1476 SHADERPERMUTATION_REFLECTION = 1<<8, ///< normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
1477 SHADERPERMUTATION_OFFSETMAPPING = 1<<9, ///< adjust texcoords to roughly simulate a displacement mapped surface
1478 SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING = 1<<10, ///< adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
1479 SHADERPERMUTATION_SHADOWMAPRECT = 1<<11, ///< (lightsource) use shadowmap rectangle texture as light filter
1480 SHADERPERMUTATION_SHADOWMAPCUBE = 1<<12, ///< (lightsource) use shadowmap cubemap texture as light filter
1481 SHADERPERMUTATION_SHADOWMAP2D = 1<<13, ///< (lightsource) use shadowmap rectangle texture as light filter
1482 SHADERPERMUTATION_SHADOWMAPPCF = 1<<14, //< (lightsource) use percentage closer filtering on shadowmap test results
1483 SHADERPERMUTATION_SHADOWMAPPCF2 = 1<<15, //< (lightsource) use higher quality percentage closer filtering on shadowmap test results
1484 SHADERPERMUTATION_SHADOWSAMPLER = 1<<16, //< (lightsource) use hardware shadowmap test
1485 SHADERPERMUTATION_SHADOWMAPVSDCT = 1<<17, //< (lightsource) use virtual shadow depth cube texture for shadowmap indexing
1486 SHADERPERMUTATION_LIMIT = 1<<18, ///< size of permutations array
1487 SHADERPERMUTATION_COUNT = 18 ///< size of shaderpermutationinfo array
1489 shaderpermutation_t;
1491 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
1492 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
1494 {"#define USEDIFFUSE\n", " diffuse"},
1495 {"#define USEVERTEXTEXTUREBLEND\n#define USEVIEWTINT\n", " vertextextureblend/tint"},
1496 {"#define USECOLORMAPPING\n#define USESATURATION\n", " colormapping/saturation"},
1497 {"#define USEFOG\n#define USEGAMMARAMPS\n", " fog/gammaramps"},
1498 {"#define USECUBEFILTER\n", " cubefilter"},
1499 {"#define USEGLOW\n#define USEBLOOM\n", " glow/bloom"},
1500 {"#define USESPECULAR\n#define USEPOSTPROCESSING", " specular/postprocessing"},
1501 {"#define USEEXACTSPECULARMATH\n", " exactspecularmath"},
1502 {"#define USEREFLECTION\n", " reflection"},
1503 {"#define USEOFFSETMAPPING\n", " offsetmapping"},
1504 {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
1505 {"#define USESHADOWMAPRECT\n", " shadowmaprect"},
1506 {"#define USESHADOWMAPCUBE\n", " shadowmapcube"},
1507 {"#define USESHADOWMAP2D\n", " shadowmap2d"},
1508 {"#define USESHADOWMAPPCF 1\n", " shadowmappcf"},
1509 {"#define USESHADOWMAPPCF 2\n", " shadowmappcf2"},
1510 {"#define USESHADOWSAMPLER\n", " shadowsampler"},
1511 {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"},
1514 /// this enum is multiplied by SHADERPERMUTATION_MODEBASE
1515 typedef enum shadermode_e
1517 SHADERMODE_GENERIC, ///< (particles/HUD/etc) vertex color, optionally multiplied by one texture
1518 SHADERMODE_POSTPROCESS, ///< postprocessing shader (r_glsl_postprocess)
1519 SHADERMODE_DEPTH_OR_SHADOW, ///< (depthfirst/shadows) vertex shader only
1520 SHADERMODE_FLATCOLOR, ///< (lightmap) modulate texture by uniform color (q1bsp, q3bsp)
1521 SHADERMODE_VERTEXCOLOR, ///< (lightmap) modulate texture by vertex colors (q3bsp)
1522 SHADERMODE_LIGHTMAP, ///< (lightmap) modulate texture by lightmap texture (q1bsp, q3bsp)
1523 SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE, ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1524 SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE, ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1525 SHADERMODE_LIGHTDIRECTION, ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1526 SHADERMODE_LIGHTSOURCE, ///< (lightsource) use directional pixel shading from light source (rtlight)
1527 SHADERMODE_REFRACTION, ///< refract background (the material is rendered normally after this pass)
1528 SHADERMODE_WATER, ///< refract background and reflection (the material is rendered normally after this pass)
1529 SHADERMODE_SHOWDEPTH, ///< (debugging) renders depth as color
1534 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
1535 shadermodeinfo_t shadermodeinfo[SHADERMODE_COUNT] =
1537 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_GENERIC\n", " generic"},
1538 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_POSTPROCESS\n", " postprocess"},
1539 {"glsl/default.glsl", NULL, NULL , "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
1540 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
1541 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
1542 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTMAP\n", " lightmap"},
1543 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
1544 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
1545 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
1546 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
1547 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_REFRACTION\n", " refraction"},
1548 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_WATER\n", " water"},
1549 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
1552 struct r_glsl_permutation_s;
1553 typedef struct r_glsl_permutation_s
1555 /// hash lookup data
1556 struct r_glsl_permutation_s *hashnext;
1558 unsigned int permutation;
1560 /// indicates if we have tried compiling this permutation already
1562 /// 0 if compilation failed
1564 /// locations of detected uniforms in program object, or -1 if not found
1565 int loc_Texture_First;
1566 int loc_Texture_Second;
1567 int loc_Texture_GammaRamps;
1568 int loc_Texture_Normal;
1569 int loc_Texture_Color;
1570 int loc_Texture_Gloss;
1571 int loc_Texture_Glow;
1572 int loc_Texture_SecondaryNormal;
1573 int loc_Texture_SecondaryColor;
1574 int loc_Texture_SecondaryGloss;
1575 int loc_Texture_SecondaryGlow;
1576 int loc_Texture_Pants;
1577 int loc_Texture_Shirt;
1578 int loc_Texture_FogMask;
1579 int loc_Texture_Lightmap;
1580 int loc_Texture_Deluxemap;
1581 int loc_Texture_Attenuation;
1582 int loc_Texture_Cube;
1583 int loc_Texture_Refraction;
1584 int loc_Texture_Reflection;
1585 int loc_Texture_ShadowMapRect;
1586 int loc_Texture_ShadowMapCube;
1587 int loc_Texture_ShadowMap2D;
1588 int loc_Texture_CubeProjection;
1590 int loc_LightPosition;
1591 int loc_EyePosition;
1592 int loc_Color_Pants;
1593 int loc_Color_Shirt;
1594 int loc_FogRangeRecip;
1595 int loc_AmbientScale;
1596 int loc_DiffuseScale;
1597 int loc_SpecularScale;
1598 int loc_SpecularPower;
1600 int loc_SceneBrightness; // or: Scenebrightness * ContrastBoost
1601 int loc_OffsetMapping_Scale;
1603 int loc_AmbientColor;
1604 int loc_DiffuseColor;
1605 int loc_SpecularColor;
1607 int loc_ContrastBoostCoeff; ///< 1 - 1/ContrastBoost
1608 int loc_GammaCoeff; ///< 1 / gamma
1609 int loc_DistortScaleRefractReflect;
1610 int loc_ScreenScaleRefractReflect;
1611 int loc_ScreenCenterRefractReflect;
1612 int loc_RefractColor;
1613 int loc_ReflectColor;
1614 int loc_ReflectFactor;
1615 int loc_ReflectOffset;
1623 int loc_ShadowMap_TextureScale;
1624 int loc_ShadowMap_Parameters;
1626 r_glsl_permutation_t;
1628 #define SHADERPERMUTATION_HASHSIZE 4096
1630 /// information about each possible shader permutation
1631 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
1632 /// currently selected permutation
1633 r_glsl_permutation_t *r_glsl_permutation;
1634 /// storage for permutations linked in the hash table
1635 memexpandablearray_t r_glsl_permutationarray;
1637 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, unsigned int permutation)
1639 //unsigned int hashdepth = 0;
1640 unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
1641 r_glsl_permutation_t *p;
1642 for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
1644 if (p->mode == mode && p->permutation == permutation)
1646 //if (hashdepth > 10)
1647 // Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
1652 p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
1654 p->permutation = permutation;
1655 p->hashnext = r_glsl_permutationhash[mode][hashindex];
1656 r_glsl_permutationhash[mode][hashindex] = p;
1657 //if (hashdepth > 10)
1658 // Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
1662 static char *R_GLSL_GetText(const char *filename, qboolean printfromdisknotice)
1665 if (!filename || !filename[0])
1667 shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
1670 if (printfromdisknotice)
1671 Con_DPrint("from disk... ");
1672 return shaderstring;
1674 else if (!strcmp(filename, "glsl/default.glsl"))
1676 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(builtinshaderstring) + 1);
1677 memcpy(shaderstring, builtinshaderstring, strlen(builtinshaderstring) + 1);
1679 return shaderstring;
1682 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, unsigned int permutation)
1685 shadermodeinfo_t *modeinfo = shadermodeinfo + mode;
1686 int vertstrings_count = 0;
1687 int geomstrings_count = 0;
1688 int fragstrings_count = 0;
1689 char *vertexstring, *geometrystring, *fragmentstring;
1690 const char *vertstrings_list[32+3];
1691 const char *geomstrings_list[32+3];
1692 const char *fragstrings_list[32+3];
1693 char permutationname[256];
1700 permutationname[0] = 0;
1701 vertexstring = R_GLSL_GetText(modeinfo->vertexfilename, true);
1702 geometrystring = R_GLSL_GetText(modeinfo->geometryfilename, false);
1703 fragmentstring = R_GLSL_GetText(modeinfo->fragmentfilename, false);
1705 strlcat(permutationname, shadermodeinfo[mode].vertexfilename, sizeof(permutationname));
1707 // the first pretext is which type of shader to compile as
1708 // (later these will all be bound together as a program object)
1709 vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
1710 geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
1711 fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
1713 // the second pretext is the mode (for example a light source)
1714 vertstrings_list[vertstrings_count++] = shadermodeinfo[mode].pretext;
1715 geomstrings_list[geomstrings_count++] = shadermodeinfo[mode].pretext;
1716 fragstrings_list[fragstrings_count++] = shadermodeinfo[mode].pretext;
1717 strlcat(permutationname, modeinfo->name, sizeof(permutationname));
1719 // now add all the permutation pretexts
1720 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1722 if (permutation & (1<<i))
1724 vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
1725 geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
1726 fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
1727 strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
1731 // keep line numbers correct
1732 vertstrings_list[vertstrings_count++] = "\n";
1733 geomstrings_list[geomstrings_count++] = "\n";
1734 fragstrings_list[fragstrings_count++] = "\n";
1738 // now append the shader text itself
1739 vertstrings_list[vertstrings_count++] = vertexstring;
1740 geomstrings_list[geomstrings_count++] = geometrystring;
1741 fragstrings_list[fragstrings_count++] = fragmentstring;
1743 // if any sources were NULL, clear the respective list
1745 vertstrings_count = 0;
1746 if (!geometrystring)
1747 geomstrings_count = 0;
1748 if (!fragmentstring)
1749 fragstrings_count = 0;
1751 // compile the shader program
1752 if (vertstrings_count + geomstrings_count + fragstrings_count)
1753 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1757 qglUseProgramObjectARB(p->program);CHECKGLERROR
1758 // look up all the uniform variable names we care about, so we don't
1759 // have to look them up every time we set them
1760 p->loc_Texture_First = qglGetUniformLocationARB(p->program, "Texture_First");
1761 p->loc_Texture_Second = qglGetUniformLocationARB(p->program, "Texture_Second");
1762 p->loc_Texture_GammaRamps = qglGetUniformLocationARB(p->program, "Texture_GammaRamps");
1763 p->loc_Texture_Normal = qglGetUniformLocationARB(p->program, "Texture_Normal");
1764 p->loc_Texture_Color = qglGetUniformLocationARB(p->program, "Texture_Color");
1765 p->loc_Texture_Gloss = qglGetUniformLocationARB(p->program, "Texture_Gloss");
1766 p->loc_Texture_Glow = qglGetUniformLocationARB(p->program, "Texture_Glow");
1767 p->loc_Texture_SecondaryNormal = qglGetUniformLocationARB(p->program, "Texture_SecondaryNormal");
1768 p->loc_Texture_SecondaryColor = qglGetUniformLocationARB(p->program, "Texture_SecondaryColor");
1769 p->loc_Texture_SecondaryGloss = qglGetUniformLocationARB(p->program, "Texture_SecondaryGloss");
1770 p->loc_Texture_SecondaryGlow = qglGetUniformLocationARB(p->program, "Texture_SecondaryGlow");
1771 p->loc_Texture_FogMask = qglGetUniformLocationARB(p->program, "Texture_FogMask");
1772 p->loc_Texture_Pants = qglGetUniformLocationARB(p->program, "Texture_Pants");
1773 p->loc_Texture_Shirt = qglGetUniformLocationARB(p->program, "Texture_Shirt");
1774 p->loc_Texture_Lightmap = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
1775 p->loc_Texture_Deluxemap = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
1776 p->loc_Texture_Refraction = qglGetUniformLocationARB(p->program, "Texture_Refraction");
1777 p->loc_Texture_Reflection = qglGetUniformLocationARB(p->program, "Texture_Reflection");
1778 p->loc_Texture_Attenuation = qglGetUniformLocationARB(p->program, "Texture_Attenuation");
1779 p->loc_Texture_Cube = qglGetUniformLocationARB(p->program, "Texture_Cube");
1780 p->loc_Texture_ShadowMapRect = qglGetUniformLocationARB(p->program, "Texture_ShadowMapRect");
1781 p->loc_Texture_ShadowMapCube = qglGetUniformLocationARB(p->program, "Texture_ShadowMapCube");
1782 p->loc_Texture_ShadowMap2D = qglGetUniformLocationARB(p->program, "Texture_ShadowMap2D");
1783 p->loc_Texture_CubeProjection = qglGetUniformLocationARB(p->program, "Texture_CubeProjection");
1784 p->loc_FogColor = qglGetUniformLocationARB(p->program, "FogColor");
1785 p->loc_LightPosition = qglGetUniformLocationARB(p->program, "LightPosition");
1786 p->loc_EyePosition = qglGetUniformLocationARB(p->program, "EyePosition");
1787 p->loc_Color_Pants = qglGetUniformLocationARB(p->program, "Color_Pants");
1788 p->loc_Color_Shirt = qglGetUniformLocationARB(p->program, "Color_Shirt");
1789 p->loc_FogRangeRecip = qglGetUniformLocationARB(p->program, "FogRangeRecip");
1790 p->loc_AmbientScale = qglGetUniformLocationARB(p->program, "AmbientScale");
1791 p->loc_DiffuseScale = qglGetUniformLocationARB(p->program, "DiffuseScale");
1792 p->loc_SpecularPower = qglGetUniformLocationARB(p->program, "SpecularPower");
1793 p->loc_SpecularScale = qglGetUniformLocationARB(p->program, "SpecularScale");
1794 p->loc_GlowColor = qglGetUniformLocationARB(p->program, "GlowColor");
1795 p->loc_SceneBrightness = qglGetUniformLocationARB(p->program, "SceneBrightness");
1796 p->loc_OffsetMapping_Scale = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
1797 p->loc_TintColor = qglGetUniformLocationARB(p->program, "TintColor");
1798 p->loc_AmbientColor = qglGetUniformLocationARB(p->program, "AmbientColor");
1799 p->loc_DiffuseColor = qglGetUniformLocationARB(p->program, "DiffuseColor");
1800 p->loc_SpecularColor = qglGetUniformLocationARB(p->program, "SpecularColor");
1801 p->loc_LightDir = qglGetUniformLocationARB(p->program, "LightDir");
1802 p->loc_ContrastBoostCoeff = qglGetUniformLocationARB(p->program, "ContrastBoostCoeff");
1803 p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
1804 p->loc_ScreenScaleRefractReflect = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
1805 p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
1806 p->loc_RefractColor = qglGetUniformLocationARB(p->program, "RefractColor");
1807 p->loc_ReflectColor = qglGetUniformLocationARB(p->program, "ReflectColor");
1808 p->loc_ReflectFactor = qglGetUniformLocationARB(p->program, "ReflectFactor");
1809 p->loc_ReflectOffset = qglGetUniformLocationARB(p->program, "ReflectOffset");
1810 p->loc_GammaCoeff = qglGetUniformLocationARB(p->program, "GammaCoeff");
1811 p->loc_UserVec1 = qglGetUniformLocationARB(p->program, "UserVec1");
1812 p->loc_UserVec2 = qglGetUniformLocationARB(p->program, "UserVec2");
1813 p->loc_UserVec3 = qglGetUniformLocationARB(p->program, "UserVec3");
1814 p->loc_UserVec4 = qglGetUniformLocationARB(p->program, "UserVec4");
1815 p->loc_ClientTime = qglGetUniformLocationARB(p->program, "ClientTime");
1816 p->loc_PixelSize = qglGetUniformLocationARB(p->program, "PixelSize");
1817 p->loc_Saturation = qglGetUniformLocationARB(p->program, "Saturation");
1818 p->loc_ShadowMap_TextureScale = qglGetUniformLocationARB(p->program, "ShadowMap_TextureScale");
1819 p->loc_ShadowMap_Parameters = qglGetUniformLocationARB(p->program, "ShadowMap_Parameters");
1820 // initialize the samplers to refer to the texture units we use
1821 if (p->loc_Texture_First >= 0) qglUniform1iARB(p->loc_Texture_First , GL20TU_FIRST);
1822 if (p->loc_Texture_Second >= 0) qglUniform1iARB(p->loc_Texture_Second , GL20TU_SECOND);
1823 if (p->loc_Texture_GammaRamps >= 0) qglUniform1iARB(p->loc_Texture_GammaRamps , GL20TU_GAMMARAMPS);
1824 if (p->loc_Texture_Normal >= 0) qglUniform1iARB(p->loc_Texture_Normal , GL20TU_NORMAL);
1825 if (p->loc_Texture_Color >= 0) qglUniform1iARB(p->loc_Texture_Color , GL20TU_COLOR);
1826 if (p->loc_Texture_Gloss >= 0) qglUniform1iARB(p->loc_Texture_Gloss , GL20TU_GLOSS);
1827 if (p->loc_Texture_Glow >= 0) qglUniform1iARB(p->loc_Texture_Glow , GL20TU_GLOW);
1828 if (p->loc_Texture_SecondaryNormal >= 0) qglUniform1iARB(p->loc_Texture_SecondaryNormal, GL20TU_SECONDARY_NORMAL);
1829 if (p->loc_Texture_SecondaryColor >= 0) qglUniform1iARB(p->loc_Texture_SecondaryColor , GL20TU_SECONDARY_COLOR);
1830 if (p->loc_Texture_SecondaryGloss >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGloss , GL20TU_SECONDARY_GLOSS);
1831 if (p->loc_Texture_SecondaryGlow >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGlow , GL20TU_SECONDARY_GLOW);
1832 if (p->loc_Texture_Pants >= 0) qglUniform1iARB(p->loc_Texture_Pants , GL20TU_PANTS);
1833 if (p->loc_Texture_Shirt >= 0) qglUniform1iARB(p->loc_Texture_Shirt , GL20TU_SHIRT);
1834 if (p->loc_Texture_FogMask >= 0) qglUniform1iARB(p->loc_Texture_FogMask , GL20TU_FOGMASK);
1835 if (p->loc_Texture_Lightmap >= 0) qglUniform1iARB(p->loc_Texture_Lightmap , GL20TU_LIGHTMAP);
1836 if (p->loc_Texture_Deluxemap >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap , GL20TU_DELUXEMAP);
1837 if (p->loc_Texture_Attenuation >= 0) qglUniform1iARB(p->loc_Texture_Attenuation , GL20TU_ATTENUATION);
1838 if (p->loc_Texture_Cube >= 0) qglUniform1iARB(p->loc_Texture_Cube , GL20TU_CUBE);
1839 if (p->loc_Texture_Refraction >= 0) qglUniform1iARB(p->loc_Texture_Refraction , GL20TU_REFRACTION);
1840 if (p->loc_Texture_Reflection >= 0) qglUniform1iARB(p->loc_Texture_Reflection , GL20TU_REFLECTION);
1841 if (p->loc_Texture_ShadowMapRect >= 0) qglUniform1iARB(p->loc_Texture_ShadowMapRect , GL20TU_SHADOWMAPRECT);
1842 if (p->loc_Texture_ShadowMapCube >= 0) qglUniform1iARB(p->loc_Texture_ShadowMapCube , GL20TU_SHADOWMAPCUBE);
1843 if (p->loc_Texture_ShadowMap2D >= 0) qglUniform1iARB(p->loc_Texture_ShadowMap2D , GL20TU_SHADOWMAP2D);
1844 if (p->loc_Texture_CubeProjection >= 0) qglUniform1iARB(p->loc_Texture_CubeProjection , GL20TU_CUBEPROJECTION);
1846 if (developer.integer)
1847 Con_Printf("GLSL shader %s compiled.\n", permutationname);
1850 Con_Printf("GLSL shader %s failed! some features may not work properly.\n", permutationname);
1854 Mem_Free(vertexstring);
1856 Mem_Free(geometrystring);
1858 Mem_Free(fragmentstring);
1861 void R_GLSL_Restart_f(void)
1863 unsigned int i, limit;
1864 r_glsl_permutation_t *p;
1865 limit = Mem_ExpandableArray_IndexRange(&r_glsl_permutationarray);
1866 for (i = 0;i < limit;i++)
1868 if ((p = (r_glsl_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_glsl_permutationarray, i)))
1870 GL_Backend_FreeProgram(p->program);
1871 Mem_ExpandableArray_FreeRecord(&r_glsl_permutationarray, (void*)p);
1874 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
1877 void R_GLSL_DumpShader_f(void)
1881 qfile_t *file = FS_OpenRealFile("glsl/default.glsl", "w", false);
1884 Con_Printf("failed to write to glsl/default.glsl\n");
1888 FS_Print(file, "/* The engine may define the following macros:\n");
1889 FS_Print(file, "#define VERTEX_SHADER\n#define GEOMETRY_SHADER\n#define FRAGMENT_SHADER\n");
1890 for (i = 0;i < SHADERMODE_COUNT;i++)
1891 FS_Print(file, shadermodeinfo[i].pretext);
1892 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1893 FS_Print(file, shaderpermutationinfo[i].pretext);
1894 FS_Print(file, "*/\n");
1895 FS_Print(file, builtinshaderstring);
1898 Con_Printf("glsl/default.glsl written\n");
1901 void R_SetupShader_SetPermutation(unsigned int mode, unsigned int permutation)
1903 r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
1904 if (r_glsl_permutation != perm)
1906 r_glsl_permutation = perm;
1907 if (!r_glsl_permutation->program)
1909 if (!r_glsl_permutation->compiled)
1910 R_GLSL_CompilePermutation(perm, mode, permutation);
1911 if (!r_glsl_permutation->program)
1913 // remove features until we find a valid permutation
1915 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1917 // reduce i more quickly whenever it would not remove any bits
1918 int j = 1<<(SHADERPERMUTATION_COUNT-1-i);
1919 if (!(permutation & j))
1922 r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
1923 if (!r_glsl_permutation->compiled)
1924 R_GLSL_CompilePermutation(perm, mode, permutation);
1925 if (r_glsl_permutation->program)
1928 if (i >= SHADERPERMUTATION_COUNT)
1930 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");
1931 Cvar_SetValueQuick(&r_glsl, 0);
1932 R_GLSL_Restart_f(); // unload shaders
1933 return; // no bit left to clear
1938 qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
1942 void R_SetupGenericShader(qboolean usetexture)
1944 if (gl_support_fragment_shader)
1946 if (r_glsl.integer && r_glsl_usegeneric.integer)
1947 R_SetupShader_SetPermutation(SHADERMODE_GENERIC, usetexture ? SHADERPERMUTATION_DIFFUSE : 0);
1948 else if (r_glsl_permutation)
1950 r_glsl_permutation = NULL;
1951 qglUseProgramObjectARB(0);CHECKGLERROR
1956 void R_SetupGenericTwoTextureShader(int texturemode)
1958 if (gl_support_fragment_shader)
1960 if (r_glsl.integer && r_glsl_usegeneric.integer)
1961 R_SetupShader_SetPermutation(SHADERMODE_GENERIC, SHADERPERMUTATION_DIFFUSE | SHADERPERMUTATION_SPECULAR | (r_shadow_glossexact.integer ? SHADERPERMUTATION_EXACTSPECULARMATH : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
1962 else if (r_glsl_permutation)
1964 r_glsl_permutation = NULL;
1965 qglUseProgramObjectARB(0);CHECKGLERROR
1968 if (!r_glsl_permutation)
1970 if (texturemode == GL_DECAL && gl_combine.integer)
1971 texturemode = GL_INTERPOLATE_ARB;
1972 R_Mesh_TexCombine(1, texturemode, texturemode, 1, 1);
1976 void R_SetupDepthOrShadowShader(void)
1978 if (gl_support_fragment_shader)
1980 if (r_glsl.integer && r_glsl_usegeneric.integer)
1981 R_SetupShader_SetPermutation(SHADERMODE_DEPTH_OR_SHADOW, 0);
1982 else if (r_glsl_permutation)
1984 r_glsl_permutation = NULL;
1985 qglUseProgramObjectARB(0);CHECKGLERROR
1990 void R_SetupShowDepthShader(void)
1992 if (gl_support_fragment_shader)
1994 if (r_glsl.integer && r_glsl_usegeneric.integer)
1995 R_SetupShader_SetPermutation(SHADERMODE_SHOWDEPTH, 0);
1996 else if (r_glsl_permutation)
1998 r_glsl_permutation = NULL;
1999 qglUseProgramObjectARB(0);CHECKGLERROR
2004 extern rtexture_t *r_shadow_attenuationgradienttexture;
2005 extern rtexture_t *r_shadow_attenuation2dtexture;
2006 extern rtexture_t *r_shadow_attenuation3dtexture;
2007 extern qboolean r_shadow_usingshadowmaprect;
2008 extern qboolean r_shadow_usingshadowmapcube;
2009 extern qboolean r_shadow_usingshadowmap2d;
2010 extern float r_shadow_shadowmap_texturescale[2];
2011 extern float r_shadow_shadowmap_parameters[4];
2012 extern qboolean r_shadow_shadowmapvsdct;
2013 extern qboolean r_shadow_shadowmapsampler;
2014 extern int r_shadow_shadowmappcf;
2015 void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, float ambientscale, float diffusescale, float specularscale, rsurfacepass_t rsurfacepass)
2017 // select a permutation of the lighting shader appropriate to this
2018 // combination of texture, entity, light source, and fogging, only use the
2019 // minimum features necessary to avoid wasting rendering time in the
2020 // fragment shader on features that are not being used
2021 unsigned int permutation = 0;
2022 unsigned int mode = 0;
2023 // TODO: implement geometry-shader based shadow volumes someday
2024 if (r_glsl_offsetmapping.integer)
2026 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
2027 if (r_glsl_offsetmapping_reliefmapping.integer)
2028 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
2030 if (rsurfacepass == RSURFPASS_BACKGROUND)
2032 // distorted background
2033 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
2034 mode = SHADERMODE_WATER;
2036 mode = SHADERMODE_REFRACTION;
2038 else if (rsurfacepass == RSURFPASS_RTLIGHT)
2041 mode = SHADERMODE_LIGHTSOURCE;
2042 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2043 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2044 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2045 permutation |= SHADERPERMUTATION_CUBEFILTER;
2046 if (diffusescale > 0)
2047 permutation |= SHADERPERMUTATION_DIFFUSE;
2048 if (specularscale > 0)
2049 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2050 if (r_refdef.fogenabled)
2051 permutation |= SHADERPERMUTATION_FOG;
2052 if (rsurface.texture->colormapping)
2053 permutation |= SHADERPERMUTATION_COLORMAPPING;
2054 if (r_shadow_usingshadowmaprect || r_shadow_usingshadowmap2d || r_shadow_usingshadowmapcube)
2056 if (r_shadow_usingshadowmaprect)
2057 permutation |= SHADERPERMUTATION_SHADOWMAPRECT;
2058 if (r_shadow_usingshadowmap2d)
2059 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2060 if (r_shadow_usingshadowmapcube)
2061 permutation |= SHADERPERMUTATION_SHADOWMAPCUBE;
2062 else if(r_shadow_shadowmapvsdct)
2063 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
2065 if (r_shadow_shadowmapsampler)
2066 permutation |= SHADERPERMUTATION_SHADOWSAMPLER;
2067 if (r_shadow_shadowmappcf > 1)
2068 permutation |= SHADERPERMUTATION_SHADOWMAPPCF2;
2069 else if (r_shadow_shadowmappcf)
2070 permutation |= SHADERPERMUTATION_SHADOWMAPPCF;
2073 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
2075 // unshaded geometry (fullbright or ambient model lighting)
2076 mode = SHADERMODE_FLATCOLOR;
2077 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2078 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2079 if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2080 permutation |= SHADERPERMUTATION_GLOW;
2081 if (r_refdef.fogenabled)
2082 permutation |= SHADERPERMUTATION_FOG;
2083 if (rsurface.texture->colormapping)
2084 permutation |= SHADERPERMUTATION_COLORMAPPING;
2085 if (r_glsl_offsetmapping.integer)
2087 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
2088 if (r_glsl_offsetmapping_reliefmapping.integer)
2089 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
2091 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2092 permutation |= SHADERPERMUTATION_REFLECTION;
2094 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT_DIRECTIONAL)
2096 // directional model lighting
2097 mode = SHADERMODE_LIGHTDIRECTION;
2098 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2099 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2100 if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2101 permutation |= SHADERPERMUTATION_GLOW;
2102 permutation |= SHADERPERMUTATION_DIFFUSE;
2103 if (specularscale > 0)
2104 permutation |= SHADERPERMUTATION_SPECULAR;
2105 if (r_refdef.fogenabled)
2106 permutation |= SHADERPERMUTATION_FOG;
2107 if (rsurface.texture->colormapping)
2108 permutation |= SHADERPERMUTATION_COLORMAPPING;
2109 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2110 permutation |= SHADERPERMUTATION_REFLECTION;
2112 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
2114 // ambient model lighting
2115 mode = SHADERMODE_LIGHTDIRECTION;
2116 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2117 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2118 if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2119 permutation |= SHADERPERMUTATION_GLOW;
2120 if (r_refdef.fogenabled)
2121 permutation |= SHADERPERMUTATION_FOG;
2122 if (rsurface.texture->colormapping)
2123 permutation |= SHADERPERMUTATION_COLORMAPPING;
2124 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2125 permutation |= SHADERPERMUTATION_REFLECTION;
2130 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping)
2132 // deluxemapping (light direction texture)
2133 if (rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping && r_refdef.scene.worldmodel->brushq3.deluxemapping_modelspace)
2134 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
2136 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
2137 permutation |= SHADERPERMUTATION_DIFFUSE;
2138 if (specularscale > 0)
2139 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2141 else if (r_glsl_deluxemapping.integer >= 2)
2143 // fake deluxemapping (uniform light direction in tangentspace)
2144 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
2145 permutation |= SHADERPERMUTATION_DIFFUSE;
2146 if (specularscale > 0)
2147 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2149 else if (rsurface.uselightmaptexture)
2151 // ordinary lightmapping (q1bsp, q3bsp)
2152 mode = SHADERMODE_LIGHTMAP;
2156 // ordinary vertex coloring (q3bsp)
2157 mode = SHADERMODE_VERTEXCOLOR;
2159 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2160 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2161 if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2162 permutation |= SHADERPERMUTATION_GLOW;
2163 if (r_refdef.fogenabled)
2164 permutation |= SHADERPERMUTATION_FOG;
2165 if (rsurface.texture->colormapping)
2166 permutation |= SHADERPERMUTATION_COLORMAPPING;
2167 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2168 permutation |= SHADERPERMUTATION_REFLECTION;
2170 if(permutation & SHADERPERMUTATION_SPECULAR)
2171 if(r_shadow_glossexact.integer)
2172 permutation |= SHADERPERMUTATION_EXACTSPECULARMATH;
2173 R_SetupShader_SetPermutation(mode, permutation);
2174 if (mode == SHADERMODE_LIGHTSOURCE)
2176 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
2177 if (permutation & SHADERPERMUTATION_DIFFUSE)
2179 if (r_glsl_permutation->loc_TintColor >= 0) qglUniform4fARB(r_glsl_permutation->loc_TintColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2], rsurface.texture->lightmapcolor[3]);
2180 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, ambientscale);
2181 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, diffusescale);
2182 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, specularscale);
2186 // ambient only is simpler
2187 if (r_glsl_permutation->loc_TintColor >= 0) qglUniform4fARB(r_glsl_permutation->loc_TintColor, lightcolorbase[0] * ambientscale, lightcolorbase[1] * ambientscale, lightcolorbase[2] * ambientscale, rsurface.texture->lightmapcolor[3]);
2188 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, 1);
2189 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, 0);
2190 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, 0);
2192 // additive passes are only darkened by fog, not tinted
2193 if (r_glsl_permutation->loc_FogColor >= 0)
2194 qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
2195 if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform2fARB(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
2196 if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4fARB(r_glsl_permutation->loc_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
2200 if (mode == SHADERMODE_LIGHTDIRECTION)
2202 if (r_glsl_permutation->loc_AmbientColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_AmbientColor , rsurface.modellight_ambient[0] * ambientscale * 0.5f, rsurface.modellight_ambient[1] * ambientscale * 0.5f, rsurface.modellight_ambient[2] * ambientscale * 0.5f);
2203 if (r_glsl_permutation->loc_DiffuseColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor , rsurface.modellight_diffuse[0] * diffusescale * 0.5f, rsurface.modellight_diffuse[1] * diffusescale * 0.5f, rsurface.modellight_diffuse[2] * diffusescale * 0.5f);
2204 if (r_glsl_permutation->loc_SpecularColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface.modellight_diffuse[0] * specularscale * 0.5f, rsurface.modellight_diffuse[1] * specularscale * 0.5f, rsurface.modellight_diffuse[2] * specularscale * 0.5f);
2205 if (r_glsl_permutation->loc_LightDir >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
2209 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, r_refdef.scene.ambient * 1.0f / 128.0f);
2210 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, r_refdef.lightmapintensity);
2211 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, r_refdef.lightmapintensity * specularscale);
2213 if (r_glsl_permutation->loc_TintColor >= 0) qglUniform4fARB(r_glsl_permutation->loc_TintColor, rsurface.texture->lightmapcolor[0], rsurface.texture->lightmapcolor[1], rsurface.texture->lightmapcolor[2], rsurface.texture->lightmapcolor[3]);
2214 if (r_glsl_permutation->loc_GlowColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_GlowColor, rsurface.glowmod[0] * r_hdr_glowintensity.value, rsurface.glowmod[1] * r_hdr_glowintensity.value, rsurface.glowmod[2] * r_hdr_glowintensity.value);
2215 // additive passes are only darkened by fog, not tinted
2216 if (r_glsl_permutation->loc_FogColor >= 0)
2218 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD)
2219 qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
2221 qglUniform3fARB(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
2223 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);
2224 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]);
2225 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]);
2226 if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_RefractColor, 1, rsurface.texture->refractcolor4f);
2227 if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_ReflectColor, 1, rsurface.texture->reflectcolor4f);
2228 if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
2229 if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectOffset, rsurface.texture->reflectmin);
2231 if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_refdef.view.colorscale);
2232 if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, rsurface.modelorg[0], rsurface.modelorg[1], rsurface.modelorg[2]);
2233 if (r_glsl_permutation->loc_Color_Pants >= 0)
2235 if (rsurface.texture->currentskinframe->pants)
2236 qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]);
2238 qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
2240 if (r_glsl_permutation->loc_Color_Shirt >= 0)
2242 if (rsurface.texture->currentskinframe->shirt)
2243 qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]);
2245 qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
2247 if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, r_refdef.fograngerecip * Matrix4x4_ScaleFromMatrix(&rsurface.matrix));
2248 if(permutation & SHADERPERMUTATION_EXACTSPECULARMATH)
2250 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower * 0.25);
2254 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower);
2256 if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
2260 #define SKINFRAME_HASH 1024
2264 int loadsequence; // incremented each level change
2265 memexpandablearray_t array;
2266 skinframe_t *hash[SKINFRAME_HASH];
2269 r_skinframe_t r_skinframe;
2271 void R_SkinFrame_PrepareForPurge(void)
2273 r_skinframe.loadsequence++;
2274 // wrap it without hitting zero
2275 if (r_skinframe.loadsequence >= 200)
2276 r_skinframe.loadsequence = 1;
2279 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
2283 // mark the skinframe as used for the purging code
2284 skinframe->loadsequence = r_skinframe.loadsequence;
2287 void R_SkinFrame_Purge(void)
2291 for (i = 0;i < SKINFRAME_HASH;i++)
2293 for (s = r_skinframe.hash[i];s;s = s->next)
2295 if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
2297 if (s->merged == s->base)
2299 // FIXME: maybe pass a pointer to the pointer to R_PurgeTexture and reset it to NULL inside? [11/29/2007 Black]
2300 R_PurgeTexture(s->stain );s->stain = NULL;
2301 R_PurgeTexture(s->merged);s->merged = NULL;
2302 R_PurgeTexture(s->base );s->base = NULL;
2303 R_PurgeTexture(s->pants );s->pants = NULL;
2304 R_PurgeTexture(s->shirt );s->shirt = NULL;
2305 R_PurgeTexture(s->nmap );s->nmap = NULL;
2306 R_PurgeTexture(s->gloss );s->gloss = NULL;
2307 R_PurgeTexture(s->glow );s->glow = NULL;
2308 R_PurgeTexture(s->fog );s->fog = NULL;
2309 s->loadsequence = 0;
2315 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
2317 char basename[MAX_QPATH];
2319 Image_StripImageExtension(name, basename, sizeof(basename));
2321 if( last == NULL ) {
2323 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2324 item = r_skinframe.hash[hashindex];
2329 // linearly search through the hash bucket
2330 for( ; item ; item = item->next ) {
2331 if( !strcmp( item->basename, basename ) ) {
2338 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qboolean add)
2342 char basename[MAX_QPATH];
2344 Image_StripImageExtension(name, basename, sizeof(basename));
2346 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2347 for (item = r_skinframe.hash[hashindex];item;item = item->next)
2348 if (!strcmp(item->basename, basename) && item->textureflags == textureflags && item->comparewidth == comparewidth && item->compareheight == compareheight && item->comparecrc == comparecrc)
2352 rtexture_t *dyntexture;
2353 // check whether its a dynamic texture
2354 dyntexture = CL_GetDynTexture( basename );
2355 if (!add && !dyntexture)
2357 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
2358 memset(item, 0, sizeof(*item));
2359 strlcpy(item->basename, basename, sizeof(item->basename));
2360 item->base = dyntexture; // either NULL or dyntexture handle
2361 item->textureflags = textureflags;
2362 item->comparewidth = comparewidth;
2363 item->compareheight = compareheight;
2364 item->comparecrc = comparecrc;
2365 item->next = r_skinframe.hash[hashindex];
2366 r_skinframe.hash[hashindex] = item;
2368 else if( item->base == NULL )
2370 rtexture_t *dyntexture;
2371 // check whether its a dynamic texture
2372 // 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]
2373 dyntexture = CL_GetDynTexture( basename );
2374 item->base = dyntexture; // either NULL or dyntexture handle
2377 R_SkinFrame_MarkUsed(item);
2381 #define R_SKINFRAME_LOAD_AVERAGE_COLORS(cnt, getpixel) \
2383 unsigned long long avgcolor[5], wsum; \
2391 for(pix = 0; pix < cnt; ++pix) \
2394 for(comp = 0; comp < 3; ++comp) \
2396 if(w) /* ignore perfectly black pixels because that is better for model skins */ \
2399 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2401 for(comp = 0; comp < 3; ++comp) \
2402 avgcolor[comp] += getpixel * w; \
2405 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2406 avgcolor[4] += getpixel; \
2408 if(avgcolor[3] == 0) /* no pixels seen? even worse */ \
2410 skinframe->avgcolor[0] = avgcolor[2] / (255.0 * avgcolor[3]); \
2411 skinframe->avgcolor[1] = avgcolor[1] / (255.0 * avgcolor[3]); \
2412 skinframe->avgcolor[2] = avgcolor[0] / (255.0 * avgcolor[3]); \
2413 skinframe->avgcolor[3] = avgcolor[4] / (255.0 * cnt); \
2416 skinframe_t *R_SkinFrame_LoadExternal_CheckAlpha(const char *name, int textureflags, qboolean complain, qboolean *has_alpha)
2418 // FIXME: it should be possible to disable loading various layers using
2419 // cvars, to prevent wasted loading time and memory usage if the user does
2421 qboolean loadnormalmap = true;
2422 qboolean loadgloss = true;
2423 qboolean loadpantsandshirt = true;
2424 qboolean loadglow = true;
2426 unsigned char *pixels;
2427 unsigned char *bumppixels;
2428 unsigned char *basepixels = NULL;
2429 int basepixels_width;
2430 int basepixels_height;
2431 skinframe_t *skinframe;
2435 if (cls.state == ca_dedicated)
2438 // return an existing skinframe if already loaded
2439 // if loading of the first image fails, don't make a new skinframe as it
2440 // would cause all future lookups of this to be missing
2441 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
2442 if (skinframe && skinframe->base)
2445 basepixels = loadimagepixelsbgra(name, complain, true);
2446 if (basepixels == NULL)
2449 if (developer_loading.integer)
2450 Con_Printf("loading skin \"%s\"\n", name);
2452 // we've got some pixels to store, so really allocate this new texture now
2454 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
2455 skinframe->stain = NULL;
2456 skinframe->merged = NULL;
2457 skinframe->base = r_texture_notexture;
2458 skinframe->pants = NULL;
2459 skinframe->shirt = NULL;
2460 skinframe->nmap = r_texture_blanknormalmap;
2461 skinframe->gloss = NULL;
2462 skinframe->glow = NULL;
2463 skinframe->fog = NULL;
2465 basepixels_width = image_width;
2466 basepixels_height = image_height;
2467 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);
2469 if (textureflags & TEXF_ALPHA)
2471 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
2472 if (basepixels[j] < 255)
2474 if (j < basepixels_width * basepixels_height * 4)
2476 // has transparent pixels
2478 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2479 for (j = 0;j < image_width * image_height * 4;j += 4)
2484 pixels[j+3] = basepixels[j+3];
2486 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);
2491 R_SKINFRAME_LOAD_AVERAGE_COLORS(basepixels_width * basepixels_height, basepixels[4 * pix + comp]);
2492 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2494 // _norm is the name used by tenebrae and has been adopted as standard
2497 if ((pixels = loadimagepixelsbgra(va("%s_norm", skinframe->basename), false, false)) != NULL)
2499 skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | skinframe->textureflags) & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
2503 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va("%s_bump", skinframe->basename), false, false)) != NULL)
2505 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2506 Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
2507 skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | skinframe->textureflags) & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
2509 Mem_Free(bumppixels);
2511 else if (r_shadow_bumpscale_basetexture.value > 0)
2513 pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
2514 Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
2515 skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), basepixels_width, basepixels_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | skinframe->textureflags) & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
2519 // _luma is supported for tenebrae compatibility
2520 // (I think it's a very stupid name, but oh well)
2521 // _glow is the preferred name
2522 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;}
2523 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;}
2524 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;}
2525 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;}
2528 Mem_Free(basepixels);
2533 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboolean complain)
2536 return R_SkinFrame_LoadExternal_CheckAlpha(name, textureflags, complain, &has_alpha);
2539 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)
2544 for (i = 0;i < width*height;i++)
2545 if (((unsigned char *)&palette[in[i]])[3] > 0)
2547 if (i == width*height)
2550 return R_LoadTexture2D (r_main_texturepool, name, width, height, in, TEXTYPE_PALETTE, textureflags, palette);
2553 // this is only used by .spr32 sprites, HL .spr files, HL .bsp files
2554 skinframe_t *R_SkinFrame_LoadInternalBGRA(const char *name, int textureflags, const unsigned char *skindata, int width, int height)
2557 unsigned char *temp1, *temp2;
2558 skinframe_t *skinframe;
2560 if (cls.state == ca_dedicated)
2563 // if already loaded just return it, otherwise make a new skinframe
2564 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height*4) : 0, true);
2565 if (skinframe && skinframe->base)
2568 skinframe->stain = NULL;
2569 skinframe->merged = NULL;
2570 skinframe->base = r_texture_notexture;
2571 skinframe->pants = NULL;
2572 skinframe->shirt = NULL;
2573 skinframe->nmap = r_texture_blanknormalmap;
2574 skinframe->gloss = NULL;
2575 skinframe->glow = NULL;
2576 skinframe->fog = NULL;
2578 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2582 if (developer_loading.integer)
2583 Con_Printf("loading 32bit skin \"%s\"\n", name);
2585 if (r_shadow_bumpscale_basetexture.value > 0)
2587 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2588 temp2 = temp1 + width * height * 4;
2589 Image_HeightmapToNormalmap_BGRA(skindata, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
2590 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
2593 skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_BGRA, skinframe->textureflags, NULL);
2594 if (textureflags & TEXF_ALPHA)
2596 for (i = 3;i < width * height * 4;i += 4)
2597 if (skindata[i] < 255)
2599 if (i < width * height * 4)
2601 unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
2602 memcpy(fogpixels, skindata, width * height * 4);
2603 for (i = 0;i < width * height * 4;i += 4)
2604 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
2605 skinframe->fog = R_LoadTexture2D(r_main_texturepool, va("%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, skinframe->textureflags, NULL);
2606 Mem_Free(fogpixels);
2610 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, skindata[4 * pix + comp]);
2611 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2616 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
2619 unsigned char *temp1, *temp2;
2620 unsigned int *palette;
2621 skinframe_t *skinframe;
2623 if (cls.state == ca_dedicated)
2626 // if already loaded just return it, otherwise make a new skinframe
2627 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
2628 if (skinframe && skinframe->base)
2631 palette = (loadglowtexture ? palette_bgra_nofullbrights : ((skinframe->textureflags & TEXF_ALPHA) ? palette_bgra_transparent : palette_bgra_complete));
2633 skinframe->stain = NULL;
2634 skinframe->merged = NULL;
2635 skinframe->base = r_texture_notexture;
2636 skinframe->pants = NULL;
2637 skinframe->shirt = NULL;
2638 skinframe->nmap = r_texture_blanknormalmap;
2639 skinframe->gloss = NULL;
2640 skinframe->glow = NULL;
2641 skinframe->fog = NULL;
2643 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2647 if (developer_loading.integer)
2648 Con_Printf("loading quake skin \"%s\"\n", name);
2650 if (r_shadow_bumpscale_basetexture.value > 0)
2652 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2653 temp2 = temp1 + width * height * 4;
2654 // use either a custom palette or the quake palette
2655 Image_Copy8bitBGRA(skindata, temp1, width * height, palette_bgra_complete);
2656 Image_HeightmapToNormalmap_BGRA(temp1, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
2657 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
2660 // use either a custom palette, or the quake palette
2661 skinframe->base = skinframe->merged = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_merged", skinframe->basename), palette, skinframe->textureflags, true); // all
2662 if (loadglowtexture)
2663 skinframe->glow = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_glow", skinframe->basename), palette_bgra_onlyfullbrights, skinframe->textureflags, false); // glow
2664 if (loadpantsandshirt)
2666 skinframe->pants = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_pants", skinframe->basename), palette_bgra_pantsaswhite, skinframe->textureflags, false); // pants
2667 skinframe->shirt = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_shirt", skinframe->basename), palette_bgra_shirtaswhite, skinframe->textureflags, false); // shirt
2669 if (skinframe->pants || skinframe->shirt)
2670 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
2671 if (textureflags & TEXF_ALPHA)
2673 for (i = 0;i < width * height;i++)
2674 if (((unsigned char *)palette_bgra_alpha)[skindata[i]*4+3] < 255)
2676 if (i < width * height)
2677 skinframe->fog = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_fog", skinframe->basename), palette_bgra_alpha, skinframe->textureflags, true); // fog mask
2680 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette)[skindata[pix]*4 + comp]);
2681 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2686 skinframe_t *R_SkinFrame_LoadMissing(void)
2688 skinframe_t *skinframe;
2690 if (cls.state == ca_dedicated)
2693 skinframe = R_SkinFrame_Find("missing", TEXF_PRECACHE | TEXF_FORCENEAREST, 0, 0, 0, true);
2694 skinframe->stain = NULL;
2695 skinframe->merged = NULL;
2696 skinframe->base = r_texture_notexture;
2697 skinframe->pants = NULL;
2698 skinframe->shirt = NULL;
2699 skinframe->nmap = r_texture_blanknormalmap;
2700 skinframe->gloss = NULL;
2701 skinframe->glow = NULL;
2702 skinframe->fog = NULL;
2704 skinframe->avgcolor[0] = rand() / RAND_MAX;
2705 skinframe->avgcolor[1] = rand() / RAND_MAX;
2706 skinframe->avgcolor[2] = rand() / RAND_MAX;
2707 skinframe->avgcolor[3] = 1;
2712 void gl_main_start(void)
2716 memset(r_queries, 0, sizeof(r_queries));
2718 memset(r_qwskincache, 0, sizeof(r_qwskincache));
2719 memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
2721 // set up r_skinframe loading system for textures
2722 memset(&r_skinframe, 0, sizeof(r_skinframe));
2723 r_skinframe.loadsequence = 1;
2724 Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
2726 r_main_texturepool = R_AllocTexturePool();
2727 R_BuildBlankTextures();
2729 if (gl_texturecubemap)
2732 R_BuildNormalizationCube();
2734 r_texture_fogattenuation = NULL;
2735 r_texture_gammaramps = NULL;
2736 //r_texture_fogintensity = NULL;
2737 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
2738 memset(&r_waterstate, 0, sizeof(r_waterstate));
2739 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
2740 Mem_ExpandableArray_NewArray(&r_glsl_permutationarray, r_main_mempool, sizeof(r_glsl_permutation_t), 256);
2741 memset(&r_svbsp, 0, sizeof (r_svbsp));
2743 r_refdef.fogmasktable_density = 0;
2746 extern rtexture_t *loadingscreentexture;
2747 void gl_main_shutdown(void)
2750 qglDeleteQueriesARB(r_maxqueries, r_queries);
2754 memset(r_queries, 0, sizeof(r_queries));
2756 memset(r_qwskincache, 0, sizeof(r_qwskincache));
2757 memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
2759 // clear out the r_skinframe state
2760 Mem_ExpandableArray_FreeArray(&r_skinframe.array);
2761 memset(&r_skinframe, 0, sizeof(r_skinframe));
2764 Mem_Free(r_svbsp.nodes);
2765 memset(&r_svbsp, 0, sizeof (r_svbsp));
2766 R_FreeTexturePool(&r_main_texturepool);
2767 loadingscreentexture = NULL;
2768 r_texture_blanknormalmap = NULL;
2769 r_texture_white = NULL;
2770 r_texture_grey128 = NULL;
2771 r_texture_black = NULL;
2772 r_texture_whitecube = NULL;
2773 r_texture_normalizationcube = NULL;
2774 r_texture_fogattenuation = NULL;
2775 r_texture_gammaramps = NULL;
2776 //r_texture_fogintensity = NULL;
2777 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
2778 memset(&r_waterstate, 0, sizeof(r_waterstate));
2782 extern void CL_ParseEntityLump(char *entitystring);
2783 void gl_main_newmap(void)
2785 // FIXME: move this code to client
2787 char *entities, entname[MAX_QPATH];
2790 strlcpy(entname, cl.worldmodel->name, sizeof(entname));
2791 l = (int)strlen(entname) - 4;
2792 if (l >= 0 && !strcmp(entname + l, ".bsp"))
2794 memcpy(entname + l, ".ent", 5);
2795 if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
2797 CL_ParseEntityLump(entities);
2802 if (cl.worldmodel->brush.entities)
2803 CL_ParseEntityLump(cl.worldmodel->brush.entities);
2807 void GL_Main_Init(void)
2809 r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
2811 Cmd_AddCommand("r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
2812 Cmd_AddCommand("r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
2813 // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
2814 if (gamemode == GAME_NEHAHRA)
2816 Cvar_RegisterVariable (&gl_fogenable);
2817 Cvar_RegisterVariable (&gl_fogdensity);
2818 Cvar_RegisterVariable (&gl_fogred);
2819 Cvar_RegisterVariable (&gl_foggreen);
2820 Cvar_RegisterVariable (&gl_fogblue);
2821 Cvar_RegisterVariable (&gl_fogstart);
2822 Cvar_RegisterVariable (&gl_fogend);
2823 Cvar_RegisterVariable (&gl_skyclip);
2825 Cvar_RegisterVariable(&r_motionblur);
2826 Cvar_RegisterVariable(&r_motionblur_maxblur);
2827 Cvar_RegisterVariable(&r_motionblur_bmin);
2828 Cvar_RegisterVariable(&r_motionblur_vmin);
2829 Cvar_RegisterVariable(&r_motionblur_vmax);
2830 Cvar_RegisterVariable(&r_motionblur_vcoeff);
2831 Cvar_RegisterVariable(&r_motionblur_randomize);
2832 Cvar_RegisterVariable(&r_damageblur);
2833 Cvar_RegisterVariable(&r_animcache);
2834 Cvar_RegisterVariable(&r_depthfirst);
2835 Cvar_RegisterVariable(&r_useinfinitefarclip);
2836 Cvar_RegisterVariable(&r_nearclip);
2837 Cvar_RegisterVariable(&r_showbboxes);
2838 Cvar_RegisterVariable(&r_showsurfaces);
2839 Cvar_RegisterVariable(&r_showtris);
2840 Cvar_RegisterVariable(&r_shownormals);
2841 Cvar_RegisterVariable(&r_showlighting);
2842 Cvar_RegisterVariable(&r_showshadowvolumes);
2843 Cvar_RegisterVariable(&r_showcollisionbrushes);
2844 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
2845 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
2846 Cvar_RegisterVariable(&r_showdisabledepthtest);
2847 Cvar_RegisterVariable(&r_drawportals);
2848 Cvar_RegisterVariable(&r_drawentities);
2849 Cvar_RegisterVariable(&r_cullentities_trace);
2850 Cvar_RegisterVariable(&r_cullentities_trace_samples);
2851 Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
2852 Cvar_RegisterVariable(&r_cullentities_trace_delay);
2853 Cvar_RegisterVariable(&r_drawviewmodel);
2854 Cvar_RegisterVariable(&r_speeds);
2855 Cvar_RegisterVariable(&r_fullbrights);
2856 Cvar_RegisterVariable(&r_wateralpha);
2857 Cvar_RegisterVariable(&r_dynamic);
2858 Cvar_RegisterVariable(&r_fullbright);
2859 Cvar_RegisterVariable(&r_shadows);
2860 Cvar_RegisterVariable(&r_shadows_darken);
2861 Cvar_RegisterVariable(&r_shadows_drawafterrtlighting);
2862 Cvar_RegisterVariable(&r_shadows_castfrombmodels);
2863 Cvar_RegisterVariable(&r_shadows_throwdistance);
2864 Cvar_RegisterVariable(&r_shadows_throwdirection);
2865 Cvar_RegisterVariable(&r_q1bsp_skymasking);
2866 Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
2867 Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
2868 Cvar_RegisterVariable(&r_fog_exp2);
2869 Cvar_RegisterVariable(&r_drawfog);
2870 Cvar_RegisterVariable(&r_textureunits);
2871 Cvar_RegisterVariable(&r_glsl);
2872 Cvar_RegisterVariable(&r_glsl_deluxemapping);
2873 Cvar_RegisterVariable(&r_glsl_offsetmapping);
2874 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
2875 Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
2876 Cvar_RegisterVariable(&r_glsl_postprocess);
2877 Cvar_RegisterVariable(&r_glsl_postprocess_uservec1);
2878 Cvar_RegisterVariable(&r_glsl_postprocess_uservec2);
2879 Cvar_RegisterVariable(&r_glsl_postprocess_uservec3);
2880 Cvar_RegisterVariable(&r_glsl_postprocess_uservec4);
2881 Cvar_RegisterVariable(&r_glsl_usegeneric);
2882 Cvar_RegisterVariable(&r_water);
2883 Cvar_RegisterVariable(&r_water_resolutionmultiplier);
2884 Cvar_RegisterVariable(&r_water_clippingplanebias);
2885 Cvar_RegisterVariable(&r_water_refractdistort);
2886 Cvar_RegisterVariable(&r_water_reflectdistort);
2887 Cvar_RegisterVariable(&r_lerpsprites);
2888 Cvar_RegisterVariable(&r_lerpmodels);
2889 Cvar_RegisterVariable(&r_lerplightstyles);
2890 Cvar_RegisterVariable(&r_waterscroll);
2891 Cvar_RegisterVariable(&r_bloom);
2892 Cvar_RegisterVariable(&r_bloom_colorscale);
2893 Cvar_RegisterVariable(&r_bloom_brighten);
2894 Cvar_RegisterVariable(&r_bloom_blur);
2895 Cvar_RegisterVariable(&r_bloom_resolution);
2896 Cvar_RegisterVariable(&r_bloom_colorexponent);
2897 Cvar_RegisterVariable(&r_bloom_colorsubtract);
2898 Cvar_RegisterVariable(&r_hdr);
2899 Cvar_RegisterVariable(&r_hdr_scenebrightness);
2900 Cvar_RegisterVariable(&r_hdr_glowintensity);
2901 Cvar_RegisterVariable(&r_hdr_range);
2902 Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
2903 Cvar_RegisterVariable(&developer_texturelogging);
2904 Cvar_RegisterVariable(&gl_lightmaps);
2905 Cvar_RegisterVariable(&r_test);
2906 Cvar_RegisterVariable(&r_batchmode);
2907 Cvar_RegisterVariable(&r_glsl_saturation);
2908 if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
2909 Cvar_SetValue("r_fullbrights", 0);
2910 R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
2912 Cvar_RegisterVariable(&r_track_sprites);
2913 Cvar_RegisterVariable(&r_track_sprites_flags);
2914 Cvar_RegisterVariable(&r_track_sprites_scalew);
2915 Cvar_RegisterVariable(&r_track_sprites_scaleh);
2918 extern void R_Textures_Init(void);
2919 extern void GL_Draw_Init(void);
2920 extern void GL_Main_Init(void);
2921 extern void R_Shadow_Init(void);
2922 extern void R_Sky_Init(void);
2923 extern void GL_Surf_Init(void);
2924 extern void R_Particles_Init(void);
2925 extern void R_Explosion_Init(void);
2926 extern void gl_backend_init(void);
2927 extern void Sbar_Init(void);
2928 extern void R_LightningBeams_Init(void);
2929 extern void Mod_RenderInit(void);
2931 void Render_Init(void)
2943 R_LightningBeams_Init();
2952 extern char *ENGINE_EXTENSIONS;
2955 gl_renderer = (const char *)qglGetString(GL_RENDERER);
2956 gl_vendor = (const char *)qglGetString(GL_VENDOR);
2957 gl_version = (const char *)qglGetString(GL_VERSION);
2958 gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
2962 if (!gl_platformextensions)
2963 gl_platformextensions = "";
2965 Con_Printf("GL_VENDOR: %s\n", gl_vendor);
2966 Con_Printf("GL_RENDERER: %s\n", gl_renderer);
2967 Con_Printf("GL_VERSION: %s\n", gl_version);
2968 Con_DPrintf("GL_EXTENSIONS: %s\n", gl_extensions);
2969 Con_DPrintf("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
2971 VID_CheckExtensions();
2973 // LordHavoc: report supported extensions
2974 Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
2976 // clear to black (loading plaque will be seen over this)
2978 qglClearColor(0,0,0,1);CHECKGLERROR
2979 qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
2982 int R_CullBox(const vec3_t mins, const vec3_t maxs)
2986 for (i = 0;i < r_refdef.view.numfrustumplanes;i++)
2988 // skip nearclip plane, it often culls portals when you are very close, and is almost never useful
2991 p = r_refdef.view.frustum + i;
2996 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
3000 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
3004 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
3008 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
3012 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
3016 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
3020 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
3024 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
3032 int R_CullBoxCustomPlanes(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
3036 for (i = 0;i < numplanes;i++)
3043 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
3047 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
3051 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
3055 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
3059 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
3063 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
3067 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
3071 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
3079 //==================================================================================
3081 // LordHavoc: animcache written by Echon, refactored and reformatted by me
3084 * Animation cache helps save re-animating a player mesh if it's re-rendered again in a given frame
3085 * (reflections, lighting, etc). All animation cache becomes invalid on the next frame and is flushed
3086 * (well, over-wrote). The memory for each cache is kept around to save on allocation thrashing.
3089 typedef struct r_animcache_entity_s
3096 qboolean wantnormals;
3097 qboolean wanttangents;
3099 r_animcache_entity_t;
3101 typedef struct r_animcache_s
3103 r_animcache_entity_t entity[MAX_EDICTS*2];
3109 static r_animcache_t r_animcachestate;
3111 void R_AnimCache_Free(void)
3114 for (idx=0 ; idx<r_animcachestate.maxindex ; idx++)
3116 r_animcachestate.entity[idx].maxvertices = 0;
3117 Mem_Free(r_animcachestate.entity[idx].vertex3f);
3118 r_animcachestate.entity[idx].vertex3f = NULL;
3119 r_animcachestate.entity[idx].normal3f = NULL;
3120 r_animcachestate.entity[idx].svector3f = NULL;
3121 r_animcachestate.entity[idx].tvector3f = NULL;
3123 r_animcachestate.currentindex = 0;
3124 r_animcachestate.maxindex = 0;
3127 void R_AnimCache_ResizeEntityCache(const int cacheIdx, const int numvertices)
3131 r_animcache_entity_t *cache = &r_animcachestate.entity[cacheIdx];
3133 if (cache->maxvertices >= numvertices)
3136 // Release existing memory
3137 if (cache->vertex3f)
3138 Mem_Free(cache->vertex3f);
3140 // Pad by 1024 verts
3141 cache->maxvertices = (numvertices + 1023) & ~1023;
3142 arraySize = cache->maxvertices * 3;
3144 // Allocate, even if we don't need this memory in this instance it will get ignored and potentially used later
3145 base = (float *)Mem_Alloc(r_main_mempool, arraySize * sizeof(float) * 4);
3146 r_animcachestate.entity[cacheIdx].vertex3f = base;
3147 r_animcachestate.entity[cacheIdx].normal3f = base + arraySize;
3148 r_animcachestate.entity[cacheIdx].svector3f = base + arraySize*2;
3149 r_animcachestate.entity[cacheIdx].tvector3f = base + arraySize*3;
3151 // Con_Printf("allocated cache for %i (%f KB)\n", cacheIdx, (arraySize*sizeof(float)*4)/1024.0f);
3154 void R_AnimCache_NewFrame(void)
3158 if (r_animcache.integer && r_drawentities.integer)
3159 r_animcachestate.maxindex = sizeof(r_animcachestate.entity) / sizeof(r_animcachestate.entity[0]);
3160 else if (r_animcachestate.maxindex)
3163 r_animcachestate.currentindex = 0;
3165 for (i = 0;i < r_refdef.scene.numentities;i++)
3166 r_refdef.scene.entities[i]->animcacheindex = -1;
3169 qboolean R_AnimCache_GetEntity(entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
3171 dp_model_t *model = ent->model;
3172 r_animcache_entity_t *c;
3173 // see if it's already cached this frame
3174 if (ent->animcacheindex >= 0)
3176 // add normals/tangents if needed
3177 c = r_animcachestate.entity + ent->animcacheindex;
3179 wantnormals = false;
3180 if (c->wanttangents)
3181 wanttangents = false;
3182 if (wantnormals || wanttangents)
3183 model->AnimateVertices(model, ent->frameblend, NULL, wantnormals ? c->normal3f : NULL, wanttangents ? c->svector3f : NULL, wanttangents ? c->tvector3f : NULL);
3187 // see if this ent is worth caching
3188 if (r_animcachestate.maxindex <= r_animcachestate.currentindex)
3190 if (!model || !model->Draw || !model->surfmesh.isanimated || !model->AnimateVertices || (ent->frameblend[0].lerp == 1 && ent->frameblend[0].subframe == 0))
3192 // assign it a cache entry and make sure the arrays are big enough
3193 R_AnimCache_ResizeEntityCache(r_animcachestate.currentindex, model->surfmesh.num_vertices);
3194 ent->animcacheindex = r_animcachestate.currentindex++;
3195 c = r_animcachestate.entity + ent->animcacheindex;
3196 c->wantnormals = wantnormals;
3197 c->wanttangents = wanttangents;
3198 model->AnimateVertices(model, ent->frameblend, c->vertex3f, wantnormals ? c->normal3f : NULL, wanttangents ? c->svector3f : NULL, wanttangents ? c->tvector3f : NULL);
3203 void R_AnimCache_CacheVisibleEntities(void)
3206 qboolean wantnormals;
3207 qboolean wanttangents;
3209 if (!r_animcachestate.maxindex)
3212 wantnormals = !r_showsurfaces.integer;
3213 wanttangents = !r_showsurfaces.integer && (r_glsl.integer || r_refdef.scene.rtworld || r_refdef.scene.rtdlight);
3215 // TODO: thread this?
3217 for (i = 0;i < r_refdef.scene.numentities;i++)
3219 if (!r_refdef.viewcache.entityvisible[i])
3221 R_AnimCache_GetEntity(r_refdef.scene.entities[i], wantnormals, wanttangents);
3225 //==================================================================================
3227 static void R_View_UpdateEntityLighting (void)
3230 entity_render_t *ent;
3231 vec3_t tempdiffusenormal;
3233 for (i = 0;i < r_refdef.scene.numentities;i++)
3235 ent = r_refdef.scene.entities[i];
3237 // skip unseen models
3238 if (!r_refdef.viewcache.entityvisible[i] && r_shadows.integer != 1)
3242 if (ent->model && ent->model->brush.num_leafs)
3244 // TODO: use modellight for r_ambient settings on world?
3245 VectorSet(ent->modellight_ambient, 0, 0, 0);
3246 VectorSet(ent->modellight_diffuse, 0, 0, 0);
3247 VectorSet(ent->modellight_lightdir, 0, 0, 1);
3251 // fetch the lighting from the worldmodel data
3252 VectorSet(ent->modellight_ambient, r_refdef.scene.ambient * (2.0f / 128.0f), r_refdef.scene.ambient * (2.0f / 128.0f), r_refdef.scene.ambient * (2.0f / 128.0f));
3253 VectorClear(ent->modellight_diffuse);
3254 VectorClear(tempdiffusenormal);
3255 if ((ent->flags & RENDER_LIGHT) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.LightPoint)
3258 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3259 r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, org, ent->modellight_ambient, ent->modellight_diffuse, tempdiffusenormal);
3262 VectorSet(ent->modellight_ambient, 1, 1, 1);
3264 // move the light direction into modelspace coordinates for lighting code
3265 Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, ent->modellight_lightdir);
3266 if(VectorLength2(ent->modellight_lightdir) == 0)
3267 VectorSet(ent->modellight_lightdir, 0, 0, 1); // have to set SOME valid vector here
3268 VectorNormalize(ent->modellight_lightdir);
3272 static void R_View_UpdateEntityVisible (void)
3275 entity_render_t *ent;
3277 if (!r_drawentities.integer)
3280 renderimask = r_refdef.envmap ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL) : ((chase_active.integer || r_waterstate.renderingscene) ? RENDER_VIEWMODEL : RENDER_EXTERIORMODEL);
3281 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs)
3283 // worldmodel can check visibility
3284 memset(r_refdef.viewcache.entityvisible, 0, r_refdef.scene.numentities);
3285 for (i = 0;i < r_refdef.scene.numentities;i++)
3287 ent = r_refdef.scene.entities[i];
3288 if (!(ent->flags & renderimask))
3289 if (!R_CullBox(ent->mins, ent->maxs) || (ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)))
3290 if ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, ent->mins, ent->maxs))
3291 r_refdef.viewcache.entityvisible[i] = true;
3293 if(r_cullentities_trace.integer && r_refdef.scene.worldmodel->brush.TraceLineOfSight)
3295 for (i = 0;i < r_refdef.scene.numentities;i++)
3297 ent = r_refdef.scene.entities[i];
3298 if(r_refdef.viewcache.entityvisible[i] && !(ent->effects & EF_NODEPTHTEST) && !(ent->flags & (RENDER_VIEWMODEL + RENDER_NOCULL)) && !(ent->model && (ent->model->name[0] == '*')))
3300 if(Mod_CanSeeBox_Trace(r_cullentities_trace_samples.integer, r_cullentities_trace_enlarge.value, r_refdef.scene.worldmodel, r_refdef.view.origin, ent->mins, ent->maxs))
3301 ent->last_trace_visibility = realtime;
3302 if(ent->last_trace_visibility < realtime - r_cullentities_trace_delay.value)
3303 r_refdef.viewcache.entityvisible[i] = 0;
3310 // no worldmodel or it can't check visibility
3311 for (i = 0;i < r_refdef.scene.numentities;i++)
3313 ent = r_refdef.scene.entities[i];
3314 r_refdef.viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model && 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));
3319 /// only used if skyrendermasked, and normally returns false
3320 int R_DrawBrushModelsSky (void)
3323 entity_render_t *ent;
3325 if (!r_drawentities.integer)
3329 for (i = 0;i < r_refdef.scene.numentities;i++)
3331 if (!r_refdef.viewcache.entityvisible[i])
3333 ent = r_refdef.scene.entities[i];
3334 if (!ent->model || !ent->model->DrawSky)
3336 ent->model->DrawSky(ent);
3342 static void R_DrawNoModel(entity_render_t *ent);
3343 static void R_DrawModels(void)
3346 entity_render_t *ent;
3348 if (!r_drawentities.integer)
3351 for (i = 0;i < r_refdef.scene.numentities;i++)
3353 if (!r_refdef.viewcache.entityvisible[i])
3355 ent = r_refdef.scene.entities[i];
3356 r_refdef.stats.entities++;
3357 if (ent->model && ent->model->Draw != NULL)
3358 ent->model->Draw(ent);
3364 static void R_DrawModelsDepth(void)
3367 entity_render_t *ent;
3369 if (!r_drawentities.integer)
3372 for (i = 0;i < r_refdef.scene.numentities;i++)
3374 if (!r_refdef.viewcache.entityvisible[i])
3376 ent = r_refdef.scene.entities[i];
3377 if (ent->model && ent->model->DrawDepth != NULL)
3378 ent->model->DrawDepth(ent);
3382 static void R_DrawModelsDebug(void)
3385 entity_render_t *ent;
3387 if (!r_drawentities.integer)
3390 for (i = 0;i < r_refdef.scene.numentities;i++)
3392 if (!r_refdef.viewcache.entityvisible[i])
3394 ent = r_refdef.scene.entities[i];
3395 if (ent->model && ent->model->DrawDebug != NULL)
3396 ent->model->DrawDebug(ent);
3400 static void R_DrawModelsAddWaterPlanes(void)
3403 entity_render_t *ent;
3405 if (!r_drawentities.integer)
3408 for (i = 0;i < r_refdef.scene.numentities;i++)
3410 if (!r_refdef.viewcache.entityvisible[i])
3412 ent = r_refdef.scene.entities[i];
3413 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
3414 ent->model->DrawAddWaterPlanes(ent);
3418 static void R_View_SetFrustum(void)
3421 double slopex, slopey;
3422 vec3_t forward, left, up, origin;
3424 // we can't trust r_refdef.view.forward and friends in reflected scenes
3425 Matrix4x4_ToVectors(&r_refdef.view.matrix, forward, left, up, origin);
3428 r_refdef.view.frustum[0].normal[0] = 0 - 1.0 / r_refdef.view.frustum_x;
3429 r_refdef.view.frustum[0].normal[1] = 0 - 0;
3430 r_refdef.view.frustum[0].normal[2] = -1 - 0;
3431 r_refdef.view.frustum[1].normal[0] = 0 + 1.0 / r_refdef.view.frustum_x;
3432 r_refdef.view.frustum[1].normal[1] = 0 + 0;
3433 r_refdef.view.frustum[1].normal[2] = -1 + 0;
3434 r_refdef.view.frustum[2].normal[0] = 0 - 0;
3435 r_refdef.view.frustum[2].normal[1] = 0 - 1.0 / r_refdef.view.frustum_y;
3436 r_refdef.view.frustum[2].normal[2] = -1 - 0;
3437 r_refdef.view.frustum[3].normal[0] = 0 + 0;
3438 r_refdef.view.frustum[3].normal[1] = 0 + 1.0 / r_refdef.view.frustum_y;
3439 r_refdef.view.frustum[3].normal[2] = -1 + 0;
3443 zNear = r_refdef.nearclip;
3444 nudge = 1.0 - 1.0 / (1<<23);
3445 r_refdef.view.frustum[4].normal[0] = 0 - 0;
3446 r_refdef.view.frustum[4].normal[1] = 0 - 0;
3447 r_refdef.view.frustum[4].normal[2] = -1 - -nudge;
3448 r_refdef.view.frustum[4].dist = 0 - -2 * zNear * nudge;
3449 r_refdef.view.frustum[5].normal[0] = 0 + 0;
3450 r_refdef.view.frustum[5].normal[1] = 0 + 0;
3451 r_refdef.view.frustum[5].normal[2] = -1 + -nudge;
3452 r_refdef.view.frustum[5].dist = 0 + -2 * zNear * nudge;
3458 r_refdef.view.frustum[0].normal[0] = m[3] - m[0];
3459 r_refdef.view.frustum[0].normal[1] = m[7] - m[4];
3460 r_refdef.view.frustum[0].normal[2] = m[11] - m[8];
3461 r_refdef.view.frustum[0].dist = m[15] - m[12];
3463 r_refdef.view.frustum[1].normal[0] = m[3] + m[0];
3464 r_refdef.view.frustum[1].normal[1] = m[7] + m[4];
3465 r_refdef.view.frustum[1].normal[2] = m[11] + m[8];
3466 r_refdef.view.frustum[1].dist = m[15] + m[12];
3468 r_refdef.view.frustum[2].normal[0] = m[3] - m[1];
3469 r_refdef.view.frustum[2].normal[1] = m[7] - m[5];
3470 r_refdef.view.frustum[2].normal[2] = m[11] - m[9];
3471 r_refdef.view.frustum[2].dist = m[15] - m[13];
3473 r_refdef.view.frustum[3].normal[0] = m[3] + m[1];
3474 r_refdef.view.frustum[3].normal[1] = m[7] + m[5];
3475 r_refdef.view.frustum[3].normal[2] = m[11] + m[9];
3476 r_refdef.view.frustum[3].dist = m[15] + m[13];
3478 r_refdef.view.frustum[4].normal[0] = m[3] - m[2];
3479 r_refdef.view.frustum[4].normal[1] = m[7] - m[6];
3480 r_refdef.view.frustum[4].normal[2] = m[11] - m[10];
3481 r_refdef.view.frustum[4].dist = m[15] - m[14];
3483 r_refdef.view.frustum[5].normal[0] = m[3] + m[2];
3484 r_refdef.view.frustum[5].normal[1] = m[7] + m[6];
3485 r_refdef.view.frustum[5].normal[2] = m[11] + m[10];
3486 r_refdef.view.frustum[5].dist = m[15] + m[14];
3489 if (r_refdef.view.useperspective)
3491 slopex = 1.0 / r_refdef.view.frustum_x;
3492 slopey = 1.0 / r_refdef.view.frustum_y;
3493 VectorMA(forward, -slopex, left, r_refdef.view.frustum[0].normal);
3494 VectorMA(forward, slopex, left, r_refdef.view.frustum[1].normal);
3495 VectorMA(forward, -slopey, up , r_refdef.view.frustum[2].normal);
3496 VectorMA(forward, slopey, up , r_refdef.view.frustum[3].normal);
3497 VectorCopy(forward, r_refdef.view.frustum[4].normal);
3499 // Leaving those out was a mistake, those were in the old code, and they
3500 // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
3501 // I couldn't reproduce it after adding those normalizations. --blub
3502 VectorNormalize(r_refdef.view.frustum[0].normal);
3503 VectorNormalize(r_refdef.view.frustum[1].normal);
3504 VectorNormalize(r_refdef.view.frustum[2].normal);
3505 VectorNormalize(r_refdef.view.frustum[3].normal);
3507 // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
3508 VectorMAMAMAM(1, r_refdef.view.origin, 1024, forward, -1024 * r_refdef.view.frustum_x, left, -1024 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[0]);
3509 VectorMAMAMAM(1, r_refdef.view.origin, 1024, forward, 1024 * r_refdef.view.frustum_x, left, -1024 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[1]);
3510 VectorMAMAMAM(1, r_refdef.view.origin, 1024, forward, -1024 * r_refdef.view.frustum_x, left, 1024 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[2]);
3511 VectorMAMAMAM(1, r_refdef.view.origin, 1024, forward, 1024 * r_refdef.view.frustum_x, left, 1024 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[3]);
3513 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal);
3514 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal);
3515 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal);
3516 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal);
3517 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) + r_refdef.nearclip;
3521 VectorScale(left, -r_refdef.view.ortho_x, r_refdef.view.frustum[0].normal);
3522 VectorScale(left, r_refdef.view.ortho_x, r_refdef.view.frustum[1].normal);
3523 VectorScale(up, -r_refdef.view.ortho_y, r_refdef.view.frustum[2].normal);
3524 VectorScale(up, r_refdef.view.ortho_y, r_refdef.view.frustum[3].normal);
3525 VectorCopy(forward, r_refdef.view.frustum[4].normal);
3526 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal) + r_refdef.view.ortho_x;
3527 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal) + r_refdef.view.ortho_x;
3528 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal) + r_refdef.view.ortho_y;
3529 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal) + r_refdef.view.ortho_y;
3530 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) + r_refdef.nearclip;
3532 r_refdef.view.numfrustumplanes = 5;
3534 if (r_refdef.view.useclipplane)
3536 r_refdef.view.numfrustumplanes = 6;
3537 r_refdef.view.frustum[5] = r_refdef.view.clipplane;
3540 for (i = 0;i < r_refdef.view.numfrustumplanes;i++)
3541 PlaneClassify(r_refdef.view.frustum + i);
3543 // LordHavoc: note to all quake engine coders, Quake had a special case
3544 // for 90 degrees which assumed a square view (wrong), so I removed it,
3545 // Quake2 has it disabled as well.
3547 // rotate R_VIEWFORWARD right by FOV_X/2 degrees
3548 //RotatePointAroundVector( r_refdef.view.frustum[0].normal, up, forward, -(90 - r_refdef.fov_x / 2));
3549 //r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, frustum[0].normal);
3550 //PlaneClassify(&frustum[0]);
3552 // rotate R_VIEWFORWARD left by FOV_X/2 degrees
3553 //RotatePointAroundVector( r_refdef.view.frustum[1].normal, up, forward, (90 - r_refdef.fov_x / 2));
3554 //r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, frustum[1].normal);
3555 //PlaneClassify(&frustum[1]);
3557 // rotate R_VIEWFORWARD up by FOV_X/2 degrees
3558 //RotatePointAroundVector( r_refdef.view.frustum[2].normal, left, forward, -(90 - r_refdef.fov_y / 2));
3559 //r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, frustum[2].normal);
3560 //PlaneClassify(&frustum[2]);
3562 // rotate R_VIEWFORWARD down by FOV_X/2 degrees
3563 //RotatePointAroundVector( r_refdef.view.frustum[3].normal, left, forward, (90 - r_refdef.fov_y / 2));
3564 //r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, frustum[3].normal);
3565 //PlaneClassify(&frustum[3]);
3568 //VectorCopy(forward, r_refdef.view.frustum[4].normal);
3569 //r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, frustum[4].normal) + r_nearclip.value;
3570 //PlaneClassify(&frustum[4]);
3573 void R_View_Update(void)
3575 R_View_SetFrustum();
3576 R_View_WorldVisibility(r_refdef.view.useclipplane);
3577 R_View_UpdateEntityVisible();
3578 R_View_UpdateEntityLighting();
3581 void R_SetupView(qboolean allowwaterclippingplane)
3583 const double *customclipplane = NULL;
3585 if (r_refdef.view.useclipplane && allowwaterclippingplane)
3587 // LordHavoc: couldn't figure out how to make this approach the
3588 vec_t dist = r_refdef.view.clipplane.dist - r_water_clippingplanebias.value;
3589 vec_t viewdist = DotProduct(r_refdef.view.origin, r_refdef.view.clipplane.normal);
3590 if (viewdist < r_refdef.view.clipplane.dist + r_water_clippingplanebias.value)
3591 dist = r_refdef.view.clipplane.dist;
3592 plane[0] = r_refdef.view.clipplane.normal[0];
3593 plane[1] = r_refdef.view.clipplane.normal[1];
3594 plane[2] = r_refdef.view.clipplane.normal[2];
3596 customclipplane = plane;
3599 if (!r_refdef.view.useperspective)
3600 R_Viewport_InitOrtho(&r_refdef.view.viewport, &r_refdef.view.matrix, r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height, -r_refdef.view.ortho_x, -r_refdef.view.ortho_y, r_refdef.view.ortho_x, r_refdef.view.ortho_y, -r_refdef.farclip, r_refdef.farclip, customclipplane);
3601 else if (gl_stencil && r_useinfinitefarclip.integer)
3602 R_Viewport_InitPerspectiveInfinite(&r_refdef.view.viewport, &r_refdef.view.matrix, r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, customclipplane);
3604 R_Viewport_InitPerspective(&r_refdef.view.viewport, &r_refdef.view.matrix, r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, r_refdef.farclip, customclipplane);
3605 R_SetViewport(&r_refdef.view.viewport);
3608 void R_ResetViewRendering2D(void)
3610 r_viewport_t viewport;
3613 // GL is weird because it's bottom to top, r_refdef.view.y is top to bottom
3614 R_Viewport_InitOrtho(&viewport, &identitymatrix, r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height, 0, 0, 1, 1, -10, 100, NULL);
3615 R_SetViewport(&viewport);
3616 GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
3617 GL_Color(1, 1, 1, 1);
3618 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
3619 GL_BlendFunc(GL_ONE, GL_ZERO);
3620 GL_AlphaTest(false);
3621 GL_ScissorTest(false);
3622 GL_DepthMask(false);
3623 GL_DepthRange(0, 1);
3624 GL_DepthTest(false);
3625 R_Mesh_Matrix(&identitymatrix);
3626 R_Mesh_ResetTextureState();
3627 GL_PolygonOffset(0, 0);
3628 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
3629 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
3630 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
3631 qglStencilMask(~0);CHECKGLERROR
3632 qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
3633 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
3634 GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
3635 R_SetupGenericShader(true);
3638 void R_ResetViewRendering3D(void)
3643 GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
3644 GL_Color(1, 1, 1, 1);
3645 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
3646 GL_BlendFunc(GL_ONE, GL_ZERO);
3647 GL_AlphaTest(false);
3648 GL_ScissorTest(true);
3650 GL_DepthRange(0, 1);
3652 R_Mesh_Matrix(&identitymatrix);
3653 R_Mesh_ResetTextureState();
3654 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3655 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
3656 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
3657 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
3658 qglStencilMask(~0);CHECKGLERROR
3659 qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
3660 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
3661 GL_CullFace(r_refdef.view.cullface_back);
3662 R_SetupGenericShader(true);
3665 void R_RenderScene(void);
3666 void R_RenderWaterPlanes(void);
3668 static void R_Water_StartFrame(void)
3671 int waterwidth, waterheight, texturewidth, textureheight;
3672 r_waterstate_waterplane_t *p;
3674 // set waterwidth and waterheight to the water resolution that will be
3675 // used (often less than the screen resolution for faster rendering)
3676 waterwidth = (int)bound(1, vid.width * r_water_resolutionmultiplier.value, vid.width);
3677 waterheight = (int)bound(1, vid.height * r_water_resolutionmultiplier.value, vid.height);
3679 // calculate desired texture sizes
3680 // can't use water if the card does not support the texture size
3681 if (!r_water.integer || !r_glsl.integer || !gl_support_fragment_shader || waterwidth > gl_max_texture_size || waterheight > gl_max_texture_size || r_showsurfaces.integer)
3682 texturewidth = textureheight = waterwidth = waterheight = 0;
3683 else if (gl_support_arb_texture_non_power_of_two)
3685 texturewidth = waterwidth;
3686 textureheight = waterheight;
3690 for (texturewidth = 1;texturewidth < waterwidth ;texturewidth *= 2);
3691 for (textureheight = 1;textureheight < waterheight;textureheight *= 2);
3694 // allocate textures as needed
3695 if (r_waterstate.texturewidth != texturewidth || r_waterstate.textureheight != textureheight)
3697 r_waterstate.maxwaterplanes = MAX_WATERPLANES;
3698 for (i = 0, p = r_waterstate.waterplanes;i < r_waterstate.maxwaterplanes;i++, p++)
3700 if (p->texture_refraction)
3701 R_FreeTexture(p->texture_refraction);
3702 p->texture_refraction = NULL;
3703 if (p->texture_reflection)
3704 R_FreeTexture(p->texture_reflection);
3705 p->texture_reflection = NULL;
3707 memset(&r_waterstate, 0, sizeof(r_waterstate));
3708 r_waterstate.texturewidth = texturewidth;
3709 r_waterstate.textureheight = textureheight;
3712 if (r_waterstate.texturewidth)
3714 r_waterstate.enabled = true;
3716 // when doing a reduced render (HDR) we want to use a smaller area
3717 r_waterstate.waterwidth = (int)bound(1, r_refdef.view.width * r_water_resolutionmultiplier.value, r_refdef.view.width);
3718 r_waterstate.waterheight = (int)bound(1, r_refdef.view.height * r_water_resolutionmultiplier.value, r_refdef.view.height);
3720 // set up variables that will be used in shader setup
3721 r_waterstate.screenscale[0] = 0.5f * (float)r_waterstate.waterwidth / (float)r_waterstate.texturewidth;
3722 r_waterstate.screenscale[1] = 0.5f * (float)r_waterstate.waterheight / (float)r_waterstate.textureheight;
3723 r_waterstate.screencenter[0] = 0.5f * (float)r_waterstate.waterwidth / (float)r_waterstate.texturewidth;
3724 r_waterstate.screencenter[1] = 0.5f * (float)r_waterstate.waterheight / (float)r_waterstate.textureheight;
3727 r_waterstate.maxwaterplanes = MAX_WATERPLANES;
3728 r_waterstate.numwaterplanes = 0;
3731 void R_Water_AddWaterPlane(msurface_t *surface)
3733 int triangleindex, planeindex;
3739 r_waterstate_waterplane_t *p;
3740 texture_t *t = R_GetCurrentTexture(surface->texture);
3741 // just use the first triangle with a valid normal for any decisions
3742 VectorClear(normal);
3743 for (triangleindex = 0, e = rsurface.modelelement3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
3745 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[0]*3, vert[0]);
3746 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[1]*3, vert[1]);
3747 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[2]*3, vert[2]);
3748 TriangleNormal(vert[0], vert[1], vert[2], normal);
3749 if (VectorLength2(normal) >= 0.001)
3753 VectorCopy(normal, plane.normal);
3754 VectorNormalize(plane.normal);
3755 plane.dist = DotProduct(vert[0], plane.normal);
3756 PlaneClassify(&plane);
3757 if (PlaneDiff(r_refdef.view.origin, &plane) < 0)
3759 // skip backfaces (except if nocullface is set)
3760 if (!(t->currentmaterialflags & MATERIALFLAG_NOCULLFACE))
3762 VectorNegate(plane.normal, plane.normal);
3764 PlaneClassify(&plane);
3768 // find a matching plane if there is one
3769 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
3770 if (fabs(PlaneDiff(vert[0], &p->plane)) < 1 && fabs(PlaneDiff(vert[1], &p->plane)) < 1 && fabs(PlaneDiff(vert[2], &p->plane)) < 1)
3772 if (planeindex >= r_waterstate.maxwaterplanes)
3773 return; // nothing we can do, out of planes
3775 // if this triangle does not fit any known plane rendered this frame, add one
3776 if (planeindex >= r_waterstate.numwaterplanes)
3778 // store the new plane
3779 r_waterstate.numwaterplanes++;
3781 // clear materialflags and pvs
3782 p->materialflags = 0;
3783 p->pvsvalid = false;
3785 // merge this surface's materialflags into the waterplane
3786 p->materialflags |= t->currentmaterialflags;
3787 // merge this surface's PVS into the waterplane
3788 VectorMAM(0.5f, surface->mins, 0.5f, surface->maxs, center);
3789 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS
3790 && r_refdef.scene.worldmodel->brush.PointInLeaf && r_refdef.scene.worldmodel->brush.PointInLeaf(r_refdef.scene.worldmodel, center)->clusterindex >= 0)
3792 r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, center, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
3797 static void R_Water_ProcessPlanes(void)
3799 r_refdef_view_t originalview;
3800 r_refdef_view_t myview;
3802 r_waterstate_waterplane_t *p;
3804 originalview = r_refdef.view;
3806 // make sure enough textures are allocated
3807 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
3809 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
3811 if (!p->texture_refraction)
3812 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);
3813 if (!p->texture_refraction)
3817 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
3819 if (!p->texture_reflection)
3820 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);
3821 if (!p->texture_reflection)
3827 r_refdef.view = originalview;
3828 r_refdef.view.showdebug = false;
3829 r_refdef.view.width = r_waterstate.waterwidth;
3830 r_refdef.view.height = r_waterstate.waterheight;
3831 r_refdef.view.useclipplane = true;
3832 myview = r_refdef.view;
3833 r_waterstate.renderingscene = true;
3834 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
3836 // render the normal view scene and copy into texture
3837 // (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)
3838 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
3840 r_refdef.view = myview;
3841 r_refdef.view.clipplane = p->plane;
3842 VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
3843 r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
3844 PlaneClassify(&r_refdef.view.clipplane);
3846 R_ResetViewRendering3D();
3847 R_ClearScreen(r_refdef.fogenabled);
3851 // copy view into the screen texture
3852 R_Mesh_TexBind(0, R_GetTexture(p->texture_refraction));
3853 GL_ActiveTexture(0);
3855 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);CHECKGLERROR
3858 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
3860 r_refdef.view = myview;
3861 // render reflected scene and copy into texture
3862 Matrix4x4_Reflect(&r_refdef.view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
3863 // update the r_refdef.view.origin because otherwise the sky renders at the wrong location (amongst other problems)
3864 Matrix4x4_OriginFromMatrix(&r_refdef.view.matrix, r_refdef.view.origin);
3865 r_refdef.view.clipplane = p->plane;
3866 // reverse the cullface settings for this render
3867 r_refdef.view.cullface_front = GL_FRONT;
3868 r_refdef.view.cullface_back = GL_BACK;
3869 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
3871 r_refdef.view.usecustompvs = true;
3873 memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
3875 memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
3878 R_ResetViewRendering3D();
3879 R_ClearScreen(r_refdef.fogenabled);
3883 R_Mesh_TexBind(0, R_GetTexture(p->texture_reflection));
3884 GL_ActiveTexture(0);
3886 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);CHECKGLERROR
3889 r_waterstate.renderingscene = false;
3890 r_refdef.view = originalview;
3891 R_ResetViewRendering3D();
3892 R_ClearScreen(r_refdef.fogenabled);
3896 r_refdef.view = originalview;
3897 r_waterstate.renderingscene = false;
3898 Cvar_SetValueQuick(&r_water, 0);
3899 Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed! Turned off r_water.\n");
3903 void R_Bloom_StartFrame(void)
3905 int bloomtexturewidth, bloomtextureheight, screentexturewidth, screentextureheight;
3907 // set bloomwidth and bloomheight to the bloom resolution that will be
3908 // used (often less than the screen resolution for faster rendering)
3909 r_bloomstate.bloomwidth = bound(1, r_bloom_resolution.integer, vid.height);
3910 r_bloomstate.bloomheight = r_bloomstate.bloomwidth * vid.height / vid.width;
3911 r_bloomstate.bloomheight = bound(1, r_bloomstate.bloomheight, vid.height);
3912 r_bloomstate.bloomwidth = bound(1, r_bloomstate.bloomwidth, gl_max_texture_size);
3913 r_bloomstate.bloomheight = bound(1, r_bloomstate.bloomheight, gl_max_texture_size);
3915 // calculate desired texture sizes
3916 if (gl_support_arb_texture_non_power_of_two)
3918 screentexturewidth = r_refdef.view.width;
3919 screentextureheight = r_refdef.view.height;
3920 bloomtexturewidth = r_bloomstate.bloomwidth;
3921 bloomtextureheight = r_bloomstate.bloomheight;
3925 for (screentexturewidth = 1;screentexturewidth < vid.width ;screentexturewidth *= 2);
3926 for (screentextureheight = 1;screentextureheight < vid.height ;screentextureheight *= 2);
3927 for (bloomtexturewidth = 1;bloomtexturewidth < r_bloomstate.bloomwidth ;bloomtexturewidth *= 2);
3928 for (bloomtextureheight = 1;bloomtextureheight < r_bloomstate.bloomheight;bloomtextureheight *= 2);
3931 if ((r_hdr.integer || r_bloom.integer || (!R_Stereo_Active() && (r_motionblur.value > 0 || r_damageblur.value > 0))) && ((r_bloom_resolution.integer < 4 || r_bloom_blur.value < 1 || r_bloom_blur.value >= 512) || r_refdef.view.width > gl_max_texture_size || r_refdef.view.height > gl_max_texture_size))
3933 Cvar_SetValueQuick(&r_hdr, 0);
3934 Cvar_SetValueQuick(&r_bloom, 0);
3935 Cvar_SetValueQuick(&r_motionblur, 0);
3936 Cvar_SetValueQuick(&r_damageblur, 0);
3939 if (!(r_glsl.integer && (r_glsl_postprocess.integer || (!R_Stereo_ColorMasking() && r_glsl_saturation.value != 1) || (v_glslgamma.integer && !vid_gammatables_trivial))) && !r_bloom.integer && !r_hdr.integer && (R_Stereo_Active() || (r_motionblur.value <= 0 && r_damageblur.value <= 0)))
3940 screentexturewidth = screentextureheight = 0;
3941 if (!r_hdr.integer && !r_bloom.integer)
3942 bloomtexturewidth = bloomtextureheight = 0;
3944 // allocate textures as needed
3945 if (r_bloomstate.screentexturewidth != screentexturewidth || r_bloomstate.screentextureheight != screentextureheight)
3947 if (r_bloomstate.texture_screen)
3948 R_FreeTexture(r_bloomstate.texture_screen);
3949 r_bloomstate.texture_screen = NULL;
3950 r_bloomstate.screentexturewidth = screentexturewidth;
3951 r_bloomstate.screentextureheight = screentextureheight;
3952 if (r_bloomstate.screentexturewidth && r_bloomstate.screentextureheight)
3953 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);
3955 if (r_bloomstate.bloomtexturewidth != bloomtexturewidth || r_bloomstate.bloomtextureheight != bloomtextureheight)
3957 if (r_bloomstate.texture_bloom)
3958 R_FreeTexture(r_bloomstate.texture_bloom);
3959 r_bloomstate.texture_bloom = NULL;
3960 r_bloomstate.bloomtexturewidth = bloomtexturewidth;
3961 r_bloomstate.bloomtextureheight = bloomtextureheight;
3962 if (r_bloomstate.bloomtexturewidth && r_bloomstate.bloomtextureheight)
3963 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);
3966 // when doing a reduced render (HDR) we want to use a smaller area
3967 r_bloomstate.bloomwidth = bound(1, r_bloom_resolution.integer, r_refdef.view.height);
3968 r_bloomstate.bloomheight = r_bloomstate.bloomwidth * r_refdef.view.height / r_refdef.view.width;
3969 r_bloomstate.bloomheight = bound(1, r_bloomstate.bloomheight, r_refdef.view.height);
3970 r_bloomstate.bloomwidth = bound(1, r_bloomstate.bloomwidth, r_bloomstate.bloomtexturewidth);
3971 r_bloomstate.bloomheight = bound(1, r_bloomstate.bloomheight, r_bloomstate.bloomtextureheight);
3973 // set up a texcoord array for the full resolution screen image
3974 // (we have to keep this around to copy back during final render)
3975 r_bloomstate.screentexcoord2f[0] = 0;
3976 r_bloomstate.screentexcoord2f[1] = (float)r_refdef.view.height / (float)r_bloomstate.screentextureheight;
3977 r_bloomstate.screentexcoord2f[2] = (float)r_refdef.view.width / (float)r_bloomstate.screentexturewidth;
3978 r_bloomstate.screentexcoord2f[3] = (float)r_refdef.view.height / (float)r_bloomstate.screentextureheight;
3979 r_bloomstate.screentexcoord2f[4] = (float)r_refdef.view.width / (float)r_bloomstate.screentexturewidth;
3980 r_bloomstate.screentexcoord2f[5] = 0;
3981 r_bloomstate.screentexcoord2f[6] = 0;
3982 r_bloomstate.screentexcoord2f[7] = 0;
3984 // set up a texcoord array for the reduced resolution bloom image
3985 // (which will be additive blended over the screen image)
3986 r_bloomstate.bloomtexcoord2f[0] = 0;
3987 r_bloomstate.bloomtexcoord2f[1] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3988 r_bloomstate.bloomtexcoord2f[2] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3989 r_bloomstate.bloomtexcoord2f[3] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3990 r_bloomstate.bloomtexcoord2f[4] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3991 r_bloomstate.bloomtexcoord2f[5] = 0;
3992 r_bloomstate.bloomtexcoord2f[6] = 0;
3993 r_bloomstate.bloomtexcoord2f[7] = 0;
3995 if (r_hdr.integer || r_bloom.integer)
3997 r_bloomstate.enabled = true;
3998 r_bloomstate.hdr = r_hdr.integer != 0;
4001 R_Viewport_InitOrtho(&r_bloomstate.viewport, &identitymatrix, r_refdef.view.x, vid.height - r_bloomstate.bloomheight - r_refdef.view.y, r_bloomstate.bloomwidth, r_bloomstate.bloomheight, 0, 0, 1, 1, -10, 100, NULL);
4004 void R_Bloom_CopyBloomTexture(float colorscale)
4006 r_refdef.stats.bloom++;
4008 // scale down screen texture to the bloom texture size
4010 R_SetViewport(&r_bloomstate.viewport);
4011 GL_BlendFunc(GL_ONE, GL_ZERO);
4012 GL_Color(colorscale, colorscale, colorscale, 1);
4013 // TODO: optimize with multitexture or GLSL
4014 R_SetupGenericShader(true);
4015 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
4016 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
4017 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4018 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
4020 // we now have a bloom image in the framebuffer
4021 // copy it into the bloom image texture for later processing
4022 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
4023 GL_ActiveTexture(0);
4025 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_bloomstate.viewport.x, r_bloomstate.viewport.y, r_bloomstate.viewport.width, r_bloomstate.viewport.height);CHECKGLERROR
4026 r_refdef.stats.bloom_copypixels += r_bloomstate.viewport.width * r_bloomstate.viewport.height;
4029 void R_Bloom_CopyHDRTexture(void)
4031 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
4032 GL_ActiveTexture(0);
4034 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);CHECKGLERROR
4035 r_refdef.stats.bloom_copypixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
4038 void R_Bloom_MakeTexture(void)
4041 float xoffset, yoffset, r, brighten;
4043 r_refdef.stats.bloom++;
4045 R_ResetViewRendering2D();
4046 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
4047 R_Mesh_ColorPointer(NULL, 0, 0);
4048 R_SetupGenericShader(true);
4050 // we have a bloom image in the framebuffer
4052 R_SetViewport(&r_bloomstate.viewport);
4054 for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
4057 r = bound(0, r_bloom_colorexponent.value / x, 1);
4058 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
4059 GL_Color(r, r, r, 1);
4060 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
4061 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
4062 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4063 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
4065 // copy the vertically blurred bloom view to a texture
4066 GL_ActiveTexture(0);
4068 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_bloomstate.viewport.x, r_bloomstate.viewport.y, r_bloomstate.viewport.width, r_bloomstate.viewport.height);CHECKGLERROR
4069 r_refdef.stats.bloom_copypixels += r_bloomstate.viewport.width * r_bloomstate.viewport.height;
4072 range = r_bloom_blur.integer * r_bloomstate.bloomwidth / 320;
4073 brighten = r_bloom_brighten.value;
4075 brighten *= r_hdr_range.value;
4076 brighten = sqrt(brighten);
4078 brighten *= (3 * range) / (2 * range - 1); // compensate for the "dot particle"
4079 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
4080 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.offsettexcoord2f, 0, 0);
4082 for (dir = 0;dir < 2;dir++)
4084 // blend on at multiple vertical offsets to achieve a vertical blur
4085 // TODO: do offset blends using GLSL
4086 // TODO instead of changing the texcoords, change the target positions to prevent artifacts at edges
4087 GL_BlendFunc(GL_ONE, GL_ZERO);
4088 for (x = -range;x <= range;x++)
4090 if (!dir){xoffset = 0;yoffset = x;}
4091 else {xoffset = x;yoffset = 0;}
4092 xoffset /= (float)r_bloomstate.bloomtexturewidth;
4093 yoffset /= (float)r_bloomstate.bloomtextureheight;
4094 // compute a texcoord array with the specified x and y offset
4095 r_bloomstate.offsettexcoord2f[0] = xoffset+0;
4096 r_bloomstate.offsettexcoord2f[1] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
4097 r_bloomstate.offsettexcoord2f[2] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
4098 r_bloomstate.offsettexcoord2f[3] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
4099 r_bloomstate.offsettexcoord2f[4] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
4100 r_bloomstate.offsettexcoord2f[5] = yoffset+0;
4101 r_bloomstate.offsettexcoord2f[6] = xoffset+0;
4102 r_bloomstate.offsettexcoord2f[7] = yoffset+0;
4103 // this r value looks like a 'dot' particle, fading sharply to
4104 // black at the edges
4105 // (probably not realistic but looks good enough)
4106 //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
4107 //r = brighten/(range*2+1);
4108 r = brighten / (range * 2 + 1);
4110 r *= (1 - x*x/(float)(range*range));
4111 GL_Color(r, r, r, 1);
4112 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4113 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
4114 GL_BlendFunc(GL_ONE, GL_ONE);
4117 // copy the vertically blurred bloom view to a texture
4118 GL_ActiveTexture(0);
4120 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_bloomstate.viewport.x, r_bloomstate.viewport.y, r_bloomstate.viewport.width, r_bloomstate.viewport.height);CHECKGLERROR
4121 r_refdef.stats.bloom_copypixels += r_bloomstate.viewport.width * r_bloomstate.viewport.height;
4124 // apply subtract last
4125 // (just like it would be in a GLSL shader)
4126 if (r_bloom_colorsubtract.value > 0 && gl_support_ext_blend_subtract)
4128 GL_BlendFunc(GL_ONE, GL_ZERO);
4129 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
4130 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
4131 GL_Color(1, 1, 1, 1);
4132 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4133 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
4135 GL_BlendFunc(GL_ONE, GL_ONE);
4136 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
4137 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
4138 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
4139 GL_Color(r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 1);
4140 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4141 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
4142 qglBlendEquationEXT(GL_FUNC_ADD_EXT);
4144 // copy the darkened bloom view to a texture
4145 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
4146 GL_ActiveTexture(0);
4148 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_bloomstate.viewport.x, r_bloomstate.viewport.y, r_bloomstate.viewport.width, r_bloomstate.viewport.height);CHECKGLERROR
4149 r_refdef.stats.bloom_copypixels += r_bloomstate.viewport.width * r_bloomstate.viewport.height;
4153 void R_HDR_RenderBloomTexture(void)
4155 int oldwidth, oldheight;
4156 float oldcolorscale;
4158 oldcolorscale = r_refdef.view.colorscale;
4159 oldwidth = r_refdef.view.width;
4160 oldheight = r_refdef.view.height;
4161 r_refdef.view.width = r_bloomstate.bloomwidth;
4162 r_refdef.view.height = r_bloomstate.bloomheight;
4164 // TODO: support GL_EXT_framebuffer_object rather than reusing the framebuffer? it might improve SLI performance.
4165 // TODO: add exposure compensation features
4166 // TODO: add fp16 framebuffer support (using GL_EXT_framebuffer_object)
4168 r_refdef.view.showdebug = false;
4169 r_refdef.view.colorscale *= r_bloom_colorscale.value / bound(1, r_hdr_range.value, 16);
4171 R_ResetViewRendering3D();
4173 R_ClearScreen(r_refdef.fogenabled);
4174 if (r_timereport_active)
4175 R_TimeReport("HDRclear");
4178 if (r_timereport_active)
4179 R_TimeReport("visibility");
4181 // only do secondary renders with HDR if r_hdr is 2 or higher
4182 r_waterstate.numwaterplanes = 0;
4183 if (r_waterstate.enabled && r_hdr.integer >= 2)
4184 R_RenderWaterPlanes();
4186 r_refdef.view.showdebug = true;
4188 r_waterstate.numwaterplanes = 0;
4190 R_ResetViewRendering2D();
4192 R_Bloom_CopyHDRTexture();
4193 R_Bloom_MakeTexture();
4195 // restore the view settings
4196 r_refdef.view.width = oldwidth;
4197 r_refdef.view.height = oldheight;
4198 r_refdef.view.colorscale = oldcolorscale;
4200 R_ResetViewRendering3D();
4202 R_ClearScreen(r_refdef.fogenabled);
4203 if (r_timereport_active)
4204 R_TimeReport("viewclear");
4207 static void R_BlendView(void)
4209 if (r_bloomstate.texture_screen)
4211 // make sure the buffer is available
4212 if (r_bloom_blur.value < 1) { Cvar_SetValueQuick(&r_bloom_blur, 1); }
4214 R_ResetViewRendering2D();
4215 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
4216 R_Mesh_ColorPointer(NULL, 0, 0);
4217 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
4218 GL_ActiveTexture(0);CHECKGLERROR
4220 if(!R_Stereo_Active() && (r_motionblur.value > 0 || r_damageblur.value > 0))
4222 // declare variables
4224 static float avgspeed;
4226 speed = VectorLength(cl.movement_velocity);
4228 cl.motionbluralpha = bound(0, (cl.time - cl.oldtime) / max(0.001, r_motionblur_vcoeff.value), 1);
4229 avgspeed = avgspeed * (1 - cl.motionbluralpha) + speed * cl.motionbluralpha;
4231 speed = (avgspeed - r_motionblur_vmin.value) / max(1, r_motionblur_vmax.value - r_motionblur_vmin.value);
4232 speed = bound(0, speed, 1);
4233 speed = speed * (1 - r_motionblur_bmin.value) + r_motionblur_bmin.value;
4235 // calculate values into a standard alpha
4236 cl.motionbluralpha = 1 - exp(-
4238 (r_motionblur.value * speed / 80)
4240 (r_damageblur.value * (cl.cshifts[CSHIFT_DAMAGE].percent / 1600))
4243 max(0.0001, cl.time - cl.oldtime) // fps independent
4246 cl.motionbluralpha *= lhrandom(1 - r_motionblur_randomize.value, 1 + r_motionblur_randomize.value);
4247 cl.motionbluralpha = bound(0, cl.motionbluralpha, r_motionblur_maxblur.value);
4249 if (cl.motionbluralpha > 0)
4251 R_SetupGenericShader(true);
4252 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
4253 GL_Color(1, 1, 1, cl.motionbluralpha);
4254 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
4255 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
4256 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4257 r_refdef.stats.bloom_drawpixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
4261 // copy view into the screen texture
4262 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);CHECKGLERROR
4263 r_refdef.stats.bloom_copypixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
4266 if (r_glsl.integer && gl_support_fragment_shader && (r_bloomstate.texture_screen || r_bloomstate.texture_bloom))
4268 unsigned int permutation =
4269 (r_bloomstate.texture_bloom ? SHADERPERMUTATION_BLOOM : 0)
4270 | (r_refdef.viewblend[3] > 0 ? SHADERPERMUTATION_VIEWTINT : 0)
4271 | ((v_glslgamma.value && !vid_gammatables_trivial) ? SHADERPERMUTATION_GAMMARAMPS : 0)
4272 | (r_glsl_postprocess.integer ? SHADERPERMUTATION_POSTPROCESSING : 0)
4273 | ((!R_Stereo_ColorMasking() && r_glsl_saturation.value != 1) ? SHADERPERMUTATION_SATURATION : 0);
4275 if (r_bloomstate.texture_bloom && !r_bloomstate.hdr)
4277 // render simple bloom effect
4278 // copy the screen and shrink it and darken it for the bloom process
4279 R_Bloom_CopyBloomTexture(r_bloom_colorscale.value);
4280 // make the bloom texture
4281 R_Bloom_MakeTexture();
4284 R_ResetViewRendering2D();
4285 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
4286 R_Mesh_ColorPointer(NULL, 0, 0);
4287 GL_Color(1, 1, 1, 1);
4288 GL_BlendFunc(GL_ONE, GL_ZERO);
4289 R_SetupShader_SetPermutation(SHADERMODE_POSTPROCESS, permutation);
4290 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
4291 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
4292 R_Mesh_TexBind(1, R_GetTexture(r_bloomstate.texture_bloom));
4293 R_Mesh_TexCoordPointer(1, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
4294 if (r_glsl_permutation->loc_Texture_GammaRamps >= 0)
4295 R_Mesh_TexBind(GL20TU_GAMMARAMPS, R_GetTexture(r_texture_gammaramps));
4296 if (r_glsl_permutation->loc_TintColor >= 0)
4297 qglUniform4fARB(r_glsl_permutation->loc_TintColor, r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
4298 if (r_glsl_permutation->loc_ClientTime >= 0)
4299 qglUniform1fARB(r_glsl_permutation->loc_ClientTime, cl.time);
4300 if (r_glsl_permutation->loc_PixelSize >= 0)
4301 qglUniform2fARB(r_glsl_permutation->loc_PixelSize, 1.0/r_bloomstate.screentexturewidth, 1.0/r_bloomstate.screentextureheight);
4302 if (r_glsl_permutation->loc_UserVec1 >= 0)
4304 float a=0, b=0, c=0, d=0;
4305 #if _MSC_VER >= 1400
4306 #define sscanf sscanf_s
4308 sscanf(r_glsl_postprocess_uservec1.string, "%f %f %f %f", &a, &b, &c, &d);
4309 qglUniform4fARB(r_glsl_permutation->loc_UserVec1, a, b, c, d);
4311 if (r_glsl_permutation->loc_UserVec2 >= 0)
4313 float a=0, b=0, c=0, d=0;
4314 sscanf(r_glsl_postprocess_uservec2.string, "%f %f %f %f", &a, &b, &c, &d);
4315 qglUniform4fARB(r_glsl_permutation->loc_UserVec2, a, b, c, d);
4317 if (r_glsl_permutation->loc_UserVec3 >= 0)
4319 float a=0, b=0, c=0, d=0;
4320 sscanf(r_glsl_postprocess_uservec3.string, "%f %f %f %f", &a, &b, &c, &d);
4321 qglUniform4fARB(r_glsl_permutation->loc_UserVec3, a, b, c, d);
4323 if (r_glsl_permutation->loc_UserVec4 >= 0)
4325 float a=0, b=0, c=0, d=0;
4326 sscanf(r_glsl_postprocess_uservec4.string, "%f %f %f %f", &a, &b, &c, &d);
4327 qglUniform4fARB(r_glsl_permutation->loc_UserVec4, a, b, c, d);
4329 if (r_glsl_permutation->loc_Saturation >= 0)
4330 qglUniform1fARB(r_glsl_permutation->loc_Saturation, r_glsl_saturation.value);
4331 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4332 r_refdef.stats.bloom_drawpixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
4338 if (r_bloomstate.texture_bloom && r_bloomstate.hdr)
4340 // render high dynamic range bloom effect
4341 // the bloom texture was made earlier this render, so we just need to
4342 // blend it onto the screen...
4343 R_ResetViewRendering2D();
4344 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
4345 R_Mesh_ColorPointer(NULL, 0, 0);
4346 R_SetupGenericShader(true);
4347 GL_Color(1, 1, 1, 1);
4348 GL_BlendFunc(GL_ONE, GL_ONE);
4349 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
4350 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
4351 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4352 r_refdef.stats.bloom_drawpixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
4354 else if (r_bloomstate.texture_bloom)
4356 // render simple bloom effect
4357 // copy the screen and shrink it and darken it for the bloom process
4358 R_Bloom_CopyBloomTexture(r_bloom_colorscale.value);
4359 // make the bloom texture
4360 R_Bloom_MakeTexture();
4361 // put the original screen image back in place and blend the bloom
4363 R_ResetViewRendering2D();
4364 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
4365 R_Mesh_ColorPointer(NULL, 0, 0);
4366 GL_Color(1, 1, 1, 1);
4367 GL_BlendFunc(GL_ONE, GL_ZERO);
4368 // do both in one pass if possible
4369 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
4370 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
4371 if (r_textureunits.integer >= 2 && gl_combine.integer)
4373 R_SetupGenericTwoTextureShader(GL_ADD);
4374 R_Mesh_TexBind(1, R_GetTexture(r_bloomstate.texture_screen));
4375 R_Mesh_TexCoordPointer(1, 2, r_bloomstate.screentexcoord2f, 0, 0);
4379 R_SetupGenericShader(true);
4380 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4381 r_refdef.stats.bloom_drawpixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
4382 // now blend on the bloom texture
4383 GL_BlendFunc(GL_ONE, GL_ONE);
4384 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
4385 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
4387 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4388 r_refdef.stats.bloom_drawpixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
4390 if (r_refdef.viewblend[3] >= (1.0f / 256.0f))
4392 // apply a color tint to the whole view
4393 R_ResetViewRendering2D();
4394 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
4395 R_Mesh_ColorPointer(NULL, 0, 0);
4396 R_SetupGenericShader(false);
4397 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
4398 GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
4399 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
4403 matrix4x4_t r_waterscrollmatrix;
4405 void R_UpdateFogColor(void) // needs to be called before HDR subrender too, as that changes colorscale!
4407 if (r_refdef.fog_density)
4409 r_refdef.fogcolor[0] = r_refdef.fog_red;
4410 r_refdef.fogcolor[1] = r_refdef.fog_green;
4411 r_refdef.fogcolor[2] = r_refdef.fog_blue;
4415 VectorCopy(r_refdef.fogcolor, fogvec);
4416 // color.rgb *= ContrastBoost * SceneBrightness;
4417 VectorScale(fogvec, r_refdef.view.colorscale, fogvec);
4418 r_refdef.fogcolor[0] = bound(0.0f, fogvec[0], 1.0f);
4419 r_refdef.fogcolor[1] = bound(0.0f, fogvec[1], 1.0f);
4420 r_refdef.fogcolor[2] = bound(0.0f, fogvec[2], 1.0f);
4425 void R_UpdateVariables(void)
4429 r_refdef.scene.ambient = r_ambient.value;
4431 r_refdef.farclip = 4096;
4432 if (r_refdef.scene.worldmodel)
4433 r_refdef.farclip += r_refdef.scene.worldmodel->radius * 2;
4434 r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
4436 if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
4437 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
4438 r_refdef.polygonfactor = 0;
4439 r_refdef.polygonoffset = 0;
4440 r_refdef.shadowpolygonfactor = r_refdef.polygonfactor + r_shadow_polygonfactor.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
4441 r_refdef.shadowpolygonoffset = r_refdef.polygonoffset + r_shadow_polygonoffset.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
4443 r_refdef.scene.rtworld = r_shadow_realtime_world.integer != 0;
4444 r_refdef.scene.rtworldshadows = r_shadow_realtime_world_shadows.integer && gl_stencil;
4445 r_refdef.scene.rtdlight = (r_shadow_realtime_world.integer || r_shadow_realtime_dlight.integer) && !gl_flashblend.integer && r_dynamic.integer;
4446 r_refdef.scene.rtdlightshadows = r_refdef.scene.rtdlight && r_shadow_realtime_dlight_shadows.integer && gl_stencil;
4447 r_refdef.lightmapintensity = r_refdef.scene.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
4448 if (r_showsurfaces.integer)
4450 r_refdef.scene.rtworld = false;
4451 r_refdef.scene.rtworldshadows = false;
4452 r_refdef.scene.rtdlight = false;
4453 r_refdef.scene.rtdlightshadows = false;
4454 r_refdef.lightmapintensity = 0;
4457 if (gamemode == GAME_NEHAHRA)
4459 if (gl_fogenable.integer)
4461 r_refdef.oldgl_fogenable = true;
4462 r_refdef.fog_density = gl_fogdensity.value;
4463 r_refdef.fog_red = gl_fogred.value;
4464 r_refdef.fog_green = gl_foggreen.value;
4465 r_refdef.fog_blue = gl_fogblue.value;
4466 r_refdef.fog_alpha = 1;
4467 r_refdef.fog_start = 0;
4468 r_refdef.fog_end = gl_skyclip.value;
4470 else if (r_refdef.oldgl_fogenable)
4472 r_refdef.oldgl_fogenable = false;
4473 r_refdef.fog_density = 0;
4474 r_refdef.fog_red = 0;
4475 r_refdef.fog_green = 0;
4476 r_refdef.fog_blue = 0;
4477 r_refdef.fog_alpha = 0;
4478 r_refdef.fog_start = 0;
4479 r_refdef.fog_end = 0;
4483 r_refdef.fog_alpha = bound(0, r_refdef.fog_alpha, 1);
4484 r_refdef.fog_start = max(0, r_refdef.fog_start);
4485 r_refdef.fog_end = max(r_refdef.fog_start + 0.01, r_refdef.fog_end);
4487 // R_UpdateFogColor(); // why? R_RenderScene does it anyway
4489 if (r_refdef.fog_density && r_drawfog.integer)
4491 r_refdef.fogenabled = true;
4492 // this is the point where the fog reaches 0.9986 alpha, which we
4493 // consider a good enough cutoff point for the texture
4494 // (0.9986 * 256 == 255.6)
4495 if (r_fog_exp2.integer)
4496 r_refdef.fogrange = 32 / (r_refdef.fog_density * r_refdef.fog_density) + r_refdef.fog_start;
4498 r_refdef.fogrange = 2048 / r_refdef.fog_density + r_refdef.fog_start;
4499 r_refdef.fogrange = bound(r_refdef.fog_start, r_refdef.fogrange, r_refdef.fog_end);
4500 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
4501 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
4502 // fog color was already set
4503 // update the fog texture
4504 if (r_refdef.fogmasktable_start != r_refdef.fog_start || r_refdef.fogmasktable_alpha != r_refdef.fog_alpha || r_refdef.fogmasktable_density != r_refdef.fog_density || r_refdef.fogmasktable_range != r_refdef.fogrange)
4505 R_BuildFogTexture();
4508 r_refdef.fogenabled = false;
4510 if(r_glsl.integer && v_glslgamma.integer && !vid_gammatables_trivial)
4512 if(!r_texture_gammaramps || vid_gammatables_serial != r_texture_gammaramps_serial)
4514 // build GLSL gamma texture
4515 #define RAMPWIDTH 256
4516 unsigned short ramp[RAMPWIDTH * 3];
4517 unsigned char rampbgr[RAMPWIDTH][4];
4520 r_texture_gammaramps_serial = vid_gammatables_serial;
4522 VID_BuildGammaTables(&ramp[0], RAMPWIDTH);
4523 for(i = 0; i < RAMPWIDTH; ++i)
4525 rampbgr[i][0] = (unsigned char) (ramp[i + 2 * RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
4526 rampbgr[i][1] = (unsigned char) (ramp[i + RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
4527 rampbgr[i][2] = (unsigned char) (ramp[i] * 255.0 / 65535.0 + 0.5);
4530 if (r_texture_gammaramps)
4532 R_UpdateTexture(r_texture_gammaramps, &rampbgr[0][0], 0, 0, RAMPWIDTH, 1);
4536 r_texture_gammaramps = R_LoadTexture2D(r_main_texturepool, "gammaramps", RAMPWIDTH, 1, &rampbgr[0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
4542 // remove GLSL gamma texture
4546 static r_refdef_scene_type_t r_currentscenetype = RST_CLIENT;
4547 static r_refdef_scene_t r_scenes_store[ RST_COUNT ];
4553 void R_SelectScene( r_refdef_scene_type_t scenetype ) {
4554 if( scenetype != r_currentscenetype ) {
4555 // store the old scenetype
4556 r_scenes_store[ r_currentscenetype ] = r_refdef.scene;
4557 r_currentscenetype = scenetype;
4558 // move in the new scene
4559 r_refdef.scene = r_scenes_store[ r_currentscenetype ];
4568 r_refdef_scene_t * R_GetScenePointer( r_refdef_scene_type_t scenetype )
4570 // of course, we could also add a qboolean that provides a lock state and a ReleaseScenePointer function..
4571 if( scenetype == r_currentscenetype ) {
4572 return &r_refdef.scene;
4574 return &r_scenes_store[ scenetype ];
4583 void R_RenderView(void)
4585 if (r_timereport_active)
4586 R_TimeReport("start");
4587 r_frame++; // used only by R_GetCurrentTexture
4588 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
4590 R_AnimCache_NewFrame();
4592 if (r_refdef.view.isoverlay)
4594 // TODO: FIXME: move this into its own backend function maybe? [2/5/2008 Andreas]
4595 GL_Clear( GL_DEPTH_BUFFER_BIT );
4596 R_TimeReport("depthclear");
4598 r_refdef.view.showdebug = false;
4600 r_waterstate.enabled = false;
4601 r_waterstate.numwaterplanes = 0;
4609 if (!r_refdef.scene.entities || r_refdef.view.width * r_refdef.view.height == 0 || !r_renderview.integer/* || !r_refdef.scene.worldmodel*/)
4610 return; //Host_Error ("R_RenderView: NULL worldmodel");
4612 r_refdef.view.colorscale = r_hdr_scenebrightness.value;
4614 // break apart the view matrix into vectors for various purposes
4615 // it is important that this occurs outside the RenderScene function because that can be called from reflection renders, where the vectors come out wrong
4616 // however the r_refdef.view.origin IS updated in RenderScene intentionally - otherwise the sky renders at the wrong origin, etc
4617 Matrix4x4_ToVectors(&r_refdef.view.matrix, r_refdef.view.forward, r_refdef.view.left, r_refdef.view.up, r_refdef.view.origin);
4618 VectorNegate(r_refdef.view.left, r_refdef.view.right);
4619 // make an inverted copy of the view matrix for tracking sprites
4620 Matrix4x4_Invert_Simple(&r_refdef.view.inverse_matrix, &r_refdef.view.matrix);
4622 R_Shadow_UpdateWorldLightSelection();
4624 R_Bloom_StartFrame();
4625 R_Water_StartFrame();
4628 if (r_timereport_active)
4629 R_TimeReport("viewsetup");
4631 R_ResetViewRendering3D();
4633 if (r_refdef.view.clear || r_refdef.fogenabled)
4635 R_ClearScreen(r_refdef.fogenabled);
4636 if (r_timereport_active)
4637 R_TimeReport("viewclear");
4639 r_refdef.view.clear = true;
4641 // this produces a bloom texture to be used in R_BlendView() later
4643 R_HDR_RenderBloomTexture();
4645 r_refdef.view.showdebug = true;
4648 if (r_timereport_active)
4649 R_TimeReport("visibility");
4651 r_waterstate.numwaterplanes = 0;
4652 if (r_waterstate.enabled)
4653 R_RenderWaterPlanes();
4656 r_waterstate.numwaterplanes = 0;
4659 if (r_timereport_active)
4660 R_TimeReport("blendview");
4662 GL_Scissor(0, 0, vid.width, vid.height);
4663 GL_ScissorTest(false);
4667 void R_RenderWaterPlanes(void)
4669 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawAddWaterPlanes)
4671 r_refdef.scene.worldmodel->DrawAddWaterPlanes(r_refdef.scene.worldentity);
4672 if (r_timereport_active)
4673 R_TimeReport("waterworld");
4676 // don't let sound skip if going slow
4677 if (r_refdef.scene.extraupdate)
4680 R_DrawModelsAddWaterPlanes();
4681 if (r_timereport_active)
4682 R_TimeReport("watermodels");
4684 if (r_waterstate.numwaterplanes)
4686 R_Water_ProcessPlanes();
4687 if (r_timereport_active)
4688 R_TimeReport("waterscenes");
4692 extern void R_DrawLightningBeams (void);
4693 extern void VM_CL_AddPolygonsToMeshQueue (void);
4694 extern void R_DrawPortals (void);
4695 extern cvar_t cl_locs_show;
4696 static void R_DrawLocs(void);
4697 static void R_DrawEntityBBoxes(void);
4698 void R_RenderScene(void)
4700 r_refdef.stats.renders++;
4704 // don't let sound skip if going slow
4705 if (r_refdef.scene.extraupdate)
4708 R_MeshQueue_BeginScene();
4712 Matrix4x4_CreateTranslate(&r_waterscrollmatrix, sin(r_refdef.scene.time) * 0.025 * r_waterscroll.value, sin(r_refdef.scene.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
4714 if (cl.csqc_vidvars.drawworld)
4716 // don't let sound skip if going slow
4717 if (r_refdef.scene.extraupdate)
4720 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawSky)
4722 r_refdef.scene.worldmodel->DrawSky(r_refdef.scene.worldentity);
4723 if (r_timereport_active)
4724 R_TimeReport("worldsky");
4727 if (R_DrawBrushModelsSky() && r_timereport_active)
4728 R_TimeReport("bmodelsky");
4731 R_AnimCache_CacheVisibleEntities();
4733 if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawDepth)
4735 r_refdef.scene.worldmodel->DrawDepth(r_refdef.scene.worldentity);
4736 if (r_timereport_active)
4737 R_TimeReport("worlddepth");
4739 if (r_depthfirst.integer >= 2)
4741 R_DrawModelsDepth();
4742 if (r_timereport_active)
4743 R_TimeReport("modeldepth");
4746 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->Draw)
4748 r_refdef.scene.worldmodel->Draw(r_refdef.scene.worldentity);
4749 if (r_timereport_active)
4750 R_TimeReport("world");
4753 // don't let sound skip if going slow
4754 if (r_refdef.scene.extraupdate)
4758 if (r_timereport_active)
4759 R_TimeReport("models");
4761 // don't let sound skip if going slow
4762 if (r_refdef.scene.extraupdate)
4765 if (r_shadows.integer > 0 && !r_shadows_drawafterrtlighting.integer && r_refdef.lightmapintensity > 0)
4767 R_DrawModelShadows();
4768 R_ResetViewRendering3D();
4769 // don't let sound skip if going slow
4770 if (r_refdef.scene.extraupdate)
4774 R_ShadowVolumeLighting(false);
4775 if (r_timereport_active)
4776 R_TimeReport("rtlights");
4778 // don't let sound skip if going slow
4779 if (r_refdef.scene.extraupdate)
4782 if (r_shadows.integer > 0 && r_shadows_drawafterrtlighting.integer && r_refdef.lightmapintensity > 0)
4784 R_DrawModelShadows();
4785 R_ResetViewRendering3D();
4786 // don't let sound skip if going slow
4787 if (r_refdef.scene.extraupdate)
4791 if (cl.csqc_vidvars.drawworld)
4793 R_DrawLightningBeams();
4794 if (r_timereport_active)
4795 R_TimeReport("lightning");
4798 if (r_timereport_active)
4799 R_TimeReport("decals");
4802 if (r_timereport_active)
4803 R_TimeReport("particles");
4806 if (r_timereport_active)
4807 R_TimeReport("explosions");
4810 R_SetupGenericShader(true);
4811 VM_CL_AddPolygonsToMeshQueue();
4813 if (r_refdef.view.showdebug)
4815 if (cl_locs_show.integer)
4818 if (r_timereport_active)
4819 R_TimeReport("showlocs");
4822 if (r_drawportals.integer)
4825 if (r_timereport_active)
4826 R_TimeReport("portals");
4829 if (r_showbboxes.value > 0)
4831 R_DrawEntityBBoxes();
4832 if (r_timereport_active)
4833 R_TimeReport("bboxes");
4837 R_SetupGenericShader(true);
4838 R_MeshQueue_RenderTransparent();
4839 if (r_timereport_active)
4840 R_TimeReport("drawtrans");
4842 R_SetupGenericShader(true);
4844 if (r_refdef.view.showdebug && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawDebug && (r_showtris.value > 0 || r_shownormals.value != 0 || r_showcollisionbrushes.value > 0))
4846 r_refdef.scene.worldmodel->DrawDebug(r_refdef.scene.worldentity);
4847 if (r_timereport_active)
4848 R_TimeReport("worlddebug");
4849 R_DrawModelsDebug();
4850 if (r_timereport_active)
4851 R_TimeReport("modeldebug");
4854 R_SetupGenericShader(true);
4856 if (cl.csqc_vidvars.drawworld)
4859 if (r_timereport_active)
4860 R_TimeReport("coronas");
4863 // don't let sound skip if going slow
4864 if (r_refdef.scene.extraupdate)
4867 R_ResetViewRendering2D();
4870 static const unsigned short bboxelements[36] =
4880 void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
4883 float *v, *c, f1, f2, vertex3f[8*3], color4f[8*4];
4884 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
4885 GL_DepthMask(false);
4886 GL_DepthRange(0, 1);
4887 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
4888 R_Mesh_Matrix(&identitymatrix);
4889 R_Mesh_ResetTextureState();
4891 vertex3f[ 0] = mins[0];vertex3f[ 1] = mins[1];vertex3f[ 2] = mins[2]; //
4892 vertex3f[ 3] = maxs[0];vertex3f[ 4] = mins[1];vertex3f[ 5] = mins[2];
4893 vertex3f[ 6] = mins[0];vertex3f[ 7] = maxs[1];vertex3f[ 8] = mins[2];
4894 vertex3f[ 9] = maxs[0];vertex3f[10] = maxs[1];vertex3f[11] = mins[2];
4895 vertex3f[12] = mins[0];vertex3f[13] = mins[1];vertex3f[14] = maxs[2];
4896 vertex3f[15] = maxs[0];vertex3f[16] = mins[1];vertex3f[17] = maxs[2];
4897 vertex3f[18] = mins[0];vertex3f[19] = maxs[1];vertex3f[20] = maxs[2];
4898 vertex3f[21] = maxs[0];vertex3f[22] = maxs[1];vertex3f[23] = maxs[2];
4899 R_FillColors(color4f, 8, cr, cg, cb, ca);
4900 if (r_refdef.fogenabled)
4902 for (i = 0, v = vertex3f, c = color4f;i < 8;i++, v += 3, c += 4)
4904 f1 = FogPoint_World(v);
4906 c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
4907 c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
4908 c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
4911 R_Mesh_VertexPointer(vertex3f, 0, 0);
4912 R_Mesh_ColorPointer(color4f, 0, 0);
4913 R_Mesh_ResetTextureState();
4914 R_SetupGenericShader(false);
4915 R_Mesh_Draw(0, 8, 0, 12, NULL, bboxelements, 0, 0);
4918 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
4922 prvm_edict_t *edict;
4923 prvm_prog_t *prog_save = prog;
4925 // this function draws bounding boxes of server entities
4929 GL_CullFace(GL_NONE);
4930 R_SetupGenericShader(false);
4934 for (i = 0;i < numsurfaces;i++)
4936 edict = PRVM_EDICT_NUM(surfacelist[i]);
4937 switch ((int)edict->fields.server->solid)
4939 case SOLID_NOT: Vector4Set(color, 1, 1, 1, 0.05);break;
4940 case SOLID_TRIGGER: Vector4Set(color, 1, 0, 1, 0.10);break;
4941 case SOLID_BBOX: Vector4Set(color, 0, 1, 0, 0.10);break;
4942 case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
4943 case SOLID_BSP: Vector4Set(color, 0, 0, 1, 0.05);break;
4944 default: Vector4Set(color, 0, 0, 0, 0.50);break;
4946 color[3] *= r_showbboxes.value;
4947 color[3] = bound(0, color[3], 1);
4948 GL_DepthTest(!r_showdisabledepthtest.integer);
4949 GL_CullFace(r_refdef.view.cullface_front);
4950 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
4956 static void R_DrawEntityBBoxes(void)
4959 prvm_edict_t *edict;
4961 prvm_prog_t *prog_save = prog;
4963 // this function draws bounding boxes of server entities
4969 for (i = 0;i < prog->num_edicts;i++)
4971 edict = PRVM_EDICT_NUM(i);
4972 if (edict->priv.server->free)
4974 // exclude the following for now, as they don't live in world coordinate space and can't be solid:
4975 if(PRVM_EDICTFIELDVALUE(edict, prog->fieldoffsets.tag_entity)->edict != 0)
4977 if(PRVM_EDICTFIELDVALUE(edict, prog->fieldoffsets.viewmodelforclient)->edict != 0)
4979 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
4980 R_MeshQueue_AddTransparent(center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)NULL);
4986 unsigned short nomodelelements[24] =
4998 float nomodelvertex3f[6*3] =
5008 float nomodelcolor4f[6*4] =
5010 0.0f, 0.0f, 0.5f, 1.0f,
5011 0.0f, 0.0f, 0.5f, 1.0f,
5012 0.0f, 0.5f, 0.0f, 1.0f,
5013 0.0f, 0.5f, 0.0f, 1.0f,
5014 0.5f, 0.0f, 0.0f, 1.0f,
5015 0.5f, 0.0f, 0.0f, 1.0f
5018 void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
5023 // this is only called once per entity so numsurfaces is always 1, and
5024 // surfacelist is always {0}, so this code does not handle batches
5025 R_Mesh_Matrix(&ent->matrix);
5027 if (ent->flags & EF_ADDITIVE)
5029 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
5030 GL_DepthMask(false);
5032 else if (ent->alpha < 1)
5034 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5035 GL_DepthMask(false);
5039 GL_BlendFunc(GL_ONE, GL_ZERO);
5042 GL_DepthRange(0, (ent->flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
5043 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
5044 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
5045 GL_CullFace((ent->effects & EF_DOUBLESIDED) ? GL_NONE : r_refdef.view.cullface_back);
5046 R_SetupGenericShader(false);
5047 R_Mesh_VertexPointer(nomodelvertex3f, 0, 0);
5048 if (r_refdef.fogenabled)
5051 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
5052 R_Mesh_ColorPointer(color4f, 0, 0);
5053 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
5054 f1 = FogPoint_World(org);
5056 for (i = 0, c = color4f;i < 6;i++, c += 4)
5058 c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
5059 c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
5060 c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
5064 else if (ent->alpha != 1)
5066 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
5067 R_Mesh_ColorPointer(color4f, 0, 0);
5068 for (i = 0, c = color4f;i < 6;i++, c += 4)
5072 R_Mesh_ColorPointer(nomodelcolor4f, 0, 0);
5073 R_Mesh_ResetTextureState();
5074 R_Mesh_Draw(0, 6, 0, 8, NULL, nomodelelements, 0, 0);
5077 void R_DrawNoModel(entity_render_t *ent)
5080 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
5081 //if ((ent->effects & EF_ADDITIVE) || (ent->alpha < 1))
5082 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_refdef.view.origin : org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
5084 // R_DrawNoModelCallback(ent, 0);
5087 void R_CalcBeam_Vertex3f (float *vert, const vec3_t org1, const vec3_t org2, float width)
5089 vec3_t right1, right2, diff, normal;
5091 VectorSubtract (org2, org1, normal);
5093 // calculate 'right' vector for start
5094 VectorSubtract (r_refdef.view.origin, org1, diff);
5095 CrossProduct (normal, diff, right1);
5096 VectorNormalize (right1);
5098 // calculate 'right' vector for end
5099 VectorSubtract (r_refdef.view.origin, org2, diff);
5100 CrossProduct (normal, diff, right2);
5101 VectorNormalize (right2);
5103 vert[ 0] = org1[0] + width * right1[0];
5104 vert[ 1] = org1[1] + width * right1[1];
5105 vert[ 2] = org1[2] + width * right1[2];
5106 vert[ 3] = org1[0] - width * right1[0];
5107 vert[ 4] = org1[1] - width * right1[1];
5108 vert[ 5] = org1[2] - width * right1[2];
5109 vert[ 6] = org2[0] - width * right2[0];
5110 vert[ 7] = org2[1] - width * right2[1];
5111 vert[ 8] = org2[2] - width * right2[2];
5112 vert[ 9] = org2[0] + width * right2[0];
5113 vert[10] = org2[1] + width * right2[1];
5114 vert[11] = org2[2] + width * right2[2];
5117 float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
5119 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)
5121 // NOTE: this must not call qglDepthFunc (see r_shadow.c, R_BeginCoronaQuery) thanks to ATI
5125 if (r_refdef.fogenabled && !depthdisable) // TODO maybe make the unfog effect a separate flag?
5126 fog = FogPoint_World(origin);
5128 R_Mesh_Matrix(&identitymatrix);
5129 GL_BlendFunc(blendfunc1, blendfunc2);
5131 GL_CullFace(GL_NONE);
5133 GL_DepthMask(false);
5134 GL_DepthRange(0, depthshort ? 0.0625 : 1);
5135 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
5136 GL_DepthTest(!depthdisable);
5138 vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
5139 vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
5140 vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
5141 vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
5142 vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
5143 vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
5144 vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
5145 vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
5146 vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
5147 vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
5148 vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
5149 vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
5151 R_Mesh_VertexPointer(vertex3f, 0, 0);
5152 R_Mesh_ColorPointer(NULL, 0, 0);
5153 R_Mesh_ResetTextureState();
5154 R_SetupGenericShader(true);
5155 R_Mesh_TexBind(0, R_GetTexture(texture));
5156 R_Mesh_TexCoordPointer(0, 2, spritetexcoord2f, 0, 0);
5157 // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
5158 GL_Color(cr * fog * r_refdef.view.colorscale, cg * fog * r_refdef.view.colorscale, cb * fog * r_refdef.view.colorscale, ca);
5159 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
5161 if (blendfunc2 == GL_ONE_MINUS_SRC_ALPHA)
5163 R_Mesh_TexBind(0, R_GetTexture(fogtexture));
5164 GL_BlendFunc(blendfunc1, GL_ONE);
5166 GL_Color(r_refdef.fogcolor[0] * fog, r_refdef.fogcolor[1] * fog, r_refdef.fogcolor[2] * fog, ca);
5167 R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
5171 int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
5176 VectorSet(v, x, y, z);
5177 for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
5178 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
5180 if (i == mesh->numvertices)
5182 if (mesh->numvertices < mesh->maxvertices)
5184 VectorCopy(v, vertex3f);
5185 mesh->numvertices++;
5187 return mesh->numvertices;
5193 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
5197 element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
5198 element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
5199 e = mesh->element3i + mesh->numtriangles * 3;
5200 for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
5202 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
5203 if (mesh->numtriangles < mesh->maxtriangles)
5208 mesh->numtriangles++;
5210 element[1] = element[2];
5214 void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
5218 element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
5219 element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
5220 e = mesh->element3i + mesh->numtriangles * 3;
5221 for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
5223 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
5224 if (mesh->numtriangles < mesh->maxtriangles)
5229 mesh->numtriangles++;
5231 element[1] = element[2];
5235 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
5236 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
5238 int planenum, planenum2;
5241 mplane_t *plane, *plane2;
5243 double temppoints[2][256*3];
5244 // figure out how large a bounding box we need to properly compute this brush
5246 for (w = 0;w < numplanes;w++)
5247 maxdist = max(maxdist, planes[w].dist);
5248 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
5249 maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
5250 for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
5254 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
5255 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
5257 if (planenum2 == planenum)
5259 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);
5262 if (tempnumpoints < 3)
5264 // generate elements forming a triangle fan for this polygon
5265 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
5269 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)
5271 texturelayer_t *layer;
5272 layer = t->currentlayers + t->currentnumlayers++;
5274 layer->depthmask = depthmask;
5275 layer->blendfunc1 = blendfunc1;
5276 layer->blendfunc2 = blendfunc2;
5277 layer->texture = texture;
5278 layer->texmatrix = *matrix;
5279 layer->color[0] = r * r_refdef.view.colorscale;
5280 layer->color[1] = g * r_refdef.view.colorscale;
5281 layer->color[2] = b * r_refdef.view.colorscale;
5282 layer->color[3] = a;
5285 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
5288 index = parms[2] + r_refdef.scene.time * parms[3];
5289 index -= floor(index);
5293 case Q3WAVEFUNC_NONE:
5294 case Q3WAVEFUNC_NOISE:
5295 case Q3WAVEFUNC_COUNT:
5298 case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
5299 case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
5300 case Q3WAVEFUNC_SAWTOOTH: f = index;break;
5301 case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
5302 case Q3WAVEFUNC_TRIANGLE:
5304 f = index - floor(index);
5315 return (float)(parms[0] + parms[1] * f);
5318 void R_tcMod_ApplyToMatrix(matrix4x4_t *texmatrix, q3shaderinfo_layer_tcmod_t *tcmod, int currentmaterialflags)
5323 matrix4x4_t matrix, temp;
5324 switch(tcmod->tcmod)
5328 if (currentmaterialflags & MATERIALFLAG_WATERSCROLL)
5329 matrix = r_waterscrollmatrix;
5331 matrix = identitymatrix;
5333 case Q3TCMOD_ENTITYTRANSLATE:
5334 // this is used in Q3 to allow the gamecode to control texcoord
5335 // scrolling on the entity, which is not supported in darkplaces yet.
5336 Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
5338 case Q3TCMOD_ROTATE:
5339 Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
5340 Matrix4x4_ConcatRotate(&matrix, tcmod->parms[0] * r_refdef.scene.time, 0, 0, 1);
5341 Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
5344 Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
5346 case Q3TCMOD_SCROLL:
5347 Matrix4x4_CreateTranslate(&matrix, tcmod->parms[0] * r_refdef.scene.time, tcmod->parms[1] * r_refdef.scene.time, 0);
5349 case Q3TCMOD_PAGE: // poor man's animmap (to store animations into a single file, useful for HTTP downloaded textures)
5350 w = (int) tcmod->parms[0];
5351 h = (int) tcmod->parms[1];
5352 f = r_refdef.scene.time / (tcmod->parms[2] * w * h);
5354 idx = (int) floor(f * w * h);
5355 Matrix4x4_CreateTranslate(&matrix, (idx % w) / tcmod->parms[0], (idx / w) / tcmod->parms[1], 0);
5357 case Q3TCMOD_STRETCH:
5358 f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
5359 Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
5361 case Q3TCMOD_TRANSFORM:
5362 VectorSet(tcmat + 0, tcmod->parms[0], tcmod->parms[1], 0);
5363 VectorSet(tcmat + 3, tcmod->parms[2], tcmod->parms[3], 0);
5364 VectorSet(tcmat + 6, 0 , 0 , 1);
5365 VectorSet(tcmat + 9, tcmod->parms[4], tcmod->parms[5], 0);
5366 Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
5368 case Q3TCMOD_TURBULENT:
5369 // this is handled in the RSurf_PrepareVertices function
5370 matrix = identitymatrix;
5374 Matrix4x4_Concat(texmatrix, &matrix, &temp);
5377 texture_t *R_GetCurrentTexture(texture_t *t)
5380 const entity_render_t *ent = rsurface.entity;
5381 dp_model_t *model = ent->model;
5382 q3shaderinfo_layer_tcmod_t *tcmod;
5384 if (t->update_lastrenderframe == r_frame && t->update_lastrenderentity == (void *)ent)
5385 return t->currentframe;
5386 t->update_lastrenderframe = r_frame;
5387 t->update_lastrenderentity = (void *)ent;
5389 // switch to an alternate material if this is a q1bsp animated material
5391 texture_t *texture = t;
5392 int s = ent->skinnum;
5393 if ((unsigned int)s >= (unsigned int)model->numskins)
5395 if (model->skinscenes)
5397 if (model->skinscenes[s].framecount > 1)
5398 s = model->skinscenes[s].firstframe + (unsigned int) (r_refdef.scene.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
5400 s = model->skinscenes[s].firstframe;
5403 t = t + s * model->num_surfaces;
5406 // use an alternate animation if the entity's frame is not 0,
5407 // and only if the texture has an alternate animation
5408 if (ent->framegroupblend[0].frame != 0 && t->anim_total[1])
5409 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(r_refdef.scene.time * 5.0f) % t->anim_total[1]) : 0];
5411 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(r_refdef.scene.time * 5.0f) % t->anim_total[0]) : 0];
5413 texture->currentframe = t;
5416 // update currentskinframe to be a qw skin or animation frame
5417 if ((i = ent->entitynumber - 1) >= 0 && i < cl.maxclients && cls.protocol == PROTOCOL_QUAKEWORLD && cl.scores[i].qw_skin[0] && !strcmp(ent->model->name, "progs/player.mdl"))
5419 if (strcmp(r_qwskincache[i], cl.scores[i].qw_skin))
5421 strlcpy(r_qwskincache[i], cl.scores[i].qw_skin, sizeof(r_qwskincache[i]));
5422 if (developer_loading.integer)
5423 Con_Printf("loading skins/%s\n", r_qwskincache[i]);
5424 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);
5426 t->currentskinframe = r_qwskincache_skinframe[i];
5427 if (t->currentskinframe == NULL)
5428 t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->shadertime)) % t->numskinframes];
5430 else if (t->numskinframes >= 2)
5431 t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->shadertime)) % t->numskinframes];
5432 if (t->backgroundnumskinframes >= 2)
5433 t->backgroundcurrentskinframe = t->backgroundskinframes[(int)(t->backgroundskinframerate * (cl.time - ent->shadertime)) % t->backgroundnumskinframes];
5435 t->currentmaterialflags = t->basematerialflags;
5436 t->currentalpha = ent->alpha;
5437 if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_novis.integer))
5438 t->currentalpha *= r_wateralpha.value;
5439 if(t->basematerialflags & MATERIALFLAG_WATERSHADER && r_waterstate.enabled && !r_refdef.view.isoverlay)
5440 t->currentalpha *= t->r_water_wateralpha;
5441 if(!r_waterstate.enabled || r_refdef.view.isoverlay)
5442 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION);
5443 if (!(ent->flags & RENDER_LIGHT))
5444 t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
5445 else if (rsurface.modeltexcoordlightmap2f == NULL)
5447 // pick a model lighting mode
5448 if (VectorLength2(ent->modellight_diffuse) >= (1.0f / 256.0f))
5449 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT | MATERIALFLAG_MODELLIGHT_DIRECTIONAL;
5451 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
5453 if (ent->effects & EF_ADDITIVE)
5454 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
5455 else if (t->currentalpha < 1)
5456 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
5457 if (ent->effects & EF_DOUBLESIDED)
5458 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
5459 if (ent->effects & EF_NODEPTHTEST)
5460 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
5461 if (ent->flags & RENDER_VIEWMODEL)
5462 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
5463 if (t->backgroundnumskinframes)
5464 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
5465 if (t->currentmaterialflags & MATERIALFLAG_BLENDED)
5467 if (t->currentmaterialflags & (MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER))
5468 t->currentmaterialflags &= ~MATERIALFLAG_BLENDED;
5471 t->currentmaterialflags &= ~(MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER);
5473 // there is no tcmod
5474 if (t->currentmaterialflags & MATERIALFLAG_WATERSCROLL)
5476 t->currenttexmatrix = r_waterscrollmatrix;
5477 t->currentbackgroundtexmatrix = r_waterscrollmatrix;
5481 Matrix4x4_CreateIdentity(&t->currenttexmatrix);
5482 Matrix4x4_CreateIdentity(&t->currentbackgroundtexmatrix);
5485 for (i = 0, tcmod = t->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
5486 R_tcMod_ApplyToMatrix(&t->currenttexmatrix, tcmod, t->currentmaterialflags);
5487 for (i = 0, tcmod = t->backgroundtcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
5488 R_tcMod_ApplyToMatrix(&t->currentbackgroundtexmatrix, tcmod, t->currentmaterialflags);
5490 t->colormapping = VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f);
5491 t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
5492 t->glosstexture = r_texture_black;
5493 t->backgroundbasetexture = t->backgroundnumskinframes ? ((!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base) : r_texture_white;
5494 t->backgroundglosstexture = r_texture_black;
5495 t->specularpower = r_shadow_glossexponent.value;
5496 // TODO: store reference values for these in the texture?
5497 t->specularscale = 0;
5498 if (r_shadow_gloss.integer > 0)
5500 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
5502 if (r_shadow_glossintensity.value > 0)
5504 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
5505 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
5506 t->specularscale = r_shadow_glossintensity.value;
5509 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
5511 t->glosstexture = r_texture_white;
5512 t->backgroundglosstexture = r_texture_white;
5513 t->specularscale = r_shadow_gloss2intensity.value;
5514 t->specularpower = r_shadow_gloss2exponent.value;
5517 t->specularscale *= t->specularscalemod;
5518 t->specularpower *= t->specularpowermod;
5520 // lightmaps mode looks bad with dlights using actual texturing, so turn
5521 // off the colormap and glossmap, but leave the normalmap on as it still
5522 // accurately represents the shading involved
5523 if (gl_lightmaps.integer)
5525 t->basetexture = r_texture_grey128;
5526 t->backgroundbasetexture = NULL;
5527 t->specularscale = 0;
5528 t->currentmaterialflags = MATERIALFLAG_WALL | (t->currentmaterialflags & (MATERIALFLAG_NOCULLFACE | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_MODELLIGHT_DIRECTIONAL | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SHORTDEPTHRANGE));
5531 Vector4Set(t->lightmapcolor, ent->colormod[0], ent->colormod[1], ent->colormod[2], t->currentalpha);
5532 VectorClear(t->dlightcolor);
5533 t->currentnumlayers = 0;
5534 if (t->currentmaterialflags & MATERIALFLAG_WALL)
5537 int blendfunc1, blendfunc2;
5539 if (t->currentmaterialflags & MATERIALFLAG_ADD)
5541 blendfunc1 = GL_SRC_ALPHA;
5542 blendfunc2 = GL_ONE;
5544 else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
5546 blendfunc1 = GL_SRC_ALPHA;
5547 blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
5549 else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
5551 blendfunc1 = t->customblendfunc[0];
5552 blendfunc2 = t->customblendfunc[1];
5556 blendfunc1 = GL_ONE;
5557 blendfunc2 = GL_ZERO;
5559 depthmask = !(t->currentmaterialflags & MATERIALFLAG_BLENDED);
5560 if (r_refdef.fogenabled && (t->currentmaterialflags & MATERIALFLAG_BLENDED))
5561 layerflags |= TEXTURELAYERFLAG_FOGDARKEN;
5562 if (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
5564 // fullbright is not affected by r_refdef.lightmapintensity
5565 R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_TEXTURE, t->basetexture, &t->currenttexmatrix, t->lightmapcolor[0], t->lightmapcolor[1], t->lightmapcolor[2], t->lightmapcolor[3]);
5566 if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
5567 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * t->lightmapcolor[0], ent->colormap_pantscolor[1] * t->lightmapcolor[1], ent->colormap_pantscolor[2] * t->lightmapcolor[2], t->lightmapcolor[3]);
5568 if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
5569 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * t->lightmapcolor[0], ent->colormap_shirtcolor[1] * t->lightmapcolor[1], ent->colormap_shirtcolor[2] * t->lightmapcolor[2], t->lightmapcolor[3]);
5573 vec3_t ambientcolor;
5575 // set the color tint used for lights affecting this surface
5576 VectorSet(t->dlightcolor, ent->colormod[0] * t->lightmapcolor[3], ent->colormod[1] * t->lightmapcolor[3], ent->colormod[2] * t->lightmapcolor[3]);
5578 // q3bsp has no lightmap updates, so the lightstylevalue that
5579 // would normally be baked into the lightmap must be
5580 // applied to the color
5581 // FIXME: r_glsl 1 rendering doesn't support overbright lightstyles with this (the default light style is not overbright)
5582 if (ent->model->type == mod_brushq3)
5583 colorscale *= r_refdef.scene.rtlightstylevalue[0];
5584 colorscale *= r_refdef.lightmapintensity;
5585 VectorScale(t->lightmapcolor, r_refdef.scene.ambient * (1.0f / 64.0f), ambientcolor);
5586 VectorScale(t->lightmapcolor, colorscale, t->lightmapcolor);
5587 // basic lit geometry
5588 R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LITTEXTURE, t->basetexture, &t->currenttexmatrix, t->lightmapcolor[0], t->lightmapcolor[1], t->lightmapcolor[2], t->lightmapcolor[3]);
5589 // add pants/shirt if needed
5590 if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
5591 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * t->lightmapcolor[0], ent->colormap_pantscolor[1] * t->lightmapcolor[1], ent->colormap_pantscolor[2] * t->lightmapcolor[2], t->lightmapcolor[3]);
5592 if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
5593 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * t->lightmapcolor[0], ent->colormap_shirtcolor[1] * t->lightmapcolor[1], ent->colormap_shirtcolor[2] * t->lightmapcolor[2], t->lightmapcolor[3]);
5594 // now add ambient passes if needed
5595 if (VectorLength2(ambientcolor) >= (1.0f/1048576.0f))
5597 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->basetexture, &t->currenttexmatrix, ambientcolor[0], ambientcolor[1], ambientcolor[2], t->lightmapcolor[3]);
5598 if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
5599 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ambientcolor[0], ent->colormap_pantscolor[1] * ambientcolor[1], ent->colormap_pantscolor[2] * ambientcolor[2], t->lightmapcolor[3]);
5600 if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
5601 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ambientcolor[0], ent->colormap_shirtcolor[1] * ambientcolor[1], ent->colormap_shirtcolor[2] * ambientcolor[2], t->lightmapcolor[3]);
5604 if (t->currentskinframe->glow != NULL && !gl_lightmaps.integer)
5605 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->lightmapcolor[3]);
5606 if (r_refdef.fogenabled && !(t->currentmaterialflags & MATERIALFLAG_ADD))
5608 // if this is opaque use alpha blend which will darken the earlier
5611 // if this is an alpha blended material, all the earlier passes
5612 // were darkened by fog already, so we only need to add the fog
5613 // color ontop through the fog mask texture
5615 // if this is an additive blended material, all the earlier passes
5616 // were darkened by fog already, and we should not add fog color
5617 // (because the background was not darkened, there is no fog color
5618 // that was lost behind it).
5619 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.view.colorscale, r_refdef.fogcolor[1] / r_refdef.view.colorscale, r_refdef.fogcolor[2] / r_refdef.view.colorscale, t->lightmapcolor[3]);
5623 return t->currentframe;
5626 rsurfacestate_t rsurface;
5628 void R_Mesh_ResizeArrays(int newvertices)
5631 if (rsurface.array_size >= newvertices)
5633 if (rsurface.array_modelvertex3f)
5634 Mem_Free(rsurface.array_modelvertex3f);
5635 rsurface.array_size = (newvertices + 1023) & ~1023;
5636 base = (float *)Mem_Alloc(r_main_mempool, rsurface.array_size * sizeof(float[33]));
5637 rsurface.array_modelvertex3f = base + rsurface.array_size * 0;
5638 rsurface.array_modelsvector3f = base + rsurface.array_size * 3;
5639 rsurface.array_modeltvector3f = base + rsurface.array_size * 6;
5640 rsurface.array_modelnormal3f = base + rsurface.array_size * 9;
5641 rsurface.array_deformedvertex3f = base + rsurface.array_size * 12;
5642 rsurface.array_deformedsvector3f = base + rsurface.array_size * 15;
5643 rsurface.array_deformedtvector3f = base + rsurface.array_size * 18;
5644 rsurface.array_deformednormal3f = base + rsurface.array_size * 21;
5645 rsurface.array_texcoord3f = base + rsurface.array_size * 24;
5646 rsurface.array_color4f = base + rsurface.array_size * 27;
5647 rsurface.array_generatedtexcoordtexture2f = base + rsurface.array_size * 31;
5650 void RSurf_ActiveWorldEntity(void)
5652 dp_model_t *model = r_refdef.scene.worldmodel;
5653 //if (rsurface.entity == r_refdef.scene.worldentity)
5655 rsurface.entity = r_refdef.scene.worldentity;
5656 if (rsurface.array_size < model->surfmesh.num_vertices)
5657 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
5658 rsurface.matrix = identitymatrix;
5659 rsurface.inversematrix = identitymatrix;
5660 R_Mesh_Matrix(&identitymatrix);
5661 VectorCopy(r_refdef.view.origin, rsurface.modelorg);
5662 VectorSet(rsurface.modellight_ambient, 0, 0, 0);
5663 VectorSet(rsurface.modellight_diffuse, 0, 0, 0);
5664 VectorSet(rsurface.modellight_lightdir, 0, 0, 1);
5665 VectorSet(rsurface.colormap_pantscolor, 0, 0, 0);
5666 VectorSet(rsurface.colormap_shirtcolor, 0, 0, 0);
5667 VectorSet(rsurface.glowmod, 1, 1, 1);
5668 memset(rsurface.frameblend, 0, sizeof(rsurface.frameblend));
5669 rsurface.frameblend[0].lerp = 1;
5670 rsurface.basepolygonfactor = r_refdef.polygonfactor;
5671 rsurface.basepolygonoffset = r_refdef.polygonoffset;
5672 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
5673 rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
5674 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
5675 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
5676 rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
5677 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
5678 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
5679 rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
5680 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
5681 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
5682 rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
5683 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
5684 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
5685 rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
5686 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
5687 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
5688 rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
5689 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
5690 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
5691 rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
5692 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
5693 rsurface.modelelement3i = model->surfmesh.data_element3i;
5694 rsurface.modelelement3s = model->surfmesh.data_element3s;
5695 rsurface.modelelement3i_bufferobject = model->surfmesh.ebo3i;
5696 rsurface.modelelement3s_bufferobject = model->surfmesh.ebo3s;
5697 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
5698 rsurface.modelnum_vertices = model->surfmesh.num_vertices;
5699 rsurface.modelnum_triangles = model->surfmesh.num_triangles;
5700 rsurface.modelsurfaces = model->data_surfaces;
5701 rsurface.generatedvertex = false;
5702 rsurface.vertex3f = rsurface.modelvertex3f;
5703 rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
5704 rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
5705 rsurface.svector3f = rsurface.modelsvector3f;
5706 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
5707 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
5708 rsurface.tvector3f = rsurface.modeltvector3f;
5709 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
5710 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
5711 rsurface.normal3f = rsurface.modelnormal3f;
5712 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
5713 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
5714 rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
5717 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
5719 dp_model_t *model = ent->model;
5720 //if (rsurface.entity == ent && (!model->surfmesh.isanimated || (!wantnormals && !wanttangents)))
5722 rsurface.entity = (entity_render_t *)ent;
5723 if (rsurface.array_size < model->surfmesh.num_vertices)
5724 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
5725 rsurface.matrix = ent->matrix;
5726 rsurface.inversematrix = ent->inversematrix;
5727 R_Mesh_Matrix(&rsurface.matrix);
5728 Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.modelorg);
5729 rsurface.modellight_ambient[0] = ent->modellight_ambient[0] * ent->colormod[0];
5730 rsurface.modellight_ambient[1] = ent->modellight_ambient[1] * ent->colormod[1];
5731 rsurface.modellight_ambient[2] = ent->modellight_ambient[2] * ent->colormod[2];
5732 rsurface.modellight_diffuse[0] = ent->modellight_diffuse[0] * ent->colormod[0];
5733 rsurface.modellight_diffuse[1] = ent->modellight_diffuse[1] * ent->colormod[1];
5734 rsurface.modellight_diffuse[2] = ent->modellight_diffuse[2] * ent->colormod[2];
5735 VectorCopy(ent->modellight_diffuse, rsurface.modellight_diffuse);
5736 VectorCopy(ent->modellight_lightdir, rsurface.modellight_lightdir);
5737 VectorCopy(ent->colormap_pantscolor, rsurface.colormap_pantscolor);
5738 VectorCopy(ent->colormap_shirtcolor, rsurface.colormap_shirtcolor);
5739 VectorCopy(ent->glowmod, rsurface.glowmod);
5740 memcpy(rsurface.frameblend, ent->frameblend, sizeof(ent->frameblend));
5741 rsurface.basepolygonfactor = r_refdef.polygonfactor;
5742 rsurface.basepolygonoffset = r_refdef.polygonoffset;
5743 if (ent->model->brush.submodel)
5745 rsurface.basepolygonfactor += r_polygonoffset_submodel_factor.value;
5746 rsurface.basepolygonoffset += r_polygonoffset_submodel_offset.value;
5748 if (model->surfmesh.isanimated && model->AnimateVertices && (rsurface.frameblend[0].lerp != 1 || rsurface.frameblend[0].subframe != 0))
5750 if (R_AnimCache_GetEntity((entity_render_t *)ent, wantnormals, wanttangents))
5752 rsurface.modelvertex3f = r_animcachestate.entity[ent->animcacheindex].vertex3f;
5753 rsurface.modelsvector3f = wanttangents ? r_animcachestate.entity[ent->animcacheindex].svector3f : NULL;
5754 rsurface.modeltvector3f = wanttangents ? r_animcachestate.entity[ent->animcacheindex].tvector3f : NULL;
5755 rsurface.modelnormal3f = wantnormals ? r_animcachestate.entity[ent->animcacheindex].normal3f : NULL;
5757 else if (wanttangents)
5759 rsurface.modelvertex3f = rsurface.array_modelvertex3f;
5760 rsurface.modelsvector3f = rsurface.array_modelsvector3f;
5761 rsurface.modeltvector3f = rsurface.array_modeltvector3f;
5762 rsurface.modelnormal3f = rsurface.array_modelnormal3f;
5763 model->AnimateVertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, rsurface.array_modelsvector3f, rsurface.array_modeltvector3f);
5765 else if (wantnormals)
5767 rsurface.modelvertex3f = rsurface.array_modelvertex3f;
5768 rsurface.modelsvector3f = NULL;
5769 rsurface.modeltvector3f = NULL;
5770 rsurface.modelnormal3f = rsurface.array_modelnormal3f;
5771 model->AnimateVertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, NULL, NULL);
5775 rsurface.modelvertex3f = rsurface.array_modelvertex3f;
5776 rsurface.modelsvector3f = NULL;
5777 rsurface.modeltvector3f = NULL;
5778 rsurface.modelnormal3f = NULL;
5779 model->AnimateVertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, NULL, NULL, NULL);
5781 rsurface.modelvertex3f_bufferobject = 0;
5782 rsurface.modelvertex3f_bufferoffset = 0;
5783 rsurface.modelsvector3f_bufferobject = 0;
5784 rsurface.modelsvector3f_bufferoffset = 0;
5785 rsurface.modeltvector3f_bufferobject = 0;
5786 rsurface.modeltvector3f_bufferoffset = 0;
5787 rsurface.modelnormal3f_bufferobject = 0;
5788 rsurface.modelnormal3f_bufferoffset = 0;
5789 rsurface.generatedvertex = true;
5793 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
5794 rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
5795 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
5796 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
5797 rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
5798 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
5799 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
5800 rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
5801 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
5802 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
5803 rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
5804 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
5805 rsurface.generatedvertex = false;
5807 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
5808 rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
5809 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
5810 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
5811 rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
5812 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
5813 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
5814 rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
5815 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
5816 rsurface.modelelement3i = model->surfmesh.data_element3i;
5817 rsurface.modelelement3s = model->surfmesh.data_element3s;
5818 rsurface.modelelement3i_bufferobject = model->surfmesh.ebo3i;
5819 rsurface.modelelement3s_bufferobject = model->surfmesh.ebo3s;
5820 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
5821 rsurface.modelnum_vertices = model->surfmesh.num_vertices;
5822 rsurface.modelnum_triangles = model->surfmesh.num_triangles;
5823 rsurface.modelsurfaces = model->data_surfaces;
5824 rsurface.vertex3f = rsurface.modelvertex3f;
5825 rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
5826 rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
5827 rsurface.svector3f = rsurface.modelsvector3f;
5828 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
5829 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
5830 rsurface.tvector3f = rsurface.modeltvector3f;
5831 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
5832 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
5833 rsurface.normal3f = rsurface.modelnormal3f;
5834 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
5835 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
5836 rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
5839 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
5840 void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist)
5843 int texturesurfaceindex;
5848 const float *v1, *in_tc;
5850 float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
5852 q3shaderinfo_deform_t *deform;
5853 // 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
5854 if (rsurface.generatedvertex)
5856 if (rsurface.texture->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
5857 generatenormals = true;
5858 for (i = 0;i < Q3MAXDEFORMS;i++)
5860 if (rsurface.texture->deforms[i].deform == Q3DEFORM_AUTOSPRITE)
5862 generatetangents = true;
5863 generatenormals = true;
5865 if (rsurface.texture->deforms[i].deform != Q3DEFORM_NONE)
5866 generatenormals = true;
5868 if (generatenormals && !rsurface.modelnormal3f)
5870 rsurface.normal3f = rsurface.modelnormal3f = rsurface.array_modelnormal3f;
5871 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject = 0;
5872 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset = 0;
5873 Mod_BuildNormals(0, rsurface.modelnum_vertices, rsurface.modelnum_triangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.array_modelnormal3f, r_smoothnormals_areaweighting.integer != 0);
5875 if (generatetangents && !rsurface.modelsvector3f)
5877 rsurface.svector3f = rsurface.modelsvector3f = rsurface.array_modelsvector3f;
5878 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject = 0;
5879 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset = 0;
5880 rsurface.tvector3f = rsurface.modeltvector3f = rsurface.array_modeltvector3f;
5881 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject = 0;
5882 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset = 0;
5883 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 != 0);
5886 rsurface.vertex3f = rsurface.modelvertex3f;
5887 rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
5888 rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
5889 rsurface.svector3f = rsurface.modelsvector3f;
5890 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
5891 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
5892 rsurface.tvector3f = rsurface.modeltvector3f;
5893 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
5894 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
5895 rsurface.normal3f = rsurface.modelnormal3f;
5896 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
5897 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
5898 // if vertices are deformed (sprite flares and things in maps, possibly
5899 // water waves, bulges and other deformations), generate them into
5900 // rsurface.deform* arrays from whatever the rsurface.* arrays point to
5901 // (may be static model data or generated data for an animated model, or
5902 // the previous deform pass)
5903 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform;deformindex++, deform++)
5905 switch (deform->deform)
5908 case Q3DEFORM_PROJECTIONSHADOW:
5909 case Q3DEFORM_TEXT0:
5910 case Q3DEFORM_TEXT1:
5911 case Q3DEFORM_TEXT2:
5912 case Q3DEFORM_TEXT3:
5913 case Q3DEFORM_TEXT4:
5914 case Q3DEFORM_TEXT5:
5915 case Q3DEFORM_TEXT6:
5916 case Q3DEFORM_TEXT7:
5919 case Q3DEFORM_AUTOSPRITE:
5920 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
5921 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
5922 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
5923 VectorNormalize(newforward);
5924 VectorNormalize(newright);
5925 VectorNormalize(newup);
5926 // make deformed versions of only the model vertices used by the specified surfaces
5927 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5929 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5930 // a single autosprite surface can contain multiple sprites...
5931 for (j = 0;j < surface->num_vertices - 3;j += 4)
5933 VectorClear(center);
5934 for (i = 0;i < 4;i++)
5935 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
5936 VectorScale(center, 0.25f, center);
5937 VectorCopy((rsurface.normal3f + 3 * surface->num_firstvertex) + j*3, forward);
5938 VectorCopy((rsurface.svector3f + 3 * surface->num_firstvertex) + j*3, right);
5939 VectorCopy((rsurface.tvector3f + 3 * surface->num_firstvertex) + j*3, up);
5940 for (i = 0;i < 4;i++)
5942 VectorSubtract((rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i)*3, center, v);
5943 VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
5946 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 != 0);
5947 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 != 0);
5949 rsurface.vertex3f = rsurface.array_deformedvertex3f;
5950 rsurface.vertex3f_bufferobject = 0;
5951 rsurface.vertex3f_bufferoffset = 0;
5952 rsurface.svector3f = rsurface.array_deformedsvector3f;
5953 rsurface.svector3f_bufferobject = 0;
5954 rsurface.svector3f_bufferoffset = 0;
5955 rsurface.tvector3f = rsurface.array_deformedtvector3f;
5956 rsurface.tvector3f_bufferobject = 0;
5957 rsurface.tvector3f_bufferoffset = 0;
5958 rsurface.normal3f = rsurface.array_deformednormal3f;
5959 rsurface.normal3f_bufferobject = 0;
5960 rsurface.normal3f_bufferoffset = 0;
5962 case Q3DEFORM_AUTOSPRITE2:
5963 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
5964 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
5965 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
5966 VectorNormalize(newforward);
5967 VectorNormalize(newright);
5968 VectorNormalize(newup);
5969 // make deformed versions of only the model vertices used by the specified surfaces
5970 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5972 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5973 const float *v1, *v2;
5983 memset(shortest, 0, sizeof(shortest));
5984 // a single autosprite surface can contain multiple sprites...
5985 for (j = 0;j < surface->num_vertices - 3;j += 4)
5987 VectorClear(center);
5988 for (i = 0;i < 4;i++)
5989 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
5990 VectorScale(center, 0.25f, center);
5991 // find the two shortest edges, then use them to define the
5992 // axis vectors for rotating around the central axis
5993 for (i = 0;i < 6;i++)
5995 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][0]);
5996 v2 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][1]);
5998 Debug_PolygonBegin(NULL, 0);
5999 Debug_PolygonVertex(v1[0], v1[1], v1[2], 0, 0, 1, 0, 0, 1);
6000 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);
6001 Debug_PolygonVertex(v2[0], v2[1], v2[2], 0, 0, 1, 0, 0, 1);
6004 l = VectorDistance2(v1, v2);
6005 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
6007 l += (1.0f / 1024.0f);
6008 if (shortest[0].length2 > l || i == 0)
6010 shortest[1] = shortest[0];
6011 shortest[0].length2 = l;
6012 shortest[0].v1 = v1;
6013 shortest[0].v2 = v2;
6015 else if (shortest[1].length2 > l || i == 1)
6017 shortest[1].length2 = l;
6018 shortest[1].v1 = v1;
6019 shortest[1].v2 = v2;
6022 VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
6023 VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
6025 Debug_PolygonBegin(NULL, 0);
6026 Debug_PolygonVertex(start[0], start[1], start[2], 0, 0, 1, 1, 0, 1);
6027 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);
6028 Debug_PolygonVertex(end[0], end[1], end[2], 0, 0, 0, 1, 1, 1);
6031 // this calculates the right vector from the shortest edge
6032 // and the up vector from the edge midpoints
6033 VectorSubtract(shortest[0].v1, shortest[0].v2, right);
6034 VectorNormalize(right);
6035 VectorSubtract(end, start, up);
6036 VectorNormalize(up);
6037 // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
6038 VectorSubtract(rsurface.modelorg, center, forward);
6039 //Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, forward);
6040 VectorNegate(forward, forward);
6041 VectorReflect(forward, 0, up, forward);
6042 VectorNormalize(forward);
6043 CrossProduct(up, forward, newright);
6044 VectorNormalize(newright);
6046 Debug_PolygonBegin(NULL, 0);
6047 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);
6048 Debug_PolygonVertex(center[0] + right[0] * 8, center[1] + right[1] * 8, center[2] + right[2] * 8, 0, 0, 0, 1, 0, 1);
6049 Debug_PolygonVertex(center[0] + up [0] * 8, center[1] + up [1] * 8, center[2] + up [2] * 8, 0, 0, 0, 0, 1, 1);
6053 Debug_PolygonBegin(NULL, 0);
6054 Debug_PolygonVertex(center[0] + forward [0] * 8, center[1] + forward [1] * 8, center[2] + forward [2] * 8, 0, 0, 1, 0, 0, 1);
6055 Debug_PolygonVertex(center[0] + newright[0] * 8, center[1] + newright[1] * 8, center[2] + newright[2] * 8, 0, 0, 0, 1, 0, 1);
6056 Debug_PolygonVertex(center[0] + up [0] * 8, center[1] + up [1] * 8, center[2] + up [2] * 8, 0, 0, 0, 0, 1, 1);
6059 // rotate the quad around the up axis vector, this is made
6060 // especially easy by the fact we know the quad is flat,
6061 // so we only have to subtract the center position and
6062 // measure distance along the right vector, and then
6063 // multiply that by the newright vector and add back the
6065 // we also need to subtract the old position to undo the
6066 // displacement from the center, which we do with a
6067 // DotProduct, the subtraction/addition of center is also
6068 // optimized into DotProducts here
6069 l = DotProduct(right, center);
6070 for (i = 0;i < 4;i++)
6072 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + j + i);
6073 f = DotProduct(right, v1) - l;
6074 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
6077 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 != 0);
6078 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 != 0);
6080 rsurface.vertex3f = rsurface.array_deformedvertex3f;
6081 rsurface.vertex3f_bufferobject = 0;
6082 rsurface.vertex3f_bufferoffset = 0;
6083 rsurface.svector3f = rsurface.array_deformedsvector3f;
6084 rsurface.svector3f_bufferobject = 0;
6085 rsurface.svector3f_bufferoffset = 0;
6086 rsurface.tvector3f = rsurface.array_deformedtvector3f;
6087 rsurface.tvector3f_bufferobject = 0;
6088 rsurface.tvector3f_bufferoffset = 0;
6089 rsurface.normal3f = rsurface.array_deformednormal3f;
6090 rsurface.normal3f_bufferobject = 0;
6091 rsurface.normal3f_bufferoffset = 0;
6093 case Q3DEFORM_NORMAL:
6094 // deform the normals to make reflections wavey
6095 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6097 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6098 for (j = 0;j < surface->num_vertices;j++)
6101 float *normal = (rsurface.array_deformednormal3f + 3 * surface->num_firstvertex) + j*3;
6102 VectorScale((rsurface.vertex3f + 3 * surface->num_firstvertex) + j*3, 0.98f, vertex);
6103 VectorCopy((rsurface.normal3f + 3 * surface->num_firstvertex) + j*3, normal);
6104 normal[0] += deform->parms[0] * noise4f( vertex[0], vertex[1], vertex[2], r_refdef.scene.time * deform->parms[1]);
6105 normal[1] += deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], r_refdef.scene.time * deform->parms[1]);
6106 normal[2] += deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], r_refdef.scene.time * deform->parms[1]);
6107 VectorNormalize(normal);
6109 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 != 0);
6111 rsurface.svector3f = rsurface.array_deformedsvector3f;
6112 rsurface.svector3f_bufferobject = 0;
6113 rsurface.svector3f_bufferoffset = 0;
6114 rsurface.tvector3f = rsurface.array_deformedtvector3f;
6115 rsurface.tvector3f_bufferobject = 0;
6116 rsurface.tvector3f_bufferoffset = 0;
6117 rsurface.normal3f = rsurface.array_deformednormal3f;
6118 rsurface.normal3f_bufferobject = 0;
6119 rsurface.normal3f_bufferoffset = 0;
6122 // deform vertex array to make wavey water and flags and such
6123 waveparms[0] = deform->waveparms[0];
6124 waveparms[1] = deform->waveparms[1];
6125 waveparms[2] = deform->waveparms[2];
6126 waveparms[3] = deform->waveparms[3];
6127 // this is how a divisor of vertex influence on deformation
6128 animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
6129 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
6130 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6132 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6133 for (j = 0;j < surface->num_vertices;j++)
6135 float *vertex = (rsurface.array_deformedvertex3f + 3 * surface->num_firstvertex) + j*3;
6136 VectorCopy((rsurface.vertex3f + 3 * surface->num_firstvertex) + j*3, vertex);
6137 // if the wavefunc depends on time, evaluate it per-vertex
6140 waveparms[2] = deform->waveparms[2] + (vertex[0] + vertex[1] + vertex[2]) * animpos;
6141 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
6143 VectorMA(vertex, scale, (rsurface.normal3f + 3 * surface->num_firstvertex) + j*3, vertex);
6146 rsurface.vertex3f = rsurface.array_deformedvertex3f;
6147 rsurface.vertex3f_bufferobject = 0;
6148 rsurface.vertex3f_bufferoffset = 0;
6150 case Q3DEFORM_BULGE:
6151 // deform vertex array to make the surface have moving bulges
6152 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6154 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6155 for (j = 0;j < surface->num_vertices;j++)
6157 scale = sin((rsurface.modeltexcoordtexture2f[2 * (surface->num_firstvertex + j)] * deform->parms[0] + r_refdef.scene.time * deform->parms[2])) * deform->parms[1];
6158 VectorMA(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), scale, rsurface.normal3f + 3 * (surface->num_firstvertex + j), rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
6161 rsurface.vertex3f = rsurface.array_deformedvertex3f;
6162 rsurface.vertex3f_bufferobject = 0;
6163 rsurface.vertex3f_bufferoffset = 0;
6166 // deform vertex array
6167 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
6168 VectorScale(deform->parms, scale, waveparms);
6169 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6171 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6172 for (j = 0;j < surface->num_vertices;j++)
6173 VectorAdd(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), waveparms, rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
6175 rsurface.vertex3f = rsurface.array_deformedvertex3f;
6176 rsurface.vertex3f_bufferobject = 0;
6177 rsurface.vertex3f_bufferoffset = 0;
6181 // generate texcoords based on the chosen texcoord source
6182 switch(rsurface.texture->tcgen.tcgen)
6185 case Q3TCGEN_TEXTURE:
6186 rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
6187 rsurface.texcoordtexture2f_bufferobject = rsurface.modeltexcoordtexture2f_bufferobject;
6188 rsurface.texcoordtexture2f_bufferoffset = rsurface.modeltexcoordtexture2f_bufferoffset;
6190 case Q3TCGEN_LIGHTMAP:
6191 rsurface.texcoordtexture2f = rsurface.modeltexcoordlightmap2f;
6192 rsurface.texcoordtexture2f_bufferobject = rsurface.modeltexcoordlightmap2f_bufferobject;
6193 rsurface.texcoordtexture2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
6195 case Q3TCGEN_VECTOR:
6196 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6198 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6199 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)
6201 out_tc[0] = DotProduct(v1, rsurface.texture->tcgen.parms);
6202 out_tc[1] = DotProduct(v1, rsurface.texture->tcgen.parms + 3);
6205 rsurface.texcoordtexture2f = rsurface.array_generatedtexcoordtexture2f;
6206 rsurface.texcoordtexture2f_bufferobject = 0;
6207 rsurface.texcoordtexture2f_bufferoffset = 0;
6209 case Q3TCGEN_ENVIRONMENT:
6210 // make environment reflections using a spheremap
6211 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6213 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6214 const float *vertex = rsurface.modelvertex3f + 3 * surface->num_firstvertex;
6215 const float *normal = rsurface.modelnormal3f + 3 * surface->num_firstvertex;
6216 float *out_tc = rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex;
6217 for (j = 0;j < surface->num_vertices;j++, vertex += 3, normal += 3, out_tc += 2)
6219 // identical to Q3A's method, but executed in worldspace so
6220 // carried models can be shiny too
6222 float viewer[3], d, reflected[3], worldreflected[3];
6224 VectorSubtract(rsurface.modelorg, vertex, viewer);
6225 // VectorNormalize(viewer);
6227 d = DotProduct(normal, viewer);
6229 reflected[0] = normal[0]*2*d - viewer[0];
6230 reflected[1] = normal[1]*2*d - viewer[1];
6231 reflected[2] = normal[2]*2*d - viewer[2];
6232 // note: this is proportinal to viewer, so we can normalize later
6234 Matrix4x4_Transform3x3(&rsurface.matrix, reflected, worldreflected);
6235 VectorNormalize(worldreflected);
6237 // note: this sphere map only uses world x and z!
6238 // so positive and negative y will LOOK THE SAME.
6239 out_tc[0] = 0.5 + 0.5 * worldreflected[1];
6240 out_tc[1] = 0.5 - 0.5 * worldreflected[2];
6243 rsurface.texcoordtexture2f = rsurface.array_generatedtexcoordtexture2f;
6244 rsurface.texcoordtexture2f_bufferobject = 0;
6245 rsurface.texcoordtexture2f_bufferoffset = 0;
6248 // the only tcmod that needs software vertex processing is turbulent, so
6249 // check for it here and apply the changes if needed
6250 // and we only support that as the first one
6251 // (handling a mixture of turbulent and other tcmods would be problematic
6252 // without punting it entirely to a software path)
6253 if (rsurface.texture->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
6255 amplitude = rsurface.texture->tcmods[0].parms[1];
6256 animpos = rsurface.texture->tcmods[0].parms[2] + r_refdef.scene.time * rsurface.texture->tcmods[0].parms[3];
6257 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6259 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6260 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)
6262 out_tc[0] = in_tc[0] + amplitude * sin(((v1[0] + v1[2]) * 1.0 / 1024.0f + animpos) * M_PI * 2);
6263 out_tc[1] = in_tc[1] + amplitude * sin(((v1[1] ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
6266 rsurface.texcoordtexture2f = rsurface.array_generatedtexcoordtexture2f;
6267 rsurface.texcoordtexture2f_bufferobject = 0;
6268 rsurface.texcoordtexture2f_bufferoffset = 0;
6270 rsurface.texcoordlightmap2f = rsurface.modeltexcoordlightmap2f;
6271 rsurface.texcoordlightmap2f_bufferobject = rsurface.modeltexcoordlightmap2f_bufferobject;
6272 rsurface.texcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
6273 R_Mesh_VertexPointer(rsurface.vertex3f, rsurface.vertex3f_bufferobject, rsurface.vertex3f_bufferoffset);
6276 void RSurf_DrawBatch_Simple(int texturenumsurfaces, msurface_t **texturesurfacelist)
6279 const msurface_t *surface = texturesurfacelist[0];
6280 const msurface_t *surface2;
6285 // TODO: lock all array ranges before render, rather than on each surface
6286 if (texturenumsurfaces == 1)
6288 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
6289 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6291 else if (r_batchmode.integer == 2)
6293 #define MAXBATCHTRIANGLES 4096
6294 int batchtriangles = 0;
6295 int batchelements[MAXBATCHTRIANGLES*3];
6296 for (i = 0;i < texturenumsurfaces;i = j)
6298 surface = texturesurfacelist[i];
6300 if (surface->num_triangles > MAXBATCHTRIANGLES)
6302 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6305 memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
6306 batchtriangles = surface->num_triangles;
6307 firstvertex = surface->num_firstvertex;
6308 endvertex = surface->num_firstvertex + surface->num_vertices;
6309 for (;j < texturenumsurfaces;j++)
6311 surface2 = texturesurfacelist[j];
6312 if (batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
6314 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
6315 batchtriangles += surface2->num_triangles;
6316 firstvertex = min(firstvertex, surface2->num_firstvertex);
6317 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
6319 surface2 = texturesurfacelist[j-1];
6320 numvertices = endvertex - firstvertex;
6321 R_Mesh_Draw(firstvertex, numvertices, 0, batchtriangles, batchelements, NULL, 0, 0);
6324 else if (r_batchmode.integer == 1)
6326 for (i = 0;i < texturenumsurfaces;i = j)
6328 surface = texturesurfacelist[i];
6329 for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
6330 if (texturesurfacelist[j] != surface2)
6332 surface2 = texturesurfacelist[j-1];
6333 numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
6334 numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
6335 GL_LockArrays(surface->num_firstvertex, numvertices);
6336 R_Mesh_Draw(surface->num_firstvertex, numvertices, surface->num_firsttriangle, numtriangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6341 for (i = 0;i < texturenumsurfaces;i++)
6343 surface = texturesurfacelist[i];
6344 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
6345 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6350 static void RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit, int refractiontexunit, int reflectiontexunit)
6352 int i, planeindex, vertexindex;
6356 r_waterstate_waterplane_t *p, *bestp;
6357 msurface_t *surface;
6358 if (r_waterstate.renderingscene)
6360 for (i = 0;i < texturenumsurfaces;i++)
6362 surface = texturesurfacelist[i];
6363 if (lightmaptexunit >= 0)
6364 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
6365 if (deluxemaptexunit >= 0)
6366 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
6367 // pick the closest matching water plane
6370 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
6373 for (vertexindex = 0, v = rsurface.modelvertex3f + surface->num_firstvertex * 3;vertexindex < surface->num_vertices;vertexindex++, v += 3)
6375 Matrix4x4_Transform(&rsurface.matrix, v, vert);
6376 d += fabs(PlaneDiff(vert, &p->plane));
6378 if (bestd > d || !bestp)
6386 if (refractiontexunit >= 0)
6387 R_Mesh_TexBind(refractiontexunit, R_GetTexture(bestp->texture_refraction));
6388 if (reflectiontexunit >= 0)
6389 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(bestp->texture_reflection));
6393 if (refractiontexunit >= 0)
6394 R_Mesh_TexBind(refractiontexunit, R_GetTexture(r_texture_black));
6395 if (reflectiontexunit >= 0)
6396 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(r_texture_black));
6398 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
6399 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6403 static void RSurf_DrawBatch_WithLightmapSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit)
6407 const msurface_t *surface = texturesurfacelist[0];
6408 const msurface_t *surface2;
6413 // TODO: lock all array ranges before render, rather than on each surface
6414 if (texturenumsurfaces == 1)
6416 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
6417 if (deluxemaptexunit >= 0)
6418 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
6419 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
6420 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6422 else if (r_batchmode.integer == 2)
6424 #define MAXBATCHTRIANGLES 4096
6425 int batchtriangles = 0;
6426 int batchelements[MAXBATCHTRIANGLES*3];
6427 for (i = 0;i < texturenumsurfaces;i = j)
6429 surface = texturesurfacelist[i];
6430 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
6431 if (deluxemaptexunit >= 0)
6432 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
6434 if (surface->num_triangles > MAXBATCHTRIANGLES)
6436 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6439 memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
6440 batchtriangles = surface->num_triangles;
6441 firstvertex = surface->num_firstvertex;
6442 endvertex = surface->num_firstvertex + surface->num_vertices;
6443 for (;j < texturenumsurfaces;j++)
6445 surface2 = texturesurfacelist[j];
6446 if (surface2->lightmaptexture != surface->lightmaptexture || batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
6448 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
6449 batchtriangles += surface2->num_triangles;
6450 firstvertex = min(firstvertex, surface2->num_firstvertex);
6451 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
6453 surface2 = texturesurfacelist[j-1];
6454 numvertices = endvertex - firstvertex;
6455 R_Mesh_Draw(firstvertex, numvertices, 0, batchtriangles, batchelements, NULL, 0, 0);
6458 else if (r_batchmode.integer == 1)
6461 Con_Printf("%s batch sizes ignoring lightmap:", rsurface.texture->name);
6462 for (i = 0;i < texturenumsurfaces;i = j)
6464 surface = texturesurfacelist[i];
6465 for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
6466 if (texturesurfacelist[j] != surface2)
6468 Con_Printf(" %i", j - i);
6471 Con_Printf("%s batch sizes honoring lightmap:", rsurface.texture->name);
6473 for (i = 0;i < texturenumsurfaces;i = j)
6475 surface = texturesurfacelist[i];
6476 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
6477 if (deluxemaptexunit >= 0)
6478 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
6479 for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
6480 if (texturesurfacelist[j] != surface2 || texturesurfacelist[j]->lightmaptexture != surface->lightmaptexture)
6483 Con_Printf(" %i", j - i);
6485 surface2 = texturesurfacelist[j-1];
6486 numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
6487 numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
6488 GL_LockArrays(surface->num_firstvertex, numvertices);
6489 R_Mesh_Draw(surface->num_firstvertex, numvertices, surface->num_firsttriangle, numtriangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6497 for (i = 0;i < texturenumsurfaces;i++)
6499 surface = texturesurfacelist[i];
6500 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
6501 if (deluxemaptexunit >= 0)
6502 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
6503 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
6504 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6509 static void RSurf_DrawBatch_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
6512 int texturesurfaceindex;
6513 if (r_showsurfaces.integer == 2)
6515 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6517 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6518 for (j = 0;j < surface->num_triangles;j++)
6520 float f = ((j + surface->num_firsttriangle) & 31) * (1.0f / 31.0f) * r_refdef.view.colorscale;
6521 GL_Color(f, f, f, 1);
6522 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle + j, 1, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6528 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6530 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6531 int k = (int)(((size_t)surface) / sizeof(msurface_t));
6532 GL_Color((k & 15) * (1.0f / 16.0f) * r_refdef.view.colorscale, ((k >> 4) & 15) * (1.0f / 16.0f) * r_refdef.view.colorscale, ((k >> 8) & 15) * (1.0f / 16.0f) * r_refdef.view.colorscale, 1);
6533 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
6534 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
6539 static void RSurf_DrawBatch_GL11_MakeFullbrightLightmapColorArray(int texturenumsurfaces, msurface_t **texturesurfacelist)
6541 int texturesurfaceindex;
6544 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6546 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6547 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)
6555 rsurface.lightmapcolor4f = rsurface.array_color4f;
6556 rsurface.lightmapcolor4f_bufferobject = 0;
6557 rsurface.lightmapcolor4f_bufferoffset = 0;
6560 static void RSurf_DrawBatch_GL11_ApplyFog(int texturenumsurfaces, msurface_t **texturesurfacelist)
6562 int texturesurfaceindex;
6566 if (rsurface.lightmapcolor4f)
6568 // generate color arrays for the surfaces in this list
6569 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6571 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6572 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)
6574 f = FogPoint_Model(v);
6584 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6586 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6587 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)
6589 f = FogPoint_Model(v);
6597 rsurface.lightmapcolor4f = rsurface.array_color4f;
6598 rsurface.lightmapcolor4f_bufferobject = 0;
6599 rsurface.lightmapcolor4f_bufferoffset = 0;
6602 static void RSurf_DrawBatch_GL11_ApplyFogToFinishedVertexColors(int texturenumsurfaces, msurface_t **texturesurfacelist)
6604 int texturesurfaceindex;
6608 if (!rsurface.lightmapcolor4f)
6610 // generate color arrays for the surfaces in this list
6611 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6613 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6614 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)
6616 f = FogPoint_Model(v);
6617 c2[0] = c[0] * f + r_refdef.fogcolor[0] * (1 - f);
6618 c2[1] = c[1] * f + r_refdef.fogcolor[1] * (1 - f);
6619 c2[2] = c[2] * f + r_refdef.fogcolor[2] * (1 - f);
6623 rsurface.lightmapcolor4f = rsurface.array_color4f;
6624 rsurface.lightmapcolor4f_bufferobject = 0;
6625 rsurface.lightmapcolor4f_bufferoffset = 0;
6628 static void RSurf_DrawBatch_GL11_ApplyColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a)
6630 int texturesurfaceindex;
6633 if (!rsurface.lightmapcolor4f)
6635 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6637 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6638 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)
6646 rsurface.lightmapcolor4f = rsurface.array_color4f;
6647 rsurface.lightmapcolor4f_bufferobject = 0;
6648 rsurface.lightmapcolor4f_bufferoffset = 0;
6651 static void RSurf_DrawBatch_GL11_ApplyAmbient(int texturenumsurfaces, msurface_t **texturesurfacelist)
6653 int texturesurfaceindex;
6656 if (!rsurface.lightmapcolor4f)
6658 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6660 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6661 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)
6663 c2[0] = c[0] + r_refdef.scene.ambient / 128.0;
6664 c2[1] = c[1] + r_refdef.scene.ambient / 128.0;
6665 c2[2] = c[2] + r_refdef.scene.ambient / 128.0;
6669 rsurface.lightmapcolor4f = rsurface.array_color4f;
6670 rsurface.lightmapcolor4f_bufferobject = 0;
6671 rsurface.lightmapcolor4f_bufferoffset = 0;
6674 static void RSurf_DrawBatch_GL11_Lightmap(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
6677 rsurface.lightmapcolor4f = NULL;
6678 rsurface.lightmapcolor4f_bufferobject = 0;
6679 rsurface.lightmapcolor4f_bufferoffset = 0;
6680 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
6681 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
6682 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
6683 GL_Color(r, g, b, a);
6684 RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 0, -1);
6687 static void RSurf_DrawBatch_GL11_Unlit(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
6689 // TODO: optimize applyfog && applycolor case
6690 // just apply fog if necessary, and tint the fog color array if necessary
6691 rsurface.lightmapcolor4f = NULL;
6692 rsurface.lightmapcolor4f_bufferobject = 0;
6693 rsurface.lightmapcolor4f_bufferoffset = 0;
6694 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
6695 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
6696 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
6697 GL_Color(r, g, b, a);
6698 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
6701 static void RSurf_DrawBatch_GL11_VertexColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
6703 int texturesurfaceindex;
6707 if (texturesurfacelist[0]->lightmapinfo)
6709 // generate color arrays for the surfaces in this list
6710 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6712 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6713 for (i = 0, c = rsurface.array_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
6715 if (surface->lightmapinfo->samples)
6717 const unsigned char *lm = surface->lightmapinfo->samples + (rsurface.modellightmapoffsets + surface->num_firstvertex)[i];
6718 float scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
6719 VectorScale(lm, scale, c);
6720 if (surface->lightmapinfo->styles[1] != 255)
6722 int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
6724 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
6725 VectorMA(c, scale, lm, c);
6726 if (surface->lightmapinfo->styles[2] != 255)
6729 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
6730 VectorMA(c, scale, lm, c);
6731 if (surface->lightmapinfo->styles[3] != 255)
6734 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
6735 VectorMA(c, scale, lm, c);
6745 rsurface.lightmapcolor4f = rsurface.array_color4f;
6746 rsurface.lightmapcolor4f_bufferobject = 0;
6747 rsurface.lightmapcolor4f_bufferoffset = 0;
6751 rsurface.lightmapcolor4f = rsurface.modellightmapcolor4f;
6752 rsurface.lightmapcolor4f_bufferobject = rsurface.modellightmapcolor4f_bufferobject;
6753 rsurface.lightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
6755 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
6756 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
6757 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
6758 GL_Color(r, g, b, a);
6759 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
6762 static void RSurf_DrawBatch_GL11_ApplyVertexShade(int texturenumsurfaces, msurface_t **texturesurfacelist, float *r, float *g, float *b, float *a, qboolean *applycolor)
6764 int texturesurfaceindex;
6767 float *v, *c, *c2, alpha;
6768 vec3_t ambientcolor;
6769 vec3_t diffusecolor;
6773 VectorCopy(rsurface.modellight_lightdir, lightdir);
6774 f = 0.5f * r_refdef.lightmapintensity;
6775 ambientcolor[0] = rsurface.modellight_ambient[0] * *r * f;
6776 ambientcolor[1] = rsurface.modellight_ambient[1] * *g * f;
6777 ambientcolor[2] = rsurface.modellight_ambient[2] * *b * f;
6778 diffusecolor[0] = rsurface.modellight_diffuse[0] * *r * f;
6779 diffusecolor[1] = rsurface.modellight_diffuse[1] * *g * f;
6780 diffusecolor[2] = rsurface.modellight_diffuse[2] * *b * f;
6782 if (VectorLength2(diffusecolor) > 0 && rsurface.normal3f)
6784 // generate color arrays for the surfaces in this list
6785 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
6787 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
6788 int numverts = surface->num_vertices;
6789 v = rsurface.vertex3f + 3 * surface->num_firstvertex;
6790 c2 = rsurface.normal3f + 3 * surface->num_firstvertex;
6791 c = rsurface.array_color4f + 4 * surface->num_firstvertex;
6792 // q3-style directional shading
6793 for (i = 0;i < numverts;i++, v += 3, c2 += 3, c += 4)
6795 if ((f = DotProduct(c2, lightdir)) > 0)
6796 VectorMA(ambientcolor, f, diffusecolor, c);
6798 VectorCopy(ambientcolor, c);
6806 rsurface.lightmapcolor4f = rsurface.array_color4f;
6807 rsurface.lightmapcolor4f_bufferobject = 0;
6808 rsurface.lightmapcolor4f_bufferoffset = 0;
6809 *applycolor = false;
6813 *r = ambientcolor[0];
6814 *g = ambientcolor[1];
6815 *b = ambientcolor[2];
6816 rsurface.lightmapcolor4f = NULL;
6817 rsurface.lightmapcolor4f_bufferobject = 0;
6818 rsurface.lightmapcolor4f_bufferoffset = 0;
6822 static void RSurf_DrawBatch_GL11_VertexShade(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
6824 RSurf_DrawBatch_GL11_ApplyVertexShade(texturenumsurfaces, texturesurfacelist, &r, &g, &b, &a, &applycolor);
6825 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
6826 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
6827 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
6828 GL_Color(r, g, b, a);
6829 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
6832 void RSurf_SetupDepthAndCulling(void)
6834 // submodels are biased to avoid z-fighting with world surfaces that they
6835 // may be exactly overlapping (avoids z-fighting artifacts on certain
6836 // doors and things in Quake maps)
6837 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
6838 GL_PolygonOffset(rsurface.basepolygonfactor + rsurface.texture->biaspolygonfactor, rsurface.basepolygonoffset + rsurface.texture->biaspolygonoffset);
6839 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
6840 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
6843 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, msurface_t **texturesurfacelist)
6845 // transparent sky would be ridiculous
6846 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
6848 R_SetupGenericShader(false);
6851 skyrendernow = false;
6852 // we have to force off the water clipping plane while rendering sky
6856 // restore entity matrix
6857 R_Mesh_Matrix(&rsurface.matrix);
6859 RSurf_SetupDepthAndCulling();
6861 // LordHavoc: HalfLife maps have freaky skypolys so don't use
6862 // skymasking on them, and Quake3 never did sky masking (unlike
6863 // software Quake and software Quake2), so disable the sky masking
6864 // in Quake3 maps as it causes problems with q3map2 sky tricks,
6865 // and skymasking also looks very bad when noclipping outside the
6866 // level, so don't use it then either.
6867 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_refdef.viewcache.world_novis)
6869 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1);
6870 R_Mesh_ColorPointer(NULL, 0, 0);
6871 R_Mesh_ResetTextureState();
6872 if (skyrendermasked)
6874 R_SetupDepthOrShadowShader();
6875 // depth-only (masking)
6876 GL_ColorMask(0,0,0,0);
6877 // just to make sure that braindead drivers don't draw
6878 // anything despite that colormask...
6879 GL_BlendFunc(GL_ZERO, GL_ONE);
6883 R_SetupGenericShader(false);
6885 GL_BlendFunc(GL_ONE, GL_ZERO);
6887 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
6888 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
6889 if (skyrendermasked)
6890 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
6892 R_Mesh_ResetTextureState();
6893 GL_Color(1, 1, 1, 1);
6896 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth)
6898 if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION)))
6901 R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
6902 R_Mesh_TexMatrix(1, &rsurface.texture->currentbackgroundtexmatrix);
6903 R_Mesh_TexBind(GL20TU_NORMAL, R_GetTexture(rsurface.texture->currentskinframe->nmap));
6904 R_Mesh_TexBind(GL20TU_COLOR, R_GetTexture(rsurface.texture->basetexture));
6905 R_Mesh_TexBind(GL20TU_GLOSS, R_GetTexture(rsurface.texture->glosstexture));
6906 R_Mesh_TexBind(GL20TU_GLOW, R_GetTexture(rsurface.texture->currentskinframe->glow));
6907 if (rsurface.texture->backgroundcurrentskinframe)
6909 R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->nmap));
6910 R_Mesh_TexBind(GL20TU_SECONDARY_COLOR, R_GetTexture(rsurface.texture->backgroundbasetexture));
6911 R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS, R_GetTexture(rsurface.texture->backgroundglosstexture));
6912 R_Mesh_TexBind(GL20TU_SECONDARY_GLOW, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->glow));
6914 if(rsurface.texture->colormapping)
6916 R_Mesh_TexBind(GL20TU_PANTS, R_GetTexture(rsurface.texture->currentskinframe->pants));
6917 R_Mesh_TexBind(GL20TU_SHIRT, R_GetTexture(rsurface.texture->currentskinframe->shirt));
6919 R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
6920 if ((rsurface.uselightmaptexture || (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)) && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND))
6921 R_Mesh_ColorPointer(NULL, 0, 0);
6923 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
6925 if (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
6927 // render background
6928 GL_BlendFunc(GL_ONE, GL_ZERO);
6930 GL_AlphaTest(false);
6932 GL_Color(1, 1, 1, 1);
6933 R_Mesh_ColorPointer(NULL, 0, 0);
6935 R_SetupSurfaceShader(vec3_origin, (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT) != 0, 1, 1, rsurface.texture->specularscale, RSURFPASS_BACKGROUND);
6936 if (r_glsl_permutation)
6938 RSurf_PrepareVerticesForBatch(true, true, texturenumsurfaces, texturesurfacelist);
6939 R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
6940 R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
6941 R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
6942 R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
6943 R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
6944 RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? GL20TU_REFRACTION : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? GL20TU_REFLECTION : -1);
6946 GL_LockArrays(0, 0);
6948 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
6949 GL_DepthMask(false);
6950 if ((rsurface.uselightmaptexture || (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)) && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND))
6951 R_Mesh_ColorPointer(NULL, 0, 0);
6953 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
6954 R_Mesh_TexBind(GL20TU_REFRACTION, R_GetTexture(r_texture_white)); // changed per surface
6955 R_Mesh_TexBind(GL20TU_REFLECTION, R_GetTexture(r_texture_white)); // changed per surface
6958 R_SetupSurfaceShader(vec3_origin, (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT) != 0, 1, 1, rsurface.texture->specularscale, RSURFPASS_BASE);
6959 if (!r_glsl_permutation)
6962 RSurf_PrepareVerticesForBatch(r_glsl_permutation->loc_Texture_Normal >= 0 || r_glsl_permutation->loc_LightDir >= 0, r_glsl_permutation->loc_Texture_Normal >= 0, texturenumsurfaces, texturesurfacelist);
6963 R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
6964 R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
6965 R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
6966 R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
6967 R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
6969 if (r_glsl_permutation->loc_Texture_Refraction >= 0)
6971 GL_BlendFunc(GL_ONE, GL_ZERO);
6973 GL_AlphaTest(false);
6977 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
6978 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
6979 GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
6982 if (rsurface.uselightmaptexture && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
6984 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
6985 RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, GL20TU_LIGHTMAP, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? GL20TU_DELUXEMAP : -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? GL20TU_REFRACTION : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? GL20TU_REFLECTION : -1);
6987 RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, GL20TU_LIGHTMAP, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? GL20TU_DELUXEMAP : -1);
6991 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
6992 RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? GL20TU_REFRACTION : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? GL20TU_REFLECTION : -1);
6994 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
6996 GL_LockArrays(0, 0);
6999 static void R_DrawTextureSurfaceList_GL13(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth)
7001 // OpenGL 1.3 path - anything not completely ancient
7002 int texturesurfaceindex;
7003 qboolean applycolor;
7007 const texturelayer_t *layer;
7008 RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
7010 for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
7013 int layertexrgbscale;
7014 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
7016 if (layerindex == 0)
7020 GL_AlphaTest(false);
7021 qglDepthFunc(GL_EQUAL);CHECKGLERROR
7024 GL_DepthMask(layer->depthmask && writedepth);
7025 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
7026 if (layer->color[0] > 2 || layer->color[1] > 2 || layer->color[2] > 2)
7028 layertexrgbscale = 4;
7029 VectorScale(layer->color, 0.25f, layercolor);
7031 else if (layer->color[0] > 1 || layer->color[1] > 1 || layer->color[2] > 1)
7033 layertexrgbscale = 2;
7034 VectorScale(layer->color, 0.5f, layercolor);
7038 layertexrgbscale = 1;
7039 VectorScale(layer->color, 1.0f, layercolor);
7041 layercolor[3] = layer->color[3];
7042 applycolor = layercolor[0] != 1 || layercolor[1] != 1 || layercolor[2] != 1 || layercolor[3] != 1;
7043 R_Mesh_ColorPointer(NULL, 0, 0);
7044 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
7045 switch (layer->type)
7047 case TEXTURELAYERTYPE_LITTEXTURE:
7048 memset(&m, 0, sizeof(m));
7049 m.tex[0] = R_GetTexture(r_texture_white);
7050 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
7051 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
7052 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
7053 m.tex[1] = R_GetTexture(layer->texture);
7054 m.texmatrix[1] = layer->texmatrix;
7055 m.texrgbscale[1] = layertexrgbscale;
7056 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
7057 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
7058 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
7059 R_Mesh_TextureState(&m);
7060 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
7061 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
7062 else if (rsurface.uselightmaptexture)
7063 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
7065 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
7067 case TEXTURELAYERTYPE_TEXTURE:
7068 memset(&m, 0, sizeof(m));
7069 m.tex[0] = R_GetTexture(layer->texture);
7070 m.texmatrix[0] = layer->texmatrix;
7071 m.texrgbscale[0] = layertexrgbscale;
7072 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
7073 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
7074 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
7075 R_Mesh_TextureState(&m);
7076 RSurf_DrawBatch_GL11_Unlit(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
7078 case TEXTURELAYERTYPE_FOG:
7079 memset(&m, 0, sizeof(m));
7080 m.texrgbscale[0] = layertexrgbscale;
7083 m.tex[0] = R_GetTexture(layer->texture);
7084 m.texmatrix[0] = layer->texmatrix;
7085 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
7086 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
7087 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
7089 R_Mesh_TextureState(&m);
7090 // generate a color array for the fog pass
7091 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
7092 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
7096 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
7097 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)
7099 f = 1 - FogPoint_Model(v);
7100 c[0] = layercolor[0];
7101 c[1] = layercolor[1];
7102 c[2] = layercolor[2];
7103 c[3] = f * layercolor[3];
7106 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
7109 Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
7111 GL_LockArrays(0, 0);
7114 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
7116 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
7117 GL_AlphaTest(false);
7121 static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth)
7123 // OpenGL 1.1 - crusty old voodoo path
7124 int texturesurfaceindex;
7128 const texturelayer_t *layer;
7129 RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
7131 for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
7133 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
7135 if (layerindex == 0)
7139 GL_AlphaTest(false);
7140 qglDepthFunc(GL_EQUAL);CHECKGLERROR
7143 GL_DepthMask(layer->depthmask && writedepth);
7144 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
7145 R_Mesh_ColorPointer(NULL, 0, 0);
7146 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
7147 switch (layer->type)
7149 case TEXTURELAYERTYPE_LITTEXTURE:
7150 if (layer->blendfunc1 == GL_ONE && layer->blendfunc2 == GL_ZERO)
7152 // two-pass lit texture with 2x rgbscale
7153 // first the lightmap pass
7154 memset(&m, 0, sizeof(m));
7155 m.tex[0] = R_GetTexture(r_texture_white);
7156 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
7157 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
7158 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
7159 R_Mesh_TextureState(&m);
7160 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
7161 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
7162 else if (rsurface.uselightmaptexture)
7163 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
7165 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
7166 GL_LockArrays(0, 0);
7167 // then apply the texture to it
7168 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
7169 memset(&m, 0, sizeof(m));
7170 m.tex[0] = R_GetTexture(layer->texture);
7171 m.texmatrix[0] = layer->texmatrix;
7172 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
7173 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
7174 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
7175 R_Mesh_TextureState(&m);
7176 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);
7180 // single pass vertex-lighting-only texture with 1x rgbscale and transparency support
7181 memset(&m, 0, sizeof(m));
7182 m.tex[0] = R_GetTexture(layer->texture);
7183 m.texmatrix[0] = layer->texmatrix;
7184 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
7185 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
7186 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
7187 R_Mesh_TextureState(&m);
7188 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
7189 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);
7191 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);
7194 case TEXTURELAYERTYPE_TEXTURE:
7195 // singletexture unlit texture with transparency support
7196 memset(&m, 0, sizeof(m));
7197 m.tex[0] = R_GetTexture(layer->texture);
7198 m.texmatrix[0] = layer->texmatrix;
7199 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
7200 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
7201 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
7202 R_Mesh_TextureState(&m);
7203 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);
7205 case TEXTURELAYERTYPE_FOG:
7206 // singletexture fogging
7207 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
7210 memset(&m, 0, sizeof(m));
7211 m.tex[0] = R_GetTexture(layer->texture);
7212 m.texmatrix[0] = layer->texmatrix;
7213 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
7214 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
7215 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
7216 R_Mesh_TextureState(&m);
7219 R_Mesh_ResetTextureState();
7220 // generate a color array for the fog pass
7221 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
7225 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
7226 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)
7228 f = 1 - FogPoint_Model(v);
7229 c[0] = layer->color[0];
7230 c[1] = layer->color[1];
7231 c[2] = layer->color[2];
7232 c[3] = f * layer->color[3];
7235 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
7238 Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
7240 GL_LockArrays(0, 0);
7243 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
7245 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
7246 GL_AlphaTest(false);
7250 static void R_DrawTextureSurfaceList_ShowSurfaces3(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth)
7254 GL_AlphaTest(false);
7255 R_Mesh_ColorPointer(NULL, 0, 0);
7256 R_Mesh_ResetTextureState();
7257 R_SetupGenericShader(false);
7259 if(rsurface.texture && rsurface.texture->currentskinframe)
7261 memcpy(c, rsurface.texture->currentskinframe->avgcolor, sizeof(c));
7262 c[3] *= rsurface.texture->currentalpha;
7272 if (rsurface.texture->currentskinframe->pants || rsurface.texture->currentskinframe->shirt)
7274 c[0] = 0.5 * (rsurface.colormap_pantscolor[0] * 0.3 + rsurface.colormap_shirtcolor[0] * 0.7);
7275 c[1] = 0.5 * (rsurface.colormap_pantscolor[1] * 0.3 + rsurface.colormap_shirtcolor[1] * 0.7);
7276 c[2] = 0.5 * (rsurface.colormap_pantscolor[2] * 0.3 + rsurface.colormap_shirtcolor[2] * 0.7);
7279 // brighten it up (as texture value 127 means "unlit")
7280 c[0] *= 2 * r_refdef.view.colorscale;
7281 c[1] *= 2 * r_refdef.view.colorscale;
7282 c[2] *= 2 * r_refdef.view.colorscale;
7284 if(rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERALPHA)
7285 c[3] *= r_wateralpha.value;
7287 if(rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHA && c[3] != 1)
7289 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
7290 GL_DepthMask(false);
7292 else if(rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD)
7294 GL_BlendFunc(GL_ONE, GL_ONE);
7295 GL_DepthMask(false);
7297 else if(rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
7299 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // can't do alpha test without texture, so let's blend instead
7300 GL_DepthMask(false);
7302 else if(rsurface.texture->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
7304 GL_BlendFunc(rsurface.texture->customblendfunc[0], rsurface.texture->customblendfunc[1]);
7305 GL_DepthMask(false);
7309 GL_BlendFunc(GL_ONE, GL_ZERO);
7310 GL_DepthMask(writedepth);
7313 rsurface.lightmapcolor4f = NULL;
7315 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
7317 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
7319 rsurface.lightmapcolor4f = NULL;
7320 rsurface.lightmapcolor4f_bufferobject = 0;
7321 rsurface.lightmapcolor4f_bufferoffset = 0;
7323 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
7325 qboolean applycolor = true;
7328 RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
7330 r_refdef.lightmapintensity = 1;
7331 RSurf_DrawBatch_GL11_ApplyVertexShade(texturenumsurfaces, texturesurfacelist, &one, &one, &one, &one, &applycolor);
7332 r_refdef.lightmapintensity = 0; // we're in showsurfaces, after all
7336 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
7338 rsurface.lightmapcolor4f = rsurface.modellightmapcolor4f;
7339 rsurface.lightmapcolor4f_bufferobject = rsurface.modellightmapcolor4f_bufferobject;
7340 rsurface.lightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
7343 if(!rsurface.lightmapcolor4f)
7344 RSurf_DrawBatch_GL11_MakeFullbrightLightmapColorArray(texturenumsurfaces, texturesurfacelist);
7346 RSurf_DrawBatch_GL11_ApplyAmbient(texturenumsurfaces, texturesurfacelist);
7347 RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, c[0], c[1], c[2], c[3]);
7348 if(r_refdef.fogenabled)
7349 RSurf_DrawBatch_GL11_ApplyFogToFinishedVertexColors(texturenumsurfaces, texturesurfacelist);
7351 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
7352 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
7355 static void R_DrawWorldTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth)
7358 RSurf_SetupDepthAndCulling();
7359 if (r_showsurfaces.integer == 3)
7360 R_DrawTextureSurfaceList_ShowSurfaces3(texturenumsurfaces, texturesurfacelist, writedepth);
7361 else if (r_glsl.integer && gl_support_fragment_shader)
7362 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist, writedepth);
7363 else if (gl_combine.integer && r_textureunits.integer >= 2)
7364 R_DrawTextureSurfaceList_GL13(texturenumsurfaces, texturesurfacelist, writedepth);
7366 R_DrawTextureSurfaceList_GL11(texturenumsurfaces, texturesurfacelist, writedepth);
7370 static void R_DrawModelTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth)
7373 RSurf_SetupDepthAndCulling();
7374 if (r_showsurfaces.integer == 3)
7375 R_DrawTextureSurfaceList_ShowSurfaces3(texturenumsurfaces, texturesurfacelist, writedepth);
7376 else if (r_glsl.integer && gl_support_fragment_shader)
7377 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist, writedepth);
7378 else if (gl_combine.integer && r_textureunits.integer >= 2)
7379 R_DrawTextureSurfaceList_GL13(texturenumsurfaces, texturesurfacelist, writedepth);
7381 R_DrawTextureSurfaceList_GL11(texturenumsurfaces, texturesurfacelist, writedepth);
7385 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
7388 int texturenumsurfaces, endsurface;
7390 msurface_t *surface;
7391 msurface_t *texturesurfacelist[1024];
7393 // if the model is static it doesn't matter what value we give for
7394 // wantnormals and wanttangents, so this logic uses only rules applicable
7395 // to a model, knowing that they are meaningless otherwise
7396 if (ent == r_refdef.scene.worldentity)
7397 RSurf_ActiveWorldEntity();
7398 else if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
7399 RSurf_ActiveModelEntity(ent, false, false);
7401 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader);
7403 for (i = 0;i < numsurfaces;i = j)
7406 surface = rsurface.modelsurfaces + surfacelist[i];
7407 texture = surface->texture;
7408 rsurface.texture = R_GetCurrentTexture(texture);
7409 rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
7410 // scan ahead until we find a different texture
7411 endsurface = min(i + 1024, numsurfaces);
7412 texturenumsurfaces = 0;
7413 texturesurfacelist[texturenumsurfaces++] = surface;
7414 for (;j < endsurface;j++)
7416 surface = rsurface.modelsurfaces + surfacelist[j];
7417 if (texture != surface->texture || rsurface.uselightmaptexture != (surface->lightmaptexture != NULL))
7419 texturesurfacelist[texturenumsurfaces++] = surface;
7421 // render the range of surfaces
7422 if (ent == r_refdef.scene.worldentity)
7423 R_DrawWorldTextureSurfaceList(texturenumsurfaces, texturesurfacelist, false);
7425 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, false);
7427 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
7428 GL_AlphaTest(false);
7431 static void R_ProcessWorldTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth, qboolean depthonly)
7433 const entity_render_t *queueentity = r_refdef.scene.worldentity;
7437 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
7439 if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
7441 RSurf_SetupDepthAndCulling();
7442 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
7443 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
7445 else if (r_showsurfaces.integer && !r_refdef.view.showdebug)
7447 RSurf_SetupDepthAndCulling();
7448 GL_AlphaTest(false);
7449 R_Mesh_ColorPointer(NULL, 0, 0);
7450 R_Mesh_ResetTextureState();
7451 R_SetupGenericShader(false);
7452 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
7454 GL_BlendFunc(GL_ONE, GL_ZERO);
7455 GL_Color(0, 0, 0, 1);
7456 GL_DepthTest(writedepth);
7457 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
7459 else if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
7461 RSurf_SetupDepthAndCulling();
7462 GL_AlphaTest(false);
7463 R_Mesh_ColorPointer(NULL, 0, 0);
7464 R_Mesh_ResetTextureState();
7465 R_SetupGenericShader(false);
7466 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
7468 GL_BlendFunc(GL_ONE, GL_ZERO);
7470 RSurf_DrawBatch_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
7472 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY)
7473 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
7474 else if (!rsurface.texture->currentnumlayers)
7476 else if (((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED) || (r_showsurfaces.integer == 3 && (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST))) && queueentity)
7478 // transparent surfaces get pushed off into the transparent queue
7479 int surfacelistindex;
7480 const msurface_t *surface;
7481 vec3_t tempcenter, center;
7482 for (surfacelistindex = 0;surfacelistindex < texturenumsurfaces;surfacelistindex++)
7484 surface = texturesurfacelist[surfacelistindex];
7485 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
7486 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
7487 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
7488 Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
7489 R_MeshQueue_AddTransparent(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_refdef.view.origin : center, R_DrawSurface_TransparentCallback, queueentity, surface - rsurface.modelsurfaces, rsurface.rtlight);
7494 // the alphatest check is to make sure we write depth for anything we skipped on the depth-only pass earlier
7495 R_DrawWorldTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST));
7500 void R_QueueWorldSurfaceList(int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly)
7504 // break the surface list down into batches by texture and use of lightmapping
7505 for (i = 0;i < numsurfaces;i = j)
7508 // texture is the base texture pointer, rsurface.texture is the
7509 // current frame/skin the texture is directing us to use (for example
7510 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
7511 // use skin 1 instead)
7512 texture = surfacelist[i]->texture;
7513 rsurface.texture = R_GetCurrentTexture(texture);
7514 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
7515 if (!(rsurface.texture->currentmaterialflags & flagsmask) || (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW))
7517 // if this texture is not the kind we want, skip ahead to the next one
7518 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
7522 // simply scan ahead until we find a different texture or lightmap state
7523 for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.uselightmaptexture == (surfacelist[j]->lightmaptexture != NULL);j++)
7525 // render the range of surfaces
7526 R_ProcessWorldTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly);
7530 static void R_ProcessModelTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth, qboolean depthonly, const entity_render_t *queueentity)
7535 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
7537 if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
7539 RSurf_SetupDepthAndCulling();
7540 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
7541 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
7543 else if (r_showsurfaces.integer && !r_refdef.view.showdebug)
7545 RSurf_SetupDepthAndCulling();
7546 GL_AlphaTest(false);
7547 R_Mesh_ColorPointer(NULL, 0, 0);
7548 R_Mesh_ResetTextureState();
7549 R_SetupGenericShader(false);
7550 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
7552 GL_BlendFunc(GL_ONE, GL_ZERO);
7553 GL_Color(0, 0, 0, 1);
7554 GL_DepthTest(writedepth);
7555 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
7557 else if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
7559 RSurf_SetupDepthAndCulling();
7560 GL_AlphaTest(false);
7561 R_Mesh_ColorPointer(NULL, 0, 0);
7562 R_Mesh_ResetTextureState();
7563 R_SetupGenericShader(false);
7564 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
7566 GL_BlendFunc(GL_ONE, GL_ZERO);
7568 RSurf_DrawBatch_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
7570 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY)
7571 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
7572 else if (!rsurface.texture->currentnumlayers)
7574 else if (((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED) || (r_showsurfaces.integer == 3 && (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST))) && queueentity)
7576 // transparent surfaces get pushed off into the transparent queue
7577 int surfacelistindex;
7578 const msurface_t *surface;
7579 vec3_t tempcenter, center;
7580 for (surfacelistindex = 0;surfacelistindex < texturenumsurfaces;surfacelistindex++)
7582 surface = texturesurfacelist[surfacelistindex];
7583 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
7584 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
7585 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
7586 Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
7587 if (queueentity->transparent_offset) // transparent offset
7589 center[0] += r_refdef.view.forward[0]*queueentity->transparent_offset;
7590 center[1] += r_refdef.view.forward[1]*queueentity->transparent_offset;
7591 center[2] += r_refdef.view.forward[2]*queueentity->transparent_offset;
7593 R_MeshQueue_AddTransparent(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_refdef.view.origin : center, R_DrawSurface_TransparentCallback, queueentity, surface - rsurface.modelsurfaces, rsurface.rtlight);
7598 // the alphatest check is to make sure we write depth for anything we skipped on the depth-only pass earlier
7599 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST));
7604 void R_QueueModelSurfaceList(entity_render_t *ent, int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly)
7608 // break the surface list down into batches by texture and use of lightmapping
7609 for (i = 0;i < numsurfaces;i = j)
7612 // texture is the base texture pointer, rsurface.texture is the
7613 // current frame/skin the texture is directing us to use (for example
7614 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
7615 // use skin 1 instead)
7616 texture = surfacelist[i]->texture;
7617 rsurface.texture = R_GetCurrentTexture(texture);
7618 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
7619 if (!(rsurface.texture->currentmaterialflags & flagsmask) || (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW))
7621 // if this texture is not the kind we want, skip ahead to the next one
7622 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
7626 // simply scan ahead until we find a different texture or lightmap state
7627 for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.uselightmaptexture == (surfacelist[j]->lightmaptexture != NULL);j++)
7629 // render the range of surfaces
7630 R_ProcessModelTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly, ent);
7634 float locboxvertex3f[6*4*3] =
7636 1,0,1, 1,0,0, 1,1,0, 1,1,1,
7637 0,1,1, 0,1,0, 0,0,0, 0,0,1,
7638 1,1,1, 1,1,0, 0,1,0, 0,1,1,
7639 0,0,1, 0,0,0, 1,0,0, 1,0,1,
7640 0,0,1, 1,0,1, 1,1,1, 0,1,1,
7641 1,0,0, 0,0,0, 0,1,0, 1,1,0
7644 unsigned short locboxelements[6*2*3] =
7654 void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
7657 cl_locnode_t *loc = (cl_locnode_t *)ent;
7659 float vertex3f[6*4*3];
7661 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
7662 GL_DepthMask(false);
7663 GL_DepthRange(0, 1);
7664 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
7666 GL_CullFace(GL_NONE);
7667 R_Mesh_Matrix(&identitymatrix);
7669 R_Mesh_VertexPointer(vertex3f, 0, 0);
7670 R_Mesh_ColorPointer(NULL, 0, 0);
7671 R_Mesh_ResetTextureState();
7672 R_SetupGenericShader(false);
7675 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_refdef.view.colorscale,
7676 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_refdef.view.colorscale,
7677 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_refdef.view.colorscale,
7678 surfacelist[0] < 0 ? 0.5f : 0.125f);
7680 if (VectorCompare(loc->mins, loc->maxs))
7682 VectorSet(size, 2, 2, 2);
7683 VectorMA(loc->mins, -0.5f, size, mins);
7687 VectorCopy(loc->mins, mins);
7688 VectorSubtract(loc->maxs, loc->mins, size);
7691 for (i = 0;i < 6*4*3;)
7692 for (j = 0;j < 3;j++, i++)
7693 vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
7695 R_Mesh_Draw(0, 6*4, 0, 6*2, NULL, locboxelements, 0, 0);
7698 void R_DrawLocs(void)
7701 cl_locnode_t *loc, *nearestloc;
7703 nearestloc = CL_Locs_FindNearest(cl.movement_origin);
7704 for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
7706 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
7707 R_MeshQueue_AddTransparent(center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
7711 void R_DrawDebugModel(entity_render_t *ent)
7713 int i, j, k, l, flagsmask;
7714 const int *elements;
7716 msurface_t *surface;
7717 dp_model_t *model = ent->model;
7720 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
7722 R_Mesh_ColorPointer(NULL, 0, 0);
7723 R_Mesh_ResetTextureState();
7724 R_SetupGenericShader(false);
7725 GL_DepthRange(0, 1);
7726 GL_DepthTest(!r_showdisabledepthtest.integer);
7727 GL_DepthMask(false);
7728 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
7730 if (r_showcollisionbrushes.value > 0 && model->brush.num_brushes)
7732 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
7733 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
7735 if (brush->colbrushf && brush->colbrushf->numtriangles)
7737 R_Mesh_VertexPointer(brush->colbrushf->points->v, 0, 0);
7738 GL_Color((i & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((i >> 5) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((i >> 10) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, r_showcollisionbrushes.value);
7739 R_Mesh_Draw(0, brush->colbrushf->numpoints, 0, brush->colbrushf->numtriangles, brush->colbrushf->elements, NULL, 0, 0);
7742 for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
7744 if (surface->num_collisiontriangles)
7746 R_Mesh_VertexPointer(surface->data_collisionvertex3f, 0, 0);
7747 GL_Color((i & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((i >> 5) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((i >> 10) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, r_showcollisionbrushes.value);
7748 R_Mesh_Draw(0, surface->num_collisionvertices, 0, surface->num_collisiontriangles, surface->data_collisionelement3i, NULL, 0, 0);
7753 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
7755 if (r_showtris.integer || r_shownormals.integer)
7757 if (r_showdisabledepthtest.integer)
7759 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
7760 GL_DepthMask(false);
7764 GL_BlendFunc(GL_ONE, GL_ZERO);
7767 for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
7769 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
7771 rsurface.texture = R_GetCurrentTexture(surface->texture);
7772 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
7774 RSurf_PrepareVerticesForBatch(true, true, 1, &surface);
7775 if (r_showtris.value > 0)
7777 if (!rsurface.texture->currentlayers->depthmask)
7778 GL_Color(r_refdef.view.colorscale, 0, 0, r_showtris.value);
7779 else if (ent == r_refdef.scene.worldentity)
7780 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, r_showtris.value);
7782 GL_Color(0, r_refdef.view.colorscale, 0, r_showtris.value);
7783 elements = (ent->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
7784 R_Mesh_VertexPointer(rsurface.vertex3f, 0, 0);
7785 R_Mesh_ColorPointer(NULL, 0, 0);
7786 R_Mesh_TexCoordPointer(0, 0, NULL, 0, 0);
7787 qglPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
7788 //R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, ent->model->surfmesh.data_element3i, NULL, 0, 0);
7789 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_firsttriangle, surface->num_triangles, rsurface.modelelement3i, rsurface.modelelement3s, rsurface.modelelement3i_bufferobject, rsurface.modelelement3s_bufferobject);
7790 qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
7793 if (r_shownormals.value < 0)
7796 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
7798 VectorCopy(rsurface.vertex3f + l * 3, v);
7799 GL_Color(r_refdef.view.colorscale, 0, 0, 1);
7800 qglVertex3f(v[0], v[1], v[2]);
7801 VectorMA(v, -r_shownormals.value, rsurface.svector3f + l * 3, v);
7802 GL_Color(r_refdef.view.colorscale, 1, 1, 1);
7803 qglVertex3f(v[0], v[1], v[2]);
7808 if (r_shownormals.value > 0)
7811 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
7813 VectorCopy(rsurface.vertex3f + l * 3, v);
7814 GL_Color(r_refdef.view.colorscale, 0, 0, 1);
7815 qglVertex3f(v[0], v[1], v[2]);
7816 VectorMA(v, r_shownormals.value, rsurface.svector3f + l * 3, v);
7817 GL_Color(r_refdef.view.colorscale, 1, 1, 1);
7818 qglVertex3f(v[0], v[1], v[2]);
7823 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
7825 VectorCopy(rsurface.vertex3f + l * 3, v);
7826 GL_Color(0, r_refdef.view.colorscale, 0, 1);
7827 qglVertex3f(v[0], v[1], v[2]);
7828 VectorMA(v, r_shownormals.value, rsurface.tvector3f + l * 3, v);
7829 GL_Color(r_refdef.view.colorscale, 1, 1, 1);
7830 qglVertex3f(v[0], v[1], v[2]);
7835 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
7837 VectorCopy(rsurface.vertex3f + l * 3, v);
7838 GL_Color(0, 0, r_refdef.view.colorscale, 1);
7839 qglVertex3f(v[0], v[1], v[2]);
7840 VectorMA(v, r_shownormals.value, rsurface.normal3f + l * 3, v);
7841 GL_Color(r_refdef.view.colorscale, 1, 1, 1);
7842 qglVertex3f(v[0], v[1], v[2]);
7849 rsurface.texture = NULL;
7853 extern void R_BuildLightMap(const entity_render_t *ent, msurface_t *surface);
7854 int r_maxsurfacelist = 0;
7855 msurface_t **r_surfacelist = NULL;
7856 void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean debug)
7858 int i, j, endj, f, flagsmask;
7860 dp_model_t *model = r_refdef.scene.worldmodel;
7861 msurface_t *surfaces;
7862 unsigned char *update;
7863 int numsurfacelist = 0;
7867 if (r_maxsurfacelist < model->num_surfaces)
7869 r_maxsurfacelist = model->num_surfaces;
7871 Mem_Free(r_surfacelist);
7872 r_surfacelist = (msurface_t **) Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
7875 RSurf_ActiveWorldEntity();
7877 surfaces = model->data_surfaces;
7878 update = model->brushq1.lightmapupdateflags;
7880 // update light styles on this submodel
7881 if (!skysurfaces && !depthonly && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
7883 model_brush_lightstyleinfo_t *style;
7884 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
7886 if (style->value != r_refdef.scene.lightstylevalue[style->style])
7888 int *list = style->surfacelist;
7889 style->value = r_refdef.scene.lightstylevalue[style->style];
7890 for (j = 0;j < style->numsurfaces;j++)
7891 update[list[j]] = true;
7896 flagsmask = skysurfaces ? MATERIALFLAG_SKY : MATERIALFLAG_WALL;
7900 R_DrawDebugModel(r_refdef.scene.worldentity);
7901 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
7907 rsurface.uselightmaptexture = false;
7908 rsurface.texture = NULL;
7909 rsurface.rtlight = NULL;
7911 // add visible surfaces to draw list
7912 for (i = 0;i < model->nummodelsurfaces;i++)
7914 j = model->sortedmodelsurfaces[i];
7915 if (r_refdef.viewcache.world_surfacevisible[j])
7916 r_surfacelist[numsurfacelist++] = surfaces + j;
7918 // update lightmaps if needed
7920 for (j = model->firstmodelsurface, endj = model->firstmodelsurface + model->nummodelsurfaces;j < endj;j++)
7921 if (r_refdef.viewcache.world_surfacevisible[j])
7923 R_BuildLightMap(r_refdef.scene.worldentity, surfaces + j);
7924 // don't do anything if there were no surfaces
7925 if (!numsurfacelist)
7927 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
7930 R_QueueWorldSurfaceList(numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly);
7931 GL_AlphaTest(false);
7933 // add to stats if desired
7934 if (r_speeds.integer && !skysurfaces && !depthonly)
7936 r_refdef.stats.world_surfaces += numsurfacelist;
7937 for (j = 0;j < numsurfacelist;j++)
7938 r_refdef.stats.world_triangles += r_surfacelist[j]->num_triangles;
7940 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
7943 void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean debug)
7945 int i, j, endj, f, flagsmask;
7947 dp_model_t *model = ent->model;
7948 msurface_t *surfaces;
7949 unsigned char *update;
7950 int numsurfacelist = 0;
7954 if (r_maxsurfacelist < model->num_surfaces)
7956 r_maxsurfacelist = model->num_surfaces;
7958 Mem_Free(r_surfacelist);
7959 r_surfacelist = (msurface_t **) Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
7962 // if the model is static it doesn't matter what value we give for
7963 // wantnormals and wanttangents, so this logic uses only rules applicable
7964 // to a model, knowing that they are meaningless otherwise
7965 if (ent == r_refdef.scene.worldentity)
7966 RSurf_ActiveWorldEntity();
7967 else if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
7968 RSurf_ActiveModelEntity(ent, false, false);
7970 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader && !depthonly);
7972 surfaces = model->data_surfaces;
7973 update = model->brushq1.lightmapupdateflags;
7975 // update light styles
7976 if (!skysurfaces && !depthonly && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
7978 model_brush_lightstyleinfo_t *style;
7979 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
7981 if (style->value != r_refdef.scene.lightstylevalue[style->style])
7983 int *list = style->surfacelist;
7984 style->value = r_refdef.scene.lightstylevalue[style->style];
7985 for (j = 0;j < style->numsurfaces;j++)
7986 update[list[j]] = true;
7991 flagsmask = skysurfaces ? MATERIALFLAG_SKY : MATERIALFLAG_WALL;
7995 R_DrawDebugModel(ent);
7996 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
8002 rsurface.uselightmaptexture = false;
8003 rsurface.texture = NULL;
8004 rsurface.rtlight = NULL;
8006 // add visible surfaces to draw list
8007 for (i = 0;i < model->nummodelsurfaces;i++)
8008 r_surfacelist[numsurfacelist++] = surfaces + model->sortedmodelsurfaces[i];
8009 // don't do anything if there were no surfaces
8010 if (!numsurfacelist)
8012 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
8015 // update lightmaps if needed
8017 for (j = model->firstmodelsurface, endj = model->firstmodelsurface + model->nummodelsurfaces;j < endj;j++)
8019 R_BuildLightMap(ent, surfaces + j);
8020 R_QueueModelSurfaceList(ent, numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly);
8021 GL_AlphaTest(false);
8023 // add to stats if desired
8024 if (r_speeds.integer && !skysurfaces && !depthonly)
8026 r_refdef.stats.entities_surfaces += numsurfacelist;
8027 for (j = 0;j < numsurfacelist;j++)
8028 r_refdef.stats.entities_triangles += r_surfacelist[j]->num_triangles;
8030 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity