]> git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_rmain.c
vid: remove obsolete cvars
[xonotic/darkplaces.git] / gl_rmain.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_main.c
21
22 #include "quakedef.h"
23 #include "r_shadow.h"
24 #include "polygon.h"
25 #include "image.h"
26 #include "ft2.h"
27 #include "csprogs.h"
28 #include "cl_video.h"
29 #include "cl_collision.h"
30
31 #ifdef WIN32
32 // Enable NVIDIA High Performance Graphics while using Integrated Graphics.
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
37 #ifdef __cplusplus
38 }
39 #endif
40 #endif
41
42 mempool_t *r_main_mempool;
43 rtexturepool_t *r_main_texturepool;
44
45 int r_textureframe = 0; ///< used only by R_GetCurrentTexture, incremented per view and per UI render
46
47 static qbool r_loadnormalmap;
48 static qbool r_loadgloss;
49 qbool r_loadfog;
50 static qbool r_loaddds;
51 static qbool r_savedds;
52 static qbool r_gpuskeletal;
53
54 //
55 // screen size info
56 //
57 r_refdef_t r_refdef;
58
59 cvar_t r_motionblur = {CF_CLIENT | CF_ARCHIVE, "r_motionblur", "0", "screen motionblur - value represents intensity, somewhere around 0.5 recommended - NOTE: bad performance on multi-gpu!"};
60 cvar_t r_damageblur = {CF_CLIENT | CF_ARCHIVE, "r_damageblur", "0", "screen motionblur based on damage - value represents intensity, somewhere around 0.5 recommended - NOTE: bad performance on multi-gpu!"};
61 cvar_t r_motionblur_averaging = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_averaging", "0.1", "sliding average reaction time for velocity (higher = slower adaption to change)"};
62 cvar_t r_motionblur_randomize = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_randomize", "0.1", "randomizing coefficient to workaround ghosting"};
63 cvar_t r_motionblur_minblur = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_minblur", "0.5", "factor of blur to apply at all times (always have this amount of blur no matter what the other factors are)"};
64 cvar_t r_motionblur_maxblur = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_maxblur", "0.9", "maxmimum amount of blur"};
65 cvar_t r_motionblur_velocityfactor = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_velocityfactor", "1", "factoring in of player velocity to the blur equation - the faster the player moves around the map, the more blur they get"};
66 cvar_t r_motionblur_velocityfactor_minspeed = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_velocityfactor_minspeed", "400", "lower value of velocity when it starts to factor into blur equation"};
67 cvar_t r_motionblur_velocityfactor_maxspeed = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_velocityfactor_maxspeed", "800", "upper value of velocity when it reaches the peak factor into blur equation"};
68 cvar_t r_motionblur_mousefactor = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_mousefactor", "2", "factoring in of mouse acceleration to the blur equation - the faster the player turns their mouse, the more blur they get"};
69 cvar_t r_motionblur_mousefactor_minspeed = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_mousefactor_minspeed", "0", "lower value of mouse acceleration when it starts to factor into blur equation"};
70 cvar_t r_motionblur_mousefactor_maxspeed = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_mousefactor_maxspeed", "50", "upper value of mouse acceleration when it reaches the peak factor into blur equation"};
71
72 cvar_t r_depthfirst = {CF_CLIENT | CF_ARCHIVE, "r_depthfirst", "0", "renders a depth-only version of the scene before normal rendering begins to eliminate overdraw, values: 0 = off, 1 = world depth, 2 = world and model depth"};
73 cvar_t r_useinfinitefarclip = {CF_CLIENT | CF_ARCHIVE, "r_useinfinitefarclip", "1", "enables use of a special kind of projection matrix that has an extremely large farclip"};
74 cvar_t r_farclip_base = {CF_CLIENT, "r_farclip_base", "65536", "farclip (furthest visible distance) for rendering when r_useinfinitefarclip is 0"};
75 cvar_t r_farclip_world = {CF_CLIENT, "r_farclip_world", "2", "adds map size to farclip multiplied by this value"};
76 cvar_t r_nearclip = {CF_CLIENT, "r_nearclip", "1", "distance from camera of nearclip plane" };
77 cvar_t r_deformvertexes = {CF_CLIENT, "r_deformvertexes", "1", "allows use of deformvertexes in shader files (can be turned off to check performance impact)"};
78 cvar_t r_transparent = {CF_CLIENT, "r_transparent", "1", "allows use of transparent surfaces (can be turned off to check performance impact)"};
79 cvar_t r_transparent_alphatocoverage = {CF_CLIENT, "r_transparent_alphatocoverage", "1", "enables GL_ALPHA_TO_COVERAGE antialiasing technique on alphablend and alphatest surfaces when using vid_samples 2 or higher"};
80 cvar_t r_transparent_sortsurfacesbynearest = {CF_CLIENT, "r_transparent_sortsurfacesbynearest", "1", "sort entity and world surfaces by nearest point on bounding box instead of using the center of the bounding box, usually reduces sorting artifacts"};
81 cvar_t r_transparent_useplanardistance = {CF_CLIENT, "r_transparent_useplanardistance", "0", "sort transparent meshes by distance from view plane rather than spherical distance to the chosen point"};
82 cvar_t r_showoverdraw = {CF_CLIENT, "r_showoverdraw", "0", "shows overlapping geometry"};
83 cvar_t r_showbboxes = {CF_CLIENT, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%,  10 = 100%)"};
84 cvar_t r_showbboxes_client = {CF_CLIENT, "r_showbboxes_client", "0", "shows bounding boxes of clientside qc entities, value controls opacity scaling (1 = 10%,  10 = 100%)"};
85 cvar_t r_showsurfaces = {CF_CLIENT, "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)"};
86 cvar_t r_showtris = {CF_CLIENT, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
87 cvar_t r_shownormals = {CF_CLIENT, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
88 cvar_t r_showlighting = {CF_CLIENT, "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"};
89 cvar_t r_showcollisionbrushes = {CF_CLIENT, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
90 cvar_t r_showcollisionbrushes_polygonfactor = {CF_CLIENT, "r_showcollisionbrushes_polygonfactor", "-1", "expands outward the brush polygons a little bit, used to make collision brushes appear infront of walls"};
91 cvar_t r_showcollisionbrushes_polygonoffset = {CF_CLIENT, "r_showcollisionbrushes_polygonoffset", "0", "nudges brush polygon depth in hardware depth units, used to make collision brushes appear infront of walls"};
92 cvar_t r_showdisabledepthtest = {CF_CLIENT, "r_showdisabledepthtest", "0", "disables depth testing on r_show* cvars, allowing you to see what hidden geometry the graphics card is processing"};
93 cvar_t r_showspriteedges = {CF_CLIENT, "r_showspriteedges", "0", "renders a debug outline to show the polygon shape of each sprite frame rendered (may be 2 or more in case of interpolated animations), for debugging rendering bugs with specific view types"};
94 cvar_t r_showparticleedges = {CF_CLIENT, "r_showparticleedges", "0", "renders a debug outline to show the polygon shape of each particle, for debugging rendering bugs with specific view types"};
95 cvar_t r_drawportals = {CF_CLIENT, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
96 cvar_t r_drawentities = {CF_CLIENT, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
97 cvar_t r_draw2d = {CF_CLIENT, "r_draw2d","1", "draw 2D stuff (dangerous to turn off)"};
98 cvar_t r_drawworld = {CF_CLIENT, "r_drawworld","1", "draw world (most static stuff)"};
99 cvar_t r_drawviewmodel = {CF_CLIENT, "r_drawviewmodel","1", "draw your weapon model"};
100 cvar_t r_drawexteriormodel = {CF_CLIENT, "r_drawexteriormodel","1", "draw your player model (e.g. in chase cam, reflections)"};
101 cvar_t r_cullentities_trace = {CF_CLIENT, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
102 cvar_t r_cullentities_trace_entityocclusion = {CF_CLIENT, "r_cullentities_trace_entityocclusion", "1", "check for occluding entities such as doors, not just world hull"};
103 cvar_t r_cullentities_trace_samples = {CF_CLIENT, "r_cullentities_trace_samples", "2", "number of samples to test for entity culling (in addition to center sample)"};
104 cvar_t r_cullentities_trace_tempentitysamples = {CF_CLIENT, "r_cullentities_trace_tempentitysamples", "-1", "number of samples to test for entity culling of temp entities (including all CSQC entities), -1 disables trace culling on these entities to prevent flicker (pvs still applies)"};
105 cvar_t r_cullentities_trace_enlarge = {CF_CLIENT, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
106 cvar_t r_cullentities_trace_expand = {CF_CLIENT, "r_cullentities_trace_expand", "0", "box expanded by this many units for entity culling"};
107 cvar_t r_cullentities_trace_pad = {CF_CLIENT, "r_cullentities_trace_pad", "8", "accept traces that hit within this many units of the box"};
108 cvar_t r_cullentities_trace_delay = {CF_CLIENT, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
109 cvar_t r_cullentities_trace_eyejitter = {CF_CLIENT, "r_cullentities_trace_eyejitter", "16", "randomly offset rays from the eye by this much to reduce the odds of flickering"};
110 cvar_t r_sortentities = {CF_CLIENT, "r_sortentities", "0", "sort entities before drawing (might be faster)"};
111 cvar_t r_speeds = {CF_CLIENT, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
112 cvar_t r_fullbright = {CF_CLIENT, "r_fullbright","0", "makes map very bright and renders faster"};
113
114 cvar_t r_fullbright_directed = {CF_CLIENT, "r_fullbright_directed", "0", "render fullbright things (unlit worldmodel and EF_FULLBRIGHT entities, but not fullbright shaders) using a constant light direction instead to add more depth while keeping uniform brightness"};
115 cvar_t r_fullbright_directed_ambient = {CF_CLIENT, "r_fullbright_directed_ambient", "0.5", "ambient light multiplier for directed fullbright"};
116 cvar_t r_fullbright_directed_diffuse = {CF_CLIENT, "r_fullbright_directed_diffuse", "0.75", "diffuse light multiplier for directed fullbright"};
117 cvar_t r_fullbright_directed_pitch = {CF_CLIENT, "r_fullbright_directed_pitch", "20", "constant pitch direction ('height') of the fake light source to use for fullbright"};
118 cvar_t r_fullbright_directed_pitch_relative = {CF_CLIENT, "r_fullbright_directed_pitch_relative", "0", "whether r_fullbright_directed_pitch is interpreted as absolute (0) or relative (1) pitch"};
119
120 cvar_t r_wateralpha = {CF_CLIENT | CF_ARCHIVE, "r_wateralpha","1", "opacity of water polygons"};
121 cvar_t r_dynamic = {CF_CLIENT | CF_ARCHIVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
122 cvar_t r_fullbrights = {CF_CLIENT | CF_ARCHIVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
123 cvar_t r_shadows = {CF_CLIENT | CF_ARCHIVE, "r_shadows", "0", "casts fake stencil shadows from models onto the world (rtlights are unaffected by this); when set to 2, always cast the shadows in the direction set by r_shadows_throwdirection, otherwise use the model lighting."};
124 cvar_t r_shadows_darken = {CF_CLIENT | CF_ARCHIVE, "r_shadows_darken", "0.5", "how much shadowed areas will be darkened"};
125 cvar_t r_shadows_throwdistance = {CF_CLIENT | CF_ARCHIVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
126 cvar_t r_shadows_throwdirection = {CF_CLIENT | CF_ARCHIVE, "r_shadows_throwdirection", "0 0 -1", "override throwing direction for r_shadows 2"};
127 cvar_t r_shadows_drawafterrtlighting = {CF_CLIENT | CF_ARCHIVE, "r_shadows_drawafterrtlighting", "0", "draw fake shadows AFTER realtime lightning is drawn. May be useful for simulating fast sunlight on large outdoor maps with only one noshadow rtlight. The price is less realistic appearance of dynamic light shadows."};
128 cvar_t r_shadows_castfrombmodels = {CF_CLIENT | CF_ARCHIVE, "r_shadows_castfrombmodels", "0", "do cast shadows from bmodels"};
129 cvar_t r_shadows_focus = {CF_CLIENT | CF_ARCHIVE, "r_shadows_focus", "0 0 0", "offset the shadowed area focus"};
130 cvar_t r_shadows_shadowmapscale = {CF_CLIENT | CF_ARCHIVE, "r_shadows_shadowmapscale", "0.25", "higher values increase shadowmap quality at a cost of area covered (multiply global shadowmap precision) for fake shadows. Needs shadowmapping ON."};
131 cvar_t r_shadows_shadowmapbias = {CF_CLIENT | CF_ARCHIVE, "r_shadows_shadowmapbias", "-1", "sets shadowmap bias for fake shadows. -1 sets the value of r_shadow_shadowmapping_bias. Needs shadowmapping ON."};
132 cvar_t r_q1bsp_skymasking = {CF_CLIENT, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
133 cvar_t r_polygonoffset_submodel_factor = {CF_CLIENT, "r_polygonoffset_submodel_factor", "0", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
134 cvar_t r_polygonoffset_submodel_offset = {CF_CLIENT, "r_polygonoffset_submodel_offset", "14", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
135 cvar_t r_polygonoffset_decals_factor = {CF_CLIENT, "r_polygonoffset_decals_factor", "0", "biases depth values of decals to prevent z-fighting artifacts"};
136 cvar_t r_polygonoffset_decals_offset = {CF_CLIENT, "r_polygonoffset_decals_offset", "-14", "biases depth values of decals to prevent z-fighting artifacts"};
137 cvar_t r_fog_exp2 = {CF_CLIENT, "r_fog_exp2", "0", "uses GL_EXP2 fog (as in Nehahra) rather than realistic GL_EXP fog"};
138 cvar_t r_fog_clear = {CF_CLIENT, "r_fog_clear", "1", "clears renderbuffer with fog color before render starts"};
139 cvar_t r_drawfog = {CF_CLIENT | CF_ARCHIVE, "r_drawfog", "1", "allows one to disable fog rendering"};
140 cvar_t r_transparentdepthmasking = {CF_CLIENT | CF_ARCHIVE, "r_transparentdepthmasking", "0", "enables depth writes on transparent meshes whose materially is normally opaque, this prevents seeing the inside of a transparent mesh"};
141 cvar_t r_transparent_sortmindist = {CF_CLIENT | CF_ARCHIVE, "r_transparent_sortmindist", "0", "lower distance limit for transparent sorting"};
142 cvar_t r_transparent_sortmaxdist = {CF_CLIENT | CF_ARCHIVE, "r_transparent_sortmaxdist", "32768", "upper distance limit for transparent sorting"};
143 cvar_t r_transparent_sortarraysize = {CF_CLIENT | CF_ARCHIVE, "r_transparent_sortarraysize", "4096", "number of distance-sorting layers"};
144 cvar_t r_celshading = {CF_CLIENT | CF_ARCHIVE, "r_celshading", "0", "cartoon-style light shading (OpenGL 2.x only)"}; // FIXME remove OpenGL 2.x only once implemented for DX9
145 cvar_t r_celoutlines = {CF_CLIENT | CF_ARCHIVE, "r_celoutlines", "0", "cartoon-style outlines (requires r_shadow_deferred)"};
146
147 cvar_t gl_fogenable = {CF_CLIENT, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
148 cvar_t gl_fogdensity = {CF_CLIENT, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
149 cvar_t gl_fogred = {CF_CLIENT, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
150 cvar_t gl_foggreen = {CF_CLIENT, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
151 cvar_t gl_fogblue = {CF_CLIENT, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
152 cvar_t gl_fogstart = {CF_CLIENT, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
153 cvar_t gl_fogend = {CF_CLIENT, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
154 cvar_t gl_skyclip = {CF_CLIENT, "gl_skyclip", "4608", "nehahra farclip distance - the real fog end (for Nehahra compatibility only)"};
155
156 cvar_t r_texture_dds_load = {CF_CLIENT | CF_ARCHIVE, "r_texture_dds_load", "0", "load compressed dds/filename.dds texture instead of filename.tga, if the file exists (requires driver support)"};
157 cvar_t r_texture_dds_save = {CF_CLIENT | CF_ARCHIVE, "r_texture_dds_save", "0", "save compressed dds/filename.dds texture when filename.tga is loaded, so that it can be loaded instead next time"};
158
159 cvar_t r_usedepthtextures = {CF_CLIENT | CF_ARCHIVE, "r_usedepthtextures", "1", "use depth texture instead of depth renderbuffer where possible, uses less video memory but may render slower (or faster) depending on hardware"};
160 cvar_t r_viewfbo = {CF_CLIENT | CF_ARCHIVE, "r_viewfbo", "0", "enables use of an 8bit (1) or 16bit (2) or 32bit (3) per component float framebuffer render, which may be at a different resolution than the video mode"};
161 cvar_t r_rendertarget_debug = {CF_CLIENT, "r_rendertarget_debug", "-1", "replaces the view with the contents of the specified render target (by number - note that these can fluctuate depending on scene)"};
162 cvar_t r_viewscale = {CF_CLIENT | CF_ARCHIVE, "r_viewscale", "1", "scaling factor for resolution of the fbo rendering method, must be > 0, can be above 1 for a costly antialiasing behavior, typical values are 0.5 for 1/4th as many pixels rendered, or 1 for normal rendering"};
163 cvar_t r_viewscale_fpsscaling = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling", "0", "change resolution based on framerate"};
164 cvar_t r_viewscale_fpsscaling_min = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_min", "0.0625", "worst acceptable quality"};
165 cvar_t r_viewscale_fpsscaling_multiply = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_multiply", "5", "adjust quality up or down by the frametime difference from 1.0/target, multiplied by this factor"};
166 cvar_t r_viewscale_fpsscaling_stepsize = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_stepsize", "0.01", "smallest adjustment to hit the target framerate (this value prevents minute oscillations)"};
167 cvar_t r_viewscale_fpsscaling_stepmax = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_stepmax", "1.00", "largest adjustment to hit the target framerate (this value prevents wild overshooting of the estimate)"};
168 cvar_t r_viewscale_fpsscaling_target = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_target", "70", "desired framerate"};
169
170 cvar_t r_glsl_skeletal = {CF_CLIENT | CF_ARCHIVE, "r_glsl_skeletal", "1", "render skeletal models faster using a gpu-skinning technique"};
171 cvar_t r_glsl_deluxemapping = {CF_CLIENT | CF_ARCHIVE, "r_glsl_deluxemapping", "1", "use per pixel lighting on deluxemap-compiled q3bsp maps (or a value of 2 forces deluxemap shading even without deluxemaps)"};
172 cvar_t r_glsl_offsetmapping = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
173 cvar_t r_glsl_offsetmapping_steps = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_steps", "2", "offset mapping steps (note: too high values may be not supported by your GPU)"};
174 cvar_t r_glsl_offsetmapping_reliefmapping = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
175 cvar_t r_glsl_offsetmapping_reliefmapping_steps = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_reliefmapping_steps", "10", "relief mapping steps (note: too high values may be not supported by your GPU)"};
176 cvar_t r_glsl_offsetmapping_reliefmapping_refinesteps = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_reliefmapping_refinesteps", "5", "relief mapping refine steps (these are a binary search executed as the last step as given by r_glsl_offsetmapping_reliefmapping_steps)"};
177 cvar_t r_glsl_offsetmapping_scale = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
178 cvar_t r_glsl_offsetmapping_lod = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_lod", "0", "apply distance-based level-of-detail correction to number of offsetmappig steps, effectively making it render faster on large open-area maps"};
179 cvar_t r_glsl_offsetmapping_lod_distance = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_lod_distance", "32", "first LOD level distance, second level (-50% steps) is 2x of this, third (33%) - 3x etc."};
180 cvar_t r_glsl_postprocess = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess", "0", "use a GLSL postprocessing shader"};
181 cvar_t r_glsl_postprocess_uservec1 = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec1", "0 0 0 0", "a 4-component vector to pass as uservec1 to the postprocessing shader (only useful if default.glsl has been customized)"};
182 cvar_t r_glsl_postprocess_uservec2 = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec2", "0 0 0 0", "a 4-component vector to pass as uservec2 to the postprocessing shader (only useful if default.glsl has been customized)"};
183 cvar_t r_glsl_postprocess_uservec3 = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec3", "0 0 0 0", "a 4-component vector to pass as uservec3 to the postprocessing shader (only useful if default.glsl has been customized)"};
184 cvar_t r_glsl_postprocess_uservec4 = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec4", "0 0 0 0", "a 4-component vector to pass as uservec4 to the postprocessing shader (only useful if default.glsl has been customized)"};
185 cvar_t r_glsl_postprocess_uservec1_enable = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec1_enable", "1", "enables postprocessing uservec1 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
186 cvar_t r_glsl_postprocess_uservec2_enable = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec2_enable", "1", "enables postprocessing uservec2 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
187 cvar_t r_glsl_postprocess_uservec3_enable = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec3_enable", "1", "enables postprocessing uservec3 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
188 cvar_t r_glsl_postprocess_uservec4_enable = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec4_enable", "1", "enables postprocessing uservec4 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
189 cvar_t r_colorfringe = {CF_CLIENT | CF_ARCHIVE, "r_colorfringe", "0", "Chromatic aberration. Values higher than 0.025 will noticeably distort the image"};
190 cvar_t r_fxaa = {CF_CLIENT | CF_ARCHIVE, "r_fxaa", "0", "fast approximate anti aliasing"};
191
192 cvar_t r_water = {CF_CLIENT | CF_ARCHIVE, "r_water", "0", "whether to use reflections and refraction on water surfaces (note: r_wateralpha must be set below 1)"};
193 cvar_t r_water_cameraentitiesonly = {CF_CLIENT | CF_ARCHIVE, "r_water_cameraentitiesonly", "0", "whether to only show QC-defined reflections/refractions (typically used for camera- or portal-like effects)"};
194 cvar_t r_water_clippingplanebias = {CF_CLIENT | CF_ARCHIVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
195 cvar_t r_water_resolutionmultiplier = {CF_CLIENT | CF_ARCHIVE, "r_water_resolutionmultiplier", "0.5", "multiplier for screen resolution when rendering refracted/reflected scenes, 1 is full quality, lower values are faster"};
196 cvar_t r_water_refractdistort = {CF_CLIENT | CF_ARCHIVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
197 cvar_t r_water_reflectdistort = {CF_CLIENT | CF_ARCHIVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
198 cvar_t r_water_scissormode = {CF_CLIENT, "r_water_scissormode", "3", "scissor (1) or cull (2) or both (3) water renders"};
199 cvar_t r_water_lowquality = {CF_CLIENT, "r_water_lowquality", "0", "special option to accelerate water rendering: 1 disables all dynamic lights, 2 disables particles too"};
200 cvar_t r_water_hideplayer = {CF_CLIENT | CF_ARCHIVE, "r_water_hideplayer", "0", "if set to 1 then player will be hidden in refraction views, if set to 2 then player will also be hidden in reflection views, player is always visible in camera views"};
201
202 cvar_t r_lerpsprites = {CF_CLIENT | CF_ARCHIVE, "r_lerpsprites", "0", "enables animation smoothing on sprites"};
203 cvar_t r_lerpmodels = {CF_CLIENT | CF_ARCHIVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
204 cvar_t r_nolerp_list = {CF_CLIENT | CF_ARCHIVE, "r_nolerp_list", "progs/v_nail.mdl,progs/v_nail2.mdl,progs/flame.mdl,progs/flame2.mdl,progs/braztall.mdl,progs/brazshrt.mdl,progs/longtrch.mdl,progs/flame_pyre.mdl,progs/v_saw.mdl,progs/v_xfist.mdl,progs/h2stuff/newfire.mdl", "comma separated list of models that will not have their animations smoothed"};
205 cvar_t r_lerplightstyles = {CF_CLIENT | CF_ARCHIVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
206 cvar_t r_waterscroll = {CF_CLIENT | CF_ARCHIVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
207
208 cvar_t r_bloom = {CF_CLIENT | CF_ARCHIVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
209 cvar_t r_bloom_colorscale = {CF_CLIENT | CF_ARCHIVE, "r_bloom_colorscale", "1", "how bright the glow is"};
210
211 cvar_t r_bloom_brighten = {CF_CLIENT | CF_ARCHIVE, "r_bloom_brighten", "1", "how bright the glow is, after subtract/power"};
212 cvar_t r_bloom_blur = {CF_CLIENT | CF_ARCHIVE, "r_bloom_blur", "4", "how large the glow is"};
213 cvar_t r_bloom_resolution = {CF_CLIENT | CF_ARCHIVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
214 cvar_t r_bloom_colorexponent = {CF_CLIENT | CF_ARCHIVE, "r_bloom_colorexponent", "1", "how exaggerated the glow is"};
215 cvar_t r_bloom_colorsubtract = {CF_CLIENT | CF_ARCHIVE, "r_bloom_colorsubtract", "0.1", "reduces bloom colors by a certain amount"};
216 cvar_t r_bloom_scenebrightness = {CF_CLIENT | CF_ARCHIVE, "r_bloom_scenebrightness", "1", "global rendering brightness when bloom is enabled"};
217
218 cvar_t r_hdr_scenebrightness = {CF_CLIENT | CF_ARCHIVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
219 cvar_t r_hdr_glowintensity = {CF_CLIENT | CF_ARCHIVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
220 cvar_t r_hdr_irisadaptation = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation", "0", "adjust scene brightness according to light intensity at player location"};
221 cvar_t r_hdr_irisadaptation_multiplier = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_multiplier", "2", "brightness at which value will be 1.0"};
222 cvar_t r_hdr_irisadaptation_minvalue = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_minvalue", "0.5", "minimum value that can result from multiplier / brightness"};
223 cvar_t r_hdr_irisadaptation_maxvalue = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_maxvalue", "4", "maximum value that can result from multiplier / brightness"};
224 cvar_t r_hdr_irisadaptation_value = {CF_CLIENT, "r_hdr_irisadaptation_value", "1", "current value as scenebrightness multiplier, changes continuously when irisadaptation is active"};
225 cvar_t r_hdr_irisadaptation_fade_up = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_fade_up", "0.1", "fade rate at which value adjusts to darkness"};
226 cvar_t r_hdr_irisadaptation_fade_down = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_fade_down", "0.5", "fade rate at which value adjusts to brightness"};
227 cvar_t r_hdr_irisadaptation_radius = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_radius", "15", "lighting within this many units of the eye is averaged"};
228
229 cvar_t r_smoothnormals_areaweighting = {CF_CLIENT, "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"};
230
231 cvar_t developer_texturelogging = {CF_CLIENT, "developer_texturelogging", "0", "produces a textures.log file containing names of skins and map textures the engine tried to load"};
232
233 cvar_t gl_lightmaps = {CF_CLIENT, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers), a value of 2 keeps normalmap shading"};
234
235 cvar_t r_test = {CF_CLIENT, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
236
237 cvar_t r_batch_multidraw = {CF_CLIENT | CF_ARCHIVE, "r_batch_multidraw", "1", "issue multiple glDrawElements calls when rendering a batch of surfaces with the same texture (otherwise the index data is copied to make it one draw)"};
238 cvar_t r_batch_multidraw_mintriangles = {CF_CLIENT | CF_ARCHIVE, "r_batch_multidraw_mintriangles", "0", "minimum number of triangles to activate multidraw path (copying small groups of triangles may be faster)"};
239 cvar_t r_batch_debugdynamicvertexpath = {CF_CLIENT | CF_ARCHIVE, "r_batch_debugdynamicvertexpath", "0", "force the dynamic batching code path for debugging purposes"};
240 cvar_t r_batch_dynamicbuffer = {CF_CLIENT | CF_ARCHIVE, "r_batch_dynamicbuffer", "0", "use vertex/index buffers for drawing dynamic and copytriangles batches"};
241
242 cvar_t r_glsl_saturation = {CF_CLIENT | CF_ARCHIVE, "r_glsl_saturation", "1", "saturation multiplier (only working in glsl!)"};
243 cvar_t r_glsl_saturation_redcompensate = {CF_CLIENT | CF_ARCHIVE, "r_glsl_saturation_redcompensate", "0", "a 'vampire sight' addition to desaturation effect, does compensation for red color, r_glsl_restart is required"};
244
245 cvar_t r_glsl_vertextextureblend_usebothalphas = {CF_CLIENT | CF_ARCHIVE, "r_glsl_vertextextureblend_usebothalphas", "0", "use both alpha layers on vertex blended surfaces, each alpha layer sets amount of 'blend leak' on another layer, requires mod_q3shader_force_terrain_alphaflag on."};
246
247 // FIXME: This cvar would grow to a ridiculous size after several launches and clean exits when used during surface sorting.
248 cvar_t r_framedatasize = {CF_CLIENT | CF_ARCHIVE, "r_framedatasize", "0.5", "size of renderer data cache used during one frame (for skeletal animation caching, light processing, etc)"};
249 cvar_t r_buffermegs[R_BUFFERDATA_COUNT] =
250 {
251         {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_vertex", "4", "vertex buffer size for one frame"},
252         {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_index16", "1", "index buffer size for one frame (16bit indices)"},
253         {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_index32", "1", "index buffer size for one frame (32bit indices)"},
254         {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_uniform", "0.25", "uniform buffer size for one frame"},
255 };
256
257 cvar_t r_q1bsp_lightmap_updates_enabled = {CF_CLIENT | CF_ARCHIVE, "r_q1bsp_lightmap_updates_enabled", "1", "allow lightmaps to be updated on Q1BSP maps (don't turn this off except for debugging)"};
258 cvar_t r_q1bsp_lightmap_updates_combine = {CF_CLIENT | CF_ARCHIVE, "r_q1bsp_lightmap_updates_combine", "2", "combine lightmap texture updates to make fewer glTexSubImage2D calls, modes: 0 = immediately upload lightmaps (may be thousands of small 3x3 updates), 1 = combine to one call, 2 = combine to one full texture update (glTexImage2D) which tells the driver it does not need to lock the resource (faster on most drivers)"};
259 cvar_t r_q1bsp_lightmap_updates_hidden_surfaces = {CF_CLIENT | CF_ARCHIVE, "r_q1bsp_lightmap_updates_hidden_surfaces", "0", "update lightmaps on surfaces that are not visible, so that updates only occur on frames where lightstyles changed value (animation or light switches), only makes sense with combine = 2"};
260
261 extern cvar_t v_glslgamma_2d;
262
263 extern qbool v_flipped_state;
264
265 r_framebufferstate_t r_fb;
266
267 /// shadow volume bsp struct with automatically growing nodes buffer
268 svbsp_t r_svbsp;
269
270 int r_uniformbufferalignment = 32; // dynamically updated to match GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT
271
272 rtexture_t *r_texture_blanknormalmap;
273 rtexture_t *r_texture_white;
274 rtexture_t *r_texture_grey128;
275 rtexture_t *r_texture_black;
276 rtexture_t *r_texture_notexture;
277 rtexture_t *r_texture_whitecube;
278 rtexture_t *r_texture_normalizationcube;
279 rtexture_t *r_texture_fogattenuation;
280 rtexture_t *r_texture_fogheighttexture;
281 rtexture_t *r_texture_gammaramps;
282 unsigned int r_texture_gammaramps_serial;
283 //rtexture_t *r_texture_fogintensity;
284 rtexture_t *r_texture_reflectcube;
285
286 // TODO: hash lookups?
287 typedef struct cubemapinfo_s
288 {
289         char basename[64];
290         rtexture_t *texture;
291 }
292 cubemapinfo_t;
293
294 int r_texture_numcubemaps;
295 cubemapinfo_t *r_texture_cubemaps[MAX_CUBEMAPS];
296
297 unsigned int r_queries[MAX_OCCLUSION_QUERIES];
298 unsigned int r_numqueries;
299 unsigned int r_maxqueries;
300
301 typedef struct r_qwskincache_s
302 {
303         char name[MAX_QPATH];
304         skinframe_t *skinframe;
305 }
306 r_qwskincache_t;
307
308 static r_qwskincache_t *r_qwskincache;
309 static int r_qwskincache_size;
310
311 /// vertex coordinates for a quad that covers the screen exactly
312 extern const float r_screenvertex3f[12];
313 const float r_screenvertex3f[12] =
314 {
315         0, 0, 0,
316         1, 0, 0,
317         1, 1, 0,
318         0, 1, 0
319 };
320
321 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
322 {
323         int i;
324         for (i = 0;i < verts;i++)
325         {
326                 out[0] = in[0] * r;
327                 out[1] = in[1] * g;
328                 out[2] = in[2] * b;
329                 out[3] = in[3];
330                 in += 4;
331                 out += 4;
332         }
333 }
334
335 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
336 {
337         int i;
338         for (i = 0;i < verts;i++)
339         {
340                 out[0] = r;
341                 out[1] = g;
342                 out[2] = b;
343                 out[3] = a;
344                 out += 4;
345         }
346 }
347
348 // FIXME: move this to client?
349 void FOG_clear(void)
350 {
351         if (gamemode == GAME_NEHAHRA)
352         {
353                 Cvar_Set(&cvars_all, "gl_fogenable", "0");
354                 Cvar_Set(&cvars_all, "gl_fogdensity", "0.2");
355                 Cvar_Set(&cvars_all, "gl_fogred", "0.3");
356                 Cvar_Set(&cvars_all, "gl_foggreen", "0.3");
357                 Cvar_Set(&cvars_all, "gl_fogblue", "0.3");
358         }
359         r_refdef.fog_density = 0;
360         r_refdef.fog_red = 0;
361         r_refdef.fog_green = 0;
362         r_refdef.fog_blue = 0;
363         r_refdef.fog_alpha = 1;
364         r_refdef.fog_start = 0;
365         r_refdef.fog_end = 16384;
366         r_refdef.fog_height = 1<<30;
367         r_refdef.fog_fadedepth = 128;
368         memset(r_refdef.fog_height_texturename, 0, sizeof(r_refdef.fog_height_texturename));
369 }
370
371 static void R_BuildBlankTextures(void)
372 {
373         unsigned char data[4];
374         data[2] = 128; // normal X
375         data[1] = 128; // normal Y
376         data[0] = 255; // normal Z
377         data[3] = 255; // height
378         r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
379         data[0] = 255;
380         data[1] = 255;
381         data[2] = 255;
382         data[3] = 255;
383         r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
384         data[0] = 128;
385         data[1] = 128;
386         data[2] = 128;
387         data[3] = 255;
388         r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
389         data[0] = 0;
390         data[1] = 0;
391         data[2] = 0;
392         data[3] = 255;
393         r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
394 }
395
396 static void R_BuildNoTexture(void)
397 {
398         r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, Image_GenerateNoTexture(), TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, -1, NULL);
399 }
400
401 static void R_BuildWhiteCube(void)
402 {
403         unsigned char data[6*1*1*4];
404         memset(data, 255, sizeof(data));
405         r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
406 }
407
408 static void R_BuildNormalizationCube(void)
409 {
410         int x, y, side;
411         vec3_t v;
412         vec_t s, t, intensity;
413 #define NORMSIZE 64
414         unsigned char *data;
415         data = (unsigned char *)Mem_Alloc(tempmempool, 6*NORMSIZE*NORMSIZE*4);
416         for (side = 0;side < 6;side++)
417         {
418                 for (y = 0;y < NORMSIZE;y++)
419                 {
420                         for (x = 0;x < NORMSIZE;x++)
421                         {
422                                 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
423                                 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
424                                 switch(side)
425                                 {
426                                 default:
427                                 case 0:
428                                         v[0] = 1;
429                                         v[1] = -t;
430                                         v[2] = -s;
431                                         break;
432                                 case 1:
433                                         v[0] = -1;
434                                         v[1] = -t;
435                                         v[2] = s;
436                                         break;
437                                 case 2:
438                                         v[0] = s;
439                                         v[1] = 1;
440                                         v[2] = t;
441                                         break;
442                                 case 3:
443                                         v[0] = s;
444                                         v[1] = -1;
445                                         v[2] = -t;
446                                         break;
447                                 case 4:
448                                         v[0] = s;
449                                         v[1] = -t;
450                                         v[2] = 1;
451                                         break;
452                                 case 5:
453                                         v[0] = -s;
454                                         v[1] = -t;
455                                         v[2] = -1;
456                                         break;
457                                 }
458                                 intensity = 127.0f / sqrt(DotProduct(v, v));
459                                 data[((side*64+y)*64+x)*4+2] = (unsigned char)(128.0f + intensity * v[0]);
460                                 data[((side*64+y)*64+x)*4+1] = (unsigned char)(128.0f + intensity * v[1]);
461                                 data[((side*64+y)*64+x)*4+0] = (unsigned char)(128.0f + intensity * v[2]);
462                                 data[((side*64+y)*64+x)*4+3] = 255;
463                         }
464                 }
465         }
466         r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
467         Mem_Free(data);
468 }
469
470 static void R_BuildFogTexture(void)
471 {
472         int x, b;
473 #define FOGWIDTH 256
474         unsigned char data1[FOGWIDTH][4];
475         //unsigned char data2[FOGWIDTH][4];
476         double d, r, alpha;
477
478         r_refdef.fogmasktable_start = r_refdef.fog_start;
479         r_refdef.fogmasktable_alpha = r_refdef.fog_alpha;
480         r_refdef.fogmasktable_range = r_refdef.fogrange;
481         r_refdef.fogmasktable_density = r_refdef.fog_density;
482
483         r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
484         for (x = 0;x < FOGMASKTABLEWIDTH;x++)
485         {
486                 d = (x * r - r_refdef.fogmasktable_start);
487                 if(developer_extra.integer)
488                         Con_DPrintf("%f ", d);
489                 d = max(0, d);
490                 if (r_fog_exp2.integer)
491                         alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
492                 else
493                         alpha = exp(-r_refdef.fogmasktable_density * 0.004 * d);
494                 if(developer_extra.integer)
495                         Con_DPrintf(" : %f ", alpha);
496                 alpha = 1 - (1 - alpha) * r_refdef.fogmasktable_alpha;
497                 if(developer_extra.integer)
498                         Con_DPrintf(" = %f\n", alpha);
499                 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
500         }
501
502         for (x = 0;x < FOGWIDTH;x++)
503         {
504                 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
505                 data1[x][0] = b;
506                 data1[x][1] = b;
507                 data1[x][2] = b;
508                 data1[x][3] = 255;
509                 //data2[x][0] = 255 - b;
510                 //data2[x][1] = 255 - b;
511                 //data2[x][2] = 255 - b;
512                 //data2[x][3] = 255;
513         }
514         if (r_texture_fogattenuation)
515         {
516                 R_UpdateTexture(r_texture_fogattenuation, &data1[0][0], 0, 0, 0, FOGWIDTH, 1, 1, 0);
517                 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, 0, FOGWIDTH, 1, 1, 0);
518         }
519         else
520         {
521                 r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
522                 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
523         }
524 }
525
526 static void R_BuildFogHeightTexture(void)
527 {
528         unsigned char *inpixels;
529         int size;
530         int x;
531         int y;
532         int j;
533         float c[4];
534         float f;
535         inpixels = NULL;
536         strlcpy(r_refdef.fogheighttexturename, r_refdef.fog_height_texturename, sizeof(r_refdef.fogheighttexturename));
537         if (r_refdef.fogheighttexturename[0])
538                 inpixels = loadimagepixelsbgra(r_refdef.fogheighttexturename, true, false, false, NULL);
539         if (!inpixels)
540         {
541                 r_refdef.fog_height_tablesize = 0;
542                 if (r_texture_fogheighttexture)
543                         R_FreeTexture(r_texture_fogheighttexture);
544                 r_texture_fogheighttexture = NULL;
545                 if (r_refdef.fog_height_table2d)
546                         Mem_Free(r_refdef.fog_height_table2d);
547                 r_refdef.fog_height_table2d = NULL;
548                 if (r_refdef.fog_height_table1d)
549                         Mem_Free(r_refdef.fog_height_table1d);
550                 r_refdef.fog_height_table1d = NULL;
551                 return;
552         }
553         size = image_width;
554         r_refdef.fog_height_tablesize = size;
555         r_refdef.fog_height_table1d = (unsigned char *)Mem_Alloc(r_main_mempool, size * 4);
556         r_refdef.fog_height_table2d = (unsigned char *)Mem_Alloc(r_main_mempool, size * size * 4);
557         memcpy(r_refdef.fog_height_table1d, inpixels, size * 4);
558         Mem_Free(inpixels);
559         // LadyHavoc: now the magic - what is that table2d for?  it is a cooked
560         // average fog color table accounting for every fog layer between a point
561         // and the camera.  (Note: attenuation is handled separately!)
562         for (y = 0;y < size;y++)
563         {
564                 for (x = 0;x < size;x++)
565                 {
566                         Vector4Clear(c);
567                         f = 0;
568                         if (x < y)
569                         {
570                                 for (j = x;j <= y;j++)
571                                 {
572                                         Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
573                                         f++;
574                                 }
575                         }
576                         else
577                         {
578                                 for (j = x;j >= y;j--)
579                                 {
580                                         Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
581                                         f++;
582                                 }
583                         }
584                         f = 1.0f / f;
585                         r_refdef.fog_height_table2d[(y*size+x)*4+0] = (unsigned char)(c[0] * f);
586                         r_refdef.fog_height_table2d[(y*size+x)*4+1] = (unsigned char)(c[1] * f);
587                         r_refdef.fog_height_table2d[(y*size+x)*4+2] = (unsigned char)(c[2] * f);
588                         r_refdef.fog_height_table2d[(y*size+x)*4+3] = (unsigned char)(c[3] * f);
589                 }
590         }
591         r_texture_fogheighttexture = R_LoadTexture2D(r_main_texturepool, "fogheighttable", size, size, r_refdef.fog_height_table2d, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_CLAMP, -1, NULL);
592 }
593
594 //=======================================================================================================================================================
595
596 static const char *builtinshaderstrings[] =
597 {
598 #include "shader_glsl.h"
599 0
600 };
601
602 //=======================================================================================================================================================
603
604 typedef struct shaderpermutationinfo_s
605 {
606         const char *pretext;
607         const char *name;
608 }
609 shaderpermutationinfo_t;
610
611 typedef struct shadermodeinfo_s
612 {
613         const char *sourcebasename;
614         const char *extension;
615         const char **builtinshaderstrings;
616         const char *pretext;
617         const char *name;
618         char *filename;
619         char *builtinstring;
620         int builtincrc;
621 }
622 shadermodeinfo_t;
623
624 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
625 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
626 {
627         {"#define USEDIFFUSE\n", " diffuse"},
628         {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
629         {"#define USEVIEWTINT\n", " viewtint"},
630         {"#define USECOLORMAPPING\n", " colormapping"},
631         {"#define USESATURATION\n", " saturation"},
632         {"#define USEFOGINSIDE\n", " foginside"},
633         {"#define USEFOGOUTSIDE\n", " fogoutside"},
634         {"#define USEFOGHEIGHTTEXTURE\n", " fogheighttexture"},
635         {"#define USEFOGALPHAHACK\n", " fogalphahack"},
636         {"#define USEGAMMARAMPS\n", " gammaramps"},
637         {"#define USECUBEFILTER\n", " cubefilter"},
638         {"#define USEGLOW\n", " glow"},
639         {"#define USEBLOOM\n", " bloom"},
640         {"#define USESPECULAR\n", " specular"},
641         {"#define USEPOSTPROCESSING\n", " postprocessing"},
642         {"#define USEREFLECTION\n", " reflection"},
643         {"#define USEOFFSETMAPPING\n", " offsetmapping"},
644         {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
645         {"#define USESHADOWMAP2D\n", " shadowmap2d"},
646         {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"}, // TODO make this a static parm
647         {"#define USESHADOWMAPORTHO\n", " shadowmaportho"},
648         {"#define USEDEFERREDLIGHTMAP\n", " deferredlightmap"},
649         {"#define USEALPHAKILL\n", " alphakill"},
650         {"#define USEREFLECTCUBE\n", " reflectcube"},
651         {"#define USENORMALMAPSCROLLBLEND\n", " normalmapscrollblend"},
652         {"#define USEBOUNCEGRID\n", " bouncegrid"},
653         {"#define USEBOUNCEGRIDDIRECTIONAL\n", " bouncegriddirectional"}, // TODO make this a static parm
654         {"#define USETRIPPY\n", " trippy"},
655         {"#define USEDEPTHRGB\n", " depthrgb"},
656         {"#define USEALPHAGENVERTEX\n", " alphagenvertex"},
657         {"#define USESKELETAL\n", " skeletal"},
658         {"#define USEOCCLUDE\n", " occlude"}
659 };
660
661 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
662 shadermodeinfo_t shadermodeinfo[SHADERLANGUAGE_COUNT][SHADERMODE_COUNT] =
663 {
664         // SHADERLANGUAGE_GLSL
665         {
666                 {"combined", "glsl", builtinshaderstrings, "#define MODE_GENERIC\n", " generic"},
667                 {"combined", "glsl", builtinshaderstrings, "#define MODE_POSTPROCESS\n", " postprocess"},
668                 {"combined", "glsl", builtinshaderstrings, "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
669                 {"combined", "glsl", builtinshaderstrings, "#define MODE_FLATCOLOR\n", " flatcolor"},
670                 {"combined", "glsl", builtinshaderstrings, "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
671                 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTMAP\n", " lightmap"},
672                 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
673                 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
674                 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP\n", " lightdirectionmap_forced_lightmap"},
675                 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR\n", " lightdirectionmap_forced_vertexcolor"},
676                 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTGRID\n", " lightgrid"},
677                 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
678                 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTSOURCE\n", " lightsource"},
679                 {"combined", "glsl", builtinshaderstrings, "#define MODE_REFRACTION\n", " refraction"},
680                 {"combined", "glsl", builtinshaderstrings, "#define MODE_WATER\n", " water"},
681                 {"combined", "glsl", builtinshaderstrings, "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
682                 {"combined", "glsl", builtinshaderstrings, "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
683         },
684 };
685
686 struct r_glsl_permutation_s;
687 typedef struct r_glsl_permutation_s
688 {
689         /// hash lookup data
690         struct r_glsl_permutation_s *hashnext;
691         unsigned int mode;
692         uint64_t permutation;
693
694         /// indicates if we have tried compiling this permutation already
695         qbool compiled;
696         /// 0 if compilation failed
697         int program;
698         // texture units assigned to each detected uniform
699         int tex_Texture_First;
700         int tex_Texture_Second;
701         int tex_Texture_GammaRamps;
702         int tex_Texture_Normal;
703         int tex_Texture_Color;
704         int tex_Texture_Gloss;
705         int tex_Texture_Glow;
706         int tex_Texture_SecondaryNormal;
707         int tex_Texture_SecondaryColor;
708         int tex_Texture_SecondaryGloss;
709         int tex_Texture_SecondaryGlow;
710         int tex_Texture_Pants;
711         int tex_Texture_Shirt;
712         int tex_Texture_FogHeightTexture;
713         int tex_Texture_FogMask;
714         int tex_Texture_LightGrid;
715         int tex_Texture_Lightmap;
716         int tex_Texture_Deluxemap;
717         int tex_Texture_Attenuation;
718         int tex_Texture_Cube;
719         int tex_Texture_Refraction;
720         int tex_Texture_Reflection;
721         int tex_Texture_ShadowMap2D;
722         int tex_Texture_CubeProjection;
723         int tex_Texture_ScreenNormalMap;
724         int tex_Texture_ScreenDiffuse;
725         int tex_Texture_ScreenSpecular;
726         int tex_Texture_ReflectMask;
727         int tex_Texture_ReflectCube;
728         int tex_Texture_BounceGrid;
729         /// locations of detected uniforms in program object, or -1 if not found
730         int loc_Texture_First;
731         int loc_Texture_Second;
732         int loc_Texture_GammaRamps;
733         int loc_Texture_Normal;
734         int loc_Texture_Color;
735         int loc_Texture_Gloss;
736         int loc_Texture_Glow;
737         int loc_Texture_SecondaryNormal;
738         int loc_Texture_SecondaryColor;
739         int loc_Texture_SecondaryGloss;
740         int loc_Texture_SecondaryGlow;
741         int loc_Texture_Pants;
742         int loc_Texture_Shirt;
743         int loc_Texture_FogHeightTexture;
744         int loc_Texture_FogMask;
745         int loc_Texture_LightGrid;
746         int loc_Texture_Lightmap;
747         int loc_Texture_Deluxemap;
748         int loc_Texture_Attenuation;
749         int loc_Texture_Cube;
750         int loc_Texture_Refraction;
751         int loc_Texture_Reflection;
752         int loc_Texture_ShadowMap2D;
753         int loc_Texture_CubeProjection;
754         int loc_Texture_ScreenNormalMap;
755         int loc_Texture_ScreenDiffuse;
756         int loc_Texture_ScreenSpecular;
757         int loc_Texture_ReflectMask;
758         int loc_Texture_ReflectCube;
759         int loc_Texture_BounceGrid;
760         int loc_Alpha;
761         int loc_BloomBlur_Parameters;
762         int loc_ClientTime;
763         int loc_Color_Ambient;
764         int loc_Color_Diffuse;
765         int loc_Color_Specular;
766         int loc_Color_Glow;
767         int loc_Color_Pants;
768         int loc_Color_Shirt;
769         int loc_DeferredColor_Ambient;
770         int loc_DeferredColor_Diffuse;
771         int loc_DeferredColor_Specular;
772         int loc_DeferredMod_Diffuse;
773         int loc_DeferredMod_Specular;
774         int loc_DistortScaleRefractReflect;
775         int loc_EyePosition;
776         int loc_FogColor;
777         int loc_FogHeightFade;
778         int loc_FogPlane;
779         int loc_FogPlaneViewDist;
780         int loc_FogRangeRecip;
781         int loc_LightColor;
782         int loc_LightDir;
783         int loc_LightGridMatrix;
784         int loc_LightGridNormalMatrix;
785         int loc_LightPosition;
786         int loc_OffsetMapping_ScaleSteps;
787         int loc_OffsetMapping_LodDistance;
788         int loc_OffsetMapping_Bias;
789         int loc_PixelSize;
790         int loc_ReflectColor;
791         int loc_ReflectFactor;
792         int loc_ReflectOffset;
793         int loc_RefractColor;
794         int loc_Saturation;
795         int loc_ScreenCenterRefractReflect;
796         int loc_ScreenScaleRefractReflect;
797         int loc_ScreenToDepth;
798         int loc_ShadowMap_Parameters;
799         int loc_ShadowMap_TextureScale;
800         int loc_SpecularPower;
801         int loc_Skeletal_Transform12;
802         int loc_UserVec1;
803         int loc_UserVec2;
804         int loc_UserVec3;
805         int loc_UserVec4;
806         int loc_ColorFringe;
807         int loc_ViewTintColor;
808         int loc_ViewToLight;
809         int loc_ModelToLight;
810         int loc_TexMatrix;
811         int loc_BackgroundTexMatrix;
812         int loc_ModelViewProjectionMatrix;
813         int loc_ModelViewMatrix;
814         int loc_PixelToScreenTexCoord;
815         int loc_ModelToReflectCube;
816         int loc_ShadowMapMatrix;
817         int loc_BloomColorSubtract;
818         int loc_NormalmapScrollBlend;
819         int loc_BounceGridMatrix;
820         int loc_BounceGridIntensity;
821         /// uniform block bindings
822         int ubibind_Skeletal_Transform12_UniformBlock;
823         /// uniform block indices
824         int ubiloc_Skeletal_Transform12_UniformBlock;
825 }
826 r_glsl_permutation_t;
827
828 #define SHADERPERMUTATION_HASHSIZE 256
829
830
831 // non-degradable "lightweight" shader parameters to keep the permutations simpler
832 // these can NOT degrade! only use for simple stuff
833 enum
834 {
835         SHADERSTATICPARM_SATURATION_REDCOMPENSATE = 0, ///< red compensation filter for saturation
836         SHADERSTATICPARM_EXACTSPECULARMATH = 1, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
837         SHADERSTATICPARM_POSTPROCESS_USERVEC1 = 2, ///< postprocess uservec1 is enabled
838         SHADERSTATICPARM_POSTPROCESS_USERVEC2 = 3, ///< postprocess uservec2 is enabled
839         SHADERSTATICPARM_POSTPROCESS_USERVEC3 = 4, ///< postprocess uservec3 is enabled
840         SHADERSTATICPARM_POSTPROCESS_USERVEC4 = 5,  ///< postprocess uservec4 is enabled
841         SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS = 6, // use both alpha layers while blending materials, allows more advanced microblending
842         SHADERSTATICPARM_OFFSETMAPPING_USELOD = 7,  ///< LOD for offsetmapping
843         SHADERSTATICPARM_SHADOWMAPPCF_1 = 8, ///< PCF 1
844         SHADERSTATICPARM_SHADOWMAPPCF_2 = 9, ///< PCF 2
845         SHADERSTATICPARM_SHADOWSAMPLER = 10, ///< sampler
846         SHADERSTATICPARM_CELSHADING = 11, ///< celshading (alternative diffuse and specular math)
847         SHADERSTATICPARM_CELOUTLINES = 12, ///< celoutline (depth buffer analysis to produce outlines)
848         SHADERSTATICPARM_FXAA = 13, ///< fast approximate anti aliasing
849         SHADERSTATICPARM_COLORFRINGE = 14 ///< colorfringe (chromatic aberration)
850 };
851 #define SHADERSTATICPARMS_COUNT 15
852
853 static const char *shaderstaticparmstrings_list[SHADERSTATICPARMS_COUNT];
854 static int shaderstaticparms_count = 0;
855
856 static unsigned int r_compileshader_staticparms[(SHADERSTATICPARMS_COUNT + 0x1F) >> 5] = {0};
857 #define R_COMPILESHADER_STATICPARM_ENABLE(p) r_compileshader_staticparms[(p) >> 5] |= (1 << ((p) & 0x1F))
858
859 extern qbool r_shadow_shadowmapsampler;
860 extern int r_shadow_shadowmappcf;
861 qbool R_CompileShader_CheckStaticParms(void)
862 {
863         static int r_compileshader_staticparms_save[(SHADERSTATICPARMS_COUNT + 0x1F) >> 5];
864         memcpy(r_compileshader_staticparms_save, r_compileshader_staticparms, sizeof(r_compileshader_staticparms));
865         memset(r_compileshader_staticparms, 0, sizeof(r_compileshader_staticparms));
866
867         // detect all
868         if (r_glsl_saturation_redcompensate.integer)
869                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SATURATION_REDCOMPENSATE);
870         if (r_glsl_vertextextureblend_usebothalphas.integer)
871                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS);
872         if (r_shadow_glossexact.integer)
873                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_EXACTSPECULARMATH);
874         if (r_glsl_postprocess.integer)
875         {
876                 if (r_glsl_postprocess_uservec1_enable.integer)
877                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC1);
878                 if (r_glsl_postprocess_uservec2_enable.integer)
879                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC2);
880                 if (r_glsl_postprocess_uservec3_enable.integer)
881                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC3);
882                 if (r_glsl_postprocess_uservec4_enable.integer)
883                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC4);
884         }
885         if (r_fxaa.integer)
886                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_FXAA);
887         if (r_glsl_offsetmapping_lod.integer && r_glsl_offsetmapping_lod_distance.integer > 0)
888                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_OFFSETMAPPING_USELOD);
889
890         if (r_shadow_shadowmapsampler)
891                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWSAMPLER);
892         if (r_shadow_shadowmappcf > 1)
893                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWMAPPCF_2);
894         else if (r_shadow_shadowmappcf)
895                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWMAPPCF_1);
896         if (r_celshading.integer)
897                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_CELSHADING);
898         if (r_celoutlines.integer)
899                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_CELOUTLINES);
900         if (r_colorfringe.value)
901                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_COLORFRINGE);
902
903         return memcmp(r_compileshader_staticparms, r_compileshader_staticparms_save, sizeof(r_compileshader_staticparms)) != 0;
904 }
905
906 #define R_COMPILESHADER_STATICPARM_EMIT(p, n) \
907         if(r_compileshader_staticparms[(p) >> 5] & (1 << ((p) & 0x1F))) \
908                 shaderstaticparmstrings_list[shaderstaticparms_count++] = "#define " n "\n"; \
909         else \
910                 shaderstaticparmstrings_list[shaderstaticparms_count++] = "\n"
911 static void R_CompileShader_AddStaticParms(unsigned int mode, uint64_t permutation)
912 {
913         shaderstaticparms_count = 0;
914
915         // emit all
916         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SATURATION_REDCOMPENSATE, "SATURATION_REDCOMPENSATE");
917         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_EXACTSPECULARMATH, "USEEXACTSPECULARMATH");
918         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC1, "USERVEC1");
919         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC2, "USERVEC2");
920         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC3, "USERVEC3");
921         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC4, "USERVEC4");
922         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS, "USEBOTHALPHAS");
923         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_OFFSETMAPPING_USELOD, "USEOFFSETMAPPING_LOD");
924         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWMAPPCF_1, "USESHADOWMAPPCF 1");
925         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWMAPPCF_2, "USESHADOWMAPPCF 2");
926         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWSAMPLER, "USESHADOWSAMPLER");
927         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_CELSHADING, "USECELSHADING");
928         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_CELOUTLINES, "USECELOUTLINES");
929         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_FXAA, "USEFXAA");
930         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_COLORFRINGE, "USECOLORFRINGE");
931 }
932
933 /// information about each possible shader permutation
934 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
935 /// currently selected permutation
936 r_glsl_permutation_t *r_glsl_permutation;
937 /// storage for permutations linked in the hash table
938 memexpandablearray_t r_glsl_permutationarray;
939
940 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, uint64_t permutation)
941 {
942         //unsigned int hashdepth = 0;
943         unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
944         r_glsl_permutation_t *p;
945         for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
946         {
947                 if (p->mode == mode && p->permutation == permutation)
948                 {
949                         //if (hashdepth > 10)
950                         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
951                         return p;
952                 }
953                 //hashdepth++;
954         }
955         p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
956         p->mode = mode;
957         p->permutation = permutation;
958         p->hashnext = r_glsl_permutationhash[mode][hashindex];
959         r_glsl_permutationhash[mode][hashindex] = p;
960         //if (hashdepth > 10)
961         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
962         return p;
963 }
964
965 static char *R_ShaderStrCat(const char **strings)
966 {
967         char *string, *s;
968         const char **p = strings;
969         const char *t;
970         size_t len = 0;
971         for (p = strings;(t = *p);p++)
972                 len += strlen(t);
973         len++;
974         s = string = (char *)Mem_Alloc(r_main_mempool, len);
975         len = 0;
976         for (p = strings;(t = *p);p++)
977         {
978                 len = strlen(t);
979                 memcpy(s, t, len);
980                 s += len;
981         }
982         *s = 0;
983         return string;
984 }
985
986 static char *R_ShaderStrCat(const char **strings);
987 static void R_InitShaderModeInfo(void)
988 {
989         int i, language;
990         shadermodeinfo_t *modeinfo;
991         // we have a bunch of things to compute that weren't calculated at engine compile time - all filenames should have a crc of the builtin strings to prevent accidental overrides (any customization must be updated to match engine)
992         for (language = 0; language < SHADERLANGUAGE_COUNT; language++)
993         {
994                 for (i = 0; i < SHADERMODE_COUNT; i++)
995                 {
996                         char filename[MAX_QPATH];
997                         modeinfo = &shadermodeinfo[language][i];
998                         modeinfo->builtinstring = R_ShaderStrCat(modeinfo->builtinshaderstrings);
999                         modeinfo->builtincrc = CRC_Block((const unsigned char *)modeinfo->builtinstring, strlen(modeinfo->builtinstring));
1000                         dpsnprintf(filename, sizeof(filename), "%s/%s_crc%i.%s", modeinfo->extension, modeinfo->sourcebasename, modeinfo->builtincrc, modeinfo->extension);
1001                         modeinfo->filename = Mem_strdup(r_main_mempool, filename);
1002                 }
1003         }
1004 }
1005
1006 static char *ShaderModeInfo_GetShaderText(shadermodeinfo_t *modeinfo, qbool printfromdisknotice, qbool builtinonly)
1007 {
1008         char *shaderstring;
1009         // if the mode has no filename we have to return the builtin string
1010         if (builtinonly || !modeinfo->filename)
1011                 return Mem_strdup(r_main_mempool, modeinfo->builtinstring);
1012         // note that FS_LoadFile appends a 0 byte to make it a valid string
1013         shaderstring = (char *)FS_LoadFile(modeinfo->filename, r_main_mempool, false, NULL);
1014         if (shaderstring)
1015         {
1016                 if (printfromdisknotice)
1017                         Con_DPrintf("Loading shaders from file %s...\n", modeinfo->filename);
1018                 return shaderstring;
1019         }
1020         // fall back to builtinstring
1021         return Mem_strdup(r_main_mempool, modeinfo->builtinstring);
1022 }
1023
1024 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, uint64_t permutation)
1025 {
1026         int i;
1027         int ubibind;
1028         int sampler;
1029         shadermodeinfo_t *modeinfo = &shadermodeinfo[SHADERLANGUAGE_GLSL][mode];
1030         char *sourcestring;
1031         char permutationname[256];
1032         int vertstrings_count = 0;
1033         int geomstrings_count = 0;
1034         int fragstrings_count = 0;
1035         const char *vertstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1036         const char *geomstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1037         const char *fragstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1038
1039         if (p->compiled)
1040                 return;
1041         p->compiled = true;
1042         p->program = 0;
1043
1044         permutationname[0] = 0;
1045         sourcestring = ShaderModeInfo_GetShaderText(modeinfo, true, false);
1046
1047         strlcat(permutationname, modeinfo->filename, sizeof(permutationname));
1048
1049         // we need 140 for r_glsl_skeletal (GL_ARB_uniform_buffer_object)
1050         if(vid.support.glshaderversion >= 140)
1051         {
1052                 vertstrings_list[vertstrings_count++] = "#version 140\n";
1053                 geomstrings_list[geomstrings_count++] = "#version 140\n";
1054                 fragstrings_list[fragstrings_count++] = "#version 140\n";
1055                 vertstrings_list[vertstrings_count++] = "#define GLSL140\n";
1056                 geomstrings_list[geomstrings_count++] = "#define GLSL140\n";
1057                 fragstrings_list[fragstrings_count++] = "#define GLSL140\n";
1058         }
1059         // if we can do #version 130, we should (this improves quality of offset/reliefmapping thanks to textureGrad)
1060         else if(vid.support.glshaderversion >= 130)
1061         {
1062                 vertstrings_list[vertstrings_count++] = "#version 130\n";
1063                 geomstrings_list[geomstrings_count++] = "#version 130\n";
1064                 fragstrings_list[fragstrings_count++] = "#version 130\n";
1065                 vertstrings_list[vertstrings_count++] = "#define GLSL130\n";
1066                 geomstrings_list[geomstrings_count++] = "#define GLSL130\n";
1067                 fragstrings_list[fragstrings_count++] = "#define GLSL130\n";
1068         }
1069         // if we can do #version 120, we should (this adds the invariant keyword)
1070         else if(vid.support.glshaderversion >= 120)
1071         {
1072                 vertstrings_list[vertstrings_count++] = "#version 120\n";
1073                 geomstrings_list[geomstrings_count++] = "#version 120\n";
1074                 fragstrings_list[fragstrings_count++] = "#version 120\n";
1075                 vertstrings_list[vertstrings_count++] = "#define GLSL120\n";
1076                 geomstrings_list[geomstrings_count++] = "#define GLSL120\n";
1077                 fragstrings_list[fragstrings_count++] = "#define GLSL120\n";
1078         }
1079         // GLES also adds several things from GLSL120
1080         switch(vid.renderpath)
1081         {
1082         case RENDERPATH_GLES2:
1083                 vertstrings_list[vertstrings_count++] = "#define GLES\n";
1084                 geomstrings_list[geomstrings_count++] = "#define GLES\n";
1085                 fragstrings_list[fragstrings_count++] = "#define GLES\n";
1086                 break;
1087         default:
1088                 break;
1089         }
1090
1091         // the first pretext is which type of shader to compile as
1092         // (later these will all be bound together as a program object)
1093         vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
1094         geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
1095         fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
1096
1097         // the second pretext is the mode (for example a light source)
1098         vertstrings_list[vertstrings_count++] = modeinfo->pretext;
1099         geomstrings_list[geomstrings_count++] = modeinfo->pretext;
1100         fragstrings_list[fragstrings_count++] = modeinfo->pretext;
1101         strlcat(permutationname, modeinfo->name, sizeof(permutationname));
1102
1103         // now add all the permutation pretexts
1104         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1105         {
1106                 if (permutation & (1ll<<i))
1107                 {
1108                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
1109                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
1110                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
1111                         strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
1112                 }
1113                 else
1114                 {
1115                         // keep line numbers correct
1116                         vertstrings_list[vertstrings_count++] = "\n";
1117                         geomstrings_list[geomstrings_count++] = "\n";
1118                         fragstrings_list[fragstrings_count++] = "\n";
1119                 }
1120         }
1121
1122         // add static parms
1123         R_CompileShader_AddStaticParms(mode, permutation);
1124         memcpy((char *)(vertstrings_list + vertstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1125         vertstrings_count += shaderstaticparms_count;
1126         memcpy((char *)(geomstrings_list + geomstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1127         geomstrings_count += shaderstaticparms_count;
1128         memcpy((char *)(fragstrings_list + fragstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1129         fragstrings_count += shaderstaticparms_count;
1130
1131         // now append the shader text itself
1132         vertstrings_list[vertstrings_count++] = sourcestring;
1133         geomstrings_list[geomstrings_count++] = sourcestring;
1134         fragstrings_list[fragstrings_count++] = sourcestring;
1135
1136         // we don't currently use geometry shaders for anything, so just empty the list
1137         geomstrings_count = 0;
1138
1139         // compile the shader program
1140         if (vertstrings_count + geomstrings_count + fragstrings_count)
1141                 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1142         if (p->program)
1143         {
1144                 CHECKGLERROR
1145                 qglUseProgram(p->program);CHECKGLERROR
1146                 // look up all the uniform variable names we care about, so we don't
1147                 // have to look them up every time we set them
1148
1149 #if 0
1150                 // debugging aid
1151                 {
1152                         GLint activeuniformindex = 0;
1153                         GLint numactiveuniforms = 0;
1154                         char uniformname[128];
1155                         GLsizei uniformnamelength = 0;
1156                         GLint uniformsize = 0;
1157                         GLenum uniformtype = 0;
1158                         memset(uniformname, 0, sizeof(uniformname));
1159                         qglGetProgramiv(p->program, GL_ACTIVE_UNIFORMS, &numactiveuniforms);
1160                         Con_Printf("Shader has %i uniforms\n", numactiveuniforms);
1161                         for (activeuniformindex = 0;activeuniformindex < numactiveuniforms;activeuniformindex++)
1162                         {
1163                                 qglGetActiveUniform(p->program, activeuniformindex, sizeof(uniformname) - 1, &uniformnamelength, &uniformsize, &uniformtype, uniformname);
1164                                 Con_Printf("Uniform %i name \"%s\" size %i type %i\n", (int)activeuniformindex, uniformname, (int)uniformsize, (int)uniformtype);
1165                         }
1166                 }
1167 #endif
1168
1169                 p->loc_Texture_First              = qglGetUniformLocation(p->program, "Texture_First");
1170                 p->loc_Texture_Second             = qglGetUniformLocation(p->program, "Texture_Second");
1171                 p->loc_Texture_GammaRamps         = qglGetUniformLocation(p->program, "Texture_GammaRamps");
1172                 p->loc_Texture_Normal             = qglGetUniformLocation(p->program, "Texture_Normal");
1173                 p->loc_Texture_Color              = qglGetUniformLocation(p->program, "Texture_Color");
1174                 p->loc_Texture_Gloss              = qglGetUniformLocation(p->program, "Texture_Gloss");
1175                 p->loc_Texture_Glow               = qglGetUniformLocation(p->program, "Texture_Glow");
1176                 p->loc_Texture_SecondaryNormal    = qglGetUniformLocation(p->program, "Texture_SecondaryNormal");
1177                 p->loc_Texture_SecondaryColor     = qglGetUniformLocation(p->program, "Texture_SecondaryColor");
1178                 p->loc_Texture_SecondaryGloss     = qglGetUniformLocation(p->program, "Texture_SecondaryGloss");
1179                 p->loc_Texture_SecondaryGlow      = qglGetUniformLocation(p->program, "Texture_SecondaryGlow");
1180                 p->loc_Texture_Pants              = qglGetUniformLocation(p->program, "Texture_Pants");
1181                 p->loc_Texture_Shirt              = qglGetUniformLocation(p->program, "Texture_Shirt");
1182                 p->loc_Texture_FogHeightTexture   = qglGetUniformLocation(p->program, "Texture_FogHeightTexture");
1183                 p->loc_Texture_FogMask            = qglGetUniformLocation(p->program, "Texture_FogMask");
1184                 p->loc_Texture_LightGrid          = qglGetUniformLocation(p->program, "Texture_LightGrid");
1185                 p->loc_Texture_Lightmap           = qglGetUniformLocation(p->program, "Texture_Lightmap");
1186                 p->loc_Texture_Deluxemap          = qglGetUniformLocation(p->program, "Texture_Deluxemap");
1187                 p->loc_Texture_Attenuation        = qglGetUniformLocation(p->program, "Texture_Attenuation");
1188                 p->loc_Texture_Cube               = qglGetUniformLocation(p->program, "Texture_Cube");
1189                 p->loc_Texture_Refraction         = qglGetUniformLocation(p->program, "Texture_Refraction");
1190                 p->loc_Texture_Reflection         = qglGetUniformLocation(p->program, "Texture_Reflection");
1191                 p->loc_Texture_ShadowMap2D        = qglGetUniformLocation(p->program, "Texture_ShadowMap2D");
1192                 p->loc_Texture_CubeProjection     = qglGetUniformLocation(p->program, "Texture_CubeProjection");
1193                 p->loc_Texture_ScreenNormalMap    = qglGetUniformLocation(p->program, "Texture_ScreenNormalMap");
1194                 p->loc_Texture_ScreenDiffuse      = qglGetUniformLocation(p->program, "Texture_ScreenDiffuse");
1195                 p->loc_Texture_ScreenSpecular     = qglGetUniformLocation(p->program, "Texture_ScreenSpecular");
1196                 p->loc_Texture_ReflectMask        = qglGetUniformLocation(p->program, "Texture_ReflectMask");
1197                 p->loc_Texture_ReflectCube        = qglGetUniformLocation(p->program, "Texture_ReflectCube");
1198                 p->loc_Texture_BounceGrid         = qglGetUniformLocation(p->program, "Texture_BounceGrid");
1199                 p->loc_Alpha                      = qglGetUniformLocation(p->program, "Alpha");
1200                 p->loc_BloomBlur_Parameters       = qglGetUniformLocation(p->program, "BloomBlur_Parameters");
1201                 p->loc_ClientTime                 = qglGetUniformLocation(p->program, "ClientTime");
1202                 p->loc_Color_Ambient              = qglGetUniformLocation(p->program, "Color_Ambient");
1203                 p->loc_Color_Diffuse              = qglGetUniformLocation(p->program, "Color_Diffuse");
1204                 p->loc_Color_Specular             = qglGetUniformLocation(p->program, "Color_Specular");
1205                 p->loc_Color_Glow                 = qglGetUniformLocation(p->program, "Color_Glow");
1206                 p->loc_Color_Pants                = qglGetUniformLocation(p->program, "Color_Pants");
1207                 p->loc_Color_Shirt                = qglGetUniformLocation(p->program, "Color_Shirt");
1208                 p->loc_DeferredColor_Ambient      = qglGetUniformLocation(p->program, "DeferredColor_Ambient");
1209                 p->loc_DeferredColor_Diffuse      = qglGetUniformLocation(p->program, "DeferredColor_Diffuse");
1210                 p->loc_DeferredColor_Specular     = qglGetUniformLocation(p->program, "DeferredColor_Specular");
1211                 p->loc_DeferredMod_Diffuse        = qglGetUniformLocation(p->program, "DeferredMod_Diffuse");
1212                 p->loc_DeferredMod_Specular       = qglGetUniformLocation(p->program, "DeferredMod_Specular");
1213                 p->loc_DistortScaleRefractReflect = qglGetUniformLocation(p->program, "DistortScaleRefractReflect");
1214                 p->loc_EyePosition                = qglGetUniformLocation(p->program, "EyePosition");
1215                 p->loc_FogColor                   = qglGetUniformLocation(p->program, "FogColor");
1216                 p->loc_FogHeightFade              = qglGetUniformLocation(p->program, "FogHeightFade");
1217                 p->loc_FogPlane                   = qglGetUniformLocation(p->program, "FogPlane");
1218                 p->loc_FogPlaneViewDist           = qglGetUniformLocation(p->program, "FogPlaneViewDist");
1219                 p->loc_FogRangeRecip              = qglGetUniformLocation(p->program, "FogRangeRecip");
1220                 p->loc_LightColor                 = qglGetUniformLocation(p->program, "LightColor");
1221                 p->loc_LightGridMatrix            = qglGetUniformLocation(p->program, "LightGridMatrix");
1222                 p->loc_LightGridNormalMatrix      = qglGetUniformLocation(p->program, "LightGridNormalMatrix");
1223                 p->loc_LightDir                   = qglGetUniformLocation(p->program, "LightDir");
1224                 p->loc_LightPosition              = qglGetUniformLocation(p->program, "LightPosition");
1225                 p->loc_OffsetMapping_ScaleSteps   = qglGetUniformLocation(p->program, "OffsetMapping_ScaleSteps");
1226                 p->loc_OffsetMapping_LodDistance  = qglGetUniformLocation(p->program, "OffsetMapping_LodDistance");
1227                 p->loc_OffsetMapping_Bias         = qglGetUniformLocation(p->program, "OffsetMapping_Bias");
1228                 p->loc_PixelSize                  = qglGetUniformLocation(p->program, "PixelSize");
1229                 p->loc_ReflectColor               = qglGetUniformLocation(p->program, "ReflectColor");
1230                 p->loc_ReflectFactor              = qglGetUniformLocation(p->program, "ReflectFactor");
1231                 p->loc_ReflectOffset              = qglGetUniformLocation(p->program, "ReflectOffset");
1232                 p->loc_RefractColor               = qglGetUniformLocation(p->program, "RefractColor");
1233                 p->loc_Saturation                 = qglGetUniformLocation(p->program, "Saturation");
1234                 p->loc_ScreenCenterRefractReflect = qglGetUniformLocation(p->program, "ScreenCenterRefractReflect");
1235                 p->loc_ScreenScaleRefractReflect  = qglGetUniformLocation(p->program, "ScreenScaleRefractReflect");
1236                 p->loc_ScreenToDepth              = qglGetUniformLocation(p->program, "ScreenToDepth");
1237                 p->loc_ShadowMap_Parameters       = qglGetUniformLocation(p->program, "ShadowMap_Parameters");
1238                 p->loc_ShadowMap_TextureScale     = qglGetUniformLocation(p->program, "ShadowMap_TextureScale");
1239                 p->loc_SpecularPower              = qglGetUniformLocation(p->program, "SpecularPower");
1240                 p->loc_UserVec1                   = qglGetUniformLocation(p->program, "UserVec1");
1241                 p->loc_UserVec2                   = qglGetUniformLocation(p->program, "UserVec2");
1242                 p->loc_UserVec3                   = qglGetUniformLocation(p->program, "UserVec3");
1243                 p->loc_UserVec4                   = qglGetUniformLocation(p->program, "UserVec4");
1244                 p->loc_ColorFringe                = qglGetUniformLocation(p->program, "ColorFringe");
1245                 p->loc_ViewTintColor              = qglGetUniformLocation(p->program, "ViewTintColor");
1246                 p->loc_ViewToLight                = qglGetUniformLocation(p->program, "ViewToLight");
1247                 p->loc_ModelToLight               = qglGetUniformLocation(p->program, "ModelToLight");
1248                 p->loc_TexMatrix                  = qglGetUniformLocation(p->program, "TexMatrix");
1249                 p->loc_BackgroundTexMatrix        = qglGetUniformLocation(p->program, "BackgroundTexMatrix");
1250                 p->loc_ModelViewMatrix            = qglGetUniformLocation(p->program, "ModelViewMatrix");
1251                 p->loc_ModelViewProjectionMatrix  = qglGetUniformLocation(p->program, "ModelViewProjectionMatrix");
1252                 p->loc_PixelToScreenTexCoord      = qglGetUniformLocation(p->program, "PixelToScreenTexCoord");
1253                 p->loc_ModelToReflectCube         = qglGetUniformLocation(p->program, "ModelToReflectCube");
1254                 p->loc_ShadowMapMatrix            = qglGetUniformLocation(p->program, "ShadowMapMatrix");
1255                 p->loc_BloomColorSubtract         = qglGetUniformLocation(p->program, "BloomColorSubtract");
1256                 p->loc_NormalmapScrollBlend       = qglGetUniformLocation(p->program, "NormalmapScrollBlend");
1257                 p->loc_BounceGridMatrix           = qglGetUniformLocation(p->program, "BounceGridMatrix");
1258                 p->loc_BounceGridIntensity        = qglGetUniformLocation(p->program, "BounceGridIntensity");
1259                 // initialize the samplers to refer to the texture units we use
1260                 p->tex_Texture_First = -1;
1261                 p->tex_Texture_Second = -1;
1262                 p->tex_Texture_GammaRamps = -1;
1263                 p->tex_Texture_Normal = -1;
1264                 p->tex_Texture_Color = -1;
1265                 p->tex_Texture_Gloss = -1;
1266                 p->tex_Texture_Glow = -1;
1267                 p->tex_Texture_SecondaryNormal = -1;
1268                 p->tex_Texture_SecondaryColor = -1;
1269                 p->tex_Texture_SecondaryGloss = -1;
1270                 p->tex_Texture_SecondaryGlow = -1;
1271                 p->tex_Texture_Pants = -1;
1272                 p->tex_Texture_Shirt = -1;
1273                 p->tex_Texture_FogHeightTexture = -1;
1274                 p->tex_Texture_FogMask = -1;
1275                 p->tex_Texture_LightGrid = -1;
1276                 p->tex_Texture_Lightmap = -1;
1277                 p->tex_Texture_Deluxemap = -1;
1278                 p->tex_Texture_Attenuation = -1;
1279                 p->tex_Texture_Cube = -1;
1280                 p->tex_Texture_Refraction = -1;
1281                 p->tex_Texture_Reflection = -1;
1282                 p->tex_Texture_ShadowMap2D = -1;
1283                 p->tex_Texture_CubeProjection = -1;
1284                 p->tex_Texture_ScreenNormalMap = -1;
1285                 p->tex_Texture_ScreenDiffuse = -1;
1286                 p->tex_Texture_ScreenSpecular = -1;
1287                 p->tex_Texture_ReflectMask = -1;
1288                 p->tex_Texture_ReflectCube = -1;
1289                 p->tex_Texture_BounceGrid = -1;
1290                 // bind the texture samplers in use
1291                 sampler = 0;
1292                 if (p->loc_Texture_First           >= 0) {p->tex_Texture_First            = sampler;qglUniform1i(p->loc_Texture_First           , sampler);sampler++;}
1293                 if (p->loc_Texture_Second          >= 0) {p->tex_Texture_Second           = sampler;qglUniform1i(p->loc_Texture_Second          , sampler);sampler++;}
1294                 if (p->loc_Texture_GammaRamps      >= 0) {p->tex_Texture_GammaRamps       = sampler;qglUniform1i(p->loc_Texture_GammaRamps      , sampler);sampler++;}
1295                 if (p->loc_Texture_Normal          >= 0) {p->tex_Texture_Normal           = sampler;qglUniform1i(p->loc_Texture_Normal          , sampler);sampler++;}
1296                 if (p->loc_Texture_Color           >= 0) {p->tex_Texture_Color            = sampler;qglUniform1i(p->loc_Texture_Color           , sampler);sampler++;}
1297                 if (p->loc_Texture_Gloss           >= 0) {p->tex_Texture_Gloss            = sampler;qglUniform1i(p->loc_Texture_Gloss           , sampler);sampler++;}
1298                 if (p->loc_Texture_Glow            >= 0) {p->tex_Texture_Glow             = sampler;qglUniform1i(p->loc_Texture_Glow            , sampler);sampler++;}
1299                 if (p->loc_Texture_SecondaryNormal >= 0) {p->tex_Texture_SecondaryNormal  = sampler;qglUniform1i(p->loc_Texture_SecondaryNormal , sampler);sampler++;}
1300                 if (p->loc_Texture_SecondaryColor  >= 0) {p->tex_Texture_SecondaryColor   = sampler;qglUniform1i(p->loc_Texture_SecondaryColor  , sampler);sampler++;}
1301                 if (p->loc_Texture_SecondaryGloss  >= 0) {p->tex_Texture_SecondaryGloss   = sampler;qglUniform1i(p->loc_Texture_SecondaryGloss  , sampler);sampler++;}
1302                 if (p->loc_Texture_SecondaryGlow   >= 0) {p->tex_Texture_SecondaryGlow    = sampler;qglUniform1i(p->loc_Texture_SecondaryGlow   , sampler);sampler++;}
1303                 if (p->loc_Texture_Pants           >= 0) {p->tex_Texture_Pants            = sampler;qglUniform1i(p->loc_Texture_Pants           , sampler);sampler++;}
1304                 if (p->loc_Texture_Shirt           >= 0) {p->tex_Texture_Shirt            = sampler;qglUniform1i(p->loc_Texture_Shirt           , sampler);sampler++;}
1305                 if (p->loc_Texture_FogHeightTexture>= 0) {p->tex_Texture_FogHeightTexture = sampler;qglUniform1i(p->loc_Texture_FogHeightTexture, sampler);sampler++;}
1306                 if (p->loc_Texture_FogMask         >= 0) {p->tex_Texture_FogMask          = sampler;qglUniform1i(p->loc_Texture_FogMask         , sampler);sampler++;}
1307                 if (p->loc_Texture_LightGrid       >= 0) {p->tex_Texture_LightGrid        = sampler;qglUniform1i(p->loc_Texture_LightGrid       , sampler);sampler++;}
1308                 if (p->loc_Texture_Lightmap        >= 0) {p->tex_Texture_Lightmap         = sampler;qglUniform1i(p->loc_Texture_Lightmap        , sampler);sampler++;}
1309                 if (p->loc_Texture_Deluxemap       >= 0) {p->tex_Texture_Deluxemap        = sampler;qglUniform1i(p->loc_Texture_Deluxemap       , sampler);sampler++;}
1310                 if (p->loc_Texture_Attenuation     >= 0) {p->tex_Texture_Attenuation      = sampler;qglUniform1i(p->loc_Texture_Attenuation     , sampler);sampler++;}
1311                 if (p->loc_Texture_Cube            >= 0) {p->tex_Texture_Cube             = sampler;qglUniform1i(p->loc_Texture_Cube            , sampler);sampler++;}
1312                 if (p->loc_Texture_Refraction      >= 0) {p->tex_Texture_Refraction       = sampler;qglUniform1i(p->loc_Texture_Refraction      , sampler);sampler++;}
1313                 if (p->loc_Texture_Reflection      >= 0) {p->tex_Texture_Reflection       = sampler;qglUniform1i(p->loc_Texture_Reflection      , sampler);sampler++;}
1314                 if (p->loc_Texture_ShadowMap2D     >= 0) {p->tex_Texture_ShadowMap2D      = sampler;qglUniform1i(p->loc_Texture_ShadowMap2D     , sampler);sampler++;}
1315                 if (p->loc_Texture_CubeProjection  >= 0) {p->tex_Texture_CubeProjection   = sampler;qglUniform1i(p->loc_Texture_CubeProjection  , sampler);sampler++;}
1316                 if (p->loc_Texture_ScreenNormalMap >= 0) {p->tex_Texture_ScreenNormalMap  = sampler;qglUniform1i(p->loc_Texture_ScreenNormalMap , sampler);sampler++;}
1317                 if (p->loc_Texture_ScreenDiffuse   >= 0) {p->tex_Texture_ScreenDiffuse    = sampler;qglUniform1i(p->loc_Texture_ScreenDiffuse   , sampler);sampler++;}
1318                 if (p->loc_Texture_ScreenSpecular  >= 0) {p->tex_Texture_ScreenSpecular   = sampler;qglUniform1i(p->loc_Texture_ScreenSpecular  , sampler);sampler++;}
1319                 if (p->loc_Texture_ReflectMask     >= 0) {p->tex_Texture_ReflectMask      = sampler;qglUniform1i(p->loc_Texture_ReflectMask     , sampler);sampler++;}
1320                 if (p->loc_Texture_ReflectCube     >= 0) {p->tex_Texture_ReflectCube      = sampler;qglUniform1i(p->loc_Texture_ReflectCube     , sampler);sampler++;}
1321                 if (p->loc_Texture_BounceGrid      >= 0) {p->tex_Texture_BounceGrid       = sampler;qglUniform1i(p->loc_Texture_BounceGrid      , sampler);sampler++;}
1322                 // get the uniform block indices so we can bind them
1323                 p->ubiloc_Skeletal_Transform12_UniformBlock = -1;
1324 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1325                 p->ubiloc_Skeletal_Transform12_UniformBlock = qglGetUniformBlockIndex(p->program, "Skeletal_Transform12_UniformBlock");
1326 #endif
1327                 // clear the uniform block bindings
1328                 p->ubibind_Skeletal_Transform12_UniformBlock = -1;
1329                 // bind the uniform blocks in use
1330                 ubibind = 0;
1331 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1332                 if (p->ubiloc_Skeletal_Transform12_UniformBlock >= 0) {p->ubibind_Skeletal_Transform12_UniformBlock = ubibind;qglUniformBlockBinding(p->program, p->ubiloc_Skeletal_Transform12_UniformBlock, ubibind);ubibind++;}
1333 #endif
1334                 // we're done compiling and setting up the shader, at least until it is used
1335                 CHECKGLERROR
1336                 Con_DPrintf("^5GLSL shader %s compiled (%i textures).\n", permutationname, sampler);
1337         }
1338         else
1339                 Con_Printf("^1GLSL shader %s failed!  some features may not work properly.\n", permutationname);
1340
1341         // free the strings
1342         if (sourcestring)
1343                 Mem_Free(sourcestring);
1344 }
1345
1346 static void R_SetupShader_SetPermutationGLSL(unsigned int mode, uint64_t permutation)
1347 {
1348         r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
1349         if (r_glsl_permutation != perm)
1350         {
1351                 r_glsl_permutation = perm;
1352                 if (!r_glsl_permutation->program)
1353                 {
1354                         if (!r_glsl_permutation->compiled)
1355                         {
1356                                 Con_DPrintf("Compiling shader mode %u permutation %" PRIx64 "\n", mode, permutation);
1357                                 R_GLSL_CompilePermutation(perm, mode, permutation);
1358                         }
1359                         if (!r_glsl_permutation->program)
1360                         {
1361                                 // remove features until we find a valid permutation
1362                                 int i;
1363                                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1364                                 {
1365                                         // reduce i more quickly whenever it would not remove any bits
1366                                         uint64_t j = 1ll<<(SHADERPERMUTATION_COUNT-1-i);
1367                                         if (!(permutation & j))
1368                                                 continue;
1369                                         permutation -= j;
1370                                         r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
1371                                         if (!r_glsl_permutation->compiled)
1372                                                 R_GLSL_CompilePermutation(perm, mode, permutation);
1373                                         if (r_glsl_permutation->program)
1374                                                 break;
1375                                 }
1376                                 if (i >= SHADERPERMUTATION_COUNT)
1377                                 {
1378                                         //Con_Printf("Could not find a working OpenGL 2.0 shader for permutation %s %s\n", shadermodeinfo[mode].filename, shadermodeinfo[mode].pretext);
1379                                         r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
1380                                         qglUseProgram(0);CHECKGLERROR
1381                                         return; // no bit left to clear, entire mode is broken
1382                                 }
1383                         }
1384                 }
1385                 CHECKGLERROR
1386                 qglUseProgram(r_glsl_permutation->program);CHECKGLERROR
1387         }
1388         if (r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
1389         if (r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
1390         if (r_glsl_permutation->loc_ClientTime >= 0) qglUniform1f(r_glsl_permutation->loc_ClientTime, cl.time);
1391         CHECKGLERROR
1392 }
1393
1394 void R_GLSL_Restart_f(cmd_state_t *cmd)
1395 {
1396         unsigned int i, limit;
1397         switch(vid.renderpath)
1398         {
1399         case RENDERPATH_GL32:
1400         case RENDERPATH_GLES2:
1401                 {
1402                         r_glsl_permutation_t *p;
1403                         r_glsl_permutation = NULL;
1404                         limit = (unsigned int)Mem_ExpandableArray_IndexRange(&r_glsl_permutationarray);
1405                         for (i = 0;i < limit;i++)
1406                         {
1407                                 if ((p = (r_glsl_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_glsl_permutationarray, i)))
1408                                 {
1409                                         GL_Backend_FreeProgram(p->program);
1410                                         Mem_ExpandableArray_FreeRecord(&r_glsl_permutationarray, (void*)p);
1411                                 }
1412                         }
1413                         memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
1414                 }
1415                 break;
1416         }
1417 }
1418
1419 static void R_GLSL_DumpShader_f(cmd_state_t *cmd)
1420 {
1421         int i, language, mode, dupe;
1422         char *text;
1423         shadermodeinfo_t *modeinfo;
1424         qfile_t *file;
1425
1426         for (language = 0;language < SHADERLANGUAGE_COUNT;language++)
1427         {
1428                 modeinfo = shadermodeinfo[language];
1429                 for (mode = 0;mode < SHADERMODE_COUNT;mode++)
1430                 {
1431                         // don't dump the same file multiple times (most or all shaders come from the same file)
1432                         for (dupe = mode - 1;dupe >= 0;dupe--)
1433                                 if (!strcmp(modeinfo[mode].filename, modeinfo[dupe].filename))
1434                                         break;
1435                         if (dupe >= 0)
1436                                 continue;
1437                         text = modeinfo[mode].builtinstring;
1438                         if (!text)
1439                                 continue;
1440                         file = FS_OpenRealFile(modeinfo[mode].filename, "w", false);
1441                         if (file)
1442                         {
1443                                 FS_Print(file, "/* The engine may define the following macros:\n");
1444                                 FS_Print(file, "#define VERTEX_SHADER\n#define GEOMETRY_SHADER\n#define FRAGMENT_SHADER\n");
1445                                 for (i = 0;i < SHADERMODE_COUNT;i++)
1446                                         FS_Print(file, modeinfo[i].pretext);
1447                                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1448                                         FS_Print(file, shaderpermutationinfo[i].pretext);
1449                                 FS_Print(file, "*/\n");
1450                                 FS_Print(file, text);
1451                                 FS_Close(file);
1452                                 Con_Printf("%s written\n", modeinfo[mode].filename);
1453                         }
1454                         else
1455                                 Con_Printf(CON_ERROR "failed to write to %s\n", modeinfo[mode].filename);
1456                 }
1457         }
1458 }
1459
1460 void R_SetupShader_Generic(rtexture_t *t, qbool usegamma, qbool notrippy, qbool suppresstexalpha)
1461 {
1462         uint64_t permutation = 0;
1463         if (r_trippy.integer && !notrippy)
1464                 permutation |= SHADERPERMUTATION_TRIPPY;
1465         permutation |= SHADERPERMUTATION_VIEWTINT;
1466         if (t)
1467                 permutation |= SHADERPERMUTATION_DIFFUSE;
1468         if (usegamma && v_glslgamma_2d.integer && !vid.sRGB2D && r_texture_gammaramps && !vid_gammatables_trivial)
1469                 permutation |= SHADERPERMUTATION_GAMMARAMPS;
1470         if (suppresstexalpha)
1471                 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1472         if (vid.allowalphatocoverage)
1473                 GL_AlphaToCoverage(false);
1474         switch (vid.renderpath)
1475         {
1476         case RENDERPATH_GL32:
1477         case RENDERPATH_GLES2:
1478                 R_SetupShader_SetPermutationGLSL(SHADERMODE_GENERIC, permutation);
1479                 if (r_glsl_permutation->tex_Texture_First >= 0)
1480                         R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First, t);
1481                 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0)
1482                         R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps);
1483                 break;
1484         }
1485 }
1486
1487 void R_SetupShader_Generic_NoTexture(qbool usegamma, qbool notrippy)
1488 {
1489         R_SetupShader_Generic(NULL, usegamma, notrippy, false);
1490 }
1491
1492 void R_SetupShader_DepthOrShadow(qbool notrippy, qbool depthrgb, qbool skeletal)
1493 {
1494         uint64_t permutation = 0;
1495         if (r_trippy.integer && !notrippy)
1496                 permutation |= SHADERPERMUTATION_TRIPPY;
1497         if (depthrgb)
1498                 permutation |= SHADERPERMUTATION_DEPTHRGB;
1499         if (skeletal)
1500                 permutation |= SHADERPERMUTATION_SKELETAL;
1501
1502         if (vid.allowalphatocoverage)
1503                 GL_AlphaToCoverage(false);
1504         switch (vid.renderpath)
1505         {
1506         case RENDERPATH_GL32:
1507         case RENDERPATH_GLES2:
1508                 R_SetupShader_SetPermutationGLSL(SHADERMODE_DEPTH_OR_SHADOW, permutation);
1509 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1510                 if (r_glsl_permutation->ubiloc_Skeletal_Transform12_UniformBlock >= 0 && rsurface.batchskeletaltransform3x4buffer) qglBindBufferRange(GL_UNIFORM_BUFFER, r_glsl_permutation->ubibind_Skeletal_Transform12_UniformBlock, rsurface.batchskeletaltransform3x4buffer->bufferobject, rsurface.batchskeletaltransform3x4offset, rsurface.batchskeletaltransform3x4size);
1511 #endif
1512                 break;
1513         }
1514 }
1515
1516 #define BLENDFUNC_ALLOWS_COLORMOD      1
1517 #define BLENDFUNC_ALLOWS_FOG           2
1518 #define BLENDFUNC_ALLOWS_FOG_HACK0     4
1519 #define BLENDFUNC_ALLOWS_FOG_HACKALPHA 8
1520 #define BLENDFUNC_ALLOWS_ANYFOG        (BLENDFUNC_ALLOWS_FOG | BLENDFUNC_ALLOWS_FOG_HACK0 | BLENDFUNC_ALLOWS_FOG_HACKALPHA)
1521 static int R_BlendFuncFlags(int src, int dst)
1522 {
1523         int r = 0;
1524
1525         // a blendfunc allows colormod if:
1526         // a) it can never keep the destination pixel invariant, or
1527         // b) it can keep the destination pixel invariant, and still can do so if colormodded
1528         // this is to prevent unintended side effects from colormod
1529
1530         // a blendfunc allows fog if:
1531         // blend(fog(src), fog(dst)) == fog(blend(src, dst))
1532         // this is to prevent unintended side effects from fog
1533
1534         // these checks are the output of fogeval.pl
1535
1536         r |= BLENDFUNC_ALLOWS_COLORMOD;
1537         if(src == GL_DST_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1538         if(src == GL_DST_ALPHA && dst == GL_ONE_MINUS_DST_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1539         if(src == GL_DST_COLOR && dst == GL_ONE_MINUS_SRC_ALPHA) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1540         if(src == GL_DST_COLOR && dst == GL_ONE_MINUS_SRC_COLOR) r |= BLENDFUNC_ALLOWS_FOG;
1541         if(src == GL_DST_COLOR && dst == GL_SRC_ALPHA) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1542         if(src == GL_DST_COLOR && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1543         if(src == GL_DST_COLOR && dst == GL_ZERO) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1544         if(src == GL_ONE && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1545         if(src == GL_ONE && dst == GL_ONE_MINUS_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG_HACKALPHA;
1546         if(src == GL_ONE && dst == GL_ZERO) r |= BLENDFUNC_ALLOWS_FOG;
1547         if(src == GL_ONE_MINUS_DST_ALPHA && dst == GL_DST_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1548         if(src == GL_ONE_MINUS_DST_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1549         if(src == GL_ONE_MINUS_DST_COLOR && dst == GL_SRC_COLOR) r |= BLENDFUNC_ALLOWS_FOG;
1550         if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1551         if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1552         if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1553         if(src == GL_ONE_MINUS_SRC_COLOR && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1554         if(src == GL_SRC_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1555         if(src == GL_SRC_ALPHA && dst == GL_ONE_MINUS_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1556         if(src == GL_ZERO && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG;
1557         if(src == GL_ZERO && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1558
1559         return r;
1560 }
1561
1562 void R_SetupShader_Surface(const float rtlightambient[3], const float rtlightdiffuse[3], const float rtlightspecular[3], rsurfacepass_t rsurfacepass, int texturenumsurfaces, const msurface_t **texturesurfacelist, void *surfacewaterplane, qbool notrippy, qbool ui)
1563 {
1564         // select a permutation of the lighting shader appropriate to this
1565         // combination of texture, entity, light source, and fogging, only use the
1566         // minimum features necessary to avoid wasting rendering time in the
1567         // fragment shader on features that are not being used
1568         uint64_t permutation = 0;
1569         unsigned int mode = 0;
1570         int blendfuncflags;
1571         texture_t *t = rsurface.texture;
1572         float m16f[16];
1573         matrix4x4_t tempmatrix;
1574         r_waterstate_waterplane_t *waterplane = (r_waterstate_waterplane_t *)surfacewaterplane;
1575         if (r_trippy.integer && !notrippy)
1576                 permutation |= SHADERPERMUTATION_TRIPPY;
1577         if (t->currentmaterialflags & MATERIALFLAG_ALPHATEST)
1578                 permutation |= SHADERPERMUTATION_ALPHAKILL;
1579         if (t->currentmaterialflags & MATERIALFLAG_OCCLUDE)
1580                 permutation |= SHADERPERMUTATION_OCCLUDE;
1581         if (t->r_water_waterscroll[0] && t->r_water_waterscroll[1])
1582                 permutation |= SHADERPERMUTATION_NORMALMAPSCROLLBLEND; // todo: make generic
1583         if (rsurfacepass == RSURFPASS_BACKGROUND)
1584         {
1585                 // distorted background
1586                 if (t->currentmaterialflags & MATERIALFLAG_WATERSHADER)
1587                 {
1588                         mode = SHADERMODE_WATER;
1589                         if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1590                                 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1591                         if((r_wateralpha.value < 1) && (t->currentmaterialflags & MATERIALFLAG_WATERALPHA))
1592                         {
1593                                 // this is the right thing to do for wateralpha
1594                                 GL_BlendFunc(GL_ONE, GL_ZERO);
1595                                 blendfuncflags = R_BlendFuncFlags(GL_ONE, GL_ZERO);
1596                         }
1597                         else
1598                         {
1599                                 // this is the right thing to do for entity alpha
1600                                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1601                                 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1602                         }
1603                 }
1604                 else if (t->currentmaterialflags & MATERIALFLAG_REFRACTION)
1605                 {
1606                         mode = SHADERMODE_REFRACTION;
1607                         if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1608                                 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1609                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1610                         blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1611                 }
1612                 else
1613                 {
1614                         mode = SHADERMODE_GENERIC;
1615                         permutation |= SHADERPERMUTATION_DIFFUSE | SHADERPERMUTATION_ALPHAKILL;
1616                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1617                         blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1618                 }
1619                 if (vid.allowalphatocoverage)
1620                         GL_AlphaToCoverage(false);
1621         }
1622         else if (rsurfacepass == RSURFPASS_DEFERREDGEOMETRY)
1623         {
1624                 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1625                 {
1626                         switch(t->offsetmapping)
1627                         {
1628                         case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1629                         case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1630                         case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1631                         case OFFSETMAPPING_OFF: break;
1632                         }
1633                 }
1634                 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1635                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1636                 // normalmap (deferred prepass), may use alpha test on diffuse
1637                 mode = SHADERMODE_DEFERREDGEOMETRY;
1638                 GL_BlendFunc(GL_ONE, GL_ZERO);
1639                 blendfuncflags = R_BlendFuncFlags(GL_ONE, GL_ZERO);
1640                 if (vid.allowalphatocoverage)
1641                         GL_AlphaToCoverage(false);
1642         }
1643         else if (rsurfacepass == RSURFPASS_RTLIGHT)
1644         {
1645                 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1646                 {
1647                         switch(t->offsetmapping)
1648                         {
1649                         case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1650                         case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1651                         case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1652                         case OFFSETMAPPING_OFF: break;
1653                         }
1654                 }
1655                 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1656                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1657                 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1658                         permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1659                 // light source
1660                 mode = SHADERMODE_LIGHTSOURCE;
1661                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1662                         permutation |= SHADERPERMUTATION_CUBEFILTER;
1663                 if (VectorLength2(rtlightdiffuse) > 0)
1664                         permutation |= SHADERPERMUTATION_DIFFUSE;
1665                 if (VectorLength2(rtlightspecular) > 0)
1666                         permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1667                 if (r_refdef.fogenabled)
1668                         permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1669                 if (t->colormapping)
1670                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1671                 if (r_shadow_usingshadowmap2d)
1672                 {
1673                         permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1674                         if(r_shadow_shadowmapvsdct)
1675                                 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
1676
1677                         if (r_shadow_shadowmap2ddepthbuffer)
1678                                 permutation |= SHADERPERMUTATION_DEPTHRGB;
1679                 }
1680                 if (t->reflectmasktexture)
1681                         permutation |= SHADERPERMUTATION_REFLECTCUBE;
1682                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1683                 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE);
1684                 if (vid.allowalphatocoverage)
1685                         GL_AlphaToCoverage(false);
1686         }
1687         else if (t->currentmaterialflags & MATERIALFLAG_LIGHTGRID)
1688         {
1689                 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1690                 {
1691                         switch(t->offsetmapping)
1692                         {
1693                         case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1694                         case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1695                         case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1696                         case OFFSETMAPPING_OFF: break;
1697                         }
1698                 }
1699                 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1700                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1701                 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1702                         permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1703                 // directional model lighting
1704                 mode = SHADERMODE_LIGHTGRID;
1705                 if ((t->glowtexture || t->backgroundglowtexture) && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
1706                         permutation |= SHADERPERMUTATION_GLOW;
1707                 permutation |= SHADERPERMUTATION_DIFFUSE;
1708                 if (t->glosstexture || t->backgroundglosstexture)
1709                         permutation |= SHADERPERMUTATION_SPECULAR;
1710                 if (r_refdef.fogenabled)
1711                         permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1712                 if (t->colormapping)
1713                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1714                 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
1715                 {
1716                         permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1717                         permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1718
1719                         if (r_shadow_shadowmap2ddepthbuffer)
1720                                 permutation |= SHADERPERMUTATION_DEPTHRGB;
1721                 }
1722                 if (t->currentmaterialflags & MATERIALFLAG_REFLECTION)
1723                         permutation |= SHADERPERMUTATION_REFLECTION;
1724                 if (r_shadow_usingdeferredprepass && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
1725                         permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
1726                 if (t->reflectmasktexture)
1727                         permutation |= SHADERPERMUTATION_REFLECTCUBE;
1728                 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1729                 {
1730                         permutation |= SHADERPERMUTATION_BOUNCEGRID;
1731                         if (r_shadow_bouncegrid_state.directional)
1732                                 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
1733                 }
1734                 GL_BlendFunc(t->currentblendfunc[0], t->currentblendfunc[1]);
1735                 blendfuncflags = R_BlendFuncFlags(t->currentblendfunc[0], t->currentblendfunc[1]);
1736                 // when using alphatocoverage, we don't need alphakill
1737                 if (vid.allowalphatocoverage)
1738                 {
1739                         if (r_transparent_alphatocoverage.integer)
1740                         {
1741                                 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1742                                 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1743                         }
1744                         else
1745                                 GL_AlphaToCoverage(false);
1746                 }
1747         }
1748         else if (t->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
1749         {
1750                 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1751                 {
1752                         switch(t->offsetmapping)
1753                         {
1754                         case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1755                         case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1756                         case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1757                         case OFFSETMAPPING_OFF: break;
1758                         }
1759                 }
1760                 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1761                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1762                 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1763                         permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1764                 // directional model lighting
1765                 mode = SHADERMODE_LIGHTDIRECTION;
1766                 if ((t->glowtexture || t->backgroundglowtexture) && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
1767                         permutation |= SHADERPERMUTATION_GLOW;
1768                 if (VectorLength2(t->render_modellight_diffuse))
1769                         permutation |= SHADERPERMUTATION_DIFFUSE;
1770                 if (VectorLength2(t->render_modellight_specular) > 0)
1771                         permutation |= SHADERPERMUTATION_SPECULAR;
1772                 if (r_refdef.fogenabled)
1773                         permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1774                 if (t->colormapping)
1775                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1776                 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
1777                 {
1778                         permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1779                         permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1780
1781                         if (r_shadow_shadowmap2ddepthbuffer)
1782                                 permutation |= SHADERPERMUTATION_DEPTHRGB;
1783                 }
1784                 if (t->currentmaterialflags & MATERIALFLAG_REFLECTION)
1785                         permutation |= SHADERPERMUTATION_REFLECTION;
1786                 if (r_shadow_usingdeferredprepass && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
1787                         permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
1788                 if (t->reflectmasktexture)
1789                         permutation |= SHADERPERMUTATION_REFLECTCUBE;
1790                 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1791                 {
1792                         permutation |= SHADERPERMUTATION_BOUNCEGRID;
1793                         if (r_shadow_bouncegrid_state.directional)
1794                                 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
1795                 }
1796                 GL_BlendFunc(t->currentblendfunc[0], t->currentblendfunc[1]);
1797                 blendfuncflags = R_BlendFuncFlags(t->currentblendfunc[0], t->currentblendfunc[1]);
1798                 // when using alphatocoverage, we don't need alphakill
1799                 if (vid.allowalphatocoverage)
1800                 {
1801                         if (r_transparent_alphatocoverage.integer)
1802                         {
1803                                 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1804                                 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1805                         }
1806                         else
1807                                 GL_AlphaToCoverage(false);
1808                 }
1809         }
1810         else
1811         {
1812                 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1813                 {
1814                         switch(t->offsetmapping)
1815                         {
1816                         case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1817                         case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1818                         case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1819                         case OFFSETMAPPING_OFF: break;
1820                         }
1821                 }
1822                 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1823                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1824                 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1825                         permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1826                 // lightmapped wall
1827                 if ((t->glowtexture || t->backgroundglowtexture) && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
1828                         permutation |= SHADERPERMUTATION_GLOW;
1829                 if (r_refdef.fogenabled && !ui)
1830                         permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1831                 if (t->colormapping)
1832                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1833                 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
1834                 {
1835                         permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1836                         permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1837
1838                         if (r_shadow_shadowmap2ddepthbuffer)
1839                                 permutation |= SHADERPERMUTATION_DEPTHRGB;
1840                 }
1841                 if (t->currentmaterialflags & MATERIALFLAG_REFLECTION)
1842                         permutation |= SHADERPERMUTATION_REFLECTION;
1843                 if (r_shadow_usingdeferredprepass && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
1844                         permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
1845                 if (t->reflectmasktexture)
1846                         permutation |= SHADERPERMUTATION_REFLECTCUBE;
1847                 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping)
1848                 {
1849                         // deluxemapping (light direction texture)
1850                         if (rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping && r_refdef.scene.worldmodel->brushq3.deluxemapping_modelspace)
1851                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
1852                         else
1853                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1854                         permutation |= SHADERPERMUTATION_DIFFUSE;
1855                         if (VectorLength2(t->render_lightmap_specular) > 0)
1856                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1857                 }
1858                 else if (r_glsl_deluxemapping.integer >= 2)
1859                 {
1860                         // fake deluxemapping (uniform light direction in tangentspace)
1861                         if (rsurface.uselightmaptexture)
1862                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP;
1863                         else
1864                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR;
1865                         permutation |= SHADERPERMUTATION_DIFFUSE;
1866                         if (VectorLength2(t->render_lightmap_specular) > 0)
1867                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1868                 }
1869                 else if (rsurface.uselightmaptexture)
1870                 {
1871                         // ordinary lightmapping (q1bsp, q3bsp)
1872                         mode = SHADERMODE_LIGHTMAP;
1873                 }
1874                 else
1875                 {
1876                         // ordinary vertex coloring (q3bsp)
1877                         mode = SHADERMODE_VERTEXCOLOR;
1878                 }
1879                 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1880                 {
1881                         permutation |= SHADERPERMUTATION_BOUNCEGRID;
1882                         if (r_shadow_bouncegrid_state.directional)
1883                                 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
1884                 }
1885                 GL_BlendFunc(t->currentblendfunc[0], t->currentblendfunc[1]);
1886                 blendfuncflags = R_BlendFuncFlags(t->currentblendfunc[0], t->currentblendfunc[1]);
1887                 // when using alphatocoverage, we don't need alphakill
1888                 if (vid.allowalphatocoverage)
1889                 {
1890                         if (r_transparent_alphatocoverage.integer)
1891                         {
1892                                 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1893                                 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1894                         }
1895                         else
1896                                 GL_AlphaToCoverage(false);
1897                 }
1898         }
1899         if(!(blendfuncflags & BLENDFUNC_ALLOWS_ANYFOG))
1900                 permutation &= ~(SHADERPERMUTATION_FOGHEIGHTTEXTURE | SHADERPERMUTATION_FOGOUTSIDE | SHADERPERMUTATION_FOGINSIDE);
1901         if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACKALPHA && !ui)
1902                 permutation |= SHADERPERMUTATION_FOGALPHAHACK;
1903         switch(vid.renderpath)
1904         {
1905         case RENDERPATH_GL32:
1906         case RENDERPATH_GLES2:
1907                 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | (rsurface.modellightmapcolor4f ? BATCHNEED_ARRAY_VERTEXCOLOR : 0) | BATCHNEED_ARRAY_TEXCOORD | (rsurface.uselightmaptexture ? BATCHNEED_ARRAY_LIGHTMAP : 0) | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
1908                 RSurf_UploadBuffersForBatch();
1909                 // this has to be after RSurf_PrepareVerticesForBatch
1910                 if (rsurface.batchskeletaltransform3x4buffer)
1911                         permutation |= SHADERPERMUTATION_SKELETAL;
1912                 R_SetupShader_SetPermutationGLSL(mode, permutation);
1913 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1914                 if (r_glsl_permutation->ubiloc_Skeletal_Transform12_UniformBlock >= 0 && rsurface.batchskeletaltransform3x4buffer) qglBindBufferRange(GL_UNIFORM_BUFFER, r_glsl_permutation->ubibind_Skeletal_Transform12_UniformBlock, rsurface.batchskeletaltransform3x4buffer->bufferobject, rsurface.batchskeletaltransform3x4offset, rsurface.batchskeletaltransform3x4size);
1915 #endif
1916                 if (r_glsl_permutation->loc_ModelToReflectCube >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.matrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ModelToReflectCube, 1, false, m16f);}
1917                 if (mode == SHADERMODE_LIGHTSOURCE)
1918                 {
1919                         if (r_glsl_permutation->loc_ModelToLight >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ModelToLight, 1, false, m16f);}
1920                         if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3f(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
1921                         if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3f(r_glsl_permutation->loc_LightColor, 1, 1, 1); // DEPRECATED
1922                         if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, rtlightambient[0], rtlightambient[1], rtlightambient[2]);
1923                         if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, rtlightdiffuse[0], rtlightdiffuse[1], rtlightdiffuse[2]);
1924                         if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, rtlightspecular[0], rtlightspecular[1], rtlightspecular[2]);
1925         
1926                         // additive passes are only darkened by fog, not tinted
1927                         if (r_glsl_permutation->loc_FogColor >= 0)
1928                                 qglUniform3f(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1929                         if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1f(r_glsl_permutation->loc_SpecularPower, t->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
1930                 }
1931                 else
1932                 {
1933                         if (mode == SHADERMODE_FLATCOLOR)
1934                         {
1935                                 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, t->render_modellight_ambient[0], t->render_modellight_ambient[1], t->render_modellight_ambient[2]);
1936                         }
1937                         else if (mode == SHADERMODE_LIGHTGRID)
1938                         {
1939                                 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, t->render_lightmap_ambient[0], t->render_lightmap_ambient[1], t->render_lightmap_ambient[2]);
1940                                 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, t->render_lightmap_diffuse[0], t->render_lightmap_diffuse[1], t->render_lightmap_diffuse[2]);
1941                                 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, t->render_lightmap_specular[0], t->render_lightmap_specular[1], t->render_lightmap_specular[2]);
1942                                 // other LightGrid uniforms handled below
1943                         }
1944                         else if (mode == SHADERMODE_LIGHTDIRECTION)
1945                         {
1946                                 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, t->render_modellight_ambient[0], t->render_modellight_ambient[1], t->render_modellight_ambient[2]);
1947                                 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, t->render_modellight_diffuse[0], t->render_modellight_diffuse[1], t->render_modellight_diffuse[2]);
1948                                 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, t->render_modellight_specular[0], t->render_modellight_specular[1], t->render_modellight_specular[2]);
1949                                 if (r_glsl_permutation->loc_DeferredMod_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Diffuse, t->render_rtlight_diffuse[0], t->render_rtlight_diffuse[1], t->render_rtlight_diffuse[2]);
1950                                 if (r_glsl_permutation->loc_DeferredMod_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Specular, t->render_rtlight_specular[0], t->render_rtlight_specular[1], t->render_rtlight_specular[2]);
1951                                 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3f(r_glsl_permutation->loc_LightColor, 1, 1, 1); // DEPRECATED
1952                                 if (r_glsl_permutation->loc_LightDir >= 0) qglUniform3f(r_glsl_permutation->loc_LightDir, t->render_modellight_lightdir_local[0], t->render_modellight_lightdir_local[1], t->render_modellight_lightdir_local[2]);
1953                         }
1954                         else
1955                         {
1956                                 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, t->render_lightmap_ambient[0], t->render_lightmap_ambient[1], t->render_lightmap_ambient[2]);
1957                                 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, t->render_lightmap_diffuse[0], t->render_lightmap_diffuse[1], t->render_lightmap_diffuse[2]);
1958                                 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, t->render_lightmap_specular[0], t->render_lightmap_specular[1], t->render_lightmap_specular[2]);
1959                                 if (r_glsl_permutation->loc_DeferredMod_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Diffuse, t->render_rtlight_diffuse[0], t->render_rtlight_diffuse[1], t->render_rtlight_diffuse[2]);
1960                                 if (r_glsl_permutation->loc_DeferredMod_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Specular, t->render_rtlight_specular[0], t->render_rtlight_specular[1], t->render_rtlight_specular[2]);
1961                         }
1962                         // additive passes are only darkened by fog, not tinted
1963                         if (r_glsl_permutation->loc_FogColor >= 0 && !ui)
1964                         {
1965                                 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACK0)
1966                                         qglUniform3f(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1967                                 else
1968                                         qglUniform3f(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
1969                         }
1970                         if (r_glsl_permutation->loc_DistortScaleRefractReflect >= 0) qglUniform4f(r_glsl_permutation->loc_DistortScaleRefractReflect, r_water_refractdistort.value * t->refractfactor, r_water_refractdistort.value * t->refractfactor, r_water_reflectdistort.value * t->reflectfactor, r_water_reflectdistort.value * t->reflectfactor);
1971                         if (r_glsl_permutation->loc_ScreenScaleRefractReflect >= 0) qglUniform4f(r_glsl_permutation->loc_ScreenScaleRefractReflect, r_fb.water.screenscale[0], r_fb.water.screenscale[1], r_fb.water.screenscale[0], r_fb.water.screenscale[1]);
1972                         if (r_glsl_permutation->loc_ScreenCenterRefractReflect >= 0) qglUniform4f(r_glsl_permutation->loc_ScreenCenterRefractReflect, r_fb.water.screencenter[0], r_fb.water.screencenter[1], r_fb.water.screencenter[0], r_fb.water.screencenter[1]);
1973                         if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4f(r_glsl_permutation->loc_RefractColor, t->refractcolor4f[0], t->refractcolor4f[1], t->refractcolor4f[2], t->refractcolor4f[3] * t->currentalpha);
1974                         if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4f(r_glsl_permutation->loc_ReflectColor, t->reflectcolor4f[0], t->reflectcolor4f[1], t->reflectcolor4f[2], t->reflectcolor4f[3] * t->currentalpha);
1975                         if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1f(r_glsl_permutation->loc_ReflectFactor, t->reflectmax - t->reflectmin);
1976                         if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1f(r_glsl_permutation->loc_ReflectOffset, t->reflectmin);
1977                         if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1f(r_glsl_permutation->loc_SpecularPower, t->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
1978                         if (r_glsl_permutation->loc_NormalmapScrollBlend >= 0) qglUniform2f(r_glsl_permutation->loc_NormalmapScrollBlend, t->r_water_waterscroll[0], t->r_water_waterscroll[1]);
1979                 }
1980                 if (r_glsl_permutation->loc_TexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&t->currenttexmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_TexMatrix, 1, false, m16f);}
1981                 if (r_glsl_permutation->loc_BackgroundTexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&t->currentbackgroundtexmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_BackgroundTexMatrix, 1, false, m16f);}
1982                 if (r_glsl_permutation->loc_ShadowMapMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&r_shadow_shadowmapmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ShadowMapMatrix, 1, false, m16f);}
1983                 if (permutation & SHADERPERMUTATION_SHADOWMAPORTHO)
1984                 {
1985                         if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform4f(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_modelshadowmap_texturescale[0], r_shadow_modelshadowmap_texturescale[1], r_shadow_modelshadowmap_texturescale[2], r_shadow_modelshadowmap_texturescale[3]);
1986                         if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4f(r_glsl_permutation->loc_ShadowMap_Parameters, r_shadow_modelshadowmap_parameters[0], r_shadow_modelshadowmap_parameters[1], r_shadow_modelshadowmap_parameters[2], r_shadow_modelshadowmap_parameters[3]);
1987                 }
1988                 else
1989                 {
1990                         if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform4f(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_lightshadowmap_texturescale[0], r_shadow_lightshadowmap_texturescale[1], r_shadow_lightshadowmap_texturescale[2], r_shadow_lightshadowmap_texturescale[3]);
1991                         if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4f(r_glsl_permutation->loc_ShadowMap_Parameters, r_shadow_lightshadowmap_parameters[0], r_shadow_lightshadowmap_parameters[1], r_shadow_lightshadowmap_parameters[2], r_shadow_lightshadowmap_parameters[3]);
1992                 }
1993
1994                 if (r_glsl_permutation->loc_Color_Glow >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Glow, t->render_glowmod[0], t->render_glowmod[1], t->render_glowmod[2]);
1995                 if (r_glsl_permutation->loc_Alpha >= 0) qglUniform1f(r_glsl_permutation->loc_Alpha, t->currentalpha * ((t->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay) ? t->r_water_wateralpha : 1));
1996                 if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3f(r_glsl_permutation->loc_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
1997                 if (r_glsl_permutation->loc_Color_Pants >= 0)
1998                 {
1999                         if (t->pantstexture)
2000                                 qglUniform3f(r_glsl_permutation->loc_Color_Pants, t->render_colormap_pants[0], t->render_colormap_pants[1], t->render_colormap_pants[2]);
2001                         else
2002                                 qglUniform3f(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
2003                 }
2004                 if (r_glsl_permutation->loc_Color_Shirt >= 0)
2005                 {
2006                         if (t->shirttexture)
2007                                 qglUniform3f(r_glsl_permutation->loc_Color_Shirt, t->render_colormap_shirt[0], t->render_colormap_shirt[1], t->render_colormap_shirt[2]);
2008                         else
2009                                 qglUniform3f(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
2010                 }
2011                 if (r_glsl_permutation->loc_FogPlane >= 0) qglUniform4f(r_glsl_permutation->loc_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]);
2012                 if (r_glsl_permutation->loc_FogPlaneViewDist >= 0) qglUniform1f(r_glsl_permutation->loc_FogPlaneViewDist, rsurface.fogplaneviewdist);
2013                 if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1f(r_glsl_permutation->loc_FogRangeRecip, rsurface.fograngerecip);
2014                 if (r_glsl_permutation->loc_FogHeightFade >= 0) qglUniform1f(r_glsl_permutation->loc_FogHeightFade, rsurface.fogheightfade);
2015                 if (r_glsl_permutation->loc_OffsetMapping_ScaleSteps >= 0) qglUniform4f(r_glsl_permutation->loc_OffsetMapping_ScaleSteps,
2016                                 r_glsl_offsetmapping_scale.value*t->offsetscale,
2017                                 max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2018                                 1.0 / max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2019                                 max(1, r_glsl_offsetmapping_reliefmapping_refinesteps.integer)
2020                         );
2021                 if (r_glsl_permutation->loc_OffsetMapping_LodDistance >= 0) qglUniform1f(r_glsl_permutation->loc_OffsetMapping_LodDistance, r_glsl_offsetmapping_lod_distance.integer * r_refdef.view.quality);
2022                 if (r_glsl_permutation->loc_OffsetMapping_Bias >= 0) qglUniform1f(r_glsl_permutation->loc_OffsetMapping_Bias, t->offsetbias);
2023                 if (r_glsl_permutation->loc_ScreenToDepth >= 0) qglUniform2f(r_glsl_permutation->loc_ScreenToDepth, r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]);
2024                 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
2025                 if (r_glsl_permutation->loc_BounceGridMatrix >= 0) {Matrix4x4_Concat(&tempmatrix, &r_shadow_bouncegrid_state.matrix, &rsurface.matrix);Matrix4x4_ToArrayFloatGL(&tempmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_BounceGridMatrix, 1, false, m16f);}
2026                 if (r_glsl_permutation->loc_BounceGridIntensity >= 0) qglUniform1f(r_glsl_permutation->loc_BounceGridIntensity, r_shadow_bouncegrid_state.intensity*r_refdef.view.colorscale);
2027                 if (r_glsl_permutation->loc_LightGridMatrix >= 0 && r_refdef.scene.worldmodel)
2028                 {
2029                         float m9f[9];
2030                         Matrix4x4_Concat(&tempmatrix, &r_refdef.scene.worldmodel->brushq3.lightgridworldtotexturematrix, &rsurface.matrix);
2031                         Matrix4x4_ToArrayFloatGL(&tempmatrix, m16f);
2032                         qglUniformMatrix4fv(r_glsl_permutation->loc_LightGridMatrix, 1, false, m16f);
2033                         Matrix4x4_Normalize3(&tempmatrix, &rsurface.matrix);
2034                         Matrix4x4_ToArrayFloatGL(&tempmatrix, m16f);
2035                         m9f[0] = m16f[0];m9f[1] = m16f[1];m9f[2] = m16f[2];
2036                         m9f[3] = m16f[4];m9f[4] = m16f[5];m9f[5] = m16f[6];
2037                         m9f[6] = m16f[8];m9f[7] = m16f[9];m9f[8] = m16f[10];
2038                         qglUniformMatrix3fv(r_glsl_permutation->loc_LightGridNormalMatrix, 1, false, m9f);
2039                 }
2040
2041                 if (r_glsl_permutation->tex_Texture_First           >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First            , r_texture_white                                     );
2042                 if (r_glsl_permutation->tex_Texture_Second          >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second           , r_texture_white                                     );
2043                 if (r_glsl_permutation->tex_Texture_GammaRamps      >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps       , r_texture_gammaramps                                );
2044                 if (r_glsl_permutation->tex_Texture_Normal          >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Normal           , t->nmaptexture                       );
2045                 if (r_glsl_permutation->tex_Texture_Color           >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Color            , t->basetexture                       );
2046                 if (r_glsl_permutation->tex_Texture_Gloss           >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Gloss            , t->glosstexture                      );
2047                 if (r_glsl_permutation->tex_Texture_Glow            >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Glow             , t->glowtexture                       );
2048                 if (r_glsl_permutation->tex_Texture_SecondaryNormal >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryNormal  , t->backgroundnmaptexture             );
2049                 if (r_glsl_permutation->tex_Texture_SecondaryColor  >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryColor   , t->backgroundbasetexture             );
2050                 if (r_glsl_permutation->tex_Texture_SecondaryGloss  >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryGloss   , t->backgroundglosstexture            );
2051                 if (r_glsl_permutation->tex_Texture_SecondaryGlow   >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryGlow    , t->backgroundglowtexture             );
2052                 if (r_glsl_permutation->tex_Texture_Pants           >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Pants            , t->pantstexture                      );
2053                 if (r_glsl_permutation->tex_Texture_Shirt           >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Shirt            , t->shirttexture                      );
2054                 if (r_glsl_permutation->tex_Texture_ReflectMask     >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ReflectMask      , t->reflectmasktexture                );
2055                 if (r_glsl_permutation->tex_Texture_ReflectCube     >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ReflectCube      , t->reflectcubetexture ? t->reflectcubetexture : r_texture_whitecube);
2056                 if (r_glsl_permutation->tex_Texture_FogHeightTexture>= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_FogHeightTexture , r_texture_fogheighttexture                          );
2057                 if (r_glsl_permutation->tex_Texture_FogMask         >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_FogMask          , r_texture_fogattenuation                            );
2058                 if (r_glsl_permutation->tex_Texture_Lightmap        >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Lightmap         , rsurface.lightmaptexture ? rsurface.lightmaptexture : r_texture_white);
2059                 if (r_glsl_permutation->tex_Texture_Deluxemap       >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Deluxemap        , rsurface.deluxemaptexture ? rsurface.deluxemaptexture : r_texture_blanknormalmap);
2060                 if (r_glsl_permutation->tex_Texture_Attenuation     >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Attenuation      , r_shadow_attenuationgradienttexture                 );
2061                 if (rsurfacepass == RSURFPASS_BACKGROUND)
2062                 {
2063                         if (r_glsl_permutation->tex_Texture_Refraction  >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Refraction        , waterplane->rt_refraction ? waterplane->rt_refraction->colortexture[0] : r_texture_black);
2064                         if (r_glsl_permutation->tex_Texture_First       >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First             , waterplane->rt_camera ? waterplane->rt_camera->colortexture[0] : r_texture_black);
2065                         if (r_glsl_permutation->tex_Texture_Reflection  >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Reflection        , waterplane->rt_reflection ? waterplane->rt_reflection->colortexture[0] : r_texture_black);
2066                 }
2067                 else
2068                 {
2069                         if (r_glsl_permutation->tex_Texture_Reflection >= 0 && waterplane) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Reflection        , waterplane->rt_reflection ? waterplane->rt_reflection->colortexture[0] : r_texture_black);
2070                 }
2071                 if (r_glsl_permutation->tex_Texture_ScreenNormalMap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenNormalMap   , r_shadow_prepassgeometrynormalmaptexture            );
2072                 if (r_glsl_permutation->tex_Texture_ScreenDiffuse   >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenDiffuse     , r_shadow_prepasslightingdiffusetexture              );
2073                 if (r_glsl_permutation->tex_Texture_ScreenSpecular  >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenSpecular    , r_shadow_prepasslightingspeculartexture             );
2074                 if (rsurface.rtlight || (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW)))
2075                 {
2076                         if (r_glsl_permutation->tex_Texture_ShadowMap2D     >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ShadowMap2D, r_shadow_shadowmap2ddepthtexture                           );
2077                         if (rsurface.rtlight)
2078                         {
2079                                 if (r_glsl_permutation->tex_Texture_Cube            >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Cube              , rsurface.rtlight->currentcubemap                    );
2080                                 if (r_glsl_permutation->tex_Texture_CubeProjection  >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_CubeProjection    , r_shadow_shadowmapvsdcttexture                      );
2081                         }
2082                 }
2083                 if (r_glsl_permutation->tex_Texture_BounceGrid  >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_BounceGrid, r_shadow_bouncegrid_state.texture);
2084                 if (r_glsl_permutation->tex_Texture_LightGrid   >= 0 && r_refdef.scene.worldmodel) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_LightGrid, r_refdef.scene.worldmodel->brushq3.lightgridtexture);
2085                 CHECKGLERROR
2086                 break;
2087         }
2088 }
2089
2090 void R_SetupShader_DeferredLight(const rtlight_t *rtlight)
2091 {
2092         // select a permutation of the lighting shader appropriate to this
2093         // combination of texture, entity, light source, and fogging, only use the
2094         // minimum features necessary to avoid wasting rendering time in the
2095         // fragment shader on features that are not being used
2096         uint64_t permutation = 0;
2097         unsigned int mode = 0;
2098         const float *lightcolorbase = rtlight->currentcolor;
2099         float ambientscale = rtlight->ambientscale;
2100         float diffusescale = rtlight->diffusescale;
2101         float specularscale = rtlight->specularscale;
2102         // this is the location of the light in view space
2103         vec3_t viewlightorigin;
2104         // this transforms from view space (camera) to light space (cubemap)
2105         matrix4x4_t viewtolight;
2106         matrix4x4_t lighttoview;
2107         float viewtolight16f[16];
2108         // light source
2109         mode = SHADERMODE_DEFERREDLIGHTSOURCE;
2110         if (rtlight->currentcubemap != r_texture_whitecube)
2111                 permutation |= SHADERPERMUTATION_CUBEFILTER;
2112         if (diffusescale > 0)
2113                 permutation |= SHADERPERMUTATION_DIFFUSE;
2114         if (specularscale > 0 && r_shadow_gloss.integer > 0)
2115                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2116         if (r_shadow_usingshadowmap2d)
2117         {
2118                 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2119                 if (r_shadow_shadowmapvsdct)
2120                         permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
2121
2122                 if (r_shadow_shadowmap2ddepthbuffer)
2123                         permutation |= SHADERPERMUTATION_DEPTHRGB;
2124         }
2125         if (vid.allowalphatocoverage)
2126                 GL_AlphaToCoverage(false);
2127         Matrix4x4_Transform(&r_refdef.view.viewport.viewmatrix, rtlight->shadoworigin, viewlightorigin);
2128         Matrix4x4_Concat(&lighttoview, &r_refdef.view.viewport.viewmatrix, &rtlight->matrix_lighttoworld);
2129         Matrix4x4_Invert_Full(&viewtolight, &lighttoview);
2130         Matrix4x4_ToArrayFloatGL(&viewtolight, viewtolight16f);
2131         switch(vid.renderpath)
2132         {
2133         case RENDERPATH_GL32:
2134         case RENDERPATH_GLES2:
2135                 R_SetupShader_SetPermutationGLSL(mode, permutation);
2136                 if (r_glsl_permutation->loc_LightPosition             >= 0) qglUniform3f(       r_glsl_permutation->loc_LightPosition            , viewlightorigin[0], viewlightorigin[1], viewlightorigin[2]);
2137                 if (r_glsl_permutation->loc_ViewToLight               >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ViewToLight              , 1, false, viewtolight16f);
2138                 if (r_glsl_permutation->loc_DeferredColor_Ambient     >= 0) qglUniform3f(       r_glsl_permutation->loc_DeferredColor_Ambient    , lightcolorbase[0] * ambientscale , lightcolorbase[1] * ambientscale , lightcolorbase[2] * ambientscale );
2139                 if (r_glsl_permutation->loc_DeferredColor_Diffuse     >= 0) qglUniform3f(       r_glsl_permutation->loc_DeferredColor_Diffuse    , lightcolorbase[0] * diffusescale , lightcolorbase[1] * diffusescale , lightcolorbase[2] * diffusescale );
2140                 if (r_glsl_permutation->loc_DeferredColor_Specular    >= 0) qglUniform3f(       r_glsl_permutation->loc_DeferredColor_Specular   , lightcolorbase[0] * specularscale, lightcolorbase[1] * specularscale, lightcolorbase[2] * specularscale);
2141                 if (r_glsl_permutation->loc_ShadowMap_TextureScale    >= 0) qglUniform4f(       r_glsl_permutation->loc_ShadowMap_TextureScale   , r_shadow_lightshadowmap_texturescale[0], r_shadow_lightshadowmap_texturescale[1], r_shadow_lightshadowmap_texturescale[2], r_shadow_lightshadowmap_texturescale[3]);
2142                 if (r_glsl_permutation->loc_ShadowMap_Parameters      >= 0) qglUniform4f(       r_glsl_permutation->loc_ShadowMap_Parameters     , r_shadow_lightshadowmap_parameters[0], r_shadow_lightshadowmap_parameters[1], r_shadow_lightshadowmap_parameters[2], r_shadow_lightshadowmap_parameters[3]);
2143                 if (r_glsl_permutation->loc_SpecularPower             >= 0) qglUniform1f(       r_glsl_permutation->loc_SpecularPower            , (r_shadow_gloss.integer == 2 ? r_shadow_gloss2exponent.value : r_shadow_glossexponent.value) * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
2144                 if (r_glsl_permutation->loc_ScreenToDepth             >= 0) qglUniform2f(       r_glsl_permutation->loc_ScreenToDepth            , r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]);
2145                 if (r_glsl_permutation->loc_PixelToScreenTexCoord     >= 0) qglUniform2f(       r_glsl_permutation->loc_PixelToScreenTexCoord    , 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
2146
2147                 if (r_glsl_permutation->tex_Texture_Attenuation       >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Attenuation        , r_shadow_attenuationgradienttexture                 );
2148                 if (r_glsl_permutation->tex_Texture_ScreenNormalMap   >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenNormalMap    , r_shadow_prepassgeometrynormalmaptexture            );
2149                 if (r_glsl_permutation->tex_Texture_Cube              >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Cube               , rsurface.rtlight->currentcubemap                    );
2150                 if (r_glsl_permutation->tex_Texture_ShadowMap2D       >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ShadowMap2D        , r_shadow_shadowmap2ddepthtexture                    );
2151                 if (r_glsl_permutation->tex_Texture_CubeProjection    >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_CubeProjection     , r_shadow_shadowmapvsdcttexture                      );
2152                 break;
2153         }
2154 }
2155
2156 #define SKINFRAME_HASH 1024
2157
2158 typedef struct
2159 {
2160         unsigned int loadsequence; // incremented each level change
2161         memexpandablearray_t array;
2162         skinframe_t *hash[SKINFRAME_HASH];
2163 }
2164 r_skinframe_t;
2165 r_skinframe_t r_skinframe;
2166
2167 void R_SkinFrame_PrepareForPurge(void)
2168 {
2169         r_skinframe.loadsequence++;
2170         // wrap it without hitting zero
2171         if (r_skinframe.loadsequence >= 200)
2172                 r_skinframe.loadsequence = 1;
2173 }
2174
2175 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
2176 {
2177         if (!skinframe)
2178                 return;
2179         // mark the skinframe as used for the purging code
2180         skinframe->loadsequence = r_skinframe.loadsequence;
2181 }
2182
2183 void R_SkinFrame_PurgeSkinFrame(skinframe_t *s)
2184 {
2185         if (s == NULL)
2186                 return;
2187         if (s->merged == s->base)
2188                 s->merged = NULL;
2189         R_PurgeTexture(s->stain); s->stain = NULL;
2190         R_PurgeTexture(s->merged); s->merged = NULL;
2191         R_PurgeTexture(s->base); s->base = NULL;
2192         R_PurgeTexture(s->pants); s->pants = NULL;
2193         R_PurgeTexture(s->shirt); s->shirt = NULL;
2194         R_PurgeTexture(s->nmap); s->nmap = NULL;
2195         R_PurgeTexture(s->gloss); s->gloss = NULL;
2196         R_PurgeTexture(s->glow); s->glow = NULL;
2197         R_PurgeTexture(s->fog); s->fog = NULL;
2198         R_PurgeTexture(s->reflect); s->reflect = NULL;
2199         s->loadsequence = 0;
2200 }
2201
2202 void R_SkinFrame_Purge(void)
2203 {
2204         int i;
2205         skinframe_t *s;
2206         for (i = 0;i < SKINFRAME_HASH;i++)
2207         {
2208                 for (s = r_skinframe.hash[i];s;s = s->next)
2209                 {
2210                         if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
2211                                 R_SkinFrame_PurgeSkinFrame(s);
2212                 }
2213         }
2214 }
2215
2216 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
2217         skinframe_t *item;
2218         char basename[MAX_QPATH];
2219
2220         Image_StripImageExtension(name, basename, sizeof(basename));
2221
2222         if( last == NULL ) {
2223                 int hashindex;
2224                 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2225                 item = r_skinframe.hash[hashindex];
2226         } else {
2227                 item = last->next;
2228         }
2229
2230         // linearly search through the hash bucket
2231         for( ; item ; item = item->next ) {
2232                 if( !strcmp( item->basename, basename ) ) {
2233                         return item;
2234                 }
2235         }
2236         return NULL;
2237 }
2238
2239 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qbool add)
2240 {
2241         skinframe_t *item;
2242         int compareflags = textureflags & TEXF_IMPORTANTBITS;
2243         int hashindex;
2244         char basename[MAX_QPATH];
2245
2246         Image_StripImageExtension(name, basename, sizeof(basename));
2247
2248         hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2249         for (item = r_skinframe.hash[hashindex];item;item = item->next)
2250                 if (!strcmp(item->basename, basename) &&
2251                         item->textureflags == compareflags &&
2252                         item->comparewidth == comparewidth &&
2253                         item->compareheight == compareheight &&
2254                         item->comparecrc == comparecrc)
2255                         break;
2256
2257         if (!item)
2258         {
2259                 if (!add)
2260                         return NULL;
2261                 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
2262                 memset(item, 0, sizeof(*item));
2263                 strlcpy(item->basename, basename, sizeof(item->basename));
2264                 item->textureflags = compareflags;
2265                 item->comparewidth = comparewidth;
2266                 item->compareheight = compareheight;
2267                 item->comparecrc = comparecrc;
2268                 item->next = r_skinframe.hash[hashindex];
2269                 r_skinframe.hash[hashindex] = item;
2270         }
2271         else if (textureflags & TEXF_FORCE_RELOAD)
2272                 R_SkinFrame_PurgeSkinFrame(item);
2273
2274         R_SkinFrame_MarkUsed(item);
2275         return item;
2276 }
2277
2278 #define R_SKINFRAME_LOAD_AVERAGE_COLORS(cnt, getpixel) \
2279         { \
2280                 unsigned long long avgcolor[5], wsum; \
2281                 int pix, comp, w; \
2282                 avgcolor[0] = 0; \
2283                 avgcolor[1] = 0; \
2284                 avgcolor[2] = 0; \
2285                 avgcolor[3] = 0; \
2286                 avgcolor[4] = 0; \
2287                 wsum = 0; \
2288                 for(pix = 0; pix < cnt; ++pix) \
2289                 { \
2290                         w = 0; \
2291                         for(comp = 0; comp < 3; ++comp) \
2292                                 w += getpixel; \
2293                         if(w) /* ignore perfectly black pixels because that is better for model skins */ \
2294                         { \
2295                                 ++wsum; \
2296                                 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2297                                 w = getpixel; \
2298                                 for(comp = 0; comp < 3; ++comp) \
2299                                         avgcolor[comp] += getpixel * w; \
2300                                 avgcolor[3] += w; \
2301                         } \
2302                         /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2303                         avgcolor[4] += getpixel; \
2304                 } \
2305                 if(avgcolor[3] == 0) /* no pixels seen? even worse */ \
2306                         avgcolor[3] = 1; \
2307                 skinframe->avgcolor[0] = avgcolor[2] / (255.0 * avgcolor[3]); \
2308                 skinframe->avgcolor[1] = avgcolor[1] / (255.0 * avgcolor[3]); \
2309                 skinframe->avgcolor[2] = avgcolor[0] / (255.0 * avgcolor[3]); \
2310                 skinframe->avgcolor[3] = avgcolor[4] / (255.0 * cnt); \
2311         }
2312
2313 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qbool complain, qbool fallbacknotexture)
2314 {
2315         skinframe_t *skinframe;
2316
2317         if (cls.state == ca_dedicated)
2318                 return NULL;
2319
2320         // return an existing skinframe if already loaded
2321         skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
2322         if (skinframe && skinframe->base)
2323                 return skinframe;
2324
2325         // if the skinframe doesn't exist this will create it
2326         return R_SkinFrame_LoadExternal_SkinFrame(skinframe, name, textureflags, complain, fallbacknotexture);
2327 }
2328
2329 extern cvar_t gl_picmip;
2330 skinframe_t *R_SkinFrame_LoadExternal_SkinFrame(skinframe_t *skinframe, const char *name, int textureflags, qbool complain, qbool fallbacknotexture)
2331 {
2332         int j;
2333         unsigned char *pixels;
2334         unsigned char *bumppixels;
2335         unsigned char *basepixels = NULL;
2336         int basepixels_width = 0;
2337         int basepixels_height = 0;
2338         rtexture_t *ddsbase = NULL;
2339         qbool ddshasalpha = false;
2340         float ddsavgcolor[4];
2341         char basename[MAX_QPATH];
2342         int miplevel = R_PicmipForFlags(textureflags);
2343         int savemiplevel = miplevel;
2344         int mymiplevel;
2345         char vabuf[1024];
2346
2347         if (cls.state == ca_dedicated)
2348                 return NULL;
2349
2350         Image_StripImageExtension(name, basename, sizeof(basename));
2351
2352         // check for DDS texture file first
2353         if (!r_loaddds || !(ddsbase = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s.dds", basename), vid.sRGB3D, textureflags, &ddshasalpha, ddsavgcolor, miplevel, false)))
2354         {
2355                 basepixels = loadimagepixelsbgra(name, complain, true, false, &miplevel);
2356                 if (basepixels == NULL && fallbacknotexture)
2357                         basepixels = Image_GenerateNoTexture();
2358                 if (basepixels == NULL)
2359                         return NULL;
2360         }
2361
2362         // FIXME handle miplevel
2363
2364         if (developer_loading.integer)
2365                 Con_Printf("loading skin \"%s\"\n", name);
2366
2367         // we've got some pixels to store, so really allocate this new texture now
2368         if (!skinframe)
2369                 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
2370         textureflags &= ~TEXF_FORCE_RELOAD;
2371         skinframe->stain = NULL;
2372         skinframe->merged = NULL;
2373         skinframe->base = NULL;
2374         skinframe->pants = NULL;
2375         skinframe->shirt = NULL;
2376         skinframe->nmap = NULL;
2377         skinframe->gloss = NULL;
2378         skinframe->glow = NULL;
2379         skinframe->fog = NULL;
2380         skinframe->reflect = NULL;
2381         skinframe->hasalpha = false;
2382         // we could store the q2animname here too
2383
2384         if (ddsbase)
2385         {
2386                 skinframe->base = ddsbase;
2387                 skinframe->hasalpha = ddshasalpha;
2388                 VectorCopy(ddsavgcolor, skinframe->avgcolor);
2389                 if (r_loadfog && skinframe->hasalpha)
2390                         skinframe->fog = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_mask.dds", skinframe->basename), false, textureflags | TEXF_ALPHA, NULL, NULL, miplevel, true);
2391                 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2392         }
2393         else
2394         {
2395                 basepixels_width = image_width;
2396                 basepixels_height = image_height;
2397                 skinframe->base = R_LoadTexture2D (r_main_texturepool, skinframe->basename, basepixels_width, basepixels_height, basepixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_color.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), miplevel, NULL);
2398                 if (textureflags & TEXF_ALPHA)
2399                 {
2400                         for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
2401                         {
2402                                 if (basepixels[j] < 255)
2403                                 {
2404                                         skinframe->hasalpha = true;
2405                                         break;
2406                                 }
2407                         }
2408                         if (r_loadfog && skinframe->hasalpha)
2409                         {
2410                                 // has transparent pixels
2411                                 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2412                                 for (j = 0;j < image_width * image_height * 4;j += 4)
2413                                 {
2414                                         pixels[j+0] = 255;
2415                                         pixels[j+1] = 255;
2416                                         pixels[j+2] = 255;
2417                                         pixels[j+3] = basepixels[j+3];
2418                                 }
2419                                 skinframe->fog = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_mask", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, textureflags & (gl_texturecompression_color.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), miplevel, NULL);
2420                                 Mem_Free(pixels);
2421                         }
2422                 }
2423                 R_SKINFRAME_LOAD_AVERAGE_COLORS(basepixels_width * basepixels_height, basepixels[4 * pix + comp]);
2424 #ifndef USE_GLES2
2425                 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2426                 if (r_savedds && skinframe->base)
2427                         R_SaveTextureDDSFile(skinframe->base, va(vabuf, sizeof(vabuf), "dds/%s.dds", skinframe->basename), r_texture_dds_save.integer < 2, skinframe->hasalpha);
2428                 if (r_savedds && skinframe->fog)
2429                         R_SaveTextureDDSFile(skinframe->fog, va(vabuf, sizeof(vabuf), "dds/%s_mask.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2430 #endif
2431         }
2432
2433         if (r_loaddds)
2434         {
2435                 mymiplevel = savemiplevel;
2436                 if (r_loadnormalmap)
2437                         skinframe->nmap = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_norm.dds", skinframe->basename), false, (TEXF_ALPHA | textureflags) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP), NULL, NULL, mymiplevel, true);
2438                 skinframe->glow = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_glow.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2439                 if (r_loadgloss)
2440                         skinframe->gloss = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_gloss.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2441                 skinframe->pants = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_pants.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2442                 skinframe->shirt = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_shirt.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2443                 skinframe->reflect = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_reflect.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2444         }
2445
2446         // _norm is the name used by tenebrae and has been adopted as standard
2447         if (r_loadnormalmap && skinframe->nmap == NULL)
2448         {
2449                 mymiplevel = savemiplevel;
2450                 if ((pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_norm", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
2451                 {
2452                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | textureflags) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP) & (gl_texturecompression_normal.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2453                         Mem_Free(pixels);
2454                         pixels = NULL;
2455                 }
2456                 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_bump", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
2457                 {
2458                         pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2459                         Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
2460                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | textureflags) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP) & (gl_texturecompression_normal.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2461                         Mem_Free(pixels);
2462                         Mem_Free(bumppixels);
2463                 }
2464                 else if (r_shadow_bumpscale_basetexture.value > 0)
2465                 {
2466                         pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
2467                         Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
2468                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), basepixels_width, basepixels_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | textureflags) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP) & (gl_texturecompression_normal.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2469                         Mem_Free(pixels);
2470                 }
2471 #ifndef USE_GLES2
2472                 if (r_savedds && skinframe->nmap)
2473                         R_SaveTextureDDSFile(skinframe->nmap, va(vabuf, sizeof(vabuf), "dds/%s_norm.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2474 #endif
2475         }
2476
2477         // _luma is supported only for tenebrae compatibility
2478         // _blend and .blend are supported only for Q3 & QL compatibility, this hack can be removed if better Q3 shader support is implemented
2479         // _glow is the preferred name
2480         mymiplevel = savemiplevel;
2481         if (skinframe->glow == NULL && ((pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_glow", skinframe->basename), false, false, false, &mymiplevel)) || (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s.blend", skinframe->basename), false, false, false, &mymiplevel)) || (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_blend", skinframe->basename), false, false, false, &mymiplevel)) || (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_luma", skinframe->basename), false, false, false, &mymiplevel))))
2482         {
2483                 skinframe->glow = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_glow", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_glow.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2484 #ifndef USE_GLES2
2485                 if (r_savedds && skinframe->glow)
2486                         R_SaveTextureDDSFile(skinframe->glow, va(vabuf, sizeof(vabuf), "dds/%s_glow.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2487 #endif
2488                 Mem_Free(pixels);pixels = NULL;
2489         }
2490
2491         mymiplevel = savemiplevel;
2492         if (skinframe->gloss == NULL && r_loadgloss && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_gloss", skinframe->basename), false, false, false, &mymiplevel)))
2493         {
2494                 skinframe->gloss = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_gloss", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, (TEXF_ALPHA | textureflags) & (gl_texturecompression_gloss.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2495 #ifndef USE_GLES2
2496                 if (r_savedds && skinframe->gloss)
2497                         R_SaveTextureDDSFile(skinframe->gloss, va(vabuf, sizeof(vabuf), "dds/%s_gloss.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2498 #endif
2499                 Mem_Free(pixels);
2500                 pixels = NULL;
2501         }
2502
2503         mymiplevel = savemiplevel;
2504         if (skinframe->pants == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_pants", skinframe->basename), false, false, false, &mymiplevel)))
2505         {
2506                 skinframe->pants = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_pants", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_color.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2507 #ifndef USE_GLES2
2508                 if (r_savedds && skinframe->pants)
2509                         R_SaveTextureDDSFile(skinframe->pants, va(vabuf, sizeof(vabuf), "dds/%s_pants.dds", skinframe->basename), r_texture_dds_save.integer < 2, false);
2510 #endif
2511                 Mem_Free(pixels);
2512                 pixels = NULL;
2513         }
2514
2515         mymiplevel = savemiplevel;
2516         if (skinframe->shirt == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_shirt", skinframe->basename), false, false, false, &mymiplevel)))
2517         {
2518                 skinframe->shirt = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_shirt", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_color.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2519 #ifndef USE_GLES2
2520                 if (r_savedds && skinframe->shirt)
2521                         R_SaveTextureDDSFile(skinframe->shirt, va(vabuf, sizeof(vabuf), "dds/%s_shirt.dds", skinframe->basename), r_texture_dds_save.integer < 2, false);
2522 #endif
2523                 Mem_Free(pixels);
2524                 pixels = NULL;
2525         }
2526
2527         mymiplevel = savemiplevel;
2528         if (skinframe->reflect == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_reflect", skinframe->basename), false, false, false, &mymiplevel)))
2529         {
2530                 skinframe->reflect = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_reflect", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_reflectmask.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2531 #ifndef USE_GLES2
2532                 if (r_savedds && skinframe->reflect)
2533                         R_SaveTextureDDSFile(skinframe->reflect, va(vabuf, sizeof(vabuf), "dds/%s_reflect.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2534 #endif
2535                 Mem_Free(pixels);
2536                 pixels = NULL;
2537         }
2538
2539         if (basepixels)
2540                 Mem_Free(basepixels);
2541
2542         return skinframe;
2543 }
2544
2545 skinframe_t *R_SkinFrame_LoadInternalBGRA(const char *name, int textureflags, const unsigned char *skindata, int width, int height, int comparewidth, int compareheight, int comparecrc, qbool sRGB)
2546 {
2547         int i;
2548         skinframe_t *skinframe;
2549         char vabuf[1024];
2550
2551         if (cls.state == ca_dedicated)
2552                 return NULL;
2553
2554         // if already loaded just return it, otherwise make a new skinframe
2555         skinframe = R_SkinFrame_Find(name, textureflags, comparewidth, compareheight, comparecrc, true);
2556         if (skinframe->base)
2557                 return skinframe;
2558         textureflags &= ~TEXF_FORCE_RELOAD;
2559
2560         skinframe->stain = NULL;
2561         skinframe->merged = NULL;
2562         skinframe->base = NULL;
2563         skinframe->pants = NULL;
2564         skinframe->shirt = NULL;
2565         skinframe->nmap = NULL;
2566         skinframe->gloss = NULL;
2567         skinframe->glow = NULL;
2568         skinframe->fog = NULL;
2569         skinframe->reflect = NULL;
2570         skinframe->hasalpha = false;
2571
2572         // if no data was provided, then clearly the caller wanted to get a blank skinframe
2573         if (!skindata)
2574                 return NULL;
2575
2576         if (developer_loading.integer)
2577                 Con_Printf("loading 32bit skin \"%s\"\n", name);
2578
2579         if (r_loadnormalmap && r_shadow_bumpscale_basetexture.value > 0)
2580         {
2581                 unsigned char *a = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2582                 unsigned char *b = a + width * height * 4;
2583                 Image_HeightmapToNormalmap_BGRA(skindata, b, width, height, false, r_shadow_bumpscale_basetexture.value);
2584                 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), width, height, b, TEXTYPE_BGRA, (textureflags | TEXF_ALPHA) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP), -1, NULL);
2585                 Mem_Free(a);
2586         }
2587         skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, sRGB ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags, -1, NULL);
2588         if (textureflags & TEXF_ALPHA)
2589         {
2590                 for (i = 3;i < width * height * 4;i += 4)
2591                 {
2592                         if (skindata[i] < 255)
2593                         {
2594                                 skinframe->hasalpha = true;
2595                                 break;
2596                         }
2597                 }
2598                 if (r_loadfog && skinframe->hasalpha)
2599                 {
2600                         unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
2601                         memcpy(fogpixels, skindata, width * height * 4);
2602                         for (i = 0;i < width * height * 4;i += 4)
2603                                 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
2604                         skinframe->fog = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, textureflags, -1, NULL);
2605                         Mem_Free(fogpixels);
2606                 }
2607         }
2608
2609         R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, skindata[4 * pix + comp]);
2610         //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2611
2612         return skinframe;
2613 }
2614
2615 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
2616 {
2617         int i;
2618         int featuresmask;
2619         skinframe_t *skinframe;
2620
2621         if (cls.state == ca_dedicated)
2622                 return NULL;
2623
2624         // if already loaded just return it, otherwise make a new skinframe
2625         skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
2626         if (skinframe->base)
2627                 return skinframe;
2628         //textureflags &= ~TEXF_FORCE_RELOAD;
2629
2630         skinframe->stain = NULL;
2631         skinframe->merged = NULL;
2632         skinframe->base = NULL;
2633         skinframe->pants = NULL;
2634         skinframe->shirt = NULL;
2635         skinframe->nmap = NULL;
2636         skinframe->gloss = NULL;
2637         skinframe->glow = NULL;
2638         skinframe->fog = NULL;
2639         skinframe->reflect = NULL;
2640         skinframe->hasalpha = false;
2641
2642         // if no data was provided, then clearly the caller wanted to get a blank skinframe
2643         if (!skindata)
2644                 return NULL;
2645
2646         if (developer_loading.integer)
2647                 Con_Printf("loading quake skin \"%s\"\n", name);
2648
2649         // we actually don't upload anything until the first use, because mdl skins frequently go unused, and are almost never used in both modes (colormapped and non-colormapped)
2650         skinframe->qpixels = (unsigned char *)Mem_Alloc(r_main_mempool, width*height); // FIXME LEAK
2651         memcpy(skinframe->qpixels, skindata, width*height);
2652         skinframe->qwidth = width;
2653         skinframe->qheight = height;
2654
2655         featuresmask = 0;
2656         for (i = 0;i < width * height;i++)
2657                 featuresmask |= palette_featureflags[skindata[i]];
2658
2659         skinframe->hasalpha = false;
2660         // fence textures
2661         if (name[0] == '{')
2662                 skinframe->hasalpha = true;
2663         skinframe->qhascolormapping = loadpantsandshirt && (featuresmask & (PALETTEFEATURE_PANTS | PALETTEFEATURE_SHIRT));
2664         skinframe->qgeneratenmap = r_shadow_bumpscale_basetexture.value > 0;
2665         skinframe->qgeneratemerged = true;
2666         skinframe->qgeneratebase = skinframe->qhascolormapping;
2667         skinframe->qgenerateglow = loadglowtexture && (featuresmask & PALETTEFEATURE_GLOW);
2668
2669         R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette_bgra_complete)[skindata[pix]*4 + comp]);
2670         //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2671
2672         return skinframe;
2673 }
2674
2675 static void R_SkinFrame_GenerateTexturesFromQPixels(skinframe_t *skinframe, qbool colormapped)
2676 {
2677         int width;
2678         int height;
2679         unsigned char *skindata;
2680         char vabuf[1024];
2681
2682         if (!skinframe->qpixels)
2683                 return;
2684
2685         if (!skinframe->qhascolormapping)
2686                 colormapped = false;
2687
2688         if (colormapped)
2689         {
2690                 if (!skinframe->qgeneratebase)
2691                         return;
2692         }
2693         else
2694         {
2695                 if (!skinframe->qgeneratemerged)
2696                         return;
2697         }
2698
2699         width = skinframe->qwidth;
2700         height = skinframe->qheight;
2701         skindata = skinframe->qpixels;
2702
2703         if (skinframe->qgeneratenmap)
2704         {
2705                 unsigned char *a, *b;
2706                 skinframe->qgeneratenmap = false;
2707                 a = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2708                 b = a + width * height * 4;
2709                 // use either a custom palette or the quake palette
2710                 Image_Copy8bitBGRA(skindata, a, width * height, palette_bgra_complete);
2711                 Image_HeightmapToNormalmap_BGRA(a, b, width, height, false, r_shadow_bumpscale_basetexture.value);
2712                 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), width, height, b, TEXTYPE_BGRA, (skinframe->textureflags | TEXF_ALPHA) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP), -1, NULL);
2713                 Mem_Free(a);
2714         }
2715
2716         if (skinframe->qgenerateglow)
2717         {
2718                 skinframe->qgenerateglow = false;
2719                 if (skinframe->hasalpha) // fence textures
2720                         skinframe->glow = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_glow", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags | TEXF_ALPHA, -1, palette_bgra_onlyfullbrights_transparent); // glow
2721                 else
2722                         skinframe->glow = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_glow", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, palette_bgra_onlyfullbrights); // glow
2723         }
2724
2725         if (colormapped)
2726         {
2727                 skinframe->qgeneratebase = false;
2728                 skinframe->base  = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nospecial", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, skinframe->glow ? palette_bgra_nocolormapnofullbrights : palette_bgra_nocolormap);
2729                 skinframe->pants = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_pants", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, palette_bgra_pantsaswhite);
2730                 skinframe->shirt = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_shirt", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, palette_bgra_shirtaswhite);
2731         }
2732         else
2733         {
2734                 skinframe->qgeneratemerged = false;
2735                 if (skinframe->hasalpha) // fence textures
2736                         skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags | TEXF_ALPHA, -1, skinframe->glow ? palette_bgra_nofullbrights_transparent : palette_bgra_transparent);
2737                 else
2738                         skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, skinframe->glow ? palette_bgra_nofullbrights : palette_bgra_complete);
2739         }
2740
2741         if (!skinframe->qgeneratemerged && !skinframe->qgeneratebase)
2742         {
2743                 Mem_Free(skinframe->qpixels);
2744                 skinframe->qpixels = NULL;
2745         }
2746 }
2747
2748 skinframe_t *R_SkinFrame_LoadInternal8bit(const char *name, int textureflags, const unsigned char *skindata, int width, int height, const unsigned int *palette, const unsigned int *alphapalette)
2749 {
2750         int i;
2751         skinframe_t *skinframe;
2752         char vabuf[1024];
2753
2754         if (cls.state == ca_dedicated)
2755                 return NULL;
2756
2757         // if already loaded just return it, otherwise make a new skinframe
2758         skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
2759         if (skinframe->base)
2760                 return skinframe;
2761         textureflags &= ~TEXF_FORCE_RELOAD;
2762
2763         skinframe->stain = NULL;
2764         skinframe->merged = NULL;
2765         skinframe->base = NULL;
2766         skinframe->pants = NULL;
2767         skinframe->shirt = NULL;
2768         skinframe->nmap = NULL;
2769         skinframe->gloss = NULL;
2770         skinframe->glow = NULL;
2771         skinframe->fog = NULL;
2772         skinframe->reflect = NULL;
2773         skinframe->hasalpha = false;
2774
2775         // if no data was provided, then clearly the caller wanted to get a blank skinframe
2776         if (!skindata)
2777                 return NULL;
2778
2779         if (developer_loading.integer)
2780                 Con_Printf("loading embedded 8bit image \"%s\"\n", name);
2781
2782         skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_PALETTE, textureflags, -1, palette);
2783         if ((textureflags & TEXF_ALPHA) && alphapalette)
2784         {
2785                 for (i = 0;i < width * height;i++)
2786                 {
2787                         if (((unsigned char *)palette)[skindata[i]*4+3] < 255)
2788                         {
2789                                 skinframe->hasalpha = true;
2790                                 break;
2791                         }
2792                 }
2793                 if (r_loadfog && skinframe->hasalpha)
2794                         skinframe->fog = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_fog", skinframe->basename), width, height, skindata, TEXTYPE_PALETTE, textureflags, -1, alphapalette);
2795         }
2796
2797         R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette)[skindata[pix]*4 + comp]);
2798         //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2799
2800         return skinframe;
2801 }
2802
2803 skinframe_t *R_SkinFrame_LoadMissing(void)
2804 {
2805         skinframe_t *skinframe;
2806
2807         if (cls.state == ca_dedicated)
2808                 return NULL;
2809
2810         skinframe = R_SkinFrame_Find("missing", TEXF_FORCENEAREST, 0, 0, 0, true);
2811         skinframe->stain = NULL;
2812         skinframe->merged = NULL;
2813         skinframe->base = NULL;
2814         skinframe->pants = NULL;
2815         skinframe->shirt = NULL;
2816         skinframe->nmap = NULL;
2817         skinframe->gloss = NULL;
2818         skinframe->glow = NULL;
2819         skinframe->fog = NULL;
2820         skinframe->reflect = NULL;
2821         skinframe->hasalpha = false;
2822
2823         skinframe->avgcolor[0] = rand() / RAND_MAX;
2824         skinframe->avgcolor[1] = rand() / RAND_MAX;
2825         skinframe->avgcolor[2] = rand() / RAND_MAX;
2826         skinframe->avgcolor[3] = 1;
2827
2828         return skinframe;
2829 }
2830
2831 skinframe_t *R_SkinFrame_LoadNoTexture(void)
2832 {
2833         if (cls.state == ca_dedicated)
2834                 return NULL;
2835
2836         return R_SkinFrame_LoadInternalBGRA("notexture", TEXF_FORCENEAREST, Image_GenerateNoTexture(), 16, 16, 0, 0, 0, false);
2837 }
2838
2839 skinframe_t *R_SkinFrame_LoadInternalUsingTexture(const char *name, int textureflags, rtexture_t *tex, int width, int height, qbool sRGB)
2840 {
2841         skinframe_t *skinframe;
2842         if (cls.state == ca_dedicated)
2843                 return NULL;
2844         // if already loaded just return it, otherwise make a new skinframe
2845         skinframe = R_SkinFrame_Find(name, textureflags, width, height, 0, true);
2846         if (skinframe->base)
2847                 return skinframe;
2848         textureflags &= ~TEXF_FORCE_RELOAD;
2849         skinframe->stain = NULL;
2850         skinframe->merged = NULL;
2851         skinframe->base = NULL;
2852         skinframe->pants = NULL;
2853         skinframe->shirt = NULL;
2854         skinframe->nmap = NULL;
2855         skinframe->gloss = NULL;
2856         skinframe->glow = NULL;
2857         skinframe->fog = NULL;
2858         skinframe->reflect = NULL;
2859         skinframe->hasalpha = (textureflags & TEXF_ALPHA) != 0;
2860         // if no data was provided, then clearly the caller wanted to get a blank skinframe
2861         if (!tex)
2862                 return NULL;
2863         if (developer_loading.integer)
2864                 Con_Printf("loading 32bit skin \"%s\"\n", name);
2865         skinframe->base = skinframe->merged = tex;
2866         Vector4Set(skinframe->avgcolor, 1, 1, 1, 1); // bogus placeholder
2867         return skinframe;
2868 }
2869
2870 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
2871 typedef struct suffixinfo_s
2872 {
2873         const char *suffix;
2874         qbool flipx, flipy, flipdiagonal;
2875 }
2876 suffixinfo_t;
2877 static suffixinfo_t suffix[3][6] =
2878 {
2879         {
2880                 {"px",   false, false, false},
2881                 {"nx",   false, false, false},
2882                 {"py",   false, false, false},
2883                 {"ny",   false, false, false},
2884                 {"pz",   false, false, false},
2885                 {"nz",   false, false, false}
2886         },
2887         {
2888                 {"posx", false, false, false},
2889                 {"negx", false, false, false},
2890                 {"posy", false, false, false},
2891                 {"negy", false, false, false},
2892                 {"posz", false, false, false},
2893                 {"negz", false, false, false}
2894         },
2895         {
2896                 {"rt",    true, false,  true},
2897                 {"lf",   false,  true,  true},
2898                 {"ft",    true,  true, false},
2899                 {"bk",   false, false, false},
2900                 {"up",    true, false,  true},
2901                 {"dn",    true, false,  true}
2902         }
2903 };
2904
2905 static int componentorder[4] = {0, 1, 2, 3};
2906
2907 static rtexture_t *R_LoadCubemap(const char *basename)
2908 {
2909         int i, j, cubemapsize, forcefilter;
2910         unsigned char *cubemappixels, *image_buffer;
2911         rtexture_t *cubemaptexture;
2912         char name[256];
2913
2914         // HACK: if the cubemap name starts with a !, the cubemap is nearest-filtered
2915         forcefilter = TEXF_FORCELINEAR;
2916         if (basename && basename[0] == '!')
2917         {
2918                 basename++;
2919                 forcefilter = TEXF_FORCENEAREST;
2920         }
2921         // must start 0 so the first loadimagepixels has no requested width/height
2922         cubemapsize = 0;
2923         cubemappixels = NULL;
2924         cubemaptexture = NULL;
2925         // keep trying different suffix groups (posx, px, rt) until one loads
2926         for (j = 0;j < 3 && !cubemappixels;j++)
2927         {
2928                 // load the 6 images in the suffix group
2929                 for (i = 0;i < 6;i++)
2930                 {
2931                         // generate an image name based on the base and and suffix
2932                         dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
2933                         // load it
2934                         if ((image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
2935                         {
2936                                 // an image loaded, make sure width and height are equal
2937                                 if (image_width == image_height && (!cubemappixels || image_width == cubemapsize))
2938                                 {
2939                                         // if this is the first image to load successfully, allocate the cubemap memory
2940                                         if (!cubemappixels && image_width >= 1)
2941                                         {
2942                                                 cubemapsize = image_width;
2943                                                 // note this clears to black, so unavailable sides are black
2944                                                 cubemappixels = (unsigned char *)Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
2945                                         }
2946                                         // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
2947                                         if (cubemappixels)
2948                                                 Image_CopyMux(cubemappixels+i*cubemapsize*cubemapsize*4, image_buffer, cubemapsize, cubemapsize, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, componentorder);
2949                                 }
2950                                 else
2951                                         Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
2952                                 // free the image
2953                                 Mem_Free(image_buffer);
2954                         }
2955                 }
2956         }
2957         // if a cubemap loaded, upload it
2958         if (cubemappixels)
2959         {
2960                 if (developer_loading.integer)
2961                         Con_Printf("loading cubemap \"%s\"\n", basename);
2962
2963                 cubemaptexture = R_LoadTextureCubeMap(r_main_texturepool, basename, cubemapsize, cubemappixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, (gl_texturecompression_lightcubemaps.integer && gl_texturecompression.integer ? TEXF_COMPRESS : 0) | forcefilter | TEXF_CLAMP, -1, NULL);
2964                 Mem_Free(cubemappixels);
2965         }
2966         else
2967         {
2968                 Con_DPrintf("failed to load cubemap \"%s\"\n", basename);
2969                 if (developer_loading.integer)
2970                 {
2971                         Con_Printf("(tried tried images ");
2972                         for (j = 0;j < 3;j++)
2973                                 for (i = 0;i < 6;i++)
2974                                         Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
2975                         Con_Print(" and was unable to find any of them).\n");
2976                 }
2977         }
2978         return cubemaptexture;
2979 }
2980
2981 rtexture_t *R_GetCubemap(const char *basename)
2982 {
2983         int i;
2984         for (i = 0;i < r_texture_numcubemaps;i++)
2985                 if (r_texture_cubemaps[i] != NULL)
2986                         if (!strcasecmp(r_texture_cubemaps[i]->basename, basename))
2987                                 return r_texture_cubemaps[i]->texture ? r_texture_cubemaps[i]->texture : r_texture_whitecube;
2988         if (i >= MAX_CUBEMAPS || !r_main_mempool)
2989                 return r_texture_whitecube;
2990         r_texture_numcubemaps++;
2991         r_texture_cubemaps[i] = (cubemapinfo_t *)Mem_Alloc(r_main_mempool, sizeof(cubemapinfo_t));
2992         strlcpy(r_texture_cubemaps[i]->basename, basename, sizeof(r_texture_cubemaps[i]->basename));
2993         r_texture_cubemaps[i]->texture = R_LoadCubemap(r_texture_cubemaps[i]->basename);
2994         return r_texture_cubemaps[i]->texture;
2995 }
2996
2997 static void R_Main_FreeViewCache(void)
2998 {
2999         if (r_refdef.viewcache.entityvisible)
3000                 Mem_Free(r_refdef.viewcache.entityvisible);
3001         if (r_refdef.viewcache.world_pvsbits)
3002                 Mem_Free(r_refdef.viewcache.world_pvsbits);
3003         if (r_refdef.viewcache.world_leafvisible)
3004                 Mem_Free(r_refdef.viewcache.world_leafvisible);
3005         if (r_refdef.viewcache.world_surfacevisible)
3006                 Mem_Free(r_refdef.viewcache.world_surfacevisible);
3007         memset(&r_refdef.viewcache, 0, sizeof(r_refdef.viewcache));
3008 }
3009
3010 static void R_Main_ResizeViewCache(void)
3011 {
3012         int numentities = r_refdef.scene.numentities;
3013         int numclusters = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusters : 1;
3014         int numclusterbytes = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusterbytes : 1;
3015         int numleafs = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_leafs : 1;
3016         int numsurfaces = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->num_surfaces : 1;
3017         if (r_refdef.viewcache.maxentities < numentities)
3018         {
3019                 r_refdef.viewcache.maxentities = numentities;
3020                 if (r_refdef.viewcache.entityvisible)
3021                         Mem_Free(r_refdef.viewcache.entityvisible);
3022                 r_refdef.viewcache.entityvisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.maxentities);
3023         }
3024         if (r_refdef.viewcache.world_numclusters != numclusters)
3025         {
3026                 r_refdef.viewcache.world_numclusters = numclusters;
3027                 r_refdef.viewcache.world_numclusterbytes = numclusterbytes;
3028                 if (r_refdef.viewcache.world_pvsbits)
3029                         Mem_Free(r_refdef.viewcache.world_pvsbits);
3030                 r_refdef.viewcache.world_pvsbits = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numclusterbytes);
3031         }
3032         if (r_refdef.viewcache.world_numleafs != numleafs)
3033         {
3034                 r_refdef.viewcache.world_numleafs = numleafs;
3035                 if (r_refdef.viewcache.world_leafvisible)
3036                         Mem_Free(r_refdef.viewcache.world_leafvisible);
3037                 r_refdef.viewcache.world_leafvisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numleafs);
3038         }
3039         if (r_refdef.viewcache.world_numsurfaces != numsurfaces)
3040         {
3041                 r_refdef.viewcache.world_numsurfaces = numsurfaces;
3042                 if (r_refdef.viewcache.world_surfacevisible)
3043                         Mem_Free(r_refdef.viewcache.world_surfacevisible);
3044                 r_refdef.viewcache.world_surfacevisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numsurfaces);
3045         }
3046 }
3047
3048 extern rtexture_t *loadingscreentexture;
3049 static void gl_main_start(void)
3050 {
3051         loadingscreentexture = NULL;
3052         r_texture_blanknormalmap = NULL;
3053         r_texture_white = NULL;
3054         r_texture_grey128 = NULL;
3055         r_texture_black = NULL;
3056         r_texture_whitecube = NULL;
3057         r_texture_normalizationcube = NULL;
3058         r_texture_fogattenuation = NULL;
3059         r_texture_fogheighttexture = NULL;
3060         r_texture_gammaramps = NULL;
3061         r_texture_numcubemaps = 0;
3062         r_uniformbufferalignment = 32;
3063
3064         r_loaddds = r_texture_dds_load.integer != 0;
3065         r_savedds = vid.support.ext_texture_compression_s3tc && r_texture_dds_save.integer;
3066
3067         switch(vid.renderpath)
3068         {
3069         case RENDERPATH_GL32:
3070         case RENDERPATH_GLES2:
3071                 r_loadnormalmap = true;
3072                 r_loadgloss = true;
3073                 r_loadfog = false;
3074 #ifdef GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT
3075                 qglGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &r_uniformbufferalignment);
3076 #endif
3077                 break;
3078         }
3079
3080         R_AnimCache_Free();
3081         R_FrameData_Reset();
3082         R_BufferData_Reset();
3083
3084         r_numqueries = 0;
3085         r_maxqueries = 0;
3086         memset(r_queries, 0, sizeof(r_queries));
3087
3088         r_qwskincache = NULL;
3089         r_qwskincache_size = 0;
3090
3091         // due to caching of texture_t references, the collision cache must be reset
3092         Collision_Cache_Reset(true);
3093
3094         // set up r_skinframe loading system for textures
3095         memset(&r_skinframe, 0, sizeof(r_skinframe));
3096         r_skinframe.loadsequence = 1;
3097         Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
3098
3099         r_main_texturepool = R_AllocTexturePool();
3100         R_BuildBlankTextures();
3101         R_BuildNoTexture();
3102         R_BuildWhiteCube();
3103 #ifndef USE_GLES2
3104         R_BuildNormalizationCube();
3105 #endif //USE_GLES2
3106         r_texture_fogattenuation = NULL;
3107         r_texture_fogheighttexture = NULL;
3108         r_texture_gammaramps = NULL;
3109         //r_texture_fogintensity = NULL;
3110         memset(&r_fb, 0, sizeof(r_fb));
3111         Mem_ExpandableArray_NewArray(&r_fb.rendertargets, r_main_mempool, sizeof(r_rendertarget_t), 128);
3112         r_glsl_permutation = NULL;
3113         memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
3114         Mem_ExpandableArray_NewArray(&r_glsl_permutationarray, r_main_mempool, sizeof(r_glsl_permutation_t), 256);
3115         memset(&r_svbsp, 0, sizeof (r_svbsp));
3116
3117         memset(r_texture_cubemaps, 0, sizeof(r_texture_cubemaps));
3118         r_texture_numcubemaps = 0;
3119
3120         r_refdef.fogmasktable_density = 0;
3121
3122 #ifdef __ANDROID__
3123         // For Steelstorm Android
3124         // FIXME CACHE the program and reload
3125         // FIXME see possible combinations for SS:BR android
3126         Con_DPrintf("Compiling most used shaders for SS:BR android... START\n");
3127         R_SetupShader_SetPermutationGLSL(0, 12);
3128         R_SetupShader_SetPermutationGLSL(0, 13);
3129         R_SetupShader_SetPermutationGLSL(0, 8388621);
3130         R_SetupShader_SetPermutationGLSL(3, 0);
3131         R_SetupShader_SetPermutationGLSL(3, 2048);
3132         R_SetupShader_SetPermutationGLSL(5, 0);
3133         R_SetupShader_SetPermutationGLSL(5, 2);
3134         R_SetupShader_SetPermutationGLSL(5, 2048);
3135         R_SetupShader_SetPermutationGLSL(5, 8388608);
3136         R_SetupShader_SetPermutationGLSL(11, 1);
3137         R_SetupShader_SetPermutationGLSL(11, 2049);
3138         R_SetupShader_SetPermutationGLSL(11, 8193);
3139         R_SetupShader_SetPermutationGLSL(11, 10241);
3140         Con_DPrintf("Compiling most used shaders for SS:BR android... END\n");
3141 #endif
3142 }
3143
3144 extern unsigned int r_shadow_occlusion_buf;
3145
3146 static void gl_main_shutdown(void)
3147 {
3148         R_RenderTarget_FreeUnused(true);
3149         Mem_ExpandableArray_FreeArray(&r_fb.rendertargets);
3150         R_AnimCache_Free();
3151         R_FrameData_Reset();
3152         R_BufferData_Reset();
3153
3154         R_Main_FreeViewCache();
3155
3156         switch(vid.renderpath)
3157         {
3158         case RENDERPATH_GL32:
3159         case RENDERPATH_GLES2:
3160 #if defined(GL_SAMPLES_PASSED) && !defined(USE_GLES2)
3161                 if (r_maxqueries)
3162                         qglDeleteQueries(r_maxqueries, r_queries);
3163 #endif
3164                 break;
3165         }
3166         r_shadow_occlusion_buf = 0;
3167         r_numqueries = 0;
3168         r_maxqueries = 0;
3169         memset(r_queries, 0, sizeof(r_queries));
3170
3171         r_qwskincache = NULL;
3172         r_qwskincache_size = 0;
3173
3174         // clear out the r_skinframe state
3175         Mem_ExpandableArray_FreeArray(&r_skinframe.array);
3176         memset(&r_skinframe, 0, sizeof(r_skinframe));
3177
3178         if (r_svbsp.nodes)
3179                 Mem_Free(r_svbsp.nodes);
3180         memset(&r_svbsp, 0, sizeof (r_svbsp));
3181         R_FreeTexturePool(&r_main_texturepool);
3182         loadingscreentexture = NULL;
3183         r_texture_blanknormalmap = NULL;
3184         r_texture_white = NULL;
3185         r_texture_grey128 = NULL;
3186         r_texture_black = NULL;
3187         r_texture_whitecube = NULL;
3188         r_texture_normalizationcube = NULL;
3189         r_texture_fogattenuation = NULL;
3190         r_texture_fogheighttexture = NULL;
3191         r_texture_gammaramps = NULL;
3192         r_texture_numcubemaps = 0;
3193         //r_texture_fogintensity = NULL;
3194         memset(&r_fb, 0, sizeof(r_fb));
3195         R_GLSL_Restart_f(cmd_local);
3196
3197         r_glsl_permutation = NULL;
3198         memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
3199         Mem_ExpandableArray_FreeArray(&r_glsl_permutationarray);
3200 }
3201
3202 static void gl_main_newmap(void)
3203 {
3204         // FIXME: move this code to client
3205         char *entities, entname[MAX_QPATH];
3206         if (r_qwskincache)
3207                 Mem_Free(r_qwskincache);
3208         r_qwskincache = NULL;
3209         r_qwskincache_size = 0;
3210         if (cl.worldmodel)
3211         {
3212                 dpsnprintf(entname, sizeof(entname), "%s.ent", cl.worldnamenoextension);
3213                 if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
3214                 {
3215                         CL_ParseEntityLump(entities);
3216                         Mem_Free(entities);
3217                         return;
3218                 }
3219                 if (cl.worldmodel->brush.entities)
3220                         CL_ParseEntityLump(cl.worldmodel->brush.entities);
3221         }
3222         R_Main_FreeViewCache();
3223
3224         R_FrameData_Reset();
3225         R_BufferData_Reset();
3226 }
3227
3228 void GL_Main_Init(void)
3229 {
3230         int i;
3231         r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
3232         R_InitShaderModeInfo();
3233
3234         Cmd_AddCommand(CF_CLIENT, "r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
3235         Cmd_AddCommand(CF_CLIENT, "r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
3236         // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
3237         if (gamemode == GAME_NEHAHRA)
3238         {
3239                 Cvar_RegisterVariable (&gl_fogenable);
3240                 Cvar_RegisterVariable (&gl_fogdensity);
3241                 Cvar_RegisterVariable (&gl_fogred);
3242                 Cvar_RegisterVariable (&gl_foggreen);
3243                 Cvar_RegisterVariable (&gl_fogblue);
3244                 Cvar_RegisterVariable (&gl_fogstart);
3245                 Cvar_RegisterVariable (&gl_fogend);
3246                 Cvar_RegisterVariable (&gl_skyclip);
3247         }
3248         Cvar_RegisterVariable(&r_motionblur);
3249         Cvar_RegisterVariable(&r_damageblur);
3250         Cvar_RegisterVariable(&r_motionblur_averaging);
3251         Cvar_RegisterVariable(&r_motionblur_randomize);
3252         Cvar_RegisterVariable(&r_motionblur_minblur);
3253         Cvar_RegisterVariable(&r_motionblur_maxblur);
3254         Cvar_RegisterVariable(&r_motionblur_velocityfactor);
3255         Cvar_RegisterVariable(&r_motionblur_velocityfactor_minspeed);
3256         Cvar_RegisterVariable(&r_motionblur_velocityfactor_maxspeed);
3257         Cvar_RegisterVariable(&r_motionblur_mousefactor);
3258         Cvar_RegisterVariable(&r_motionblur_mousefactor_minspeed);
3259         Cvar_RegisterVariable(&r_motionblur_mousefactor_maxspeed);
3260         Cvar_RegisterVariable(&r_depthfirst);
3261         Cvar_RegisterVariable(&r_useinfinitefarclip);
3262         Cvar_RegisterVariable(&r_farclip_base);
3263         Cvar_RegisterVariable(&r_farclip_world);
3264         Cvar_RegisterVariable(&r_nearclip);
3265         Cvar_RegisterVariable(&r_deformvertexes);
3266         Cvar_RegisterVariable(&r_transparent);
3267         Cvar_RegisterVariable(&r_transparent_alphatocoverage);
3268         Cvar_RegisterVariable(&r_transparent_sortsurfacesbynearest);
3269         Cvar_RegisterVariable(&r_transparent_useplanardistance);
3270         Cvar_RegisterVariable(&r_showoverdraw);
3271         Cvar_RegisterVariable(&r_showbboxes);
3272         Cvar_RegisterVariable(&r_showbboxes_client);
3273         Cvar_RegisterVariable(&r_showsurfaces);
3274         Cvar_RegisterVariable(&r_showtris);
3275         Cvar_RegisterVariable(&r_shownormals);
3276         Cvar_RegisterVariable(&r_showlighting);
3277         Cvar_RegisterVariable(&r_showcollisionbrushes);
3278         Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
3279         Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
3280         Cvar_RegisterVariable(&r_showdisabledepthtest);
3281         Cvar_RegisterVariable(&r_showspriteedges);
3282         Cvar_RegisterVariable(&r_showparticleedges);
3283         Cvar_RegisterVariable(&r_drawportals);
3284         Cvar_RegisterVariable(&r_drawentities);
3285         Cvar_RegisterVariable(&r_draw2d);
3286         Cvar_RegisterVariable(&r_drawworld);
3287         Cvar_RegisterVariable(&r_cullentities_trace);
3288         Cvar_RegisterVariable(&r_cullentities_trace_entityocclusion);
3289         Cvar_RegisterVariable(&r_cullentities_trace_samples);
3290         Cvar_RegisterVariable(&r_cullentities_trace_tempentitysamples);
3291         Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
3292         Cvar_RegisterVariable(&r_cullentities_trace_expand);
3293         Cvar_RegisterVariable(&r_cullentities_trace_pad);
3294         Cvar_RegisterVariable(&r_cullentities_trace_delay);
3295         Cvar_RegisterVariable(&r_cullentities_trace_eyejitter);
3296         Cvar_RegisterVariable(&r_sortentities);
3297         Cvar_RegisterVariable(&r_drawviewmodel);
3298         Cvar_RegisterVariable(&r_drawexteriormodel);
3299         Cvar_RegisterVariable(&r_speeds);
3300         Cvar_RegisterVariable(&r_fullbrights);
3301         Cvar_RegisterVariable(&r_wateralpha);
3302         Cvar_RegisterVariable(&r_dynamic);
3303         Cvar_RegisterVariable(&r_fullbright_directed);
3304         Cvar_RegisterVariable(&r_fullbright_directed_ambient);
3305         Cvar_RegisterVariable(&r_fullbright_directed_diffuse);
3306         Cvar_RegisterVariable(&r_fullbright_directed_pitch);
3307         Cvar_RegisterVariable(&r_fullbright_directed_pitch_relative);
3308         Cvar_RegisterVariable(&r_fullbright);
3309         Cvar_RegisterVariable(&r_shadows);
3310         Cvar_RegisterVariable(&r_shadows_darken);
3311         Cvar_RegisterVariable(&r_shadows_drawafterrtlighting);
3312         Cvar_RegisterVariable(&r_shadows_castfrombmodels);
3313         Cvar_RegisterVariable(&r_shadows_throwdistance);
3314         Cvar_RegisterVariable(&r_shadows_throwdirection);
3315         Cvar_RegisterVariable(&r_shadows_focus);
3316         Cvar_RegisterVariable(&r_shadows_shadowmapscale);
3317         Cvar_RegisterVariable(&r_shadows_shadowmapbias);
3318         Cvar_RegisterVariable(&r_q1bsp_skymasking);
3319         Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
3320         Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
3321         Cvar_RegisterVariable(&r_polygonoffset_decals_factor);
3322         Cvar_RegisterVariable(&r_polygonoffset_decals_offset);
3323         Cvar_RegisterVariable(&r_fog_exp2);
3324         Cvar_RegisterVariable(&r_fog_clear);
3325         Cvar_RegisterVariable(&r_drawfog);
3326         Cvar_RegisterVariable(&r_transparentdepthmasking);
3327         Cvar_RegisterVariable(&r_transparent_sortmindist);
3328         Cvar_RegisterVariable(&r_transparent_sortmaxdist);
3329         Cvar_RegisterVariable(&r_transparent_sortarraysize);
3330         Cvar_RegisterVariable(&r_texture_dds_load);
3331         Cvar_RegisterVariable(&r_texture_dds_save);
3332         Cvar_RegisterVariable(&r_usedepthtextures);
3333         Cvar_RegisterVariable(&r_viewfbo);
3334         Cvar_RegisterVariable(&r_rendertarget_debug);
3335         Cvar_RegisterVariable(&r_viewscale);
3336         Cvar_RegisterVariable(&r_viewscale_fpsscaling);
3337         Cvar_RegisterVariable(&r_viewscale_fpsscaling_min);
3338         Cvar_RegisterVariable(&r_viewscale_fpsscaling_multiply);
3339         Cvar_RegisterVariable(&r_viewscale_fpsscaling_stepsize);
3340         Cvar_RegisterVariable(&r_viewscale_fpsscaling_stepmax);
3341         Cvar_RegisterVariable(&r_viewscale_fpsscaling_target);
3342         Cvar_RegisterVariable(&r_glsl_deluxemapping);
3343         Cvar_RegisterVariable(&r_glsl_offsetmapping);
3344         Cvar_RegisterVariable(&r_glsl_offsetmapping_steps);
3345         Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
3346         Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping_steps);
3347         Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping_refinesteps);
3348         Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
3349         Cvar_RegisterVariable(&r_glsl_offsetmapping_lod);
3350         Cvar_RegisterVariable(&r_glsl_offsetmapping_lod_distance);
3351         Cvar_RegisterVariable(&r_glsl_postprocess);
3352         Cvar_RegisterVariable(&r_glsl_postprocess_uservec1);
3353         Cvar_RegisterVariable(&r_glsl_postprocess_uservec2);
3354         Cvar_RegisterVariable(&r_glsl_postprocess_uservec3);
3355         Cvar_RegisterVariable(&r_glsl_postprocess_uservec4);
3356         Cvar_RegisterVariable(&r_glsl_postprocess_uservec1_enable);
3357         Cvar_RegisterVariable(&r_glsl_postprocess_uservec2_enable);
3358         Cvar_RegisterVariable(&r_glsl_postprocess_uservec3_enable);
3359         Cvar_RegisterVariable(&r_glsl_postprocess_uservec4_enable);
3360         Cvar_RegisterVariable(&r_celshading);
3361         Cvar_RegisterVariable(&r_celoutlines);
3362         Cvar_RegisterVariable(&r_fxaa);
3363
3364         Cvar_RegisterVariable(&r_water);
3365         Cvar_RegisterVariable(&r_water_cameraentitiesonly);
3366         Cvar_RegisterVariable(&r_water_resolutionmultiplier);
3367         Cvar_RegisterVariable(&r_water_clippingplanebias);
3368         Cvar_RegisterVariable(&r_water_refractdistort);
3369         Cvar_RegisterVariable(&r_water_reflectdistort);
3370         Cvar_RegisterVariable(&r_water_scissormode);
3371         Cvar_RegisterVariable(&r_water_lowquality);
3372         Cvar_RegisterVariable(&r_water_hideplayer);
3373
3374         Cvar_RegisterVariable(&r_lerpsprites);
3375         Cvar_RegisterVariable(&r_lerpmodels);
3376         Cvar_RegisterVariable(&r_nolerp_list);
3377         Cvar_RegisterVariable(&r_lerplightstyles);
3378         Cvar_RegisterVariable(&r_waterscroll);
3379         Cvar_RegisterVariable(&r_bloom);
3380         Cvar_RegisterVariable(&r_colorfringe);
3381         Cvar_RegisterVariable(&r_bloom_colorscale);
3382         Cvar_RegisterVariable(&r_bloom_brighten);
3383         Cvar_RegisterVariable(&r_bloom_blur);
3384         Cvar_RegisterVariable(&r_bloom_resolution);
3385         Cvar_RegisterVariable(&r_bloom_colorexponent);
3386         Cvar_RegisterVariable(&r_bloom_colorsubtract);
3387         Cvar_RegisterVariable(&r_bloom_scenebrightness);
3388         Cvar_RegisterVariable(&r_hdr_scenebrightness);
3389         Cvar_RegisterVariable(&r_hdr_glowintensity);
3390         Cvar_RegisterVariable(&r_hdr_irisadaptation);
3391         Cvar_RegisterVariable(&r_hdr_irisadaptation_multiplier);
3392         Cvar_RegisterVariable(&r_hdr_irisadaptation_minvalue);
3393         Cvar_RegisterVariable(&r_hdr_irisadaptation_maxvalue);
3394         Cvar_RegisterVariable(&r_hdr_irisadaptation_value);
3395         Cvar_RegisterVariable(&r_hdr_irisadaptation_fade_up);
3396         Cvar_RegisterVariable(&r_hdr_irisadaptation_fade_down);
3397         Cvar_RegisterVariable(&r_hdr_irisadaptation_radius);
3398         Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
3399         Cvar_RegisterVariable(&developer_texturelogging);
3400         Cvar_RegisterVariable(&gl_lightmaps);
3401         Cvar_RegisterVariable(&r_test);
3402         Cvar_RegisterVariable(&r_batch_multidraw);
3403         Cvar_RegisterVariable(&r_batch_multidraw_mintriangles);
3404         Cvar_RegisterVariable(&r_batch_debugdynamicvertexpath);
3405         Cvar_RegisterVariable(&r_glsl_skeletal);
3406         Cvar_RegisterVariable(&r_glsl_saturation);
3407         Cvar_RegisterVariable(&r_glsl_saturation_redcompensate);
3408         Cvar_RegisterVariable(&r_glsl_vertextextureblend_usebothalphas);
3409         Cvar_RegisterVariable(&r_framedatasize);
3410         for (i = 0;i < R_BUFFERDATA_COUNT;i++)
3411                 Cvar_RegisterVariable(&r_buffermegs[i]);
3412         Cvar_RegisterVariable(&r_batch_dynamicbuffer);
3413         Cvar_RegisterVariable(&r_q1bsp_lightmap_updates_enabled);
3414         Cvar_RegisterVariable(&r_q1bsp_lightmap_updates_combine);
3415         Cvar_RegisterVariable(&r_q1bsp_lightmap_updates_hidden_surfaces);
3416         if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
3417                 Cvar_SetValue(&cvars_all, "r_fullbrights", 0);
3418 #ifdef DP_MOBILETOUCH
3419         // GLES devices have terrible depth precision in general, so...
3420         Cvar_SetValueQuick(&r_nearclip, 4);
3421         Cvar_SetValueQuick(&r_farclip_base, 4096);
3422         Cvar_SetValueQuick(&r_farclip_world, 0);
3423         Cvar_SetValueQuick(&r_useinfinitefarclip, 0);
3424 #endif
3425         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap, NULL, NULL);
3426 }
3427
3428 void Render_Init(void)
3429 {
3430         gl_backend_init();
3431         R_Textures_Init();
3432         GL_Main_Init();
3433         Font_Init();
3434         GL_Draw_Init();
3435         R_Shadow_Init();
3436         R_Sky_Init();
3437         GL_Surf_Init();
3438         Sbar_Init();
3439         R_Particles_Init();
3440         R_Explosion_Init();
3441         R_LightningBeams_Init();
3442         CL_MeshEntities_Init();
3443         Mod_RenderInit();
3444 }
3445
3446 static void R_GetCornerOfBox(vec3_t out, const vec3_t mins, const vec3_t maxs, int signbits)
3447 {
3448         out[0] = ((signbits & 1) ? mins : maxs)[0];
3449         out[1] = ((signbits & 2) ? mins : maxs)[1];
3450         out[2] = ((signbits & 4) ? mins : maxs)[2];
3451 }
3452
3453 static qbool _R_CullBox(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes, int ignore)
3454 {
3455         int i;
3456         const mplane_t *p;
3457         vec3_t corner;
3458         if (r_trippy.integer)
3459                 return false;
3460         for (i = 0;i < numplanes;i++)
3461         {
3462                 if(i == ignore)
3463                         continue;
3464                 p = planes + i;
3465                 R_GetCornerOfBox(corner, mins, maxs, p->signbits);
3466                 if (DotProduct(p->normal, corner) < p->dist)
3467                         return true;
3468         }
3469         return false;
3470 }
3471
3472 qbool R_CullFrustum(const vec3_t mins, const vec3_t maxs)
3473 {
3474         // skip nearclip plane, it often culls portals when you are very close, and is almost never useful
3475         return _R_CullBox(mins, maxs, r_refdef.view.numfrustumplanes, r_refdef.view.frustum, 4);
3476 }
3477
3478 qbool R_CullBox(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
3479 {
3480         // nothing to ignore
3481         return _R_CullBox(mins, maxs, numplanes, planes, -1);
3482 }
3483
3484 //==================================================================================
3485
3486 // LadyHavoc: this stores temporary data used within the same frame
3487
3488 typedef struct r_framedata_mem_s
3489 {
3490         struct r_framedata_mem_s *purge; // older mem block to free on next frame
3491         size_t size; // how much usable space
3492         size_t current; // how much space in use
3493         size_t mark; // last "mark" location, temporary memory can be freed by returning to this
3494         size_t wantedsize; // how much space was allocated
3495         unsigned char *data; // start of real data (16byte aligned)
3496 }
3497 r_framedata_mem_t;
3498
3499 static r_framedata_mem_t *r_framedata_mem;
3500
3501 void R_FrameData_Reset(void)
3502 {
3503         while (r_framedata_mem)
3504         {
3505                 r_framedata_mem_t *next = r_framedata_mem->purge;
3506                 Mem_Free(r_framedata_mem);
3507                 r_framedata_mem = next;
3508         }
3509 }
3510
3511 static void R_FrameData_Resize(qbool mustgrow)
3512 {
3513         size_t wantedsize;
3514         wantedsize = (size_t)(r_framedatasize.value * 1024*1024);
3515         wantedsize = bound(65536, wantedsize, 1000*1024*1024);
3516         if (!r_framedata_mem || r_framedata_mem->wantedsize != wantedsize || mustgrow)
3517         {
3518                 r_framedata_mem_t *newmem = (r_framedata_mem_t *)Mem_Alloc(r_main_mempool, wantedsize);
3519                 newmem->wantedsize = wantedsize;
3520                 newmem->data = (unsigned char *)(((size_t)(newmem+1) + 15) & ~15);
3521                 newmem->size = (unsigned char *)newmem + wantedsize - newmem->data;
3522                 newmem->current = 0;
3523                 newmem->mark = 0;
3524                 newmem->purge = r_framedata_mem;
3525                 r_framedata_mem = newmem;
3526         }
3527 }
3528
3529 void R_FrameData_NewFrame(void)
3530 {
3531         R_FrameData_Resize(false);
3532         if (!r_framedata_mem)
3533                 return;
3534         // if we ran out of space on the last frame, free the old memory now
3535         while (r_framedata_mem->purge)
3536         {
3537                 // repeatedly remove the second item in the list, leaving only head
3538                 r_framedata_mem_t *next = r_framedata_mem->purge->purge;
3539                 Mem_Free(r_framedata_mem->purge);
3540                 r_framedata_mem->purge = next;
3541         }
3542         // reset the current mem pointer
3543         r_framedata_mem->current = 0;
3544         r_framedata_mem->mark = 0;
3545 }
3546
3547 void *R_FrameData_Alloc(size_t size)
3548 {
3549         void *data;
3550         float newvalue;
3551
3552         // align to 16 byte boundary - the data pointer is already aligned, so we
3553         // only need to ensure the size of every allocation is also aligned
3554         size = (size + 15) & ~15;
3555
3556         while (!r_framedata_mem || r_framedata_mem->current + size > r_framedata_mem->size)
3557         {
3558                 // emergency - we ran out of space, allocate more memory
3559                 // note: this has no upper-bound, we'll fail to allocate memory eventually and just die
3560                 newvalue = r_framedatasize.value * 2.0f;
3561                 // upper bound based on architecture - if we try to allocate more than this we could overflow, better to loop until we error out on allocation failure
3562                 if (sizeof(size_t) >= 8)
3563                         newvalue = bound(0.25f, newvalue, (float)(1ll << 42));
3564                 else
3565                         newvalue = bound(0.25f, newvalue, (float)(1 << 10));
3566                 // this might not be a growing it, but we'll allocate another buffer every time
3567                 Cvar_SetValueQuick(&r_framedatasize, newvalue);
3568                 R_FrameData_Resize(true);
3569         }
3570
3571         data = r_framedata_mem->data + r_framedata_mem->current;
3572         r_framedata_mem->current += size;
3573
3574         // count the usage for stats
3575         r_refdef.stats[r_stat_framedatacurrent] = max(r_refdef.stats[r_stat_framedatacurrent], (int)r_framedata_mem->current);
3576         r_refdef.stats[r_stat_framedatasize] = max(r_refdef.stats[r_stat_framedatasize], (int)r_framedata_mem->size);
3577
3578         return (void *)data;
3579 }
3580
3581 void *R_FrameData_Store(size_t size, void *data)
3582 {
3583         void *d = R_FrameData_Alloc(size);
3584         if (d && data)
3585                 memcpy(d, data, size);
3586         return d;
3587 }
3588
3589 void R_FrameData_SetMark(void)
3590 {
3591         if (!r_framedata_mem)
3592                 return;
3593         r_framedata_mem->mark = r_framedata_mem->current;
3594 }
3595
3596 void R_FrameData_ReturnToMark(void)
3597 {
3598         if (!r_framedata_mem)
3599                 return;
3600         r_framedata_mem->current = r_framedata_mem->mark;
3601 }
3602
3603 //==================================================================================
3604
3605 // avoid reusing the same buffer objects on consecutive frames
3606 #define R_BUFFERDATA_CYCLE 3
3607
3608 typedef struct r_bufferdata_buffer_s
3609 {
3610         struct r_bufferdata_buffer_s *purge; // older buffer to free on next frame
3611         size_t size; // how much usable space
3612         size_t current; // how much space in use
3613         r_meshbuffer_t *buffer; // the buffer itself
3614 }
3615 r_bufferdata_buffer_t;
3616
3617 static int r_bufferdata_cycle = 0; // incremented and wrapped each frame
3618 static r_bufferdata_buffer_t *r_bufferdata_buffer[R_BUFFERDATA_CYCLE][R_BUFFERDATA_COUNT];
3619
3620 /// frees all dynamic buffers
3621 void R_BufferData_Reset(void)
3622 {
3623         int cycle, type;
3624         r_bufferdata_buffer_t **p, *mem;
3625         for (cycle = 0;cycle < R_BUFFERDATA_CYCLE;cycle++)
3626         {
3627                 for (type = 0;type < R_BUFFERDATA_COUNT;type++)
3628                 {
3629                         // free all buffers
3630                         p = &r_bufferdata_buffer[cycle][type];
3631                         while (*p)
3632                         {
3633                                 mem = *p;
3634                                 *p = (*p)->purge;
3635                                 if (mem->buffer)
3636                                         R_Mesh_DestroyMeshBuffer(mem->buffer);
3637                                 Mem_Free(mem);
3638                         }
3639                 }
3640         }
3641 }
3642
3643 // resize buffer as needed (this actually makes a new one, the old one will be recycled next frame)
3644 static void R_BufferData_Resize(r_bufferdata_type_t type, qbool mustgrow, size_t minsize)
3645 {
3646         r_bufferdata_buffer_t *mem = r_bufferdata_buffer[r_bufferdata_cycle][type];
3647         size_t size;
3648         float newvalue = r_buffermegs[type].value;
3649
3650         // increase the cvar if we have to (but only if we already have a mem)
3651         if (mustgrow && mem)
3652                 newvalue *= 2.0f;
3653         newvalue = bound(0.25f, newvalue, 256.0f);
3654         while (newvalue * 1024*1024 < minsize)
3655                 newvalue *= 2.0f;
3656
3657         // clamp the cvar to valid range
3658         newvalue = bound(0.25f, newvalue, 256.0f);
3659         if (r_buffermegs[type].value != newvalue)
3660                 Cvar_SetValueQuick(&r_buffermegs[type], newvalue);
3661
3662         // calculate size in bytes
3663         size = (size_t)(newvalue * 1024*1024);
3664         size = bound(131072, size, 256*1024*1024);
3665
3666         // allocate a new buffer if the size is different (purge old one later)
3667         // or if we were told we must grow the buffer
3668         if (!mem || mem->size != size || mustgrow)
3669         {
3670                 mem = (r_bufferdata_buffer_t *)Mem_Alloc(r_main_mempool, sizeof(*mem));
3671                 mem->size = size;
3672                 mem->current = 0;
3673                 if (type == R_BUFFERDATA_VERTEX)
3674                         mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbuffervertex", false, false, true, false);
3675                 else if (type == R_BUFFERDATA_INDEX16)
3676                         mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbufferindex16", true, false, true, true);
3677                 else if (type == R_BUFFERDATA_INDEX32)
3678                         mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbufferindex32", true, false, true, false);
3679                 else if (type == R_BUFFERDATA_UNIFORM)
3680                         mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbufferuniform", false, true, true, false);
3681                 mem->purge = r_bufferdata_buffer[r_bufferdata_cycle][type];
3682                 r_bufferdata_buffer[r_bufferdata_cycle][type] = mem;
3683         }
3684 }
3685
3686 void R_BufferData_NewFrame(void)
3687 {
3688         int type;
3689         r_bufferdata_buffer_t **p, *mem;
3690         // cycle to the next frame's buffers
3691         r_bufferdata_cycle = (r_bufferdata_cycle + 1) % R_BUFFERDATA_CYCLE;
3692         // if we ran out of space on the last time we used these buffers, free the old memory now
3693         for (type = 0;type < R_BUFFERDATA_COUNT;type++)
3694         {
3695                 if (r_bufferdata_buffer[r_bufferdata_cycle][type])
3696                 {
3697                         R_BufferData_Resize((r_bufferdata_type_t)type, false, 131072);
3698                         // free all but the head buffer, this is how we recycle obsolete
3699                         // buffers after they are no longer in use
3700                         p = &r_bufferdata_buffer[r_bufferdata_cycle][type]->purge;
3701                         while (*p)
3702                         {
3703                                 mem = *p;
3704                                 *p = (*p)->purge;
3705                                 if (mem->buffer)
3706                                         R_Mesh_DestroyMeshBuffer(mem->buffer);
3707                                 Mem_Free(mem);
3708                         }
3709                         // reset the current offset
3710                         r_bufferdata_buffer[r_bufferdata_cycle][type]->current = 0;
3711                 }
3712         }
3713 }
3714
3715 r_meshbuffer_t *R_BufferData_Store(size_t datasize, const void *data, r_bufferdata_type_t type, int *returnbufferoffset)
3716 {
3717         r_bufferdata_buffer_t *mem;
3718         int offset = 0;
3719         int padsize;
3720
3721         *returnbufferoffset = 0;
3722
3723         // align size to a byte boundary appropriate for the buffer type, this
3724         // makes all allocations have aligned start offsets
3725         if (type == R_BUFFERDATA_UNIFORM)
3726                 padsize = (datasize + r_uniformbufferalignment - 1) & ~(r_uniformbufferalignment - 1);
3727         else
3728                 padsize = (datasize + 15) & ~15;
3729
3730         // if we ran out of space in this buffer we must allocate a new one
3731         if (!r_bufferdata_buffer[r_bufferdata_cycle][type] || r_bufferdata_buffer[r_bufferdata_cycle][type]->current + padsize > r_bufferdata_buffer[r_bufferdata_cycle][type]->size)
3732                 R_BufferData_Resize(type, true, padsize);
3733
3734         // if the resize did not give us enough memory, fail
3735         if (!r_bufferdata_buffer[r_bufferdata_cycle][type] || r_bufferdata_buffer[r_bufferdata_cycle][type]->current + padsize > r_bufferdata_buffer[r_bufferdata_cycle][type]->size)
3736                 Sys_Error("R_BufferData_Store: failed to create a new buffer of sufficient size\n");
3737
3738         mem = r_bufferdata_buffer[r_bufferdata_cycle][type];
3739         offset = (int)mem->current;
3740         mem->current += padsize;
3741
3742         // upload the data to the buffer at the chosen offset
3743         if (offset == 0)
3744                 R_Mesh_UpdateMeshBuffer(mem->buffer, NULL, mem->size, false, 0);
3745         R_Mesh_UpdateMeshBuffer(mem->buffer, data, datasize, true, offset);
3746
3747         // count the usage for stats
3748         r_refdef.stats[r_stat_bufferdatacurrent_vertex + type] = max(r_refdef.stats[r_stat_bufferdatacurrent_vertex + type], (int)mem->current);
3749         r_refdef.stats[r_stat_bufferdatasize_vertex + type] = max(r_refdef.stats[r_stat_bufferdatasize_vertex + type], (int)mem->size);
3750
3751         // return the buffer offset
3752         *returnbufferoffset = offset;
3753
3754         return mem->buffer;
3755 }
3756
3757 //==================================================================================
3758
3759 // LadyHavoc: animcache originally written by Echon, rewritten since then
3760
3761 /**
3762  * Animation cache prevents re-generating mesh data for an animated model
3763  * multiple times in one frame for lighting, shadowing, reflections, etc.
3764  */
3765
3766 void R_AnimCache_Free(void)
3767 {
3768 }
3769
3770 void R_AnimCache_ClearCache(void)
3771 {
3772         int i;
3773         entity_render_t *ent;
3774
3775         for (i = 0;i < r_refdef.scene.numentities;i++)
3776         {
3777                 ent = r_refdef.scene.entities[i];
3778                 ent->animcache_vertex3f = NULL;
3779                 ent->animcache_vertex3f_vertexbuffer = NULL;
3780                 ent->animcache_vertex3f_bufferoffset = 0;
3781                 ent->animcache_normal3f = NULL;
3782                 ent->animcache_normal3f_vertexbuffer = NULL;
3783                 ent->animcache_normal3f_bufferoffset = 0;
3784                 ent->animcache_svector3f = NULL;
3785                 ent->animcache_svector3f_vertexbuffer = NULL;
3786                 ent->animcache_svector3f_bufferoffset = 0;
3787                 ent->animcache_tvector3f = NULL;
3788                 ent->animcache_tvector3f_vertexbuffer = NULL;
3789                 ent->animcache_tvector3f_bufferoffset = 0;
3790                 ent->animcache_skeletaltransform3x4 = NULL;
3791                 ent->animcache_skeletaltransform3x4buffer = NULL;
3792                 ent->animcache_skeletaltransform3x4offset = 0;
3793                 ent->animcache_skeletaltransform3x4size = 0;
3794         }
3795 }
3796
3797 qbool R_AnimCache_GetEntity(entity_render_t *ent, qbool wantnormals, qbool wanttangents)
3798 {
3799         model_t *model = ent->model;
3800         int numvertices;
3801
3802         // see if this ent is worth caching
3803         if (!model || !model->Draw || !model->AnimateVertices)
3804                 return false;
3805         // nothing to cache if it contains no animations and has no skeleton
3806         if (!model->surfmesh.isanimated && !(model->num_bones && ent->skeleton && ent->skeleton->relativetransforms))
3807                 return false;
3808         // see if it is already cached for gpuskeletal
3809         if (ent->animcache_skeletaltransform3x4)
3810                 return false;
3811         // see if it is already cached as a mesh
3812         if (ent->animcache_vertex3f)
3813         {
3814                 // check if we need to add normals or tangents
3815                 if (ent->animcache_normal3f)
3816                         wantnormals = false;
3817                 if (ent->animcache_svector3f)
3818                         wanttangents = false;
3819                 if (!wantnormals && !wanttangents)
3820                         return false;
3821         }
3822
3823         // check which kind of cache we need to generate
3824         if (r_gpuskeletal && model->num_bones > 0 && model->surfmesh.data_skeletalindex4ub)
3825         {
3826                 // cache the skeleton so the vertex shader can use it
3827                 r_refdef.stats[r_stat_animcache_skeletal_count] += 1;
3828                 r_refdef.stats[r_stat_animcache_skeletal_bones] += model->num_bones;
3829                 r_refdef.stats[r_stat_animcache_skeletal_maxbones] = max(r_refdef.stats[r_stat_animcache_skeletal_maxbones], model->num_bones);
3830                 ent->animcache_skeletaltransform3x4 = (float *)R_FrameData_Alloc(sizeof(float[3][4]) * model->num_bones);
3831                 Mod_Skeletal_BuildTransforms(model, ent->frameblend, ent->skeleton, NULL, ent->animcache_skeletaltransform3x4); 
3832                 // note: this can fail if the buffer is at the grow limit
3833                 ent->animcache_skeletaltransform3x4size = sizeof(float[3][4]) * model->num_bones;
3834                 ent->animcache_skeletaltransform3x4buffer = R_BufferData_Store(ent->animcache_skeletaltransform3x4size, ent->animcache_skeletaltransform3x4, R_BUFFERDATA_UNIFORM, &ent->animcache_skeletaltransform3x4offset);
3835         }
3836         else if (ent->animcache_vertex3f)
3837         {
3838                 // mesh was already cached but we may need to add normals/tangents
3839                 // (this only happens with multiple views, reflections, cameras, etc)
3840                 if (wantnormals || wanttangents)
3841                 {
3842                         numvertices = model->surfmesh.num_vertices;
3843                         if (wantnormals)
3844                                 ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3845                         if (wanttangents)
3846                         {
3847                                 ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3848                                 ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3849                         }
3850                         model->AnimateVertices(model, ent->frameblend, ent->skeleton, NULL, wantnormals ? ent->animcache_normal3f : NULL, wanttangents ? ent->animcache_svector3f : NULL, wanttangents ? ent->animcache_tvector3f : NULL);
3851                         r_refdef.stats[r_stat_animcache_shade_count] += 1;
3852                         r_refdef.stats[r_stat_animcache_shade_vertices] += numvertices;
3853                         r_refdef.stats[r_stat_animcache_shade_maxvertices] = max(r_refdef.stats[r_stat_animcache_shade_maxvertices], numvertices);
3854                 }
3855         }
3856         else
3857         {
3858                 // generate mesh cache
3859                 numvertices = model->surfmesh.num_vertices;
3860                 ent->animcache_vertex3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3861                 if (wantnormals)
3862                         ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3863                 if (wanttangents)
3864                 {
3865                         ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3866                         ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3867                 }
3868                 model->AnimateVertices(model, ent->frameblend, ent->skeleton, ent->animcache_vertex3f, ent->animcache_normal3f, ent->animcache_svector3f, ent->animcache_tvector3f);
3869                 if (wantnormals || wanttangents)
3870                 {
3871                         r_refdef.stats[r_stat_animcache_shade_count] += 1;
3872                         r_refdef.stats[r_stat_animcache_shade_vertices] += numvertices;
3873                         r_refdef.stats[r_stat_animcache_shade_maxvertices] = max(r_refdef.stats[r_stat_animcache_shade_maxvertices], numvertices);
3874                 }
3875                 r_refdef.stats[r_stat_animcache_shape_count] += 1;
3876                 r_refdef.stats[r_stat_animcache_shape_vertices] += numvertices;
3877                 r_refdef.stats[r_stat_animcache_shape_maxvertices] = max(r_refdef.stats[r_stat_animcache_shape_maxvertices], numvertices);
3878         }
3879         return true;
3880 }
3881
3882 void R_AnimCache_CacheVisibleEntities(void)
3883 {
3884         int i;
3885
3886         // TODO: thread this
3887         // NOTE: R_PrepareRTLights() also caches entities
3888
3889         for (i = 0;i < r_refdef.scene.numentities;i++)
3890                 if (r_refdef.viewcache.entityvisible[i])
3891                         R_AnimCache_GetEntity(r_refdef.scene.entities[i], true, true);
3892 }
3893
3894 //==================================================================================
3895
3896 qbool R_CanSeeBox(int numsamples, vec_t eyejitter, vec_t entboxenlarge, vec_t entboxexpand, vec_t pad, vec3_t eye, vec3_t entboxmins, vec3_t entboxmaxs)
3897 {
3898         long unsigned int i;
3899         int j;
3900         vec3_t eyemins, eyemaxs;
3901         vec3_t boxmins, boxmaxs;
3902         vec3_t padmins, padmaxs;
3903         vec3_t start;
3904         vec3_t end;
3905         model_t *model = r_refdef.scene.worldmodel;
3906         static vec3_t positions[] = {
3907                 { 0.5f, 0.5f, 0.5f },
3908                 { 0.0f, 0.0f, 0.0f },
3909                 { 0.0f, 0.0f, 1.0f },
3910                 { 0.0f, 1.0f, 0.0f },
3911                 { 0.0f, 1.0f, 1.0f },
3912                 { 1.0f, 0.0f, 0.0f },
3913                 { 1.0f, 0.0f, 1.0f },
3914                 { 1.0f, 1.0f, 0.0f },
3915                 { 1.0f, 1.0f, 1.0f },
3916         };
3917
3918         // sample count can be set to -1 to skip this logic, for flicker-prone objects
3919         if (numsamples < 0)
3920                 return true;
3921
3922         // view origin is not used for culling in portal/reflection/refraction renders or isometric views
3923         if (!r_refdef.view.usevieworiginculling)
3924                 return true;
3925
3926         if (!r_cullentities_trace_entityocclusion.integer && (!model || !model->brush.TraceLineOfSight))
3927                 return true;
3928
3929         // expand the eye box a little
3930         eyemins[0] = eye[0] - eyejitter;
3931         eyemaxs[0] = eye[0] + eyejitter;
3932         eyemins[1] = eye[1] - eyejitter;
3933         eyemaxs[1] = eye[1] + eyejitter;
3934         eyemins[2] = eye[2] - eyejitter;
3935         eyemaxs[2] = eye[2] + eyejitter;
3936         // expand the box a little
3937         boxmins[0] = (entboxenlarge + 1) * entboxmins[0] - entboxenlarge * entboxmaxs[0] - entboxexpand;
3938         boxmaxs[0] = (entboxenlarge + 1) * entboxmaxs[0] - entboxenlarge * entboxmins[0] + entboxexpand;
3939         boxmins[1] = (entboxenlarge + 1) * entboxmins[1] - entboxenlarge * entboxmaxs[1] - entboxexpand;
3940         boxmaxs[1] = (entboxenlarge + 1) * entboxmaxs[1] - entboxenlarge * entboxmins[1] + entboxexpand;
3941         boxmins[2] = (entboxenlarge + 1) * entboxmins[2] - entboxenlarge * entboxmaxs[2] - entboxexpand;
3942         boxmaxs[2] = (entboxenlarge + 1) * entboxmaxs[2] - entboxenlarge * entboxmins[2] + entboxexpand;
3943         // make an even larger box for the acceptable area
3944         padmins[0] = boxmins[0] - pad;
3945         padmaxs[0] = boxmaxs[0] + pad;
3946         padmins[1] = boxmins[1] - pad;
3947         padmaxs[1] = boxmaxs[1] + pad;
3948         padmins[2] = boxmins[2] - pad;
3949         padmaxs[2] = boxmaxs[2] + pad;
3950
3951         // return true if eye overlaps enlarged box
3952         if (BoxesOverlap(boxmins, boxmaxs, eyemins, eyemaxs))
3953                 return true;
3954
3955         VectorCopy(eye, start);
3956         // try specific positions in the box first - note that these can be cached
3957         if (r_cullentities_trace_entityocclusion.integer)
3958         {
3959                 for (i = 0; i < sizeof(positions) / sizeof(positions[0]); i++)
3960                 {
3961                         trace_t trace;
3962                         end[0] = boxmins[0] + (boxmaxs[0] - boxmins[0]) * positions[i][0];
3963                         end[1] = boxmins[1] + (boxmaxs[1] - boxmins[1]) * positions[i][1];
3964                         end[2] = boxmins[2] + (boxmaxs[2] - boxmins[2]) * positions[i][2];
3965                         //trace_t trace = CL_TraceLine(start, end, MOVE_NORMAL, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, MATERIALFLAGMASK_TRANSLUCENT, 0.0f, true, false, NULL, true, true);
3966                         trace = CL_Cache_TraceLineSurfaces(start, end, MOVE_NORMAL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT);
3967                         // not picky - if the trace ended anywhere in the box we're good
3968                         if (BoxesOverlap(trace.endpos, trace.endpos, padmins, padmaxs))
3969                                 return true;
3970                 }
3971         }
3972         else
3973         {
3974                 // try center
3975                 VectorMAM(0.5f, boxmins, 0.5f, boxmaxs, end);
3976                 if (model->brush.TraceLineOfSight(model, start, end, padmins, padmaxs))
3977                         return true;
3978         }
3979
3980         // try various random positions
3981         for (j = 0; j < numsamples; j++)
3982         {
3983                 VectorSet(start, lhrandom(eyemins[0], eyemaxs[0]), lhrandom(eyemins[1], eyemaxs[1]), lhrandom(eyemins[2], eyemaxs[2]));
3984                 VectorSet(end, lhrandom(boxmins[0], boxmaxs[0]), lhrandom(boxmins[1], boxmaxs[1]), lhrandom(boxmins[2], boxmaxs[2]));
3985                 if (r_cullentities_trace_entityocclusion.integer)
3986                 {
3987                         trace_t trace = CL_TraceLine(start, end, MOVE_NORMAL, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, MATERIALFLAGMASK_TRANSLUCENT, 0.0f, true, false, NULL, true, true);
3988                         // not picky - if the trace ended anywhere in the box we're good
3989                         if (BoxesOverlap(trace.endpos, trace.endpos, padmins, padmaxs))
3990                                 return true;
3991                 }
3992                 else if (model->brush.TraceLineOfSight(model, start, end, padmins, padmaxs))
3993                         return true;
3994         }
3995
3996         return false;
3997 }
3998
3999
4000 static void R_View_UpdateEntityVisible (void)
4001 {
4002         int i;
4003         int renderimask;
4004         int samples;
4005         entity_render_t *ent;
4006
4007         if (r_refdef.envmap || r_fb.water.hideplayer)
4008                 renderimask = RENDER_EXTERIORMODEL | RENDER_VIEWMODEL;
4009         else if (chase_active.integer || r_fb.water.renderingscene)
4010                 renderimask = RENDER_VIEWMODEL;
4011         else
4012                 renderimask = RENDER_EXTERIORMODEL;
4013         if (!r_drawviewmodel.integer)
4014                 renderimask |= RENDER_VIEWMODEL;
4015         if (!r_drawexteriormodel.integer)
4016                 renderimask |= RENDER_EXTERIORMODEL;
4017         memset(r_refdef.viewcache.entityvisible, 0, r_refdef.scene.numentities);
4018         if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs)
4019         {
4020                 // worldmodel can check visibility
4021                 for (i = 0;i < r_refdef.scene.numentities;i++)
4022                 {
4023                         ent = r_refdef.scene.entities[i];
4024                         if (r_refdef.viewcache.world_novis && !(ent->flags & RENDER_VIEWMODEL))
4025                         {
4026                                 r_refdef.viewcache.entityvisible[i] = false;
4027                                 continue;
4028                         }
4029                         if (!(ent->flags & renderimask))
4030                         if (!R_CullFrustum(ent->mins, ent->maxs) || (ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)))
4031                         if ((ent->flags & (RENDER_NODEPTHTEST | RENDER_WORLDOBJECT | RENDER_VIEWMODEL)) || r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, ent->mins, ent->maxs))
4032                                 r_refdef.viewcache.entityvisible[i] = true;
4033                 }
4034         }
4035         else
4036         {
4037                 // no worldmodel or it can't check visibility
4038                 for (i = 0;i < r_refdef.scene.numentities;i++)
4039                 {
4040                         ent = r_refdef.scene.entities[i];
4041                         if (!(ent->flags & renderimask))
4042                         if (!R_CullFrustum(ent->mins, ent->maxs) || (ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)))
4043                                 r_refdef.viewcache.entityvisible[i] = true;
4044                 }
4045         }
4046         if (r_cullentities_trace.integer)
4047         {
4048                 for (i = 0;i < r_refdef.scene.numentities;i++)
4049                 {
4050                         if (!r_refdef.viewcache.entityvisible[i])
4051                                 continue;
4052                         ent = r_refdef.scene.entities[i];
4053                         if (!(ent->flags & (RENDER_VIEWMODEL | RENDER_WORLDOBJECT | RENDER_NODEPTHTEST)) && !(ent->model && (ent->model->name[0] == '*')))
4054                         {
4055                                 samples = ent->last_trace_visibility == 0 ? r_cullentities_trace_tempentitysamples.integer : r_cullentities_trace_samples.integer;
4056                                 if (R_CanSeeBox(samples, r_cullentities_trace_eyejitter.value, r_cullentities_trace_enlarge.value, r_cullentities_trace_expand.value, r_cullentities_trace_pad.value, r_refdef.view.origin, ent->mins, ent->maxs))
4057                                         ent->last_trace_visibility = host.realtime;
4058                                 if (ent->last_trace_visibility < host.realtime - r_cullentities_trace_delay.value)
4059                                         r_refdef.viewcache.entityvisible[i] = 0;
4060                         }
4061                 }
4062         }
4063 }
4064
4065 /// only used if skyrendermasked, and normally returns false
4066 static int R_DrawBrushModelsSky (void)
4067 {
4068         int i, sky;
4069         entity_render_t *ent;
4070
4071         sky = false;
4072         for (i = 0;i < r_refdef.scene.numentities;i++)
4073         {
4074                 if (!r_refdef.viewcache.entityvisible[i])
4075                         continue;
4076                 ent = r_refdef.scene.entities[i];
4077                 if (!ent->model || !ent->model->DrawSky)
4078                         continue;
4079                 ent->model->DrawSky(ent);
4080                 sky = true;
4081         }
4082         return sky;
4083 }
4084
4085 static void R_DrawNoModel(entity_render_t *ent);
4086 static void R_DrawModels(void)
4087 {
4088         int i;
4089         entity_render_t *ent;
4090
4091         for (i = 0;i < r_refdef.scene.numentities;i++)
4092         {
4093                 if (!r_refdef.viewcache.entityvisible[i])
4094                         continue;
4095                 ent = r_refdef.scene.entities[i];
4096                 r_refdef.stats[r_stat_entities]++;
4097
4098                 if (ent->model && ent->model->Draw != NULL)
4099                         ent->model->Draw(ent);
4100                 else
4101                         R_DrawNoModel(ent);
4102         }
4103 }
4104
4105 static void R_DrawModelsDepth(void)
4106 {
4107         int i;
4108         entity_render_t *ent;
4109
4110         for (i = 0;i < r_refdef.scene.numentities;i++)
4111         {
4112                 if (!r_refdef.viewcache.entityvisible[i])
4113                         continue;
4114                 ent = r_refdef.scene.entities[i];
4115                 if (ent->model && ent->model->DrawDepth != NULL)
4116                         ent->model->DrawDepth(ent);
4117         }
4118 }
4119
4120 static void R_DrawModelsDebug(void)
4121 {
4122         int i;
4123         entity_render_t *ent;
4124
4125         for (i = 0;i < r_refdef.scene.numentities;i++)
4126         {
4127                 if (!r_refdef.viewcache.entityvisible[i])
4128                         continue;
4129                 ent = r_refdef.scene.entities[i];
4130                 if (ent->model && ent->model->DrawDebug != NULL)
4131                         ent->model->DrawDebug(ent);
4132         }
4133 }
4134
4135 static void R_DrawModelsAddWaterPlanes(void)
4136 {
4137         int i;
4138         entity_render_t *ent;
4139
4140         for (i = 0;i < r_refdef.scene.numentities;i++)
4141         {
4142                 if (!r_refdef.viewcache.entityvisible[i])
4143                         continue;
4144                 ent = r_refdef.scene.entities[i];
4145                 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
4146                         ent->model->DrawAddWaterPlanes(ent);
4147         }
4148 }
4149
4150 static float irisvecs[7][3] = {{0, 0, 0}, {-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
4151
4152 void R_HDR_UpdateIrisAdaptation(const vec3_t point)
4153 {
4154         if (r_hdr_irisadaptation.integer)
4155         {
4156                 vec3_t p;
4157                 vec3_t ambient;
4158                 vec3_t diffuse;
4159                 vec3_t diffusenormal;
4160                 vec3_t forward;
4161                 vec_t brightness = 0.0f;
4162                 vec_t goal;
4163                 vec_t current;
4164                 vec_t d;
4165                 int c;
4166                 VectorCopy(r_refdef.view.forward, forward);
4167                 for (c = 0;c < (int)(sizeof(irisvecs)/sizeof(irisvecs[0]));c++)
4168                 {
4169                         p[0] = point[0] + irisvecs[c][0] * r_hdr_irisadaptation_radius.value;
4170                         p[1] = point[1] + irisvecs[c][1] * r_hdr_irisadaptation_radius.value;
4171                         p[2] = point[2] + irisvecs[c][2] * r_hdr_irisadaptation_radius.value;
4172                         R_CompleteLightPoint(ambient, diffuse, diffusenormal, p, LP_LIGHTMAP | LP_RTWORLD | LP_DYNLIGHT, r_refdef.scene.lightmapintensity, r_refdef.scene.ambientintensity);
4173                         d = DotProduct(forward, diffusenormal);
4174                         brightness += VectorLength(ambient);
4175                         if (d > 0)
4176                                 brightness += d * VectorLength(diffuse);
4177                 }
4178                 brightness *= 1.0f / c;
4179                 brightness += 0.00001f; // make sure it's never zero
4180                 goal = r_hdr_irisadaptation_multiplier.value / brightness;
4181                 goal = bound(r_hdr_irisadaptation_minvalue.value, goal, r_hdr_irisadaptation_maxvalue.value);
4182                 current = r_hdr_irisadaptation_value.value;
4183                 if (current < goal)
4184                         current = min(current + r_hdr_irisadaptation_fade_up.value * cl.realframetime, goal);
4185                 else if (current > goal)
4186                         current = max(current - r_hdr_irisadaptation_fade_down.value * cl.realframetime, goal);
4187                 if (fabs(r_hdr_irisadaptation_value.value - current) > 0.0001f)
4188                         Cvar_SetValueQuick(&r_hdr_irisadaptation_value, current);
4189         }
4190         else if (r_hdr_irisadaptation_value.value != 1.0f)
4191                 Cvar_SetValueQuick(&r_hdr_irisadaptation_value, 1.0f);
4192 }
4193
4194 extern cvar_t r_lockvisibility;
4195 extern cvar_t r_lockpvs;
4196
4197 static void R_View_SetFrustum(const int *scissor)
4198 {
4199         int i;
4200         double fpx = +1, fnx = -1, fpy = +1, fny = -1;
4201         vec3_t forward, left, up, origin, v;
4202         if(r_lockvisibility.integer)
4203                 return;
4204         if(scissor)
4205         {
4206                 // flipped x coordinates (because x points left here)
4207                 fpx =  1.0 - 2.0 * (scissor[0]              - r_refdef.view.viewport.x) / (double) (r_refdef.view.viewport.width);
4208                 fnx =  1.0 - 2.0 * (scissor[0] + scissor[2] - r_refdef.view.viewport.x) / (double) (r_refdef.view.viewport.width);
4209                 // non-flipped y coordinates
4210                 fny = -1.0 + 2.0 * (scissor[1]              - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
4211                 fpy = -1.0 + 2.0 * (scissor[1] + scissor[3] - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
4212         }
4213
4214         // we can't trust r_refdef.view.forward and friends in reflected scenes
4215         Matrix4x4_ToVectors(&r_refdef.view.matrix, forward, left, up, origin);
4216
4217 #if 0
4218         r_refdef.view.frustum[0].normal[0] = 0 - 1.0 / r_refdef.view.frustum_x;
4219         r_refdef.view.frustum[0].normal[1] = 0 - 0;
4220         r_refdef.view.frustum[0].normal[2] = -1 - 0;
4221         r_refdef.view.frustum[1].normal[0] = 0 + 1.0 / r_refdef.view.frustum_x;
4222         r_refdef.view.frustum[1].normal[1] = 0 + 0;
4223         r_refdef.view.frustum[1].normal[2] = -1 + 0;
4224         r_refdef.view.frustum[2].normal[0] = 0 - 0;
4225         r_refdef.view.frustum[2].normal[1] = 0 - 1.0 / r_refdef.view.frustum_y;
4226         r_refdef.view.frustum[2].normal[2] = -1 - 0;
4227         r_refdef.view.frustum[3].normal[0] = 0 + 0;
4228         r_refdef.view.frustum[3].normal[1] = 0 + 1.0 / r_refdef.view.frustum_y;
4229         r_refdef.view.frustum[3].normal[2] = -1 + 0;
4230 #endif
4231
4232 #if 0
4233         zNear = r_refdef.nearclip;
4234         nudge = 1.0 - 1.0 / (1<<23);
4235         r_refdef.view.frustum[4].normal[0] = 0 - 0;
4236         r_refdef.view.frustum[4].normal[1] = 0 - 0;
4237         r_refdef.view.frustum[4].normal[2] = -1 - -nudge;
4238         r_refdef.view.frustum[4].dist = 0 - -2 * zNear * nudge;
4239         r_refdef.view.frustum[5].normal[0] = 0 + 0;
4240         r_refdef.view.frustum[5].normal[1] = 0 + 0;
4241         r_refdef.view.frustum[5].normal[2] = -1 + -nudge;
4242         r_refdef.view.frustum[5].dist = 0 + -2 * zNear * nudge;
4243 #endif
4244
4245
4246
4247 #if 0
4248         r_refdef.view.frustum[0].normal[0] = m[3] - m[0];
4249         r_refdef.view.frustum[0].normal[1] = m[7] - m[4];
4250         r_refdef.view.frustum[0].normal[2] = m[11] - m[8];
4251         r_refdef.view.frustum[0].dist = m[15] - m[12];
4252
4253         r_refdef.view.frustum[1].normal[0] = m[3] + m[0];
4254         r_refdef.view.frustum[1].normal[1] = m[7] + m[4];
4255         r_refdef.view.frustum[1].normal[2] = m[11] + m[8];
4256         r_refdef.view.frustum[1].dist = m[15] + m[12];
4257
4258         r_refdef.view.frustum[2].normal[0] = m[3] - m[1];
4259         r_refdef.view.frustum[2].normal[1] = m[7] - m[5];
4260         r_refdef.view.frustum[2].normal[2] = m[11] - m[9];
4261         r_refdef.view.frustum[2].dist = m[15] - m[13];
4262
4263         r_refdef.view.frustum[3].normal[0] = m[3] + m[1];
4264         r_refdef.view.frustum[3].normal[1] = m[7] + m[5];
4265         r_refdef.view.frustum[3].normal[2] = m[11] + m[9];
4266         r_refdef.view.frustum[3].dist = m[15] + m[13];
4267
4268         r_refdef.view.frustum[4].normal[0] = m[3] - m[2];
4269         r_refdef.view.frustum[4].normal[1] = m[7] - m[6];
4270         r_refdef.view.frustum[4].normal[2] = m[11] - m[10];
4271         r_refdef.view.frustum[4].dist = m[15] - m[14];
4272
4273         r_refdef.view.frustum[5].normal[0] = m[3] + m[2];
4274         r_refdef.view.frustum[5].normal[1] = m[7] + m[6];
4275         r_refdef.view.frustum[5].normal[2] = m[11] + m[10];
4276         r_refdef.view.frustum[5].dist = m[15] + m[14];
4277 #endif
4278
4279         if (r_refdef.view.useperspective)
4280         {
4281                 // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
4282                 VectorMAMAM(1024, forward, fnx * 1024.0 * r_refdef.view.frustum_x, left, fny * 1024.0 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[0]);
4283                 VectorMAMAM(1024, forward, fpx * 1024.0 * r_refdef.view.frustum_x, left, fny * 1024.0 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[1]);
4284                 VectorMAMAM(1024, forward, fnx * 1024.0 * r_refdef.view.frustum_x, left, fpy * 1024.0 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[2]);
4285                 VectorMAMAM(1024, forward, fpx * 1024.0 * r_refdef.view.frustum_x, left, fpy * 1024.0 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[3]);
4286
4287                 // then the normals from the corners relative to origin
4288                 CrossProduct(r_refdef.view.frustumcorner[2], r_refdef.view.frustumcorner[0], r_refdef.view.frustum[0].normal);
4289                 CrossProduct(r_refdef.view.frustumcorner[1], r_refdef.view.frustumcorner[3], r_refdef.view.frustum[1].normal);
4290                 CrossProduct(r_refdef.view.frustumcorner[0], r_refdef.view.frustumcorner[1], r_refdef.view.frustum[2].normal);
4291                 CrossProduct(r_refdef.view.frustumcorner[3], r_refdef.view.frustumcorner[2], r_refdef.view.frustum[3].normal);
4292
4293                 // in a NORMAL view, forward cross left == up
4294                 // in a REFLECTED view, forward cross left == down
4295                 // so our cross products above need to be adjusted for a left handed coordinate system
4296                 CrossProduct(forward, left, v);
4297                 if(DotProduct(v, up) < 0)
4298                 {
4299                         VectorNegate(r_refdef.view.frustum[0].normal, r_refdef.view.frustum[0].normal);
4300                         VectorNegate(r_refdef.view.frustum[1].normal, r_refdef.view.frustum[1].normal);
4301                         VectorNegate(r_refdef.view.frustum[2].normal, r_refdef.view.frustum[2].normal);
4302                         VectorNegate(r_refdef.view.frustum[3].normal, r_refdef.view.frustum[3].normal);
4303                 }
4304
4305                 // Leaving those out was a mistake, those were in the old code, and they
4306                 // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
4307                 // I couldn't reproduce it after adding those normalizations. --blub
4308                 VectorNormalize(r_refdef.view.frustum[0].normal);
4309                 VectorNormalize(r_refdef.view.frustum[1].normal);
4310                 VectorNormalize(r_refdef.view.frustum[2].normal);
4311                 VectorNormalize(r_refdef.view.frustum[3].normal);
4312
4313                 // make the corners absolute
4314                 VectorAdd(r_refdef.view.frustumcorner[0], r_refdef.view.origin, r_refdef.view.frustumcorner[0]);
4315                 VectorAdd(r_refdef.view.frustumcorner[1], r_refdef.view.origin, r_refdef.view.frustumcorner[1]);
4316                 VectorAdd(r_refdef.view.frustumcorner[2], r_refdef.view.origin, r_refdef.view.frustumcorner[2]);
4317                 VectorAdd(r_refdef.view.frustumcorner[3], r_refdef.view.origin, r_refdef.view.frustumcorner[3]);
4318
4319                 // one more normal
4320                 VectorCopy(forward, r_refdef.view.frustum[4].normal);
4321
4322                 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal);
4323                 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal);
4324                 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal);
4325                 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal);
4326                 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) + r_refdef.nearclip;
4327         }
4328         else
4329         {
4330                 VectorScale(left, -1.0f, r_refdef.view.frustum[0].normal);
4331                 VectorScale(left,  1.0f, r_refdef.view.frustum[1].normal);
4332                 VectorScale(up, -1.0f, r_refdef.view.frustum[2].normal);
4333                 VectorScale(up,  1.0f, r_refdef.view.frustum[3].normal);
4334                 VectorScale(forward, -1.0f, r_refdef.view.frustum[4].normal);
4335                 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal) - r_refdef.view.ortho_x;
4336                 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal) - r_refdef.view.ortho_x;
4337                 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal) - r_refdef.view.ortho_y;
4338                 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal) - r_refdef.view.ortho_y;
4339                 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) - r_refdef.farclip;
4340         }
4341         r_refdef.view.numfrustumplanes = 5;
4342
4343         if (r_refdef.view.useclipplane)
4344         {
4345                 r_refdef.view.numfrustumplanes = 6;
4346                 r_refdef.view.frustum[5] = r_refdef.view.clipplane;
4347         }
4348
4349         for (i = 0;i < r_refdef.view.numfrustumplanes;i++)
4350                 PlaneClassify(r_refdef.view.frustum + i);
4351
4352         // LadyHavoc: note to all quake engine coders, Quake had a special case
4353         // for 90 degrees which assumed a square view (wrong), so I removed it,
4354         // Quake2 has it disabled as well.
4355
4356         // rotate R_VIEWFORWARD right by FOV_X/2 degrees
4357         //RotatePointAroundVector( r_refdef.view.frustum[0].normal, up, forward, -(90 - r_refdef.fov_x / 2));
4358         //r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, frustum[0].normal);
4359         //PlaneClassify(&frustum[0]);
4360
4361         // rotate R_VIEWFORWARD left by FOV_X/2 degrees
4362         //RotatePointAroundVector( r_refdef.view.frustum[1].normal, up, forward, (90 - r_refdef.fov_x / 2));
4363         //r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, frustum[1].normal);
4364         //PlaneClassify(&frustum[1]);
4365
4366         // rotate R_VIEWFORWARD up by FOV_X/2 degrees
4367         //RotatePointAroundVector( r_refdef.view.frustum[2].normal, left, forward, -(90 - r_refdef.fov_y / 2));
4368         //r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, frustum[2].normal);
4369         //PlaneClassify(&frustum[2]);
4370
4371         // rotate R_VIEWFORWARD down by FOV_X/2 degrees
4372         //RotatePointAroundVector( r_refdef.view.frustum[3].normal, left, forward, (90 - r_refdef.fov_y / 2));
4373         //r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, frustum[3].normal);
4374         //PlaneClassify(&frustum[3]);
4375
4376         // nearclip plane
4377         //VectorCopy(forward, r_refdef.view.frustum[4].normal);
4378         //r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, frustum[4].normal) + r_nearclip.value;
4379         //PlaneClassify(&frustum[4]);
4380 }
4381
4382 static void R_View_UpdateWithScissor(const int *myscissor)
4383 {
4384         R_Main_ResizeViewCache();
4385         R_View_SetFrustum(myscissor);
4386         R_View_WorldVisibility(!r_refdef.view.usevieworiginculling);
4387         R_View_UpdateEntityVisible();
4388 }
4389
4390 static void R_View_Update(void)
4391 {
4392         R_Main_ResizeViewCache();
4393         R_View_SetFrustum(NULL);
4394         R_View_WorldVisibility(!r_refdef.view.usevieworiginculling);
4395         R_View_UpdateEntityVisible();
4396 }
4397
4398 float viewscalefpsadjusted = 1.0f;
4399
4400 void R_SetupView(qbool allowwaterclippingplane, int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4401 {
4402         const float *customclipplane = NULL;
4403         float plane[4];
4404         int /*rtwidth,*/ rtheight;
4405         if (r_refdef.view.useclipplane && allowwaterclippingplane)
4406         {
4407                 // LadyHavoc: couldn't figure out how to make this approach work the same in DPSOFTRAST
4408                 vec_t dist = r_refdef.view.clipplane.dist - r_water_clippingplanebias.value;
4409                 vec_t viewdist = DotProduct(r_refdef.view.origin, r_refdef.view.clipplane.normal);
4410                 if (viewdist < r_refdef.view.clipplane.dist + r_water_clippingplanebias.value)
4411                         dist = r_refdef.view.clipplane.dist;
4412                 plane[0] = r_refdef.view.clipplane.normal[0];
4413                 plane[1] = r_refdef.view.clipplane.normal[1];
4414                 plane[2] = r_refdef.view.clipplane.normal[2];
4415                 plane[3] = -dist;
4416                 customclipplane = plane;
4417         }
4418
4419         //rtwidth = viewfbo ? R_TextureWidth(viewdepthtexture ? viewdepthtexture : viewcolortexture) : vid.width;
4420         rtheight = viewfbo ? R_TextureHeight(viewdepthtexture ? viewdepthtexture : viewcolortexture) : vid.height;
4421
4422         if (!r_refdef.view.useperspective)
4423                 R_Viewport_InitOrtho3D(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, rtheight - viewheight - viewy, viewwidth, viewheight, r_refdef.view.ortho_x, r_refdef.view.ortho_y, -r_refdef.farclip, r_refdef.farclip, customclipplane);
4424         else if (vid.stencil && r_useinfinitefarclip.integer)
4425                 R_Viewport_InitPerspectiveInfinite(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, rtheight - viewheight - viewy, viewwidth, viewheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, customclipplane);
4426         else
4427                 R_Viewport_InitPerspective(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, rtheight - viewheight - viewy, viewwidth, viewheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, r_refdef.farclip, customclipplane);
4428         R_Mesh_SetRenderTargets(viewfbo, viewdepthtexture, viewcolortexture, NULL, NULL, NULL);
4429         R_SetViewport(&r_refdef.view.viewport);
4430 }
4431
4432 void R_EntityMatrix(const matrix4x4_t *matrix)
4433 {
4434         if (gl_modelmatrixchanged || memcmp(matrix, &gl_modelmatrix, sizeof(matrix4x4_t)))
4435         {
4436                 gl_modelmatrixchanged = false;
4437                 gl_modelmatrix = *matrix;
4438                 Matrix4x4_Concat(&gl_modelviewmatrix, &gl_viewmatrix, &gl_modelmatrix);
4439                 Matrix4x4_Concat(&gl_modelviewprojectionmatrix, &gl_projectionmatrix, &gl_modelviewmatrix);
4440                 Matrix4x4_ToArrayFloatGL(&gl_modelviewmatrix, gl_modelview16f);
4441                 Matrix4x4_ToArrayFloatGL(&gl_modelviewprojectionmatrix, gl_modelviewprojection16f);
4442                 CHECKGLERROR
4443                 switch(vid.renderpath)
4444                 {
4445                 case RENDERPATH_GL32:
4446                 case RENDERPATH_GLES2:
4447                         if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
4448                         if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
4449                         break;
4450                 }
4451         }
4452 }
4453
4454 void R_ResetViewRendering2D_Common(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight, float x2, float y2)
4455 {
4456         r_viewport_t viewport;
4457
4458         CHECKGLERROR
4459
4460         // GL is weird because it's bottom to top, r_refdef.view.y is top to bottom
4461         R_Viewport_InitOrtho(&viewport, &identitymatrix, viewx, vid.height - viewheight - viewy, viewwidth, viewheight, 0, 0, x2, y2, -10, 100, NULL);
4462         R_Mesh_SetRenderTargets(viewfbo, viewdepthtexture, viewcolortexture, NULL, NULL, NULL);
4463         R_SetViewport(&viewport);
4464         GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
4465         GL_Color(1, 1, 1, 1);
4466         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4467         GL_BlendFunc(GL_ONE, GL_ZERO);
4468         GL_ScissorTest(false);
4469         GL_DepthMask(false);
4470         GL_DepthRange(0, 1);
4471         GL_DepthTest(false);
4472         GL_DepthFunc(GL_LEQUAL);
4473         R_EntityMatrix(&identitymatrix);
4474         R_Mesh_ResetTextureState();
4475         GL_PolygonOffset(0, 0);
4476         switch(vid.renderpath)
4477         {
4478         case RENDERPATH_GL32:
4479         case RENDERPATH_GLES2:
4480                 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
4481                 break;
4482         }
4483         GL_CullFace(GL_NONE);
4484
4485         CHECKGLERROR
4486 }
4487
4488 void R_ResetViewRendering2D(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4489 {
4490         R_ResetViewRendering2D_Common(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight, 1.0f, 1.0f);
4491 }
4492
4493 void R_ResetViewRendering3D(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4494 {
4495         R_SetupView(true, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
4496         GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
4497         GL_Color(1, 1, 1, 1);
4498         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4499         GL_BlendFunc(GL_ONE, GL_ZERO);
4500         GL_ScissorTest(true);
4501         GL_DepthMask(true);
4502         GL_DepthRange(0, 1);
4503         GL_DepthTest(true);
4504         GL_DepthFunc(GL_LEQUAL);
4505         R_EntityMatrix(&identitymatrix);
4506         R_Mesh_ResetTextureState();
4507         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
4508         switch(vid.renderpath)
4509         {
4510         case RENDERPATH_GL32:
4511         case RENDERPATH_GLES2:
4512                 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
4513                 break;
4514         }
4515         GL_CullFace(r_refdef.view.cullface_back);
4516 }
4517
4518 /*
4519 ================
4520 R_RenderView_UpdateViewVectors
4521 ================
4522 */
4523 void R_RenderView_UpdateViewVectors(void)
4524 {
4525         // break apart the view matrix into vectors for various purposes
4526         // it is important that this occurs outside the RenderScene function because that can be called from reflection renders, where the vectors come out wrong
4527         // however the r_refdef.view.origin IS updated in RenderScene intentionally - otherwise the sky renders at the wrong origin, etc
4528         Matrix4x4_ToVectors(&r_refdef.view.matrix, r_refdef.view.forward, r_refdef.view.left, r_refdef.view.up, r_refdef.view.origin);
4529         VectorNegate(r_refdef.view.left, r_refdef.view.right);
4530         // make an inverted copy of the view matrix for tracking sprites
4531         Matrix4x4_Invert_Full(&r_refdef.view.inverse_matrix, &r_refdef.view.matrix);
4532 }
4533
4534 void R_RenderTarget_FreeUnused(qbool force)
4535 {
4536         unsigned int i, j, end;
4537         end = (unsigned int)Mem_ExpandableArray_IndexRange(&r_fb.rendertargets); // checked
4538         for (i = 0; i < end; i++)
4539         {
4540                 r_rendertarget_t *r = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, i);
4541                 // free resources for rendertargets that have not been used for a while
4542                 // (note: this check is run after the frame render, so any targets used
4543                 // this frame will not be affected even at low framerates)
4544                 if (r && (host.realtime - r->lastusetime > 0.2 || force))
4545                 {
4546                         if (r->fbo)
4547                                 R_Mesh_DestroyFramebufferObject(r->fbo);
4548                         for (j = 0; j < sizeof(r->colortexture) / sizeof(r->colortexture[0]); j++)
4549                                 if (r->colortexture[j])
4550                                         R_FreeTexture(r->colortexture[j]);
4551                         if (r->depthtexture)
4552                                 R_FreeTexture(r->depthtexture);
4553                         Mem_ExpandableArray_FreeRecord(&r_fb.rendertargets, r);
4554                 }
4555         }
4556 }
4557
4558 static void R_CalcTexCoordsForView(float x, float y, float w, float h, float tw, float th, float *texcoord2f)
4559 {
4560         float iw = 1.0f / tw, ih = 1.0f / th, x1, y1, x2, y2;
4561         x1 = x * iw;
4562         x2 = (x + w) * iw;
4563         y1 = (th - y) * ih;
4564         y2 = (th - y - h) * ih;
4565         texcoord2f[0] = x1;
4566         texcoord2f[2] = x2;
4567         texcoord2f[4] = x2;
4568         texcoord2f[6] = x1;
4569         texcoord2f[1] = y1;
4570         texcoord2f[3] = y1;
4571         texcoord2f[5] = y2;
4572         texcoord2f[7] = y2;
4573 }
4574
4575 r_rendertarget_t *R_RenderTarget_Get(int texturewidth, int textureheight, textype_t depthtextype, qbool depthisrenderbuffer, textype_t colortextype0, textype_t colortextype1, textype_t colortextype2, textype_t colortextype3)
4576 {
4577         unsigned int i, j, end;
4578         r_rendertarget_t *r = NULL;
4579         char vabuf[256];
4580         // first try to reuse an existing slot if possible
4581         end = (unsigned int)Mem_ExpandableArray_IndexRange(&r_fb.rendertargets); // checked
4582         for (i = 0; i < end; i++)
4583         {
4584                 r = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, i);
4585                 if (r && r->lastusetime != host.realtime && r->texturewidth == texturewidth && r->textureheight == textureheight && r->depthtextype == depthtextype && r->colortextype[0] == colortextype0 && r->colortextype[1] == colortextype1 && r->colortextype[2] == colortextype2 && r->colortextype[3] == colortextype3)
4586                         break;
4587         }
4588         if (i == end)
4589         {
4590                 // no unused exact match found, so we have to make one in the first unused slot
4591                 r = (r_rendertarget_t *)Mem_ExpandableArray_AllocRecord(&r_fb.rendertargets);
4592                 r->texturewidth = texturewidth;
4593                 r->textureheight = textureheight;
4594                 r->colortextype[0] = colortextype0;
4595                 r->colortextype[1] = colortextype1;
4596                 r->colortextype[2] = colortextype2;
4597                 r->colortextype[3] = colortextype3;
4598                 r->depthtextype = depthtextype;
4599                 r->depthisrenderbuffer = depthisrenderbuffer;
4600                 for (j = 0; j < 4; j++)
4601                         if (r->colortextype[j])
4602                                 r->colortexture[j] = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "rendertarget%i_%i_type%i", i, j, (int)r->colortextype[j]), r->texturewidth, r->textureheight, NULL, r->colortextype[j], TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
4603                 if (r->depthtextype)
4604                 {
4605                         if (r->depthisrenderbuffer)
4606                                 r->depthtexture = R_LoadTextureRenderBuffer(r_main_texturepool, va(vabuf, sizeof(vabuf), "renderbuffer%i_depth_type%i", i, (int)r->depthtextype), r->texturewidth, r->textureheight, r->depthtextype);
4607                         else
4608                                 r->depthtexture = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "rendertarget%i_depth_type%i", i, (int)r->depthtextype), r->texturewidth, r->textureheight, NULL, r->depthtextype, TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
4609                 }
4610                 r->fbo = R_Mesh_CreateFramebufferObject(r->depthtexture, r->colortexture[0], r->colortexture[1], r->colortexture[2], r->colortexture[3]);
4611         }
4612         r_refdef.stats[r_stat_rendertargets_used]++;
4613         r_refdef.stats[r_stat_rendertargets_pixels] += r->texturewidth * r->textureheight;
4614         r->lastusetime = host.realtime;
4615         R_CalcTexCoordsForView(0, 0, r->texturewidth, r->textureheight, r->texturewidth, r->textureheight, r->texcoord2f);
4616         return r;
4617 }
4618
4619 static void R_Water_StartFrame(int viewwidth, int viewheight)
4620 {
4621         int waterwidth, waterheight;
4622
4623         if (viewwidth > (int)vid.maxtexturesize_2d || viewheight > (int)vid.maxtexturesize_2d)
4624                 return;
4625
4626         // set waterwidth and waterheight to the water resolution that will be
4627         // used (often less than the screen resolution for faster rendering)
4628         waterwidth = (int)bound(16, viewwidth * r_water_resolutionmultiplier.value, viewwidth);
4629         waterheight = (int)bound(16, viewheight * r_water_resolutionmultiplier.value, viewheight);
4630
4631         if (!r_water.integer || r_showsurfaces.integer || r_lockvisibility.integer || r_lockpvs.integer)
4632                 waterwidth = waterheight = 0;
4633
4634         // set up variables that will be used in shader setup
4635         r_fb.water.waterwidth = waterwidth;
4636         r_fb.water.waterheight = waterheight;
4637         r_fb.water.texturewidth = waterwidth;
4638         r_fb.water.textureheight = waterheight;
4639         r_fb.water.camerawidth = waterwidth;
4640         r_fb.water.cameraheight = waterheight;
4641         r_fb.water.screenscale[0] = 0.5f;
4642         r_fb.water.screenscale[1] = 0.5f;
4643         r_fb.water.screencenter[0] = 0.5f;
4644         r_fb.water.screencenter[1] = 0.5f;
4645         r_fb.water.enabled = waterwidth != 0;
4646
4647         r_fb.water.maxwaterplanes = MAX_WATERPLANES;
4648         r_fb.water.numwaterplanes = 0;
4649 }
4650
4651 void R_Water_AddWaterPlane(msurface_t *surface, int entno)
4652 {
4653         int planeindex, bestplaneindex, vertexindex;
4654         vec3_t mins, maxs, normal, center, v, n;
4655         vec_t planescore, bestplanescore;
4656         mplane_t plane;
4657         r_waterstate_waterplane_t *p;
4658         texture_t *t = R_GetCurrentTexture(surface->texture);
4659
4660         rsurface.texture = t;
4661         RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS, 1, ((const msurface_t **)&surface));
4662         // if the model has no normals, it's probably off-screen and they were not generated, so don't add it anyway
4663         if (!rsurface.batchnormal3f || rsurface.batchnumvertices < 1)
4664                 return;
4665         // average the vertex normals, find the surface bounds (after deformvertexes)
4666         Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f, v);
4667         Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f, n);
4668         VectorCopy(n, normal);
4669         VectorCopy(v, mins);
4670         VectorCopy(v, maxs);
4671         for (vertexindex = 1;vertexindex < rsurface.batchnumvertices;vertexindex++)
4672         {
4673                 Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f + vertexindex*3, v);
4674                 Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f + vertexindex*3, n);
4675                 VectorAdd(normal, n, normal);
4676                 mins[0] = min(mins[0], v[0]);
4677                 mins[1] = min(mins[1], v[1]);
4678                 mins[2] = min(mins[2], v[2]);
4679                 maxs[0] = max(maxs[0], v[0]);
4680                 maxs[1] = max(maxs[1], v[1]);
4681                 maxs[2] = max(maxs[2], v[2]);
4682         }
4683         VectorNormalize(normal);
4684         VectorMAM(0.5f, mins, 0.5f, maxs, center);
4685
4686         VectorCopy(normal, plane.normal);
4687         VectorNormalize(plane.normal);
4688         plane.dist = DotProduct(center, plane.normal);
4689         PlaneClassify(&plane);
4690         if (PlaneDiff(r_refdef.view.origin, &plane) < 0)
4691         {
4692                 // skip backfaces (except if nocullface is set)
4693 //              if (!(t->currentmaterialflags & MATERIALFLAG_NOCULLFACE))
4694 //                      return;
4695                 VectorNegate(plane.normal, plane.normal);
4696                 plane.dist *= -1;
4697                 PlaneClassify(&plane);
4698         }
4699
4700
4701         // find a matching plane if there is one
4702         bestplaneindex = -1;
4703         bestplanescore = 1048576.0f;
4704         for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
4705         {
4706                 if(p->camera_entity == t->camera_entity)
4707                 {
4708                         planescore = 1.0f - DotProduct(plane.normal, p->plane.normal) + fabs(plane.dist - p->plane.dist) * 0.001f;
4709                         if (bestplaneindex < 0 || bestplanescore > planescore)
4710                         {
4711                                 bestplaneindex = planeindex;
4712                                 bestplanescore = planescore;
4713                         }
4714                 }
4715         }
4716         planeindex = bestplaneindex;
4717
4718         // if this surface does not fit any known plane rendered this frame, add one
4719         if (planeindex < 0 || bestplanescore > 0.001f)
4720         {
4721                 if (r_fb.water.numwaterplanes < r_fb.water.maxwaterplanes)
4722                 {
4723                         // store the new plane
4724                         planeindex = r_fb.water.numwaterplanes++;
4725                         p = r_fb.water.waterplanes + planeindex;
4726                         p->plane = plane;
4727                         // clear materialflags and pvs
4728                         p->materialflags = 0;
4729                         p->pvsvalid = false;
4730                         p->camera_entity = t->camera_entity;
4731                         VectorCopy(mins, p->mins);
4732                         VectorCopy(maxs, p->maxs);
4733                 }
4734                 else
4735                 {
4736                         // We're totally screwed.
4737                         return;
4738                 }
4739         }
4740         else
4741         {
4742                 // merge mins/maxs when we're adding this surface to the plane
4743                 p = r_fb.water.waterplanes + planeindex;
4744                 p->mins[0] = min(p->mins[0], mins[0]);
4745                 p->mins[1] = min(p->mins[1], mins[1]);
4746                 p->mins[2] = min(p->mins[2], mins[2]);
4747                 p->maxs[0] = max(p->maxs[0], maxs[0]);
4748                 p->maxs[1] = max(p->maxs[1], maxs[1]);
4749                 p->maxs[2] = max(p->maxs[2], maxs[2]);
4750         }
4751         // merge this surface's materialflags into the waterplane
4752         p->materialflags |= t->currentmaterialflags;
4753         if(!(p->materialflags & MATERIALFLAG_CAMERA))
4754         {
4755                 // merge this surface's PVS into the waterplane
4756                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS
4757                  && r_refdef.scene.worldmodel->brush.PointInLeaf && r_refdef.scene.worldmodel->brush.PointInLeaf(r_refdef.scene.worldmodel, center)->clusterindex >= 0)
4758                 {
4759                         r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, center, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
4760                         p->pvsvalid = true;
4761                 }
4762         }
4763 }
4764
4765 extern cvar_t r_drawparticles;
4766 extern cvar_t r_drawdecals;
4767
4768 static void R_Water_ProcessPlanes(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, int viewx, int viewy, int viewwidth, int viewheight)
4769 {
4770         int myscissor[4];
4771         r_refdef_view_t originalview;
4772         r_refdef_view_t myview;
4773         int planeindex, qualityreduction = 0, old_r_dynamic = 0, old_r_shadows = 0, old_r_worldrtlight = 0, old_r_dlight = 0, old_r_particles = 0, old_r_decals = 0;
4774         r_waterstate_waterplane_t *p;
4775         vec3_t visorigin;
4776         r_rendertarget_t *rt;
4777
4778         originalview = r_refdef.view;
4779
4780         // lowquality hack, temporarily shut down some cvars and restore afterwards
4781         qualityreduction = r_water_lowquality.integer;
4782         if (qualityreduction > 0)
4783         {
4784                 if (qualityreduction >= 1)
4785                 {
4786                         old_r_shadows = r_shadows.integer;
4787                         old_r_worldrtlight = r_shadow_realtime_world.integer;
4788                         old_r_dlight = r_shadow_realtime_dlight.integer;
4789                         Cvar_SetValueQuick(&r_shadows, 0);
4790                         Cvar_SetValueQuick(&r_shadow_realtime_world, 0);
4791                         Cvar_SetValueQuick(&r_shadow_realtime_dlight, 0);
4792                 }
4793                 if (qualityreduction >= 2)
4794                 {
4795                         old_r_dynamic = r_dynamic.integer;
4796                         old_r_particles = r_drawparticles.integer;
4797                         old_r_decals = r_drawdecals.integer;
4798                         Cvar_SetValueQuick(&r_dynamic, 0);
4799                         Cvar_SetValueQuick(&r_drawparticles, 0);
4800                         Cvar_SetValueQuick(&r_drawdecals, 0);
4801                 }
4802         }
4803
4804         for (planeindex = 0, p = r_fb.water.waterplanes; planeindex < r_fb.water.numwaterplanes; planeindex++, p++)
4805         {
4806                 p->rt_reflection = NULL;
4807                 p->rt_refraction = NULL;
4808                 p->rt_camera = NULL;
4809         }
4810
4811         // render views
4812         r_refdef.view = originalview;
4813         r_refdef.view.showdebug = false;
4814         r_refdef.view.width = r_fb.water.waterwidth;
4815         r_refdef.view.height = r_fb.water.waterheight;
4816         r_refdef.view.useclipplane = true;
4817         myview = r_refdef.view;
4818         r_fb.water.renderingscene = true;
4819         for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
4820         {
4821                 if (r_water_cameraentitiesonly.value != 0 && !p->camera_entity)
4822                         continue;
4823
4824                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
4825                 {
4826                         rt = R_RenderTarget_Get(r_fb.water.waterwidth, r_fb.water.waterheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, r_fb.rt_screen->colortextype[0], TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
4827                         if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4828                                 goto error;
4829                         r_refdef.view = myview;
4830                         Matrix4x4_Reflect(&r_refdef.view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
4831                         Matrix4x4_OriginFromMatrix(&r_refdef.view.matrix, r_refdef.view.origin);
4832                         if(r_water_scissormode.integer)
4833                         {
4834                                 R_SetupView(true, rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, r_fb.water.waterwidth, r_fb.water.waterheight);
4835                                 if (R_ScissorForBBox(p->mins, p->maxs, myscissor))
4836                                 {
4837                                         p->rt_reflection = NULL;
4838                                         p->rt_refraction = NULL;
4839                                         p->rt_camera = NULL;
4840                                         continue;
4841                                 }
4842                         }
4843
4844                         r_refdef.view.clipplane = p->plane;
4845                         // reflected view origin may be in solid, so don't cull with it
4846                         r_refdef.view.usevieworiginculling = false;
4847                         // reverse the cullface settings for this render
4848                         r_refdef.view.cullface_front = GL_FRONT;
4849                         r_refdef.view.cullface_back = GL_BACK;
4850                         // combined pvs (based on what can be seen from each surface center)
4851                         if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
4852                         {
4853                                 r_refdef.view.usecustompvs = true;
4854                                 if (p->pvsvalid)
4855                                         memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4856                                 else
4857                                         memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4858                         }
4859
4860                         r_fb.water.hideplayer = ((r_water_hideplayer.integer >= 2) && !chase_active.integer);
4861                         R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4862                         GL_ScissorTest(false);
4863                         R_ClearScreen(r_refdef.fogenabled);
4864                         GL_ScissorTest(true);
4865                         if(r_water_scissormode.integer & 2)
4866                                 R_View_UpdateWithScissor(myscissor);
4867                         else
4868                                 R_View_Update();
4869                         R_AnimCache_CacheVisibleEntities();
4870                         if(r_water_scissormode.integer & 1)
4871                                 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
4872                         R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4873
4874                         r_fb.water.hideplayer = false;
4875                         p->rt_reflection = rt;
4876                 }
4877
4878                 // render the normal view scene and copy into texture
4879                 // (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)
4880                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
4881                 {
4882                         rt = R_RenderTarget_Get(r_fb.water.waterwidth, r_fb.water.waterheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, r_fb.rt_screen->colortextype[0], TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
4883                         if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4884                                 goto error;
4885                         r_refdef.view = myview;
4886                         if(r_water_scissormode.integer)
4887                         {
4888                                 R_SetupView(true, rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, r_fb.water.waterwidth, r_fb.water.waterheight);
4889                                 if (R_ScissorForBBox(p->mins, p->maxs, myscissor))
4890                                 {
4891                                         p->rt_reflection = NULL;
4892                                         p->rt_refraction = NULL;
4893                                         p->rt_camera = NULL;
4894                                         continue;
4895                                 }
4896                         }
4897
4898                         // combined pvs (based on what can be seen from each surface center)
4899                         if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
4900                         {
4901                                 r_refdef.view.usecustompvs = true;
4902                                 if (p->pvsvalid)
4903                                         memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4904                                 else
4905                                         memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4906                         }
4907
4908                         r_fb.water.hideplayer = ((r_water_hideplayer.integer >= 1) && !chase_active.integer);
4909
4910                         r_refdef.view.clipplane = p->plane;
4911                         VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
4912                         r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
4913
4914                         if((p->materialflags & MATERIALFLAG_CAMERA) && p->camera_entity)
4915                         {
4916                                 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
4917                                 r_fb.water.hideplayer = false; // we don't want to hide the player model from these ones
4918                                 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
4919                                 R_RenderView_UpdateViewVectors();
4920                                 if(r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
4921                                 {
4922                                         r_refdef.view.usecustompvs = true;
4923                                         r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, visorigin, 2, r_refdef.viewcache.world_pvsbits, (r_refdef.viewcache.world_numclusters+7)>>3, false);
4924                                 }
4925                         }
4926
4927                         PlaneClassify(&r_refdef.view.clipplane);
4928
4929                         R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4930                         GL_ScissorTest(false);
4931                         R_ClearScreen(r_refdef.fogenabled);
4932                         GL_ScissorTest(true);
4933                         if(r_water_scissormode.integer & 2)
4934                                 R_View_UpdateWithScissor(myscissor);
4935                         else
4936                                 R_View_Update();
4937                         R_AnimCache_CacheVisibleEntities();
4938                         if(r_water_scissormode.integer & 1)
4939                                 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
4940                         R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4941
4942                         r_fb.water.hideplayer = false;
4943                         p->rt_refraction = rt;
4944                 }
4945                 else if (p->materialflags & MATERIALFLAG_CAMERA)
4946                 {
4947                         rt = R_RenderTarget_Get(r_fb.water.waterwidth, r_fb.water.waterheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, r_fb.rt_screen->colortextype[0], TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
4948                         if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4949                                 goto error;
4950                         r_refdef.view = myview;
4951
4952                         r_refdef.view.clipplane = p->plane;
4953                         VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
4954                         r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
4955
4956                         r_refdef.view.width = r_fb.water.camerawidth;
4957                         r_refdef.view.height = r_fb.water.cameraheight;
4958                         r_refdef.view.frustum_x = 1; // tan(45 * M_PI / 180.0);
4959                         r_refdef.view.frustum_y = 1; // tan(45 * M_PI / 180.0);
4960                         r_refdef.view.ortho_x = 90; // abused as angle by VM_CL_R_SetView
4961                         r_refdef.view.ortho_y = 90; // abused as angle by VM_CL_R_SetView
4962
4963                         if(p->camera_entity)
4964                         {
4965                                 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
4966                                 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
4967                         }
4968
4969                         // note: all of the view is used for displaying... so
4970                         // there is no use in scissoring
4971
4972                         // reverse the cullface settings for this render
4973                         r_refdef.view.cullface_front = GL_FRONT;
4974                         r_refdef.view.cullface_back = GL_BACK;
4975                         // also reverse the view matrix
4976                         Matrix4x4_ConcatScale3(&r_refdef.view.matrix, 1, 1, -1); // this serves to invert texcoords in the result, as the copied texture is mapped the wrong way round
4977                         R_RenderView_UpdateViewVectors();
4978                         if(p->camera_entity && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
4979                         {
4980                                 r_refdef.view.usecustompvs = true;
4981                                 r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, visorigin, 2, r_refdef.viewcache.world_pvsbits, (r_refdef.viewcache.world_numclusters+7)>>3, false);
4982                         }
4983                         
4984                         // camera needs no clipplane
4985                         r_refdef.view.useclipplane = false;
4986                         // TODO: is the camera origin always valid?  if so we don't need to clear this
4987                         r_refdef.view.usevieworiginculling = false;
4988
4989                         PlaneClassify(&r_refdef.view.clipplane);
4990
4991                         r_fb.water.hideplayer = false;
4992
4993                         R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4994                         GL_ScissorTest(false);
4995                         R_ClearScreen(r_refdef.fogenabled);
4996                         GL_ScissorTest(true);
4997                         R_View_Update();
4998                         R_AnimCache_CacheVisibleEntities();
4999                         R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
5000
5001                         r_fb.water.hideplayer = false;
5002                         p->rt_camera = rt;
5003                 }
5004
5005         }
5006         r_fb.water.renderingscene = false;
5007         r_refdef.view = originalview;
5008         R_ResetViewRendering3D(fbo, depthtexture, colortexture, viewx, viewy, viewwidth, viewheight);
5009         R_View_Update();
5010         R_AnimCache_CacheVisibleEntities();
5011         goto finish;
5012 error:
5013         r_refdef.view = originalview;
5014         r_fb.water.renderingscene = false;
5015         Cvar_SetValueQuick(&r_water, 0);
5016         Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed!  Turned off r_water.\n");
5017 finish:
5018         // lowquality hack, restore cvars
5019         if (qualityreduction > 0)
5020         {
5021                 if (qualityreduction >= 1)
5022                 {
5023                         Cvar_SetValueQuick(&r_shadows, old_r_shadows);
5024                         Cvar_SetValueQuick(&r_shadow_realtime_world, old_r_worldrtlight);
5025                         Cvar_SetValueQuick(&r_shadow_realtime_dlight, old_r_dlight);
5026                 }
5027                 if (qualityreduction >= 2)
5028                 {
5029                         Cvar_SetValueQuick(&r_dynamic, old_r_dynamic);
5030                         Cvar_SetValueQuick(&r_drawparticles, old_r_particles);
5031                         Cvar_SetValueQuick(&r_drawdecals, old_r_decals);
5032                 }
5033         }
5034 }
5035
5036 static void R_Bloom_StartFrame(void)
5037 {
5038         int screentexturewidth, screentextureheight;
5039         textype_t textype = TEXTYPE_COLORBUFFER;
5040         double scale;
5041
5042         // clear the pointers to rendertargets from last frame as they're stale
5043         r_fb.rt_screen = NULL;
5044         r_fb.rt_bloom = NULL;
5045
5046         switch (vid.renderpath)
5047         {
5048         case RENDERPATH_GL32:
5049                 r_fb.usedepthtextures = r_usedepthtextures.integer != 0;
5050                 if (r_viewfbo.integer == 2) textype = TEXTYPE_COLORBUFFER16F;
5051                 if (r_viewfbo.integer == 3) textype = TEXTYPE_COLORBUFFER32F;
5052                 break;
5053         case RENDERPATH_GLES2:
5054                 r_fb.usedepthtextures = false;
5055                 break;
5056         }
5057
5058         if (r_viewscale_fpsscaling.integer)
5059         {
5060                 double actualframetime;
5061                 double targetframetime;
5062                 double adjust;
5063                 actualframetime = r_refdef.lastdrawscreentime;
5064                 targetframetime = (1.0 / r_viewscale_fpsscaling_target.value);
5065                 adjust = (targetframetime - actualframetime) * r_viewscale_fpsscaling_multiply.value;
5066                 adjust = bound(-r_viewscale_fpsscaling_stepmax.value, adjust, r_viewscale_fpsscaling_stepmax.value);
5067                 if (r_viewscale_fpsscaling_stepsize.value > 0)
5068                 {
5069                         if (adjust > 0)
5070                                 adjust = floor(adjust / r_viewscale_fpsscaling_stepsize.value) * r_viewscale_fpsscaling_stepsize.value;
5071                         else
5072                                 adjust = ceil(adjust / r_viewscale_fpsscaling_stepsize.value) * r_viewscale_fpsscaling_stepsize.value;
5073                 }
5074                 viewscalefpsadjusted += adjust;
5075                 viewscalefpsadjusted = bound(r_viewscale_fpsscaling_min.value, viewscalefpsadjusted, 1.0f);
5076         }
5077         else
5078                 viewscalefpsadjusted = 1.0f;
5079
5080         scale = r_viewscale.value * sqrt(viewscalefpsadjusted);
5081         if (vid.samples)
5082                 scale *= sqrt(vid.samples); // supersampling
5083         scale = bound(0.03125f, scale, 4.0f);
5084         screentexturewidth = (int)ceil(r_refdef.view.width * scale);
5085         screentextureheight = (int)ceil(r_refdef.view.height * scale);
5086         screentexturewidth = bound(1, screentexturewidth, (int)vid.maxtexturesize_2d);
5087         screentextureheight = bound(1, screentextureheight, (int)vid.maxtexturesize_2d);
5088
5089         // set bloomwidth and bloomheight to the bloom resolution that will be
5090         // used (often less than the screen resolution for faster rendering)
5091         r_fb.bloomheight = bound(1, r_bloom_resolution.value * 0.75f, screentextureheight);
5092         r_fb.bloomwidth = r_fb.bloomheight * screentexturewidth / screentextureheight;
5093         r_fb.bloomwidth = bound(1, r_fb.bloomwidth, screentexturewidth);
5094         r_fb.bloomwidth = bound(1, r_fb.bloomwidth, (int)vid.maxtexturesize_2d);
5095         r_fb.bloomheight = bound(1, r_fb.bloomheight, (int)vid.maxtexturesize_2d);
5096
5097         if ((r_bloom.integer || (!R_Stereo_Active() && (r_motionblur.value > 0 || r_damageblur.value > 0))) && ((r_bloom_resolution.integer < 4 || r_bloom_blur.value < 1 || r_bloom_blur.value >= 512) || r_refdef.view.width > (int)vid.maxtexturesize_2d || r_refdef.view.height > (int)vid.maxtexturesize_2d))
5098         {
5099                 Cvar_SetValueQuick(&r_bloom, 0);
5100                 Cvar_SetValueQuick(&r_motionblur, 0);
5101                 Cvar_SetValueQuick(&r_damageblur, 0);
5102         }
5103         if (!r_bloom.integer)
5104                 r_fb.bloomwidth = r_fb.bloomheight = 0;
5105
5106         // allocate motionblur ghost texture if needed - this is the only persistent texture and is only useful on the main view
5107         if (r_refdef.view.ismain && (r_fb.screentexturewidth != screentexturewidth || r_fb.screentextureheight != screentextureheight || r_fb.textype != textype))
5108         {
5109                 if (r_fb.ghosttexture)
5110                         R_FreeTexture(r_fb.ghosttexture);
5111                 r_fb.ghosttexture = NULL;
5112
5113                 r_fb.screentexturewidth = screentexturewidth;
5114                 r_fb.screentextureheight = screentextureheight;
5115                 r_fb.textype = textype;
5116
5117                 if (r_fb.screentexturewidth && r_fb.screentextureheight)
5118                 {
5119                         if (r_motionblur.value > 0 || r_damageblur.value > 0)
5120                                 r_fb.ghosttexture = R_LoadTexture2D(r_main_texturepool, "framebuffermotionblur", r_fb.screentexturewidth, r_fb.screentextureheight, NULL, r_fb.textype, TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
5121                         r_fb.ghosttexture_valid = false;
5122                 }
5123         }
5124
5125         r_fb.rt_screen = R_RenderTarget_Get(screentexturewidth, screentextureheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5126
5127         r_refdef.view.clear = true;
5128 }
5129
5130 static void R_Bloom_MakeTexture(void)
5131 {
5132         int x, range, dir;
5133         float xoffset, yoffset, r, brighten;
5134         float colorscale = r_bloom_colorscale.value;
5135         r_viewport_t bloomviewport;
5136         r_rendertarget_t *prev, *cur;
5137         textype_t textype = r_fb.rt_screen->colortextype[0];
5138
5139         r_refdef.stats[r_stat_bloom]++;
5140
5141         R_Viewport_InitOrtho(&bloomviewport, &identitymatrix, 0, 0, r_fb.bloomwidth, r_fb.bloomheight, 0, 0, 1, 1, -10, 100, NULL);
5142
5143         // scale down screen texture to the bloom texture size
5144         CHECKGLERROR
5145         prev = r_fb.rt_screen;
5146         cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5147         R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5148         R_SetViewport(&bloomviewport);
5149         GL_CullFace(GL_NONE);
5150         GL_DepthTest(false);
5151         GL_BlendFunc(GL_ONE, GL_ZERO);
5152         GL_Color(colorscale, colorscale, colorscale, 1);
5153         R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, prev->texcoord2f);
5154         // TODO: do boxfilter scale-down in shader?
5155         R_SetupShader_Generic(prev->colortexture[0], false, true, true);
5156         R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5157         r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5158         // we now have a properly scaled bloom image
5159
5160         // multiply bloom image by itself as many times as desired to darken it
5161         // TODO: if people actually use this it could be done more quickly in the previous shader pass
5162         for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
5163         {
5164                 prev = cur;
5165                 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5166                 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5167                 x *= 2;
5168                 r = bound(0, r_bloom_colorexponent.value / x, 1); // always 0.5 to 1
5169                 if(x <= 2)
5170                         GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 0);
5171                 GL_BlendFunc(GL_SRC_COLOR, GL_ZERO); // square it
5172                 GL_Color(1,1,1,1); // no fix factor supported here
5173                 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, prev->texcoord2f);
5174                 R_SetupShader_Generic(prev->colortexture[0], false, true, false);
5175                 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5176                 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5177         }
5178         CHECKGLERROR
5179
5180         range = r_bloom_blur.integer * r_fb.bloomwidth / 320;
5181         brighten = r_bloom_brighten.value;
5182         brighten = sqrt(brighten);
5183         if(range >= 1)
5184                 brighten *= (3 * range) / (2 * range - 1); // compensate for the "dot particle"
5185
5186         for (dir = 0;dir < 2;dir++)
5187         {
5188                 prev = cur;
5189                 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5190                 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5191                 // blend on at multiple vertical offsets to achieve a vertical blur
5192                 // TODO: do offset blends using GLSL
5193                 // TODO instead of changing the texcoords, change the target positions to prevent artifacts at edges
5194                 CHECKGLERROR
5195                 GL_BlendFunc(GL_ONE, GL_ZERO);
5196                 CHECKGLERROR
5197                 R_SetupShader_Generic(prev->colortexture[0], false, true, false);
5198                 CHECKGLERROR
5199                 for (x = -range;x <= range;x++)
5200                 {
5201                         if (!dir){xoffset = 0;yoffset = x;}
5202                         else {xoffset = x;yoffset = 0;}
5203                         xoffset /= (float)prev->texturewidth;
5204                         yoffset /= (float)prev->textureheight;
5205                         // compute a texcoord array with the specified x and y offset
5206                         r_fb.offsettexcoord2f[0] = xoffset+prev->texcoord2f[0];
5207                         r_fb.offsettexcoord2f[1] = yoffset+prev->texcoord2f[1];
5208                         r_fb.offsettexcoord2f[2] = xoffset+prev->texcoord2f[2];
5209                         r_fb.offsettexcoord2f[3] = yoffset+prev->texcoord2f[3];
5210                         r_fb.offsettexcoord2f[4] = xoffset+prev->texcoord2f[4];
5211                         r_fb.offsettexcoord2f[5] = yoffset+prev->texcoord2f[5];
5212                         r_fb.offsettexcoord2f[6] = xoffset+prev->texcoord2f[6];
5213                         r_fb.offsettexcoord2f[7] = yoffset+prev->texcoord2f[7];
5214                         // this r value looks like a 'dot' particle, fading sharply to
5215                         // black at the edges
5216                         // (probably not realistic but looks good enough)
5217                         //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
5218                         //r = brighten/(range*2+1);
5219                         r = brighten / (range * 2 + 1);
5220                         if(range >= 1)
5221                                 r *= (1 - x*x/(float)((range+1)*(range+1)));
5222                         if (r <= 0)
5223                                 continue;
5224                         CHECKGLERROR
5225                         GL_Color(r, r, r, 1);
5226                         CHECKGLERROR
5227                         R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.offsettexcoord2f);
5228                         CHECKGLERROR
5229                         R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5230                         r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5231                         CHECKGLERROR
5232                         GL_BlendFunc(GL_ONE, GL_ONE);
5233                         CHECKGLERROR
5234                 }
5235         }
5236
5237         // now we have the bloom image, so keep track of it
5238         r_fb.rt_bloom = cur;
5239 }
5240
5241 static void R_BlendView(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5242 {
5243         uint64_t permutation;
5244         float uservecs[4][4];
5245         rtexture_t *viewtexture;
5246         rtexture_t *bloomtexture;
5247
5248         R_EntityMatrix(&identitymatrix);
5249
5250         if(r_refdef.view.ismain && !R_Stereo_Active() && (r_motionblur.value > 0 || r_damageblur.value > 0) && r_fb.ghosttexture)
5251         {
5252                 // declare variables
5253                 float blur_factor, blur_mouseaccel, blur_velocity;
5254                 static float blur_average; 
5255                 static vec3_t blur_oldangles; // used to see how quickly the mouse is moving
5256
5257                 // set a goal for the factoring
5258                 blur_velocity = bound(0, (VectorLength(cl.movement_velocity) - r_motionblur_velocityfactor_minspeed.value) 
5259                         / max(1, r_motionblur_velocityfactor_maxspeed.value - r_motionblur_velocityfactor_minspeed.value), 1);
5260                 blur_mouseaccel = bound(0, ((fabs(VectorLength(cl.viewangles) - VectorLength(blur_oldangles)) * 10) - r_motionblur_mousefactor_minspeed.value) 
5261                         / max(1, r_motionblur_mousefactor_maxspeed.value - r_motionblur_mousefactor_minspeed.value), 1);
5262                 blur_factor = ((blur_velocity * r_motionblur_velocityfactor.value) 
5263                         + (blur_mouseaccel * r_motionblur_mousefactor.value));
5264
5265                 // from the goal, pick an averaged value between goal and last value
5266                 cl.motionbluralpha = bound(0, (cl.time - cl.oldtime) / max(0.001, r_motionblur_averaging.value), 1);
5267                 blur_average = blur_average * (1 - cl.motionbluralpha) + blur_factor * cl.motionbluralpha;
5268
5269                 // enforce minimum amount of blur 
5270                 blur_factor = blur_average * (1 - r_motionblur_minblur.value) + r_motionblur_minblur.value;
5271
5272                 //Con_Printf("motionblur: direct factor: %f, averaged factor: %f, velocity: %f, mouse accel: %f \n", blur_factor, blur_average, blur_velocity, blur_mouseaccel);
5273
5274                 // calculate values into a standard alpha
5275                 cl.motionbluralpha = 1 - exp(-
5276                                 (
5277                                         (r_motionblur.value * blur_factor / 80)
5278                                         +
5279                                         (r_damageblur.value * (cl.cshifts[CSHIFT_DAMAGE].percent / 1600))
5280                                 )
5281                                 /
5282                                 max(0.0001, cl.time - cl.oldtime) // fps independent
5283                                 );
5284
5285                 // randomization for the blur value to combat persistent ghosting
5286                 cl.motionbluralpha *= lhrandom(1 - r_motionblur_randomize.value, 1 + r_motionblur_randomize.value);
5287                 cl.motionbluralpha = bound(0, cl.motionbluralpha, r_motionblur_maxblur.value);
5288
5289                 // apply the blur
5290                 R_ResetViewRendering2D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5291                 if (cl.motionbluralpha > 0 && !r_refdef.envmap && r_fb.ghosttexture_valid)
5292                 {
5293                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5294                         GL_Color(1, 1, 1, cl.motionbluralpha);
5295                         R_CalcTexCoordsForView(0, 0, viewwidth, viewheight, viewwidth, viewheight, r_fb.ghosttexcoord2f);
5296                         R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.ghosttexcoord2f);
5297                         R_SetupShader_Generic(r_fb.ghosttexture, false, true, true);
5298                         R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5299                         r_refdef.stats[r_stat_bloom_drawpixels] += viewwidth * viewheight;
5300                 }
5301
5302                 // updates old view angles for next pass
5303                 VectorCopy(cl.viewangles, blur_oldangles);
5304
5305                 // copy view into the ghost texture
5306                 R_Mesh_CopyToTexture(r_fb.ghosttexture, 0, 0, viewx, viewy, viewwidth, viewheight);
5307                 r_refdef.stats[r_stat_bloom_copypixels] += viewwidth * viewheight;
5308                 r_fb.ghosttexture_valid = true;
5309         }
5310
5311         if (r_fb.bloomwidth)
5312         {
5313                 // make the bloom texture
5314                 R_Bloom_MakeTexture();
5315         }
5316
5317 #if _MSC_VER >= 1400
5318 #define sscanf sscanf_s
5319 #endif
5320         memset(uservecs, 0, sizeof(uservecs));
5321         if (r_glsl_postprocess_uservec1_enable.integer)
5322                 sscanf(r_glsl_postprocess_uservec1.string, "%f %f %f %f", &uservecs[0][0], &uservecs[0][1], &uservecs[0][2], &uservecs[0][3]);
5323         if (r_glsl_postprocess_uservec2_enable.integer)
5324                 sscanf(r_glsl_postprocess_uservec2.string, "%f %f %f %f", &uservecs[1][0], &uservecs[1][1], &uservecs[1][2], &uservecs[1][3]);
5325         if (r_glsl_postprocess_uservec3_enable.integer)
5326                 sscanf(r_glsl_postprocess_uservec3.string, "%f %f %f %f", &uservecs[2][0], &uservecs[2][1], &uservecs[2][2], &uservecs[2][3]);
5327         if (r_glsl_postprocess_uservec4_enable.integer)
5328                 sscanf(r_glsl_postprocess_uservec4.string, "%f %f %f %f", &uservecs[3][0], &uservecs[3][1], &uservecs[3][2], &uservecs[3][3]);
5329
5330         // render to the screen fbo
5331         R_ResetViewRendering2D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5332         GL_Color(1, 1, 1, 1);
5333         GL_BlendFunc(GL_ONE, GL_ZERO);
5334
5335         viewtexture = r_fb.rt_screen->colortexture[0];
5336         bloomtexture = r_fb.rt_bloom ? r_fb.rt_bloom->colortexture[0] : NULL;
5337
5338         if (r_rendertarget_debug.integer >= 0)
5339         {
5340                 r_rendertarget_t *rt = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, r_rendertarget_debug.integer);
5341                 if (rt && rt->colortexture[0])
5342                 {
5343                         viewtexture = rt->colortexture[0];
5344                         bloomtexture = NULL;
5345                 }
5346         }
5347
5348         R_Mesh_PrepareVertices_Mesh_Arrays(4, r_screenvertex3f, NULL, NULL, NULL, NULL, r_fb.rt_screen->texcoord2f, bloomtexture ? r_fb.rt_bloom->texcoord2f : NULL);
5349         switch(vid.renderpath)
5350         {
5351         case RENDERPATH_GL32:
5352         case RENDERPATH_GLES2:
5353                 permutation =
5354                         (r_fb.bloomwidth ? SHADERPERMUTATION_BLOOM : 0)
5355                         | (r_refdef.viewblend[3] > 0 ? SHADERPERMUTATION_VIEWTINT : 0)
5356                         | (!vid_gammatables_trivial ? SHADERPERMUTATION_GAMMARAMPS : 0)
5357                         | (r_glsl_postprocess.integer ? SHADERPERMUTATION_POSTPROCESSING : 0)
5358                         | ((!R_Stereo_ColorMasking() && r_glsl_saturation.value != 1) ? SHADERPERMUTATION_SATURATION : 0);
5359                 R_SetupShader_SetPermutationGLSL(SHADERMODE_POSTPROCESS, permutation);
5360                 if (r_glsl_permutation->tex_Texture_First           >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First     , viewtexture);
5361                 if (r_glsl_permutation->tex_Texture_Second          >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second    , bloomtexture);
5362                 if (r_glsl_permutation->tex_Texture_GammaRamps      >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps       );
5363                 if (r_glsl_permutation->loc_ViewTintColor           >= 0) qglUniform4f(r_glsl_permutation->loc_ViewTintColor     , r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
5364                 if (r_glsl_permutation->loc_PixelSize               >= 0) qglUniform2f(r_glsl_permutation->loc_PixelSize         , 1.0/r_fb.screentexturewidth, 1.0/r_fb.screentextureheight);
5365                 if (r_glsl_permutation->loc_UserVec1                >= 0) qglUniform4f(r_glsl_permutation->loc_UserVec1          , uservecs[0][0], uservecs[0][1], uservecs[0][2], uservecs[0][3]);
5366                 if (r_glsl_permutation->loc_UserVec2                >= 0) qglUniform4f(r_glsl_permutation->loc_UserVec2          , uservecs[1][0], uservecs[1][1], uservecs[1][2], uservecs[1][3]);
5367                 if (r_glsl_permutation->loc_UserVec3                >= 0) qglUniform4f(r_glsl_permutation->loc_UserVec3          , uservecs[2][0], uservecs[2][1], uservecs[2][2], uservecs[2][3]);
5368                 if (r_glsl_permutation->loc_UserVec4                >= 0) qglUniform4f(r_glsl_permutation->loc_UserVec4          , uservecs[3][0], uservecs[3][1], uservecs[3][2], uservecs[3][3]);
5369                 if (r_glsl_permutation->loc_Saturation              >= 0) qglUniform1f(r_glsl_permutation->loc_Saturation        , r_glsl_saturation.value);
5370                 if (r_glsl_permutation->loc_PixelToScreenTexCoord   >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
5371                 if (r_glsl_permutation->loc_BloomColorSubtract      >= 0) qglUniform4f(r_glsl_permutation->loc_BloomColorSubtract   , r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 0.0f);
5372                 if (r_glsl_permutation->loc_ColorFringe             >= 0) qglUniform1f(r_glsl_permutation->loc_ColorFringe, r_colorfringe.value );
5373                 break;
5374         }
5375         R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5376         r_refdef.stats[r_stat_bloom_drawpixels] += r_refdef.view.width * r_refdef.view.height;
5377 }
5378
5379 matrix4x4_t r_waterscrollmatrix;
5380
5381 void R_UpdateFog(void)
5382 {
5383         // Nehahra fog
5384         if (gamemode == GAME_NEHAHRA)
5385         {
5386                 if (gl_fogenable.integer)
5387                 {
5388                         r_refdef.oldgl_fogenable = true;
5389                         r_refdef.fog_density = gl_fogdensity.value;
5390                         r_refdef.fog_red = gl_fogred.value;
5391                         r_refdef.fog_green = gl_foggreen.value;
5392                         r_refdef.fog_blue = gl_fogblue.value;
5393                         r_refdef.fog_alpha = 1;
5394                         r_refdef.fog_start = 0;
5395                         r_refdef.fog_end = gl_skyclip.value;
5396                         r_refdef.fog_height = 1<<30;
5397                         r_refdef.fog_fadedepth = 128;
5398                 }
5399                 else if (r_refdef.oldgl_fogenable)
5400                 {
5401                         r_refdef.oldgl_fogenable = false;
5402                         r_refdef.fog_density = 0;
5403                         r_refdef.fog_red = 0;
5404                         r_refdef.fog_green = 0;
5405                         r_refdef.fog_blue = 0;
5406                         r_refdef.fog_alpha = 0;
5407                         r_refdef.fog_start = 0;
5408                         r_refdef.fog_end = 0;
5409                         r_refdef.fog_height = 1<<30;
5410                         r_refdef.fog_fadedepth = 128;
5411                 }
5412         }
5413
5414         // fog parms
5415         r_refdef.fog_alpha = bound(0, r_refdef.fog_alpha, 1);
5416         r_refdef.fog_start = max(0, r_refdef.fog_start);
5417         r_refdef.fog_end = max(r_refdef.fog_start + 0.01, r_refdef.fog_end);
5418
5419         if (r_refdef.fog_density && r_drawfog.integer)
5420         {
5421                 r_refdef.fogenabled = true;
5422                 // this is the point where the fog reaches 0.9986 alpha, which we
5423                 // consider a good enough cutoff point for the texture
5424                 // (0.9986 * 256 == 255.6)
5425                 if (r_fog_exp2.integer)
5426                         r_refdef.fogrange = 32 / (r_refdef.fog_density * r_refdef.fog_density) + r_refdef.fog_start;
5427                 else
5428                         r_refdef.fogrange = 2048 / r_refdef.fog_density + r_refdef.fog_start;
5429                 r_refdef.fogrange = bound(r_refdef.fog_start, r_refdef.fogrange, r_refdef.fog_end);
5430                 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
5431                 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
5432                 if (strcmp(r_refdef.fogheighttexturename, r_refdef.fog_height_texturename))
5433                         R_BuildFogHeightTexture();
5434                 // fog color was already set
5435                 // update the fog texture
5436                 if (r_refdef.fogmasktable_start != r_refdef.fog_start || r_refdef.fogmasktable_alpha != r_refdef.fog_alpha || r_refdef.fogmasktable_density != r_refdef.fog_density || r_refdef.fogmasktable_range != r_refdef.fogrange)
5437                         R_BuildFogTexture();
5438                 r_refdef.fog_height_texcoordscale = 1.0f / max(0.125f, r_refdef.fog_fadedepth);
5439                 r_refdef.fog_height_tablescale = r_refdef.fog_height_tablesize * r_refdef.fog_height_texcoordscale;
5440         }
5441         else
5442                 r_refdef.fogenabled = false;
5443
5444         // fog color
5445         if (r_refdef.fog_density)
5446         {
5447                 r_refdef.fogcolor[0] = r_refdef.fog_red;
5448                 r_refdef.fogcolor[1] = r_refdef.fog_green;
5449                 r_refdef.fogcolor[2] = r_refdef.fog_blue;
5450
5451                 Vector4Set(r_refdef.fogplane, 0, 0, 1, -r_refdef.fog_height);
5452                 r_refdef.fogplaneviewdist = DotProduct(r_refdef.fogplane, r_refdef.view.origin) + r_refdef.fogplane[3];
5453                 r_refdef.fogplaneviewabove = r_refdef.fogplaneviewdist >= 0;
5454                 r_refdef.fogheightfade = -0.5f/max(0.125f, r_refdef.fog_fadedepth);
5455
5456                 {
5457                         vec3_t fogvec;
5458                         VectorCopy(r_refdef.fogcolor, fogvec);
5459                         //   color.rgb *= ContrastBoost * SceneBrightness;
5460                         VectorScale(fogvec, r_refdef.view.colorscale, fogvec);
5461                         r_refdef.fogcolor[0] = bound(0.0f, fogvec[0], 1.0f);
5462                         r_refdef.fogcolor[1] = bound(0.0f, fogvec[1], 1.0f);
5463                         r_refdef.fogcolor[2] = bound(0.0f, fogvec[2], 1.0f);
5464                 }
5465         }
5466 }
5467
5468 void R_UpdateVariables(void)
5469 {
5470         R_Textures_Frame();
5471
5472         r_refdef.scene.ambientintensity = r_ambient.value * (1.0f / 64.0f);
5473
5474         r_refdef.farclip = r_farclip_base.value;
5475         if (r_refdef.scene.worldmodel)
5476                 r_refdef.farclip += r_refdef.scene.worldmodel->radius * r_farclip_world.value * 2;
5477         r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
5478
5479         if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
5480                 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
5481         r_refdef.polygonfactor = 0;
5482         r_refdef.polygonoffset = 0;
5483
5484         r_refdef.scene.rtworld = r_shadow_realtime_world.integer != 0;
5485         r_refdef.scene.rtworldshadows = r_shadow_realtime_world_shadows.integer && vid.stencil;
5486         r_refdef.scene.rtdlight = r_shadow_realtime_dlight.integer != 0 && !gl_flashblend.integer && r_dynamic.integer;
5487         r_refdef.scene.rtdlightshadows = r_refdef.scene.rtdlight && r_shadow_realtime_dlight_shadows.integer && vid.stencil;
5488         r_refdef.scene.lightmapintensity = r_refdef.scene.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
5489         if (r_refdef.scene.worldmodel)
5490         {
5491                 r_refdef.scene.lightmapintensity *= r_refdef.scene.worldmodel->lightmapscale;
5492         }
5493         if (r_showsurfaces.integer)
5494         {
5495                 r_refdef.scene.rtworld = false;
5496                 r_refdef.scene.rtworldshadows = false;
5497                 r_refdef.scene.rtdlight = false;
5498                 r_refdef.scene.rtdlightshadows = false;
5499                 r_refdef.scene.lightmapintensity = 0;
5500         }
5501
5502         r_gpuskeletal = false;
5503         switch(vid.renderpath)
5504         {
5505         case RENDERPATH_GL32:
5506                 r_gpuskeletal = r_glsl_skeletal.integer && !r_showsurfaces.integer;
5507         case RENDERPATH_GLES2:
5508                 if(!vid_gammatables_trivial)
5509                 {
5510                         if(!r_texture_gammaramps || vid_gammatables_serial != r_texture_gammaramps_serial)
5511                         {
5512                                 // build GLSL gamma texture
5513 #define RAMPWIDTH 256
5514                                 unsigned short ramp[RAMPWIDTH * 3];
5515                                 unsigned char rampbgr[RAMPWIDTH][4];
5516                                 int i;
5517
5518                                 r_texture_gammaramps_serial = vid_gammatables_serial;
5519
5520                                 VID_BuildGammaTables(&ramp[0], RAMPWIDTH);
5521                                 for(i = 0; i < RAMPWIDTH; ++i)
5522                                 {
5523                                         rampbgr[i][0] = (unsigned char) (ramp[i + 2 * RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
5524                                         rampbgr[i][1] = (unsigned char) (ramp[i + RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
5525                                         rampbgr[i][2] = (unsigned char) (ramp[i] * 255.0 / 65535.0 + 0.5);
5526                                         rampbgr[i][3] = 0;
5527                                 }
5528                                 if (r_texture_gammaramps)
5529                                 {
5530                                         R_UpdateTexture(r_texture_gammaramps, &rampbgr[0][0], 0, 0, 0, RAMPWIDTH, 1, 1, 0);
5531                                 }
5532                                 else
5533                                 {
5534                                         r_texture_gammaramps = R_LoadTexture2D(r_main_texturepool, "gammaramps", RAMPWIDTH, 1, &rampbgr[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
5535                                 }
5536                         }
5537                 }
5538                 else
5539                 {
5540                         // remove GLSL gamma texture
5541                 }
5542                 break;
5543         }
5544 }
5545
5546 static r_refdef_scene_type_t r_currentscenetype = RST_CLIENT;
5547 static r_refdef_scene_t r_scenes_store[ RST_COUNT ];
5548 /*
5549 ================
5550 R_SelectScene
5551 ================
5552 */
5553 void R_SelectScene( r_refdef_scene_type_t scenetype ) {
5554         if( scenetype != r_currentscenetype ) {
5555                 // store the old scenetype
5556                 r_scenes_store[ r_currentscenetype ] = r_refdef.scene;
5557                 r_currentscenetype = scenetype;
5558                 // move in the new scene
5559                 r_refdef.scene = r_scenes_store[ r_currentscenetype ];
5560         }
5561 }
5562
5563 /*
5564 ================
5565 R_GetScenePointer
5566 ================
5567 */
5568 r_refdef_scene_t * R_GetScenePointer( r_refdef_scene_type_t scenetype )
5569 {
5570         // of course, we could also add a qbool that provides a lock state and a ReleaseScenePointer function..
5571         if( scenetype == r_currentscenetype ) {
5572                 return &r_refdef.scene;
5573         } else {
5574                 return &r_scenes_store[ scenetype ];
5575         }
5576 }
5577
5578 static int R_SortEntities_Compare(const void *ap, const void *bp)
5579 {
5580         const entity_render_t *a = *(const entity_render_t **)ap;
5581         const entity_render_t *b = *(const entity_render_t **)bp;
5582
5583         // 1. compare model
5584         if(a->model < b->model)
5585                 return -1;
5586         if(a->model > b->model)
5587                 return +1;
5588
5589         // 2. compare skin
5590         // TODO possibly calculate the REAL skinnum here first using
5591         // skinscenes?
5592         if(a->skinnum < b->skinnum)
5593                 return -1;
5594         if(a->skinnum > b->skinnum)
5595                 return +1;
5596
5597         // everything we compared is equal
5598         return 0;
5599 }
5600 static void R_SortEntities(void)
5601 {
5602         // below or equal 2 ents, sorting never gains anything
5603         if(r_refdef.scene.numentities <= 2)
5604                 return;
5605         // sort
5606         qsort(r_refdef.scene.entities, r_refdef.scene.numentities, sizeof(*r_refdef.scene.entities), R_SortEntities_Compare);
5607 }
5608
5609 /*
5610 ================
5611 R_RenderView
5612 ================
5613 */
5614 extern cvar_t r_shadow_bouncegrid;
5615 extern cvar_t v_isometric;
5616 extern void V_MakeViewIsometric(void);
5617 void R_RenderView(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, int x, int y, int width, int height)
5618 {
5619         matrix4x4_t originalmatrix = r_refdef.view.matrix, offsetmatrix;
5620         int viewfbo = 0;
5621         rtexture_t *viewdepthtexture = NULL;
5622         rtexture_t *viewcolortexture = NULL;
5623         int viewx = r_refdef.view.x, viewy = r_refdef.view.y, viewwidth = r_refdef.view.width, viewheight = r_refdef.view.height;
5624
5625         // finish any 2D rendering that was queued
5626         DrawQ_Finish();
5627
5628         if (r_timereport_active)
5629                 R_TimeReport("start");
5630         r_textureframe++; // used only by R_GetCurrentTexture
5631         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
5632
5633         if(R_CompileShader_CheckStaticParms())
5634                 R_GLSL_Restart_f(cmd_local);
5635
5636         if (!r_drawentities.integer)
5637                 r_refdef.scene.numentities = 0;
5638         else if (r_sortentities.integer)
5639                 R_SortEntities();
5640
5641         R_AnimCache_ClearCache();
5642
5643         /* adjust for stereo display */
5644         if(R_Stereo_Active())
5645         {
5646                 Matrix4x4_CreateFromQuakeEntity(&offsetmatrix, 0, r_stereo_separation.value * (0.5f - r_stereo_side), 0, 0, r_stereo_angle.value * (0.5f - r_stereo_side), 0, 1);
5647                 Matrix4x4_Concat(&r_refdef.view.matrix, &originalmatrix, &offsetmatrix);
5648         }
5649
5650         if (r_refdef.view.isoverlay)
5651         {
5652                 // TODO: FIXME: move this into its own backend function maybe? [2/5/2008 Andreas]
5653                 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
5654                 GL_Clear(GL_DEPTH_BUFFER_BIT, NULL, 1.0f, 0);
5655                 R_TimeReport("depthclear");
5656
5657                 r_refdef.view.showdebug = false;
5658
5659                 r_fb.water.enabled = false;
5660                 r_fb.water.numwaterplanes = 0;
5661
5662                 R_RenderScene(0, NULL, NULL, r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
5663
5664                 r_refdef.view.matrix = originalmatrix;
5665
5666                 CHECKGLERROR
5667                 return;
5668         }
5669
5670         if (!r_refdef.scene.entities || r_refdef.view.width * r_refdef.view.height == 0 || !r_renderview.integer || cl_videoplaying/* || !r_refdef.scene.worldmodel*/)
5671         {
5672                 r_refdef.view.matrix = originalmatrix;
5673                 return;
5674         }
5675
5676         r_refdef.view.usevieworiginculling = !r_trippy.value && r_refdef.view.useperspective;
5677         if (v_isometric.integer && r_refdef.view.ismain)
5678                 V_MakeViewIsometric();
5679
5680         r_refdef.view.colorscale = r_hdr_scenebrightness.value * r_hdr_irisadaptation_value.value;
5681
5682         if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D)
5683                 // in sRGB fallback, behave similar to true sRGB: convert this
5684                 // value from linear to sRGB
5685                 r_refdef.view.colorscale = Image_sRGBFloatFromLinearFloat(r_refdef.view.colorscale);
5686
5687         R_RenderView_UpdateViewVectors();
5688
5689         R_Shadow_UpdateWorldLightSelection();
5690
5691         // this will set up r_fb.rt_screen
5692         R_Bloom_StartFrame();
5693
5694         // apply bloom brightness offset
5695         if(r_fb.rt_bloom)
5696                 r_refdef.view.colorscale *= r_bloom_scenebrightness.value;
5697
5698         // R_Bloom_StartFrame probably set up an fbo for us to render into, it will be rendered to the window later in R_BlendView
5699         if (r_fb.rt_screen)
5700         {
5701                 viewfbo = r_fb.rt_screen->fbo;
5702                 viewdepthtexture = r_fb.rt_screen->depthtexture;
5703                 viewcolortexture = r_fb.rt_screen->colortexture[0];
5704                 viewx = 0;
5705                 viewy = 0;
5706                 viewwidth = r_fb.rt_screen->texturewidth;
5707                 viewheight = r_fb.rt_screen->textureheight;
5708         }
5709
5710         R_Water_StartFrame(viewwidth, viewheight);
5711
5712         CHECKGLERROR
5713         if (r_timereport_active)
5714                 R_TimeReport("viewsetup");
5715
5716         R_ResetViewRendering3D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5717
5718         // clear the whole fbo every frame - otherwise the driver will consider
5719         // it to be an inter-frame texture and stall in multi-gpu configurations
5720         if (r_fb.rt_screen)
5721                 GL_ScissorTest(false);
5722         R_ClearScreen(r_refdef.fogenabled);
5723         if (r_timereport_active)
5724                 R_TimeReport("viewclear");
5725
5726         r_refdef.view.clear = true;
5727
5728         r_refdef.view.showdebug = true;
5729
5730         R_View_Update();
5731         if (r_timereport_active)
5732                 R_TimeReport("visibility");
5733
5734         R_AnimCache_CacheVisibleEntities();
5735         if (r_timereport_active)
5736                 R_TimeReport("animcache");
5737
5738         R_Shadow_UpdateBounceGridTexture();
5739         // R_Shadow_UpdateBounceGridTexture called R_TimeReport a few times internally, so we don't need to do that here.
5740
5741         r_fb.water.numwaterplanes = 0;
5742         if (r_fb.water.enabled)
5743                 R_RenderWaterPlanes(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5744
5745         // for the actual view render we use scissoring a fair amount, so scissor
5746         // test needs to be on
5747         if (r_fb.rt_screen)
5748                 GL_ScissorTest(true);
5749         GL_Scissor(viewx, viewy, viewwidth, viewheight);
5750         R_RenderScene(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5751         r_fb.water.numwaterplanes = 0;
5752
5753         // postprocess uses textures that are not aligned with the viewport we're rendering, so no scissoring
5754         GL_ScissorTest(false);
5755
5756         R_BlendView(fbo, depthtexture, colortexture, x, y, width, height);
5757         if (r_timereport_active)
5758                 R_TimeReport("blendview");
5759
5760         r_refdef.view.matrix = originalmatrix;
5761
5762         CHECKGLERROR
5763
5764         // go back to 2d rendering
5765         DrawQ_Start();
5766 }
5767
5768 void R_RenderWaterPlanes(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5769 {
5770         if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawAddWaterPlanes)
5771         {
5772                 r_refdef.scene.worldmodel->DrawAddWaterPlanes(r_refdef.scene.worldentity);
5773                 if (r_timereport_active)
5774                         R_TimeReport("waterworld");
5775         }
5776
5777         // don't let sound skip if going slow
5778         if (r_refdef.scene.extraupdate)
5779                 S_ExtraUpdate ();
5780
5781         R_DrawModelsAddWaterPlanes();
5782         if (r_timereport_active)
5783                 R_TimeReport("watermodels");
5784
5785         if (r_fb.water.numwaterplanes)
5786         {
5787                 R_Water_ProcessPlanes(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5788                 if (r_timereport_active)
5789                         R_TimeReport("waterscenes");
5790         }
5791 }
5792
5793 extern cvar_t cl_locs_show;
5794 static void R_DrawLocs(void);
5795 static void R_DrawEntityBBoxes(prvm_prog_t *prog);
5796 static void R_DrawModelDecals(void);
5797 extern qbool r_shadow_usingdeferredprepass;
5798 extern int r_shadow_shadowmapatlas_modelshadows_size;
5799 void R_RenderScene(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5800 {
5801         qbool shadowmapping = false;
5802
5803         if (r_timereport_active)
5804                 R_TimeReport("beginscene");
5805
5806         r_refdef.stats[r_stat_renders]++;
5807
5808         R_UpdateFog();
5809
5810         // don't let sound skip if going slow
5811         if (r_refdef.scene.extraupdate)
5812                 S_ExtraUpdate ();
5813
5814         R_MeshQueue_BeginScene();
5815
5816         R_SkyStartFrame();
5817
5818         Matrix4x4_CreateTranslate(&r_waterscrollmatrix, sin(r_refdef.scene.time) * 0.025 * r_waterscroll.value, sin(r_refdef.scene.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
5819
5820         if (r_timereport_active)
5821                 R_TimeReport("skystartframe");
5822
5823         if (cl.csqc_vidvars.drawworld)
5824         {
5825                 // don't let sound skip if going slow
5826                 if (r_refdef.scene.extraupdate)
5827                         S_ExtraUpdate ();
5828
5829                 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawSky)
5830                 {
5831                         r_refdef.scene.worldmodel->DrawSky(r_refdef.scene.worldentity);
5832                         if (r_timereport_active)
5833                                 R_TimeReport("worldsky");
5834                 }
5835
5836                 if (R_DrawBrushModelsSky() && r_timereport_active)
5837                         R_TimeReport("bmodelsky");
5838
5839                 if (skyrendermasked && skyrenderlater)
5840                 {
5841                         // we have to force off the water clipping plane while rendering sky
5842                         R_SetupView(false, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5843                         R_Sky();
5844                         R_SetupView(true, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5845                         if (r_timereport_active)
5846                                 R_TimeReport("sky");
5847                 }
5848         }
5849
5850         // save the framebuffer info for R_Shadow_RenderMode_Reset during this view render
5851         r_shadow_viewfbo = viewfbo;
5852         r_shadow_viewdepthtexture = viewdepthtexture;
5853         r_shadow_viewcolortexture = viewcolortexture;
5854         r_shadow_viewx = viewx;
5855         r_shadow_viewy = viewy;
5856         r_shadow_viewwidth = viewwidth;
5857         r_shadow_viewheight = viewheight;
5858
5859         R_Shadow_PrepareModelShadows();
5860         R_Shadow_PrepareLights();
5861         if (r_timereport_active)
5862                 R_TimeReport("preparelights");
5863
5864         // render all the shadowmaps that will be used for this view
5865         shadowmapping = R_Shadow_ShadowMappingEnabled();
5866         if (shadowmapping || r_shadow_shadowmapatlas_modelshadows_size)
5867         {
5868                 R_Shadow_DrawShadowMaps();
5869                 if (r_timereport_active)
5870                         R_TimeReport("shadowmaps");
5871         }
5872
5873         // render prepass deferred lighting if r_shadow_deferred is on, this produces light buffers that will be sampled in forward pass
5874         if (r_shadow_usingdeferredprepass)
5875                 R_Shadow_DrawPrepass();
5876
5877         // now we begin the forward pass of the view render
5878         if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawDepth)
5879         {
5880                 r_refdef.scene.worldmodel->DrawDepth(r_refdef.scene.worldentity);
5881                 if (r_timereport_active)
5882                         R_TimeReport("worlddepth");
5883         }
5884         if (r_depthfirst.integer >= 2)
5885         {
5886                 R_DrawModelsDepth();
5887                 if (r_timereport_active)
5888                         R_TimeReport("modeldepth");
5889         }
5890
5891         if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->Draw)
5892         {
5893                 r_refdef.scene.worldmodel->Draw(r_refdef.scene.worldentity);
5894                 if (r_timereport_active)
5895                         R_TimeReport("world");
5896         }
5897
5898         // don't let sound skip if going slow
5899         if (r_refdef.scene.extraupdate)
5900                 S_ExtraUpdate ();
5901
5902         R_DrawModels();
5903         if (r_timereport_active)
5904                 R_TimeReport("models");
5905
5906         // don't let sound skip if going slow
5907         if (r_refdef.scene.extraupdate)
5908                 S_ExtraUpdate ();
5909
5910         if (!r_shadow_usingdeferredprepass)
5911         {
5912                 R_Shadow_DrawLights();
5913                 if (r_timereport_active)
5914                         R_TimeReport("rtlights");
5915         }
5916
5917         // don't let sound skip if going slow
5918         if (r_refdef.scene.extraupdate)
5919                 S_ExtraUpdate ();
5920
5921         if (cl.csqc_vidvars.drawworld)
5922         {
5923                 R_DrawModelDecals();
5924                 if (r_timereport_active)
5925                         R_TimeReport("modeldecals");
5926
5927                 R_DrawParticles();
5928                 if (r_timereport_active)
5929                         R_TimeReport("particles");
5930
5931                 R_DrawExplosions();
5932                 if (r_timereport_active)
5933                         R_TimeReport("explosions");
5934         }
5935
5936         if (r_refdef.view.showdebug)
5937         {
5938                 if (cl_locs_show.integer)
5939                 {
5940                         R_DrawLocs();
5941                         if (r_timereport_active)
5942                                 R_TimeReport("showlocs");
5943                 }
5944
5945                 if (r_drawportals.integer)
5946                 {
5947                         R_DrawPortals();
5948                         if (r_timereport_active)
5949                                 R_TimeReport("portals");
5950                 }
5951
5952                 if (r_showbboxes_client.value > 0)
5953                 {
5954                         R_DrawEntityBBoxes(CLVM_prog);
5955                         if (r_timereport_active)
5956                                 R_TimeReport("clbboxes");
5957                 }
5958                 if (r_showbboxes.value > 0)
5959                 {
5960                         R_DrawEntityBBoxes(SVVM_prog);
5961                         if (r_timereport_active)
5962                                 R_TimeReport("svbboxes");
5963                 }
5964         }
5965
5966         if (r_transparent.integer)
5967         {
5968                 R_MeshQueue_RenderTransparent();
5969                 if (r_timereport_active)
5970                         R_TimeReport("drawtrans");
5971         }
5972
5973         if (r_refdef.view.showdebug && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawDebug && (r_showtris.value > 0 || r_shownormals.value != 0 || r_showcollisionbrushes.value > 0 || r_showoverdraw.value > 0))
5974         {
5975                 r_refdef.scene.worldmodel->DrawDebug(r_refdef.scene.worldentity);
5976                 if (r_timereport_active)
5977                         R_TimeReport("worlddebug");
5978                 R_DrawModelsDebug();
5979                 if (r_timereport_active)
5980                         R_TimeReport("modeldebug");
5981         }
5982
5983         if (cl.csqc_vidvars.drawworld)
5984         {
5985                 R_Shadow_DrawCoronas();
5986                 if (r_timereport_active)
5987                         R_TimeReport("coronas");
5988         }
5989
5990         // don't let sound skip if going slow
5991         if (r_refdef.scene.extraupdate)
5992                 S_ExtraUpdate ();
5993 }
5994
5995 static const unsigned short bboxelements[36] =
5996 {
5997         5, 1, 3, 5, 3, 7,
5998         6, 2, 0, 6, 0, 4,
5999         7, 3, 2, 7, 2, 6,
6000         4, 0, 1, 4, 1, 5,
6001         4, 5, 7, 4, 7, 6,
6002         1, 0, 2, 1, 2, 3,
6003 };
6004
6005 #define BBOXEDGES 13
6006 static const float bboxedges[BBOXEDGES][6] = 
6007 {
6008         // whole box
6009         { 0, 0, 0, 1, 1, 1 },
6010         // bottom edges
6011         { 0, 0, 0, 0, 1, 0 },
6012         { 0, 0, 0, 1, 0, 0 },
6013         { 0, 1, 0, 1, 1, 0 },
6014         { 1, 0, 0, 1, 1, 0 },
6015         // top edges
6016         { 0, 0, 1, 0, 1, 1 },
6017         { 0, 0, 1, 1, 0, 1 },
6018         { 0, 1, 1, 1, 1, 1 },
6019         { 1, 0, 1, 1, 1, 1 },
6020         // vertical edges
6021         { 0, 0, 0, 0, 0, 1 },
6022         { 1, 0, 0, 1, 0, 1 },
6023         { 0, 1, 0, 0, 1, 1 },
6024         { 1, 1, 0, 1, 1, 1 },
6025 };
6026
6027 static void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
6028 {
6029         int numvertices = BBOXEDGES * 8;
6030         float vertex3f[BBOXEDGES * 8 * 3], color4f[BBOXEDGES * 8 * 4];
6031         int numtriangles = BBOXEDGES * 12;
6032         unsigned short elements[BBOXEDGES * 36];
6033         int i, edge;
6034         float *v, *c, f1, f2, edgemins[3], edgemaxs[3];
6035
6036         RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false);
6037
6038         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6039         GL_DepthMask(false);
6040         GL_DepthRange(0, 1);
6041         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6042
6043         for (edge = 0; edge < BBOXEDGES; edge++)
6044         {
6045                 for (i = 0; i < 3; i++)
6046                 {
6047                         edgemins[i] = mins[i] + (maxs[i] - mins[i]) * bboxedges[edge][i] - 0.25f;
6048                         edgemaxs[i] = mins[i] + (maxs[i] - mins[i]) * bboxedges[edge][3 + i] + 0.25f;
6049                 }
6050                 vertex3f[edge * 24 + 0] = edgemins[0]; vertex3f[edge * 24 + 1] = edgemins[1]; vertex3f[edge * 24 + 2] = edgemins[2];
6051                 vertex3f[edge * 24 + 3] = edgemaxs[0]; vertex3f[edge * 24 + 4] = edgemins[1]; vertex3f[edge * 24 + 5] = edgemins[2];
6052                 vertex3f[edge * 24 + 6] = edgemins[0]; vertex3f[edge * 24 + 7] = edgemaxs[1]; vertex3f[edge * 24 + 8] = edgemins[2];
6053                 vertex3f[edge * 24 + 9] = edgemaxs[0]; vertex3f[edge * 24 + 10] = edgemaxs[1]; vertex3f[edge * 24 + 11] = edgemins[2];
6054                 vertex3f[edge * 24 + 12] = edgemins[0]; vertex3f[edge * 24 + 13] = edgemins[1]; vertex3f[edge * 24 + 14] = edgemaxs[2];
6055                 vertex3f[edge * 24 + 15] = edgemaxs[0]; vertex3f[edge * 24 + 16] = edgemins[1]; vertex3f[edge * 24 + 17] = edgemaxs[2];
6056                 vertex3f[edge * 24 + 18] = edgemins[0]; vertex3f[edge * 24 + 19] = edgemaxs[1]; vertex3f[edge * 24 + 20] = edgemaxs[2];
6057                 vertex3f[edge * 24 + 21] = edgemaxs[0]; vertex3f[edge * 24 + 22] = edgemaxs[1]; vertex3f[edge * 24 + 23] = edgemaxs[2];
6058                 for (i = 0; i < 36; i++)
6059                         elements[edge * 36 + i] = edge * 8 + bboxelements[i];
6060         }
6061         R_FillColors(color4f, numvertices, cr, cg, cb, ca);
6062         if (r_refdef.fogenabled)
6063         {
6064                 for (i = 0, v = vertex3f, c = color4f; i < numvertices; i++, v += 3, c += 4)
6065                 {
6066                         f1 = RSurf_FogVertex(v);
6067                         f2 = 1 - f1;
6068                         c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
6069                         c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
6070                         c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
6071                 }
6072         }
6073         R_Mesh_PrepareVertices_Generic_Arrays(numvertices, vertex3f, color4f, NULL);
6074         R_Mesh_ResetTextureState();
6075         R_SetupShader_Generic_NoTexture(false, false);
6076         R_Mesh_Draw(0, numvertices, 0, numtriangles, NULL, NULL, 0, elements, NULL, 0);
6077 }
6078
6079 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6080 {
6081         // hacky overloading of the parameters
6082         prvm_prog_t *prog = (prvm_prog_t *)rtlight;
6083         int i;
6084         float color[4];
6085         prvm_edict_t *edict;
6086
6087         GL_CullFace(GL_NONE);
6088         R_SetupShader_Generic_NoTexture(false, false);
6089
6090         for (i = 0;i < numsurfaces;i++)
6091         {
6092                 edict = PRVM_EDICT_NUM(surfacelist[i]);
6093                 switch ((int)PRVM_serveredictfloat(edict, solid))
6094                 {
6095                         case SOLID_NOT:      Vector4Set(color, 1, 1, 1, 0.05);break;
6096                         case SOLID_TRIGGER:  Vector4Set(color, 1, 0, 1, 0.10);break;
6097                         case SOLID_BBOX:     Vector4Set(color, 0, 1, 0, 0.10);break;
6098                         case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
6099                         case SOLID_BSP:      Vector4Set(color, 0, 0, 1, 0.05);break;
6100                         case SOLID_CORPSE:   Vector4Set(color, 1, 0.5, 0, 0.05);break;
6101                         default:             Vector4Set(color, 0, 0, 0, 0.50);break;
6102                 }
6103                 if (prog == CLVM_prog)
6104                         color[3] *= r_showbboxes_client.value;
6105                 else
6106                         color[3] *= r_showbboxes.value;
6107                 color[3] = bound(0, color[3], 1);
6108                 GL_DepthTest(!r_showdisabledepthtest.integer);
6109                 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
6110         }
6111 }
6112
6113 static void R_DrawEntityBBoxes(prvm_prog_t *prog)
6114 {
6115         int i;
6116         prvm_edict_t *edict;
6117         vec3_t center;
6118
6119         if (prog == NULL)
6120                 return;
6121
6122         for (i = 0; i < prog->num_edicts; i++)
6123         {
6124                 edict = PRVM_EDICT_NUM(i);
6125                 if (edict->free)
6126                         continue;
6127                 // exclude the following for now, as they don't live in world coordinate space and can't be solid:
6128                 if (PRVM_gameedictedict(edict, tag_entity) != 0)
6129                         continue;
6130                 if (prog == SVVM_prog && PRVM_serveredictedict(edict, viewmodelforclient) != 0)
6131                         continue;
6132                 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
6133                 R_MeshQueue_AddTransparent(TRANSPARENTSORT_DISTANCE, center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)prog);
6134         }
6135 }
6136
6137 static const int nomodelelement3i[24] =
6138 {
6139         5, 2, 0,
6140         5, 1, 2,
6141         5, 0, 3,
6142         5, 3, 1,
6143         0, 2, 4,
6144         2, 1, 4,
6145         3, 0, 4,
6146         1, 3, 4
6147 };
6148
6149 static const unsigned short nomodelelement3s[24] =
6150 {
6151         5, 2, 0,
6152         5, 1, 2,
6153         5, 0, 3,
6154         5, 3, 1,
6155         0, 2, 4,
6156         2, 1, 4,
6157         3, 0, 4,
6158         1, 3, 4
6159 };
6160
6161 static const float nomodelvertex3f[6*3] =
6162 {
6163         -16,   0,   0,
6164          16,   0,   0,
6165           0, -16,   0,
6166           0,  16,   0,
6167           0,   0, -16,
6168           0,   0,  16
6169 };
6170
6171 static const float nomodelcolor4f[6*4] =
6172 {
6173         0.0f, 0.0f, 0.5f, 1.0f,
6174         0.0f, 0.0f, 0.5f, 1.0f,
6175         0.0f, 0.5f, 0.0f, 1.0f,
6176         0.0f, 0.5f, 0.0f, 1.0f,
6177         0.5f, 0.0f, 0.0f, 1.0f,
6178         0.5f, 0.0f, 0.0f, 1.0f
6179 };
6180
6181 static void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6182 {
6183         int i;
6184         float f1, f2, *c;
6185         float color4f[6*4];
6186
6187         RSurf_ActiveCustomEntity(&ent->matrix, &ent->inversematrix, ent->flags, ent->shadertime, ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha, 6, nomodelvertex3f, NULL, NULL, NULL, NULL, nomodelcolor4f, 8, nomodelelement3i, nomodelelement3s, false, false);
6188
6189         // this is only called once per entity so numsurfaces is always 1, and
6190         // surfacelist is always {0}, so this code does not handle batches
6191
6192         if (rsurface.ent_flags & RENDER_ADDITIVE)
6193         {
6194                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
6195                 GL_DepthMask(false);
6196         }
6197         else if (ent->alpha < 1)
6198         {
6199                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6200                 GL_DepthMask(false);
6201         }
6202         else
6203         {
6204                 GL_BlendFunc(GL_ONE, GL_ZERO);
6205                 GL_DepthMask(true);
6206         }
6207         GL_DepthRange(0, (rsurface.ent_flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
6208         GL_PolygonOffset(rsurface.basepolygonfactor, rsurface.basepolygonoffset);
6209         GL_DepthTest(!(rsurface.ent_flags & RENDER_NODEPTHTEST));
6210         GL_CullFace((rsurface.ent_flags & RENDER_DOUBLESIDED) ? GL_NONE : r_refdef.view.cullface_back);
6211         memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
6212         for (i = 0, c = color4f;i < 6;i++, c += 4)
6213         {
6214                 c[0] *= ent->render_fullbright[0] * r_refdef.view.colorscale;
6215                 c[1] *= ent->render_fullbright[1] * r_refdef.view.colorscale;
6216                 c[2] *= ent->render_fullbright[2] * r_refdef.view.colorscale;
6217                 c[3] *= ent->alpha;
6218         }
6219         if (r_refdef.fogenabled)
6220         {
6221                 for (i = 0, c = color4f;i < 6;i++, c += 4)
6222                 {
6223                         f1 = RSurf_FogVertex(nomodelvertex3f + 3*i);
6224                         f2 = 1 - f1;
6225                         c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
6226                         c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
6227                         c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
6228                 }
6229         }
6230 //      R_Mesh_ResetTextureState();
6231         R_SetupShader_Generic_NoTexture(false, false);
6232         R_Mesh_PrepareVertices_Generic_Arrays(6, nomodelvertex3f, color4f, NULL);
6233         R_Mesh_Draw(0, 6, 0, 8, nomodelelement3i, NULL, 0, nomodelelement3s, NULL, 0);
6234 }
6235
6236 void R_DrawNoModel(entity_render_t *ent)
6237 {
6238         vec3_t org;
6239         Matrix4x4_OriginFromMatrix(&ent->matrix, org);
6240         if ((ent->flags & RENDER_ADDITIVE) || (ent->alpha < 1))
6241                 R_MeshQueue_AddTransparent((ent->flags & RENDER_NODEPTHTEST) ? TRANSPARENTSORT_HUD : TRANSPARENTSORT_DISTANCE, org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
6242         else
6243                 R_DrawNoModel_TransparentCallback(ent, rsurface.rtlight, 0, NULL);
6244 }
6245
6246 void R_CalcBeam_Vertex3f (float *vert, const float *org1, const float *org2, float width)
6247 {
6248         vec3_t right1, right2, diff, normal;
6249
6250         VectorSubtract (org2, org1, normal);
6251
6252         // calculate 'right' vector for start
6253         VectorSubtract (r_refdef.view.origin, org1, diff);
6254         CrossProduct (normal, diff, right1);
6255         VectorNormalize (right1);
6256
6257         // calculate 'right' vector for end
6258         VectorSubtract (r_refdef.view.origin, org2, diff);
6259         CrossProduct (normal, diff, right2);
6260         VectorNormalize (right2);
6261
6262         vert[ 0] = org1[0] + width * right1[0];
6263         vert[ 1] = org1[1] + width * right1[1];
6264         vert[ 2] = org1[2] + width * right1[2];
6265         vert[ 3] = org1[0] - width * right1[0];
6266         vert[ 4] = org1[1] - width * right1[1];
6267         vert[ 5] = org1[2] - width * right1[2];
6268         vert[ 6] = org2[0] - width * right2[0];
6269         vert[ 7] = org2[1] - width * right2[1];
6270         vert[ 8] = org2[2] - width * right2[2];
6271         vert[ 9] = org2[0] + width * right2[0];
6272         vert[10] = org2[1] + width * right2[1];
6273         vert[11] = org2[2] + width * right2[2];
6274 }
6275
6276 void R_CalcSprite_Vertex3f(float *vertex3f, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2)
6277 {
6278         vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
6279         vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
6280         vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
6281         vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
6282         vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
6283         vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
6284         vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
6285         vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
6286         vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
6287         vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
6288         vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
6289         vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
6290 }
6291
6292 static int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
6293 {
6294         int i;
6295         float *vertex3f;
6296         float v[3];
6297         VectorSet(v, x, y, z);
6298         for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
6299                 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
6300                         break;
6301         if (i == mesh->numvertices)
6302         {
6303                 if (mesh->numvertices < mesh->maxvertices)
6304                 {
6305                         VectorCopy(v, vertex3f);
6306                         mesh->numvertices++;
6307                 }
6308                 return mesh->numvertices;
6309         }
6310         else
6311                 return i;
6312 }
6313
6314 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
6315 {
6316         int i;
6317         int *e, element[3];
6318         element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
6319         element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
6320         e = mesh->element3i + mesh->numtriangles * 3;
6321         for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
6322         {
6323                 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
6324                 if (mesh->numtriangles < mesh->maxtriangles)
6325                 {
6326                         *e++ = element[0];
6327                         *e++ = element[1];
6328                         *e++ = element[2];
6329                         mesh->numtriangles++;
6330                 }
6331                 element[1] = element[2];
6332         }
6333 }
6334
6335 static void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
6336 {
6337         int i;
6338         int *e, element[3];
6339         element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
6340         element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
6341         e = mesh->element3i + mesh->numtriangles * 3;
6342         for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
6343         {
6344                 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
6345                 if (mesh->numtriangles < mesh->maxtriangles)
6346                 {
6347                         *e++ = element[0];
6348                         *e++ = element[1];
6349                         *e++ = element[2];
6350                         mesh->numtriangles++;
6351                 }
6352                 element[1] = element[2];
6353         }
6354 }
6355
6356 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
6357 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
6358 {
6359         int planenum, planenum2;
6360         int w;
6361         int tempnumpoints;
6362         mplane_t *plane, *plane2;
6363         double maxdist;
6364         double temppoints[2][256*3];
6365         // figure out how large a bounding box we need to properly compute this brush
6366         maxdist = 0;
6367         for (w = 0;w < numplanes;w++)
6368                 maxdist = max(maxdist, fabs(planes[w].dist));
6369         // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
6370         maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
6371         for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
6372         {
6373                 w = 0;
6374                 tempnumpoints = 4;
6375                 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
6376                 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
6377                 {
6378                         if (planenum2 == planenum)
6379                                 continue;
6380                         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);
6381                         w = !w;
6382                 }
6383                 if (tempnumpoints < 3)
6384                         continue;
6385                 // generate elements forming a triangle fan for this polygon
6386                 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
6387         }
6388 }
6389
6390 static qbool R_TestQ3WaveFunc(q3wavefunc_t func, const float *parms)
6391 {
6392         if(parms[0] == 0 && parms[1] == 0)
6393                 return false;
6394         if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
6395                 if(rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)] == 0)
6396                         return false;
6397         return true;
6398 }
6399
6400 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
6401 {
6402         double index, f;
6403         index = parms[2] + rsurface.shadertime * parms[3];
6404         index -= floor(index);
6405         switch (func & ((1 << Q3WAVEFUNC_USER_SHIFT) - 1))
6406         {
6407         default:
6408         case Q3WAVEFUNC_NONE:
6409         case Q3WAVEFUNC_NOISE:
6410         case Q3WAVEFUNC_COUNT:
6411                 f = 0;
6412                 break;
6413         case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
6414         case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
6415         case Q3WAVEFUNC_SAWTOOTH: f = index;break;
6416         case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
6417         case Q3WAVEFUNC_TRIANGLE:
6418                 index *= 4;
6419                 f = index - floor(index);
6420                 if (index < 1)
6421                 {
6422                         // f = f;
6423                 }
6424                 else if (index < 2)
6425                         f = 1 - f;
6426                 else if (index < 3)
6427                         f = -f;
6428                 else
6429                         f = -(1 - f);
6430                 break;
6431         }
6432         f = parms[0] + parms[1] * f;
6433         if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
6434                 f *= rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)];
6435         return (float) f;
6436 }
6437
6438 static void R_tcMod_ApplyToMatrix(matrix4x4_t *texmatrix, q3shaderinfo_layer_tcmod_t *tcmod, int currentmaterialflags)
6439 {
6440         int w, h, idx;
6441         float shadertime;
6442         float f;
6443         float offsetd[2];
6444         float tcmat[12];
6445         matrix4x4_t matrix, temp;
6446         // if shadertime exceeds about 9 hours (32768 seconds), just wrap it,
6447         // it's better to have one huge fixup every 9 hours than gradual
6448         // degradation over time which looks consistently bad after many hours.
6449         //
6450         // tcmod scroll in particular suffers from this degradation which can't be
6451         // effectively worked around even with floor() tricks because we don't
6452         // know if tcmod scroll is the last tcmod being applied, and for clampmap
6453         // a workaround involving floor() would be incorrect anyway...
6454         shadertime = rsurface.shadertime;
6455         if (shadertime >= 32768.0f)
6456                 shadertime -= floor(rsurface.shadertime * (1.0f / 32768.0f)) * 32768.0f;
6457         switch(tcmod->tcmod)
6458         {
6459                 case Q3TCMOD_COUNT:
6460                 case Q3TCMOD_NONE:
6461                         if (currentmaterialflags & MATERIALFLAG_WATERSCROLL)
6462                                 matrix = r_waterscrollmatrix;
6463                         else
6464                                 matrix = identitymatrix;
6465                         break;
6466                 case Q3TCMOD_ENTITYTRANSLATE:
6467                         // this is used in Q3 to allow the gamecode to control texcoord
6468                         // scrolling on the entity, which is not supported in darkplaces yet.
6469                         Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
6470                         break;
6471                 case Q3TCMOD_ROTATE:
6472                         Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
6473                         Matrix4x4_ConcatRotate(&matrix, tcmod->parms[0] * rsurface.shadertime, 0, 0, 1);
6474                         Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
6475                         break;
6476                 case Q3TCMOD_SCALE:
6477                         Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
6478                         break;
6479                 case Q3TCMOD_SCROLL:
6480                         // this particular tcmod is a "bug for bug" compatible one with regards to
6481                         // Quake3, the wrapping is unnecessary with our shadetime fix but quake3
6482                         // specifically did the wrapping and so we must mimic that...
6483                         offsetd[0] = tcmod->parms[0] * rsurface.shadertime;
6484                         offsetd[1] = tcmod->parms[1] * rsurface.shadertime;
6485                         Matrix4x4_CreateTranslate(&matrix, offsetd[0] - floor(offsetd[0]), offsetd[1] - floor(offsetd[1]), 0);
6486                         break;
6487                 case Q3TCMOD_PAGE: // poor man's animmap (to store animations into a single file, useful for HTTP downloaded textures)
6488                         w = (int) tcmod->parms[0];
6489                         h = (int) tcmod->parms[1];
6490                         f = rsurface.shadertime / (tcmod->parms[2] * w * h);
6491                         f = f - floor(f);
6492                         idx = (int) floor(f * w * h);
6493                         Matrix4x4_CreateTranslate(&matrix, (idx % w) / tcmod->parms[0], (idx / w) / tcmod->parms[1], 0);
6494                         break;
6495                 case Q3TCMOD_STRETCH:
6496                         f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
6497                         Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
6498                         break;
6499                 case Q3TCMOD_TRANSFORM:
6500                         VectorSet(tcmat +  0, tcmod->parms[0], tcmod->parms[1], 0);
6501                         VectorSet(tcmat +  3, tcmod->parms[2], tcmod->parms[3], 0);
6502                         VectorSet(tcmat +  6, 0                   , 0                , 1);
6503                         VectorSet(tcmat +  9, tcmod->parms[4], tcmod->parms[5], 0);
6504                         Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
6505                         break;
6506                 case Q3TCMOD_TURBULENT:
6507                         // this is handled in the RSurf_PrepareVertices function
6508                         matrix = identitymatrix;
6509                         break;
6510         }
6511         temp = *texmatrix;
6512         Matrix4x4_Concat(texmatrix, &matrix, &temp);
6513 }
6514
6515 static void R_LoadQWSkin(r_qwskincache_t *cache, const char *skinname)
6516 {
6517         int textureflags = (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_PICMIP;
6518         char name[MAX_QPATH];
6519         skinframe_t *skinframe;
6520         unsigned char pixels[296*194];
6521         strlcpy(cache->name, skinname, sizeof(cache->name));
6522         dpsnprintf(name, sizeof(name), "skins/%s.pcx", cache->name);
6523         if (developer_loading.integer)
6524                 Con_Printf("loading %s\n", name);
6525         skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
6526         if (!skinframe || !skinframe->base)
6527         {
6528                 unsigned char *f;
6529                 fs_offset_t filesize;
6530                 skinframe = NULL;
6531                 f = FS_LoadFile(name, tempmempool, true, &filesize);
6532                 if (f)
6533                 {
6534                         if (LoadPCX_QWSkin(f, (int)filesize, pixels, 296, 194))
6535                                 skinframe = R_SkinFrame_LoadInternalQuake(name, textureflags, true, r_fullbrights.integer, pixels, image_width, image_height);
6536                         Mem_Free(f);
6537                 }
6538         }
6539         cache->skinframe = skinframe;
6540 }
6541
6542 texture_t *R_GetCurrentTexture(texture_t *t)
6543 {
6544         int i, q;
6545         const entity_render_t *ent = rsurface.entity;
6546         model_t *model = ent->model; // when calling this, ent must not be NULL
6547         q3shaderinfo_layer_tcmod_t *tcmod;
6548         float specularscale = 0.0f;
6549
6550         if (t->update_lastrenderframe == r_textureframe && t->update_lastrenderentity == (void *)ent && !rsurface.forcecurrenttextureupdate)
6551                 return t->currentframe;
6552         t->update_lastrenderframe = r_textureframe;
6553         t->update_lastrenderentity = (void *)ent;
6554
6555         if(ent->entitynumber >= MAX_EDICTS && ent->entitynumber < 2 * MAX_EDICTS)
6556                 t->camera_entity = ent->entitynumber;
6557         else
6558                 t->camera_entity = 0;
6559
6560         // switch to an alternate material if this is a q1bsp animated material
6561         {
6562                 texture_t *texture = t;
6563                 int s = rsurface.ent_skinnum;
6564                 if ((unsigned int)s >= (unsigned int)model->numskins)
6565                         s = 0;
6566                 if (model->skinscenes)
6567                 {
6568                         if (model->skinscenes[s].framecount > 1)
6569                                 s = model->skinscenes[s].firstframe + (unsigned int) (rsurface.shadertime * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
6570                         else
6571                                 s = model->skinscenes[s].firstframe;
6572                 }
6573                 if (s > 0)
6574                         t = t + s * model->num_surfaces;
6575                 if (t->animated)
6576                 {
6577                         // use an alternate animation if the entity's frame is not 0,
6578                         // and only if the texture has an alternate animation
6579                         if (t->animated == 2) // q2bsp
6580                                 t = t->anim_frames[0][ent->framegroupblend[0].frame % t->anim_total[0]];
6581                         else if (rsurface.ent_alttextures && t->anim_total[1])
6582                                 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[1]) : 0];
6583                         else
6584                                 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[0]) : 0];
6585                 }
6586                 texture->currentframe = t;
6587         }
6588
6589         // update currentskinframe to be a qw skin or animation frame
6590         if (rsurface.ent_qwskin >= 0)
6591         {
6592                 i = rsurface.ent_qwskin;
6593                 if (!r_qwskincache || r_qwskincache_size != cl.maxclients)
6594                 {
6595                         r_qwskincache_size = cl.maxclients;
6596                         if (r_qwskincache)
6597                                 Mem_Free(r_qwskincache);
6598                         r_qwskincache = (r_qwskincache_t *)Mem_Alloc(r_main_mempool, sizeof(*r_qwskincache) * r_qwskincache_size);
6599                 }
6600                 if (strcmp(r_qwskincache[i].name, cl.scores[i].qw_skin))
6601                         R_LoadQWSkin(&r_qwskincache[i], cl.scores[i].qw_skin);
6602                 t->currentskinframe = r_qwskincache[i].skinframe;
6603                 if (t->materialshaderpass && t->currentskinframe == NULL)
6604                         t->currentskinframe = t->materialshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->materialshaderpass->framerate, t->materialshaderpass->numframes)];
6605         }
6606         else if (t->materialshaderpass && t->materialshaderpass->numframes >= 2)
6607                 t->currentskinframe = t->materialshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->materialshaderpass->framerate, t->materialshaderpass->numframes)];
6608         if (t->backgroundshaderpass && t->backgroundshaderpass->numframes >= 2)
6609                 t->backgroundcurrentskinframe = t->backgroundshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->backgroundshaderpass->framerate, t->backgroundshaderpass->numframes)];
6610
6611         t->currentmaterialflags = t->basematerialflags;
6612         t->currentalpha = rsurface.entity->alpha * t->basealpha;
6613         if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_water.integer || r_novis.integer || r_trippy.integer))
6614                 t->currentalpha *= r_wateralpha.value;
6615         if(t->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay)
6616                 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; // we apply wateralpha later
6617         if(!r_fb.water.enabled || r_refdef.view.isoverlay)
6618                 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA);
6619
6620         // decide on which type of lighting to use for this surface
6621         if (rsurface.entity->render_modellight_forced)
6622                 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
6623         if (rsurface.entity->render_rtlight_disabled)
6624                 t->currentmaterialflags |= MATERIALFLAG_NORTLIGHT;
6625         if (rsurface.entity->render_lightgrid)
6626                 t->currentmaterialflags |= MATERIALFLAG_LIGHTGRID;
6627         if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND && !(R_BlendFuncFlags(t->customblendfunc[0], t->customblendfunc[1]) & BLENDFUNC_ALLOWS_COLORMOD))
6628         {
6629                 // some CUSTOMBLEND blendfuncs are too weird, we have to ignore colormod and view colorscale
6630                 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_NORTLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6631                 for (q = 0; q < 3; q++)
6632                 {
6633                         t->render_glowmod[q] = rsurface.entity->glowmod[q];
6634                         t->render_modellight_lightdir_world[q] = q == 2;
6635                         t->render_modellight_lightdir_local[q] = q == 2;
6636                         t->render_modellight_ambient[q] = 1;
6637                         t->render_modellight_diffuse[q] = 0;
6638                         t->render_modellight_specular[q] = 0;
6639                         t->render_lightmap_ambient[q] = 0;
6640                         t->render_lightmap_diffuse[q] = 0;
6641                         t->render_lightmap_specular[q] = 0;
6642                         t->render_rtlight_diffuse[q] = 0;
6643                         t->render_rtlight_specular[q] = 0;
6644                 }
6645         }
6646         else if ((t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) || !(rsurface.ent_flags & RENDER_LIGHT))
6647         {
6648                 // fullbright is basically MATERIALFLAG_MODELLIGHT but with ambient locked to 1,1,1 and no shading
6649                 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_NORTLIGHT | MATERIALFLAG_MODELLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6650                 for (q = 0; q < 3; q++)
6651                 {
6652                         t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6653                         t->render_modellight_ambient[q] = rsurface.entity->render_fullbright[q] * r_refdef.view.colorscale;
6654                         t->render_modellight_lightdir_world[q] = q == 2;
6655                         t->render_modellight_lightdir_local[q] = q == 2;
6656                         t->render_modellight_diffuse[q] = 0;
6657                         t->render_modellight_specular[q] = 0;
6658                         t->render_lightmap_ambient[q] = 0;
6659                         t->render_lightmap_diffuse[q] = 0;
6660                         t->render_lightmap_specular[q] = 0;
6661                         t->render_rtlight_diffuse[q] = 0;
6662                         t->render_rtlight_specular[q] = 0;
6663                 }
6664         }
6665         else if (t->currentmaterialflags & MATERIALFLAG_LIGHTGRID)
6666         {
6667                 t->currentmaterialflags &= ~MATERIALFLAG_MODELLIGHT;
6668                 for (q = 0; q < 3; q++)
6669                 {
6670                         t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6671                         t->render_modellight_lightdir_world[q] = q == 2;
6672                         t->render_modellight_lightdir_local[q] = q == 2;
6673                         t->render_modellight_ambient[q] = 0;
6674                         t->render_modellight_diffuse[q] = 0;
6675                         t->render_modellight_specular[q] = 0;
6676                         t->render_lightmap_ambient[q] = rsurface.entity->render_lightmap_ambient[q] * r_refdef.view.colorscale;
6677                         t->render_lightmap_diffuse[q] = rsurface.entity->render_lightmap_diffuse[q] * 2 * r_refdef.view.colorscale;
6678                         t->render_lightmap_specular[q] = rsurface.entity->render_lightmap_specular[q] * 2 * r_refdef.view.colorscale;
6679                         t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6680                         t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6681                 }
6682         }
6683         else if ((rsurface.ent_flags & (RENDER_DYNAMICMODELLIGHT | RENDER_CUSTOMIZEDMODELLIGHT)) || rsurface.modeltexcoordlightmap2f == NULL)
6684         {
6685                 // ambient + single direction light (modellight)
6686                 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_MODELLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6687                 for (q = 0; q < 3; q++)
6688                 {
6689                         t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6690                         t->render_modellight_lightdir_world[q] = rsurface.entity->render_modellight_lightdir_world[q];
6691                         t->render_modellight_lightdir_local[q] = rsurface.entity->render_modellight_lightdir_local[q];
6692                         t->render_modellight_ambient[q] = rsurface.entity->render_modellight_ambient[q] * r_refdef.view.colorscale;
6693                         t->render_modellight_diffuse[q] = rsurface.entity->render_modellight_diffuse[q] * r_refdef.view.colorscale;
6694                         t->render_modellight_specular[q] = rsurface.entity->render_modellight_specular[q] * r_refdef.view.colorscale;
6695                         t->render_lightmap_ambient[q] = 0;
6696                         t->render_lightmap_diffuse[q] = 0;
6697                         t->render_lightmap_specular[q] = 0;
6698                         t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6699                         t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6700                 }
6701         }
6702         else
6703         {
6704                 // lightmap - 2x diffuse and specular brightness because bsp files have 0-2 colors as 0-1
6705                 for (q = 0; q < 3; q++)
6706                 {
6707                         t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6708                         t->render_modellight_lightdir_world[q] = q == 2;
6709                         t->render_modellight_lightdir_local[q] = q == 2;
6710                         t->render_modellight_ambient[q] = 0;
6711                         t->render_modellight_diffuse[q] = 0;
6712                         t->render_modellight_specular[q] = 0;
6713                         t->render_lightmap_ambient[q] = rsurface.entity->render_lightmap_ambient[q] * r_refdef.view.colorscale;
6714                         t->render_lightmap_diffuse[q] = rsurface.entity->render_lightmap_diffuse[q] * 2 * r_refdef.view.colorscale;
6715                         t->render_lightmap_specular[q] = rsurface.entity->render_lightmap_specular[q] * 2 * r_refdef.view.colorscale;
6716                         t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6717                         t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6718                 }
6719         }
6720
6721         if (t->currentmaterialflags & MATERIALFLAG_VERTEXCOLOR)
6722         {
6723                 // since MATERIALFLAG_VERTEXCOLOR uses the lightmapcolor4f vertex
6724                 // attribute, we punt it to the lightmap path and hope for the best,
6725                 // but lighting doesn't work.
6726                 //
6727                 // FIXME: this is fine for effects but CSQC polygons should be subject
6728                 // to lighting.
6729                 t->currentmaterialflags &= ~(MATERIALFLAG_MODELLIGHT | MATERIALFLAG_LIGHTGRID);
6730                 for (q = 0; q < 3; q++)
6731                 {
6732                         t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6733                         t->render_modellight_lightdir_world[q] = q == 2;
6734                         t->render_modellight_lightdir_local[q] = q == 2;
6735                         t->render_modellight_ambient[q] = 0;
6736                         t->render_modellight_diffuse[q] = 0;
6737                         t->render_modellight_specular[q] = 0;
6738                         t->render_lightmap_ambient[q] = 0;
6739                         t->render_lightmap_diffuse[q] = rsurface.entity->render_fullbright[q] * r_refdef.view.colorscale;
6740                         t->render_lightmap_specular[q] = 0;
6741                         t->render_rtlight_diffuse[q] = 0;
6742                         t->render_rtlight_specular[q] = 0;
6743                 }
6744         }
6745
6746         for (q = 0; q < 3; q++)
6747         {
6748                 t->render_colormap_pants[q] = rsurface.entity->colormap_pantscolor[q];
6749                 t->render_colormap_shirt[q] = rsurface.entity->colormap_shirtcolor[q];
6750         }
6751
6752         if (rsurface.ent_flags & RENDER_ADDITIVE)
6753                 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
6754         else if (t->currentalpha < 1)
6755                 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
6756         // LadyHavoc: prevent bugs where code checks add or alpha at higher priority than customblend by clearing these flags
6757         if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
6758                 t->currentmaterialflags &= ~(MATERIALFLAG_ADD | MATERIALFLAG_ALPHA);
6759         if (rsurface.ent_flags & RENDER_DOUBLESIDED)
6760                 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
6761         if (rsurface.ent_flags & (RENDER_NODEPTHTEST | RENDER_VIEWMODEL))
6762                 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
6763         if (t->backgroundshaderpass)
6764                 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
6765         if (t->currentmaterialflags & MATERIALFLAG_BLENDED)
6766         {
6767                 if (t->currentmaterialflags & (MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA))
6768                         t->currentmaterialflags &= ~MATERIALFLAG_BLENDED;
6769         }
6770         else
6771                 t->currentmaterialflags &= ~(MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA);
6772         if (vid.allowalphatocoverage && r_transparent_alphatocoverage.integer >= 2 && ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA | MATERIALFLAG_ADD | MATERIALFLAG_CUSTOMBLEND)) == (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)))
6773         {
6774                 // promote alphablend to alphatocoverage (a type of alphatest) if antialiasing is on
6775                 t->currentmaterialflags = (t->currentmaterialflags & ~(MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)) | MATERIALFLAG_ALPHATEST;
6776         }
6777         if ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST)) == MATERIALFLAG_BLENDED && r_transparentdepthmasking.integer && !(t->basematerialflags & MATERIALFLAG_BLENDED))
6778                 t->currentmaterialflags |= MATERIALFLAG_TRANSDEPTH;
6779
6780         // there is no tcmod
6781         if (t->currentmaterialflags & MATERIALFLAG_WATERSCROLL)
6782         {
6783                 t->currenttexmatrix = r_waterscrollmatrix;
6784                 t->currentbackgroundtexmatrix = r_waterscrollmatrix;
6785         }
6786         else if (!(t->currentmaterialflags & MATERIALFLAG_CUSTOMSURFACE))
6787         {
6788                 Matrix4x4_CreateIdentity(&t->currenttexmatrix);
6789                 Matrix4x4_CreateIdentity(&t->currentbackgroundtexmatrix);
6790         }
6791
6792         if (t->materialshaderpass)
6793                 for (i = 0, tcmod = t->materialshaderpass->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
6794                         R_tcMod_ApplyToMatrix(&t->currenttexmatrix, tcmod, t->currentmaterialflags);
6795
6796         t->colormapping = VectorLength2(t->render_colormap_pants) + VectorLength2(t->render_colormap_shirt) >= (1.0f / 1048576.0f);
6797         if (t->currentskinframe->qpixels)
6798                 R_SkinFrame_GenerateTexturesFromQPixels(t->currentskinframe, t->colormapping);
6799         t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
6800         if (!t->basetexture)
6801                 t->basetexture = r_texture_notexture;
6802         t->pantstexture = t->colormapping ? t->currentskinframe->pants : NULL;
6803         t->shirttexture = t->colormapping ? t->currentskinframe->shirt : NULL;
6804         t->nmaptexture = t->currentskinframe->nmap;
6805         if (!t->nmaptexture)
6806                 t->nmaptexture = r_texture_blanknormalmap;
6807         t->glosstexture = r_texture_black;
6808         t->glowtexture = t->currentskinframe->glow;
6809         t->fogtexture = t->currentskinframe->fog;
6810         t->reflectmasktexture = t->currentskinframe->reflect;
6811         if (t->backgroundshaderpass)
6812         {
6813                 for (i = 0, tcmod = t->backgroundshaderpass->tcmods; i < Q3MAXTCMODS && tcmod->tcmod; i++, tcmod++)
6814                         R_tcMod_ApplyToMatrix(&t->currentbackgroundtexmatrix, tcmod, t->currentmaterialflags);
6815                 t->backgroundbasetexture = (!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base;
6816                 t->backgroundnmaptexture = t->backgroundcurrentskinframe->nmap;
6817                 t->backgroundglosstexture = r_texture_black;
6818                 t->backgroundglowtexture = t->backgroundcurrentskinframe->glow;
6819                 if (!t->backgroundnmaptexture)
6820                         t->backgroundnmaptexture = r_texture_blanknormalmap;
6821                 // make sure that if glow is going to be used, both textures are not NULL
6822                 if (!t->backgroundglowtexture && t->glowtexture)
6823                         t->backgroundglowtexture = r_texture_black;
6824                 if (!t->glowtexture && t->backgroundglowtexture)
6825                         t->glowtexture = r_texture_black;
6826         }
6827         else
6828         {
6829                 t->backgroundbasetexture = r_texture_white;
6830                 t->backgroundnmaptexture = r_texture_blanknormalmap;
6831                 t->backgroundglosstexture = r_texture_black;
6832                 t->backgroundglowtexture = NULL;
6833         }
6834         t->specularpower = r_shadow_glossexponent.value;
6835         // TODO: store reference values for these in the texture?
6836         if (r_shadow_gloss.integer > 0)
6837         {
6838                 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
6839                 {
6840                         if (r_shadow_glossintensity.value > 0)
6841                         {
6842                                 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
6843                                 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
6844                                 specularscale = r_shadow_glossintensity.value;
6845                         }
6846                 }
6847                 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
6848                 {
6849                         t->glosstexture = r_texture_white;
6850                         t->backgroundglosstexture = r_texture_white;
6851                         specularscale = r_shadow_gloss2intensity.value;
6852                         t->specularpower = r_shadow_gloss2exponent.value;
6853                 }
6854         }
6855         specularscale *= t->specularscalemod;
6856         t->specularpower *= t->specularpowermod;
6857
6858         // lightmaps mode looks bad with dlights using actual texturing, so turn
6859         // off the colormap and glossmap, but leave the normalmap on as it still
6860         // accurately represents the shading involved
6861         if (gl_lightmaps.integer && ent != &cl_meshentities[MESH_UI].render)
6862         {
6863                 t->basetexture = r_texture_grey128;
6864                 t->pantstexture = r_texture_black;
6865                 t->shirttexture = r_texture_black;
6866                 if (gl_lightmaps.integer < 2)
6867                         t->nmaptexture = r_texture_blanknormalmap;
6868                 t->glosstexture = r_texture_black;
6869                 t->glowtexture = NULL;
6870                 t->fogtexture = NULL;
6871                 t->reflectmasktexture = NULL;
6872                 t->backgroundbasetexture = NULL;
6873                 if (gl_lightmaps.integer < 2)
6874                         t->backgroundnmaptexture = r_texture_blanknormalmap;
6875                 t->backgroundglosstexture = r_texture_black;
6876                 t->backgroundglowtexture = NULL;
6877                 specularscale = 0;
6878                 t->currentmaterialflags = MATERIALFLAG_WALL | (t->currentmaterialflags & (MATERIALFLAG_NOCULLFACE | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SHORTDEPTHRANGE));
6879         }
6880
6881         if (specularscale != 1.0f)
6882         {
6883                 for (q = 0; q < 3; q++)
6884                 {
6885                         t->render_modellight_specular[q] *= specularscale;
6886                         t->render_lightmap_specular[q] *= specularscale;
6887                         t->render_rtlight_specular[q] *= specularscale;
6888                 }
6889         }
6890
6891         t->currentblendfunc[0] = GL_ONE;
6892         t->currentblendfunc[1] = GL_ZERO;
6893         if (t->currentmaterialflags & MATERIALFLAG_ADD)
6894         {
6895                 t->currentblendfunc[0] = GL_SRC_ALPHA;
6896                 t->currentblendfunc[1] = GL_ONE;
6897         }
6898         else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
6899         {
6900                 t->currentblendfunc[0] = GL_SRC_ALPHA;
6901                 t->currentblendfunc[1] = GL_ONE_MINUS_SRC_ALPHA;
6902         }
6903         else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
6904         {
6905                 t->currentblendfunc[0] = t->customblendfunc[0];
6906                 t->currentblendfunc[1] = t->customblendfunc[1];
6907         }
6908
6909         return t;
6910 }
6911
6912 rsurfacestate_t rsurface;
6913
6914 void RSurf_ActiveModelEntity(const entity_render_t *ent, qbool wantnormals, qbool wanttangents, qbool prepass)
6915 {
6916         model_t *model = ent->model;
6917         //if (rsurface.entity == ent && (!model->surfmesh.isanimated || (!wantnormals && !wanttangents)))
6918         //      return;
6919         rsurface.entity = (entity_render_t *)ent;
6920         rsurface.skeleton = ent->skeleton;
6921         memcpy(rsurface.userwavefunc_param, ent->userwavefunc_param, sizeof(rsurface.userwavefunc_param));
6922         rsurface.ent_skinnum = ent->skinnum;
6923         rsurface.ent_qwskin = (ent->entitynumber <= cl.maxclients && ent->entitynumber >= 1 && cls.protocol == PROTOCOL_QUAKEWORLD && cl.scores[ent->entitynumber - 1].qw_skin[0] && !strcmp(ent->model->name, "progs/player.mdl")) ? (ent->entitynumber - 1) : -1;
6924         rsurface.ent_flags = ent->flags;
6925         if (r_fullbright_directed.integer && (r_fullbright.integer || !model->lit))
6926                 rsurface.ent_flags |= RENDER_LIGHT | RENDER_DYNAMICMODELLIGHT;
6927         rsurface.shadertime = r_refdef.scene.time - ent->shadertime;
6928         rsurface.matrix = ent->matrix;
6929         rsurface.inversematrix = ent->inversematrix;
6930         rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
6931         rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
6932         R_EntityMatrix(&rsurface.matrix);
6933         Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
6934         Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
6935         rsurface.fogplaneviewdist = r_refdef.fogplaneviewdist * rsurface.inversematrixscale;
6936         rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
6937         rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
6938         rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
6939         memcpy(rsurface.frameblend, ent->frameblend, sizeof(ent->frameblend));
6940         rsurface.ent_alttextures = ent->framegroupblend[0].frame != 0;
6941         rsurface.basepolygonfactor = r_refdef.polygonfactor;
6942         rsurface.basepolygonoffset = r_refdef.polygonoffset;
6943         if (ent->model->brush.submodel && !prepass)
6944         {
6945                 rsurface.basepolygonfactor += r_polygonoffset_submodel_factor.value;
6946                 rsurface.basepolygonoffset += r_polygonoffset_submodel_offset.value;
6947         }
6948         // if the animcache code decided it should use the shader path, skip the deform step
6949         rsurface.entityskeletaltransform3x4 = ent->animcache_skeletaltransform3x4;
6950         rsurface.entityskeletaltransform3x4buffer = ent->animcache_skeletaltransform3x4buffer;
6951         rsurface.entityskeletaltransform3x4offset = ent->animcache_skeletaltransform3x4offset;
6952         rsurface.entityskeletaltransform3x4size = ent->animcache_skeletaltransform3x4size;
6953         rsurface.entityskeletalnumtransforms = rsurface.entityskeletaltransform3x4 ? model->num_bones : 0;
6954         if (model->surfmesh.isanimated && model->AnimateVertices && !rsurface.entityskeletaltransform3x4)
6955         {
6956                 if (ent->animcache_vertex3f)
6957                 {
6958                         r_refdef.stats[r_stat_batch_entitycache_count]++;
6959                         r_refdef.stats[r_stat_batch_entitycache_surfaces] += model->num_surfaces;
6960                         r_refdef.stats[r_stat_batch_entitycache_vertices] += model->surfmesh.num_vertices;
6961                         r_refdef.stats[r_stat_batch_entitycache_triangles] += model->surfmesh.num_triangles;
6962                         rsurface.modelvertex3f = ent->animcache_vertex3f;
6963                         rsurface.modelvertex3f_vertexbuffer = ent->animcache_vertex3f_vertexbuffer;
6964                         rsurface.modelvertex3f_bufferoffset = ent->animcache_vertex3f_bufferoffset;
6965                         rsurface.modelsvector3f = wanttangents ? ent->animcache_svector3f : NULL;
6966                         rsurface.modelsvector3f_vertexbuffer = wanttangents ? ent->animcache_svector3f_vertexbuffer : NULL;
6967                         rsurface.modelsvector3f_bufferoffset = wanttangents ? ent->animcache_svector3f_bufferoffset : 0;
6968                         rsurface.modeltvector3f = wanttangents ? ent->animcache_tvector3f : NULL;
6969                         rsurface.modeltvector3f_vertexbuffer = wanttangents ? ent->animcache_tvector3f_vertexbuffer : NULL;
6970                         rsurface.modeltvector3f_bufferoffset = wanttangents ? ent->animcache_tvector3f_bufferoffset : 0;
6971                         rsurface.modelnormal3f = wantnormals ? ent->animcache_normal3f : NULL;
6972                         rsurface.modelnormal3f_vertexbuffer = wantnormals ? ent->animcache_normal3f_vertexbuffer : NULL;
6973                         rsurface.modelnormal3f_bufferoffset = wantnormals ? ent->animcache_normal3f_bufferoffset : 0;
6974                 }
6975                 else if (wanttangents)
6976                 {
6977                         r_refdef.stats[r_stat_batch_entityanimate_count]++;
6978                         r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
6979                         r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
6980                         r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
6981                         rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6982                         rsurface.modelsvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6983                         rsurface.modeltvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6984                         rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6985                         model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, rsurface.modelsvector3f, rsurface.modeltvector3f);
6986                         rsurface.modelvertex3f_vertexbuffer = NULL;
6987                         rsurface.modelvertex3f_bufferoffset = 0;
6988                         rsurface.modelvertex3f_vertexbuffer = 0;
6989                         rsurface.modelvertex3f_bufferoffset = 0;
6990                         rsurface.modelsvector3f_vertexbuffer = 0;
6991                         rsurface.modelsvector3f_bufferoffset = 0;
6992                         rsurface.modeltvector3f_vertexbuffer = 0;
6993                         rsurface.modeltvector3f_bufferoffset = 0;
6994                         rsurface.modelnormal3f_vertexbuffer = 0;
6995                         rsurface.modelnormal3f_bufferoffset = 0;
6996                 }
6997                 else if (wantnormals)
6998                 {
6999                         r_refdef.stats[r_stat_batch_entityanimate_count]++;
7000                         r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
7001                         r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
7002                         r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
7003                         rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7004                         rsurface.modelsvector3f = NULL;
7005                         rsurface.modeltvector3f = NULL;
7006                         rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7007                         model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, NULL, NULL);
7008                         rsurface.modelvertex3f_vertexbuffer = NULL;
7009                         rsurface.modelvertex3f_bufferoffset = 0;
7010                         rsurface.modelvertex3f_vertexbuffer = 0;
7011                         rsurface.modelvertex3f_bufferoffset = 0;
7012                         rsurface.modelsvector3f_vertexbuffer = 0;
7013                         rsurface.modelsvector3f_bufferoffset = 0;
7014                         rsurface.modeltvector3f_vertexbuffer = 0;
7015                         rsurface.modeltvector3f_bufferoffset = 0;
7016                         rsurface.modelnormal3f_vertexbuffer = 0;
7017                         rsurface.modelnormal3f_bufferoffset = 0;
7018                 }
7019                 else
7020                 {
7021                         r_refdef.stats[r_stat_batch_entityanimate_count]++;
7022                         r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
7023                         r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
7024                         r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
7025                         rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7026                         rsurface.modelsvector3f = NULL;
7027                         rsurface.modeltvector3f = NULL;
7028                         rsurface.modelnormal3f = NULL;
7029                         model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, NULL, NULL, NULL);
7030                         rsurface.modelvertex3f_vertexbuffer = NULL;
7031                         rsurface.modelvertex3f_bufferoffset = 0;
7032                         rsurface.modelvertex3f_vertexbuffer = 0;
7033                         rsurface.modelvertex3f_bufferoffset = 0;
7034                         rsurface.modelsvector3f_vertexbuffer = 0;
7035                         rsurface.modelsvector3f_bufferoffset = 0;
7036                         rsurface.modeltvector3f_vertexbuffer = 0;
7037                         rsurface.modeltvector3f_bufferoffset = 0;
7038                         rsurface.modelnormal3f_vertexbuffer = 0;
7039                         rsurface.modelnormal3f_bufferoffset = 0;
7040                 }
7041                 rsurface.modelgeneratedvertex = true;
7042         }
7043         else
7044         {
7045                 if (rsurface.entityskeletaltransform3x4)
7046                 {
7047                         r_refdef.stats[r_stat_batch_entityskeletal_count]++;
7048                         r_refdef.stats[r_stat_batch_entityskeletal_surfaces] += model->num_surfaces;
7049                         r_refdef.stats[r_stat_batch_entityskeletal_vertices] += model->surfmesh.num_vertices;
7050                         r_refdef.stats[r_stat_batch_entityskeletal_triangles] += model->surfmesh.num_triangles;
7051                 }
7052                 else
7053                 {
7054                         r_refdef.stats[r_stat_batch_entitystatic_count]++;
7055                         r_refdef.stats[r_stat_batch_entitystatic_surfaces] += model->num_surfaces;
7056                         r_refdef.stats[r_stat_batch_entitystatic_vertices] += model->surfmesh.num_vertices;
7057                         r_refdef.stats[r_stat_batch_entitystatic_triangles] += model->surfmesh.num_triangles;
7058                 }
7059                 rsurface.modelvertex3f  = model->surfmesh.data_vertex3f;
7060                 rsurface.modelvertex3f_vertexbuffer = model->surfmesh.data_vertex3f_vertexbuffer;
7061                 rsurface.modelvertex3f_bufferoffset = model->surfmesh.data_vertex3f_bufferoffset;
7062                 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
7063                 rsurface.modelsvector3f_vertexbuffer = model->surfmesh.data_svector3f_vertexbuffer;
7064                 rsurface.modelsvector3f_bufferoffset = model->surfmesh.data_svector3f_bufferoffset;
7065                 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
7066                 rsurface.modeltvector3f_vertexbuffer = model->surfmesh.data_tvector3f_vertexbuffer;
7067                 rsurface.modeltvector3f_bufferoffset = model->surfmesh.data_tvector3f_bufferoffset;
7068                 rsurface.modelnormal3f  = model->surfmesh.data_normal3f;
7069                 rsurface.modelnormal3f_vertexbuffer = model->surfmesh.data_normal3f_vertexbuffer;
7070                 rsurface.modelnormal3f_bufferoffset = model->surfmesh.data_normal3f_bufferoffset;
7071                 rsurface.modelgeneratedvertex = false;
7072         }
7073         rsurface.modellightmapcolor4f  = model->surfmesh.data_lightmapcolor4f;
7074         rsurface.modellightmapcolor4f_vertexbuffer = model->surfmesh.data_lightmapcolor4f_vertexbuffer;
7075         rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.data_lightmapcolor4f_bufferoffset;
7076         rsurface.modeltexcoordtexture2f  = model->surfmesh.data_texcoordtexture2f;
7077         rsurface.modeltexcoordtexture2f_vertexbuffer = model->surfmesh.data_texcoordtexture2f_vertexbuffer;
7078         rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.data_texcoordtexture2f_bufferoffset;
7079         rsurface.modeltexcoordlightmap2f  = model->surfmesh.data_texcoordlightmap2f;
7080         rsurface.modeltexcoordlightmap2f_vertexbuffer = model->surfmesh.data_texcoordlightmap2f_vertexbuffer;
7081         rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.data_texcoordlightmap2f_bufferoffset;
7082         rsurface.modelskeletalindex4ub = model->surfmesh.data_skeletalindex4ub;
7083         rsurface.modelskeletalindex4ub_vertexbuffer = model->surfmesh.data_skeletalindex4ub_vertexbuffer;
7084         rsurface.modelskeletalindex4ub_bufferoffset = model->surfmesh.data_skeletalindex4ub_bufferoffset;
7085         rsurface.modelskeletalweight4ub = model->surfmesh.data_skeletalweight4ub;
7086         rsurface.modelskeletalweight4ub_vertexbuffer = model->surfmesh.data_skeletalweight4ub_vertexbuffer;
7087         rsurface.modelskeletalweight4ub_bufferoffset = model->surfmesh.data_skeletalweight4ub_bufferoffset;
7088         rsurface.modelelement3i = model->surfmesh.data_element3i;
7089         rsurface.modelelement3i_indexbuffer = model->surfmesh.data_element3i_indexbuffer;
7090         rsurface.modelelement3i_bufferoffset = model->surfmesh.data_element3i_bufferoffset;
7091         rsurface.modelelement3s = model->surfmesh.data_element3s;
7092         rsurface.modelelement3s_indexbuffer = model->surfmesh.data_element3s_indexbuffer;
7093         rsurface.modelelement3s_bufferoffset = model->surfmesh.data_element3s_bufferoffset;
7094         rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
7095         rsurface.modelnumvertices = model->surfmesh.num_vertices;
7096         rsurface.modelnumtriangles = model->surfmesh.num_triangles;
7097         rsurface.modelsurfaces = model->data_surfaces;
7098         rsurface.batchgeneratedvertex = false;
7099         rsurface.batchfirstvertex = 0;
7100         rsurface.batchnumvertices = 0;
7101         rsurface.batchfirsttriangle = 0;
7102         rsurface.batchnumtriangles = 0;
7103         rsurface.batchvertex3f  = NULL;
7104         rsurface.batchvertex3f_vertexbuffer = NULL;
7105         rsurface.batchvertex3f_bufferoffset = 0;
7106         rsurface.batchsvector3f = NULL;
7107         rsurface.batchsvector3f_vertexbuffer = NULL;
7108         rsurface.batchsvector3f_bufferoffset = 0;
7109         rsurface.batchtvector3f = NULL;
7110         rsurface.batchtvector3f_vertexbuffer = NULL;
7111         rsurface.batchtvector3f_bufferoffset = 0;
7112         rsurface.batchnormal3f  = NULL;
7113         rsurface.batchnormal3f_vertexbuffer = NULL;
7114         rsurface.batchnormal3f_bufferoffset = 0;
7115         rsurface.batchlightmapcolor4f = NULL;
7116         rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7117         rsurface.batchlightmapcolor4f_bufferoffset = 0;
7118         rsurface.batchtexcoordtexture2f = NULL;
7119         rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7120         rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7121         rsurface.batchtexcoordlightmap2f = NULL;
7122         rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7123         rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7124         rsurface.batchskeletalindex4ub = NULL;
7125         rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7126         rsurface.batchskeletalindex4ub_bufferoffset = 0;
7127         rsurface.batchskeletalweight4ub = NULL;
7128         rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7129         rsurface.batchskeletalweight4ub_bufferoffset = 0;
7130         rsurface.batchelement3i = NULL;
7131         rsurface.batchelement3i_indexbuffer = NULL;
7132         rsurface.batchelement3i_bufferoffset = 0;
7133         rsurface.batchelement3s = NULL;
7134         rsurface.batchelement3s_indexbuffer = NULL;
7135         rsurface.batchelement3s_bufferoffset = 0;
7136         rsurface.forcecurrenttextureupdate = false;
7137 }
7138
7139 void RSurf_ActiveCustomEntity(const matrix4x4_t *matrix, const matrix4x4_t *inversematrix, int entflags, double shadertime, float r, float g, float b, float a, int numvertices, const float *vertex3f, const float *texcoord2f, const float *normal3f, const float *svector3f, const float *tvector3f, const float *color4f, int numtriangles, const int *element3i, const unsigned short *element3s, qbool wantnormals, qbool wanttangents)
7140 {
7141         rsurface.entity = r_refdef.scene.worldentity;
7142         if (r != 1.0f || g != 1.0f || b != 1.0f || a != 1.0f) {
7143                 // HACK to provide a valid entity with modded colors to R_GetCurrentTexture.
7144                 // A better approach could be making this copy only once per frame.
7145                 static entity_render_t custom_entity;
7146                 int q;
7147                 custom_entity = *rsurface.entity;
7148                 for (q = 0; q < 3; ++q) {
7149                         float colormod = q == 0 ? r : q == 1 ? g : b;
7150                         custom_entity.render_fullbright[q] *= colormod;
7151                         custom_entity.render_modellight_ambient[q] *= colormod;
7152                         custom_entity.render_modellight_diffuse[q] *= colormod;
7153                         custom_entity.render_lightmap_ambient[q] *= colormod;
7154                         custom_entity.render_lightmap_diffuse[q] *= colormod;
7155                         custom_entity.render_rtlight_diffuse[q] *= colormod;
7156                 }
7157                 custom_entity.alpha *= a;
7158                 rsurface.entity = &custom_entity;
7159         }
7160         rsurface.skeleton = NULL;
7161         rsurface.ent_skinnum = 0;
7162         rsurface.ent_qwskin = -1;
7163         rsurface.ent_flags = entflags;
7164         rsurface.shadertime = r_refdef.scene.time - shadertime;
7165         rsurface.modelnumvertices = numvertices;
7166         rsurface.modelnumtriangles = numtriangles;
7167         rsurface.matrix = *matrix;
7168         rsurface.inversematrix = *inversematrix;
7169         rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
7170         rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
7171         R_EntityMatrix(&rsurface.matrix);
7172         Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
7173         Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
7174         rsurface.fogplaneviewdist *= rsurface.inversematrixscale;
7175         rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
7176         rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
7177         rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
7178         memset(rsurface.frameblend, 0, sizeof(rsurface.frameblend));
7179         rsurface.frameblend[0].lerp = 1;
7180         rsurface.ent_alttextures = false;
7181         rsurface.basepolygonfactor = r_refdef.polygonfactor;
7182         rsurface.basepolygonoffset = r_refdef.polygonoffset;
7183         rsurface.entityskeletaltransform3x4 = NULL;
7184         rsurface.entityskeletaltransform3x4buffer = NULL;
7185         rsurface.entityskeletaltransform3x4offset = 0;
7186         rsurface.entityskeletaltransform3x4size = 0;
7187         rsurface.entityskeletalnumtransforms = 0;
7188         r_refdef.stats[r_stat_batch_entitycustom_count]++;
7189         r_refdef.stats[r_stat_batch_entitycustom_surfaces] += 1;
7190         r_refdef.stats[r_stat_batch_entitycustom_vertices] += rsurface.modelnumvertices;
7191         r_refdef.stats[r_stat_batch_entitycustom_triangles] += rsurface.modelnumtriangles;
7192         if (wanttangents)
7193         {
7194                 rsurface.modelvertex3f = (float *)vertex3f;
7195                 rsurface.modelsvector3f = svector3f ? (float *)svector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7196                 rsurface.modeltvector3f = tvector3f ? (float *)tvector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7197                 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7198         }
7199         else if (wantnormals)
7200         {
7201                 rsurface.modelvertex3f = (float *)vertex3f;
7202                 rsurface.modelsvector3f = NULL;
7203                 rsurface.modeltvector3f = NULL;
7204                 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7205         }
7206         else
7207         {
7208                 rsurface.modelvertex3f = (float *)vertex3f;
7209                 rsurface.modelsvector3f = NULL;
7210                 rsurface.modeltvector3f = NULL;
7211                 rsurface.modelnormal3f = NULL;
7212         }
7213         rsurface.modelvertex3f_vertexbuffer = 0;
7214         rsurface.modelvertex3f_bufferoffset = 0;
7215         rsurface.modelsvector3f_vertexbuffer = 0;
7216         rsurface.modelsvector3f_bufferoffset = 0;
7217         rsurface.modeltvector3f_vertexbuffer = 0;
7218         rsurface.modeltvector3f_bufferoffset = 0;
7219         rsurface.modelnormal3f_vertexbuffer = 0;
7220         rsurface.modelnormal3f_bufferoffset = 0;
7221         rsurface.modelgeneratedvertex = true;
7222         rsurface.modellightmapcolor4f  = (float *)color4f;
7223         rsurface.modellightmapcolor4f_vertexbuffer = 0;
7224         rsurface.modellightmapcolor4f_bufferoffset = 0;
7225         rsurface.modeltexcoordtexture2f  = (float *)texcoord2f;
7226         rsurface.modeltexcoordtexture2f_vertexbuffer = 0;
7227         rsurface.modeltexcoordtexture2f_bufferoffset = 0;
7228         rsurface.modeltexcoordlightmap2f  = NULL;
7229         rsurface.modeltexcoordlightmap2f_vertexbuffer = 0;
7230         rsurface.modeltexcoordlightmap2f_bufferoffset = 0;
7231         rsurface.modelskeletalindex4ub = NULL;
7232         rsurface.modelskeletalindex4ub_vertexbuffer = NULL;
7233         rsurface.modelskeletalindex4ub_bufferoffset = 0;
7234         rsurface.modelskeletalweight4ub = NULL;
7235         rsurface.modelskeletalweight4ub_vertexbuffer = NULL;
7236         rsurface.modelskeletalweight4ub_bufferoffset = 0;
7237         rsurface.modelelement3i = (int *)element3i;
7238         rsurface.modelelement3i_indexbuffer = NULL;
7239         rsurface.modelelement3i_bufferoffset = 0;
7240         rsurface.modelelement3s = (unsigned short *)element3s;
7241         rsurface.modelelement3s_indexbuffer = NULL;
7242         rsurface.modelelement3s_bufferoffset = 0;
7243         rsurface.modellightmapoffsets = NULL;
7244         rsurface.modelsurfaces = NULL;
7245         rsurface.batchgeneratedvertex = false;
7246         rsurface.batchfirstvertex = 0;
7247         rsurface.batchnumvertices = 0;
7248         rsurface.batchfirsttriangle = 0;
7249         rsurface.batchnumtriangles = 0;
7250         rsurface.batchvertex3f  = NULL;
7251         rsurface.batchvertex3f_vertexbuffer = NULL;
7252         rsurface.batchvertex3f_bufferoffset = 0;
7253         rsurface.batchsvector3f = NULL;
7254         rsurface.batchsvector3f_vertexbuffer = NULL;
7255         rsurface.batchsvector3f_bufferoffset = 0;
7256         rsurface.batchtvector3f = NULL;
7257         rsurface.batchtvector3f_vertexbuffer = NULL;
7258         rsurface.batchtvector3f_bufferoffset = 0;
7259         rsurface.batchnormal3f  = NULL;
7260         rsurface.batchnormal3f_vertexbuffer = NULL;
7261         rsurface.batchnormal3f_bufferoffset = 0;
7262         rsurface.batchlightmapcolor4f = NULL;
7263         rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7264         rsurface.batchlightmapcolor4f_bufferoffset = 0;
7265         rsurface.batchtexcoordtexture2f = NULL;
7266         rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7267         rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7268         rsurface.batchtexcoordlightmap2f = NULL;
7269         rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7270         rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7271         rsurface.batchskeletalindex4ub = NULL;
7272         rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7273         rsurface.batchskeletalindex4ub_bufferoffset = 0;
7274         rsurface.batchskeletalweight4ub = NULL;
7275         rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7276         rsurface.batchskeletalweight4ub_bufferoffset = 0;
7277         rsurface.batchelement3i = NULL;
7278         rsurface.batchelement3i_indexbuffer = NULL;
7279         rsurface.batchelement3i_bufferoffset = 0;
7280         rsurface.batchelement3s = NULL;
7281         rsurface.batchelement3s_indexbuffer = NULL;
7282         rsurface.batchelement3s_bufferoffset = 0;
7283         rsurface.forcecurrenttextureupdate = true;
7284
7285         if (rsurface.modelnumvertices && rsurface.modelelement3i)
7286         {
7287                 if ((wantnormals || wanttangents) && !normal3f)
7288                 {
7289                         rsurface.modelnormal3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7290                         Mod_BuildNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.modelnormal3f, r_smoothnormals_areaweighting.integer != 0);
7291                 }
7292                 if (wanttangents && !svector3f)
7293                 {
7294                         rsurface.modelsvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7295                         rsurface.modeltvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7296                         Mod_BuildTextureVectorsFromNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modeltexcoordtexture2f, rsurface.modelnormal3f, rsurface.modelelement3i, rsurface.modelsvector3f, rsurface.modeltvector3f, r_smoothnormals_areaweighting.integer != 0);
7297                 }
7298         }
7299 }
7300
7301 float RSurf_FogPoint(const float *v)
7302 {
7303         // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
7304         float FogPlaneViewDist = r_refdef.fogplaneviewdist;
7305         float FogPlaneVertexDist = DotProduct(r_refdef.fogplane, v) + r_refdef.fogplane[3];
7306         float FogHeightFade = r_refdef.fogheightfade;
7307         float fogfrac;
7308         unsigned int fogmasktableindex;
7309         if (r_refdef.fogplaneviewabove)
7310                 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
7311         else
7312                 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
7313         fogmasktableindex = (unsigned int)(VectorDistance(r_refdef.view.origin, v) * fogfrac * r_refdef.fogmasktabledistmultiplier);
7314         return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
7315 }
7316
7317 float RSurf_FogVertex(const float *v)
7318 {
7319         // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
7320         float FogPlaneViewDist = rsurface.fogplaneviewdist;
7321         float FogPlaneVertexDist = DotProduct(rsurface.fogplane, v) + rsurface.fogplane[3];
7322         float FogHeightFade = rsurface.fogheightfade;
7323         float fogfrac;
7324         unsigned int fogmasktableindex;
7325         if (r_refdef.fogplaneviewabove)
7326                 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
7327         else
7328                 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
7329         fogmasktableindex = (unsigned int)(VectorDistance(rsurface.localvieworigin, v) * fogfrac * rsurface.fogmasktabledistmultiplier);
7330         return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
7331 }
7332
7333 void RSurf_UploadBuffersForBatch(void)
7334 {
7335         // upload buffer data for generated vertex data (dynamicvertex case) or index data (copytriangles case) and models that lack it to begin with (e.g. DrawQ_FlushUI)
7336         // note that if rsurface.batchvertex3f_vertexbuffer is NULL, dynamicvertex is forced as we don't account for the proper base vertex here.
7337         if (rsurface.batchvertex3f && !rsurface.batchvertex3f_vertexbuffer)
7338                 rsurface.batchvertex3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f, R_BUFFERDATA_VERTEX, &rsurface.batchvertex3f_bufferoffset);
7339         if (rsurface.batchsvector3f && !rsurface.batchsvector3f_vertexbuffer)
7340                 rsurface.batchsvector3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchsvector3f, R_BUFFERDATA_VERTEX, &rsurface.batchsvector3f_bufferoffset);
7341         if (rsurface.batchtvector3f && !rsurface.batchtvector3f_vertexbuffer)
7342                 rsurface.batchtvector3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchtvector3f, R_BUFFERDATA_VERTEX, &rsurface.batchtvector3f_bufferoffset);
7343         if (rsurface.batchnormal3f && !rsurface.batchnormal3f_vertexbuffer)
7344                 rsurface.batchnormal3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f, R_BUFFERDATA_VERTEX, &rsurface.batchnormal3f_bufferoffset);
7345         if (rsurface.batchlightmapcolor4f && !rsurface.batchlightmapcolor4f_vertexbuffer)
7346                 rsurface.batchlightmapcolor4f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[4]), rsurface.batchlightmapcolor4f, R_BUFFERDATA_VERTEX, &rsurface.batchlightmapcolor4f_bufferoffset);
7347         if (rsurface.batchtexcoordtexture2f && !rsurface.batchtexcoordtexture2f_vertexbuffer)
7348                 rsurface.batchtexcoordtexture2f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[2]), rsurface.batchtexcoordtexture2f, R_BUFFERDATA_VERTEX, &rsurface.batchtexcoordtexture2f_bufferoffset);
7349         if (rsurface.batchtexcoordlightmap2f && !rsurface.batchtexcoordlightmap2f_vertexbuffer)
7350                 rsurface.batchtexcoordlightmap2f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[2]), rsurface.batchtexcoordlightmap2f, R_BUFFERDATA_VERTEX, &rsurface.batchtexcoordlightmap2f_bufferoffset);
7351         if (rsurface.batchskeletalindex4ub && !rsurface.batchskeletalindex4ub_vertexbuffer)
7352                 rsurface.batchskeletalindex4ub_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(unsigned char[4]), rsurface.batchskeletalindex4ub, R_BUFFERDATA_VERTEX, &rsurface.batchskeletalindex4ub_bufferoffset);
7353         if (rsurface.batchskeletalweight4ub && !rsurface.batchskeletalweight4ub_vertexbuffer)
7354                 rsurface.batchskeletalweight4ub_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(unsigned char[4]), rsurface.batchskeletalweight4ub, R_BUFFERDATA_VERTEX, &rsurface.batchskeletalweight4ub_bufferoffset);
7355
7356         if (rsurface.batchelement3s && !rsurface.batchelement3s_indexbuffer)
7357                 rsurface.batchelement3s_indexbuffer = R_BufferData_Store(rsurface.batchnumtriangles * sizeof(short[3]), rsurface.batchelement3s, R_BUFFERDATA_INDEX16, &rsurface.batchelement3s_bufferoffset);
7358         else if (rsurface.batchelement3i && !rsurface.batchelement3i_indexbuffer)
7359                 rsurface.batchelement3i_indexbuffer = R_BufferData_Store(rsurface.batchnumtriangles * sizeof(int[3]), rsurface.batchelement3i, R_BUFFERDATA_INDEX32, &rsurface.batchelement3i_bufferoffset);
7360
7361         R_Mesh_VertexPointer(     3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
7362         R_Mesh_ColorPointer(      4, GL_FLOAT, sizeof(float[4]), rsurface.batchlightmapcolor4f, rsurface.batchlightmapcolor4f_vertexbuffer, rsurface.batchlightmapcolor4f_bufferoffset);
7363         R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
7364         R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchsvector3f, rsurface.batchsvector3f_vertexbuffer, rsurface.batchsvector3f_bufferoffset);
7365         R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchtvector3f, rsurface.batchtvector3f_vertexbuffer, rsurface.batchtvector3f_bufferoffset);
7366         R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchnormal3f, rsurface.batchnormal3f_vertexbuffer, rsurface.batchnormal3f_bufferoffset);
7367         R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordlightmap2f, rsurface.batchtexcoordlightmap2f_vertexbuffer, rsurface.batchtexcoordlightmap2f_bufferoffset);
7368         R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
7369         R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(unsigned char[4]), rsurface.batchskeletalindex4ub, rsurface.batchskeletalindex4ub_vertexbuffer, rsurface.batchskeletalindex4ub_bufferoffset);
7370         R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), rsurface.batchskeletalweight4ub, rsurface.batchskeletalweight4ub_vertexbuffer, rsurface.batchskeletalweight4ub_bufferoffset);
7371 }
7372
7373 static void RSurf_RenumberElements(const int *inelement3i, int *outelement3i, int numelements, int adjust)
7374 {
7375         int i;
7376         for (i = 0;i < numelements;i++)
7377                 outelement3i[i] = inelement3i[i] + adjust;
7378 }
7379
7380 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
7381 void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const msurface_t **texturesurfacelist)
7382 {
7383         int deformindex;
7384         int firsttriangle;
7385         int numtriangles;
7386         int firstvertex;
7387         int endvertex;
7388         int numvertices;
7389         int surfacefirsttriangle;
7390         int surfacenumtriangles;
7391         int surfacefirstvertex;
7392         int surfaceendvertex;
7393         int surfacenumvertices;
7394         int batchnumsurfaces = texturenumsurfaces;
7395         int batchnumvertices;
7396         int batchnumtriangles;
7397         int i, j;
7398         qbool gaps;
7399         qbool dynamicvertex;
7400         float amplitude;
7401         float animpos;
7402         float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
7403         float waveparms[4];
7404         unsigned char *ub;
7405         q3shaderinfo_deform_t *deform;
7406         const msurface_t *surface, *firstsurface;
7407         if (!texturenumsurfaces)
7408                 return;
7409         // find vertex range of this surface batch
7410         gaps = false;
7411         firstsurface = texturesurfacelist[0];
7412         firsttriangle = firstsurface->num_firsttriangle;
7413         batchnumvertices = 0;
7414         batchnumtriangles = 0;
7415         firstvertex = endvertex = firstsurface->num_firstvertex;
7416         for (i = 0;i < texturenumsurfaces;i++)
7417         {
7418                 surface = texturesurfacelist[i];
7419                 if (surface != firstsurface + i)
7420                         gaps = true;
7421                 surfacefirstvertex = surface->num_firstvertex;
7422                 surfaceendvertex = surfacefirstvertex + surface->num_vertices;
7423                 surfacenumvertices = surface->num_vertices;
7424                 surfacenumtriangles = surface->num_triangles;
7425                 if (firstvertex > surfacefirstvertex)
7426                         firstvertex = surfacefirstvertex;
7427                 if (endvertex < surfaceendvertex)
7428                         endvertex = surfaceendvertex;
7429                 batchnumvertices += surfacenumvertices;
7430                 batchnumtriangles += surfacenumtriangles;
7431         }
7432
7433         r_refdef.stats[r_stat_batch_batches]++;
7434         if (gaps)
7435                 r_refdef.stats[r_stat_batch_withgaps]++;
7436         r_refdef.stats[r_stat_batch_surfaces] += batchnumsurfaces;
7437         r_refdef.stats[r_stat_batch_vertices] += batchnumvertices;
7438         r_refdef.stats[r_stat_batch_triangles] += batchnumtriangles;
7439
7440         // we now know the vertex range used, and if there are any gaps in it
7441         rsurface.batchfirstvertex = firstvertex;
7442         rsurface.batchnumvertices = endvertex - firstvertex;
7443         rsurface.batchfirsttriangle = firsttriangle;
7444         rsurface.batchnumtriangles = batchnumtriangles;
7445
7446         // check if any dynamic vertex processing must occur
7447         dynamicvertex = false;
7448
7449         // we must use vertexbuffers for rendering, we can upload vertex buffers
7450         // easily enough but if the basevertex is non-zero it becomes more
7451         // difficult, so force dynamicvertex path in that case - it's suboptimal
7452         // but the most optimal case is to have the geometry sources provide their
7453         // own anyway.
7454         if (!rsurface.modelvertex3f_vertexbuffer && firstvertex != 0)
7455                 dynamicvertex = true;
7456
7457         // a cvar to force the dynamic vertex path to be taken, for debugging
7458         if (r_batch_debugdynamicvertexpath.integer)
7459         {
7460                 if (!dynamicvertex)
7461                 {
7462                         r_refdef.stats[r_stat_batch_dynamic_batches_because_cvar] += 1;
7463                         r_refdef.stats[r_stat_batch_dynamic_surfaces_because_cvar] += batchnumsurfaces;
7464                         r_refdef.stats[r_stat_batch_dynamic_vertices_because_cvar] += batchnumvertices;
7465                         r_refdef.stats[r_stat_batch_dynamic_triangles_because_cvar] += batchnumtriangles;
7466                 }
7467                 dynamicvertex = true;
7468         }
7469
7470         // if there is a chance of animated vertex colors, it's a dynamic batch
7471         if ((batchneed & BATCHNEED_ARRAY_VERTEXCOLOR) && texturesurfacelist[0]->lightmapinfo)
7472         {
7473                 if (!dynamicvertex)
7474                 {
7475                         r_refdef.stats[r_stat_batch_dynamic_batches_because_lightmapvertex] += 1;
7476                         r_refdef.stats[r_stat_batch_dynamic_surfaces_because_lightmapvertex] += batchnumsurfaces;
7477                         r_refdef.stats[r_stat_batch_dynamic_vertices_because_lightmapvertex] += batchnumvertices;
7478                         r_refdef.stats[r_stat_batch_dynamic_triangles_because_lightmapvertex] += batchnumtriangles;
7479                 }
7480                 dynamicvertex = true;
7481         }
7482
7483         for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
7484         {
7485                 switch (deform->deform)
7486                 {
7487                 default:
7488                 case Q3DEFORM_PROJECTIONSHADOW:
7489                 case Q3DEFORM_TEXT0:
7490                 case Q3DEFORM_TEXT1:
7491                 case Q3DEFORM_TEXT2:
7492                 case Q3DEFORM_TEXT3:
7493                 case Q3DEFORM_TEXT4:
7494                 case Q3DEFORM_TEXT5:
7495                 case Q3DEFORM_TEXT6:
7496                 case Q3DEFORM_TEXT7:
7497                 case Q3DEFORM_NONE:
7498                         break;
7499                 case Q3DEFORM_AUTOSPRITE:
7500                         if (!dynamicvertex)
7501                         {
7502                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_autosprite] += 1;
7503                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_autosprite] += batchnumsurfaces;
7504                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_autosprite] += batchnumvertices;
7505                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_autosprite] += batchnumtriangles;
7506                         }
7507                         dynamicvertex = true;
7508                         batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_TEXCOORD;
7509                         break;
7510                 case Q3DEFORM_AUTOSPRITE2:
7511                         if (!dynamicvertex)
7512                         {
7513                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_autosprite2] += 1;
7514                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_autosprite2] += batchnumsurfaces;
7515                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_autosprite2] += batchnumvertices;
7516                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_autosprite2] += batchnumtriangles;
7517                         }
7518                         dynamicvertex = true;
7519                         batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD;
7520                         break;
7521                 case Q3DEFORM_NORMAL:
7522                         if (!dynamicvertex)
7523                         {
7524                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_normal] += 1;
7525                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_normal] += batchnumsurfaces;
7526                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_normal] += batchnumvertices;
7527                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_normal] += batchnumtriangles;
7528                         }
7529                         dynamicvertex = true;
7530                         batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7531                         break;
7532                 case Q3DEFORM_WAVE:
7533                         if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
7534                                 break; // if wavefunc is a nop, ignore this transform
7535                         if (!dynamicvertex)
7536                         {
7537                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_wave] += 1;
7538                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_wave] += batchnumsurfaces;
7539                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_wave] += batchnumvertices;
7540                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_wave] += batchnumtriangles;
7541                         }
7542                         dynamicvertex = true;
7543                         batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7544                         break;
7545                 case Q3DEFORM_BULGE:
7546                         if (!dynamicvertex)
7547                         {
7548                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_bulge] += 1;
7549                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_bulge] += batchnumsurfaces;
7550                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_bulge] += batchnumvertices;
7551                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_bulge] += batchnumtriangles;
7552                         }
7553                         dynamicvertex = true;
7554                         batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7555                         break;
7556                 case Q3DEFORM_MOVE:
7557                         if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
7558                                 break; // if wavefunc is a nop, ignore this transform
7559                         if (!dynamicvertex)
7560                         {
7561                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_move] += 1;
7562                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_move] += batchnumsurfaces;
7563                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_move] += batchnumvertices;
7564                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_move] += batchnumtriangles;
7565                         }
7566                         dynamicvertex = true;
7567                         batchneed |= BATCHNEED_ARRAY_VERTEX;
7568                         break;
7569                 }
7570         }
7571         if (rsurface.texture->materialshaderpass)
7572         {
7573                 switch (rsurface.texture->materialshaderpass->tcgen.tcgen)
7574                 {
7575                 default:
7576                 case Q3TCGEN_TEXTURE:
7577                         break;
7578                 case Q3TCGEN_LIGHTMAP:
7579                         if (!dynamicvertex)
7580                         {
7581                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_lightmap] += 1;
7582                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_lightmap] += batchnumsurfaces;
7583                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_lightmap] += batchnumvertices;
7584                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_lightmap] += batchnumtriangles;
7585                         }
7586                         dynamicvertex = true;
7587                         batchneed |= BATCHNEED_ARRAY_LIGHTMAP;
7588                         break;
7589                 case Q3TCGEN_VECTOR:
7590                         if (!dynamicvertex)
7591                         {
7592                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_vector] += 1;
7593                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_vector] += batchnumsurfaces;
7594                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_vector] += batchnumvertices;
7595                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_vector] += batchnumtriangles;
7596                         }
7597                         dynamicvertex = true;
7598                         batchneed |= BATCHNEED_ARRAY_VERTEX;
7599                         break;
7600                 case Q3TCGEN_ENVIRONMENT:
7601                         if (!dynamicvertex)
7602                         {
7603                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_environment] += 1;
7604                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_environment] += batchnumsurfaces;
7605                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_environment] += batchnumvertices;
7606                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_environment] += batchnumtriangles;
7607                         }
7608                         dynamicvertex = true;
7609                         batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL;
7610                         break;
7611                 }
7612                 if (rsurface.texture->materialshaderpass->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
7613                 {
7614                         if (!dynamicvertex)
7615                         {
7616                                 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcmod_turbulent] += 1;
7617                                 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcmod_turbulent] += batchnumsurfaces;
7618                                 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcmod_turbulent] += batchnumvertices;
7619                                 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcmod_turbulent] += batchnumtriangles;
7620                         }
7621                         dynamicvertex = true;
7622                         batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD;
7623                 }
7624         }
7625
7626         // the caller can specify BATCHNEED_NOGAPS to force a batch with
7627         // firstvertex = 0 and endvertex = numvertices (no gaps, no firstvertex),
7628         // we ensure this by treating the vertex batch as dynamic...
7629         if ((batchneed & BATCHNEED_ALWAYSCOPY) || ((batchneed & BATCHNEED_NOGAPS) && (gaps || firstvertex > 0)))
7630         {
7631                 if (!dynamicvertex)
7632                 {
7633                         r_refdef.stats[r_stat_batch_dynamic_batches_because_nogaps] += 1;
7634                         r_refdef.stats[r_stat_batch_dynamic_surfaces_because_nogaps] += batchnumsurfaces;
7635                         r_refdef.stats[r_stat_batch_dynamic_vertices_because_nogaps] += batchnumvertices;
7636                         r_refdef.stats[r_stat_batch_dynamic_triangles_because_nogaps] += batchnumtriangles;
7637                 }
7638                 dynamicvertex = true;
7639         }
7640
7641         // if we're going to have to apply the skeletal transform manually, we need to batch the skeletal data
7642         if (dynamicvertex && rsurface.entityskeletaltransform3x4)
7643                 batchneed |= BATCHNEED_ARRAY_SKELETAL;
7644
7645         rsurface.batchvertex3f = rsurface.modelvertex3f;
7646         rsurface.batchvertex3f_vertexbuffer = rsurface.modelvertex3f_vertexbuffer;
7647         rsurface.batchvertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
7648         rsurface.batchsvector3f = rsurface.modelsvector3f;
7649         rsurface.batchsvector3f_vertexbuffer = rsurface.modelsvector3f_vertexbuffer;
7650         rsurface.batchsvector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
7651         rsurface.batchtvector3f = rsurface.modeltvector3f;
7652         rsurface.batchtvector3f_vertexbuffer = rsurface.modeltvector3f_vertexbuffer;
7653         rsurface.batchtvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
7654         rsurface.batchnormal3f = rsurface.modelnormal3f;
7655         rsurface.batchnormal3f_vertexbuffer = rsurface.modelnormal3f_vertexbuffer;
7656         rsurface.batchnormal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
7657         rsurface.batchlightmapcolor4f = rsurface.modellightmapcolor4f;
7658         rsurface.batchlightmapcolor4f_vertexbuffer  = rsurface.modellightmapcolor4f_vertexbuffer;
7659         rsurface.batchlightmapcolor4f_bufferoffset  = rsurface.modellightmapcolor4f_bufferoffset;
7660         rsurface.batchtexcoordtexture2f = rsurface.modeltexcoordtexture2f;
7661         rsurface.batchtexcoordtexture2f_vertexbuffer  = rsurface.modeltexcoordtexture2f_vertexbuffer;
7662         rsurface.batchtexcoordtexture2f_bufferoffset  = rsurface.modeltexcoordtexture2f_bufferoffset;
7663         rsurface.batchtexcoordlightmap2f = rsurface.modeltexcoordlightmap2f;
7664         rsurface.batchtexcoordlightmap2f_vertexbuffer = rsurface.modeltexcoordlightmap2f_vertexbuffer;
7665         rsurface.batchtexcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
7666         rsurface.batchskeletalindex4ub = rsurface.modelskeletalindex4ub;
7667         rsurface.batchskeletalindex4ub_vertexbuffer = rsurface.modelskeletalindex4ub_vertexbuffer;
7668         rsurface.batchskeletalindex4ub_bufferoffset = rsurface.modelskeletalindex4ub_bufferoffset;
7669         rsurface.batchskeletalweight4ub = rsurface.modelskeletalweight4ub;
7670         rsurface.batchskeletalweight4ub_vertexbuffer = rsurface.modelskeletalweight4ub_vertexbuffer;
7671         rsurface.batchskeletalweight4ub_bufferoffset = rsurface.modelskeletalweight4ub_bufferoffset;
7672         rsurface.batchelement3i = rsurface.modelelement3i;
7673         rsurface.batchelement3i_indexbuffer = rsurface.modelelement3i_indexbuffer;
7674         rsurface.batchelement3i_bufferoffset = rsurface.modelelement3i_bufferoffset;
7675         rsurface.batchelement3s = rsurface.modelelement3s;
7676         rsurface.batchelement3s_indexbuffer = rsurface.modelelement3s_indexbuffer;
7677         rsurface.batchelement3s_bufferoffset = rsurface.modelelement3s_bufferoffset;
7678         rsurface.batchskeletaltransform3x4 = rsurface.entityskeletaltransform3x4;
7679         rsurface.batchskeletaltransform3x4buffer = rsurface.entityskeletaltransform3x4buffer;
7680         rsurface.batchskeletaltransform3x4offset = rsurface.entityskeletaltransform3x4offset;
7681         rsurface.batchskeletaltransform3x4size = rsurface.entityskeletaltransform3x4size;
7682         rsurface.batchskeletalnumtransforms = rsurface.entityskeletalnumtransforms;
7683
7684         // if any dynamic vertex processing has to occur in software, we copy the
7685         // entire surface list together before processing to rebase the vertices
7686         // to start at 0 (otherwise we waste a lot of room in a vertex buffer).
7687         //
7688         // if any gaps exist and we do not have a static vertex buffer, we have to
7689         // copy the surface list together to avoid wasting upload bandwidth on the
7690         // vertices in the gaps.
7691         //
7692         // if gaps exist and we have a static vertex buffer, we can choose whether
7693         // to combine the index buffer ranges into one dynamic index buffer or
7694         // simply issue multiple glDrawElements calls (BATCHNEED_ALLOWMULTIDRAW).
7695         //
7696         // in many cases the batch is reduced to one draw call.
7697
7698         rsurface.batchmultidraw = false;
7699         rsurface.batchmultidrawnumsurfaces = 0;
7700         rsurface.batchmultidrawsurfacelist = NULL;
7701
7702         if (!dynamicvertex)
7703         {
7704                 // static vertex data, just set pointers...
7705                 rsurface.batchgeneratedvertex = false;
7706                 // if there are gaps, we want to build a combined index buffer,
7707                 // otherwise use the original static buffer with an appropriate offset
7708                 if (gaps)
7709                 {
7710                         r_refdef.stats[r_stat_batch_copytriangles_batches] += 1;
7711                         r_refdef.stats[r_stat_batch_copytriangles_surfaces] += batchnumsurfaces;
7712                         r_refdef.stats[r_stat_batch_copytriangles_vertices] += batchnumvertices;
7713                         r_refdef.stats[r_stat_batch_copytriangles_triangles] += batchnumtriangles;
7714                         if ((batchneed & BATCHNEED_ALLOWMULTIDRAW) && r_batch_multidraw.integer && batchnumtriangles >= r_batch_multidraw_mintriangles.integer)
7715                         {
7716                                 rsurface.batchmultidraw = true;
7717                                 rsurface.batchmultidrawnumsurfaces = texturenumsurfaces;
7718                                 rsurface.batchmultidrawsurfacelist = texturesurfacelist;
7719                                 return;
7720                         }
7721                         // build a new triangle elements array for this batch
7722                         rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
7723                         rsurface.batchfirsttriangle = 0;
7724                         numtriangles = 0;
7725                         for (i = 0;i < texturenumsurfaces;i++)
7726                         {
7727                                 surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
7728                                 surfacenumtriangles = texturesurfacelist[i]->num_triangles;
7729                                 memcpy(rsurface.batchelement3i + 3*numtriangles, rsurface.modelelement3i + 3*surfacefirsttriangle, surfacenumtriangles*sizeof(int[3]));
7730                                 numtriangles += surfacenumtriangles;
7731                         }
7732                         rsurface.batchelement3i_indexbuffer = NULL;
7733                         rsurface.batchelement3i_bufferoffset = 0;
7734                         rsurface.batchelement3s = NULL;
7735                         rsurface.batchelement3s_indexbuffer = NULL;
7736                         rsurface.batchelement3s_bufferoffset = 0;
7737                         if (endvertex <= 65536)
7738                         {
7739                                 // make a 16bit (unsigned short) index array if possible
7740                                 rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
7741                                 for (i = 0;i < numtriangles*3;i++)
7742                                         rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
7743                         }
7744                 }
7745                 else
7746                 {
7747                         r_refdef.stats[r_stat_batch_fast_batches] += 1;
7748                         r_refdef.stats[r_stat_batch_fast_surfaces] += batchnumsurfaces;
7749                         r_refdef.stats[r_stat_batch_fast_vertices] += batchnumvertices;
7750                         r_refdef.stats[r_stat_batch_fast_triangles] += batchnumtriangles;
7751                 }
7752                 return;
7753         }
7754
7755         // something needs software processing, do it for real...
7756         // we only directly handle separate array data in this case and then
7757         // generate interleaved data if needed...
7758         rsurface.batchgeneratedvertex = true;
7759         r_refdef.stats[r_stat_batch_dynamic_batches] += 1;
7760         r_refdef.stats[r_stat_batch_dynamic_surfaces] += batchnumsurfaces;
7761         r_refdef.stats[r_stat_batch_dynamic_vertices] += batchnumvertices;
7762         r_refdef.stats[r_stat_batch_dynamic_triangles] += batchnumtriangles;
7763
7764         // now copy the vertex data into a combined array and make an index array
7765         // (this is what Quake3 does all the time)
7766         // we also apply any skeletal animation here that would have been done in
7767         // the vertex shader, because most of the dynamic vertex animation cases
7768         // need actual vertex positions and normals
7769         //if (dynamicvertex)
7770         {
7771                 rsurface.batchvertex3f = NULL;
7772                 rsurface.batchvertex3f_vertexbuffer = NULL;
7773                 rsurface.batchvertex3f_bufferoffset = 0;
7774                 rsurface.batchsvector3f = NULL;
7775                 rsurface.batchsvector3f_vertexbuffer = NULL;
7776                 rsurface.batchsvector3f_bufferoffset = 0;
7777                 rsurface.batchtvector3f = NULL;
7778                 rsurface.batchtvector3f_vertexbuffer = NULL;
7779                 rsurface.batchtvector3f_bufferoffset = 0;
7780                 rsurface.batchnormal3f = NULL;
7781                 rsurface.batchnormal3f_vertexbuffer = NULL;
7782                 rsurface.batchnormal3f_bufferoffset = 0;
7783                 rsurface.batchlightmapcolor4f = NULL;
7784                 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7785                 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7786                 rsurface.batchtexcoordtexture2f = NULL;
7787                 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7788                 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7789                 rsurface.batchtexcoordlightmap2f = NULL;
7790                 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7791                 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7792                 rsurface.batchskeletalindex4ub = NULL;
7793                 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7794                 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7795                 rsurface.batchskeletalweight4ub = NULL;
7796                 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7797                 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7798                 rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
7799                 rsurface.batchelement3i_indexbuffer = NULL;
7800                 rsurface.batchelement3i_bufferoffset = 0;
7801                 rsurface.batchelement3s = NULL;
7802                 rsurface.batchelement3s_indexbuffer = NULL;
7803                 rsurface.batchelement3s_bufferoffset = 0;
7804                 rsurface.batchskeletaltransform3x4buffer = NULL;
7805                 rsurface.batchskeletaltransform3x4offset = 0;
7806                 rsurface.batchskeletaltransform3x4size = 0;
7807                 // we'll only be setting up certain arrays as needed
7808                 if (batchneed & BATCHNEED_ARRAY_VERTEX)
7809                         rsurface.batchvertex3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7810                 if (batchneed & BATCHNEED_ARRAY_NORMAL)
7811                         rsurface.batchnormal3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7812                 if (batchneed & BATCHNEED_ARRAY_VECTOR)
7813                 {
7814                         rsurface.batchsvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7815                         rsurface.batchtvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7816                 }
7817                 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
7818                         rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
7819                 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
7820                         rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
7821                 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
7822                         rsurface.batchtexcoordlightmap2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
7823                 if (batchneed & BATCHNEED_ARRAY_SKELETAL)
7824                 {
7825                         rsurface.batchskeletalindex4ub = (unsigned char *)R_FrameData_Alloc(batchnumvertices * sizeof(unsigned char[4]));
7826                         rsurface.batchskeletalweight4ub = (unsigned char *)R_FrameData_Alloc(batchnumvertices * sizeof(unsigned char[4]));
7827                 }
7828                 numvertices = 0;
7829                 numtriangles = 0;
7830                 for (i = 0;i < texturenumsurfaces;i++)
7831                 {
7832                         surfacefirstvertex = texturesurfacelist[i]->num_firstvertex;
7833                         surfacenumvertices = texturesurfacelist[i]->num_vertices;
7834                         surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
7835                         surfacenumtriangles = texturesurfacelist[i]->num_triangles;
7836                         // copy only the data requested
7837                         if (batchneed & (BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_ARRAY_LIGHTMAP))
7838                         {
7839                                 if (batchneed & BATCHNEED_ARRAY_VERTEX)
7840                                 {
7841                                         if (rsurface.batchvertex3f)
7842                                                 memcpy(rsurface.batchvertex3f + 3*numvertices, rsurface.modelvertex3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7843                                         else
7844                                                 memset(rsurface.batchvertex3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7845                                 }
7846                                 if (batchneed & BATCHNEED_ARRAY_NORMAL)
7847                                 {
7848                                         if (rsurface.modelnormal3f)
7849                                                 memcpy(rsurface.batchnormal3f + 3*numvertices, rsurface.modelnormal3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7850                                         else
7851                                                 memset(rsurface.batchnormal3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7852                                 }
7853                                 if (batchneed & BATCHNEED_ARRAY_VECTOR)
7854                                 {
7855                                         if (rsurface.modelsvector3f)
7856                                         {
7857                                                 memcpy(rsurface.batchsvector3f + 3*numvertices, rsurface.modelsvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7858                                                 memcpy(rsurface.batchtvector3f + 3*numvertices, rsurface.modeltvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7859                                         }
7860                                         else
7861                                         {
7862                                                 memset(rsurface.batchsvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7863                                                 memset(rsurface.batchtvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7864                                         }
7865                                 }
7866                                 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
7867                                 {
7868                                         if (rsurface.modellightmapcolor4f)
7869                                                 memcpy(rsurface.batchlightmapcolor4f + 4*numvertices, rsurface.modellightmapcolor4f + 4*surfacefirstvertex, surfacenumvertices * sizeof(float[4]));
7870                                         else
7871                                                 memset(rsurface.batchlightmapcolor4f + 4*numvertices, 0, surfacenumvertices * sizeof(float[4]));
7872                                 }
7873                                 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
7874                                 {
7875                                         if (rsurface.modeltexcoordtexture2f)
7876                                                 memcpy(rsurface.batchtexcoordtexture2f + 2*numvertices, rsurface.modeltexcoordtexture2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
7877                                         else
7878                                                 memset(rsurface.batchtexcoordtexture2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
7879                                 }
7880                                 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
7881                                 {
7882                                         if (rsurface.modeltexcoordlightmap2f)
7883                                                 memcpy(rsurface.batchtexcoordlightmap2f + 2*numvertices, rsurface.modeltexcoordlightmap2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
7884                                         else
7885                                                 memset(rsurface.batchtexcoordlightmap2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
7886                                 }
7887                                 if (batchneed & BATCHNEED_ARRAY_SKELETAL)
7888                                 {
7889                                         if (rsurface.modelskeletalindex4ub)
7890                                         {
7891                                                 memcpy(rsurface.batchskeletalindex4ub + 4*numvertices, rsurface.modelskeletalindex4ub + 4*surfacefirstvertex, surfacenumvertices * sizeof(unsigned char[4]));
7892                                                 memcpy(rsurface.batchskeletalweight4ub + 4*numvertices, rsurface.modelskeletalweight4ub + 4*surfacefirstvertex, surfacenumvertices * sizeof(unsigned char[4]));
7893                                         }
7894                                         else
7895                                         {
7896                                                 memset(rsurface.batchskeletalindex4ub + 4*numvertices, 0, surfacenumvertices * sizeof(unsigned char[4]));
7897                                                 memset(rsurface.batchskeletalweight4ub + 4*numvertices, 0, surfacenumvertices * sizeof(unsigned char[4]));
7898                                                 ub = rsurface.batchskeletalweight4ub + 4*numvertices;
7899                                                 for (j = 0;j < surfacenumvertices;j++)
7900                                                         ub[j*4] = 255;
7901                                         }
7902                                 }
7903                         }
7904                         RSurf_RenumberElements(rsurface.modelelement3i + 3*surfacefirsttriangle, rsurface.batchelement3i + 3*numtriangles, 3*surfacenumtriangles, numvertices - surfacefirstvertex);
7905                         numvertices += surfacenumvertices;
7906                         numtriangles += surfacenumtriangles;
7907                 }
7908
7909                 // generate a 16bit index array as well if possible
7910                 // (in general, dynamic batches fit)
7911                 if (numvertices <= 65536)
7912                 {
7913                         rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
7914                         for (i = 0;i < numtriangles*3;i++)
7915                                 rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
7916                 }
7917
7918                 // since we've copied everything, the batch now starts at 0
7919                 rsurface.batchfirstvertex = 0;
7920                 rsurface.batchnumvertices = batchnumvertices;
7921                 rsurface.batchfirsttriangle = 0;
7922                 rsurface.batchnumtriangles = batchnumtriangles;
7923         }
7924
7925         // apply skeletal animation that would have been done in the vertex shader
7926         if (rsurface.batchskeletaltransform3x4)
7927         {
7928                 const unsigned char *si;
7929                 const unsigned char *sw;
7930                 const float *t[4];
7931                 const float *b = rsurface.batchskeletaltransform3x4;
7932                 float *vp, *vs, *vt, *vn;
7933                 float w[4];
7934                 float m[3][4], n[3][4];
7935                 float tp[3], ts[3], tt[3], tn[3];
7936                 r_refdef.stats[r_stat_batch_dynamicskeletal_batches] += 1;
7937                 r_refdef.stats[r_stat_batch_dynamicskeletal_surfaces] += batchnumsurfaces;
7938                 r_refdef.stats[r_stat_batch_dynamicskeletal_vertices] += batchnumvertices;
7939                 r_refdef.stats[r_stat_batch_dynamicskeletal_triangles] += batchnumtriangles;
7940                 si = rsurface.batchskeletalindex4ub;
7941                 sw = rsurface.batchskeletalweight4ub;
7942                 vp = rsurface.batchvertex3f;
7943                 vs = rsurface.batchsvector3f;
7944                 vt = rsurface.batchtvector3f;
7945                 vn = rsurface.batchnormal3f;
7946                 memset(m[0], 0, sizeof(m));
7947                 memset(n[0], 0, sizeof(n));
7948                 for (i = 0;i < batchnumvertices;i++)
7949                 {
7950                         t[0] = b + si[0]*12;
7951                         if (sw[0] == 255)
7952                         {
7953                                 // common case - only one matrix
7954                                 m[0][0] = t[0][ 0];
7955                                 m[0][1] = t[0][ 1];
7956                                 m[0][2] = t[0][ 2];
7957                                 m[0][3] = t[0][ 3];
7958                                 m[1][0] = t[0][ 4];
7959                                 m[1][1] = t[0][ 5];
7960                                 m[1][2] = t[0][ 6];
7961                                 m[1][3] = t[0][ 7];
7962                                 m[2][0] = t[0][ 8];
7963                                 m[2][1] = t[0][ 9];
7964                                 m[2][2] = t[0][10];
7965                                 m[2][3] = t[0][11];
7966                         }
7967                         else if (sw[2] + sw[3])
7968                         {
7969                                 // blend 4 matrices
7970                                 t[1] = b + si[1]*12;
7971                                 t[2] = b + si[2]*12;
7972                                 t[3] = b + si[3]*12;
7973                                 w[0] = sw[0] * (1.0f / 255.0f);
7974                                 w[1] = sw[1] * (1.0f / 255.0f);
7975                                 w[2] = sw[2] * (1.0f / 255.0f);
7976                                 w[3] = sw[3] * (1.0f / 255.0f);
7977                                 // blend the matrices
7978                                 m[0][0] = t[0][ 0] * w[0] + t[1][ 0] * w[1] + t[2][ 0] * w[2] + t[3][ 0] * w[3];
7979                                 m[0][1] = t[0][ 1] * w[0] + t[1][ 1] * w[1] + t[2][ 1] * w[2] + t[3][ 1] * w[3];
7980                                 m[0][2] = t[0][ 2] * w[0] + t[1][ 2] * w[1] + t[2][ 2] * w[2] + t[3][ 2] * w[3];
7981                                 m[0][3] = t[0][ 3] * w[0] + t[1][ 3] * w[1] + t[2][ 3] * w[2] + t[3][ 3] * w[3];
7982                                 m[1][0] = t[0][ 4] * w[0] + t[1][ 4] * w[1] + t[2][ 4] * w[2] + t[3][ 4] * w[3];
7983                                 m[1][1] = t[0][ 5] * w[0] + t[1][ 5] * w[1] + t[2][ 5] * w[2] + t[3][ 5] * w[3];
7984                                 m[1][2] = t[0][ 6] * w[0] + t[1][ 6] * w[1] + t[2][ 6] * w[2] + t[3][ 6] * w[3];
7985                                 m[1][3] = t[0][ 7] * w[0] + t[1][ 7] * w[1] + t[2][ 7] * w[2] + t[3][ 7] * w[3];
7986                                 m[2][0] = t[0][ 8] * w[0] + t[1][ 8] * w[1] + t[2][ 8] * w[2] + t[3][ 8] * w[3];
7987                                 m[2][1] = t[0][ 9] * w[0] + t[1][ 9] * w[1] + t[2][ 9] * w[2] + t[3][ 9] * w[3];
7988                                 m[2][2] = t[0][10] * w[0] + t[1][10] * w[1] + t[2][10] * w[2] + t[3][10] * w[3];
7989                                 m[2][3] = t[0][11] * w[0] + t[1][11] * w[1] + t[2][11] * w[2] + t[3][11] * w[3];
7990                         }
7991                         else
7992                         {
7993                                 // blend 2 matrices
7994                                 t[1] = b + si[1]*12;
7995                                 w[0] = sw[0] * (1.0f / 255.0f);
7996                                 w[1] = sw[1] * (1.0f / 255.0f);
7997                                 // blend the matrices
7998                                 m[0][0] = t[0][ 0] * w[0] + t[1][ 0] * w[1];
7999                                 m[0][1] = t[0][ 1] * w[0] + t[1][ 1] * w[1];
8000                                 m[0][2] = t[0][ 2] * w[0] + t[1][ 2] * w[1];
8001                                 m[0][3] = t[0][ 3] * w[0] + t[1][ 3] * w[1];
8002                                 m[1][0] = t[0][ 4] * w[0] + t[1][ 4] * w[1];
8003                                 m[1][1] = t[0][ 5] * w[0] + t[1][ 5] * w[1];
8004                                 m[1][2] = t[0][ 6] * w[0] + t[1][ 6] * w[1];
8005                                 m[1][3] = t[0][ 7] * w[0] + t[1][ 7] * w[1];
8006                                 m[2][0] = t[0][ 8] * w[0] + t[1][ 8] * w[1];
8007                                 m[2][1] = t[0][ 9] * w[0] + t[1][ 9] * w[1];
8008                                 m[2][2] = t[0][10] * w[0] + t[1][10] * w[1];
8009                                 m[2][3] = t[0][11] * w[0] + t[1][11] * w[1];
8010                         }
8011                         si += 4;
8012                         sw += 4;
8013                         // modify the vertex
8014                         VectorCopy(vp, tp);
8015                         vp[0] = tp[0] * m[0][0] + tp[1] * m[0][1] + tp[2] * m[0][2] + m[0][3];
8016                         vp[1] = tp[0] * m[1][0] + tp[1] * m[1][1] + tp[2] * m[1][2] + m[1][3];
8017                         vp[2] = tp[0] * m[2][0] + tp[1] * m[2][1] + tp[2] * m[2][2] + m[2][3];
8018                         vp += 3;
8019                         if (vn)
8020                         {
8021                                 // the normal transformation matrix is a set of cross products...
8022                                 CrossProduct(m[1], m[2], n[0]);
8023                                 CrossProduct(m[2], m[0], n[1]);
8024                                 CrossProduct(m[0], m[1], n[2]); // is actually transpose(inverse(m)) * det(m)
8025                                 VectorCopy(vn, tn);
8026                                 vn[0] = tn[0] * n[0][0] + tn[1] * n[0][1] + tn[2] * n[0][2];
8027                                 vn[1] = tn[0] * n[1][0] + tn[1] * n[1][1] + tn[2] * n[1][2];
8028                                 vn[2] = tn[0] * n[2][0] + tn[1] * n[2][1] + tn[2] * n[2][2];
8029                                 VectorNormalize(vn);
8030                                 vn += 3;
8031                                 if (vs)
8032                                 {
8033                                         VectorCopy(vs, ts);
8034                                         vs[0] = ts[0] * n[0][0] + ts[1] * n[0][1] + ts[2] * n[0][2];
8035                                         vs[1] = ts[0] * n[1][0] + ts[1] * n[1][1] + ts[2] * n[1][2];
8036                                         vs[2] = ts[0] * n[2][0] + ts[1] * n[2][1] + ts[2] * n[2][2];
8037                                         VectorNormalize(vs);
8038                                         vs += 3;
8039                                         VectorCopy(vt, tt);
8040                                         vt[0] = tt[0] * n[0][0] + tt[1] * n[0][1] + tt[2] * n[0][2];
8041                                         vt[1] = tt[0] * n[1][0] + tt[1] * n[1][1] + tt[2] * n[1][2];
8042                                         vt[2] = tt[0] * n[2][0] + tt[1] * n[2][1] + tt[2] * n[2][2];
8043                                         VectorNormalize(vt);
8044                                         vt += 3;
8045                                 }
8046                         }
8047                 }
8048                 rsurface.batchskeletaltransform3x4 = NULL;
8049                 rsurface.batchskeletalnumtransforms = 0;
8050         }
8051
8052         // q1bsp surfaces rendered in vertex color mode have to have colors
8053         // calculated based on lightstyles
8054         if ((batchneed & BATCHNEED_ARRAY_VERTEXCOLOR) && texturesurfacelist[0]->lightmapinfo)
8055         {
8056                 // generate color arrays for the surfaces in this list
8057                 int c[4];
8058                 int scale;
8059                 int size3;
8060                 const int *offsets;
8061                 const unsigned char *lm;
8062                 rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
8063                 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
8064                 rsurface.batchlightmapcolor4f_bufferoffset = 0;
8065                 numvertices = 0;
8066                 for (i = 0;i < texturenumsurfaces;i++)
8067                 {
8068                         surface = texturesurfacelist[i];
8069                         offsets = rsurface.modellightmapoffsets + surface->num_firstvertex;
8070                         surfacenumvertices = surface->num_vertices;
8071                         if (surface->lightmapinfo->samples)
8072                         {
8073                                 for (j = 0;j < surfacenumvertices;j++)
8074                                 {
8075                                         lm = surface->lightmapinfo->samples + offsets[j];
8076                                         scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[0]];
8077                                         VectorScale(lm, scale, c);
8078                                         if (surface->lightmapinfo->styles[1] != 255)
8079                                         {
8080                                                 size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
8081                                                 lm += size3;
8082                                                 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[1]];
8083                                                 VectorMA(c, scale, lm, c);
8084                                                 if (surface->lightmapinfo->styles[2] != 255)
8085                                                 {
8086                                                         lm += size3;
8087                                                         scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[2]];
8088                                                         VectorMA(c, scale, lm, c);
8089                                                         if (surface->lightmapinfo->styles[3] != 255)
8090                                                         {
8091                                                                 lm += size3;
8092                                                                 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[3]];
8093                                                                 VectorMA(c, scale, lm, c);
8094                                                         }
8095                                                 }
8096                                         }
8097                                         c[0] >>= 7;
8098                                         c[1] >>= 7;
8099                                         c[2] >>= 7;
8100                                         Vector4Set(rsurface.batchlightmapcolor4f + 4*numvertices, min(c[0], 255) * (1.0f / 255.0f), min(c[1], 255) * (1.0f / 255.0f), min(c[2], 255) * (1.0f / 255.0f), 1);
8101                                         numvertices++;
8102                                 }
8103                         }
8104                         else
8105                         {
8106                                 for (j = 0;j < surfacenumvertices;j++)
8107                                 {
8108                                         Vector4Set(rsurface.batchlightmapcolor4f + 4*numvertices, 0, 0, 0, 1);
8109                                         numvertices++;
8110                                 }
8111                         }
8112                 }
8113         }
8114
8115         // if vertices are deformed (sprite flares and things in maps, possibly
8116         // water waves, bulges and other deformations), modify the copied vertices
8117         // in place
8118         for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
8119         {
8120                 float scale;
8121                 switch (deform->deform)
8122                 {
8123                 default:
8124                 case Q3DEFORM_PROJECTIONSHADOW:
8125                 case Q3DEFORM_TEXT0:
8126                 case Q3DEFORM_TEXT1:
8127                 case Q3DEFORM_TEXT2:
8128                 case Q3DEFORM_TEXT3:
8129                 case Q3DEFORM_TEXT4:
8130                 case Q3DEFORM_TEXT5:
8131                 case Q3DEFORM_TEXT6:
8132                 case Q3DEFORM_TEXT7:
8133                 case Q3DEFORM_NONE:
8134                         break;
8135                 case Q3DEFORM_AUTOSPRITE:
8136                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
8137                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
8138                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
8139                         VectorNormalize(newforward);
8140                         VectorNormalize(newright);
8141                         VectorNormalize(newup);
8142 //                      rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8143 //                      rsurface.batchvertex3f_vertexbuffer = NULL;
8144 //                      rsurface.batchvertex3f_bufferoffset = 0;
8145 //                      rsurface.batchsvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchsvector3f);
8146 //                      rsurface.batchsvector3f_vertexbuffer = NULL;
8147 //                      rsurface.batchsvector3f_bufferoffset = 0;
8148 //                      rsurface.batchtvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchtvector3f);
8149 //                      rsurface.batchtvector3f_vertexbuffer = NULL;
8150 //                      rsurface.batchtvector3f_bufferoffset = 0;
8151 //                      rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8152 //                      rsurface.batchnormal3f_vertexbuffer = NULL;
8153 //                      rsurface.batchnormal3f_bufferoffset = 0;
8154                         // sometimes we're on a renderpath that does not use vectors (GL11/GL13/GLES1)
8155                         if (!VectorLength2(rsurface.batchnormal3f + 3*rsurface.batchfirstvertex))
8156                                 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8157                         if (!VectorLength2(rsurface.batchsvector3f + 3*rsurface.batchfirstvertex))
8158                                 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8159                         // a single autosprite surface can contain multiple sprites...
8160                         for (j = 0;j < batchnumvertices - 3;j += 4)
8161                         {
8162                                 VectorClear(center);
8163                                 for (i = 0;i < 4;i++)
8164                                         VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
8165                                 VectorScale(center, 0.25f, center);
8166                                 VectorCopy(rsurface.batchnormal3f + 3*j, forward);
8167                                 VectorCopy(rsurface.batchsvector3f + 3*j, right);
8168                                 VectorCopy(rsurface.batchtvector3f + 3*j, up);
8169                                 for (i = 0;i < 4;i++)
8170                                 {
8171                                         VectorSubtract(rsurface.batchvertex3f + 3*(j+i), center, v);
8172                                         VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.batchvertex3f + 3*(j+i));
8173                                 }
8174                         }
8175                         // if we get here, BATCHNEED_ARRAY_NORMAL and BATCHNEED_ARRAY_VECTOR are in batchneed, so no need to check
8176                         Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8177                         Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8178                         break;
8179                 case Q3DEFORM_AUTOSPRITE2:
8180                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
8181                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
8182                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
8183                         VectorNormalize(newforward);
8184                         VectorNormalize(newright);
8185                         VectorNormalize(newup);
8186 //                      rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8187 //                      rsurface.batchvertex3f_vertexbuffer = NULL;
8188 //                      rsurface.batchvertex3f_bufferoffset = 0;
8189                         {
8190                                 const float *v1, *v2;
8191                                 vec3_t start, end;
8192                                 float f, l;
8193                                 struct
8194                                 {
8195                                         float length2;
8196                                         const float *v1;
8197                                         const float *v2;
8198                                 }
8199                                 shortest[2];
8200                                 memset(shortest, 0, sizeof(shortest));
8201                                 // a single autosprite surface can contain multiple sprites...
8202                                 for (j = 0;j < batchnumvertices - 3;j += 4)
8203                                 {
8204                                         VectorClear(center);
8205                                         for (i = 0;i < 4;i++)
8206                                                 VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
8207                                         VectorScale(center, 0.25f, center);
8208                                         // find the two shortest edges, then use them to define the
8209                                         // axis vectors for rotating around the central axis
8210                                         for (i = 0;i < 6;i++)
8211                                         {
8212                                                 v1 = rsurface.batchvertex3f + 3*(j+quadedges[i][0]);
8213                                                 v2 = rsurface.batchvertex3f + 3*(j+quadedges[i][1]);
8214                                                 l = VectorDistance2(v1, v2);
8215                                                 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
8216                                                 if (v1[2] != v2[2])
8217                                                         l += (1.0f / 1024.0f);
8218                                                 if (shortest[0].length2 > l || i == 0)
8219                                                 {
8220                                                         shortest[1] = shortest[0];
8221                                                         shortest[0].length2 = l;
8222                                                         shortest[0].v1 = v1;
8223                                                         shortest[0].v2 = v2;
8224                                                 }
8225                                                 else if (shortest[1].length2 > l || i == 1)
8226                                                 {
8227                                                         shortest[1].length2 = l;
8228                                                         shortest[1].v1 = v1;
8229                                                         shortest[1].v2 = v2;
8230                                                 }
8231                                         }
8232                                         VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
8233                                         VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
8234                                         // this calculates the right vector from the shortest edge
8235                                         // and the up vector from the edge midpoints
8236                                         VectorSubtract(shortest[0].v1, shortest[0].v2, right);
8237                                         VectorNormalize(right);
8238                                         VectorSubtract(end, start, up);
8239                                         VectorNormalize(up);
8240                                         // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
8241                                         VectorSubtract(rsurface.localvieworigin, center, forward);
8242                                         //Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, forward);
8243                                         VectorNegate(forward, forward);
8244                                         VectorReflect(forward, 0, up, forward);
8245                                         VectorNormalize(forward);
8246                                         CrossProduct(up, forward, newright);
8247                                         VectorNormalize(newright);
8248                                         // rotate the quad around the up axis vector, this is made
8249                                         // especially easy by the fact we know the quad is flat,
8250                                         // so we only have to subtract the center position and
8251                                         // measure distance along the right vector, and then
8252                                         // multiply that by the newright vector and add back the
8253                                         // center position
8254                                         // we also need to subtract the old position to undo the
8255                                         // displacement from the center, which we do with a
8256                                         // DotProduct, the subtraction/addition of center is also
8257                                         // optimized into DotProducts here
8258                                         l = DotProduct(right, center);
8259                                         for (i = 0;i < 4;i++)
8260                                         {
8261                                                 v1 = rsurface.batchvertex3f + 3*(j+i);
8262                                                 f = DotProduct(right, v1) - l;
8263                                                 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.batchvertex3f + 3*(j+i));
8264                                         }
8265                                 }
8266                         }
8267                         if(batchneed & (BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR)) // otherwise these can stay NULL
8268                         {
8269 //                              rsurface.batchnormal3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8270 //                              rsurface.batchnormal3f_vertexbuffer = NULL;
8271 //                              rsurface.batchnormal3f_bufferoffset = 0;
8272                                 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8273                         }
8274                         if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8275                         {
8276 //                              rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8277 //                              rsurface.batchsvector3f_vertexbuffer = NULL;
8278 //                              rsurface.batchsvector3f_bufferoffset = 0;
8279 //                              rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8280 //                              rsurface.batchtvector3f_vertexbuffer = NULL;
8281 //                              rsurface.batchtvector3f_bufferoffset = 0;
8282                                 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8283                         }
8284                         break;
8285                 case Q3DEFORM_NORMAL:
8286                         // deform the normals to make reflections wavey
8287                         rsurface.batchnormal3f = (float *)R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8288                         rsurface.batchnormal3f_vertexbuffer = NULL;
8289                         rsurface.batchnormal3f_bufferoffset = 0;
8290                         for (j = 0;j < batchnumvertices;j++)
8291                         {
8292                                 float vertex[3];
8293                                 float *normal = rsurface.batchnormal3f + 3*j;
8294                                 VectorScale(rsurface.batchvertex3f + 3*j, 0.98f, vertex);
8295                                 normal[0] = rsurface.batchnormal3f[j*3+0] + deform->parms[0] * noise4f(      vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8296                                 normal[1] = rsurface.batchnormal3f[j*3+1] + deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8297                                 normal[2] = rsurface.batchnormal3f[j*3+2] + deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8298                                 VectorNormalize(normal);
8299                         }
8300                         if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8301                         {
8302 //                              rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8303 //                              rsurface.batchsvector3f_vertexbuffer = NULL;
8304 //                              rsurface.batchsvector3f_bufferoffset = 0;
8305 //                              rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8306 //                              rsurface.batchtvector3f_vertexbuffer = NULL;
8307 //                              rsurface.batchtvector3f_bufferoffset = 0;
8308                                 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8309                         }
8310                         break;
8311                 case Q3DEFORM_WAVE:
8312                         // deform vertex array to make wavey water and flags and such
8313                         waveparms[0] = deform->waveparms[0];
8314                         waveparms[1] = deform->waveparms[1];
8315                         waveparms[2] = deform->waveparms[2];
8316                         waveparms[3] = deform->waveparms[3];
8317                         if(!R_TestQ3WaveFunc(deform->wavefunc, waveparms))
8318                                 break; // if wavefunc is a nop, don't make a dynamic vertex array
8319                         // this is how a divisor of vertex influence on deformation
8320                         animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
8321                         scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
8322 //                      rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8323 //                      rsurface.batchvertex3f_vertexbuffer = NULL;
8324 //                      rsurface.batchvertex3f_bufferoffset = 0;
8325 //                      rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8326 //                      rsurface.batchnormal3f_vertexbuffer = NULL;
8327 //                      rsurface.batchnormal3f_bufferoffset = 0;
8328                         for (j = 0;j < batchnumvertices;j++)
8329                         {
8330                                 // if the wavefunc depends on time, evaluate it per-vertex
8331                                 if (waveparms[3])
8332                                 {
8333                                         waveparms[2] = deform->waveparms[2] + (rsurface.batchvertex3f[j*3+0] + rsurface.batchvertex3f[j*3+1] + rsurface.batchvertex3f[j*3+2]) * animpos;
8334                                         scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
8335                                 }
8336                                 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
8337                         }
8338                         // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
8339                         Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8340                         if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8341                         {
8342 //                              rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8343 //                              rsurface.batchsvector3f_vertexbuffer = NULL;
8344 //                              rsurface.batchsvector3f_bufferoffset = 0;
8345 //                              rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8346 //                              rsurface.batchtvector3f_vertexbuffer = NULL;
8347 //                              rsurface.batchtvector3f_bufferoffset = 0;
8348                                 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8349                         }
8350                         break;
8351                 case Q3DEFORM_BULGE:
8352                         // deform vertex array to make the surface have moving bulges
8353 //                      rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8354 //                      rsurface.batchvertex3f_vertexbuffer = NULL;
8355 //                      rsurface.batchvertex3f_bufferoffset = 0;
8356 //                      rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8357 //                      rsurface.batchnormal3f_vertexbuffer = NULL;
8358 //                      rsurface.batchnormal3f_bufferoffset = 0;
8359                         for (j = 0;j < batchnumvertices;j++)
8360                         {
8361                                 scale = sin(rsurface.batchtexcoordtexture2f[j*2+0] * deform->parms[0] + rsurface.shadertime * deform->parms[2]) * deform->parms[1];
8362                                 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
8363                         }
8364                         // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
8365                         Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8366                         if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8367                         {
8368 //                              rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8369 //                              rsurface.batchsvector3f_vertexbuffer = NULL;
8370 //                              rsurface.batchsvector3f_bufferoffset = 0;
8371 //                              rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8372 //                              rsurface.batchtvector3f_vertexbuffer = NULL;
8373 //                              rsurface.batchtvector3f_bufferoffset = 0;
8374                                 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8375                         }
8376                         break;
8377                 case Q3DEFORM_MOVE:
8378                         // deform vertex array
8379                         if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
8380                                 break; // if wavefunc is a nop, don't make a dynamic vertex array
8381                         scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
8382                         VectorScale(deform->parms, scale, waveparms);
8383 //                      rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8384 //                      rsurface.batchvertex3f_vertexbuffer = NULL;
8385 //                      rsurface.batchvertex3f_bufferoffset = 0;
8386                         for (j = 0;j < batchnumvertices;j++)
8387                                 VectorAdd(rsurface.batchvertex3f + 3*j, waveparms, rsurface.batchvertex3f + 3*j);
8388                         break;
8389                 }
8390         }
8391
8392         if (rsurface.batchtexcoordtexture2f && rsurface.texture->materialshaderpass)
8393         {
8394         // generate texcoords based on the chosen texcoord source
8395                 switch(rsurface.texture->materialshaderpass->tcgen.tcgen)
8396                 {
8397                 default:
8398                 case Q3TCGEN_TEXTURE:
8399                         break;
8400                 case Q3TCGEN_LIGHTMAP:
8401         //              rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8402         //              rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8403         //              rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8404                         if (rsurface.batchtexcoordlightmap2f)
8405                                 memcpy(rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordlightmap2f, batchnumvertices * sizeof(float[2]));
8406                         break;
8407                 case Q3TCGEN_VECTOR:
8408         //              rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8409         //              rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8410         //              rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8411                         for (j = 0;j < batchnumvertices;j++)
8412                         {
8413                                 rsurface.batchtexcoordtexture2f[j*2+0] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->materialshaderpass->tcgen.parms);
8414                                 rsurface.batchtexcoordtexture2f[j*2+1] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->materialshaderpass->tcgen.parms + 3);
8415                         }
8416                         break;
8417                 case Q3TCGEN_ENVIRONMENT:
8418                         // make environment reflections using a spheremap
8419                         rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8420                         rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8421                         rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8422                         for (j = 0;j < batchnumvertices;j++)
8423                         {
8424                                 // identical to Q3A's method, but executed in worldspace so
8425                                 // carried models can be shiny too
8426
8427                                 float viewer[3], d, reflected[3], worldreflected[3];
8428
8429                                 VectorSubtract(rsurface.localvieworigin, rsurface.batchvertex3f + 3*j, viewer);
8430                                 // VectorNormalize(viewer);
8431
8432                                 d = DotProduct(rsurface.batchnormal3f + 3*j, viewer);
8433
8434                                 reflected[0] = rsurface.batchnormal3f[j*3+0]*2*d - viewer[0];
8435                                 reflected[1] = rsurface.batchnormal3f[j*3+1]*2*d - viewer[1];
8436                                 reflected[2] = rsurface.batchnormal3f[j*3+2]*2*d - viewer[2];
8437                                 // note: this is proportinal to viewer, so we can normalize later
8438
8439                                 Matrix4x4_Transform3x3(&rsurface.matrix, reflected, worldreflected);
8440                                 VectorNormalize(worldreflected);
8441
8442                                 // note: this sphere map only uses world x and z!
8443                                 // so positive and negative y will LOOK THE SAME.
8444                                 rsurface.batchtexcoordtexture2f[j*2+0] = 0.5 + 0.5 * worldreflected[1];
8445                                 rsurface.batchtexcoordtexture2f[j*2+1] = 0.5 - 0.5 * worldreflected[2];
8446                         }
8447                         break;
8448                 }
8449                 // the only tcmod that needs software vertex processing is turbulent, so
8450                 // check for it here and apply the changes if needed
8451                 // and we only support that as the first one
8452                 // (handling a mixture of turbulent and other tcmods would be problematic
8453                 //  without punting it entirely to a software path)
8454                 if (rsurface.texture->materialshaderpass->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
8455                 {
8456                         amplitude = rsurface.texture->materialshaderpass->tcmods[0].parms[1];
8457                         animpos = rsurface.texture->materialshaderpass->tcmods[0].parms[2] + rsurface.shadertime * rsurface.texture->materialshaderpass->tcmods[0].parms[3];
8458         //              rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8459         //              rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8460         //              rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8461                         for (j = 0;j < batchnumvertices;j++)
8462                         {
8463                                 rsurface.batchtexcoordtexture2f[j*2+0] += amplitude * sin(((rsurface.batchvertex3f[j*3+0] + rsurface.batchvertex3f[j*3+2]) * 1.0 / 1024.0f + animpos) * M_PI * 2);
8464                                 rsurface.batchtexcoordtexture2f[j*2+1] += amplitude * sin(((rsurface.batchvertex3f[j*3+1]                                ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
8465                         }
8466                 }
8467         }
8468 }
8469
8470 void RSurf_DrawBatch(void)
8471 {
8472         // sometimes a zero triangle surface (usually a degenerate patch) makes it
8473         // through the pipeline, killing it earlier in the pipeline would have
8474         // per-surface overhead rather than per-batch overhead, so it's best to
8475         // reject it here, before it hits glDraw.
8476         if (rsurface.batchnumtriangles == 0)
8477                 return;
8478 #if 0
8479         // batch debugging code
8480         if (r_test.integer && rsurface.entity == r_refdef.scene.worldentity && rsurface.batchvertex3f == r_refdef.scene.worldentity->model->surfmesh.data_vertex3f)
8481         {
8482                 int i;
8483                 int j;
8484                 int c;
8485                 const int *e;
8486                 e = rsurface.batchelement3i + rsurface.batchfirsttriangle*3;
8487                 for (i = 0;i < rsurface.batchnumtriangles*3;i++)
8488                 {
8489                         c = e[i];
8490                         for (j = 0;j < rsurface.entity->model->num_surfaces;j++)
8491                         {
8492                                 if (c >= rsurface.modelsurfaces[j].num_firstvertex && c < (rsurface.modelsurfaces[j].num_firstvertex + rsurface.modelsurfaces[j].num_vertices))
8493                                 {
8494                                         if (rsurface.modelsurfaces[j].texture != rsurface.texture)
8495                                                 Sys_Error("RSurf_DrawBatch: index %i uses different texture (%s) than surface %i which it belongs to (which uses %s)\n", c, rsurface.texture->name, j, rsurface.modelsurfaces[j].texture->name);
8496                                         break;
8497                                 }
8498                         }
8499                 }
8500         }
8501 #endif
8502         if (rsurface.batchmultidraw)
8503         {
8504                 // issue multiple draws rather than copying index data
8505                 int numsurfaces = rsurface.batchmultidrawnumsurfaces;
8506                 const msurface_t **surfacelist = rsurface.batchmultidrawsurfacelist;
8507                 int i, j, k, firstvertex, endvertex, firsttriangle, endtriangle;
8508                 for (i = 0;i < numsurfaces;)
8509                 {
8510                         // combine consecutive surfaces as one draw
8511                         for (k = i, j = i + 1;j < numsurfaces;k = j, j++)
8512                                 if (surfacelist[j] != surfacelist[k] + 1)
8513                                         break;
8514                         firstvertex = surfacelist[i]->num_firstvertex;
8515                         endvertex = surfacelist[k]->num_firstvertex + surfacelist[k]->num_vertices;
8516                         firsttriangle = surfacelist[i]->num_firsttriangle;
8517                         endtriangle = surfacelist[k]->num_firsttriangle + surfacelist[k]->num_triangles;
8518                         R_Mesh_Draw(firstvertex, endvertex - firstvertex, firsttriangle, endtriangle - firsttriangle, rsurface.batchelement3i, rsurface.batchelement3i_indexbuffer, rsurface.batchelement3i_bufferoffset, rsurface.batchelement3s, rsurface.batchelement3s_indexbuffer, rsurface.batchelement3s_bufferoffset);
8519                         i = j;
8520                 }
8521         }
8522         else
8523         {
8524                 // there is only one consecutive run of index data (may have been combined)
8525                 R_Mesh_Draw(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchfirsttriangle, rsurface.batchnumtriangles, rsurface.batchelement3i, rsurface.batchelement3i_indexbuffer, rsurface.batchelement3i_bufferoffset, rsurface.batchelement3s, rsurface.batchelement3s_indexbuffer, rsurface.batchelement3s_bufferoffset);
8526         }
8527 }
8528
8529 static int RSurf_FindWaterPlaneForSurface(const msurface_t *surface)
8530 {
8531         // pick the closest matching water plane
8532         int planeindex, vertexindex, bestplaneindex = -1;
8533         float d, bestd;
8534         vec3_t vert;
8535         const float *v;
8536         r_waterstate_waterplane_t *p;
8537         qbool prepared = false;
8538         bestd = 0;
8539         for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
8540         {
8541                 if(p->camera_entity != rsurface.texture->camera_entity)
8542                         continue;
8543                 d = 0;
8544                 if(!prepared)
8545                 {
8546                         RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX, 1, &surface);
8547                         prepared = true;
8548                         if(rsurface.batchnumvertices == 0)
8549                                 break;
8550                 }
8551                 for (vertexindex = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3;vertexindex < rsurface.batchnumvertices;vertexindex++, v += 3)
8552                 {
8553                         Matrix4x4_Transform(&rsurface.matrix, v, vert);
8554                         d += fabs(PlaneDiff(vert, &p->plane));
8555                 }
8556                 if (bestd > d || bestplaneindex < 0)
8557                 {
8558                         bestd = d;
8559                         bestplaneindex = planeindex;
8560                 }
8561         }
8562         return bestplaneindex;
8563         // NOTE: this MAY return a totally unrelated water plane; we can ignore
8564         // this situation though, as it might be better to render single larger
8565         // batches with useless stuff (backface culled for example) than to
8566         // render multiple smaller batches
8567 }
8568
8569 void RSurf_SetupDepthAndCulling(void)
8570 {
8571         // submodels are biased to avoid z-fighting with world surfaces that they
8572         // may be exactly overlapping (avoids z-fighting artifacts on certain
8573         // doors and things in Quake maps)
8574         GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
8575         GL_PolygonOffset(rsurface.basepolygonfactor + rsurface.texture->biaspolygonfactor, rsurface.basepolygonoffset + rsurface.texture->biaspolygonoffset);
8576         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
8577         GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
8578 }
8579
8580 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8581 {
8582         int j;
8583         const float *v;
8584         float p[3], mins[3], maxs[3];
8585         int scissor[4];
8586         // transparent sky would be ridiculous
8587         if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
8588                 return;
8589         R_SetupShader_Generic_NoTexture(false, false);
8590         skyrenderlater = true;
8591         RSurf_SetupDepthAndCulling();
8592         GL_DepthMask(true);
8593
8594         // add the vertices of the surfaces to a world bounding box so we can scissor the sky render later
8595         if (r_sky_scissor.integer)
8596         {
8597                 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
8598                 for (j = 0, v = rsurface.batchvertex3f + 3 * rsurface.batchfirstvertex; j < rsurface.batchnumvertices; j++, v += 3)
8599                 {
8600                         Matrix4x4_Transform(&rsurface.matrix, v, p);
8601                         if (j > 0)
8602                         {
8603                                 if (mins[0] > p[0]) mins[0] = p[0];
8604                                 if (mins[1] > p[1]) mins[1] = p[1];
8605                                 if (mins[2] > p[2]) mins[2] = p[2];
8606                                 if (maxs[0] < p[0]) maxs[0] = p[0];
8607                                 if (maxs[1] < p[1]) maxs[1] = p[1];
8608                                 if (maxs[2] < p[2]) maxs[2] = p[2];
8609                         }
8610                         else
8611                         {
8612                                 VectorCopy(p, mins);
8613                                 VectorCopy(p, maxs);
8614                         }
8615                 }
8616                 if (!R_ScissorForBBox(mins, maxs, scissor))
8617                 {
8618                         if (skyscissor[2])
8619                         {
8620                                 if (skyscissor[0] > scissor[0])
8621                                 {
8622                                         skyscissor[2] += skyscissor[0] - scissor[0];
8623                                         skyscissor[0] = scissor[0];
8624                                 }
8625                                 if (skyscissor[1] > scissor[1])
8626                                 {
8627                                         skyscissor[3] += skyscissor[1] - scissor[1];
8628                                         skyscissor[1] = scissor[1];
8629                                 }
8630                                 if (skyscissor[0] + skyscissor[2] < scissor[0] + scissor[2])
8631                                         skyscissor[2] = scissor[0] + scissor[2] - skyscissor[0];
8632                                 if (skyscissor[1] + skyscissor[3] < scissor[1] + scissor[3])
8633                                         skyscissor[3] = scissor[1] + scissor[3] - skyscissor[1];
8634                         }
8635                         else
8636                                 Vector4Copy(scissor, skyscissor);
8637                 }
8638         }
8639
8640         // LadyHavoc: HalfLife maps have freaky skypolys so don't use
8641         // skymasking on them, and Quake3 never did sky masking (unlike
8642         // software Quake and software Quake2), so disable the sky masking
8643         // in Quake3 maps as it causes problems with q3map2 sky tricks,
8644         // and skymasking also looks very bad when noclipping outside the
8645         // level, so don't use it then either.
8646         if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.skymasking && (r_refdef.scene.worldmodel->brush.isq3bsp ? r_q3bsp_renderskydepth.integer : r_q1bsp_skymasking.integer) && !r_refdef.viewcache.world_novis && !r_trippy.integer)
8647         {
8648                 R_Mesh_ResetTextureState();
8649                 if (skyrendermasked)
8650                 {
8651                         R_SetupShader_DepthOrShadow(false, false, false);
8652                         // depth-only (masking)
8653                         GL_ColorMask(0, 0, 0, 0);
8654                         // just to make sure that braindead drivers don't draw
8655                         // anything despite that colormask...
8656                         GL_BlendFunc(GL_ZERO, GL_ONE);
8657                         RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8658                         R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8659                 }
8660                 else
8661                 {
8662                         R_SetupShader_Generic_NoTexture(false, false);
8663                         // fog sky
8664                         GL_BlendFunc(GL_ONE, GL_ZERO);
8665                         RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
8666                         GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1);
8667                         R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
8668                 }
8669                 RSurf_DrawBatch();
8670                 if (skyrendermasked)
8671                         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
8672         }
8673         R_Mesh_ResetTextureState();
8674         GL_Color(1, 1, 1, 1);
8675 }
8676
8677 extern rtexture_t *r_shadow_prepasslightingdiffusetexture;
8678 extern rtexture_t *r_shadow_prepasslightingspeculartexture;
8679 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool prepass, qbool ui)
8680 {
8681         if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA)))
8682                 return;
8683         if (prepass)
8684         {
8685                 // render screenspace normalmap to texture
8686                 GL_DepthMask(true);
8687                 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_DEFERREDGEOMETRY, texturenumsurfaces, texturesurfacelist, NULL, false, false);
8688                 RSurf_DrawBatch();
8689                 return;
8690         }
8691
8692         // bind lightmap texture
8693
8694         // water/refraction/reflection/camera surfaces have to be handled specially
8695         if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA | MATERIALFLAG_REFLECTION)))
8696         {
8697                 int start, end, startplaneindex;
8698                 for (start = 0;start < texturenumsurfaces;start = end)
8699                 {
8700                         startplaneindex = RSurf_FindWaterPlaneForSurface(texturesurfacelist[start]);
8701                         if(startplaneindex < 0)
8702                         {
8703                                 // this happens if the plane e.g. got backface culled and thus didn't get a water plane. We can just ignore this.
8704                                 // Con_Printf("No matching water plane for surface with material flags 0x%08x - PLEASE DEBUG THIS\n", rsurface.texture->currentmaterialflags);
8705                                 end = start + 1;
8706                                 continue;
8707                         }
8708                         for (end = start + 1;end < texturenumsurfaces && startplaneindex == RSurf_FindWaterPlaneForSurface(texturesurfacelist[end]);end++)
8709                                 ;
8710                         // now that we have a batch using the same planeindex, render it
8711                         if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA)))
8712                         {
8713                                 // render water or distortion background
8714                                 GL_DepthMask(true);
8715                                 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BACKGROUND, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false, false);
8716                                 RSurf_DrawBatch();
8717                                 // blend surface on top
8718                                 GL_DepthMask(false);
8719                                 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, end-start, texturesurfacelist + start, NULL, false, false);
8720                                 RSurf_DrawBatch();
8721                         }
8722                         else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION))
8723                         {
8724                                 // render surface with reflection texture as input
8725                                 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
8726                                 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false, false);
8727                                 RSurf_DrawBatch();
8728                         }
8729                 }
8730                 return;
8731         }
8732
8733         // render surface batch normally
8734         GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
8735         R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, texturenumsurfaces, texturesurfacelist, NULL, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) != 0 || ui, ui);
8736         RSurf_DrawBatch();
8737 }
8738
8739 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth)
8740 {
8741         int vi;
8742         int j;
8743         int texturesurfaceindex;
8744         int k;
8745         const msurface_t *surface;
8746         float surfacecolor4f[4];
8747
8748 //      R_Mesh_ResetTextureState();
8749         R_SetupShader_Generic_NoTexture(false, false);
8750
8751         GL_BlendFunc(GL_ONE, GL_ZERO);
8752         GL_DepthMask(writedepth);
8753
8754         RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_ALWAYSCOPY, texturenumsurfaces, texturesurfacelist);
8755         vi = 0;
8756         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
8757         {
8758                 surface = texturesurfacelist[texturesurfaceindex];
8759                 k = (int)(((size_t)surface) / sizeof(msurface_t));
8760                 Vector4Set(surfacecolor4f, (k & 0xF) * (1.0f / 16.0f), (k & 0xF0) * (1.0f / 256.0f), (k & 0xF00) * (1.0f / 4096.0f), 1);
8761                 for (j = 0;j < surface->num_vertices;j++)
8762                 {
8763                         Vector4Copy(surfacecolor4f, rsurface.batchlightmapcolor4f + 4 * vi);
8764                         vi++;
8765                 }
8766         }
8767         R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchlightmapcolor4f, rsurface.batchtexcoordtexture2f);
8768         RSurf_DrawBatch();
8769 }
8770
8771 static void R_DrawModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool prepass, qbool ui)
8772 {
8773         CHECKGLERROR
8774         RSurf_SetupDepthAndCulling();
8775         if (r_showsurfaces.integer && r_refdef.view.showdebug)
8776         {
8777                 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist, writedepth);
8778                 return;
8779         }
8780         switch (vid.renderpath)
8781         {
8782         case RENDERPATH_GL32:
8783         case RENDERPATH_GLES2:
8784                 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8785                 break;
8786         }
8787         CHECKGLERROR
8788 }
8789
8790 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
8791 {
8792         int i, j;
8793         int texturenumsurfaces, endsurface;
8794         texture_t *texture;
8795         const msurface_t *surface;
8796         const msurface_t *texturesurfacelist[MESHQUEUE_TRANSPARENT_BATCHSIZE];
8797
8798         RSurf_ActiveModelEntity(ent, true, true, false);
8799
8800         if (r_transparentdepthmasking.integer)
8801         {
8802                 qbool setup = false;
8803                 for (i = 0;i < numsurfaces;i = j)
8804                 {
8805                         j = i + 1;
8806                         surface = rsurface.modelsurfaces + surfacelist[i];
8807                         texture = surface->texture;
8808                         rsurface.texture = R_GetCurrentTexture(texture);
8809                         rsurface.lightmaptexture = NULL;
8810                         rsurface.deluxemaptexture = NULL;
8811                         rsurface.uselightmaptexture = false;
8812                         // scan ahead until we find a different texture
8813                         endsurface = min(i + 1024, numsurfaces);
8814                         texturenumsurfaces = 0;
8815                         texturesurfacelist[texturenumsurfaces++] = surface;
8816                         for (;j < endsurface;j++)
8817                         {
8818                                 surface = rsurface.modelsurfaces + surfacelist[j];
8819                                 if (texture != surface->texture)
8820                                         break;
8821                                 texturesurfacelist[texturenumsurfaces++] = surface;
8822                         }
8823                         if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_TRANSDEPTH))
8824                                 continue;
8825                         // render the range of surfaces as depth
8826                         if (!setup)
8827                         {
8828                                 setup = true;
8829                                 GL_ColorMask(0,0,0,0);
8830                                 GL_Color(1,1,1,1);
8831                                 GL_DepthTest(true);
8832                                 GL_BlendFunc(GL_ONE, GL_ZERO);
8833                                 GL_DepthMask(true);
8834 //                              R_Mesh_ResetTextureState();
8835                         }
8836                         RSurf_SetupDepthAndCulling();
8837                         RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8838                         R_SetupShader_DepthOrShadow(false, false, !!rsurface.batchskeletaltransform3x4);
8839                         R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8840                         RSurf_DrawBatch();
8841                 }
8842                 if (setup)
8843                         GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
8844         }
8845
8846         for (i = 0;i < numsurfaces;i = j)
8847         {
8848                 j = i + 1;
8849                 surface = rsurface.modelsurfaces + surfacelist[i];
8850                 texture = surface->texture;
8851                 rsurface.texture = R_GetCurrentTexture(texture);
8852                 // scan ahead until we find a different texture
8853                 endsurface = min(i + MESHQUEUE_TRANSPARENT_BATCHSIZE, numsurfaces);
8854                 texturenumsurfaces = 0;
8855                 texturesurfacelist[texturenumsurfaces++] = surface;
8856                         rsurface.lightmaptexture = surface->lightmaptexture;
8857                         rsurface.deluxemaptexture = surface->deluxemaptexture;
8858                         rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
8859                         for (;j < endsurface;j++)
8860                         {
8861                                 surface = rsurface.modelsurfaces + surfacelist[j];
8862                                 if (texture != surface->texture || rsurface.lightmaptexture != surface->lightmaptexture)
8863                                         break;
8864                                 texturesurfacelist[texturenumsurfaces++] = surface;
8865                         }
8866                 // render the range of surfaces
8867                 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, false, false, false);
8868         }
8869         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
8870 }
8871
8872 static void R_ProcessTransparentTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8873 {
8874         // transparent surfaces get pushed off into the transparent queue
8875         int surfacelistindex;
8876         const msurface_t *surface;
8877         vec3_t tempcenter, center;
8878         for (surfacelistindex = 0;surfacelistindex < texturenumsurfaces;surfacelistindex++)
8879         {
8880                 surface = texturesurfacelist[surfacelistindex];
8881                 if (r_transparent_sortsurfacesbynearest.integer)
8882                 {
8883                         tempcenter[0] = bound(surface->mins[0], rsurface.localvieworigin[0], surface->maxs[0]);
8884                         tempcenter[1] = bound(surface->mins[1], rsurface.localvieworigin[1], surface->maxs[1]);
8885                         tempcenter[2] = bound(surface->mins[2], rsurface.localvieworigin[2], surface->maxs[2]);
8886                 }
8887                 else
8888                 {
8889                         tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
8890                         tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
8891                         tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
8892                 }
8893                 Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
8894                 if (rsurface.entity->transparent_offset) // transparent offset
8895                 {
8896                         center[0] += r_refdef.view.forward[0]*rsurface.entity->transparent_offset;
8897                         center[1] += r_refdef.view.forward[1]*rsurface.entity->transparent_offset;
8898                         center[2] += r_refdef.view.forward[2]*rsurface.entity->transparent_offset;
8899                 }
8900                 R_MeshQueue_AddTransparent((rsurface.entity->flags & RENDER_WORLDOBJECT) ? TRANSPARENTSORT_SKY : (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) ? TRANSPARENTSORT_HUD : rsurface.texture->transparentsort, center, R_DrawSurface_TransparentCallback, rsurface.entity, surface - rsurface.modelsurfaces, rsurface.rtlight);
8901         }
8902 }
8903
8904 static void R_DrawTextureSurfaceList_DepthOnly(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8905 {
8906         if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
8907                 return;
8908         if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
8909                 return;
8910         RSurf_SetupDepthAndCulling();
8911         RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8912         R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8913         R_SetupShader_DepthOrShadow(false, false, !!rsurface.batchskeletaltransform3x4);
8914         RSurf_DrawBatch();
8915 }
8916
8917 static void R_ProcessModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool depthonly, qbool prepass, qbool ui)
8918 {
8919         CHECKGLERROR
8920         if (ui)
8921                 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8922         else if (depthonly)
8923                 R_DrawTextureSurfaceList_DepthOnly(texturenumsurfaces, texturesurfacelist);
8924         else if (prepass)
8925         {
8926                 if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_WALL))
8927                         return;
8928                 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
8929                         R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
8930                 else
8931                         R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8932         }
8933         else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) && (!r_showsurfaces.integer || r_showsurfaces.integer == 3))
8934                 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
8935         else if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_WALL))
8936                 return;
8937         else if (((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED) || (r_showsurfaces.integer == 3 && (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST))))
8938         {
8939                 // in the deferred case, transparent surfaces were queued during prepass
8940                 if (!r_shadow_usingdeferredprepass)
8941                         R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
8942         }
8943         else
8944         {
8945                 // the alphatest check is to make sure we write depth for anything we skipped on the depth-only pass earlier
8946                 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST), prepass, ui);
8947         }
8948         CHECKGLERROR
8949 }
8950
8951 static void R_QueueModelSurfaceList(entity_render_t *ent, int numsurfaces, const msurface_t **surfacelist, int flagsmask, qbool writedepth, qbool depthonly, qbool prepass, qbool ui)
8952 {
8953         int i, j;
8954         texture_t *texture;
8955         R_FrameData_SetMark();
8956         // break the surface list down into batches by texture and use of lightmapping
8957         for (i = 0;i < numsurfaces;i = j)
8958         {
8959                 j = i + 1;
8960                 // texture is the base texture pointer, rsurface.texture is the
8961                 // current frame/skin the texture is directing us to use (for example
8962                 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
8963                 // use skin 1 instead)
8964                 texture = surfacelist[i]->texture;
8965                 rsurface.texture = R_GetCurrentTexture(texture);
8966                 if (!(rsurface.texture->currentmaterialflags & flagsmask) || (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW))
8967                 {
8968                         // if this texture is not the kind we want, skip ahead to the next one
8969                         for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
8970                                 ;
8971                         continue;
8972                 }
8973                 if(depthonly || prepass)
8974                 {
8975                         rsurface.lightmaptexture = NULL;
8976                         rsurface.deluxemaptexture = NULL;
8977                         rsurface.uselightmaptexture = false;
8978                         // simply scan ahead until we find a different texture or lightmap state
8979                         for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
8980                                 ;
8981                 }
8982                 else
8983                 {
8984                         rsurface.lightmaptexture = surfacelist[i]->lightmaptexture;
8985                         rsurface.deluxemaptexture = surfacelist[i]->deluxemaptexture;
8986                         rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
8987                         // simply scan ahead until we find a different texture or lightmap state
8988                         for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.lightmaptexture == surfacelist[j]->lightmaptexture;j++)
8989                                 ;
8990                 }
8991                 // render the range of surfaces
8992                 R_ProcessModelTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly, prepass, ui);
8993         }
8994         R_FrameData_ReturnToMark();
8995 }
8996
8997 float locboxvertex3f[6*4*3] =
8998 {
8999         1,0,1, 1,0,0, 1,1,0, 1,1,1,
9000         0,1,1, 0,1,0, 0,0,0, 0,0,1,
9001         1,1,1, 1,1,0, 0,1,0, 0,1,1,
9002         0,0,1, 0,0,0, 1,0,0, 1,0,1,
9003         0,0,1, 1,0,1, 1,1,1, 0,1,1,
9004         1,0,0, 0,0,0, 0,1,0, 1,1,0
9005 };
9006
9007 unsigned short locboxelements[6*2*3] =
9008 {
9009          0, 1, 2, 0, 2, 3,
9010          4, 5, 6, 4, 6, 7,
9011          8, 9,10, 8,10,11,
9012         12,13,14, 12,14,15,
9013         16,17,18, 16,18,19,
9014         20,21,22, 20,22,23
9015 };
9016
9017 static void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
9018 {
9019         int i, j;
9020         cl_locnode_t *loc = (cl_locnode_t *)ent;
9021         vec3_t mins, size;
9022         float vertex3f[6*4*3];
9023         CHECKGLERROR
9024         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9025         GL_DepthMask(false);
9026         GL_DepthRange(0, 1);
9027         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
9028         GL_DepthTest(true);
9029         GL_CullFace(GL_NONE);
9030         R_EntityMatrix(&identitymatrix);
9031
9032 //      R_Mesh_ResetTextureState();
9033
9034         i = surfacelist[0];
9035         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9036                          ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9037                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9038                         surfacelist[0] < 0 ? 0.5f : 0.125f);
9039
9040         if (VectorCompare(loc->mins, loc->maxs))
9041         {
9042                 VectorSet(size, 2, 2, 2);
9043                 VectorMA(loc->mins, -0.5f, size, mins);
9044         }
9045         else
9046         {
9047                 VectorCopy(loc->mins, mins);
9048                 VectorSubtract(loc->maxs, loc->mins, size);
9049         }
9050
9051         for (i = 0;i < 6*4*3;)
9052                 for (j = 0;j < 3;j++, i++)
9053                         vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
9054
9055         R_Mesh_PrepareVertices_Generic_Arrays(6*4, vertex3f, NULL, NULL);
9056         R_SetupShader_Generic_NoTexture(false, false);
9057         R_Mesh_Draw(0, 6*4, 0, 6*2, NULL, NULL, 0, locboxelements, NULL, 0);
9058 }
9059
9060 void R_DrawLocs(void)
9061 {
9062         int index;
9063         cl_locnode_t *loc, *nearestloc;
9064         vec3_t center;
9065         nearestloc = CL_Locs_FindNearest(cl.movement_origin);
9066         for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
9067         {
9068                 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
9069                 R_MeshQueue_AddTransparent(TRANSPARENTSORT_DISTANCE, center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
9070         }
9071 }
9072
9073 void R_DecalSystem_Reset(decalsystem_t *decalsystem)
9074 {
9075         if (decalsystem->decals)
9076                 Mem_Free(decalsystem->decals);
9077         memset(decalsystem, 0, sizeof(*decalsystem));
9078 }
9079
9080 static void R_DecalSystem_SpawnTriangle(decalsystem_t *decalsystem, const float *v0, const float *v1, const float *v2, const float *t0, const float *t1, const float *t2, const float *c0, const float *c1, const float *c2, int triangleindex, int surfaceindex, unsigned int decalsequence)
9081 {
9082         tridecal_t *decal;
9083         tridecal_t *decals;
9084         int i;
9085
9086         // expand or initialize the system
9087         if (decalsystem->maxdecals <= decalsystem->numdecals)
9088         {
9089                 decalsystem_t old = *decalsystem;
9090                 qbool useshortelements;
9091                 decalsystem->maxdecals = max(16, decalsystem->maxdecals * 2);
9092                 useshortelements = decalsystem->maxdecals * 3 <= 65536;
9093                 decalsystem->decals = (tridecal_t *)Mem_Alloc(cls.levelmempool, decalsystem->maxdecals * (sizeof(tridecal_t) + sizeof(float[3][3]) + sizeof(float[3][2]) + sizeof(float[3][4]) + sizeof(int[3]) + (useshortelements ? sizeof(unsigned short[3]) : 0)));
9094                 decalsystem->color4f = (float *)(decalsystem->decals + decalsystem->maxdecals);
9095                 decalsystem->texcoord2f = (float *)(decalsystem->color4f + decalsystem->maxdecals*12);
9096                 decalsystem->vertex3f = (float *)(decalsystem->texcoord2f + decalsystem->maxdecals*6);
9097                 decalsystem->element3i = (int *)(decalsystem->vertex3f + decalsystem->maxdecals*9);
9098                 decalsystem->element3s = (useshortelements ? ((unsigned short *)(decalsystem->element3i + decalsystem->maxdecals*3)) : NULL);
9099                 if (decalsystem->numdecals)
9100                         memcpy(decalsystem->decals, old.decals, decalsystem->numdecals * sizeof(tridecal_t));
9101                 if (old.decals)
9102                         Mem_Free(old.decals);
9103                 for (i = 0;i < decalsystem->maxdecals*3;i++)
9104                         decalsystem->element3i[i] = i;
9105                 if (useshortelements)
9106                         for (i = 0;i < decalsystem->maxdecals*3;i++)
9107                                 decalsystem->element3s[i] = i;
9108         }
9109
9110         // grab a decal and search for another free slot for the next one
9111         decals = decalsystem->decals;
9112         decal = decalsystem->decals + (i = decalsystem->freedecal++);
9113         for (i = decalsystem->freedecal;i < decalsystem->numdecals && decals[i].color4f[0][3];i++)
9114                 ;
9115         decalsystem->freedecal = i;
9116         if (decalsystem->numdecals <= i)
9117                 decalsystem->numdecals = i + 1;
9118
9119         // initialize the decal
9120         decal->lived = 0;
9121         decal->triangleindex = triangleindex;
9122         decal->surfaceindex = surfaceindex;
9123         decal->decalsequence = decalsequence;
9124         decal->color4f[0][0] = c0[0];
9125         decal->color4f[0][1] = c0[1];
9126         decal->color4f[0][2] = c0[2];
9127         decal->color4f[0][3] = 1;
9128         decal->color4f[1][0] = c1[0];
9129         decal->color4f[1][1] = c1[1];
9130         decal->color4f[1][2] = c1[2];
9131         decal->color4f[1][3] = 1;
9132         decal->color4f[2][0] = c2[0];
9133         decal->color4f[2][1] = c2[1];
9134         decal->color4f[2][2] = c2[2];
9135         decal->color4f[2][3] = 1;
9136         decal->vertex3f[0][0] = v0[0];
9137         decal->vertex3f[0][1] = v0[1];
9138         decal->vertex3f[0][2] = v0[2];
9139         decal->vertex3f[1][0] = v1[0];
9140         decal->vertex3f[1][1] = v1[1];
9141         decal->vertex3f[1][2] = v1[2];
9142         decal->vertex3f[2][0] = v2[0];
9143         decal->vertex3f[2][1] = v2[1];
9144         decal->vertex3f[2][2] = v2[2];
9145         decal->texcoord2f[0][0] = t0[0];
9146         decal->texcoord2f[0][1] = t0[1];
9147         decal->texcoord2f[1][0] = t1[0];
9148         decal->texcoord2f[1][1] = t1[1];
9149         decal->texcoord2f[2][0] = t2[0];
9150         decal->texcoord2f[2][1] = t2[1];
9151         TriangleNormal(v0, v1, v2, decal->plane);
9152         VectorNormalize(decal->plane);
9153         decal->plane[3] = DotProduct(v0, decal->plane);
9154 }
9155
9156 extern cvar_t cl_decals_bias;
9157 extern cvar_t cl_decals_models;
9158 extern cvar_t cl_decals_newsystem_intensitymultiplier;
9159 // baseparms, parms, temps
9160 static void R_DecalSystem_SplatTriangle(decalsystem_t *decalsystem, float r, float g, float b, float a, float s1, float t1, float s2, float t2, unsigned int decalsequence, qbool dynamic, float (*planes)[4], matrix4x4_t *projection, int triangleindex, int surfaceindex)
9161 {
9162         int cornerindex;
9163         int index;
9164         float v[9][3];
9165         const float *vertex3f;
9166         const float *normal3f;
9167         int numpoints;
9168         float points[2][9][3];
9169         float temp[3];
9170         float tc[9][2];
9171         float f;
9172         float c[9][4];
9173         const int *e;
9174
9175         e = rsurface.modelelement3i + 3*triangleindex;
9176
9177         vertex3f = rsurface.modelvertex3f;
9178         normal3f = rsurface.modelnormal3f;
9179
9180         if (normal3f)
9181         {
9182                 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9183                 {
9184                         index = 3*e[cornerindex];
9185                         VectorMA(vertex3f + index, cl_decals_bias.value, normal3f + index, v[cornerindex]);
9186                 }
9187         }
9188         else
9189         {
9190                 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9191                 {
9192                         index = 3*e[cornerindex];
9193                         VectorCopy(vertex3f + index, v[cornerindex]);
9194                 }
9195         }
9196
9197         // cull backfaces
9198         //TriangleNormal(v[0], v[1], v[2], normal);
9199         //if (DotProduct(normal, localnormal) < 0.0f)
9200         //      continue;
9201         // clip by each of the box planes formed from the projection matrix
9202         // if anything survives, we emit the decal
9203         numpoints = PolygonF_Clip(3        , v[0]        , planes[0][0], planes[0][1], planes[0][2], planes[0][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[1][0]);
9204         if (numpoints < 3)
9205                 return;
9206         numpoints = PolygonF_Clip(numpoints, points[1][0], planes[1][0], planes[1][1], planes[1][2], planes[1][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[0][0]);
9207         if (numpoints < 3)
9208                 return;
9209         numpoints = PolygonF_Clip(numpoints, points[0][0], planes[2][0], planes[2][1], planes[2][2], planes[2][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[1][0]);
9210         if (numpoints < 3)
9211                 return;
9212         numpoints = PolygonF_Clip(numpoints, points[1][0], planes[3][0], planes[3][1], planes[3][2], planes[3][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[0][0]);
9213         if (numpoints < 3)
9214                 return;
9215         numpoints = PolygonF_Clip(numpoints, points[0][0], planes[4][0], planes[4][1], planes[4][2], planes[4][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[1][0]);
9216         if (numpoints < 3)
9217                 return;
9218         numpoints = PolygonF_Clip(numpoints, points[1][0], planes[5][0], planes[5][1], planes[5][2], planes[5][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), v[0]);
9219         if (numpoints < 3)
9220                 return;
9221         // some part of the triangle survived, so we have to accept it...
9222         if (dynamic)
9223         {
9224                 // dynamic always uses the original triangle
9225                 numpoints = 3;
9226                 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9227                 {
9228                         index = 3*e[cornerindex];
9229                         VectorCopy(vertex3f + index, v[cornerindex]);
9230                 }
9231         }
9232         for (cornerindex = 0;cornerindex < numpoints;cornerindex++)
9233         {
9234                 // convert vertex positions to texcoords
9235                 Matrix4x4_Transform(projection, v[cornerindex], temp);
9236                 tc[cornerindex][0] = (temp[1]+1.0f)*0.5f * (s2-s1) + s1;
9237                 tc[cornerindex][1] = (temp[2]+1.0f)*0.5f * (t2-t1) + t1;
9238                 // calculate distance fade from the projection origin
9239                 f = a * (1.0f-fabs(temp[0])) * cl_decals_newsystem_intensitymultiplier.value;
9240                 f = bound(0.0f, f, 1.0f);
9241                 c[cornerindex][0] = r * f;
9242                 c[cornerindex][1] = g * f;
9243                 c[cornerindex][2] = b * f;
9244                 c[cornerindex][3] = 1.0f;
9245                 //VectorMA(v[cornerindex], cl_decals_bias.value, localnormal, v[cornerindex]);
9246         }
9247         if (dynamic)
9248                 R_DecalSystem_SpawnTriangle(decalsystem, v[0], v[1], v[2], tc[0], tc[1], tc[2], c[0], c[1], c[2], triangleindex, surfaceindex, decalsequence);
9249         else
9250                 for (cornerindex = 0;cornerindex < numpoints-2;cornerindex++)
9251                         R_DecalSystem_SpawnTriangle(decalsystem, v[0], v[cornerindex+1], v[cornerindex+2], tc[0], tc[cornerindex+1], tc[cornerindex+2], c[0], c[cornerindex+1], c[cornerindex+2], -1, surfaceindex, decalsequence);
9252 }
9253 static void R_DecalSystem_SplatEntity(entity_render_t *ent, const vec3_t worldorigin, const vec3_t worldnormal, float r, float g, float b, float a, float s1, float t1, float s2, float t2, float worldsize, unsigned int decalsequence)
9254 {
9255         matrix4x4_t projection;
9256         decalsystem_t *decalsystem;
9257         qbool dynamic;
9258         model_t *model;
9259         const msurface_t *surface;
9260         const msurface_t *surfaces;
9261         const texture_t *texture;
9262         int numtriangles;
9263         int surfaceindex;
9264         int triangleindex;
9265         float localorigin[3];
9266         float localnormal[3];
9267         float localmins[3];
9268         float localmaxs[3];
9269         float localsize;
9270         //float normal[3];
9271         float planes[6][4];
9272         float angles[3];
9273         bih_t *bih;
9274         int bih_triangles_count;
9275         int bih_triangles[256];
9276         int bih_surfaces[256];
9277
9278         decalsystem = &ent->decalsystem;
9279         model = ent->model;
9280         if (!model || !ent->allowdecals || ent->alpha < 1 || (ent->flags & (RENDER_ADDITIVE | RENDER_NODEPTHTEST)))
9281         {
9282                 R_DecalSystem_Reset(&ent->decalsystem);
9283                 return;
9284         }
9285
9286         if (!model->brush.data_leafs && !cl_decals_models.integer)
9287         {
9288                 if (decalsystem->model)
9289                         R_DecalSystem_Reset(decalsystem);
9290                 return;
9291         }
9292
9293         if (decalsystem->model != model)
9294                 R_DecalSystem_Reset(decalsystem);
9295         decalsystem->model = model;
9296
9297         RSurf_ActiveModelEntity(ent, true, false, false);
9298
9299         Matrix4x4_Transform(&rsurface.inversematrix, worldorigin, localorigin);
9300         Matrix4x4_Transform3x3(&rsurface.inversematrix, worldnormal, localnormal);
9301         VectorNormalize(localnormal);
9302         localsize = worldsize*rsurface.inversematrixscale;
9303         localmins[0] = localorigin[0] - localsize;
9304         localmins[1] = localorigin[1] - localsize;
9305         localmins[2] = localorigin[2] - localsize;
9306         localmaxs[0] = localorigin[0] + localsize;
9307         localmaxs[1] = localorigin[1] + localsize;
9308         localmaxs[2] = localorigin[2] + localsize;
9309
9310         //VectorCopy(localnormal, planes[4]);
9311         //VectorVectors(planes[4], planes[2], planes[0]);
9312         AnglesFromVectors(angles, localnormal, NULL, false);
9313         AngleVectors(angles, planes[0], planes[2], planes[4]);
9314         VectorNegate(planes[0], planes[1]);
9315         VectorNegate(planes[2], planes[3]);
9316         VectorNegate(planes[4], planes[5]);
9317         planes[0][3] = DotProduct(planes[0], localorigin) - localsize;
9318         planes[1][3] = DotProduct(planes[1], localorigin) - localsize;
9319         planes[2][3] = DotProduct(planes[2], localorigin) - localsize;
9320         planes[3][3] = DotProduct(planes[3], localorigin) - localsize;
9321         planes[4][3] = DotProduct(planes[4], localorigin) - localsize;
9322         planes[5][3] = DotProduct(planes[5], localorigin) - localsize;
9323
9324 #if 1
9325 // works
9326 {
9327         matrix4x4_t forwardprojection;
9328         Matrix4x4_CreateFromQuakeEntity(&forwardprojection, localorigin[0], localorigin[1], localorigin[2], angles[0], angles[1], angles[2], localsize);
9329         Matrix4x4_Invert_Simple(&projection, &forwardprojection);
9330 }
9331 #else
9332 // broken
9333 {
9334         float projectionvector[4][3];
9335         VectorScale(planes[0], ilocalsize, projectionvector[0]);
9336         VectorScale(planes[2], ilocalsize, projectionvector[1]);
9337         VectorScale(planes[4], ilocalsize, projectionvector[2]);
9338         projectionvector[0][0] = planes[0][0] * ilocalsize;
9339         projectionvector[0][1] = planes[1][0] * ilocalsize;
9340         projectionvector[0][2] = planes[2][0] * ilocalsize;
9341         projectionvector[1][0] = planes[0][1] * ilocalsize;
9342         projectionvector[1][1] = planes[1][1] * ilocalsize;
9343         projectionvector[1][2] = planes[2][1] * ilocalsize;
9344         projectionvector[2][0] = planes[0][2] * ilocalsize;
9345         projectionvector[2][1] = planes[1][2] * ilocalsize;
9346         projectionvector[2][2] = planes[2][2] * ilocalsize;
9347         projectionvector[3][0] = -(localorigin[0]*projectionvector[0][0]+localorigin[1]*projectionvector[1][0]+localorigin[2]*projectionvector[2][0]);
9348         projectionvector[3][1] = -(localorigin[0]*projectionvector[0][1]+localorigin[1]*projectionvector[1][1]+localorigin[2]*projectionvector[2][1]);
9349         projectionvector[3][2] = -(localorigin[0]*projectionvector[0][2]+localorigin[1]*projectionvector[1][2]+localorigin[2]*projectionvector[2][2]);
9350         Matrix4x4_FromVectors(&projection, projectionvector[0], projectionvector[1], projectionvector[2], projectionvector[3]);
9351 }
9352 #endif
9353
9354         dynamic = model->surfmesh.isanimated;
9355         surfaces = model->data_surfaces;
9356
9357         bih = NULL;
9358         bih_triangles_count = -1;
9359         if(!dynamic)
9360         {
9361                 if(model->render_bih.numleafs)
9362                         bih = &model->render_bih;
9363                 else if(model->collision_bih.numleafs)
9364                         bih = &model->collision_bih;
9365         }
9366         if(bih)
9367                 bih_triangles_count = BIH_GetTriangleListForBox(bih, sizeof(bih_triangles) / sizeof(*bih_triangles), bih_triangles, bih_surfaces, localmins, localmaxs);
9368         if(bih_triangles_count == 0)
9369                 return;
9370         if(bih_triangles_count > (int) (sizeof(bih_triangles) / sizeof(*bih_triangles))) // hit too many, likely bad anyway
9371                 return;
9372         if(bih_triangles_count > 0)
9373         {
9374                 for (triangleindex = 0; triangleindex < bih_triangles_count; ++triangleindex)
9375                 {
9376                         surfaceindex = bih_surfaces[triangleindex];
9377                         surface = surfaces + surfaceindex;
9378                         texture = surface->texture;
9379                         if (!texture)
9380                                 continue;
9381                         if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
9382                                 continue;
9383                         if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
9384                                 continue;
9385                         R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, bih_triangles[triangleindex], surfaceindex);
9386                 }
9387         }
9388         else
9389         {
9390                 for (surfaceindex = model->submodelsurfaces_start;surfaceindex < model->submodelsurfaces_end;surfaceindex++)
9391                 {
9392                         surface = surfaces + surfaceindex;
9393                         // check cull box first because it rejects more than any other check
9394                         if (!dynamic && !BoxesOverlap(surface->mins, surface->maxs, localmins, localmaxs))
9395                                 continue;
9396                         // skip transparent surfaces
9397                         texture = surface->texture;
9398                         if (!texture)
9399                                 continue;
9400                         if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
9401                                 continue;
9402                         if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
9403                                 continue;
9404                         numtriangles = surface->num_triangles;
9405                         for (triangleindex = 0; triangleindex < numtriangles; triangleindex++)
9406                                 R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, triangleindex + surface->num_firsttriangle, surfaceindex);
9407                 }
9408         }
9409 }
9410
9411 // do not call this outside of rendering code - use R_DecalSystem_SplatEntities instead
9412 static void R_DecalSystem_ApplySplatEntities(const vec3_t worldorigin, const vec3_t worldnormal, float r, float g, float b, float a, float s1, float t1, float s2, float t2, float worldsize, unsigned int decalsequence)
9413 {
9414         int renderentityindex;
9415         float worldmins[3];
9416         float worldmaxs[3];
9417         entity_render_t *ent;
9418
9419         worldmins[0] = worldorigin[0] - worldsize;
9420         worldmins[1] = worldorigin[1] - worldsize;
9421         worldmins[2] = worldorigin[2] - worldsize;
9422         worldmaxs[0] = worldorigin[0] + worldsize;
9423         worldmaxs[1] = worldorigin[1] + worldsize;
9424         worldmaxs[2] = worldorigin[2] + worldsize;
9425
9426         R_DecalSystem_SplatEntity(r_refdef.scene.worldentity, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
9427
9428         for (renderentityindex = 0;renderentityindex < r_refdef.scene.numentities;renderentityindex++)
9429         {
9430                 ent = r_refdef.scene.entities[renderentityindex];
9431                 if (!BoxesOverlap(ent->mins, ent->maxs, worldmins, worldmaxs))
9432                         continue;
9433
9434                 R_DecalSystem_SplatEntity(ent, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
9435         }
9436 }
9437
9438 typedef struct r_decalsystem_splatqueue_s
9439 {
9440         vec3_t worldorigin;
9441         vec3_t worldnormal;
9442         float color[4];
9443         float tcrange[4];
9444         float worldsize;
9445         unsigned int decalsequence;
9446 }
9447 r_decalsystem_splatqueue_t;
9448
9449 int r_decalsystem_numqueued = 0;
9450 r_decalsystem_splatqueue_t r_decalsystem_queue[MAX_DECALSYSTEM_QUEUE];
9451
9452 void R_DecalSystem_SplatEntities(const vec3_t worldorigin, const vec3_t worldnormal, float r, float g, float b, float a, float s1, float t1, float s2, float t2, float worldsize)
9453 {
9454         r_decalsystem_splatqueue_t *queue;
9455
9456         if (r_decalsystem_numqueued == MAX_DECALSYSTEM_QUEUE)
9457                 return;
9458
9459         queue = &r_decalsystem_queue[r_decalsystem_numqueued++];
9460         VectorCopy(worldorigin, queue->worldorigin);
9461         VectorCopy(worldnormal, queue->worldnormal);
9462         Vector4Set(queue->color, r, g, b, a);
9463         Vector4Set(queue->tcrange, s1, t1, s2, t2);
9464         queue->worldsize = worldsize;
9465         queue->decalsequence = cl.decalsequence++;
9466 }
9467
9468 static void R_DecalSystem_ApplySplatEntitiesQueue(void)
9469 {
9470         int i;
9471         r_decalsystem_splatqueue_t *queue;
9472
9473         for (i = 0, queue = r_decalsystem_queue;i < r_decalsystem_numqueued;i++, queue++)
9474                 R_DecalSystem_ApplySplatEntities(queue->worldorigin, queue->worldnormal, queue->color[0], queue->color[1], queue->color[2], queue->color[3], queue->tcrange[0], queue->tcrange[1], queue->tcrange[2], queue->tcrange[3], queue->worldsize, queue->decalsequence);
9475         r_decalsystem_numqueued = 0;
9476 }
9477
9478 extern cvar_t cl_decals_max;
9479 static void R_DrawModelDecals_FadeEntity(entity_render_t *ent)
9480 {
9481         int i;
9482         decalsystem_t *decalsystem = &ent->decalsystem;
9483         int numdecals;
9484         unsigned int killsequence;
9485         tridecal_t *decal;
9486         float frametime;
9487         float lifetime;
9488
9489         if (!decalsystem->numdecals)
9490                 return;
9491
9492         if (r_showsurfaces.integer)
9493                 return;
9494
9495         if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
9496         {
9497                 R_DecalSystem_Reset(decalsystem);
9498                 return;
9499         }
9500
9501         killsequence = cl.decalsequence - bound(1, (unsigned int) cl_decals_max.integer, cl.decalsequence);
9502         lifetime = cl_decals_time.value + cl_decals_fadetime.value;
9503
9504         if (decalsystem->lastupdatetime)
9505                 frametime = (r_refdef.scene.time - decalsystem->lastupdatetime);
9506         else
9507                 frametime = 0;
9508         decalsystem->lastupdatetime = r_refdef.scene.time;
9509         numdecals = decalsystem->numdecals;
9510
9511         for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
9512         {
9513                 if (decal->color4f[0][3])
9514                 {
9515                         decal->lived += frametime;
9516                         if (killsequence > decal->decalsequence || decal->lived >= lifetime)
9517                         {
9518                                 memset(decal, 0, sizeof(*decal));
9519                                 if (decalsystem->freedecal > i)
9520                                         decalsystem->freedecal = i;
9521                         }
9522                 }
9523         }
9524         decal = decalsystem->decals;
9525         while (numdecals > 0 && !decal[numdecals-1].color4f[0][3])
9526                 numdecals--;
9527
9528         // collapse the array by shuffling the tail decals into the gaps
9529         for (;;)
9530         {
9531                 while (decalsystem->freedecal < numdecals && decal[decalsystem->freedecal].color4f[0][3])
9532                         decalsystem->freedecal++;
9533                 if (decalsystem->freedecal == numdecals)
9534                         break;
9535                 decal[decalsystem->freedecal] = decal[--numdecals];
9536         }
9537
9538         decalsystem->numdecals = numdecals;
9539
9540         if (numdecals <= 0)
9541         {
9542                 // if there are no decals left, reset decalsystem
9543                 R_DecalSystem_Reset(decalsystem);
9544         }
9545 }
9546
9547 extern skinframe_t *decalskinframe;
9548 static void R_DrawModelDecals_Entity(entity_render_t *ent)
9549 {
9550         int i;
9551         decalsystem_t *decalsystem = &ent->decalsystem;
9552         int numdecals;
9553         tridecal_t *decal;
9554         float faderate;
9555         float alpha;
9556         float *v3f;
9557         float *c4f;
9558         float *t2f;
9559         const int *e;
9560         const unsigned char *surfacevisible = ent == r_refdef.scene.worldentity ? r_refdef.viewcache.world_surfacevisible : NULL;
9561         int numtris = 0;
9562
9563         numdecals = decalsystem->numdecals;
9564         if (!numdecals)
9565                 return;
9566
9567         if (r_showsurfaces.integer)
9568                 return;
9569
9570         if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
9571         {
9572                 R_DecalSystem_Reset(decalsystem);
9573                 return;
9574         }
9575
9576         // if the model is static it doesn't matter what value we give for
9577         // wantnormals and wanttangents, so this logic uses only rules applicable
9578         // to a model, knowing that they are meaningless otherwise
9579         RSurf_ActiveModelEntity(ent, false, false, false);
9580
9581         decalsystem->lastupdatetime = r_refdef.scene.time;
9582
9583         faderate = 1.0f / max(0.001f, cl_decals_fadetime.value);
9584
9585         // update vertex positions for animated models
9586         v3f = decalsystem->vertex3f;
9587         c4f = decalsystem->color4f;
9588         t2f = decalsystem->texcoord2f;
9589         for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
9590         {
9591                 if (!decal->color4f[0][3])
9592                         continue;
9593
9594                 if (surfacevisible && !surfacevisible[decal->surfaceindex])
9595                         continue;
9596
9597                 // skip backfaces
9598                 if (decal->triangleindex < 0 && DotProduct(r_refdef.view.origin, decal->plane) < decal->plane[3])
9599                         continue;
9600
9601                 // update color values for fading decals
9602                 if (decal->lived >= cl_decals_time.value)
9603                         alpha = 1 - faderate * (decal->lived - cl_decals_time.value);
9604                 else
9605                         alpha = 1.0f;
9606
9607                 c4f[ 0] = decal->color4f[0][0] * alpha;
9608                 c4f[ 1] = decal->color4f[0][1] * alpha;
9609                 c4f[ 2] = decal->color4f[0][2] * alpha;
9610                 c4f[ 3] = 1;
9611                 c4f[ 4] = decal->color4f[1][0] * alpha;
9612                 c4f[ 5] = decal->color4f[1][1] * alpha;
9613                 c4f[ 6] = decal->color4f[1][2] * alpha;
9614                 c4f[ 7] = 1;
9615                 c4f[ 8] = decal->color4f[2][0] * alpha;
9616                 c4f[ 9] = decal->color4f[2][1] * alpha;
9617                 c4f[10] = decal->color4f[2][2] * alpha;
9618                 c4f[11] = 1;
9619
9620                 t2f[0] = decal->texcoord2f[0][0];
9621                 t2f[1] = decal->texcoord2f[0][1];
9622                 t2f[2] = decal->texcoord2f[1][0];
9623                 t2f[3] = decal->texcoord2f[1][1];
9624                 t2f[4] = decal->texcoord2f[2][0];
9625                 t2f[5] = decal->texcoord2f[2][1];
9626
9627                 // update vertex positions for animated models
9628                 if (decal->triangleindex >= 0 && decal->triangleindex < rsurface.modelnumtriangles)
9629                 {
9630                         e = rsurface.modelelement3i + 3*decal->triangleindex;
9631                         VectorCopy(rsurface.modelvertex3f + 3*e[0], v3f);
9632                         VectorCopy(rsurface.modelvertex3f + 3*e[1], v3f + 3);
9633                         VectorCopy(rsurface.modelvertex3f + 3*e[2], v3f + 6);
9634                 }
9635                 else
9636                 {
9637                         VectorCopy(decal->vertex3f[0], v3f);
9638                         VectorCopy(decal->vertex3f[1], v3f + 3);
9639                         VectorCopy(decal->vertex3f[2], v3f + 6);
9640                 }
9641
9642                 if (r_refdef.fogenabled)
9643                 {
9644                         alpha = RSurf_FogVertex(v3f);
9645                         VectorScale(c4f, alpha, c4f);
9646                         alpha = RSurf_FogVertex(v3f + 3);
9647                         VectorScale(c4f + 4, alpha, c4f + 4);
9648                         alpha = RSurf_FogVertex(v3f + 6);
9649                         VectorScale(c4f + 8, alpha, c4f + 8);
9650                 }
9651
9652                 v3f += 9;
9653                 c4f += 12;
9654                 t2f += 6;
9655                 numtris++;
9656         }
9657
9658         if (numtris > 0)
9659         {
9660                 r_refdef.stats[r_stat_drawndecals] += numtris;
9661
9662                 // now render the decals all at once
9663                 // (this assumes they all use one particle font texture!)
9664                 RSurf_ActiveCustomEntity(&rsurface.matrix, &rsurface.inversematrix, rsurface.ent_flags, ent->shadertime, 1, 1, 1, 1, numdecals*3, decalsystem->vertex3f, decalsystem->texcoord2f, NULL, NULL, NULL, decalsystem->color4f, numtris, decalsystem->element3i, decalsystem->element3s, false, false);
9665 //              R_Mesh_ResetTextureState();
9666                 R_Mesh_PrepareVertices_Generic_Arrays(numtris * 3, decalsystem->vertex3f, decalsystem->color4f, decalsystem->texcoord2f);
9667                 GL_DepthMask(false);
9668                 GL_DepthRange(0, 1);
9669                 GL_PolygonOffset(rsurface.basepolygonfactor + r_polygonoffset_decals_factor.value, rsurface.basepolygonoffset + r_polygonoffset_decals_offset.value);
9670                 GL_DepthTest(true);
9671                 GL_CullFace(GL_NONE);
9672                 GL_BlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
9673                 R_SetupShader_Generic(decalskinframe->base, false, false, false);
9674                 R_Mesh_Draw(0, numtris * 3, 0, numtris, decalsystem->element3i, NULL, 0, decalsystem->element3s, NULL, 0);
9675         }
9676 }
9677
9678 static void R_DrawModelDecals(void)
9679 {
9680         int i, numdecals;
9681
9682         // fade faster when there are too many decals
9683         numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
9684         for (i = 0;i < r_refdef.scene.numentities;i++)
9685                 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
9686
9687         R_DrawModelDecals_FadeEntity(r_refdef.scene.worldentity);
9688         for (i = 0;i < r_refdef.scene.numentities;i++)
9689                 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
9690                         R_DrawModelDecals_FadeEntity(r_refdef.scene.entities[i]);
9691
9692         R_DecalSystem_ApplySplatEntitiesQueue();
9693
9694         numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
9695         for (i = 0;i < r_refdef.scene.numentities;i++)
9696                 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
9697
9698         r_refdef.stats[r_stat_totaldecals] += numdecals;
9699
9700         if (r_showsurfaces.integer || !r_drawdecals.integer)
9701                 return;
9702
9703         R_DrawModelDecals_Entity(r_refdef.scene.worldentity);
9704
9705         for (i = 0;i < r_refdef.scene.numentities;i++)
9706         {
9707                 if (!r_refdef.viewcache.entityvisible[i])
9708                         continue;
9709                 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
9710                         R_DrawModelDecals_Entity(r_refdef.scene.entities[i]);
9711         }
9712 }
9713
9714 static void R_DrawDebugModel(void)
9715 {
9716         entity_render_t *ent = rsurface.entity;
9717         int j, flagsmask;
9718         const msurface_t *surface;
9719         model_t *model = ent->model;
9720
9721         if (!sv.active  && !cls.demoplayback && ent != r_refdef.scene.worldentity)
9722                 return;
9723
9724         if (r_showoverdraw.value > 0)
9725         {
9726                 float c = r_refdef.view.colorscale * r_showoverdraw.value * 0.125f;
9727                 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
9728                 R_SetupShader_Generic_NoTexture(false, false);
9729                 GL_DepthTest(false);
9730                 GL_DepthMask(false);
9731                 GL_DepthRange(0, 1);
9732                 GL_BlendFunc(GL_ONE, GL_ONE);
9733                 for (j = model->submodelsurfaces_start;j < model->submodelsurfaces_end;j++)
9734                 {
9735                         if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9736                                 continue;
9737                         surface = model->data_surfaces + j;
9738                         rsurface.texture = R_GetCurrentTexture(surface->texture);
9739                         if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9740                         {
9741                                 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, 1, &surface);
9742                                 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
9743                                 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
9744                                         GL_Color(c, 0, 0, 1.0f);
9745                                 else if (ent == r_refdef.scene.worldentity)
9746                                         GL_Color(c, c, c, 1.0f);
9747                                 else
9748                                         GL_Color(0, c, 0, 1.0f);
9749                                 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
9750                                 RSurf_DrawBatch();
9751                         }
9752                 }
9753                 rsurface.texture = NULL;
9754         }
9755
9756         flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
9757
9758 //      R_Mesh_ResetTextureState();
9759         R_SetupShader_Generic_NoTexture(false, false);
9760         GL_DepthRange(0, 1);
9761         GL_DepthTest(!r_showdisabledepthtest.integer);
9762         GL_DepthMask(false);
9763         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9764
9765         if (r_showcollisionbrushes.value > 0 && model->collision_bih.numleafs)
9766         {
9767                 int triangleindex;
9768                 int bihleafindex;
9769                 qbool cullbox = false;
9770                 const q3mbrush_t *brush;
9771                 const bih_t *bih = &model->collision_bih;
9772                 const bih_leaf_t *bihleaf;
9773                 float vertex3f[3][3];
9774                 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
9775                 for (bihleafindex = 0, bihleaf = bih->leafs;bihleafindex < bih->numleafs;bihleafindex++, bihleaf++)
9776                 {
9777                         if (cullbox && R_CullFrustum(bihleaf->mins, bihleaf->maxs))
9778                                 continue;
9779                         switch (bihleaf->type)
9780                         {
9781                         case BIH_BRUSH:
9782                                 brush = model->brush.data_brushes + bihleaf->itemindex;
9783                                 if (brush->colbrushf && brush->colbrushf->numtriangles)
9784                                 {
9785                                         GL_Color((bihleafindex & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 5) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 10) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, r_showcollisionbrushes.value);
9786                                         R_Mesh_PrepareVertices_Generic_Arrays(brush->colbrushf->numpoints, brush->colbrushf->points->v, NULL, NULL);
9787                                         R_Mesh_Draw(0, brush->colbrushf->numpoints, 0, brush->colbrushf->numtriangles, brush->colbrushf->elements, NULL, 0, NULL, NULL, 0);
9788                                 }
9789                                 break;
9790                         case BIH_COLLISIONTRIANGLE:
9791                                 triangleindex = bihleaf->itemindex;
9792                                 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+0], vertex3f[0]);
9793                                 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+1], vertex3f[1]);
9794                                 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+2], vertex3f[2]);
9795                                 GL_Color((bihleafindex & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 5) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 10) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, r_showcollisionbrushes.value);
9796                                 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
9797                                 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
9798                                 break;
9799                         case BIH_RENDERTRIANGLE:
9800                                 triangleindex = bihleaf->itemindex;
9801                                 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+0], vertex3f[0]);
9802                                 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+1], vertex3f[1]);
9803                                 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+2], vertex3f[2]);
9804                                 GL_Color((bihleafindex & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 5) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 10) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, r_showcollisionbrushes.value);
9805                                 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
9806                                 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
9807                                 break;
9808                         }
9809                 }
9810         }
9811
9812         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
9813
9814 #ifndef USE_GLES2
9815         if (r_showtris.value > 0 && qglPolygonMode)
9816         {
9817                 if (r_showdisabledepthtest.integer)
9818                 {
9819                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9820                         GL_DepthMask(false);
9821                 }
9822                 else
9823                 {
9824                         GL_BlendFunc(GL_ONE, GL_ZERO);
9825                         GL_DepthMask(true);
9826                 }
9827                 qglPolygonMode(GL_FRONT_AND_BACK, GL_LINE);CHECKGLERROR
9828                 for (j = model->submodelsurfaces_start; j < model->submodelsurfaces_end; j++)
9829                 {
9830                         if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9831                                 continue;
9832                         surface = model->data_surfaces + j;
9833                         rsurface.texture = R_GetCurrentTexture(surface->texture);
9834                         if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9835                         {
9836                                 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
9837                                 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
9838                                         GL_Color(r_refdef.view.colorscale, 0, 0, r_showtris.value);
9839                                 else if (ent == r_refdef.scene.worldentity)
9840                                         GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, r_showtris.value);
9841                                 else
9842                                         GL_Color(0, r_refdef.view.colorscale, 0, r_showtris.value);
9843                                 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
9844                                 RSurf_DrawBatch();
9845                         }
9846                 }
9847                 qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);CHECKGLERROR
9848                 rsurface.texture = NULL;
9849         }
9850
9851 # if 0
9852         // FIXME!  implement r_shownormals with just triangles
9853         if (r_shownormals.value != 0 && qglBegin)
9854         {
9855                 int l, k;
9856                 vec3_t v;
9857                 if (r_showdisabledepthtest.integer)
9858                 {
9859                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9860                         GL_DepthMask(false);
9861                 }
9862                 else
9863                 {
9864                         GL_BlendFunc(GL_ONE, GL_ZERO);
9865                         GL_DepthMask(true);
9866                 }
9867                 for (j = model->submodelsurfaces_start; j < model->submodelsurfaces_end; j++)
9868                 {
9869                         if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9870                                 continue;
9871                         surface = model->data_surfaces + j;
9872                         rsurface.texture = R_GetCurrentTexture(surface->texture);
9873                         if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9874                         {
9875                                 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
9876                                 qglBegin(GL_LINES);
9877                                 if (r_shownormals.value < 0 && rsurface.batchnormal3f)
9878                                 {
9879                                         for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9880                                         {
9881                                                 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9882                                                 GL_Color(0, 0, r_refdef.view.colorscale, 1);
9883                                                 qglVertex3f(v[0], v[1], v[2]);
9884                                                 VectorMA(v, -r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
9885                                                 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9886                                                 qglVertex3f(v[0], v[1], v[2]);
9887                                         }
9888                                 }
9889                                 if (r_shownormals.value > 0 && rsurface.batchsvector3f)
9890                                 {
9891                                         for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9892                                         {
9893                                                 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9894                                                 GL_Color(r_refdef.view.colorscale, 0, 0, 1);
9895                                                 qglVertex3f(v[0], v[1], v[2]);
9896                                                 VectorMA(v, r_shownormals.value, rsurface.batchsvector3f + l * 3, v);
9897                                                 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9898                                                 qglVertex3f(v[0], v[1], v[2]);
9899                                         }
9900                                 }
9901                                 if (r_shownormals.value > 0 && rsurface.batchtvector3f)
9902                                 {
9903                                         for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9904                                         {
9905                                                 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9906                                                 GL_Color(0, r_refdef.view.colorscale, 0, 1);
9907                                                 qglVertex3f(v[0], v[1], v[2]);
9908                                                 VectorMA(v, r_shownormals.value, rsurface.batchtvector3f + l * 3, v);
9909                                                 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9910                                                 qglVertex3f(v[0], v[1], v[2]);
9911                                         }
9912                                 }
9913                                 if (r_shownormals.value > 0 && rsurface.batchnormal3f)
9914                                 {
9915                                         for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9916                                         {
9917                                                 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9918                                                 GL_Color(0, 0, r_refdef.view.colorscale, 1);
9919                                                 qglVertex3f(v[0], v[1], v[2]);
9920                                                 VectorMA(v, r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
9921                                                 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9922                                                 qglVertex3f(v[0], v[1], v[2]);
9923                                         }
9924                                 }
9925                                 qglEnd();
9926                                 CHECKGLERROR
9927                         }
9928                 }
9929                 rsurface.texture = NULL;
9930         }
9931 # endif
9932 #endif
9933 }
9934
9935 int r_maxsurfacelist = 0;
9936 const msurface_t **r_surfacelist = NULL;
9937 void R_DrawModelSurfaces(entity_render_t *ent, qbool skysurfaces, qbool writedepth, qbool depthonly, qbool debug, qbool prepass, qbool ui)
9938 {
9939         int i, j, flagsmask;
9940         model_t *model = ent->model;
9941         msurface_t *surfaces;
9942         unsigned char *update;
9943         int numsurfacelist = 0;
9944         if (model == NULL)
9945                 return;
9946
9947         if (r_maxsurfacelist < model->num_surfaces)
9948         {
9949                 r_maxsurfacelist = model->num_surfaces;
9950                 if (r_surfacelist)
9951                         Mem_Free((msurface_t **)r_surfacelist);
9952                 r_surfacelist = (const msurface_t **) Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
9953         }
9954
9955         if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
9956                 RSurf_ActiveModelEntity(ent, false, false, false);
9957         else if (prepass)
9958                 RSurf_ActiveModelEntity(ent, true, true, true);
9959         else if (depthonly)
9960                 RSurf_ActiveModelEntity(ent, model->wantnormals, model->wanttangents, false);
9961         else
9962                 RSurf_ActiveModelEntity(ent, true, true, false);
9963
9964         surfaces = model->data_surfaces;
9965         update = model->brushq1.lightmapupdateflags;
9966
9967         flagsmask = skysurfaces ? MATERIALFLAG_SKY : MATERIALFLAG_WALL;
9968
9969         if (debug)
9970         {
9971                 R_DrawDebugModel();
9972                 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
9973                 return;
9974         }
9975
9976         // check if this is an empty model
9977         if (model->submodelsurfaces_start >= model->submodelsurfaces_end)
9978                 return;
9979
9980         rsurface.lightmaptexture = NULL;
9981         rsurface.deluxemaptexture = NULL;
9982         rsurface.uselightmaptexture = false;
9983         rsurface.texture = NULL;
9984         rsurface.rtlight = NULL;
9985         numsurfacelist = 0;
9986
9987         // add visible surfaces to draw list
9988         if (ent == r_refdef.scene.worldentity)
9989         {
9990                 // for the world entity, check surfacevisible
9991                 for (i = model->submodelsurfaces_start;i < model->submodelsurfaces_end;i++)
9992                 {
9993                         j = model->modelsurfaces_sorted[i];
9994                         if (r_refdef.viewcache.world_surfacevisible[j])
9995                                 r_surfacelist[numsurfacelist++] = surfaces + j;
9996                 }
9997
9998                 // don't do anything if there were no surfaces added (none of the world entity is visible)
9999                 if (!numsurfacelist)
10000                 {
10001                         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
10002                         return;
10003                 }
10004         }
10005         else if (ui)
10006         {
10007                 // for ui we have to preserve the order of surfaces (not using modelsurfaces_sorted)
10008                 for (i = model->submodelsurfaces_start; i < model->submodelsurfaces_end; i++)
10009                         r_surfacelist[numsurfacelist++] = surfaces + i;
10010         }
10011         else
10012         {
10013                 // add all surfaces
10014                 for (i = model->submodelsurfaces_start; i < model->submodelsurfaces_end; i++)
10015                         r_surfacelist[numsurfacelist++] = surfaces + model->modelsurfaces_sorted[i];
10016         }
10017
10018         /*
10019          * Mark lightmaps as dirty if their lightstyle's value changed. We do this by
10020          * using style chains because most styles do not change on most frames, and most
10021          * surfaces do not have styles on them. Mods like Arcane Dimensions (e.g. ad_necrokeep)
10022          * break this rule and animate most surfaces.
10023          */
10024         if (update && !skysurfaces && !depthonly && !prepass && model->brushq1.num_lightstyles && r_refdef.scene.lightmapintensity > 0 && r_q1bsp_lightmap_updates_enabled.integer)
10025         {
10026                 model_brush_lightstyleinfo_t *style;
10027
10028                 // For each lightstyle, check if its value changed and mark the lightmaps as dirty if so
10029                 for (i = 0, style = model->brushq1.data_lightstyleinfo; i < model->brushq1.num_lightstyles; i++, style++)
10030                 {
10031                         if (style->value != r_refdef.scene.lightstylevalue[style->style])
10032                         {
10033                                 int* list = style->surfacelist;
10034                                 style->value = r_refdef.scene.lightstylevalue[style->style];
10035                                 // Value changed - mark the surfaces belonging to this style chain as dirty
10036                                 for (j = 0; j < style->numsurfaces; j++)
10037                                         update[list[j]] = true;
10038                         }
10039                 }
10040                 // Now check if update flags are set on any surfaces that are visible
10041                 if (r_q1bsp_lightmap_updates_hidden_surfaces.integer)
10042                 {
10043                         /* 
10044                          * We can do less frequent texture uploads (approximately 10hz for animated
10045                          * lightstyles) by rebuilding lightmaps on surfaces that are not currently visible.
10046                          * For optimal efficiency, this includes the submodels of the worldmodel, so we
10047                          * use model->num_surfaces, not nummodelsurfaces.
10048                          */
10049                         for (i = 0; i < model->num_surfaces;i++)
10050                                 if (update[i])
10051                                         R_BuildLightMap(ent, surfaces + i, r_q1bsp_lightmap_updates_combine.integer);
10052                 }
10053                 else
10054                 {
10055                         for (i = 0; i < numsurfacelist; i++)
10056                                 if (update[r_surfacelist[i] - surfaces])
10057                                         R_BuildLightMap(ent, (msurface_t *)r_surfacelist[i], r_q1bsp_lightmap_updates_combine.integer);
10058                 }
10059         }
10060
10061         R_QueueModelSurfaceList(ent, numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly, prepass, ui);
10062
10063         // add to stats if desired
10064         if (r_speeds.integer && !skysurfaces && !depthonly)
10065         {
10066                 r_refdef.stats[r_stat_entities_surfaces] += numsurfacelist;
10067                 for (j = 0;j < numsurfacelist;j++)
10068                         r_refdef.stats[r_stat_entities_triangles] += r_surfacelist[j]->num_triangles;
10069         }
10070
10071         rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
10072 }
10073
10074 void R_DebugLine(vec3_t start, vec3_t end)
10075 {
10076         model_t *mod = CL_Mesh_UI();
10077         msurface_t *surf;
10078         int e0, e1, e2, e3;
10079         float offsetx, offsety, x1, y1, x2, y2, width = 1.0f;
10080         float r1 = 1.0f, g1 = 0.0f, b1 = 0.0f, alpha1 = 0.25f;
10081         float r2 = 1.0f, g2 = 1.0f, b2 = 0.0f, alpha2 = 0.25f;
10082         vec4_t w[2], s[2];
10083
10084         // transform to screen coords first
10085         Vector4Set(w[0], start[0], start[1], start[2], 1);
10086         Vector4Set(w[1], end[0], end[1], end[2], 1);
10087         R_Viewport_TransformToScreen(&r_refdef.view.viewport, w[0], s[0]);
10088         R_Viewport_TransformToScreen(&r_refdef.view.viewport, w[1], s[1]);
10089         x1 = s[0][0] * vid_conwidth.value / vid.width;
10090         y1 = (vid.height - s[0][1]) * vid_conheight.value / vid.height;
10091         x2 = s[1][0] * vid_conwidth.value / vid.width;
10092         y2 = (vid.height - s[1][1]) * vid_conheight.value / vid.height;
10093         //Con_DPrintf("R_DebugLine: %.0f,%.0f to %.0f,%.0f\n", x1, y1, x2, y2);
10094
10095         // add the line to the UI mesh for drawing later
10096
10097         // width is measured in real pixels
10098         if (fabs(x2 - x1) > fabs(y2 - y1))
10099         {
10100                 offsetx = 0;
10101                 offsety = 0.5f * width * vid_conheight.value / vid.height;
10102         }
10103         else
10104         {
10105                 offsetx = 0.5f * width * vid_conwidth.value / vid.width;
10106                 offsety = 0;
10107         }
10108         surf = Mod_Mesh_AddSurface(mod, Mod_Mesh_GetTexture(mod, "white", 0, 0, MATERIALFLAG_WALL | MATERIALFLAG_VERTEXCOLOR | MATERIALFLAG_ALPHAGEN_VERTEX | MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW), true);
10109         e0 = Mod_Mesh_IndexForVertex(mod, surf, x1 - offsetx, y1 - offsety, 10, 0, 0, -1, 0, 0, 0, 0, r1, g1, b1, alpha1);
10110         e1 = Mod_Mesh_IndexForVertex(mod, surf, x2 - offsetx, y2 - offsety, 10, 0, 0, -1, 0, 0, 0, 0, r2, g2, b2, alpha2);
10111         e2 = Mod_Mesh_IndexForVertex(mod, surf, x2 + offsetx, y2 + offsety, 10, 0, 0, -1, 0, 0, 0, 0, r2, g2, b2, alpha2);
10112         e3 = Mod_Mesh_IndexForVertex(mod, surf, x1 + offsetx, y1 + offsety, 10, 0, 0, -1, 0, 0, 0, 0, r1, g1, b1, alpha1);
10113         Mod_Mesh_AddTriangle(mod, surf, e0, e1, e2);
10114         Mod_Mesh_AddTriangle(mod, surf, e0, e2, e3);
10115
10116 }
10117
10118
10119 void R_DrawCustomSurface(skinframe_t *skinframe, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qbool writedepth, qbool prepass, qbool ui)
10120 {
10121         static texture_t texture;
10122
10123         // fake enough texture and surface state to render this geometry
10124
10125         texture.update_lastrenderframe = -1; // regenerate this texture
10126         texture.basematerialflags = materialflags | MATERIALFLAG_CUSTOMSURFACE | MATERIALFLAG_WALL;
10127         texture.basealpha = 1.0f;
10128         texture.currentskinframe = skinframe;
10129         texture.currenttexmatrix = *texmatrix; // requires MATERIALFLAG_CUSTOMSURFACE
10130         texture.offsetmapping = OFFSETMAPPING_OFF;
10131         texture.offsetscale = 1;
10132         texture.specularscalemod = 1;
10133         texture.specularpowermod = 1;
10134         texture.transparentsort = TRANSPARENTSORT_DISTANCE;
10135
10136         R_DrawCustomSurface_Texture(&texture, texmatrix, materialflags, firstvertex, numvertices, firsttriangle, numtriangles, writedepth, prepass, ui);
10137 }
10138
10139 void R_DrawCustomSurface_Texture(texture_t *texture, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qbool writedepth, qbool prepass, qbool ui)
10140 {
10141         static msurface_t surface;
10142         const msurface_t *surfacelist = &surface;
10143
10144         // fake enough texture and surface state to render this geometry
10145         surface.texture = texture;
10146         surface.num_triangles = numtriangles;
10147         surface.num_firsttriangle = firsttriangle;
10148         surface.num_vertices = numvertices;
10149         surface.num_firstvertex = firstvertex;
10150
10151         // now render it
10152         rsurface.texture = R_GetCurrentTexture(surface.texture);
10153         rsurface.lightmaptexture = NULL;
10154         rsurface.deluxemaptexture = NULL;
10155         rsurface.uselightmaptexture = false;
10156         R_DrawModelTextureSurfaceList(1, &surfacelist, writedepth, prepass, ui);
10157 }