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;
36 r_viewcache_t r_viewcache;
38 cvar_t r_depthfirst = {CVAR_SAVE, "r_depthfirst", "1", "renders a depth-only version of the scene before normal rendering begins to eliminate overdraw, values: 0 = off, 1 = world depth, 2 = world and model depth"};
39 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
40 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%, 10 = 100%)"};
41 cvar_t r_showsurfaces = {0, "r_showsurfaces", "0", "1 shows surfaces as different colors, or a value of 2 shows triangle draw order (for analyzing whether meshes are optimized for vertex cache)"};
42 cvar_t r_showtris = {0, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
43 cvar_t r_shownormals = {0, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
44 cvar_t r_showlighting = {0, "r_showlighting", "0", "shows areas lit by lights, useful for finding out why some areas of a map render slowly (bright orange = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
45 cvar_t r_showshadowvolumes = {0, "r_showshadowvolumes", "0", "shows areas shadowed by lights, useful for finding out why some areas of a map render slowly (bright blue = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
46 cvar_t r_showcollisionbrushes = {0, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
47 cvar_t r_showcollisionbrushes_polygonfactor = {0, "r_showcollisionbrushes_polygonfactor", "-1", "expands outward the brush polygons a little bit, used to make collision brushes appear infront of walls"};
48 cvar_t r_showcollisionbrushes_polygonoffset = {0, "r_showcollisionbrushes_polygonoffset", "0", "nudges brush polygon depth in hardware depth units, used to make collision brushes appear infront of walls"};
49 cvar_t r_showdisabledepthtest = {0, "r_showdisabledepthtest", "0", "disables depth testing on r_show* cvars, allowing you to see what hidden geometry the graphics card is processing"};
50 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
51 cvar_t r_drawentities = {0, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
52 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1", "draw your weapon model"};
53 cvar_t r_cullentities_trace = {0, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
54 cvar_t r_cullentities_trace_samples = {0, "r_cullentities_trace_samples", "2", "number of samples to test for entity culling"};
55 cvar_t r_cullentities_trace_enlarge = {0, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
56 cvar_t r_cullentities_trace_delay = {0, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
57 cvar_t r_speeds = {0, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
58 cvar_t r_fullbright = {0, "r_fullbright","0", "makes map very bright and renders faster"};
59 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1", "opacity of water polygons"};
60 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
61 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
62 cvar_t r_shadows = {CVAR_SAVE, "r_shadows", "0", "casts fake stencil shadows from models onto the world (rtlights are unaffected by this)"};
63 cvar_t r_shadows_throwdistance = {CVAR_SAVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
64 cvar_t r_q1bsp_skymasking = {0, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
65 cvar_t r_polygonoffset_submodel_factor = {0, "r_polygonoffset_submodel_factor", "0", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
66 cvar_t r_polygonoffset_submodel_offset = {0, "r_polygonoffset_submodel_offset", "2", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
68 cvar_t gl_fogenable = {0, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
69 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
70 cvar_t gl_fogred = {0, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
71 cvar_t gl_foggreen = {0, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
72 cvar_t gl_fogblue = {0, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
73 cvar_t gl_fogstart = {0, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
74 cvar_t gl_fogend = {0, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
76 cvar_t r_textureunits = {0, "r_textureunits", "32", "number of hardware texture units reported by driver (note: setting this to 1 turns off gl_combine)"};
78 cvar_t r_glsl = {CVAR_SAVE, "r_glsl", "1", "enables use of OpenGL 2.0 pixel shaders for lighting"};
79 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
80 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
81 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
82 cvar_t r_glsl_deluxemapping = {CVAR_SAVE, "r_glsl_deluxemapping", "1", "use per pixel lighting on deluxemap-compiled q3bsp maps (or a value of 2 forces deluxemap shading even without deluxemaps)"};
83 cvar_t r_glsl_contrastboost = {CVAR_SAVE, "r_glsl_contrastboost", "1", "by how much to multiply the contrast in dark areas (1 is no change)"};
85 cvar_t r_water = {CVAR_SAVE, "r_water", "0", "whether to use reflections and refraction on water surfaces (note: r_wateralpha must be set below 1)"};
86 cvar_t r_water_clippingplanebias = {CVAR_SAVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
87 cvar_t r_water_resolutionmultiplier = {CVAR_SAVE, "r_water_resolutionmultiplier", "0.5", "multiplier for screen resolution when rendering refracted/reflected scenes, 1 is full quality, lower values are faster"};
88 cvar_t r_water_refractdistort = {CVAR_SAVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
89 cvar_t r_water_reflectdistort = {CVAR_SAVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
91 cvar_t r_lerpsprites = {CVAR_SAVE, "r_lerpsprites", "1", "enables animation smoothing on sprites (requires r_lerpmodels 1)"};
92 cvar_t r_lerpmodels = {CVAR_SAVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
93 cvar_t r_lerplightstyles = {CVAR_SAVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
94 cvar_t r_waterscroll = {CVAR_SAVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
96 cvar_t r_bloom = {CVAR_SAVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
97 cvar_t r_bloom_colorscale = {CVAR_SAVE, "r_bloom_colorscale", "1", "how bright the glow is"};
98 cvar_t r_bloom_brighten = {CVAR_SAVE, "r_bloom_brighten", "2", "how bright the glow is, after subtract/power"};
99 cvar_t r_bloom_blur = {CVAR_SAVE, "r_bloom_blur", "4", "how large the glow is"};
100 cvar_t r_bloom_resolution = {CVAR_SAVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
101 cvar_t r_bloom_colorexponent = {CVAR_SAVE, "r_bloom_colorexponent", "1", "how exagerated the glow is"};
102 cvar_t r_bloom_colorsubtract = {CVAR_SAVE, "r_bloom_colorsubtract", "0.125", "reduces bloom colors by a certain amount"};
104 cvar_t r_hdr = {CVAR_SAVE, "r_hdr", "0", "enables High Dynamic Range bloom effect (higher quality version of r_bloom)"};
105 cvar_t r_hdr_scenebrightness = {CVAR_SAVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
106 cvar_t r_hdr_glowintensity = {CVAR_SAVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
107 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)"};
109 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"};
111 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"};
113 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers)"};
115 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
116 cvar_t r_batchmode = {0, "r_batchmode", "1", "selects method of rendering multiple surfaces with one driver call (values are 0, 1, 2, etc...)"};
117 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"};
118 cvar_t r_track_sprites_flags = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accodringly, 2: Make it a continuous rotation"};
119 cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
120 cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
122 extern qboolean v_flipped_state;
124 typedef struct r_glsl_bloomshader_s
127 int loc_Texture_Bloom;
129 r_glsl_bloomshader_t;
131 static struct r_bloomstate_s
136 int bloomwidth, bloomheight;
138 int screentexturewidth, screentextureheight;
139 rtexture_t *texture_screen;
141 int bloomtexturewidth, bloomtextureheight;
142 rtexture_t *texture_bloom;
144 r_glsl_bloomshader_t *shader;
146 // arrays for rendering the screen passes
147 float screentexcoord2f[8];
148 float bloomtexcoord2f[8];
149 float offsettexcoord2f[8];
153 typedef struct r_waterstate_waterplane_s
155 rtexture_t *texture_refraction;
156 rtexture_t *texture_reflection;
158 int materialflags; // combined flags of all water surfaces on this plane
159 unsigned char pvsbits[(32768+7)>>3]; // FIXME: buffer overflow on huge maps
162 r_waterstate_waterplane_t;
164 #define MAX_WATERPLANES 16
166 static struct r_waterstate_s
170 qboolean renderingscene; // true while rendering a refraction or reflection texture, disables water surfaces
172 int waterwidth, waterheight;
173 int texturewidth, textureheight;
175 int maxwaterplanes; // same as MAX_WATERPLANES
177 r_waterstate_waterplane_t waterplanes[MAX_WATERPLANES];
179 float screenscale[2];
180 float screencenter[2];
184 // shadow volume bsp struct with automatically growing nodes buffer
187 rtexture_t *r_texture_blanknormalmap;
188 rtexture_t *r_texture_white;
189 rtexture_t *r_texture_grey128;
190 rtexture_t *r_texture_black;
191 rtexture_t *r_texture_notexture;
192 rtexture_t *r_texture_whitecube;
193 rtexture_t *r_texture_normalizationcube;
194 rtexture_t *r_texture_fogattenuation;
195 //rtexture_t *r_texture_fogintensity;
197 char r_qwskincache[MAX_SCOREBOARD][MAX_QPATH];
198 skinframe_t *r_qwskincache_skinframe[MAX_SCOREBOARD];
200 // vertex coordinates for a quad that covers the screen exactly
201 const static float r_screenvertex3f[12] =
209 extern void R_DrawModelShadows(void);
211 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
214 for (i = 0;i < verts;i++)
225 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
228 for (i = 0;i < verts;i++)
238 // FIXME: move this to client?
241 if (gamemode == GAME_NEHAHRA)
243 Cvar_Set("gl_fogenable", "0");
244 Cvar_Set("gl_fogdensity", "0.2");
245 Cvar_Set("gl_fogred", "0.3");
246 Cvar_Set("gl_foggreen", "0.3");
247 Cvar_Set("gl_fogblue", "0.3");
249 r_refdef.fog_density = r_refdef.fog_red = r_refdef.fog_green = r_refdef.fog_blue = 0.0f;
252 float FogPoint_World(const vec3_t p)
254 unsigned int fogmasktableindex = (unsigned int)(VectorDistance((p), r_view.origin) * r_refdef.fogmasktabledistmultiplier);
255 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
258 float FogPoint_Model(const vec3_t p)
260 unsigned int fogmasktableindex = (unsigned int)(VectorDistance((p), rsurface.modelorg) * r_refdef.fogmasktabledistmultiplier);
261 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
264 static void R_BuildBlankTextures(void)
266 unsigned char data[4];
267 data[2] = 128; // normal X
268 data[1] = 128; // normal Y
269 data[0] = 255; // normal Z
270 data[3] = 128; // height
271 r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
276 r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
281 r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
286 r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
289 static void R_BuildNoTexture(void)
292 unsigned char pix[16][16][4];
293 // this makes a light grey/dark grey checkerboard texture
294 for (y = 0;y < 16;y++)
296 for (x = 0;x < 16;x++)
298 if ((y < 8) ^ (x < 8))
314 r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, NULL);
317 static void R_BuildWhiteCube(void)
319 unsigned char data[6*1*1*4];
320 memset(data, 255, sizeof(data));
321 r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
324 static void R_BuildNormalizationCube(void)
328 vec_t s, t, intensity;
330 unsigned char data[6][NORMSIZE][NORMSIZE][4];
331 for (side = 0;side < 6;side++)
333 for (y = 0;y < NORMSIZE;y++)
335 for (x = 0;x < NORMSIZE;x++)
337 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
338 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
373 intensity = 127.0f / sqrt(DotProduct(v, v));
374 data[side][y][x][2] = (unsigned char)(128.0f + intensity * v[0]);
375 data[side][y][x][1] = (unsigned char)(128.0f + intensity * v[1]);
376 data[side][y][x][0] = (unsigned char)(128.0f + intensity * v[2]);
377 data[side][y][x][3] = 255;
381 r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, &data[0][0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
384 static void R_BuildFogTexture(void)
388 unsigned char data1[FOGWIDTH][4];
389 //unsigned char data2[FOGWIDTH][4];
390 for (x = 0;x < FOGWIDTH;x++)
392 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
397 //data2[x][0] = 255 - b;
398 //data2[x][1] = 255 - b;
399 //data2[x][2] = 255 - b;
402 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);
403 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
406 static const char *builtinshaderstring =
407 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
408 "// written by Forest 'LordHavoc' Hale\n"
410 "// common definitions between vertex shader and fragment shader:\n"
412 "#ifdef __GLSL_CG_DATA_TYPES\n"
413 "# define myhalf half\n"
414 "# define myhvec2 hvec2\n"
415 "# define myhvec3 hvec3\n"
416 "# define myhvec4 hvec4\n"
418 "# define myhalf float\n"
419 "# define myhvec2 vec2\n"
420 "# define myhvec3 vec3\n"
421 "# define myhvec4 vec4\n"
424 "varying vec2 TexCoord;\n"
425 "varying vec2 TexCoordLightmap;\n"
427 "//#ifdef MODE_LIGHTSOURCE\n"
428 "varying vec3 CubeVector;\n"
431 "//#ifdef MODE_LIGHTSOURCE\n"
432 "varying vec3 LightVector;\n"
434 "//# ifdef MODE_LIGHTDIRECTION\n"
435 "//varying vec3 LightVector;\n"
439 "varying vec3 EyeVector;\n"
441 "varying vec3 EyeVectorModelSpace;\n"
444 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
445 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
446 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
448 "//#ifdef MODE_WATER\n"
449 "varying vec4 ModelViewProjectionPosition;\n"
451 "//# ifdef MODE_REFRACTION\n"
452 "//varying vec4 ModelViewProjectionPosition;\n"
454 "//# ifdef USEREFLECTION\n"
455 "//varying vec4 ModelViewProjectionPosition;\n"
464 "// vertex shader specific:\n"
465 "#ifdef VERTEX_SHADER\n"
467 "uniform vec3 LightPosition;\n"
468 "uniform vec3 EyePosition;\n"
469 "uniform vec3 LightDir;\n"
471 "// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3)\n"
475 " gl_FrontColor = gl_Color;\n"
476 " // copy the surface texcoord\n"
477 " TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);\n"
478 "#ifndef MODE_LIGHTSOURCE\n"
479 "# ifndef MODE_LIGHTDIRECTION\n"
480 " TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
484 "#ifdef MODE_LIGHTSOURCE\n"
485 " // transform vertex position into light attenuation/cubemap space\n"
486 " // (-1 to +1 across the light box)\n"
487 " CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex);\n"
489 " // transform unnormalized light direction into tangent space\n"
490 " // (we use unnormalized to ensure that it interpolates correctly and then\n"
491 " // normalize it per pixel)\n"
492 " vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
493 " LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
494 " LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
495 " LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
498 "#ifdef MODE_LIGHTDIRECTION\n"
499 " LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
500 " LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
501 " LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
504 " // transform unnormalized eye direction into tangent space\n"
506 " vec3 EyeVectorModelSpace;\n"
508 " EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
509 " EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
510 " EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
511 " EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
513 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
514 " VectorS = gl_MultiTexCoord1.xyz;\n"
515 " VectorT = gl_MultiTexCoord2.xyz;\n"
516 " VectorR = gl_MultiTexCoord3.xyz;\n"
519 "//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION)\n"
520 "// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix;\n"
521 "// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
522 "// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
525 "// transform vertex to camera space, using ftransform to match non-VS\n"
527 " gl_Position = ftransform();\n"
529 "#ifdef MODE_WATER\n"
530 " ModelViewProjectionPosition = gl_Position;\n"
532 "#ifdef MODE_REFRACTION\n"
533 " ModelViewProjectionPosition = gl_Position;\n"
535 "#ifdef USEREFLECTION\n"
536 " ModelViewProjectionPosition = gl_Position;\n"
540 "#endif // VERTEX_SHADER\n"
545 "// fragment shader specific:\n"
546 "#ifdef FRAGMENT_SHADER\n"
548 "// 13 textures, we can only use up to 16 on DX9-class hardware\n"
549 "uniform sampler2D Texture_Normal;\n"
550 "uniform sampler2D Texture_Color;\n"
551 "uniform sampler2D Texture_Gloss;\n"
552 "uniform samplerCube Texture_Cube;\n"
553 "uniform sampler2D Texture_Attenuation;\n"
554 "uniform sampler2D Texture_FogMask;\n"
555 "uniform sampler2D Texture_Pants;\n"
556 "uniform sampler2D Texture_Shirt;\n"
557 "uniform sampler2D Texture_Lightmap;\n"
558 "uniform sampler2D Texture_Deluxemap;\n"
559 "uniform sampler2D Texture_Glow;\n"
560 "uniform sampler2D Texture_Reflection;\n"
561 "uniform sampler2D Texture_Refraction;\n"
563 "uniform myhvec3 LightColor;\n"
564 "uniform myhvec3 AmbientColor;\n"
565 "uniform myhvec3 DiffuseColor;\n"
566 "uniform myhvec3 SpecularColor;\n"
567 "uniform myhvec3 Color_Pants;\n"
568 "uniform myhvec3 Color_Shirt;\n"
569 "uniform myhvec3 FogColor;\n"
571 "//#ifdef MODE_WATER\n"
572 "uniform vec4 DistortScaleRefractReflect;\n"
573 "uniform vec4 ScreenScaleRefractReflect;\n"
574 "uniform vec4 ScreenCenterRefractReflect;\n"
575 "uniform myhvec4 RefractColor;\n"
576 "uniform myhvec4 ReflectColor;\n"
577 "uniform myhalf ReflectFactor;\n"
578 "uniform myhalf ReflectOffset;\n"
580 "//# ifdef MODE_REFRACTION\n"
581 "//uniform vec4 DistortScaleRefractReflect;\n"
582 "//uniform vec4 ScreenScaleRefractReflect;\n"
583 "//uniform vec4 ScreenCenterRefractReflect;\n"
584 "//uniform myhvec4 RefractColor;\n"
585 "//# ifdef USEREFLECTION\n"
586 "//uniform myhvec4 ReflectColor;\n"
589 "//# ifdef USEREFLECTION\n"
590 "//uniform vec4 DistortScaleRefractReflect;\n"
591 "//uniform vec4 ScreenScaleRefractReflect;\n"
592 "//uniform vec4 ScreenCenterRefractReflect;\n"
593 "//uniform myhvec4 ReflectColor;\n"
598 "uniform myhalf GlowScale;\n"
599 "uniform myhalf SceneBrightness;\n"
600 "#ifdef USECONTRASTBOOST\n"
601 "uniform myhalf ContrastBoostCoeff;\n"
604 "uniform float OffsetMapping_Scale;\n"
605 "uniform float OffsetMapping_Bias;\n"
606 "uniform float FogRangeRecip;\n"
608 "uniform myhalf AmbientScale;\n"
609 "uniform myhalf DiffuseScale;\n"
610 "uniform myhalf SpecularScale;\n"
611 "uniform myhalf SpecularPower;\n"
613 "#ifdef USEOFFSETMAPPING\n"
614 "vec2 OffsetMapping(vec2 TexCoord)\n"
616 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
617 " // 14 sample relief mapping: linear search and then binary search\n"
618 " // this basically steps forward a small amount repeatedly until it finds\n"
619 " // itself inside solid, then jitters forward and back using decreasing\n"
620 " // amounts to find the impact\n"
621 " //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
622 " //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
623 " vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
624 " vec3 RT = vec3(TexCoord, 1);\n"
625 " OffsetVector *= 0.1;\n"
626 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
627 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
628 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
629 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
630 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
631 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
632 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
633 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
634 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
635 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5);\n"
636 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25);\n"
637 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125);\n"
638 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625);\n"
639 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
642 " // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
643 " // this basically moves forward the full distance, and then backs up based\n"
644 " // on height of samples\n"
645 " //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
646 " //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
647 " vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
648 " TexCoord += OffsetVector;\n"
649 " OffsetVector *= 0.333;\n"
650 " TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
651 " TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
652 " TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
653 " return TexCoord;\n"
656 "#endif // USEOFFSETMAPPING\n"
658 "#ifdef MODE_WATER\n"
663 "#ifdef USEOFFSETMAPPING\n"
664 " // apply offsetmapping\n"
665 " vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
666 "#define TexCoord TexCoordOffset\n"
669 " vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
670 " //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
671 " vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec2(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
672 " float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 5.0) * ReflectFactor + ReflectOffset;\n"
673 " gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel);\n"
676 "#else // MODE_WATER\n"
677 "#ifdef MODE_REFRACTION\n"
679 "// refraction pass\n"
682 "#ifdef USEOFFSETMAPPING\n"
683 " // apply offsetmapping\n"
684 " vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
685 "#define TexCoord TexCoordOffset\n"
688 " vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
689 " //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
690 " vec2 ScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy + vec2(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
691 " gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor;\n"
694 "#else // MODE_REFRACTION\n"
697 "#ifdef USEOFFSETMAPPING\n"
698 " // apply offsetmapping\n"
699 " vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
700 "#define TexCoord TexCoordOffset\n"
703 " // combine the diffuse textures (base, pants, shirt)\n"
704 " myhvec4 color = myhvec4(texture2D(Texture_Color, TexCoord));\n"
705 "#ifdef USECOLORMAPPING\n"
706 " color.rgb += myhvec3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhvec3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
712 "#ifdef MODE_LIGHTSOURCE\n"
715 " // calculate surface normal, light normal, and specular normal\n"
716 " // compute color intensity for the two textures (colormap and glossmap)\n"
717 " // scale by light color and attenuation as efficiently as possible\n"
718 " // (do as much scalar math as possible rather than vector math)\n"
719 "# ifdef USESPECULAR\n"
720 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
721 " myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
722 " myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
724 " // calculate directional shading\n"
725 " color.rgb = LightColor * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * myhvec3(texture2D(Texture_Gloss, TexCoord)));\n"
727 "# ifdef USEDIFFUSE\n"
728 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
729 " myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
731 " // calculate directional shading\n"
732 " color.rgb = color.rgb * LightColor * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))));\n"
734 " // calculate directionless shading\n"
735 " color.rgb = color.rgb * LightColor * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
739 "# ifdef USECUBEFILTER\n"
740 " // apply light cubemap filter\n"
741 " //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector));\n"
742 " color.rgb *= myhvec3(textureCube(Texture_Cube, CubeVector));\n"
744 " color *= myhvec4(gl_Color);\n"
745 "#endif // MODE_LIGHTSOURCE\n"
750 "#ifdef MODE_LIGHTDIRECTION\n"
751 " // directional model lighting\n"
752 "# ifdef USESPECULAR\n"
753 " // get the surface normal and light normal\n"
754 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
755 " myhvec3 diffusenormal = myhvec3(LightVector);\n"
757 " // calculate directional shading\n"
758 " color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
759 " myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
760 " color.rgb += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
762 "# ifdef USEDIFFUSE\n"
763 " // get the surface normal and light normal\n"
764 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
765 " myhvec3 diffusenormal = myhvec3(LightVector);\n"
767 " // calculate directional shading\n"
768 " color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
770 " color.rgb *= AmbientColor;\n"
774 " color *= myhvec4(gl_Color);\n"
775 "#endif // MODE_LIGHTDIRECTION\n"
780 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
781 " // deluxemap lightmapping using light vectors in modelspace (evil q3map2)\n"
783 " // get the surface normal and light normal\n"
784 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
786 " myhvec3 diffusenormal_modelspace = myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5);\n"
787 " myhvec3 diffusenormal = normalize(myhvec3(dot(diffusenormal_modelspace, myhvec3(VectorS)), dot(diffusenormal_modelspace, myhvec3(VectorT)), dot(diffusenormal_modelspace, myhvec3(VectorR))));\n"
788 " // calculate directional shading\n"
789 " myhvec3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)));\n"
790 "# ifdef USESPECULAR\n"
791 " myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
792 " tempcolor += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
795 " // apply lightmap color\n"
796 " color.rgb = myhvec4(tempcolor,1) * myhvec4(texture2D(Texture_Lightmap, TexCoordLightmap)) * myhvec4(gl_Color) + myhvec4(color.rgb * AmbientScale, 0);\n"
797 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
802 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
803 " // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
805 " // get the surface normal and light normal\n"
806 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
808 " myhvec3 diffusenormal = normalize(myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5));\n"
809 " // calculate directional shading\n"
810 " myhvec3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)));\n"
811 "# ifdef USESPECULAR\n"
812 " myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
813 " tempcolor += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
816 " // apply lightmap color\n"
817 " color = myhvec4(tempcolor, 1) * myhvec4(texture2D(Texture_Lightmap, TexCoordLightmap)) * myhvec4(gl_Color) + myhvec4(color.rgb * AmbientScale, 0);\n"
818 "#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
823 "#ifdef MODE_LIGHTMAP\n"
824 " // apply lightmap color\n"
825 " color *= myhvec4(texture2D(Texture_Lightmap, TexCoordLightmap)) * myhvec4(gl_Color) * myhvec4(myhvec3(DiffuseScale), 1) + myhvec4(myhvec3(AmbientScale), 0);\n"
826 "#endif // MODE_LIGHTMAP\n"
836 " color.rgb += myhvec3(texture2D(Texture_Glow, TexCoord)) * GlowScale;\n"
839 "#ifndef MODE_LIGHTSOURCE\n"
840 "# ifdef USEREFLECTION\n"
841 " vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
842 " //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
843 " vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec3(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
844 " color.rgb = mix(color.rgb, myhvec3(texture2D(Texture_Reflection, ScreenTexCoord.zw)) * ReflectColor.rgb, ReflectColor.a);\n"
850 " color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhvec2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0))));\n"
853 "#ifdef USECONTRASTBOOST\n"
854 " color.rgb = color.rgb / (ContrastBoostCoeff * color.rgb + myhvec3(1, 1, 1));\n"
857 " color.rgb *= SceneBrightness;\n"
859 " gl_FragColor = vec4(color);\n"
861 "#endif // MODE_REFRACTION\n"
862 "#endif // MODE_WATER\n"
864 "#endif // FRAGMENT_SHADER\n"
867 #define SHADERPERMUTATION_COLORMAPPING (1<<0) // indicates this is a colormapped skin
868 #define SHADERPERMUTATION_CONTRASTBOOST (1<<1) // r_glsl_contrastboost boosts the contrast at low color levels (similar to gamma)
869 #define SHADERPERMUTATION_FOG (1<<2) // tint the color by fog color or black if using additive blend mode
870 #define SHADERPERMUTATION_CUBEFILTER (1<<3) // (lightsource) use cubemap light filter
871 #define SHADERPERMUTATION_GLOW (1<<4) // (lightmap) blend in an additive glow texture
872 #define SHADERPERMUTATION_DIFFUSE (1<<5) // (lightsource) whether to use directional shading
873 #define SHADERPERMUTATION_SPECULAR (1<<6) // (lightsource or deluxemapping) render specular effects
874 #define SHADERPERMUTATION_REFLECTION (1<<7) // normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
875 #define SHADERPERMUTATION_OFFSETMAPPING (1<<8) // adjust texcoords to roughly simulate a displacement mapped surface
876 #define SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING (1<<9) // adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
877 #define SHADERPERMUTATION_MODEBASE (1<<10) // multiplier for the SHADERMODE_ values to get a valid index
879 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
880 const char *shaderpermutationinfo[][2] =
882 {"#define USECOLORMAPPING\n", " colormapping"},
883 {"#define USECONTRASTBOOST\n", " contrastboost"},
884 {"#define USEFOG\n", " fog"},
885 {"#define USECUBEFILTER\n", " cubefilter"},
886 {"#define USEGLOW\n", " glow"},
887 {"#define USEDIFFUSE\n", " diffuse"},
888 {"#define USESPECULAR\n", " specular"},
889 {"#define USEREFLECTION\n", " reflection"},
890 {"#define USEOFFSETMAPPING\n", " offsetmapping"},
891 {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
895 // this enum is multiplied by SHADERPERMUTATION_MODEBASE
896 typedef enum shadermode_e
898 SHADERMODE_LIGHTMAP, // (lightmap) use directional pixel shading from fixed light direction (q3bsp)
899 SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE, // (lightmap) use directional pixel shading from texture containing modelspace light directions (deluxemap)
900 SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE, // (lightmap) use directional pixel shading from texture containing tangentspace light directions (deluxemap)
901 SHADERMODE_LIGHTDIRECTION, // (lightmap) use directional pixel shading from fixed light direction (q3bsp)
902 SHADERMODE_LIGHTSOURCE, // (lightsource) use directional pixel shading from light source (rtlight)
903 SHADERMODE_REFRACTION, // refract background (the material is rendered normally after this pass)
904 SHADERMODE_WATER, // refract background and reflection (the material is rendered normally after this pass)
909 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
910 const char *shadermodeinfo[][2] =
912 {"#define MODE_LIGHTMAP\n", " lightmap"},
913 {"#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
914 {"#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
915 {"#define MODE_LIGHTDIRECTION\n", " lightdirection"},
916 {"#define MODE_LIGHTSOURCE\n", " lightsource"},
917 {"#define MODE_REFRACTION\n", " refraction"},
918 {"#define MODE_WATER\n", " water"},
922 #define SHADERPERMUTATION_INDICES (SHADERPERMUTATION_MODEBASE * SHADERMODE_COUNT)
924 typedef struct r_glsl_permutation_s
926 // indicates if we have tried compiling this permutation already
928 // 0 if compilation failed
930 // locations of detected uniforms in program object, or -1 if not found
931 int loc_Texture_Normal;
932 int loc_Texture_Color;
933 int loc_Texture_Gloss;
934 int loc_Texture_Cube;
935 int loc_Texture_Attenuation;
936 int loc_Texture_FogMask;
937 int loc_Texture_Pants;
938 int loc_Texture_Shirt;
939 int loc_Texture_Lightmap;
940 int loc_Texture_Deluxemap;
941 int loc_Texture_Glow;
942 int loc_Texture_Refraction;
943 int loc_Texture_Reflection;
945 int loc_LightPosition;
950 int loc_FogRangeRecip;
951 int loc_AmbientScale;
952 int loc_DiffuseScale;
953 int loc_SpecularScale;
954 int loc_SpecularPower;
956 int loc_SceneBrightness; // or: Scenebrightness * ContrastBoost
957 int loc_OffsetMapping_Scale;
958 int loc_AmbientColor;
959 int loc_DiffuseColor;
960 int loc_SpecularColor;
962 int loc_ContrastBoostCoeff; // 1 - 1/ContrastBoost
963 int loc_DistortScaleRefractReflect;
964 int loc_ScreenScaleRefractReflect;
965 int loc_ScreenCenterRefractReflect;
966 int loc_RefractColor;
967 int loc_ReflectColor;
968 int loc_ReflectFactor;
969 int loc_ReflectOffset;
971 r_glsl_permutation_t;
973 // information about each possible shader permutation
974 r_glsl_permutation_t r_glsl_permutations[SHADERPERMUTATION_INDICES];
975 // currently selected permutation
976 r_glsl_permutation_t *r_glsl_permutation;
978 // these are additional flags used only by R_GLSL_CompilePermutation
979 #define SHADERTYPE_USES_VERTEXSHADER (1<<0)
980 #define SHADERTYPE_USES_GEOMETRYSHADER (1<<1)
981 #define SHADERTYPE_USES_FRAGMENTSHADER (1<<2)
983 static void R_GLSL_CompilePermutation(const char *filename, int permutation, int shadertype)
986 qboolean shaderfound;
987 r_glsl_permutation_t *p = r_glsl_permutations + permutation;
988 int vertstrings_count;
989 int geomstrings_count;
990 int fragstrings_count;
992 const char *vertstrings_list[32+1];
993 const char *geomstrings_list[32+1];
994 const char *fragstrings_list[32+1];
995 char permutationname[256];
1000 vertstrings_list[0] = "#define VERTEX_SHADER\n";
1001 geomstrings_list[0] = "#define GEOMETRY_SHADER\n";
1002 fragstrings_list[0] = "#define FRAGMENT_SHADER\n";
1003 vertstrings_count = 1;
1004 geomstrings_count = 1;
1005 fragstrings_count = 1;
1006 permutationname[0] = 0;
1007 i = permutation / SHADERPERMUTATION_MODEBASE;
1008 vertstrings_list[vertstrings_count++] = shadermodeinfo[i][0];
1009 geomstrings_list[geomstrings_count++] = shadermodeinfo[i][0];
1010 fragstrings_list[fragstrings_count++] = shadermodeinfo[i][0];
1011 strlcat(permutationname, shadermodeinfo[i][1], sizeof(permutationname));
1012 for (i = 0;shaderpermutationinfo[i][0];i++)
1014 if (permutation & (1<<i))
1016 vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i][0];
1017 geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i][0];
1018 fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i][0];
1019 strlcat(permutationname, shaderpermutationinfo[i][1], sizeof(permutationname));
1023 // keep line numbers correct
1024 vertstrings_list[vertstrings_count++] = "\n";
1025 geomstrings_list[geomstrings_count++] = "\n";
1026 fragstrings_list[fragstrings_count++] = "\n";
1029 shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
1030 shaderfound = false;
1033 Con_DPrint("from disk... ");
1034 vertstrings_list[vertstrings_count++] = shaderstring;
1035 geomstrings_list[geomstrings_count++] = shaderstring;
1036 fragstrings_list[fragstrings_count++] = shaderstring;
1039 else if (!strcmp(filename, "glsl/default.glsl"))
1041 vertstrings_list[vertstrings_count++] = builtinshaderstring;
1042 geomstrings_list[geomstrings_count++] = builtinshaderstring;
1043 fragstrings_list[fragstrings_count++] = builtinshaderstring;
1046 // clear any lists that are not needed by this shader
1047 if (!(shadertype & SHADERTYPE_USES_VERTEXSHADER))
1048 vertstrings_count = 0;
1049 if (!(shadertype & SHADERTYPE_USES_GEOMETRYSHADER))
1050 geomstrings_count = 0;
1051 if (!(shadertype & SHADERTYPE_USES_FRAGMENTSHADER))
1052 fragstrings_count = 0;
1053 // compile the shader program
1054 if (shaderfound && vertstrings_count + geomstrings_count + fragstrings_count)
1055 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1059 qglUseProgramObjectARB(p->program);CHECKGLERROR
1060 // look up all the uniform variable names we care about, so we don't
1061 // have to look them up every time we set them
1062 p->loc_Texture_Normal = qglGetUniformLocationARB(p->program, "Texture_Normal");
1063 p->loc_Texture_Color = qglGetUniformLocationARB(p->program, "Texture_Color");
1064 p->loc_Texture_Gloss = qglGetUniformLocationARB(p->program, "Texture_Gloss");
1065 p->loc_Texture_Cube = qglGetUniformLocationARB(p->program, "Texture_Cube");
1066 p->loc_Texture_Attenuation = qglGetUniformLocationARB(p->program, "Texture_Attenuation");
1067 p->loc_Texture_FogMask = qglGetUniformLocationARB(p->program, "Texture_FogMask");
1068 p->loc_Texture_Pants = qglGetUniformLocationARB(p->program, "Texture_Pants");
1069 p->loc_Texture_Shirt = qglGetUniformLocationARB(p->program, "Texture_Shirt");
1070 p->loc_Texture_Lightmap = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
1071 p->loc_Texture_Deluxemap = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
1072 p->loc_Texture_Glow = qglGetUniformLocationARB(p->program, "Texture_Glow");
1073 p->loc_Texture_Refraction = qglGetUniformLocationARB(p->program, "Texture_Refraction");
1074 p->loc_Texture_Reflection = qglGetUniformLocationARB(p->program, "Texture_Reflection");
1075 p->loc_FogColor = qglGetUniformLocationARB(p->program, "FogColor");
1076 p->loc_LightPosition = qglGetUniformLocationARB(p->program, "LightPosition");
1077 p->loc_EyePosition = qglGetUniformLocationARB(p->program, "EyePosition");
1078 p->loc_LightColor = qglGetUniformLocationARB(p->program, "LightColor");
1079 p->loc_Color_Pants = qglGetUniformLocationARB(p->program, "Color_Pants");
1080 p->loc_Color_Shirt = qglGetUniformLocationARB(p->program, "Color_Shirt");
1081 p->loc_FogRangeRecip = qglGetUniformLocationARB(p->program, "FogRangeRecip");
1082 p->loc_AmbientScale = qglGetUniformLocationARB(p->program, "AmbientScale");
1083 p->loc_DiffuseScale = qglGetUniformLocationARB(p->program, "DiffuseScale");
1084 p->loc_SpecularPower = qglGetUniformLocationARB(p->program, "SpecularPower");
1085 p->loc_SpecularScale = qglGetUniformLocationARB(p->program, "SpecularScale");
1086 p->loc_GlowScale = qglGetUniformLocationARB(p->program, "GlowScale");
1087 p->loc_SceneBrightness = qglGetUniformLocationARB(p->program, "SceneBrightness");
1088 p->loc_OffsetMapping_Scale = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
1089 p->loc_AmbientColor = qglGetUniformLocationARB(p->program, "AmbientColor");
1090 p->loc_DiffuseColor = qglGetUniformLocationARB(p->program, "DiffuseColor");
1091 p->loc_SpecularColor = qglGetUniformLocationARB(p->program, "SpecularColor");
1092 p->loc_LightDir = qglGetUniformLocationARB(p->program, "LightDir");
1093 p->loc_ContrastBoostCoeff = qglGetUniformLocationARB(p->program, "ContrastBoostCoeff");
1094 p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
1095 p->loc_ScreenScaleRefractReflect = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
1096 p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
1097 p->loc_RefractColor = qglGetUniformLocationARB(p->program, "RefractColor");
1098 p->loc_ReflectColor = qglGetUniformLocationARB(p->program, "ReflectColor");
1099 p->loc_ReflectFactor = qglGetUniformLocationARB(p->program, "ReflectFactor");
1100 p->loc_ReflectOffset = qglGetUniformLocationARB(p->program, "ReflectOffset");
1101 // initialize the samplers to refer to the texture units we use
1102 if (p->loc_Texture_Normal >= 0) qglUniform1iARB(p->loc_Texture_Normal, 0);
1103 if (p->loc_Texture_Color >= 0) qglUniform1iARB(p->loc_Texture_Color, 1);
1104 if (p->loc_Texture_Gloss >= 0) qglUniform1iARB(p->loc_Texture_Gloss, 2);
1105 if (p->loc_Texture_Cube >= 0) qglUniform1iARB(p->loc_Texture_Cube, 3);
1106 if (p->loc_Texture_FogMask >= 0) qglUniform1iARB(p->loc_Texture_FogMask, 4);
1107 if (p->loc_Texture_Pants >= 0) qglUniform1iARB(p->loc_Texture_Pants, 5);
1108 if (p->loc_Texture_Shirt >= 0) qglUniform1iARB(p->loc_Texture_Shirt, 6);
1109 if (p->loc_Texture_Lightmap >= 0) qglUniform1iARB(p->loc_Texture_Lightmap, 7);
1110 if (p->loc_Texture_Deluxemap >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap, 8);
1111 if (p->loc_Texture_Glow >= 0) qglUniform1iARB(p->loc_Texture_Glow, 9);
1112 if (p->loc_Texture_Attenuation >= 0) qglUniform1iARB(p->loc_Texture_Attenuation, 10);
1113 if (p->loc_Texture_Refraction >= 0) qglUniform1iARB(p->loc_Texture_Refraction, 11);
1114 if (p->loc_Texture_Reflection >= 0) qglUniform1iARB(p->loc_Texture_Reflection, 12);
1116 qglUseProgramObjectARB(0);CHECKGLERROR
1117 if (developer.integer)
1118 Con_Printf("GLSL shader %s :%s compiled.\n", filename, permutationname);
1122 if (developer.integer)
1123 Con_Printf("GLSL shader %s :%s failed! source code line offset for above errors is %i.\n", permutationname, filename, -(vertstrings_count - 1));
1125 Con_Printf("GLSL shader %s :%s failed! some features may not work properly.\n", permutationname, filename);
1128 Mem_Free(shaderstring);
1131 void R_GLSL_Restart_f(void)
1134 for (i = 0;i < SHADERPERMUTATION_INDICES;i++)
1135 if (r_glsl_permutations[i].program)
1136 GL_Backend_FreeProgram(r_glsl_permutations[i].program);
1137 memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
1140 void R_GLSL_DumpShader_f(void)
1144 qfile_t *file = FS_Open("glsl/default.glsl", "w", false, false);
1147 Con_Printf("failed to write to glsl/default.glsl\n");
1151 FS_Print(file, "// The engine may define the following macros:\n");
1152 FS_Print(file, "// #define VERTEX_SHADER\n// #define GEOMETRY_SHADER\n// #define FRAGMENT_SHADER\n");
1153 for (i = 0;shadermodeinfo[i][0];i++)
1154 FS_Printf(file, "// %s", shadermodeinfo[i][0]);
1155 for (i = 0;shaderpermutationinfo[i][0];i++)
1156 FS_Printf(file, "// %s", shaderpermutationinfo[i][0]);
1157 FS_Print(file, "\n");
1158 FS_Print(file, builtinshaderstring);
1161 Con_Printf("glsl/default.glsl written\n");
1164 extern rtexture_t *r_shadow_attenuationgradienttexture;
1165 extern rtexture_t *r_shadow_attenuation2dtexture;
1166 extern rtexture_t *r_shadow_attenuation3dtexture;
1167 int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, float ambientscale, float diffusescale, float specularscale, rsurfacepass_t rsurfacepass)
1169 // select a permutation of the lighting shader appropriate to this
1170 // combination of texture, entity, light source, and fogging, only use the
1171 // minimum features necessary to avoid wasting rendering time in the
1172 // fragment shader on features that are not being used
1173 const char *shaderfilename = NULL;
1174 unsigned int permutation = 0;
1175 unsigned int shadertype = 0;
1176 shadermode_t mode = 0;
1177 r_glsl_permutation = NULL;
1178 shaderfilename = "glsl/default.glsl";
1179 shadertype = SHADERTYPE_USES_VERTEXSHADER | SHADERTYPE_USES_FRAGMENTSHADER;
1180 // TODO: implement geometry-shader based shadow volumes someday
1181 if (r_glsl_offsetmapping.integer)
1183 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
1184 if (r_glsl_offsetmapping_reliefmapping.integer)
1185 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
1187 if (rsurfacepass == RSURFPASS_BACKGROUND)
1189 // distorted background
1190 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
1191 mode = SHADERMODE_WATER;
1193 mode = SHADERMODE_REFRACTION;
1195 else if (rsurfacepass == RSURFPASS_RTLIGHT)
1198 mode = SHADERMODE_LIGHTSOURCE;
1199 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1200 permutation |= SHADERPERMUTATION_CUBEFILTER;
1201 if (diffusescale > 0)
1202 permutation |= SHADERPERMUTATION_DIFFUSE;
1203 if (specularscale > 0)
1204 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1205 if (r_refdef.fogenabled)
1206 permutation |= SHADERPERMUTATION_FOG;
1207 if (rsurface.texture->colormapping)
1208 permutation |= SHADERPERMUTATION_COLORMAPPING;
1209 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1210 permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1211 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1212 permutation |= SHADERPERMUTATION_REFLECTION;
1214 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
1216 // unshaded geometry (fullbright or ambient model lighting)
1217 mode = SHADERMODE_LIGHTMAP;
1218 if (rsurface.texture->currentskinframe->glow)
1219 permutation |= SHADERPERMUTATION_GLOW;
1220 if (r_refdef.fogenabled)
1221 permutation |= SHADERPERMUTATION_FOG;
1222 if (rsurface.texture->colormapping)
1223 permutation |= SHADERPERMUTATION_COLORMAPPING;
1224 if (r_glsl_offsetmapping.integer)
1226 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
1227 if (r_glsl_offsetmapping_reliefmapping.integer)
1228 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
1230 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1231 permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1232 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1233 permutation |= SHADERPERMUTATION_REFLECTION;
1235 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT_DIRECTIONAL)
1237 // directional model lighting
1238 mode = SHADERMODE_LIGHTDIRECTION;
1239 if (rsurface.texture->currentskinframe->glow)
1240 permutation |= SHADERPERMUTATION_GLOW;
1241 permutation |= SHADERPERMUTATION_DIFFUSE;
1242 if (specularscale > 0)
1243 permutation |= SHADERPERMUTATION_SPECULAR;
1244 if (r_refdef.fogenabled)
1245 permutation |= SHADERPERMUTATION_FOG;
1246 if (rsurface.texture->colormapping)
1247 permutation |= SHADERPERMUTATION_COLORMAPPING;
1248 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1249 permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1250 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1251 permutation |= SHADERPERMUTATION_REFLECTION;
1253 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
1255 // ambient model lighting
1256 mode = SHADERMODE_LIGHTDIRECTION;
1257 if (rsurface.texture->currentskinframe->glow)
1258 permutation |= SHADERPERMUTATION_GLOW;
1259 if (r_refdef.fogenabled)
1260 permutation |= SHADERPERMUTATION_FOG;
1261 if (rsurface.texture->colormapping)
1262 permutation |= SHADERPERMUTATION_COLORMAPPING;
1263 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1264 permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1265 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1266 permutation |= SHADERPERMUTATION_REFLECTION;
1271 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping)
1273 // deluxemapping (light direction texture)
1274 if (rsurface.uselightmaptexture && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping && r_refdef.worldmodel->brushq3.deluxemapping_modelspace)
1275 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
1277 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1278 if (specularscale > 0)
1279 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1281 else if (r_glsl_deluxemapping.integer >= 2)
1283 // fake deluxemapping (uniform light direction in tangentspace)
1284 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1285 if (specularscale > 0)
1286 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1290 // ordinary lightmapping
1291 mode = SHADERMODE_LIGHTMAP;
1293 if (rsurface.texture->currentskinframe->glow)
1294 permutation |= SHADERPERMUTATION_GLOW;
1295 if (r_refdef.fogenabled)
1296 permutation |= SHADERPERMUTATION_FOG;
1297 if (rsurface.texture->colormapping)
1298 permutation |= SHADERPERMUTATION_COLORMAPPING;
1299 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1300 permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1301 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1302 permutation |= SHADERPERMUTATION_REFLECTION;
1304 permutation |= mode * SHADERPERMUTATION_MODEBASE;
1305 if (!r_glsl_permutations[permutation].program)
1307 if (!r_glsl_permutations[permutation].compiled)
1308 R_GLSL_CompilePermutation(shaderfilename, permutation, shadertype);
1309 if (!r_glsl_permutations[permutation].program)
1311 // remove features until we find a valid permutation
1313 for (i = (SHADERPERMUTATION_MODEBASE >> 1);;i>>=1)
1317 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");
1318 Cvar_SetValueQuick(&r_glsl, 0);
1319 return 0; // no bit left to clear
1321 // reduce i more quickly whenever it would not remove any bits
1322 if (!(permutation & i))
1325 if (!r_glsl_permutations[permutation].compiled)
1326 R_GLSL_CompilePermutation(shaderfilename, permutation, shadertype);
1327 if (r_glsl_permutations[permutation].program)
1332 r_glsl_permutation = r_glsl_permutations + permutation;
1334 qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
1335 if (mode == SHADERMODE_LIGHTSOURCE)
1337 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
1338 if (permutation & SHADERPERMUTATION_DIFFUSE)
1340 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
1341 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, ambientscale);
1342 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, diffusescale);
1343 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, specularscale);
1347 // ambient only is simpler
1348 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0] * ambientscale, lightcolorbase[1] * ambientscale, lightcolorbase[2] * ambientscale);
1349 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, 1);
1350 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, 0);
1351 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, 0);
1354 else if (mode == SHADERMODE_LIGHTDIRECTION)
1356 if (r_glsl_permutation->loc_AmbientColor >= 0)
1357 qglUniform3fARB(r_glsl_permutation->loc_AmbientColor, rsurface.modellight_ambient[0] * ambientscale, rsurface.modellight_ambient[1] * ambientscale, rsurface.modellight_ambient[2] * ambientscale);
1358 if (r_glsl_permutation->loc_DiffuseColor >= 0)
1359 qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor, rsurface.modellight_diffuse[0] * diffusescale, rsurface.modellight_diffuse[1] * diffusescale, rsurface.modellight_diffuse[2] * diffusescale);
1360 if (r_glsl_permutation->loc_SpecularColor >= 0)
1361 qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface.modellight_diffuse[0] * specularscale, rsurface.modellight_diffuse[1] * specularscale, rsurface.modellight_diffuse[2] * specularscale);
1362 if (r_glsl_permutation->loc_LightDir >= 0)
1363 qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
1367 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, r_ambient.value * 2.0f / 128.0f);
1368 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, r_refdef.lightmapintensity * 2.0f);
1369 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, r_refdef.lightmapintensity * specularscale * 2.0f);
1371 if (r_glsl_permutation->loc_GlowScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_GlowScale, r_hdr_glowintensity.value);
1372 if (r_glsl_permutation->loc_ContrastBoostCoeff >= 0)
1374 // The formula used is actually:
1375 // color.rgb *= SceneBrightness;
1376 // color.rgb *= ContrastBoost / ((ContrastBoost - 1) * color.rgb + 1);
1377 // I simplify that to
1378 // color.rgb *= [[SceneBrightness * ContrastBoost]];
1379 // color.rgb /= [[(ContrastBoost - 1) / ContrastBoost]] * color.rgb + 1;
1381 // color.rgb = [[SceneBrightness * ContrastBoost]] * color.rgb / ([[(ContrastBoost - 1) * SceneBrightness]] * color.rgb + 1);
1382 // and do [[calculations]] here in the engine
1383 qglUniform1fARB(r_glsl_permutation->loc_ContrastBoostCoeff, (r_glsl_contrastboost.value - 1) * r_view.colorscale);
1384 if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale * r_glsl_contrastboost.value);
1387 if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale);
1388 if (r_glsl_permutation->loc_FogColor >= 0)
1390 // additive passes are only darkened by fog, not tinted
1391 if (rsurface.rtlight || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD))
1392 qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1397 // color.rgb *= SceneBrightness;
1398 VectorScale(r_refdef.fogcolor, r_view.colorscale, fogvec);
1399 if(r_glsl_permutation->loc_ContrastBoostCoeff >= 0) // need to support contrast boost
1401 // color.rgb *= ContrastBoost / ((ContrastBoost - 1) * color.rgb + 1);
1402 fogvec[0] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[0] + 1);
1403 fogvec[1] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[1] + 1);
1404 fogvec[2] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[2] + 1);
1406 qglUniform3fARB(r_glsl_permutation->loc_FogColor, fogvec[0], fogvec[1], fogvec[2]);
1409 qglUniform3fARB(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
1411 if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, rsurface.modelorg[0], rsurface.modelorg[1], rsurface.modelorg[2]);
1412 if (r_glsl_permutation->loc_Color_Pants >= 0)
1414 if (rsurface.texture->currentskinframe->pants)
1415 qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]);
1417 qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
1419 if (r_glsl_permutation->loc_Color_Shirt >= 0)
1421 if (rsurface.texture->currentskinframe->shirt)
1422 qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]);
1424 qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
1426 if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, r_refdef.fograngerecip);
1427 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower);
1428 if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
1429 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);
1430 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]);
1431 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]);
1432 if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_RefractColor, 1, rsurface.texture->refractcolor4f);
1433 if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_ReflectColor, 1, rsurface.texture->reflectcolor4f);
1434 if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
1435 if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectOffset, rsurface.texture->reflectmin);
1440 #define SKINFRAME_HASH 1024
1444 int loadsequence; // incremented each level change
1445 memexpandablearray_t array;
1446 skinframe_t *hash[SKINFRAME_HASH];
1450 void R_SkinFrame_PrepareForPurge(void)
1452 r_skinframe.loadsequence++;
1453 // wrap it without hitting zero
1454 if (r_skinframe.loadsequence >= 200)
1455 r_skinframe.loadsequence = 1;
1458 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
1462 // mark the skinframe as used for the purging code
1463 skinframe->loadsequence = r_skinframe.loadsequence;
1466 void R_SkinFrame_Purge(void)
1470 for (i = 0;i < SKINFRAME_HASH;i++)
1472 for (s = r_skinframe.hash[i];s;s = s->next)
1474 if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
1476 if (s->merged == s->base)
1478 // FIXME: maybe pass a pointer to the pointer to R_PurgeTexture and reset it to NULL inside? [11/29/2007 Black]
1479 R_PurgeTexture(s->stain );s->stain = NULL;
1480 R_PurgeTexture(s->merged);s->merged = NULL;
1481 R_PurgeTexture(s->base );s->base = NULL;
1482 R_PurgeTexture(s->pants );s->pants = NULL;
1483 R_PurgeTexture(s->shirt );s->shirt = NULL;
1484 R_PurgeTexture(s->nmap );s->nmap = NULL;
1485 R_PurgeTexture(s->gloss );s->gloss = NULL;
1486 R_PurgeTexture(s->glow );s->glow = NULL;
1487 R_PurgeTexture(s->fog );s->fog = NULL;
1488 s->loadsequence = 0;
1494 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
1496 char basename[MAX_QPATH];
1498 Image_StripImageExtension(name, basename, sizeof(basename));
1500 if( last == NULL ) {
1502 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
1503 item = r_skinframe.hash[hashindex];
1508 // linearly search through the hash bucket
1509 for( ; item ; item = item->next ) {
1510 if( !strcmp( item->basename, basename ) ) {
1517 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qboolean add)
1521 char basename[MAX_QPATH];
1523 Image_StripImageExtension(name, basename, sizeof(basename));
1525 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
1526 for (item = r_skinframe.hash[hashindex];item;item = item->next)
1527 if (!strcmp(item->basename, basename) && item->textureflags == textureflags && item->comparewidth == comparewidth && item->compareheight == compareheight && item->comparecrc == comparecrc)
1531 rtexture_t *dyntexture;
1532 // check whether its a dynamic texture
1533 dyntexture = CL_GetDynTexture( basename );
1534 if (!add && !dyntexture)
1536 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
1537 memset(item, 0, sizeof(*item));
1538 strlcpy(item->basename, basename, sizeof(item->basename));
1539 item->base = dyntexture; // either NULL or dyntexture handle
1540 item->textureflags = textureflags;
1541 item->comparewidth = comparewidth;
1542 item->compareheight = compareheight;
1543 item->comparecrc = comparecrc;
1544 item->next = r_skinframe.hash[hashindex];
1545 r_skinframe.hash[hashindex] = item;
1547 else if( item->base == NULL )
1549 rtexture_t *dyntexture;
1550 // check whether its a dynamic texture
1551 // 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]
1552 dyntexture = CL_GetDynTexture( basename );
1553 item->base = dyntexture; // either NULL or dyntexture handle
1556 R_SkinFrame_MarkUsed(item);
1560 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboolean complain)
1562 // FIXME: it should be possible to disable loading various layers using
1563 // cvars, to prevent wasted loading time and memory usage if the user does
1565 qboolean loadnormalmap = true;
1566 qboolean loadgloss = true;
1567 qboolean loadpantsandshirt = true;
1568 qboolean loadglow = true;
1570 unsigned char *pixels;
1571 unsigned char *bumppixels;
1572 unsigned char *basepixels = NULL;
1573 int basepixels_width;
1574 int basepixels_height;
1575 skinframe_t *skinframe;
1577 if (cls.state == ca_dedicated)
1580 // return an existing skinframe if already loaded
1581 // if loading of the first image fails, don't make a new skinframe as it
1582 // would cause all future lookups of this to be missing
1583 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
1584 if (skinframe && skinframe->base)
1587 basepixels = loadimagepixelsbgra(name, complain, true);
1588 if (basepixels == NULL)
1591 // we've got some pixels to store, so really allocate this new texture now
1593 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
1594 skinframe->stain = NULL;
1595 skinframe->merged = NULL;
1596 skinframe->base = r_texture_notexture;
1597 skinframe->pants = NULL;
1598 skinframe->shirt = NULL;
1599 skinframe->nmap = r_texture_blanknormalmap;
1600 skinframe->gloss = NULL;
1601 skinframe->glow = NULL;
1602 skinframe->fog = NULL;
1604 basepixels_width = image_width;
1605 basepixels_height = image_height;
1606 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);
1608 if (textureflags & TEXF_ALPHA)
1610 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
1611 if (basepixels[j] < 255)
1613 if (j < basepixels_width * basepixels_height * 4)
1615 // has transparent pixels
1616 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
1617 for (j = 0;j < image_width * image_height * 4;j += 4)
1622 pixels[j+3] = basepixels[j+3];
1624 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);
1629 // _norm is the name used by tenebrae and has been adopted as standard
1632 if ((pixels = loadimagepixelsbgra(va("%s_norm", skinframe->basename), false, false)) != NULL)
1634 skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1638 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va("%s_bump", skinframe->basename), false, false)) != NULL)
1640 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
1641 Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
1642 skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1644 Mem_Free(bumppixels);
1646 else if (r_shadow_bumpscale_basetexture.value > 0)
1648 pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
1649 Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
1650 skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), basepixels_width, basepixels_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1654 // _luma is supported for tenebrae compatibility
1655 // (I think it's a very stupid name, but oh well)
1656 // _glow is the preferred name
1657 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;}
1658 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;}
1659 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;}
1660 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;}
1663 Mem_Free(basepixels);
1668 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)
1673 for (i = 0;i < width*height;i++)
1674 if (((unsigned char *)&palette[in[i]])[3] > 0)
1676 if (i == width*height)
1679 return R_LoadTexture2D (r_main_texturepool, name, width, height, in, TEXTYPE_PALETTE, textureflags, palette);
1682 // this is only used by .spr32 sprites, HL .spr files, HL .bsp files
1683 skinframe_t *R_SkinFrame_LoadInternalBGRA(const char *name, int textureflags, const unsigned char *skindata, int width, int height)
1686 unsigned char *temp1, *temp2;
1687 skinframe_t *skinframe;
1689 if (cls.state == ca_dedicated)
1692 // if already loaded just return it, otherwise make a new skinframe
1693 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height*4) : 0, true);
1694 if (skinframe && skinframe->base)
1697 skinframe->stain = NULL;
1698 skinframe->merged = NULL;
1699 skinframe->base = r_texture_notexture;
1700 skinframe->pants = NULL;
1701 skinframe->shirt = NULL;
1702 skinframe->nmap = r_texture_blanknormalmap;
1703 skinframe->gloss = NULL;
1704 skinframe->glow = NULL;
1705 skinframe->fog = NULL;
1707 // if no data was provided, then clearly the caller wanted to get a blank skinframe
1711 if (r_shadow_bumpscale_basetexture.value > 0)
1713 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
1714 temp2 = temp1 + width * height * 4;
1715 Image_HeightmapToNormalmap_BGRA(skindata, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
1716 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
1719 skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_BGRA, skinframe->textureflags, NULL);
1720 if (textureflags & TEXF_ALPHA)
1722 for (i = 3;i < width * height * 4;i += 4)
1723 if (skindata[i] < 255)
1725 if (i < width * height * 4)
1727 unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
1728 memcpy(fogpixels, skindata, width * height * 4);
1729 for (i = 0;i < width * height * 4;i += 4)
1730 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
1731 skinframe->fog = R_LoadTexture2D(r_main_texturepool, va("%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, skinframe->textureflags, NULL);
1732 Mem_Free(fogpixels);
1739 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
1742 unsigned char *temp1, *temp2;
1743 skinframe_t *skinframe;
1745 if (cls.state == ca_dedicated)
1748 // if already loaded just return it, otherwise make a new skinframe
1749 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
1750 if (skinframe && skinframe->base)
1753 skinframe->stain = NULL;
1754 skinframe->merged = NULL;
1755 skinframe->base = r_texture_notexture;
1756 skinframe->pants = NULL;
1757 skinframe->shirt = NULL;
1758 skinframe->nmap = r_texture_blanknormalmap;
1759 skinframe->gloss = NULL;
1760 skinframe->glow = NULL;
1761 skinframe->fog = NULL;
1763 // if no data was provided, then clearly the caller wanted to get a blank skinframe
1767 if (r_shadow_bumpscale_basetexture.value > 0)
1769 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
1770 temp2 = temp1 + width * height * 4;
1771 // use either a custom palette or the quake palette
1772 Image_Copy8bitBGRA(skindata, temp1, width * height, palette_bgra_complete);
1773 Image_HeightmapToNormalmap_BGRA(temp1, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
1774 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
1777 // use either a custom palette, or the quake palette
1778 skinframe->base = skinframe->merged = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_merged", skinframe->basename), (loadglowtexture ? palette_bgra_nofullbrights : ((skinframe->textureflags & TEXF_ALPHA) ? palette_bgra_transparent : palette_bgra_complete)), skinframe->textureflags, true); // all
1779 if (loadglowtexture)
1780 skinframe->glow = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_glow", skinframe->basename), palette_bgra_onlyfullbrights, skinframe->textureflags, false); // glow
1781 if (loadpantsandshirt)
1783 skinframe->pants = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_pants", skinframe->basename), palette_bgra_pantsaswhite, skinframe->textureflags, false); // pants
1784 skinframe->shirt = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_shirt", skinframe->basename), palette_bgra_shirtaswhite, skinframe->textureflags, false); // shirt
1786 if (skinframe->pants || skinframe->shirt)
1787 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
1788 if (textureflags & TEXF_ALPHA)
1790 for (i = 0;i < width * height;i++)
1791 if (((unsigned char *)palette_bgra_alpha)[skindata[i]*4+3] < 255)
1793 if (i < width * height)
1794 skinframe->fog = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_fog", skinframe->basename), palette_bgra_alpha, skinframe->textureflags, true); // fog mask
1800 skinframe_t *R_SkinFrame_LoadMissing(void)
1802 skinframe_t *skinframe;
1804 if (cls.state == ca_dedicated)
1807 skinframe = R_SkinFrame_Find("missing", TEXF_PRECACHE, 0, 0, 0, true);
1808 skinframe->stain = NULL;
1809 skinframe->merged = NULL;
1810 skinframe->base = r_texture_notexture;
1811 skinframe->pants = NULL;
1812 skinframe->shirt = NULL;
1813 skinframe->nmap = r_texture_blanknormalmap;
1814 skinframe->gloss = NULL;
1815 skinframe->glow = NULL;
1816 skinframe->fog = NULL;
1821 void gl_main_start(void)
1826 r = (-1.0/256.0) * (FOGMASKTABLEWIDTH * FOGMASKTABLEWIDTH);
1827 for (x = 0;x < FOGMASKTABLEWIDTH;x++)
1829 alpha = 1 - exp(r / ((double)x*(double)x));
1830 if (x == FOGMASKTABLEWIDTH - 1)
1832 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
1835 memset(r_qwskincache, 0, sizeof(r_qwskincache));
1836 memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
1838 // set up r_skinframe loading system for textures
1839 memset(&r_skinframe, 0, sizeof(r_skinframe));
1840 r_skinframe.loadsequence = 1;
1841 Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
1843 r_main_texturepool = R_AllocTexturePool();
1844 R_BuildBlankTextures();
1846 if (gl_texturecubemap)
1849 R_BuildNormalizationCube();
1851 R_BuildFogTexture();
1852 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
1853 memset(&r_waterstate, 0, sizeof(r_waterstate));
1854 memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
1855 memset(&r_svbsp, 0, sizeof (r_svbsp));
1858 void gl_main_shutdown(void)
1860 memset(r_qwskincache, 0, sizeof(r_qwskincache));
1861 memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
1863 // clear out the r_skinframe state
1864 Mem_ExpandableArray_FreeArray(&r_skinframe.array);
1865 memset(&r_skinframe, 0, sizeof(r_skinframe));
1868 Mem_Free(r_svbsp.nodes);
1869 memset(&r_svbsp, 0, sizeof (r_svbsp));
1870 R_FreeTexturePool(&r_main_texturepool);
1871 r_texture_blanknormalmap = NULL;
1872 r_texture_white = NULL;
1873 r_texture_grey128 = NULL;
1874 r_texture_black = NULL;
1875 r_texture_whitecube = NULL;
1876 r_texture_normalizationcube = NULL;
1877 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
1878 memset(&r_waterstate, 0, sizeof(r_waterstate));
1882 extern void CL_ParseEntityLump(char *entitystring);
1883 void gl_main_newmap(void)
1885 // FIXME: move this code to client
1887 char *entities, entname[MAX_QPATH];
1890 strlcpy(entname, cl.worldmodel->name, sizeof(entname));
1891 l = (int)strlen(entname) - 4;
1892 if (l >= 0 && !strcmp(entname + l, ".bsp"))
1894 memcpy(entname + l, ".ent", 5);
1895 if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
1897 CL_ParseEntityLump(entities);
1902 if (cl.worldmodel->brush.entities)
1903 CL_ParseEntityLump(cl.worldmodel->brush.entities);
1907 void GL_Main_Init(void)
1909 r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
1911 Cmd_AddCommand("r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
1912 Cmd_AddCommand("r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
1913 // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
1914 if (gamemode == GAME_NEHAHRA)
1916 Cvar_RegisterVariable (&gl_fogenable);
1917 Cvar_RegisterVariable (&gl_fogdensity);
1918 Cvar_RegisterVariable (&gl_fogred);
1919 Cvar_RegisterVariable (&gl_foggreen);
1920 Cvar_RegisterVariable (&gl_fogblue);
1921 Cvar_RegisterVariable (&gl_fogstart);
1922 Cvar_RegisterVariable (&gl_fogend);
1924 Cvar_RegisterVariable(&r_depthfirst);
1925 Cvar_RegisterVariable(&r_nearclip);
1926 Cvar_RegisterVariable(&r_showbboxes);
1927 Cvar_RegisterVariable(&r_showsurfaces);
1928 Cvar_RegisterVariable(&r_showtris);
1929 Cvar_RegisterVariable(&r_shownormals);
1930 Cvar_RegisterVariable(&r_showlighting);
1931 Cvar_RegisterVariable(&r_showshadowvolumes);
1932 Cvar_RegisterVariable(&r_showcollisionbrushes);
1933 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
1934 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
1935 Cvar_RegisterVariable(&r_showdisabledepthtest);
1936 Cvar_RegisterVariable(&r_drawportals);
1937 Cvar_RegisterVariable(&r_drawentities);
1938 Cvar_RegisterVariable(&r_cullentities_trace);
1939 Cvar_RegisterVariable(&r_cullentities_trace_samples);
1940 Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
1941 Cvar_RegisterVariable(&r_cullentities_trace_delay);
1942 Cvar_RegisterVariable(&r_drawviewmodel);
1943 Cvar_RegisterVariable(&r_speeds);
1944 Cvar_RegisterVariable(&r_fullbrights);
1945 Cvar_RegisterVariable(&r_wateralpha);
1946 Cvar_RegisterVariable(&r_dynamic);
1947 Cvar_RegisterVariable(&r_fullbright);
1948 Cvar_RegisterVariable(&r_shadows);
1949 Cvar_RegisterVariable(&r_shadows_throwdistance);
1950 Cvar_RegisterVariable(&r_q1bsp_skymasking);
1951 Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
1952 Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
1953 Cvar_RegisterVariable(&r_textureunits);
1954 Cvar_RegisterVariable(&r_glsl);
1955 Cvar_RegisterVariable(&r_glsl_offsetmapping);
1956 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
1957 Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
1958 Cvar_RegisterVariable(&r_glsl_deluxemapping);
1959 Cvar_RegisterVariable(&r_water);
1960 Cvar_RegisterVariable(&r_water_resolutionmultiplier);
1961 Cvar_RegisterVariable(&r_water_clippingplanebias);
1962 Cvar_RegisterVariable(&r_water_refractdistort);
1963 Cvar_RegisterVariable(&r_water_reflectdistort);
1964 Cvar_RegisterVariable(&r_lerpsprites);
1965 Cvar_RegisterVariable(&r_lerpmodels);
1966 Cvar_RegisterVariable(&r_lerplightstyles);
1967 Cvar_RegisterVariable(&r_waterscroll);
1968 Cvar_RegisterVariable(&r_bloom);
1969 Cvar_RegisterVariable(&r_bloom_colorscale);
1970 Cvar_RegisterVariable(&r_bloom_brighten);
1971 Cvar_RegisterVariable(&r_bloom_blur);
1972 Cvar_RegisterVariable(&r_bloom_resolution);
1973 Cvar_RegisterVariable(&r_bloom_colorexponent);
1974 Cvar_RegisterVariable(&r_bloom_colorsubtract);
1975 Cvar_RegisterVariable(&r_hdr);
1976 Cvar_RegisterVariable(&r_hdr_scenebrightness);
1977 Cvar_RegisterVariable(&r_glsl_contrastboost);
1978 Cvar_RegisterVariable(&r_hdr_glowintensity);
1979 Cvar_RegisterVariable(&r_hdr_range);
1980 Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
1981 Cvar_RegisterVariable(&developer_texturelogging);
1982 Cvar_RegisterVariable(&gl_lightmaps);
1983 Cvar_RegisterVariable(&r_test);
1984 Cvar_RegisterVariable(&r_batchmode);
1985 if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
1986 Cvar_SetValue("r_fullbrights", 0);
1987 R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
1989 Cvar_RegisterVariable(&r_track_sprites);
1990 Cvar_RegisterVariable(&r_track_sprites_flags);
1991 Cvar_RegisterVariable(&r_track_sprites_scalew);
1992 Cvar_RegisterVariable(&r_track_sprites_scaleh);
1995 extern void R_Textures_Init(void);
1996 extern void GL_Draw_Init(void);
1997 extern void GL_Main_Init(void);
1998 extern void R_Shadow_Init(void);
1999 extern void R_Sky_Init(void);
2000 extern void GL_Surf_Init(void);
2001 extern void R_Light_Init(void);
2002 extern void R_Particles_Init(void);
2003 extern void R_Explosion_Init(void);
2004 extern void gl_backend_init(void);
2005 extern void Sbar_Init(void);
2006 extern void R_LightningBeams_Init(void);
2007 extern void Mod_RenderInit(void);
2009 void Render_Init(void)
2022 R_LightningBeams_Init();
2031 extern char *ENGINE_EXTENSIONS;
2034 VID_CheckExtensions();
2036 // LordHavoc: report supported extensions
2037 Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
2039 // clear to black (loading plaque will be seen over this)
2041 qglClearColor(0,0,0,1);CHECKGLERROR
2042 qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
2045 int R_CullBox(const vec3_t mins, const vec3_t maxs)
2049 for (i = 0;i < r_view.numfrustumplanes;i++)
2051 // skip nearclip plane, it often culls portals when you are very close, and is almost never useful
2054 p = r_view.frustum + i;
2059 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2063 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2067 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2071 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2075 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2079 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2083 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2087 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2095 int R_CullBoxCustomPlanes(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
2099 for (i = 0;i < numplanes;i++)
2106 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2110 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2114 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2118 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2122 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2126 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2130 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2134 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2142 //==================================================================================
2144 static void R_UpdateEntityLighting(entity_render_t *ent)
2146 vec3_t tempdiffusenormal;
2148 // fetch the lighting from the worldmodel data
2149 VectorSet(ent->modellight_ambient, r_ambient.value * (2.0f / 128.0f), r_ambient.value * (2.0f / 128.0f), r_ambient.value * (2.0f / 128.0f));
2150 VectorClear(ent->modellight_diffuse);
2151 VectorClear(tempdiffusenormal);
2152 if ((ent->flags & RENDER_LIGHT) && r_refdef.worldmodel && r_refdef.worldmodel->brush.LightPoint)
2155 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
2156 r_refdef.worldmodel->brush.LightPoint(r_refdef.worldmodel, org, ent->modellight_ambient, ent->modellight_diffuse, tempdiffusenormal);
2159 VectorSet(ent->modellight_ambient, 1, 1, 1);
2161 // move the light direction into modelspace coordinates for lighting code
2162 Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, ent->modellight_lightdir);
2163 if(VectorLength2(ent->modellight_lightdir) > 0)
2165 VectorNormalize(ent->modellight_lightdir);
2169 VectorSet(ent->modellight_lightdir, 0, 0, 1); // have to set SOME valid vector here
2172 // scale ambient and directional light contributions according to rendering variables
2173 ent->modellight_ambient[0] *= ent->colormod[0] * r_refdef.lightmapintensity;
2174 ent->modellight_ambient[1] *= ent->colormod[1] * r_refdef.lightmapintensity;
2175 ent->modellight_ambient[2] *= ent->colormod[2] * r_refdef.lightmapintensity;
2176 ent->modellight_diffuse[0] *= ent->colormod[0] * r_refdef.lightmapintensity;
2177 ent->modellight_diffuse[1] *= ent->colormod[1] * r_refdef.lightmapintensity;
2178 ent->modellight_diffuse[2] *= ent->colormod[2] * r_refdef.lightmapintensity;
2181 static void R_View_UpdateEntityVisible (void)
2184 entity_render_t *ent;
2186 if (!r_drawentities.integer)
2189 renderimask = r_refdef.envmap ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL) : ((chase_active.integer || r_waterstate.renderingscene) ? RENDER_VIEWMODEL : RENDER_EXTERIORMODEL);
2190 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs)
2192 // worldmodel can check visibility
2193 for (i = 0;i < r_refdef.numentities;i++)
2195 ent = r_refdef.entities[i];
2196 r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs)) && ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.worldmodel, r_viewcache.world_leafvisible, ent->mins, ent->maxs));
2199 if(r_cullentities_trace.integer)
2201 for (i = 0;i < r_refdef.numentities;i++)
2203 ent = r_refdef.entities[i];
2204 if(r_viewcache.entityvisible[i] && !(ent->effects & EF_NODEPTHTEST) && !(ent->flags & RENDER_VIEWMODEL) && !(ent->model && (ent->model->name[0] == '*')))
2206 if(Mod_CanSeeBox_Trace(r_cullentities_trace_samples.integer, r_cullentities_trace_enlarge.value, r_refdef.worldmodel, r_view.origin, ent->mins, ent->maxs))
2207 ent->last_trace_visibility = realtime;
2208 if(ent->last_trace_visibility < realtime - r_cullentities_trace_delay.value)
2209 r_viewcache.entityvisible[i] = 0;
2216 // no worldmodel or it can't check visibility
2217 for (i = 0;i < r_refdef.numentities;i++)
2219 ent = r_refdef.entities[i];
2220 r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs));
2224 // update entity lighting (even on hidden entities for r_shadows)
2225 for (i = 0;i < r_refdef.numentities;i++)
2226 R_UpdateEntityLighting(r_refdef.entities[i]);
2229 // only used if skyrendermasked, and normally returns false
2230 int R_DrawBrushModelsSky (void)
2233 entity_render_t *ent;
2235 if (!r_drawentities.integer)
2239 for (i = 0;i < r_refdef.numentities;i++)
2241 if (!r_viewcache.entityvisible[i])
2243 ent = r_refdef.entities[i];
2244 if (!ent->model || !ent->model->DrawSky)
2246 ent->model->DrawSky(ent);
2252 static void R_DrawNoModel(entity_render_t *ent);
2253 static void R_DrawModels(void)
2256 entity_render_t *ent;
2258 if (!r_drawentities.integer)
2261 for (i = 0;i < r_refdef.numentities;i++)
2263 if (!r_viewcache.entityvisible[i])
2265 ent = r_refdef.entities[i];
2266 r_refdef.stats.entities++;
2267 if (ent->model && ent->model->Draw != NULL)
2268 ent->model->Draw(ent);
2274 static void R_DrawModelsDepth(void)
2277 entity_render_t *ent;
2279 if (!r_drawentities.integer)
2282 for (i = 0;i < r_refdef.numentities;i++)
2284 if (!r_viewcache.entityvisible[i])
2286 ent = r_refdef.entities[i];
2287 if (ent->model && ent->model->DrawDepth != NULL)
2288 ent->model->DrawDepth(ent);
2292 static void R_DrawModelsDebug(void)
2295 entity_render_t *ent;
2297 if (!r_drawentities.integer)
2300 for (i = 0;i < r_refdef.numentities;i++)
2302 if (!r_viewcache.entityvisible[i])
2304 ent = r_refdef.entities[i];
2305 if (ent->model && ent->model->DrawDebug != NULL)
2306 ent->model->DrawDebug(ent);
2310 static void R_DrawModelsAddWaterPlanes(void)
2313 entity_render_t *ent;
2315 if (!r_drawentities.integer)
2318 for (i = 0;i < r_refdef.numentities;i++)
2320 if (!r_viewcache.entityvisible[i])
2322 ent = r_refdef.entities[i];
2323 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
2324 ent->model->DrawAddWaterPlanes(ent);
2328 static void R_View_SetFrustum(void)
2331 double slopex, slopey;
2333 // break apart the view matrix into vectors for various purposes
2334 Matrix4x4_ToVectors(&r_view.matrix, r_view.forward, r_view.left, r_view.up, r_view.origin);
2335 VectorNegate(r_view.left, r_view.right);
2338 r_view.frustum[0].normal[0] = 0 - 1.0 / r_view.frustum_x;
2339 r_view.frustum[0].normal[1] = 0 - 0;
2340 r_view.frustum[0].normal[2] = -1 - 0;
2341 r_view.frustum[1].normal[0] = 0 + 1.0 / r_view.frustum_x;
2342 r_view.frustum[1].normal[1] = 0 + 0;
2343 r_view.frustum[1].normal[2] = -1 + 0;
2344 r_view.frustum[2].normal[0] = 0 - 0;
2345 r_view.frustum[2].normal[1] = 0 - 1.0 / r_view.frustum_y;
2346 r_view.frustum[2].normal[2] = -1 - 0;
2347 r_view.frustum[3].normal[0] = 0 + 0;
2348 r_view.frustum[3].normal[1] = 0 + 1.0 / r_view.frustum_y;
2349 r_view.frustum[3].normal[2] = -1 + 0;
2353 zNear = r_refdef.nearclip;
2354 nudge = 1.0 - 1.0 / (1<<23);
2355 r_view.frustum[4].normal[0] = 0 - 0;
2356 r_view.frustum[4].normal[1] = 0 - 0;
2357 r_view.frustum[4].normal[2] = -1 - -nudge;
2358 r_view.frustum[4].dist = 0 - -2 * zNear * nudge;
2359 r_view.frustum[5].normal[0] = 0 + 0;
2360 r_view.frustum[5].normal[1] = 0 + 0;
2361 r_view.frustum[5].normal[2] = -1 + -nudge;
2362 r_view.frustum[5].dist = 0 + -2 * zNear * nudge;
2368 r_view.frustum[0].normal[0] = m[3] - m[0];
2369 r_view.frustum[0].normal[1] = m[7] - m[4];
2370 r_view.frustum[0].normal[2] = m[11] - m[8];
2371 r_view.frustum[0].dist = m[15] - m[12];
2373 r_view.frustum[1].normal[0] = m[3] + m[0];
2374 r_view.frustum[1].normal[1] = m[7] + m[4];
2375 r_view.frustum[1].normal[2] = m[11] + m[8];
2376 r_view.frustum[1].dist = m[15] + m[12];
2378 r_view.frustum[2].normal[0] = m[3] - m[1];
2379 r_view.frustum[2].normal[1] = m[7] - m[5];
2380 r_view.frustum[2].normal[2] = m[11] - m[9];
2381 r_view.frustum[2].dist = m[15] - m[13];
2383 r_view.frustum[3].normal[0] = m[3] + m[1];
2384 r_view.frustum[3].normal[1] = m[7] + m[5];
2385 r_view.frustum[3].normal[2] = m[11] + m[9];
2386 r_view.frustum[3].dist = m[15] + m[13];
2388 r_view.frustum[4].normal[0] = m[3] - m[2];
2389 r_view.frustum[4].normal[1] = m[7] - m[6];
2390 r_view.frustum[4].normal[2] = m[11] - m[10];
2391 r_view.frustum[4].dist = m[15] - m[14];
2393 r_view.frustum[5].normal[0] = m[3] + m[2];
2394 r_view.frustum[5].normal[1] = m[7] + m[6];
2395 r_view.frustum[5].normal[2] = m[11] + m[10];
2396 r_view.frustum[5].dist = m[15] + m[14];
2399 if (r_view.useperspective)
2401 slopex = 1.0 / r_view.frustum_x;
2402 slopey = 1.0 / r_view.frustum_y;
2403 VectorMA(r_view.forward, -slopex, r_view.left, r_view.frustum[0].normal);
2404 VectorMA(r_view.forward, slopex, r_view.left, r_view.frustum[1].normal);
2405 VectorMA(r_view.forward, -slopey, r_view.up , r_view.frustum[2].normal);
2406 VectorMA(r_view.forward, slopey, r_view.up , r_view.frustum[3].normal);
2407 VectorCopy(r_view.forward, r_view.frustum[4].normal);
2409 // Leaving those out was a mistake, those were in the old code, and they
2410 // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
2411 // I couldn't reproduce it after adding those normalizations. --blub
2412 VectorNormalize(r_view.frustum[0].normal);
2413 VectorNormalize(r_view.frustum[1].normal);
2414 VectorNormalize(r_view.frustum[2].normal);
2415 VectorNormalize(r_view.frustum[3].normal);
2417 // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
2418 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[0]);
2419 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, 1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[1]);
2420 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left, 1024 * slopey, r_view.up, r_view.frustumcorner[2]);
2421 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, 1024 * slopex, r_view.left, 1024 * slopey, r_view.up, r_view.frustumcorner[3]);
2423 r_view.frustum[0].dist = DotProduct (r_view.origin, r_view.frustum[0].normal);
2424 r_view.frustum[1].dist = DotProduct (r_view.origin, r_view.frustum[1].normal);
2425 r_view.frustum[2].dist = DotProduct (r_view.origin, r_view.frustum[2].normal);
2426 r_view.frustum[3].dist = DotProduct (r_view.origin, r_view.frustum[3].normal);
2427 r_view.frustum[4].dist = DotProduct (r_view.origin, r_view.frustum[4].normal) + r_refdef.nearclip;
2431 VectorScale(r_view.left, -r_view.ortho_x, r_view.frustum[0].normal);
2432 VectorScale(r_view.left, r_view.ortho_x, r_view.frustum[1].normal);
2433 VectorScale(r_view.up, -r_view.ortho_y, r_view.frustum[2].normal);
2434 VectorScale(r_view.up, r_view.ortho_y, r_view.frustum[3].normal);
2435 VectorCopy(r_view.forward, r_view.frustum[4].normal);
2436 r_view.frustum[0].dist = DotProduct (r_view.origin, r_view.frustum[0].normal) + r_view.ortho_x;
2437 r_view.frustum[1].dist = DotProduct (r_view.origin, r_view.frustum[1].normal) + r_view.ortho_x;
2438 r_view.frustum[2].dist = DotProduct (r_view.origin, r_view.frustum[2].normal) + r_view.ortho_y;
2439 r_view.frustum[3].dist = DotProduct (r_view.origin, r_view.frustum[3].normal) + r_view.ortho_y;
2440 r_view.frustum[4].dist = DotProduct (r_view.origin, r_view.frustum[4].normal) + r_refdef.nearclip;
2442 r_view.numfrustumplanes = 5;
2444 if (r_view.useclipplane)
2446 r_view.numfrustumplanes = 6;
2447 r_view.frustum[5] = r_view.clipplane;
2450 for (i = 0;i < r_view.numfrustumplanes;i++)
2451 PlaneClassify(r_view.frustum + i);
2453 // LordHavoc: note to all quake engine coders, Quake had a special case
2454 // for 90 degrees which assumed a square view (wrong), so I removed it,
2455 // Quake2 has it disabled as well.
2457 // rotate R_VIEWFORWARD right by FOV_X/2 degrees
2458 //RotatePointAroundVector( r_view.frustum[0].normal, r_view.up, r_view.forward, -(90 - r_refdef.fov_x / 2));
2459 //r_view.frustum[0].dist = DotProduct (r_view.origin, frustum[0].normal);
2460 //PlaneClassify(&frustum[0]);
2462 // rotate R_VIEWFORWARD left by FOV_X/2 degrees
2463 //RotatePointAroundVector( r_view.frustum[1].normal, r_view.up, r_view.forward, (90 - r_refdef.fov_x / 2));
2464 //r_view.frustum[1].dist = DotProduct (r_view.origin, frustum[1].normal);
2465 //PlaneClassify(&frustum[1]);
2467 // rotate R_VIEWFORWARD up by FOV_X/2 degrees
2468 //RotatePointAroundVector( r_view.frustum[2].normal, r_view.left, r_view.forward, -(90 - r_refdef.fov_y / 2));
2469 //r_view.frustum[2].dist = DotProduct (r_view.origin, frustum[2].normal);
2470 //PlaneClassify(&frustum[2]);
2472 // rotate R_VIEWFORWARD down by FOV_X/2 degrees
2473 //RotatePointAroundVector( r_view.frustum[3].normal, r_view.left, r_view.forward, (90 - r_refdef.fov_y / 2));
2474 //r_view.frustum[3].dist = DotProduct (r_view.origin, frustum[3].normal);
2475 //PlaneClassify(&frustum[3]);
2478 //VectorCopy(r_view.forward, r_view.frustum[4].normal);
2479 //r_view.frustum[4].dist = DotProduct (r_view.origin, frustum[4].normal) + r_nearclip.value;
2480 //PlaneClassify(&frustum[4]);
2483 void R_View_Update(void)
2485 R_View_SetFrustum();
2486 R_View_WorldVisibility(r_view.useclipplane);
2487 R_View_UpdateEntityVisible();
2490 void R_SetupView(void)
2492 if (!r_view.useperspective)
2493 GL_SetupView_Mode_Ortho(-r_view.ortho_x, -r_view.ortho_y, r_view.ortho_x, r_view.ortho_y, -r_refdef.farclip, r_refdef.farclip);
2494 else if (r_refdef.rtworldshadows || r_refdef.rtdlightshadows)
2495 GL_SetupView_Mode_PerspectiveInfiniteFarClip(r_view.frustum_x, r_view.frustum_y, r_refdef.nearclip);
2497 GL_SetupView_Mode_Perspective(r_view.frustum_x, r_view.frustum_y, r_refdef.nearclip, r_refdef.farclip);
2499 GL_SetupView_Orientation_FromEntity(&r_view.matrix);
2501 if (r_view.useclipplane)
2503 // LordHavoc: couldn't figure out how to make this approach the
2504 vec_t dist = r_view.clipplane.dist - r_water_clippingplanebias.value;
2505 vec_t viewdist = DotProduct(r_view.origin, r_view.clipplane.normal);
2506 if (viewdist < r_view.clipplane.dist + r_water_clippingplanebias.value)
2507 dist = r_view.clipplane.dist;
2508 GL_SetupView_ApplyCustomNearClipPlane(r_view.clipplane.normal[0], r_view.clipplane.normal[1], r_view.clipplane.normal[2], dist);
2512 void R_ResetViewRendering2D(void)
2514 if (gl_support_fragment_shader)
2516 qglUseProgramObjectARB(0);CHECKGLERROR
2521 // GL is weird because it's bottom to top, r_view.y is top to bottom
2522 qglViewport(r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2523 GL_SetupView_Mode_Ortho(0, 0, 1, 1, -10, 100);
2524 GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
2525 GL_Color(1, 1, 1, 1);
2526 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
2527 GL_BlendFunc(GL_ONE, GL_ZERO);
2528 GL_AlphaTest(false);
2529 GL_ScissorTest(false);
2530 GL_DepthMask(false);
2531 GL_DepthRange(0, 1);
2532 GL_DepthTest(false);
2533 R_Mesh_Matrix(&identitymatrix);
2534 R_Mesh_ResetTextureState();
2535 GL_PolygonOffset(0, 0);
2536 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
2537 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2538 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2539 qglStencilMask(~0);CHECKGLERROR
2540 qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
2541 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
2542 GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
2545 void R_ResetViewRendering3D(void)
2547 if (gl_support_fragment_shader)
2549 qglUseProgramObjectARB(0);CHECKGLERROR
2554 // GL is weird because it's bottom to top, r_view.y is top to bottom
2555 qglViewport(r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2557 GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
2558 GL_Color(1, 1, 1, 1);
2559 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
2560 GL_BlendFunc(GL_ONE, GL_ZERO);
2561 GL_AlphaTest(false);
2562 GL_ScissorTest(true);
2564 GL_DepthRange(0, 1);
2566 R_Mesh_Matrix(&identitymatrix);
2567 R_Mesh_ResetTextureState();
2568 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
2569 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
2570 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2571 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2572 qglStencilMask(~0);CHECKGLERROR
2573 qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
2574 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
2575 GL_CullFace(r_view.cullface_back);
2579 R_Bloom_SetupShader(
2581 "// written by Forest 'LordHavoc' Hale\n"
2583 "// common definitions between vertex shader and fragment shader:\n"
2585 "#ifdef __GLSL_CG_DATA_TYPES\n"
2586 "#define myhalf half\n"
2587 "#define myhvec2 hvec2\n"
2588 "#define myhvec3 hvec3\n"
2589 "#define myhvec4 hvec4\n"
2591 "#define myhalf float\n"
2592 "#define myhvec2 vec2\n"
2593 "#define myhvec3 vec3\n"
2594 "#define myhvec4 vec4\n"
2597 "varying vec2 ScreenTexCoord;\n"
2598 "varying vec2 BloomTexCoord;\n"
2603 "// vertex shader specific:\n"
2604 "#ifdef VERTEX_SHADER\n"
2608 " ScreenTexCoord = vec2(gl_MultiTexCoord0);\n"
2609 " BloomTexCoord = vec2(gl_MultiTexCoord1);\n"
2610 " // transform vertex to camera space, using ftransform to match non-VS\n"
2612 " gl_Position = ftransform();\n"
2615 "#endif // VERTEX_SHADER\n"
2620 "// fragment shader specific:\n"
2621 "#ifdef FRAGMENT_SHADER\n"
2626 " myhvec3 color = myhvec3(texture2D(Texture_Screen, ScreenTexCoord));\n"
2627 " for (x = -BLUR_X;x <= BLUR_X;x++)
2628 " color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2629 " color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2630 " color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2631 " color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2633 " gl_FragColor = vec4(color);\n"
2636 "#endif // FRAGMENT_SHADER\n"
2639 void R_RenderScene(qboolean addwaterplanes);
2641 static void R_Water_StartFrame(void)
2644 int waterwidth, waterheight, texturewidth, textureheight;
2645 r_waterstate_waterplane_t *p;
2647 // set waterwidth and waterheight to the water resolution that will be
2648 // used (often less than the screen resolution for faster rendering)
2649 waterwidth = (int)bound(1, r_view.width * r_water_resolutionmultiplier.value, r_view.width);
2650 waterheight = (int)bound(1, r_view.height * r_water_resolutionmultiplier.value, r_view.height);
2652 // calculate desired texture sizes
2653 // can't use water if the card does not support the texture size
2654 if (!r_water.integer || !r_glsl.integer || !gl_support_fragment_shader || waterwidth > gl_max_texture_size || waterheight > gl_max_texture_size)
2655 texturewidth = textureheight = waterwidth = waterheight = 0;
2656 else if (gl_support_arb_texture_non_power_of_two)
2658 texturewidth = waterwidth;
2659 textureheight = waterheight;
2663 for (texturewidth = 1;texturewidth < waterwidth ;texturewidth *= 2);
2664 for (textureheight = 1;textureheight < waterheight;textureheight *= 2);
2667 // allocate textures as needed
2668 if (r_waterstate.waterwidth != waterwidth || r_waterstate.waterheight != waterheight || r_waterstate.texturewidth != texturewidth || r_waterstate.textureheight != textureheight)
2670 r_waterstate.maxwaterplanes = MAX_WATERPLANES;
2671 for (i = 0, p = r_waterstate.waterplanes;i < r_waterstate.maxwaterplanes;i++, p++)
2673 if (p->texture_refraction)
2674 R_FreeTexture(p->texture_refraction);
2675 p->texture_refraction = NULL;
2676 if (p->texture_reflection)
2677 R_FreeTexture(p->texture_reflection);
2678 p->texture_reflection = NULL;
2680 memset(&r_waterstate, 0, sizeof(r_waterstate));
2681 r_waterstate.waterwidth = waterwidth;
2682 r_waterstate.waterheight = waterheight;
2683 r_waterstate.texturewidth = texturewidth;
2684 r_waterstate.textureheight = textureheight;
2687 if (r_waterstate.waterwidth)
2689 r_waterstate.enabled = true;
2691 // set up variables that will be used in shader setup
2692 r_waterstate.screenscale[0] = 0.5f * (float)waterwidth / (float)texturewidth;
2693 r_waterstate.screenscale[1] = 0.5f * (float)waterheight / (float)textureheight;
2694 r_waterstate.screencenter[0] = 0.5f * (float)waterwidth / (float)texturewidth;
2695 r_waterstate.screencenter[1] = 0.5f * (float)waterheight / (float)textureheight;
2698 r_waterstate.maxwaterplanes = MAX_WATERPLANES;
2699 r_waterstate.numwaterplanes = 0;
2702 static void R_Water_AddWaterPlane(msurface_t *surface)
2704 int triangleindex, planeindex;
2710 r_waterstate_waterplane_t *p;
2711 // just use the first triangle with a valid normal for any decisions
2712 VectorClear(normal);
2713 VectorClear(center);
2714 for (triangleindex = 0, e = rsurface.modelelement3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
2716 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[0]*3, vert[0]);
2717 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[1]*3, vert[1]);
2718 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[2]*3, vert[2]);
2719 TriangleNormal(vert[0], vert[1], vert[2], normal);
2720 if (VectorLength2(normal) >= 0.001)
2723 // now find the center of this surface
2724 for (triangleindex = 0, e = rsurface.modelelement3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles*3;triangleindex++, e++)
2726 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[0]*3, vert[0]);
2727 VectorAdd(center, vert[0], center);
2729 f = 1.0 / surface->num_triangles*3;
2730 VectorScale(center, f, center);
2732 // find a matching plane if there is one
2733 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2734 if (fabs(PlaneDiff(vert[0], &p->plane)) < 1 && fabs(PlaneDiff(vert[1], &p->plane)) < 1 && fabs(PlaneDiff(vert[2], &p->plane)) < 1)
2736 if (planeindex >= r_waterstate.maxwaterplanes)
2737 return; // nothing we can do, out of planes
2739 // if this triangle does not fit any known plane rendered this frame, add one
2740 if (planeindex >= r_waterstate.numwaterplanes)
2742 // store the new plane
2743 r_waterstate.numwaterplanes++;
2744 VectorCopy(normal, p->plane.normal);
2745 VectorNormalize(p->plane.normal);
2746 p->plane.dist = DotProduct(vert[0], p->plane.normal);
2747 PlaneClassify(&p->plane);
2748 // flip the plane if it does not face the viewer
2749 if (PlaneDiff(r_view.origin, &p->plane) < 0)
2751 VectorNegate(p->plane.normal, p->plane.normal);
2752 p->plane.dist *= -1;
2753 PlaneClassify(&p->plane);
2755 // clear materialflags and pvs
2756 p->materialflags = 0;
2757 p->pvsvalid = false;
2759 // merge this surface's materialflags into the waterplane
2760 p->materialflags |= surface->texture->currentframe->currentmaterialflags;
2761 // merge this surface's PVS into the waterplane
2762 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) && r_refdef.worldmodel && r_refdef.worldmodel->brush.FatPVS)
2764 r_refdef.worldmodel->brush.FatPVS(r_refdef.worldmodel, r_view.origin, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
2769 static void R_Water_ProcessPlanes(void)
2771 r_view_t originalview;
2773 r_waterstate_waterplane_t *p;
2775 originalview = r_view;
2777 // make sure enough textures are allocated
2778 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2780 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
2782 if (!p->texture_refraction)
2783 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);
2784 if (!p->texture_refraction)
2788 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
2790 if (!p->texture_reflection)
2791 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);
2792 if (!p->texture_reflection)
2798 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2800 r_view.showdebug = false;
2801 r_view.width = r_waterstate.waterwidth;
2802 r_view.height = r_waterstate.waterheight;
2803 r_view.useclipplane = true;
2804 r_waterstate.renderingscene = true;
2806 // render the normal view scene and copy into texture
2807 // (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)
2808 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
2810 r_view.clipplane = p->plane;
2811 VectorNegate(r_view.clipplane.normal, r_view.clipplane.normal);
2812 r_view.clipplane.dist = -r_view.clipplane.dist;
2813 PlaneClassify(&r_view.clipplane);
2815 R_RenderScene(false);
2817 // copy view into the screen texture
2818 R_Mesh_TexBind(0, R_GetTexture(p->texture_refraction));
2819 GL_ActiveTexture(0);
2821 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2824 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
2826 // render reflected scene and copy into texture
2827 Matrix4x4_Reflect(&r_view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
2828 r_view.clipplane = p->plane;
2829 // reverse the cullface settings for this render
2830 r_view.cullface_front = GL_FRONT;
2831 r_view.cullface_back = GL_BACK;
2832 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.num_pvsclusterbytes)
2834 r_view.usecustompvs = true;
2836 memcpy(r_viewcache.world_pvsbits, p->pvsbits, r_refdef.worldmodel->brush.num_pvsclusterbytes);
2838 memset(r_viewcache.world_pvsbits, 0xFF, r_refdef.worldmodel->brush.num_pvsclusterbytes);
2841 R_ResetViewRendering3D();
2843 if (r_timereport_active)
2844 R_TimeReport("viewclear");
2846 R_RenderScene(false);
2848 R_Mesh_TexBind(0, R_GetTexture(p->texture_reflection));
2849 GL_ActiveTexture(0);
2851 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2853 R_ResetViewRendering3D();
2855 if (r_timereport_active)
2856 R_TimeReport("viewclear");
2859 r_view = originalview;
2860 r_view.clear = true;
2861 r_waterstate.renderingscene = false;
2865 r_view = originalview;
2866 r_waterstate.renderingscene = false;
2867 Cvar_SetValueQuick(&r_water, 0);
2868 Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed! Turned off r_water.\n");
2872 void R_Bloom_StartFrame(void)
2874 int bloomtexturewidth, bloomtextureheight, screentexturewidth, screentextureheight;
2876 // set bloomwidth and bloomheight to the bloom resolution that will be
2877 // used (often less than the screen resolution for faster rendering)
2878 r_bloomstate.bloomwidth = bound(1, r_bloom_resolution.integer, r_view.width);
2879 r_bloomstate.bloomheight = r_bloomstate.bloomwidth * r_view.height / r_view.width;
2880 r_bloomstate.bloomheight = bound(1, r_bloomstate.bloomheight, r_view.height);
2882 // calculate desired texture sizes
2883 if (gl_support_arb_texture_non_power_of_two)
2885 screentexturewidth = r_view.width;
2886 screentextureheight = r_view.height;
2887 bloomtexturewidth = r_bloomstate.bloomwidth;
2888 bloomtextureheight = r_bloomstate.bloomheight;
2892 for (screentexturewidth = 1;screentexturewidth < vid.width ;screentexturewidth *= 2);
2893 for (screentextureheight = 1;screentextureheight < vid.height ;screentextureheight *= 2);
2894 for (bloomtexturewidth = 1;bloomtexturewidth < r_bloomstate.bloomwidth ;bloomtexturewidth *= 2);
2895 for (bloomtextureheight = 1;bloomtextureheight < r_bloomstate.bloomheight;bloomtextureheight *= 2);
2900 screentexturewidth = screentextureheight = 0;
2902 else if (r_bloom.integer)
2907 screentexturewidth = screentextureheight = 0;
2908 bloomtexturewidth = bloomtextureheight = 0;
2911 if ((!bloomtexturewidth && !bloomtextureheight) || r_bloom_resolution.integer < 4 || r_bloom_blur.value < 1 || r_bloom_blur.value >= 512 || screentexturewidth > gl_max_texture_size || screentextureheight > gl_max_texture_size || bloomtexturewidth > gl_max_texture_size || bloomtextureheight > gl_max_texture_size)
2913 // can't use bloom if the parameters are too weird
2914 // can't use bloom if the card does not support the texture size
2915 if (r_bloomstate.texture_screen)
2916 R_FreeTexture(r_bloomstate.texture_screen);
2917 if (r_bloomstate.texture_bloom)
2918 R_FreeTexture(r_bloomstate.texture_bloom);
2919 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
2923 r_bloomstate.enabled = true;
2924 r_bloomstate.hdr = r_hdr.integer != 0;
2926 // allocate textures as needed
2927 if (r_bloomstate.screentexturewidth != screentexturewidth || r_bloomstate.screentextureheight != screentextureheight)
2929 if (r_bloomstate.texture_screen)
2930 R_FreeTexture(r_bloomstate.texture_screen);
2931 r_bloomstate.texture_screen = NULL;
2932 r_bloomstate.screentexturewidth = screentexturewidth;
2933 r_bloomstate.screentextureheight = screentextureheight;
2934 if (r_bloomstate.screentexturewidth && r_bloomstate.screentextureheight)
2935 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);
2937 if (r_bloomstate.bloomtexturewidth != bloomtexturewidth || r_bloomstate.bloomtextureheight != bloomtextureheight)
2939 if (r_bloomstate.texture_bloom)
2940 R_FreeTexture(r_bloomstate.texture_bloom);
2941 r_bloomstate.texture_bloom = NULL;
2942 r_bloomstate.bloomtexturewidth = bloomtexturewidth;
2943 r_bloomstate.bloomtextureheight = bloomtextureheight;
2944 if (r_bloomstate.bloomtexturewidth && r_bloomstate.bloomtextureheight)
2945 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);
2948 // set up a texcoord array for the full resolution screen image
2949 // (we have to keep this around to copy back during final render)
2950 r_bloomstate.screentexcoord2f[0] = 0;
2951 r_bloomstate.screentexcoord2f[1] = (float)r_view.height / (float)r_bloomstate.screentextureheight;
2952 r_bloomstate.screentexcoord2f[2] = (float)r_view.width / (float)r_bloomstate.screentexturewidth;
2953 r_bloomstate.screentexcoord2f[3] = (float)r_view.height / (float)r_bloomstate.screentextureheight;
2954 r_bloomstate.screentexcoord2f[4] = (float)r_view.width / (float)r_bloomstate.screentexturewidth;
2955 r_bloomstate.screentexcoord2f[5] = 0;
2956 r_bloomstate.screentexcoord2f[6] = 0;
2957 r_bloomstate.screentexcoord2f[7] = 0;
2959 // set up a texcoord array for the reduced resolution bloom image
2960 // (which will be additive blended over the screen image)
2961 r_bloomstate.bloomtexcoord2f[0] = 0;
2962 r_bloomstate.bloomtexcoord2f[1] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
2963 r_bloomstate.bloomtexcoord2f[2] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
2964 r_bloomstate.bloomtexcoord2f[3] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
2965 r_bloomstate.bloomtexcoord2f[4] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
2966 r_bloomstate.bloomtexcoord2f[5] = 0;
2967 r_bloomstate.bloomtexcoord2f[6] = 0;
2968 r_bloomstate.bloomtexcoord2f[7] = 0;
2971 void R_Bloom_CopyScreenTexture(float colorscale)
2973 r_refdef.stats.bloom++;
2975 R_ResetViewRendering2D();
2976 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
2977 R_Mesh_ColorPointer(NULL, 0, 0);
2978 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
2979 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
2981 // copy view into the screen texture
2982 GL_ActiveTexture(0);
2984 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2985 r_refdef.stats.bloom_copypixels += r_view.width * r_view.height;
2987 // now scale it down to the bloom texture size
2989 qglViewport(r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
2990 GL_BlendFunc(GL_ONE, GL_ZERO);
2991 GL_Color(colorscale, colorscale, colorscale, 1);
2992 // TODO: optimize with multitexture or GLSL
2993 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2994 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
2996 // we now have a bloom image in the framebuffer
2997 // copy it into the bloom image texture for later processing
2998 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
2999 GL_ActiveTexture(0);
3001 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3002 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3005 void R_Bloom_CopyHDRTexture(void)
3007 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3008 GL_ActiveTexture(0);
3010 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
3011 r_refdef.stats.bloom_copypixels += r_view.width * r_view.height;
3014 void R_Bloom_MakeTexture(void)
3017 float xoffset, yoffset, r, brighten;
3019 r_refdef.stats.bloom++;
3021 R_ResetViewRendering2D();
3022 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3023 R_Mesh_ColorPointer(NULL, 0, 0);
3025 // we have a bloom image in the framebuffer
3027 qglViewport(r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3029 for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
3032 r = bound(0, r_bloom_colorexponent.value / x, 1);
3033 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
3034 GL_Color(r, r, r, 1);
3035 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3036 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3037 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3038 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3040 // copy the vertically blurred bloom view to a texture
3041 GL_ActiveTexture(0);
3043 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3044 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3047 range = r_bloom_blur.integer * r_bloomstate.bloomwidth / 320;
3048 brighten = r_bloom_brighten.value;
3050 brighten *= r_hdr_range.value;
3051 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3052 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.offsettexcoord2f, 0, 0);
3054 for (dir = 0;dir < 2;dir++)
3056 // blend on at multiple vertical offsets to achieve a vertical blur
3057 // TODO: do offset blends using GLSL
3058 GL_BlendFunc(GL_ONE, GL_ZERO);
3059 for (x = -range;x <= range;x++)
3061 if (!dir){xoffset = 0;yoffset = x;}
3062 else {xoffset = x;yoffset = 0;}
3063 xoffset /= (float)r_bloomstate.bloomtexturewidth;
3064 yoffset /= (float)r_bloomstate.bloomtextureheight;
3065 // compute a texcoord array with the specified x and y offset
3066 r_bloomstate.offsettexcoord2f[0] = xoffset+0;
3067 r_bloomstate.offsettexcoord2f[1] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3068 r_bloomstate.offsettexcoord2f[2] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3069 r_bloomstate.offsettexcoord2f[3] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3070 r_bloomstate.offsettexcoord2f[4] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3071 r_bloomstate.offsettexcoord2f[5] = yoffset+0;
3072 r_bloomstate.offsettexcoord2f[6] = xoffset+0;
3073 r_bloomstate.offsettexcoord2f[7] = yoffset+0;
3074 // this r value looks like a 'dot' particle, fading sharply to
3075 // black at the edges
3076 // (probably not realistic but looks good enough)
3077 //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
3078 //r = (dir ? 1.0f : brighten)/(range*2+1);
3079 r = (dir ? 1.0f : brighten)/(range*2+1)*(1 - x*x/(float)(range*range));
3080 GL_Color(r, r, r, 1);
3081 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3082 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3083 GL_BlendFunc(GL_ONE, GL_ONE);
3086 // copy the vertically blurred bloom view to a texture
3087 GL_ActiveTexture(0);
3089 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3090 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3093 // apply subtract last
3094 // (just like it would be in a GLSL shader)
3095 if (r_bloom_colorsubtract.value > 0 && gl_support_ext_blend_subtract)
3097 GL_BlendFunc(GL_ONE, GL_ZERO);
3098 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3099 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3100 GL_Color(1, 1, 1, 1);
3101 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3102 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3104 GL_BlendFunc(GL_ONE, GL_ONE);
3105 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
3106 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
3107 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3108 GL_Color(r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 1);
3109 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3110 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3111 qglBlendEquationEXT(GL_FUNC_ADD_EXT);
3113 // copy the darkened bloom view to a texture
3114 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3115 GL_ActiveTexture(0);
3117 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3118 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3122 void R_HDR_RenderBloomTexture(void)
3124 int oldwidth, oldheight;
3126 oldwidth = r_view.width;
3127 oldheight = r_view.height;
3128 r_view.width = r_bloomstate.bloomwidth;
3129 r_view.height = r_bloomstate.bloomheight;
3131 // TODO: support GL_EXT_framebuffer_object rather than reusing the framebuffer? it might improve SLI performance.
3132 // TODO: add exposure compensation features
3133 // TODO: add fp16 framebuffer support
3135 r_view.showdebug = false;
3136 r_view.colorscale = r_bloom_colorscale.value * r_hdr_scenebrightness.value;
3138 r_view.colorscale /= r_hdr_range.value;
3139 r_waterstate.numwaterplanes = 0;
3140 R_RenderScene(r_waterstate.enabled);
3141 r_view.showdebug = true;
3143 R_ResetViewRendering2D();
3145 R_Bloom_CopyHDRTexture();
3146 R_Bloom_MakeTexture();
3148 R_ResetViewRendering3D();
3151 if (r_timereport_active)
3152 R_TimeReport("viewclear");
3154 // restore the view settings
3155 r_view.width = oldwidth;
3156 r_view.height = oldheight;
3159 static void R_BlendView(void)
3161 if (r_bloomstate.enabled && r_bloomstate.hdr)
3163 // render high dynamic range bloom effect
3164 // the bloom texture was made earlier this render, so we just need to
3165 // blend it onto the screen...
3166 R_ResetViewRendering2D();
3167 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3168 R_Mesh_ColorPointer(NULL, 0, 0);
3169 GL_Color(1, 1, 1, 1);
3170 GL_BlendFunc(GL_ONE, GL_ONE);
3171 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3172 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3173 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3174 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3176 else if (r_bloomstate.enabled)
3178 // render simple bloom effect
3179 // copy the screen and shrink it and darken it for the bloom process
3180 R_Bloom_CopyScreenTexture(r_bloom_colorscale.value);
3181 // make the bloom texture
3182 R_Bloom_MakeTexture();
3183 // put the original screen image back in place and blend the bloom
3185 R_ResetViewRendering2D();
3186 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3187 R_Mesh_ColorPointer(NULL, 0, 0);
3188 GL_Color(1, 1, 1, 1);
3189 GL_BlendFunc(GL_ONE, GL_ZERO);
3190 // do both in one pass if possible
3191 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3192 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3193 if (r_textureunits.integer >= 2 && gl_combine.integer)
3195 R_Mesh_TexCombine(1, GL_ADD, GL_ADD, 1, 1);
3196 R_Mesh_TexBind(1, R_GetTexture(r_bloomstate.texture_screen));
3197 R_Mesh_TexCoordPointer(1, 2, r_bloomstate.screentexcoord2f, 0, 0);
3201 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3202 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3203 // now blend on the bloom texture
3204 GL_BlendFunc(GL_ONE, GL_ONE);
3205 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
3206 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
3208 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3209 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3211 if (r_refdef.viewblend[3] >= (1.0f / 256.0f))
3213 // apply a color tint to the whole view
3214 R_ResetViewRendering2D();
3215 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3216 R_Mesh_ColorPointer(NULL, 0, 0);
3217 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3218 GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
3219 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3223 void R_RenderScene(qboolean addwaterplanes);
3225 matrix4x4_t r_waterscrollmatrix;
3227 void R_UpdateVariables(void)
3231 r_refdef.farclip = 4096;
3232 if (r_refdef.worldmodel)
3233 r_refdef.farclip += VectorDistance(r_refdef.worldmodel->normalmins, r_refdef.worldmodel->normalmaxs);
3234 r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
3236 if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
3237 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
3238 r_refdef.polygonfactor = 0;
3239 r_refdef.polygonoffset = 0;
3240 r_refdef.shadowpolygonfactor = r_refdef.polygonfactor + r_shadow_polygonfactor.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
3241 r_refdef.shadowpolygonoffset = r_refdef.polygonoffset + r_shadow_polygonoffset.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
3243 r_refdef.rtworld = r_shadow_realtime_world.integer;
3244 r_refdef.rtworldshadows = r_shadow_realtime_world_shadows.integer && gl_stencil;
3245 r_refdef.rtdlight = (r_shadow_realtime_world.integer || r_shadow_realtime_dlight.integer) && !gl_flashblend.integer && r_dynamic.integer;
3246 r_refdef.rtdlightshadows = r_refdef.rtdlight && r_shadow_realtime_dlight_shadows.integer && gl_stencil;
3247 r_refdef.lightmapintensity = r_refdef.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
3248 if (r_showsurfaces.integer)
3250 r_refdef.rtworld = false;
3251 r_refdef.rtworldshadows = false;
3252 r_refdef.rtdlight = false;
3253 r_refdef.rtdlightshadows = false;
3254 r_refdef.lightmapintensity = 0;
3257 if (gamemode == GAME_NEHAHRA)
3259 if (gl_fogenable.integer)
3261 r_refdef.oldgl_fogenable = true;
3262 r_refdef.fog_density = gl_fogdensity.value;
3263 r_refdef.fog_red = gl_fogred.value;
3264 r_refdef.fog_green = gl_foggreen.value;
3265 r_refdef.fog_blue = gl_fogblue.value;
3267 else if (r_refdef.oldgl_fogenable)
3269 r_refdef.oldgl_fogenable = false;
3270 r_refdef.fog_density = 0;
3271 r_refdef.fog_red = 0;
3272 r_refdef.fog_green = 0;
3273 r_refdef.fog_blue = 0;
3276 if (r_refdef.fog_density)
3278 r_refdef.fogcolor[0] = bound(0.0f, r_refdef.fog_red , 1.0f);
3279 r_refdef.fogcolor[1] = bound(0.0f, r_refdef.fog_green, 1.0f);
3280 r_refdef.fogcolor[2] = bound(0.0f, r_refdef.fog_blue , 1.0f);
3282 if (r_refdef.fog_density)
3284 r_refdef.fogenabled = true;
3285 // this is the point where the fog reaches 0.9986 alpha, which we
3286 // consider a good enough cutoff point for the texture
3287 // (0.9986 * 256 == 255.6)
3288 r_refdef.fogrange = 400 / r_refdef.fog_density;
3289 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
3290 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
3291 // fog color was already set
3294 r_refdef.fogenabled = false;
3302 void R_RenderView(void)
3304 if (!r_refdef.entities/* || !r_refdef.worldmodel*/)
3305 return; //Host_Error ("R_RenderView: NULL worldmodel");
3307 R_Shadow_UpdateWorldLightSelection();
3309 R_Bloom_StartFrame();
3310 R_Water_StartFrame();
3313 if (r_timereport_active)
3314 R_TimeReport("viewsetup");
3316 R_ResetViewRendering3D();
3321 if (r_timereport_active)
3322 R_TimeReport("viewclear");
3324 r_view.clear = true;
3326 r_view.showdebug = true;
3328 // this produces a bloom texture to be used in R_BlendView() later
3330 R_HDR_RenderBloomTexture();
3332 r_view.colorscale = r_hdr_scenebrightness.value;
3333 r_waterstate.numwaterplanes = 0;
3334 R_RenderScene(r_waterstate.enabled);
3337 if (r_timereport_active)
3338 R_TimeReport("blendview");
3340 GL_Scissor(0, 0, vid.width, vid.height);
3341 GL_ScissorTest(false);
3345 extern void R_DrawLightningBeams (void);
3346 extern void VM_CL_AddPolygonsToMeshQueue (void);
3347 extern void R_DrawPortals (void);
3348 extern cvar_t cl_locs_show;
3349 static void R_DrawLocs(void);
3350 static void R_DrawEntityBBoxes(void);
3351 void R_RenderScene(qboolean addwaterplanes)
3355 R_ResetViewRendering3D();
3358 if (r_timereport_active)
3359 R_TimeReport("watervis");
3361 if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->DrawAddWaterPlanes)
3363 r_refdef.worldmodel->DrawAddWaterPlanes(r_refdef.worldentity);
3364 if (r_timereport_active)
3365 R_TimeReport("waterworld");
3368 // don't let sound skip if going slow
3369 if (r_refdef.extraupdate)
3372 R_DrawModelsAddWaterPlanes();
3373 if (r_timereport_active)
3374 R_TimeReport("watermodels");
3376 R_Water_ProcessPlanes();
3377 if (r_timereport_active)
3378 R_TimeReport("waterscenes");
3381 R_ResetViewRendering3D();
3383 // don't let sound skip if going slow
3384 if (r_refdef.extraupdate)
3387 R_MeshQueue_BeginScene();
3392 if (r_timereport_active)
3393 R_TimeReport("visibility");
3395 Matrix4x4_CreateTranslate(&r_waterscrollmatrix, sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
3397 if (cl.csqc_vidvars.drawworld)
3399 // don't let sound skip if going slow
3400 if (r_refdef.extraupdate)
3403 if (r_refdef.worldmodel && r_refdef.worldmodel->DrawSky)
3405 r_refdef.worldmodel->DrawSky(r_refdef.worldentity);
3406 if (r_timereport_active)
3407 R_TimeReport("worldsky");
3410 if (R_DrawBrushModelsSky() && r_timereport_active)
3411 R_TimeReport("bmodelsky");
3414 if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->DrawDepth)
3416 r_refdef.worldmodel->DrawDepth(r_refdef.worldentity);
3417 if (r_timereport_active)
3418 R_TimeReport("worlddepth");
3420 if (r_depthfirst.integer >= 2)
3422 R_DrawModelsDepth();
3423 if (r_timereport_active)
3424 R_TimeReport("modeldepth");
3427 if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->Draw)
3429 r_refdef.worldmodel->Draw(r_refdef.worldentity);
3430 if (r_timereport_active)
3431 R_TimeReport("world");
3434 // don't let sound skip if going slow
3435 if (r_refdef.extraupdate)
3439 if (r_timereport_active)
3440 R_TimeReport("models");
3442 // don't let sound skip if going slow
3443 if (r_refdef.extraupdate)
3446 if (r_shadows.integer > 0 && r_refdef.lightmapintensity > 0)
3448 R_DrawModelShadows();
3450 R_ResetViewRendering3D();
3452 // don't let sound skip if going slow
3453 if (r_refdef.extraupdate)
3457 R_ShadowVolumeLighting(false);
3458 if (r_timereport_active)
3459 R_TimeReport("rtlights");
3461 // don't let sound skip if going slow
3462 if (r_refdef.extraupdate)
3465 if (cl.csqc_vidvars.drawworld)
3467 R_DrawLightningBeams();
3468 if (r_timereport_active)
3469 R_TimeReport("lightning");
3472 if (r_timereport_active)
3473 R_TimeReport("decals");
3476 if (r_timereport_active)
3477 R_TimeReport("particles");
3480 if (r_timereport_active)
3481 R_TimeReport("explosions");
3484 if (gl_support_fragment_shader)
3486 qglUseProgramObjectARB(0);CHECKGLERROR
3488 VM_CL_AddPolygonsToMeshQueue();
3490 if (r_view.showdebug)
3492 if (cl_locs_show.integer)
3495 if (r_timereport_active)
3496 R_TimeReport("showlocs");
3499 if (r_drawportals.integer)
3502 if (r_timereport_active)
3503 R_TimeReport("portals");
3506 if (r_showbboxes.value > 0)
3508 R_DrawEntityBBoxes();
3509 if (r_timereport_active)
3510 R_TimeReport("bboxes");
3514 if (gl_support_fragment_shader)
3516 qglUseProgramObjectARB(0);CHECKGLERROR
3518 R_MeshQueue_RenderTransparent();
3519 if (r_timereport_active)
3520 R_TimeReport("drawtrans");
3522 if (gl_support_fragment_shader)
3524 qglUseProgramObjectARB(0);CHECKGLERROR
3527 if (r_view.showdebug && r_refdef.worldmodel && r_refdef.worldmodel->DrawDebug && (r_showtris.value > 0 || r_shownormals.value > 0 || r_showcollisionbrushes.value > 0))
3529 r_refdef.worldmodel->DrawDebug(r_refdef.worldentity);
3530 if (r_timereport_active)
3531 R_TimeReport("worlddebug");
3532 R_DrawModelsDebug();
3533 if (r_timereport_active)
3534 R_TimeReport("modeldebug");
3537 if (gl_support_fragment_shader)
3539 qglUseProgramObjectARB(0);CHECKGLERROR
3542 if (cl.csqc_vidvars.drawworld)
3545 if (r_timereport_active)
3546 R_TimeReport("coronas");
3549 // don't let sound skip if going slow
3550 if (r_refdef.extraupdate)
3553 R_ResetViewRendering2D();
3556 static const int bboxelements[36] =
3566 void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
3569 float *v, *c, f1, f2, vertex3f[8*3], color4f[8*4];
3570 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3571 GL_DepthMask(false);
3572 GL_DepthRange(0, 1);
3573 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3574 R_Mesh_Matrix(&identitymatrix);
3575 R_Mesh_ResetTextureState();
3577 vertex3f[ 0] = mins[0];vertex3f[ 1] = mins[1];vertex3f[ 2] = mins[2]; //
3578 vertex3f[ 3] = maxs[0];vertex3f[ 4] = mins[1];vertex3f[ 5] = mins[2];
3579 vertex3f[ 6] = mins[0];vertex3f[ 7] = maxs[1];vertex3f[ 8] = mins[2];
3580 vertex3f[ 9] = maxs[0];vertex3f[10] = maxs[1];vertex3f[11] = mins[2];
3581 vertex3f[12] = mins[0];vertex3f[13] = mins[1];vertex3f[14] = maxs[2];
3582 vertex3f[15] = maxs[0];vertex3f[16] = mins[1];vertex3f[17] = maxs[2];
3583 vertex3f[18] = mins[0];vertex3f[19] = maxs[1];vertex3f[20] = maxs[2];
3584 vertex3f[21] = maxs[0];vertex3f[22] = maxs[1];vertex3f[23] = maxs[2];
3585 R_FillColors(color4f, 8, cr, cg, cb, ca);
3586 if (r_refdef.fogenabled)
3588 for (i = 0, v = vertex3f, c = color4f;i < 8;i++, v += 3, c += 4)
3590 f1 = FogPoint_World(v);
3592 c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
3593 c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
3594 c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
3597 R_Mesh_VertexPointer(vertex3f, 0, 0);
3598 R_Mesh_ColorPointer(color4f, 0, 0);
3599 R_Mesh_ResetTextureState();
3600 R_Mesh_Draw(0, 8, 12, bboxelements, 0, 0);
3603 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3607 prvm_edict_t *edict;
3608 // this function draws bounding boxes of server entities
3612 for (i = 0;i < numsurfaces;i++)
3614 edict = PRVM_EDICT_NUM(surfacelist[i]);
3615 switch ((int)edict->fields.server->solid)
3617 case SOLID_NOT: Vector4Set(color, 1, 1, 1, 0.05);break;
3618 case SOLID_TRIGGER: Vector4Set(color, 1, 0, 1, 0.10);break;
3619 case SOLID_BBOX: Vector4Set(color, 0, 1, 0, 0.10);break;
3620 case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
3621 case SOLID_BSP: Vector4Set(color, 0, 0, 1, 0.05);break;
3622 default: Vector4Set(color, 0, 0, 0, 0.50);break;
3624 color[3] *= r_showbboxes.value;
3625 color[3] = bound(0, color[3], 1);
3626 GL_DepthTest(!r_showdisabledepthtest.integer);
3627 GL_CullFace(r_view.cullface_front);
3628 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
3633 static void R_DrawEntityBBoxes(void)
3636 prvm_edict_t *edict;
3638 // this function draws bounding boxes of server entities
3642 for (i = 0;i < prog->num_edicts;i++)
3644 edict = PRVM_EDICT_NUM(i);
3645 if (edict->priv.server->free)
3647 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
3648 R_MeshQueue_AddTransparent(center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)NULL);
3653 int nomodelelements[24] =
3665 float nomodelvertex3f[6*3] =
3675 float nomodelcolor4f[6*4] =
3677 0.0f, 0.0f, 0.5f, 1.0f,
3678 0.0f, 0.0f, 0.5f, 1.0f,
3679 0.0f, 0.5f, 0.0f, 1.0f,
3680 0.0f, 0.5f, 0.0f, 1.0f,
3681 0.5f, 0.0f, 0.0f, 1.0f,
3682 0.5f, 0.0f, 0.0f, 1.0f
3685 void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3690 // this is only called once per entity so numsurfaces is always 1, and
3691 // surfacelist is always {0}, so this code does not handle batches
3692 R_Mesh_Matrix(&ent->matrix);
3694 if (ent->flags & EF_ADDITIVE)
3696 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
3697 GL_DepthMask(false);
3699 else if (ent->alpha < 1)
3701 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3702 GL_DepthMask(false);
3706 GL_BlendFunc(GL_ONE, GL_ZERO);
3709 GL_DepthRange(0, (ent->flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
3710 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3711 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
3712 GL_CullFace((ent->effects & EF_DOUBLESIDED) ? GL_NONE : r_view.cullface_back);
3713 R_Mesh_VertexPointer(nomodelvertex3f, 0, 0);
3714 if (r_refdef.fogenabled)
3717 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
3718 R_Mesh_ColorPointer(color4f, 0, 0);
3719 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3720 f1 = FogPoint_World(org);
3722 for (i = 0, c = color4f;i < 6;i++, c += 4)
3724 c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
3725 c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
3726 c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
3730 else if (ent->alpha != 1)
3732 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
3733 R_Mesh_ColorPointer(color4f, 0, 0);
3734 for (i = 0, c = color4f;i < 6;i++, c += 4)
3738 R_Mesh_ColorPointer(nomodelcolor4f, 0, 0);
3739 R_Mesh_ResetTextureState();
3740 R_Mesh_Draw(0, 6, 8, nomodelelements, 0, 0);
3743 void R_DrawNoModel(entity_render_t *ent)
3746 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3747 //if ((ent->effects & EF_ADDITIVE) || (ent->alpha < 1))
3748 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_view.origin : org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
3750 // R_DrawNoModelCallback(ent, 0);
3753 void R_CalcBeam_Vertex3f (float *vert, const vec3_t org1, const vec3_t org2, float width)
3755 vec3_t right1, right2, diff, normal;
3757 VectorSubtract (org2, org1, normal);
3759 // calculate 'right' vector for start
3760 VectorSubtract (r_view.origin, org1, diff);
3761 CrossProduct (normal, diff, right1);
3762 VectorNormalize (right1);
3764 // calculate 'right' vector for end
3765 VectorSubtract (r_view.origin, org2, diff);
3766 CrossProduct (normal, diff, right2);
3767 VectorNormalize (right2);
3769 vert[ 0] = org1[0] + width * right1[0];
3770 vert[ 1] = org1[1] + width * right1[1];
3771 vert[ 2] = org1[2] + width * right1[2];
3772 vert[ 3] = org1[0] - width * right1[0];
3773 vert[ 4] = org1[1] - width * right1[1];
3774 vert[ 5] = org1[2] - width * right1[2];
3775 vert[ 6] = org2[0] - width * right2[0];
3776 vert[ 7] = org2[1] - width * right2[1];
3777 vert[ 8] = org2[2] - width * right2[2];
3778 vert[ 9] = org2[0] + width * right2[0];
3779 vert[10] = org2[1] + width * right2[1];
3780 vert[11] = org2[2] + width * right2[2];
3783 float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
3785 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)
3790 if (r_refdef.fogenabled)
3791 fog = FogPoint_World(origin);
3793 R_Mesh_Matrix(&identitymatrix);
3794 GL_BlendFunc(blendfunc1, blendfunc2);
3800 GL_CullFace(r_view.cullface_front);
3803 GL_CullFace(r_view.cullface_back);
3805 GL_DepthMask(false);
3806 GL_DepthRange(0, depthshort ? 0.0625 : 1);
3807 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3808 GL_DepthTest(!depthdisable);
3810 vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
3811 vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
3812 vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
3813 vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
3814 vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
3815 vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
3816 vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
3817 vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
3818 vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
3819 vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
3820 vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
3821 vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
3823 R_Mesh_VertexPointer(vertex3f, 0, 0);
3824 R_Mesh_ColorPointer(NULL, 0, 0);
3825 R_Mesh_ResetTextureState();
3826 R_Mesh_TexBind(0, R_GetTexture(texture));
3827 R_Mesh_TexCoordPointer(0, 2, spritetexcoord2f, 0, 0);
3828 // FIXME: fixed function path can't properly handle r_view.colorscale > 1
3829 GL_Color(cr * fog * r_view.colorscale, cg * fog * r_view.colorscale, cb * fog * r_view.colorscale, ca);
3830 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3832 if (blendfunc2 == GL_ONE_MINUS_SRC_ALPHA)
3834 R_Mesh_TexBind(0, R_GetTexture(fogtexture));
3835 GL_BlendFunc(blendfunc1, GL_ONE);
3837 GL_Color(r_refdef.fogcolor[0] * fog * r_view.colorscale, r_refdef.fogcolor[1] * fog * r_view.colorscale, r_refdef.fogcolor[2] * fog * r_view.colorscale, ca);
3838 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3842 int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
3847 VectorSet(v, x, y, z);
3848 for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
3849 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
3851 if (i == mesh->numvertices)
3853 if (mesh->numvertices < mesh->maxvertices)
3855 VectorCopy(v, vertex3f);
3856 mesh->numvertices++;
3858 return mesh->numvertices;
3864 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
3868 element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
3869 element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
3870 e = mesh->element3i + mesh->numtriangles * 3;
3871 for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
3873 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
3874 if (mesh->numtriangles < mesh->maxtriangles)
3879 mesh->numtriangles++;
3881 element[1] = element[2];
3885 void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
3889 element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
3890 element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
3891 e = mesh->element3i + mesh->numtriangles * 3;
3892 for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
3894 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
3895 if (mesh->numtriangles < mesh->maxtriangles)
3900 mesh->numtriangles++;
3902 element[1] = element[2];
3906 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
3907 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
3909 int planenum, planenum2;
3912 mplane_t *plane, *plane2;
3914 double temppoints[2][256*3];
3915 // figure out how large a bounding box we need to properly compute this brush
3917 for (w = 0;w < numplanes;w++)
3918 maxdist = max(maxdist, planes[w].dist);
3919 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
3920 maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
3921 for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
3925 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
3926 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
3928 if (planenum2 == planenum)
3930 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);
3933 if (tempnumpoints < 3)
3935 // generate elements forming a triangle fan for this polygon
3936 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
3940 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)
3942 texturelayer_t *layer;
3943 layer = t->currentlayers + t->currentnumlayers++;
3945 layer->depthmask = depthmask;
3946 layer->blendfunc1 = blendfunc1;
3947 layer->blendfunc2 = blendfunc2;
3948 layer->texture = texture;
3949 layer->texmatrix = *matrix;
3950 layer->color[0] = r * r_view.colorscale;
3951 layer->color[1] = g * r_view.colorscale;
3952 layer->color[2] = b * r_view.colorscale;
3953 layer->color[3] = a;
3956 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
3959 index = parms[2] + r_refdef.time * parms[3];
3960 index -= floor(index);
3964 case Q3WAVEFUNC_NONE:
3965 case Q3WAVEFUNC_NOISE:
3966 case Q3WAVEFUNC_COUNT:
3969 case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
3970 case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
3971 case Q3WAVEFUNC_SAWTOOTH: f = index;break;
3972 case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
3973 case Q3WAVEFUNC_TRIANGLE:
3975 f = index - floor(index);
3986 return (float)(parms[0] + parms[1] * f);
3989 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
3992 model_t *model = ent->model;
3995 q3shaderinfo_layer_tcmod_t *tcmod;
3997 // switch to an alternate material if this is a q1bsp animated material
3999 texture_t *texture = t;
4000 int s = ent->skinnum;
4001 if ((unsigned int)s >= (unsigned int)model->numskins)
4003 if (model->skinscenes)
4005 if (model->skinscenes[s].framecount > 1)
4006 s = model->skinscenes[s].firstframe + (unsigned int) (r_refdef.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
4008 s = model->skinscenes[s].firstframe;
4011 t = t + s * model->num_surfaces;
4014 // use an alternate animation if the entity's frame is not 0,
4015 // and only if the texture has an alternate animation
4016 if (ent->frame2 != 0 && t->anim_total[1])
4017 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[1]) : 0];
4019 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[0]) : 0];
4021 texture->currentframe = t;
4024 // update currentskinframe to be a qw skin or animation frame
4025 if ((i = ent->entitynumber - 1) >= 0 && i < cl.maxclients)
4027 if (strcmp(r_qwskincache[i], cl.scores[i].qw_skin))
4029 strlcpy(r_qwskincache[i], cl.scores[i].qw_skin, sizeof(r_qwskincache[i]));
4030 Con_DPrintf("loading skins/%s\n", r_qwskincache[i]);
4031 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);
4033 t->currentskinframe = r_qwskincache_skinframe[i];
4034 if (t->currentskinframe == NULL)
4035 t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->frame2time)) % t->numskinframes];
4037 else if (t->numskinframes >= 2)
4038 t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->frame2time)) % t->numskinframes];
4039 if (t->backgroundnumskinframes >= 2)
4040 t->backgroundcurrentskinframe = t->backgroundskinframes[(int)(t->backgroundskinframerate * (cl.time - ent->frame2time)) % t->backgroundnumskinframes];
4042 t->currentmaterialflags = t->basematerialflags;
4043 t->currentalpha = ent->alpha;
4044 if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_novis.integer))
4046 t->currentalpha *= r_wateralpha.value;
4048 * FIXME what is this supposed to do?
4049 // if rendering refraction/reflection, disable transparency
4050 if (r_waterstate.enabled && (t->currentalpha < 1 || (t->currentmaterialflags & MATERIALFLAG_ALPHA)))
4051 t->currentmaterialflags |= MATERIALFLAG_WATERSHADER;
4054 if(!r_waterstate.enabled)
4055 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION);
4056 if (!(ent->flags & RENDER_LIGHT))
4057 t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
4058 else if (rsurface.modeltexcoordlightmap2f == NULL)
4060 // pick a model lighting mode
4061 if (VectorLength2(ent->modellight_diffuse) >= (1.0f / 256.0f))
4062 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT | MATERIALFLAG_MODELLIGHT_DIRECTIONAL;
4064 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
4066 if (ent->effects & EF_ADDITIVE)
4067 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
4068 else if (t->currentalpha < 1)
4069 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
4070 if (ent->effects & EF_DOUBLESIDED)
4071 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
4072 if (ent->effects & EF_NODEPTHTEST)
4073 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
4074 if (ent->flags & RENDER_VIEWMODEL)
4075 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
4076 if (t->backgroundnumskinframes && !(t->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
4077 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
4079 // make sure that the waterscroll matrix is used on water surfaces when
4080 // there is no tcmod
4081 if (t->currentmaterialflags & MATERIALFLAG_WATER && r_waterscroll.value != 0)
4082 t->currenttexmatrix = r_waterscrollmatrix;
4084 for (i = 0, tcmod = t->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
4087 switch(tcmod->tcmod)
4091 if (t->currentmaterialflags & MATERIALFLAG_WATER && r_waterscroll.value != 0)
4092 matrix = r_waterscrollmatrix;
4094 matrix = identitymatrix;
4096 case Q3TCMOD_ENTITYTRANSLATE:
4097 // this is used in Q3 to allow the gamecode to control texcoord
4098 // scrolling on the entity, which is not supported in darkplaces yet.
4099 Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
4101 case Q3TCMOD_ROTATE:
4102 Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
4103 Matrix4x4_ConcatRotate(&matrix, tcmod->parms[0] * r_refdef.time, 0, 0, 1);
4104 Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
4107 Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
4109 case Q3TCMOD_SCROLL:
4110 Matrix4x4_CreateTranslate(&matrix, tcmod->parms[0] * r_refdef.time, tcmod->parms[1] * r_refdef.time, 0);
4112 case Q3TCMOD_STRETCH:
4113 f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
4114 Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
4116 case Q3TCMOD_TRANSFORM:
4117 VectorSet(tcmat + 0, tcmod->parms[0], tcmod->parms[1], 0);
4118 VectorSet(tcmat + 3, tcmod->parms[2], tcmod->parms[3], 0);
4119 VectorSet(tcmat + 6, 0 , 0 , 1);
4120 VectorSet(tcmat + 9, tcmod->parms[4], tcmod->parms[5], 0);
4121 Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
4123 case Q3TCMOD_TURBULENT:
4124 // this is handled in the RSurf_PrepareVertices function
4125 matrix = identitymatrix;
4128 // either replace or concatenate the transformation
4130 t->currenttexmatrix = matrix;
4133 matrix4x4_t temp = t->currenttexmatrix;
4134 Matrix4x4_Concat(&t->currenttexmatrix, &matrix, &temp);
4138 t->colormapping = VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f);
4139 t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
4140 t->glosstexture = r_texture_black;
4141 t->backgroundbasetexture = t->backgroundnumskinframes ? ((!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base) : r_texture_white;
4142 t->backgroundglosstexture = r_texture_black;
4143 t->specularpower = r_shadow_glossexponent.value;
4144 // TODO: store reference values for these in the texture?
4145 t->specularscale = 0;
4146 if (r_shadow_gloss.integer > 0)
4148 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
4150 if (r_shadow_glossintensity.value > 0)
4152 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
4153 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
4154 t->specularscale = r_shadow_glossintensity.value;
4157 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
4159 t->glosstexture = r_texture_white;
4160 t->backgroundglosstexture = r_texture_white;
4161 t->specularscale = r_shadow_gloss2intensity.value;
4165 // lightmaps mode looks bad with dlights using actual texturing, so turn
4166 // off the colormap and glossmap, but leave the normalmap on as it still
4167 // accurately represents the shading involved
4168 if (gl_lightmaps.integer && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
4170 t->basetexture = r_texture_white;
4171 t->specularscale = 0;
4174 t->currentpolygonfactor = r_refdef.polygonfactor + t->basepolygonfactor;
4175 t->currentpolygonoffset = r_refdef.polygonoffset + t->basepolygonoffset;
4176 // submodels are biased to avoid z-fighting with world surfaces that they
4177 // may be exactly overlapping (avoids z-fighting artifacts on certain
4178 // doors and things in Quake maps)
4179 if (ent->model->brush.submodel)
4181 t->currentpolygonfactor += r_polygonoffset_submodel_factor.value;
4182 t->currentpolygonoffset += r_polygonoffset_submodel_offset.value;
4185 VectorClear(t->dlightcolor);
4186 t->currentnumlayers = 0;
4187 if (!(t->currentmaterialflags & MATERIALFLAG_NODRAW))
4189 if (!(t->currentmaterialflags & MATERIALFLAG_SKY))
4191 int blendfunc1, blendfunc2, depthmask;
4192 if (t->currentmaterialflags & MATERIALFLAG_ADD)
4194 blendfunc1 = GL_SRC_ALPHA;
4195 blendfunc2 = GL_ONE;
4197 else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
4199 blendfunc1 = GL_SRC_ALPHA;
4200 blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
4202 else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
4204 blendfunc1 = t->customblendfunc[0];
4205 blendfunc2 = t->customblendfunc[1];
4209 blendfunc1 = GL_ONE;
4210 blendfunc2 = GL_ZERO;
4212 depthmask = !(t->currentmaterialflags & MATERIALFLAG_BLENDED);
4213 if (t->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
4215 rtexture_t *currentbasetexture;
4217 if (r_refdef.fogenabled && (t->currentmaterialflags & MATERIALFLAG_BLENDED))
4218 layerflags |= TEXTURELAYERFLAG_FOGDARKEN;
4219 currentbasetexture = (VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) < (1.0f / 1048576.0f) && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
4220 if (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
4222 // fullbright is not affected by r_refdef.lightmapintensity
4223 R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0], ent->colormod[1], ent->colormod[2], t->currentalpha);
4224 if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
4225 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0], ent->colormap_pantscolor[1] * ent->colormod[1], ent->colormap_pantscolor[2] * ent->colormod[2], t->currentalpha);
4226 if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
4227 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0], ent->colormap_shirtcolor[1] * ent->colormod[1], ent->colormap_shirtcolor[2] * ent->colormod[2], t->currentalpha);
4232 // set the color tint used for lights affecting this surface
4233 VectorSet(t->dlightcolor, ent->colormod[0] * t->currentalpha, ent->colormod[1] * t->currentalpha, ent->colormod[2] * t->currentalpha);
4235 // q3bsp has no lightmap updates, so the lightstylevalue that
4236 // would normally be baked into the lightmap must be
4237 // applied to the color
4238 // FIXME: r_glsl 1 rendering doesn't support overbright lightstyles with this (the default light style is not overbright)
4239 if (ent->model->type == mod_brushq3)
4240 colorscale *= r_refdef.lightstylevalue[0] * (1.0f / 256.0f);
4241 colorscale *= r_refdef.lightmapintensity;
4242 R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LITTEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * colorscale, ent->colormod[1] * colorscale, ent->colormod[2] * colorscale, t->currentalpha);
4243 if (r_ambient.value >= (1.0f/64.0f))
4244 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
4245 if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
4247 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0] * colorscale, ent->colormap_pantscolor[1] * ent->colormod[1] * colorscale, ent->colormap_pantscolor[2] * ent->colormod[2] * colorscale, t->currentalpha);
4248 if (r_ambient.value >= (1.0f/64.0f))
4249 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[1] * ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[2] * ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
4251 if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
4253 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0] * colorscale, ent->colormap_shirtcolor[1] * ent->colormod[1] * colorscale, ent->colormap_shirtcolor[2] * ent->colormod[2] * colorscale, t->currentalpha);
4254 if (r_ambient.value >= (1.0f/64.0f))
4255 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[1] * ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[2] * ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
4258 if (t->currentskinframe->glow != NULL)
4259 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->glow, &t->currenttexmatrix, r_hdr_glowintensity.value, r_hdr_glowintensity.value, r_hdr_glowintensity.value, t->currentalpha);
4260 if (r_refdef.fogenabled && !(t->currentmaterialflags & MATERIALFLAG_ADD))
4262 // if this is opaque use alpha blend which will darken the earlier
4265 // if this is an alpha blended material, all the earlier passes
4266 // were darkened by fog already, so we only need to add the fog
4267 // color ontop through the fog mask texture
4269 // if this is an additive blended material, all the earlier passes
4270 // were darkened by fog already, and we should not add fog color
4271 // (because the background was not darkened, there is no fog color
4272 // that was lost behind it).
4273 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, (t->currentmaterialflags & MATERIALFLAG_BLENDED) ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, TEXTURELAYERTYPE_FOG, t->currentskinframe->fog, &identitymatrix, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], t->currentalpha);
4280 void R_UpdateAllTextureInfo(entity_render_t *ent)
4284 for (i = 0;i < ent->model->num_texturesperskin;i++)
4285 R_UpdateTextureInfo(ent, ent->model->data_textures + i);
4288 rsurfacestate_t rsurface;
4290 void R_Mesh_ResizeArrays(int newvertices)
4293 if (rsurface.array_size >= newvertices)
4295 if (rsurface.array_modelvertex3f)
4296 Mem_Free(rsurface.array_modelvertex3f);
4297 rsurface.array_size = (newvertices + 1023) & ~1023;
4298 base = (float *)Mem_Alloc(r_main_mempool, rsurface.array_size * sizeof(float[33]));
4299 rsurface.array_modelvertex3f = base + rsurface.array_size * 0;
4300 rsurface.array_modelsvector3f = base + rsurface.array_size * 3;
4301 rsurface.array_modeltvector3f = base + rsurface.array_size * 6;
4302 rsurface.array_modelnormal3f = base + rsurface.array_size * 9;
4303 rsurface.array_deformedvertex3f = base + rsurface.array_size * 12;
4304 rsurface.array_deformedsvector3f = base + rsurface.array_size * 15;
4305 rsurface.array_deformedtvector3f = base + rsurface.array_size * 18;
4306 rsurface.array_deformednormal3f = base + rsurface.array_size * 21;
4307 rsurface.array_texcoord3f = base + rsurface.array_size * 24;
4308 rsurface.array_color4f = base + rsurface.array_size * 27;
4309 rsurface.array_generatedtexcoordtexture2f = base + rsurface.array_size * 31;
4312 void RSurf_CleanUp(void)
4315 if (rsurface.mode == RSURFMODE_GLSL)
4317 qglUseProgramObjectARB(0);CHECKGLERROR
4319 GL_AlphaTest(false);
4320 rsurface.mode = RSURFMODE_NONE;
4321 rsurface.uselightmaptexture = false;
4322 rsurface.texture = NULL;
4325 void RSurf_ActiveWorldEntity(void)
4327 model_t *model = r_refdef.worldmodel;
4329 if (rsurface.array_size < model->surfmesh.num_vertices)
4330 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
4331 rsurface.matrix = identitymatrix;
4332 rsurface.inversematrix = identitymatrix;
4333 R_Mesh_Matrix(&identitymatrix);
4334 VectorCopy(r_view.origin, rsurface.modelorg);
4335 VectorSet(rsurface.modellight_ambient, 0, 0, 0);
4336 VectorSet(rsurface.modellight_diffuse, 0, 0, 0);
4337 VectorSet(rsurface.modellight_lightdir, 0, 0, 1);
4338 VectorSet(rsurface.colormap_pantscolor, 0, 0, 0);
4339 VectorSet(rsurface.colormap_shirtcolor, 0, 0, 0);
4340 rsurface.frameblend[0].frame = 0;
4341 rsurface.frameblend[0].lerp = 1;
4342 rsurface.frameblend[1].frame = 0;
4343 rsurface.frameblend[1].lerp = 0;
4344 rsurface.frameblend[2].frame = 0;
4345 rsurface.frameblend[2].lerp = 0;
4346 rsurface.frameblend[3].frame = 0;
4347 rsurface.frameblend[3].lerp = 0;
4348 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
4349 rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
4350 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
4351 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
4352 rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
4353 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
4354 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
4355 rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
4356 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
4357 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
4358 rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
4359 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
4360 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
4361 rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
4362 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
4363 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
4364 rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
4365 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
4366 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
4367 rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
4368 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
4369 rsurface.modelelement3i = model->surfmesh.data_element3i;
4370 rsurface.modelelement3i_bufferobject = model->surfmesh.ebo;
4371 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
4372 rsurface.modelnum_vertices = model->surfmesh.num_vertices;
4373 rsurface.modelnum_triangles = model->surfmesh.num_triangles;
4374 rsurface.modelsurfaces = model->data_surfaces;
4375 rsurface.generatedvertex = false;
4376 rsurface.vertex3f = rsurface.modelvertex3f;
4377 rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4378 rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4379 rsurface.svector3f = rsurface.modelsvector3f;
4380 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4381 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4382 rsurface.tvector3f = rsurface.modeltvector3f;
4383 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4384 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4385 rsurface.normal3f = rsurface.modelnormal3f;
4386 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4387 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4388 rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4391 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
4393 model_t *model = ent->model;
4395 if (rsurface.array_size < model->surfmesh.num_vertices)
4396 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
4397 rsurface.matrix = ent->matrix;
4398 rsurface.inversematrix = ent->inversematrix;
4399 R_Mesh_Matrix(&rsurface.matrix);
4400 Matrix4x4_Transform(&rsurface.inversematrix, r_view.origin, rsurface.modelorg);
4401 VectorCopy(ent->modellight_ambient, rsurface.modellight_ambient);
4402 VectorCopy(ent->modellight_diffuse, rsurface.modellight_diffuse);
4403 VectorCopy(ent->modellight_lightdir, rsurface.modellight_lightdir);
4404 VectorCopy(ent->colormap_pantscolor, rsurface.colormap_pantscolor);
4405 VectorCopy(ent->colormap_shirtcolor, rsurface.colormap_shirtcolor);
4406 rsurface.frameblend[0] = ent->frameblend[0];
4407 rsurface.frameblend[1] = ent->frameblend[1];
4408 rsurface.frameblend[2] = ent->frameblend[2];
4409 rsurface.frameblend[3] = ent->frameblend[3];
4410 if (model->surfmesh.isanimated && (rsurface.frameblend[0].lerp != 1 || rsurface.frameblend[0].frame != 0))
4414 rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4415 rsurface.modelsvector3f = rsurface.array_modelsvector3f;
4416 rsurface.modeltvector3f = rsurface.array_modeltvector3f;
4417 rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4418 Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, rsurface.array_modelsvector3f, rsurface.array_modeltvector3f);
4420 else if (wantnormals)
4422 rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4423 rsurface.modelsvector3f = NULL;
4424 rsurface.modeltvector3f = NULL;
4425 rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4426 Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, NULL, NULL);
4430 rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4431 rsurface.modelsvector3f = NULL;
4432 rsurface.modeltvector3f = NULL;
4433 rsurface.modelnormal3f = NULL;
4434 Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, NULL, NULL, NULL);
4436 rsurface.modelvertex3f_bufferobject = 0;
4437 rsurface.modelvertex3f_bufferoffset = 0;
4438 rsurface.modelsvector3f_bufferobject = 0;
4439 rsurface.modelsvector3f_bufferoffset = 0;
4440 rsurface.modeltvector3f_bufferobject = 0;
4441 rsurface.modeltvector3f_bufferoffset = 0;
4442 rsurface.modelnormal3f_bufferobject = 0;
4443 rsurface.modelnormal3f_bufferoffset = 0;
4444 rsurface.generatedvertex = true;
4448 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
4449 rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
4450 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
4451 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
4452 rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
4453 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
4454 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
4455 rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
4456 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
4457 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
4458 rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
4459 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
4460 rsurface.generatedvertex = false;
4462 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
4463 rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
4464 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
4465 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
4466 rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
4467 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
4468 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
4469 rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
4470 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
4471 rsurface.modelelement3i = model->surfmesh.data_element3i;
4472 rsurface.modelelement3i_bufferobject = model->surfmesh.ebo;
4473 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
4474 rsurface.modelnum_vertices = model->surfmesh.num_vertices;
4475 rsurface.modelnum_triangles = model->surfmesh.num_triangles;
4476 rsurface.modelsurfaces = model->data_surfaces;
4477 rsurface.vertex3f = rsurface.modelvertex3f;
4478 rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4479 rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4480 rsurface.svector3f = rsurface.modelsvector3f;
4481 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4482 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4483 rsurface.tvector3f = rsurface.modeltvector3f;
4484 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4485 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4486 rsurface.normal3f = rsurface.modelnormal3f;
4487 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4488 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4489 rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4492 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
4493 void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist)
4496 int texturesurfaceindex;
4501 const float *v1, *in_tc;
4503 float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
4505 q3shaderinfo_deform_t *deform;
4506 // 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
4507 if (rsurface.generatedvertex)
4509 if (rsurface.texture->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
4510 generatenormals = true;
4511 for (i = 0;i < Q3MAXDEFORMS;i++)
4513 if (rsurface.texture->deforms[i].deform == Q3DEFORM_AUTOSPRITE)
4515 generatetangents = true;
4516 generatenormals = true;
4518 if (rsurface.texture->deforms[i].deform != Q3DEFORM_NONE)
4519 generatenormals = true;
4521 if (generatenormals && !rsurface.modelnormal3f)
4523 rsurface.normal3f = rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4524 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject = 0;
4525 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset = 0;
4526 Mod_BuildNormals(0, rsurface.modelnum_vertices, rsurface.modelnum_triangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.array_modelnormal3f, r_smoothnormals_areaweighting.integer);
4528 if (generatetangents && !rsurface.modelsvector3f)
4530 rsurface.svector3f = rsurface.modelsvector3f = rsurface.array_modelsvector3f;
4531 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject = 0;
4532 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset = 0;
4533 rsurface.tvector3f = rsurface.modeltvector3f = rsurface.array_modeltvector3f;
4534 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject = 0;
4535 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset = 0;
4536 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);
4539 rsurface.vertex3f = rsurface.modelvertex3f;
4540 rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4541 rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4542 rsurface.svector3f = rsurface.modelsvector3f;
4543 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4544 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4545 rsurface.tvector3f = rsurface.modeltvector3f;
4546 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4547 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4548 rsurface.normal3f = rsurface.modelnormal3f;
4549 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4550 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4551 // if vertices are deformed (sprite flares and things in maps, possibly
4552 // water waves, bulges and other deformations), generate them into
4553 // rsurface.deform* arrays from whatever the rsurface.* arrays point to
4554 // (may be static model data or generated data for an animated model, or
4555 // the previous deform pass)
4556 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform;deformindex++, deform++)
4558 switch (deform->deform)
4561 case Q3DEFORM_PROJECTIONSHADOW:
4562 case Q3DEFORM_TEXT0:
4563 case Q3DEFORM_TEXT1:
4564 case Q3DEFORM_TEXT2:
4565 case Q3DEFORM_TEXT3:
4566 case Q3DEFORM_TEXT4:
4567 case Q3DEFORM_TEXT5:
4568 case Q3DEFORM_TEXT6:
4569 case Q3DEFORM_TEXT7:
4572 case Q3DEFORM_AUTOSPRITE:
4573 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, newforward);
4574 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.right, newright);
4575 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.up, newup);
4576 VectorNormalize(newforward);
4577 VectorNormalize(newright);
4578 VectorNormalize(newup);
4579 // make deformed versions of only the model vertices used by the specified surfaces
4580 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4582 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4583 // a single autosprite surface can contain multiple sprites...
4584 for (j = 0;j < surface->num_vertices - 3;j += 4)
4586 VectorClear(center);
4587 for (i = 0;i < 4;i++)
4588 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
4589 VectorScale(center, 0.25f, center);
4590 VectorCopy((rsurface.normal3f + 3 * surface->num_firstvertex) + j*3, forward);
4591 VectorCopy((rsurface.svector3f + 3 * surface->num_firstvertex) + j*3, right);
4592 VectorCopy((rsurface.tvector3f + 3 * surface->num_firstvertex) + j*3, up);
4593 for (i = 0;i < 4;i++)
4595 VectorSubtract((rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i)*3, center, v);
4596 VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
4599 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);
4600 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);
4602 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4603 rsurface.vertex3f_bufferobject = 0;
4604 rsurface.vertex3f_bufferoffset = 0;
4605 rsurface.svector3f = rsurface.array_deformedsvector3f;
4606 rsurface.svector3f_bufferobject = 0;
4607 rsurface.svector3f_bufferoffset = 0;
4608 rsurface.tvector3f = rsurface.array_deformedtvector3f;
4609 rsurface.tvector3f_bufferobject = 0;
4610 rsurface.tvector3f_bufferoffset = 0;
4611 rsurface.normal3f = rsurface.array_deformednormal3f;
4612 rsurface.normal3f_bufferobject = 0;
4613 rsurface.normal3f_bufferoffset = 0;
4615 case Q3DEFORM_AUTOSPRITE2:
4616 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, newforward);
4617 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.right, newright);
4618 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.up, newup);
4619 VectorNormalize(newforward);
4620 VectorNormalize(newright);
4621 VectorNormalize(newup);
4622 // make deformed versions of only the model vertices used by the specified surfaces
4623 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4625 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4626 const float *v1, *v2;
4636 memset(shortest, 0, sizeof(shortest));
4637 // a single autosprite surface can contain multiple sprites...
4638 for (j = 0;j < surface->num_vertices - 3;j += 4)
4640 VectorClear(center);
4641 for (i = 0;i < 4;i++)
4642 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
4643 VectorScale(center, 0.25f, center);
4644 // find the two shortest edges, then use them to define the
4645 // axis vectors for rotating around the central axis
4646 for (i = 0;i < 6;i++)
4648 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][0]);
4649 v2 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][1]);
4651 Debug_PolygonBegin(NULL, 0, false, 0);
4652 Debug_PolygonVertex(v1[0], v1[1], v1[2], 0, 0, 1, 0, 0, 1);
4653 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);
4654 Debug_PolygonVertex(v2[0], v2[1], v2[2], 0, 0, 1, 0, 0, 1);
4657 l = VectorDistance2(v1, v2);
4658 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
4660 l += (1.0f / 1024.0f);
4661 if (shortest[0].length2 > l || i == 0)
4663 shortest[1] = shortest[0];
4664 shortest[0].length2 = l;
4665 shortest[0].v1 = v1;
4666 shortest[0].v2 = v2;
4668 else if (shortest[1].length2 > l || i == 1)
4670 shortest[1].length2 = l;
4671 shortest[1].v1 = v1;
4672 shortest[1].v2 = v2;
4675 VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
4676 VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
4678 Debug_PolygonBegin(NULL, 0, false, 0);
4679 Debug_PolygonVertex(start[0], start[1], start[2], 0, 0, 1, 1, 0, 1);
4680 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);
4681 Debug_PolygonVertex(end[0], end[1], end[2], 0, 0, 0, 1, 1, 1);
4684 // this calculates the right vector from the shortest edge
4685 // and the up vector from the edge midpoints
4686 VectorSubtract(shortest[0].v1, shortest[0].v2, right);
4687 VectorNormalize(right);
4688 VectorSubtract(end, start, up);
4689 VectorNormalize(up);
4690 // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
4691 //VectorSubtract(rsurface.modelorg, center, forward);
4692 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, forward);
4693 VectorNegate(forward, forward);
4694 VectorReflect(forward, 0, up, forward);
4695 VectorNormalize(forward);
4696 CrossProduct(up, forward, newright);
4697 VectorNormalize(newright);
4699 Debug_PolygonBegin(NULL, 0, false, 0);
4700 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);
4701 Debug_PolygonVertex(center[0] + right[0] * 8, center[1] + right[1] * 8, center[2] + right[2] * 8, 0, 0, 0, 1, 0, 1);
4702 Debug_PolygonVertex(center[0] + up [0] * 8, center[1] + up [1] * 8, center[2] + up [2] * 8, 0, 0, 0, 0, 1, 1);
4706 Debug_PolygonBegin(NULL, 0, false, 0);
4707 Debug_PolygonVertex(center[0] + forward [0] * 8, center[1] + forward [1] * 8, center[2] + forward [2] * 8, 0, 0, 1, 0, 0, 1);
4708 Debug_PolygonVertex(center[0] + newright[0] * 8, center[1] + newright[1] * 8, center[2] + newright[2] * 8, 0, 0, 0, 1, 0, 1);
4709 Debug_PolygonVertex(center[0] + up [0] * 8, center[1] + up [1] * 8, center[2] + up [2] * 8, 0, 0, 0, 0, 1, 1);
4712 // rotate the quad around the up axis vector, this is made
4713 // especially easy by the fact we know the quad is flat,
4714 // so we only have to subtract the center position and
4715 // measure distance along the right vector, and then
4716 // multiply that by the newright vector and add back the
4718 // we also need to subtract the old position to undo the
4719 // displacement from the center, which we do with a
4720 // DotProduct, the subtraction/addition of center is also
4721 // optimized into DotProducts here
4722 l = DotProduct(right, center);
4723 for (i = 0;i < 4;i++)
4725 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + j + i);
4726 f = DotProduct(right, v1) - l;
4727 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
4730 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);
4731 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);
4733 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4734 rsurface.vertex3f_bufferobject = 0;
4735 rsurface.vertex3f_bufferoffset = 0;
4736 rsurface.svector3f = rsurface.array_deformedsvector3f;
4737 rsurface.svector3f_bufferobject = 0;
4738 rsurface.svector3f_bufferoffset = 0;
4739 rsurface.tvector3f = rsurface.array_deformedtvector3f;
4740 rsurface.tvector3f_bufferobject = 0;
4741 rsurface.tvector3f_bufferoffset = 0;
4742 rsurface.normal3f = rsurface.array_deformednormal3f;
4743 rsurface.normal3f_bufferobject = 0;
4744 rsurface.normal3f_bufferoffset = 0;
4746 case Q3DEFORM_NORMAL:
4747 // deform the normals to make reflections wavey
4748 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4750 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4751 for (j = 0;j < surface->num_vertices;j++)
4754 float *normal = (rsurface.array_deformednormal3f + 3 * surface->num_firstvertex) + j*3;
4755 VectorScale((rsurface.vertex3f + 3 * surface->num_firstvertex) + j*3, 0.98f, vertex);
4756 VectorCopy((rsurface.normal3f + 3 * surface->num_firstvertex) + j*3, normal);
4757 normal[0] += deform->parms[0] * noise4f( vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4758 normal[1] += deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4759 normal[2] += deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4760 VectorNormalize(normal);
4762 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);
4764 rsurface.svector3f = rsurface.array_deformedsvector3f;
4765 rsurface.svector3f_bufferobject = 0;
4766 rsurface.svector3f_bufferoffset = 0;
4767 rsurface.tvector3f = rsurface.array_deformedtvector3f;
4768 rsurface.tvector3f_bufferobject = 0;
4769 rsurface.tvector3f_bufferoffset = 0;
4770 rsurface.normal3f = rsurface.array_deformednormal3f;
4771 rsurface.normal3f_bufferobject = 0;
4772 rsurface.normal3f_bufferoffset = 0;
4775 // deform vertex array to make wavey water and flags and such
4776 waveparms[0] = deform->waveparms[0];
4777 waveparms[1] = deform->waveparms[1];
4778 waveparms[2] = deform->waveparms[2];
4779 waveparms[3] = deform->waveparms[3];
4780 // this is how a divisor of vertex influence on deformation
4781 animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
4782 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
4783 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4785 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4786 for (j = 0;j < surface->num_vertices;j++)
4788 float *vertex = (rsurface.array_deformedvertex3f + 3 * surface->num_firstvertex) + j*3;
4789 VectorCopy((rsurface.vertex3f + 3 * surface->num_firstvertex) + j*3, vertex);
4790 // if the wavefunc depends on time, evaluate it per-vertex
4793 waveparms[2] = deform->waveparms[2] + (vertex[0] + vertex[1] + vertex[2]) * animpos;
4794 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
4796 VectorMA(vertex, scale, (rsurface.normal3f + 3 * surface->num_firstvertex) + j*3, vertex);
4799 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4800 rsurface.vertex3f_bufferobject = 0;
4801 rsurface.vertex3f_bufferoffset = 0;
4803 case Q3DEFORM_BULGE:
4804 // deform vertex array to make the surface have moving bulges
4805 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4807 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4808 for (j = 0;j < surface->num_vertices;j++)
4810 scale = sin((rsurface.modeltexcoordtexture2f[2 * (surface->num_firstvertex + j)] * deform->parms[0] + r_refdef.time * deform->parms[2])) * deform->parms[1];
4811 VectorMA(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), scale, rsurface.normal3f + 3 * (surface->num_firstvertex + j), rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
4814 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4815 rsurface.vertex3f_bufferobject = 0;
4816 rsurface.vertex3f_bufferoffset = 0;
4819 // deform vertex array
4820 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
4821 VectorScale(deform->parms, scale, waveparms);
4822 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4824 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4825 for (j = 0;j < surface->num_vertices;j++)
4826 VectorAdd(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), waveparms, rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
4828 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4829 rsurface.vertex3f_bufferobject = 0;
4830 rsurface.vertex3f_bufferoffset = 0;
4834 // generate texcoords based on the chosen texcoord source
4835 switch(rsurface.texture->tcgen.tcgen)
4838 case Q3TCGEN_TEXTURE:
4839 rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4840 rsurface.texcoordtexture2f_bufferobject = rsurface.modeltexcoordtexture2f_bufferobject;
4841 rsurface.texcoordtexture2f_bufferoffset = rsurface.modeltexcoordtexture2f_bufferoffset;
4843 case Q3TCGEN_LIGHTMAP:
4844 rsurface.texcoordtexture2f = rsurface.modeltexcoordlightmap2f;
4845 rsurface.texcoordtexture2f_bufferobject = rsurface.modeltexcoordlightmap2f_bufferobject;
4846 rsurface.texcoordtexture2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
4848 case Q3TCGEN_VECTOR:
4849 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4851 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4852 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)
4854 out_tc[0] = DotProduct(v1, rsurface.texture->tcgen.parms);
4855 out_tc[1] = DotProduct(v1, rsurface.texture->tcgen.parms + 3);
4858 rsurface.texcoordtexture2f = rsurface.array_generatedtexcoordtexture2f;
4859 rsurface.texcoordtexture2f_bufferobject = 0;
4860 rsurface.texcoordtexture2f_bufferoffset = 0;
4862 case Q3TCGEN_ENVIRONMENT:
4863 // make environment reflections using a spheremap
4864 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4866 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4867 const float *vertex = rsurface.modelvertex3f + 3 * surface->num_firstvertex;
4868 const float *normal = rsurface.modelnormal3f + 3 * surface->num_firstvertex;
4869 float *out_tc = rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex;
4870 for (j = 0;j < surface->num_vertices;j++, vertex += 3, normal += 3, out_tc += 2)
4872 float l, d, eyedir[3];
4873 VectorSubtract(rsurface.modelorg, vertex, eyedir);
4874 l = 0.5f / VectorLength(eyedir);
4875 d = DotProduct(normal, eyedir)*2;
4876 out_tc[0] = 0.5f + (normal[1]*d - eyedir[1])*l;
4877 out_tc[1] = 0.5f - (normal[2]*d - eyedir[2])*l;
4880 rsurface.texcoordtexture2f = rsurface.array_generatedtexcoordtexture2f;
4881 rsurface.texcoordtexture2f_bufferobject = 0;
4882 rsurface.texcoordtexture2f_bufferoffset = 0;
4885 // the only tcmod that needs software vertex processing is turbulent, so
4886 // check for it here and apply the changes if needed
4887 // and we only support that as the first one
4888 // (handling a mixture of turbulent and other tcmods would be problematic
4889 // without punting it entirely to a software path)
4890 if (rsurface.texture->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
4892 amplitude = rsurface.texture->tcmods[0].parms[1];
4893 animpos = rsurface.texture->tcmods[0].parms[2] + r_refdef.time * rsurface.texture->tcmods[0].parms[3];
4894 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4896 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4897 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)
4899 out_tc[0] = in_tc[0] + amplitude * sin(((v1[0] + v1[2]) * 1.0 / 1024.0f + animpos) * M_PI * 2);
4900 out_tc[1] = in_tc[1] + amplitude * sin(((v1[1] ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
4903 rsurface.texcoordtexture2f = rsurface.array_generatedtexcoordtexture2f;
4904 rsurface.texcoordtexture2f_bufferobject = 0;
4905 rsurface.texcoordtexture2f_bufferoffset = 0;
4907 rsurface.texcoordlightmap2f = rsurface.modeltexcoordlightmap2f;
4908 rsurface.texcoordlightmap2f_bufferobject = rsurface.modeltexcoordlightmap2f_bufferobject;
4909 rsurface.texcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
4910 R_Mesh_VertexPointer(rsurface.vertex3f, rsurface.vertex3f_bufferobject, rsurface.vertex3f_bufferoffset);
4913 void RSurf_DrawBatch_Simple(int texturenumsurfaces, msurface_t **texturesurfacelist)
4916 const msurface_t *surface = texturesurfacelist[0];
4917 const msurface_t *surface2;
4922 // TODO: lock all array ranges before render, rather than on each surface
4923 if (texturenumsurfaces == 1)
4925 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
4926 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4928 else if (r_batchmode.integer == 2)
4930 #define MAXBATCHTRIANGLES 4096
4931 int batchtriangles = 0;
4932 int batchelements[MAXBATCHTRIANGLES*3];
4933 for (i = 0;i < texturenumsurfaces;i = j)
4935 surface = texturesurfacelist[i];
4937 if (surface->num_triangles > MAXBATCHTRIANGLES)
4939 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4942 memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
4943 batchtriangles = surface->num_triangles;
4944 firstvertex = surface->num_firstvertex;
4945 endvertex = surface->num_firstvertex + surface->num_vertices;
4946 for (;j < texturenumsurfaces;j++)
4948 surface2 = texturesurfacelist[j];
4949 if (batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
4951 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
4952 batchtriangles += surface2->num_triangles;
4953 firstvertex = min(firstvertex, surface2->num_firstvertex);
4954 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
4956 surface2 = texturesurfacelist[j-1];
4957 numvertices = endvertex - firstvertex;
4958 R_Mesh_Draw(firstvertex, numvertices, batchtriangles, batchelements, 0, 0);
4961 else if (r_batchmode.integer == 1)
4963 for (i = 0;i < texturenumsurfaces;i = j)
4965 surface = texturesurfacelist[i];
4966 for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
4967 if (texturesurfacelist[j] != surface2)
4969 surface2 = texturesurfacelist[j-1];
4970 numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
4971 numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
4972 GL_LockArrays(surface->num_firstvertex, numvertices);
4973 R_Mesh_Draw(surface->num_firstvertex, numvertices, numtriangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4978 for (i = 0;i < texturenumsurfaces;i++)
4980 surface = texturesurfacelist[i];
4981 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
4982 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4987 static void RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit, int refractiontexunit, int reflectiontexunit)
4989 int i, planeindex, vertexindex;
4993 r_waterstate_waterplane_t *p, *bestp;
4994 msurface_t *surface;
4995 if (r_waterstate.renderingscene)
4997 for (i = 0;i < texturenumsurfaces;i++)
4999 surface = texturesurfacelist[i];
5000 if (lightmaptexunit >= 0)
5001 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5002 if (deluxemaptexunit >= 0)
5003 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5004 // pick the closest matching water plane
5007 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
5010 for (vertexindex = 0, v = rsurface.modelvertex3f + surface->num_firstvertex * 3;vertexindex < surface->num_vertices;vertexindex++, v += 3)
5012 Matrix4x4_Transform(&rsurface.matrix, v, vert);
5013 d += fabs(PlaneDiff(vert, &p->plane));
5015 if (bestd > d || !bestp)
5023 if (refractiontexunit >= 0)
5024 R_Mesh_TexBind(refractiontexunit, R_GetTexture(bestp->texture_refraction));
5025 if (reflectiontexunit >= 0)
5026 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(bestp->texture_reflection));
5030 if (refractiontexunit >= 0)
5031 R_Mesh_TexBind(refractiontexunit, R_GetTexture(r_texture_black));
5032 if (reflectiontexunit >= 0)
5033 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(r_texture_black));
5035 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5036 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5040 static void RSurf_DrawBatch_WithLightmapSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit)
5044 const msurface_t *surface = texturesurfacelist[0];
5045 const msurface_t *surface2;
5050 // TODO: lock all array ranges before render, rather than on each surface
5051 if (texturenumsurfaces == 1)
5053 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5054 if (deluxemaptexunit >= 0)
5055 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5056 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5057 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5059 else if (r_batchmode.integer == 2)
5061 #define MAXBATCHTRIANGLES 4096
5062 int batchtriangles = 0;
5063 int batchelements[MAXBATCHTRIANGLES*3];
5064 for (i = 0;i < texturenumsurfaces;i = j)
5066 surface = texturesurfacelist[i];
5067 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5068 if (deluxemaptexunit >= 0)
5069 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5071 if (surface->num_triangles > MAXBATCHTRIANGLES)
5073 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5076 memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
5077 batchtriangles = surface->num_triangles;
5078 firstvertex = surface->num_firstvertex;
5079 endvertex = surface->num_firstvertex + surface->num_vertices;
5080 for (;j < texturenumsurfaces;j++)
5082 surface2 = texturesurfacelist[j];
5083 if (surface2->lightmaptexture != surface->lightmaptexture || batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
5085 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
5086 batchtriangles += surface2->num_triangles;
5087 firstvertex = min(firstvertex, surface2->num_firstvertex);
5088 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
5090 surface2 = texturesurfacelist[j-1];
5091 numvertices = endvertex - firstvertex;
5092 R_Mesh_Draw(firstvertex, numvertices, batchtriangles, batchelements, 0, 0);
5095 else if (r_batchmode.integer == 1)
5098 Con_Printf("%s batch sizes ignoring lightmap:", rsurface.texture->name);
5099 for (i = 0;i < texturenumsurfaces;i = j)
5101 surface = texturesurfacelist[i];
5102 for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
5103 if (texturesurfacelist[j] != surface2)
5105 Con_Printf(" %i", j - i);
5108 Con_Printf("%s batch sizes honoring lightmap:", rsurface.texture->name);
5110 for (i = 0;i < texturenumsurfaces;i = j)
5112 surface = texturesurfacelist[i];
5113 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5114 if (deluxemaptexunit >= 0)
5115 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5116 for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
5117 if (texturesurfacelist[j] != surface2 || texturesurfacelist[j]->lightmaptexture != surface->lightmaptexture)
5120 Con_Printf(" %i", j - i);
5122 surface2 = texturesurfacelist[j-1];
5123 numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
5124 numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
5125 GL_LockArrays(surface->num_firstvertex, numvertices);
5126 R_Mesh_Draw(surface->num_firstvertex, numvertices, numtriangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5134 for (i = 0;i < texturenumsurfaces;i++)
5136 surface = texturesurfacelist[i];
5137 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5138 if (deluxemaptexunit >= 0)
5139 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5140 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5141 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5146 static void RSurf_DrawBatch_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
5149 int texturesurfaceindex;
5150 if (r_showsurfaces.integer == 2)
5152 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5154 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5155 for (j = 0;j < surface->num_triangles;j++)
5157 float f = ((j + surface->num_firsttriangle) & 31) * (1.0f / 31.0f) * r_view.colorscale;
5158 GL_Color(f, f, f, 1);
5159 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, 1, (rsurface.modelelement3i + 3 * (j + surface->num_firsttriangle)), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * (j + surface->num_firsttriangle)));
5165 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5167 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5168 int k = (int)(((size_t)surface) / sizeof(msurface_t));
5169 GL_Color((k & 15) * (1.0f / 16.0f) * r_view.colorscale, ((k >> 4) & 15) * (1.0f / 16.0f) * r_view.colorscale, ((k >> 8) & 15) * (1.0f / 16.0f) * r_view.colorscale, 1);
5170 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5171 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5176 static void RSurf_DrawBatch_GL11_ApplyFog(int texturenumsurfaces, msurface_t **texturesurfacelist)
5178 int texturesurfaceindex;
5182 if (rsurface.lightmapcolor4f)
5184 // generate color arrays for the surfaces in this list
5185 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5187 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5188 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)
5190 f = FogPoint_Model(v);
5200 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5202 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5203 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)
5205 f = FogPoint_Model(v);
5213 rsurface.lightmapcolor4f = rsurface.array_color4f;
5214 rsurface.lightmapcolor4f_bufferobject = 0;
5215 rsurface.lightmapcolor4f_bufferoffset = 0;
5218 static void RSurf_DrawBatch_GL11_ApplyColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a)
5220 int texturesurfaceindex;
5223 if (!rsurface.lightmapcolor4f)
5225 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5227 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5228 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)
5236 rsurface.lightmapcolor4f = rsurface.array_color4f;
5237 rsurface.lightmapcolor4f_bufferobject = 0;
5238 rsurface.lightmapcolor4f_bufferoffset = 0;
5241 static void RSurf_DrawBatch_GL11_Lightmap(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5244 rsurface.lightmapcolor4f = NULL;
5245 rsurface.lightmapcolor4f_bufferobject = 0;
5246 rsurface.lightmapcolor4f_bufferoffset = 0;
5247 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5248 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5249 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5250 GL_Color(r, g, b, a);
5251 RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 0, -1);
5254 static void RSurf_DrawBatch_GL11_Unlit(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5256 // TODO: optimize applyfog && applycolor case
5257 // just apply fog if necessary, and tint the fog color array if necessary
5258 rsurface.lightmapcolor4f = NULL;
5259 rsurface.lightmapcolor4f_bufferobject = 0;
5260 rsurface.lightmapcolor4f_bufferoffset = 0;
5261 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5262 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5263 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5264 GL_Color(r, g, b, a);
5265 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5268 static void RSurf_DrawBatch_GL11_VertexColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5270 int texturesurfaceindex;
5274 if (texturesurfacelist[0]->lightmapinfo && texturesurfacelist[0]->lightmapinfo->stainsamples)
5276 // generate color arrays for the surfaces in this list
5277 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5279 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5280 for (i = 0, c = rsurface.array_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
5282 if (surface->lightmapinfo->samples)
5284 const unsigned char *lm = surface->lightmapinfo->samples + (rsurface.modellightmapoffsets + surface->num_firstvertex)[i];
5285 float scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
5286 VectorScale(lm, scale, c);
5287 if (surface->lightmapinfo->styles[1] != 255)
5289 int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
5291 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
5292 VectorMA(c, scale, lm, c);
5293 if (surface->lightmapinfo->styles[2] != 255)
5296 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
5297 VectorMA(c, scale, lm, c);
5298 if (surface->lightmapinfo->styles[3] != 255)
5301 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
5302 VectorMA(c, scale, lm, c);
5312 rsurface.lightmapcolor4f = rsurface.array_color4f;
5313 rsurface.lightmapcolor4f_bufferobject = 0;
5314 rsurface.lightmapcolor4f_bufferoffset = 0;
5318 rsurface.lightmapcolor4f = rsurface.modellightmapcolor4f;
5319 rsurface.lightmapcolor4f_bufferobject = rsurface.modellightmapcolor4f_bufferobject;
5320 rsurface.lightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
5322 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5323 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5324 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5325 GL_Color(r, g, b, a);
5326 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5329 static void RSurf_DrawBatch_GL11_VertexShade(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5331 int texturesurfaceindex;
5335 vec3_t ambientcolor;
5336 vec3_t diffusecolor;
5340 VectorCopy(rsurface.modellight_lightdir, lightdir);
5341 ambientcolor[0] = rsurface.modellight_ambient[0] * r * 0.5f;
5342 ambientcolor[1] = rsurface.modellight_ambient[1] * g * 0.5f;
5343 ambientcolor[2] = rsurface.modellight_ambient[2] * b * 0.5f;
5344 diffusecolor[0] = rsurface.modellight_diffuse[0] * r * 0.5f;
5345 diffusecolor[1] = rsurface.modellight_diffuse[1] * g * 0.5f;
5346 diffusecolor[2] = rsurface.modellight_diffuse[2] * b * 0.5f;
5347 if (VectorLength2(diffusecolor) > 0)
5349 // generate color arrays for the surfaces in this list
5350 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5352 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5353 int numverts = surface->num_vertices;
5354 v = rsurface.vertex3f + 3 * surface->num_firstvertex;
5355 c2 = rsurface.normal3f + 3 * surface->num_firstvertex;
5356 c = rsurface.array_color4f + 4 * surface->num_firstvertex;
5357 // q3-style directional shading
5358 for (i = 0;i < numverts;i++, v += 3, c2 += 3, c += 4)
5360 if ((f = DotProduct(c2, lightdir)) > 0)
5361 VectorMA(ambientcolor, f, diffusecolor, c);
5363 VectorCopy(ambientcolor, c);
5372 rsurface.lightmapcolor4f = rsurface.array_color4f;
5373 rsurface.lightmapcolor4f_bufferobject = 0;
5374 rsurface.lightmapcolor4f_bufferoffset = 0;
5378 r = ambientcolor[0];
5379 g = ambientcolor[1];
5380 b = ambientcolor[2];
5381 rsurface.lightmapcolor4f = NULL;
5382 rsurface.lightmapcolor4f_bufferobject = 0;
5383 rsurface.lightmapcolor4f_bufferoffset = 0;
5385 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5386 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5387 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5388 GL_Color(r, g, b, a);
5389 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5392 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
5394 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5395 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5396 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5397 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5398 if (rsurface.mode != RSURFMODE_SHOWSURFACES)
5400 rsurface.mode = RSURFMODE_SHOWSURFACES;
5402 GL_BlendFunc(GL_ONE, GL_ZERO);
5403 R_Mesh_ColorPointer(NULL, 0, 0);
5404 R_Mesh_ResetTextureState();
5406 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5407 RSurf_DrawBatch_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
5410 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, msurface_t **texturesurfacelist)
5412 // transparent sky would be ridiculous
5413 if ((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
5415 if (rsurface.mode != RSURFMODE_SKY)
5417 if (rsurface.mode == RSURFMODE_GLSL)
5419 qglUseProgramObjectARB(0);CHECKGLERROR
5421 rsurface.mode = RSURFMODE_SKY;
5425 skyrendernow = false;
5427 // restore entity matrix
5428 R_Mesh_Matrix(&rsurface.matrix);
5430 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5431 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5432 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5433 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5435 // LordHavoc: HalfLife maps have freaky skypolys so don't use
5436 // skymasking on them, and Quake3 never did sky masking (unlike
5437 // software Quake and software Quake2), so disable the sky masking
5438 // in Quake3 maps as it causes problems with q3map2 sky tricks,
5439 // and skymasking also looks very bad when noclipping outside the
5440 // level, so don't use it then either.
5441 if (r_refdef.worldmodel && r_refdef.worldmodel->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_viewcache.world_novis)
5443 GL_Color(r_refdef.fogcolor[0] * r_view.colorscale, r_refdef.fogcolor[1] * r_view.colorscale, r_refdef.fogcolor[2] * r_view.colorscale, 1);
5444 R_Mesh_ColorPointer(NULL, 0, 0);
5445 R_Mesh_ResetTextureState();
5446 if (skyrendermasked)
5448 // depth-only (masking)
5449 GL_ColorMask(0,0,0,0);
5450 // just to make sure that braindead drivers don't draw
5451 // anything despite that colormask...
5452 GL_BlendFunc(GL_ZERO, GL_ONE);
5457 GL_BlendFunc(GL_ONE, GL_ZERO);
5459 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5460 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5461 if (skyrendermasked)
5462 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
5466 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, msurface_t **texturesurfacelist)
5468 if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION)))
5471 if (rsurface.mode != RSURFMODE_GLSL)
5473 rsurface.mode = RSURFMODE_GLSL;
5474 R_Mesh_ResetTextureState();
5477 R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
5478 R_Mesh_TexBind(0, R_GetTexture(rsurface.texture->currentskinframe->nmap));
5479 R_Mesh_TexBind(1, R_GetTexture(rsurface.texture->basetexture));
5480 R_Mesh_TexBind(2, R_GetTexture(rsurface.texture->glosstexture));
5481 R_Mesh_TexBind(4, R_GetTexture(r_texture_fogattenuation));
5482 R_Mesh_TexBind(5, R_GetTexture(rsurface.texture->currentskinframe->pants));
5483 R_Mesh_TexBind(6, R_GetTexture(rsurface.texture->currentskinframe->shirt));
5484 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
5486 R_Mesh_TexBind(7, R_GetTexture(r_texture_grey128));
5487 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5488 R_Mesh_ColorPointer(NULL, 0, 0);
5490 else if (rsurface.uselightmaptexture)
5492 R_Mesh_TexBind(7, R_GetTexture(texturesurfacelist[0]->lightmaptexture));
5493 R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
5494 R_Mesh_ColorPointer(NULL, 0, 0);
5498 R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
5499 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5500 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
5502 R_Mesh_TexBind(9, R_GetTexture(rsurface.texture->currentskinframe->glow));
5503 R_Mesh_TexBind(11, R_GetTexture(r_texture_white)); // changed per surface
5504 R_Mesh_TexBind(12, R_GetTexture(r_texture_white)); // changed per surface
5506 if (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
5508 // render background
5509 GL_BlendFunc(GL_ONE, GL_ZERO);
5511 GL_AlphaTest(false);
5513 GL_Color(1, 1, 1, 1);
5514 R_Mesh_ColorPointer(NULL, 0, 0);
5516 R_SetupSurfaceShader(vec3_origin, rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, 1, 1, rsurface.texture->specularscale, RSURFPASS_BACKGROUND);
5517 if (r_glsl_permutation)
5519 RSurf_PrepareVerticesForBatch(true, true, texturenumsurfaces, texturesurfacelist);
5520 R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
5521 R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
5522 R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
5523 R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
5524 R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
5525 RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection ? 12 : -1);
5528 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
5529 GL_DepthMask(false);
5530 GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
5531 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
5533 R_Mesh_TexBind(7, R_GetTexture(r_texture_grey128));
5534 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5535 R_Mesh_ColorPointer(NULL, 0, 0);
5537 else if (rsurface.uselightmaptexture)
5539 R_Mesh_TexBind(7, R_GetTexture(texturesurfacelist[0]->lightmaptexture));
5540 R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
5541 R_Mesh_ColorPointer(NULL, 0, 0);
5545 R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
5546 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5547 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
5549 R_Mesh_TexBind(11, R_GetTexture(r_texture_white)); // changed per surface
5550 R_Mesh_TexBind(12, R_GetTexture(r_texture_white)); // changed per surface
5553 R_SetupSurfaceShader(vec3_origin, rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, 1, 1, rsurface.texture->specularscale, RSURFPASS_BASE);
5554 if (!r_glsl_permutation)
5557 RSurf_PrepareVerticesForBatch(r_glsl_permutation->loc_Texture_Normal >= 0 || r_glsl_permutation->loc_LightDir >= 0, r_glsl_permutation->loc_Texture_Normal >= 0, texturenumsurfaces, texturesurfacelist);
5558 R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
5559 R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
5560 R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
5561 R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
5562 R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
5563 GL_Color(rsurface.texture->currentlayers[0].color[0], rsurface.texture->currentlayers[0].color[1], rsurface.texture->currentlayers[0].color[2], rsurface.texture->currentlayers[0].color[3]);
5565 if (r_glsl_permutation->loc_Texture_Refraction >= 0)
5567 GL_BlendFunc(GL_ONE, GL_ZERO);
5569 GL_AlphaTest(false);
5572 if (rsurface.uselightmaptexture && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
5574 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
5575 RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? 12 : -1);
5577 RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1);
5581 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
5582 RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? 12 : -1);
5584 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5586 if (rsurface.texture->backgroundnumskinframes && !(rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
5591 static void R_DrawTextureSurfaceList_GL13(int texturenumsurfaces, msurface_t **texturesurfacelist)
5593 // OpenGL 1.3 path - anything not completely ancient
5594 int texturesurfaceindex;
5595 qboolean applycolor;
5599 const texturelayer_t *layer;
5600 if (rsurface.mode != RSURFMODE_MULTIPASS)
5601 rsurface.mode = RSURFMODE_MULTIPASS;
5602 RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
5604 for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
5607 int layertexrgbscale;
5608 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5610 if (layerindex == 0)
5614 GL_AlphaTest(false);
5615 qglDepthFunc(GL_EQUAL);CHECKGLERROR
5618 GL_DepthMask(layer->depthmask);
5619 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
5620 if ((layer->color[0] > 2 || layer->color[1] > 2 || layer->color[2] > 2) && (gl_combine.integer || layer->depthmask))
5622 layertexrgbscale = 4;
5623 VectorScale(layer->color, 0.25f, layercolor);
5625 else if ((layer->color[0] > 1 || layer->color[1] > 1 || layer->color[2] > 1) && (gl_combine.integer || layer->depthmask))
5627 layertexrgbscale = 2;
5628 VectorScale(layer->color, 0.5f, layercolor);
5632 layertexrgbscale = 1;
5633 VectorScale(layer->color, 1.0f, layercolor);
5635 layercolor[3] = layer->color[3];
5636 applycolor = layercolor[0] != 1 || layercolor[1] != 1 || layercolor[2] != 1 || layercolor[3] != 1;
5637 R_Mesh_ColorPointer(NULL, 0, 0);
5638 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
5639 switch (layer->type)
5641 case TEXTURELAYERTYPE_LITTEXTURE:
5642 memset(&m, 0, sizeof(m));
5643 m.tex[0] = R_GetTexture(r_texture_white);
5644 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5645 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5646 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5647 m.tex[1] = R_GetTexture(layer->texture);
5648 m.texmatrix[1] = layer->texmatrix;
5649 m.texrgbscale[1] = layertexrgbscale;
5650 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
5651 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
5652 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
5653 R_Mesh_TextureState(&m);
5654 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5655 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5656 else if (rsurface.uselightmaptexture)
5657 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5659 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5661 case TEXTURELAYERTYPE_TEXTURE:
5662 memset(&m, 0, sizeof(m));
5663 m.tex[0] = R_GetTexture(layer->texture);
5664 m.texmatrix[0] = layer->texmatrix;
5665 m.texrgbscale[0] = layertexrgbscale;
5666 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5667 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5668 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5669 R_Mesh_TextureState(&m);
5670 RSurf_DrawBatch_GL11_Unlit(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5672 case TEXTURELAYERTYPE_FOG:
5673 memset(&m, 0, sizeof(m));
5674 m.texrgbscale[0] = layertexrgbscale;
5677 m.tex[0] = R_GetTexture(layer->texture);
5678 m.texmatrix[0] = layer->texmatrix;
5679 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5680 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5681 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5683 R_Mesh_TextureState(&m);
5684 // generate a color array for the fog pass
5685 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
5686 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5690 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5691 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)
5693 f = 1 - FogPoint_Model(v);
5694 c[0] = layercolor[0];
5695 c[1] = layercolor[1];
5696 c[2] = layercolor[2];
5697 c[3] = f * layercolor[3];
5700 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5703 Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
5705 GL_LockArrays(0, 0);
5708 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5710 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
5711 GL_AlphaTest(false);
5715 static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, msurface_t **texturesurfacelist)
5717 // OpenGL 1.1 - crusty old voodoo path
5718 int texturesurfaceindex;
5722 const texturelayer_t *layer;
5723 if (rsurface.mode != RSURFMODE_MULTIPASS)
5724 rsurface.mode = RSURFMODE_MULTIPASS;
5725 RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
5727 for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
5729 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5731 if (layerindex == 0)
5735 GL_AlphaTest(false);
5736 qglDepthFunc(GL_EQUAL);CHECKGLERROR
5739 GL_DepthMask(layer->depthmask);
5740 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
5741 R_Mesh_ColorPointer(NULL, 0, 0);
5742 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
5743 switch (layer->type)
5745 case TEXTURELAYERTYPE_LITTEXTURE:
5746 if (layer->blendfunc1 == GL_ONE && layer->blendfunc2 == GL_ZERO)
5748 // two-pass lit texture with 2x rgbscale
5749 // first the lightmap pass
5750 memset(&m, 0, sizeof(m));
5751 m.tex[0] = R_GetTexture(r_texture_white);
5752 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5753 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5754 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5755 R_Mesh_TextureState(&m);
5756 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5757 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5758 else if (rsurface.uselightmaptexture)
5759 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5761 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5762 GL_LockArrays(0, 0);
5763 // then apply the texture to it
5764 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
5765 memset(&m, 0, sizeof(m));
5766 m.tex[0] = R_GetTexture(layer->texture);
5767 m.texmatrix[0] = layer->texmatrix;
5768 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5769 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5770 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5771 R_Mesh_TextureState(&m);
5772 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);
5776 // single pass vertex-lighting-only texture with 1x rgbscale and transparency support
5777 memset(&m, 0, sizeof(m));
5778 m.tex[0] = R_GetTexture(layer->texture);
5779 m.texmatrix[0] = layer->texmatrix;
5780 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5781 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5782 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5783 R_Mesh_TextureState(&m);
5784 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5785 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);
5787 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);
5790 case TEXTURELAYERTYPE_TEXTURE:
5791 // singletexture unlit texture with transparency support
5792 memset(&m, 0, sizeof(m));
5793 m.tex[0] = R_GetTexture(layer->texture);
5794 m.texmatrix[0] = layer->texmatrix;
5795 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5796 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5797 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5798 R_Mesh_TextureState(&m);
5799 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);
5801 case TEXTURELAYERTYPE_FOG:
5802 // singletexture fogging
5803 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
5806 memset(&m, 0, sizeof(m));
5807 m.tex[0] = R_GetTexture(layer->texture);
5808 m.texmatrix[0] = layer->texmatrix;
5809 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5810 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5811 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5812 R_Mesh_TextureState(&m);
5815 R_Mesh_ResetTextureState();
5816 // generate a color array for the fog pass
5817 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5821 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5822 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)
5824 f = 1 - FogPoint_Model(v);
5825 c[0] = layer->color[0];
5826 c[1] = layer->color[1];
5827 c[2] = layer->color[2];
5828 c[3] = f * layer->color[3];
5831 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5834 Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
5836 GL_LockArrays(0, 0);
5839 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5841 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
5842 GL_AlphaTest(false);
5846 static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth, qboolean depthonly)
5848 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW)
5850 rsurface.rtlight = NULL;
5854 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
5856 if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
5858 if (rsurface.mode != RSURFMODE_MULTIPASS)
5859 rsurface.mode = RSURFMODE_MULTIPASS;
5860 if (r_depthfirst.integer == 3)
5862 int i = (int)(texturesurfacelist[0] - rsurface.modelsurfaces);
5863 if (!r_view.showdebug)
5864 GL_Color(0, 0, 0, 1);
5866 GL_Color(((i >> 6) & 7) / 7.0f, ((i >> 3) & 7) / 7.0f, (i & 7) / 7.0f,1);
5870 GL_ColorMask(0,0,0,0);
5873 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5874 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5875 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5877 GL_BlendFunc(GL_ONE, GL_ZERO);
5879 GL_AlphaTest(false);
5880 R_Mesh_ColorPointer(NULL, 0, 0);
5881 R_Mesh_ResetTextureState();
5882 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5883 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5884 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
5886 else if (r_depthfirst.integer == 3)
5888 else if (!r_view.showdebug && (r_showsurfaces.integer || gl_lightmaps.integer))
5890 GL_Color(0, 0, 0, 1);
5891 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5893 else if (r_showsurfaces.integer)
5895 if (rsurface.mode != RSURFMODE_MULTIPASS)
5896 rsurface.mode = RSURFMODE_MULTIPASS;
5897 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5898 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5900 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5901 GL_BlendFunc(GL_ONE, GL_ZERO);
5902 GL_DepthMask(writedepth);
5904 GL_AlphaTest(false);
5905 R_Mesh_ColorPointer(NULL, 0, 0);
5906 R_Mesh_ResetTextureState();
5907 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5908 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
5910 else if (gl_lightmaps.integer)
5913 if (rsurface.mode != RSURFMODE_MULTIPASS)
5914 rsurface.mode = RSURFMODE_MULTIPASS;
5915 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5917 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5918 GL_BlendFunc(GL_ONE, GL_ZERO);
5919 GL_DepthMask(writedepth);
5921 GL_AlphaTest(false);
5922 R_Mesh_ColorPointer(NULL, 0, 0);
5923 memset(&m, 0, sizeof(m));
5924 m.tex[0] = R_GetTexture(r_texture_white);
5925 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5926 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5927 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5928 R_Mesh_TextureState(&m);
5929 RSurf_PrepareVerticesForBatch(rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, false, texturenumsurfaces, texturesurfacelist);
5930 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5931 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5932 else if (rsurface.uselightmaptexture)
5933 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5935 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5937 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY)
5938 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
5939 else if (rsurface.texture->currentnumlayers)
5941 // write depth for anything we skipped on the depth-only pass earlier
5942 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5944 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5945 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5946 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5947 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5948 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
5949 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
5950 GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
5951 if (r_glsl.integer && gl_support_fragment_shader)
5952 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist);
5953 else if (gl_combine.integer && r_textureunits.integer >= 2)
5954 R_DrawTextureSurfaceList_GL13(texturenumsurfaces, texturesurfacelist);
5956 R_DrawTextureSurfaceList_GL11(texturenumsurfaces, texturesurfacelist);
5959 GL_LockArrays(0, 0);
5962 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
5965 int texturenumsurfaces, endsurface;
5967 msurface_t *surface;
5968 msurface_t *texturesurfacelist[1024];
5970 // if the model is static it doesn't matter what value we give for
5971 // wantnormals and wanttangents, so this logic uses only rules applicable
5972 // to a model, knowing that they are meaningless otherwise
5973 if (ent == r_refdef.worldentity)
5974 RSurf_ActiveWorldEntity();
5975 else if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
5976 RSurf_ActiveModelEntity(ent, false, false);
5978 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader);
5980 for (i = 0;i < numsurfaces;i = j)
5983 surface = rsurface.modelsurfaces + surfacelist[i];
5984 texture = surface->texture;
5985 R_UpdateTextureInfo(ent, texture);
5986 rsurface.texture = texture->currentframe;
5987 rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
5988 // scan ahead until we find a different texture
5989 endsurface = min(i + 1024, numsurfaces);
5990 texturenumsurfaces = 0;
5991 texturesurfacelist[texturenumsurfaces++] = surface;
5992 for (;j < endsurface;j++)
5994 surface = rsurface.modelsurfaces + surfacelist[j];
5995 if (texture != surface->texture || rsurface.uselightmaptexture != (surface->lightmaptexture != NULL))
5997 texturesurfacelist[texturenumsurfaces++] = surface;
5999 // render the range of surfaces
6000 R_DrawTextureSurfaceList(texturenumsurfaces, texturesurfacelist, true, false);
6006 void R_QueueSurfaceList(entity_render_t *ent, int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes)
6009 vec3_t tempcenter, center;
6011 // if we're rendering water textures (extra scene renders), use a separate loop to avoid burdening the main one
6014 for (i = 0;i < numsurfaces;i++)
6015 if (surfacelist[i]->texture->currentframe->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION))
6016 R_Water_AddWaterPlane(surfacelist[i]);
6019 // break the surface list down into batches by texture and use of lightmapping
6020 for (i = 0;i < numsurfaces;i = j)
6023 // texture is the base texture pointer, rsurface.texture is the
6024 // current frame/skin the texture is directing us to use (for example
6025 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
6026 // use skin 1 instead)
6027 texture = surfacelist[i]->texture;
6028 rsurface.texture = texture->currentframe;
6029 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
6030 if (!(rsurface.texture->currentmaterialflags & flagsmask))
6032 // if this texture is not the kind we want, skip ahead to the next one
6033 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
6037 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
6039 // transparent surfaces get pushed off into the transparent queue
6040 const msurface_t *surface = surfacelist[i];
6043 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
6044 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
6045 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
6046 Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
6047 R_MeshQueue_AddTransparent(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_view.origin : center, R_DrawSurface_TransparentCallback, ent, surface - rsurface.modelsurfaces, rsurface.rtlight);
6051 // simply scan ahead until we find a different texture or lightmap state
6052 for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.uselightmaptexture == (surfacelist[j]->lightmaptexture != NULL);j++)
6054 // render the range of surfaces
6055 R_DrawTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly);
6060 float locboxvertex3f[6*4*3] =
6062 1,0,1, 1,0,0, 1,1,0, 1,1,1,
6063 0,1,1, 0,1,0, 0,0,0, 0,0,1,
6064 1,1,1, 1,1,0, 0,1,0, 0,1,1,
6065 0,0,1, 0,0,0, 1,0,0, 1,0,1,
6066 0,0,1, 1,0,1, 1,1,1, 0,1,1,
6067 1,0,0, 0,0,0, 0,1,0, 1,1,0
6070 int locboxelement3i[6*2*3] =
6080 void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6083 cl_locnode_t *loc = (cl_locnode_t *)ent;
6085 float vertex3f[6*4*3];
6087 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6088 GL_DepthMask(false);
6089 GL_DepthRange(0, 1);
6090 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6092 GL_CullFace(GL_NONE);
6093 R_Mesh_Matrix(&identitymatrix);
6095 R_Mesh_VertexPointer(vertex3f, 0, 0);
6096 R_Mesh_ColorPointer(NULL, 0, 0);
6097 R_Mesh_ResetTextureState();
6100 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_view.colorscale,
6101 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_view.colorscale,
6102 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_view.colorscale,
6103 surfacelist[0] < 0 ? 0.5f : 0.125f);
6105 if (VectorCompare(loc->mins, loc->maxs))
6107 VectorSet(size, 2, 2, 2);
6108 VectorMA(loc->mins, -0.5f, size, mins);
6112 VectorCopy(loc->mins, mins);
6113 VectorSubtract(loc->maxs, loc->mins, size);
6116 for (i = 0;i < 6*4*3;)
6117 for (j = 0;j < 3;j++, i++)
6118 vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
6120 R_Mesh_Draw(0, 6*4, 6*2, locboxelement3i, 0, 0);
6123 void R_DrawLocs(void)
6126 cl_locnode_t *loc, *nearestloc;
6128 nearestloc = CL_Locs_FindNearest(cl.movement_origin);
6129 for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
6131 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
6132 R_MeshQueue_AddTransparent(center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
6136 void R_DrawDebugModel(entity_render_t *ent)
6138 int i, j, k, l, flagsmask;
6139 const int *elements;
6141 msurface_t *surface;
6142 model_t *model = ent->model;
6145 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WATER | MATERIALFLAG_WALL;
6147 R_Mesh_ColorPointer(NULL, 0, 0);
6148 R_Mesh_ResetTextureState();
6149 GL_DepthRange(0, 1);
6150 GL_DepthTest(!r_showdisabledepthtest.integer);
6151 GL_DepthMask(false);
6152 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6154 if (r_showcollisionbrushes.value > 0 && model->brush.num_brushes)
6156 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
6157 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
6159 if (brush->colbrushf && brush->colbrushf->numtriangles)
6161 R_Mesh_VertexPointer(brush->colbrushf->points->v, 0, 0);
6162 GL_Color((i & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 5) & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 10) & 31) * (1.0f / 32.0f) * r_view.colorscale, r_showcollisionbrushes.value);
6163 R_Mesh_Draw(0, brush->colbrushf->numpoints, brush->colbrushf->numtriangles, brush->colbrushf->elements, 0, 0);
6166 for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
6168 if (surface->num_collisiontriangles)
6170 R_Mesh_VertexPointer(surface->data_collisionvertex3f, 0, 0);
6171 GL_Color((i & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 5) & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 10) & 31) * (1.0f / 32.0f) * r_view.colorscale, r_showcollisionbrushes.value);
6172 R_Mesh_Draw(0, surface->num_collisionvertices, surface->num_collisiontriangles, surface->data_collisionelement3i, 0, 0);
6177 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6179 if (r_showtris.integer || r_shownormals.integer)
6181 if (r_showdisabledepthtest.integer)
6183 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6184 GL_DepthMask(false);
6188 GL_BlendFunc(GL_ONE, GL_ZERO);
6191 for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
6193 if (ent == r_refdef.worldentity && !r_viewcache.world_surfacevisible[j])
6195 rsurface.texture = surface->texture->currentframe;
6196 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
6198 RSurf_PrepareVerticesForBatch(true, true, 1, &surface);
6199 if (r_showtris.value > 0)
6201 if (!rsurface.texture->currentlayers->depthmask)
6202 GL_Color(r_view.colorscale, 0, 0, r_showtris.value);
6203 else if (ent == r_refdef.worldentity)
6204 GL_Color(r_view.colorscale, r_view.colorscale, r_view.colorscale, r_showtris.value);
6206 GL_Color(0, r_view.colorscale, 0, r_showtris.value);
6207 elements = (ent->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
6210 for (k = 0;k < surface->num_triangles;k++, elements += 3)
6212 #define GLVERTEXELEMENT(n) qglVertex3f(rsurface.vertex3f[elements[n]*3+0], rsurface.vertex3f[elements[n]*3+1], rsurface.vertex3f[elements[n]*3+2])
6213 GLVERTEXELEMENT(0);GLVERTEXELEMENT(1);
6214 GLVERTEXELEMENT(1);GLVERTEXELEMENT(2);
6215 GLVERTEXELEMENT(2);GLVERTEXELEMENT(0);
6220 if (r_shownormals.value > 0)
6222 GL_Color(r_view.colorscale, 0, 0, r_shownormals.value);
6224 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6226 VectorCopy(rsurface.vertex3f + l * 3, v);
6227 qglVertex3f(v[0], v[1], v[2]);
6228 VectorMA(v, 8, rsurface.svector3f + l * 3, v);
6229 qglVertex3f(v[0], v[1], v[2]);
6233 GL_Color(0, 0, r_view.colorscale, r_shownormals.value);
6235 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6237 VectorCopy(rsurface.vertex3f + l * 3, v);
6238 qglVertex3f(v[0], v[1], v[2]);
6239 VectorMA(v, 8, rsurface.tvector3f + l * 3, v);
6240 qglVertex3f(v[0], v[1], v[2]);
6244 GL_Color(0, r_view.colorscale, 0, r_shownormals.value);
6246 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6248 VectorCopy(rsurface.vertex3f + l * 3, v);
6249 qglVertex3f(v[0], v[1], v[2]);
6250 VectorMA(v, 8, rsurface.normal3f + l * 3, v);
6251 qglVertex3f(v[0], v[1], v[2]);
6258 rsurface.texture = NULL;
6262 extern void R_BuildLightMap(const entity_render_t *ent, msurface_t *surface);
6263 void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
6265 int i, j, endj, f, flagsmask;
6266 msurface_t *surface;
6268 model_t *model = r_refdef.worldmodel;
6269 const int maxsurfacelist = 1024;
6270 int numsurfacelist = 0;
6271 msurface_t *surfacelist[1024];
6275 RSurf_ActiveWorldEntity();
6277 // update light styles on this submodel
6278 if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.num_lightstyles)
6280 model_brush_lightstyleinfo_t *style;
6281 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
6283 if (style->value != r_refdef.lightstylevalue[style->style])
6285 msurface_t *surfaces = model->data_surfaces;
6286 int *list = style->surfacelist;
6287 style->value = r_refdef.lightstylevalue[style->style];
6288 for (j = 0;j < style->numsurfaces;j++)
6289 surfaces[list[j]].cached_dlight = true;
6294 R_UpdateAllTextureInfo(r_refdef.worldentity);
6295 flagsmask = addwaterplanes ? (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
6299 R_DrawDebugModel(r_refdef.worldentity);
6305 rsurface.uselightmaptexture = false;
6306 rsurface.texture = NULL;
6308 j = model->firstmodelsurface;
6309 endj = j + model->nummodelsurfaces;
6312 // quickly skip over non-visible surfaces
6313 for (;j < endj && !r_viewcache.world_surfacevisible[j];j++)
6315 // quickly iterate over visible surfaces
6316 for (;j < endj && r_viewcache.world_surfacevisible[j];j++)
6318 // process this surface
6319 surface = model->data_surfaces + j;
6320 // if this surface fits the criteria, add it to the list
6321 if (surface->num_triangles)
6323 // if lightmap parameters changed, rebuild lightmap texture
6324 if (surface->cached_dlight)
6325 R_BuildLightMap(r_refdef.worldentity, surface);
6326 // add face to draw list
6327 surfacelist[numsurfacelist++] = surface;
6328 r_refdef.stats.world_triangles += surface->num_triangles;
6329 if (numsurfacelist >= maxsurfacelist)
6331 r_refdef.stats.world_surfaces += numsurfacelist;
6332 R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6338 r_refdef.stats.world_surfaces += numsurfacelist;
6340 R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6344 void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
6346 int i, j, f, flagsmask;
6347 msurface_t *surface, *endsurface;
6349 model_t *model = ent->model;
6350 const int maxsurfacelist = 1024;
6351 int numsurfacelist = 0;
6352 msurface_t *surfacelist[1024];
6356 // if the model is static it doesn't matter what value we give for
6357 // wantnormals and wanttangents, so this logic uses only rules applicable
6358 // to a model, knowing that they are meaningless otherwise
6359 if (ent == r_refdef.worldentity)
6360 RSurf_ActiveWorldEntity();
6361 else if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
6362 RSurf_ActiveModelEntity(ent, false, false);
6364 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader && !depthonly);
6366 // update light styles
6367 if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.num_lightstyles)
6369 model_brush_lightstyleinfo_t *style;
6370 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
6372 if (style->value != r_refdef.lightstylevalue[style->style])
6374 msurface_t *surfaces = model->data_surfaces;
6375 int *list = style->surfacelist;
6376 style->value = r_refdef.lightstylevalue[style->style];
6377 for (j = 0;j < style->numsurfaces;j++)
6378 surfaces[list[j]].cached_dlight = true;
6383 R_UpdateAllTextureInfo(ent);
6384 flagsmask = addwaterplanes ? (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
6388 R_DrawDebugModel(ent);
6394 rsurface.uselightmaptexture = false;
6395 rsurface.texture = NULL;
6397 surface = model->data_surfaces + model->firstmodelsurface;
6398 endsurface = surface + model->nummodelsurfaces;
6399 for (;surface < endsurface;surface++)
6401 // if this surface fits the criteria, add it to the list
6402 if (surface->num_triangles)
6404 // if lightmap parameters changed, rebuild lightmap texture
6405 if (surface->cached_dlight)
6406 R_BuildLightMap(ent, surface);
6407 // add face to draw list
6408 surfacelist[numsurfacelist++] = surface;
6409 r_refdef.stats.entities_triangles += surface->num_triangles;
6410 if (numsurfacelist >= maxsurfacelist)
6412 r_refdef.stats.entities_surfaces += numsurfacelist;
6413 R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6418 r_refdef.stats.entities_surfaces += numsurfacelist;
6420 R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);