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;
250 r_refdef.fog_start = 0;
251 r_refdef.fog_end = 1000000000;
254 float FogForDistance(vec_t dist)
256 unsigned int fogmasktableindex = (unsigned int)(bound(0, dist - r_refdef.fog_start, r_refdef.fog_end - r_refdef.fog_start) * r_refdef.fogmasktabledistmultiplier);
257 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
260 float FogPoint_World(const vec3_t p)
262 return FogForDistance(VectorDistance((p), r_view.origin));
265 float FogPoint_Model(const vec3_t p)
267 return FogForDistance(VectorDistance((p), rsurface.modelorg));
270 static void R_BuildBlankTextures(void)
272 unsigned char data[4];
273 data[2] = 128; // normal X
274 data[1] = 128; // normal Y
275 data[0] = 255; // normal Z
276 data[3] = 128; // height
277 r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
282 r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
287 r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
292 r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
295 static void R_BuildNoTexture(void)
298 unsigned char pix[16][16][4];
299 // this makes a light grey/dark grey checkerboard texture
300 for (y = 0;y < 16;y++)
302 for (x = 0;x < 16;x++)
304 if ((y < 8) ^ (x < 8))
320 r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, NULL);
323 static void R_BuildWhiteCube(void)
325 unsigned char data[6*1*1*4];
326 memset(data, 255, sizeof(data));
327 r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
330 static void R_BuildNormalizationCube(void)
334 vec_t s, t, intensity;
336 unsigned char data[6][NORMSIZE][NORMSIZE][4];
337 for (side = 0;side < 6;side++)
339 for (y = 0;y < NORMSIZE;y++)
341 for (x = 0;x < NORMSIZE;x++)
343 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
344 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
379 intensity = 127.0f / sqrt(DotProduct(v, v));
380 data[side][y][x][2] = (unsigned char)(128.0f + intensity * v[0]);
381 data[side][y][x][1] = (unsigned char)(128.0f + intensity * v[1]);
382 data[side][y][x][0] = (unsigned char)(128.0f + intensity * v[2]);
383 data[side][y][x][3] = 255;
387 r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, &data[0][0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
390 static void R_BuildFogTexture(void)
394 unsigned char data1[FOGWIDTH][4];
395 //unsigned char data2[FOGWIDTH][4];
396 for (x = 0;x < FOGWIDTH;x++)
398 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
403 //data2[x][0] = 255 - b;
404 //data2[x][1] = 255 - b;
405 //data2[x][2] = 255 - b;
408 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);
409 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
412 static const char *builtinshaderstring =
413 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
414 "// written by Forest 'LordHavoc' Hale\n"
416 "// common definitions between vertex shader and fragment shader:\n"
418 "#ifdef __GLSL_CG_DATA_TYPES\n"
419 "# define myhalf half\n"
420 "# define myhvec2 hvec2\n"
421 "# define myhvec3 hvec3\n"
422 "# define myhvec4 hvec4\n"
424 "# define myhalf float\n"
425 "# define myhvec2 vec2\n"
426 "# define myhvec3 vec3\n"
427 "# define myhvec4 vec4\n"
430 "varying vec2 TexCoord;\n"
431 "varying vec2 TexCoordLightmap;\n"
433 "//#ifdef MODE_LIGHTSOURCE\n"
434 "varying vec3 CubeVector;\n"
437 "//#ifdef MODE_LIGHTSOURCE\n"
438 "varying vec3 LightVector;\n"
440 "//# ifdef MODE_LIGHTDIRECTION\n"
441 "//varying vec3 LightVector;\n"
445 "varying vec3 EyeVector;\n"
447 "varying vec3 EyeVectorModelSpace;\n"
450 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
451 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
452 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
454 "//#ifdef MODE_WATER\n"
455 "varying vec4 ModelViewProjectionPosition;\n"
457 "//# ifdef MODE_REFRACTION\n"
458 "//varying vec4 ModelViewProjectionPosition;\n"
460 "//# ifdef USEREFLECTION\n"
461 "//varying vec4 ModelViewProjectionPosition;\n"
470 "// vertex shader specific:\n"
471 "#ifdef VERTEX_SHADER\n"
473 "uniform vec3 LightPosition;\n"
474 "uniform vec3 EyePosition;\n"
475 "uniform vec3 LightDir;\n"
477 "// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3)\n"
481 " gl_FrontColor = gl_Color;\n"
482 " // copy the surface texcoord\n"
483 " TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);\n"
484 "#ifndef MODE_LIGHTSOURCE\n"
485 "# ifndef MODE_LIGHTDIRECTION\n"
486 " TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
490 "#ifdef MODE_LIGHTSOURCE\n"
491 " // transform vertex position into light attenuation/cubemap space\n"
492 " // (-1 to +1 across the light box)\n"
493 " CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex);\n"
495 " // transform unnormalized light direction into tangent space\n"
496 " // (we use unnormalized to ensure that it interpolates correctly and then\n"
497 " // normalize it per pixel)\n"
498 " vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
499 " LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
500 " LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
501 " LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
504 "#ifdef MODE_LIGHTDIRECTION\n"
505 " LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
506 " LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
507 " LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
510 " // transform unnormalized eye direction into tangent space\n"
512 " vec3 EyeVectorModelSpace;\n"
514 " EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
515 " EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
516 " EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
517 " EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
519 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
520 " VectorS = gl_MultiTexCoord1.xyz;\n"
521 " VectorT = gl_MultiTexCoord2.xyz;\n"
522 " VectorR = gl_MultiTexCoord3.xyz;\n"
525 "//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION)\n"
526 "// ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix;\n"
527 "// //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
528 "// //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
531 "// transform vertex to camera space, using ftransform to match non-VS\n"
533 " gl_Position = ftransform();\n"
535 "#ifdef MODE_WATER\n"
536 " ModelViewProjectionPosition = gl_Position;\n"
538 "#ifdef MODE_REFRACTION\n"
539 " ModelViewProjectionPosition = gl_Position;\n"
541 "#ifdef USEREFLECTION\n"
542 " ModelViewProjectionPosition = gl_Position;\n"
546 "#endif // VERTEX_SHADER\n"
551 "// fragment shader specific:\n"
552 "#ifdef FRAGMENT_SHADER\n"
554 "// 13 textures, we can only use up to 16 on DX9-class hardware\n"
555 "uniform sampler2D Texture_Normal;\n"
556 "uniform sampler2D Texture_Color;\n"
557 "uniform sampler2D Texture_Gloss;\n"
558 "uniform samplerCube Texture_Cube;\n"
559 "uniform sampler2D Texture_Attenuation;\n"
560 "uniform sampler2D Texture_FogMask;\n"
561 "uniform sampler2D Texture_Pants;\n"
562 "uniform sampler2D Texture_Shirt;\n"
563 "uniform sampler2D Texture_Lightmap;\n"
564 "uniform sampler2D Texture_Deluxemap;\n"
565 "uniform sampler2D Texture_Glow;\n"
566 "uniform sampler2D Texture_Reflection;\n"
567 "uniform sampler2D Texture_Refraction;\n"
569 "uniform myhvec3 LightColor;\n"
570 "uniform myhvec3 AmbientColor;\n"
571 "uniform myhvec3 DiffuseColor;\n"
572 "uniform myhvec3 SpecularColor;\n"
573 "uniform myhvec3 Color_Pants;\n"
574 "uniform myhvec3 Color_Shirt;\n"
575 "uniform myhvec3 FogColor;\n"
577 "//#ifdef MODE_WATER\n"
578 "uniform vec4 DistortScaleRefractReflect;\n"
579 "uniform vec4 ScreenScaleRefractReflect;\n"
580 "uniform vec4 ScreenCenterRefractReflect;\n"
581 "uniform myhvec4 RefractColor;\n"
582 "uniform myhvec4 ReflectColor;\n"
583 "uniform myhalf ReflectFactor;\n"
584 "uniform myhalf ReflectOffset;\n"
586 "//# ifdef MODE_REFRACTION\n"
587 "//uniform vec4 DistortScaleRefractReflect;\n"
588 "//uniform vec4 ScreenScaleRefractReflect;\n"
589 "//uniform vec4 ScreenCenterRefractReflect;\n"
590 "//uniform myhvec4 RefractColor;\n"
591 "//# ifdef USEREFLECTION\n"
592 "//uniform myhvec4 ReflectColor;\n"
595 "//# ifdef USEREFLECTION\n"
596 "//uniform vec4 DistortScaleRefractReflect;\n"
597 "//uniform vec4 ScreenScaleRefractReflect;\n"
598 "//uniform vec4 ScreenCenterRefractReflect;\n"
599 "//uniform myhvec4 ReflectColor;\n"
604 "uniform myhalf GlowScale;\n"
605 "uniform myhalf SceneBrightness;\n"
606 "#ifdef USECONTRASTBOOST\n"
607 "uniform myhalf ContrastBoostCoeff;\n"
610 "uniform float OffsetMapping_Scale;\n"
611 "uniform float OffsetMapping_Bias;\n"
612 "uniform float FogRangeRecip;\n"
613 "uniform float FogStart;\n"
614 "uniform float FogLength;\n"
616 "uniform myhalf AmbientScale;\n"
617 "uniform myhalf DiffuseScale;\n"
618 "uniform myhalf SpecularScale;\n"
619 "uniform myhalf SpecularPower;\n"
621 "#ifdef USEOFFSETMAPPING\n"
622 "vec2 OffsetMapping(vec2 TexCoord)\n"
624 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
625 " // 14 sample relief mapping: linear search and then binary search\n"
626 " // this basically steps forward a small amount repeatedly until it finds\n"
627 " // itself inside solid, then jitters forward and back using decreasing\n"
628 " // amounts to find the impact\n"
629 " //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
630 " //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
631 " vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
632 " vec3 RT = vec3(TexCoord, 1);\n"
633 " OffsetVector *= 0.1;\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);\n"
636 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
637 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
638 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
639 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
640 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
641 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
642 " RT += OffsetVector * step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
643 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) - 0.5);\n"
644 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5 - 0.25);\n"
645 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25 - 0.125);\n"
646 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125 - 0.0625);\n"
647 " RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
650 " // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
651 " // this basically moves forward the full distance, and then backs up based\n"
652 " // on height of samples\n"
653 " //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
654 " //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
655 " vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
656 " TexCoord += OffsetVector;\n"
657 " OffsetVector *= 0.333;\n"
658 " TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
659 " TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
660 " TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
661 " return TexCoord;\n"
664 "#endif // USEOFFSETMAPPING\n"
666 "#ifdef MODE_WATER\n"
671 "#ifdef USEOFFSETMAPPING\n"
672 " // apply offsetmapping\n"
673 " vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
674 "#define TexCoord TexCoordOffset\n"
677 " vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
678 " //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
679 " vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec2(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
680 " float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 5.0) * ReflectFactor + ReflectOffset;\n"
681 " gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel);\n"
684 "#else // MODE_WATER\n"
685 "#ifdef MODE_REFRACTION\n"
687 "// refraction pass\n"
690 "#ifdef USEOFFSETMAPPING\n"
691 " // apply offsetmapping\n"
692 " vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
693 "#define TexCoord TexCoordOffset\n"
696 " vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
697 " //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
698 " vec2 ScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy + vec2(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
699 " gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor;\n"
702 "#else // MODE_REFRACTION\n"
705 "#ifdef USEOFFSETMAPPING\n"
706 " // apply offsetmapping\n"
707 " vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
708 "#define TexCoord TexCoordOffset\n"
711 " // combine the diffuse textures (base, pants, shirt)\n"
712 " myhvec4 color = myhvec4(texture2D(Texture_Color, TexCoord));\n"
713 "#ifdef USECOLORMAPPING\n"
714 " color.rgb += myhvec3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhvec3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
720 "#ifdef MODE_LIGHTSOURCE\n"
723 " // calculate surface normal, light normal, and specular normal\n"
724 " // compute color intensity for the two textures (colormap and glossmap)\n"
725 " // scale by light color and attenuation as efficiently as possible\n"
726 " // (do as much scalar math as possible rather than vector math)\n"
727 "# ifdef USESPECULAR\n"
728 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
729 " myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
730 " myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
732 " // calculate directional shading\n"
733 " 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"
735 "# ifdef USEDIFFUSE\n"
736 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
737 " myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
739 " // calculate directional shading\n"
740 " 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"
742 " // calculate directionless shading\n"
743 " color.rgb = color.rgb * LightColor * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
747 "# ifdef USECUBEFILTER\n"
748 " // apply light cubemap filter\n"
749 " //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector));\n"
750 " color.rgb *= myhvec3(textureCube(Texture_Cube, CubeVector));\n"
752 " color *= myhvec4(gl_Color);\n"
753 "#endif // MODE_LIGHTSOURCE\n"
758 "#ifdef MODE_LIGHTDIRECTION\n"
759 " // directional model lighting\n"
760 "# ifdef USESPECULAR\n"
761 " // get the surface normal and light normal\n"
762 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
763 " myhvec3 diffusenormal = myhvec3(LightVector);\n"
765 " // calculate directional shading\n"
766 " color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
767 " myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
768 " color.rgb += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
770 "# ifdef USEDIFFUSE\n"
771 " // get the surface normal and light normal\n"
772 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
773 " myhvec3 diffusenormal = myhvec3(LightVector);\n"
775 " // calculate directional shading\n"
776 " color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
778 " color.rgb *= AmbientColor;\n"
782 " color *= myhvec4(gl_Color);\n"
783 "#endif // MODE_LIGHTDIRECTION\n"
788 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
789 " // deluxemap lightmapping using light vectors in modelspace (evil q3map2)\n"
791 " // get the surface normal and light normal\n"
792 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
794 " myhvec3 diffusenormal_modelspace = myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5);\n"
795 " myhvec3 diffusenormal = normalize(myhvec3(dot(diffusenormal_modelspace, myhvec3(VectorS)), dot(diffusenormal_modelspace, myhvec3(VectorT)), dot(diffusenormal_modelspace, myhvec3(VectorR))));\n"
796 " // calculate directional shading\n"
797 " myhvec3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)));\n"
798 "# ifdef USESPECULAR\n"
799 " myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
800 " tempcolor += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
803 " // apply lightmap color\n"
804 " color.rgb = color.rgb * AmbientScale + tempcolor * myhvec3(texture2D(Texture_Lightmap, TexCoordLightmap)) * myhvec3(gl_Color);\n"
805 " color.a *= myhalf(gl_Color.a);\n"
806 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
811 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
812 " // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
814 " // get the surface normal and light normal\n"
815 " myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
817 " myhvec3 diffusenormal = normalize(myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5));\n"
818 " // calculate directional shading\n"
819 " myhvec3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)));\n"
820 "# ifdef USESPECULAR\n"
821 " myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
822 " tempcolor += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
825 " // apply lightmap color\n"
826 " color.rgb = color.rgb * AmbientScale + tempcolor * myhvec3(texture2D(Texture_Lightmap, TexCoordLightmap)) * myhvec3(gl_Color);\n"
827 " color.a *= myhalf(gl_Color.a);\n"
828 "#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
833 "#ifdef MODE_LIGHTMAP\n"
834 " // apply lightmap color\n"
835 " color *= myhvec4(texture2D(Texture_Lightmap, TexCoordLightmap)) * myhvec4(gl_Color) * myhvec4(myhvec3(DiffuseScale), 1) + myhvec4(myhvec3(AmbientScale), 0);\n"
836 "#endif // MODE_LIGHTMAP\n"
846 " color.rgb += myhvec3(texture2D(Texture_Glow, TexCoord)) * GlowScale;\n"
849 "#ifndef MODE_LIGHTSOURCE\n"
850 "# ifdef USEREFLECTION\n"
851 " vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
852 " //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
853 " vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec3(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
854 " color.rgb = mix(color.rgb, myhvec3(texture2D(Texture_Reflection, ScreenTexCoord.zw)) * ReflectColor.rgb, ReflectColor.a);\n"
858 "#ifdef USECONTRASTBOOST\n"
859 " color.rgb = color.rgb / (ContrastBoostCoeff * color.rgb + myhvec3(1, 1, 1));\n"
862 " color.rgb *= SceneBrightness;\n"
866 " color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhvec2(max(0.0, min(length(EyeVectorModelSpace) - FogStart, FogLength))*FogRangeRecip, 0.0))));\n"
867 //" color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhvec2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0))));\n"
870 " gl_FragColor = vec4(color);\n"
872 "#endif // MODE_REFRACTION\n"
873 "#endif // MODE_WATER\n"
875 "#endif // FRAGMENT_SHADER\n"
878 #define SHADERPERMUTATION_COLORMAPPING (1<<0) // indicates this is a colormapped skin
879 #define SHADERPERMUTATION_CONTRASTBOOST (1<<1) // r_glsl_contrastboost boosts the contrast at low color levels (similar to gamma)
880 #define SHADERPERMUTATION_FOG (1<<2) // tint the color by fog color or black if using additive blend mode
881 #define SHADERPERMUTATION_CUBEFILTER (1<<3) // (lightsource) use cubemap light filter
882 #define SHADERPERMUTATION_GLOW (1<<4) // (lightmap) blend in an additive glow texture
883 #define SHADERPERMUTATION_DIFFUSE (1<<5) // (lightsource) whether to use directional shading
884 #define SHADERPERMUTATION_SPECULAR (1<<6) // (lightsource or deluxemapping) render specular effects
885 #define SHADERPERMUTATION_REFLECTION (1<<7) // normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
886 #define SHADERPERMUTATION_OFFSETMAPPING (1<<8) // adjust texcoords to roughly simulate a displacement mapped surface
887 #define SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING (1<<9) // adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
888 #define SHADERPERMUTATION_MODEBASE (1<<10) // multiplier for the SHADERMODE_ values to get a valid index
890 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
891 const char *shaderpermutationinfo[][2] =
893 {"#define USECOLORMAPPING\n", " colormapping"},
894 {"#define USECONTRASTBOOST\n", " contrastboost"},
895 {"#define USEFOG\n", " fog"},
896 {"#define USECUBEFILTER\n", " cubefilter"},
897 {"#define USEGLOW\n", " glow"},
898 {"#define USEDIFFUSE\n", " diffuse"},
899 {"#define USESPECULAR\n", " specular"},
900 {"#define USEREFLECTION\n", " reflection"},
901 {"#define USEOFFSETMAPPING\n", " offsetmapping"},
902 {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
906 // this enum is multiplied by SHADERPERMUTATION_MODEBASE
907 typedef enum shadermode_e
909 SHADERMODE_LIGHTMAP, // (lightmap) use directional pixel shading from fixed light direction (q3bsp)
910 SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE, // (lightmap) use directional pixel shading from texture containing modelspace light directions (deluxemap)
911 SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE, // (lightmap) use directional pixel shading from texture containing tangentspace light directions (deluxemap)
912 SHADERMODE_LIGHTDIRECTION, // (lightmap) use directional pixel shading from fixed light direction (q3bsp)
913 SHADERMODE_LIGHTSOURCE, // (lightsource) use directional pixel shading from light source (rtlight)
914 SHADERMODE_REFRACTION, // refract background (the material is rendered normally after this pass)
915 SHADERMODE_WATER, // refract background and reflection (the material is rendered normally after this pass)
920 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
921 const char *shadermodeinfo[][2] =
923 {"#define MODE_LIGHTMAP\n", " lightmap"},
924 {"#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
925 {"#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
926 {"#define MODE_LIGHTDIRECTION\n", " lightdirection"},
927 {"#define MODE_LIGHTSOURCE\n", " lightsource"},
928 {"#define MODE_REFRACTION\n", " refraction"},
929 {"#define MODE_WATER\n", " water"},
933 #define SHADERPERMUTATION_INDICES (SHADERPERMUTATION_MODEBASE * SHADERMODE_COUNT)
935 typedef struct r_glsl_permutation_s
937 // indicates if we have tried compiling this permutation already
939 // 0 if compilation failed
941 // locations of detected uniforms in program object, or -1 if not found
942 int loc_Texture_Normal;
943 int loc_Texture_Color;
944 int loc_Texture_Gloss;
945 int loc_Texture_Cube;
946 int loc_Texture_Attenuation;
947 int loc_Texture_FogMask;
948 int loc_Texture_Pants;
949 int loc_Texture_Shirt;
950 int loc_Texture_Lightmap;
951 int loc_Texture_Deluxemap;
952 int loc_Texture_Glow;
953 int loc_Texture_Refraction;
954 int loc_Texture_Reflection;
956 int loc_LightPosition;
961 int loc_FogRangeRecip;
964 int loc_AmbientScale;
965 int loc_DiffuseScale;
966 int loc_SpecularScale;
967 int loc_SpecularPower;
969 int loc_SceneBrightness; // or: Scenebrightness * ContrastBoost
970 int loc_OffsetMapping_Scale;
971 int loc_AmbientColor;
972 int loc_DiffuseColor;
973 int loc_SpecularColor;
975 int loc_ContrastBoostCoeff; // 1 - 1/ContrastBoost
976 int loc_DistortScaleRefractReflect;
977 int loc_ScreenScaleRefractReflect;
978 int loc_ScreenCenterRefractReflect;
979 int loc_RefractColor;
980 int loc_ReflectColor;
981 int loc_ReflectFactor;
982 int loc_ReflectOffset;
984 r_glsl_permutation_t;
986 // information about each possible shader permutation
987 r_glsl_permutation_t r_glsl_permutations[SHADERPERMUTATION_INDICES];
988 // currently selected permutation
989 r_glsl_permutation_t *r_glsl_permutation;
991 // these are additional flags used only by R_GLSL_CompilePermutation
992 #define SHADERTYPE_USES_VERTEXSHADER (1<<0)
993 #define SHADERTYPE_USES_GEOMETRYSHADER (1<<1)
994 #define SHADERTYPE_USES_FRAGMENTSHADER (1<<2)
996 static void R_GLSL_CompilePermutation(const char *filename, int permutation, int shadertype)
999 qboolean shaderfound;
1000 r_glsl_permutation_t *p = r_glsl_permutations + permutation;
1001 int vertstrings_count;
1002 int geomstrings_count;
1003 int fragstrings_count;
1005 const char *vertstrings_list[32+1];
1006 const char *geomstrings_list[32+1];
1007 const char *fragstrings_list[32+1];
1008 char permutationname[256];
1013 vertstrings_list[0] = "#define VERTEX_SHADER\n";
1014 geomstrings_list[0] = "#define GEOMETRY_SHADER\n";
1015 fragstrings_list[0] = "#define FRAGMENT_SHADER\n";
1016 vertstrings_count = 1;
1017 geomstrings_count = 1;
1018 fragstrings_count = 1;
1019 permutationname[0] = 0;
1020 i = permutation / SHADERPERMUTATION_MODEBASE;
1021 vertstrings_list[vertstrings_count++] = shadermodeinfo[i][0];
1022 geomstrings_list[geomstrings_count++] = shadermodeinfo[i][0];
1023 fragstrings_list[fragstrings_count++] = shadermodeinfo[i][0];
1024 strlcat(permutationname, shadermodeinfo[i][1], sizeof(permutationname));
1025 for (i = 0;shaderpermutationinfo[i][0];i++)
1027 if (permutation & (1<<i))
1029 vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i][0];
1030 geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i][0];
1031 fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i][0];
1032 strlcat(permutationname, shaderpermutationinfo[i][1], sizeof(permutationname));
1036 // keep line numbers correct
1037 vertstrings_list[vertstrings_count++] = "\n";
1038 geomstrings_list[geomstrings_count++] = "\n";
1039 fragstrings_list[fragstrings_count++] = "\n";
1042 shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
1043 shaderfound = false;
1046 Con_DPrint("from disk... ");
1047 vertstrings_list[vertstrings_count++] = shaderstring;
1048 geomstrings_list[geomstrings_count++] = shaderstring;
1049 fragstrings_list[fragstrings_count++] = shaderstring;
1052 else if (!strcmp(filename, "glsl/default.glsl"))
1054 vertstrings_list[vertstrings_count++] = builtinshaderstring;
1055 geomstrings_list[geomstrings_count++] = builtinshaderstring;
1056 fragstrings_list[fragstrings_count++] = builtinshaderstring;
1059 // clear any lists that are not needed by this shader
1060 if (!(shadertype & SHADERTYPE_USES_VERTEXSHADER))
1061 vertstrings_count = 0;
1062 if (!(shadertype & SHADERTYPE_USES_GEOMETRYSHADER))
1063 geomstrings_count = 0;
1064 if (!(shadertype & SHADERTYPE_USES_FRAGMENTSHADER))
1065 fragstrings_count = 0;
1066 // compile the shader program
1067 if (shaderfound && vertstrings_count + geomstrings_count + fragstrings_count)
1068 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1072 qglUseProgramObjectARB(p->program);CHECKGLERROR
1073 // look up all the uniform variable names we care about, so we don't
1074 // have to look them up every time we set them
1075 p->loc_Texture_Normal = qglGetUniformLocationARB(p->program, "Texture_Normal");
1076 p->loc_Texture_Color = qglGetUniformLocationARB(p->program, "Texture_Color");
1077 p->loc_Texture_Gloss = qglGetUniformLocationARB(p->program, "Texture_Gloss");
1078 p->loc_Texture_Cube = qglGetUniformLocationARB(p->program, "Texture_Cube");
1079 p->loc_Texture_Attenuation = qglGetUniformLocationARB(p->program, "Texture_Attenuation");
1080 p->loc_Texture_FogMask = qglGetUniformLocationARB(p->program, "Texture_FogMask");
1081 p->loc_Texture_Pants = qglGetUniformLocationARB(p->program, "Texture_Pants");
1082 p->loc_Texture_Shirt = qglGetUniformLocationARB(p->program, "Texture_Shirt");
1083 p->loc_Texture_Lightmap = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
1084 p->loc_Texture_Deluxemap = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
1085 p->loc_Texture_Glow = qglGetUniformLocationARB(p->program, "Texture_Glow");
1086 p->loc_Texture_Refraction = qglGetUniformLocationARB(p->program, "Texture_Refraction");
1087 p->loc_Texture_Reflection = qglGetUniformLocationARB(p->program, "Texture_Reflection");
1088 p->loc_FogColor = qglGetUniformLocationARB(p->program, "FogColor");
1089 p->loc_LightPosition = qglGetUniformLocationARB(p->program, "LightPosition");
1090 p->loc_EyePosition = qglGetUniformLocationARB(p->program, "EyePosition");
1091 p->loc_LightColor = qglGetUniformLocationARB(p->program, "LightColor");
1092 p->loc_Color_Pants = qglGetUniformLocationARB(p->program, "Color_Pants");
1093 p->loc_Color_Shirt = qglGetUniformLocationARB(p->program, "Color_Shirt");
1094 p->loc_FogRangeRecip = qglGetUniformLocationARB(p->program, "FogRangeRecip");
1095 p->loc_FogStart = qglGetUniformLocationARB(p->program, "FogStart");
1096 p->loc_FogLength = qglGetUniformLocationARB(p->program, "FogLength");
1097 p->loc_AmbientScale = qglGetUniformLocationARB(p->program, "AmbientScale");
1098 p->loc_DiffuseScale = qglGetUniformLocationARB(p->program, "DiffuseScale");
1099 p->loc_SpecularPower = qglGetUniformLocationARB(p->program, "SpecularPower");
1100 p->loc_SpecularScale = qglGetUniformLocationARB(p->program, "SpecularScale");
1101 p->loc_GlowScale = qglGetUniformLocationARB(p->program, "GlowScale");
1102 p->loc_SceneBrightness = qglGetUniformLocationARB(p->program, "SceneBrightness");
1103 p->loc_OffsetMapping_Scale = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
1104 p->loc_AmbientColor = qglGetUniformLocationARB(p->program, "AmbientColor");
1105 p->loc_DiffuseColor = qglGetUniformLocationARB(p->program, "DiffuseColor");
1106 p->loc_SpecularColor = qglGetUniformLocationARB(p->program, "SpecularColor");
1107 p->loc_LightDir = qglGetUniformLocationARB(p->program, "LightDir");
1108 p->loc_ContrastBoostCoeff = qglGetUniformLocationARB(p->program, "ContrastBoostCoeff");
1109 p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
1110 p->loc_ScreenScaleRefractReflect = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
1111 p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
1112 p->loc_RefractColor = qglGetUniformLocationARB(p->program, "RefractColor");
1113 p->loc_ReflectColor = qglGetUniformLocationARB(p->program, "ReflectColor");
1114 p->loc_ReflectFactor = qglGetUniformLocationARB(p->program, "ReflectFactor");
1115 p->loc_ReflectOffset = qglGetUniformLocationARB(p->program, "ReflectOffset");
1116 // initialize the samplers to refer to the texture units we use
1117 if (p->loc_Texture_Normal >= 0) qglUniform1iARB(p->loc_Texture_Normal, 0);
1118 if (p->loc_Texture_Color >= 0) qglUniform1iARB(p->loc_Texture_Color, 1);
1119 if (p->loc_Texture_Gloss >= 0) qglUniform1iARB(p->loc_Texture_Gloss, 2);
1120 if (p->loc_Texture_Cube >= 0) qglUniform1iARB(p->loc_Texture_Cube, 3);
1121 if (p->loc_Texture_FogMask >= 0) qglUniform1iARB(p->loc_Texture_FogMask, 4);
1122 if (p->loc_Texture_Pants >= 0) qglUniform1iARB(p->loc_Texture_Pants, 5);
1123 if (p->loc_Texture_Shirt >= 0) qglUniform1iARB(p->loc_Texture_Shirt, 6);
1124 if (p->loc_Texture_Lightmap >= 0) qglUniform1iARB(p->loc_Texture_Lightmap, 7);
1125 if (p->loc_Texture_Deluxemap >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap, 8);
1126 if (p->loc_Texture_Glow >= 0) qglUniform1iARB(p->loc_Texture_Glow, 9);
1127 if (p->loc_Texture_Attenuation >= 0) qglUniform1iARB(p->loc_Texture_Attenuation, 10);
1128 if (p->loc_Texture_Refraction >= 0) qglUniform1iARB(p->loc_Texture_Refraction, 11);
1129 if (p->loc_Texture_Reflection >= 0) qglUniform1iARB(p->loc_Texture_Reflection, 12);
1131 qglUseProgramObjectARB(0);CHECKGLERROR
1132 if (developer.integer)
1133 Con_Printf("GLSL shader %s :%s compiled.\n", filename, permutationname);
1137 if (developer.integer)
1138 Con_Printf("GLSL shader %s :%s failed! source code line offset for above errors is %i.\n", permutationname, filename, -(vertstrings_count - 1));
1140 Con_Printf("GLSL shader %s :%s failed! some features may not work properly.\n", permutationname, filename);
1143 Mem_Free(shaderstring);
1146 void R_GLSL_Restart_f(void)
1149 for (i = 0;i < SHADERPERMUTATION_INDICES;i++)
1150 if (r_glsl_permutations[i].program)
1151 GL_Backend_FreeProgram(r_glsl_permutations[i].program);
1152 memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
1155 void R_GLSL_DumpShader_f(void)
1159 qfile_t *file = FS_Open("glsl/default.glsl", "w", false, false);
1162 Con_Printf("failed to write to glsl/default.glsl\n");
1166 FS_Print(file, "// The engine may define the following macros:\n");
1167 FS_Print(file, "// #define VERTEX_SHADER\n// #define GEOMETRY_SHADER\n// #define FRAGMENT_SHADER\n");
1168 for (i = 0;shadermodeinfo[i][0];i++)
1169 FS_Printf(file, "// %s", shadermodeinfo[i][0]);
1170 for (i = 0;shaderpermutationinfo[i][0];i++)
1171 FS_Printf(file, "// %s", shaderpermutationinfo[i][0]);
1172 FS_Print(file, "\n");
1173 FS_Print(file, builtinshaderstring);
1176 Con_Printf("glsl/default.glsl written\n");
1179 extern rtexture_t *r_shadow_attenuationgradienttexture;
1180 extern rtexture_t *r_shadow_attenuation2dtexture;
1181 extern rtexture_t *r_shadow_attenuation3dtexture;
1182 int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, float ambientscale, float diffusescale, float specularscale, rsurfacepass_t rsurfacepass)
1184 // select a permutation of the lighting shader appropriate to this
1185 // combination of texture, entity, light source, and fogging, only use the
1186 // minimum features necessary to avoid wasting rendering time in the
1187 // fragment shader on features that are not being used
1188 const char *shaderfilename = NULL;
1189 unsigned int permutation = 0;
1190 unsigned int shadertype = 0;
1191 shadermode_t mode = 0;
1192 r_glsl_permutation = NULL;
1193 shaderfilename = "glsl/default.glsl";
1194 shadertype = SHADERTYPE_USES_VERTEXSHADER | SHADERTYPE_USES_FRAGMENTSHADER;
1195 // TODO: implement geometry-shader based shadow volumes someday
1196 if (r_glsl_offsetmapping.integer)
1198 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
1199 if (r_glsl_offsetmapping_reliefmapping.integer)
1200 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
1202 if (rsurfacepass == RSURFPASS_BACKGROUND)
1204 // distorted background
1205 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
1206 mode = SHADERMODE_WATER;
1208 mode = SHADERMODE_REFRACTION;
1210 else if (rsurfacepass == RSURFPASS_RTLIGHT)
1213 mode = SHADERMODE_LIGHTSOURCE;
1214 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1215 permutation |= SHADERPERMUTATION_CUBEFILTER;
1216 if (diffusescale > 0)
1217 permutation |= SHADERPERMUTATION_DIFFUSE;
1218 if (specularscale > 0)
1219 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1220 if (r_refdef.fogenabled)
1221 permutation |= SHADERPERMUTATION_FOG;
1222 if (rsurface.texture->colormapping)
1223 permutation |= SHADERPERMUTATION_COLORMAPPING;
1224 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1225 permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1226 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1227 permutation |= SHADERPERMUTATION_REFLECTION;
1229 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
1231 // unshaded geometry (fullbright or ambient model lighting)
1232 mode = SHADERMODE_LIGHTMAP;
1233 if (rsurface.texture->currentskinframe->glow)
1234 permutation |= SHADERPERMUTATION_GLOW;
1235 if (r_refdef.fogenabled)
1236 permutation |= SHADERPERMUTATION_FOG;
1237 if (rsurface.texture->colormapping)
1238 permutation |= SHADERPERMUTATION_COLORMAPPING;
1239 if (r_glsl_offsetmapping.integer)
1241 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
1242 if (r_glsl_offsetmapping_reliefmapping.integer)
1243 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
1245 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1246 permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1247 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1248 permutation |= SHADERPERMUTATION_REFLECTION;
1250 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT_DIRECTIONAL)
1252 // directional model lighting
1253 mode = SHADERMODE_LIGHTDIRECTION;
1254 if (rsurface.texture->currentskinframe->glow)
1255 permutation |= SHADERPERMUTATION_GLOW;
1256 permutation |= SHADERPERMUTATION_DIFFUSE;
1257 if (specularscale > 0)
1258 permutation |= SHADERPERMUTATION_SPECULAR;
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;
1268 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
1270 // ambient model lighting
1271 mode = SHADERMODE_LIGHTDIRECTION;
1272 if (rsurface.texture->currentskinframe->glow)
1273 permutation |= SHADERPERMUTATION_GLOW;
1274 if (r_refdef.fogenabled)
1275 permutation |= SHADERPERMUTATION_FOG;
1276 if (rsurface.texture->colormapping)
1277 permutation |= SHADERPERMUTATION_COLORMAPPING;
1278 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1279 permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1280 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1281 permutation |= SHADERPERMUTATION_REFLECTION;
1286 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping)
1288 // deluxemapping (light direction texture)
1289 if (rsurface.uselightmaptexture && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping && r_refdef.worldmodel->brushq3.deluxemapping_modelspace)
1290 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
1292 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1293 if (specularscale > 0)
1294 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1296 else if (r_glsl_deluxemapping.integer >= 2)
1298 // fake deluxemapping (uniform light direction in tangentspace)
1299 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1300 if (specularscale > 0)
1301 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1305 // ordinary lightmapping
1306 mode = SHADERMODE_LIGHTMAP;
1308 if (rsurface.texture->currentskinframe->glow)
1309 permutation |= SHADERPERMUTATION_GLOW;
1310 if (r_refdef.fogenabled)
1311 permutation |= SHADERPERMUTATION_FOG;
1312 if (rsurface.texture->colormapping)
1313 permutation |= SHADERPERMUTATION_COLORMAPPING;
1314 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1315 permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1316 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1317 permutation |= SHADERPERMUTATION_REFLECTION;
1319 permutation |= mode * SHADERPERMUTATION_MODEBASE;
1320 if (!r_glsl_permutations[permutation].program)
1322 if (!r_glsl_permutations[permutation].compiled)
1323 R_GLSL_CompilePermutation(shaderfilename, permutation, shadertype);
1324 if (!r_glsl_permutations[permutation].program)
1326 // remove features until we find a valid permutation
1328 for (i = (SHADERPERMUTATION_MODEBASE >> 1);;i>>=1)
1332 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");
1333 Cvar_SetValueQuick(&r_glsl, 0);
1334 return 0; // no bit left to clear
1336 // reduce i more quickly whenever it would not remove any bits
1337 if (!(permutation & i))
1340 if (!r_glsl_permutations[permutation].compiled)
1341 R_GLSL_CompilePermutation(shaderfilename, permutation, shadertype);
1342 if (r_glsl_permutations[permutation].program)
1347 r_glsl_permutation = r_glsl_permutations + permutation;
1349 qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
1350 if (mode == SHADERMODE_LIGHTSOURCE)
1352 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
1353 if (permutation & SHADERPERMUTATION_DIFFUSE)
1355 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
1356 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, ambientscale);
1357 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, diffusescale);
1358 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, specularscale);
1362 // ambient only is simpler
1363 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0] * ambientscale, lightcolorbase[1] * ambientscale, lightcolorbase[2] * ambientscale);
1364 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, 1);
1365 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, 0);
1366 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, 0);
1369 else if (mode == SHADERMODE_LIGHTDIRECTION)
1371 if (r_glsl_permutation->loc_AmbientColor >= 0)
1372 qglUniform3fARB(r_glsl_permutation->loc_AmbientColor, rsurface.modellight_ambient[0] * ambientscale * r_refdef.lightmapintensity, rsurface.modellight_ambient[1] * ambientscale * r_refdef.lightmapintensity, rsurface.modellight_ambient[2] * ambientscale * r_refdef.lightmapintensity);
1373 if (r_glsl_permutation->loc_DiffuseColor >= 0)
1374 qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor, rsurface.modellight_diffuse[0] * diffusescale * r_refdef.lightmapintensity, rsurface.modellight_diffuse[1] * diffusescale * r_refdef.lightmapintensity, rsurface.modellight_diffuse[2] * diffusescale * r_refdef.lightmapintensity);
1375 if (r_glsl_permutation->loc_SpecularColor >= 0)
1376 qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface.modellight_diffuse[0] * specularscale * r_refdef.lightmapintensity, rsurface.modellight_diffuse[1] * specularscale * r_refdef.lightmapintensity, rsurface.modellight_diffuse[2] * specularscale * r_refdef.lightmapintensity);
1377 if (r_glsl_permutation->loc_LightDir >= 0)
1378 qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
1382 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, r_ambient.value * 2.0f / 128.0f);
1383 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, r_refdef.lightmapintensity * 2.0f);
1384 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, r_refdef.lightmapintensity * specularscale * 2.0f);
1386 if (r_glsl_permutation->loc_GlowScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_GlowScale, r_hdr_glowintensity.value);
1387 if (r_glsl_permutation->loc_ContrastBoostCoeff >= 0)
1389 // The formula used is actually:
1390 // color.rgb *= SceneBrightness;
1391 // color.rgb *= ContrastBoost / ((ContrastBoost - 1) * color.rgb + 1);
1392 // I simplify that to
1393 // color.rgb *= [[SceneBrightness * ContrastBoost]];
1394 // color.rgb /= [[(ContrastBoost - 1) / ContrastBoost]] * color.rgb + 1;
1396 // color.rgb = [[SceneBrightness * ContrastBoost]] * color.rgb / ([[(ContrastBoost - 1) * SceneBrightness]] * color.rgb + 1);
1397 // and do [[calculations]] here in the engine
1398 qglUniform1fARB(r_glsl_permutation->loc_ContrastBoostCoeff, (r_glsl_contrastboost.value - 1) * r_view.colorscale);
1399 if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale * r_glsl_contrastboost.value);
1402 if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale);
1403 if (r_glsl_permutation->loc_FogColor >= 0)
1405 // additive passes are only darkened by fog, not tinted
1406 if (rsurface.rtlight || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD))
1407 qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
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_FogStart >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogStart, r_refdef.fog_start);
1428 if (r_glsl_permutation->loc_FogLength >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogLength, r_refdef.fog_end - r_refdef.fog_start);
1429 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower);
1430 if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
1431 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);
1432 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]);
1433 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]);
1434 if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_RefractColor, 1, rsurface.texture->refractcolor4f);
1435 if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_ReflectColor, 1, rsurface.texture->reflectcolor4f);
1436 if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
1437 if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectOffset, rsurface.texture->reflectmin);
1442 #define SKINFRAME_HASH 1024
1446 int loadsequence; // incremented each level change
1447 memexpandablearray_t array;
1448 skinframe_t *hash[SKINFRAME_HASH];
1452 void R_SkinFrame_PrepareForPurge(void)
1454 r_skinframe.loadsequence++;
1455 // wrap it without hitting zero
1456 if (r_skinframe.loadsequence >= 200)
1457 r_skinframe.loadsequence = 1;
1460 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
1464 // mark the skinframe as used for the purging code
1465 skinframe->loadsequence = r_skinframe.loadsequence;
1468 void R_SkinFrame_Purge(void)
1472 for (i = 0;i < SKINFRAME_HASH;i++)
1474 for (s = r_skinframe.hash[i];s;s = s->next)
1476 if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
1478 if (s->merged == s->base)
1480 // FIXME: maybe pass a pointer to the pointer to R_PurgeTexture and reset it to NULL inside? [11/29/2007 Black]
1481 R_PurgeTexture(s->stain );s->stain = NULL;
1482 R_PurgeTexture(s->merged);s->merged = NULL;
1483 R_PurgeTexture(s->base );s->base = NULL;
1484 R_PurgeTexture(s->pants );s->pants = NULL;
1485 R_PurgeTexture(s->shirt );s->shirt = NULL;
1486 R_PurgeTexture(s->nmap );s->nmap = NULL;
1487 R_PurgeTexture(s->gloss );s->gloss = NULL;
1488 R_PurgeTexture(s->glow );s->glow = NULL;
1489 R_PurgeTexture(s->fog );s->fog = NULL;
1490 s->loadsequence = 0;
1496 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
1498 char basename[MAX_QPATH];
1500 Image_StripImageExtension(name, basename, sizeof(basename));
1502 if( last == NULL ) {
1504 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
1505 item = r_skinframe.hash[hashindex];
1510 // linearly search through the hash bucket
1511 for( ; item ; item = item->next ) {
1512 if( !strcmp( item->basename, basename ) ) {
1519 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qboolean add)
1523 char basename[MAX_QPATH];
1525 Image_StripImageExtension(name, basename, sizeof(basename));
1527 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
1528 for (item = r_skinframe.hash[hashindex];item;item = item->next)
1529 if (!strcmp(item->basename, basename) && item->textureflags == textureflags && item->comparewidth == comparewidth && item->compareheight == compareheight && item->comparecrc == comparecrc)
1533 rtexture_t *dyntexture;
1534 // check whether its a dynamic texture
1535 dyntexture = CL_GetDynTexture( basename );
1536 if (!add && !dyntexture)
1538 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
1539 memset(item, 0, sizeof(*item));
1540 strlcpy(item->basename, basename, sizeof(item->basename));
1541 item->base = dyntexture; // either NULL or dyntexture handle
1542 item->textureflags = textureflags;
1543 item->comparewidth = comparewidth;
1544 item->compareheight = compareheight;
1545 item->comparecrc = comparecrc;
1546 item->next = r_skinframe.hash[hashindex];
1547 r_skinframe.hash[hashindex] = item;
1549 else if( item->base == NULL )
1551 rtexture_t *dyntexture;
1552 // check whether its a dynamic texture
1553 // 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]
1554 dyntexture = CL_GetDynTexture( basename );
1555 item->base = dyntexture; // either NULL or dyntexture handle
1558 R_SkinFrame_MarkUsed(item);
1562 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboolean complain)
1564 // FIXME: it should be possible to disable loading various layers using
1565 // cvars, to prevent wasted loading time and memory usage if the user does
1567 qboolean loadnormalmap = true;
1568 qboolean loadgloss = true;
1569 qboolean loadpantsandshirt = true;
1570 qboolean loadglow = true;
1572 unsigned char *pixels;
1573 unsigned char *bumppixels;
1574 unsigned char *basepixels = NULL;
1575 int basepixels_width;
1576 int basepixels_height;
1577 skinframe_t *skinframe;
1579 if (cls.state == ca_dedicated)
1582 // return an existing skinframe if already loaded
1583 // if loading of the first image fails, don't make a new skinframe as it
1584 // would cause all future lookups of this to be missing
1585 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
1586 if (skinframe && skinframe->base)
1589 basepixels = loadimagepixelsbgra(name, complain, true);
1590 if (basepixels == NULL)
1593 // we've got some pixels to store, so really allocate this new texture now
1595 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
1596 skinframe->stain = NULL;
1597 skinframe->merged = NULL;
1598 skinframe->base = r_texture_notexture;
1599 skinframe->pants = NULL;
1600 skinframe->shirt = NULL;
1601 skinframe->nmap = r_texture_blanknormalmap;
1602 skinframe->gloss = NULL;
1603 skinframe->glow = NULL;
1604 skinframe->fog = NULL;
1606 basepixels_width = image_width;
1607 basepixels_height = image_height;
1608 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);
1610 if (textureflags & TEXF_ALPHA)
1612 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
1613 if (basepixels[j] < 255)
1615 if (j < basepixels_width * basepixels_height * 4)
1617 // has transparent pixels
1618 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
1619 for (j = 0;j < image_width * image_height * 4;j += 4)
1624 pixels[j+3] = basepixels[j+3];
1626 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);
1631 // _norm is the name used by tenebrae and has been adopted as standard
1634 if ((pixels = loadimagepixelsbgra(va("%s_norm", skinframe->basename), false, false)) != NULL)
1636 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);
1640 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va("%s_bump", skinframe->basename), false, false)) != NULL)
1642 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
1643 Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
1644 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);
1646 Mem_Free(bumppixels);
1648 else if (r_shadow_bumpscale_basetexture.value > 0)
1650 pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
1651 Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
1652 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);
1656 // _luma is supported for tenebrae compatibility
1657 // (I think it's a very stupid name, but oh well)
1658 // _glow is the preferred name
1659 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;}
1660 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;}
1661 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;}
1662 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;}
1665 Mem_Free(basepixels);
1670 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)
1675 for (i = 0;i < width*height;i++)
1676 if (((unsigned char *)&palette[in[i]])[3] > 0)
1678 if (i == width*height)
1681 return R_LoadTexture2D (r_main_texturepool, name, width, height, in, TEXTYPE_PALETTE, textureflags, palette);
1684 // this is only used by .spr32 sprites, HL .spr files, HL .bsp files
1685 skinframe_t *R_SkinFrame_LoadInternalBGRA(const char *name, int textureflags, const unsigned char *skindata, int width, int height)
1688 unsigned char *temp1, *temp2;
1689 skinframe_t *skinframe;
1691 if (cls.state == ca_dedicated)
1694 // if already loaded just return it, otherwise make a new skinframe
1695 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height*4) : 0, true);
1696 if (skinframe && skinframe->base)
1699 skinframe->stain = NULL;
1700 skinframe->merged = NULL;
1701 skinframe->base = r_texture_notexture;
1702 skinframe->pants = NULL;
1703 skinframe->shirt = NULL;
1704 skinframe->nmap = r_texture_blanknormalmap;
1705 skinframe->gloss = NULL;
1706 skinframe->glow = NULL;
1707 skinframe->fog = NULL;
1709 // if no data was provided, then clearly the caller wanted to get a blank skinframe
1713 if (r_shadow_bumpscale_basetexture.value > 0)
1715 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
1716 temp2 = temp1 + width * height * 4;
1717 Image_HeightmapToNormalmap_BGRA(skindata, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
1718 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
1721 skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_BGRA, skinframe->textureflags, NULL);
1722 if (textureflags & TEXF_ALPHA)
1724 for (i = 3;i < width * height * 4;i += 4)
1725 if (skindata[i] < 255)
1727 if (i < width * height * 4)
1729 unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
1730 memcpy(fogpixels, skindata, width * height * 4);
1731 for (i = 0;i < width * height * 4;i += 4)
1732 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
1733 skinframe->fog = R_LoadTexture2D(r_main_texturepool, va("%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, skinframe->textureflags, NULL);
1734 Mem_Free(fogpixels);
1741 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
1744 unsigned char *temp1, *temp2;
1745 skinframe_t *skinframe;
1747 if (cls.state == ca_dedicated)
1750 // if already loaded just return it, otherwise make a new skinframe
1751 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
1752 if (skinframe && skinframe->base)
1755 skinframe->stain = NULL;
1756 skinframe->merged = NULL;
1757 skinframe->base = r_texture_notexture;
1758 skinframe->pants = NULL;
1759 skinframe->shirt = NULL;
1760 skinframe->nmap = r_texture_blanknormalmap;
1761 skinframe->gloss = NULL;
1762 skinframe->glow = NULL;
1763 skinframe->fog = NULL;
1765 // if no data was provided, then clearly the caller wanted to get a blank skinframe
1769 if (r_shadow_bumpscale_basetexture.value > 0)
1771 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
1772 temp2 = temp1 + width * height * 4;
1773 // use either a custom palette or the quake palette
1774 Image_Copy8bitBGRA(skindata, temp1, width * height, palette_bgra_complete);
1775 Image_HeightmapToNormalmap_BGRA(temp1, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
1776 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
1779 // use either a custom palette, or the quake palette
1780 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
1781 if (loadglowtexture)
1782 skinframe->glow = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_glow", skinframe->basename), palette_bgra_onlyfullbrights, skinframe->textureflags, false); // glow
1783 if (loadpantsandshirt)
1785 skinframe->pants = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_pants", skinframe->basename), palette_bgra_pantsaswhite, skinframe->textureflags, false); // pants
1786 skinframe->shirt = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_shirt", skinframe->basename), palette_bgra_shirtaswhite, skinframe->textureflags, false); // shirt
1788 if (skinframe->pants || skinframe->shirt)
1789 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
1790 if (textureflags & TEXF_ALPHA)
1792 for (i = 0;i < width * height;i++)
1793 if (((unsigned char *)palette_bgra_alpha)[skindata[i]*4+3] < 255)
1795 if (i < width * height)
1796 skinframe->fog = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_fog", skinframe->basename), palette_bgra_alpha, skinframe->textureflags, true); // fog mask
1802 skinframe_t *R_SkinFrame_LoadMissing(void)
1804 skinframe_t *skinframe;
1806 if (cls.state == ca_dedicated)
1809 skinframe = R_SkinFrame_Find("missing", TEXF_PRECACHE, 0, 0, 0, true);
1810 skinframe->stain = NULL;
1811 skinframe->merged = NULL;
1812 skinframe->base = r_texture_notexture;
1813 skinframe->pants = NULL;
1814 skinframe->shirt = NULL;
1815 skinframe->nmap = r_texture_blanknormalmap;
1816 skinframe->gloss = NULL;
1817 skinframe->glow = NULL;
1818 skinframe->fog = NULL;
1823 void gl_main_start(void)
1828 r = -16.0 / (1.0 * FOGMASKTABLEWIDTH * FOGMASKTABLEWIDTH);
1829 for (x = 0;x < FOGMASKTABLEWIDTH;x++)
1831 alpha = exp(r * ((double)x*(double)x));
1832 if (x == FOGMASKTABLEWIDTH - 1)
1834 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
1837 memset(r_qwskincache, 0, sizeof(r_qwskincache));
1838 memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
1840 // set up r_skinframe loading system for textures
1841 memset(&r_skinframe, 0, sizeof(r_skinframe));
1842 r_skinframe.loadsequence = 1;
1843 Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
1845 r_main_texturepool = R_AllocTexturePool();
1846 R_BuildBlankTextures();
1848 if (gl_texturecubemap)
1851 R_BuildNormalizationCube();
1853 R_BuildFogTexture();
1854 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
1855 memset(&r_waterstate, 0, sizeof(r_waterstate));
1856 memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
1857 memset(&r_svbsp, 0, sizeof (r_svbsp));
1860 void gl_main_shutdown(void)
1862 memset(r_qwskincache, 0, sizeof(r_qwskincache));
1863 memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
1865 // clear out the r_skinframe state
1866 Mem_ExpandableArray_FreeArray(&r_skinframe.array);
1867 memset(&r_skinframe, 0, sizeof(r_skinframe));
1870 Mem_Free(r_svbsp.nodes);
1871 memset(&r_svbsp, 0, sizeof (r_svbsp));
1872 R_FreeTexturePool(&r_main_texturepool);
1873 r_texture_blanknormalmap = NULL;
1874 r_texture_white = NULL;
1875 r_texture_grey128 = NULL;
1876 r_texture_black = NULL;
1877 r_texture_whitecube = NULL;
1878 r_texture_normalizationcube = NULL;
1879 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
1880 memset(&r_waterstate, 0, sizeof(r_waterstate));
1884 extern void CL_ParseEntityLump(char *entitystring);
1885 void gl_main_newmap(void)
1887 // FIXME: move this code to client
1889 char *entities, entname[MAX_QPATH];
1892 strlcpy(entname, cl.worldmodel->name, sizeof(entname));
1893 l = (int)strlen(entname) - 4;
1894 if (l >= 0 && !strcmp(entname + l, ".bsp"))
1896 memcpy(entname + l, ".ent", 5);
1897 if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
1899 CL_ParseEntityLump(entities);
1904 if (cl.worldmodel->brush.entities)
1905 CL_ParseEntityLump(cl.worldmodel->brush.entities);
1909 void GL_Main_Init(void)
1911 r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
1913 Cmd_AddCommand("r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
1914 Cmd_AddCommand("r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
1915 // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
1916 if (gamemode == GAME_NEHAHRA)
1918 Cvar_RegisterVariable (&gl_fogenable);
1919 Cvar_RegisterVariable (&gl_fogdensity);
1920 Cvar_RegisterVariable (&gl_fogred);
1921 Cvar_RegisterVariable (&gl_foggreen);
1922 Cvar_RegisterVariable (&gl_fogblue);
1923 Cvar_RegisterVariable (&gl_fogstart);
1924 Cvar_RegisterVariable (&gl_fogend);
1926 Cvar_RegisterVariable(&r_depthfirst);
1927 Cvar_RegisterVariable(&r_nearclip);
1928 Cvar_RegisterVariable(&r_showbboxes);
1929 Cvar_RegisterVariable(&r_showsurfaces);
1930 Cvar_RegisterVariable(&r_showtris);
1931 Cvar_RegisterVariable(&r_shownormals);
1932 Cvar_RegisterVariable(&r_showlighting);
1933 Cvar_RegisterVariable(&r_showshadowvolumes);
1934 Cvar_RegisterVariable(&r_showcollisionbrushes);
1935 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
1936 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
1937 Cvar_RegisterVariable(&r_showdisabledepthtest);
1938 Cvar_RegisterVariable(&r_drawportals);
1939 Cvar_RegisterVariable(&r_drawentities);
1940 Cvar_RegisterVariable(&r_cullentities_trace);
1941 Cvar_RegisterVariable(&r_cullentities_trace_samples);
1942 Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
1943 Cvar_RegisterVariable(&r_cullentities_trace_delay);
1944 Cvar_RegisterVariable(&r_drawviewmodel);
1945 Cvar_RegisterVariable(&r_speeds);
1946 Cvar_RegisterVariable(&r_fullbrights);
1947 Cvar_RegisterVariable(&r_wateralpha);
1948 Cvar_RegisterVariable(&r_dynamic);
1949 Cvar_RegisterVariable(&r_fullbright);
1950 Cvar_RegisterVariable(&r_shadows);
1951 Cvar_RegisterVariable(&r_shadows_throwdistance);
1952 Cvar_RegisterVariable(&r_q1bsp_skymasking);
1953 Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
1954 Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
1955 Cvar_RegisterVariable(&r_textureunits);
1956 Cvar_RegisterVariable(&r_glsl);
1957 Cvar_RegisterVariable(&r_glsl_offsetmapping);
1958 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
1959 Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
1960 Cvar_RegisterVariable(&r_glsl_deluxemapping);
1961 Cvar_RegisterVariable(&r_water);
1962 Cvar_RegisterVariable(&r_water_resolutionmultiplier);
1963 Cvar_RegisterVariable(&r_water_clippingplanebias);
1964 Cvar_RegisterVariable(&r_water_refractdistort);
1965 Cvar_RegisterVariable(&r_water_reflectdistort);
1966 Cvar_RegisterVariable(&r_lerpsprites);
1967 Cvar_RegisterVariable(&r_lerpmodels);
1968 Cvar_RegisterVariable(&r_lerplightstyles);
1969 Cvar_RegisterVariable(&r_waterscroll);
1970 Cvar_RegisterVariable(&r_bloom);
1971 Cvar_RegisterVariable(&r_bloom_colorscale);
1972 Cvar_RegisterVariable(&r_bloom_brighten);
1973 Cvar_RegisterVariable(&r_bloom_blur);
1974 Cvar_RegisterVariable(&r_bloom_resolution);
1975 Cvar_RegisterVariable(&r_bloom_colorexponent);
1976 Cvar_RegisterVariable(&r_bloom_colorsubtract);
1977 Cvar_RegisterVariable(&r_hdr);
1978 Cvar_RegisterVariable(&r_hdr_scenebrightness);
1979 Cvar_RegisterVariable(&r_glsl_contrastboost);
1980 Cvar_RegisterVariable(&r_hdr_glowintensity);
1981 Cvar_RegisterVariable(&r_hdr_range);
1982 Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
1983 Cvar_RegisterVariable(&developer_texturelogging);
1984 Cvar_RegisterVariable(&gl_lightmaps);
1985 Cvar_RegisterVariable(&r_test);
1986 Cvar_RegisterVariable(&r_batchmode);
1987 if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
1988 Cvar_SetValue("r_fullbrights", 0);
1989 R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
1991 Cvar_RegisterVariable(&r_track_sprites);
1992 Cvar_RegisterVariable(&r_track_sprites_flags);
1993 Cvar_RegisterVariable(&r_track_sprites_scalew);
1994 Cvar_RegisterVariable(&r_track_sprites_scaleh);
1997 extern void R_Textures_Init(void);
1998 extern void GL_Draw_Init(void);
1999 extern void GL_Main_Init(void);
2000 extern void R_Shadow_Init(void);
2001 extern void R_Sky_Init(void);
2002 extern void GL_Surf_Init(void);
2003 extern void R_Particles_Init(void);
2004 extern void R_Explosion_Init(void);
2005 extern void gl_backend_init(void);
2006 extern void Sbar_Init(void);
2007 extern void R_LightningBeams_Init(void);
2008 extern void Mod_RenderInit(void);
2010 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_View_UpdateEntityVisible (void)
2147 entity_render_t *ent;
2149 if (!r_drawentities.integer)
2152 renderimask = r_refdef.envmap ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL) : ((chase_active.integer || r_waterstate.renderingscene) ? RENDER_VIEWMODEL : RENDER_EXTERIORMODEL);
2153 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs)
2155 // worldmodel can check visibility
2156 for (i = 0;i < r_refdef.numentities;i++)
2158 ent = r_refdef.entities[i];
2159 r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs)) && ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.worldmodel, r_viewcache.world_leafvisible, ent->mins, ent->maxs));
2162 if(r_cullentities_trace.integer)
2164 for (i = 0;i < r_refdef.numentities;i++)
2166 ent = r_refdef.entities[i];
2167 if(r_viewcache.entityvisible[i] && !(ent->effects & EF_NODEPTHTEST) && !(ent->flags & RENDER_VIEWMODEL) && !(ent->model && (ent->model->name[0] == '*')))
2169 if(Mod_CanSeeBox_Trace(r_cullentities_trace_samples.integer, r_cullentities_trace_enlarge.value, r_refdef.worldmodel, r_view.origin, ent->mins, ent->maxs))
2170 ent->last_trace_visibility = realtime;
2171 if(ent->last_trace_visibility < realtime - r_cullentities_trace_delay.value)
2172 r_viewcache.entityvisible[i] = 0;
2179 // no worldmodel or it can't check visibility
2180 for (i = 0;i < r_refdef.numentities;i++)
2182 ent = r_refdef.entities[i];
2183 r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs));
2188 // only used if skyrendermasked, and normally returns false
2189 int R_DrawBrushModelsSky (void)
2192 entity_render_t *ent;
2194 if (!r_drawentities.integer)
2198 for (i = 0;i < r_refdef.numentities;i++)
2200 if (!r_viewcache.entityvisible[i])
2202 ent = r_refdef.entities[i];
2203 if (!ent->model || !ent->model->DrawSky)
2205 ent->model->DrawSky(ent);
2211 static void R_DrawNoModel(entity_render_t *ent);
2212 static void R_DrawModels(void)
2215 entity_render_t *ent;
2217 if (!r_drawentities.integer)
2220 for (i = 0;i < r_refdef.numentities;i++)
2222 if (!r_viewcache.entityvisible[i])
2224 ent = r_refdef.entities[i];
2225 r_refdef.stats.entities++;
2226 if (ent->model && ent->model->Draw != NULL)
2227 ent->model->Draw(ent);
2233 static void R_DrawModelsDepth(void)
2236 entity_render_t *ent;
2238 if (!r_drawentities.integer)
2241 for (i = 0;i < r_refdef.numentities;i++)
2243 if (!r_viewcache.entityvisible[i])
2245 ent = r_refdef.entities[i];
2246 if (ent->model && ent->model->DrawDepth != NULL)
2247 ent->model->DrawDepth(ent);
2251 static void R_DrawModelsDebug(void)
2254 entity_render_t *ent;
2256 if (!r_drawentities.integer)
2259 for (i = 0;i < r_refdef.numentities;i++)
2261 if (!r_viewcache.entityvisible[i])
2263 ent = r_refdef.entities[i];
2264 if (ent->model && ent->model->DrawDebug != NULL)
2265 ent->model->DrawDebug(ent);
2269 static void R_DrawModelsAddWaterPlanes(void)
2272 entity_render_t *ent;
2274 if (!r_drawentities.integer)
2277 for (i = 0;i < r_refdef.numentities;i++)
2279 if (!r_viewcache.entityvisible[i])
2281 ent = r_refdef.entities[i];
2282 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
2283 ent->model->DrawAddWaterPlanes(ent);
2287 static void R_View_SetFrustum(void)
2290 double slopex, slopey;
2292 // break apart the view matrix into vectors for various purposes
2293 Matrix4x4_ToVectors(&r_view.matrix, r_view.forward, r_view.left, r_view.up, r_view.origin);
2294 VectorNegate(r_view.left, r_view.right);
2297 r_view.frustum[0].normal[0] = 0 - 1.0 / r_view.frustum_x;
2298 r_view.frustum[0].normal[1] = 0 - 0;
2299 r_view.frustum[0].normal[2] = -1 - 0;
2300 r_view.frustum[1].normal[0] = 0 + 1.0 / r_view.frustum_x;
2301 r_view.frustum[1].normal[1] = 0 + 0;
2302 r_view.frustum[1].normal[2] = -1 + 0;
2303 r_view.frustum[2].normal[0] = 0 - 0;
2304 r_view.frustum[2].normal[1] = 0 - 1.0 / r_view.frustum_y;
2305 r_view.frustum[2].normal[2] = -1 - 0;
2306 r_view.frustum[3].normal[0] = 0 + 0;
2307 r_view.frustum[3].normal[1] = 0 + 1.0 / r_view.frustum_y;
2308 r_view.frustum[3].normal[2] = -1 + 0;
2312 zNear = r_refdef.nearclip;
2313 nudge = 1.0 - 1.0 / (1<<23);
2314 r_view.frustum[4].normal[0] = 0 - 0;
2315 r_view.frustum[4].normal[1] = 0 - 0;
2316 r_view.frustum[4].normal[2] = -1 - -nudge;
2317 r_view.frustum[4].dist = 0 - -2 * zNear * nudge;
2318 r_view.frustum[5].normal[0] = 0 + 0;
2319 r_view.frustum[5].normal[1] = 0 + 0;
2320 r_view.frustum[5].normal[2] = -1 + -nudge;
2321 r_view.frustum[5].dist = 0 + -2 * zNear * nudge;
2327 r_view.frustum[0].normal[0] = m[3] - m[0];
2328 r_view.frustum[0].normal[1] = m[7] - m[4];
2329 r_view.frustum[0].normal[2] = m[11] - m[8];
2330 r_view.frustum[0].dist = m[15] - m[12];
2332 r_view.frustum[1].normal[0] = m[3] + m[0];
2333 r_view.frustum[1].normal[1] = m[7] + m[4];
2334 r_view.frustum[1].normal[2] = m[11] + m[8];
2335 r_view.frustum[1].dist = m[15] + m[12];
2337 r_view.frustum[2].normal[0] = m[3] - m[1];
2338 r_view.frustum[2].normal[1] = m[7] - m[5];
2339 r_view.frustum[2].normal[2] = m[11] - m[9];
2340 r_view.frustum[2].dist = m[15] - m[13];
2342 r_view.frustum[3].normal[0] = m[3] + m[1];
2343 r_view.frustum[3].normal[1] = m[7] + m[5];
2344 r_view.frustum[3].normal[2] = m[11] + m[9];
2345 r_view.frustum[3].dist = m[15] + m[13];
2347 r_view.frustum[4].normal[0] = m[3] - m[2];
2348 r_view.frustum[4].normal[1] = m[7] - m[6];
2349 r_view.frustum[4].normal[2] = m[11] - m[10];
2350 r_view.frustum[4].dist = m[15] - m[14];
2352 r_view.frustum[5].normal[0] = m[3] + m[2];
2353 r_view.frustum[5].normal[1] = m[7] + m[6];
2354 r_view.frustum[5].normal[2] = m[11] + m[10];
2355 r_view.frustum[5].dist = m[15] + m[14];
2358 if (r_view.useperspective)
2360 slopex = 1.0 / r_view.frustum_x;
2361 slopey = 1.0 / r_view.frustum_y;
2362 VectorMA(r_view.forward, -slopex, r_view.left, r_view.frustum[0].normal);
2363 VectorMA(r_view.forward, slopex, r_view.left, r_view.frustum[1].normal);
2364 VectorMA(r_view.forward, -slopey, r_view.up , r_view.frustum[2].normal);
2365 VectorMA(r_view.forward, slopey, r_view.up , r_view.frustum[3].normal);
2366 VectorCopy(r_view.forward, r_view.frustum[4].normal);
2368 // Leaving those out was a mistake, those were in the old code, and they
2369 // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
2370 // I couldn't reproduce it after adding those normalizations. --blub
2371 VectorNormalize(r_view.frustum[0].normal);
2372 VectorNormalize(r_view.frustum[1].normal);
2373 VectorNormalize(r_view.frustum[2].normal);
2374 VectorNormalize(r_view.frustum[3].normal);
2376 // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
2377 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[0]);
2378 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, 1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[1]);
2379 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left, 1024 * slopey, r_view.up, r_view.frustumcorner[2]);
2380 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, 1024 * slopex, r_view.left, 1024 * slopey, r_view.up, r_view.frustumcorner[3]);
2382 r_view.frustum[0].dist = DotProduct (r_view.origin, r_view.frustum[0].normal);
2383 r_view.frustum[1].dist = DotProduct (r_view.origin, r_view.frustum[1].normal);
2384 r_view.frustum[2].dist = DotProduct (r_view.origin, r_view.frustum[2].normal);
2385 r_view.frustum[3].dist = DotProduct (r_view.origin, r_view.frustum[3].normal);
2386 r_view.frustum[4].dist = DotProduct (r_view.origin, r_view.frustum[4].normal) + r_refdef.nearclip;
2390 VectorScale(r_view.left, -r_view.ortho_x, r_view.frustum[0].normal);
2391 VectorScale(r_view.left, r_view.ortho_x, r_view.frustum[1].normal);
2392 VectorScale(r_view.up, -r_view.ortho_y, r_view.frustum[2].normal);
2393 VectorScale(r_view.up, r_view.ortho_y, r_view.frustum[3].normal);
2394 VectorCopy(r_view.forward, r_view.frustum[4].normal);
2395 r_view.frustum[0].dist = DotProduct (r_view.origin, r_view.frustum[0].normal) + r_view.ortho_x;
2396 r_view.frustum[1].dist = DotProduct (r_view.origin, r_view.frustum[1].normal) + r_view.ortho_x;
2397 r_view.frustum[2].dist = DotProduct (r_view.origin, r_view.frustum[2].normal) + r_view.ortho_y;
2398 r_view.frustum[3].dist = DotProduct (r_view.origin, r_view.frustum[3].normal) + r_view.ortho_y;
2399 r_view.frustum[4].dist = DotProduct (r_view.origin, r_view.frustum[4].normal) + r_refdef.nearclip;
2401 r_view.numfrustumplanes = 5;
2403 if (r_view.useclipplane)
2405 r_view.numfrustumplanes = 6;
2406 r_view.frustum[5] = r_view.clipplane;
2409 for (i = 0;i < r_view.numfrustumplanes;i++)
2410 PlaneClassify(r_view.frustum + i);
2412 // LordHavoc: note to all quake engine coders, Quake had a special case
2413 // for 90 degrees which assumed a square view (wrong), so I removed it,
2414 // Quake2 has it disabled as well.
2416 // rotate R_VIEWFORWARD right by FOV_X/2 degrees
2417 //RotatePointAroundVector( r_view.frustum[0].normal, r_view.up, r_view.forward, -(90 - r_refdef.fov_x / 2));
2418 //r_view.frustum[0].dist = DotProduct (r_view.origin, frustum[0].normal);
2419 //PlaneClassify(&frustum[0]);
2421 // rotate R_VIEWFORWARD left by FOV_X/2 degrees
2422 //RotatePointAroundVector( r_view.frustum[1].normal, r_view.up, r_view.forward, (90 - r_refdef.fov_x / 2));
2423 //r_view.frustum[1].dist = DotProduct (r_view.origin, frustum[1].normal);
2424 //PlaneClassify(&frustum[1]);
2426 // rotate R_VIEWFORWARD up by FOV_X/2 degrees
2427 //RotatePointAroundVector( r_view.frustum[2].normal, r_view.left, r_view.forward, -(90 - r_refdef.fov_y / 2));
2428 //r_view.frustum[2].dist = DotProduct (r_view.origin, frustum[2].normal);
2429 //PlaneClassify(&frustum[2]);
2431 // rotate R_VIEWFORWARD down by FOV_X/2 degrees
2432 //RotatePointAroundVector( r_view.frustum[3].normal, r_view.left, r_view.forward, (90 - r_refdef.fov_y / 2));
2433 //r_view.frustum[3].dist = DotProduct (r_view.origin, frustum[3].normal);
2434 //PlaneClassify(&frustum[3]);
2437 //VectorCopy(r_view.forward, r_view.frustum[4].normal);
2438 //r_view.frustum[4].dist = DotProduct (r_view.origin, frustum[4].normal) + r_nearclip.value;
2439 //PlaneClassify(&frustum[4]);
2442 void R_View_Update(void)
2444 R_View_SetFrustum();
2445 R_View_WorldVisibility(r_view.useclipplane);
2446 R_View_UpdateEntityVisible();
2449 void R_SetupView(void)
2451 if (!r_view.useperspective)
2452 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);
2453 else if (r_refdef.rtworldshadows || r_refdef.rtdlightshadows)
2454 GL_SetupView_Mode_PerspectiveInfiniteFarClip(r_view.frustum_x, r_view.frustum_y, r_refdef.nearclip);
2456 GL_SetupView_Mode_Perspective(r_view.frustum_x, r_view.frustum_y, r_refdef.nearclip, r_refdef.farclip);
2458 GL_SetupView_Orientation_FromEntity(&r_view.matrix);
2460 if (r_view.useclipplane)
2462 // LordHavoc: couldn't figure out how to make this approach the
2463 vec_t dist = r_view.clipplane.dist - r_water_clippingplanebias.value;
2464 vec_t viewdist = DotProduct(r_view.origin, r_view.clipplane.normal);
2465 if (viewdist < r_view.clipplane.dist + r_water_clippingplanebias.value)
2466 dist = r_view.clipplane.dist;
2467 GL_SetupView_ApplyCustomNearClipPlane(r_view.clipplane.normal[0], r_view.clipplane.normal[1], r_view.clipplane.normal[2], dist);
2471 void R_ResetViewRendering2D(void)
2473 if (gl_support_fragment_shader)
2475 qglUseProgramObjectARB(0);CHECKGLERROR
2480 // GL is weird because it's bottom to top, r_view.y is top to bottom
2481 qglViewport(r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2482 GL_SetupView_Mode_Ortho(0, 0, 1, 1, -10, 100);
2483 GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
2484 GL_Color(1, 1, 1, 1);
2485 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
2486 GL_BlendFunc(GL_ONE, GL_ZERO);
2487 GL_AlphaTest(false);
2488 GL_ScissorTest(false);
2489 GL_DepthMask(false);
2490 GL_DepthRange(0, 1);
2491 GL_DepthTest(false);
2492 R_Mesh_Matrix(&identitymatrix);
2493 R_Mesh_ResetTextureState();
2494 GL_PolygonOffset(0, 0);
2495 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
2496 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2497 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2498 qglStencilMask(~0);CHECKGLERROR
2499 qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
2500 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
2501 GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
2504 void R_ResetViewRendering3D(void)
2506 if (gl_support_fragment_shader)
2508 qglUseProgramObjectARB(0);CHECKGLERROR
2513 // GL is weird because it's bottom to top, r_view.y is top to bottom
2514 qglViewport(r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2516 GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
2517 GL_Color(1, 1, 1, 1);
2518 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
2519 GL_BlendFunc(GL_ONE, GL_ZERO);
2520 GL_AlphaTest(false);
2521 GL_ScissorTest(true);
2523 GL_DepthRange(0, 1);
2525 R_Mesh_Matrix(&identitymatrix);
2526 R_Mesh_ResetTextureState();
2527 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
2528 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
2529 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2530 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2531 qglStencilMask(~0);CHECKGLERROR
2532 qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
2533 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
2534 GL_CullFace(r_view.cullface_back);
2538 R_Bloom_SetupShader(
2540 "// written by Forest 'LordHavoc' Hale\n"
2542 "// common definitions between vertex shader and fragment shader:\n"
2544 "#ifdef __GLSL_CG_DATA_TYPES\n"
2545 "#define myhalf half\n"
2546 "#define myhvec2 hvec2\n"
2547 "#define myhvec3 hvec3\n"
2548 "#define myhvec4 hvec4\n"
2550 "#define myhalf float\n"
2551 "#define myhvec2 vec2\n"
2552 "#define myhvec3 vec3\n"
2553 "#define myhvec4 vec4\n"
2556 "varying vec2 ScreenTexCoord;\n"
2557 "varying vec2 BloomTexCoord;\n"
2562 "// vertex shader specific:\n"
2563 "#ifdef VERTEX_SHADER\n"
2567 " ScreenTexCoord = vec2(gl_MultiTexCoord0);\n"
2568 " BloomTexCoord = vec2(gl_MultiTexCoord1);\n"
2569 " // transform vertex to camera space, using ftransform to match non-VS\n"
2571 " gl_Position = ftransform();\n"
2574 "#endif // VERTEX_SHADER\n"
2579 "// fragment shader specific:\n"
2580 "#ifdef FRAGMENT_SHADER\n"
2585 " myhvec3 color = myhvec3(texture2D(Texture_Screen, ScreenTexCoord));\n"
2586 " for (x = -BLUR_X;x <= BLUR_X;x++)
2587 " color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2588 " color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2589 " color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2590 " color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2592 " gl_FragColor = vec4(color);\n"
2595 "#endif // FRAGMENT_SHADER\n"
2598 void R_RenderScene(qboolean addwaterplanes);
2600 static void R_Water_StartFrame(void)
2603 int waterwidth, waterheight, texturewidth, textureheight;
2604 r_waterstate_waterplane_t *p;
2606 // set waterwidth and waterheight to the water resolution that will be
2607 // used (often less than the screen resolution for faster rendering)
2608 waterwidth = (int)bound(1, r_view.width * r_water_resolutionmultiplier.value, r_view.width);
2609 waterheight = (int)bound(1, r_view.height * r_water_resolutionmultiplier.value, r_view.height);
2611 // calculate desired texture sizes
2612 // can't use water if the card does not support the texture size
2613 if (!r_water.integer || !r_glsl.integer || !gl_support_fragment_shader || waterwidth > gl_max_texture_size || waterheight > gl_max_texture_size)
2614 texturewidth = textureheight = waterwidth = waterheight = 0;
2615 else if (gl_support_arb_texture_non_power_of_two)
2617 texturewidth = waterwidth;
2618 textureheight = waterheight;
2622 for (texturewidth = 1;texturewidth < waterwidth ;texturewidth *= 2);
2623 for (textureheight = 1;textureheight < waterheight;textureheight *= 2);
2626 // allocate textures as needed
2627 if (r_waterstate.waterwidth != waterwidth || r_waterstate.waterheight != waterheight || r_waterstate.texturewidth != texturewidth || r_waterstate.textureheight != textureheight)
2629 r_waterstate.maxwaterplanes = MAX_WATERPLANES;
2630 for (i = 0, p = r_waterstate.waterplanes;i < r_waterstate.maxwaterplanes;i++, p++)
2632 if (p->texture_refraction)
2633 R_FreeTexture(p->texture_refraction);
2634 p->texture_refraction = NULL;
2635 if (p->texture_reflection)
2636 R_FreeTexture(p->texture_reflection);
2637 p->texture_reflection = NULL;
2639 memset(&r_waterstate, 0, sizeof(r_waterstate));
2640 r_waterstate.waterwidth = waterwidth;
2641 r_waterstate.waterheight = waterheight;
2642 r_waterstate.texturewidth = texturewidth;
2643 r_waterstate.textureheight = textureheight;
2646 if (r_waterstate.waterwidth)
2648 r_waterstate.enabled = true;
2650 // set up variables that will be used in shader setup
2651 r_waterstate.screenscale[0] = 0.5f * (float)waterwidth / (float)texturewidth;
2652 r_waterstate.screenscale[1] = 0.5f * (float)waterheight / (float)textureheight;
2653 r_waterstate.screencenter[0] = 0.5f * (float)waterwidth / (float)texturewidth;
2654 r_waterstate.screencenter[1] = 0.5f * (float)waterheight / (float)textureheight;
2657 r_waterstate.maxwaterplanes = MAX_WATERPLANES;
2658 r_waterstate.numwaterplanes = 0;
2661 static void R_Water_AddWaterPlane(msurface_t *surface)
2663 int triangleindex, planeindex;
2668 r_waterstate_waterplane_t *p;
2669 // just use the first triangle with a valid normal for any decisions
2670 VectorClear(normal);
2671 for (triangleindex = 0, e = rsurface.modelelement3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
2673 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[0]*3, vert[0]);
2674 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[1]*3, vert[1]);
2675 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[2]*3, vert[2]);
2676 TriangleNormal(vert[0], vert[1], vert[2], normal);
2677 if (VectorLength2(normal) >= 0.001)
2681 // find a matching plane if there is one
2682 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2683 if (fabs(PlaneDiff(vert[0], &p->plane)) < 1 && fabs(PlaneDiff(vert[1], &p->plane)) < 1 && fabs(PlaneDiff(vert[2], &p->plane)) < 1)
2685 if (planeindex >= r_waterstate.maxwaterplanes)
2686 return; // nothing we can do, out of planes
2688 // if this triangle does not fit any known plane rendered this frame, add one
2689 if (planeindex >= r_waterstate.numwaterplanes)
2691 // store the new plane
2692 r_waterstate.numwaterplanes++;
2693 VectorCopy(normal, p->plane.normal);
2694 VectorNormalize(p->plane.normal);
2695 p->plane.dist = DotProduct(vert[0], p->plane.normal);
2696 PlaneClassify(&p->plane);
2697 // flip the plane if it does not face the viewer
2698 if (PlaneDiff(r_view.origin, &p->plane) < 0)
2700 VectorNegate(p->plane.normal, p->plane.normal);
2701 p->plane.dist *= -1;
2702 PlaneClassify(&p->plane);
2704 // clear materialflags and pvs
2705 p->materialflags = 0;
2706 p->pvsvalid = false;
2708 // merge this surface's materialflags into the waterplane
2709 p->materialflags |= surface->texture->currentframe->currentmaterialflags;
2710 // merge this surface's PVS into the waterplane
2711 VectorMAM(0.5f, surface->mins, 0.5f, surface->maxs, center);
2712 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) && r_refdef.worldmodel && r_refdef.worldmodel->brush.FatPVS
2713 && r_refdef.worldmodel->brush.PointInLeaf && r_refdef.worldmodel->brush.PointInLeaf(r_refdef.worldmodel, center)->clusterindex >= 0)
2715 r_refdef.worldmodel->brush.FatPVS(r_refdef.worldmodel, center, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
2720 static void R_Water_ProcessPlanes(void)
2722 r_view_t originalview;
2724 r_waterstate_waterplane_t *p;
2726 originalview = r_view;
2728 // make sure enough textures are allocated
2729 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2731 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
2733 if (!p->texture_refraction)
2734 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);
2735 if (!p->texture_refraction)
2739 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
2741 if (!p->texture_reflection)
2742 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);
2743 if (!p->texture_reflection)
2749 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2751 r_view.showdebug = false;
2752 r_view.width = r_waterstate.waterwidth;
2753 r_view.height = r_waterstate.waterheight;
2754 r_view.useclipplane = true;
2755 r_waterstate.renderingscene = true;
2757 // render the normal view scene and copy into texture
2758 // (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)
2759 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
2761 r_view.clipplane = p->plane;
2762 VectorNegate(r_view.clipplane.normal, r_view.clipplane.normal);
2763 r_view.clipplane.dist = -r_view.clipplane.dist;
2764 PlaneClassify(&r_view.clipplane);
2766 R_RenderScene(false);
2768 // copy view into the screen texture
2769 R_Mesh_TexBind(0, R_GetTexture(p->texture_refraction));
2770 GL_ActiveTexture(0);
2772 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
2775 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
2777 // render reflected scene and copy into texture
2778 Matrix4x4_Reflect(&r_view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
2779 r_view.clipplane = p->plane;
2780 // reverse the cullface settings for this render
2781 r_view.cullface_front = GL_FRONT;
2782 r_view.cullface_back = GL_BACK;
2783 if (r_refdef.worldmodel && r_refdef.worldmodel->brush.num_pvsclusterbytes)
2785 r_view.usecustompvs = true;
2787 memcpy(r_viewcache.world_pvsbits, p->pvsbits, r_refdef.worldmodel->brush.num_pvsclusterbytes);
2789 memset(r_viewcache.world_pvsbits, 0xFF, r_refdef.worldmodel->brush.num_pvsclusterbytes);
2792 R_ResetViewRendering3D();
2794 if (r_timereport_active)
2795 R_TimeReport("viewclear");
2797 R_RenderScene(false);
2799 R_Mesh_TexBind(0, R_GetTexture(p->texture_reflection));
2800 GL_ActiveTexture(0);
2802 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
2804 R_ResetViewRendering3D();
2806 if (r_timereport_active)
2807 R_TimeReport("viewclear");
2810 r_view = originalview;
2811 r_view.clear = true;
2812 r_waterstate.renderingscene = false;
2816 r_view = originalview;
2817 r_waterstate.renderingscene = false;
2818 Cvar_SetValueQuick(&r_water, 0);
2819 Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed! Turned off r_water.\n");
2823 void R_Bloom_StartFrame(void)
2825 int bloomtexturewidth, bloomtextureheight, screentexturewidth, screentextureheight;
2827 // set bloomwidth and bloomheight to the bloom resolution that will be
2828 // used (often less than the screen resolution for faster rendering)
2829 r_bloomstate.bloomwidth = bound(1, r_bloom_resolution.integer, r_view.width);
2830 r_bloomstate.bloomheight = r_bloomstate.bloomwidth * r_view.height / r_view.width;
2831 r_bloomstate.bloomheight = bound(1, r_bloomstate.bloomheight, r_view.height);
2833 // calculate desired texture sizes
2834 if (gl_support_arb_texture_non_power_of_two)
2836 screentexturewidth = r_view.width;
2837 screentextureheight = r_view.height;
2838 bloomtexturewidth = r_bloomstate.bloomwidth;
2839 bloomtextureheight = r_bloomstate.bloomheight;
2843 for (screentexturewidth = 1;screentexturewidth < vid.width ;screentexturewidth *= 2);
2844 for (screentextureheight = 1;screentextureheight < vid.height ;screentextureheight *= 2);
2845 for (bloomtexturewidth = 1;bloomtexturewidth < r_bloomstate.bloomwidth ;bloomtexturewidth *= 2);
2846 for (bloomtextureheight = 1;bloomtextureheight < r_bloomstate.bloomheight;bloomtextureheight *= 2);
2851 screentexturewidth = screentextureheight = 0;
2853 else if (r_bloom.integer)
2858 screentexturewidth = screentextureheight = 0;
2859 bloomtexturewidth = bloomtextureheight = 0;
2862 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)
2864 // can't use bloom if the parameters are too weird
2865 // can't use bloom if the card does not support the texture size
2866 if (r_bloomstate.texture_screen)
2867 R_FreeTexture(r_bloomstate.texture_screen);
2868 if (r_bloomstate.texture_bloom)
2869 R_FreeTexture(r_bloomstate.texture_bloom);
2870 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
2874 r_bloomstate.enabled = true;
2875 r_bloomstate.hdr = r_hdr.integer != 0;
2877 // allocate textures as needed
2878 if (r_bloomstate.screentexturewidth != screentexturewidth || r_bloomstate.screentextureheight != screentextureheight)
2880 if (r_bloomstate.texture_screen)
2881 R_FreeTexture(r_bloomstate.texture_screen);
2882 r_bloomstate.texture_screen = NULL;
2883 r_bloomstate.screentexturewidth = screentexturewidth;
2884 r_bloomstate.screentextureheight = screentextureheight;
2885 if (r_bloomstate.screentexturewidth && r_bloomstate.screentextureheight)
2886 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);
2888 if (r_bloomstate.bloomtexturewidth != bloomtexturewidth || r_bloomstate.bloomtextureheight != bloomtextureheight)
2890 if (r_bloomstate.texture_bloom)
2891 R_FreeTexture(r_bloomstate.texture_bloom);
2892 r_bloomstate.texture_bloom = NULL;
2893 r_bloomstate.bloomtexturewidth = bloomtexturewidth;
2894 r_bloomstate.bloomtextureheight = bloomtextureheight;
2895 if (r_bloomstate.bloomtexturewidth && r_bloomstate.bloomtextureheight)
2896 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);
2899 // set up a texcoord array for the full resolution screen image
2900 // (we have to keep this around to copy back during final render)
2901 r_bloomstate.screentexcoord2f[0] = 0;
2902 r_bloomstate.screentexcoord2f[1] = (float)r_view.height / (float)r_bloomstate.screentextureheight;
2903 r_bloomstate.screentexcoord2f[2] = (float)r_view.width / (float)r_bloomstate.screentexturewidth;
2904 r_bloomstate.screentexcoord2f[3] = (float)r_view.height / (float)r_bloomstate.screentextureheight;
2905 r_bloomstate.screentexcoord2f[4] = (float)r_view.width / (float)r_bloomstate.screentexturewidth;
2906 r_bloomstate.screentexcoord2f[5] = 0;
2907 r_bloomstate.screentexcoord2f[6] = 0;
2908 r_bloomstate.screentexcoord2f[7] = 0;
2910 // set up a texcoord array for the reduced resolution bloom image
2911 // (which will be additive blended over the screen image)
2912 r_bloomstate.bloomtexcoord2f[0] = 0;
2913 r_bloomstate.bloomtexcoord2f[1] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
2914 r_bloomstate.bloomtexcoord2f[2] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
2915 r_bloomstate.bloomtexcoord2f[3] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
2916 r_bloomstate.bloomtexcoord2f[4] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
2917 r_bloomstate.bloomtexcoord2f[5] = 0;
2918 r_bloomstate.bloomtexcoord2f[6] = 0;
2919 r_bloomstate.bloomtexcoord2f[7] = 0;
2922 void R_Bloom_CopyScreenTexture(float colorscale)
2924 r_refdef.stats.bloom++;
2926 R_ResetViewRendering2D();
2927 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
2928 R_Mesh_ColorPointer(NULL, 0, 0);
2929 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
2930 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
2932 // copy view into the screen texture
2933 GL_ActiveTexture(0);
2935 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
2936 r_refdef.stats.bloom_copypixels += r_view.width * r_view.height;
2938 // now scale it down to the bloom texture size
2940 qglViewport(r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
2941 GL_BlendFunc(GL_ONE, GL_ZERO);
2942 GL_Color(colorscale, colorscale, colorscale, 1);
2943 // TODO: optimize with multitexture or GLSL
2944 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2945 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
2947 // we now have a bloom image in the framebuffer
2948 // copy it into the bloom image texture for later processing
2949 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
2950 GL_ActiveTexture(0);
2952 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
2953 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
2956 void R_Bloom_CopyHDRTexture(void)
2958 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
2959 GL_ActiveTexture(0);
2961 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
2962 r_refdef.stats.bloom_copypixels += r_view.width * r_view.height;
2965 void R_Bloom_MakeTexture(void)
2968 float xoffset, yoffset, r, brighten;
2970 r_refdef.stats.bloom++;
2972 R_ResetViewRendering2D();
2973 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
2974 R_Mesh_ColorPointer(NULL, 0, 0);
2976 // we have a bloom image in the framebuffer
2978 qglViewport(r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
2980 for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
2983 r = bound(0, r_bloom_colorexponent.value / x, 1);
2984 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2985 GL_Color(r, r, r, 1);
2986 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
2987 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
2988 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2989 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
2991 // copy the vertically blurred bloom view to a texture
2992 GL_ActiveTexture(0);
2994 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
2995 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
2998 range = r_bloom_blur.integer * r_bloomstate.bloomwidth / 320;
2999 brighten = r_bloom_brighten.value;
3001 brighten *= r_hdr_range.value;
3002 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3003 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.offsettexcoord2f, 0, 0);
3005 for (dir = 0;dir < 2;dir++)
3007 // blend on at multiple vertical offsets to achieve a vertical blur
3008 // TODO: do offset blends using GLSL
3009 GL_BlendFunc(GL_ONE, GL_ZERO);
3010 for (x = -range;x <= range;x++)
3012 if (!dir){xoffset = 0;yoffset = x;}
3013 else {xoffset = x;yoffset = 0;}
3014 xoffset /= (float)r_bloomstate.bloomtexturewidth;
3015 yoffset /= (float)r_bloomstate.bloomtextureheight;
3016 // compute a texcoord array with the specified x and y offset
3017 r_bloomstate.offsettexcoord2f[0] = xoffset+0;
3018 r_bloomstate.offsettexcoord2f[1] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3019 r_bloomstate.offsettexcoord2f[2] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3020 r_bloomstate.offsettexcoord2f[3] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3021 r_bloomstate.offsettexcoord2f[4] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3022 r_bloomstate.offsettexcoord2f[5] = yoffset+0;
3023 r_bloomstate.offsettexcoord2f[6] = xoffset+0;
3024 r_bloomstate.offsettexcoord2f[7] = yoffset+0;
3025 // this r value looks like a 'dot' particle, fading sharply to
3026 // black at the edges
3027 // (probably not realistic but looks good enough)
3028 //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
3029 //r = (dir ? 1.0f : brighten)/(range*2+1);
3030 r = (dir ? 1.0f : brighten)/(range*2+1)*(1 - x*x/(float)(range*range));
3031 GL_Color(r, r, r, 1);
3032 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3033 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3034 GL_BlendFunc(GL_ONE, GL_ONE);
3037 // copy the vertically blurred bloom view to a texture
3038 GL_ActiveTexture(0);
3040 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
3041 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3044 // apply subtract last
3045 // (just like it would be in a GLSL shader)
3046 if (r_bloom_colorsubtract.value > 0 && gl_support_ext_blend_subtract)
3048 GL_BlendFunc(GL_ONE, GL_ZERO);
3049 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3050 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3051 GL_Color(1, 1, 1, 1);
3052 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3053 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3055 GL_BlendFunc(GL_ONE, GL_ONE);
3056 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
3057 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
3058 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3059 GL_Color(r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 1);
3060 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3061 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3062 qglBlendEquationEXT(GL_FUNC_ADD_EXT);
3064 // copy the darkened bloom view to a texture
3065 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3066 GL_ActiveTexture(0);
3068 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
3069 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3073 static void R_UpdateFogColor(void); // needs to be called before HDR subrender too, as that changes colorscale!
3075 void R_HDR_RenderBloomTexture(void)
3077 int oldwidth, oldheight;
3079 oldwidth = r_view.width;
3080 oldheight = r_view.height;
3081 r_view.width = r_bloomstate.bloomwidth;
3082 r_view.height = r_bloomstate.bloomheight;
3084 // TODO: support GL_EXT_framebuffer_object rather than reusing the framebuffer? it might improve SLI performance.
3085 // TODO: add exposure compensation features
3086 // TODO: add fp16 framebuffer support
3088 r_view.showdebug = false;
3089 r_view.colorscale = r_bloom_colorscale.value * r_hdr_scenebrightness.value;
3091 r_view.colorscale /= r_hdr_range.value;
3095 r_waterstate.numwaterplanes = 0;
3096 R_RenderScene(r_waterstate.enabled);
3097 r_view.showdebug = true;
3099 R_ResetViewRendering2D();
3101 R_Bloom_CopyHDRTexture();
3102 R_Bloom_MakeTexture();
3104 R_ResetViewRendering3D();
3107 if (r_timereport_active)
3108 R_TimeReport("viewclear");
3110 // restore the view settings
3111 r_view.width = oldwidth;
3112 r_view.height = oldheight;
3115 static void R_BlendView(void)
3117 if (r_bloomstate.enabled && r_bloomstate.hdr)
3119 // render high dynamic range bloom effect
3120 // the bloom texture was made earlier this render, so we just need to
3121 // blend it onto the screen...
3122 R_ResetViewRendering2D();
3123 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3124 R_Mesh_ColorPointer(NULL, 0, 0);
3125 GL_Color(1, 1, 1, 1);
3126 GL_BlendFunc(GL_ONE, GL_ONE);
3127 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3128 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3129 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3130 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3132 else if (r_bloomstate.enabled)
3134 // render simple bloom effect
3135 // copy the screen and shrink it and darken it for the bloom process
3136 R_Bloom_CopyScreenTexture(r_bloom_colorscale.value);
3137 // make the bloom texture
3138 R_Bloom_MakeTexture();
3139 // put the original screen image back in place and blend the bloom
3141 R_ResetViewRendering2D();
3142 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3143 R_Mesh_ColorPointer(NULL, 0, 0);
3144 GL_Color(1, 1, 1, 1);
3145 GL_BlendFunc(GL_ONE, GL_ZERO);
3146 // do both in one pass if possible
3147 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3148 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3149 if (r_textureunits.integer >= 2 && gl_combine.integer)
3151 R_Mesh_TexCombine(1, GL_ADD, GL_ADD, 1, 1);
3152 R_Mesh_TexBind(1, R_GetTexture(r_bloomstate.texture_screen));
3153 R_Mesh_TexCoordPointer(1, 2, r_bloomstate.screentexcoord2f, 0, 0);
3157 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3158 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3159 // now blend on the bloom texture
3160 GL_BlendFunc(GL_ONE, GL_ONE);
3161 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
3162 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
3164 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3165 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3167 if (r_refdef.viewblend[3] >= (1.0f / 256.0f))
3169 // apply a color tint to the whole view
3170 R_ResetViewRendering2D();
3171 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3172 R_Mesh_ColorPointer(NULL, 0, 0);
3173 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3174 GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
3175 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3179 void R_RenderScene(qboolean addwaterplanes);
3181 matrix4x4_t r_waterscrollmatrix;
3183 static void R_UpdateFogColor(void) // needs to be called before HDR subrender too, as that changes colorscale!
3185 if (r_refdef.fog_density)
3187 r_refdef.fogcolor[0] = r_refdef.fog_red;
3188 r_refdef.fogcolor[1] = r_refdef.fog_green;
3189 r_refdef.fogcolor[2] = r_refdef.fog_blue;
3193 // color.rgb *= SceneBrightness;
3194 VectorScale(r_refdef.fogcolor, r_view.colorscale, fogvec);
3195 if(r_glsl.integer && (r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)) // need to support contrast boost
3197 // color.rgb *= ContrastBoost / ((ContrastBoost - 1) * color.rgb + 1);
3198 fogvec[0] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[0] + 1);
3199 fogvec[1] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[1] + 1);
3200 fogvec[2] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[2] + 1);
3202 r_refdef.fogcolor[0] = bound(0.0f, fogvec[0], 1.0f);
3203 r_refdef.fogcolor[1] = bound(0.0f, fogvec[1], 1.0f);
3204 r_refdef.fogcolor[2] = bound(0.0f, fogvec[2], 1.0f);
3209 void R_UpdateVariables(void)
3213 r_refdef.farclip = 4096;
3214 if (r_refdef.worldmodel)
3215 r_refdef.farclip += VectorDistance(r_refdef.worldmodel->normalmins, r_refdef.worldmodel->normalmaxs);
3216 r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
3218 if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
3219 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
3220 r_refdef.polygonfactor = 0;
3221 r_refdef.polygonoffset = 0;
3222 r_refdef.shadowpolygonfactor = r_refdef.polygonfactor + r_shadow_polygonfactor.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
3223 r_refdef.shadowpolygonoffset = r_refdef.polygonoffset + r_shadow_polygonoffset.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
3225 r_refdef.rtworld = r_shadow_realtime_world.integer;
3226 r_refdef.rtworldshadows = r_shadow_realtime_world_shadows.integer && gl_stencil;
3227 r_refdef.rtdlight = (r_shadow_realtime_world.integer || r_shadow_realtime_dlight.integer) && !gl_flashblend.integer && r_dynamic.integer;
3228 r_refdef.rtdlightshadows = r_refdef.rtdlight && r_shadow_realtime_dlight_shadows.integer && gl_stencil;
3229 r_refdef.lightmapintensity = r_refdef.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
3230 if (r_showsurfaces.integer)
3232 r_refdef.rtworld = false;
3233 r_refdef.rtworldshadows = false;
3234 r_refdef.rtdlight = false;
3235 r_refdef.rtdlightshadows = false;
3236 r_refdef.lightmapintensity = 0;
3239 if (gamemode == GAME_NEHAHRA)
3241 if (gl_fogenable.integer)
3243 r_refdef.oldgl_fogenable = true;
3244 r_refdef.fog_density = gl_fogdensity.value;
3245 r_refdef.fog_red = gl_fogred.value;
3246 r_refdef.fog_green = gl_foggreen.value;
3247 r_refdef.fog_blue = gl_fogblue.value;
3249 else if (r_refdef.oldgl_fogenable)
3251 r_refdef.oldgl_fogenable = false;
3252 r_refdef.fog_density = 0;
3253 r_refdef.fog_red = 0;
3254 r_refdef.fog_green = 0;
3255 r_refdef.fog_blue = 0;
3259 if (r_refdef.fog_start >= r_refdef.fog_end || r_refdef.fog_start < 0)
3261 r_refdef.fog_start = 0;
3262 r_refdef.fog_end = 1000000000;
3263 // TODO update fog cvars here too
3268 if (r_refdef.fog_density)
3270 r_refdef.fogenabled = true;
3271 // this is the point where the fog reaches 0.9986 alpha, which we
3272 // consider a good enough cutoff point for the texture
3273 // (0.9986 * 256 == 255.6)
3274 r_refdef.fogrange = 16 / (r_refdef.fog_density * r_refdef.fog_density);
3275 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
3276 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
3277 // fog color was already set
3280 r_refdef.fogenabled = false;
3288 void R_RenderView(void)
3290 if (!r_refdef.entities/* || !r_refdef.worldmodel*/)
3291 return; //Host_Error ("R_RenderView: NULL worldmodel");
3293 R_Shadow_UpdateWorldLightSelection();
3295 R_Bloom_StartFrame();
3296 R_Water_StartFrame();
3299 if (r_timereport_active)
3300 R_TimeReport("viewsetup");
3302 R_ResetViewRendering3D();
3307 if (r_timereport_active)
3308 R_TimeReport("viewclear");
3310 r_view.clear = true;
3312 r_view.showdebug = true;
3314 // this produces a bloom texture to be used in R_BlendView() later
3316 R_HDR_RenderBloomTexture();
3318 r_view.colorscale = r_hdr_scenebrightness.value;
3319 r_waterstate.numwaterplanes = 0;
3320 R_RenderScene(r_waterstate.enabled);
3323 if (r_timereport_active)
3324 R_TimeReport("blendview");
3326 GL_Scissor(0, 0, vid.width, vid.height);
3327 GL_ScissorTest(false);
3331 extern void R_DrawLightningBeams (void);
3332 extern void VM_CL_AddPolygonsToMeshQueue (void);
3333 extern void R_DrawPortals (void);
3334 extern cvar_t cl_locs_show;
3335 static void R_DrawLocs(void);
3336 static void R_DrawEntityBBoxes(void);
3337 void R_RenderScene(qboolean addwaterplanes)
3341 R_ResetViewRendering3D();
3344 if (r_timereport_active)
3345 R_TimeReport("watervis");
3347 if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->DrawAddWaterPlanes)
3349 r_refdef.worldmodel->DrawAddWaterPlanes(r_refdef.worldentity);
3350 if (r_timereport_active)
3351 R_TimeReport("waterworld");
3354 // don't let sound skip if going slow
3355 if (r_refdef.extraupdate)
3358 R_DrawModelsAddWaterPlanes();
3359 if (r_timereport_active)
3360 R_TimeReport("watermodels");
3362 R_Water_ProcessPlanes();
3363 if (r_timereport_active)
3364 R_TimeReport("waterscenes");
3367 R_ResetViewRendering3D();
3369 // don't let sound skip if going slow
3370 if (r_refdef.extraupdate)
3373 R_MeshQueue_BeginScene();
3378 if (r_timereport_active)
3379 R_TimeReport("visibility");
3381 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);
3383 if (cl.csqc_vidvars.drawworld)
3385 // don't let sound skip if going slow
3386 if (r_refdef.extraupdate)
3389 if (r_refdef.worldmodel && r_refdef.worldmodel->DrawSky)
3391 r_refdef.worldmodel->DrawSky(r_refdef.worldentity);
3392 if (r_timereport_active)
3393 R_TimeReport("worldsky");
3396 if (R_DrawBrushModelsSky() && r_timereport_active)
3397 R_TimeReport("bmodelsky");
3400 if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->DrawDepth)
3402 r_refdef.worldmodel->DrawDepth(r_refdef.worldentity);
3403 if (r_timereport_active)
3404 R_TimeReport("worlddepth");
3406 if (r_depthfirst.integer >= 2)
3408 R_DrawModelsDepth();
3409 if (r_timereport_active)
3410 R_TimeReport("modeldepth");
3413 if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->Draw)
3415 r_refdef.worldmodel->Draw(r_refdef.worldentity);
3416 if (r_timereport_active)
3417 R_TimeReport("world");
3420 // don't let sound skip if going slow
3421 if (r_refdef.extraupdate)
3425 if (r_timereport_active)
3426 R_TimeReport("models");
3428 // don't let sound skip if going slow
3429 if (r_refdef.extraupdate)
3432 if (r_shadows.integer > 0 && r_refdef.lightmapintensity > 0)
3434 R_DrawModelShadows();
3436 R_ResetViewRendering3D();
3438 // don't let sound skip if going slow
3439 if (r_refdef.extraupdate)
3443 R_ShadowVolumeLighting(false);
3444 if (r_timereport_active)
3445 R_TimeReport("rtlights");
3447 // don't let sound skip if going slow
3448 if (r_refdef.extraupdate)
3451 if (cl.csqc_vidvars.drawworld)
3453 R_DrawLightningBeams();
3454 if (r_timereport_active)
3455 R_TimeReport("lightning");
3458 if (r_timereport_active)
3459 R_TimeReport("decals");
3462 if (r_timereport_active)
3463 R_TimeReport("particles");
3466 if (r_timereport_active)
3467 R_TimeReport("explosions");
3470 if (gl_support_fragment_shader)
3472 qglUseProgramObjectARB(0);CHECKGLERROR
3474 VM_CL_AddPolygonsToMeshQueue();
3476 if (r_view.showdebug)
3478 if (cl_locs_show.integer)
3481 if (r_timereport_active)
3482 R_TimeReport("showlocs");
3485 if (r_drawportals.integer)
3488 if (r_timereport_active)
3489 R_TimeReport("portals");
3492 if (r_showbboxes.value > 0)
3494 R_DrawEntityBBoxes();
3495 if (r_timereport_active)
3496 R_TimeReport("bboxes");
3500 if (gl_support_fragment_shader)
3502 qglUseProgramObjectARB(0);CHECKGLERROR
3504 R_MeshQueue_RenderTransparent();
3505 if (r_timereport_active)
3506 R_TimeReport("drawtrans");
3508 if (gl_support_fragment_shader)
3510 qglUseProgramObjectARB(0);CHECKGLERROR
3513 if (r_view.showdebug && r_refdef.worldmodel && r_refdef.worldmodel->DrawDebug && (r_showtris.value > 0 || r_shownormals.value > 0 || r_showcollisionbrushes.value > 0))
3515 r_refdef.worldmodel->DrawDebug(r_refdef.worldentity);
3516 if (r_timereport_active)
3517 R_TimeReport("worlddebug");
3518 R_DrawModelsDebug();
3519 if (r_timereport_active)
3520 R_TimeReport("modeldebug");
3523 if (gl_support_fragment_shader)
3525 qglUseProgramObjectARB(0);CHECKGLERROR
3528 if (cl.csqc_vidvars.drawworld)
3531 if (r_timereport_active)
3532 R_TimeReport("coronas");
3535 // don't let sound skip if going slow
3536 if (r_refdef.extraupdate)
3539 R_ResetViewRendering2D();
3542 static const int bboxelements[36] =
3552 void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
3555 float *v, *c, f1, f2, vertex3f[8*3], color4f[8*4];
3556 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3557 GL_DepthMask(false);
3558 GL_DepthRange(0, 1);
3559 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3560 R_Mesh_Matrix(&identitymatrix);
3561 R_Mesh_ResetTextureState();
3563 vertex3f[ 0] = mins[0];vertex3f[ 1] = mins[1];vertex3f[ 2] = mins[2]; //
3564 vertex3f[ 3] = maxs[0];vertex3f[ 4] = mins[1];vertex3f[ 5] = mins[2];
3565 vertex3f[ 6] = mins[0];vertex3f[ 7] = maxs[1];vertex3f[ 8] = mins[2];
3566 vertex3f[ 9] = maxs[0];vertex3f[10] = maxs[1];vertex3f[11] = mins[2];
3567 vertex3f[12] = mins[0];vertex3f[13] = mins[1];vertex3f[14] = maxs[2];
3568 vertex3f[15] = maxs[0];vertex3f[16] = mins[1];vertex3f[17] = maxs[2];
3569 vertex3f[18] = mins[0];vertex3f[19] = maxs[1];vertex3f[20] = maxs[2];
3570 vertex3f[21] = maxs[0];vertex3f[22] = maxs[1];vertex3f[23] = maxs[2];
3571 R_FillColors(color4f, 8, cr, cg, cb, ca);
3572 if (r_refdef.fogenabled)
3574 for (i = 0, v = vertex3f, c = color4f;i < 8;i++, v += 3, c += 4)
3576 f1 = FogPoint_World(v);
3578 c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
3579 c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
3580 c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
3583 R_Mesh_VertexPointer(vertex3f, 0, 0);
3584 R_Mesh_ColorPointer(color4f, 0, 0);
3585 R_Mesh_ResetTextureState();
3586 R_Mesh_Draw(0, 8, 12, bboxelements, 0, 0);
3589 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3593 prvm_edict_t *edict;
3594 // this function draws bounding boxes of server entities
3598 for (i = 0;i < numsurfaces;i++)
3600 edict = PRVM_EDICT_NUM(surfacelist[i]);
3601 switch ((int)edict->fields.server->solid)
3603 case SOLID_NOT: Vector4Set(color, 1, 1, 1, 0.05);break;
3604 case SOLID_TRIGGER: Vector4Set(color, 1, 0, 1, 0.10);break;
3605 case SOLID_BBOX: Vector4Set(color, 0, 1, 0, 0.10);break;
3606 case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
3607 case SOLID_BSP: Vector4Set(color, 0, 0, 1, 0.05);break;
3608 default: Vector4Set(color, 0, 0, 0, 0.50);break;
3610 color[3] *= r_showbboxes.value;
3611 color[3] = bound(0, color[3], 1);
3612 GL_DepthTest(!r_showdisabledepthtest.integer);
3613 GL_CullFace(r_view.cullface_front);
3614 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
3619 static void R_DrawEntityBBoxes(void)
3622 prvm_edict_t *edict;
3624 // this function draws bounding boxes of server entities
3628 for (i = 0;i < prog->num_edicts;i++)
3630 edict = PRVM_EDICT_NUM(i);
3631 if (edict->priv.server->free)
3633 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
3634 R_MeshQueue_AddTransparent(center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)NULL);
3639 int nomodelelements[24] =
3651 float nomodelvertex3f[6*3] =
3661 float nomodelcolor4f[6*4] =
3663 0.0f, 0.0f, 0.5f, 1.0f,
3664 0.0f, 0.0f, 0.5f, 1.0f,
3665 0.0f, 0.5f, 0.0f, 1.0f,
3666 0.0f, 0.5f, 0.0f, 1.0f,
3667 0.5f, 0.0f, 0.0f, 1.0f,
3668 0.5f, 0.0f, 0.0f, 1.0f
3671 void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3676 // this is only called once per entity so numsurfaces is always 1, and
3677 // surfacelist is always {0}, so this code does not handle batches
3678 R_Mesh_Matrix(&ent->matrix);
3680 if (ent->flags & EF_ADDITIVE)
3682 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
3683 GL_DepthMask(false);
3685 else if (ent->alpha < 1)
3687 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3688 GL_DepthMask(false);
3692 GL_BlendFunc(GL_ONE, GL_ZERO);
3695 GL_DepthRange(0, (ent->flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
3696 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3697 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
3698 GL_CullFace((ent->effects & EF_DOUBLESIDED) ? GL_NONE : r_view.cullface_back);
3699 R_Mesh_VertexPointer(nomodelvertex3f, 0, 0);
3700 if (r_refdef.fogenabled)
3703 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
3704 R_Mesh_ColorPointer(color4f, 0, 0);
3705 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3706 f1 = FogPoint_World(org);
3708 for (i = 0, c = color4f;i < 6;i++, c += 4)
3710 c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
3711 c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
3712 c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
3716 else if (ent->alpha != 1)
3718 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
3719 R_Mesh_ColorPointer(color4f, 0, 0);
3720 for (i = 0, c = color4f;i < 6;i++, c += 4)
3724 R_Mesh_ColorPointer(nomodelcolor4f, 0, 0);
3725 R_Mesh_ResetTextureState();
3726 R_Mesh_Draw(0, 6, 8, nomodelelements, 0, 0);
3729 void R_DrawNoModel(entity_render_t *ent)
3732 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3733 //if ((ent->effects & EF_ADDITIVE) || (ent->alpha < 1))
3734 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_view.origin : org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
3736 // R_DrawNoModelCallback(ent, 0);
3739 void R_CalcBeam_Vertex3f (float *vert, const vec3_t org1, const vec3_t org2, float width)
3741 vec3_t right1, right2, diff, normal;
3743 VectorSubtract (org2, org1, normal);
3745 // calculate 'right' vector for start
3746 VectorSubtract (r_view.origin, org1, diff);
3747 CrossProduct (normal, diff, right1);
3748 VectorNormalize (right1);
3750 // calculate 'right' vector for end
3751 VectorSubtract (r_view.origin, org2, diff);
3752 CrossProduct (normal, diff, right2);
3753 VectorNormalize (right2);
3755 vert[ 0] = org1[0] + width * right1[0];
3756 vert[ 1] = org1[1] + width * right1[1];
3757 vert[ 2] = org1[2] + width * right1[2];
3758 vert[ 3] = org1[0] - width * right1[0];
3759 vert[ 4] = org1[1] - width * right1[1];
3760 vert[ 5] = org1[2] - width * right1[2];
3761 vert[ 6] = org2[0] - width * right2[0];
3762 vert[ 7] = org2[1] - width * right2[1];
3763 vert[ 8] = org2[2] - width * right2[2];
3764 vert[ 9] = org2[0] + width * right2[0];
3765 vert[10] = org2[1] + width * right2[1];
3766 vert[11] = org2[2] + width * right2[2];
3769 float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
3771 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)
3776 if (r_refdef.fogenabled)
3777 fog = FogPoint_World(origin);
3779 R_Mesh_Matrix(&identitymatrix);
3780 GL_BlendFunc(blendfunc1, blendfunc2);
3786 GL_CullFace(r_view.cullface_front);
3789 GL_CullFace(r_view.cullface_back);
3791 GL_DepthMask(false);
3792 GL_DepthRange(0, depthshort ? 0.0625 : 1);
3793 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3794 GL_DepthTest(!depthdisable);
3796 vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
3797 vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
3798 vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
3799 vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
3800 vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
3801 vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
3802 vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
3803 vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
3804 vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
3805 vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
3806 vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
3807 vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
3809 R_Mesh_VertexPointer(vertex3f, 0, 0);
3810 R_Mesh_ColorPointer(NULL, 0, 0);
3811 R_Mesh_ResetTextureState();
3812 R_Mesh_TexBind(0, R_GetTexture(texture));
3813 R_Mesh_TexCoordPointer(0, 2, spritetexcoord2f, 0, 0);
3814 // FIXME: fixed function path can't properly handle r_view.colorscale > 1
3815 GL_Color(cr * fog * r_view.colorscale, cg * fog * r_view.colorscale, cb * fog * r_view.colorscale, ca);
3816 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3818 if (blendfunc2 == GL_ONE_MINUS_SRC_ALPHA)
3820 R_Mesh_TexBind(0, R_GetTexture(fogtexture));
3821 GL_BlendFunc(blendfunc1, GL_ONE);
3823 GL_Color(r_refdef.fogcolor[0] * fog, r_refdef.fogcolor[1] * fog, r_refdef.fogcolor[2] * fog, ca);
3824 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3828 int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
3833 VectorSet(v, x, y, z);
3834 for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
3835 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
3837 if (i == mesh->numvertices)
3839 if (mesh->numvertices < mesh->maxvertices)
3841 VectorCopy(v, vertex3f);
3842 mesh->numvertices++;
3844 return mesh->numvertices;
3850 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
3854 element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
3855 element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
3856 e = mesh->element3i + mesh->numtriangles * 3;
3857 for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
3859 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
3860 if (mesh->numtriangles < mesh->maxtriangles)
3865 mesh->numtriangles++;
3867 element[1] = element[2];
3871 void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
3875 element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
3876 element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
3877 e = mesh->element3i + mesh->numtriangles * 3;
3878 for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
3880 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
3881 if (mesh->numtriangles < mesh->maxtriangles)
3886 mesh->numtriangles++;
3888 element[1] = element[2];
3892 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
3893 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
3895 int planenum, planenum2;
3898 mplane_t *plane, *plane2;
3900 double temppoints[2][256*3];
3901 // figure out how large a bounding box we need to properly compute this brush
3903 for (w = 0;w < numplanes;w++)
3904 maxdist = max(maxdist, planes[w].dist);
3905 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
3906 maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
3907 for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
3911 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
3912 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
3914 if (planenum2 == planenum)
3916 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);
3919 if (tempnumpoints < 3)
3921 // generate elements forming a triangle fan for this polygon
3922 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
3926 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)
3928 texturelayer_t *layer;
3929 layer = t->currentlayers + t->currentnumlayers++;
3931 layer->depthmask = depthmask;
3932 layer->blendfunc1 = blendfunc1;
3933 layer->blendfunc2 = blendfunc2;
3934 layer->texture = texture;
3935 layer->texmatrix = *matrix;
3936 layer->color[0] = r * r_view.colorscale;
3937 layer->color[1] = g * r_view.colorscale;
3938 layer->color[2] = b * r_view.colorscale;
3939 layer->color[3] = a;
3942 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
3945 index = parms[2] + r_refdef.time * parms[3];
3946 index -= floor(index);
3950 case Q3WAVEFUNC_NONE:
3951 case Q3WAVEFUNC_NOISE:
3952 case Q3WAVEFUNC_COUNT:
3955 case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
3956 case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
3957 case Q3WAVEFUNC_SAWTOOTH: f = index;break;
3958 case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
3959 case Q3WAVEFUNC_TRIANGLE:
3961 f = index - floor(index);
3972 return (float)(parms[0] + parms[1] * f);
3975 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
3978 model_t *model = ent->model;
3981 q3shaderinfo_layer_tcmod_t *tcmod;
3983 // switch to an alternate material if this is a q1bsp animated material
3985 texture_t *texture = t;
3986 int s = ent->skinnum;
3987 if ((unsigned int)s >= (unsigned int)model->numskins)
3989 if (model->skinscenes)
3991 if (model->skinscenes[s].framecount > 1)
3992 s = model->skinscenes[s].firstframe + (unsigned int) (r_refdef.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
3994 s = model->skinscenes[s].firstframe;
3997 t = t + s * model->num_surfaces;
4000 // use an alternate animation if the entity's frame is not 0,
4001 // and only if the texture has an alternate animation
4002 if (ent->frame2 != 0 && t->anim_total[1])
4003 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[1]) : 0];
4005 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[0]) : 0];
4007 texture->currentframe = t;
4010 // update currentskinframe to be a qw skin or animation frame
4011 if ((i = ent->entitynumber - 1) >= 0 && i < cl.maxclients)
4013 if (strcmp(r_qwskincache[i], cl.scores[i].qw_skin))
4015 strlcpy(r_qwskincache[i], cl.scores[i].qw_skin, sizeof(r_qwskincache[i]));
4016 Con_DPrintf("loading skins/%s\n", r_qwskincache[i]);
4017 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);
4019 t->currentskinframe = r_qwskincache_skinframe[i];
4020 if (t->currentskinframe == NULL)
4021 t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->frame2time)) % t->numskinframes];
4023 else if (t->numskinframes >= 2)
4024 t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->frame2time)) % t->numskinframes];
4025 if (t->backgroundnumskinframes >= 2)
4026 t->backgroundcurrentskinframe = t->backgroundskinframes[(int)(t->backgroundskinframerate * (cl.time - ent->frame2time)) % t->backgroundnumskinframes];
4028 t->currentmaterialflags = t->basematerialflags;
4029 t->currentalpha = ent->alpha;
4030 if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_novis.integer))
4032 t->currentalpha *= r_wateralpha.value;
4034 * FIXME what is this supposed to do?
4035 // if rendering refraction/reflection, disable transparency
4036 if (r_waterstate.enabled && (t->currentalpha < 1 || (t->currentmaterialflags & MATERIALFLAG_ALPHA)))
4037 t->currentmaterialflags |= MATERIALFLAG_WATERSHADER;
4040 if(!r_waterstate.enabled)
4041 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION);
4042 if (!(ent->flags & RENDER_LIGHT))
4043 t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
4044 else if (rsurface.modeltexcoordlightmap2f == NULL)
4046 // pick a model lighting mode
4047 if (VectorLength2(ent->modellight_diffuse) >= (1.0f / 256.0f))
4048 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT | MATERIALFLAG_MODELLIGHT_DIRECTIONAL;
4050 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
4052 if (ent->effects & EF_ADDITIVE)
4053 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
4054 else if (t->currentalpha < 1)
4055 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
4056 if (ent->effects & EF_DOUBLESIDED)
4057 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
4058 if (ent->effects & EF_NODEPTHTEST)
4059 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
4060 if (ent->flags & RENDER_VIEWMODEL)
4061 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
4062 if (t->backgroundnumskinframes && !(t->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
4063 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
4065 // make sure that the waterscroll matrix is used on water surfaces when
4066 // there is no tcmod
4067 if (t->currentmaterialflags & MATERIALFLAG_WATER && r_waterscroll.value != 0)
4068 t->currenttexmatrix = r_waterscrollmatrix;
4070 for (i = 0, tcmod = t->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
4073 switch(tcmod->tcmod)
4077 if (t->currentmaterialflags & MATERIALFLAG_WATER && r_waterscroll.value != 0)
4078 matrix = r_waterscrollmatrix;
4080 matrix = identitymatrix;
4082 case Q3TCMOD_ENTITYTRANSLATE:
4083 // this is used in Q3 to allow the gamecode to control texcoord
4084 // scrolling on the entity, which is not supported in darkplaces yet.
4085 Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
4087 case Q3TCMOD_ROTATE:
4088 Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
4089 Matrix4x4_ConcatRotate(&matrix, tcmod->parms[0] * r_refdef.time, 0, 0, 1);
4090 Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
4093 Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
4095 case Q3TCMOD_SCROLL:
4096 Matrix4x4_CreateTranslate(&matrix, tcmod->parms[0] * r_refdef.time, tcmod->parms[1] * r_refdef.time, 0);
4098 case Q3TCMOD_STRETCH:
4099 f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
4100 Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
4102 case Q3TCMOD_TRANSFORM:
4103 VectorSet(tcmat + 0, tcmod->parms[0], tcmod->parms[1], 0);
4104 VectorSet(tcmat + 3, tcmod->parms[2], tcmod->parms[3], 0);
4105 VectorSet(tcmat + 6, 0 , 0 , 1);
4106 VectorSet(tcmat + 9, tcmod->parms[4], tcmod->parms[5], 0);
4107 Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
4109 case Q3TCMOD_TURBULENT:
4110 // this is handled in the RSurf_PrepareVertices function
4111 matrix = identitymatrix;
4114 // either replace or concatenate the transformation
4116 t->currenttexmatrix = matrix;
4119 matrix4x4_t temp = t->currenttexmatrix;
4120 Matrix4x4_Concat(&t->currenttexmatrix, &matrix, &temp);
4124 t->colormapping = VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f);
4125 t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
4126 t->glosstexture = r_texture_black;
4127 t->backgroundbasetexture = t->backgroundnumskinframes ? ((!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base) : r_texture_white;
4128 t->backgroundglosstexture = r_texture_black;
4129 t->specularpower = r_shadow_glossexponent.value;
4130 // TODO: store reference values for these in the texture?
4131 t->specularscale = 0;
4132 if (r_shadow_gloss.integer > 0)
4134 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
4136 if (r_shadow_glossintensity.value > 0)
4138 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
4139 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
4140 t->specularscale = r_shadow_glossintensity.value;
4143 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
4145 t->glosstexture = r_texture_white;
4146 t->backgroundglosstexture = r_texture_white;
4147 t->specularscale = r_shadow_gloss2intensity.value;
4151 // lightmaps mode looks bad with dlights using actual texturing, so turn
4152 // off the colormap and glossmap, but leave the normalmap on as it still
4153 // accurately represents the shading involved
4154 if (gl_lightmaps.integer && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
4156 t->basetexture = r_texture_white;
4157 t->specularscale = 0;
4160 t->currentpolygonfactor = r_refdef.polygonfactor + t->basepolygonfactor;
4161 t->currentpolygonoffset = r_refdef.polygonoffset + t->basepolygonoffset;
4162 // submodels are biased to avoid z-fighting with world surfaces that they
4163 // may be exactly overlapping (avoids z-fighting artifacts on certain
4164 // doors and things in Quake maps)
4165 if (ent->model->brush.submodel)
4167 t->currentpolygonfactor += r_polygonoffset_submodel_factor.value;
4168 t->currentpolygonoffset += r_polygonoffset_submodel_offset.value;
4171 VectorClear(t->dlightcolor);
4172 t->currentnumlayers = 0;
4173 if (!(t->currentmaterialflags & MATERIALFLAG_NODRAW))
4175 if (!(t->currentmaterialflags & MATERIALFLAG_SKY))
4177 int blendfunc1, blendfunc2, depthmask;
4178 if (t->currentmaterialflags & MATERIALFLAG_ADD)
4180 blendfunc1 = GL_SRC_ALPHA;
4181 blendfunc2 = GL_ONE;
4183 else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
4185 blendfunc1 = GL_SRC_ALPHA;
4186 blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
4188 else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
4190 blendfunc1 = t->customblendfunc[0];
4191 blendfunc2 = t->customblendfunc[1];
4195 blendfunc1 = GL_ONE;
4196 blendfunc2 = GL_ZERO;
4198 depthmask = !(t->currentmaterialflags & MATERIALFLAG_BLENDED);
4199 if (t->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
4201 rtexture_t *currentbasetexture;
4203 if (r_refdef.fogenabled && (t->currentmaterialflags & MATERIALFLAG_BLENDED))
4204 layerflags |= TEXTURELAYERFLAG_FOGDARKEN;
4205 currentbasetexture = (VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) < (1.0f / 1048576.0f) && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
4206 if (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
4208 // fullbright is not affected by r_refdef.lightmapintensity
4209 R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0], ent->colormod[1], ent->colormod[2], t->currentalpha);
4210 if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
4211 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);
4212 if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
4213 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);
4218 // set the color tint used for lights affecting this surface
4219 VectorSet(t->dlightcolor, ent->colormod[0] * t->currentalpha, ent->colormod[1] * t->currentalpha, ent->colormod[2] * t->currentalpha);
4221 // q3bsp has no lightmap updates, so the lightstylevalue that
4222 // would normally be baked into the lightmap must be
4223 // applied to the color
4224 // FIXME: r_glsl 1 rendering doesn't support overbright lightstyles with this (the default light style is not overbright)
4225 if (ent->model->type == mod_brushq3)
4226 colorscale *= r_refdef.rtlightstylevalue[0];
4227 colorscale *= r_refdef.lightmapintensity;
4228 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);
4229 if (r_ambient.value >= (1.0f/64.0f))
4230 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);
4231 if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
4233 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);
4234 if (r_ambient.value >= (1.0f/64.0f))
4235 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);
4237 if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
4239 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);
4240 if (r_ambient.value >= (1.0f/64.0f))
4241 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);
4244 if (t->currentskinframe->glow != NULL)
4245 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);
4246 if (r_refdef.fogenabled && !(t->currentmaterialflags & MATERIALFLAG_ADD))
4248 // if this is opaque use alpha blend which will darken the earlier
4251 // if this is an alpha blended material, all the earlier passes
4252 // were darkened by fog already, so we only need to add the fog
4253 // color ontop through the fog mask texture
4255 // if this is an additive blended material, all the earlier passes
4256 // were darkened by fog already, and we should not add fog color
4257 // (because the background was not darkened, there is no fog color
4258 // that was lost behind it).
4259 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);
4266 void R_UpdateAllTextureInfo(entity_render_t *ent)
4270 for (i = 0;i < ent->model->num_texturesperskin;i++)
4271 R_UpdateTextureInfo(ent, ent->model->data_textures + i);
4274 rsurfacestate_t rsurface;
4276 void R_Mesh_ResizeArrays(int newvertices)
4279 if (rsurface.array_size >= newvertices)
4281 if (rsurface.array_modelvertex3f)
4282 Mem_Free(rsurface.array_modelvertex3f);
4283 rsurface.array_size = (newvertices + 1023) & ~1023;
4284 base = (float *)Mem_Alloc(r_main_mempool, rsurface.array_size * sizeof(float[33]));
4285 rsurface.array_modelvertex3f = base + rsurface.array_size * 0;
4286 rsurface.array_modelsvector3f = base + rsurface.array_size * 3;
4287 rsurface.array_modeltvector3f = base + rsurface.array_size * 6;
4288 rsurface.array_modelnormal3f = base + rsurface.array_size * 9;
4289 rsurface.array_deformedvertex3f = base + rsurface.array_size * 12;
4290 rsurface.array_deformedsvector3f = base + rsurface.array_size * 15;
4291 rsurface.array_deformedtvector3f = base + rsurface.array_size * 18;
4292 rsurface.array_deformednormal3f = base + rsurface.array_size * 21;
4293 rsurface.array_texcoord3f = base + rsurface.array_size * 24;
4294 rsurface.array_color4f = base + rsurface.array_size * 27;
4295 rsurface.array_generatedtexcoordtexture2f = base + rsurface.array_size * 31;
4298 void RSurf_CleanUp(void)
4301 if (rsurface.mode == RSURFMODE_GLSL)
4303 qglUseProgramObjectARB(0);CHECKGLERROR
4305 GL_AlphaTest(false);
4306 rsurface.mode = RSURFMODE_NONE;
4307 rsurface.uselightmaptexture = false;
4308 rsurface.texture = NULL;
4311 void RSurf_ActiveWorldEntity(void)
4313 model_t *model = r_refdef.worldmodel;
4315 if (rsurface.array_size < model->surfmesh.num_vertices)
4316 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
4317 rsurface.matrix = identitymatrix;
4318 rsurface.inversematrix = identitymatrix;
4319 R_Mesh_Matrix(&identitymatrix);
4320 VectorCopy(r_view.origin, rsurface.modelorg);
4321 VectorSet(rsurface.modellight_ambient, 0, 0, 0);
4322 VectorSet(rsurface.modellight_diffuse, 0, 0, 0);
4323 VectorSet(rsurface.modellight_lightdir, 0, 0, 1);
4324 VectorSet(rsurface.colormap_pantscolor, 0, 0, 0);
4325 VectorSet(rsurface.colormap_shirtcolor, 0, 0, 0);
4326 rsurface.frameblend[0].frame = 0;
4327 rsurface.frameblend[0].lerp = 1;
4328 rsurface.frameblend[1].frame = 0;
4329 rsurface.frameblend[1].lerp = 0;
4330 rsurface.frameblend[2].frame = 0;
4331 rsurface.frameblend[2].lerp = 0;
4332 rsurface.frameblend[3].frame = 0;
4333 rsurface.frameblend[3].lerp = 0;
4334 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
4335 rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
4336 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
4337 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
4338 rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
4339 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
4340 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
4341 rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
4342 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
4343 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
4344 rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
4345 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
4346 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
4347 rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
4348 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
4349 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
4350 rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
4351 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
4352 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
4353 rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
4354 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
4355 rsurface.modelelement3i = model->surfmesh.data_element3i;
4356 rsurface.modelelement3i_bufferobject = model->surfmesh.ebo;
4357 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
4358 rsurface.modelnum_vertices = model->surfmesh.num_vertices;
4359 rsurface.modelnum_triangles = model->surfmesh.num_triangles;
4360 rsurface.modelsurfaces = model->data_surfaces;
4361 rsurface.generatedvertex = false;
4362 rsurface.vertex3f = rsurface.modelvertex3f;
4363 rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4364 rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4365 rsurface.svector3f = rsurface.modelsvector3f;
4366 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4367 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4368 rsurface.tvector3f = rsurface.modeltvector3f;
4369 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4370 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4371 rsurface.normal3f = rsurface.modelnormal3f;
4372 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4373 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4374 rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4377 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
4379 model_t *model = ent->model;
4381 if (rsurface.array_size < model->surfmesh.num_vertices)
4382 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
4383 rsurface.matrix = ent->matrix;
4384 rsurface.inversematrix = ent->inversematrix;
4385 R_Mesh_Matrix(&rsurface.matrix);
4386 Matrix4x4_Transform(&rsurface.inversematrix, r_view.origin, rsurface.modelorg);
4387 VectorCopy(ent->modellight_ambient, rsurface.modellight_ambient);
4388 VectorCopy(ent->modellight_diffuse, rsurface.modellight_diffuse);
4389 VectorCopy(ent->modellight_lightdir, rsurface.modellight_lightdir);
4390 VectorCopy(ent->colormap_pantscolor, rsurface.colormap_pantscolor);
4391 VectorCopy(ent->colormap_shirtcolor, rsurface.colormap_shirtcolor);
4392 rsurface.frameblend[0] = ent->frameblend[0];
4393 rsurface.frameblend[1] = ent->frameblend[1];
4394 rsurface.frameblend[2] = ent->frameblend[2];
4395 rsurface.frameblend[3] = ent->frameblend[3];
4396 if (model->surfmesh.isanimated && (rsurface.frameblend[0].lerp != 1 || rsurface.frameblend[0].frame != 0))
4400 rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4401 rsurface.modelsvector3f = rsurface.array_modelsvector3f;
4402 rsurface.modeltvector3f = rsurface.array_modeltvector3f;
4403 rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4404 Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, rsurface.array_modelsvector3f, rsurface.array_modeltvector3f);
4406 else if (wantnormals)
4408 rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4409 rsurface.modelsvector3f = NULL;
4410 rsurface.modeltvector3f = NULL;
4411 rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4412 Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, NULL, NULL);
4416 rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4417 rsurface.modelsvector3f = NULL;
4418 rsurface.modeltvector3f = NULL;
4419 rsurface.modelnormal3f = NULL;
4420 Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, NULL, NULL, NULL);
4422 rsurface.modelvertex3f_bufferobject = 0;
4423 rsurface.modelvertex3f_bufferoffset = 0;
4424 rsurface.modelsvector3f_bufferobject = 0;
4425 rsurface.modelsvector3f_bufferoffset = 0;
4426 rsurface.modeltvector3f_bufferobject = 0;
4427 rsurface.modeltvector3f_bufferoffset = 0;
4428 rsurface.modelnormal3f_bufferobject = 0;
4429 rsurface.modelnormal3f_bufferoffset = 0;
4430 rsurface.generatedvertex = true;
4434 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
4435 rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
4436 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
4437 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
4438 rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
4439 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
4440 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
4441 rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
4442 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
4443 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
4444 rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
4445 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
4446 rsurface.generatedvertex = false;
4448 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
4449 rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
4450 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
4451 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
4452 rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
4453 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
4454 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
4455 rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
4456 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
4457 rsurface.modelelement3i = model->surfmesh.data_element3i;
4458 rsurface.modelelement3i_bufferobject = model->surfmesh.ebo;
4459 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
4460 rsurface.modelnum_vertices = model->surfmesh.num_vertices;
4461 rsurface.modelnum_triangles = model->surfmesh.num_triangles;
4462 rsurface.modelsurfaces = model->data_surfaces;
4463 rsurface.vertex3f = rsurface.modelvertex3f;
4464 rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4465 rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4466 rsurface.svector3f = rsurface.modelsvector3f;
4467 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4468 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4469 rsurface.tvector3f = rsurface.modeltvector3f;
4470 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4471 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4472 rsurface.normal3f = rsurface.modelnormal3f;
4473 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4474 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4475 rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4478 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
4479 void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist)
4482 int texturesurfaceindex;
4487 const float *v1, *in_tc;
4489 float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
4491 q3shaderinfo_deform_t *deform;
4492 // 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
4493 if (rsurface.generatedvertex)
4495 if (rsurface.texture->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
4496 generatenormals = true;
4497 for (i = 0;i < Q3MAXDEFORMS;i++)
4499 if (rsurface.texture->deforms[i].deform == Q3DEFORM_AUTOSPRITE)
4501 generatetangents = true;
4502 generatenormals = true;
4504 if (rsurface.texture->deforms[i].deform != Q3DEFORM_NONE)
4505 generatenormals = true;
4507 if (generatenormals && !rsurface.modelnormal3f)
4509 rsurface.normal3f = rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4510 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject = 0;
4511 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset = 0;
4512 Mod_BuildNormals(0, rsurface.modelnum_vertices, rsurface.modelnum_triangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.array_modelnormal3f, r_smoothnormals_areaweighting.integer);
4514 if (generatetangents && !rsurface.modelsvector3f)
4516 rsurface.svector3f = rsurface.modelsvector3f = rsurface.array_modelsvector3f;
4517 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject = 0;
4518 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset = 0;
4519 rsurface.tvector3f = rsurface.modeltvector3f = rsurface.array_modeltvector3f;
4520 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject = 0;
4521 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset = 0;
4522 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);
4525 rsurface.vertex3f = rsurface.modelvertex3f;
4526 rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4527 rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4528 rsurface.svector3f = rsurface.modelsvector3f;
4529 rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4530 rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4531 rsurface.tvector3f = rsurface.modeltvector3f;
4532 rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4533 rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4534 rsurface.normal3f = rsurface.modelnormal3f;
4535 rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4536 rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4537 // if vertices are deformed (sprite flares and things in maps, possibly
4538 // water waves, bulges and other deformations), generate them into
4539 // rsurface.deform* arrays from whatever the rsurface.* arrays point to
4540 // (may be static model data or generated data for an animated model, or
4541 // the previous deform pass)
4542 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform;deformindex++, deform++)
4544 switch (deform->deform)
4547 case Q3DEFORM_PROJECTIONSHADOW:
4548 case Q3DEFORM_TEXT0:
4549 case Q3DEFORM_TEXT1:
4550 case Q3DEFORM_TEXT2:
4551 case Q3DEFORM_TEXT3:
4552 case Q3DEFORM_TEXT4:
4553 case Q3DEFORM_TEXT5:
4554 case Q3DEFORM_TEXT6:
4555 case Q3DEFORM_TEXT7:
4558 case Q3DEFORM_AUTOSPRITE:
4559 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, newforward);
4560 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.right, newright);
4561 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.up, newup);
4562 VectorNormalize(newforward);
4563 VectorNormalize(newright);
4564 VectorNormalize(newup);
4565 // make deformed versions of only the model vertices used by the specified surfaces
4566 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4568 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4569 // a single autosprite surface can contain multiple sprites...
4570 for (j = 0;j < surface->num_vertices - 3;j += 4)
4572 VectorClear(center);
4573 for (i = 0;i < 4;i++)
4574 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
4575 VectorScale(center, 0.25f, center);
4576 VectorCopy((rsurface.normal3f + 3 * surface->num_firstvertex) + j*3, forward);
4577 VectorCopy((rsurface.svector3f + 3 * surface->num_firstvertex) + j*3, right);
4578 VectorCopy((rsurface.tvector3f + 3 * surface->num_firstvertex) + j*3, up);
4579 for (i = 0;i < 4;i++)
4581 VectorSubtract((rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i)*3, center, v);
4582 VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
4585 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);
4586 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);
4588 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4589 rsurface.vertex3f_bufferobject = 0;
4590 rsurface.vertex3f_bufferoffset = 0;
4591 rsurface.svector3f = rsurface.array_deformedsvector3f;
4592 rsurface.svector3f_bufferobject = 0;
4593 rsurface.svector3f_bufferoffset = 0;
4594 rsurface.tvector3f = rsurface.array_deformedtvector3f;
4595 rsurface.tvector3f_bufferobject = 0;
4596 rsurface.tvector3f_bufferoffset = 0;
4597 rsurface.normal3f = rsurface.array_deformednormal3f;
4598 rsurface.normal3f_bufferobject = 0;
4599 rsurface.normal3f_bufferoffset = 0;
4601 case Q3DEFORM_AUTOSPRITE2:
4602 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, newforward);
4603 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.right, newright);
4604 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.up, newup);
4605 VectorNormalize(newforward);
4606 VectorNormalize(newright);
4607 VectorNormalize(newup);
4608 // make deformed versions of only the model vertices used by the specified surfaces
4609 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4611 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4612 const float *v1, *v2;
4622 memset(shortest, 0, sizeof(shortest));
4623 // a single autosprite surface can contain multiple sprites...
4624 for (j = 0;j < surface->num_vertices - 3;j += 4)
4626 VectorClear(center);
4627 for (i = 0;i < 4;i++)
4628 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
4629 VectorScale(center, 0.25f, center);
4630 // find the two shortest edges, then use them to define the
4631 // axis vectors for rotating around the central axis
4632 for (i = 0;i < 6;i++)
4634 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][0]);
4635 v2 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][1]);
4637 Debug_PolygonBegin(NULL, 0, false, 0);
4638 Debug_PolygonVertex(v1[0], v1[1], v1[2], 0, 0, 1, 0, 0, 1);
4639 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);
4640 Debug_PolygonVertex(v2[0], v2[1], v2[2], 0, 0, 1, 0, 0, 1);
4643 l = VectorDistance2(v1, v2);
4644 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
4646 l += (1.0f / 1024.0f);
4647 if (shortest[0].length2 > l || i == 0)
4649 shortest[1] = shortest[0];
4650 shortest[0].length2 = l;
4651 shortest[0].v1 = v1;
4652 shortest[0].v2 = v2;
4654 else if (shortest[1].length2 > l || i == 1)
4656 shortest[1].length2 = l;
4657 shortest[1].v1 = v1;
4658 shortest[1].v2 = v2;
4661 VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
4662 VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
4664 Debug_PolygonBegin(NULL, 0, false, 0);
4665 Debug_PolygonVertex(start[0], start[1], start[2], 0, 0, 1, 1, 0, 1);
4666 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);
4667 Debug_PolygonVertex(end[0], end[1], end[2], 0, 0, 0, 1, 1, 1);
4670 // this calculates the right vector from the shortest edge
4671 // and the up vector from the edge midpoints
4672 VectorSubtract(shortest[0].v1, shortest[0].v2, right);
4673 VectorNormalize(right);
4674 VectorSubtract(end, start, up);
4675 VectorNormalize(up);
4676 // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
4677 //VectorSubtract(rsurface.modelorg, center, forward);
4678 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, forward);
4679 VectorNegate(forward, forward);
4680 VectorReflect(forward, 0, up, forward);
4681 VectorNormalize(forward);
4682 CrossProduct(up, forward, newright);
4683 VectorNormalize(newright);
4685 Debug_PolygonBegin(NULL, 0, false, 0);
4686 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);
4687 Debug_PolygonVertex(center[0] + right[0] * 8, center[1] + right[1] * 8, center[2] + right[2] * 8, 0, 0, 0, 1, 0, 1);
4688 Debug_PolygonVertex(center[0] + up [0] * 8, center[1] + up [1] * 8, center[2] + up [2] * 8, 0, 0, 0, 0, 1, 1);
4692 Debug_PolygonBegin(NULL, 0, false, 0);
4693 Debug_PolygonVertex(center[0] + forward [0] * 8, center[1] + forward [1] * 8, center[2] + forward [2] * 8, 0, 0, 1, 0, 0, 1);
4694 Debug_PolygonVertex(center[0] + newright[0] * 8, center[1] + newright[1] * 8, center[2] + newright[2] * 8, 0, 0, 0, 1, 0, 1);
4695 Debug_PolygonVertex(center[0] + up [0] * 8, center[1] + up [1] * 8, center[2] + up [2] * 8, 0, 0, 0, 0, 1, 1);
4698 // rotate the quad around the up axis vector, this is made
4699 // especially easy by the fact we know the quad is flat,
4700 // so we only have to subtract the center position and
4701 // measure distance along the right vector, and then
4702 // multiply that by the newright vector and add back the
4704 // we also need to subtract the old position to undo the
4705 // displacement from the center, which we do with a
4706 // DotProduct, the subtraction/addition of center is also
4707 // optimized into DotProducts here
4708 l = DotProduct(right, center);
4709 for (i = 0;i < 4;i++)
4711 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + j + i);
4712 f = DotProduct(right, v1) - l;
4713 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
4716 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);
4717 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);
4719 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4720 rsurface.vertex3f_bufferobject = 0;
4721 rsurface.vertex3f_bufferoffset = 0;
4722 rsurface.svector3f = rsurface.array_deformedsvector3f;
4723 rsurface.svector3f_bufferobject = 0;
4724 rsurface.svector3f_bufferoffset = 0;
4725 rsurface.tvector3f = rsurface.array_deformedtvector3f;
4726 rsurface.tvector3f_bufferobject = 0;
4727 rsurface.tvector3f_bufferoffset = 0;
4728 rsurface.normal3f = rsurface.array_deformednormal3f;
4729 rsurface.normal3f_bufferobject = 0;
4730 rsurface.normal3f_bufferoffset = 0;
4732 case Q3DEFORM_NORMAL:
4733 // deform the normals to make reflections wavey
4734 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4736 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4737 for (j = 0;j < surface->num_vertices;j++)
4740 float *normal = (rsurface.array_deformednormal3f + 3 * surface->num_firstvertex) + j*3;
4741 VectorScale((rsurface.vertex3f + 3 * surface->num_firstvertex) + j*3, 0.98f, vertex);
4742 VectorCopy((rsurface.normal3f + 3 * surface->num_firstvertex) + j*3, normal);
4743 normal[0] += deform->parms[0] * noise4f( vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4744 normal[1] += deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4745 normal[2] += deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4746 VectorNormalize(normal);
4748 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);
4750 rsurface.svector3f = rsurface.array_deformedsvector3f;
4751 rsurface.svector3f_bufferobject = 0;
4752 rsurface.svector3f_bufferoffset = 0;
4753 rsurface.tvector3f = rsurface.array_deformedtvector3f;
4754 rsurface.tvector3f_bufferobject = 0;
4755 rsurface.tvector3f_bufferoffset = 0;
4756 rsurface.normal3f = rsurface.array_deformednormal3f;
4757 rsurface.normal3f_bufferobject = 0;
4758 rsurface.normal3f_bufferoffset = 0;
4761 // deform vertex array to make wavey water and flags and such
4762 waveparms[0] = deform->waveparms[0];
4763 waveparms[1] = deform->waveparms[1];
4764 waveparms[2] = deform->waveparms[2];
4765 waveparms[3] = deform->waveparms[3];
4766 // this is how a divisor of vertex influence on deformation
4767 animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
4768 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
4769 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4771 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4772 for (j = 0;j < surface->num_vertices;j++)
4774 float *vertex = (rsurface.array_deformedvertex3f + 3 * surface->num_firstvertex) + j*3;
4775 VectorCopy((rsurface.vertex3f + 3 * surface->num_firstvertex) + j*3, vertex);
4776 // if the wavefunc depends on time, evaluate it per-vertex
4779 waveparms[2] = deform->waveparms[2] + (vertex[0] + vertex[1] + vertex[2]) * animpos;
4780 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
4782 VectorMA(vertex, scale, (rsurface.normal3f + 3 * surface->num_firstvertex) + j*3, vertex);
4785 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4786 rsurface.vertex3f_bufferobject = 0;
4787 rsurface.vertex3f_bufferoffset = 0;
4789 case Q3DEFORM_BULGE:
4790 // deform vertex array to make the surface have moving bulges
4791 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4793 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4794 for (j = 0;j < surface->num_vertices;j++)
4796 scale = sin((rsurface.modeltexcoordtexture2f[2 * (surface->num_firstvertex + j)] * deform->parms[0] + r_refdef.time * deform->parms[2])) * deform->parms[1];
4797 VectorMA(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), scale, rsurface.normal3f + 3 * (surface->num_firstvertex + j), rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
4800 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4801 rsurface.vertex3f_bufferobject = 0;
4802 rsurface.vertex3f_bufferoffset = 0;
4805 // deform vertex array
4806 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
4807 VectorScale(deform->parms, scale, waveparms);
4808 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4810 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4811 for (j = 0;j < surface->num_vertices;j++)
4812 VectorAdd(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), waveparms, rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
4814 rsurface.vertex3f = rsurface.array_deformedvertex3f;
4815 rsurface.vertex3f_bufferobject = 0;
4816 rsurface.vertex3f_bufferoffset = 0;
4820 // generate texcoords based on the chosen texcoord source
4821 switch(rsurface.texture->tcgen.tcgen)
4824 case Q3TCGEN_TEXTURE:
4825 rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4826 rsurface.texcoordtexture2f_bufferobject = rsurface.modeltexcoordtexture2f_bufferobject;
4827 rsurface.texcoordtexture2f_bufferoffset = rsurface.modeltexcoordtexture2f_bufferoffset;
4829 case Q3TCGEN_LIGHTMAP:
4830 rsurface.texcoordtexture2f = rsurface.modeltexcoordlightmap2f;
4831 rsurface.texcoordtexture2f_bufferobject = rsurface.modeltexcoordlightmap2f_bufferobject;
4832 rsurface.texcoordtexture2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
4834 case Q3TCGEN_VECTOR:
4835 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4837 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4838 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)
4840 out_tc[0] = DotProduct(v1, rsurface.texture->tcgen.parms);
4841 out_tc[1] = DotProduct(v1, rsurface.texture->tcgen.parms + 3);
4844 rsurface.texcoordtexture2f = rsurface.array_generatedtexcoordtexture2f;
4845 rsurface.texcoordtexture2f_bufferobject = 0;
4846 rsurface.texcoordtexture2f_bufferoffset = 0;
4848 case Q3TCGEN_ENVIRONMENT:
4849 // make environment reflections using a spheremap
4850 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4852 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4853 const float *vertex = rsurface.modelvertex3f + 3 * surface->num_firstvertex;
4854 const float *normal = rsurface.modelnormal3f + 3 * surface->num_firstvertex;
4855 float *out_tc = rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex;
4856 for (j = 0;j < surface->num_vertices;j++, vertex += 3, normal += 3, out_tc += 2)
4858 float l, d, eyedir[3];
4859 VectorSubtract(rsurface.modelorg, vertex, eyedir);
4860 l = 0.5f / VectorLength(eyedir);
4861 d = DotProduct(normal, eyedir)*2;
4862 out_tc[0] = 0.5f + (normal[1]*d - eyedir[1])*l;
4863 out_tc[1] = 0.5f - (normal[2]*d - eyedir[2])*l;
4866 rsurface.texcoordtexture2f = rsurface.array_generatedtexcoordtexture2f;
4867 rsurface.texcoordtexture2f_bufferobject = 0;
4868 rsurface.texcoordtexture2f_bufferoffset = 0;
4871 // the only tcmod that needs software vertex processing is turbulent, so
4872 // check for it here and apply the changes if needed
4873 // and we only support that as the first one
4874 // (handling a mixture of turbulent and other tcmods would be problematic
4875 // without punting it entirely to a software path)
4876 if (rsurface.texture->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
4878 amplitude = rsurface.texture->tcmods[0].parms[1];
4879 animpos = rsurface.texture->tcmods[0].parms[2] + r_refdef.time * rsurface.texture->tcmods[0].parms[3];
4880 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4882 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4883 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)
4885 out_tc[0] = in_tc[0] + amplitude * sin(((v1[0] + v1[2]) * 1.0 / 1024.0f + animpos) * M_PI * 2);
4886 out_tc[1] = in_tc[1] + amplitude * sin(((v1[1] ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
4889 rsurface.texcoordtexture2f = rsurface.array_generatedtexcoordtexture2f;
4890 rsurface.texcoordtexture2f_bufferobject = 0;
4891 rsurface.texcoordtexture2f_bufferoffset = 0;
4893 rsurface.texcoordlightmap2f = rsurface.modeltexcoordlightmap2f;
4894 rsurface.texcoordlightmap2f_bufferobject = rsurface.modeltexcoordlightmap2f_bufferobject;
4895 rsurface.texcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
4896 R_Mesh_VertexPointer(rsurface.vertex3f, rsurface.vertex3f_bufferobject, rsurface.vertex3f_bufferoffset);
4899 void RSurf_DrawBatch_Simple(int texturenumsurfaces, msurface_t **texturesurfacelist)
4902 const msurface_t *surface = texturesurfacelist[0];
4903 const msurface_t *surface2;
4908 // TODO: lock all array ranges before render, rather than on each surface
4909 if (texturenumsurfaces == 1)
4911 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
4912 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));
4914 else if (r_batchmode.integer == 2)
4916 #define MAXBATCHTRIANGLES 4096
4917 int batchtriangles = 0;
4918 int batchelements[MAXBATCHTRIANGLES*3];
4919 for (i = 0;i < texturenumsurfaces;i = j)
4921 surface = texturesurfacelist[i];
4923 if (surface->num_triangles > MAXBATCHTRIANGLES)
4925 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 memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
4929 batchtriangles = surface->num_triangles;
4930 firstvertex = surface->num_firstvertex;
4931 endvertex = surface->num_firstvertex + surface->num_vertices;
4932 for (;j < texturenumsurfaces;j++)
4934 surface2 = texturesurfacelist[j];
4935 if (batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
4937 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
4938 batchtriangles += surface2->num_triangles;
4939 firstvertex = min(firstvertex, surface2->num_firstvertex);
4940 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
4942 surface2 = texturesurfacelist[j-1];
4943 numvertices = endvertex - firstvertex;
4944 R_Mesh_Draw(firstvertex, numvertices, batchtriangles, batchelements, 0, 0);
4947 else if (r_batchmode.integer == 1)
4949 for (i = 0;i < texturenumsurfaces;i = j)
4951 surface = texturesurfacelist[i];
4952 for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
4953 if (texturesurfacelist[j] != surface2)
4955 surface2 = texturesurfacelist[j-1];
4956 numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
4957 numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
4958 GL_LockArrays(surface->num_firstvertex, numvertices);
4959 R_Mesh_Draw(surface->num_firstvertex, numvertices, numtriangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4964 for (i = 0;i < texturenumsurfaces;i++)
4966 surface = texturesurfacelist[i];
4967 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
4968 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));
4973 static void RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit, int refractiontexunit, int reflectiontexunit)
4975 int i, planeindex, vertexindex;
4979 r_waterstate_waterplane_t *p, *bestp;
4980 msurface_t *surface;
4981 if (r_waterstate.renderingscene)
4983 for (i = 0;i < texturenumsurfaces;i++)
4985 surface = texturesurfacelist[i];
4986 if (lightmaptexunit >= 0)
4987 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
4988 if (deluxemaptexunit >= 0)
4989 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
4990 // pick the closest matching water plane
4993 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
4996 for (vertexindex = 0, v = rsurface.modelvertex3f + surface->num_firstvertex * 3;vertexindex < surface->num_vertices;vertexindex++, v += 3)
4998 Matrix4x4_Transform(&rsurface.matrix, v, vert);
4999 d += fabs(PlaneDiff(vert, &p->plane));
5001 if (bestd > d || !bestp)
5009 if (refractiontexunit >= 0)
5010 R_Mesh_TexBind(refractiontexunit, R_GetTexture(bestp->texture_refraction));
5011 if (reflectiontexunit >= 0)
5012 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(bestp->texture_reflection));
5016 if (refractiontexunit >= 0)
5017 R_Mesh_TexBind(refractiontexunit, R_GetTexture(r_texture_black));
5018 if (reflectiontexunit >= 0)
5019 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(r_texture_black));
5021 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5022 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));
5026 static void RSurf_DrawBatch_WithLightmapSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit)
5030 const msurface_t *surface = texturesurfacelist[0];
5031 const msurface_t *surface2;
5036 // TODO: lock all array ranges before render, rather than on each surface
5037 if (texturenumsurfaces == 1)
5039 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5040 if (deluxemaptexunit >= 0)
5041 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5042 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5043 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));
5045 else if (r_batchmode.integer == 2)
5047 #define MAXBATCHTRIANGLES 4096
5048 int batchtriangles = 0;
5049 int batchelements[MAXBATCHTRIANGLES*3];
5050 for (i = 0;i < texturenumsurfaces;i = j)
5052 surface = texturesurfacelist[i];
5053 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5054 if (deluxemaptexunit >= 0)
5055 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5057 if (surface->num_triangles > MAXBATCHTRIANGLES)
5059 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));
5062 memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
5063 batchtriangles = surface->num_triangles;
5064 firstvertex = surface->num_firstvertex;
5065 endvertex = surface->num_firstvertex + surface->num_vertices;
5066 for (;j < texturenumsurfaces;j++)
5068 surface2 = texturesurfacelist[j];
5069 if (surface2->lightmaptexture != surface->lightmaptexture || batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
5071 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
5072 batchtriangles += surface2->num_triangles;
5073 firstvertex = min(firstvertex, surface2->num_firstvertex);
5074 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
5076 surface2 = texturesurfacelist[j-1];
5077 numvertices = endvertex - firstvertex;
5078 R_Mesh_Draw(firstvertex, numvertices, batchtriangles, batchelements, 0, 0);
5081 else if (r_batchmode.integer == 1)
5084 Con_Printf("%s batch sizes ignoring lightmap:", rsurface.texture->name);
5085 for (i = 0;i < texturenumsurfaces;i = j)
5087 surface = texturesurfacelist[i];
5088 for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
5089 if (texturesurfacelist[j] != surface2)
5091 Con_Printf(" %i", j - i);
5094 Con_Printf("%s batch sizes honoring lightmap:", rsurface.texture->name);
5096 for (i = 0;i < texturenumsurfaces;i = j)
5098 surface = texturesurfacelist[i];
5099 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5100 if (deluxemaptexunit >= 0)
5101 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5102 for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
5103 if (texturesurfacelist[j] != surface2 || texturesurfacelist[j]->lightmaptexture != surface->lightmaptexture)
5106 Con_Printf(" %i", j - i);
5108 surface2 = texturesurfacelist[j-1];
5109 numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
5110 numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
5111 GL_LockArrays(surface->num_firstvertex, numvertices);
5112 R_Mesh_Draw(surface->num_firstvertex, numvertices, numtriangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5120 for (i = 0;i < texturenumsurfaces;i++)
5122 surface = texturesurfacelist[i];
5123 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5124 if (deluxemaptexunit >= 0)
5125 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5126 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5127 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));
5132 static void RSurf_DrawBatch_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
5135 int texturesurfaceindex;
5136 if (r_showsurfaces.integer == 2)
5138 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5140 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5141 for (j = 0;j < surface->num_triangles;j++)
5143 float f = ((j + surface->num_firsttriangle) & 31) * (1.0f / 31.0f) * r_view.colorscale;
5144 GL_Color(f, f, f, 1);
5145 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)));
5151 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5153 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5154 int k = (int)(((size_t)surface) / sizeof(msurface_t));
5155 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);
5156 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5157 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));
5162 static void RSurf_DrawBatch_GL11_ApplyFog(int texturenumsurfaces, msurface_t **texturesurfacelist)
5164 int texturesurfaceindex;
5168 if (rsurface.lightmapcolor4f)
5170 // generate color arrays for the surfaces in this list
5171 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5173 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5174 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)
5176 f = FogPoint_Model(v);
5186 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5188 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5189 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)
5191 f = FogPoint_Model(v);
5199 rsurface.lightmapcolor4f = rsurface.array_color4f;
5200 rsurface.lightmapcolor4f_bufferobject = 0;
5201 rsurface.lightmapcolor4f_bufferoffset = 0;
5204 static void RSurf_DrawBatch_GL11_ApplyColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a)
5206 int texturesurfaceindex;
5209 if (!rsurface.lightmapcolor4f)
5211 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5213 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5214 for (i = 0, c = (rsurface.lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4, c2 += 4)
5222 rsurface.lightmapcolor4f = rsurface.array_color4f;
5223 rsurface.lightmapcolor4f_bufferobject = 0;
5224 rsurface.lightmapcolor4f_bufferoffset = 0;
5227 static void RSurf_DrawBatch_GL11_Lightmap(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5230 rsurface.lightmapcolor4f = NULL;
5231 rsurface.lightmapcolor4f_bufferobject = 0;
5232 rsurface.lightmapcolor4f_bufferoffset = 0;
5233 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5234 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5235 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5236 GL_Color(r, g, b, a);
5237 RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 0, -1);
5240 static void RSurf_DrawBatch_GL11_Unlit(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5242 // TODO: optimize applyfog && applycolor case
5243 // just apply fog if necessary, and tint the fog color array if necessary
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_Simple(texturenumsurfaces, texturesurfacelist);
5254 static void RSurf_DrawBatch_GL11_VertexColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5256 int texturesurfaceindex;
5260 if (texturesurfacelist[0]->lightmapinfo && texturesurfacelist[0]->lightmapinfo->stainsamples)
5262 // generate color arrays for the surfaces in this list
5263 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5265 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5266 for (i = 0, c = rsurface.array_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
5268 if (surface->lightmapinfo->samples)
5270 const unsigned char *lm = surface->lightmapinfo->samples + (rsurface.modellightmapoffsets + surface->num_firstvertex)[i];
5271 float scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
5272 VectorScale(lm, scale, c);
5273 if (surface->lightmapinfo->styles[1] != 255)
5275 int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
5277 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
5278 VectorMA(c, scale, lm, c);
5279 if (surface->lightmapinfo->styles[2] != 255)
5282 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
5283 VectorMA(c, scale, lm, c);
5284 if (surface->lightmapinfo->styles[3] != 255)
5287 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
5288 VectorMA(c, scale, lm, c);
5298 rsurface.lightmapcolor4f = rsurface.array_color4f;
5299 rsurface.lightmapcolor4f_bufferobject = 0;
5300 rsurface.lightmapcolor4f_bufferoffset = 0;
5304 rsurface.lightmapcolor4f = rsurface.modellightmapcolor4f;
5305 rsurface.lightmapcolor4f_bufferobject = rsurface.modellightmapcolor4f_bufferobject;
5306 rsurface.lightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
5308 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5309 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5310 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5311 GL_Color(r, g, b, a);
5312 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5315 static void RSurf_DrawBatch_GL11_VertexShade(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5317 int texturesurfaceindex;
5321 vec3_t ambientcolor;
5322 vec3_t diffusecolor;
5326 VectorCopy(rsurface.modellight_lightdir, lightdir);
5327 f = 0.5f * r_refdef.lightmapintensity;
5328 ambientcolor[0] = rsurface.modellight_ambient[0] * r * f;
5329 ambientcolor[1] = rsurface.modellight_ambient[1] * g * f;
5330 ambientcolor[2] = rsurface.modellight_ambient[2] * b * f;
5331 diffusecolor[0] = rsurface.modellight_diffuse[0] * r * f;
5332 diffusecolor[1] = rsurface.modellight_diffuse[1] * g * f;
5333 diffusecolor[2] = rsurface.modellight_diffuse[2] * b * f;
5334 if (VectorLength2(diffusecolor) > 0)
5336 // generate color arrays for the surfaces in this list
5337 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5339 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5340 int numverts = surface->num_vertices;
5341 v = rsurface.vertex3f + 3 * surface->num_firstvertex;
5342 c2 = rsurface.normal3f + 3 * surface->num_firstvertex;
5343 c = rsurface.array_color4f + 4 * surface->num_firstvertex;
5344 // q3-style directional shading
5345 for (i = 0;i < numverts;i++, v += 3, c2 += 3, c += 4)
5347 if ((f = DotProduct(c2, lightdir)) > 0)
5348 VectorMA(ambientcolor, f, diffusecolor, c);
5350 VectorCopy(ambientcolor, c);
5359 rsurface.lightmapcolor4f = rsurface.array_color4f;
5360 rsurface.lightmapcolor4f_bufferobject = 0;
5361 rsurface.lightmapcolor4f_bufferoffset = 0;
5365 r = ambientcolor[0];
5366 g = ambientcolor[1];
5367 b = ambientcolor[2];
5368 rsurface.lightmapcolor4f = NULL;
5369 rsurface.lightmapcolor4f_bufferobject = 0;
5370 rsurface.lightmapcolor4f_bufferoffset = 0;
5372 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5373 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5374 R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5375 GL_Color(r, g, b, a);
5376 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5379 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
5381 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5382 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5383 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5384 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5385 if (rsurface.mode != RSURFMODE_SHOWSURFACES)
5387 rsurface.mode = RSURFMODE_SHOWSURFACES;
5389 GL_BlendFunc(GL_ONE, GL_ZERO);
5390 R_Mesh_ColorPointer(NULL, 0, 0);
5391 R_Mesh_ResetTextureState();
5393 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5394 RSurf_DrawBatch_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
5397 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, msurface_t **texturesurfacelist)
5399 // transparent sky would be ridiculous
5400 if ((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
5402 if (rsurface.mode != RSURFMODE_SKY)
5404 if (rsurface.mode == RSURFMODE_GLSL)
5406 qglUseProgramObjectARB(0);CHECKGLERROR
5408 rsurface.mode = RSURFMODE_SKY;
5412 skyrendernow = false;
5414 // restore entity matrix
5415 R_Mesh_Matrix(&rsurface.matrix);
5417 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5418 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5419 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5420 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5422 // LordHavoc: HalfLife maps have freaky skypolys so don't use
5423 // skymasking on them, and Quake3 never did sky masking (unlike
5424 // software Quake and software Quake2), so disable the sky masking
5425 // in Quake3 maps as it causes problems with q3map2 sky tricks,
5426 // and skymasking also looks very bad when noclipping outside the
5427 // level, so don't use it then either.
5428 if (r_refdef.worldmodel && r_refdef.worldmodel->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_viewcache.world_novis)
5430 GL_Color(r_refdef.fogcolor[0] * r_view.colorscale, r_refdef.fogcolor[1] * r_view.colorscale, r_refdef.fogcolor[2] * r_view.colorscale, 1);
5431 R_Mesh_ColorPointer(NULL, 0, 0);
5432 R_Mesh_ResetTextureState();
5433 if (skyrendermasked)
5435 // depth-only (masking)
5436 GL_ColorMask(0,0,0,0);
5437 // just to make sure that braindead drivers don't draw
5438 // anything despite that colormask...
5439 GL_BlendFunc(GL_ZERO, GL_ONE);
5444 GL_BlendFunc(GL_ONE, GL_ZERO);
5446 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5447 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5448 if (skyrendermasked)
5449 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
5453 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, msurface_t **texturesurfacelist)
5455 if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION)))
5458 if (rsurface.mode != RSURFMODE_GLSL)
5460 rsurface.mode = RSURFMODE_GLSL;
5461 R_Mesh_ResetTextureState();
5464 R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
5465 R_Mesh_TexBind(0, R_GetTexture(rsurface.texture->currentskinframe->nmap));
5466 R_Mesh_TexBind(1, R_GetTexture(rsurface.texture->basetexture));
5467 R_Mesh_TexBind(2, R_GetTexture(rsurface.texture->glosstexture));
5468 R_Mesh_TexBind(4, R_GetTexture(r_texture_fogattenuation));
5469 R_Mesh_TexBind(5, R_GetTexture(rsurface.texture->currentskinframe->pants));
5470 R_Mesh_TexBind(6, R_GetTexture(rsurface.texture->currentskinframe->shirt));
5471 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
5473 R_Mesh_TexBind(7, R_GetTexture(r_texture_grey128));
5474 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5475 R_Mesh_ColorPointer(NULL, 0, 0);
5477 else if (rsurface.uselightmaptexture)
5479 R_Mesh_TexBind(7, R_GetTexture(texturesurfacelist[0]->lightmaptexture));
5480 R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
5481 R_Mesh_ColorPointer(NULL, 0, 0);
5485 R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
5486 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5487 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
5489 R_Mesh_TexBind(9, R_GetTexture(rsurface.texture->currentskinframe->glow));
5490 R_Mesh_TexBind(11, R_GetTexture(r_texture_white)); // changed per surface
5491 R_Mesh_TexBind(12, R_GetTexture(r_texture_white)); // changed per surface
5493 if (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
5495 // render background
5496 GL_BlendFunc(GL_ONE, GL_ZERO);
5498 GL_AlphaTest(false);
5500 GL_Color(1, 1, 1, 1);
5501 R_Mesh_ColorPointer(NULL, 0, 0);
5503 R_SetupSurfaceShader(vec3_origin, rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, 1, 1, rsurface.texture->specularscale, RSURFPASS_BACKGROUND);
5504 if (r_glsl_permutation)
5506 RSurf_PrepareVerticesForBatch(true, true, texturenumsurfaces, texturesurfacelist);
5507 R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
5508 R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
5509 R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
5510 R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
5511 R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
5512 RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection ? 12 : -1);
5515 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
5516 GL_DepthMask(false);
5517 GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
5518 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
5520 R_Mesh_TexBind(7, R_GetTexture(r_texture_grey128));
5521 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5522 R_Mesh_ColorPointer(NULL, 0, 0);
5524 else if (rsurface.uselightmaptexture)
5526 R_Mesh_TexBind(7, R_GetTexture(texturesurfacelist[0]->lightmaptexture));
5527 R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
5528 R_Mesh_ColorPointer(NULL, 0, 0);
5532 R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
5533 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5534 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
5536 R_Mesh_TexBind(11, R_GetTexture(r_texture_white)); // changed per surface
5537 R_Mesh_TexBind(12, R_GetTexture(r_texture_white)); // changed per surface
5540 R_SetupSurfaceShader(vec3_origin, rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, 1, 1, rsurface.texture->specularscale, RSURFPASS_BASE);
5541 if (!r_glsl_permutation)
5544 RSurf_PrepareVerticesForBatch(r_glsl_permutation->loc_Texture_Normal >= 0 || r_glsl_permutation->loc_LightDir >= 0, r_glsl_permutation->loc_Texture_Normal >= 0, texturenumsurfaces, texturesurfacelist);
5545 R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
5546 R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
5547 R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
5548 R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
5549 R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
5550 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]);
5552 if (r_glsl_permutation->loc_Texture_Refraction >= 0)
5554 GL_BlendFunc(GL_ONE, GL_ZERO);
5556 GL_AlphaTest(false);
5559 if (rsurface.uselightmaptexture && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
5561 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
5562 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);
5564 RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1);
5568 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
5569 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);
5571 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5573 if (rsurface.texture->backgroundnumskinframes && !(rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
5578 static void R_DrawTextureSurfaceList_GL13(int texturenumsurfaces, msurface_t **texturesurfacelist)
5580 // OpenGL 1.3 path - anything not completely ancient
5581 int texturesurfaceindex;
5582 qboolean applycolor;
5586 const texturelayer_t *layer;
5587 if (rsurface.mode != RSURFMODE_MULTIPASS)
5588 rsurface.mode = RSURFMODE_MULTIPASS;
5589 RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
5591 for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
5594 int layertexrgbscale;
5595 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5597 if (layerindex == 0)
5601 GL_AlphaTest(false);
5602 qglDepthFunc(GL_EQUAL);CHECKGLERROR
5605 GL_DepthMask(layer->depthmask);
5606 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
5607 if ((layer->color[0] > 2 || layer->color[1] > 2 || layer->color[2] > 2) && (gl_combine.integer || layer->depthmask))
5609 layertexrgbscale = 4;
5610 VectorScale(layer->color, 0.25f, layercolor);
5612 else if ((layer->color[0] > 1 || layer->color[1] > 1 || layer->color[2] > 1) && (gl_combine.integer || layer->depthmask))
5614 layertexrgbscale = 2;
5615 VectorScale(layer->color, 0.5f, layercolor);
5619 layertexrgbscale = 1;
5620 VectorScale(layer->color, 1.0f, layercolor);
5622 layercolor[3] = layer->color[3];
5623 applycolor = layercolor[0] != 1 || layercolor[1] != 1 || layercolor[2] != 1 || layercolor[3] != 1;
5624 R_Mesh_ColorPointer(NULL, 0, 0);
5625 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
5626 switch (layer->type)
5628 case TEXTURELAYERTYPE_LITTEXTURE:
5629 memset(&m, 0, sizeof(m));
5630 m.tex[0] = R_GetTexture(r_texture_white);
5631 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5632 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5633 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5634 m.tex[1] = R_GetTexture(layer->texture);
5635 m.texmatrix[1] = layer->texmatrix;
5636 m.texrgbscale[1] = layertexrgbscale;
5637 m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
5638 m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
5639 m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
5640 R_Mesh_TextureState(&m);
5641 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5642 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5643 else if (rsurface.uselightmaptexture)
5644 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5646 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5648 case TEXTURELAYERTYPE_TEXTURE:
5649 memset(&m, 0, sizeof(m));
5650 m.tex[0] = R_GetTexture(layer->texture);
5651 m.texmatrix[0] = layer->texmatrix;
5652 m.texrgbscale[0] = layertexrgbscale;
5653 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5654 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5655 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5656 R_Mesh_TextureState(&m);
5657 RSurf_DrawBatch_GL11_Unlit(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5659 case TEXTURELAYERTYPE_FOG:
5660 memset(&m, 0, sizeof(m));
5661 m.texrgbscale[0] = layertexrgbscale;
5664 m.tex[0] = R_GetTexture(layer->texture);
5665 m.texmatrix[0] = layer->texmatrix;
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;
5670 R_Mesh_TextureState(&m);
5671 // generate a color array for the fog pass
5672 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
5673 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5677 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5678 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)
5680 f = 1 - FogPoint_Model(v);
5681 c[0] = layercolor[0];
5682 c[1] = layercolor[1];
5683 c[2] = layercolor[2];
5684 c[3] = f * layercolor[3];
5687 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5690 Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
5692 GL_LockArrays(0, 0);
5695 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5697 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
5698 GL_AlphaTest(false);
5702 static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, msurface_t **texturesurfacelist)
5704 // OpenGL 1.1 - crusty old voodoo path
5705 int texturesurfaceindex;
5709 const texturelayer_t *layer;
5710 if (rsurface.mode != RSURFMODE_MULTIPASS)
5711 rsurface.mode = RSURFMODE_MULTIPASS;
5712 RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
5714 for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
5716 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5718 if (layerindex == 0)
5722 GL_AlphaTest(false);
5723 qglDepthFunc(GL_EQUAL);CHECKGLERROR
5726 GL_DepthMask(layer->depthmask);
5727 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
5728 R_Mesh_ColorPointer(NULL, 0, 0);
5729 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
5730 switch (layer->type)
5732 case TEXTURELAYERTYPE_LITTEXTURE:
5733 if (layer->blendfunc1 == GL_ONE && layer->blendfunc2 == GL_ZERO)
5735 // two-pass lit texture with 2x rgbscale
5736 // first the lightmap pass
5737 memset(&m, 0, sizeof(m));
5738 m.tex[0] = R_GetTexture(r_texture_white);
5739 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5740 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5741 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5742 R_Mesh_TextureState(&m);
5743 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5744 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5745 else if (rsurface.uselightmaptexture)
5746 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5748 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5749 GL_LockArrays(0, 0);
5750 // then apply the texture to it
5751 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
5752 memset(&m, 0, sizeof(m));
5753 m.tex[0] = R_GetTexture(layer->texture);
5754 m.texmatrix[0] = layer->texmatrix;
5755 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5756 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5757 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5758 R_Mesh_TextureState(&m);
5759 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);
5763 // single pass vertex-lighting-only texture with 1x rgbscale and transparency support
5764 memset(&m, 0, sizeof(m));
5765 m.tex[0] = R_GetTexture(layer->texture);
5766 m.texmatrix[0] = layer->texmatrix;
5767 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5768 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5769 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5770 R_Mesh_TextureState(&m);
5771 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5772 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);
5774 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);
5777 case TEXTURELAYERTYPE_TEXTURE:
5778 // singletexture unlit texture with transparency support
5779 memset(&m, 0, sizeof(m));
5780 m.tex[0] = R_GetTexture(layer->texture);
5781 m.texmatrix[0] = layer->texmatrix;
5782 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5783 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5784 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5785 R_Mesh_TextureState(&m);
5786 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);
5788 case TEXTURELAYERTYPE_FOG:
5789 // singletexture fogging
5790 R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
5793 memset(&m, 0, sizeof(m));
5794 m.tex[0] = R_GetTexture(layer->texture);
5795 m.texmatrix[0] = layer->texmatrix;
5796 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5797 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5798 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5799 R_Mesh_TextureState(&m);
5802 R_Mesh_ResetTextureState();
5803 // generate a color array for the fog pass
5804 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5808 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5809 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)
5811 f = 1 - FogPoint_Model(v);
5812 c[0] = layer->color[0];
5813 c[1] = layer->color[1];
5814 c[2] = layer->color[2];
5815 c[3] = f * layer->color[3];
5818 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5821 Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
5823 GL_LockArrays(0, 0);
5826 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5828 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
5829 GL_AlphaTest(false);
5833 static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth, qboolean depthonly)
5835 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW)
5837 rsurface.rtlight = NULL;
5841 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
5843 if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
5845 if (rsurface.mode != RSURFMODE_MULTIPASS)
5846 rsurface.mode = RSURFMODE_MULTIPASS;
5847 if (r_depthfirst.integer == 3)
5849 int i = (int)(texturesurfacelist[0] - rsurface.modelsurfaces);
5850 if (!r_view.showdebug)
5851 GL_Color(0, 0, 0, 1);
5853 GL_Color(((i >> 6) & 7) / 7.0f, ((i >> 3) & 7) / 7.0f, (i & 7) / 7.0f,1);
5857 GL_ColorMask(0,0,0,0);
5860 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5861 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5862 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5864 GL_BlendFunc(GL_ONE, GL_ZERO);
5866 GL_AlphaTest(false);
5867 R_Mesh_ColorPointer(NULL, 0, 0);
5868 R_Mesh_ResetTextureState();
5869 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5870 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5871 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
5873 else if (r_depthfirst.integer == 3)
5875 else if (!r_view.showdebug && (r_showsurfaces.integer || gl_lightmaps.integer))
5877 GL_Color(0, 0, 0, 1);
5878 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5880 else if (r_showsurfaces.integer)
5882 if (rsurface.mode != RSURFMODE_MULTIPASS)
5883 rsurface.mode = RSURFMODE_MULTIPASS;
5884 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5885 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5887 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5888 GL_BlendFunc(GL_ONE, GL_ZERO);
5889 GL_DepthMask(writedepth);
5891 GL_AlphaTest(false);
5892 R_Mesh_ColorPointer(NULL, 0, 0);
5893 R_Mesh_ResetTextureState();
5894 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5895 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
5897 else if (gl_lightmaps.integer)
5900 if (rsurface.mode != RSURFMODE_MULTIPASS)
5901 rsurface.mode = RSURFMODE_MULTIPASS;
5902 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5904 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5905 GL_BlendFunc(GL_ONE, GL_ZERO);
5906 GL_DepthMask(writedepth);
5908 GL_AlphaTest(false);
5909 R_Mesh_ColorPointer(NULL, 0, 0);
5910 memset(&m, 0, sizeof(m));
5911 m.tex[0] = R_GetTexture(r_texture_white);
5912 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5913 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5914 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5915 R_Mesh_TextureState(&m);
5916 RSurf_PrepareVerticesForBatch(rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, false, texturenumsurfaces, texturesurfacelist);
5917 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5918 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5919 else if (rsurface.uselightmaptexture)
5920 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5922 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5924 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY)
5925 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
5926 else if (rsurface.texture->currentnumlayers)
5928 // write depth for anything we skipped on the depth-only pass earlier
5929 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5931 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5932 GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
5933 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5934 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5935 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
5936 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
5937 GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
5938 if (r_glsl.integer && gl_support_fragment_shader)
5939 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist);
5940 else if (gl_combine.integer && r_textureunits.integer >= 2)
5941 R_DrawTextureSurfaceList_GL13(texturenumsurfaces, texturesurfacelist);
5943 R_DrawTextureSurfaceList_GL11(texturenumsurfaces, texturesurfacelist);
5946 GL_LockArrays(0, 0);
5949 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
5952 int texturenumsurfaces, endsurface;
5954 msurface_t *surface;
5955 msurface_t *texturesurfacelist[1024];
5957 // if the model is static it doesn't matter what value we give for
5958 // wantnormals and wanttangents, so this logic uses only rules applicable
5959 // to a model, knowing that they are meaningless otherwise
5960 if (ent == r_refdef.worldentity)
5961 RSurf_ActiveWorldEntity();
5962 else if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
5963 RSurf_ActiveModelEntity(ent, false, false);
5965 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader);
5967 for (i = 0;i < numsurfaces;i = j)
5970 surface = rsurface.modelsurfaces + surfacelist[i];
5971 texture = surface->texture;
5972 R_UpdateTextureInfo(ent, texture);
5973 rsurface.texture = texture->currentframe;
5974 rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
5975 // scan ahead until we find a different texture
5976 endsurface = min(i + 1024, numsurfaces);
5977 texturenumsurfaces = 0;
5978 texturesurfacelist[texturenumsurfaces++] = surface;
5979 for (;j < endsurface;j++)
5981 surface = rsurface.modelsurfaces + surfacelist[j];
5982 if (texture != surface->texture || rsurface.uselightmaptexture != (surface->lightmaptexture != NULL))
5984 texturesurfacelist[texturenumsurfaces++] = surface;
5986 // render the range of surfaces
5987 R_DrawTextureSurfaceList(texturenumsurfaces, texturesurfacelist, true, false);
5993 void R_QueueSurfaceList(entity_render_t *ent, int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes)
5996 vec3_t tempcenter, center;
5998 // if we're rendering water textures (extra scene renders), use a separate loop to avoid burdening the main one
6001 for (i = 0;i < numsurfaces;i++)
6002 if (surfacelist[i]->texture->currentframe->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION))
6003 R_Water_AddWaterPlane(surfacelist[i]);
6006 // break the surface list down into batches by texture and use of lightmapping
6007 for (i = 0;i < numsurfaces;i = j)
6010 // texture is the base texture pointer, rsurface.texture is the
6011 // current frame/skin the texture is directing us to use (for example
6012 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
6013 // use skin 1 instead)
6014 texture = surfacelist[i]->texture;
6015 rsurface.texture = texture->currentframe;
6016 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
6017 if (!(rsurface.texture->currentmaterialflags & flagsmask))
6019 // if this texture is not the kind we want, skip ahead to the next one
6020 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
6024 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
6026 // transparent surfaces get pushed off into the transparent queue
6027 const msurface_t *surface = surfacelist[i];
6030 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
6031 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
6032 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
6033 Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
6034 R_MeshQueue_AddTransparent(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_view.origin : center, R_DrawSurface_TransparentCallback, ent, surface - rsurface.modelsurfaces, rsurface.rtlight);
6038 // simply scan ahead until we find a different texture or lightmap state
6039 for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.uselightmaptexture == (surfacelist[j]->lightmaptexture != NULL);j++)
6041 // render the range of surfaces
6042 R_DrawTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly);
6047 float locboxvertex3f[6*4*3] =
6049 1,0,1, 1,0,0, 1,1,0, 1,1,1,
6050 0,1,1, 0,1,0, 0,0,0, 0,0,1,
6051 1,1,1, 1,1,0, 0,1,0, 0,1,1,
6052 0,0,1, 0,0,0, 1,0,0, 1,0,1,
6053 0,0,1, 1,0,1, 1,1,1, 0,1,1,
6054 1,0,0, 0,0,0, 0,1,0, 1,1,0
6057 int locboxelement3i[6*2*3] =
6067 void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6070 cl_locnode_t *loc = (cl_locnode_t *)ent;
6072 float vertex3f[6*4*3];
6074 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6075 GL_DepthMask(false);
6076 GL_DepthRange(0, 1);
6077 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6079 GL_CullFace(GL_NONE);
6080 R_Mesh_Matrix(&identitymatrix);
6082 R_Mesh_VertexPointer(vertex3f, 0, 0);
6083 R_Mesh_ColorPointer(NULL, 0, 0);
6084 R_Mesh_ResetTextureState();
6087 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_view.colorscale,
6088 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_view.colorscale,
6089 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_view.colorscale,
6090 surfacelist[0] < 0 ? 0.5f : 0.125f);
6092 if (VectorCompare(loc->mins, loc->maxs))
6094 VectorSet(size, 2, 2, 2);
6095 VectorMA(loc->mins, -0.5f, size, mins);
6099 VectorCopy(loc->mins, mins);
6100 VectorSubtract(loc->maxs, loc->mins, size);
6103 for (i = 0;i < 6*4*3;)
6104 for (j = 0;j < 3;j++, i++)
6105 vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
6107 R_Mesh_Draw(0, 6*4, 6*2, locboxelement3i, 0, 0);
6110 void R_DrawLocs(void)
6113 cl_locnode_t *loc, *nearestloc;
6115 nearestloc = CL_Locs_FindNearest(cl.movement_origin);
6116 for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
6118 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
6119 R_MeshQueue_AddTransparent(center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
6123 void R_DrawDebugModel(entity_render_t *ent)
6125 int i, j, k, l, flagsmask;
6126 const int *elements;
6128 msurface_t *surface;
6129 model_t *model = ent->model;
6132 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WATER | MATERIALFLAG_WALL;
6134 R_Mesh_ColorPointer(NULL, 0, 0);
6135 R_Mesh_ResetTextureState();
6136 GL_DepthRange(0, 1);
6137 GL_DepthTest(!r_showdisabledepthtest.integer);
6138 GL_DepthMask(false);
6139 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6141 if (r_showcollisionbrushes.value > 0 && model->brush.num_brushes)
6143 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
6144 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
6146 if (brush->colbrushf && brush->colbrushf->numtriangles)
6148 R_Mesh_VertexPointer(brush->colbrushf->points->v, 0, 0);
6149 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);
6150 R_Mesh_Draw(0, brush->colbrushf->numpoints, brush->colbrushf->numtriangles, brush->colbrushf->elements, 0, 0);
6153 for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
6155 if (surface->num_collisiontriangles)
6157 R_Mesh_VertexPointer(surface->data_collisionvertex3f, 0, 0);
6158 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);
6159 R_Mesh_Draw(0, surface->num_collisionvertices, surface->num_collisiontriangles, surface->data_collisionelement3i, 0, 0);
6164 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6166 if (r_showtris.integer || r_shownormals.integer)
6168 if (r_showdisabledepthtest.integer)
6170 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6171 GL_DepthMask(false);
6175 GL_BlendFunc(GL_ONE, GL_ZERO);
6178 for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
6180 if (ent == r_refdef.worldentity && !r_viewcache.world_surfacevisible[j])
6182 rsurface.texture = surface->texture->currentframe;
6183 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
6185 RSurf_PrepareVerticesForBatch(true, true, 1, &surface);
6186 if (r_showtris.value > 0)
6188 if (!rsurface.texture->currentlayers->depthmask)
6189 GL_Color(r_view.colorscale, 0, 0, r_showtris.value);
6190 else if (ent == r_refdef.worldentity)
6191 GL_Color(r_view.colorscale, r_view.colorscale, r_view.colorscale, r_showtris.value);
6193 GL_Color(0, r_view.colorscale, 0, r_showtris.value);
6194 elements = (ent->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
6197 for (k = 0;k < surface->num_triangles;k++, elements += 3)
6199 #define GLVERTEXELEMENT(n) qglVertex3f(rsurface.vertex3f[elements[n]*3+0], rsurface.vertex3f[elements[n]*3+1], rsurface.vertex3f[elements[n]*3+2])
6200 GLVERTEXELEMENT(0);GLVERTEXELEMENT(1);
6201 GLVERTEXELEMENT(1);GLVERTEXELEMENT(2);
6202 GLVERTEXELEMENT(2);GLVERTEXELEMENT(0);
6207 if (r_shownormals.value > 0)
6210 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6212 VectorCopy(rsurface.vertex3f + l * 3, v);
6213 GL_Color(r_view.colorscale, 0, 0, 1);
6214 qglVertex3f(v[0], v[1], v[2]);
6215 VectorMA(v, r_shownormals.value, rsurface.svector3f + l * 3, v);
6216 GL_Color(r_view.colorscale, 1, 1, 1);
6217 qglVertex3f(v[0], v[1], v[2]);
6222 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6224 VectorCopy(rsurface.vertex3f + l * 3, v);
6225 GL_Color(0, r_view.colorscale, 0, 1);
6226 qglVertex3f(v[0], v[1], v[2]);
6227 VectorMA(v, r_shownormals.value, rsurface.tvector3f + l * 3, v);
6228 GL_Color(r_view.colorscale, 1, 1, 1);
6229 qglVertex3f(v[0], v[1], v[2]);
6234 for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6236 VectorCopy(rsurface.vertex3f + l * 3, v);
6237 GL_Color(0, 0, r_view.colorscale, 1);
6238 qglVertex3f(v[0], v[1], v[2]);
6239 VectorMA(v, r_shownormals.value, rsurface.normal3f + l * 3, v);
6240 GL_Color(r_view.colorscale, 1, 1, 1);
6241 qglVertex3f(v[0], v[1], v[2]);
6248 rsurface.texture = NULL;
6252 extern void R_BuildLightMap(const entity_render_t *ent, msurface_t *surface);
6253 void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
6255 int i, j, endj, f, flagsmask;
6256 msurface_t *surface;
6258 model_t *model = r_refdef.worldmodel;
6259 const int maxsurfacelist = 1024;
6260 int numsurfacelist = 0;
6261 msurface_t *surfacelist[1024];
6265 RSurf_ActiveWorldEntity();
6267 // update light styles on this submodel
6268 if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
6270 model_brush_lightstyleinfo_t *style;
6271 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
6273 if (style->value != r_refdef.lightstylevalue[style->style])
6275 msurface_t *surfaces = model->data_surfaces;
6276 int *list = style->surfacelist;
6277 style->value = r_refdef.lightstylevalue[style->style];
6278 for (j = 0;j < style->numsurfaces;j++)
6279 surfaces[list[j]].cached_dlight = true;
6284 R_UpdateAllTextureInfo(r_refdef.worldentity);
6285 flagsmask = addwaterplanes ? (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
6289 R_DrawDebugModel(r_refdef.worldentity);
6295 rsurface.uselightmaptexture = false;
6296 rsurface.texture = NULL;
6298 j = model->firstmodelsurface;
6299 endj = j + model->nummodelsurfaces;
6302 // quickly skip over non-visible surfaces
6303 for (;j < endj && !r_viewcache.world_surfacevisible[j];j++)
6305 // quickly iterate over visible surfaces
6306 for (;j < endj && r_viewcache.world_surfacevisible[j];j++)
6308 // process this surface
6309 surface = model->data_surfaces + j;
6310 // if this surface fits the criteria, add it to the list
6311 if (surface->num_triangles)
6313 // if lightmap parameters changed, rebuild lightmap texture
6314 if (surface->cached_dlight)
6315 R_BuildLightMap(r_refdef.worldentity, surface);
6316 // add face to draw list
6317 surfacelist[numsurfacelist++] = surface;
6318 r_refdef.stats.world_triangles += surface->num_triangles;
6319 if (numsurfacelist >= maxsurfacelist)
6321 r_refdef.stats.world_surfaces += numsurfacelist;
6322 R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6328 r_refdef.stats.world_surfaces += numsurfacelist;
6330 R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6334 void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
6336 int i, j, f, flagsmask;
6337 msurface_t *surface, *endsurface;
6339 model_t *model = ent->model;
6340 const int maxsurfacelist = 1024;
6341 int numsurfacelist = 0;
6342 msurface_t *surfacelist[1024];
6346 // if the model is static it doesn't matter what value we give for
6347 // wantnormals and wanttangents, so this logic uses only rules applicable
6348 // to a model, knowing that they are meaningless otherwise
6349 if (ent == r_refdef.worldentity)
6350 RSurf_ActiveWorldEntity();
6351 else if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
6352 RSurf_ActiveModelEntity(ent, false, false);
6354 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader && !depthonly);
6356 // update light styles
6357 if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
6359 model_brush_lightstyleinfo_t *style;
6360 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
6362 if (style->value != r_refdef.lightstylevalue[style->style])
6364 msurface_t *surfaces = model->data_surfaces;
6365 int *list = style->surfacelist;
6366 style->value = r_refdef.lightstylevalue[style->style];
6367 for (j = 0;j < style->numsurfaces;j++)
6368 surfaces[list[j]].cached_dlight = true;
6373 R_UpdateAllTextureInfo(ent);
6374 flagsmask = addwaterplanes ? (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
6378 R_DrawDebugModel(ent);
6384 rsurface.uselightmaptexture = false;
6385 rsurface.texture = NULL;
6387 surface = model->data_surfaces + model->firstmodelsurface;
6388 endsurface = surface + model->nummodelsurfaces;
6389 for (;surface < endsurface;surface++)
6391 // if this surface fits the criteria, add it to the list
6392 if (surface->num_triangles)
6394 // if lightmap parameters changed, rebuild lightmap texture
6395 if (surface->cached_dlight)
6396 R_BuildLightMap(ent, surface);
6397 // add face to draw list
6398 surfacelist[numsurfacelist++] = surface;
6399 r_refdef.stats.entities_triangles += surface->num_triangles;
6400 if (numsurfacelist >= maxsurfacelist)
6402 r_refdef.stats.entities_surfaces += numsurfacelist;
6403 R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6408 r_refdef.stats.entities_surfaces += numsurfacelist;
6410 R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);