2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "cl_collision.h"
32 // Enable NVIDIA High Performance Graphics while using Integrated Graphics.
36 __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
42 mempool_t *r_main_mempool;
43 rtexturepool_t *r_main_texturepool;
45 int r_textureframe = 0; ///< used only by R_GetCurrentTexture, incremented per view and per UI render
47 static qbool r_loadnormalmap;
48 static qbool r_loadgloss;
50 static qbool r_loaddds;
51 static qbool r_savedds;
52 static qbool r_gpuskeletal;
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"};
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"};
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"};
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)"};
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)"};
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"};
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; the default setting of 0 uses a framebuffer render when required, and renders directly to the screen otherwise"};
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"};
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"};
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"};
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"};
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"};
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"};
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"};
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"};
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"};
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"};
235 cvar_t r_test = {CF_CLIENT, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
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"};
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"};
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."};
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] =
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"},
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"};
261 extern cvar_t v_glslgamma_2d;
263 extern qbool v_flipped_state;
265 r_framebufferstate_t r_fb;
267 /// shadow volume bsp struct with automatically growing nodes buffer
270 int r_uniformbufferalignment = 32; // dynamically updated to match GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT
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;
286 // TODO: hash lookups?
287 typedef struct cubemapinfo_s
294 int r_texture_numcubemaps;
295 cubemapinfo_t *r_texture_cubemaps[MAX_CUBEMAPS];
297 unsigned int r_queries[MAX_OCCLUSION_QUERIES];
298 unsigned int r_numqueries;
299 unsigned int r_maxqueries;
301 typedef struct r_qwskincache_s
303 char name[MAX_QPATH];
304 skinframe_t *skinframe;
308 static r_qwskincache_t *r_qwskincache;
309 static int r_qwskincache_size;
311 /// vertex coordinates for a quad that covers the screen exactly
312 extern const float r_screenvertex3f[12];
313 const float r_screenvertex3f[12] =
321 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
324 for (i = 0;i < verts;i++)
335 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
338 for (i = 0;i < verts;i++)
348 // FIXME: move this to client?
351 if (gamemode == GAME_NEHAHRA)
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");
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));
371 static void R_BuildBlankTextures(void)
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);
383 r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
388 r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
393 r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
396 static void R_BuildNoTexture(void)
398 r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, Image_GenerateNoTexture(), TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, -1, NULL);
401 static void R_BuildWhiteCube(void)
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);
408 static void R_BuildNormalizationCube(void)
412 vec_t s, t, intensity;
415 data = (unsigned char *)Mem_Alloc(tempmempool, 6*NORMSIZE*NORMSIZE*4);
416 for (side = 0;side < 6;side++)
418 for (y = 0;y < NORMSIZE;y++)
420 for (x = 0;x < NORMSIZE;x++)
422 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
423 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
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;
466 r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
470 static void R_BuildFogTexture(void)
474 unsigned char data1[FOGWIDTH][4];
475 //unsigned char data2[FOGWIDTH][4];
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;
483 r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
484 for (x = 0;x < FOGMASKTABLEWIDTH;x++)
486 d = (x * r - r_refdef.fogmasktable_start);
487 if(developer_extra.integer)
488 Con_DPrintf("%f ", d);
490 if (r_fog_exp2.integer)
491 alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
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);
502 for (x = 0;x < FOGWIDTH;x++)
504 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
509 //data2[x][0] = 255 - b;
510 //data2[x][1] = 255 - b;
511 //data2[x][2] = 255 - b;
514 if (r_texture_fogattenuation)
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);
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);
526 static void R_BuildFogHeightTexture(void)
528 unsigned char *inpixels;
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);
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;
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);
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++)
564 for (x = 0;x < size;x++)
570 for (j = x;j <= y;j++)
572 Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
578 for (j = x;j >= y;j--)
580 Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
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);
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);
594 //=======================================================================================================================================================
596 static const char *builtinshaderstrings[] =
598 #include "shader_glsl.h"
602 //=======================================================================================================================================================
604 typedef struct shaderpermutationinfo_s
609 shaderpermutationinfo_t;
611 typedef struct shadermodeinfo_s
613 const char *sourcebasename;
614 const char *extension;
615 const char **builtinshaderstrings;
624 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
625 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
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"}
661 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
662 shadermodeinfo_t shadermodeinfo[SHADERLANGUAGE_COUNT][SHADERMODE_COUNT] =
664 // SHADERLANGUAGE_GLSL
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"},
686 struct r_glsl_permutation_s;
687 typedef struct r_glsl_permutation_s
690 struct r_glsl_permutation_s *hashnext;
692 uint64_t permutation;
694 /// indicates if we have tried compiling this permutation already
696 /// 0 if compilation failed
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;
761 int loc_BloomBlur_Parameters;
763 int loc_Color_Ambient;
764 int loc_Color_Diffuse;
765 int loc_Color_Specular;
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;
777 int loc_FogHeightFade;
779 int loc_FogPlaneViewDist;
780 int loc_FogRangeRecip;
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;
790 int loc_ReflectColor;
791 int loc_ReflectFactor;
792 int loc_ReflectOffset;
793 int loc_RefractColor;
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;
807 int loc_ViewTintColor;
809 int loc_ModelToLight;
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;
826 r_glsl_permutation_t;
828 #define SHADERPERMUTATION_HASHSIZE 256
831 // non-degradable "lightweight" shader parameters to keep the permutations simpler
832 // these can NOT degrade! only use for simple stuff
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)
851 #define SHADERSTATICPARMS_COUNT 15
853 static const char *shaderstaticparmstrings_list[SHADERSTATICPARMS_COUNT];
854 static int shaderstaticparms_count = 0;
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))
859 extern qbool r_shadow_shadowmapsampler;
860 extern int r_shadow_shadowmappcf;
861 qbool R_CompileShader_CheckStaticParms(void)
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));
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)
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);
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);
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);
903 return memcmp(r_compileshader_staticparms, r_compileshader_staticparms_save, sizeof(r_compileshader_staticparms)) != 0;
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"; \
910 shaderstaticparmstrings_list[shaderstaticparms_count++] = "\n"
911 static void R_CompileShader_AddStaticParms(unsigned int mode, uint64_t permutation)
913 shaderstaticparms_count = 0;
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");
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;
940 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, uint64_t permutation)
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)
947 if (p->mode == mode && p->permutation == permutation)
949 //if (hashdepth > 10)
950 // Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
955 p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
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);
965 static char *R_ShaderStrCat(const char **strings)
968 const char **p = strings;
971 for (p = strings;(t = *p);p++)
974 s = string = (char *)Mem_Alloc(r_main_mempool, len);
976 for (p = strings;(t = *p);p++)
986 static char *R_ShaderStrCat(const char **strings);
987 static void R_InitShaderModeInfo(void)
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++)
994 for (i = 0; i < SHADERMODE_COUNT; i++)
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);
1006 static char *ShaderModeInfo_GetShaderText(shadermodeinfo_t *modeinfo, qbool printfromdisknotice, qbool builtinonly)
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);
1016 if (printfromdisknotice)
1017 Con_DPrintf("Loading shaders from file %s...\n", modeinfo->filename);
1018 return shaderstring;
1020 // fall back to builtinstring
1021 return Mem_strdup(r_main_mempool, modeinfo->builtinstring);
1024 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, uint64_t permutation)
1029 shadermodeinfo_t *modeinfo = &shadermodeinfo[SHADERLANGUAGE_GLSL][mode];
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];
1044 permutationname[0] = 0;
1045 sourcestring = ShaderModeInfo_GetShaderText(modeinfo, true, false);
1047 strlcat(permutationname, modeinfo->filename, sizeof(permutationname));
1049 // we need 140 for r_glsl_skeletal (GL_ARB_uniform_buffer_object)
1050 if(vid.support.glshaderversion >= 140)
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";
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)
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";
1069 // if we can do #version 120, we should (this adds the invariant keyword)
1070 else if(vid.support.glshaderversion >= 120)
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";
1079 // GLES also adds several things from GLSL120
1080 switch(vid.renderpath)
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";
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";
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));
1103 // now add all the permutation pretexts
1104 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1106 if (permutation & (1ll<<i))
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));
1115 // keep line numbers correct
1116 vertstrings_list[vertstrings_count++] = "\n";
1117 geomstrings_list[geomstrings_count++] = "\n";
1118 fragstrings_list[fragstrings_count++] = "\n";
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;
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;
1136 // we don't currently use geometry shaders for anything, so just empty the list
1137 geomstrings_count = 0;
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);
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
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++)
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);
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
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");
1327 // clear the uniform block bindings
1328 p->ubibind_Skeletal_Transform12_UniformBlock = -1;
1329 // bind the uniform blocks in use
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++;}
1334 // we're done compiling and setting up the shader, at least until it is used
1336 Con_DPrintf("^5GLSL shader %s compiled (%i textures).\n", permutationname, sampler);
1339 Con_Printf("^1GLSL shader %s failed! some features may not work properly.\n", permutationname);
1343 Mem_Free(sourcestring);
1346 static void R_SetupShader_SetPermutationGLSL(unsigned int mode, uint64_t permutation)
1348 r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
1349 if (r_glsl_permutation != perm)
1351 r_glsl_permutation = perm;
1352 if (!r_glsl_permutation->program)
1354 if (!r_glsl_permutation->compiled)
1356 Con_DPrintf("Compiling shader mode %u permutation %" PRIx64 "\n", mode, permutation);
1357 R_GLSL_CompilePermutation(perm, mode, permutation);
1359 if (!r_glsl_permutation->program)
1361 // remove features until we find a valid permutation
1363 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
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))
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)
1376 if (i >= SHADERPERMUTATION_COUNT)
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
1386 qglUseProgram(r_glsl_permutation->program);CHECKGLERROR
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);
1394 void R_GLSL_Restart_f(cmd_state_t *cmd)
1396 unsigned int i, limit;
1397 switch(vid.renderpath)
1399 case RENDERPATH_GL32:
1400 case RENDERPATH_GLES2:
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++)
1407 if ((p = (r_glsl_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_glsl_permutationarray, i)))
1409 GL_Backend_FreeProgram(p->program);
1410 Mem_ExpandableArray_FreeRecord(&r_glsl_permutationarray, (void*)p);
1413 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
1419 static void R_GLSL_DumpShader_f(cmd_state_t *cmd)
1421 int i, language, mode, dupe;
1423 shadermodeinfo_t *modeinfo;
1426 for (language = 0;language < SHADERLANGUAGE_COUNT;language++)
1428 modeinfo = shadermodeinfo[language];
1429 for (mode = 0;mode < SHADERMODE_COUNT;mode++)
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))
1437 text = modeinfo[mode].builtinstring;
1440 file = FS_OpenRealFile(modeinfo[mode].filename, "w", false);
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);
1452 Con_Printf("%s written\n", modeinfo[mode].filename);
1455 Con_Printf(CON_ERROR "failed to write to %s\n", modeinfo[mode].filename);
1460 void R_SetupShader_Generic(rtexture_t *t, qbool usegamma, qbool notrippy, qbool suppresstexalpha)
1462 uint64_t permutation = 0;
1463 if (r_trippy.integer && !notrippy)
1464 permutation |= SHADERPERMUTATION_TRIPPY;
1465 permutation |= SHADERPERMUTATION_VIEWTINT;
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)
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);
1487 void R_SetupShader_Generic_NoTexture(qbool usegamma, qbool notrippy)
1489 R_SetupShader_Generic(NULL, usegamma, notrippy, false);
1492 void R_SetupShader_DepthOrShadow(qbool notrippy, qbool depthrgb, qbool skeletal)
1494 uint64_t permutation = 0;
1495 if (r_trippy.integer && !notrippy)
1496 permutation |= SHADERPERMUTATION_TRIPPY;
1498 permutation |= SHADERPERMUTATION_DEPTHRGB;
1500 permutation |= SHADERPERMUTATION_SKELETAL;
1502 if (vid.allowalphatocoverage)
1503 GL_AlphaToCoverage(false);
1504 switch (vid.renderpath)
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);
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)
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
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
1534 // these checks are the output of fogeval.pl
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;
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)
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;
1571 texture_t *t = rsurface.texture;
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)
1585 // distorted background
1586 if (t->currentmaterialflags & MATERIALFLAG_WATERSHADER)
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))
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);
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);
1604 else if (t->currentmaterialflags & MATERIALFLAG_REFRACTION)
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);
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);
1619 if (vid.allowalphatocoverage)
1620 GL_AlphaToCoverage(false);
1622 else if (rsurfacepass == RSURFPASS_DEFERREDGEOMETRY)
1624 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1626 switch(t->offsetmapping)
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;
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);
1643 else if (rsurfacepass == RSURFPASS_RTLIGHT)
1645 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1647 switch(t->offsetmapping)
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;
1655 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1656 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1657 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1658 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
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)
1673 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1674 if(r_shadow_shadowmapvsdct)
1675 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
1677 if (r_shadow_shadowmap2ddepthbuffer)
1678 permutation |= SHADERPERMUTATION_DEPTHRGB;
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);
1687 else if (t->currentmaterialflags & MATERIALFLAG_LIGHTGRID)
1689 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1691 switch(t->offsetmapping)
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;
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))
1716 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1717 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1719 if (r_shadow_shadowmap2ddepthbuffer)
1720 permutation |= SHADERPERMUTATION_DEPTHRGB;
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)
1730 permutation |= SHADERPERMUTATION_BOUNCEGRID;
1731 if (r_shadow_bouncegrid_state.directional)
1732 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
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)
1739 if (r_transparent_alphatocoverage.integer)
1741 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1742 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1745 GL_AlphaToCoverage(false);
1748 else if (t->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
1750 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1752 switch(t->offsetmapping)
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;
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))
1778 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1779 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1781 if (r_shadow_shadowmap2ddepthbuffer)
1782 permutation |= SHADERPERMUTATION_DEPTHRGB;
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)
1792 permutation |= SHADERPERMUTATION_BOUNCEGRID;
1793 if (r_shadow_bouncegrid_state.directional)
1794 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
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)
1801 if (r_transparent_alphatocoverage.integer)
1803 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1804 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1807 GL_AlphaToCoverage(false);
1812 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1814 switch(t->offsetmapping)
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;
1822 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1823 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1824 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1825 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
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))
1835 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1836 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1838 if (r_shadow_shadowmap2ddepthbuffer)
1839 permutation |= SHADERPERMUTATION_DEPTHRGB;
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)
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;
1853 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1854 permutation |= SHADERPERMUTATION_DIFFUSE;
1855 if (VectorLength2(t->render_lightmap_specular) > 0)
1856 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1858 else if (r_glsl_deluxemapping.integer >= 2)
1860 // fake deluxemapping (uniform light direction in tangentspace)
1861 if (rsurface.uselightmaptexture)
1862 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP;
1864 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR;
1865 permutation |= SHADERPERMUTATION_DIFFUSE;
1866 if (VectorLength2(t->render_lightmap_specular) > 0)
1867 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1869 else if (rsurface.uselightmaptexture)
1871 // ordinary lightmapping (q1bsp, q3bsp)
1872 mode = SHADERMODE_LIGHTMAP;
1876 // ordinary vertex coloring (q3bsp)
1877 mode = SHADERMODE_VERTEXCOLOR;
1879 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1881 permutation |= SHADERPERMUTATION_BOUNCEGRID;
1882 if (r_shadow_bouncegrid_state.directional)
1883 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
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)
1890 if (r_transparent_alphatocoverage.integer)
1892 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1893 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1896 GL_AlphaToCoverage(false);
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)
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);
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)
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]);
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);
1933 if (mode == SHADERMODE_FLATCOLOR)
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]);
1937 else if (mode == SHADERMODE_LIGHTGRID)
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
1944 else if (mode == SHADERMODE_LIGHTDIRECTION)
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]);
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]);
1962 // additive passes are only darkened by fog, not tinted
1963 if (r_glsl_permutation->loc_FogColor >= 0 && !ui)
1965 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACK0)
1966 qglUniform3f(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1968 qglUniform3f(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
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]);
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)
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]);
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]);
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)
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]);
2002 qglUniform3f(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
2004 if (r_glsl_permutation->loc_Color_Shirt >= 0)
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]);
2009 qglUniform3f(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
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)
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)
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);
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)
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);
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);
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)))
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)
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 );
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);
2090 void R_SetupShader_DeferredLight(const rtlight_t *rtlight)
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];
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)
2118 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2119 if (r_shadow_shadowmapvsdct)
2120 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
2122 if (r_shadow_shadowmap2ddepthbuffer)
2123 permutation |= SHADERPERMUTATION_DEPTHRGB;
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)
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);
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 );
2156 #define SKINFRAME_HASH 1024
2160 unsigned int loadsequence; // incremented each level change
2161 memexpandablearray_t array;
2162 skinframe_t *hash[SKINFRAME_HASH];
2165 r_skinframe_t r_skinframe;
2167 void R_SkinFrame_PrepareForPurge(void)
2169 r_skinframe.loadsequence++;
2170 // wrap it without hitting zero
2171 if (r_skinframe.loadsequence >= 200)
2172 r_skinframe.loadsequence = 1;
2175 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
2179 // mark the skinframe as used for the purging code
2180 skinframe->loadsequence = r_skinframe.loadsequence;
2183 void R_SkinFrame_PurgeSkinFrame(skinframe_t *s)
2187 if (s->merged == s->base)
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;
2202 void R_SkinFrame_Purge(void)
2206 for (i = 0;i < SKINFRAME_HASH;i++)
2208 for (s = r_skinframe.hash[i];s;s = s->next)
2210 if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
2211 R_SkinFrame_PurgeSkinFrame(s);
2216 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
2218 char basename[MAX_QPATH];
2220 Image_StripImageExtension(name, basename, sizeof(basename));
2222 if( last == NULL ) {
2224 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2225 item = r_skinframe.hash[hashindex];
2230 // linearly search through the hash bucket
2231 for( ; item ; item = item->next ) {
2232 if( !strcmp( item->basename, basename ) ) {
2239 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qbool add)
2242 int compareflags = textureflags & TEXF_IMPORTANTBITS;
2244 char basename[MAX_QPATH];
2246 Image_StripImageExtension(name, basename, sizeof(basename));
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)
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;
2271 else if (textureflags & TEXF_FORCE_RELOAD)
2272 R_SkinFrame_PurgeSkinFrame(item);
2274 R_SkinFrame_MarkUsed(item);
2278 #define R_SKINFRAME_LOAD_AVERAGE_COLORS(cnt, getpixel) \
2280 unsigned long long avgcolor[5], wsum; \
2288 for(pix = 0; pix < cnt; ++pix) \
2291 for(comp = 0; comp < 3; ++comp) \
2293 if(w) /* ignore perfectly black pixels because that is better for model skins */ \
2296 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2298 for(comp = 0; comp < 3; ++comp) \
2299 avgcolor[comp] += getpixel * w; \
2302 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2303 avgcolor[4] += getpixel; \
2305 if(avgcolor[3] == 0) /* no pixels seen? even worse */ \
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); \
2313 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qbool complain, qbool fallbacknotexture)
2315 skinframe_t *skinframe;
2317 if (cls.state == ca_dedicated)
2320 // return an existing skinframe if already loaded
2321 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
2322 if (skinframe && skinframe->base)
2325 // if the skinframe doesn't exist this will create it
2326 return R_SkinFrame_LoadExternal_SkinFrame(skinframe, name, textureflags, complain, fallbacknotexture);
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)
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;
2347 if (cls.state == ca_dedicated)
2350 Image_StripImageExtension(name, basename, sizeof(basename));
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)))
2355 basepixels = loadimagepixelsbgra(name, complain, true, false, &miplevel);
2356 if (basepixels == NULL && fallbacknotexture)
2357 basepixels = Image_GenerateNoTexture();
2358 if (basepixels == NULL)
2362 // FIXME handle miplevel
2364 if (developer_loading.integer)
2365 Con_Printf("loading skin \"%s\"\n", name);
2367 // we've got some pixels to store, so really allocate this new texture now
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
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]);
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)
2400 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
2402 if (basepixels[j] < 255)
2404 skinframe->hasalpha = true;
2408 if (r_loadfog && skinframe->hasalpha)
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)
2417 pixels[j+3] = basepixels[j+3];
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);
2423 R_SKINFRAME_LOAD_AVERAGE_COLORS(basepixels_width * basepixels_height, basepixels[4 * pix + comp]);
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);
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);
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);
2446 // _norm is the name used by tenebrae and has been adopted as standard
2447 if (r_loadnormalmap && skinframe->nmap == NULL)
2449 mymiplevel = savemiplevel;
2450 if ((pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_norm", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
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);
2456 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_bump", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
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);
2462 Mem_Free(bumppixels);
2464 else if (r_shadow_bumpscale_basetexture.value > 0)
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);
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);
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))))
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);
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);
2488 Mem_Free(pixels);pixels = NULL;
2491 mymiplevel = savemiplevel;
2492 if (skinframe->gloss == NULL && r_loadgloss && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_gloss", skinframe->basename), false, false, false, &mymiplevel)))
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);
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);
2503 mymiplevel = savemiplevel;
2504 if (skinframe->pants == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_pants", skinframe->basename), false, false, false, &mymiplevel)))
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);
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);
2515 mymiplevel = savemiplevel;
2516 if (skinframe->shirt == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_shirt", skinframe->basename), false, false, false, &mymiplevel)))
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);
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);
2527 mymiplevel = savemiplevel;
2528 if (skinframe->reflect == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_reflect", skinframe->basename), false, false, false, &mymiplevel)))
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);
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);
2540 Mem_Free(basepixels);
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)
2548 skinframe_t *skinframe;
2551 if (cls.state == ca_dedicated)
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)
2558 textureflags &= ~TEXF_FORCE_RELOAD;
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;
2572 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2576 if (developer_loading.integer)
2577 Con_Printf("loading 32bit skin \"%s\"\n", name);
2579 if (r_loadnormalmap && r_shadow_bumpscale_basetexture.value > 0)
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);
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)
2590 for (i = 3;i < width * height * 4;i += 4)
2592 if (skindata[i] < 255)
2594 skinframe->hasalpha = true;
2598 if (r_loadfog && skinframe->hasalpha)
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);
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]);
2615 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
2619 skinframe_t *skinframe;
2621 if (cls.state == ca_dedicated)
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)
2628 //textureflags &= ~TEXF_FORCE_RELOAD;
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;
2642 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2646 if (developer_loading.integer)
2647 Con_Printf("loading quake skin \"%s\"\n", name);
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;
2656 for (i = 0;i < width * height;i++)
2657 featuresmask |= palette_featureflags[skindata[i]];
2659 skinframe->hasalpha = false;
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);
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]);
2675 static void R_SkinFrame_GenerateTexturesFromQPixels(skinframe_t *skinframe, qbool colormapped)
2679 unsigned char *skindata;
2682 if (!skinframe->qpixels)
2685 if (!skinframe->qhascolormapping)
2686 colormapped = false;
2690 if (!skinframe->qgeneratebase)
2695 if (!skinframe->qgeneratemerged)
2699 width = skinframe->qwidth;
2700 height = skinframe->qheight;
2701 skindata = skinframe->qpixels;
2703 if (skinframe->qgeneratenmap)
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);
2716 if (skinframe->qgenerateglow)
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
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
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);
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);
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);
2741 if (!skinframe->qgeneratemerged && !skinframe->qgeneratebase)
2743 Mem_Free(skinframe->qpixels);
2744 skinframe->qpixels = NULL;
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)
2751 skinframe_t *skinframe;
2754 if (cls.state == ca_dedicated)
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)
2761 textureflags &= ~TEXF_FORCE_RELOAD;
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;
2775 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2779 if (developer_loading.integer)
2780 Con_Printf("loading embedded 8bit image \"%s\"\n", name);
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)
2785 for (i = 0;i < width * height;i++)
2787 if (((unsigned char *)palette)[skindata[i]*4+3] < 255)
2789 skinframe->hasalpha = true;
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);
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]);
2803 skinframe_t *R_SkinFrame_LoadMissing(void)
2805 skinframe_t *skinframe;
2807 if (cls.state == ca_dedicated)
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;
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;
2831 skinframe_t *R_SkinFrame_LoadNoTexture(void)
2833 if (cls.state == ca_dedicated)
2836 return R_SkinFrame_LoadInternalBGRA("notexture", TEXF_FORCENEAREST, Image_GenerateNoTexture(), 16, 16, 0, 0, 0, false);
2839 skinframe_t *R_SkinFrame_LoadInternalUsingTexture(const char *name, int textureflags, rtexture_t *tex, int width, int height, qbool sRGB)
2841 skinframe_t *skinframe;
2842 if (cls.state == ca_dedicated)
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)
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
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
2870 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
2871 typedef struct suffixinfo_s
2874 qbool flipx, flipy, flipdiagonal;
2877 static suffixinfo_t suffix[3][6] =
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}
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}
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}
2905 static int componentorder[4] = {0, 1, 2, 3};
2907 static rtexture_t *R_LoadCubemap(const char *basename)
2909 int i, j, cubemapsize, forcefilter;
2910 unsigned char *cubemappixels, *image_buffer;
2911 rtexture_t *cubemaptexture;
2914 // HACK: if the cubemap name starts with a !, the cubemap is nearest-filtered
2915 forcefilter = TEXF_FORCELINEAR;
2916 if (basename && basename[0] == '!')
2919 forcefilter = TEXF_FORCENEAREST;
2921 // must start 0 so the first loadimagepixels has no requested width/height
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++)
2928 // load the 6 images in the suffix group
2929 for (i = 0;i < 6;i++)
2931 // generate an image name based on the base and and suffix
2932 dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
2934 if ((image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
2936 // an image loaded, make sure width and height are equal
2937 if (image_width == image_height && (!cubemappixels || image_width == cubemapsize))
2939 // if this is the first image to load successfully, allocate the cubemap memory
2940 if (!cubemappixels && image_width >= 1)
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);
2946 // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
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);
2951 Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
2953 Mem_Free(image_buffer);
2957 // if a cubemap loaded, upload it
2960 if (developer_loading.integer)
2961 Con_Printf("loading cubemap \"%s\"\n", basename);
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);
2968 Con_DPrintf("failed to load cubemap \"%s\"\n", basename);
2969 if (developer_loading.integer)
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");
2978 return cubemaptexture;
2981 rtexture_t *R_GetCubemap(const char *basename)
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;
2997 static void R_Main_FreeViewCache(void)
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));
3010 static void R_Main_ResizeViewCache(void)
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)
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);
3024 if (r_refdef.viewcache.world_numclusters != numclusters)
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);
3032 if (r_refdef.viewcache.world_numleafs != numleafs)
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);
3039 if (r_refdef.viewcache.world_numsurfaces != numsurfaces)
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);
3048 extern rtexture_t *loadingscreentexture;
3049 static void gl_main_start(void)
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;
3064 r_loaddds = r_texture_dds_load.integer != 0;
3065 r_savedds = vid.support.ext_texture_compression_s3tc && r_texture_dds_save.integer;
3067 switch(vid.renderpath)
3069 case RENDERPATH_GL32:
3070 case RENDERPATH_GLES2:
3071 r_loadnormalmap = true;
3074 #ifdef GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT
3075 qglGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &r_uniformbufferalignment);
3081 R_FrameData_Reset();
3082 R_BufferData_Reset();
3086 memset(r_queries, 0, sizeof(r_queries));
3088 r_qwskincache = NULL;
3089 r_qwskincache_size = 0;
3091 // due to caching of texture_t references, the collision cache must be reset
3092 Collision_Cache_Reset(true);
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);
3099 r_main_texturepool = R_AllocTexturePool();
3100 R_BuildBlankTextures();
3104 R_BuildNormalizationCube();
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));
3117 memset(r_texture_cubemaps, 0, sizeof(r_texture_cubemaps));
3118 r_texture_numcubemaps = 0;
3120 r_refdef.fogmasktable_density = 0;
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");
3144 extern unsigned int r_shadow_occlusion_buf;
3146 static void gl_main_shutdown(void)
3148 R_RenderTarget_FreeUnused(true);
3149 Mem_ExpandableArray_FreeArray(&r_fb.rendertargets);
3151 R_FrameData_Reset();
3152 R_BufferData_Reset();
3154 R_Main_FreeViewCache();
3156 switch(vid.renderpath)
3158 case RENDERPATH_GL32:
3159 case RENDERPATH_GLES2:
3160 #if defined(GL_SAMPLES_PASSED) && !defined(USE_GLES2)
3162 qglDeleteQueries(r_maxqueries, r_queries);
3166 r_shadow_occlusion_buf = 0;
3169 memset(r_queries, 0, sizeof(r_queries));
3171 r_qwskincache = NULL;
3172 r_qwskincache_size = 0;
3174 // clear out the r_skinframe state
3175 Mem_ExpandableArray_FreeArray(&r_skinframe.array);
3176 memset(&r_skinframe, 0, sizeof(r_skinframe));
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);
3197 r_glsl_permutation = NULL;
3198 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
3199 Mem_ExpandableArray_FreeArray(&r_glsl_permutationarray);
3202 static void gl_main_newmap(void)
3204 // FIXME: move this code to client
3205 char *entities, entname[MAX_QPATH];
3207 Mem_Free(r_qwskincache);
3208 r_qwskincache = NULL;
3209 r_qwskincache_size = 0;
3212 dpsnprintf(entname, sizeof(entname), "%s.ent", cl.worldnamenoextension);
3213 if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
3215 CL_ParseEntityLump(entities);
3219 if (cl.worldmodel->brush.entities)
3220 CL_ParseEntityLump(cl.worldmodel->brush.entities);
3222 R_Main_FreeViewCache();
3224 R_FrameData_Reset();
3225 R_BufferData_Reset();
3228 void GL_Main_Init(void)
3231 r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
3232 R_InitShaderModeInfo();
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)
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);
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);
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);
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);
3425 R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap, NULL, NULL);
3428 void Render_Init(void)
3441 R_LightningBeams_Init();
3442 CL_MeshEntities_Init();
3446 static void R_GetCornerOfBox(vec3_t out, const vec3_t mins, const vec3_t maxs, int signbits)
3448 out[0] = ((signbits & 1) ? mins : maxs)[0];
3449 out[1] = ((signbits & 2) ? mins : maxs)[1];
3450 out[2] = ((signbits & 4) ? mins : maxs)[2];
3453 static qbool _R_CullBox(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes, int ignore)
3458 if (r_trippy.integer)
3460 for (i = 0;i < numplanes;i++)
3465 R_GetCornerOfBox(corner, mins, maxs, p->signbits);
3466 if (DotProduct(p->normal, corner) < p->dist)
3472 qbool R_CullFrustum(const vec3_t mins, const vec3_t maxs)
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);
3478 qbool R_CullBox(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
3480 // nothing to ignore
3481 return _R_CullBox(mins, maxs, numplanes, planes, -1);
3484 //==================================================================================
3486 // LadyHavoc: this stores temporary data used within the same frame
3488 typedef struct r_framedata_mem_s
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)
3499 static r_framedata_mem_t *r_framedata_mem;
3501 void R_FrameData_Reset(void)
3503 while (r_framedata_mem)
3505 r_framedata_mem_t *next = r_framedata_mem->purge;
3506 Mem_Free(r_framedata_mem);
3507 r_framedata_mem = next;
3511 static void R_FrameData_Resize(qbool mustgrow)
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)
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;
3524 newmem->purge = r_framedata_mem;
3525 r_framedata_mem = newmem;
3529 void R_FrameData_NewFrame(void)
3531 R_FrameData_Resize(false);
3532 if (!r_framedata_mem)
3534 // if we ran out of space on the last frame, free the old memory now
3535 while (r_framedata_mem->purge)
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;
3542 // reset the current mem pointer
3543 r_framedata_mem->current = 0;
3544 r_framedata_mem->mark = 0;
3547 void *R_FrameData_Alloc(size_t size)
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;
3556 while (!r_framedata_mem || r_framedata_mem->current + size > r_framedata_mem->size)
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));
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);
3571 data = r_framedata_mem->data + r_framedata_mem->current;
3572 r_framedata_mem->current += size;
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);
3578 return (void *)data;
3581 void *R_FrameData_Store(size_t size, void *data)
3583 void *d = R_FrameData_Alloc(size);
3585 memcpy(d, data, size);
3589 void R_FrameData_SetMark(void)
3591 if (!r_framedata_mem)
3593 r_framedata_mem->mark = r_framedata_mem->current;
3596 void R_FrameData_ReturnToMark(void)
3598 if (!r_framedata_mem)
3600 r_framedata_mem->current = r_framedata_mem->mark;
3603 //==================================================================================
3605 // avoid reusing the same buffer objects on consecutive frames
3606 #define R_BUFFERDATA_CYCLE 3
3608 typedef struct r_bufferdata_buffer_s
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
3615 r_bufferdata_buffer_t;
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];
3620 /// frees all dynamic buffers
3621 void R_BufferData_Reset(void)
3624 r_bufferdata_buffer_t **p, *mem;
3625 for (cycle = 0;cycle < R_BUFFERDATA_CYCLE;cycle++)
3627 for (type = 0;type < R_BUFFERDATA_COUNT;type++)
3630 p = &r_bufferdata_buffer[cycle][type];
3636 R_Mesh_DestroyMeshBuffer(mem->buffer);
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)
3646 r_bufferdata_buffer_t *mem = r_bufferdata_buffer[r_bufferdata_cycle][type];
3648 float newvalue = r_buffermegs[type].value;
3650 // increase the cvar if we have to (but only if we already have a mem)
3651 if (mustgrow && mem)
3653 newvalue = bound(0.25f, newvalue, 256.0f);
3654 while (newvalue * 1024*1024 < minsize)
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);
3662 // calculate size in bytes
3663 size = (size_t)(newvalue * 1024*1024);
3664 size = bound(131072, size, 256*1024*1024);
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)
3670 mem = (r_bufferdata_buffer_t *)Mem_Alloc(r_main_mempool, sizeof(*mem));
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;
3686 void R_BufferData_NewFrame(void)
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++)
3695 if (r_bufferdata_buffer[r_bufferdata_cycle][type])
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;
3706 R_Mesh_DestroyMeshBuffer(mem->buffer);
3709 // reset the current offset
3710 r_bufferdata_buffer[r_bufferdata_cycle][type]->current = 0;
3715 r_meshbuffer_t *R_BufferData_Store(size_t datasize, const void *data, r_bufferdata_type_t type, int *returnbufferoffset)
3717 r_bufferdata_buffer_t *mem;
3721 *returnbufferoffset = 0;
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);
3728 padsize = (datasize + 15) & ~15;
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);
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");
3738 mem = r_bufferdata_buffer[r_bufferdata_cycle][type];
3739 offset = (int)mem->current;
3740 mem->current += padsize;
3742 // upload the data to the buffer at the chosen offset
3744 R_Mesh_UpdateMeshBuffer(mem->buffer, NULL, mem->size, false, 0);
3745 R_Mesh_UpdateMeshBuffer(mem->buffer, data, datasize, true, offset);
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);
3751 // return the buffer offset
3752 *returnbufferoffset = offset;
3757 //==================================================================================
3759 // LadyHavoc: animcache originally written by Echon, rewritten since then
3762 * Animation cache prevents re-generating mesh data for an animated model
3763 * multiple times in one frame for lighting, shadowing, reflections, etc.
3766 void R_AnimCache_Free(void)
3770 void R_AnimCache_ClearCache(void)
3773 entity_render_t *ent;
3775 for (i = 0;i < r_refdef.scene.numentities;i++)
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;
3797 qbool R_AnimCache_GetEntity(entity_render_t *ent, qbool wantnormals, qbool wanttangents)
3799 model_t *model = ent->model;
3802 // see if this ent is worth caching
3803 if (!model || !model->Draw || !model->AnimateVertices)
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))
3808 // see if it is already cached for gpuskeletal
3809 if (ent->animcache_skeletaltransform3x4)
3811 // see if it is already cached as a mesh
3812 if (ent->animcache_vertex3f)
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)
3823 // check which kind of cache we need to generate
3824 if (r_gpuskeletal && model->num_bones > 0 && model->surfmesh.data_skeletalindex4ub)
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);
3836 else if (ent->animcache_vertex3f)
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)
3842 numvertices = model->surfmesh.num_vertices;
3844 ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3847 ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3848 ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
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);
3858 // generate mesh cache
3859 numvertices = model->surfmesh.num_vertices;
3860 ent->animcache_vertex3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3862 ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3865 ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3866 ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3868 model->AnimateVertices(model, ent->frameblend, ent->skeleton, ent->animcache_vertex3f, ent->animcache_normal3f, ent->animcache_svector3f, ent->animcache_tvector3f);
3869 if (wantnormals || wanttangents)
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);
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);
3882 void R_AnimCache_CacheVisibleEntities(void)
3886 // TODO: thread this
3887 // NOTE: R_PrepareRTLights() also caches entities
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);
3894 //==================================================================================
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)
3898 long unsigned int i;
3900 vec3_t eyemins, eyemaxs;
3901 vec3_t boxmins, boxmaxs;
3902 vec3_t padmins, padmaxs;
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 },
3918 // sample count can be set to -1 to skip this logic, for flicker-prone objects
3922 // view origin is not used for culling in portal/reflection/refraction renders or isometric views
3923 if (!r_refdef.view.usevieworiginculling)
3926 if (!r_cullentities_trace_entityocclusion.integer && (!model || !model->brush.TraceLineOfSight))
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;
3951 // return true if eye overlaps enlarged box
3952 if (BoxesOverlap(boxmins, boxmaxs, eyemins, eyemaxs))
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)
3959 for (i = 0; i < sizeof(positions) / sizeof(positions[0]); i++)
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))
3975 VectorMAM(0.5f, boxmins, 0.5f, boxmaxs, end);
3976 if (model->brush.TraceLineOfSight(model, start, end, padmins, padmaxs))
3980 // try various random positions
3981 for (j = 0; j < numsamples; j++)
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)
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))
3992 else if (model->brush.TraceLineOfSight(model, start, end, padmins, padmaxs))
4000 static void R_View_UpdateEntityVisible (void)
4005 entity_render_t *ent;
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;
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)
4020 // worldmodel can check visibility
4021 for (i = 0;i < r_refdef.scene.numentities;i++)
4023 ent = r_refdef.scene.entities[i];
4024 if (r_refdef.viewcache.world_novis && !(ent->flags & RENDER_VIEWMODEL))
4026 r_refdef.viewcache.entityvisible[i] = false;
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;
4037 // no worldmodel or it can't check visibility
4038 for (i = 0;i < r_refdef.scene.numentities;i++)
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;
4046 if (r_cullentities_trace.integer)
4048 for (i = 0;i < r_refdef.scene.numentities;i++)
4050 if (!r_refdef.viewcache.entityvisible[i])
4052 ent = r_refdef.scene.entities[i];
4053 if (!(ent->flags & (RENDER_VIEWMODEL | RENDER_WORLDOBJECT | RENDER_NODEPTHTEST)) && !(ent->model && (ent->model->name[0] == '*')))
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;
4065 /// only used if skyrendermasked, and normally returns false
4066 static int R_DrawBrushModelsSky (void)
4069 entity_render_t *ent;
4072 for (i = 0;i < r_refdef.scene.numentities;i++)
4074 if (!r_refdef.viewcache.entityvisible[i])
4076 ent = r_refdef.scene.entities[i];
4077 if (!ent->model || !ent->model->DrawSky)
4079 ent->model->DrawSky(ent);
4085 static void R_DrawNoModel(entity_render_t *ent);
4086 static void R_DrawModels(void)
4089 entity_render_t *ent;
4091 for (i = 0;i < r_refdef.scene.numentities;i++)
4093 if (!r_refdef.viewcache.entityvisible[i])
4095 ent = r_refdef.scene.entities[i];
4096 r_refdef.stats[r_stat_entities]++;
4098 if (ent->model && ent->model->Draw != NULL)
4099 ent->model->Draw(ent);
4105 static void R_DrawModelsDepth(void)
4108 entity_render_t *ent;
4110 for (i = 0;i < r_refdef.scene.numentities;i++)
4112 if (!r_refdef.viewcache.entityvisible[i])
4114 ent = r_refdef.scene.entities[i];
4115 if (ent->model && ent->model->DrawDepth != NULL)
4116 ent->model->DrawDepth(ent);
4120 static void R_DrawModelsDebug(void)
4123 entity_render_t *ent;
4125 for (i = 0;i < r_refdef.scene.numentities;i++)
4127 if (!r_refdef.viewcache.entityvisible[i])
4129 ent = r_refdef.scene.entities[i];
4130 if (ent->model && ent->model->DrawDebug != NULL)
4131 ent->model->DrawDebug(ent);
4135 static void R_DrawModelsAddWaterPlanes(void)
4138 entity_render_t *ent;
4140 for (i = 0;i < r_refdef.scene.numentities;i++)
4142 if (!r_refdef.viewcache.entityvisible[i])
4144 ent = r_refdef.scene.entities[i];
4145 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
4146 ent->model->DrawAddWaterPlanes(ent);
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}};
4152 void R_HDR_UpdateIrisAdaptation(const vec3_t point)
4154 if (r_hdr_irisadaptation.integer)
4159 vec3_t diffusenormal;
4161 vec_t brightness = 0.0f;
4166 VectorCopy(r_refdef.view.forward, forward);
4167 for (c = 0;c < (int)(sizeof(irisvecs)/sizeof(irisvecs[0]));c++)
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);
4176 brightness += d * VectorLength(diffuse);
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;
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);
4190 else if (r_hdr_irisadaptation_value.value != 1.0f)
4191 Cvar_SetValueQuick(&r_hdr_irisadaptation_value, 1.0f);
4194 extern cvar_t r_lockvisibility;
4195 extern cvar_t r_lockpvs;
4197 static void R_View_SetFrustum(const int *scissor)
4200 double fpx = +1, fnx = -1, fpy = +1, fny = -1;
4201 vec3_t forward, left, up, origin, v;
4202 if(r_lockvisibility.integer)
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);
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);
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;
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;
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];
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];
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];
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];
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];
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];
4279 if (r_refdef.view.useperspective)
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]);
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);
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)
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);
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);
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]);
4320 VectorCopy(forward, r_refdef.view.frustum[4].normal);
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;
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;
4341 r_refdef.view.numfrustumplanes = 5;
4343 if (r_refdef.view.useclipplane)
4345 r_refdef.view.numfrustumplanes = 6;
4346 r_refdef.view.frustum[5] = r_refdef.view.clipplane;
4349 for (i = 0;i < r_refdef.view.numfrustumplanes;i++)
4350 PlaneClassify(r_refdef.view.frustum + i);
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.
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]);
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]);
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]);
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]);
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]);
4382 static void R_View_UpdateWithScissor(const int *myscissor)
4384 R_Main_ResizeViewCache();
4385 R_View_SetFrustum(myscissor);
4386 R_View_WorldVisibility(!r_refdef.view.usevieworiginculling);
4387 R_View_UpdateEntityVisible();
4390 static void R_View_Update(void)
4392 R_Main_ResizeViewCache();
4393 R_View_SetFrustum(NULL);
4394 R_View_WorldVisibility(!r_refdef.view.usevieworiginculling);
4395 R_View_UpdateEntityVisible();
4398 float viewscalefpsadjusted = 1.0f;
4400 void R_SetupView(qbool allowwaterclippingplane, int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4402 const float *customclipplane = NULL;
4405 if (r_refdef.view.useclipplane && allowwaterclippingplane)
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];
4416 customclipplane = plane;
4419 // GL is weird because it's bottom to top, r_refdef.view.y is top to bottom.
4420 // Unless the render target is a FBO...
4421 viewy_adjusted = viewfbo ? viewy : vid.height - viewheight - viewy;
4423 if (!r_refdef.view.useperspective)
4424 R_Viewport_InitOrtho3D(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, viewy_adjusted, viewwidth, viewheight, r_refdef.view.ortho_x, r_refdef.view.ortho_y, -r_refdef.farclip, r_refdef.farclip, customclipplane);
4425 else if (vid.stencil && r_useinfinitefarclip.integer)
4426 R_Viewport_InitPerspectiveInfinite(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, viewy_adjusted, viewwidth, viewheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, customclipplane);
4428 R_Viewport_InitPerspective(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, viewy_adjusted, viewwidth, viewheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, r_refdef.farclip, customclipplane);
4429 R_Mesh_SetRenderTargets(viewfbo, viewdepthtexture, viewcolortexture, NULL, NULL, NULL);
4430 R_SetViewport(&r_refdef.view.viewport);
4433 void R_EntityMatrix(const matrix4x4_t *matrix)
4435 if (gl_modelmatrixchanged || memcmp(matrix, &gl_modelmatrix, sizeof(matrix4x4_t)))
4437 gl_modelmatrixchanged = false;
4438 gl_modelmatrix = *matrix;
4439 Matrix4x4_Concat(&gl_modelviewmatrix, &gl_viewmatrix, &gl_modelmatrix);
4440 Matrix4x4_Concat(&gl_modelviewprojectionmatrix, &gl_projectionmatrix, &gl_modelviewmatrix);
4441 Matrix4x4_ToArrayFloatGL(&gl_modelviewmatrix, gl_modelview16f);
4442 Matrix4x4_ToArrayFloatGL(&gl_modelviewprojectionmatrix, gl_modelviewprojection16f);
4444 switch(vid.renderpath)
4446 case RENDERPATH_GL32:
4447 case RENDERPATH_GLES2:
4448 if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
4449 if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
4455 void R_ResetViewRendering2D_Common(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight, float x2, float y2)
4457 r_viewport_t viewport;
4462 // GL is weird because it's bottom to top, r_refdef.view.y is top to bottom.
4463 // Unless the render target is a FBO...
4464 viewy_adjusted = viewfbo ? viewy : vid.height - viewheight - viewy;
4466 R_Viewport_InitOrtho(&viewport, &identitymatrix, viewx, viewy_adjusted, viewwidth, viewheight, 0, 0, x2, y2, -10, 100, NULL);
4467 R_Mesh_SetRenderTargets(viewfbo, viewdepthtexture, viewcolortexture, NULL, NULL, NULL);
4468 R_SetViewport(&viewport);
4469 GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
4470 GL_Color(1, 1, 1, 1);
4471 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4472 GL_BlendFunc(GL_ONE, GL_ZERO);
4473 GL_ScissorTest(false);
4474 GL_DepthMask(false);
4475 GL_DepthRange(0, 1);
4476 GL_DepthTest(false);
4477 GL_DepthFunc(GL_LEQUAL);
4478 R_EntityMatrix(&identitymatrix);
4479 R_Mesh_ResetTextureState();
4480 GL_PolygonOffset(0, 0);
4481 switch(vid.renderpath)
4483 case RENDERPATH_GL32:
4484 case RENDERPATH_GLES2:
4485 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
4488 GL_CullFace(GL_NONE);
4493 void R_ResetViewRendering2D(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4495 R_ResetViewRendering2D_Common(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight, 1.0f, 1.0f);
4498 void R_ResetViewRendering3D(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4500 R_SetupView(true, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
4501 GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
4502 GL_Color(1, 1, 1, 1);
4503 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4504 GL_BlendFunc(GL_ONE, GL_ZERO);
4505 GL_ScissorTest(true);
4507 GL_DepthRange(0, 1);
4509 GL_DepthFunc(GL_LEQUAL);
4510 R_EntityMatrix(&identitymatrix);
4511 R_Mesh_ResetTextureState();
4512 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
4513 switch(vid.renderpath)
4515 case RENDERPATH_GL32:
4516 case RENDERPATH_GLES2:
4517 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
4520 GL_CullFace(r_refdef.view.cullface_back);
4525 R_RenderView_UpdateViewVectors
4528 void R_RenderView_UpdateViewVectors(void)
4530 // break apart the view matrix into vectors for various purposes
4531 // it is important that this occurs outside the RenderScene function because that can be called from reflection renders, where the vectors come out wrong
4532 // however the r_refdef.view.origin IS updated in RenderScene intentionally - otherwise the sky renders at the wrong origin, etc
4533 Matrix4x4_ToVectors(&r_refdef.view.matrix, r_refdef.view.forward, r_refdef.view.left, r_refdef.view.up, r_refdef.view.origin);
4534 VectorNegate(r_refdef.view.left, r_refdef.view.right);
4535 // make an inverted copy of the view matrix for tracking sprites
4536 Matrix4x4_Invert_Full(&r_refdef.view.inverse_matrix, &r_refdef.view.matrix);
4539 void R_RenderTarget_FreeUnused(qbool force)
4541 unsigned int i, j, end;
4542 end = (unsigned int)Mem_ExpandableArray_IndexRange(&r_fb.rendertargets); // checked
4543 for (i = 0; i < end; i++)
4545 r_rendertarget_t *r = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, i);
4546 // free resources for rendertargets that have not been used for a while
4547 // (note: this check is run after the frame render, so any targets used
4548 // this frame will not be affected even at low framerates)
4549 if (r && (host.realtime - r->lastusetime > 0.2 || force))
4552 R_Mesh_DestroyFramebufferObject(r->fbo);
4553 for (j = 0; j < sizeof(r->colortexture) / sizeof(r->colortexture[0]); j++)
4554 if (r->colortexture[j])
4555 R_FreeTexture(r->colortexture[j]);
4556 if (r->depthtexture)
4557 R_FreeTexture(r->depthtexture);
4558 Mem_ExpandableArray_FreeRecord(&r_fb.rendertargets, r);
4563 static void R_CalcTexCoordsForView(float x, float y, float w, float h, float tw, float th, float *texcoord2f)
4565 float iw = 1.0f / tw, ih = 1.0f / th, x1, y1, x2, y2;
4569 y2 = (th - y - h) * ih;
4580 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)
4582 unsigned int i, j, end;
4583 r_rendertarget_t *r = NULL;
4585 // first try to reuse an existing slot if possible
4586 end = (unsigned int)Mem_ExpandableArray_IndexRange(&r_fb.rendertargets); // checked
4587 for (i = 0; i < end; i++)
4589 r = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, i);
4590 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)
4595 // no unused exact match found, so we have to make one in the first unused slot
4596 r = (r_rendertarget_t *)Mem_ExpandableArray_AllocRecord(&r_fb.rendertargets);
4597 r->texturewidth = texturewidth;
4598 r->textureheight = textureheight;
4599 r->colortextype[0] = colortextype0;
4600 r->colortextype[1] = colortextype1;
4601 r->colortextype[2] = colortextype2;
4602 r->colortextype[3] = colortextype3;
4603 r->depthtextype = depthtextype;
4604 r->depthisrenderbuffer = depthisrenderbuffer;
4605 for (j = 0; j < 4; j++)
4606 if (r->colortextype[j])
4607 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);
4608 if (r->depthtextype)
4610 if (r->depthisrenderbuffer)
4611 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);
4613 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);
4615 r->fbo = R_Mesh_CreateFramebufferObject(r->depthtexture, r->colortexture[0], r->colortexture[1], r->colortexture[2], r->colortexture[3]);
4617 r_refdef.stats[r_stat_rendertargets_used]++;
4618 r_refdef.stats[r_stat_rendertargets_pixels] += r->texturewidth * r->textureheight;
4619 r->lastusetime = host.realtime;
4620 R_CalcTexCoordsForView(0, 0, r->texturewidth, r->textureheight, r->texturewidth, r->textureheight, r->texcoord2f);
4624 static void R_Water_StartFrame(int viewwidth, int viewheight)
4626 int waterwidth, waterheight;
4628 if (viewwidth > (int)vid.maxtexturesize_2d || viewheight > (int)vid.maxtexturesize_2d)
4631 // set waterwidth and waterheight to the water resolution that will be
4632 // used (often less than the screen resolution for faster rendering)
4633 waterwidth = (int)bound(16, viewwidth * r_water_resolutionmultiplier.value, viewwidth);
4634 waterheight = (int)bound(16, viewheight * r_water_resolutionmultiplier.value, viewheight);
4636 if (!r_water.integer || r_showsurfaces.integer || r_lockvisibility.integer || r_lockpvs.integer)
4637 waterwidth = waterheight = 0;
4639 // set up variables that will be used in shader setup
4640 r_fb.water.waterwidth = waterwidth;
4641 r_fb.water.waterheight = waterheight;
4642 r_fb.water.texturewidth = waterwidth;
4643 r_fb.water.textureheight = waterheight;
4644 r_fb.water.camerawidth = waterwidth;
4645 r_fb.water.cameraheight = waterheight;
4646 r_fb.water.screenscale[0] = 0.5f;
4647 r_fb.water.screenscale[1] = 0.5f;
4648 r_fb.water.screencenter[0] = 0.5f;
4649 r_fb.water.screencenter[1] = 0.5f;
4650 r_fb.water.enabled = waterwidth != 0;
4652 r_fb.water.maxwaterplanes = MAX_WATERPLANES;
4653 r_fb.water.numwaterplanes = 0;
4656 void R_Water_AddWaterPlane(msurface_t *surface, int entno)
4658 int planeindex, bestplaneindex, vertexindex;
4659 vec3_t mins, maxs, normal, center, v, n;
4660 vec_t planescore, bestplanescore;
4662 r_waterstate_waterplane_t *p;
4663 texture_t *t = R_GetCurrentTexture(surface->texture);
4665 rsurface.texture = t;
4666 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS, 1, ((const msurface_t **)&surface));
4667 // if the model has no normals, it's probably off-screen and they were not generated, so don't add it anyway
4668 if (!rsurface.batchnormal3f || rsurface.batchnumvertices < 1)
4670 // average the vertex normals, find the surface bounds (after deformvertexes)
4671 Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f, v);
4672 Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f, n);
4673 VectorCopy(n, normal);
4674 VectorCopy(v, mins);
4675 VectorCopy(v, maxs);
4676 for (vertexindex = 1;vertexindex < rsurface.batchnumvertices;vertexindex++)
4678 Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f + vertexindex*3, v);
4679 Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f + vertexindex*3, n);
4680 VectorAdd(normal, n, normal);
4681 mins[0] = min(mins[0], v[0]);
4682 mins[1] = min(mins[1], v[1]);
4683 mins[2] = min(mins[2], v[2]);
4684 maxs[0] = max(maxs[0], v[0]);
4685 maxs[1] = max(maxs[1], v[1]);
4686 maxs[2] = max(maxs[2], v[2]);
4688 VectorNormalize(normal);
4689 VectorMAM(0.5f, mins, 0.5f, maxs, center);
4691 VectorCopy(normal, plane.normal);
4692 VectorNormalize(plane.normal);
4693 plane.dist = DotProduct(center, plane.normal);
4694 PlaneClassify(&plane);
4695 if (PlaneDiff(r_refdef.view.origin, &plane) < 0)
4697 // skip backfaces (except if nocullface is set)
4698 // if (!(t->currentmaterialflags & MATERIALFLAG_NOCULLFACE))
4700 VectorNegate(plane.normal, plane.normal);
4702 PlaneClassify(&plane);
4706 // find a matching plane if there is one
4707 bestplaneindex = -1;
4708 bestplanescore = 1048576.0f;
4709 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
4711 if(p->camera_entity == t->camera_entity)
4713 planescore = 1.0f - DotProduct(plane.normal, p->plane.normal) + fabs(plane.dist - p->plane.dist) * 0.001f;
4714 if (bestplaneindex < 0 || bestplanescore > planescore)
4716 bestplaneindex = planeindex;
4717 bestplanescore = planescore;
4721 planeindex = bestplaneindex;
4723 // if this surface does not fit any known plane rendered this frame, add one
4724 if (planeindex < 0 || bestplanescore > 0.001f)
4726 if (r_fb.water.numwaterplanes < r_fb.water.maxwaterplanes)
4728 // store the new plane
4729 planeindex = r_fb.water.numwaterplanes++;
4730 p = r_fb.water.waterplanes + planeindex;
4732 // clear materialflags and pvs
4733 p->materialflags = 0;
4734 p->pvsvalid = false;
4735 p->camera_entity = t->camera_entity;
4736 VectorCopy(mins, p->mins);
4737 VectorCopy(maxs, p->maxs);
4741 // We're totally screwed.
4747 // merge mins/maxs when we're adding this surface to the plane
4748 p = r_fb.water.waterplanes + planeindex;
4749 p->mins[0] = min(p->mins[0], mins[0]);
4750 p->mins[1] = min(p->mins[1], mins[1]);
4751 p->mins[2] = min(p->mins[2], mins[2]);
4752 p->maxs[0] = max(p->maxs[0], maxs[0]);
4753 p->maxs[1] = max(p->maxs[1], maxs[1]);
4754 p->maxs[2] = max(p->maxs[2], maxs[2]);
4756 // merge this surface's materialflags into the waterplane
4757 p->materialflags |= t->currentmaterialflags;
4758 if(!(p->materialflags & MATERIALFLAG_CAMERA))
4760 // merge this surface's PVS into the waterplane
4761 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS
4762 && r_refdef.scene.worldmodel->brush.PointInLeaf && r_refdef.scene.worldmodel->brush.PointInLeaf(r_refdef.scene.worldmodel, center)->clusterindex >= 0)
4764 r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, center, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
4770 extern cvar_t r_drawparticles;
4771 extern cvar_t r_drawdecals;
4773 static void R_Water_ProcessPlanes(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, int viewx, int viewy, int viewwidth, int viewheight)
4776 r_refdef_view_t originalview;
4777 r_refdef_view_t myview;
4778 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;
4779 r_waterstate_waterplane_t *p;
4781 r_rendertarget_t *rt;
4783 originalview = r_refdef.view;
4785 // lowquality hack, temporarily shut down some cvars and restore afterwards
4786 qualityreduction = r_water_lowquality.integer;
4787 if (qualityreduction > 0)
4789 if (qualityreduction >= 1)
4791 old_r_shadows = r_shadows.integer;
4792 old_r_worldrtlight = r_shadow_realtime_world.integer;
4793 old_r_dlight = r_shadow_realtime_dlight.integer;
4794 Cvar_SetValueQuick(&r_shadows, 0);
4795 Cvar_SetValueQuick(&r_shadow_realtime_world, 0);
4796 Cvar_SetValueQuick(&r_shadow_realtime_dlight, 0);
4798 if (qualityreduction >= 2)
4800 old_r_dynamic = r_dynamic.integer;
4801 old_r_particles = r_drawparticles.integer;
4802 old_r_decals = r_drawdecals.integer;
4803 Cvar_SetValueQuick(&r_dynamic, 0);
4804 Cvar_SetValueQuick(&r_drawparticles, 0);
4805 Cvar_SetValueQuick(&r_drawdecals, 0);
4809 for (planeindex = 0, p = r_fb.water.waterplanes; planeindex < r_fb.water.numwaterplanes; planeindex++, p++)
4811 p->rt_reflection = NULL;
4812 p->rt_refraction = NULL;
4813 p->rt_camera = NULL;
4817 r_refdef.view = originalview;
4818 r_refdef.view.showdebug = false;
4819 r_refdef.view.width = r_fb.water.waterwidth;
4820 r_refdef.view.height = r_fb.water.waterheight;
4821 r_refdef.view.useclipplane = true;
4822 myview = r_refdef.view;
4823 r_fb.water.renderingscene = true;
4824 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
4826 if (r_water_cameraentitiesonly.value != 0 && !p->camera_entity)
4829 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
4831 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);
4832 if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4834 r_refdef.view = myview;
4835 Matrix4x4_Reflect(&r_refdef.view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
4836 Matrix4x4_OriginFromMatrix(&r_refdef.view.matrix, r_refdef.view.origin);
4837 if(r_water_scissormode.integer)
4839 R_SetupView(true, rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, r_fb.water.waterwidth, r_fb.water.waterheight);
4840 if (R_ScissorForBBox(p->mins, p->maxs, myscissor))
4842 p->rt_reflection = NULL;
4843 p->rt_refraction = NULL;
4844 p->rt_camera = NULL;
4849 r_refdef.view.clipplane = p->plane;
4850 // reflected view origin may be in solid, so don't cull with it
4851 r_refdef.view.usevieworiginculling = false;
4852 // reverse the cullface settings for this render
4853 r_refdef.view.cullface_front = GL_FRONT;
4854 r_refdef.view.cullface_back = GL_BACK;
4855 // combined pvs (based on what can be seen from each surface center)
4856 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
4858 r_refdef.view.usecustompvs = true;
4860 memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4862 memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4865 r_fb.water.hideplayer = ((r_water_hideplayer.integer >= 2) && !chase_active.integer);
4866 R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4867 GL_ScissorTest(false);
4868 R_ClearScreen(r_refdef.fogenabled);
4869 GL_ScissorTest(true);
4870 if(r_water_scissormode.integer & 2)
4871 R_View_UpdateWithScissor(myscissor);
4874 R_AnimCache_CacheVisibleEntities();
4875 if(r_water_scissormode.integer & 1)
4876 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
4877 R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4879 r_fb.water.hideplayer = false;
4880 p->rt_reflection = rt;
4883 // render the normal view scene and copy into texture
4884 // (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)
4885 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
4887 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);
4888 if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4890 r_refdef.view = myview;
4891 if(r_water_scissormode.integer)
4893 R_SetupView(true, rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, r_fb.water.waterwidth, r_fb.water.waterheight);
4894 if (R_ScissorForBBox(p->mins, p->maxs, myscissor))
4896 p->rt_reflection = NULL;
4897 p->rt_refraction = NULL;
4898 p->rt_camera = NULL;
4903 // combined pvs (based on what can be seen from each surface center)
4904 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
4906 r_refdef.view.usecustompvs = true;
4908 memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4910 memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4913 r_fb.water.hideplayer = ((r_water_hideplayer.integer >= 1) && !chase_active.integer);
4915 r_refdef.view.clipplane = p->plane;
4916 VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
4917 r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
4919 if((p->materialflags & MATERIALFLAG_CAMERA) && p->camera_entity)
4921 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
4922 r_fb.water.hideplayer = false; // we don't want to hide the player model from these ones
4923 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
4924 R_RenderView_UpdateViewVectors();
4925 if(r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
4927 r_refdef.view.usecustompvs = true;
4928 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);
4932 PlaneClassify(&r_refdef.view.clipplane);
4934 R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4935 GL_ScissorTest(false);
4936 R_ClearScreen(r_refdef.fogenabled);
4937 GL_ScissorTest(true);
4938 if(r_water_scissormode.integer & 2)
4939 R_View_UpdateWithScissor(myscissor);
4942 R_AnimCache_CacheVisibleEntities();
4943 if(r_water_scissormode.integer & 1)
4944 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
4945 R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4947 r_fb.water.hideplayer = false;
4948 p->rt_refraction = rt;
4950 else if (p->materialflags & MATERIALFLAG_CAMERA)
4952 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);
4953 if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4955 r_refdef.view = myview;
4957 r_refdef.view.clipplane = p->plane;
4958 VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
4959 r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
4961 r_refdef.view.width = r_fb.water.camerawidth;
4962 r_refdef.view.height = r_fb.water.cameraheight;
4963 r_refdef.view.frustum_x = 1; // tan(45 * M_PI / 180.0);
4964 r_refdef.view.frustum_y = 1; // tan(45 * M_PI / 180.0);
4965 r_refdef.view.ortho_x = 90; // abused as angle by VM_CL_R_SetView
4966 r_refdef.view.ortho_y = 90; // abused as angle by VM_CL_R_SetView
4968 if(p->camera_entity)
4970 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
4971 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
4974 // note: all of the view is used for displaying... so
4975 // there is no use in scissoring
4977 // reverse the cullface settings for this render
4978 r_refdef.view.cullface_front = GL_FRONT;
4979 r_refdef.view.cullface_back = GL_BACK;
4980 // also reverse the view matrix
4981 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
4982 R_RenderView_UpdateViewVectors();
4983 if(p->camera_entity && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
4985 r_refdef.view.usecustompvs = true;
4986 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);
4989 // camera needs no clipplane
4990 r_refdef.view.useclipplane = false;
4991 // TODO: is the camera origin always valid? if so we don't need to clear this
4992 r_refdef.view.usevieworiginculling = false;
4994 PlaneClassify(&r_refdef.view.clipplane);
4996 r_fb.water.hideplayer = false;
4998 R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4999 GL_ScissorTest(false);
5000 R_ClearScreen(r_refdef.fogenabled);
5001 GL_ScissorTest(true);
5003 R_AnimCache_CacheVisibleEntities();
5004 R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
5006 r_fb.water.hideplayer = false;
5011 r_fb.water.renderingscene = false;
5012 r_refdef.view = originalview;
5013 R_ResetViewRendering3D(fbo, depthtexture, colortexture, viewx, viewy, viewwidth, viewheight);
5015 R_AnimCache_CacheVisibleEntities();
5018 r_refdef.view = originalview;
5019 r_fb.water.renderingscene = false;
5020 Cvar_SetValueQuick(&r_water, 0);
5021 Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed! Turned off r_water.\n");
5023 // lowquality hack, restore cvars
5024 if (qualityreduction > 0)
5026 if (qualityreduction >= 1)
5028 Cvar_SetValueQuick(&r_shadows, old_r_shadows);
5029 Cvar_SetValueQuick(&r_shadow_realtime_world, old_r_worldrtlight);
5030 Cvar_SetValueQuick(&r_shadow_realtime_dlight, old_r_dlight);
5032 if (qualityreduction >= 2)
5034 Cvar_SetValueQuick(&r_dynamic, old_r_dynamic);
5035 Cvar_SetValueQuick(&r_drawparticles, old_r_particles);
5036 Cvar_SetValueQuick(&r_drawdecals, old_r_decals);
5041 static void R_Bloom_StartFrame(void)
5043 int screentexturewidth, screentextureheight;
5044 textype_t textype = TEXTYPE_COLORBUFFER;
5047 // clear the pointers to rendertargets from last frame as they're stale
5048 r_fb.rt_screen = NULL;
5049 r_fb.rt_bloom = NULL;
5051 switch (vid.renderpath)
5053 case RENDERPATH_GL32:
5054 r_fb.usedepthtextures = r_usedepthtextures.integer != 0;
5055 if (r_viewfbo.integer == 2) textype = TEXTYPE_COLORBUFFER16F;
5056 if (r_viewfbo.integer == 3) textype = TEXTYPE_COLORBUFFER32F;
5058 case RENDERPATH_GLES2:
5059 r_fb.usedepthtextures = false;
5063 if (r_viewscale_fpsscaling.integer)
5065 double actualframetime;
5066 double targetframetime;
5068 actualframetime = r_refdef.lastdrawscreentime;
5069 targetframetime = (1.0 / r_viewscale_fpsscaling_target.value);
5070 adjust = (targetframetime - actualframetime) * r_viewscale_fpsscaling_multiply.value;
5071 adjust = bound(-r_viewscale_fpsscaling_stepmax.value, adjust, r_viewscale_fpsscaling_stepmax.value);
5072 if (r_viewscale_fpsscaling_stepsize.value > 0)
5075 adjust = floor(adjust / r_viewscale_fpsscaling_stepsize.value) * r_viewscale_fpsscaling_stepsize.value;
5077 adjust = ceil(adjust / r_viewscale_fpsscaling_stepsize.value) * r_viewscale_fpsscaling_stepsize.value;
5079 viewscalefpsadjusted += adjust;
5080 viewscalefpsadjusted = bound(r_viewscale_fpsscaling_min.value, viewscalefpsadjusted, 1.0f);
5083 viewscalefpsadjusted = 1.0f;
5085 scale = r_viewscale.value * sqrt(viewscalefpsadjusted);
5087 scale *= sqrt(vid.samples); // supersampling
5088 scale = bound(0.03125f, scale, 4.0f);
5089 screentexturewidth = (int)ceil(r_refdef.view.width * scale);
5090 screentextureheight = (int)ceil(r_refdef.view.height * scale);
5091 screentexturewidth = bound(1, screentexturewidth, (int)vid.maxtexturesize_2d);
5092 screentextureheight = bound(1, screentextureheight, (int)vid.maxtexturesize_2d);
5094 // set bloomwidth and bloomheight to the bloom resolution that will be
5095 // used (often less than the screen resolution for faster rendering)
5096 r_fb.bloomheight = bound(1, r_bloom_resolution.value * 0.75f, screentextureheight);
5097 r_fb.bloomwidth = r_fb.bloomheight * screentexturewidth / screentextureheight;
5098 r_fb.bloomwidth = bound(1, r_fb.bloomwidth, screentexturewidth);
5099 r_fb.bloomwidth = bound(1, r_fb.bloomwidth, (int)vid.maxtexturesize_2d);
5100 r_fb.bloomheight = bound(1, r_fb.bloomheight, (int)vid.maxtexturesize_2d);
5102 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))
5104 Cvar_SetValueQuick(&r_bloom, 0);
5105 Cvar_SetValueQuick(&r_motionblur, 0);
5106 Cvar_SetValueQuick(&r_damageblur, 0);
5108 if (!r_bloom.integer)
5109 r_fb.bloomwidth = r_fb.bloomheight = 0;
5111 // allocate motionblur ghost texture if needed - this is the only persistent texture and is only useful on the main view
5112 if (r_refdef.view.ismain && (r_fb.screentexturewidth != screentexturewidth || r_fb.screentextureheight != screentextureheight || r_fb.textype != textype))
5114 if (r_fb.ghosttexture)
5115 R_FreeTexture(r_fb.ghosttexture);
5116 r_fb.ghosttexture = NULL;
5118 r_fb.screentexturewidth = screentexturewidth;
5119 r_fb.screentextureheight = screentextureheight;
5120 r_fb.textype = textype;
5122 if (r_fb.screentexturewidth && r_fb.screentextureheight)
5124 if (r_motionblur.value > 0 || r_damageblur.value > 0)
5125 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);
5126 r_fb.ghosttexture_valid = false;
5130 r_fb.rt_screen = R_RenderTarget_Get(screentexturewidth, screentextureheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5132 r_refdef.view.clear = true;
5135 static void R_Bloom_MakeTexture(void)
5138 float xoffset, yoffset, r, brighten;
5139 float colorscale = r_bloom_colorscale.value;
5140 r_viewport_t bloomviewport;
5141 r_rendertarget_t *prev, *cur;
5142 textype_t textype = r_fb.rt_screen->colortextype[0];
5144 r_refdef.stats[r_stat_bloom]++;
5146 R_Viewport_InitOrtho(&bloomviewport, &identitymatrix, 0, 0, r_fb.bloomwidth, r_fb.bloomheight, 0, 0, 1, 1, -10, 100, NULL);
5148 // scale down screen texture to the bloom texture size
5150 prev = r_fb.rt_screen;
5151 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5152 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5153 R_SetViewport(&bloomviewport);
5154 GL_CullFace(GL_NONE);
5155 GL_DepthTest(false);
5156 GL_BlendFunc(GL_ONE, GL_ZERO);
5157 GL_Color(colorscale, colorscale, colorscale, 1);
5158 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, prev->texcoord2f);
5159 // TODO: do boxfilter scale-down in shader?
5160 R_SetupShader_Generic(prev->colortexture[0], false, true, true);
5161 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5162 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5163 // we now have a properly scaled bloom image
5165 // multiply bloom image by itself as many times as desired to darken it
5166 // TODO: if people actually use this it could be done more quickly in the previous shader pass
5167 for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
5170 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5171 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5173 r = bound(0, r_bloom_colorexponent.value / x, 1); // always 0.5 to 1
5175 GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 0);
5176 GL_BlendFunc(GL_SRC_COLOR, GL_ZERO); // square it
5177 GL_Color(1,1,1,1); // no fix factor supported here
5178 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, prev->texcoord2f);
5179 R_SetupShader_Generic(prev->colortexture[0], false, true, false);
5180 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5181 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5185 range = r_bloom_blur.integer * r_fb.bloomwidth / 320;
5186 brighten = r_bloom_brighten.value;
5187 brighten = sqrt(brighten);
5189 brighten *= (3 * range) / (2 * range - 1); // compensate for the "dot particle"
5191 for (dir = 0;dir < 2;dir++)
5194 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5195 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5196 // blend on at multiple vertical offsets to achieve a vertical blur
5197 // TODO: do offset blends using GLSL
5198 // TODO instead of changing the texcoords, change the target positions to prevent artifacts at edges
5200 GL_BlendFunc(GL_ONE, GL_ZERO);
5202 R_SetupShader_Generic(prev->colortexture[0], false, true, false);
5204 for (x = -range;x <= range;x++)
5206 if (!dir){xoffset = 0;yoffset = x;}
5207 else {xoffset = x;yoffset = 0;}
5208 xoffset /= (float)prev->texturewidth;
5209 yoffset /= (float)prev->textureheight;
5210 // compute a texcoord array with the specified x and y offset
5211 r_fb.offsettexcoord2f[0] = xoffset+prev->texcoord2f[0];
5212 r_fb.offsettexcoord2f[1] = yoffset+prev->texcoord2f[1];
5213 r_fb.offsettexcoord2f[2] = xoffset+prev->texcoord2f[2];
5214 r_fb.offsettexcoord2f[3] = yoffset+prev->texcoord2f[3];
5215 r_fb.offsettexcoord2f[4] = xoffset+prev->texcoord2f[4];
5216 r_fb.offsettexcoord2f[5] = yoffset+prev->texcoord2f[5];
5217 r_fb.offsettexcoord2f[6] = xoffset+prev->texcoord2f[6];
5218 r_fb.offsettexcoord2f[7] = yoffset+prev->texcoord2f[7];
5219 // this r value looks like a 'dot' particle, fading sharply to
5220 // black at the edges
5221 // (probably not realistic but looks good enough)
5222 //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
5223 //r = brighten/(range*2+1);
5224 r = brighten / (range * 2 + 1);
5226 r *= (1 - x*x/(float)((range+1)*(range+1)));
5230 GL_Color(r, r, r, 1);
5232 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.offsettexcoord2f);
5234 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5235 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5237 GL_BlendFunc(GL_ONE, GL_ONE);
5242 // now we have the bloom image, so keep track of it
5243 r_fb.rt_bloom = cur;
5246 static qbool R_BlendView_IsTrivial(int viewx, int viewy, int viewwidth, int viewheight, int x, int y, int width, int height)
5248 // Shifting requested?
5249 // (It should be possible to work around this otherwise)
5250 if (viewx != x || viewy != y)
5252 // Scaling requested?
5253 if (viewwidth != width || viewheight != height)
5255 // Higher bit depth or explicit FBO requested?
5256 if (r_viewfbo.integer)
5258 // Non-trivial postprocessing shader permutation?
5260 || r_refdef.viewblend[3] > 0
5261 || !vid_gammatables_trivial
5262 || r_glsl_postprocess.integer
5263 || ((!R_Stereo_ColorMasking() && r_glsl_saturation.value != 1)))
5265 // Other reasons for a non-trivial default postprocessing shader?
5266 // (See R_CompileShader_CheckStaticParms but only those relevant for MODE_POSTPROCESS in shader_glsl.h)
5267 // Skip: if (r_glsl_saturation_redcompensate.integer) (already covered by saturation above).
5268 // Skip: if (r_glsl_postprocess.integer) (already covered by r_glsl_postprocess above).
5269 // Skip: if (r_glsl_postprocess_uservec1_enable.integer) (already covered by r_glsl_postprocessing above).
5272 if (r_colorfringe.value)
5277 static void R_MotionBlurView(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5279 R_EntityMatrix(&identitymatrix);
5281 if(r_refdef.view.ismain && !R_Stereo_Active() && (r_motionblur.value > 0 || (r_damageblur.value > 0 && cl.cshifts[CSHIFT_DAMAGE].percent != 0)) && r_fb.ghosttexture)
5283 // declare variables
5284 float blur_factor, blur_mouseaccel, blur_velocity;
5285 static float blur_average;
5286 static vec3_t blur_oldangles; // used to see how quickly the mouse is moving
5288 // set a goal for the factoring
5289 blur_velocity = bound(0, (VectorLength(cl.movement_velocity) - r_motionblur_velocityfactor_minspeed.value)
5290 / max(1, r_motionblur_velocityfactor_maxspeed.value - r_motionblur_velocityfactor_minspeed.value), 1);
5291 blur_mouseaccel = bound(0, ((fabs(VectorLength(cl.viewangles) - VectorLength(blur_oldangles)) * 10) - r_motionblur_mousefactor_minspeed.value)
5292 / max(1, r_motionblur_mousefactor_maxspeed.value - r_motionblur_mousefactor_minspeed.value), 1);
5293 blur_factor = ((blur_velocity * r_motionblur_velocityfactor.value)
5294 + (blur_mouseaccel * r_motionblur_mousefactor.value));
5296 // from the goal, pick an averaged value between goal and last value
5297 cl.motionbluralpha = bound(0, (cl.time - cl.oldtime) / max(0.001, r_motionblur_averaging.value), 1);
5298 blur_average = blur_average * (1 - cl.motionbluralpha) + blur_factor * cl.motionbluralpha;
5300 // enforce minimum amount of blur
5301 blur_factor = blur_average * (1 - r_motionblur_minblur.value) + r_motionblur_minblur.value;
5303 //Con_Printf("motionblur: direct factor: %f, averaged factor: %f, velocity: %f, mouse accel: %f \n", blur_factor, blur_average, blur_velocity, blur_mouseaccel);
5305 // calculate values into a standard alpha
5306 cl.motionbluralpha = 1 - exp(-
5308 (r_motionblur.value * blur_factor / 80)
5310 (r_damageblur.value * (cl.cshifts[CSHIFT_DAMAGE].percent / 1600))
5313 max(0.0001, cl.time - cl.oldtime) // fps independent
5316 // randomization for the blur value to combat persistent ghosting
5317 cl.motionbluralpha *= lhrandom(1 - r_motionblur_randomize.value, 1 + r_motionblur_randomize.value);
5318 cl.motionbluralpha = bound(0, cl.motionbluralpha, r_motionblur_maxblur.value);
5320 // apply the blur on top of the current view
5321 R_ResetViewRendering2D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5322 if (cl.motionbluralpha > 0 && !r_refdef.envmap && r_fb.ghosttexture_valid)
5324 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5325 GL_Color(1, 1, 1, cl.motionbluralpha);
5326 R_CalcTexCoordsForView(0, 0, viewwidth, viewheight, viewwidth, viewheight, r_fb.ghosttexcoord2f);
5327 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.ghosttexcoord2f);
5328 R_SetupShader_Generic(r_fb.ghosttexture, false, true, true);
5329 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5330 r_refdef.stats[r_stat_bloom_drawpixels] += viewwidth * viewheight;
5333 // updates old view angles for next pass
5334 VectorCopy(cl.viewangles, blur_oldangles);
5336 // copy view into the ghost texture
5337 R_Mesh_CopyToTexture(r_fb.ghosttexture, 0, 0, viewx, viewy, viewwidth, viewheight);
5338 r_refdef.stats[r_stat_bloom_copypixels] += viewwidth * viewheight;
5339 r_fb.ghosttexture_valid = true;
5343 static void R_BlendView(rtexture_t *viewcolortexture, int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, int x, int y, int width, int height)
5345 uint64_t permutation;
5346 float uservecs[4][4];
5347 rtexture_t *viewtexture;
5348 rtexture_t *bloomtexture;
5350 R_EntityMatrix(&identitymatrix);
5352 if (r_fb.bloomwidth)
5354 // make the bloom texture
5355 R_Bloom_MakeTexture();
5358 #if _MSC_VER >= 1400
5359 #define sscanf sscanf_s
5361 memset(uservecs, 0, sizeof(uservecs));
5362 if (r_glsl_postprocess_uservec1_enable.integer)
5363 sscanf(r_glsl_postprocess_uservec1.string, "%f %f %f %f", &uservecs[0][0], &uservecs[0][1], &uservecs[0][2], &uservecs[0][3]);
5364 if (r_glsl_postprocess_uservec2_enable.integer)
5365 sscanf(r_glsl_postprocess_uservec2.string, "%f %f %f %f", &uservecs[1][0], &uservecs[1][1], &uservecs[1][2], &uservecs[1][3]);
5366 if (r_glsl_postprocess_uservec3_enable.integer)
5367 sscanf(r_glsl_postprocess_uservec3.string, "%f %f %f %f", &uservecs[2][0], &uservecs[2][1], &uservecs[2][2], &uservecs[2][3]);
5368 if (r_glsl_postprocess_uservec4_enable.integer)
5369 sscanf(r_glsl_postprocess_uservec4.string, "%f %f %f %f", &uservecs[3][0], &uservecs[3][1], &uservecs[3][2], &uservecs[3][3]);
5371 // render to the screen fbo
5372 R_ResetViewRendering2D(fbo, depthtexture, colortexture, x, y, width, height);
5373 GL_Color(1, 1, 1, 1);
5374 GL_BlendFunc(GL_ONE, GL_ZERO);
5376 viewtexture = viewcolortexture;
5377 bloomtexture = r_fb.rt_bloom ? r_fb.rt_bloom->colortexture[0] : NULL;
5379 if (r_rendertarget_debug.integer >= 0)
5381 r_rendertarget_t *rt = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, r_rendertarget_debug.integer);
5382 if (rt && rt->colortexture[0])
5384 viewtexture = rt->colortexture[0];
5385 bloomtexture = NULL;
5389 R_Mesh_PrepareVertices_Mesh_Arrays(4, r_screenvertex3f, NULL, NULL, NULL, NULL, r_fb.rt_screen->texcoord2f, bloomtexture ? r_fb.rt_bloom->texcoord2f : NULL);
5390 switch(vid.renderpath)
5392 case RENDERPATH_GL32:
5393 case RENDERPATH_GLES2:
5395 (r_fb.bloomwidth ? SHADERPERMUTATION_BLOOM : 0)
5396 | (r_refdef.viewblend[3] > 0 ? SHADERPERMUTATION_VIEWTINT : 0)
5397 | (!vid_gammatables_trivial ? SHADERPERMUTATION_GAMMARAMPS : 0)
5398 | (r_glsl_postprocess.integer ? SHADERPERMUTATION_POSTPROCESSING : 0)
5399 | ((!R_Stereo_ColorMasking() && r_glsl_saturation.value != 1) ? SHADERPERMUTATION_SATURATION : 0);
5400 R_SetupShader_SetPermutationGLSL(SHADERMODE_POSTPROCESS, permutation);
5401 if (r_glsl_permutation->tex_Texture_First >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , viewtexture);
5402 if (r_glsl_permutation->tex_Texture_Second >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second , bloomtexture);
5403 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps );
5404 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]);
5405 if (r_glsl_permutation->loc_PixelSize >= 0) qglUniform2f(r_glsl_permutation->loc_PixelSize , 1.0/r_fb.screentexturewidth, 1.0/r_fb.screentextureheight);
5406 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]);
5407 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]);
5408 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]);
5409 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]);
5410 if (r_glsl_permutation->loc_Saturation >= 0) qglUniform1f(r_glsl_permutation->loc_Saturation , r_glsl_saturation.value);
5411 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
5412 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);
5413 if (r_glsl_permutation->loc_ColorFringe >= 0) qglUniform1f(r_glsl_permutation->loc_ColorFringe, r_colorfringe.value );
5416 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5417 r_refdef.stats[r_stat_bloom_drawpixels] += r_refdef.view.width * r_refdef.view.height;
5420 matrix4x4_t r_waterscrollmatrix;
5422 void R_UpdateFog(void)
5425 if (gamemode == GAME_NEHAHRA)
5427 if (gl_fogenable.integer)
5429 r_refdef.oldgl_fogenable = true;
5430 r_refdef.fog_density = gl_fogdensity.value;
5431 r_refdef.fog_red = gl_fogred.value;
5432 r_refdef.fog_green = gl_foggreen.value;
5433 r_refdef.fog_blue = gl_fogblue.value;
5434 r_refdef.fog_alpha = 1;
5435 r_refdef.fog_start = 0;
5436 r_refdef.fog_end = gl_skyclip.value;
5437 r_refdef.fog_height = 1<<30;
5438 r_refdef.fog_fadedepth = 128;
5440 else if (r_refdef.oldgl_fogenable)
5442 r_refdef.oldgl_fogenable = false;
5443 r_refdef.fog_density = 0;
5444 r_refdef.fog_red = 0;
5445 r_refdef.fog_green = 0;
5446 r_refdef.fog_blue = 0;
5447 r_refdef.fog_alpha = 0;
5448 r_refdef.fog_start = 0;
5449 r_refdef.fog_end = 0;
5450 r_refdef.fog_height = 1<<30;
5451 r_refdef.fog_fadedepth = 128;
5456 r_refdef.fog_alpha = bound(0, r_refdef.fog_alpha, 1);
5457 r_refdef.fog_start = max(0, r_refdef.fog_start);
5458 r_refdef.fog_end = max(r_refdef.fog_start + 0.01, r_refdef.fog_end);
5460 if (r_refdef.fog_density && r_drawfog.integer)
5462 r_refdef.fogenabled = true;
5463 // this is the point where the fog reaches 0.9986 alpha, which we
5464 // consider a good enough cutoff point for the texture
5465 // (0.9986 * 256 == 255.6)
5466 if (r_fog_exp2.integer)
5467 r_refdef.fogrange = 32 / (r_refdef.fog_density * r_refdef.fog_density) + r_refdef.fog_start;
5469 r_refdef.fogrange = 2048 / r_refdef.fog_density + r_refdef.fog_start;
5470 r_refdef.fogrange = bound(r_refdef.fog_start, r_refdef.fogrange, r_refdef.fog_end);
5471 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
5472 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
5473 if (strcmp(r_refdef.fogheighttexturename, r_refdef.fog_height_texturename))
5474 R_BuildFogHeightTexture();
5475 // fog color was already set
5476 // update the fog texture
5477 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)
5478 R_BuildFogTexture();
5479 r_refdef.fog_height_texcoordscale = 1.0f / max(0.125f, r_refdef.fog_fadedepth);
5480 r_refdef.fog_height_tablescale = r_refdef.fog_height_tablesize * r_refdef.fog_height_texcoordscale;
5483 r_refdef.fogenabled = false;
5486 if (r_refdef.fog_density)
5488 r_refdef.fogcolor[0] = r_refdef.fog_red;
5489 r_refdef.fogcolor[1] = r_refdef.fog_green;
5490 r_refdef.fogcolor[2] = r_refdef.fog_blue;
5492 Vector4Set(r_refdef.fogplane, 0, 0, 1, -r_refdef.fog_height);
5493 r_refdef.fogplaneviewdist = DotProduct(r_refdef.fogplane, r_refdef.view.origin) + r_refdef.fogplane[3];
5494 r_refdef.fogplaneviewabove = r_refdef.fogplaneviewdist >= 0;
5495 r_refdef.fogheightfade = -0.5f/max(0.125f, r_refdef.fog_fadedepth);
5499 VectorCopy(r_refdef.fogcolor, fogvec);
5500 // color.rgb *= ContrastBoost * SceneBrightness;
5501 VectorScale(fogvec, r_refdef.view.colorscale, fogvec);
5502 r_refdef.fogcolor[0] = bound(0.0f, fogvec[0], 1.0f);
5503 r_refdef.fogcolor[1] = bound(0.0f, fogvec[1], 1.0f);
5504 r_refdef.fogcolor[2] = bound(0.0f, fogvec[2], 1.0f);
5509 void R_UpdateVariables(void)
5513 r_refdef.scene.ambientintensity = r_ambient.value * (1.0f / 64.0f);
5515 r_refdef.farclip = r_farclip_base.value;
5516 if (r_refdef.scene.worldmodel)
5517 r_refdef.farclip += r_refdef.scene.worldmodel->radius * r_farclip_world.value * 2;
5518 r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
5520 if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
5521 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
5522 r_refdef.polygonfactor = 0;
5523 r_refdef.polygonoffset = 0;
5525 r_refdef.scene.rtworld = r_shadow_realtime_world.integer != 0;
5526 r_refdef.scene.rtworldshadows = r_shadow_realtime_world_shadows.integer && vid.stencil;
5527 r_refdef.scene.rtdlight = r_shadow_realtime_dlight.integer != 0 && !gl_flashblend.integer && r_dynamic.integer;
5528 r_refdef.scene.rtdlightshadows = r_refdef.scene.rtdlight && r_shadow_realtime_dlight_shadows.integer && vid.stencil;
5529 r_refdef.scene.lightmapintensity = r_refdef.scene.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
5530 if (r_refdef.scene.worldmodel)
5532 r_refdef.scene.lightmapintensity *= r_refdef.scene.worldmodel->lightmapscale;
5534 // Apply the default lightstyle to the lightmap even on q3bsp
5535 if (cl.worldmodel && cl.worldmodel->type == mod_brushq3) {
5536 r_refdef.scene.lightmapintensity *= r_refdef.scene.rtlightstylevalue[0];
5539 if (r_showsurfaces.integer)
5541 r_refdef.scene.rtworld = false;
5542 r_refdef.scene.rtworldshadows = false;
5543 r_refdef.scene.rtdlight = false;
5544 r_refdef.scene.rtdlightshadows = false;
5545 r_refdef.scene.lightmapintensity = 0;
5548 r_gpuskeletal = false;
5549 switch(vid.renderpath)
5551 case RENDERPATH_GL32:
5552 r_gpuskeletal = r_glsl_skeletal.integer && !r_showsurfaces.integer;
5553 case RENDERPATH_GLES2:
5554 if(!vid_gammatables_trivial)
5556 if(!r_texture_gammaramps || vid_gammatables_serial != r_texture_gammaramps_serial)
5558 // build GLSL gamma texture
5559 #define RAMPWIDTH 256
5560 unsigned short ramp[RAMPWIDTH * 3];
5561 unsigned char rampbgr[RAMPWIDTH][4];
5564 r_texture_gammaramps_serial = vid_gammatables_serial;
5566 VID_BuildGammaTables(&ramp[0], RAMPWIDTH);
5567 for(i = 0; i < RAMPWIDTH; ++i)
5569 rampbgr[i][0] = (unsigned char) (ramp[i + 2 * RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
5570 rampbgr[i][1] = (unsigned char) (ramp[i + RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
5571 rampbgr[i][2] = (unsigned char) (ramp[i] * 255.0 / 65535.0 + 0.5);
5574 if (r_texture_gammaramps)
5576 R_UpdateTexture(r_texture_gammaramps, &rampbgr[0][0], 0, 0, 0, RAMPWIDTH, 1, 1, 0);
5580 r_texture_gammaramps = R_LoadTexture2D(r_main_texturepool, "gammaramps", RAMPWIDTH, 1, &rampbgr[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
5586 // remove GLSL gamma texture
5592 static r_refdef_scene_type_t r_currentscenetype = RST_CLIENT;
5593 static r_refdef_scene_t r_scenes_store[ RST_COUNT ];
5599 void R_SelectScene( r_refdef_scene_type_t scenetype ) {
5600 if( scenetype != r_currentscenetype ) {
5601 // store the old scenetype
5602 r_scenes_store[ r_currentscenetype ] = r_refdef.scene;
5603 r_currentscenetype = scenetype;
5604 // move in the new scene
5605 r_refdef.scene = r_scenes_store[ r_currentscenetype ];
5614 r_refdef_scene_t * R_GetScenePointer( r_refdef_scene_type_t scenetype )
5616 // of course, we could also add a qbool that provides a lock state and a ReleaseScenePointer function..
5617 if( scenetype == r_currentscenetype ) {
5618 return &r_refdef.scene;
5620 return &r_scenes_store[ scenetype ];
5624 static int R_SortEntities_Compare(const void *ap, const void *bp)
5626 const entity_render_t *a = *(const entity_render_t **)ap;
5627 const entity_render_t *b = *(const entity_render_t **)bp;
5630 if(a->model < b->model)
5632 if(a->model > b->model)
5636 // TODO possibly calculate the REAL skinnum here first using
5638 if(a->skinnum < b->skinnum)
5640 if(a->skinnum > b->skinnum)
5643 // everything we compared is equal
5646 static void R_SortEntities(void)
5648 // below or equal 2 ents, sorting never gains anything
5649 if(r_refdef.scene.numentities <= 2)
5652 qsort(r_refdef.scene.entities, r_refdef.scene.numentities, sizeof(*r_refdef.scene.entities), R_SortEntities_Compare);
5660 extern cvar_t r_shadow_bouncegrid;
5661 extern cvar_t v_isometric;
5662 extern void V_MakeViewIsometric(void);
5663 void R_RenderView(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, int x, int y, int width, int height)
5665 matrix4x4_t originalmatrix = r_refdef.view.matrix, offsetmatrix;
5667 rtexture_t *viewdepthtexture = NULL;
5668 rtexture_t *viewcolortexture = NULL;
5669 int viewx = r_refdef.view.x, viewy = r_refdef.view.y, viewwidth = r_refdef.view.width, viewheight = r_refdef.view.height;
5672 // finish any 2D rendering that was queued
5675 if (r_timereport_active)
5676 R_TimeReport("start");
5677 r_textureframe++; // used only by R_GetCurrentTexture
5678 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
5680 if(R_CompileShader_CheckStaticParms())
5681 R_GLSL_Restart_f(cmd_local);
5683 if (!r_drawentities.integer)
5684 r_refdef.scene.numentities = 0;
5685 else if (r_sortentities.integer)
5688 R_AnimCache_ClearCache();
5690 /* adjust for stereo display */
5691 if(R_Stereo_Active())
5693 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);
5694 Matrix4x4_Concat(&r_refdef.view.matrix, &originalmatrix, &offsetmatrix);
5697 if (r_refdef.view.isoverlay)
5699 // TODO: FIXME: move this into its own backend function maybe? [2/5/2008 Andreas]
5700 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
5701 GL_Clear(GL_DEPTH_BUFFER_BIT, NULL, 1.0f, 0);
5702 R_TimeReport("depthclear");
5704 r_refdef.view.showdebug = false;
5706 r_fb.water.enabled = false;
5707 r_fb.water.numwaterplanes = 0;
5709 R_RenderScene(0, NULL, NULL, r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
5711 r_refdef.view.matrix = originalmatrix;
5717 if (!r_refdef.scene.entities || r_refdef.view.width * r_refdef.view.height == 0 || !r_renderview.integer || cl_videoplaying/* || !r_refdef.scene.worldmodel*/)
5719 r_refdef.view.matrix = originalmatrix;
5723 r_refdef.view.usevieworiginculling = !r_trippy.value && r_refdef.view.useperspective;
5724 if (v_isometric.integer && r_refdef.view.ismain)
5725 V_MakeViewIsometric();
5727 r_refdef.view.colorscale = r_hdr_scenebrightness.value * r_hdr_irisadaptation_value.value;
5729 if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D)
5730 // in sRGB fallback, behave similar to true sRGB: convert this
5731 // value from linear to sRGB
5732 r_refdef.view.colorscale = Image_sRGBFloatFromLinearFloat(r_refdef.view.colorscale);
5734 R_RenderView_UpdateViewVectors();
5736 R_Shadow_UpdateWorldLightSelection();
5738 // this will set up r_fb.rt_screen
5739 R_Bloom_StartFrame();
5741 // apply bloom brightness offset
5743 r_refdef.view.colorscale *= r_bloom_scenebrightness.value;
5745 skipblend = R_BlendView_IsTrivial(0, 0, r_fb.rt_screen->texturewidth, r_fb.rt_screen->textureheight, x, y, width, height);
5748 // Render to the screen right away.
5750 viewdepthtexture = depthtexture;
5751 viewcolortexture = colortexture;
5755 viewheight = height;
5757 else if (r_fb.rt_screen)
5759 // R_Bloom_StartFrame probably set up an fbo for us to render into, it will be rendered to the window later in R_BlendView
5760 viewfbo = r_fb.rt_screen->fbo;
5761 viewdepthtexture = r_fb.rt_screen->depthtexture;
5762 viewcolortexture = r_fb.rt_screen->colortexture[0];
5765 viewwidth = r_fb.rt_screen->texturewidth;
5766 viewheight = r_fb.rt_screen->textureheight;
5769 R_Water_StartFrame(viewwidth, viewheight);
5772 if (r_timereport_active)
5773 R_TimeReport("viewsetup");
5775 R_ResetViewRendering3D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5777 // clear the whole fbo every frame - otherwise the driver will consider
5778 // it to be an inter-frame texture and stall in multi-gpu configurations
5780 GL_ScissorTest(false);
5781 R_ClearScreen(r_refdef.fogenabled);
5782 if (r_timereport_active)
5783 R_TimeReport("viewclear");
5785 r_refdef.view.clear = true;
5787 r_refdef.view.showdebug = true;
5790 if (r_timereport_active)
5791 R_TimeReport("visibility");
5793 R_AnimCache_CacheVisibleEntities();
5794 if (r_timereport_active)
5795 R_TimeReport("animcache");
5797 R_Shadow_UpdateBounceGridTexture();
5798 // R_Shadow_UpdateBounceGridTexture called R_TimeReport a few times internally, so we don't need to do that here.
5800 r_fb.water.numwaterplanes = 0;
5801 if (r_fb.water.enabled)
5802 R_RenderWaterPlanes(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5804 // for the actual view render we use scissoring a fair amount, so scissor
5805 // test needs to be on
5807 GL_ScissorTest(true);
5808 GL_Scissor(viewx, viewy, viewwidth, viewheight);
5809 R_RenderScene(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5810 r_fb.water.numwaterplanes = 0;
5812 // postprocess uses textures that are not aligned with the viewport we're rendering, so no scissoring
5813 GL_ScissorTest(false);
5815 R_MotionBlurView(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5817 R_BlendView(viewcolortexture, fbo, depthtexture, colortexture, x, y, width, height);
5818 if (r_timereport_active)
5819 R_TimeReport("blendview");
5821 r_refdef.view.matrix = originalmatrix;
5825 // go back to 2d rendering
5829 void R_RenderWaterPlanes(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5831 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawAddWaterPlanes)
5833 r_refdef.scene.worldmodel->DrawAddWaterPlanes(r_refdef.scene.worldentity);
5834 if (r_timereport_active)
5835 R_TimeReport("waterworld");
5838 // don't let sound skip if going slow
5839 if (r_refdef.scene.extraupdate)
5842 R_DrawModelsAddWaterPlanes();
5843 if (r_timereport_active)
5844 R_TimeReport("watermodels");
5846 if (r_fb.water.numwaterplanes)
5848 R_Water_ProcessPlanes(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5849 if (r_timereport_active)
5850 R_TimeReport("waterscenes");
5854 extern cvar_t cl_locs_show;
5855 static void R_DrawLocs(void);
5856 static void R_DrawEntityBBoxes(prvm_prog_t *prog);
5857 static void R_DrawModelDecals(void);
5858 extern qbool r_shadow_usingdeferredprepass;
5859 extern int r_shadow_shadowmapatlas_modelshadows_size;
5860 void R_RenderScene(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5862 qbool shadowmapping = false;
5864 if (r_timereport_active)
5865 R_TimeReport("beginscene");
5867 r_refdef.stats[r_stat_renders]++;
5871 // don't let sound skip if going slow
5872 if (r_refdef.scene.extraupdate)
5875 R_MeshQueue_BeginScene();
5879 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);
5881 if (r_timereport_active)
5882 R_TimeReport("skystartframe");
5884 if (cl.csqc_vidvars.drawworld)
5886 // don't let sound skip if going slow
5887 if (r_refdef.scene.extraupdate)
5890 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawSky)
5892 r_refdef.scene.worldmodel->DrawSky(r_refdef.scene.worldentity);
5893 if (r_timereport_active)
5894 R_TimeReport("worldsky");
5897 if (R_DrawBrushModelsSky() && r_timereport_active)
5898 R_TimeReport("bmodelsky");
5900 if (skyrendermasked && skyrenderlater)
5902 // we have to force off the water clipping plane while rendering sky
5903 R_SetupView(false, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5905 R_SetupView(true, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5906 if (r_timereport_active)
5907 R_TimeReport("sky");
5911 // save the framebuffer info for R_Shadow_RenderMode_Reset during this view render
5912 r_shadow_viewfbo = viewfbo;
5913 r_shadow_viewdepthtexture = viewdepthtexture;
5914 r_shadow_viewcolortexture = viewcolortexture;
5915 r_shadow_viewx = viewx;
5916 r_shadow_viewy = viewy;
5917 r_shadow_viewwidth = viewwidth;
5918 r_shadow_viewheight = viewheight;
5920 R_Shadow_PrepareModelShadows();
5921 R_Shadow_PrepareLights();
5922 if (r_timereport_active)
5923 R_TimeReport("preparelights");
5925 // render all the shadowmaps that will be used for this view
5926 shadowmapping = R_Shadow_ShadowMappingEnabled();
5927 if (shadowmapping || r_shadow_shadowmapatlas_modelshadows_size)
5929 R_Shadow_DrawShadowMaps();
5930 if (r_timereport_active)
5931 R_TimeReport("shadowmaps");
5934 // render prepass deferred lighting if r_shadow_deferred is on, this produces light buffers that will be sampled in forward pass
5935 if (r_shadow_usingdeferredprepass)
5936 R_Shadow_DrawPrepass();
5938 // now we begin the forward pass of the view render
5939 if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawDepth)
5941 r_refdef.scene.worldmodel->DrawDepth(r_refdef.scene.worldentity);
5942 if (r_timereport_active)
5943 R_TimeReport("worlddepth");
5945 if (r_depthfirst.integer >= 2)
5947 R_DrawModelsDepth();
5948 if (r_timereport_active)
5949 R_TimeReport("modeldepth");
5952 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->Draw)
5954 r_refdef.scene.worldmodel->Draw(r_refdef.scene.worldentity);
5955 if (r_timereport_active)
5956 R_TimeReport("world");
5959 // don't let sound skip if going slow
5960 if (r_refdef.scene.extraupdate)
5964 if (r_timereport_active)
5965 R_TimeReport("models");
5967 // don't let sound skip if going slow
5968 if (r_refdef.scene.extraupdate)
5971 if (!r_shadow_usingdeferredprepass)
5973 R_Shadow_DrawLights();
5974 if (r_timereport_active)
5975 R_TimeReport("rtlights");
5978 // don't let sound skip if going slow
5979 if (r_refdef.scene.extraupdate)
5982 if (cl.csqc_vidvars.drawworld)
5984 R_DrawModelDecals();
5985 if (r_timereport_active)
5986 R_TimeReport("modeldecals");
5989 if (r_timereport_active)
5990 R_TimeReport("particles");
5993 if (r_timereport_active)
5994 R_TimeReport("explosions");
5997 if (r_refdef.view.showdebug)
5999 if (cl_locs_show.integer)
6002 if (r_timereport_active)
6003 R_TimeReport("showlocs");
6006 if (r_drawportals.integer)
6009 if (r_timereport_active)
6010 R_TimeReport("portals");
6013 if (r_showbboxes_client.value > 0)
6015 R_DrawEntityBBoxes(CLVM_prog);
6016 if (r_timereport_active)
6017 R_TimeReport("clbboxes");
6019 if (r_showbboxes.value > 0)
6021 R_DrawEntityBBoxes(SVVM_prog);
6022 if (r_timereport_active)
6023 R_TimeReport("svbboxes");
6027 if (r_transparent.integer)
6029 R_MeshQueue_RenderTransparent();
6030 if (r_timereport_active)
6031 R_TimeReport("drawtrans");
6034 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))
6036 r_refdef.scene.worldmodel->DrawDebug(r_refdef.scene.worldentity);
6037 if (r_timereport_active)
6038 R_TimeReport("worlddebug");
6039 R_DrawModelsDebug();
6040 if (r_timereport_active)
6041 R_TimeReport("modeldebug");
6044 if (cl.csqc_vidvars.drawworld)
6046 R_Shadow_DrawCoronas();
6047 if (r_timereport_active)
6048 R_TimeReport("coronas");
6051 // don't let sound skip if going slow
6052 if (r_refdef.scene.extraupdate)
6056 static const unsigned short bboxelements[36] =
6066 #define BBOXEDGES 13
6067 static const float bboxedges[BBOXEDGES][6] =
6070 { 0, 0, 0, 1, 1, 1 },
6072 { 0, 0, 0, 0, 1, 0 },
6073 { 0, 0, 0, 1, 0, 0 },
6074 { 0, 1, 0, 1, 1, 0 },
6075 { 1, 0, 0, 1, 1, 0 },
6077 { 0, 0, 1, 0, 1, 1 },
6078 { 0, 0, 1, 1, 0, 1 },
6079 { 0, 1, 1, 1, 1, 1 },
6080 { 1, 0, 1, 1, 1, 1 },
6082 { 0, 0, 0, 0, 0, 1 },
6083 { 1, 0, 0, 1, 0, 1 },
6084 { 0, 1, 0, 0, 1, 1 },
6085 { 1, 1, 0, 1, 1, 1 },
6088 static void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
6090 int numvertices = BBOXEDGES * 8;
6091 float vertex3f[BBOXEDGES * 8 * 3], color4f[BBOXEDGES * 8 * 4];
6092 int numtriangles = BBOXEDGES * 12;
6093 unsigned short elements[BBOXEDGES * 36];
6095 float *v, *c, f1, f2, edgemins[3], edgemaxs[3];
6097 RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false);
6099 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6100 GL_DepthMask(false);
6101 GL_DepthRange(0, 1);
6102 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6104 for (edge = 0; edge < BBOXEDGES; edge++)
6106 for (i = 0; i < 3; i++)
6108 edgemins[i] = mins[i] + (maxs[i] - mins[i]) * bboxedges[edge][i] - 0.25f;
6109 edgemaxs[i] = mins[i] + (maxs[i] - mins[i]) * bboxedges[edge][3 + i] + 0.25f;
6111 vertex3f[edge * 24 + 0] = edgemins[0]; vertex3f[edge * 24 + 1] = edgemins[1]; vertex3f[edge * 24 + 2] = edgemins[2];
6112 vertex3f[edge * 24 + 3] = edgemaxs[0]; vertex3f[edge * 24 + 4] = edgemins[1]; vertex3f[edge * 24 + 5] = edgemins[2];
6113 vertex3f[edge * 24 + 6] = edgemins[0]; vertex3f[edge * 24 + 7] = edgemaxs[1]; vertex3f[edge * 24 + 8] = edgemins[2];
6114 vertex3f[edge * 24 + 9] = edgemaxs[0]; vertex3f[edge * 24 + 10] = edgemaxs[1]; vertex3f[edge * 24 + 11] = edgemins[2];
6115 vertex3f[edge * 24 + 12] = edgemins[0]; vertex3f[edge * 24 + 13] = edgemins[1]; vertex3f[edge * 24 + 14] = edgemaxs[2];
6116 vertex3f[edge * 24 + 15] = edgemaxs[0]; vertex3f[edge * 24 + 16] = edgemins[1]; vertex3f[edge * 24 + 17] = edgemaxs[2];
6117 vertex3f[edge * 24 + 18] = edgemins[0]; vertex3f[edge * 24 + 19] = edgemaxs[1]; vertex3f[edge * 24 + 20] = edgemaxs[2];
6118 vertex3f[edge * 24 + 21] = edgemaxs[0]; vertex3f[edge * 24 + 22] = edgemaxs[1]; vertex3f[edge * 24 + 23] = edgemaxs[2];
6119 for (i = 0; i < 36; i++)
6120 elements[edge * 36 + i] = edge * 8 + bboxelements[i];
6122 R_FillColors(color4f, numvertices, cr, cg, cb, ca);
6123 if (r_refdef.fogenabled)
6125 for (i = 0, v = vertex3f, c = color4f; i < numvertices; i++, v += 3, c += 4)
6127 f1 = RSurf_FogVertex(v);
6129 c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
6130 c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
6131 c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
6134 R_Mesh_PrepareVertices_Generic_Arrays(numvertices, vertex3f, color4f, NULL);
6135 R_Mesh_ResetTextureState();
6136 R_SetupShader_Generic_NoTexture(false, false);
6137 R_Mesh_Draw(0, numvertices, 0, numtriangles, NULL, NULL, 0, elements, NULL, 0);
6140 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6142 // hacky overloading of the parameters
6143 prvm_prog_t *prog = (prvm_prog_t *)rtlight;
6146 prvm_edict_t *edict;
6148 GL_CullFace(GL_NONE);
6149 R_SetupShader_Generic_NoTexture(false, false);
6151 for (i = 0;i < numsurfaces;i++)
6153 edict = PRVM_EDICT_NUM(surfacelist[i]);
6154 switch ((int)PRVM_serveredictfloat(edict, solid))
6156 case SOLID_NOT: Vector4Set(color, 1, 1, 1, 0.05);break;
6157 case SOLID_TRIGGER: Vector4Set(color, 1, 0, 1, 0.10);break;
6158 case SOLID_BBOX: Vector4Set(color, 0, 1, 0, 0.10);break;
6159 case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
6160 case SOLID_BSP: Vector4Set(color, 0, 0, 1, 0.05);break;
6161 case SOLID_CORPSE: Vector4Set(color, 1, 0.5, 0, 0.05);break;
6162 default: Vector4Set(color, 0, 0, 0, 0.50);break;
6164 if (prog == CLVM_prog)
6165 color[3] *= r_showbboxes_client.value;
6167 color[3] *= r_showbboxes.value;
6168 color[3] = bound(0, color[3], 1);
6169 GL_DepthTest(!r_showdisabledepthtest.integer);
6170 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
6174 static void R_DrawEntityBBoxes(prvm_prog_t *prog)
6177 prvm_edict_t *edict;
6183 for (i = 0; i < prog->num_edicts; i++)
6185 edict = PRVM_EDICT_NUM(i);
6188 // exclude the following for now, as they don't live in world coordinate space and can't be solid:
6189 if (PRVM_gameedictedict(edict, tag_entity) != 0)
6191 if (prog == SVVM_prog && PRVM_serveredictedict(edict, viewmodelforclient) != 0)
6193 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
6194 R_MeshQueue_AddTransparent(TRANSPARENTSORT_DISTANCE, center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)prog);
6198 static const int nomodelelement3i[24] =
6210 static const unsigned short nomodelelement3s[24] =
6222 static const float nomodelvertex3f[6*3] =
6232 static const float nomodelcolor4f[6*4] =
6234 0.0f, 0.0f, 0.5f, 1.0f,
6235 0.0f, 0.0f, 0.5f, 1.0f,
6236 0.0f, 0.5f, 0.0f, 1.0f,
6237 0.0f, 0.5f, 0.0f, 1.0f,
6238 0.5f, 0.0f, 0.0f, 1.0f,
6239 0.5f, 0.0f, 0.0f, 1.0f
6242 static void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6248 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);
6250 // this is only called once per entity so numsurfaces is always 1, and
6251 // surfacelist is always {0}, so this code does not handle batches
6253 if (rsurface.ent_flags & RENDER_ADDITIVE)
6255 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
6256 GL_DepthMask(false);
6258 else if (ent->alpha < 1)
6260 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6261 GL_DepthMask(false);
6265 GL_BlendFunc(GL_ONE, GL_ZERO);
6268 GL_DepthRange(0, (rsurface.ent_flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
6269 GL_PolygonOffset(rsurface.basepolygonfactor, rsurface.basepolygonoffset);
6270 GL_DepthTest(!(rsurface.ent_flags & RENDER_NODEPTHTEST));
6271 GL_CullFace((rsurface.ent_flags & RENDER_DOUBLESIDED) ? GL_NONE : r_refdef.view.cullface_back);
6272 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
6273 for (i = 0, c = color4f;i < 6;i++, c += 4)
6275 c[0] *= ent->render_fullbright[0] * r_refdef.view.colorscale;
6276 c[1] *= ent->render_fullbright[1] * r_refdef.view.colorscale;
6277 c[2] *= ent->render_fullbright[2] * r_refdef.view.colorscale;
6280 if (r_refdef.fogenabled)
6282 for (i = 0, c = color4f;i < 6;i++, c += 4)
6284 f1 = RSurf_FogVertex(nomodelvertex3f + 3*i);
6286 c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
6287 c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
6288 c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
6291 // R_Mesh_ResetTextureState();
6292 R_SetupShader_Generic_NoTexture(false, false);
6293 R_Mesh_PrepareVertices_Generic_Arrays(6, nomodelvertex3f, color4f, NULL);
6294 R_Mesh_Draw(0, 6, 0, 8, nomodelelement3i, NULL, 0, nomodelelement3s, NULL, 0);
6297 void R_DrawNoModel(entity_render_t *ent)
6300 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
6301 if ((ent->flags & RENDER_ADDITIVE) || (ent->alpha < 1))
6302 R_MeshQueue_AddTransparent((ent->flags & RENDER_NODEPTHTEST) ? TRANSPARENTSORT_HUD : TRANSPARENTSORT_DISTANCE, org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
6304 R_DrawNoModel_TransparentCallback(ent, rsurface.rtlight, 0, NULL);
6307 void R_CalcBeam_Vertex3f (float *vert, const float *org1, const float *org2, float width)
6309 vec3_t right1, right2, diff, normal;
6311 VectorSubtract (org2, org1, normal);
6313 // calculate 'right' vector for start
6314 VectorSubtract (r_refdef.view.origin, org1, diff);
6315 CrossProduct (normal, diff, right1);
6316 VectorNormalize (right1);
6318 // calculate 'right' vector for end
6319 VectorSubtract (r_refdef.view.origin, org2, diff);
6320 CrossProduct (normal, diff, right2);
6321 VectorNormalize (right2);
6323 vert[ 0] = org1[0] + width * right1[0];
6324 vert[ 1] = org1[1] + width * right1[1];
6325 vert[ 2] = org1[2] + width * right1[2];
6326 vert[ 3] = org1[0] - width * right1[0];
6327 vert[ 4] = org1[1] - width * right1[1];
6328 vert[ 5] = org1[2] - width * right1[2];
6329 vert[ 6] = org2[0] - width * right2[0];
6330 vert[ 7] = org2[1] - width * right2[1];
6331 vert[ 8] = org2[2] - width * right2[2];
6332 vert[ 9] = org2[0] + width * right2[0];
6333 vert[10] = org2[1] + width * right2[1];
6334 vert[11] = org2[2] + width * right2[2];
6337 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)
6339 vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
6340 vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
6341 vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
6342 vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
6343 vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
6344 vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
6345 vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
6346 vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
6347 vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
6348 vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
6349 vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
6350 vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
6353 static int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
6358 VectorSet(v, x, y, z);
6359 for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
6360 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
6362 if (i == mesh->numvertices)
6364 if (mesh->numvertices < mesh->maxvertices)
6366 VectorCopy(v, vertex3f);
6367 mesh->numvertices++;
6369 return mesh->numvertices;
6375 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
6379 element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
6380 element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
6381 e = mesh->element3i + mesh->numtriangles * 3;
6382 for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
6384 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
6385 if (mesh->numtriangles < mesh->maxtriangles)
6390 mesh->numtriangles++;
6392 element[1] = element[2];
6396 static void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
6400 element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
6401 element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
6402 e = mesh->element3i + mesh->numtriangles * 3;
6403 for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
6405 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
6406 if (mesh->numtriangles < mesh->maxtriangles)
6411 mesh->numtriangles++;
6413 element[1] = element[2];
6417 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
6418 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
6420 int planenum, planenum2;
6423 mplane_t *plane, *plane2;
6425 double temppoints[2][256*3];
6426 // figure out how large a bounding box we need to properly compute this brush
6428 for (w = 0;w < numplanes;w++)
6429 maxdist = max(maxdist, fabs(planes[w].dist));
6430 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
6431 maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
6432 for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
6436 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
6437 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
6439 if (planenum2 == planenum)
6441 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);
6444 if (tempnumpoints < 3)
6446 // generate elements forming a triangle fan for this polygon
6447 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
6451 static qbool R_TestQ3WaveFunc(q3wavefunc_t func, const float *parms)
6453 if(parms[0] == 0 && parms[1] == 0)
6455 if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
6456 if(rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)] == 0)
6461 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
6464 index = parms[2] + rsurface.shadertime * parms[3];
6465 index -= floor(index);
6466 switch (func & ((1 << Q3WAVEFUNC_USER_SHIFT) - 1))
6469 case Q3WAVEFUNC_NONE:
6470 case Q3WAVEFUNC_NOISE:
6471 case Q3WAVEFUNC_COUNT:
6474 case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
6475 case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
6476 case Q3WAVEFUNC_SAWTOOTH: f = index;break;
6477 case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
6478 case Q3WAVEFUNC_TRIANGLE:
6480 f = index - floor(index);
6493 f = parms[0] + parms[1] * f;
6494 if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
6495 f *= rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)];
6499 static void R_tcMod_ApplyToMatrix(matrix4x4_t *texmatrix, q3shaderinfo_layer_tcmod_t *tcmod, int currentmaterialflags)
6506 matrix4x4_t matrix, temp;
6507 // if shadertime exceeds about 9 hours (32768 seconds), just wrap it,
6508 // it's better to have one huge fixup every 9 hours than gradual
6509 // degradation over time which looks consistently bad after many hours.
6511 // tcmod scroll in particular suffers from this degradation which can't be
6512 // effectively worked around even with floor() tricks because we don't
6513 // know if tcmod scroll is the last tcmod being applied, and for clampmap
6514 // a workaround involving floor() would be incorrect anyway...
6515 shadertime = rsurface.shadertime;
6516 if (shadertime >= 32768.0f)
6517 shadertime -= floor(rsurface.shadertime * (1.0f / 32768.0f)) * 32768.0f;
6518 switch(tcmod->tcmod)
6522 if (currentmaterialflags & MATERIALFLAG_WATERSCROLL)
6523 matrix = r_waterscrollmatrix;
6525 matrix = identitymatrix;
6527 case Q3TCMOD_ENTITYTRANSLATE:
6528 // this is used in Q3 to allow the gamecode to control texcoord
6529 // scrolling on the entity, which is not supported in darkplaces yet.
6530 Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
6532 case Q3TCMOD_ROTATE:
6533 Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
6534 Matrix4x4_ConcatRotate(&matrix, tcmod->parms[0] * rsurface.shadertime, 0, 0, 1);
6535 Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
6538 Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
6540 case Q3TCMOD_SCROLL:
6541 // this particular tcmod is a "bug for bug" compatible one with regards to
6542 // Quake3, the wrapping is unnecessary with our shadetime fix but quake3
6543 // specifically did the wrapping and so we must mimic that...
6544 offsetd[0] = tcmod->parms[0] * rsurface.shadertime;
6545 offsetd[1] = tcmod->parms[1] * rsurface.shadertime;
6546 Matrix4x4_CreateTranslate(&matrix, offsetd[0] - floor(offsetd[0]), offsetd[1] - floor(offsetd[1]), 0);
6548 case Q3TCMOD_PAGE: // poor man's animmap (to store animations into a single file, useful for HTTP downloaded textures)
6549 w = (int) tcmod->parms[0];
6550 h = (int) tcmod->parms[1];
6551 f = rsurface.shadertime / (tcmod->parms[2] * w * h);
6553 idx = (int) floor(f * w * h);
6554 Matrix4x4_CreateTranslate(&matrix, (idx % w) / tcmod->parms[0], (idx / w) / tcmod->parms[1], 0);
6556 case Q3TCMOD_STRETCH:
6557 f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
6558 Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
6560 case Q3TCMOD_TRANSFORM:
6561 VectorSet(tcmat + 0, tcmod->parms[0], tcmod->parms[1], 0);
6562 VectorSet(tcmat + 3, tcmod->parms[2], tcmod->parms[3], 0);
6563 VectorSet(tcmat + 6, 0 , 0 , 1);
6564 VectorSet(tcmat + 9, tcmod->parms[4], tcmod->parms[5], 0);
6565 Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
6567 case Q3TCMOD_TURBULENT:
6568 // this is handled in the RSurf_PrepareVertices function
6569 matrix = identitymatrix;
6573 Matrix4x4_Concat(texmatrix, &matrix, &temp);
6576 static void R_LoadQWSkin(r_qwskincache_t *cache, const char *skinname)
6578 int textureflags = (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_PICMIP;
6579 char name[MAX_QPATH];
6580 skinframe_t *skinframe;
6581 unsigned char pixels[296*194];
6582 strlcpy(cache->name, skinname, sizeof(cache->name));
6583 dpsnprintf(name, sizeof(name), "skins/%s.pcx", cache->name);
6584 if (developer_loading.integer)
6585 Con_Printf("loading %s\n", name);
6586 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
6587 if (!skinframe || !skinframe->base)
6590 fs_offset_t filesize;
6592 f = FS_LoadFile(name, tempmempool, true, &filesize);
6595 if (LoadPCX_QWSkin(f, (int)filesize, pixels, 296, 194))
6596 skinframe = R_SkinFrame_LoadInternalQuake(name, textureflags, true, r_fullbrights.integer, pixels, image_width, image_height);
6600 cache->skinframe = skinframe;
6603 texture_t *R_GetCurrentTexture(texture_t *t)
6606 const entity_render_t *ent = rsurface.entity;
6607 model_t *model = ent->model; // when calling this, ent must not be NULL
6608 q3shaderinfo_layer_tcmod_t *tcmod;
6609 float specularscale = 0.0f;
6611 if (t->update_lastrenderframe == r_textureframe && t->update_lastrenderentity == (void *)ent && !rsurface.forcecurrenttextureupdate)
6612 return t->currentframe;
6613 t->update_lastrenderframe = r_textureframe;
6614 t->update_lastrenderentity = (void *)ent;
6616 if(ent->entitynumber >= MAX_EDICTS && ent->entitynumber < 2 * MAX_EDICTS)
6617 t->camera_entity = ent->entitynumber;
6619 t->camera_entity = 0;
6621 // switch to an alternate material if this is a q1bsp animated material
6623 texture_t *texture = t;
6624 int s = rsurface.ent_skinnum;
6625 if ((unsigned int)s >= (unsigned int)model->numskins)
6627 if (model->skinscenes)
6629 if (model->skinscenes[s].framecount > 1)
6630 s = model->skinscenes[s].firstframe + (unsigned int) (rsurface.shadertime * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
6632 s = model->skinscenes[s].firstframe;
6635 t = t + s * model->num_surfaces;
6638 // use an alternate animation if the entity's frame is not 0,
6639 // and only if the texture has an alternate animation
6640 if (t->animated == 2) // q2bsp
6641 t = t->anim_frames[0][ent->framegroupblend[0].frame % t->anim_total[0]];
6642 else if (rsurface.ent_alttextures && t->anim_total[1])
6643 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[1]) : 0];
6645 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[0]) : 0];
6647 texture->currentframe = t;
6650 // update currentskinframe to be a qw skin or animation frame
6651 if (rsurface.ent_qwskin >= 0)
6653 i = rsurface.ent_qwskin;
6654 if (!r_qwskincache || r_qwskincache_size != cl.maxclients)
6656 r_qwskincache_size = cl.maxclients;
6658 Mem_Free(r_qwskincache);
6659 r_qwskincache = (r_qwskincache_t *)Mem_Alloc(r_main_mempool, sizeof(*r_qwskincache) * r_qwskincache_size);
6661 if (strcmp(r_qwskincache[i].name, cl.scores[i].qw_skin))
6662 R_LoadQWSkin(&r_qwskincache[i], cl.scores[i].qw_skin);
6663 t->currentskinframe = r_qwskincache[i].skinframe;
6664 if (t->materialshaderpass && t->currentskinframe == NULL)
6665 t->currentskinframe = t->materialshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->materialshaderpass->framerate, t->materialshaderpass->numframes)];
6667 else if (t->materialshaderpass && t->materialshaderpass->numframes >= 2)
6668 t->currentskinframe = t->materialshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->materialshaderpass->framerate, t->materialshaderpass->numframes)];
6669 if (t->backgroundshaderpass && t->backgroundshaderpass->numframes >= 2)
6670 t->backgroundcurrentskinframe = t->backgroundshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->backgroundshaderpass->framerate, t->backgroundshaderpass->numframes)];
6672 t->currentmaterialflags = t->basematerialflags;
6673 t->currentalpha = rsurface.entity->alpha * t->basealpha;
6674 if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_water.integer || r_novis.integer || r_trippy.integer))
6675 t->currentalpha *= r_wateralpha.value;
6676 if(t->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay)
6677 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; // we apply wateralpha later
6678 if(!r_fb.water.enabled || r_refdef.view.isoverlay)
6679 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA);
6681 // decide on which type of lighting to use for this surface
6682 if (rsurface.entity->render_modellight_forced)
6683 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
6684 if (rsurface.entity->render_rtlight_disabled)
6685 t->currentmaterialflags |= MATERIALFLAG_NORTLIGHT;
6686 if (rsurface.entity->render_lightgrid)
6687 t->currentmaterialflags |= MATERIALFLAG_LIGHTGRID;
6688 if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND && !(R_BlendFuncFlags(t->customblendfunc[0], t->customblendfunc[1]) & BLENDFUNC_ALLOWS_COLORMOD))
6690 // some CUSTOMBLEND blendfuncs are too weird, we have to ignore colormod and view colorscale
6691 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_NORTLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6692 for (q = 0; q < 3; q++)
6694 t->render_glowmod[q] = rsurface.entity->glowmod[q];
6695 t->render_modellight_lightdir_world[q] = q == 2;
6696 t->render_modellight_lightdir_local[q] = q == 2;
6697 t->render_modellight_ambient[q] = 1;
6698 t->render_modellight_diffuse[q] = 0;
6699 t->render_modellight_specular[q] = 0;
6700 t->render_lightmap_ambient[q] = 0;
6701 t->render_lightmap_diffuse[q] = 0;
6702 t->render_lightmap_specular[q] = 0;
6703 t->render_rtlight_diffuse[q] = 0;
6704 t->render_rtlight_specular[q] = 0;
6707 else if ((t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) || !(rsurface.ent_flags & RENDER_LIGHT))
6709 // fullbright is basically MATERIALFLAG_MODELLIGHT but with ambient locked to 1,1,1 and no shading
6710 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_NORTLIGHT | MATERIALFLAG_MODELLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6711 for (q = 0; q < 3; q++)
6713 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6714 t->render_modellight_ambient[q] = rsurface.entity->render_fullbright[q] * r_refdef.view.colorscale;
6715 t->render_modellight_lightdir_world[q] = q == 2;
6716 t->render_modellight_lightdir_local[q] = q == 2;
6717 t->render_modellight_diffuse[q] = 0;
6718 t->render_modellight_specular[q] = 0;
6719 t->render_lightmap_ambient[q] = 0;
6720 t->render_lightmap_diffuse[q] = 0;
6721 t->render_lightmap_specular[q] = 0;
6722 t->render_rtlight_diffuse[q] = 0;
6723 t->render_rtlight_specular[q] = 0;
6726 else if (t->currentmaterialflags & MATERIALFLAG_LIGHTGRID)
6728 t->currentmaterialflags &= ~MATERIALFLAG_MODELLIGHT;
6729 for (q = 0; q < 3; q++)
6731 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6732 t->render_modellight_lightdir_world[q] = q == 2;
6733 t->render_modellight_lightdir_local[q] = q == 2;
6734 t->render_modellight_ambient[q] = 0;
6735 t->render_modellight_diffuse[q] = 0;
6736 t->render_modellight_specular[q] = 0;
6737 t->render_lightmap_ambient[q] = rsurface.entity->render_lightmap_ambient[q] * r_refdef.view.colorscale;
6738 t->render_lightmap_diffuse[q] = rsurface.entity->render_lightmap_diffuse[q] * 2 * r_refdef.view.colorscale;
6739 t->render_lightmap_specular[q] = rsurface.entity->render_lightmap_specular[q] * 2 * r_refdef.view.colorscale;
6740 t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6741 t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6744 else if ((rsurface.ent_flags & (RENDER_DYNAMICMODELLIGHT | RENDER_CUSTOMIZEDMODELLIGHT)) || rsurface.modeltexcoordlightmap2f == NULL)
6746 // ambient + single direction light (modellight)
6747 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_MODELLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6748 for (q = 0; q < 3; q++)
6750 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6751 t->render_modellight_lightdir_world[q] = rsurface.entity->render_modellight_lightdir_world[q];
6752 t->render_modellight_lightdir_local[q] = rsurface.entity->render_modellight_lightdir_local[q];
6753 t->render_modellight_ambient[q] = rsurface.entity->render_modellight_ambient[q] * r_refdef.view.colorscale;
6754 t->render_modellight_diffuse[q] = rsurface.entity->render_modellight_diffuse[q] * r_refdef.view.colorscale;
6755 t->render_modellight_specular[q] = rsurface.entity->render_modellight_specular[q] * r_refdef.view.colorscale;
6756 t->render_lightmap_ambient[q] = 0;
6757 t->render_lightmap_diffuse[q] = 0;
6758 t->render_lightmap_specular[q] = 0;
6759 t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6760 t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6765 // lightmap - 2x diffuse and specular brightness because bsp files have 0-2 colors as 0-1
6766 for (q = 0; q < 3; q++)
6768 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6769 t->render_modellight_lightdir_world[q] = q == 2;
6770 t->render_modellight_lightdir_local[q] = q == 2;
6771 t->render_modellight_ambient[q] = 0;
6772 t->render_modellight_diffuse[q] = 0;
6773 t->render_modellight_specular[q] = 0;
6774 t->render_lightmap_ambient[q] = rsurface.entity->render_lightmap_ambient[q] * r_refdef.view.colorscale;
6775 t->render_lightmap_diffuse[q] = rsurface.entity->render_lightmap_diffuse[q] * 2 * r_refdef.view.colorscale;
6776 t->render_lightmap_specular[q] = rsurface.entity->render_lightmap_specular[q] * 2 * r_refdef.view.colorscale;
6777 t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6778 t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6782 if (t->currentmaterialflags & MATERIALFLAG_VERTEXCOLOR)
6784 // since MATERIALFLAG_VERTEXCOLOR uses the lightmapcolor4f vertex
6785 // attribute, we punt it to the lightmap path and hope for the best,
6786 // but lighting doesn't work.
6788 // FIXME: this is fine for effects but CSQC polygons should be subject
6790 t->currentmaterialflags &= ~(MATERIALFLAG_MODELLIGHT | MATERIALFLAG_LIGHTGRID);
6791 for (q = 0; q < 3; q++)
6793 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6794 t->render_modellight_lightdir_world[q] = q == 2;
6795 t->render_modellight_lightdir_local[q] = q == 2;
6796 t->render_modellight_ambient[q] = 0;
6797 t->render_modellight_diffuse[q] = 0;
6798 t->render_modellight_specular[q] = 0;
6799 t->render_lightmap_ambient[q] = 0;
6800 t->render_lightmap_diffuse[q] = rsurface.entity->render_fullbright[q] * r_refdef.view.colorscale;
6801 t->render_lightmap_specular[q] = 0;
6802 t->render_rtlight_diffuse[q] = 0;
6803 t->render_rtlight_specular[q] = 0;
6807 for (q = 0; q < 3; q++)
6809 t->render_colormap_pants[q] = rsurface.entity->colormap_pantscolor[q];
6810 t->render_colormap_shirt[q] = rsurface.entity->colormap_shirtcolor[q];
6813 if (rsurface.ent_flags & RENDER_ADDITIVE)
6814 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
6815 else if (t->currentalpha < 1)
6816 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
6817 // LadyHavoc: prevent bugs where code checks add or alpha at higher priority than customblend by clearing these flags
6818 if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
6819 t->currentmaterialflags &= ~(MATERIALFLAG_ADD | MATERIALFLAG_ALPHA);
6820 if (rsurface.ent_flags & RENDER_DOUBLESIDED)
6821 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
6822 if (rsurface.ent_flags & (RENDER_NODEPTHTEST | RENDER_VIEWMODEL))
6823 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
6824 if (t->backgroundshaderpass)
6825 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
6826 if (t->currentmaterialflags & MATERIALFLAG_BLENDED)
6828 if (t->currentmaterialflags & (MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA))
6829 t->currentmaterialflags &= ~MATERIALFLAG_BLENDED;
6832 t->currentmaterialflags &= ~(MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA);
6833 if (vid.allowalphatocoverage && r_transparent_alphatocoverage.integer >= 2 && ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA | MATERIALFLAG_ADD | MATERIALFLAG_CUSTOMBLEND)) == (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)))
6835 // promote alphablend to alphatocoverage (a type of alphatest) if antialiasing is on
6836 t->currentmaterialflags = (t->currentmaterialflags & ~(MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)) | MATERIALFLAG_ALPHATEST;
6838 if ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST)) == MATERIALFLAG_BLENDED && r_transparentdepthmasking.integer && !(t->basematerialflags & MATERIALFLAG_BLENDED))
6839 t->currentmaterialflags |= MATERIALFLAG_TRANSDEPTH;
6841 // there is no tcmod
6842 if (t->currentmaterialflags & MATERIALFLAG_WATERSCROLL)
6844 t->currenttexmatrix = r_waterscrollmatrix;
6845 t->currentbackgroundtexmatrix = r_waterscrollmatrix;
6847 else if (!(t->currentmaterialflags & MATERIALFLAG_CUSTOMSURFACE))
6849 Matrix4x4_CreateIdentity(&t->currenttexmatrix);
6850 Matrix4x4_CreateIdentity(&t->currentbackgroundtexmatrix);
6853 if (t->materialshaderpass)
6854 for (i = 0, tcmod = t->materialshaderpass->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
6855 R_tcMod_ApplyToMatrix(&t->currenttexmatrix, tcmod, t->currentmaterialflags);
6857 t->colormapping = VectorLength2(t->render_colormap_pants) + VectorLength2(t->render_colormap_shirt) >= (1.0f / 1048576.0f);
6858 if (t->currentskinframe->qpixels)
6859 R_SkinFrame_GenerateTexturesFromQPixels(t->currentskinframe, t->colormapping);
6860 t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
6861 if (!t->basetexture)
6862 t->basetexture = r_texture_notexture;
6863 t->pantstexture = t->colormapping ? t->currentskinframe->pants : NULL;
6864 t->shirttexture = t->colormapping ? t->currentskinframe->shirt : NULL;
6865 t->nmaptexture = t->currentskinframe->nmap;
6866 if (!t->nmaptexture)
6867 t->nmaptexture = r_texture_blanknormalmap;
6868 t->glosstexture = r_texture_black;
6869 t->glowtexture = t->currentskinframe->glow;
6870 t->fogtexture = t->currentskinframe->fog;
6871 t->reflectmasktexture = t->currentskinframe->reflect;
6872 if (t->backgroundshaderpass)
6874 for (i = 0, tcmod = t->backgroundshaderpass->tcmods; i < Q3MAXTCMODS && tcmod->tcmod; i++, tcmod++)
6875 R_tcMod_ApplyToMatrix(&t->currentbackgroundtexmatrix, tcmod, t->currentmaterialflags);
6876 t->backgroundbasetexture = (!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base;
6877 t->backgroundnmaptexture = t->backgroundcurrentskinframe->nmap;
6878 t->backgroundglosstexture = r_texture_black;
6879 t->backgroundglowtexture = t->backgroundcurrentskinframe->glow;
6880 if (!t->backgroundnmaptexture)
6881 t->backgroundnmaptexture = r_texture_blanknormalmap;
6882 // make sure that if glow is going to be used, both textures are not NULL
6883 if (!t->backgroundglowtexture && t->glowtexture)
6884 t->backgroundglowtexture = r_texture_black;
6885 if (!t->glowtexture && t->backgroundglowtexture)
6886 t->glowtexture = r_texture_black;
6890 t->backgroundbasetexture = r_texture_white;
6891 t->backgroundnmaptexture = r_texture_blanknormalmap;
6892 t->backgroundglosstexture = r_texture_black;
6893 t->backgroundglowtexture = NULL;
6895 t->specularpower = r_shadow_glossexponent.value;
6896 // TODO: store reference values for these in the texture?
6897 if (r_shadow_gloss.integer > 0)
6899 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
6901 if (r_shadow_glossintensity.value > 0)
6903 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
6904 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
6905 specularscale = r_shadow_glossintensity.value;
6908 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
6910 t->glosstexture = r_texture_white;
6911 t->backgroundglosstexture = r_texture_white;
6912 specularscale = r_shadow_gloss2intensity.value;
6913 t->specularpower = r_shadow_gloss2exponent.value;
6916 specularscale *= t->specularscalemod;
6917 t->specularpower *= t->specularpowermod;
6919 // lightmaps mode looks bad with dlights using actual texturing, so turn
6920 // off the colormap and glossmap, but leave the normalmap on as it still
6921 // accurately represents the shading involved
6922 if (gl_lightmaps.integer && ent != &cl_meshentities[MESH_UI].render)
6924 t->basetexture = r_texture_grey128;
6925 t->pantstexture = r_texture_black;
6926 t->shirttexture = r_texture_black;
6927 if (gl_lightmaps.integer < 2)
6928 t->nmaptexture = r_texture_blanknormalmap;
6929 t->glosstexture = r_texture_black;
6930 t->glowtexture = NULL;
6931 t->fogtexture = NULL;
6932 t->reflectmasktexture = NULL;
6933 t->backgroundbasetexture = NULL;
6934 if (gl_lightmaps.integer < 2)
6935 t->backgroundnmaptexture = r_texture_blanknormalmap;
6936 t->backgroundglosstexture = r_texture_black;
6937 t->backgroundglowtexture = NULL;
6939 t->currentmaterialflags = MATERIALFLAG_WALL | (t->currentmaterialflags & (MATERIALFLAG_NOCULLFACE | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SHORTDEPTHRANGE));
6942 if (specularscale != 1.0f)
6944 for (q = 0; q < 3; q++)
6946 t->render_modellight_specular[q] *= specularscale;
6947 t->render_lightmap_specular[q] *= specularscale;
6948 t->render_rtlight_specular[q] *= specularscale;
6952 t->currentblendfunc[0] = GL_ONE;
6953 t->currentblendfunc[1] = GL_ZERO;
6954 if (t->currentmaterialflags & MATERIALFLAG_ADD)
6956 t->currentblendfunc[0] = GL_SRC_ALPHA;
6957 t->currentblendfunc[1] = GL_ONE;
6959 else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
6961 t->currentblendfunc[0] = GL_SRC_ALPHA;
6962 t->currentblendfunc[1] = GL_ONE_MINUS_SRC_ALPHA;
6964 else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
6966 t->currentblendfunc[0] = t->customblendfunc[0];
6967 t->currentblendfunc[1] = t->customblendfunc[1];
6973 rsurfacestate_t rsurface;
6975 void RSurf_ActiveModelEntity(const entity_render_t *ent, qbool wantnormals, qbool wanttangents, qbool prepass)
6977 model_t *model = ent->model;
6978 //if (rsurface.entity == ent && (!model->surfmesh.isanimated || (!wantnormals && !wanttangents)))
6980 rsurface.entity = (entity_render_t *)ent;
6981 rsurface.skeleton = ent->skeleton;
6982 memcpy(rsurface.userwavefunc_param, ent->userwavefunc_param, sizeof(rsurface.userwavefunc_param));
6983 rsurface.ent_skinnum = ent->skinnum;
6984 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;
6985 rsurface.ent_flags = ent->flags;
6986 if (r_fullbright_directed.integer && (r_fullbright.integer || !model->lit))
6987 rsurface.ent_flags |= RENDER_LIGHT | RENDER_DYNAMICMODELLIGHT;
6988 rsurface.shadertime = r_refdef.scene.time - ent->shadertime;
6989 rsurface.matrix = ent->matrix;
6990 rsurface.inversematrix = ent->inversematrix;
6991 rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
6992 rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
6993 R_EntityMatrix(&rsurface.matrix);
6994 Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
6995 Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
6996 rsurface.fogplaneviewdist = r_refdef.fogplaneviewdist * rsurface.inversematrixscale;
6997 rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
6998 rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
6999 rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
7000 memcpy(rsurface.frameblend, ent->frameblend, sizeof(ent->frameblend));
7001 rsurface.ent_alttextures = ent->framegroupblend[0].frame != 0;
7002 rsurface.basepolygonfactor = r_refdef.polygonfactor;
7003 rsurface.basepolygonoffset = r_refdef.polygonoffset;
7004 if (ent->model->brush.submodel && !prepass)
7006 rsurface.basepolygonfactor += r_polygonoffset_submodel_factor.value;
7007 rsurface.basepolygonoffset += r_polygonoffset_submodel_offset.value;
7009 // if the animcache code decided it should use the shader path, skip the deform step
7010 rsurface.entityskeletaltransform3x4 = ent->animcache_skeletaltransform3x4;
7011 rsurface.entityskeletaltransform3x4buffer = ent->animcache_skeletaltransform3x4buffer;
7012 rsurface.entityskeletaltransform3x4offset = ent->animcache_skeletaltransform3x4offset;
7013 rsurface.entityskeletaltransform3x4size = ent->animcache_skeletaltransform3x4size;
7014 rsurface.entityskeletalnumtransforms = rsurface.entityskeletaltransform3x4 ? model->num_bones : 0;
7015 if (model->surfmesh.isanimated && model->AnimateVertices && !rsurface.entityskeletaltransform3x4)
7017 if (ent->animcache_vertex3f)
7019 r_refdef.stats[r_stat_batch_entitycache_count]++;
7020 r_refdef.stats[r_stat_batch_entitycache_surfaces] += model->num_surfaces;
7021 r_refdef.stats[r_stat_batch_entitycache_vertices] += model->surfmesh.num_vertices;
7022 r_refdef.stats[r_stat_batch_entitycache_triangles] += model->surfmesh.num_triangles;
7023 rsurface.modelvertex3f = ent->animcache_vertex3f;
7024 rsurface.modelvertex3f_vertexbuffer = ent->animcache_vertex3f_vertexbuffer;
7025 rsurface.modelvertex3f_bufferoffset = ent->animcache_vertex3f_bufferoffset;
7026 rsurface.modelsvector3f = wanttangents ? ent->animcache_svector3f : NULL;
7027 rsurface.modelsvector3f_vertexbuffer = wanttangents ? ent->animcache_svector3f_vertexbuffer : NULL;
7028 rsurface.modelsvector3f_bufferoffset = wanttangents ? ent->animcache_svector3f_bufferoffset : 0;
7029 rsurface.modeltvector3f = wanttangents ? ent->animcache_tvector3f : NULL;
7030 rsurface.modeltvector3f_vertexbuffer = wanttangents ? ent->animcache_tvector3f_vertexbuffer : NULL;
7031 rsurface.modeltvector3f_bufferoffset = wanttangents ? ent->animcache_tvector3f_bufferoffset : 0;
7032 rsurface.modelnormal3f = wantnormals ? ent->animcache_normal3f : NULL;
7033 rsurface.modelnormal3f_vertexbuffer = wantnormals ? ent->animcache_normal3f_vertexbuffer : NULL;
7034 rsurface.modelnormal3f_bufferoffset = wantnormals ? ent->animcache_normal3f_bufferoffset : 0;
7036 else if (wanttangents)
7038 r_refdef.stats[r_stat_batch_entityanimate_count]++;
7039 r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
7040 r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
7041 r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
7042 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7043 rsurface.modelsvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7044 rsurface.modeltvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7045 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7046 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, rsurface.modelsvector3f, rsurface.modeltvector3f);
7047 rsurface.modelvertex3f_vertexbuffer = NULL;
7048 rsurface.modelvertex3f_bufferoffset = 0;
7049 rsurface.modelvertex3f_vertexbuffer = 0;
7050 rsurface.modelvertex3f_bufferoffset = 0;
7051 rsurface.modelsvector3f_vertexbuffer = 0;
7052 rsurface.modelsvector3f_bufferoffset = 0;
7053 rsurface.modeltvector3f_vertexbuffer = 0;
7054 rsurface.modeltvector3f_bufferoffset = 0;
7055 rsurface.modelnormal3f_vertexbuffer = 0;
7056 rsurface.modelnormal3f_bufferoffset = 0;
7058 else if (wantnormals)
7060 r_refdef.stats[r_stat_batch_entityanimate_count]++;
7061 r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
7062 r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
7063 r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
7064 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7065 rsurface.modelsvector3f = NULL;
7066 rsurface.modeltvector3f = NULL;
7067 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7068 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, NULL, NULL);
7069 rsurface.modelvertex3f_vertexbuffer = NULL;
7070 rsurface.modelvertex3f_bufferoffset = 0;
7071 rsurface.modelvertex3f_vertexbuffer = 0;
7072 rsurface.modelvertex3f_bufferoffset = 0;
7073 rsurface.modelsvector3f_vertexbuffer = 0;
7074 rsurface.modelsvector3f_bufferoffset = 0;
7075 rsurface.modeltvector3f_vertexbuffer = 0;
7076 rsurface.modeltvector3f_bufferoffset = 0;
7077 rsurface.modelnormal3f_vertexbuffer = 0;
7078 rsurface.modelnormal3f_bufferoffset = 0;
7082 r_refdef.stats[r_stat_batch_entityanimate_count]++;
7083 r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
7084 r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
7085 r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
7086 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7087 rsurface.modelsvector3f = NULL;
7088 rsurface.modeltvector3f = NULL;
7089 rsurface.modelnormal3f = NULL;
7090 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, NULL, NULL, NULL);
7091 rsurface.modelvertex3f_vertexbuffer = NULL;
7092 rsurface.modelvertex3f_bufferoffset = 0;
7093 rsurface.modelvertex3f_vertexbuffer = 0;
7094 rsurface.modelvertex3f_bufferoffset = 0;
7095 rsurface.modelsvector3f_vertexbuffer = 0;
7096 rsurface.modelsvector3f_bufferoffset = 0;
7097 rsurface.modeltvector3f_vertexbuffer = 0;
7098 rsurface.modeltvector3f_bufferoffset = 0;
7099 rsurface.modelnormal3f_vertexbuffer = 0;
7100 rsurface.modelnormal3f_bufferoffset = 0;
7102 rsurface.modelgeneratedvertex = true;
7106 if (rsurface.entityskeletaltransform3x4)
7108 r_refdef.stats[r_stat_batch_entityskeletal_count]++;
7109 r_refdef.stats[r_stat_batch_entityskeletal_surfaces] += model->num_surfaces;
7110 r_refdef.stats[r_stat_batch_entityskeletal_vertices] += model->surfmesh.num_vertices;
7111 r_refdef.stats[r_stat_batch_entityskeletal_triangles] += model->surfmesh.num_triangles;
7115 r_refdef.stats[r_stat_batch_entitystatic_count]++;
7116 r_refdef.stats[r_stat_batch_entitystatic_surfaces] += model->num_surfaces;
7117 r_refdef.stats[r_stat_batch_entitystatic_vertices] += model->surfmesh.num_vertices;
7118 r_refdef.stats[r_stat_batch_entitystatic_triangles] += model->surfmesh.num_triangles;
7120 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
7121 rsurface.modelvertex3f_vertexbuffer = model->surfmesh.data_vertex3f_vertexbuffer;
7122 rsurface.modelvertex3f_bufferoffset = model->surfmesh.data_vertex3f_bufferoffset;
7123 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
7124 rsurface.modelsvector3f_vertexbuffer = model->surfmesh.data_svector3f_vertexbuffer;
7125 rsurface.modelsvector3f_bufferoffset = model->surfmesh.data_svector3f_bufferoffset;
7126 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
7127 rsurface.modeltvector3f_vertexbuffer = model->surfmesh.data_tvector3f_vertexbuffer;
7128 rsurface.modeltvector3f_bufferoffset = model->surfmesh.data_tvector3f_bufferoffset;
7129 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
7130 rsurface.modelnormal3f_vertexbuffer = model->surfmesh.data_normal3f_vertexbuffer;
7131 rsurface.modelnormal3f_bufferoffset = model->surfmesh.data_normal3f_bufferoffset;
7132 rsurface.modelgeneratedvertex = false;
7134 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
7135 rsurface.modellightmapcolor4f_vertexbuffer = model->surfmesh.data_lightmapcolor4f_vertexbuffer;
7136 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.data_lightmapcolor4f_bufferoffset;
7137 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
7138 rsurface.modeltexcoordtexture2f_vertexbuffer = model->surfmesh.data_texcoordtexture2f_vertexbuffer;
7139 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.data_texcoordtexture2f_bufferoffset;
7140 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
7141 rsurface.modeltexcoordlightmap2f_vertexbuffer = model->surfmesh.data_texcoordlightmap2f_vertexbuffer;
7142 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.data_texcoordlightmap2f_bufferoffset;
7143 rsurface.modelskeletalindex4ub = model->surfmesh.data_skeletalindex4ub;
7144 rsurface.modelskeletalindex4ub_vertexbuffer = model->surfmesh.data_skeletalindex4ub_vertexbuffer;
7145 rsurface.modelskeletalindex4ub_bufferoffset = model->surfmesh.data_skeletalindex4ub_bufferoffset;
7146 rsurface.modelskeletalweight4ub = model->surfmesh.data_skeletalweight4ub;
7147 rsurface.modelskeletalweight4ub_vertexbuffer = model->surfmesh.data_skeletalweight4ub_vertexbuffer;
7148 rsurface.modelskeletalweight4ub_bufferoffset = model->surfmesh.data_skeletalweight4ub_bufferoffset;
7149 rsurface.modelelement3i = model->surfmesh.data_element3i;
7150 rsurface.modelelement3i_indexbuffer = model->surfmesh.data_element3i_indexbuffer;
7151 rsurface.modelelement3i_bufferoffset = model->surfmesh.data_element3i_bufferoffset;
7152 rsurface.modelelement3s = model->surfmesh.data_element3s;
7153 rsurface.modelelement3s_indexbuffer = model->surfmesh.data_element3s_indexbuffer;
7154 rsurface.modelelement3s_bufferoffset = model->surfmesh.data_element3s_bufferoffset;
7155 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
7156 rsurface.modelnumvertices = model->surfmesh.num_vertices;
7157 rsurface.modelnumtriangles = model->surfmesh.num_triangles;
7158 rsurface.modelsurfaces = model->data_surfaces;
7159 rsurface.batchgeneratedvertex = false;
7160 rsurface.batchfirstvertex = 0;
7161 rsurface.batchnumvertices = 0;
7162 rsurface.batchfirsttriangle = 0;
7163 rsurface.batchnumtriangles = 0;
7164 rsurface.batchvertex3f = NULL;
7165 rsurface.batchvertex3f_vertexbuffer = NULL;
7166 rsurface.batchvertex3f_bufferoffset = 0;
7167 rsurface.batchsvector3f = NULL;
7168 rsurface.batchsvector3f_vertexbuffer = NULL;
7169 rsurface.batchsvector3f_bufferoffset = 0;
7170 rsurface.batchtvector3f = NULL;
7171 rsurface.batchtvector3f_vertexbuffer = NULL;
7172 rsurface.batchtvector3f_bufferoffset = 0;
7173 rsurface.batchnormal3f = NULL;
7174 rsurface.batchnormal3f_vertexbuffer = NULL;
7175 rsurface.batchnormal3f_bufferoffset = 0;
7176 rsurface.batchlightmapcolor4f = NULL;
7177 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7178 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7179 rsurface.batchtexcoordtexture2f = NULL;
7180 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7181 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7182 rsurface.batchtexcoordlightmap2f = NULL;
7183 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7184 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7185 rsurface.batchskeletalindex4ub = NULL;
7186 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7187 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7188 rsurface.batchskeletalweight4ub = NULL;
7189 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7190 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7191 rsurface.batchelement3i = NULL;
7192 rsurface.batchelement3i_indexbuffer = NULL;
7193 rsurface.batchelement3i_bufferoffset = 0;
7194 rsurface.batchelement3s = NULL;
7195 rsurface.batchelement3s_indexbuffer = NULL;
7196 rsurface.batchelement3s_bufferoffset = 0;
7197 rsurface.forcecurrenttextureupdate = false;
7200 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)
7202 rsurface.entity = r_refdef.scene.worldentity;
7203 if (r != 1.0f || g != 1.0f || b != 1.0f || a != 1.0f) {
7204 // HACK to provide a valid entity with modded colors to R_GetCurrentTexture.
7205 // A better approach could be making this copy only once per frame.
7206 static entity_render_t custom_entity;
7208 custom_entity = *rsurface.entity;
7209 for (q = 0; q < 3; ++q) {
7210 float colormod = q == 0 ? r : q == 1 ? g : b;
7211 custom_entity.render_fullbright[q] *= colormod;
7212 custom_entity.render_modellight_ambient[q] *= colormod;
7213 custom_entity.render_modellight_diffuse[q] *= colormod;
7214 custom_entity.render_lightmap_ambient[q] *= colormod;
7215 custom_entity.render_lightmap_diffuse[q] *= colormod;
7216 custom_entity.render_rtlight_diffuse[q] *= colormod;
7218 custom_entity.alpha *= a;
7219 rsurface.entity = &custom_entity;
7221 rsurface.skeleton = NULL;
7222 rsurface.ent_skinnum = 0;
7223 rsurface.ent_qwskin = -1;
7224 rsurface.ent_flags = entflags;
7225 rsurface.shadertime = r_refdef.scene.time - shadertime;
7226 rsurface.modelnumvertices = numvertices;
7227 rsurface.modelnumtriangles = numtriangles;
7228 rsurface.matrix = *matrix;
7229 rsurface.inversematrix = *inversematrix;
7230 rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
7231 rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
7232 R_EntityMatrix(&rsurface.matrix);
7233 Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
7234 Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
7235 rsurface.fogplaneviewdist *= rsurface.inversematrixscale;
7236 rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
7237 rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
7238 rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
7239 memset(rsurface.frameblend, 0, sizeof(rsurface.frameblend));
7240 rsurface.frameblend[0].lerp = 1;
7241 rsurface.ent_alttextures = false;
7242 rsurface.basepolygonfactor = r_refdef.polygonfactor;
7243 rsurface.basepolygonoffset = r_refdef.polygonoffset;
7244 rsurface.entityskeletaltransform3x4 = NULL;
7245 rsurface.entityskeletaltransform3x4buffer = NULL;
7246 rsurface.entityskeletaltransform3x4offset = 0;
7247 rsurface.entityskeletaltransform3x4size = 0;
7248 rsurface.entityskeletalnumtransforms = 0;
7249 r_refdef.stats[r_stat_batch_entitycustom_count]++;
7250 r_refdef.stats[r_stat_batch_entitycustom_surfaces] += 1;
7251 r_refdef.stats[r_stat_batch_entitycustom_vertices] += rsurface.modelnumvertices;
7252 r_refdef.stats[r_stat_batch_entitycustom_triangles] += rsurface.modelnumtriangles;
7255 rsurface.modelvertex3f = (float *)vertex3f;
7256 rsurface.modelsvector3f = svector3f ? (float *)svector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7257 rsurface.modeltvector3f = tvector3f ? (float *)tvector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7258 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7260 else if (wantnormals)
7262 rsurface.modelvertex3f = (float *)vertex3f;
7263 rsurface.modelsvector3f = NULL;
7264 rsurface.modeltvector3f = NULL;
7265 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7269 rsurface.modelvertex3f = (float *)vertex3f;
7270 rsurface.modelsvector3f = NULL;
7271 rsurface.modeltvector3f = NULL;
7272 rsurface.modelnormal3f = NULL;
7274 rsurface.modelvertex3f_vertexbuffer = 0;
7275 rsurface.modelvertex3f_bufferoffset = 0;
7276 rsurface.modelsvector3f_vertexbuffer = 0;
7277 rsurface.modelsvector3f_bufferoffset = 0;
7278 rsurface.modeltvector3f_vertexbuffer = 0;
7279 rsurface.modeltvector3f_bufferoffset = 0;
7280 rsurface.modelnormal3f_vertexbuffer = 0;
7281 rsurface.modelnormal3f_bufferoffset = 0;
7282 rsurface.modelgeneratedvertex = true;
7283 rsurface.modellightmapcolor4f = (float *)color4f;
7284 rsurface.modellightmapcolor4f_vertexbuffer = 0;
7285 rsurface.modellightmapcolor4f_bufferoffset = 0;
7286 rsurface.modeltexcoordtexture2f = (float *)texcoord2f;
7287 rsurface.modeltexcoordtexture2f_vertexbuffer = 0;
7288 rsurface.modeltexcoordtexture2f_bufferoffset = 0;
7289 rsurface.modeltexcoordlightmap2f = NULL;
7290 rsurface.modeltexcoordlightmap2f_vertexbuffer = 0;
7291 rsurface.modeltexcoordlightmap2f_bufferoffset = 0;
7292 rsurface.modelskeletalindex4ub = NULL;
7293 rsurface.modelskeletalindex4ub_vertexbuffer = NULL;
7294 rsurface.modelskeletalindex4ub_bufferoffset = 0;
7295 rsurface.modelskeletalweight4ub = NULL;
7296 rsurface.modelskeletalweight4ub_vertexbuffer = NULL;
7297 rsurface.modelskeletalweight4ub_bufferoffset = 0;
7298 rsurface.modelelement3i = (int *)element3i;
7299 rsurface.modelelement3i_indexbuffer = NULL;
7300 rsurface.modelelement3i_bufferoffset = 0;
7301 rsurface.modelelement3s = (unsigned short *)element3s;
7302 rsurface.modelelement3s_indexbuffer = NULL;
7303 rsurface.modelelement3s_bufferoffset = 0;
7304 rsurface.modellightmapoffsets = NULL;
7305 rsurface.modelsurfaces = NULL;
7306 rsurface.batchgeneratedvertex = false;
7307 rsurface.batchfirstvertex = 0;
7308 rsurface.batchnumvertices = 0;
7309 rsurface.batchfirsttriangle = 0;
7310 rsurface.batchnumtriangles = 0;
7311 rsurface.batchvertex3f = NULL;
7312 rsurface.batchvertex3f_vertexbuffer = NULL;
7313 rsurface.batchvertex3f_bufferoffset = 0;
7314 rsurface.batchsvector3f = NULL;
7315 rsurface.batchsvector3f_vertexbuffer = NULL;
7316 rsurface.batchsvector3f_bufferoffset = 0;
7317 rsurface.batchtvector3f = NULL;
7318 rsurface.batchtvector3f_vertexbuffer = NULL;
7319 rsurface.batchtvector3f_bufferoffset = 0;
7320 rsurface.batchnormal3f = NULL;
7321 rsurface.batchnormal3f_vertexbuffer = NULL;
7322 rsurface.batchnormal3f_bufferoffset = 0;
7323 rsurface.batchlightmapcolor4f = NULL;
7324 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7325 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7326 rsurface.batchtexcoordtexture2f = NULL;
7327 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7328 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7329 rsurface.batchtexcoordlightmap2f = NULL;
7330 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7331 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7332 rsurface.batchskeletalindex4ub = NULL;
7333 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7334 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7335 rsurface.batchskeletalweight4ub = NULL;
7336 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7337 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7338 rsurface.batchelement3i = NULL;
7339 rsurface.batchelement3i_indexbuffer = NULL;
7340 rsurface.batchelement3i_bufferoffset = 0;
7341 rsurface.batchelement3s = NULL;
7342 rsurface.batchelement3s_indexbuffer = NULL;
7343 rsurface.batchelement3s_bufferoffset = 0;
7344 rsurface.forcecurrenttextureupdate = true;
7346 if (rsurface.modelnumvertices && rsurface.modelelement3i)
7348 if ((wantnormals || wanttangents) && !normal3f)
7350 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7351 Mod_BuildNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.modelnormal3f, r_smoothnormals_areaweighting.integer != 0);
7353 if (wanttangents && !svector3f)
7355 rsurface.modelsvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7356 rsurface.modeltvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7357 Mod_BuildTextureVectorsFromNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modeltexcoordtexture2f, rsurface.modelnormal3f, rsurface.modelelement3i, rsurface.modelsvector3f, rsurface.modeltvector3f, r_smoothnormals_areaweighting.integer != 0);
7362 float RSurf_FogPoint(const float *v)
7364 // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
7365 float FogPlaneViewDist = r_refdef.fogplaneviewdist;
7366 float FogPlaneVertexDist = DotProduct(r_refdef.fogplane, v) + r_refdef.fogplane[3];
7367 float FogHeightFade = r_refdef.fogheightfade;
7369 unsigned int fogmasktableindex;
7370 if (r_refdef.fogplaneviewabove)
7371 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
7373 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
7374 fogmasktableindex = (unsigned int)(VectorDistance(r_refdef.view.origin, v) * fogfrac * r_refdef.fogmasktabledistmultiplier);
7375 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
7378 float RSurf_FogVertex(const float *v)
7380 // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
7381 float FogPlaneViewDist = rsurface.fogplaneviewdist;
7382 float FogPlaneVertexDist = DotProduct(rsurface.fogplane, v) + rsurface.fogplane[3];
7383 float FogHeightFade = rsurface.fogheightfade;
7385 unsigned int fogmasktableindex;
7386 if (r_refdef.fogplaneviewabove)
7387 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
7389 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
7390 fogmasktableindex = (unsigned int)(VectorDistance(rsurface.localvieworigin, v) * fogfrac * rsurface.fogmasktabledistmultiplier);
7391 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
7394 void RSurf_UploadBuffersForBatch(void)
7396 // 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)
7397 // note that if rsurface.batchvertex3f_vertexbuffer is NULL, dynamicvertex is forced as we don't account for the proper base vertex here.
7398 if (rsurface.batchvertex3f && !rsurface.batchvertex3f_vertexbuffer)
7399 rsurface.batchvertex3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f, R_BUFFERDATA_VERTEX, &rsurface.batchvertex3f_bufferoffset);
7400 if (rsurface.batchsvector3f && !rsurface.batchsvector3f_vertexbuffer)
7401 rsurface.batchsvector3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchsvector3f, R_BUFFERDATA_VERTEX, &rsurface.batchsvector3f_bufferoffset);
7402 if (rsurface.batchtvector3f && !rsurface.batchtvector3f_vertexbuffer)
7403 rsurface.batchtvector3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchtvector3f, R_BUFFERDATA_VERTEX, &rsurface.batchtvector3f_bufferoffset);
7404 if (rsurface.batchnormal3f && !rsurface.batchnormal3f_vertexbuffer)
7405 rsurface.batchnormal3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f, R_BUFFERDATA_VERTEX, &rsurface.batchnormal3f_bufferoffset);
7406 if (rsurface.batchlightmapcolor4f && !rsurface.batchlightmapcolor4f_vertexbuffer)
7407 rsurface.batchlightmapcolor4f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[4]), rsurface.batchlightmapcolor4f, R_BUFFERDATA_VERTEX, &rsurface.batchlightmapcolor4f_bufferoffset);
7408 if (rsurface.batchtexcoordtexture2f && !rsurface.batchtexcoordtexture2f_vertexbuffer)
7409 rsurface.batchtexcoordtexture2f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[2]), rsurface.batchtexcoordtexture2f, R_BUFFERDATA_VERTEX, &rsurface.batchtexcoordtexture2f_bufferoffset);
7410 if (rsurface.batchtexcoordlightmap2f && !rsurface.batchtexcoordlightmap2f_vertexbuffer)
7411 rsurface.batchtexcoordlightmap2f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[2]), rsurface.batchtexcoordlightmap2f, R_BUFFERDATA_VERTEX, &rsurface.batchtexcoordlightmap2f_bufferoffset);
7412 if (rsurface.batchskeletalindex4ub && !rsurface.batchskeletalindex4ub_vertexbuffer)
7413 rsurface.batchskeletalindex4ub_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(unsigned char[4]), rsurface.batchskeletalindex4ub, R_BUFFERDATA_VERTEX, &rsurface.batchskeletalindex4ub_bufferoffset);
7414 if (rsurface.batchskeletalweight4ub && !rsurface.batchskeletalweight4ub_vertexbuffer)
7415 rsurface.batchskeletalweight4ub_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(unsigned char[4]), rsurface.batchskeletalweight4ub, R_BUFFERDATA_VERTEX, &rsurface.batchskeletalweight4ub_bufferoffset);
7417 if (rsurface.batchelement3s && !rsurface.batchelement3s_indexbuffer)
7418 rsurface.batchelement3s_indexbuffer = R_BufferData_Store(rsurface.batchnumtriangles * sizeof(short[3]), rsurface.batchelement3s, R_BUFFERDATA_INDEX16, &rsurface.batchelement3s_bufferoffset);
7419 else if (rsurface.batchelement3i && !rsurface.batchelement3i_indexbuffer)
7420 rsurface.batchelement3i_indexbuffer = R_BufferData_Store(rsurface.batchnumtriangles * sizeof(int[3]), rsurface.batchelement3i, R_BUFFERDATA_INDEX32, &rsurface.batchelement3i_bufferoffset);
7422 R_Mesh_VertexPointer( 3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
7423 R_Mesh_ColorPointer( 4, GL_FLOAT, sizeof(float[4]), rsurface.batchlightmapcolor4f, rsurface.batchlightmapcolor4f_vertexbuffer, rsurface.batchlightmapcolor4f_bufferoffset);
7424 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
7425 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchsvector3f, rsurface.batchsvector3f_vertexbuffer, rsurface.batchsvector3f_bufferoffset);
7426 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchtvector3f, rsurface.batchtvector3f_vertexbuffer, rsurface.batchtvector3f_bufferoffset);
7427 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchnormal3f, rsurface.batchnormal3f_vertexbuffer, rsurface.batchnormal3f_bufferoffset);
7428 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordlightmap2f, rsurface.batchtexcoordlightmap2f_vertexbuffer, rsurface.batchtexcoordlightmap2f_bufferoffset);
7429 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
7430 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(unsigned char[4]), rsurface.batchskeletalindex4ub, rsurface.batchskeletalindex4ub_vertexbuffer, rsurface.batchskeletalindex4ub_bufferoffset);
7431 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), rsurface.batchskeletalweight4ub, rsurface.batchskeletalweight4ub_vertexbuffer, rsurface.batchskeletalweight4ub_bufferoffset);
7434 static void RSurf_RenumberElements(const int *inelement3i, int *outelement3i, int numelements, int adjust)
7437 for (i = 0;i < numelements;i++)
7438 outelement3i[i] = inelement3i[i] + adjust;
7441 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
7442 void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const msurface_t **texturesurfacelist)
7450 int surfacefirsttriangle;
7451 int surfacenumtriangles;
7452 int surfacefirstvertex;
7453 int surfaceendvertex;
7454 int surfacenumvertices;
7455 int batchnumsurfaces = texturenumsurfaces;
7456 int batchnumvertices;
7457 int batchnumtriangles;
7460 qbool dynamicvertex;
7463 float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
7466 q3shaderinfo_deform_t *deform;
7467 const msurface_t *surface, *firstsurface;
7468 if (!texturenumsurfaces)
7470 // find vertex range of this surface batch
7472 firstsurface = texturesurfacelist[0];
7473 firsttriangle = firstsurface->num_firsttriangle;
7474 batchnumvertices = 0;
7475 batchnumtriangles = 0;
7476 firstvertex = endvertex = firstsurface->num_firstvertex;
7477 for (i = 0;i < texturenumsurfaces;i++)
7479 surface = texturesurfacelist[i];
7480 if (surface != firstsurface + i)
7482 surfacefirstvertex = surface->num_firstvertex;
7483 surfaceendvertex = surfacefirstvertex + surface->num_vertices;
7484 surfacenumvertices = surface->num_vertices;
7485 surfacenumtriangles = surface->num_triangles;
7486 if (firstvertex > surfacefirstvertex)
7487 firstvertex = surfacefirstvertex;
7488 if (endvertex < surfaceendvertex)
7489 endvertex = surfaceendvertex;
7490 batchnumvertices += surfacenumvertices;
7491 batchnumtriangles += surfacenumtriangles;
7494 r_refdef.stats[r_stat_batch_batches]++;
7496 r_refdef.stats[r_stat_batch_withgaps]++;
7497 r_refdef.stats[r_stat_batch_surfaces] += batchnumsurfaces;
7498 r_refdef.stats[r_stat_batch_vertices] += batchnumvertices;
7499 r_refdef.stats[r_stat_batch_triangles] += batchnumtriangles;
7501 // we now know the vertex range used, and if there are any gaps in it
7502 rsurface.batchfirstvertex = firstvertex;
7503 rsurface.batchnumvertices = endvertex - firstvertex;
7504 rsurface.batchfirsttriangle = firsttriangle;
7505 rsurface.batchnumtriangles = batchnumtriangles;
7507 // check if any dynamic vertex processing must occur
7508 dynamicvertex = false;
7510 // we must use vertexbuffers for rendering, we can upload vertex buffers
7511 // easily enough but if the basevertex is non-zero it becomes more
7512 // difficult, so force dynamicvertex path in that case - it's suboptimal
7513 // but the most optimal case is to have the geometry sources provide their
7515 if (!rsurface.modelvertex3f_vertexbuffer && firstvertex != 0)
7516 dynamicvertex = true;
7518 // a cvar to force the dynamic vertex path to be taken, for debugging
7519 if (r_batch_debugdynamicvertexpath.integer)
7523 r_refdef.stats[r_stat_batch_dynamic_batches_because_cvar] += 1;
7524 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_cvar] += batchnumsurfaces;
7525 r_refdef.stats[r_stat_batch_dynamic_vertices_because_cvar] += batchnumvertices;
7526 r_refdef.stats[r_stat_batch_dynamic_triangles_because_cvar] += batchnumtriangles;
7528 dynamicvertex = true;
7531 // if there is a chance of animated vertex colors, it's a dynamic batch
7532 if ((batchneed & BATCHNEED_ARRAY_VERTEXCOLOR) && texturesurfacelist[0]->lightmapinfo)
7536 r_refdef.stats[r_stat_batch_dynamic_batches_because_lightmapvertex] += 1;
7537 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_lightmapvertex] += batchnumsurfaces;
7538 r_refdef.stats[r_stat_batch_dynamic_vertices_because_lightmapvertex] += batchnumvertices;
7539 r_refdef.stats[r_stat_batch_dynamic_triangles_because_lightmapvertex] += batchnumtriangles;
7541 dynamicvertex = true;
7544 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
7546 switch (deform->deform)
7549 case Q3DEFORM_PROJECTIONSHADOW:
7550 case Q3DEFORM_TEXT0:
7551 case Q3DEFORM_TEXT1:
7552 case Q3DEFORM_TEXT2:
7553 case Q3DEFORM_TEXT3:
7554 case Q3DEFORM_TEXT4:
7555 case Q3DEFORM_TEXT5:
7556 case Q3DEFORM_TEXT6:
7557 case Q3DEFORM_TEXT7:
7560 case Q3DEFORM_AUTOSPRITE:
7563 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_autosprite] += 1;
7564 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_autosprite] += batchnumsurfaces;
7565 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_autosprite] += batchnumvertices;
7566 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_autosprite] += batchnumtriangles;
7568 dynamicvertex = true;
7569 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_TEXCOORD;
7571 case Q3DEFORM_AUTOSPRITE2:
7574 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_autosprite2] += 1;
7575 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_autosprite2] += batchnumsurfaces;
7576 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_autosprite2] += batchnumvertices;
7577 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_autosprite2] += batchnumtriangles;
7579 dynamicvertex = true;
7580 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD;
7582 case Q3DEFORM_NORMAL:
7585 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_normal] += 1;
7586 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_normal] += batchnumsurfaces;
7587 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_normal] += batchnumvertices;
7588 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_normal] += batchnumtriangles;
7590 dynamicvertex = true;
7591 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7594 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
7595 break; // if wavefunc is a nop, ignore this transform
7598 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_wave] += 1;
7599 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_wave] += batchnumsurfaces;
7600 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_wave] += batchnumvertices;
7601 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_wave] += batchnumtriangles;
7603 dynamicvertex = true;
7604 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7606 case Q3DEFORM_BULGE:
7609 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_bulge] += 1;
7610 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_bulge] += batchnumsurfaces;
7611 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_bulge] += batchnumvertices;
7612 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_bulge] += batchnumtriangles;
7614 dynamicvertex = true;
7615 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7618 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
7619 break; // if wavefunc is a nop, ignore this transform
7622 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_move] += 1;
7623 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_move] += batchnumsurfaces;
7624 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_move] += batchnumvertices;
7625 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_move] += batchnumtriangles;
7627 dynamicvertex = true;
7628 batchneed |= BATCHNEED_ARRAY_VERTEX;
7632 if (rsurface.texture->materialshaderpass)
7634 switch (rsurface.texture->materialshaderpass->tcgen.tcgen)
7637 case Q3TCGEN_TEXTURE:
7639 case Q3TCGEN_LIGHTMAP:
7642 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_lightmap] += 1;
7643 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_lightmap] += batchnumsurfaces;
7644 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_lightmap] += batchnumvertices;
7645 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_lightmap] += batchnumtriangles;
7647 dynamicvertex = true;
7648 batchneed |= BATCHNEED_ARRAY_LIGHTMAP;
7650 case Q3TCGEN_VECTOR:
7653 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_vector] += 1;
7654 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_vector] += batchnumsurfaces;
7655 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_vector] += batchnumvertices;
7656 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_vector] += batchnumtriangles;
7658 dynamicvertex = true;
7659 batchneed |= BATCHNEED_ARRAY_VERTEX;
7661 case Q3TCGEN_ENVIRONMENT:
7664 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_environment] += 1;
7665 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_environment] += batchnumsurfaces;
7666 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_environment] += batchnumvertices;
7667 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_environment] += batchnumtriangles;
7669 dynamicvertex = true;
7670 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL;
7673 if (rsurface.texture->materialshaderpass->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
7677 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcmod_turbulent] += 1;
7678 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcmod_turbulent] += batchnumsurfaces;
7679 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcmod_turbulent] += batchnumvertices;
7680 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcmod_turbulent] += batchnumtriangles;
7682 dynamicvertex = true;
7683 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD;
7687 // the caller can specify BATCHNEED_NOGAPS to force a batch with
7688 // firstvertex = 0 and endvertex = numvertices (no gaps, no firstvertex),
7689 // we ensure this by treating the vertex batch as dynamic...
7690 if ((batchneed & BATCHNEED_ALWAYSCOPY) || ((batchneed & BATCHNEED_NOGAPS) && (gaps || firstvertex > 0)))
7694 r_refdef.stats[r_stat_batch_dynamic_batches_because_nogaps] += 1;
7695 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_nogaps] += batchnumsurfaces;
7696 r_refdef.stats[r_stat_batch_dynamic_vertices_because_nogaps] += batchnumvertices;
7697 r_refdef.stats[r_stat_batch_dynamic_triangles_because_nogaps] += batchnumtriangles;
7699 dynamicvertex = true;
7702 // if we're going to have to apply the skeletal transform manually, we need to batch the skeletal data
7703 if (dynamicvertex && rsurface.entityskeletaltransform3x4)
7704 batchneed |= BATCHNEED_ARRAY_SKELETAL;
7706 rsurface.batchvertex3f = rsurface.modelvertex3f;
7707 rsurface.batchvertex3f_vertexbuffer = rsurface.modelvertex3f_vertexbuffer;
7708 rsurface.batchvertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
7709 rsurface.batchsvector3f = rsurface.modelsvector3f;
7710 rsurface.batchsvector3f_vertexbuffer = rsurface.modelsvector3f_vertexbuffer;
7711 rsurface.batchsvector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
7712 rsurface.batchtvector3f = rsurface.modeltvector3f;
7713 rsurface.batchtvector3f_vertexbuffer = rsurface.modeltvector3f_vertexbuffer;
7714 rsurface.batchtvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
7715 rsurface.batchnormal3f = rsurface.modelnormal3f;
7716 rsurface.batchnormal3f_vertexbuffer = rsurface.modelnormal3f_vertexbuffer;
7717 rsurface.batchnormal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
7718 rsurface.batchlightmapcolor4f = rsurface.modellightmapcolor4f;
7719 rsurface.batchlightmapcolor4f_vertexbuffer = rsurface.modellightmapcolor4f_vertexbuffer;
7720 rsurface.batchlightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
7721 rsurface.batchtexcoordtexture2f = rsurface.modeltexcoordtexture2f;
7722 rsurface.batchtexcoordtexture2f_vertexbuffer = rsurface.modeltexcoordtexture2f_vertexbuffer;
7723 rsurface.batchtexcoordtexture2f_bufferoffset = rsurface.modeltexcoordtexture2f_bufferoffset;
7724 rsurface.batchtexcoordlightmap2f = rsurface.modeltexcoordlightmap2f;
7725 rsurface.batchtexcoordlightmap2f_vertexbuffer = rsurface.modeltexcoordlightmap2f_vertexbuffer;
7726 rsurface.batchtexcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
7727 rsurface.batchskeletalindex4ub = rsurface.modelskeletalindex4ub;
7728 rsurface.batchskeletalindex4ub_vertexbuffer = rsurface.modelskeletalindex4ub_vertexbuffer;
7729 rsurface.batchskeletalindex4ub_bufferoffset = rsurface.modelskeletalindex4ub_bufferoffset;
7730 rsurface.batchskeletalweight4ub = rsurface.modelskeletalweight4ub;
7731 rsurface.batchskeletalweight4ub_vertexbuffer = rsurface.modelskeletalweight4ub_vertexbuffer;
7732 rsurface.batchskeletalweight4ub_bufferoffset = rsurface.modelskeletalweight4ub_bufferoffset;
7733 rsurface.batchelement3i = rsurface.modelelement3i;
7734 rsurface.batchelement3i_indexbuffer = rsurface.modelelement3i_indexbuffer;
7735 rsurface.batchelement3i_bufferoffset = rsurface.modelelement3i_bufferoffset;
7736 rsurface.batchelement3s = rsurface.modelelement3s;
7737 rsurface.batchelement3s_indexbuffer = rsurface.modelelement3s_indexbuffer;
7738 rsurface.batchelement3s_bufferoffset = rsurface.modelelement3s_bufferoffset;
7739 rsurface.batchskeletaltransform3x4 = rsurface.entityskeletaltransform3x4;
7740 rsurface.batchskeletaltransform3x4buffer = rsurface.entityskeletaltransform3x4buffer;
7741 rsurface.batchskeletaltransform3x4offset = rsurface.entityskeletaltransform3x4offset;
7742 rsurface.batchskeletaltransform3x4size = rsurface.entityskeletaltransform3x4size;
7743 rsurface.batchskeletalnumtransforms = rsurface.entityskeletalnumtransforms;
7745 // if any dynamic vertex processing has to occur in software, we copy the
7746 // entire surface list together before processing to rebase the vertices
7747 // to start at 0 (otherwise we waste a lot of room in a vertex buffer).
7749 // if any gaps exist and we do not have a static vertex buffer, we have to
7750 // copy the surface list together to avoid wasting upload bandwidth on the
7751 // vertices in the gaps.
7753 // if gaps exist and we have a static vertex buffer, we can choose whether
7754 // to combine the index buffer ranges into one dynamic index buffer or
7755 // simply issue multiple glDrawElements calls (BATCHNEED_ALLOWMULTIDRAW).
7757 // in many cases the batch is reduced to one draw call.
7759 rsurface.batchmultidraw = false;
7760 rsurface.batchmultidrawnumsurfaces = 0;
7761 rsurface.batchmultidrawsurfacelist = NULL;
7765 // static vertex data, just set pointers...
7766 rsurface.batchgeneratedvertex = false;
7767 // if there are gaps, we want to build a combined index buffer,
7768 // otherwise use the original static buffer with an appropriate offset
7771 r_refdef.stats[r_stat_batch_copytriangles_batches] += 1;
7772 r_refdef.stats[r_stat_batch_copytriangles_surfaces] += batchnumsurfaces;
7773 r_refdef.stats[r_stat_batch_copytriangles_vertices] += batchnumvertices;
7774 r_refdef.stats[r_stat_batch_copytriangles_triangles] += batchnumtriangles;
7775 if ((batchneed & BATCHNEED_ALLOWMULTIDRAW) && r_batch_multidraw.integer && batchnumtriangles >= r_batch_multidraw_mintriangles.integer)
7777 rsurface.batchmultidraw = true;
7778 rsurface.batchmultidrawnumsurfaces = texturenumsurfaces;
7779 rsurface.batchmultidrawsurfacelist = texturesurfacelist;
7782 // build a new triangle elements array for this batch
7783 rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
7784 rsurface.batchfirsttriangle = 0;
7786 for (i = 0;i < texturenumsurfaces;i++)
7788 surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
7789 surfacenumtriangles = texturesurfacelist[i]->num_triangles;
7790 memcpy(rsurface.batchelement3i + 3*numtriangles, rsurface.modelelement3i + 3*surfacefirsttriangle, surfacenumtriangles*sizeof(int[3]));
7791 numtriangles += surfacenumtriangles;
7793 rsurface.batchelement3i_indexbuffer = NULL;
7794 rsurface.batchelement3i_bufferoffset = 0;
7795 rsurface.batchelement3s = NULL;
7796 rsurface.batchelement3s_indexbuffer = NULL;
7797 rsurface.batchelement3s_bufferoffset = 0;
7798 if (endvertex <= 65536)
7800 // make a 16bit (unsigned short) index array if possible
7801 rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
7802 for (i = 0;i < numtriangles*3;i++)
7803 rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
7808 r_refdef.stats[r_stat_batch_fast_batches] += 1;
7809 r_refdef.stats[r_stat_batch_fast_surfaces] += batchnumsurfaces;
7810 r_refdef.stats[r_stat_batch_fast_vertices] += batchnumvertices;
7811 r_refdef.stats[r_stat_batch_fast_triangles] += batchnumtriangles;
7816 // something needs software processing, do it for real...
7817 // we only directly handle separate array data in this case and then
7818 // generate interleaved data if needed...
7819 rsurface.batchgeneratedvertex = true;
7820 r_refdef.stats[r_stat_batch_dynamic_batches] += 1;
7821 r_refdef.stats[r_stat_batch_dynamic_surfaces] += batchnumsurfaces;
7822 r_refdef.stats[r_stat_batch_dynamic_vertices] += batchnumvertices;
7823 r_refdef.stats[r_stat_batch_dynamic_triangles] += batchnumtriangles;
7825 // now copy the vertex data into a combined array and make an index array
7826 // (this is what Quake3 does all the time)
7827 // we also apply any skeletal animation here that would have been done in
7828 // the vertex shader, because most of the dynamic vertex animation cases
7829 // need actual vertex positions and normals
7830 //if (dynamicvertex)
7832 rsurface.batchvertex3f = NULL;
7833 rsurface.batchvertex3f_vertexbuffer = NULL;
7834 rsurface.batchvertex3f_bufferoffset = 0;
7835 rsurface.batchsvector3f = NULL;
7836 rsurface.batchsvector3f_vertexbuffer = NULL;
7837 rsurface.batchsvector3f_bufferoffset = 0;
7838 rsurface.batchtvector3f = NULL;
7839 rsurface.batchtvector3f_vertexbuffer = NULL;
7840 rsurface.batchtvector3f_bufferoffset = 0;
7841 rsurface.batchnormal3f = NULL;
7842 rsurface.batchnormal3f_vertexbuffer = NULL;
7843 rsurface.batchnormal3f_bufferoffset = 0;
7844 rsurface.batchlightmapcolor4f = NULL;
7845 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7846 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7847 rsurface.batchtexcoordtexture2f = NULL;
7848 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7849 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7850 rsurface.batchtexcoordlightmap2f = NULL;
7851 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7852 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7853 rsurface.batchskeletalindex4ub = NULL;
7854 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7855 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7856 rsurface.batchskeletalweight4ub = NULL;
7857 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7858 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7859 rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
7860 rsurface.batchelement3i_indexbuffer = NULL;
7861 rsurface.batchelement3i_bufferoffset = 0;
7862 rsurface.batchelement3s = NULL;
7863 rsurface.batchelement3s_indexbuffer = NULL;
7864 rsurface.batchelement3s_bufferoffset = 0;
7865 rsurface.batchskeletaltransform3x4buffer = NULL;
7866 rsurface.batchskeletaltransform3x4offset = 0;
7867 rsurface.batchskeletaltransform3x4size = 0;
7868 // we'll only be setting up certain arrays as needed
7869 if (batchneed & BATCHNEED_ARRAY_VERTEX)
7870 rsurface.batchvertex3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7871 if (batchneed & BATCHNEED_ARRAY_NORMAL)
7872 rsurface.batchnormal3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7873 if (batchneed & BATCHNEED_ARRAY_VECTOR)
7875 rsurface.batchsvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7876 rsurface.batchtvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7878 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
7879 rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
7880 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
7881 rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
7882 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
7883 rsurface.batchtexcoordlightmap2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
7884 if (batchneed & BATCHNEED_ARRAY_SKELETAL)
7886 rsurface.batchskeletalindex4ub = (unsigned char *)R_FrameData_Alloc(batchnumvertices * sizeof(unsigned char[4]));
7887 rsurface.batchskeletalweight4ub = (unsigned char *)R_FrameData_Alloc(batchnumvertices * sizeof(unsigned char[4]));
7891 for (i = 0;i < texturenumsurfaces;i++)
7893 surfacefirstvertex = texturesurfacelist[i]->num_firstvertex;
7894 surfacenumvertices = texturesurfacelist[i]->num_vertices;
7895 surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
7896 surfacenumtriangles = texturesurfacelist[i]->num_triangles;
7897 // copy only the data requested
7898 if (batchneed & (BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_ARRAY_LIGHTMAP))
7900 if (batchneed & BATCHNEED_ARRAY_VERTEX)
7902 if (rsurface.batchvertex3f)
7903 memcpy(rsurface.batchvertex3f + 3*numvertices, rsurface.modelvertex3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7905 memset(rsurface.batchvertex3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7907 if (batchneed & BATCHNEED_ARRAY_NORMAL)
7909 if (rsurface.modelnormal3f)
7910 memcpy(rsurface.batchnormal3f + 3*numvertices, rsurface.modelnormal3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7912 memset(rsurface.batchnormal3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7914 if (batchneed & BATCHNEED_ARRAY_VECTOR)
7916 if (rsurface.modelsvector3f)
7918 memcpy(rsurface.batchsvector3f + 3*numvertices, rsurface.modelsvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7919 memcpy(rsurface.batchtvector3f + 3*numvertices, rsurface.modeltvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7923 memset(rsurface.batchsvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7924 memset(rsurface.batchtvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7927 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
7929 if (rsurface.modellightmapcolor4f)
7930 memcpy(rsurface.batchlightmapcolor4f + 4*numvertices, rsurface.modellightmapcolor4f + 4*surfacefirstvertex, surfacenumvertices * sizeof(float[4]));
7932 memset(rsurface.batchlightmapcolor4f + 4*numvertices, 0, surfacenumvertices * sizeof(float[4]));
7934 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
7936 if (rsurface.modeltexcoordtexture2f)
7937 memcpy(rsurface.batchtexcoordtexture2f + 2*numvertices, rsurface.modeltexcoordtexture2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
7939 memset(rsurface.batchtexcoordtexture2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
7941 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
7943 if (rsurface.modeltexcoordlightmap2f)
7944 memcpy(rsurface.batchtexcoordlightmap2f + 2*numvertices, rsurface.modeltexcoordlightmap2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
7946 memset(rsurface.batchtexcoordlightmap2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
7948 if (batchneed & BATCHNEED_ARRAY_SKELETAL)
7950 if (rsurface.modelskeletalindex4ub)
7952 memcpy(rsurface.batchskeletalindex4ub + 4*numvertices, rsurface.modelskeletalindex4ub + 4*surfacefirstvertex, surfacenumvertices * sizeof(unsigned char[4]));
7953 memcpy(rsurface.batchskeletalweight4ub + 4*numvertices, rsurface.modelskeletalweight4ub + 4*surfacefirstvertex, surfacenumvertices * sizeof(unsigned char[4]));
7957 memset(rsurface.batchskeletalindex4ub + 4*numvertices, 0, surfacenumvertices * sizeof(unsigned char[4]));
7958 memset(rsurface.batchskeletalweight4ub + 4*numvertices, 0, surfacenumvertices * sizeof(unsigned char[4]));
7959 ub = rsurface.batchskeletalweight4ub + 4*numvertices;
7960 for (j = 0;j < surfacenumvertices;j++)
7965 RSurf_RenumberElements(rsurface.modelelement3i + 3*surfacefirsttriangle, rsurface.batchelement3i + 3*numtriangles, 3*surfacenumtriangles, numvertices - surfacefirstvertex);
7966 numvertices += surfacenumvertices;
7967 numtriangles += surfacenumtriangles;
7970 // generate a 16bit index array as well if possible
7971 // (in general, dynamic batches fit)
7972 if (numvertices <= 65536)
7974 rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
7975 for (i = 0;i < numtriangles*3;i++)
7976 rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
7979 // since we've copied everything, the batch now starts at 0
7980 rsurface.batchfirstvertex = 0;
7981 rsurface.batchnumvertices = batchnumvertices;
7982 rsurface.batchfirsttriangle = 0;
7983 rsurface.batchnumtriangles = batchnumtriangles;
7986 // apply skeletal animation that would have been done in the vertex shader
7987 if (rsurface.batchskeletaltransform3x4)
7989 const unsigned char *si;
7990 const unsigned char *sw;
7992 const float *b = rsurface.batchskeletaltransform3x4;
7993 float *vp, *vs, *vt, *vn;
7995 float m[3][4], n[3][4];
7996 float tp[3], ts[3], tt[3], tn[3];
7997 r_refdef.stats[r_stat_batch_dynamicskeletal_batches] += 1;
7998 r_refdef.stats[r_stat_batch_dynamicskeletal_surfaces] += batchnumsurfaces;
7999 r_refdef.stats[r_stat_batch_dynamicskeletal_vertices] += batchnumvertices;
8000 r_refdef.stats[r_stat_batch_dynamicskeletal_triangles] += batchnumtriangles;
8001 si = rsurface.batchskeletalindex4ub;
8002 sw = rsurface.batchskeletalweight4ub;
8003 vp = rsurface.batchvertex3f;
8004 vs = rsurface.batchsvector3f;
8005 vt = rsurface.batchtvector3f;
8006 vn = rsurface.batchnormal3f;
8007 memset(m[0], 0, sizeof(m));
8008 memset(n[0], 0, sizeof(n));
8009 for (i = 0;i < batchnumvertices;i++)
8011 t[0] = b + si[0]*12;
8014 // common case - only one matrix
8028 else if (sw[2] + sw[3])
8031 t[1] = b + si[1]*12;
8032 t[2] = b + si[2]*12;
8033 t[3] = b + si[3]*12;
8034 w[0] = sw[0] * (1.0f / 255.0f);
8035 w[1] = sw[1] * (1.0f / 255.0f);
8036 w[2] = sw[2] * (1.0f / 255.0f);
8037 w[3] = sw[3] * (1.0f / 255.0f);
8038 // blend the matrices
8039 m[0][0] = t[0][ 0] * w[0] + t[1][ 0] * w[1] + t[2][ 0] * w[2] + t[3][ 0] * w[3];
8040 m[0][1] = t[0][ 1] * w[0] + t[1][ 1] * w[1] + t[2][ 1] * w[2] + t[3][ 1] * w[3];
8041 m[0][2] = t[0][ 2] * w[0] + t[1][ 2] * w[1] + t[2][ 2] * w[2] + t[3][ 2] * w[3];
8042 m[0][3] = t[0][ 3] * w[0] + t[1][ 3] * w[1] + t[2][ 3] * w[2] + t[3][ 3] * w[3];
8043 m[1][0] = t[0][ 4] * w[0] + t[1][ 4] * w[1] + t[2][ 4] * w[2] + t[3][ 4] * w[3];
8044 m[1][1] = t[0][ 5] * w[0] + t[1][ 5] * w[1] + t[2][ 5] * w[2] + t[3][ 5] * w[3];
8045 m[1][2] = t[0][ 6] * w[0] + t[1][ 6] * w[1] + t[2][ 6] * w[2] + t[3][ 6] * w[3];
8046 m[1][3] = t[0][ 7] * w[0] + t[1][ 7] * w[1] + t[2][ 7] * w[2] + t[3][ 7] * w[3];
8047 m[2][0] = t[0][ 8] * w[0] + t[1][ 8] * w[1] + t[2][ 8] * w[2] + t[3][ 8] * w[3];
8048 m[2][1] = t[0][ 9] * w[0] + t[1][ 9] * w[1] + t[2][ 9] * w[2] + t[3][ 9] * w[3];
8049 m[2][2] = t[0][10] * w[0] + t[1][10] * w[1] + t[2][10] * w[2] + t[3][10] * w[3];
8050 m[2][3] = t[0][11] * w[0] + t[1][11] * w[1] + t[2][11] * w[2] + t[3][11] * w[3];
8055 t[1] = b + si[1]*12;
8056 w[0] = sw[0] * (1.0f / 255.0f);
8057 w[1] = sw[1] * (1.0f / 255.0f);
8058 // blend the matrices
8059 m[0][0] = t[0][ 0] * w[0] + t[1][ 0] * w[1];
8060 m[0][1] = t[0][ 1] * w[0] + t[1][ 1] * w[1];
8061 m[0][2] = t[0][ 2] * w[0] + t[1][ 2] * w[1];
8062 m[0][3] = t[0][ 3] * w[0] + t[1][ 3] * w[1];
8063 m[1][0] = t[0][ 4] * w[0] + t[1][ 4] * w[1];
8064 m[1][1] = t[0][ 5] * w[0] + t[1][ 5] * w[1];
8065 m[1][2] = t[0][ 6] * w[0] + t[1][ 6] * w[1];
8066 m[1][3] = t[0][ 7] * w[0] + t[1][ 7] * w[1];
8067 m[2][0] = t[0][ 8] * w[0] + t[1][ 8] * w[1];
8068 m[2][1] = t[0][ 9] * w[0] + t[1][ 9] * w[1];
8069 m[2][2] = t[0][10] * w[0] + t[1][10] * w[1];
8070 m[2][3] = t[0][11] * w[0] + t[1][11] * w[1];
8074 // modify the vertex
8076 vp[0] = tp[0] * m[0][0] + tp[1] * m[0][1] + tp[2] * m[0][2] + m[0][3];
8077 vp[1] = tp[0] * m[1][0] + tp[1] * m[1][1] + tp[2] * m[1][2] + m[1][3];
8078 vp[2] = tp[0] * m[2][0] + tp[1] * m[2][1] + tp[2] * m[2][2] + m[2][3];
8082 // the normal transformation matrix is a set of cross products...
8083 CrossProduct(m[1], m[2], n[0]);
8084 CrossProduct(m[2], m[0], n[1]);
8085 CrossProduct(m[0], m[1], n[2]); // is actually transpose(inverse(m)) * det(m)
8087 vn[0] = tn[0] * n[0][0] + tn[1] * n[0][1] + tn[2] * n[0][2];
8088 vn[1] = tn[0] * n[1][0] + tn[1] * n[1][1] + tn[2] * n[1][2];
8089 vn[2] = tn[0] * n[2][0] + tn[1] * n[2][1] + tn[2] * n[2][2];
8090 VectorNormalize(vn);
8095 vs[0] = ts[0] * n[0][0] + ts[1] * n[0][1] + ts[2] * n[0][2];
8096 vs[1] = ts[0] * n[1][0] + ts[1] * n[1][1] + ts[2] * n[1][2];
8097 vs[2] = ts[0] * n[2][0] + ts[1] * n[2][1] + ts[2] * n[2][2];
8098 VectorNormalize(vs);
8101 vt[0] = tt[0] * n[0][0] + tt[1] * n[0][1] + tt[2] * n[0][2];
8102 vt[1] = tt[0] * n[1][0] + tt[1] * n[1][1] + tt[2] * n[1][2];
8103 vt[2] = tt[0] * n[2][0] + tt[1] * n[2][1] + tt[2] * n[2][2];
8104 VectorNormalize(vt);
8109 rsurface.batchskeletaltransform3x4 = NULL;
8110 rsurface.batchskeletalnumtransforms = 0;
8113 // q1bsp surfaces rendered in vertex color mode have to have colors
8114 // calculated based on lightstyles
8115 if ((batchneed & BATCHNEED_ARRAY_VERTEXCOLOR) && texturesurfacelist[0]->lightmapinfo)
8117 // generate color arrays for the surfaces in this list
8122 const unsigned char *lm;
8123 rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
8124 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
8125 rsurface.batchlightmapcolor4f_bufferoffset = 0;
8127 for (i = 0;i < texturenumsurfaces;i++)
8129 surface = texturesurfacelist[i];
8130 offsets = rsurface.modellightmapoffsets + surface->num_firstvertex;
8131 surfacenumvertices = surface->num_vertices;
8132 if (surface->lightmapinfo->samples)
8134 for (j = 0;j < surfacenumvertices;j++)
8136 lm = surface->lightmapinfo->samples + offsets[j];
8137 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[0]];
8138 VectorScale(lm, scale, c);
8139 if (surface->lightmapinfo->styles[1] != 255)
8141 size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
8143 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[1]];
8144 VectorMA(c, scale, lm, c);
8145 if (surface->lightmapinfo->styles[2] != 255)
8148 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[2]];
8149 VectorMA(c, scale, lm, c);
8150 if (surface->lightmapinfo->styles[3] != 255)
8153 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[3]];
8154 VectorMA(c, scale, lm, c);
8161 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);
8167 for (j = 0;j < surfacenumvertices;j++)
8169 Vector4Set(rsurface.batchlightmapcolor4f + 4*numvertices, 0, 0, 0, 1);
8176 // if vertices are deformed (sprite flares and things in maps, possibly
8177 // water waves, bulges and other deformations), modify the copied vertices
8179 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
8182 switch (deform->deform)
8185 case Q3DEFORM_PROJECTIONSHADOW:
8186 case Q3DEFORM_TEXT0:
8187 case Q3DEFORM_TEXT1:
8188 case Q3DEFORM_TEXT2:
8189 case Q3DEFORM_TEXT3:
8190 case Q3DEFORM_TEXT4:
8191 case Q3DEFORM_TEXT5:
8192 case Q3DEFORM_TEXT6:
8193 case Q3DEFORM_TEXT7:
8196 case Q3DEFORM_AUTOSPRITE:
8197 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
8198 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
8199 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
8200 VectorNormalize(newforward);
8201 VectorNormalize(newright);
8202 VectorNormalize(newup);
8203 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8204 // rsurface.batchvertex3f_vertexbuffer = NULL;
8205 // rsurface.batchvertex3f_bufferoffset = 0;
8206 // rsurface.batchsvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchsvector3f);
8207 // rsurface.batchsvector3f_vertexbuffer = NULL;
8208 // rsurface.batchsvector3f_bufferoffset = 0;
8209 // rsurface.batchtvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchtvector3f);
8210 // rsurface.batchtvector3f_vertexbuffer = NULL;
8211 // rsurface.batchtvector3f_bufferoffset = 0;
8212 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8213 // rsurface.batchnormal3f_vertexbuffer = NULL;
8214 // rsurface.batchnormal3f_bufferoffset = 0;
8215 // sometimes we're on a renderpath that does not use vectors (GL11/GL13/GLES1)
8216 if (!VectorLength2(rsurface.batchnormal3f + 3*rsurface.batchfirstvertex))
8217 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8218 if (!VectorLength2(rsurface.batchsvector3f + 3*rsurface.batchfirstvertex))
8219 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);
8220 // a single autosprite surface can contain multiple sprites...
8221 for (j = 0;j < batchnumvertices - 3;j += 4)
8223 VectorClear(center);
8224 for (i = 0;i < 4;i++)
8225 VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
8226 VectorScale(center, 0.25f, center);
8227 VectorCopy(rsurface.batchnormal3f + 3*j, forward);
8228 VectorCopy(rsurface.batchsvector3f + 3*j, right);
8229 VectorCopy(rsurface.batchtvector3f + 3*j, up);
8230 for (i = 0;i < 4;i++)
8232 VectorSubtract(rsurface.batchvertex3f + 3*(j+i), center, v);
8233 VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.batchvertex3f + 3*(j+i));
8236 // if we get here, BATCHNEED_ARRAY_NORMAL and BATCHNEED_ARRAY_VECTOR are in batchneed, so no need to check
8237 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8238 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);
8240 case Q3DEFORM_AUTOSPRITE2:
8241 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
8242 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
8243 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
8244 VectorNormalize(newforward);
8245 VectorNormalize(newright);
8246 VectorNormalize(newup);
8247 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8248 // rsurface.batchvertex3f_vertexbuffer = NULL;
8249 // rsurface.batchvertex3f_bufferoffset = 0;
8251 const float *v1, *v2;
8261 memset(shortest, 0, sizeof(shortest));
8262 // a single autosprite surface can contain multiple sprites...
8263 for (j = 0;j < batchnumvertices - 3;j += 4)
8265 VectorClear(center);
8266 for (i = 0;i < 4;i++)
8267 VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
8268 VectorScale(center, 0.25f, center);
8269 // find the two shortest edges, then use them to define the
8270 // axis vectors for rotating around the central axis
8271 for (i = 0;i < 6;i++)
8273 v1 = rsurface.batchvertex3f + 3*(j+quadedges[i][0]);
8274 v2 = rsurface.batchvertex3f + 3*(j+quadedges[i][1]);
8275 l = VectorDistance2(v1, v2);
8276 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
8278 l += (1.0f / 1024.0f);
8279 if (shortest[0].length2 > l || i == 0)
8281 shortest[1] = shortest[0];
8282 shortest[0].length2 = l;
8283 shortest[0].v1 = v1;
8284 shortest[0].v2 = v2;
8286 else if (shortest[1].length2 > l || i == 1)
8288 shortest[1].length2 = l;
8289 shortest[1].v1 = v1;
8290 shortest[1].v2 = v2;
8293 VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
8294 VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
8295 // this calculates the right vector from the shortest edge
8296 // and the up vector from the edge midpoints
8297 VectorSubtract(shortest[0].v1, shortest[0].v2, right);
8298 VectorNormalize(right);
8299 VectorSubtract(end, start, up);
8300 VectorNormalize(up);
8301 // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
8302 VectorSubtract(rsurface.localvieworigin, center, forward);
8303 //Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, forward);
8304 VectorNegate(forward, forward);
8305 VectorReflect(forward, 0, up, forward);
8306 VectorNormalize(forward);
8307 CrossProduct(up, forward, newright);
8308 VectorNormalize(newright);
8309 // rotate the quad around the up axis vector, this is made
8310 // especially easy by the fact we know the quad is flat,
8311 // so we only have to subtract the center position and
8312 // measure distance along the right vector, and then
8313 // multiply that by the newright vector and add back the
8315 // we also need to subtract the old position to undo the
8316 // displacement from the center, which we do with a
8317 // DotProduct, the subtraction/addition of center is also
8318 // optimized into DotProducts here
8319 l = DotProduct(right, center);
8320 for (i = 0;i < 4;i++)
8322 v1 = rsurface.batchvertex3f + 3*(j+i);
8323 f = DotProduct(right, v1) - l;
8324 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.batchvertex3f + 3*(j+i));
8328 if(batchneed & (BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR)) // otherwise these can stay NULL
8330 // rsurface.batchnormal3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8331 // rsurface.batchnormal3f_vertexbuffer = NULL;
8332 // rsurface.batchnormal3f_bufferoffset = 0;
8333 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8335 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8337 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8338 // rsurface.batchsvector3f_vertexbuffer = NULL;
8339 // rsurface.batchsvector3f_bufferoffset = 0;
8340 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8341 // rsurface.batchtvector3f_vertexbuffer = NULL;
8342 // rsurface.batchtvector3f_bufferoffset = 0;
8343 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);
8346 case Q3DEFORM_NORMAL:
8347 // deform the normals to make reflections wavey
8348 rsurface.batchnormal3f = (float *)R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8349 rsurface.batchnormal3f_vertexbuffer = NULL;
8350 rsurface.batchnormal3f_bufferoffset = 0;
8351 for (j = 0;j < batchnumvertices;j++)
8354 float *normal = rsurface.batchnormal3f + 3*j;
8355 VectorScale(rsurface.batchvertex3f + 3*j, 0.98f, vertex);
8356 normal[0] = rsurface.batchnormal3f[j*3+0] + deform->parms[0] * noise4f( vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8357 normal[1] = rsurface.batchnormal3f[j*3+1] + deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8358 normal[2] = rsurface.batchnormal3f[j*3+2] + deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8359 VectorNormalize(normal);
8361 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8363 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8364 // rsurface.batchsvector3f_vertexbuffer = NULL;
8365 // rsurface.batchsvector3f_bufferoffset = 0;
8366 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8367 // rsurface.batchtvector3f_vertexbuffer = NULL;
8368 // rsurface.batchtvector3f_bufferoffset = 0;
8369 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);
8373 // deform vertex array to make wavey water and flags and such
8374 waveparms[0] = deform->waveparms[0];
8375 waveparms[1] = deform->waveparms[1];
8376 waveparms[2] = deform->waveparms[2];
8377 waveparms[3] = deform->waveparms[3];
8378 if(!R_TestQ3WaveFunc(deform->wavefunc, waveparms))
8379 break; // if wavefunc is a nop, don't make a dynamic vertex array
8380 // this is how a divisor of vertex influence on deformation
8381 animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
8382 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
8383 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8384 // rsurface.batchvertex3f_vertexbuffer = NULL;
8385 // rsurface.batchvertex3f_bufferoffset = 0;
8386 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8387 // rsurface.batchnormal3f_vertexbuffer = NULL;
8388 // rsurface.batchnormal3f_bufferoffset = 0;
8389 for (j = 0;j < batchnumvertices;j++)
8391 // if the wavefunc depends on time, evaluate it per-vertex
8394 waveparms[2] = deform->waveparms[2] + (rsurface.batchvertex3f[j*3+0] + rsurface.batchvertex3f[j*3+1] + rsurface.batchvertex3f[j*3+2]) * animpos;
8395 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
8397 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
8399 // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
8400 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8401 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8403 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8404 // rsurface.batchsvector3f_vertexbuffer = NULL;
8405 // rsurface.batchsvector3f_bufferoffset = 0;
8406 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8407 // rsurface.batchtvector3f_vertexbuffer = NULL;
8408 // rsurface.batchtvector3f_bufferoffset = 0;
8409 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);
8412 case Q3DEFORM_BULGE:
8413 // deform vertex array to make the surface have moving bulges
8414 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8415 // rsurface.batchvertex3f_vertexbuffer = NULL;
8416 // rsurface.batchvertex3f_bufferoffset = 0;
8417 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8418 // rsurface.batchnormal3f_vertexbuffer = NULL;
8419 // rsurface.batchnormal3f_bufferoffset = 0;
8420 for (j = 0;j < batchnumvertices;j++)
8422 scale = sin(rsurface.batchtexcoordtexture2f[j*2+0] * deform->parms[0] + rsurface.shadertime * deform->parms[2]) * deform->parms[1];
8423 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
8425 // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
8426 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8427 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8429 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8430 // rsurface.batchsvector3f_vertexbuffer = NULL;
8431 // rsurface.batchsvector3f_bufferoffset = 0;
8432 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8433 // rsurface.batchtvector3f_vertexbuffer = NULL;
8434 // rsurface.batchtvector3f_bufferoffset = 0;
8435 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);
8439 // deform vertex array
8440 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
8441 break; // if wavefunc is a nop, don't make a dynamic vertex array
8442 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
8443 VectorScale(deform->parms, scale, waveparms);
8444 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8445 // rsurface.batchvertex3f_vertexbuffer = NULL;
8446 // rsurface.batchvertex3f_bufferoffset = 0;
8447 for (j = 0;j < batchnumvertices;j++)
8448 VectorAdd(rsurface.batchvertex3f + 3*j, waveparms, rsurface.batchvertex3f + 3*j);
8453 if (rsurface.batchtexcoordtexture2f && rsurface.texture->materialshaderpass)
8455 // generate texcoords based on the chosen texcoord source
8456 switch(rsurface.texture->materialshaderpass->tcgen.tcgen)
8459 case Q3TCGEN_TEXTURE:
8461 case Q3TCGEN_LIGHTMAP:
8462 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8463 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8464 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8465 if (rsurface.batchtexcoordlightmap2f)
8466 memcpy(rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordlightmap2f, batchnumvertices * sizeof(float[2]));
8468 case Q3TCGEN_VECTOR:
8469 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8470 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8471 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8472 for (j = 0;j < batchnumvertices;j++)
8474 rsurface.batchtexcoordtexture2f[j*2+0] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->materialshaderpass->tcgen.parms);
8475 rsurface.batchtexcoordtexture2f[j*2+1] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->materialshaderpass->tcgen.parms + 3);
8478 case Q3TCGEN_ENVIRONMENT:
8479 // make environment reflections using a spheremap
8480 rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8481 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8482 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8483 for (j = 0;j < batchnumvertices;j++)
8485 // identical to Q3A's method, but executed in worldspace so
8486 // carried models can be shiny too
8488 float viewer[3], d, reflected[3], worldreflected[3];
8490 VectorSubtract(rsurface.localvieworigin, rsurface.batchvertex3f + 3*j, viewer);
8491 // VectorNormalize(viewer);
8493 d = DotProduct(rsurface.batchnormal3f + 3*j, viewer);
8495 reflected[0] = rsurface.batchnormal3f[j*3+0]*2*d - viewer[0];
8496 reflected[1] = rsurface.batchnormal3f[j*3+1]*2*d - viewer[1];
8497 reflected[2] = rsurface.batchnormal3f[j*3+2]*2*d - viewer[2];
8498 // note: this is proportinal to viewer, so we can normalize later
8500 Matrix4x4_Transform3x3(&rsurface.matrix, reflected, worldreflected);
8501 VectorNormalize(worldreflected);
8503 // note: this sphere map only uses world x and z!
8504 // so positive and negative y will LOOK THE SAME.
8505 rsurface.batchtexcoordtexture2f[j*2+0] = 0.5 + 0.5 * worldreflected[1];
8506 rsurface.batchtexcoordtexture2f[j*2+1] = 0.5 - 0.5 * worldreflected[2];
8510 // the only tcmod that needs software vertex processing is turbulent, so
8511 // check for it here and apply the changes if needed
8512 // and we only support that as the first one
8513 // (handling a mixture of turbulent and other tcmods would be problematic
8514 // without punting it entirely to a software path)
8515 if (rsurface.texture->materialshaderpass->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
8517 amplitude = rsurface.texture->materialshaderpass->tcmods[0].parms[1];
8518 animpos = rsurface.texture->materialshaderpass->tcmods[0].parms[2] + rsurface.shadertime * rsurface.texture->materialshaderpass->tcmods[0].parms[3];
8519 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8520 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8521 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8522 for (j = 0;j < batchnumvertices;j++)
8524 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);
8525 rsurface.batchtexcoordtexture2f[j*2+1] += amplitude * sin(((rsurface.batchvertex3f[j*3+1] ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
8531 void RSurf_DrawBatch(void)
8533 // sometimes a zero triangle surface (usually a degenerate patch) makes it
8534 // through the pipeline, killing it earlier in the pipeline would have
8535 // per-surface overhead rather than per-batch overhead, so it's best to
8536 // reject it here, before it hits glDraw.
8537 if (rsurface.batchnumtriangles == 0)
8540 // batch debugging code
8541 if (r_test.integer && rsurface.entity == r_refdef.scene.worldentity && rsurface.batchvertex3f == r_refdef.scene.worldentity->model->surfmesh.data_vertex3f)
8547 e = rsurface.batchelement3i + rsurface.batchfirsttriangle*3;
8548 for (i = 0;i < rsurface.batchnumtriangles*3;i++)
8551 for (j = 0;j < rsurface.entity->model->num_surfaces;j++)
8553 if (c >= rsurface.modelsurfaces[j].num_firstvertex && c < (rsurface.modelsurfaces[j].num_firstvertex + rsurface.modelsurfaces[j].num_vertices))
8555 if (rsurface.modelsurfaces[j].texture != rsurface.texture)
8556 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);
8563 if (rsurface.batchmultidraw)
8565 // issue multiple draws rather than copying index data
8566 int numsurfaces = rsurface.batchmultidrawnumsurfaces;
8567 const msurface_t **surfacelist = rsurface.batchmultidrawsurfacelist;
8568 int i, j, k, firstvertex, endvertex, firsttriangle, endtriangle;
8569 for (i = 0;i < numsurfaces;)
8571 // combine consecutive surfaces as one draw
8572 for (k = i, j = i + 1;j < numsurfaces;k = j, j++)
8573 if (surfacelist[j] != surfacelist[k] + 1)
8575 firstvertex = surfacelist[i]->num_firstvertex;
8576 endvertex = surfacelist[k]->num_firstvertex + surfacelist[k]->num_vertices;
8577 firsttriangle = surfacelist[i]->num_firsttriangle;
8578 endtriangle = surfacelist[k]->num_firsttriangle + surfacelist[k]->num_triangles;
8579 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);
8585 // there is only one consecutive run of index data (may have been combined)
8586 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);
8590 static int RSurf_FindWaterPlaneForSurface(const msurface_t *surface)
8592 // pick the closest matching water plane
8593 int planeindex, vertexindex, bestplaneindex = -1;
8597 r_waterstate_waterplane_t *p;
8598 qbool prepared = false;
8600 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
8602 if(p->camera_entity != rsurface.texture->camera_entity)
8607 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX, 1, &surface);
8609 if(rsurface.batchnumvertices == 0)
8612 for (vertexindex = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3;vertexindex < rsurface.batchnumvertices;vertexindex++, v += 3)
8614 Matrix4x4_Transform(&rsurface.matrix, v, vert);
8615 d += fabs(PlaneDiff(vert, &p->plane));
8617 if (bestd > d || bestplaneindex < 0)
8620 bestplaneindex = planeindex;
8623 return bestplaneindex;
8624 // NOTE: this MAY return a totally unrelated water plane; we can ignore
8625 // this situation though, as it might be better to render single larger
8626 // batches with useless stuff (backface culled for example) than to
8627 // render multiple smaller batches
8630 void RSurf_SetupDepthAndCulling(bool ui)
8632 // submodels are biased to avoid z-fighting with world surfaces that they
8633 // may be exactly overlapping (avoids z-fighting artifacts on certain
8634 // doors and things in Quake maps)
8635 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
8636 GL_PolygonOffset(rsurface.basepolygonfactor + rsurface.texture->biaspolygonfactor, rsurface.basepolygonoffset + rsurface.texture->biaspolygonoffset);
8637 GL_DepthTest(!ui && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
8638 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
8641 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8645 float p[3], mins[3], maxs[3];
8647 // transparent sky would be ridiculous
8648 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
8650 R_SetupShader_Generic_NoTexture(false, false);
8651 skyrenderlater = true;
8652 RSurf_SetupDepthAndCulling(false);
8655 // add the vertices of the surfaces to a world bounding box so we can scissor the sky render later
8656 if (r_sky_scissor.integer)
8658 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
8659 for (j = 0, v = rsurface.batchvertex3f + 3 * rsurface.batchfirstvertex; j < rsurface.batchnumvertices; j++, v += 3)
8661 Matrix4x4_Transform(&rsurface.matrix, v, p);
8664 if (mins[0] > p[0]) mins[0] = p[0];
8665 if (mins[1] > p[1]) mins[1] = p[1];
8666 if (mins[2] > p[2]) mins[2] = p[2];
8667 if (maxs[0] < p[0]) maxs[0] = p[0];
8668 if (maxs[1] < p[1]) maxs[1] = p[1];
8669 if (maxs[2] < p[2]) maxs[2] = p[2];
8673 VectorCopy(p, mins);
8674 VectorCopy(p, maxs);
8677 if (!R_ScissorForBBox(mins, maxs, scissor))
8681 if (skyscissor[0] > scissor[0])
8683 skyscissor[2] += skyscissor[0] - scissor[0];
8684 skyscissor[0] = scissor[0];
8686 if (skyscissor[1] > scissor[1])
8688 skyscissor[3] += skyscissor[1] - scissor[1];
8689 skyscissor[1] = scissor[1];
8691 if (skyscissor[0] + skyscissor[2] < scissor[0] + scissor[2])
8692 skyscissor[2] = scissor[0] + scissor[2] - skyscissor[0];
8693 if (skyscissor[1] + skyscissor[3] < scissor[1] + scissor[3])
8694 skyscissor[3] = scissor[1] + scissor[3] - skyscissor[1];
8697 Vector4Copy(scissor, skyscissor);
8701 // LadyHavoc: HalfLife maps have freaky skypolys so don't use
8702 // skymasking on them, and Quake3 never did sky masking (unlike
8703 // software Quake and software Quake2), so disable the sky masking
8704 // in Quake3 maps as it causes problems with q3map2 sky tricks,
8705 // and skymasking also looks very bad when noclipping outside the
8706 // level, so don't use it then either.
8707 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)
8709 R_Mesh_ResetTextureState();
8710 if (skyrendermasked)
8712 R_SetupShader_DepthOrShadow(false, false, false);
8713 // depth-only (masking)
8714 GL_ColorMask(0, 0, 0, 0);
8715 // just to make sure that braindead drivers don't draw
8716 // anything despite that colormask...
8717 GL_BlendFunc(GL_ZERO, GL_ONE);
8718 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8719 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8723 R_SetupShader_Generic_NoTexture(false, false);
8725 GL_BlendFunc(GL_ONE, GL_ZERO);
8726 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
8727 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1);
8728 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
8731 if (skyrendermasked)
8732 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
8734 R_Mesh_ResetTextureState();
8735 GL_Color(1, 1, 1, 1);
8738 extern rtexture_t *r_shadow_prepasslightingdiffusetexture;
8739 extern rtexture_t *r_shadow_prepasslightingspeculartexture;
8740 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool prepass, qbool ui)
8742 if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA)))
8746 // render screenspace normalmap to texture
8748 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_DEFERREDGEOMETRY, texturenumsurfaces, texturesurfacelist, NULL, false, false);
8753 // bind lightmap texture
8755 // water/refraction/reflection/camera surfaces have to be handled specially
8756 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA | MATERIALFLAG_REFLECTION)))
8758 int start, end, startplaneindex;
8759 for (start = 0;start < texturenumsurfaces;start = end)
8761 startplaneindex = RSurf_FindWaterPlaneForSurface(texturesurfacelist[start]);
8762 if(startplaneindex < 0)
8764 // this happens if the plane e.g. got backface culled and thus didn't get a water plane. We can just ignore this.
8765 // Con_Printf("No matching water plane for surface with material flags 0x%08x - PLEASE DEBUG THIS\n", rsurface.texture->currentmaterialflags);
8769 for (end = start + 1;end < texturenumsurfaces && startplaneindex == RSurf_FindWaterPlaneForSurface(texturesurfacelist[end]);end++)
8771 // now that we have a batch using the same planeindex, render it
8772 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA)))
8774 // render water or distortion background
8776 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BACKGROUND, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false, false);
8778 // blend surface on top
8779 GL_DepthMask(false);
8780 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, end-start, texturesurfacelist + start, NULL, false, false);
8783 else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION))
8785 // render surface with reflection texture as input
8786 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
8787 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false, false);
8794 // render surface batch normally
8795 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
8796 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, texturenumsurfaces, texturesurfacelist, NULL, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) != 0 || ui, ui);
8800 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth)
8804 int texturesurfaceindex;
8806 const msurface_t *surface;
8807 float surfacecolor4f[4];
8809 // R_Mesh_ResetTextureState();
8810 R_SetupShader_Generic_NoTexture(false, false);
8812 GL_BlendFunc(GL_ONE, GL_ZERO);
8813 GL_DepthMask(writedepth);
8815 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_ALWAYSCOPY, texturenumsurfaces, texturesurfacelist);
8817 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
8819 surface = texturesurfacelist[texturesurfaceindex];
8820 k = (int)(((size_t)surface) / sizeof(msurface_t));
8821 Vector4Set(surfacecolor4f, (k & 0xF) * (1.0f / 16.0f), (k & 0xF0) * (1.0f / 256.0f), (k & 0xF00) * (1.0f / 4096.0f), 1);
8822 for (j = 0;j < surface->num_vertices;j++)
8824 Vector4Copy(surfacecolor4f, rsurface.batchlightmapcolor4f + 4 * vi);
8828 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchlightmapcolor4f, rsurface.batchtexcoordtexture2f);
8832 static void R_DrawModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool prepass, qbool ui)
8835 RSurf_SetupDepthAndCulling(ui);
8836 if (r_showsurfaces.integer && r_refdef.view.showdebug)
8838 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist, writedepth);
8841 switch (vid.renderpath)
8843 case RENDERPATH_GL32:
8844 case RENDERPATH_GLES2:
8845 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8851 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
8854 int texturenumsurfaces, endsurface;
8856 const msurface_t *surface;
8857 const msurface_t *texturesurfacelist[MESHQUEUE_TRANSPARENT_BATCHSIZE];
8859 RSurf_ActiveModelEntity(ent, true, true, false);
8861 if (r_transparentdepthmasking.integer)
8863 qbool setup = false;
8864 for (i = 0;i < numsurfaces;i = j)
8867 surface = rsurface.modelsurfaces + surfacelist[i];
8868 texture = surface->texture;
8869 rsurface.texture = R_GetCurrentTexture(texture);
8870 rsurface.lightmaptexture = NULL;
8871 rsurface.deluxemaptexture = NULL;
8872 rsurface.uselightmaptexture = false;
8873 // scan ahead until we find a different texture
8874 endsurface = min(i + 1024, numsurfaces);
8875 texturenumsurfaces = 0;
8876 texturesurfacelist[texturenumsurfaces++] = surface;
8877 for (;j < endsurface;j++)
8879 surface = rsurface.modelsurfaces + surfacelist[j];
8880 if (texture != surface->texture)
8882 texturesurfacelist[texturenumsurfaces++] = surface;
8884 if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_TRANSDEPTH))
8886 // render the range of surfaces as depth
8890 GL_ColorMask(0,0,0,0);
8893 GL_BlendFunc(GL_ONE, GL_ZERO);
8895 // R_Mesh_ResetTextureState();
8897 RSurf_SetupDepthAndCulling(false);
8898 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8899 R_SetupShader_DepthOrShadow(false, false, !!rsurface.batchskeletaltransform3x4);
8900 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8904 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
8907 for (i = 0;i < numsurfaces;i = j)
8910 surface = rsurface.modelsurfaces + surfacelist[i];
8911 texture = surface->texture;
8912 rsurface.texture = R_GetCurrentTexture(texture);
8913 // scan ahead until we find a different texture
8914 endsurface = min(i + MESHQUEUE_TRANSPARENT_BATCHSIZE, numsurfaces);
8915 texturenumsurfaces = 0;
8916 texturesurfacelist[texturenumsurfaces++] = surface;
8917 rsurface.lightmaptexture = surface->lightmaptexture;
8918 rsurface.deluxemaptexture = surface->deluxemaptexture;
8919 rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
8920 for (;j < endsurface;j++)
8922 surface = rsurface.modelsurfaces + surfacelist[j];
8923 if (texture != surface->texture || rsurface.lightmaptexture != surface->lightmaptexture)
8925 texturesurfacelist[texturenumsurfaces++] = surface;
8927 // render the range of surfaces
8928 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, false, false, false);
8930 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
8933 static void R_ProcessTransparentTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8935 // transparent surfaces get pushed off into the transparent queue
8936 int surfacelistindex;
8937 const msurface_t *surface;
8938 vec3_t tempcenter, center;
8939 for (surfacelistindex = 0;surfacelistindex < texturenumsurfaces;surfacelistindex++)
8941 surface = texturesurfacelist[surfacelistindex];
8942 if (r_transparent_sortsurfacesbynearest.integer)
8944 tempcenter[0] = bound(surface->mins[0], rsurface.localvieworigin[0], surface->maxs[0]);
8945 tempcenter[1] = bound(surface->mins[1], rsurface.localvieworigin[1], surface->maxs[1]);
8946 tempcenter[2] = bound(surface->mins[2], rsurface.localvieworigin[2], surface->maxs[2]);
8950 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
8951 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
8952 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
8954 Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
8955 if (rsurface.entity->transparent_offset) // transparent offset
8957 center[0] += r_refdef.view.forward[0]*rsurface.entity->transparent_offset;
8958 center[1] += r_refdef.view.forward[1]*rsurface.entity->transparent_offset;
8959 center[2] += r_refdef.view.forward[2]*rsurface.entity->transparent_offset;
8961 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);
8965 static void R_DrawTextureSurfaceList_DepthOnly(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8967 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
8969 if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
8971 RSurf_SetupDepthAndCulling(false);
8972 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8973 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8974 R_SetupShader_DepthOrShadow(false, false, !!rsurface.batchskeletaltransform3x4);
8978 static void R_ProcessModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool depthonly, qbool prepass, qbool ui)
8982 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8984 R_DrawTextureSurfaceList_DepthOnly(texturenumsurfaces, texturesurfacelist);
8987 if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_WALL))
8989 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
8990 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
8992 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8994 else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) && (!r_showsurfaces.integer || r_showsurfaces.integer == 3))
8995 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
8996 else if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_WALL))
8998 else if (((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED) || (r_showsurfaces.integer == 3 && (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST))))
9000 // in the deferred case, transparent surfaces were queued during prepass
9001 if (!r_shadow_usingdeferredprepass)
9002 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
9006 // the alphatest check is to make sure we write depth for anything we skipped on the depth-only pass earlier
9007 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST), prepass, ui);
9012 static void R_QueueModelSurfaceList(entity_render_t *ent, int numsurfaces, const msurface_t **surfacelist, int flagsmask, qbool writedepth, qbool depthonly, qbool prepass, qbool ui)
9016 R_FrameData_SetMark();
9017 // break the surface list down into batches by texture and use of lightmapping
9018 for (i = 0;i < numsurfaces;i = j)
9021 // texture is the base texture pointer, rsurface.texture is the
9022 // current frame/skin the texture is directing us to use (for example
9023 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
9024 // use skin 1 instead)
9025 texture = surfacelist[i]->texture;
9026 rsurface.texture = R_GetCurrentTexture(texture);
9027 if (!(rsurface.texture->currentmaterialflags & flagsmask) || (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW))
9029 // if this texture is not the kind we want, skip ahead to the next one
9030 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
9034 if(depthonly || prepass)
9036 rsurface.lightmaptexture = NULL;
9037 rsurface.deluxemaptexture = NULL;
9038 rsurface.uselightmaptexture = false;
9039 // simply scan ahead until we find a different texture or lightmap state
9040 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
9045 rsurface.lightmaptexture = surfacelist[i]->lightmaptexture;
9046 rsurface.deluxemaptexture = surfacelist[i]->deluxemaptexture;
9047 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
9048 // simply scan ahead until we find a different texture or lightmap state
9049 for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.lightmaptexture == surfacelist[j]->lightmaptexture;j++)
9052 // render the range of surfaces
9053 R_ProcessModelTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly, prepass, ui);
9055 R_FrameData_ReturnToMark();
9058 float locboxvertex3f[6*4*3] =
9060 1,0,1, 1,0,0, 1,1,0, 1,1,1,
9061 0,1,1, 0,1,0, 0,0,0, 0,0,1,
9062 1,1,1, 1,1,0, 0,1,0, 0,1,1,
9063 0,0,1, 0,0,0, 1,0,0, 1,0,1,
9064 0,0,1, 1,0,1, 1,1,1, 0,1,1,
9065 1,0,0, 0,0,0, 0,1,0, 1,1,0
9068 unsigned short locboxelements[6*2*3] =
9078 static void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
9081 cl_locnode_t *loc = (cl_locnode_t *)ent;
9083 float vertex3f[6*4*3];
9085 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9086 GL_DepthMask(false);
9087 GL_DepthRange(0, 1);
9088 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
9090 GL_CullFace(GL_NONE);
9091 R_EntityMatrix(&identitymatrix);
9093 // R_Mesh_ResetTextureState();
9096 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9097 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9098 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9099 surfacelist[0] < 0 ? 0.5f : 0.125f);
9101 if (VectorCompare(loc->mins, loc->maxs))
9103 VectorSet(size, 2, 2, 2);
9104 VectorMA(loc->mins, -0.5f, size, mins);
9108 VectorCopy(loc->mins, mins);
9109 VectorSubtract(loc->maxs, loc->mins, size);
9112 for (i = 0;i < 6*4*3;)
9113 for (j = 0;j < 3;j++, i++)
9114 vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
9116 R_Mesh_PrepareVertices_Generic_Arrays(6*4, vertex3f, NULL, NULL);
9117 R_SetupShader_Generic_NoTexture(false, false);
9118 R_Mesh_Draw(0, 6*4, 0, 6*2, NULL, NULL, 0, locboxelements, NULL, 0);
9121 void R_DrawLocs(void)
9124 cl_locnode_t *loc, *nearestloc;
9126 nearestloc = CL_Locs_FindNearest(cl.movement_origin);
9127 for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
9129 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
9130 R_MeshQueue_AddTransparent(TRANSPARENTSORT_DISTANCE, center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
9134 void R_DecalSystem_Reset(decalsystem_t *decalsystem)
9136 if (decalsystem->decals)
9137 Mem_Free(decalsystem->decals);
9138 memset(decalsystem, 0, sizeof(*decalsystem));
9141 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)
9147 // expand or initialize the system
9148 if (decalsystem->maxdecals <= decalsystem->numdecals)
9150 decalsystem_t old = *decalsystem;
9151 qbool useshortelements;
9152 decalsystem->maxdecals = max(16, decalsystem->maxdecals * 2);
9153 useshortelements = decalsystem->maxdecals * 3 <= 65536;
9154 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)));
9155 decalsystem->color4f = (float *)(decalsystem->decals + decalsystem->maxdecals);
9156 decalsystem->texcoord2f = (float *)(decalsystem->color4f + decalsystem->maxdecals*12);
9157 decalsystem->vertex3f = (float *)(decalsystem->texcoord2f + decalsystem->maxdecals*6);
9158 decalsystem->element3i = (int *)(decalsystem->vertex3f + decalsystem->maxdecals*9);
9159 decalsystem->element3s = (useshortelements ? ((unsigned short *)(decalsystem->element3i + decalsystem->maxdecals*3)) : NULL);
9160 if (decalsystem->numdecals)
9161 memcpy(decalsystem->decals, old.decals, decalsystem->numdecals * sizeof(tridecal_t));
9163 Mem_Free(old.decals);
9164 for (i = 0;i < decalsystem->maxdecals*3;i++)
9165 decalsystem->element3i[i] = i;
9166 if (useshortelements)
9167 for (i = 0;i < decalsystem->maxdecals*3;i++)
9168 decalsystem->element3s[i] = i;
9171 // grab a decal and search for another free slot for the next one
9172 decals = decalsystem->decals;
9173 decal = decalsystem->decals + (i = decalsystem->freedecal++);
9174 for (i = decalsystem->freedecal;i < decalsystem->numdecals && decals[i].color4f[0][3];i++)
9176 decalsystem->freedecal = i;
9177 if (decalsystem->numdecals <= i)
9178 decalsystem->numdecals = i + 1;
9180 // initialize the decal
9182 decal->triangleindex = triangleindex;
9183 decal->surfaceindex = surfaceindex;
9184 decal->decalsequence = decalsequence;
9185 decal->color4f[0][0] = c0[0];
9186 decal->color4f[0][1] = c0[1];
9187 decal->color4f[0][2] = c0[2];
9188 decal->color4f[0][3] = 1;
9189 decal->color4f[1][0] = c1[0];
9190 decal->color4f[1][1] = c1[1];
9191 decal->color4f[1][2] = c1[2];
9192 decal->color4f[1][3] = 1;
9193 decal->color4f[2][0] = c2[0];
9194 decal->color4f[2][1] = c2[1];
9195 decal->color4f[2][2] = c2[2];
9196 decal->color4f[2][3] = 1;
9197 decal->vertex3f[0][0] = v0[0];
9198 decal->vertex3f[0][1] = v0[1];
9199 decal->vertex3f[0][2] = v0[2];
9200 decal->vertex3f[1][0] = v1[0];
9201 decal->vertex3f[1][1] = v1[1];
9202 decal->vertex3f[1][2] = v1[2];
9203 decal->vertex3f[2][0] = v2[0];
9204 decal->vertex3f[2][1] = v2[1];
9205 decal->vertex3f[2][2] = v2[2];
9206 decal->texcoord2f[0][0] = t0[0];
9207 decal->texcoord2f[0][1] = t0[1];
9208 decal->texcoord2f[1][0] = t1[0];
9209 decal->texcoord2f[1][1] = t1[1];
9210 decal->texcoord2f[2][0] = t2[0];
9211 decal->texcoord2f[2][1] = t2[1];
9212 TriangleNormal(v0, v1, v2, decal->plane);
9213 VectorNormalize(decal->plane);
9214 decal->plane[3] = DotProduct(v0, decal->plane);
9217 extern cvar_t cl_decals_bias;
9218 extern cvar_t cl_decals_models;
9219 extern cvar_t cl_decals_newsystem_intensitymultiplier;
9220 // baseparms, parms, temps
9221 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)
9226 const float *vertex3f;
9227 const float *normal3f;
9229 float points[2][9][3];
9236 e = rsurface.modelelement3i + 3*triangleindex;
9238 vertex3f = rsurface.modelvertex3f;
9239 normal3f = rsurface.modelnormal3f;
9243 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9245 index = 3*e[cornerindex];
9246 VectorMA(vertex3f + index, cl_decals_bias.value, normal3f + index, v[cornerindex]);
9251 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9253 index = 3*e[cornerindex];
9254 VectorCopy(vertex3f + index, v[cornerindex]);
9259 //TriangleNormal(v[0], v[1], v[2], normal);
9260 //if (DotProduct(normal, localnormal) < 0.0f)
9262 // clip by each of the box planes formed from the projection matrix
9263 // if anything survives, we emit the decal
9264 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]);
9267 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]);
9270 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]);
9273 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]);
9276 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]);
9279 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]);
9282 // some part of the triangle survived, so we have to accept it...
9285 // dynamic always uses the original triangle
9287 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9289 index = 3*e[cornerindex];
9290 VectorCopy(vertex3f + index, v[cornerindex]);
9293 for (cornerindex = 0;cornerindex < numpoints;cornerindex++)
9295 // convert vertex positions to texcoords
9296 Matrix4x4_Transform(projection, v[cornerindex], temp);
9297 tc[cornerindex][0] = (temp[1]+1.0f)*0.5f * (s2-s1) + s1;
9298 tc[cornerindex][1] = (temp[2]+1.0f)*0.5f * (t2-t1) + t1;
9299 // calculate distance fade from the projection origin
9300 f = a * (1.0f-fabs(temp[0])) * cl_decals_newsystem_intensitymultiplier.value;
9301 f = bound(0.0f, f, 1.0f);
9302 c[cornerindex][0] = r * f;
9303 c[cornerindex][1] = g * f;
9304 c[cornerindex][2] = b * f;
9305 c[cornerindex][3] = 1.0f;
9306 //VectorMA(v[cornerindex], cl_decals_bias.value, localnormal, v[cornerindex]);
9309 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);
9311 for (cornerindex = 0;cornerindex < numpoints-2;cornerindex++)
9312 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);
9314 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)
9316 matrix4x4_t projection;
9317 decalsystem_t *decalsystem;
9320 const msurface_t *surface;
9321 const msurface_t *surfaces;
9322 const texture_t *texture;
9326 float localorigin[3];
9327 float localnormal[3];
9335 int bih_triangles_count;
9336 int bih_triangles[256];
9337 int bih_surfaces[256];
9339 decalsystem = &ent->decalsystem;
9341 if (!model || !ent->allowdecals || ent->alpha < 1 || (ent->flags & (RENDER_ADDITIVE | RENDER_NODEPTHTEST)))
9343 R_DecalSystem_Reset(&ent->decalsystem);
9347 if (!model->brush.data_leafs && !cl_decals_models.integer)
9349 if (decalsystem->model)
9350 R_DecalSystem_Reset(decalsystem);
9354 if (decalsystem->model != model)
9355 R_DecalSystem_Reset(decalsystem);
9356 decalsystem->model = model;
9358 RSurf_ActiveModelEntity(ent, true, false, false);
9360 Matrix4x4_Transform(&rsurface.inversematrix, worldorigin, localorigin);
9361 Matrix4x4_Transform3x3(&rsurface.inversematrix, worldnormal, localnormal);
9362 VectorNormalize(localnormal);
9363 localsize = worldsize*rsurface.inversematrixscale;
9364 localmins[0] = localorigin[0] - localsize;
9365 localmins[1] = localorigin[1] - localsize;
9366 localmins[2] = localorigin[2] - localsize;
9367 localmaxs[0] = localorigin[0] + localsize;
9368 localmaxs[1] = localorigin[1] + localsize;
9369 localmaxs[2] = localorigin[2] + localsize;
9371 //VectorCopy(localnormal, planes[4]);
9372 //VectorVectors(planes[4], planes[2], planes[0]);
9373 AnglesFromVectors(angles, localnormal, NULL, false);
9374 AngleVectors(angles, planes[0], planes[2], planes[4]);
9375 VectorNegate(planes[0], planes[1]);
9376 VectorNegate(planes[2], planes[3]);
9377 VectorNegate(planes[4], planes[5]);
9378 planes[0][3] = DotProduct(planes[0], localorigin) - localsize;
9379 planes[1][3] = DotProduct(planes[1], localorigin) - localsize;
9380 planes[2][3] = DotProduct(planes[2], localorigin) - localsize;
9381 planes[3][3] = DotProduct(planes[3], localorigin) - localsize;
9382 planes[4][3] = DotProduct(planes[4], localorigin) - localsize;
9383 planes[5][3] = DotProduct(planes[5], localorigin) - localsize;
9388 matrix4x4_t forwardprojection;
9389 Matrix4x4_CreateFromQuakeEntity(&forwardprojection, localorigin[0], localorigin[1], localorigin[2], angles[0], angles[1], angles[2], localsize);
9390 Matrix4x4_Invert_Simple(&projection, &forwardprojection);
9395 float projectionvector[4][3];
9396 VectorScale(planes[0], ilocalsize, projectionvector[0]);
9397 VectorScale(planes[2], ilocalsize, projectionvector[1]);
9398 VectorScale(planes[4], ilocalsize, projectionvector[2]);
9399 projectionvector[0][0] = planes[0][0] * ilocalsize;
9400 projectionvector[0][1] = planes[1][0] * ilocalsize;
9401 projectionvector[0][2] = planes[2][0] * ilocalsize;
9402 projectionvector[1][0] = planes[0][1] * ilocalsize;
9403 projectionvector[1][1] = planes[1][1] * ilocalsize;
9404 projectionvector[1][2] = planes[2][1] * ilocalsize;
9405 projectionvector[2][0] = planes[0][2] * ilocalsize;
9406 projectionvector[2][1] = planes[1][2] * ilocalsize;
9407 projectionvector[2][2] = planes[2][2] * ilocalsize;
9408 projectionvector[3][0] = -(localorigin[0]*projectionvector[0][0]+localorigin[1]*projectionvector[1][0]+localorigin[2]*projectionvector[2][0]);
9409 projectionvector[3][1] = -(localorigin[0]*projectionvector[0][1]+localorigin[1]*projectionvector[1][1]+localorigin[2]*projectionvector[2][1]);
9410 projectionvector[3][2] = -(localorigin[0]*projectionvector[0][2]+localorigin[1]*projectionvector[1][2]+localorigin[2]*projectionvector[2][2]);
9411 Matrix4x4_FromVectors(&projection, projectionvector[0], projectionvector[1], projectionvector[2], projectionvector[3]);
9415 dynamic = model->surfmesh.isanimated;
9416 surfaces = model->data_surfaces;
9419 bih_triangles_count = -1;
9422 if(model->render_bih.numleafs)
9423 bih = &model->render_bih;
9424 else if(model->collision_bih.numleafs)
9425 bih = &model->collision_bih;
9428 bih_triangles_count = BIH_GetTriangleListForBox(bih, sizeof(bih_triangles) / sizeof(*bih_triangles), bih_triangles, bih_surfaces, localmins, localmaxs);
9429 if(bih_triangles_count == 0)
9431 if(bih_triangles_count > (int) (sizeof(bih_triangles) / sizeof(*bih_triangles))) // hit too many, likely bad anyway
9433 if(bih_triangles_count > 0)
9435 for (triangleindex = 0; triangleindex < bih_triangles_count; ++triangleindex)
9437 surfaceindex = bih_surfaces[triangleindex];
9438 surface = surfaces + surfaceindex;
9439 texture = surface->texture;
9442 if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
9444 if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
9446 R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, bih_triangles[triangleindex], surfaceindex);
9451 for (surfaceindex = model->submodelsurfaces_start;surfaceindex < model->submodelsurfaces_end;surfaceindex++)
9453 surface = surfaces + surfaceindex;
9454 // check cull box first because it rejects more than any other check
9455 if (!dynamic && !BoxesOverlap(surface->mins, surface->maxs, localmins, localmaxs))
9457 // skip transparent surfaces
9458 texture = surface->texture;
9461 if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
9463 if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
9465 numtriangles = surface->num_triangles;
9466 for (triangleindex = 0; triangleindex < numtriangles; triangleindex++)
9467 R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, triangleindex + surface->num_firsttriangle, surfaceindex);
9472 // do not call this outside of rendering code - use R_DecalSystem_SplatEntities instead
9473 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)
9475 int renderentityindex;
9478 entity_render_t *ent;
9480 worldmins[0] = worldorigin[0] - worldsize;
9481 worldmins[1] = worldorigin[1] - worldsize;
9482 worldmins[2] = worldorigin[2] - worldsize;
9483 worldmaxs[0] = worldorigin[0] + worldsize;
9484 worldmaxs[1] = worldorigin[1] + worldsize;
9485 worldmaxs[2] = worldorigin[2] + worldsize;
9487 R_DecalSystem_SplatEntity(r_refdef.scene.worldentity, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
9489 for (renderentityindex = 0;renderentityindex < r_refdef.scene.numentities;renderentityindex++)
9491 ent = r_refdef.scene.entities[renderentityindex];
9492 if (!BoxesOverlap(ent->mins, ent->maxs, worldmins, worldmaxs))
9495 R_DecalSystem_SplatEntity(ent, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
9499 typedef struct r_decalsystem_splatqueue_s
9506 unsigned int decalsequence;
9508 r_decalsystem_splatqueue_t;
9510 int r_decalsystem_numqueued = 0;
9511 r_decalsystem_splatqueue_t r_decalsystem_queue[MAX_DECALSYSTEM_QUEUE];
9513 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)
9515 r_decalsystem_splatqueue_t *queue;
9517 if (r_decalsystem_numqueued == MAX_DECALSYSTEM_QUEUE)
9520 queue = &r_decalsystem_queue[r_decalsystem_numqueued++];
9521 VectorCopy(worldorigin, queue->worldorigin);
9522 VectorCopy(worldnormal, queue->worldnormal);
9523 Vector4Set(queue->color, r, g, b, a);
9524 Vector4Set(queue->tcrange, s1, t1, s2, t2);
9525 queue->worldsize = worldsize;
9526 queue->decalsequence = cl.decalsequence++;
9529 static void R_DecalSystem_ApplySplatEntitiesQueue(void)
9532 r_decalsystem_splatqueue_t *queue;
9534 for (i = 0, queue = r_decalsystem_queue;i < r_decalsystem_numqueued;i++, queue++)
9535 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);
9536 r_decalsystem_numqueued = 0;
9539 extern cvar_t cl_decals_max;
9540 static void R_DrawModelDecals_FadeEntity(entity_render_t *ent)
9543 decalsystem_t *decalsystem = &ent->decalsystem;
9545 unsigned int killsequence;
9550 if (!decalsystem->numdecals)
9553 if (r_showsurfaces.integer)
9556 if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
9558 R_DecalSystem_Reset(decalsystem);
9562 killsequence = cl.decalsequence - bound(1, (unsigned int) cl_decals_max.integer, cl.decalsequence);
9563 lifetime = cl_decals_time.value + cl_decals_fadetime.value;
9565 if (decalsystem->lastupdatetime)
9566 frametime = (r_refdef.scene.time - decalsystem->lastupdatetime);
9569 decalsystem->lastupdatetime = r_refdef.scene.time;
9570 numdecals = decalsystem->numdecals;
9572 for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
9574 if (decal->color4f[0][3])
9576 decal->lived += frametime;
9577 if (killsequence > decal->decalsequence || decal->lived >= lifetime)
9579 memset(decal, 0, sizeof(*decal));
9580 if (decalsystem->freedecal > i)
9581 decalsystem->freedecal = i;
9585 decal = decalsystem->decals;
9586 while (numdecals > 0 && !decal[numdecals-1].color4f[0][3])
9589 // collapse the array by shuffling the tail decals into the gaps
9592 while (decalsystem->freedecal < numdecals && decal[decalsystem->freedecal].color4f[0][3])
9593 decalsystem->freedecal++;
9594 if (decalsystem->freedecal == numdecals)
9596 decal[decalsystem->freedecal] = decal[--numdecals];
9599 decalsystem->numdecals = numdecals;
9603 // if there are no decals left, reset decalsystem
9604 R_DecalSystem_Reset(decalsystem);
9608 extern skinframe_t *decalskinframe;
9609 static void R_DrawModelDecals_Entity(entity_render_t *ent)
9612 decalsystem_t *decalsystem = &ent->decalsystem;
9621 const unsigned char *surfacevisible = ent == r_refdef.scene.worldentity ? r_refdef.viewcache.world_surfacevisible : NULL;
9624 numdecals = decalsystem->numdecals;
9628 if (r_showsurfaces.integer)
9631 if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
9633 R_DecalSystem_Reset(decalsystem);
9637 // if the model is static it doesn't matter what value we give for
9638 // wantnormals and wanttangents, so this logic uses only rules applicable
9639 // to a model, knowing that they are meaningless otherwise
9640 RSurf_ActiveModelEntity(ent, false, false, false);
9642 decalsystem->lastupdatetime = r_refdef.scene.time;
9644 faderate = 1.0f / max(0.001f, cl_decals_fadetime.value);
9646 // update vertex positions for animated models
9647 v3f = decalsystem->vertex3f;
9648 c4f = decalsystem->color4f;
9649 t2f = decalsystem->texcoord2f;
9650 for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
9652 if (!decal->color4f[0][3])
9655 if (surfacevisible && !surfacevisible[decal->surfaceindex])
9659 if (decal->triangleindex < 0 && DotProduct(r_refdef.view.origin, decal->plane) < decal->plane[3])
9662 // update color values for fading decals
9663 if (decal->lived >= cl_decals_time.value)
9664 alpha = 1 - faderate * (decal->lived - cl_decals_time.value);
9668 c4f[ 0] = decal->color4f[0][0] * alpha;
9669 c4f[ 1] = decal->color4f[0][1] * alpha;
9670 c4f[ 2] = decal->color4f[0][2] * alpha;
9672 c4f[ 4] = decal->color4f[1][0] * alpha;
9673 c4f[ 5] = decal->color4f[1][1] * alpha;
9674 c4f[ 6] = decal->color4f[1][2] * alpha;
9676 c4f[ 8] = decal->color4f[2][0] * alpha;
9677 c4f[ 9] = decal->color4f[2][1] * alpha;
9678 c4f[10] = decal->color4f[2][2] * alpha;
9681 t2f[0] = decal->texcoord2f[0][0];
9682 t2f[1] = decal->texcoord2f[0][1];
9683 t2f[2] = decal->texcoord2f[1][0];
9684 t2f[3] = decal->texcoord2f[1][1];
9685 t2f[4] = decal->texcoord2f[2][0];
9686 t2f[5] = decal->texcoord2f[2][1];
9688 // update vertex positions for animated models
9689 if (decal->triangleindex >= 0 && decal->triangleindex < rsurface.modelnumtriangles)
9691 e = rsurface.modelelement3i + 3*decal->triangleindex;
9692 VectorCopy(rsurface.modelvertex3f + 3*e[0], v3f);
9693 VectorCopy(rsurface.modelvertex3f + 3*e[1], v3f + 3);
9694 VectorCopy(rsurface.modelvertex3f + 3*e[2], v3f + 6);
9698 VectorCopy(decal->vertex3f[0], v3f);
9699 VectorCopy(decal->vertex3f[1], v3f + 3);
9700 VectorCopy(decal->vertex3f[2], v3f + 6);
9703 if (r_refdef.fogenabled)
9705 alpha = RSurf_FogVertex(v3f);
9706 VectorScale(c4f, alpha, c4f);
9707 alpha = RSurf_FogVertex(v3f + 3);
9708 VectorScale(c4f + 4, alpha, c4f + 4);
9709 alpha = RSurf_FogVertex(v3f + 6);
9710 VectorScale(c4f + 8, alpha, c4f + 8);
9721 r_refdef.stats[r_stat_drawndecals] += numtris;
9723 // now render the decals all at once
9724 // (this assumes they all use one particle font texture!)
9725 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);
9726 // R_Mesh_ResetTextureState();
9727 R_Mesh_PrepareVertices_Generic_Arrays(numtris * 3, decalsystem->vertex3f, decalsystem->color4f, decalsystem->texcoord2f);
9728 GL_DepthMask(false);
9729 GL_DepthRange(0, 1);
9730 GL_PolygonOffset(rsurface.basepolygonfactor + r_polygonoffset_decals_factor.value, rsurface.basepolygonoffset + r_polygonoffset_decals_offset.value);
9732 GL_CullFace(GL_NONE);
9733 GL_BlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
9734 R_SetupShader_Generic(decalskinframe->base, false, false, false);
9735 R_Mesh_Draw(0, numtris * 3, 0, numtris, decalsystem->element3i, NULL, 0, decalsystem->element3s, NULL, 0);
9739 static void R_DrawModelDecals(void)
9743 // fade faster when there are too many decals
9744 numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
9745 for (i = 0;i < r_refdef.scene.numentities;i++)
9746 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
9748 R_DrawModelDecals_FadeEntity(r_refdef.scene.worldentity);
9749 for (i = 0;i < r_refdef.scene.numentities;i++)
9750 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
9751 R_DrawModelDecals_FadeEntity(r_refdef.scene.entities[i]);
9753 R_DecalSystem_ApplySplatEntitiesQueue();
9755 numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
9756 for (i = 0;i < r_refdef.scene.numentities;i++)
9757 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
9759 r_refdef.stats[r_stat_totaldecals] += numdecals;
9761 if (r_showsurfaces.integer || !r_drawdecals.integer)
9764 R_DrawModelDecals_Entity(r_refdef.scene.worldentity);
9766 for (i = 0;i < r_refdef.scene.numentities;i++)
9768 if (!r_refdef.viewcache.entityvisible[i])
9770 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
9771 R_DrawModelDecals_Entity(r_refdef.scene.entities[i]);
9775 static void R_DrawDebugModel(void)
9777 entity_render_t *ent = rsurface.entity;
9779 const msurface_t *surface;
9780 model_t *model = ent->model;
9782 if (!sv.active && !cls.demoplayback && ent != r_refdef.scene.worldentity)
9785 if (r_showoverdraw.value > 0)
9787 float c = r_refdef.view.colorscale * r_showoverdraw.value * 0.125f;
9788 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
9789 R_SetupShader_Generic_NoTexture(false, false);
9790 GL_DepthTest(false);
9791 GL_DepthMask(false);
9792 GL_DepthRange(0, 1);
9793 GL_BlendFunc(GL_ONE, GL_ONE);
9794 for (j = model->submodelsurfaces_start;j < model->submodelsurfaces_end;j++)
9796 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9798 surface = model->data_surfaces + j;
9799 rsurface.texture = R_GetCurrentTexture(surface->texture);
9800 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9802 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, 1, &surface);
9803 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
9804 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
9805 GL_Color(c, 0, 0, 1.0f);
9806 else if (ent == r_refdef.scene.worldentity)
9807 GL_Color(c, c, c, 1.0f);
9809 GL_Color(0, c, 0, 1.0f);
9810 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
9814 rsurface.texture = NULL;
9817 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
9819 // R_Mesh_ResetTextureState();
9820 R_SetupShader_Generic_NoTexture(false, false);
9821 GL_DepthRange(0, 1);
9822 GL_DepthTest(!r_showdisabledepthtest.integer);
9823 GL_DepthMask(false);
9824 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9826 if (r_showcollisionbrushes.value > 0 && model->collision_bih.numleafs)
9830 qbool cullbox = false;
9831 const q3mbrush_t *brush;
9832 const bih_t *bih = &model->collision_bih;
9833 const bih_leaf_t *bihleaf;
9834 float vertex3f[3][3];
9835 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
9836 for (bihleafindex = 0, bihleaf = bih->leafs;bihleafindex < bih->numleafs;bihleafindex++, bihleaf++)
9838 if (cullbox && R_CullFrustum(bihleaf->mins, bihleaf->maxs))
9840 switch (bihleaf->type)
9843 brush = model->brush.data_brushes + bihleaf->itemindex;
9844 if (brush->colbrushf && brush->colbrushf->numtriangles)
9846 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);
9847 R_Mesh_PrepareVertices_Generic_Arrays(brush->colbrushf->numpoints, brush->colbrushf->points->v, NULL, NULL);
9848 R_Mesh_Draw(0, brush->colbrushf->numpoints, 0, brush->colbrushf->numtriangles, brush->colbrushf->elements, NULL, 0, NULL, NULL, 0);
9851 case BIH_COLLISIONTRIANGLE:
9852 triangleindex = bihleaf->itemindex;
9853 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+0], vertex3f[0]);
9854 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+1], vertex3f[1]);
9855 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+2], vertex3f[2]);
9856 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);
9857 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
9858 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
9860 case BIH_RENDERTRIANGLE:
9861 triangleindex = bihleaf->itemindex;
9862 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+0], vertex3f[0]);
9863 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+1], vertex3f[1]);
9864 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+2], vertex3f[2]);
9865 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);
9866 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
9867 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
9873 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
9876 if (r_showtris.value > 0 && qglPolygonMode)
9878 if (r_showdisabledepthtest.integer)
9880 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9881 GL_DepthMask(false);
9885 GL_BlendFunc(GL_ONE, GL_ZERO);
9888 qglPolygonMode(GL_FRONT_AND_BACK, GL_LINE);CHECKGLERROR
9889 for (j = model->submodelsurfaces_start; j < model->submodelsurfaces_end; j++)
9891 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9893 surface = model->data_surfaces + j;
9894 rsurface.texture = R_GetCurrentTexture(surface->texture);
9895 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9897 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
9898 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
9899 GL_Color(r_refdef.view.colorscale, 0, 0, r_showtris.value);
9900 else if (ent == r_refdef.scene.worldentity)
9901 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, r_showtris.value);
9903 GL_Color(0, r_refdef.view.colorscale, 0, r_showtris.value);
9904 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
9908 qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);CHECKGLERROR
9909 rsurface.texture = NULL;
9913 // FIXME! implement r_shownormals with just triangles
9914 if (r_shownormals.value != 0 && qglBegin)
9918 if (r_showdisabledepthtest.integer)
9920 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9921 GL_DepthMask(false);
9925 GL_BlendFunc(GL_ONE, GL_ZERO);
9928 for (j = model->submodelsurfaces_start; j < model->submodelsurfaces_end; j++)
9930 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9932 surface = model->data_surfaces + j;
9933 rsurface.texture = R_GetCurrentTexture(surface->texture);
9934 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9936 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
9938 if (r_shownormals.value < 0 && rsurface.batchnormal3f)
9940 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9942 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9943 GL_Color(0, 0, r_refdef.view.colorscale, 1);
9944 qglVertex3f(v[0], v[1], v[2]);
9945 VectorMA(v, -r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
9946 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9947 qglVertex3f(v[0], v[1], v[2]);
9950 if (r_shownormals.value > 0 && rsurface.batchsvector3f)
9952 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9954 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9955 GL_Color(r_refdef.view.colorscale, 0, 0, 1);
9956 qglVertex3f(v[0], v[1], v[2]);
9957 VectorMA(v, r_shownormals.value, rsurface.batchsvector3f + l * 3, v);
9958 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9959 qglVertex3f(v[0], v[1], v[2]);
9962 if (r_shownormals.value > 0 && rsurface.batchtvector3f)
9964 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9966 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9967 GL_Color(0, r_refdef.view.colorscale, 0, 1);
9968 qglVertex3f(v[0], v[1], v[2]);
9969 VectorMA(v, r_shownormals.value, rsurface.batchtvector3f + l * 3, v);
9970 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9971 qglVertex3f(v[0], v[1], v[2]);
9974 if (r_shownormals.value > 0 && rsurface.batchnormal3f)
9976 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9978 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9979 GL_Color(0, 0, r_refdef.view.colorscale, 1);
9980 qglVertex3f(v[0], v[1], v[2]);
9981 VectorMA(v, r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
9982 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9983 qglVertex3f(v[0], v[1], v[2]);
9990 rsurface.texture = NULL;
9996 int r_maxsurfacelist = 0;
9997 const msurface_t **r_surfacelist = NULL;
9998 void R_DrawModelSurfaces(entity_render_t *ent, qbool skysurfaces, qbool writedepth, qbool depthonly, qbool debug, qbool prepass, qbool ui)
10000 int i, j, flagsmask;
10001 model_t *model = ent->model;
10002 msurface_t *surfaces;
10003 unsigned char *update;
10004 int numsurfacelist = 0;
10008 if (r_maxsurfacelist < model->num_surfaces)
10010 r_maxsurfacelist = model->num_surfaces;
10012 Mem_Free((msurface_t **)r_surfacelist);
10013 r_surfacelist = (const msurface_t **) Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
10016 if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
10017 RSurf_ActiveModelEntity(ent, false, false, false);
10019 RSurf_ActiveModelEntity(ent, true, true, true);
10020 else if (depthonly)
10021 RSurf_ActiveModelEntity(ent, model->wantnormals, model->wanttangents, false);
10023 RSurf_ActiveModelEntity(ent, true, true, false);
10025 surfaces = model->data_surfaces;
10026 update = model->brushq1.lightmapupdateflags;
10028 flagsmask = skysurfaces ? MATERIALFLAG_SKY : MATERIALFLAG_WALL;
10032 R_DrawDebugModel();
10033 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
10037 // check if this is an empty model
10038 if (model->submodelsurfaces_start >= model->submodelsurfaces_end)
10041 rsurface.lightmaptexture = NULL;
10042 rsurface.deluxemaptexture = NULL;
10043 rsurface.uselightmaptexture = false;
10044 rsurface.texture = NULL;
10045 rsurface.rtlight = NULL;
10046 numsurfacelist = 0;
10048 // add visible surfaces to draw list
10049 if (ent == r_refdef.scene.worldentity)
10051 // for the world entity, check surfacevisible
10052 for (i = model->submodelsurfaces_start;i < model->submodelsurfaces_end;i++)
10054 j = model->modelsurfaces_sorted[i];
10055 if (r_refdef.viewcache.world_surfacevisible[j])
10056 r_surfacelist[numsurfacelist++] = surfaces + j;
10059 // don't do anything if there were no surfaces added (none of the world entity is visible)
10060 if (!numsurfacelist)
10062 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
10068 // for ui we have to preserve the order of surfaces (not using modelsurfaces_sorted)
10069 for (i = model->submodelsurfaces_start; i < model->submodelsurfaces_end; i++)
10070 r_surfacelist[numsurfacelist++] = surfaces + i;
10074 // add all surfaces
10075 for (i = model->submodelsurfaces_start; i < model->submodelsurfaces_end; i++)
10076 r_surfacelist[numsurfacelist++] = surfaces + model->modelsurfaces_sorted[i];
10080 * Mark lightmaps as dirty if their lightstyle's value changed. We do this by
10081 * using style chains because most styles do not change on most frames, and most
10082 * surfaces do not have styles on them. Mods like Arcane Dimensions (e.g. ad_necrokeep)
10083 * break this rule and animate most surfaces.
10085 if (update && !skysurfaces && !depthonly && !prepass && model->brushq1.num_lightstyles && r_refdef.scene.lightmapintensity > 0 && r_q1bsp_lightmap_updates_enabled.integer)
10087 model_brush_lightstyleinfo_t *style;
10089 // For each lightstyle, check if its value changed and mark the lightmaps as dirty if so
10090 for (i = 0, style = model->brushq1.data_lightstyleinfo; i < model->brushq1.num_lightstyles; i++, style++)
10092 if (style->value != r_refdef.scene.lightstylevalue[style->style])
10094 int* list = style->surfacelist;
10095 style->value = r_refdef.scene.lightstylevalue[style->style];
10096 // Value changed - mark the surfaces belonging to this style chain as dirty
10097 for (j = 0; j < style->numsurfaces; j++)
10098 update[list[j]] = true;
10101 // Now check if update flags are set on any surfaces that are visible
10102 if (r_q1bsp_lightmap_updates_hidden_surfaces.integer)
10105 * We can do less frequent texture uploads (approximately 10hz for animated
10106 * lightstyles) by rebuilding lightmaps on surfaces that are not currently visible.
10107 * For optimal efficiency, this includes the submodels of the worldmodel, so we
10108 * use model->num_surfaces, not nummodelsurfaces.
10110 for (i = 0; i < model->num_surfaces;i++)
10112 R_BuildLightMap(ent, surfaces + i, r_q1bsp_lightmap_updates_combine.integer);
10116 for (i = 0; i < numsurfacelist; i++)
10117 if (update[r_surfacelist[i] - surfaces])
10118 R_BuildLightMap(ent, (msurface_t *)r_surfacelist[i], r_q1bsp_lightmap_updates_combine.integer);
10122 R_QueueModelSurfaceList(ent, numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly, prepass, ui);
10124 // add to stats if desired
10125 if (r_speeds.integer && !skysurfaces && !depthonly)
10127 r_refdef.stats[r_stat_entities_surfaces] += numsurfacelist;
10128 for (j = 0;j < numsurfacelist;j++)
10129 r_refdef.stats[r_stat_entities_triangles] += r_surfacelist[j]->num_triangles;
10132 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
10135 void R_DebugLine(vec3_t start, vec3_t end)
10137 model_t *mod = CL_Mesh_UI();
10139 int e0, e1, e2, e3;
10140 float offsetx, offsety, x1, y1, x2, y2, width = 1.0f;
10141 float r1 = 1.0f, g1 = 0.0f, b1 = 0.0f, alpha1 = 0.25f;
10142 float r2 = 1.0f, g2 = 1.0f, b2 = 0.0f, alpha2 = 0.25f;
10145 // transform to screen coords first
10146 Vector4Set(w[0], start[0], start[1], start[2], 1);
10147 Vector4Set(w[1], end[0], end[1], end[2], 1);
10148 R_Viewport_TransformToScreen(&r_refdef.view.viewport, w[0], s[0]);
10149 R_Viewport_TransformToScreen(&r_refdef.view.viewport, w[1], s[1]);
10150 x1 = s[0][0] * vid_conwidth.value / vid.width;
10151 y1 = (vid.height - s[0][1]) * vid_conheight.value / vid.height;
10152 x2 = s[1][0] * vid_conwidth.value / vid.width;
10153 y2 = (vid.height - s[1][1]) * vid_conheight.value / vid.height;
10154 //Con_DPrintf("R_DebugLine: %.0f,%.0f to %.0f,%.0f\n", x1, y1, x2, y2);
10156 // add the line to the UI mesh for drawing later
10158 // width is measured in real pixels
10159 if (fabs(x2 - x1) > fabs(y2 - y1))
10162 offsety = 0.5f * width * vid_conheight.value / vid.height;
10166 offsetx = 0.5f * width * vid_conwidth.value / vid.width;
10169 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);
10170 e0 = Mod_Mesh_IndexForVertex(mod, surf, x1 - offsetx, y1 - offsety, 10, 0, 0, -1, 0, 0, 0, 0, r1, g1, b1, alpha1);
10171 e1 = Mod_Mesh_IndexForVertex(mod, surf, x2 - offsetx, y2 - offsety, 10, 0, 0, -1, 0, 0, 0, 0, r2, g2, b2, alpha2);
10172 e2 = Mod_Mesh_IndexForVertex(mod, surf, x2 + offsetx, y2 + offsety, 10, 0, 0, -1, 0, 0, 0, 0, r2, g2, b2, alpha2);
10173 e3 = Mod_Mesh_IndexForVertex(mod, surf, x1 + offsetx, y1 + offsety, 10, 0, 0, -1, 0, 0, 0, 0, r1, g1, b1, alpha1);
10174 Mod_Mesh_AddTriangle(mod, surf, e0, e1, e2);
10175 Mod_Mesh_AddTriangle(mod, surf, e0, e2, e3);
10180 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)
10182 static texture_t texture;
10184 // fake enough texture and surface state to render this geometry
10186 texture.update_lastrenderframe = -1; // regenerate this texture
10187 texture.basematerialflags = materialflags | MATERIALFLAG_CUSTOMSURFACE | MATERIALFLAG_WALL;
10188 texture.basealpha = 1.0f;
10189 texture.currentskinframe = skinframe;
10190 texture.currenttexmatrix = *texmatrix; // requires MATERIALFLAG_CUSTOMSURFACE
10191 texture.offsetmapping = OFFSETMAPPING_OFF;
10192 texture.offsetscale = 1;
10193 texture.specularscalemod = 1;
10194 texture.specularpowermod = 1;
10195 texture.transparentsort = TRANSPARENTSORT_DISTANCE;
10197 R_DrawCustomSurface_Texture(&texture, texmatrix, materialflags, firstvertex, numvertices, firsttriangle, numtriangles, writedepth, prepass, ui);
10200 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)
10202 static msurface_t surface;
10203 const msurface_t *surfacelist = &surface;
10205 // fake enough texture and surface state to render this geometry
10206 surface.texture = texture;
10207 surface.num_triangles = numtriangles;
10208 surface.num_firsttriangle = firsttriangle;
10209 surface.num_vertices = numvertices;
10210 surface.num_firstvertex = firstvertex;
10213 rsurface.texture = R_GetCurrentTexture(surface.texture);
10214 rsurface.lightmaptexture = NULL;
10215 rsurface.deluxemaptexture = NULL;
10216 rsurface.uselightmaptexture = false;
10217 R_DrawModelTextureSurfaceList(1, &surfacelist, writedepth, prepass, ui);