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_textureunits = {CF_CLIENT, "r_textureunits", "32", "number of texture units to use in GL 1.1 and GL 1.3 rendering paths"};
160 static cvar_t gl_combine = {CF_CLIENT | CF_READONLY, "gl_combine", "1", "indicates whether the OpenGL 1.3 rendering path is active"};
161 static cvar_t r_glsl = {CF_CLIENT | CF_READONLY, "r_glsl", "1", "indicates whether the OpenGL 2.0 rendering path is active"};
163 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"};
164 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"};
165 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)"};
166 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"};
167 cvar_t r_viewscale_fpsscaling = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling", "0", "change resolution based on framerate"};
168 cvar_t r_viewscale_fpsscaling_min = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_min", "0.0625", "worst acceptable quality"};
169 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"};
170 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)"};
171 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)"};
172 cvar_t r_viewscale_fpsscaling_target = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_target", "70", "desired framerate"};
174 cvar_t r_glsl_skeletal = {CF_CLIENT | CF_ARCHIVE, "r_glsl_skeletal", "1", "render skeletal models faster using a gpu-skinning technique"};
175 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)"};
176 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)"};
177 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)"};
178 cvar_t r_glsl_offsetmapping_reliefmapping = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
179 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)"};
180 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)"};
181 cvar_t r_glsl_offsetmapping_scale = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
182 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"};
183 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."};
184 cvar_t r_glsl_postprocess = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess", "0", "use a GLSL postprocessing shader"};
185 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)"};
186 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)"};
187 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)"};
188 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)"};
189 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)"};
190 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)"};
191 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)"};
192 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)"};
193 cvar_t r_colorfringe = {CF_CLIENT | CF_ARCHIVE, "r_colorfringe", "0", "Chromatic aberration. Values higher than 0.025 will noticeably distort the image"};
195 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)"};
196 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)"};
197 cvar_t r_water_clippingplanebias = {CF_CLIENT | CF_ARCHIVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
198 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"};
199 cvar_t r_water_refractdistort = {CF_CLIENT | CF_ARCHIVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
200 cvar_t r_water_reflectdistort = {CF_CLIENT | CF_ARCHIVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
201 cvar_t r_water_scissormode = {CF_CLIENT, "r_water_scissormode", "3", "scissor (1) or cull (2) or both (3) water renders"};
202 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"};
203 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"};
205 cvar_t r_lerpsprites = {CF_CLIENT | CF_ARCHIVE, "r_lerpsprites", "0", "enables animation smoothing on sprites"};
206 cvar_t r_lerpmodels = {CF_CLIENT | CF_ARCHIVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
207 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"};
208 cvar_t r_lerplightstyles = {CF_CLIENT | CF_ARCHIVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
209 cvar_t r_waterscroll = {CF_CLIENT | CF_ARCHIVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
211 cvar_t r_bloom = {CF_CLIENT | CF_ARCHIVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
212 cvar_t r_bloom_colorscale = {CF_CLIENT | CF_ARCHIVE, "r_bloom_colorscale", "1", "how bright the glow is"};
214 cvar_t r_bloom_brighten = {CF_CLIENT | CF_ARCHIVE, "r_bloom_brighten", "1", "how bright the glow is, after subtract/power"};
215 cvar_t r_bloom_blur = {CF_CLIENT | CF_ARCHIVE, "r_bloom_blur", "4", "how large the glow is"};
216 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)"};
217 cvar_t r_bloom_colorexponent = {CF_CLIENT | CF_ARCHIVE, "r_bloom_colorexponent", "1", "how exaggerated the glow is"};
218 cvar_t r_bloom_colorsubtract = {CF_CLIENT | CF_ARCHIVE, "r_bloom_colorsubtract", "0.1", "reduces bloom colors by a certain amount"};
219 cvar_t r_bloom_scenebrightness = {CF_CLIENT | CF_ARCHIVE, "r_bloom_scenebrightness", "1", "global rendering brightness when bloom is enabled"};
221 cvar_t r_hdr_scenebrightness = {CF_CLIENT | CF_ARCHIVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
222 cvar_t r_hdr_glowintensity = {CF_CLIENT | CF_ARCHIVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
223 cvar_t r_hdr_irisadaptation = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation", "0", "adjust scene brightness according to light intensity at player location"};
224 cvar_t r_hdr_irisadaptation_multiplier = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_multiplier", "2", "brightness at which value will be 1.0"};
225 cvar_t r_hdr_irisadaptation_minvalue = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_minvalue", "0.5", "minimum value that can result from multiplier / brightness"};
226 cvar_t r_hdr_irisadaptation_maxvalue = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_maxvalue", "4", "maximum value that can result from multiplier / brightness"};
227 cvar_t r_hdr_irisadaptation_value = {CF_CLIENT, "r_hdr_irisadaptation_value", "1", "current value as scenebrightness multiplier, changes continuously when irisadaptation is active"};
228 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"};
229 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"};
230 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"};
232 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"};
234 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"};
236 cvar_t gl_lightmaps = {CF_CLIENT, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers), a value of 2 keeps normalmap shading"};
238 cvar_t r_test = {CF_CLIENT, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
240 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)"};
241 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)"};
242 cvar_t r_batch_debugdynamicvertexpath = {CF_CLIENT | CF_ARCHIVE, "r_batch_debugdynamicvertexpath", "0", "force the dynamic batching code path for debugging purposes"};
243 cvar_t r_batch_dynamicbuffer = {CF_CLIENT | CF_ARCHIVE, "r_batch_dynamicbuffer", "0", "use vertex/index buffers for drawing dynamic and copytriangles batches"};
245 cvar_t r_glsl_saturation = {CF_CLIENT | CF_ARCHIVE, "r_glsl_saturation", "1", "saturation multiplier (only working in glsl!)"};
246 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"};
248 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."};
250 // FIXME: This cvar would grow to a ridiculous size after several launches and clean exits when used during surface sorting.
251 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)"};
252 cvar_t r_buffermegs[R_BUFFERDATA_COUNT] =
254 {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_vertex", "4", "vertex buffer size for one frame"},
255 {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_index16", "1", "index buffer size for one frame (16bit indices)"},
256 {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_index32", "1", "index buffer size for one frame (32bit indices)"},
257 {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_uniform", "0.25", "uniform buffer size for one frame"},
260 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)"};
261 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)"};
262 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"};
264 extern cvar_t v_glslgamma_2d;
266 extern qbool v_flipped_state;
268 r_framebufferstate_t r_fb;
270 /// shadow volume bsp struct with automatically growing nodes buffer
273 int r_uniformbufferalignment = 32; // dynamically updated to match GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT
275 rtexture_t *r_texture_blanknormalmap;
276 rtexture_t *r_texture_white;
277 rtexture_t *r_texture_grey128;
278 rtexture_t *r_texture_black;
279 rtexture_t *r_texture_notexture;
280 rtexture_t *r_texture_whitecube;
281 rtexture_t *r_texture_normalizationcube;
282 rtexture_t *r_texture_fogattenuation;
283 rtexture_t *r_texture_fogheighttexture;
284 rtexture_t *r_texture_gammaramps;
285 unsigned int r_texture_gammaramps_serial;
286 //rtexture_t *r_texture_fogintensity;
287 rtexture_t *r_texture_reflectcube;
289 // TODO: hash lookups?
290 typedef struct cubemapinfo_s
297 int r_texture_numcubemaps;
298 cubemapinfo_t *r_texture_cubemaps[MAX_CUBEMAPS];
300 unsigned int r_queries[MAX_OCCLUSION_QUERIES];
301 unsigned int r_numqueries;
302 unsigned int r_maxqueries;
304 typedef struct r_qwskincache_s
306 char name[MAX_QPATH];
307 skinframe_t *skinframe;
311 static r_qwskincache_t *r_qwskincache;
312 static int r_qwskincache_size;
314 /// vertex coordinates for a quad that covers the screen exactly
315 extern const float r_screenvertex3f[12];
316 const float r_screenvertex3f[12] =
324 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
327 for (i = 0;i < verts;i++)
338 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
341 for (i = 0;i < verts;i++)
351 // FIXME: move this to client?
354 if (gamemode == GAME_NEHAHRA)
356 Cvar_Set(&cvars_all, "gl_fogenable", "0");
357 Cvar_Set(&cvars_all, "gl_fogdensity", "0.2");
358 Cvar_Set(&cvars_all, "gl_fogred", "0.3");
359 Cvar_Set(&cvars_all, "gl_foggreen", "0.3");
360 Cvar_Set(&cvars_all, "gl_fogblue", "0.3");
362 r_refdef.fog_density = 0;
363 r_refdef.fog_red = 0;
364 r_refdef.fog_green = 0;
365 r_refdef.fog_blue = 0;
366 r_refdef.fog_alpha = 1;
367 r_refdef.fog_start = 0;
368 r_refdef.fog_end = 16384;
369 r_refdef.fog_height = 1<<30;
370 r_refdef.fog_fadedepth = 128;
371 memset(r_refdef.fog_height_texturename, 0, sizeof(r_refdef.fog_height_texturename));
374 static void R_BuildBlankTextures(void)
376 unsigned char data[4];
377 data[2] = 128; // normal X
378 data[1] = 128; // normal Y
379 data[0] = 255; // normal Z
380 data[3] = 255; // height
381 r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
386 r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
391 r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
396 r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
399 static void R_BuildNoTexture(void)
401 r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, Image_GenerateNoTexture(), TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, -1, NULL);
404 static void R_BuildWhiteCube(void)
406 unsigned char data[6*1*1*4];
407 memset(data, 255, sizeof(data));
408 r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
411 static void R_BuildNormalizationCube(void)
415 vec_t s, t, intensity;
418 data = (unsigned char *)Mem_Alloc(tempmempool, 6*NORMSIZE*NORMSIZE*4);
419 for (side = 0;side < 6;side++)
421 for (y = 0;y < NORMSIZE;y++)
423 for (x = 0;x < NORMSIZE;x++)
425 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
426 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
461 intensity = 127.0f / sqrt(DotProduct(v, v));
462 data[((side*64+y)*64+x)*4+2] = (unsigned char)(128.0f + intensity * v[0]);
463 data[((side*64+y)*64+x)*4+1] = (unsigned char)(128.0f + intensity * v[1]);
464 data[((side*64+y)*64+x)*4+0] = (unsigned char)(128.0f + intensity * v[2]);
465 data[((side*64+y)*64+x)*4+3] = 255;
469 r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
473 static void R_BuildFogTexture(void)
477 unsigned char data1[FOGWIDTH][4];
478 //unsigned char data2[FOGWIDTH][4];
481 r_refdef.fogmasktable_start = r_refdef.fog_start;
482 r_refdef.fogmasktable_alpha = r_refdef.fog_alpha;
483 r_refdef.fogmasktable_range = r_refdef.fogrange;
484 r_refdef.fogmasktable_density = r_refdef.fog_density;
486 r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
487 for (x = 0;x < FOGMASKTABLEWIDTH;x++)
489 d = (x * r - r_refdef.fogmasktable_start);
490 if(developer_extra.integer)
491 Con_DPrintf("%f ", d);
493 if (r_fog_exp2.integer)
494 alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
496 alpha = exp(-r_refdef.fogmasktable_density * 0.004 * d);
497 if(developer_extra.integer)
498 Con_DPrintf(" : %f ", alpha);
499 alpha = 1 - (1 - alpha) * r_refdef.fogmasktable_alpha;
500 if(developer_extra.integer)
501 Con_DPrintf(" = %f\n", alpha);
502 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
505 for (x = 0;x < FOGWIDTH;x++)
507 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
512 //data2[x][0] = 255 - b;
513 //data2[x][1] = 255 - b;
514 //data2[x][2] = 255 - b;
517 if (r_texture_fogattenuation)
519 R_UpdateTexture(r_texture_fogattenuation, &data1[0][0], 0, 0, 0, FOGWIDTH, 1, 1, 0);
520 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, 0, FOGWIDTH, 1, 1, 0);
524 r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
525 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
529 static void R_BuildFogHeightTexture(void)
531 unsigned char *inpixels;
539 strlcpy(r_refdef.fogheighttexturename, r_refdef.fog_height_texturename, sizeof(r_refdef.fogheighttexturename));
540 if (r_refdef.fogheighttexturename[0])
541 inpixels = loadimagepixelsbgra(r_refdef.fogheighttexturename, true, false, false, NULL);
544 r_refdef.fog_height_tablesize = 0;
545 if (r_texture_fogheighttexture)
546 R_FreeTexture(r_texture_fogheighttexture);
547 r_texture_fogheighttexture = NULL;
548 if (r_refdef.fog_height_table2d)
549 Mem_Free(r_refdef.fog_height_table2d);
550 r_refdef.fog_height_table2d = NULL;
551 if (r_refdef.fog_height_table1d)
552 Mem_Free(r_refdef.fog_height_table1d);
553 r_refdef.fog_height_table1d = NULL;
557 r_refdef.fog_height_tablesize = size;
558 r_refdef.fog_height_table1d = (unsigned char *)Mem_Alloc(r_main_mempool, size * 4);
559 r_refdef.fog_height_table2d = (unsigned char *)Mem_Alloc(r_main_mempool, size * size * 4);
560 memcpy(r_refdef.fog_height_table1d, inpixels, size * 4);
562 // LadyHavoc: now the magic - what is that table2d for? it is a cooked
563 // average fog color table accounting for every fog layer between a point
564 // and the camera. (Note: attenuation is handled separately!)
565 for (y = 0;y < size;y++)
567 for (x = 0;x < size;x++)
573 for (j = x;j <= y;j++)
575 Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
581 for (j = x;j >= y;j--)
583 Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
588 r_refdef.fog_height_table2d[(y*size+x)*4+0] = (unsigned char)(c[0] * f);
589 r_refdef.fog_height_table2d[(y*size+x)*4+1] = (unsigned char)(c[1] * f);
590 r_refdef.fog_height_table2d[(y*size+x)*4+2] = (unsigned char)(c[2] * f);
591 r_refdef.fog_height_table2d[(y*size+x)*4+3] = (unsigned char)(c[3] * f);
594 r_texture_fogheighttexture = R_LoadTexture2D(r_main_texturepool, "fogheighttable", size, size, r_refdef.fog_height_table2d, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_CLAMP, -1, NULL);
597 //=======================================================================================================================================================
599 static const char *builtinshaderstrings[] =
601 #include "shader_glsl.h"
605 //=======================================================================================================================================================
607 typedef struct shaderpermutationinfo_s
612 shaderpermutationinfo_t;
614 typedef struct shadermodeinfo_s
616 const char *sourcebasename;
617 const char *extension;
618 const char **builtinshaderstrings;
627 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
628 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
630 {"#define USEDIFFUSE\n", " diffuse"},
631 {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
632 {"#define USEVIEWTINT\n", " viewtint"},
633 {"#define USECOLORMAPPING\n", " colormapping"},
634 {"#define USESATURATION\n", " saturation"},
635 {"#define USEFOGINSIDE\n", " foginside"},
636 {"#define USEFOGOUTSIDE\n", " fogoutside"},
637 {"#define USEFOGHEIGHTTEXTURE\n", " fogheighttexture"},
638 {"#define USEFOGALPHAHACK\n", " fogalphahack"},
639 {"#define USEGAMMARAMPS\n", " gammaramps"},
640 {"#define USECUBEFILTER\n", " cubefilter"},
641 {"#define USEGLOW\n", " glow"},
642 {"#define USEBLOOM\n", " bloom"},
643 {"#define USESPECULAR\n", " specular"},
644 {"#define USEPOSTPROCESSING\n", " postprocessing"},
645 {"#define USEREFLECTION\n", " reflection"},
646 {"#define USEOFFSETMAPPING\n", " offsetmapping"},
647 {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
648 {"#define USESHADOWMAP2D\n", " shadowmap2d"},
649 {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"}, // TODO make this a static parm
650 {"#define USESHADOWMAPORTHO\n", " shadowmaportho"},
651 {"#define USEDEFERREDLIGHTMAP\n", " deferredlightmap"},
652 {"#define USEALPHAKILL\n", " alphakill"},
653 {"#define USEREFLECTCUBE\n", " reflectcube"},
654 {"#define USENORMALMAPSCROLLBLEND\n", " normalmapscrollblend"},
655 {"#define USEBOUNCEGRID\n", " bouncegrid"},
656 {"#define USEBOUNCEGRIDDIRECTIONAL\n", " bouncegriddirectional"}, // TODO make this a static parm
657 {"#define USETRIPPY\n", " trippy"},
658 {"#define USEDEPTHRGB\n", " depthrgb"},
659 {"#define USEALPHAGENVERTEX\n", " alphagenvertex"},
660 {"#define USESKELETAL\n", " skeletal"},
661 {"#define USEOCCLUDE\n", " occlude"}
664 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
665 shadermodeinfo_t shadermodeinfo[SHADERLANGUAGE_COUNT][SHADERMODE_COUNT] =
667 // SHADERLANGUAGE_GLSL
669 {"combined", "glsl", builtinshaderstrings, "#define MODE_GENERIC\n", " generic"},
670 {"combined", "glsl", builtinshaderstrings, "#define MODE_POSTPROCESS\n", " postprocess"},
671 {"combined", "glsl", builtinshaderstrings, "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
672 {"combined", "glsl", builtinshaderstrings, "#define MODE_FLATCOLOR\n", " flatcolor"},
673 {"combined", "glsl", builtinshaderstrings, "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
674 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTMAP\n", " lightmap"},
675 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
676 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
677 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP\n", " lightdirectionmap_forced_lightmap"},
678 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR\n", " lightdirectionmap_forced_vertexcolor"},
679 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTGRID\n", " lightgrid"},
680 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
681 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTSOURCE\n", " lightsource"},
682 {"combined", "glsl", builtinshaderstrings, "#define MODE_REFRACTION\n", " refraction"},
683 {"combined", "glsl", builtinshaderstrings, "#define MODE_WATER\n", " water"},
684 {"combined", "glsl", builtinshaderstrings, "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
685 {"combined", "glsl", builtinshaderstrings, "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
689 struct r_glsl_permutation_s;
690 typedef struct r_glsl_permutation_s
693 struct r_glsl_permutation_s *hashnext;
695 uint64_t permutation;
697 /// indicates if we have tried compiling this permutation already
699 /// 0 if compilation failed
701 // texture units assigned to each detected uniform
702 int tex_Texture_First;
703 int tex_Texture_Second;
704 int tex_Texture_GammaRamps;
705 int tex_Texture_Normal;
706 int tex_Texture_Color;
707 int tex_Texture_Gloss;
708 int tex_Texture_Glow;
709 int tex_Texture_SecondaryNormal;
710 int tex_Texture_SecondaryColor;
711 int tex_Texture_SecondaryGloss;
712 int tex_Texture_SecondaryGlow;
713 int tex_Texture_Pants;
714 int tex_Texture_Shirt;
715 int tex_Texture_FogHeightTexture;
716 int tex_Texture_FogMask;
717 int tex_Texture_LightGrid;
718 int tex_Texture_Lightmap;
719 int tex_Texture_Deluxemap;
720 int tex_Texture_Attenuation;
721 int tex_Texture_Cube;
722 int tex_Texture_Refraction;
723 int tex_Texture_Reflection;
724 int tex_Texture_ShadowMap2D;
725 int tex_Texture_CubeProjection;
726 int tex_Texture_ScreenNormalMap;
727 int tex_Texture_ScreenDiffuse;
728 int tex_Texture_ScreenSpecular;
729 int tex_Texture_ReflectMask;
730 int tex_Texture_ReflectCube;
731 int tex_Texture_BounceGrid;
732 /// locations of detected uniforms in program object, or -1 if not found
733 int loc_Texture_First;
734 int loc_Texture_Second;
735 int loc_Texture_GammaRamps;
736 int loc_Texture_Normal;
737 int loc_Texture_Color;
738 int loc_Texture_Gloss;
739 int loc_Texture_Glow;
740 int loc_Texture_SecondaryNormal;
741 int loc_Texture_SecondaryColor;
742 int loc_Texture_SecondaryGloss;
743 int loc_Texture_SecondaryGlow;
744 int loc_Texture_Pants;
745 int loc_Texture_Shirt;
746 int loc_Texture_FogHeightTexture;
747 int loc_Texture_FogMask;
748 int loc_Texture_LightGrid;
749 int loc_Texture_Lightmap;
750 int loc_Texture_Deluxemap;
751 int loc_Texture_Attenuation;
752 int loc_Texture_Cube;
753 int loc_Texture_Refraction;
754 int loc_Texture_Reflection;
755 int loc_Texture_ShadowMap2D;
756 int loc_Texture_CubeProjection;
757 int loc_Texture_ScreenNormalMap;
758 int loc_Texture_ScreenDiffuse;
759 int loc_Texture_ScreenSpecular;
760 int loc_Texture_ReflectMask;
761 int loc_Texture_ReflectCube;
762 int loc_Texture_BounceGrid;
764 int loc_BloomBlur_Parameters;
766 int loc_Color_Ambient;
767 int loc_Color_Diffuse;
768 int loc_Color_Specular;
772 int loc_DeferredColor_Ambient;
773 int loc_DeferredColor_Diffuse;
774 int loc_DeferredColor_Specular;
775 int loc_DeferredMod_Diffuse;
776 int loc_DeferredMod_Specular;
777 int loc_DistortScaleRefractReflect;
780 int loc_FogHeightFade;
782 int loc_FogPlaneViewDist;
783 int loc_FogRangeRecip;
786 int loc_LightGridMatrix;
787 int loc_LightGridNormalMatrix;
788 int loc_LightPosition;
789 int loc_OffsetMapping_ScaleSteps;
790 int loc_OffsetMapping_LodDistance;
791 int loc_OffsetMapping_Bias;
793 int loc_ReflectColor;
794 int loc_ReflectFactor;
795 int loc_ReflectOffset;
796 int loc_RefractColor;
798 int loc_ScreenCenterRefractReflect;
799 int loc_ScreenScaleRefractReflect;
800 int loc_ScreenToDepth;
801 int loc_ShadowMap_Parameters;
802 int loc_ShadowMap_TextureScale;
803 int loc_SpecularPower;
804 int loc_Skeletal_Transform12;
810 int loc_ViewTintColor;
812 int loc_ModelToLight;
814 int loc_BackgroundTexMatrix;
815 int loc_ModelViewProjectionMatrix;
816 int loc_ModelViewMatrix;
817 int loc_PixelToScreenTexCoord;
818 int loc_ModelToReflectCube;
819 int loc_ShadowMapMatrix;
820 int loc_BloomColorSubtract;
821 int loc_NormalmapScrollBlend;
822 int loc_BounceGridMatrix;
823 int loc_BounceGridIntensity;
824 /// uniform block bindings
825 int ubibind_Skeletal_Transform12_UniformBlock;
826 /// uniform block indices
827 int ubiloc_Skeletal_Transform12_UniformBlock;
829 r_glsl_permutation_t;
831 #define SHADERPERMUTATION_HASHSIZE 256
834 // non-degradable "lightweight" shader parameters to keep the permutations simpler
835 // these can NOT degrade! only use for simple stuff
838 SHADERSTATICPARM_SATURATION_REDCOMPENSATE = 0, ///< red compensation filter for saturation
839 SHADERSTATICPARM_EXACTSPECULARMATH = 1, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
840 SHADERSTATICPARM_POSTPROCESS_USERVEC1 = 2, ///< postprocess uservec1 is enabled
841 SHADERSTATICPARM_POSTPROCESS_USERVEC2 = 3, ///< postprocess uservec2 is enabled
842 SHADERSTATICPARM_POSTPROCESS_USERVEC3 = 4, ///< postprocess uservec3 is enabled
843 SHADERSTATICPARM_POSTPROCESS_USERVEC4 = 5, ///< postprocess uservec4 is enabled
844 SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS = 6, // use both alpha layers while blending materials, allows more advanced microblending
845 SHADERSTATICPARM_OFFSETMAPPING_USELOD = 7, ///< LOD for offsetmapping
846 SHADERSTATICPARM_SHADOWMAPPCF_1 = 8, ///< PCF 1
847 SHADERSTATICPARM_SHADOWMAPPCF_2 = 9, ///< PCF 2
848 SHADERSTATICPARM_SHADOWSAMPLER = 10, ///< sampler
849 SHADERSTATICPARM_CELSHADING = 11, ///< celshading (alternative diffuse and specular math)
850 SHADERSTATICPARM_CELOUTLINES = 12, ///< celoutline (depth buffer analysis to produce outlines)
851 SHADERSTATICPARM_FXAA = 13, ///< fast approximate anti aliasing
852 SHADERSTATICPARM_COLORFRINGE = 14 ///< colorfringe (chromatic aberration)
854 #define SHADERSTATICPARMS_COUNT 15
856 static const char *shaderstaticparmstrings_list[SHADERSTATICPARMS_COUNT];
857 static int shaderstaticparms_count = 0;
859 static unsigned int r_compileshader_staticparms[(SHADERSTATICPARMS_COUNT + 0x1F) >> 5] = {0};
860 #define R_COMPILESHADER_STATICPARM_ENABLE(p) r_compileshader_staticparms[(p) >> 5] |= (1 << ((p) & 0x1F))
862 extern qbool r_shadow_shadowmapsampler;
863 extern int r_shadow_shadowmappcf;
864 qbool R_CompileShader_CheckStaticParms(void)
866 static int r_compileshader_staticparms_save[(SHADERSTATICPARMS_COUNT + 0x1F) >> 5];
867 memcpy(r_compileshader_staticparms_save, r_compileshader_staticparms, sizeof(r_compileshader_staticparms));
868 memset(r_compileshader_staticparms, 0, sizeof(r_compileshader_staticparms));
871 if (r_glsl_saturation_redcompensate.integer)
872 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SATURATION_REDCOMPENSATE);
873 if (r_glsl_vertextextureblend_usebothalphas.integer)
874 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS);
875 if (r_shadow_glossexact.integer)
876 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_EXACTSPECULARMATH);
877 if (r_glsl_postprocess.integer)
879 if (r_glsl_postprocess_uservec1_enable.integer)
880 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC1);
881 if (r_glsl_postprocess_uservec2_enable.integer)
882 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC2);
883 if (r_glsl_postprocess_uservec3_enable.integer)
884 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC3);
885 if (r_glsl_postprocess_uservec4_enable.integer)
886 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC4);
889 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_FXAA);
890 if (r_glsl_offsetmapping_lod.integer && r_glsl_offsetmapping_lod_distance.integer > 0)
891 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_OFFSETMAPPING_USELOD);
893 if (r_shadow_shadowmapsampler)
894 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWSAMPLER);
895 if (r_shadow_shadowmappcf > 1)
896 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWMAPPCF_2);
897 else if (r_shadow_shadowmappcf)
898 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWMAPPCF_1);
899 if (r_celshading.integer)
900 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_CELSHADING);
901 if (r_celoutlines.integer)
902 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_CELOUTLINES);
903 if (r_colorfringe.value)
904 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_COLORFRINGE);
906 return memcmp(r_compileshader_staticparms, r_compileshader_staticparms_save, sizeof(r_compileshader_staticparms)) != 0;
909 #define R_COMPILESHADER_STATICPARM_EMIT(p, n) \
910 if(r_compileshader_staticparms[(p) >> 5] & (1 << ((p) & 0x1F))) \
911 shaderstaticparmstrings_list[shaderstaticparms_count++] = "#define " n "\n"; \
913 shaderstaticparmstrings_list[shaderstaticparms_count++] = "\n"
914 static void R_CompileShader_AddStaticParms(unsigned int mode, uint64_t permutation)
916 shaderstaticparms_count = 0;
919 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SATURATION_REDCOMPENSATE, "SATURATION_REDCOMPENSATE");
920 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_EXACTSPECULARMATH, "USEEXACTSPECULARMATH");
921 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC1, "USERVEC1");
922 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC2, "USERVEC2");
923 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC3, "USERVEC3");
924 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC4, "USERVEC4");
925 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS, "USEBOTHALPHAS");
926 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_OFFSETMAPPING_USELOD, "USEOFFSETMAPPING_LOD");
927 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWMAPPCF_1, "USESHADOWMAPPCF 1");
928 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWMAPPCF_2, "USESHADOWMAPPCF 2");
929 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWSAMPLER, "USESHADOWSAMPLER");
930 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_CELSHADING, "USECELSHADING");
931 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_CELOUTLINES, "USECELOUTLINES");
932 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_FXAA, "USEFXAA");
933 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_COLORFRINGE, "USECOLORFRINGE");
936 /// information about each possible shader permutation
937 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
938 /// currently selected permutation
939 r_glsl_permutation_t *r_glsl_permutation;
940 /// storage for permutations linked in the hash table
941 memexpandablearray_t r_glsl_permutationarray;
943 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, uint64_t permutation)
945 //unsigned int hashdepth = 0;
946 unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
947 r_glsl_permutation_t *p;
948 for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
950 if (p->mode == mode && p->permutation == permutation)
952 //if (hashdepth > 10)
953 // Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
958 p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
960 p->permutation = permutation;
961 p->hashnext = r_glsl_permutationhash[mode][hashindex];
962 r_glsl_permutationhash[mode][hashindex] = p;
963 //if (hashdepth > 10)
964 // Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
968 static char *R_ShaderStrCat(const char **strings)
971 const char **p = strings;
974 for (p = strings;(t = *p);p++)
977 s = string = (char *)Mem_Alloc(r_main_mempool, len);
979 for (p = strings;(t = *p);p++)
989 static char *R_ShaderStrCat(const char **strings);
990 static void R_InitShaderModeInfo(void)
993 shadermodeinfo_t *modeinfo;
994 // 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)
995 for (language = 0; language < SHADERLANGUAGE_COUNT; language++)
997 for (i = 0; i < SHADERMODE_COUNT; i++)
999 char filename[MAX_QPATH];
1000 modeinfo = &shadermodeinfo[language][i];
1001 modeinfo->builtinstring = R_ShaderStrCat(modeinfo->builtinshaderstrings);
1002 modeinfo->builtincrc = CRC_Block((const unsigned char *)modeinfo->builtinstring, strlen(modeinfo->builtinstring));
1003 dpsnprintf(filename, sizeof(filename), "%s/%s_crc%i.%s", modeinfo->extension, modeinfo->sourcebasename, modeinfo->builtincrc, modeinfo->extension);
1004 modeinfo->filename = Mem_strdup(r_main_mempool, filename);
1009 static char *ShaderModeInfo_GetShaderText(shadermodeinfo_t *modeinfo, qbool printfromdisknotice, qbool builtinonly)
1012 // if the mode has no filename we have to return the builtin string
1013 if (builtinonly || !modeinfo->filename)
1014 return Mem_strdup(r_main_mempool, modeinfo->builtinstring);
1015 // note that FS_LoadFile appends a 0 byte to make it a valid string
1016 shaderstring = (char *)FS_LoadFile(modeinfo->filename, r_main_mempool, false, NULL);
1019 if (printfromdisknotice)
1020 Con_DPrintf("Loading shaders from file %s...\n", modeinfo->filename);
1021 return shaderstring;
1023 // fall back to builtinstring
1024 return Mem_strdup(r_main_mempool, modeinfo->builtinstring);
1027 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, uint64_t permutation)
1032 shadermodeinfo_t *modeinfo = &shadermodeinfo[SHADERLANGUAGE_GLSL][mode];
1034 char permutationname[256];
1035 int vertstrings_count = 0;
1036 int geomstrings_count = 0;
1037 int fragstrings_count = 0;
1038 const char *vertstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1039 const char *geomstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1040 const char *fragstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1047 permutationname[0] = 0;
1048 sourcestring = ShaderModeInfo_GetShaderText(modeinfo, true, false);
1050 strlcat(permutationname, modeinfo->filename, sizeof(permutationname));
1052 // we need 140 for r_glsl_skeletal (GL_ARB_uniform_buffer_object)
1053 if(vid.support.glshaderversion >= 140)
1055 vertstrings_list[vertstrings_count++] = "#version 140\n";
1056 geomstrings_list[geomstrings_count++] = "#version 140\n";
1057 fragstrings_list[fragstrings_count++] = "#version 140\n";
1058 vertstrings_list[vertstrings_count++] = "#define GLSL140\n";
1059 geomstrings_list[geomstrings_count++] = "#define GLSL140\n";
1060 fragstrings_list[fragstrings_count++] = "#define GLSL140\n";
1062 // if we can do #version 130, we should (this improves quality of offset/reliefmapping thanks to textureGrad)
1063 else if(vid.support.glshaderversion >= 130)
1065 vertstrings_list[vertstrings_count++] = "#version 130\n";
1066 geomstrings_list[geomstrings_count++] = "#version 130\n";
1067 fragstrings_list[fragstrings_count++] = "#version 130\n";
1068 vertstrings_list[vertstrings_count++] = "#define GLSL130\n";
1069 geomstrings_list[geomstrings_count++] = "#define GLSL130\n";
1070 fragstrings_list[fragstrings_count++] = "#define GLSL130\n";
1072 // if we can do #version 120, we should (this adds the invariant keyword)
1073 else if(vid.support.glshaderversion >= 120)
1075 vertstrings_list[vertstrings_count++] = "#version 120\n";
1076 geomstrings_list[geomstrings_count++] = "#version 120\n";
1077 fragstrings_list[fragstrings_count++] = "#version 120\n";
1078 vertstrings_list[vertstrings_count++] = "#define GLSL120\n";
1079 geomstrings_list[geomstrings_count++] = "#define GLSL120\n";
1080 fragstrings_list[fragstrings_count++] = "#define GLSL120\n";
1082 // GLES also adds several things from GLSL120
1083 switch(vid.renderpath)
1085 case RENDERPATH_GLES2:
1086 vertstrings_list[vertstrings_count++] = "#define GLES\n";
1087 geomstrings_list[geomstrings_count++] = "#define GLES\n";
1088 fragstrings_list[fragstrings_count++] = "#define GLES\n";
1094 // the first pretext is which type of shader to compile as
1095 // (later these will all be bound together as a program object)
1096 vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
1097 geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
1098 fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
1100 // the second pretext is the mode (for example a light source)
1101 vertstrings_list[vertstrings_count++] = modeinfo->pretext;
1102 geomstrings_list[geomstrings_count++] = modeinfo->pretext;
1103 fragstrings_list[fragstrings_count++] = modeinfo->pretext;
1104 strlcat(permutationname, modeinfo->name, sizeof(permutationname));
1106 // now add all the permutation pretexts
1107 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1109 if (permutation & (1ll<<i))
1111 vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
1112 geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
1113 fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
1114 strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
1118 // keep line numbers correct
1119 vertstrings_list[vertstrings_count++] = "\n";
1120 geomstrings_list[geomstrings_count++] = "\n";
1121 fragstrings_list[fragstrings_count++] = "\n";
1126 R_CompileShader_AddStaticParms(mode, permutation);
1127 memcpy((char *)(vertstrings_list + vertstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1128 vertstrings_count += shaderstaticparms_count;
1129 memcpy((char *)(geomstrings_list + geomstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1130 geomstrings_count += shaderstaticparms_count;
1131 memcpy((char *)(fragstrings_list + fragstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1132 fragstrings_count += shaderstaticparms_count;
1134 // now append the shader text itself
1135 vertstrings_list[vertstrings_count++] = sourcestring;
1136 geomstrings_list[geomstrings_count++] = sourcestring;
1137 fragstrings_list[fragstrings_count++] = sourcestring;
1139 // we don't currently use geometry shaders for anything, so just empty the list
1140 geomstrings_count = 0;
1142 // compile the shader program
1143 if (vertstrings_count + geomstrings_count + fragstrings_count)
1144 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1148 qglUseProgram(p->program);CHECKGLERROR
1149 // look up all the uniform variable names we care about, so we don't
1150 // have to look them up every time we set them
1155 GLint activeuniformindex = 0;
1156 GLint numactiveuniforms = 0;
1157 char uniformname[128];
1158 GLsizei uniformnamelength = 0;
1159 GLint uniformsize = 0;
1160 GLenum uniformtype = 0;
1161 memset(uniformname, 0, sizeof(uniformname));
1162 qglGetProgramiv(p->program, GL_ACTIVE_UNIFORMS, &numactiveuniforms);
1163 Con_Printf("Shader has %i uniforms\n", numactiveuniforms);
1164 for (activeuniformindex = 0;activeuniformindex < numactiveuniforms;activeuniformindex++)
1166 qglGetActiveUniform(p->program, activeuniformindex, sizeof(uniformname) - 1, &uniformnamelength, &uniformsize, &uniformtype, uniformname);
1167 Con_Printf("Uniform %i name \"%s\" size %i type %i\n", (int)activeuniformindex, uniformname, (int)uniformsize, (int)uniformtype);
1172 p->loc_Texture_First = qglGetUniformLocation(p->program, "Texture_First");
1173 p->loc_Texture_Second = qglGetUniformLocation(p->program, "Texture_Second");
1174 p->loc_Texture_GammaRamps = qglGetUniformLocation(p->program, "Texture_GammaRamps");
1175 p->loc_Texture_Normal = qglGetUniformLocation(p->program, "Texture_Normal");
1176 p->loc_Texture_Color = qglGetUniformLocation(p->program, "Texture_Color");
1177 p->loc_Texture_Gloss = qglGetUniformLocation(p->program, "Texture_Gloss");
1178 p->loc_Texture_Glow = qglGetUniformLocation(p->program, "Texture_Glow");
1179 p->loc_Texture_SecondaryNormal = qglGetUniformLocation(p->program, "Texture_SecondaryNormal");
1180 p->loc_Texture_SecondaryColor = qglGetUniformLocation(p->program, "Texture_SecondaryColor");
1181 p->loc_Texture_SecondaryGloss = qglGetUniformLocation(p->program, "Texture_SecondaryGloss");
1182 p->loc_Texture_SecondaryGlow = qglGetUniformLocation(p->program, "Texture_SecondaryGlow");
1183 p->loc_Texture_Pants = qglGetUniformLocation(p->program, "Texture_Pants");
1184 p->loc_Texture_Shirt = qglGetUniformLocation(p->program, "Texture_Shirt");
1185 p->loc_Texture_FogHeightTexture = qglGetUniformLocation(p->program, "Texture_FogHeightTexture");
1186 p->loc_Texture_FogMask = qglGetUniformLocation(p->program, "Texture_FogMask");
1187 p->loc_Texture_LightGrid = qglGetUniformLocation(p->program, "Texture_LightGrid");
1188 p->loc_Texture_Lightmap = qglGetUniformLocation(p->program, "Texture_Lightmap");
1189 p->loc_Texture_Deluxemap = qglGetUniformLocation(p->program, "Texture_Deluxemap");
1190 p->loc_Texture_Attenuation = qglGetUniformLocation(p->program, "Texture_Attenuation");
1191 p->loc_Texture_Cube = qglGetUniformLocation(p->program, "Texture_Cube");
1192 p->loc_Texture_Refraction = qglGetUniformLocation(p->program, "Texture_Refraction");
1193 p->loc_Texture_Reflection = qglGetUniformLocation(p->program, "Texture_Reflection");
1194 p->loc_Texture_ShadowMap2D = qglGetUniformLocation(p->program, "Texture_ShadowMap2D");
1195 p->loc_Texture_CubeProjection = qglGetUniformLocation(p->program, "Texture_CubeProjection");
1196 p->loc_Texture_ScreenNormalMap = qglGetUniformLocation(p->program, "Texture_ScreenNormalMap");
1197 p->loc_Texture_ScreenDiffuse = qglGetUniformLocation(p->program, "Texture_ScreenDiffuse");
1198 p->loc_Texture_ScreenSpecular = qglGetUniformLocation(p->program, "Texture_ScreenSpecular");
1199 p->loc_Texture_ReflectMask = qglGetUniformLocation(p->program, "Texture_ReflectMask");
1200 p->loc_Texture_ReflectCube = qglGetUniformLocation(p->program, "Texture_ReflectCube");
1201 p->loc_Texture_BounceGrid = qglGetUniformLocation(p->program, "Texture_BounceGrid");
1202 p->loc_Alpha = qglGetUniformLocation(p->program, "Alpha");
1203 p->loc_BloomBlur_Parameters = qglGetUniformLocation(p->program, "BloomBlur_Parameters");
1204 p->loc_ClientTime = qglGetUniformLocation(p->program, "ClientTime");
1205 p->loc_Color_Ambient = qglGetUniformLocation(p->program, "Color_Ambient");
1206 p->loc_Color_Diffuse = qglGetUniformLocation(p->program, "Color_Diffuse");
1207 p->loc_Color_Specular = qglGetUniformLocation(p->program, "Color_Specular");
1208 p->loc_Color_Glow = qglGetUniformLocation(p->program, "Color_Glow");
1209 p->loc_Color_Pants = qglGetUniformLocation(p->program, "Color_Pants");
1210 p->loc_Color_Shirt = qglGetUniformLocation(p->program, "Color_Shirt");
1211 p->loc_DeferredColor_Ambient = qglGetUniformLocation(p->program, "DeferredColor_Ambient");
1212 p->loc_DeferredColor_Diffuse = qglGetUniformLocation(p->program, "DeferredColor_Diffuse");
1213 p->loc_DeferredColor_Specular = qglGetUniformLocation(p->program, "DeferredColor_Specular");
1214 p->loc_DeferredMod_Diffuse = qglGetUniformLocation(p->program, "DeferredMod_Diffuse");
1215 p->loc_DeferredMod_Specular = qglGetUniformLocation(p->program, "DeferredMod_Specular");
1216 p->loc_DistortScaleRefractReflect = qglGetUniformLocation(p->program, "DistortScaleRefractReflect");
1217 p->loc_EyePosition = qglGetUniformLocation(p->program, "EyePosition");
1218 p->loc_FogColor = qglGetUniformLocation(p->program, "FogColor");
1219 p->loc_FogHeightFade = qglGetUniformLocation(p->program, "FogHeightFade");
1220 p->loc_FogPlane = qglGetUniformLocation(p->program, "FogPlane");
1221 p->loc_FogPlaneViewDist = qglGetUniformLocation(p->program, "FogPlaneViewDist");
1222 p->loc_FogRangeRecip = qglGetUniformLocation(p->program, "FogRangeRecip");
1223 p->loc_LightColor = qglGetUniformLocation(p->program, "LightColor");
1224 p->loc_LightGridMatrix = qglGetUniformLocation(p->program, "LightGridMatrix");
1225 p->loc_LightGridNormalMatrix = qglGetUniformLocation(p->program, "LightGridNormalMatrix");
1226 p->loc_LightDir = qglGetUniformLocation(p->program, "LightDir");
1227 p->loc_LightPosition = qglGetUniformLocation(p->program, "LightPosition");
1228 p->loc_OffsetMapping_ScaleSteps = qglGetUniformLocation(p->program, "OffsetMapping_ScaleSteps");
1229 p->loc_OffsetMapping_LodDistance = qglGetUniformLocation(p->program, "OffsetMapping_LodDistance");
1230 p->loc_OffsetMapping_Bias = qglGetUniformLocation(p->program, "OffsetMapping_Bias");
1231 p->loc_PixelSize = qglGetUniformLocation(p->program, "PixelSize");
1232 p->loc_ReflectColor = qglGetUniformLocation(p->program, "ReflectColor");
1233 p->loc_ReflectFactor = qglGetUniformLocation(p->program, "ReflectFactor");
1234 p->loc_ReflectOffset = qglGetUniformLocation(p->program, "ReflectOffset");
1235 p->loc_RefractColor = qglGetUniformLocation(p->program, "RefractColor");
1236 p->loc_Saturation = qglGetUniformLocation(p->program, "Saturation");
1237 p->loc_ScreenCenterRefractReflect = qglGetUniformLocation(p->program, "ScreenCenterRefractReflect");
1238 p->loc_ScreenScaleRefractReflect = qglGetUniformLocation(p->program, "ScreenScaleRefractReflect");
1239 p->loc_ScreenToDepth = qglGetUniformLocation(p->program, "ScreenToDepth");
1240 p->loc_ShadowMap_Parameters = qglGetUniformLocation(p->program, "ShadowMap_Parameters");
1241 p->loc_ShadowMap_TextureScale = qglGetUniformLocation(p->program, "ShadowMap_TextureScale");
1242 p->loc_SpecularPower = qglGetUniformLocation(p->program, "SpecularPower");
1243 p->loc_UserVec1 = qglGetUniformLocation(p->program, "UserVec1");
1244 p->loc_UserVec2 = qglGetUniformLocation(p->program, "UserVec2");
1245 p->loc_UserVec3 = qglGetUniformLocation(p->program, "UserVec3");
1246 p->loc_UserVec4 = qglGetUniformLocation(p->program, "UserVec4");
1247 p->loc_ColorFringe = qglGetUniformLocation(p->program, "ColorFringe");
1248 p->loc_ViewTintColor = qglGetUniformLocation(p->program, "ViewTintColor");
1249 p->loc_ViewToLight = qglGetUniformLocation(p->program, "ViewToLight");
1250 p->loc_ModelToLight = qglGetUniformLocation(p->program, "ModelToLight");
1251 p->loc_TexMatrix = qglGetUniformLocation(p->program, "TexMatrix");
1252 p->loc_BackgroundTexMatrix = qglGetUniformLocation(p->program, "BackgroundTexMatrix");
1253 p->loc_ModelViewMatrix = qglGetUniformLocation(p->program, "ModelViewMatrix");
1254 p->loc_ModelViewProjectionMatrix = qglGetUniformLocation(p->program, "ModelViewProjectionMatrix");
1255 p->loc_PixelToScreenTexCoord = qglGetUniformLocation(p->program, "PixelToScreenTexCoord");
1256 p->loc_ModelToReflectCube = qglGetUniformLocation(p->program, "ModelToReflectCube");
1257 p->loc_ShadowMapMatrix = qglGetUniformLocation(p->program, "ShadowMapMatrix");
1258 p->loc_BloomColorSubtract = qglGetUniformLocation(p->program, "BloomColorSubtract");
1259 p->loc_NormalmapScrollBlend = qglGetUniformLocation(p->program, "NormalmapScrollBlend");
1260 p->loc_BounceGridMatrix = qglGetUniformLocation(p->program, "BounceGridMatrix");
1261 p->loc_BounceGridIntensity = qglGetUniformLocation(p->program, "BounceGridIntensity");
1262 // initialize the samplers to refer to the texture units we use
1263 p->tex_Texture_First = -1;
1264 p->tex_Texture_Second = -1;
1265 p->tex_Texture_GammaRamps = -1;
1266 p->tex_Texture_Normal = -1;
1267 p->tex_Texture_Color = -1;
1268 p->tex_Texture_Gloss = -1;
1269 p->tex_Texture_Glow = -1;
1270 p->tex_Texture_SecondaryNormal = -1;
1271 p->tex_Texture_SecondaryColor = -1;
1272 p->tex_Texture_SecondaryGloss = -1;
1273 p->tex_Texture_SecondaryGlow = -1;
1274 p->tex_Texture_Pants = -1;
1275 p->tex_Texture_Shirt = -1;
1276 p->tex_Texture_FogHeightTexture = -1;
1277 p->tex_Texture_FogMask = -1;
1278 p->tex_Texture_LightGrid = -1;
1279 p->tex_Texture_Lightmap = -1;
1280 p->tex_Texture_Deluxemap = -1;
1281 p->tex_Texture_Attenuation = -1;
1282 p->tex_Texture_Cube = -1;
1283 p->tex_Texture_Refraction = -1;
1284 p->tex_Texture_Reflection = -1;
1285 p->tex_Texture_ShadowMap2D = -1;
1286 p->tex_Texture_CubeProjection = -1;
1287 p->tex_Texture_ScreenNormalMap = -1;
1288 p->tex_Texture_ScreenDiffuse = -1;
1289 p->tex_Texture_ScreenSpecular = -1;
1290 p->tex_Texture_ReflectMask = -1;
1291 p->tex_Texture_ReflectCube = -1;
1292 p->tex_Texture_BounceGrid = -1;
1293 // bind the texture samplers in use
1295 if (p->loc_Texture_First >= 0) {p->tex_Texture_First = sampler;qglUniform1i(p->loc_Texture_First , sampler);sampler++;}
1296 if (p->loc_Texture_Second >= 0) {p->tex_Texture_Second = sampler;qglUniform1i(p->loc_Texture_Second , sampler);sampler++;}
1297 if (p->loc_Texture_GammaRamps >= 0) {p->tex_Texture_GammaRamps = sampler;qglUniform1i(p->loc_Texture_GammaRamps , sampler);sampler++;}
1298 if (p->loc_Texture_Normal >= 0) {p->tex_Texture_Normal = sampler;qglUniform1i(p->loc_Texture_Normal , sampler);sampler++;}
1299 if (p->loc_Texture_Color >= 0) {p->tex_Texture_Color = sampler;qglUniform1i(p->loc_Texture_Color , sampler);sampler++;}
1300 if (p->loc_Texture_Gloss >= 0) {p->tex_Texture_Gloss = sampler;qglUniform1i(p->loc_Texture_Gloss , sampler);sampler++;}
1301 if (p->loc_Texture_Glow >= 0) {p->tex_Texture_Glow = sampler;qglUniform1i(p->loc_Texture_Glow , sampler);sampler++;}
1302 if (p->loc_Texture_SecondaryNormal >= 0) {p->tex_Texture_SecondaryNormal = sampler;qglUniform1i(p->loc_Texture_SecondaryNormal , sampler);sampler++;}
1303 if (p->loc_Texture_SecondaryColor >= 0) {p->tex_Texture_SecondaryColor = sampler;qglUniform1i(p->loc_Texture_SecondaryColor , sampler);sampler++;}
1304 if (p->loc_Texture_SecondaryGloss >= 0) {p->tex_Texture_SecondaryGloss = sampler;qglUniform1i(p->loc_Texture_SecondaryGloss , sampler);sampler++;}
1305 if (p->loc_Texture_SecondaryGlow >= 0) {p->tex_Texture_SecondaryGlow = sampler;qglUniform1i(p->loc_Texture_SecondaryGlow , sampler);sampler++;}
1306 if (p->loc_Texture_Pants >= 0) {p->tex_Texture_Pants = sampler;qglUniform1i(p->loc_Texture_Pants , sampler);sampler++;}
1307 if (p->loc_Texture_Shirt >= 0) {p->tex_Texture_Shirt = sampler;qglUniform1i(p->loc_Texture_Shirt , sampler);sampler++;}
1308 if (p->loc_Texture_FogHeightTexture>= 0) {p->tex_Texture_FogHeightTexture = sampler;qglUniform1i(p->loc_Texture_FogHeightTexture, sampler);sampler++;}
1309 if (p->loc_Texture_FogMask >= 0) {p->tex_Texture_FogMask = sampler;qglUniform1i(p->loc_Texture_FogMask , sampler);sampler++;}
1310 if (p->loc_Texture_LightGrid >= 0) {p->tex_Texture_LightGrid = sampler;qglUniform1i(p->loc_Texture_LightGrid , sampler);sampler++;}
1311 if (p->loc_Texture_Lightmap >= 0) {p->tex_Texture_Lightmap = sampler;qglUniform1i(p->loc_Texture_Lightmap , sampler);sampler++;}
1312 if (p->loc_Texture_Deluxemap >= 0) {p->tex_Texture_Deluxemap = sampler;qglUniform1i(p->loc_Texture_Deluxemap , sampler);sampler++;}
1313 if (p->loc_Texture_Attenuation >= 0) {p->tex_Texture_Attenuation = sampler;qglUniform1i(p->loc_Texture_Attenuation , sampler);sampler++;}
1314 if (p->loc_Texture_Cube >= 0) {p->tex_Texture_Cube = sampler;qglUniform1i(p->loc_Texture_Cube , sampler);sampler++;}
1315 if (p->loc_Texture_Refraction >= 0) {p->tex_Texture_Refraction = sampler;qglUniform1i(p->loc_Texture_Refraction , sampler);sampler++;}
1316 if (p->loc_Texture_Reflection >= 0) {p->tex_Texture_Reflection = sampler;qglUniform1i(p->loc_Texture_Reflection , sampler);sampler++;}
1317 if (p->loc_Texture_ShadowMap2D >= 0) {p->tex_Texture_ShadowMap2D = sampler;qglUniform1i(p->loc_Texture_ShadowMap2D , sampler);sampler++;}
1318 if (p->loc_Texture_CubeProjection >= 0) {p->tex_Texture_CubeProjection = sampler;qglUniform1i(p->loc_Texture_CubeProjection , sampler);sampler++;}
1319 if (p->loc_Texture_ScreenNormalMap >= 0) {p->tex_Texture_ScreenNormalMap = sampler;qglUniform1i(p->loc_Texture_ScreenNormalMap , sampler);sampler++;}
1320 if (p->loc_Texture_ScreenDiffuse >= 0) {p->tex_Texture_ScreenDiffuse = sampler;qglUniform1i(p->loc_Texture_ScreenDiffuse , sampler);sampler++;}
1321 if (p->loc_Texture_ScreenSpecular >= 0) {p->tex_Texture_ScreenSpecular = sampler;qglUniform1i(p->loc_Texture_ScreenSpecular , sampler);sampler++;}
1322 if (p->loc_Texture_ReflectMask >= 0) {p->tex_Texture_ReflectMask = sampler;qglUniform1i(p->loc_Texture_ReflectMask , sampler);sampler++;}
1323 if (p->loc_Texture_ReflectCube >= 0) {p->tex_Texture_ReflectCube = sampler;qglUniform1i(p->loc_Texture_ReflectCube , sampler);sampler++;}
1324 if (p->loc_Texture_BounceGrid >= 0) {p->tex_Texture_BounceGrid = sampler;qglUniform1i(p->loc_Texture_BounceGrid , sampler);sampler++;}
1325 // get the uniform block indices so we can bind them
1326 p->ubiloc_Skeletal_Transform12_UniformBlock = -1;
1327 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1328 p->ubiloc_Skeletal_Transform12_UniformBlock = qglGetUniformBlockIndex(p->program, "Skeletal_Transform12_UniformBlock");
1330 // clear the uniform block bindings
1331 p->ubibind_Skeletal_Transform12_UniformBlock = -1;
1332 // bind the uniform blocks in use
1334 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1335 if (p->ubiloc_Skeletal_Transform12_UniformBlock >= 0) {p->ubibind_Skeletal_Transform12_UniformBlock = ubibind;qglUniformBlockBinding(p->program, p->ubiloc_Skeletal_Transform12_UniformBlock, ubibind);ubibind++;}
1337 // we're done compiling and setting up the shader, at least until it is used
1339 Con_DPrintf("^5GLSL shader %s compiled (%i textures).\n", permutationname, sampler);
1342 Con_Printf("^1GLSL shader %s failed! some features may not work properly.\n", permutationname);
1346 Mem_Free(sourcestring);
1349 static void R_SetupShader_SetPermutationGLSL(unsigned int mode, uint64_t permutation)
1351 r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
1352 if (r_glsl_permutation != perm)
1354 r_glsl_permutation = perm;
1355 if (!r_glsl_permutation->program)
1357 if (!r_glsl_permutation->compiled)
1359 Con_DPrintf("Compiling shader mode %u permutation %" PRIx64 "\n", mode, permutation);
1360 R_GLSL_CompilePermutation(perm, mode, permutation);
1362 if (!r_glsl_permutation->program)
1364 // remove features until we find a valid permutation
1366 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1368 // reduce i more quickly whenever it would not remove any bits
1369 uint64_t j = 1ll<<(SHADERPERMUTATION_COUNT-1-i);
1370 if (!(permutation & j))
1373 r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
1374 if (!r_glsl_permutation->compiled)
1375 R_GLSL_CompilePermutation(perm, mode, permutation);
1376 if (r_glsl_permutation->program)
1379 if (i >= SHADERPERMUTATION_COUNT)
1381 //Con_Printf("Could not find a working OpenGL 2.0 shader for permutation %s %s\n", shadermodeinfo[mode].filename, shadermodeinfo[mode].pretext);
1382 r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
1383 qglUseProgram(0);CHECKGLERROR
1384 return; // no bit left to clear, entire mode is broken
1389 qglUseProgram(r_glsl_permutation->program);CHECKGLERROR
1391 if (r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
1392 if (r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
1393 if (r_glsl_permutation->loc_ClientTime >= 0) qglUniform1f(r_glsl_permutation->loc_ClientTime, cl.time);
1397 void R_GLSL_Restart_f(cmd_state_t *cmd)
1399 unsigned int i, limit;
1400 switch(vid.renderpath)
1402 case RENDERPATH_GL32:
1403 case RENDERPATH_GLES2:
1405 r_glsl_permutation_t *p;
1406 r_glsl_permutation = NULL;
1407 limit = (unsigned int)Mem_ExpandableArray_IndexRange(&r_glsl_permutationarray);
1408 for (i = 0;i < limit;i++)
1410 if ((p = (r_glsl_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_glsl_permutationarray, i)))
1412 GL_Backend_FreeProgram(p->program);
1413 Mem_ExpandableArray_FreeRecord(&r_glsl_permutationarray, (void*)p);
1416 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
1422 static void R_GLSL_DumpShader_f(cmd_state_t *cmd)
1424 int i, language, mode, dupe;
1426 shadermodeinfo_t *modeinfo;
1429 for (language = 0;language < SHADERLANGUAGE_COUNT;language++)
1431 modeinfo = shadermodeinfo[language];
1432 for (mode = 0;mode < SHADERMODE_COUNT;mode++)
1434 // don't dump the same file multiple times (most or all shaders come from the same file)
1435 for (dupe = mode - 1;dupe >= 0;dupe--)
1436 if (!strcmp(modeinfo[mode].filename, modeinfo[dupe].filename))
1440 text = modeinfo[mode].builtinstring;
1443 file = FS_OpenRealFile(modeinfo[mode].filename, "w", false);
1446 FS_Print(file, "/* The engine may define the following macros:\n");
1447 FS_Print(file, "#define VERTEX_SHADER\n#define GEOMETRY_SHADER\n#define FRAGMENT_SHADER\n");
1448 for (i = 0;i < SHADERMODE_COUNT;i++)
1449 FS_Print(file, modeinfo[i].pretext);
1450 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1451 FS_Print(file, shaderpermutationinfo[i].pretext);
1452 FS_Print(file, "*/\n");
1453 FS_Print(file, text);
1455 Con_Printf("%s written\n", modeinfo[mode].filename);
1458 Con_Printf(CON_ERROR "failed to write to %s\n", modeinfo[mode].filename);
1463 void R_SetupShader_Generic(rtexture_t *t, qbool usegamma, qbool notrippy, qbool suppresstexalpha)
1465 uint64_t permutation = 0;
1466 if (r_trippy.integer && !notrippy)
1467 permutation |= SHADERPERMUTATION_TRIPPY;
1468 permutation |= SHADERPERMUTATION_VIEWTINT;
1470 permutation |= SHADERPERMUTATION_DIFFUSE;
1471 if (usegamma && v_glslgamma_2d.integer && !vid.sRGB2D && r_texture_gammaramps && !vid_gammatables_trivial)
1472 permutation |= SHADERPERMUTATION_GAMMARAMPS;
1473 if (suppresstexalpha)
1474 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1475 if (vid.allowalphatocoverage)
1476 GL_AlphaToCoverage(false);
1477 switch (vid.renderpath)
1479 case RENDERPATH_GL32:
1480 case RENDERPATH_GLES2:
1481 R_SetupShader_SetPermutationGLSL(SHADERMODE_GENERIC, permutation);
1482 if (r_glsl_permutation->tex_Texture_First >= 0)
1483 R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First, t);
1484 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0)
1485 R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps);
1490 void R_SetupShader_Generic_NoTexture(qbool usegamma, qbool notrippy)
1492 R_SetupShader_Generic(NULL, usegamma, notrippy, false);
1495 void R_SetupShader_DepthOrShadow(qbool notrippy, qbool depthrgb, qbool skeletal)
1497 uint64_t permutation = 0;
1498 if (r_trippy.integer && !notrippy)
1499 permutation |= SHADERPERMUTATION_TRIPPY;
1501 permutation |= SHADERPERMUTATION_DEPTHRGB;
1503 permutation |= SHADERPERMUTATION_SKELETAL;
1505 if (vid.allowalphatocoverage)
1506 GL_AlphaToCoverage(false);
1507 switch (vid.renderpath)
1509 case RENDERPATH_GL32:
1510 case RENDERPATH_GLES2:
1511 R_SetupShader_SetPermutationGLSL(SHADERMODE_DEPTH_OR_SHADOW, permutation);
1512 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1513 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);
1519 #define BLENDFUNC_ALLOWS_COLORMOD 1
1520 #define BLENDFUNC_ALLOWS_FOG 2
1521 #define BLENDFUNC_ALLOWS_FOG_HACK0 4
1522 #define BLENDFUNC_ALLOWS_FOG_HACKALPHA 8
1523 #define BLENDFUNC_ALLOWS_ANYFOG (BLENDFUNC_ALLOWS_FOG | BLENDFUNC_ALLOWS_FOG_HACK0 | BLENDFUNC_ALLOWS_FOG_HACKALPHA)
1524 static int R_BlendFuncFlags(int src, int dst)
1528 // a blendfunc allows colormod if:
1529 // a) it can never keep the destination pixel invariant, or
1530 // b) it can keep the destination pixel invariant, and still can do so if colormodded
1531 // this is to prevent unintended side effects from colormod
1533 // a blendfunc allows fog if:
1534 // blend(fog(src), fog(dst)) == fog(blend(src, dst))
1535 // this is to prevent unintended side effects from fog
1537 // these checks are the output of fogeval.pl
1539 r |= BLENDFUNC_ALLOWS_COLORMOD;
1540 if(src == GL_DST_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1541 if(src == GL_DST_ALPHA && dst == GL_ONE_MINUS_DST_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1542 if(src == GL_DST_COLOR && dst == GL_ONE_MINUS_SRC_ALPHA) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1543 if(src == GL_DST_COLOR && dst == GL_ONE_MINUS_SRC_COLOR) r |= BLENDFUNC_ALLOWS_FOG;
1544 if(src == GL_DST_COLOR && dst == GL_SRC_ALPHA) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1545 if(src == GL_DST_COLOR && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1546 if(src == GL_DST_COLOR && dst == GL_ZERO) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1547 if(src == GL_ONE && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1548 if(src == GL_ONE && dst == GL_ONE_MINUS_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG_HACKALPHA;
1549 if(src == GL_ONE && dst == GL_ZERO) r |= BLENDFUNC_ALLOWS_FOG;
1550 if(src == GL_ONE_MINUS_DST_ALPHA && dst == GL_DST_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1551 if(src == GL_ONE_MINUS_DST_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1552 if(src == GL_ONE_MINUS_DST_COLOR && dst == GL_SRC_COLOR) r |= BLENDFUNC_ALLOWS_FOG;
1553 if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1554 if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1555 if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1556 if(src == GL_ONE_MINUS_SRC_COLOR && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1557 if(src == GL_SRC_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1558 if(src == GL_SRC_ALPHA && dst == GL_ONE_MINUS_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1559 if(src == GL_ZERO && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG;
1560 if(src == GL_ZERO && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1565 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)
1567 // select a permutation of the lighting shader appropriate to this
1568 // combination of texture, entity, light source, and fogging, only use the
1569 // minimum features necessary to avoid wasting rendering time in the
1570 // fragment shader on features that are not being used
1571 uint64_t permutation = 0;
1572 unsigned int mode = 0;
1574 texture_t *t = rsurface.texture;
1576 matrix4x4_t tempmatrix;
1577 r_waterstate_waterplane_t *waterplane = (r_waterstate_waterplane_t *)surfacewaterplane;
1578 if (r_trippy.integer && !notrippy)
1579 permutation |= SHADERPERMUTATION_TRIPPY;
1580 if (t->currentmaterialflags & MATERIALFLAG_ALPHATEST)
1581 permutation |= SHADERPERMUTATION_ALPHAKILL;
1582 if (t->currentmaterialflags & MATERIALFLAG_OCCLUDE)
1583 permutation |= SHADERPERMUTATION_OCCLUDE;
1584 if (t->r_water_waterscroll[0] && t->r_water_waterscroll[1])
1585 permutation |= SHADERPERMUTATION_NORMALMAPSCROLLBLEND; // todo: make generic
1586 if (rsurfacepass == RSURFPASS_BACKGROUND)
1588 // distorted background
1589 if (t->currentmaterialflags & MATERIALFLAG_WATERSHADER)
1591 mode = SHADERMODE_WATER;
1592 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1593 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1594 if((r_wateralpha.value < 1) && (t->currentmaterialflags & MATERIALFLAG_WATERALPHA))
1596 // this is the right thing to do for wateralpha
1597 GL_BlendFunc(GL_ONE, GL_ZERO);
1598 blendfuncflags = R_BlendFuncFlags(GL_ONE, GL_ZERO);
1602 // this is the right thing to do for entity alpha
1603 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1604 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1607 else if (t->currentmaterialflags & MATERIALFLAG_REFRACTION)
1609 mode = SHADERMODE_REFRACTION;
1610 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1611 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1612 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1613 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1617 mode = SHADERMODE_GENERIC;
1618 permutation |= SHADERPERMUTATION_DIFFUSE | SHADERPERMUTATION_ALPHAKILL;
1619 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1620 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1622 if (vid.allowalphatocoverage)
1623 GL_AlphaToCoverage(false);
1625 else if (rsurfacepass == RSURFPASS_DEFERREDGEOMETRY)
1627 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1629 switch(t->offsetmapping)
1631 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1632 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1633 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1634 case OFFSETMAPPING_OFF: break;
1637 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1638 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1639 // normalmap (deferred prepass), may use alpha test on diffuse
1640 mode = SHADERMODE_DEFERREDGEOMETRY;
1641 GL_BlendFunc(GL_ONE, GL_ZERO);
1642 blendfuncflags = R_BlendFuncFlags(GL_ONE, GL_ZERO);
1643 if (vid.allowalphatocoverage)
1644 GL_AlphaToCoverage(false);
1646 else if (rsurfacepass == RSURFPASS_RTLIGHT)
1648 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1650 switch(t->offsetmapping)
1652 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1653 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1654 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1655 case OFFSETMAPPING_OFF: break;
1658 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1659 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1660 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1661 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1663 mode = SHADERMODE_LIGHTSOURCE;
1664 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1665 permutation |= SHADERPERMUTATION_CUBEFILTER;
1666 if (VectorLength2(rtlightdiffuse) > 0)
1667 permutation |= SHADERPERMUTATION_DIFFUSE;
1668 if (VectorLength2(rtlightspecular) > 0)
1669 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1670 if (r_refdef.fogenabled)
1671 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1672 if (t->colormapping)
1673 permutation |= SHADERPERMUTATION_COLORMAPPING;
1674 if (r_shadow_usingshadowmap2d)
1676 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1677 if(r_shadow_shadowmapvsdct)
1678 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
1680 if (r_shadow_shadowmap2ddepthbuffer)
1681 permutation |= SHADERPERMUTATION_DEPTHRGB;
1683 if (t->reflectmasktexture)
1684 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1685 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1686 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE);
1687 if (vid.allowalphatocoverage)
1688 GL_AlphaToCoverage(false);
1690 else if (t->currentmaterialflags & MATERIALFLAG_LIGHTGRID)
1692 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1694 switch(t->offsetmapping)
1696 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1697 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1698 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1699 case OFFSETMAPPING_OFF: break;
1702 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1703 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1704 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1705 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1706 // directional model lighting
1707 mode = SHADERMODE_LIGHTGRID;
1708 if ((t->glowtexture || t->backgroundglowtexture) && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
1709 permutation |= SHADERPERMUTATION_GLOW;
1710 permutation |= SHADERPERMUTATION_DIFFUSE;
1711 if (t->glosstexture || t->backgroundglosstexture)
1712 permutation |= SHADERPERMUTATION_SPECULAR;
1713 if (r_refdef.fogenabled)
1714 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1715 if (t->colormapping)
1716 permutation |= SHADERPERMUTATION_COLORMAPPING;
1717 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
1719 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1720 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1722 if (r_shadow_shadowmap2ddepthbuffer)
1723 permutation |= SHADERPERMUTATION_DEPTHRGB;
1725 if (t->currentmaterialflags & MATERIALFLAG_REFLECTION)
1726 permutation |= SHADERPERMUTATION_REFLECTION;
1727 if (r_shadow_usingdeferredprepass && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
1728 permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
1729 if (t->reflectmasktexture)
1730 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1731 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1733 permutation |= SHADERPERMUTATION_BOUNCEGRID;
1734 if (r_shadow_bouncegrid_state.directional)
1735 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
1737 GL_BlendFunc(t->currentblendfunc[0], t->currentblendfunc[1]);
1738 blendfuncflags = R_BlendFuncFlags(t->currentblendfunc[0], t->currentblendfunc[1]);
1739 // when using alphatocoverage, we don't need alphakill
1740 if (vid.allowalphatocoverage)
1742 if (r_transparent_alphatocoverage.integer)
1744 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1745 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1748 GL_AlphaToCoverage(false);
1751 else if (t->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
1753 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1755 switch(t->offsetmapping)
1757 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1758 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1759 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1760 case OFFSETMAPPING_OFF: break;
1763 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1764 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1765 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1766 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1767 // directional model lighting
1768 mode = SHADERMODE_LIGHTDIRECTION;
1769 if ((t->glowtexture || t->backgroundglowtexture) && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
1770 permutation |= SHADERPERMUTATION_GLOW;
1771 if (VectorLength2(t->render_modellight_diffuse))
1772 permutation |= SHADERPERMUTATION_DIFFUSE;
1773 if (VectorLength2(t->render_modellight_specular) > 0)
1774 permutation |= SHADERPERMUTATION_SPECULAR;
1775 if (r_refdef.fogenabled)
1776 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1777 if (t->colormapping)
1778 permutation |= SHADERPERMUTATION_COLORMAPPING;
1779 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
1781 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1782 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1784 if (r_shadow_shadowmap2ddepthbuffer)
1785 permutation |= SHADERPERMUTATION_DEPTHRGB;
1787 if (t->currentmaterialflags & MATERIALFLAG_REFLECTION)
1788 permutation |= SHADERPERMUTATION_REFLECTION;
1789 if (r_shadow_usingdeferredprepass && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
1790 permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
1791 if (t->reflectmasktexture)
1792 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1793 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1795 permutation |= SHADERPERMUTATION_BOUNCEGRID;
1796 if (r_shadow_bouncegrid_state.directional)
1797 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
1799 GL_BlendFunc(t->currentblendfunc[0], t->currentblendfunc[1]);
1800 blendfuncflags = R_BlendFuncFlags(t->currentblendfunc[0], t->currentblendfunc[1]);
1801 // when using alphatocoverage, we don't need alphakill
1802 if (vid.allowalphatocoverage)
1804 if (r_transparent_alphatocoverage.integer)
1806 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1807 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1810 GL_AlphaToCoverage(false);
1815 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1817 switch(t->offsetmapping)
1819 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1820 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1821 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1822 case OFFSETMAPPING_OFF: break;
1825 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1826 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1827 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1828 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1830 if ((t->glowtexture || t->backgroundglowtexture) && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
1831 permutation |= SHADERPERMUTATION_GLOW;
1832 if (r_refdef.fogenabled && !ui)
1833 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1834 if (t->colormapping)
1835 permutation |= SHADERPERMUTATION_COLORMAPPING;
1836 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
1838 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1839 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1841 if (r_shadow_shadowmap2ddepthbuffer)
1842 permutation |= SHADERPERMUTATION_DEPTHRGB;
1844 if (t->currentmaterialflags & MATERIALFLAG_REFLECTION)
1845 permutation |= SHADERPERMUTATION_REFLECTION;
1846 if (r_shadow_usingdeferredprepass && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
1847 permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
1848 if (t->reflectmasktexture)
1849 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1850 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping)
1852 // deluxemapping (light direction texture)
1853 if (rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping && r_refdef.scene.worldmodel->brushq3.deluxemapping_modelspace)
1854 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
1856 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1857 permutation |= SHADERPERMUTATION_DIFFUSE;
1858 if (VectorLength2(t->render_lightmap_specular) > 0)
1859 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1861 else if (r_glsl_deluxemapping.integer >= 2)
1863 // fake deluxemapping (uniform light direction in tangentspace)
1864 if (rsurface.uselightmaptexture)
1865 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP;
1867 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR;
1868 permutation |= SHADERPERMUTATION_DIFFUSE;
1869 if (VectorLength2(t->render_lightmap_specular) > 0)
1870 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1872 else if (rsurface.uselightmaptexture)
1874 // ordinary lightmapping (q1bsp, q3bsp)
1875 mode = SHADERMODE_LIGHTMAP;
1879 // ordinary vertex coloring (q3bsp)
1880 mode = SHADERMODE_VERTEXCOLOR;
1882 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1884 permutation |= SHADERPERMUTATION_BOUNCEGRID;
1885 if (r_shadow_bouncegrid_state.directional)
1886 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
1888 GL_BlendFunc(t->currentblendfunc[0], t->currentblendfunc[1]);
1889 blendfuncflags = R_BlendFuncFlags(t->currentblendfunc[0], t->currentblendfunc[1]);
1890 // when using alphatocoverage, we don't need alphakill
1891 if (vid.allowalphatocoverage)
1893 if (r_transparent_alphatocoverage.integer)
1895 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1896 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1899 GL_AlphaToCoverage(false);
1902 if(!(blendfuncflags & BLENDFUNC_ALLOWS_ANYFOG))
1903 permutation &= ~(SHADERPERMUTATION_FOGHEIGHTTEXTURE | SHADERPERMUTATION_FOGOUTSIDE | SHADERPERMUTATION_FOGINSIDE);
1904 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACKALPHA && !ui)
1905 permutation |= SHADERPERMUTATION_FOGALPHAHACK;
1906 switch(vid.renderpath)
1908 case RENDERPATH_GL32:
1909 case RENDERPATH_GLES2:
1910 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);
1911 RSurf_UploadBuffersForBatch();
1912 // this has to be after RSurf_PrepareVerticesForBatch
1913 if (rsurface.batchskeletaltransform3x4buffer)
1914 permutation |= SHADERPERMUTATION_SKELETAL;
1915 R_SetupShader_SetPermutationGLSL(mode, permutation);
1916 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1917 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);
1919 if (r_glsl_permutation->loc_ModelToReflectCube >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.matrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ModelToReflectCube, 1, false, m16f);}
1920 if (mode == SHADERMODE_LIGHTSOURCE)
1922 if (r_glsl_permutation->loc_ModelToLight >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ModelToLight, 1, false, m16f);}
1923 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3f(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
1924 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3f(r_glsl_permutation->loc_LightColor, 1, 1, 1); // DEPRECATED
1925 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, rtlightambient[0], rtlightambient[1], rtlightambient[2]);
1926 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, rtlightdiffuse[0], rtlightdiffuse[1], rtlightdiffuse[2]);
1927 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, rtlightspecular[0], rtlightspecular[1], rtlightspecular[2]);
1929 // additive passes are only darkened by fog, not tinted
1930 if (r_glsl_permutation->loc_FogColor >= 0)
1931 qglUniform3f(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1932 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);
1936 if (mode == SHADERMODE_FLATCOLOR)
1938 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]);
1940 else if (mode == SHADERMODE_LIGHTGRID)
1942 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]);
1943 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]);
1944 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]);
1945 // other LightGrid uniforms handled below
1947 else if (mode == SHADERMODE_LIGHTDIRECTION)
1949 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]);
1950 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]);
1951 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]);
1952 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]);
1953 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]);
1954 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3f(r_glsl_permutation->loc_LightColor, 1, 1, 1); // DEPRECATED
1955 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]);
1959 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]);
1960 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]);
1961 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]);
1962 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]);
1963 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]);
1965 // additive passes are only darkened by fog, not tinted
1966 if (r_glsl_permutation->loc_FogColor >= 0 && !ui)
1968 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACK0)
1969 qglUniform3f(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1971 qglUniform3f(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
1973 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);
1974 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]);
1975 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]);
1976 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);
1977 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);
1978 if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1f(r_glsl_permutation->loc_ReflectFactor, t->reflectmax - t->reflectmin);
1979 if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1f(r_glsl_permutation->loc_ReflectOffset, t->reflectmin);
1980 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);
1981 if (r_glsl_permutation->loc_NormalmapScrollBlend >= 0) qglUniform2f(r_glsl_permutation->loc_NormalmapScrollBlend, t->r_water_waterscroll[0], t->r_water_waterscroll[1]);
1983 if (r_glsl_permutation->loc_TexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&t->currenttexmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_TexMatrix, 1, false, m16f);}
1984 if (r_glsl_permutation->loc_BackgroundTexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&t->currentbackgroundtexmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_BackgroundTexMatrix, 1, false, m16f);}
1985 if (r_glsl_permutation->loc_ShadowMapMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&r_shadow_shadowmapmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ShadowMapMatrix, 1, false, m16f);}
1986 if (permutation & SHADERPERMUTATION_SHADOWMAPORTHO)
1988 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]);
1989 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]);
1993 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]);
1994 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]);
1997 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]);
1998 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));
1999 if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3f(r_glsl_permutation->loc_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
2000 if (r_glsl_permutation->loc_Color_Pants >= 0)
2002 if (t->pantstexture)
2003 qglUniform3f(r_glsl_permutation->loc_Color_Pants, t->render_colormap_pants[0], t->render_colormap_pants[1], t->render_colormap_pants[2]);
2005 qglUniform3f(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
2007 if (r_glsl_permutation->loc_Color_Shirt >= 0)
2009 if (t->shirttexture)
2010 qglUniform3f(r_glsl_permutation->loc_Color_Shirt, t->render_colormap_shirt[0], t->render_colormap_shirt[1], t->render_colormap_shirt[2]);
2012 qglUniform3f(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
2014 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]);
2015 if (r_glsl_permutation->loc_FogPlaneViewDist >= 0) qglUniform1f(r_glsl_permutation->loc_FogPlaneViewDist, rsurface.fogplaneviewdist);
2016 if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1f(r_glsl_permutation->loc_FogRangeRecip, rsurface.fograngerecip);
2017 if (r_glsl_permutation->loc_FogHeightFade >= 0) qglUniform1f(r_glsl_permutation->loc_FogHeightFade, rsurface.fogheightfade);
2018 if (r_glsl_permutation->loc_OffsetMapping_ScaleSteps >= 0) qglUniform4f(r_glsl_permutation->loc_OffsetMapping_ScaleSteps,
2019 r_glsl_offsetmapping_scale.value*t->offsetscale,
2020 max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2021 1.0 / max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2022 max(1, r_glsl_offsetmapping_reliefmapping_refinesteps.integer)
2024 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);
2025 if (r_glsl_permutation->loc_OffsetMapping_Bias >= 0) qglUniform1f(r_glsl_permutation->loc_OffsetMapping_Bias, t->offsetbias);
2026 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]);
2027 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
2028 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);}
2029 if (r_glsl_permutation->loc_BounceGridIntensity >= 0) qglUniform1f(r_glsl_permutation->loc_BounceGridIntensity, r_shadow_bouncegrid_state.intensity*r_refdef.view.colorscale);
2030 if (r_glsl_permutation->loc_LightGridMatrix >= 0 && r_refdef.scene.worldmodel)
2033 Matrix4x4_Concat(&tempmatrix, &r_refdef.scene.worldmodel->brushq3.lightgridworldtotexturematrix, &rsurface.matrix);
2034 Matrix4x4_ToArrayFloatGL(&tempmatrix, m16f);
2035 qglUniformMatrix4fv(r_glsl_permutation->loc_LightGridMatrix, 1, false, m16f);
2036 Matrix4x4_Normalize3(&tempmatrix, &rsurface.matrix);
2037 Matrix4x4_ToArrayFloatGL(&tempmatrix, m16f);
2038 m9f[0] = m16f[0];m9f[1] = m16f[1];m9f[2] = m16f[2];
2039 m9f[3] = m16f[4];m9f[4] = m16f[5];m9f[5] = m16f[6];
2040 m9f[6] = m16f[8];m9f[7] = m16f[9];m9f[8] = m16f[10];
2041 qglUniformMatrix3fv(r_glsl_permutation->loc_LightGridNormalMatrix, 1, false, m9f);
2044 if (r_glsl_permutation->tex_Texture_First >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , r_texture_white );
2045 if (r_glsl_permutation->tex_Texture_Second >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second , r_texture_white );
2046 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps , r_texture_gammaramps );
2047 if (r_glsl_permutation->tex_Texture_Normal >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Normal , t->nmaptexture );
2048 if (r_glsl_permutation->tex_Texture_Color >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Color , t->basetexture );
2049 if (r_glsl_permutation->tex_Texture_Gloss >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Gloss , t->glosstexture );
2050 if (r_glsl_permutation->tex_Texture_Glow >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Glow , t->glowtexture );
2051 if (r_glsl_permutation->tex_Texture_SecondaryNormal >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryNormal , t->backgroundnmaptexture );
2052 if (r_glsl_permutation->tex_Texture_SecondaryColor >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryColor , t->backgroundbasetexture );
2053 if (r_glsl_permutation->tex_Texture_SecondaryGloss >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryGloss , t->backgroundglosstexture );
2054 if (r_glsl_permutation->tex_Texture_SecondaryGlow >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryGlow , t->backgroundglowtexture );
2055 if (r_glsl_permutation->tex_Texture_Pants >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Pants , t->pantstexture );
2056 if (r_glsl_permutation->tex_Texture_Shirt >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Shirt , t->shirttexture );
2057 if (r_glsl_permutation->tex_Texture_ReflectMask >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ReflectMask , t->reflectmasktexture );
2058 if (r_glsl_permutation->tex_Texture_ReflectCube >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ReflectCube , t->reflectcubetexture ? t->reflectcubetexture : r_texture_whitecube);
2059 if (r_glsl_permutation->tex_Texture_FogHeightTexture>= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_FogHeightTexture , r_texture_fogheighttexture );
2060 if (r_glsl_permutation->tex_Texture_FogMask >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_FogMask , r_texture_fogattenuation );
2061 if (r_glsl_permutation->tex_Texture_Lightmap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Lightmap , rsurface.lightmaptexture ? rsurface.lightmaptexture : r_texture_white);
2062 if (r_glsl_permutation->tex_Texture_Deluxemap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Deluxemap , rsurface.deluxemaptexture ? rsurface.deluxemaptexture : r_texture_blanknormalmap);
2063 if (r_glsl_permutation->tex_Texture_Attenuation >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Attenuation , r_shadow_attenuationgradienttexture );
2064 if (rsurfacepass == RSURFPASS_BACKGROUND)
2066 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);
2067 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);
2068 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);
2072 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);
2074 if (r_glsl_permutation->tex_Texture_ScreenNormalMap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenNormalMap , r_shadow_prepassgeometrynormalmaptexture );
2075 if (r_glsl_permutation->tex_Texture_ScreenDiffuse >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenDiffuse , r_shadow_prepasslightingdiffusetexture );
2076 if (r_glsl_permutation->tex_Texture_ScreenSpecular >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenSpecular , r_shadow_prepasslightingspeculartexture );
2077 if (rsurface.rtlight || (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW)))
2079 if (r_glsl_permutation->tex_Texture_ShadowMap2D >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ShadowMap2D, r_shadow_shadowmap2ddepthtexture );
2080 if (rsurface.rtlight)
2082 if (r_glsl_permutation->tex_Texture_Cube >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Cube , rsurface.rtlight->currentcubemap );
2083 if (r_glsl_permutation->tex_Texture_CubeProjection >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_CubeProjection , r_shadow_shadowmapvsdcttexture );
2086 if (r_glsl_permutation->tex_Texture_BounceGrid >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_BounceGrid, r_shadow_bouncegrid_state.texture);
2087 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);
2093 void R_SetupShader_DeferredLight(const rtlight_t *rtlight)
2095 // select a permutation of the lighting shader appropriate to this
2096 // combination of texture, entity, light source, and fogging, only use the
2097 // minimum features necessary to avoid wasting rendering time in the
2098 // fragment shader on features that are not being used
2099 uint64_t permutation = 0;
2100 unsigned int mode = 0;
2101 const float *lightcolorbase = rtlight->currentcolor;
2102 float ambientscale = rtlight->ambientscale;
2103 float diffusescale = rtlight->diffusescale;
2104 float specularscale = rtlight->specularscale;
2105 // this is the location of the light in view space
2106 vec3_t viewlightorigin;
2107 // this transforms from view space (camera) to light space (cubemap)
2108 matrix4x4_t viewtolight;
2109 matrix4x4_t lighttoview;
2110 float viewtolight16f[16];
2112 mode = SHADERMODE_DEFERREDLIGHTSOURCE;
2113 if (rtlight->currentcubemap != r_texture_whitecube)
2114 permutation |= SHADERPERMUTATION_CUBEFILTER;
2115 if (diffusescale > 0)
2116 permutation |= SHADERPERMUTATION_DIFFUSE;
2117 if (specularscale > 0 && r_shadow_gloss.integer > 0)
2118 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2119 if (r_shadow_usingshadowmap2d)
2121 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2122 if (r_shadow_shadowmapvsdct)
2123 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
2125 if (r_shadow_shadowmap2ddepthbuffer)
2126 permutation |= SHADERPERMUTATION_DEPTHRGB;
2128 if (vid.allowalphatocoverage)
2129 GL_AlphaToCoverage(false);
2130 Matrix4x4_Transform(&r_refdef.view.viewport.viewmatrix, rtlight->shadoworigin, viewlightorigin);
2131 Matrix4x4_Concat(&lighttoview, &r_refdef.view.viewport.viewmatrix, &rtlight->matrix_lighttoworld);
2132 Matrix4x4_Invert_Full(&viewtolight, &lighttoview);
2133 Matrix4x4_ToArrayFloatGL(&viewtolight, viewtolight16f);
2134 switch(vid.renderpath)
2136 case RENDERPATH_GL32:
2137 case RENDERPATH_GLES2:
2138 R_SetupShader_SetPermutationGLSL(mode, permutation);
2139 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3f( r_glsl_permutation->loc_LightPosition , viewlightorigin[0], viewlightorigin[1], viewlightorigin[2]);
2140 if (r_glsl_permutation->loc_ViewToLight >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ViewToLight , 1, false, viewtolight16f);
2141 if (r_glsl_permutation->loc_DeferredColor_Ambient >= 0) qglUniform3f( r_glsl_permutation->loc_DeferredColor_Ambient , lightcolorbase[0] * ambientscale , lightcolorbase[1] * ambientscale , lightcolorbase[2] * ambientscale );
2142 if (r_glsl_permutation->loc_DeferredColor_Diffuse >= 0) qglUniform3f( r_glsl_permutation->loc_DeferredColor_Diffuse , lightcolorbase[0] * diffusescale , lightcolorbase[1] * diffusescale , lightcolorbase[2] * diffusescale );
2143 if (r_glsl_permutation->loc_DeferredColor_Specular >= 0) qglUniform3f( r_glsl_permutation->loc_DeferredColor_Specular , lightcolorbase[0] * specularscale, lightcolorbase[1] * specularscale, lightcolorbase[2] * specularscale);
2144 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]);
2145 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]);
2146 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);
2147 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]);
2148 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f( r_glsl_permutation->loc_PixelToScreenTexCoord , 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
2150 if (r_glsl_permutation->tex_Texture_Attenuation >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Attenuation , r_shadow_attenuationgradienttexture );
2151 if (r_glsl_permutation->tex_Texture_ScreenNormalMap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenNormalMap , r_shadow_prepassgeometrynormalmaptexture );
2152 if (r_glsl_permutation->tex_Texture_Cube >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Cube , rsurface.rtlight->currentcubemap );
2153 if (r_glsl_permutation->tex_Texture_ShadowMap2D >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ShadowMap2D , r_shadow_shadowmap2ddepthtexture );
2154 if (r_glsl_permutation->tex_Texture_CubeProjection >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_CubeProjection , r_shadow_shadowmapvsdcttexture );
2159 #define SKINFRAME_HASH 1024
2163 unsigned int loadsequence; // incremented each level change
2164 memexpandablearray_t array;
2165 skinframe_t *hash[SKINFRAME_HASH];
2168 r_skinframe_t r_skinframe;
2170 void R_SkinFrame_PrepareForPurge(void)
2172 r_skinframe.loadsequence++;
2173 // wrap it without hitting zero
2174 if (r_skinframe.loadsequence >= 200)
2175 r_skinframe.loadsequence = 1;
2178 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
2182 // mark the skinframe as used for the purging code
2183 skinframe->loadsequence = r_skinframe.loadsequence;
2186 void R_SkinFrame_PurgeSkinFrame(skinframe_t *s)
2190 if (s->merged == s->base)
2192 R_PurgeTexture(s->stain); s->stain = NULL;
2193 R_PurgeTexture(s->merged); s->merged = NULL;
2194 R_PurgeTexture(s->base); s->base = NULL;
2195 R_PurgeTexture(s->pants); s->pants = NULL;
2196 R_PurgeTexture(s->shirt); s->shirt = NULL;
2197 R_PurgeTexture(s->nmap); s->nmap = NULL;
2198 R_PurgeTexture(s->gloss); s->gloss = NULL;
2199 R_PurgeTexture(s->glow); s->glow = NULL;
2200 R_PurgeTexture(s->fog); s->fog = NULL;
2201 R_PurgeTexture(s->reflect); s->reflect = NULL;
2202 s->loadsequence = 0;
2205 void R_SkinFrame_Purge(void)
2209 for (i = 0;i < SKINFRAME_HASH;i++)
2211 for (s = r_skinframe.hash[i];s;s = s->next)
2213 if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
2214 R_SkinFrame_PurgeSkinFrame(s);
2219 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
2221 char basename[MAX_QPATH];
2223 Image_StripImageExtension(name, basename, sizeof(basename));
2225 if( last == NULL ) {
2227 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2228 item = r_skinframe.hash[hashindex];
2233 // linearly search through the hash bucket
2234 for( ; item ; item = item->next ) {
2235 if( !strcmp( item->basename, basename ) ) {
2242 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qbool add)
2245 int compareflags = textureflags & TEXF_IMPORTANTBITS;
2247 char basename[MAX_QPATH];
2249 Image_StripImageExtension(name, basename, sizeof(basename));
2251 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2252 for (item = r_skinframe.hash[hashindex];item;item = item->next)
2253 if (!strcmp(item->basename, basename) &&
2254 item->textureflags == compareflags &&
2255 item->comparewidth == comparewidth &&
2256 item->compareheight == compareheight &&
2257 item->comparecrc == comparecrc)
2264 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
2265 memset(item, 0, sizeof(*item));
2266 strlcpy(item->basename, basename, sizeof(item->basename));
2267 item->textureflags = compareflags;
2268 item->comparewidth = comparewidth;
2269 item->compareheight = compareheight;
2270 item->comparecrc = comparecrc;
2271 item->next = r_skinframe.hash[hashindex];
2272 r_skinframe.hash[hashindex] = item;
2274 else if (textureflags & TEXF_FORCE_RELOAD)
2275 R_SkinFrame_PurgeSkinFrame(item);
2277 R_SkinFrame_MarkUsed(item);
2281 #define R_SKINFRAME_LOAD_AVERAGE_COLORS(cnt, getpixel) \
2283 unsigned long long avgcolor[5], wsum; \
2291 for(pix = 0; pix < cnt; ++pix) \
2294 for(comp = 0; comp < 3; ++comp) \
2296 if(w) /* ignore perfectly black pixels because that is better for model skins */ \
2299 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2301 for(comp = 0; comp < 3; ++comp) \
2302 avgcolor[comp] += getpixel * w; \
2305 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2306 avgcolor[4] += getpixel; \
2308 if(avgcolor[3] == 0) /* no pixels seen? even worse */ \
2310 skinframe->avgcolor[0] = avgcolor[2] / (255.0 * avgcolor[3]); \
2311 skinframe->avgcolor[1] = avgcolor[1] / (255.0 * avgcolor[3]); \
2312 skinframe->avgcolor[2] = avgcolor[0] / (255.0 * avgcolor[3]); \
2313 skinframe->avgcolor[3] = avgcolor[4] / (255.0 * cnt); \
2316 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qbool complain, qbool fallbacknotexture)
2318 skinframe_t *skinframe;
2320 if (cls.state == ca_dedicated)
2323 // return an existing skinframe if already loaded
2324 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
2325 if (skinframe && skinframe->base)
2328 // if the skinframe doesn't exist this will create it
2329 return R_SkinFrame_LoadExternal_SkinFrame(skinframe, name, textureflags, complain, fallbacknotexture);
2332 extern cvar_t gl_picmip;
2333 skinframe_t *R_SkinFrame_LoadExternal_SkinFrame(skinframe_t *skinframe, const char *name, int textureflags, qbool complain, qbool fallbacknotexture)
2336 unsigned char *pixels;
2337 unsigned char *bumppixels;
2338 unsigned char *basepixels = NULL;
2339 int basepixels_width = 0;
2340 int basepixels_height = 0;
2341 rtexture_t *ddsbase = NULL;
2342 qbool ddshasalpha = false;
2343 float ddsavgcolor[4];
2344 char basename[MAX_QPATH];
2345 int miplevel = R_PicmipForFlags(textureflags);
2346 int savemiplevel = miplevel;
2350 if (cls.state == ca_dedicated)
2353 Image_StripImageExtension(name, basename, sizeof(basename));
2355 // check for DDS texture file first
2356 if (!r_loaddds || !(ddsbase = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s.dds", basename), vid.sRGB3D, textureflags, &ddshasalpha, ddsavgcolor, miplevel, false)))
2358 basepixels = loadimagepixelsbgra(name, complain, true, false, &miplevel);
2359 if (basepixels == NULL && fallbacknotexture)
2360 basepixels = Image_GenerateNoTexture();
2361 if (basepixels == NULL)
2365 // FIXME handle miplevel
2367 if (developer_loading.integer)
2368 Con_Printf("loading skin \"%s\"\n", name);
2370 // we've got some pixels to store, so really allocate this new texture now
2372 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
2373 textureflags &= ~TEXF_FORCE_RELOAD;
2374 skinframe->stain = NULL;
2375 skinframe->merged = NULL;
2376 skinframe->base = NULL;
2377 skinframe->pants = NULL;
2378 skinframe->shirt = NULL;
2379 skinframe->nmap = NULL;
2380 skinframe->gloss = NULL;
2381 skinframe->glow = NULL;
2382 skinframe->fog = NULL;
2383 skinframe->reflect = NULL;
2384 skinframe->hasalpha = false;
2385 // we could store the q2animname here too
2389 skinframe->base = ddsbase;
2390 skinframe->hasalpha = ddshasalpha;
2391 VectorCopy(ddsavgcolor, skinframe->avgcolor);
2392 if (r_loadfog && skinframe->hasalpha)
2393 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);
2394 //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]);
2398 basepixels_width = image_width;
2399 basepixels_height = image_height;
2400 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);
2401 if (textureflags & TEXF_ALPHA)
2403 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
2405 if (basepixels[j] < 255)
2407 skinframe->hasalpha = true;
2411 if (r_loadfog && skinframe->hasalpha)
2413 // has transparent pixels
2414 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2415 for (j = 0;j < image_width * image_height * 4;j += 4)
2420 pixels[j+3] = basepixels[j+3];
2422 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);
2426 R_SKINFRAME_LOAD_AVERAGE_COLORS(basepixels_width * basepixels_height, basepixels[4 * pix + comp]);
2428 //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]);
2429 if (r_savedds && skinframe->base)
2430 R_SaveTextureDDSFile(skinframe->base, va(vabuf, sizeof(vabuf), "dds/%s.dds", skinframe->basename), r_texture_dds_save.integer < 2, skinframe->hasalpha);
2431 if (r_savedds && skinframe->fog)
2432 R_SaveTextureDDSFile(skinframe->fog, va(vabuf, sizeof(vabuf), "dds/%s_mask.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2438 mymiplevel = savemiplevel;
2439 if (r_loadnormalmap)
2440 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);
2441 skinframe->glow = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_glow.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2443 skinframe->gloss = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_gloss.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2444 skinframe->pants = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_pants.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2445 skinframe->shirt = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_shirt.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2446 skinframe->reflect = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_reflect.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2449 // _norm is the name used by tenebrae and has been adopted as standard
2450 if (r_loadnormalmap && skinframe->nmap == NULL)
2452 mymiplevel = savemiplevel;
2453 if ((pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_norm", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
2455 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);
2459 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_bump", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
2461 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2462 Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
2463 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);
2465 Mem_Free(bumppixels);
2467 else if (r_shadow_bumpscale_basetexture.value > 0)
2469 pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
2470 Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
2471 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);
2475 if (r_savedds && skinframe->nmap)
2476 R_SaveTextureDDSFile(skinframe->nmap, va(vabuf, sizeof(vabuf), "dds/%s_norm.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2480 // _luma is supported only for tenebrae compatibility
2481 // _blend and .blend are supported only for Q3 & QL compatibility, this hack can be removed if better Q3 shader support is implemented
2482 // _glow is the preferred name
2483 mymiplevel = savemiplevel;
2484 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))))
2486 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);
2488 if (r_savedds && skinframe->glow)
2489 R_SaveTextureDDSFile(skinframe->glow, va(vabuf, sizeof(vabuf), "dds/%s_glow.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2491 Mem_Free(pixels);pixels = NULL;
2494 mymiplevel = savemiplevel;
2495 if (skinframe->gloss == NULL && r_loadgloss && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_gloss", skinframe->basename), false, false, false, &mymiplevel)))
2497 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);
2499 if (r_savedds && skinframe->gloss)
2500 R_SaveTextureDDSFile(skinframe->gloss, va(vabuf, sizeof(vabuf), "dds/%s_gloss.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2506 mymiplevel = savemiplevel;
2507 if (skinframe->pants == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_pants", skinframe->basename), false, false, false, &mymiplevel)))
2509 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);
2511 if (r_savedds && skinframe->pants)
2512 R_SaveTextureDDSFile(skinframe->pants, va(vabuf, sizeof(vabuf), "dds/%s_pants.dds", skinframe->basename), r_texture_dds_save.integer < 2, false);
2518 mymiplevel = savemiplevel;
2519 if (skinframe->shirt == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_shirt", skinframe->basename), false, false, false, &mymiplevel)))
2521 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);
2523 if (r_savedds && skinframe->shirt)
2524 R_SaveTextureDDSFile(skinframe->shirt, va(vabuf, sizeof(vabuf), "dds/%s_shirt.dds", skinframe->basename), r_texture_dds_save.integer < 2, false);
2530 mymiplevel = savemiplevel;
2531 if (skinframe->reflect == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_reflect", skinframe->basename), false, false, false, &mymiplevel)))
2533 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);
2535 if (r_savedds && skinframe->reflect)
2536 R_SaveTextureDDSFile(skinframe->reflect, va(vabuf, sizeof(vabuf), "dds/%s_reflect.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2543 Mem_Free(basepixels);
2548 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)
2551 skinframe_t *skinframe;
2554 if (cls.state == ca_dedicated)
2557 // if already loaded just return it, otherwise make a new skinframe
2558 skinframe = R_SkinFrame_Find(name, textureflags, comparewidth, compareheight, comparecrc, true);
2559 if (skinframe->base)
2561 textureflags &= ~TEXF_FORCE_RELOAD;
2563 skinframe->stain = NULL;
2564 skinframe->merged = NULL;
2565 skinframe->base = NULL;
2566 skinframe->pants = NULL;
2567 skinframe->shirt = NULL;
2568 skinframe->nmap = NULL;
2569 skinframe->gloss = NULL;
2570 skinframe->glow = NULL;
2571 skinframe->fog = NULL;
2572 skinframe->reflect = NULL;
2573 skinframe->hasalpha = false;
2575 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2579 if (developer_loading.integer)
2580 Con_Printf("loading 32bit skin \"%s\"\n", name);
2582 if (r_loadnormalmap && r_shadow_bumpscale_basetexture.value > 0)
2584 unsigned char *a = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2585 unsigned char *b = a + width * height * 4;
2586 Image_HeightmapToNormalmap_BGRA(skindata, b, width, height, false, r_shadow_bumpscale_basetexture.value);
2587 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);
2590 skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, sRGB ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags, -1, NULL);
2591 if (textureflags & TEXF_ALPHA)
2593 for (i = 3;i < width * height * 4;i += 4)
2595 if (skindata[i] < 255)
2597 skinframe->hasalpha = true;
2601 if (r_loadfog && skinframe->hasalpha)
2603 unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
2604 memcpy(fogpixels, skindata, width * height * 4);
2605 for (i = 0;i < width * height * 4;i += 4)
2606 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
2607 skinframe->fog = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, textureflags, -1, NULL);
2608 Mem_Free(fogpixels);
2612 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, skindata[4 * pix + comp]);
2613 //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]);
2618 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
2622 skinframe_t *skinframe;
2624 if (cls.state == ca_dedicated)
2627 // if already loaded just return it, otherwise make a new skinframe
2628 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
2629 if (skinframe->base)
2631 //textureflags &= ~TEXF_FORCE_RELOAD;
2633 skinframe->stain = NULL;
2634 skinframe->merged = NULL;
2635 skinframe->base = NULL;
2636 skinframe->pants = NULL;
2637 skinframe->shirt = NULL;
2638 skinframe->nmap = NULL;
2639 skinframe->gloss = NULL;
2640 skinframe->glow = NULL;
2641 skinframe->fog = NULL;
2642 skinframe->reflect = NULL;
2643 skinframe->hasalpha = false;
2645 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2649 if (developer_loading.integer)
2650 Con_Printf("loading quake skin \"%s\"\n", name);
2652 // 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)
2653 skinframe->qpixels = (unsigned char *)Mem_Alloc(r_main_mempool, width*height); // FIXME LEAK
2654 memcpy(skinframe->qpixels, skindata, width*height);
2655 skinframe->qwidth = width;
2656 skinframe->qheight = height;
2659 for (i = 0;i < width * height;i++)
2660 featuresmask |= palette_featureflags[skindata[i]];
2662 skinframe->hasalpha = false;
2665 skinframe->hasalpha = true;
2666 skinframe->qhascolormapping = loadpantsandshirt && (featuresmask & (PALETTEFEATURE_PANTS | PALETTEFEATURE_SHIRT));
2667 skinframe->qgeneratenmap = r_shadow_bumpscale_basetexture.value > 0;
2668 skinframe->qgeneratemerged = true;
2669 skinframe->qgeneratebase = skinframe->qhascolormapping;
2670 skinframe->qgenerateglow = loadglowtexture && (featuresmask & PALETTEFEATURE_GLOW);
2672 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette_bgra_complete)[skindata[pix]*4 + comp]);
2673 //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]);
2678 static void R_SkinFrame_GenerateTexturesFromQPixels(skinframe_t *skinframe, qbool colormapped)
2682 unsigned char *skindata;
2685 if (!skinframe->qpixels)
2688 if (!skinframe->qhascolormapping)
2689 colormapped = false;
2693 if (!skinframe->qgeneratebase)
2698 if (!skinframe->qgeneratemerged)
2702 width = skinframe->qwidth;
2703 height = skinframe->qheight;
2704 skindata = skinframe->qpixels;
2706 if (skinframe->qgeneratenmap)
2708 unsigned char *a, *b;
2709 skinframe->qgeneratenmap = false;
2710 a = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2711 b = a + width * height * 4;
2712 // use either a custom palette or the quake palette
2713 Image_Copy8bitBGRA(skindata, a, width * height, palette_bgra_complete);
2714 Image_HeightmapToNormalmap_BGRA(a, b, width, height, false, r_shadow_bumpscale_basetexture.value);
2715 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);
2719 if (skinframe->qgenerateglow)
2721 skinframe->qgenerateglow = false;
2722 if (skinframe->hasalpha) // fence textures
2723 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
2725 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
2730 skinframe->qgeneratebase = false;
2731 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);
2732 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);
2733 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);
2737 skinframe->qgeneratemerged = false;
2738 if (skinframe->hasalpha) // fence textures
2739 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);
2741 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);
2744 if (!skinframe->qgeneratemerged && !skinframe->qgeneratebase)
2746 Mem_Free(skinframe->qpixels);
2747 skinframe->qpixels = NULL;
2751 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)
2754 skinframe_t *skinframe;
2757 if (cls.state == ca_dedicated)
2760 // if already loaded just return it, otherwise make a new skinframe
2761 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
2762 if (skinframe->base)
2764 textureflags &= ~TEXF_FORCE_RELOAD;
2766 skinframe->stain = NULL;
2767 skinframe->merged = NULL;
2768 skinframe->base = NULL;
2769 skinframe->pants = NULL;
2770 skinframe->shirt = NULL;
2771 skinframe->nmap = NULL;
2772 skinframe->gloss = NULL;
2773 skinframe->glow = NULL;
2774 skinframe->fog = NULL;
2775 skinframe->reflect = NULL;
2776 skinframe->hasalpha = false;
2778 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2782 if (developer_loading.integer)
2783 Con_Printf("loading embedded 8bit image \"%s\"\n", name);
2785 skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_PALETTE, textureflags, -1, palette);
2786 if ((textureflags & TEXF_ALPHA) && alphapalette)
2788 for (i = 0;i < width * height;i++)
2790 if (((unsigned char *)palette)[skindata[i]*4+3] < 255)
2792 skinframe->hasalpha = true;
2796 if (r_loadfog && skinframe->hasalpha)
2797 skinframe->fog = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_fog", skinframe->basename), width, height, skindata, TEXTYPE_PALETTE, textureflags, -1, alphapalette);
2800 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette)[skindata[pix]*4 + comp]);
2801 //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]);
2806 skinframe_t *R_SkinFrame_LoadMissing(void)
2808 skinframe_t *skinframe;
2810 if (cls.state == ca_dedicated)
2813 skinframe = R_SkinFrame_Find("missing", TEXF_FORCENEAREST, 0, 0, 0, true);
2814 skinframe->stain = NULL;
2815 skinframe->merged = NULL;
2816 skinframe->base = NULL;
2817 skinframe->pants = NULL;
2818 skinframe->shirt = NULL;
2819 skinframe->nmap = NULL;
2820 skinframe->gloss = NULL;
2821 skinframe->glow = NULL;
2822 skinframe->fog = NULL;
2823 skinframe->reflect = NULL;
2824 skinframe->hasalpha = false;
2826 skinframe->avgcolor[0] = rand() / RAND_MAX;
2827 skinframe->avgcolor[1] = rand() / RAND_MAX;
2828 skinframe->avgcolor[2] = rand() / RAND_MAX;
2829 skinframe->avgcolor[3] = 1;
2834 skinframe_t *R_SkinFrame_LoadNoTexture(void)
2836 if (cls.state == ca_dedicated)
2839 return R_SkinFrame_LoadInternalBGRA("notexture", TEXF_FORCENEAREST, Image_GenerateNoTexture(), 16, 16, 0, 0, 0, false);
2842 skinframe_t *R_SkinFrame_LoadInternalUsingTexture(const char *name, int textureflags, rtexture_t *tex, int width, int height, qbool sRGB)
2844 skinframe_t *skinframe;
2845 if (cls.state == ca_dedicated)
2847 // if already loaded just return it, otherwise make a new skinframe
2848 skinframe = R_SkinFrame_Find(name, textureflags, width, height, 0, true);
2849 if (skinframe->base)
2851 textureflags &= ~TEXF_FORCE_RELOAD;
2852 skinframe->stain = NULL;
2853 skinframe->merged = NULL;
2854 skinframe->base = NULL;
2855 skinframe->pants = NULL;
2856 skinframe->shirt = NULL;
2857 skinframe->nmap = NULL;
2858 skinframe->gloss = NULL;
2859 skinframe->glow = NULL;
2860 skinframe->fog = NULL;
2861 skinframe->reflect = NULL;
2862 skinframe->hasalpha = (textureflags & TEXF_ALPHA) != 0;
2863 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2866 if (developer_loading.integer)
2867 Con_Printf("loading 32bit skin \"%s\"\n", name);
2868 skinframe->base = skinframe->merged = tex;
2869 Vector4Set(skinframe->avgcolor, 1, 1, 1, 1); // bogus placeholder
2873 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
2874 typedef struct suffixinfo_s
2877 qbool flipx, flipy, flipdiagonal;
2880 static suffixinfo_t suffix[3][6] =
2883 {"px", false, false, false},
2884 {"nx", false, false, false},
2885 {"py", false, false, false},
2886 {"ny", false, false, false},
2887 {"pz", false, false, false},
2888 {"nz", false, false, false}
2891 {"posx", false, false, false},
2892 {"negx", false, false, false},
2893 {"posy", false, false, false},
2894 {"negy", false, false, false},
2895 {"posz", false, false, false},
2896 {"negz", false, false, false}
2899 {"rt", true, false, true},
2900 {"lf", false, true, true},
2901 {"ft", true, true, false},
2902 {"bk", false, false, false},
2903 {"up", true, false, true},
2904 {"dn", true, false, true}
2908 static int componentorder[4] = {0, 1, 2, 3};
2910 static rtexture_t *R_LoadCubemap(const char *basename)
2912 int i, j, cubemapsize, forcefilter;
2913 unsigned char *cubemappixels, *image_buffer;
2914 rtexture_t *cubemaptexture;
2917 // HACK: if the cubemap name starts with a !, the cubemap is nearest-filtered
2918 forcefilter = TEXF_FORCELINEAR;
2919 if (basename && basename[0] == '!')
2922 forcefilter = TEXF_FORCENEAREST;
2924 // must start 0 so the first loadimagepixels has no requested width/height
2926 cubemappixels = NULL;
2927 cubemaptexture = NULL;
2928 // keep trying different suffix groups (posx, px, rt) until one loads
2929 for (j = 0;j < 3 && !cubemappixels;j++)
2931 // load the 6 images in the suffix group
2932 for (i = 0;i < 6;i++)
2934 // generate an image name based on the base and and suffix
2935 dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
2937 if ((image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
2939 // an image loaded, make sure width and height are equal
2940 if (image_width == image_height && (!cubemappixels || image_width == cubemapsize))
2942 // if this is the first image to load successfully, allocate the cubemap memory
2943 if (!cubemappixels && image_width >= 1)
2945 cubemapsize = image_width;
2946 // note this clears to black, so unavailable sides are black
2947 cubemappixels = (unsigned char *)Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
2949 // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
2951 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);
2954 Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
2956 Mem_Free(image_buffer);
2960 // if a cubemap loaded, upload it
2963 if (developer_loading.integer)
2964 Con_Printf("loading cubemap \"%s\"\n", basename);
2966 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);
2967 Mem_Free(cubemappixels);
2971 Con_DPrintf("failed to load cubemap \"%s\"\n", basename);
2972 if (developer_loading.integer)
2974 Con_Printf("(tried tried images ");
2975 for (j = 0;j < 3;j++)
2976 for (i = 0;i < 6;i++)
2977 Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
2978 Con_Print(" and was unable to find any of them).\n");
2981 return cubemaptexture;
2984 rtexture_t *R_GetCubemap(const char *basename)
2987 for (i = 0;i < r_texture_numcubemaps;i++)
2988 if (r_texture_cubemaps[i] != NULL)
2989 if (!strcasecmp(r_texture_cubemaps[i]->basename, basename))
2990 return r_texture_cubemaps[i]->texture ? r_texture_cubemaps[i]->texture : r_texture_whitecube;
2991 if (i >= MAX_CUBEMAPS || !r_main_mempool)
2992 return r_texture_whitecube;
2993 r_texture_numcubemaps++;
2994 r_texture_cubemaps[i] = (cubemapinfo_t *)Mem_Alloc(r_main_mempool, sizeof(cubemapinfo_t));
2995 strlcpy(r_texture_cubemaps[i]->basename, basename, sizeof(r_texture_cubemaps[i]->basename));
2996 r_texture_cubemaps[i]->texture = R_LoadCubemap(r_texture_cubemaps[i]->basename);
2997 return r_texture_cubemaps[i]->texture;
3000 static void R_Main_FreeViewCache(void)
3002 if (r_refdef.viewcache.entityvisible)
3003 Mem_Free(r_refdef.viewcache.entityvisible);
3004 if (r_refdef.viewcache.world_pvsbits)
3005 Mem_Free(r_refdef.viewcache.world_pvsbits);
3006 if (r_refdef.viewcache.world_leafvisible)
3007 Mem_Free(r_refdef.viewcache.world_leafvisible);
3008 if (r_refdef.viewcache.world_surfacevisible)
3009 Mem_Free(r_refdef.viewcache.world_surfacevisible);
3010 memset(&r_refdef.viewcache, 0, sizeof(r_refdef.viewcache));
3013 static void R_Main_ResizeViewCache(void)
3015 int numentities = r_refdef.scene.numentities;
3016 int numclusters = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusters : 1;
3017 int numclusterbytes = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusterbytes : 1;
3018 int numleafs = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_leafs : 1;
3019 int numsurfaces = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->num_surfaces : 1;
3020 if (r_refdef.viewcache.maxentities < numentities)
3022 r_refdef.viewcache.maxentities = numentities;
3023 if (r_refdef.viewcache.entityvisible)
3024 Mem_Free(r_refdef.viewcache.entityvisible);
3025 r_refdef.viewcache.entityvisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.maxentities);
3027 if (r_refdef.viewcache.world_numclusters != numclusters)
3029 r_refdef.viewcache.world_numclusters = numclusters;
3030 r_refdef.viewcache.world_numclusterbytes = numclusterbytes;
3031 if (r_refdef.viewcache.world_pvsbits)
3032 Mem_Free(r_refdef.viewcache.world_pvsbits);
3033 r_refdef.viewcache.world_pvsbits = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numclusterbytes);
3035 if (r_refdef.viewcache.world_numleafs != numleafs)
3037 r_refdef.viewcache.world_numleafs = numleafs;
3038 if (r_refdef.viewcache.world_leafvisible)
3039 Mem_Free(r_refdef.viewcache.world_leafvisible);
3040 r_refdef.viewcache.world_leafvisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numleafs);
3042 if (r_refdef.viewcache.world_numsurfaces != numsurfaces)
3044 r_refdef.viewcache.world_numsurfaces = numsurfaces;
3045 if (r_refdef.viewcache.world_surfacevisible)
3046 Mem_Free(r_refdef.viewcache.world_surfacevisible);
3047 r_refdef.viewcache.world_surfacevisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numsurfaces);
3051 extern rtexture_t *loadingscreentexture;
3052 static void gl_main_start(void)
3054 loadingscreentexture = NULL;
3055 r_texture_blanknormalmap = NULL;
3056 r_texture_white = NULL;
3057 r_texture_grey128 = NULL;
3058 r_texture_black = NULL;
3059 r_texture_whitecube = NULL;
3060 r_texture_normalizationcube = NULL;
3061 r_texture_fogattenuation = NULL;
3062 r_texture_fogheighttexture = NULL;
3063 r_texture_gammaramps = NULL;
3064 r_texture_numcubemaps = 0;
3065 r_uniformbufferalignment = 32;
3067 r_loaddds = r_texture_dds_load.integer != 0;
3068 r_savedds = vid.support.ext_texture_compression_s3tc && r_texture_dds_save.integer;
3070 switch(vid.renderpath)
3072 case RENDERPATH_GL32:
3073 case RENDERPATH_GLES2:
3074 Cvar_SetValueQuick(&r_textureunits, MAX_TEXTUREUNITS);
3075 Cvar_SetValueQuick(&gl_combine, 1);
3076 Cvar_SetValueQuick(&r_glsl, 1);
3077 r_loadnormalmap = true;
3080 #ifdef GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT
3081 qglGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &r_uniformbufferalignment);
3087 R_FrameData_Reset();
3088 R_BufferData_Reset();
3092 memset(r_queries, 0, sizeof(r_queries));
3094 r_qwskincache = NULL;
3095 r_qwskincache_size = 0;
3097 // due to caching of texture_t references, the collision cache must be reset
3098 Collision_Cache_Reset(true);
3100 // set up r_skinframe loading system for textures
3101 memset(&r_skinframe, 0, sizeof(r_skinframe));
3102 r_skinframe.loadsequence = 1;
3103 Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
3105 r_main_texturepool = R_AllocTexturePool();
3106 R_BuildBlankTextures();
3110 R_BuildNormalizationCube();
3112 r_texture_fogattenuation = NULL;
3113 r_texture_fogheighttexture = NULL;
3114 r_texture_gammaramps = NULL;
3115 //r_texture_fogintensity = NULL;
3116 memset(&r_fb, 0, sizeof(r_fb));
3117 Mem_ExpandableArray_NewArray(&r_fb.rendertargets, r_main_mempool, sizeof(r_rendertarget_t), 128);
3118 r_glsl_permutation = NULL;
3119 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
3120 Mem_ExpandableArray_NewArray(&r_glsl_permutationarray, r_main_mempool, sizeof(r_glsl_permutation_t), 256);
3121 memset(&r_svbsp, 0, sizeof (r_svbsp));
3123 memset(r_texture_cubemaps, 0, sizeof(r_texture_cubemaps));
3124 r_texture_numcubemaps = 0;
3126 r_refdef.fogmasktable_density = 0;
3129 // For Steelstorm Android
3130 // FIXME CACHE the program and reload
3131 // FIXME see possible combinations for SS:BR android
3132 Con_DPrintf("Compiling most used shaders for SS:BR android... START\n");
3133 R_SetupShader_SetPermutationGLSL(0, 12);
3134 R_SetupShader_SetPermutationGLSL(0, 13);
3135 R_SetupShader_SetPermutationGLSL(0, 8388621);
3136 R_SetupShader_SetPermutationGLSL(3, 0);
3137 R_SetupShader_SetPermutationGLSL(3, 2048);
3138 R_SetupShader_SetPermutationGLSL(5, 0);
3139 R_SetupShader_SetPermutationGLSL(5, 2);
3140 R_SetupShader_SetPermutationGLSL(5, 2048);
3141 R_SetupShader_SetPermutationGLSL(5, 8388608);
3142 R_SetupShader_SetPermutationGLSL(11, 1);
3143 R_SetupShader_SetPermutationGLSL(11, 2049);
3144 R_SetupShader_SetPermutationGLSL(11, 8193);
3145 R_SetupShader_SetPermutationGLSL(11, 10241);
3146 Con_DPrintf("Compiling most used shaders for SS:BR android... END\n");
3150 extern unsigned int r_shadow_occlusion_buf;
3152 static void gl_main_shutdown(void)
3154 R_RenderTarget_FreeUnused(true);
3155 Mem_ExpandableArray_FreeArray(&r_fb.rendertargets);
3157 R_FrameData_Reset();
3158 R_BufferData_Reset();
3160 R_Main_FreeViewCache();
3162 switch(vid.renderpath)
3164 case RENDERPATH_GL32:
3165 case RENDERPATH_GLES2:
3166 #if defined(GL_SAMPLES_PASSED) && !defined(USE_GLES2)
3168 qglDeleteQueries(r_maxqueries, r_queries);
3172 r_shadow_occlusion_buf = 0;
3175 memset(r_queries, 0, sizeof(r_queries));
3177 r_qwskincache = NULL;
3178 r_qwskincache_size = 0;
3180 // clear out the r_skinframe state
3181 Mem_ExpandableArray_FreeArray(&r_skinframe.array);
3182 memset(&r_skinframe, 0, sizeof(r_skinframe));
3185 Mem_Free(r_svbsp.nodes);
3186 memset(&r_svbsp, 0, sizeof (r_svbsp));
3187 R_FreeTexturePool(&r_main_texturepool);
3188 loadingscreentexture = NULL;
3189 r_texture_blanknormalmap = NULL;
3190 r_texture_white = NULL;
3191 r_texture_grey128 = NULL;
3192 r_texture_black = NULL;
3193 r_texture_whitecube = NULL;
3194 r_texture_normalizationcube = NULL;
3195 r_texture_fogattenuation = NULL;
3196 r_texture_fogheighttexture = NULL;
3197 r_texture_gammaramps = NULL;
3198 r_texture_numcubemaps = 0;
3199 //r_texture_fogintensity = NULL;
3200 memset(&r_fb, 0, sizeof(r_fb));
3201 R_GLSL_Restart_f(cmd_local);
3203 r_glsl_permutation = NULL;
3204 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
3205 Mem_ExpandableArray_FreeArray(&r_glsl_permutationarray);
3208 static void gl_main_newmap(void)
3210 // FIXME: move this code to client
3211 char *entities, entname[MAX_QPATH];
3213 Mem_Free(r_qwskincache);
3214 r_qwskincache = NULL;
3215 r_qwskincache_size = 0;
3218 dpsnprintf(entname, sizeof(entname), "%s.ent", cl.worldnamenoextension);
3219 if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
3221 CL_ParseEntityLump(entities);
3225 if (cl.worldmodel->brush.entities)
3226 CL_ParseEntityLump(cl.worldmodel->brush.entities);
3228 R_Main_FreeViewCache();
3230 R_FrameData_Reset();
3231 R_BufferData_Reset();
3234 void GL_Main_Init(void)
3237 r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
3238 R_InitShaderModeInfo();
3240 Cmd_AddCommand(CF_CLIENT, "r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
3241 Cmd_AddCommand(CF_CLIENT, "r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
3242 // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
3243 if (gamemode == GAME_NEHAHRA)
3245 Cvar_RegisterVariable (&gl_fogenable);
3246 Cvar_RegisterVariable (&gl_fogdensity);
3247 Cvar_RegisterVariable (&gl_fogred);
3248 Cvar_RegisterVariable (&gl_foggreen);
3249 Cvar_RegisterVariable (&gl_fogblue);
3250 Cvar_RegisterVariable (&gl_fogstart);
3251 Cvar_RegisterVariable (&gl_fogend);
3252 Cvar_RegisterVariable (&gl_skyclip);
3254 Cvar_RegisterVariable(&r_motionblur);
3255 Cvar_RegisterVariable(&r_damageblur);
3256 Cvar_RegisterVariable(&r_motionblur_averaging);
3257 Cvar_RegisterVariable(&r_motionblur_randomize);
3258 Cvar_RegisterVariable(&r_motionblur_minblur);
3259 Cvar_RegisterVariable(&r_motionblur_maxblur);
3260 Cvar_RegisterVariable(&r_motionblur_velocityfactor);
3261 Cvar_RegisterVariable(&r_motionblur_velocityfactor_minspeed);
3262 Cvar_RegisterVariable(&r_motionblur_velocityfactor_maxspeed);
3263 Cvar_RegisterVariable(&r_motionblur_mousefactor);
3264 Cvar_RegisterVariable(&r_motionblur_mousefactor_minspeed);
3265 Cvar_RegisterVariable(&r_motionblur_mousefactor_maxspeed);
3266 Cvar_RegisterVariable(&r_depthfirst);
3267 Cvar_RegisterVariable(&r_useinfinitefarclip);
3268 Cvar_RegisterVariable(&r_farclip_base);
3269 Cvar_RegisterVariable(&r_farclip_world);
3270 Cvar_RegisterVariable(&r_nearclip);
3271 Cvar_RegisterVariable(&r_deformvertexes);
3272 Cvar_RegisterVariable(&r_transparent);
3273 Cvar_RegisterVariable(&r_transparent_alphatocoverage);
3274 Cvar_RegisterVariable(&r_transparent_sortsurfacesbynearest);
3275 Cvar_RegisterVariable(&r_transparent_useplanardistance);
3276 Cvar_RegisterVariable(&r_showoverdraw);
3277 Cvar_RegisterVariable(&r_showbboxes);
3278 Cvar_RegisterVariable(&r_showbboxes_client);
3279 Cvar_RegisterVariable(&r_showsurfaces);
3280 Cvar_RegisterVariable(&r_showtris);
3281 Cvar_RegisterVariable(&r_shownormals);
3282 Cvar_RegisterVariable(&r_showlighting);
3283 Cvar_RegisterVariable(&r_showcollisionbrushes);
3284 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
3285 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
3286 Cvar_RegisterVariable(&r_showdisabledepthtest);
3287 Cvar_RegisterVariable(&r_showspriteedges);
3288 Cvar_RegisterVariable(&r_showparticleedges);
3289 Cvar_RegisterVariable(&r_drawportals);
3290 Cvar_RegisterVariable(&r_drawentities);
3291 Cvar_RegisterVariable(&r_draw2d);
3292 Cvar_RegisterVariable(&r_drawworld);
3293 Cvar_RegisterVariable(&r_cullentities_trace);
3294 Cvar_RegisterVariable(&r_cullentities_trace_entityocclusion);
3295 Cvar_RegisterVariable(&r_cullentities_trace_samples);
3296 Cvar_RegisterVariable(&r_cullentities_trace_tempentitysamples);
3297 Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
3298 Cvar_RegisterVariable(&r_cullentities_trace_expand);
3299 Cvar_RegisterVariable(&r_cullentities_trace_pad);
3300 Cvar_RegisterVariable(&r_cullentities_trace_delay);
3301 Cvar_RegisterVariable(&r_cullentities_trace_eyejitter);
3302 Cvar_RegisterVariable(&r_sortentities);
3303 Cvar_RegisterVariable(&r_drawviewmodel);
3304 Cvar_RegisterVariable(&r_drawexteriormodel);
3305 Cvar_RegisterVariable(&r_speeds);
3306 Cvar_RegisterVariable(&r_fullbrights);
3307 Cvar_RegisterVariable(&r_wateralpha);
3308 Cvar_RegisterVariable(&r_dynamic);
3309 Cvar_RegisterVariable(&r_fullbright_directed);
3310 Cvar_RegisterVariable(&r_fullbright_directed_ambient);
3311 Cvar_RegisterVariable(&r_fullbright_directed_diffuse);
3312 Cvar_RegisterVariable(&r_fullbright_directed_pitch);
3313 Cvar_RegisterVariable(&r_fullbright_directed_pitch_relative);
3314 Cvar_RegisterVariable(&r_fullbright);
3315 Cvar_RegisterVariable(&r_shadows);
3316 Cvar_RegisterVariable(&r_shadows_darken);
3317 Cvar_RegisterVariable(&r_shadows_drawafterrtlighting);
3318 Cvar_RegisterVariable(&r_shadows_castfrombmodels);
3319 Cvar_RegisterVariable(&r_shadows_throwdistance);
3320 Cvar_RegisterVariable(&r_shadows_throwdirection);
3321 Cvar_RegisterVariable(&r_shadows_focus);
3322 Cvar_RegisterVariable(&r_shadows_shadowmapscale);
3323 Cvar_RegisterVariable(&r_shadows_shadowmapbias);
3324 Cvar_RegisterVariable(&r_q1bsp_skymasking);
3325 Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
3326 Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
3327 Cvar_RegisterVariable(&r_polygonoffset_decals_factor);
3328 Cvar_RegisterVariable(&r_polygonoffset_decals_offset);
3329 Cvar_RegisterVariable(&r_fog_exp2);
3330 Cvar_RegisterVariable(&r_fog_clear);
3331 Cvar_RegisterVariable(&r_drawfog);
3332 Cvar_RegisterVariable(&r_transparentdepthmasking);
3333 Cvar_RegisterVariable(&r_transparent_sortmindist);
3334 Cvar_RegisterVariable(&r_transparent_sortmaxdist);
3335 Cvar_RegisterVariable(&r_transparent_sortarraysize);
3336 Cvar_RegisterVariable(&r_texture_dds_load);
3337 Cvar_RegisterVariable(&r_texture_dds_save);
3338 Cvar_RegisterVariable(&r_textureunits);
3339 Cvar_RegisterVariable(&gl_combine);
3340 Cvar_RegisterVariable(&r_usedepthtextures);
3341 Cvar_RegisterVariable(&r_viewfbo);
3342 Cvar_RegisterVariable(&r_rendertarget_debug);
3343 Cvar_RegisterVariable(&r_viewscale);
3344 Cvar_RegisterVariable(&r_viewscale_fpsscaling);
3345 Cvar_RegisterVariable(&r_viewscale_fpsscaling_min);
3346 Cvar_RegisterVariable(&r_viewscale_fpsscaling_multiply);
3347 Cvar_RegisterVariable(&r_viewscale_fpsscaling_stepsize);
3348 Cvar_RegisterVariable(&r_viewscale_fpsscaling_stepmax);
3349 Cvar_RegisterVariable(&r_viewscale_fpsscaling_target);
3350 Cvar_RegisterVariable(&r_glsl);
3351 Cvar_RegisterVariable(&r_glsl_deluxemapping);
3352 Cvar_RegisterVariable(&r_glsl_offsetmapping);
3353 Cvar_RegisterVariable(&r_glsl_offsetmapping_steps);
3354 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
3355 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping_steps);
3356 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping_refinesteps);
3357 Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
3358 Cvar_RegisterVariable(&r_glsl_offsetmapping_lod);
3359 Cvar_RegisterVariable(&r_glsl_offsetmapping_lod_distance);
3360 Cvar_RegisterVariable(&r_glsl_postprocess);
3361 Cvar_RegisterVariable(&r_glsl_postprocess_uservec1);
3362 Cvar_RegisterVariable(&r_glsl_postprocess_uservec2);
3363 Cvar_RegisterVariable(&r_glsl_postprocess_uservec3);
3364 Cvar_RegisterVariable(&r_glsl_postprocess_uservec4);
3365 Cvar_RegisterVariable(&r_glsl_postprocess_uservec1_enable);
3366 Cvar_RegisterVariable(&r_glsl_postprocess_uservec2_enable);
3367 Cvar_RegisterVariable(&r_glsl_postprocess_uservec3_enable);
3368 Cvar_RegisterVariable(&r_glsl_postprocess_uservec4_enable);
3369 Cvar_RegisterVariable(&r_celshading);
3370 Cvar_RegisterVariable(&r_celoutlines);
3372 Cvar_RegisterVariable(&r_water);
3373 Cvar_RegisterVariable(&r_water_cameraentitiesonly);
3374 Cvar_RegisterVariable(&r_water_resolutionmultiplier);
3375 Cvar_RegisterVariable(&r_water_clippingplanebias);
3376 Cvar_RegisterVariable(&r_water_refractdistort);
3377 Cvar_RegisterVariable(&r_water_reflectdistort);
3378 Cvar_RegisterVariable(&r_water_scissormode);
3379 Cvar_RegisterVariable(&r_water_lowquality);
3380 Cvar_RegisterVariable(&r_water_hideplayer);
3382 Cvar_RegisterVariable(&r_lerpsprites);
3383 Cvar_RegisterVariable(&r_lerpmodels);
3384 Cvar_RegisterVariable(&r_nolerp_list);
3385 Cvar_RegisterVariable(&r_lerplightstyles);
3386 Cvar_RegisterVariable(&r_waterscroll);
3387 Cvar_RegisterVariable(&r_bloom);
3388 Cvar_RegisterVariable(&r_colorfringe);
3389 Cvar_RegisterVariable(&r_bloom_colorscale);
3390 Cvar_RegisterVariable(&r_bloom_brighten);
3391 Cvar_RegisterVariable(&r_bloom_blur);
3392 Cvar_RegisterVariable(&r_bloom_resolution);
3393 Cvar_RegisterVariable(&r_bloom_colorexponent);
3394 Cvar_RegisterVariable(&r_bloom_colorsubtract);
3395 Cvar_RegisterVariable(&r_bloom_scenebrightness);
3396 Cvar_RegisterVariable(&r_hdr_scenebrightness);
3397 Cvar_RegisterVariable(&r_hdr_glowintensity);
3398 Cvar_RegisterVariable(&r_hdr_irisadaptation);
3399 Cvar_RegisterVariable(&r_hdr_irisadaptation_multiplier);
3400 Cvar_RegisterVariable(&r_hdr_irisadaptation_minvalue);
3401 Cvar_RegisterVariable(&r_hdr_irisadaptation_maxvalue);
3402 Cvar_RegisterVariable(&r_hdr_irisadaptation_value);
3403 Cvar_RegisterVariable(&r_hdr_irisadaptation_fade_up);
3404 Cvar_RegisterVariable(&r_hdr_irisadaptation_fade_down);
3405 Cvar_RegisterVariable(&r_hdr_irisadaptation_radius);
3406 Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
3407 Cvar_RegisterVariable(&developer_texturelogging);
3408 Cvar_RegisterVariable(&gl_lightmaps);
3409 Cvar_RegisterVariable(&r_test);
3410 Cvar_RegisterVariable(&r_batch_multidraw);
3411 Cvar_RegisterVariable(&r_batch_multidraw_mintriangles);
3412 Cvar_RegisterVariable(&r_batch_debugdynamicvertexpath);
3413 Cvar_RegisterVariable(&r_glsl_skeletal);
3414 Cvar_RegisterVariable(&r_glsl_saturation);
3415 Cvar_RegisterVariable(&r_glsl_saturation_redcompensate);
3416 Cvar_RegisterVariable(&r_glsl_vertextextureblend_usebothalphas);
3417 Cvar_RegisterVariable(&r_framedatasize);
3418 for (i = 0;i < R_BUFFERDATA_COUNT;i++)
3419 Cvar_RegisterVariable(&r_buffermegs[i]);
3420 Cvar_RegisterVariable(&r_batch_dynamicbuffer);
3421 Cvar_RegisterVariable(&r_q1bsp_lightmap_updates_enabled);
3422 Cvar_RegisterVariable(&r_q1bsp_lightmap_updates_combine);
3423 Cvar_RegisterVariable(&r_q1bsp_lightmap_updates_hidden_surfaces);
3424 if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
3425 Cvar_SetValue(&cvars_all, "r_fullbrights", 0);
3426 #ifdef DP_MOBILETOUCH
3427 // GLES devices have terrible depth precision in general, so...
3428 Cvar_SetValueQuick(&r_nearclip, 4);
3429 Cvar_SetValueQuick(&r_farclip_base, 4096);
3430 Cvar_SetValueQuick(&r_farclip_world, 0);
3431 Cvar_SetValueQuick(&r_useinfinitefarclip, 0);
3433 R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap, NULL, NULL);
3436 void Render_Init(void)
3449 R_LightningBeams_Init();
3450 CL_MeshEntities_Init();
3454 static void R_GetCornerOfBox(vec3_t out, const vec3_t mins, const vec3_t maxs, int signbits)
3456 out[0] = ((signbits & 1) ? mins : maxs)[0];
3457 out[1] = ((signbits & 2) ? mins : maxs)[1];
3458 out[2] = ((signbits & 4) ? mins : maxs)[2];
3461 static qbool _R_CullBox(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes, int ignore)
3466 if (r_trippy.integer)
3468 for (i = 0;i < numplanes;i++)
3473 R_GetCornerOfBox(corner, mins, maxs, p->signbits);
3474 if (DotProduct(p->normal, corner) < p->dist)
3480 qbool R_CullFrustum(const vec3_t mins, const vec3_t maxs)
3482 // skip nearclip plane, it often culls portals when you are very close, and is almost never useful
3483 return _R_CullBox(mins, maxs, r_refdef.view.numfrustumplanes, r_refdef.view.frustum, 4);
3486 qbool R_CullBox(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
3488 // nothing to ignore
3489 return _R_CullBox(mins, maxs, numplanes, planes, -1);
3492 //==================================================================================
3494 // LadyHavoc: this stores temporary data used within the same frame
3496 typedef struct r_framedata_mem_s
3498 struct r_framedata_mem_s *purge; // older mem block to free on next frame
3499 size_t size; // how much usable space
3500 size_t current; // how much space in use
3501 size_t mark; // last "mark" location, temporary memory can be freed by returning to this
3502 size_t wantedsize; // how much space was allocated
3503 unsigned char *data; // start of real data (16byte aligned)
3507 static r_framedata_mem_t *r_framedata_mem;
3509 void R_FrameData_Reset(void)
3511 while (r_framedata_mem)
3513 r_framedata_mem_t *next = r_framedata_mem->purge;
3514 Mem_Free(r_framedata_mem);
3515 r_framedata_mem = next;
3519 static void R_FrameData_Resize(qbool mustgrow)
3522 wantedsize = (size_t)(r_framedatasize.value * 1024*1024);
3523 wantedsize = bound(65536, wantedsize, 1000*1024*1024);
3524 if (!r_framedata_mem || r_framedata_mem->wantedsize != wantedsize || mustgrow)
3526 r_framedata_mem_t *newmem = (r_framedata_mem_t *)Mem_Alloc(r_main_mempool, wantedsize);
3527 newmem->wantedsize = wantedsize;
3528 newmem->data = (unsigned char *)(((size_t)(newmem+1) + 15) & ~15);
3529 newmem->size = (unsigned char *)newmem + wantedsize - newmem->data;
3530 newmem->current = 0;
3532 newmem->purge = r_framedata_mem;
3533 r_framedata_mem = newmem;
3537 void R_FrameData_NewFrame(void)
3539 R_FrameData_Resize(false);
3540 if (!r_framedata_mem)
3542 // if we ran out of space on the last frame, free the old memory now
3543 while (r_framedata_mem->purge)
3545 // repeatedly remove the second item in the list, leaving only head
3546 r_framedata_mem_t *next = r_framedata_mem->purge->purge;
3547 Mem_Free(r_framedata_mem->purge);
3548 r_framedata_mem->purge = next;
3550 // reset the current mem pointer
3551 r_framedata_mem->current = 0;
3552 r_framedata_mem->mark = 0;
3555 void *R_FrameData_Alloc(size_t size)
3560 // align to 16 byte boundary - the data pointer is already aligned, so we
3561 // only need to ensure the size of every allocation is also aligned
3562 size = (size + 15) & ~15;
3564 while (!r_framedata_mem || r_framedata_mem->current + size > r_framedata_mem->size)
3566 // emergency - we ran out of space, allocate more memory
3567 // note: this has no upper-bound, we'll fail to allocate memory eventually and just die
3568 newvalue = r_framedatasize.value * 2.0f;
3569 // 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
3570 if (sizeof(size_t) >= 8)
3571 newvalue = bound(0.25f, newvalue, (float)(1ll << 42));
3573 newvalue = bound(0.25f, newvalue, (float)(1 << 10));
3574 // this might not be a growing it, but we'll allocate another buffer every time
3575 Cvar_SetValueQuick(&r_framedatasize, newvalue);
3576 R_FrameData_Resize(true);
3579 data = r_framedata_mem->data + r_framedata_mem->current;
3580 r_framedata_mem->current += size;
3582 // count the usage for stats
3583 r_refdef.stats[r_stat_framedatacurrent] = max(r_refdef.stats[r_stat_framedatacurrent], (int)r_framedata_mem->current);
3584 r_refdef.stats[r_stat_framedatasize] = max(r_refdef.stats[r_stat_framedatasize], (int)r_framedata_mem->size);
3586 return (void *)data;
3589 void *R_FrameData_Store(size_t size, void *data)
3591 void *d = R_FrameData_Alloc(size);
3593 memcpy(d, data, size);
3597 void R_FrameData_SetMark(void)
3599 if (!r_framedata_mem)
3601 r_framedata_mem->mark = r_framedata_mem->current;
3604 void R_FrameData_ReturnToMark(void)
3606 if (!r_framedata_mem)
3608 r_framedata_mem->current = r_framedata_mem->mark;
3611 //==================================================================================
3613 // avoid reusing the same buffer objects on consecutive frames
3614 #define R_BUFFERDATA_CYCLE 3
3616 typedef struct r_bufferdata_buffer_s
3618 struct r_bufferdata_buffer_s *purge; // older buffer to free on next frame
3619 size_t size; // how much usable space
3620 size_t current; // how much space in use
3621 r_meshbuffer_t *buffer; // the buffer itself
3623 r_bufferdata_buffer_t;
3625 static int r_bufferdata_cycle = 0; // incremented and wrapped each frame
3626 static r_bufferdata_buffer_t *r_bufferdata_buffer[R_BUFFERDATA_CYCLE][R_BUFFERDATA_COUNT];
3628 /// frees all dynamic buffers
3629 void R_BufferData_Reset(void)
3632 r_bufferdata_buffer_t **p, *mem;
3633 for (cycle = 0;cycle < R_BUFFERDATA_CYCLE;cycle++)
3635 for (type = 0;type < R_BUFFERDATA_COUNT;type++)
3638 p = &r_bufferdata_buffer[cycle][type];
3644 R_Mesh_DestroyMeshBuffer(mem->buffer);
3651 // resize buffer as needed (this actually makes a new one, the old one will be recycled next frame)
3652 static void R_BufferData_Resize(r_bufferdata_type_t type, qbool mustgrow, size_t minsize)
3654 r_bufferdata_buffer_t *mem = r_bufferdata_buffer[r_bufferdata_cycle][type];
3656 float newvalue = r_buffermegs[type].value;
3658 // increase the cvar if we have to (but only if we already have a mem)
3659 if (mustgrow && mem)
3661 newvalue = bound(0.25f, newvalue, 256.0f);
3662 while (newvalue * 1024*1024 < minsize)
3665 // clamp the cvar to valid range
3666 newvalue = bound(0.25f, newvalue, 256.0f);
3667 if (r_buffermegs[type].value != newvalue)
3668 Cvar_SetValueQuick(&r_buffermegs[type], newvalue);
3670 // calculate size in bytes
3671 size = (size_t)(newvalue * 1024*1024);
3672 size = bound(131072, size, 256*1024*1024);
3674 // allocate a new buffer if the size is different (purge old one later)
3675 // or if we were told we must grow the buffer
3676 if (!mem || mem->size != size || mustgrow)
3678 mem = (r_bufferdata_buffer_t *)Mem_Alloc(r_main_mempool, sizeof(*mem));
3681 if (type == R_BUFFERDATA_VERTEX)
3682 mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbuffervertex", false, false, true, false);
3683 else if (type == R_BUFFERDATA_INDEX16)
3684 mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbufferindex16", true, false, true, true);
3685 else if (type == R_BUFFERDATA_INDEX32)
3686 mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbufferindex32", true, false, true, false);
3687 else if (type == R_BUFFERDATA_UNIFORM)
3688 mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbufferuniform", false, true, true, false);
3689 mem->purge = r_bufferdata_buffer[r_bufferdata_cycle][type];
3690 r_bufferdata_buffer[r_bufferdata_cycle][type] = mem;
3694 void R_BufferData_NewFrame(void)
3697 r_bufferdata_buffer_t **p, *mem;
3698 // cycle to the next frame's buffers
3699 r_bufferdata_cycle = (r_bufferdata_cycle + 1) % R_BUFFERDATA_CYCLE;
3700 // if we ran out of space on the last time we used these buffers, free the old memory now
3701 for (type = 0;type < R_BUFFERDATA_COUNT;type++)
3703 if (r_bufferdata_buffer[r_bufferdata_cycle][type])
3705 R_BufferData_Resize((r_bufferdata_type_t)type, false, 131072);
3706 // free all but the head buffer, this is how we recycle obsolete
3707 // buffers after they are no longer in use
3708 p = &r_bufferdata_buffer[r_bufferdata_cycle][type]->purge;
3714 R_Mesh_DestroyMeshBuffer(mem->buffer);
3717 // reset the current offset
3718 r_bufferdata_buffer[r_bufferdata_cycle][type]->current = 0;
3723 r_meshbuffer_t *R_BufferData_Store(size_t datasize, const void *data, r_bufferdata_type_t type, int *returnbufferoffset)
3725 r_bufferdata_buffer_t *mem;
3729 *returnbufferoffset = 0;
3731 // align size to a byte boundary appropriate for the buffer type, this
3732 // makes all allocations have aligned start offsets
3733 if (type == R_BUFFERDATA_UNIFORM)
3734 padsize = (datasize + r_uniformbufferalignment - 1) & ~(r_uniformbufferalignment - 1);
3736 padsize = (datasize + 15) & ~15;
3738 // if we ran out of space in this buffer we must allocate a new one
3739 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)
3740 R_BufferData_Resize(type, true, padsize);
3742 // if the resize did not give us enough memory, fail
3743 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)
3744 Sys_Error("R_BufferData_Store: failed to create a new buffer of sufficient size\n");
3746 mem = r_bufferdata_buffer[r_bufferdata_cycle][type];
3747 offset = (int)mem->current;
3748 mem->current += padsize;
3750 // upload the data to the buffer at the chosen offset
3752 R_Mesh_UpdateMeshBuffer(mem->buffer, NULL, mem->size, false, 0);
3753 R_Mesh_UpdateMeshBuffer(mem->buffer, data, datasize, true, offset);
3755 // count the usage for stats
3756 r_refdef.stats[r_stat_bufferdatacurrent_vertex + type] = max(r_refdef.stats[r_stat_bufferdatacurrent_vertex + type], (int)mem->current);
3757 r_refdef.stats[r_stat_bufferdatasize_vertex + type] = max(r_refdef.stats[r_stat_bufferdatasize_vertex + type], (int)mem->size);
3759 // return the buffer offset
3760 *returnbufferoffset = offset;
3765 //==================================================================================
3767 // LadyHavoc: animcache originally written by Echon, rewritten since then
3770 * Animation cache prevents re-generating mesh data for an animated model
3771 * multiple times in one frame for lighting, shadowing, reflections, etc.
3774 void R_AnimCache_Free(void)
3778 void R_AnimCache_ClearCache(void)
3781 entity_render_t *ent;
3783 for (i = 0;i < r_refdef.scene.numentities;i++)
3785 ent = r_refdef.scene.entities[i];
3786 ent->animcache_vertex3f = NULL;
3787 ent->animcache_vertex3f_vertexbuffer = NULL;
3788 ent->animcache_vertex3f_bufferoffset = 0;
3789 ent->animcache_normal3f = NULL;
3790 ent->animcache_normal3f_vertexbuffer = NULL;
3791 ent->animcache_normal3f_bufferoffset = 0;
3792 ent->animcache_svector3f = NULL;
3793 ent->animcache_svector3f_vertexbuffer = NULL;
3794 ent->animcache_svector3f_bufferoffset = 0;
3795 ent->animcache_tvector3f = NULL;
3796 ent->animcache_tvector3f_vertexbuffer = NULL;
3797 ent->animcache_tvector3f_bufferoffset = 0;
3798 ent->animcache_skeletaltransform3x4 = NULL;
3799 ent->animcache_skeletaltransform3x4buffer = NULL;
3800 ent->animcache_skeletaltransform3x4offset = 0;
3801 ent->animcache_skeletaltransform3x4size = 0;
3805 qbool R_AnimCache_GetEntity(entity_render_t *ent, qbool wantnormals, qbool wanttangents)
3807 model_t *model = ent->model;
3810 // see if this ent is worth caching
3811 if (!model || !model->Draw || !model->AnimateVertices)
3813 // nothing to cache if it contains no animations and has no skeleton
3814 if (!model->surfmesh.isanimated && !(model->num_bones && ent->skeleton && ent->skeleton->relativetransforms))
3816 // see if it is already cached for gpuskeletal
3817 if (ent->animcache_skeletaltransform3x4)
3819 // see if it is already cached as a mesh
3820 if (ent->animcache_vertex3f)
3822 // check if we need to add normals or tangents
3823 if (ent->animcache_normal3f)
3824 wantnormals = false;
3825 if (ent->animcache_svector3f)
3826 wanttangents = false;
3827 if (!wantnormals && !wanttangents)
3831 // check which kind of cache we need to generate
3832 if (r_gpuskeletal && model->num_bones > 0 && model->surfmesh.data_skeletalindex4ub)
3834 // cache the skeleton so the vertex shader can use it
3835 r_refdef.stats[r_stat_animcache_skeletal_count] += 1;
3836 r_refdef.stats[r_stat_animcache_skeletal_bones] += model->num_bones;
3837 r_refdef.stats[r_stat_animcache_skeletal_maxbones] = max(r_refdef.stats[r_stat_animcache_skeletal_maxbones], model->num_bones);
3838 ent->animcache_skeletaltransform3x4 = (float *)R_FrameData_Alloc(sizeof(float[3][4]) * model->num_bones);
3839 Mod_Skeletal_BuildTransforms(model, ent->frameblend, ent->skeleton, NULL, ent->animcache_skeletaltransform3x4);
3840 // note: this can fail if the buffer is at the grow limit
3841 ent->animcache_skeletaltransform3x4size = sizeof(float[3][4]) * model->num_bones;
3842 ent->animcache_skeletaltransform3x4buffer = R_BufferData_Store(ent->animcache_skeletaltransform3x4size, ent->animcache_skeletaltransform3x4, R_BUFFERDATA_UNIFORM, &ent->animcache_skeletaltransform3x4offset);
3844 else if (ent->animcache_vertex3f)
3846 // mesh was already cached but we may need to add normals/tangents
3847 // (this only happens with multiple views, reflections, cameras, etc)
3848 if (wantnormals || wanttangents)
3850 numvertices = model->surfmesh.num_vertices;
3852 ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3855 ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3856 ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3858 model->AnimateVertices(model, ent->frameblend, ent->skeleton, NULL, wantnormals ? ent->animcache_normal3f : NULL, wanttangents ? ent->animcache_svector3f : NULL, wanttangents ? ent->animcache_tvector3f : NULL);
3859 r_refdef.stats[r_stat_animcache_shade_count] += 1;
3860 r_refdef.stats[r_stat_animcache_shade_vertices] += numvertices;
3861 r_refdef.stats[r_stat_animcache_shade_maxvertices] = max(r_refdef.stats[r_stat_animcache_shade_maxvertices], numvertices);
3866 // generate mesh cache
3867 numvertices = model->surfmesh.num_vertices;
3868 ent->animcache_vertex3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3870 ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3873 ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3874 ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3876 model->AnimateVertices(model, ent->frameblend, ent->skeleton, ent->animcache_vertex3f, ent->animcache_normal3f, ent->animcache_svector3f, ent->animcache_tvector3f);
3877 if (wantnormals || wanttangents)
3879 r_refdef.stats[r_stat_animcache_shade_count] += 1;
3880 r_refdef.stats[r_stat_animcache_shade_vertices] += numvertices;
3881 r_refdef.stats[r_stat_animcache_shade_maxvertices] = max(r_refdef.stats[r_stat_animcache_shade_maxvertices], numvertices);
3883 r_refdef.stats[r_stat_animcache_shape_count] += 1;
3884 r_refdef.stats[r_stat_animcache_shape_vertices] += numvertices;
3885 r_refdef.stats[r_stat_animcache_shape_maxvertices] = max(r_refdef.stats[r_stat_animcache_shape_maxvertices], numvertices);
3890 void R_AnimCache_CacheVisibleEntities(void)
3894 // TODO: thread this
3895 // NOTE: R_PrepareRTLights() also caches entities
3897 for (i = 0;i < r_refdef.scene.numentities;i++)
3898 if (r_refdef.viewcache.entityvisible[i])
3899 R_AnimCache_GetEntity(r_refdef.scene.entities[i], true, true);
3902 //==================================================================================
3904 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)
3906 long unsigned int i;
3908 vec3_t eyemins, eyemaxs;
3909 vec3_t boxmins, boxmaxs;
3910 vec3_t padmins, padmaxs;
3913 model_t *model = r_refdef.scene.worldmodel;
3914 static vec3_t positions[] = {
3915 { 0.5f, 0.5f, 0.5f },
3916 { 0.0f, 0.0f, 0.0f },
3917 { 0.0f, 0.0f, 1.0f },
3918 { 0.0f, 1.0f, 0.0f },
3919 { 0.0f, 1.0f, 1.0f },
3920 { 1.0f, 0.0f, 0.0f },
3921 { 1.0f, 0.0f, 1.0f },
3922 { 1.0f, 1.0f, 0.0f },
3923 { 1.0f, 1.0f, 1.0f },
3926 // sample count can be set to -1 to skip this logic, for flicker-prone objects
3930 // view origin is not used for culling in portal/reflection/refraction renders or isometric views
3931 if (!r_refdef.view.usevieworiginculling)
3934 if (!r_cullentities_trace_entityocclusion.integer && (!model || !model->brush.TraceLineOfSight))
3937 // expand the eye box a little
3938 eyemins[0] = eye[0] - eyejitter;
3939 eyemaxs[0] = eye[0] + eyejitter;
3940 eyemins[1] = eye[1] - eyejitter;
3941 eyemaxs[1] = eye[1] + eyejitter;
3942 eyemins[2] = eye[2] - eyejitter;
3943 eyemaxs[2] = eye[2] + eyejitter;
3944 // expand the box a little
3945 boxmins[0] = (entboxenlarge + 1) * entboxmins[0] - entboxenlarge * entboxmaxs[0] - entboxexpand;
3946 boxmaxs[0] = (entboxenlarge + 1) * entboxmaxs[0] - entboxenlarge * entboxmins[0] + entboxexpand;
3947 boxmins[1] = (entboxenlarge + 1) * entboxmins[1] - entboxenlarge * entboxmaxs[1] - entboxexpand;
3948 boxmaxs[1] = (entboxenlarge + 1) * entboxmaxs[1] - entboxenlarge * entboxmins[1] + entboxexpand;
3949 boxmins[2] = (entboxenlarge + 1) * entboxmins[2] - entboxenlarge * entboxmaxs[2] - entboxexpand;
3950 boxmaxs[2] = (entboxenlarge + 1) * entboxmaxs[2] - entboxenlarge * entboxmins[2] + entboxexpand;
3951 // make an even larger box for the acceptable area
3952 padmins[0] = boxmins[0] - pad;
3953 padmaxs[0] = boxmaxs[0] + pad;
3954 padmins[1] = boxmins[1] - pad;
3955 padmaxs[1] = boxmaxs[1] + pad;
3956 padmins[2] = boxmins[2] - pad;
3957 padmaxs[2] = boxmaxs[2] + pad;
3959 // return true if eye overlaps enlarged box
3960 if (BoxesOverlap(boxmins, boxmaxs, eyemins, eyemaxs))
3963 // try specific positions in the box first - note that these can be cached
3964 if (r_cullentities_trace_entityocclusion.integer)
3966 for (i = 0; i < sizeof(positions) / sizeof(positions[0]); i++)
3969 VectorCopy(eye, start);
3970 end[0] = boxmins[0] + (boxmaxs[0] - boxmins[0]) * positions[i][0];
3971 end[1] = boxmins[1] + (boxmaxs[1] - boxmins[1]) * positions[i][1];
3972 end[2] = boxmins[2] + (boxmaxs[2] - boxmins[2]) * positions[i][2];
3973 //trace_t trace = CL_TraceLine(start, end, MOVE_NORMAL, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, MATERIALFLAGMASK_TRANSLUCENT, 0.0f, true, false, NULL, true, true);
3974 trace = CL_Cache_TraceLineSurfaces(start, end, MOVE_NORMAL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT);
3975 // not picky - if the trace ended anywhere in the box we're good
3976 if (BoxesOverlap(trace.endpos, trace.endpos, padmins, padmaxs))
3980 else if (model->brush.TraceLineOfSight(model, start, end, padmins, padmaxs))
3983 // try various random positions
3984 for (j = 0; j < numsamples; j++)
3986 VectorSet(start, lhrandom(eyemins[0], eyemaxs[0]), lhrandom(eyemins[1], eyemaxs[1]), lhrandom(eyemins[2], eyemaxs[2]));
3987 VectorSet(end, lhrandom(boxmins[0], boxmaxs[0]), lhrandom(boxmins[1], boxmaxs[1]), lhrandom(boxmins[2], boxmaxs[2]));
3988 if (r_cullentities_trace_entityocclusion.integer)
3990 trace_t trace = CL_TraceLine(start, end, MOVE_NORMAL, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, MATERIALFLAGMASK_TRANSLUCENT, 0.0f, true, false, NULL, true, true);
3991 // not picky - if the trace ended anywhere in the box we're good
3992 if (BoxesOverlap(trace.endpos, trace.endpos, padmins, padmaxs))
3995 else if (model->brush.TraceLineOfSight(model, start, end, padmins, padmaxs))
4003 static void R_View_UpdateEntityVisible (void)
4008 entity_render_t *ent;
4010 if (r_refdef.envmap || r_fb.water.hideplayer)
4011 renderimask = RENDER_EXTERIORMODEL | RENDER_VIEWMODEL;
4012 else if (chase_active.integer || r_fb.water.renderingscene)
4013 renderimask = RENDER_VIEWMODEL;
4015 renderimask = RENDER_EXTERIORMODEL;
4016 if (!r_drawviewmodel.integer)
4017 renderimask |= RENDER_VIEWMODEL;
4018 if (!r_drawexteriormodel.integer)
4019 renderimask |= RENDER_EXTERIORMODEL;
4020 memset(r_refdef.viewcache.entityvisible, 0, r_refdef.scene.numentities);
4021 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs)
4023 // worldmodel can check visibility
4024 for (i = 0;i < r_refdef.scene.numentities;i++)
4026 ent = r_refdef.scene.entities[i];
4027 if (r_refdef.viewcache.world_novis && !(ent->flags & RENDER_VIEWMODEL))
4029 r_refdef.viewcache.entityvisible[i] = false;
4032 if (!(ent->flags & renderimask))
4033 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)))
4034 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))
4035 r_refdef.viewcache.entityvisible[i] = true;
4040 // no worldmodel or it can't check visibility
4041 for (i = 0;i < r_refdef.scene.numentities;i++)
4043 ent = r_refdef.scene.entities[i];
4044 if (!(ent->flags & renderimask))
4045 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)))
4046 r_refdef.viewcache.entityvisible[i] = true;
4049 if (r_cullentities_trace.integer)
4051 for (i = 0;i < r_refdef.scene.numentities;i++)
4053 if (!r_refdef.viewcache.entityvisible[i])
4055 ent = r_refdef.scene.entities[i];
4056 if (!(ent->flags & (RENDER_VIEWMODEL | RENDER_WORLDOBJECT | RENDER_NODEPTHTEST)) && !(ent->model && (ent->model->name[0] == '*')))
4058 samples = ent->last_trace_visibility == 0 ? r_cullentities_trace_tempentitysamples.integer : r_cullentities_trace_samples.integer;
4059 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))
4060 ent->last_trace_visibility = host.realtime;
4061 if (ent->last_trace_visibility < host.realtime - r_cullentities_trace_delay.value)
4062 r_refdef.viewcache.entityvisible[i] = 0;
4068 /// only used if skyrendermasked, and normally returns false
4069 static int R_DrawBrushModelsSky (void)
4072 entity_render_t *ent;
4075 for (i = 0;i < r_refdef.scene.numentities;i++)
4077 if (!r_refdef.viewcache.entityvisible[i])
4079 ent = r_refdef.scene.entities[i];
4080 if (!ent->model || !ent->model->DrawSky)
4082 ent->model->DrawSky(ent);
4088 static void R_DrawNoModel(entity_render_t *ent);
4089 static void R_DrawModels(void)
4092 entity_render_t *ent;
4094 for (i = 0;i < r_refdef.scene.numentities;i++)
4096 if (!r_refdef.viewcache.entityvisible[i])
4098 ent = r_refdef.scene.entities[i];
4099 r_refdef.stats[r_stat_entities]++;
4101 if (ent->model && ent->model->Draw != NULL)
4102 ent->model->Draw(ent);
4108 static void R_DrawModelsDepth(void)
4111 entity_render_t *ent;
4113 for (i = 0;i < r_refdef.scene.numentities;i++)
4115 if (!r_refdef.viewcache.entityvisible[i])
4117 ent = r_refdef.scene.entities[i];
4118 if (ent->model && ent->model->DrawDepth != NULL)
4119 ent->model->DrawDepth(ent);
4123 static void R_DrawModelsDebug(void)
4126 entity_render_t *ent;
4128 for (i = 0;i < r_refdef.scene.numentities;i++)
4130 if (!r_refdef.viewcache.entityvisible[i])
4132 ent = r_refdef.scene.entities[i];
4133 if (ent->model && ent->model->DrawDebug != NULL)
4134 ent->model->DrawDebug(ent);
4138 static void R_DrawModelsAddWaterPlanes(void)
4141 entity_render_t *ent;
4143 for (i = 0;i < r_refdef.scene.numentities;i++)
4145 if (!r_refdef.viewcache.entityvisible[i])
4147 ent = r_refdef.scene.entities[i];
4148 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
4149 ent->model->DrawAddWaterPlanes(ent);
4153 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}};
4155 void R_HDR_UpdateIrisAdaptation(const vec3_t point)
4157 if (r_hdr_irisadaptation.integer)
4162 vec3_t diffusenormal;
4164 vec_t brightness = 0.0f;
4169 VectorCopy(r_refdef.view.forward, forward);
4170 for (c = 0;c < (int)(sizeof(irisvecs)/sizeof(irisvecs[0]));c++)
4172 p[0] = point[0] + irisvecs[c][0] * r_hdr_irisadaptation_radius.value;
4173 p[1] = point[1] + irisvecs[c][1] * r_hdr_irisadaptation_radius.value;
4174 p[2] = point[2] + irisvecs[c][2] * r_hdr_irisadaptation_radius.value;
4175 R_CompleteLightPoint(ambient, diffuse, diffusenormal, p, LP_LIGHTMAP | LP_RTWORLD | LP_DYNLIGHT, r_refdef.scene.lightmapintensity, r_refdef.scene.ambientintensity);
4176 d = DotProduct(forward, diffusenormal);
4177 brightness += VectorLength(ambient);
4179 brightness += d * VectorLength(diffuse);
4181 brightness *= 1.0f / c;
4182 brightness += 0.00001f; // make sure it's never zero
4183 goal = r_hdr_irisadaptation_multiplier.value / brightness;
4184 goal = bound(r_hdr_irisadaptation_minvalue.value, goal, r_hdr_irisadaptation_maxvalue.value);
4185 current = r_hdr_irisadaptation_value.value;
4187 current = min(current + r_hdr_irisadaptation_fade_up.value * cl.realframetime, goal);
4188 else if (current > goal)
4189 current = max(current - r_hdr_irisadaptation_fade_down.value * cl.realframetime, goal);
4190 if (fabs(r_hdr_irisadaptation_value.value - current) > 0.0001f)
4191 Cvar_SetValueQuick(&r_hdr_irisadaptation_value, current);
4193 else if (r_hdr_irisadaptation_value.value != 1.0f)
4194 Cvar_SetValueQuick(&r_hdr_irisadaptation_value, 1.0f);
4197 extern cvar_t r_lockvisibility;
4198 extern cvar_t r_lockpvs;
4200 static void R_View_SetFrustum(const int *scissor)
4203 double fpx = +1, fnx = -1, fpy = +1, fny = -1;
4204 vec3_t forward, left, up, origin, v;
4205 if(r_lockvisibility.integer)
4209 // flipped x coordinates (because x points left here)
4210 fpx = 1.0 - 2.0 * (scissor[0] - r_refdef.view.viewport.x) / (double) (r_refdef.view.viewport.width);
4211 fnx = 1.0 - 2.0 * (scissor[0] + scissor[2] - r_refdef.view.viewport.x) / (double) (r_refdef.view.viewport.width);
4212 // non-flipped y coordinates
4213 fny = -1.0 + 2.0 * (scissor[1] - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
4214 fpy = -1.0 + 2.0 * (scissor[1] + scissor[3] - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
4217 // we can't trust r_refdef.view.forward and friends in reflected scenes
4218 Matrix4x4_ToVectors(&r_refdef.view.matrix, forward, left, up, origin);
4221 r_refdef.view.frustum[0].normal[0] = 0 - 1.0 / r_refdef.view.frustum_x;
4222 r_refdef.view.frustum[0].normal[1] = 0 - 0;
4223 r_refdef.view.frustum[0].normal[2] = -1 - 0;
4224 r_refdef.view.frustum[1].normal[0] = 0 + 1.0 / r_refdef.view.frustum_x;
4225 r_refdef.view.frustum[1].normal[1] = 0 + 0;
4226 r_refdef.view.frustum[1].normal[2] = -1 + 0;
4227 r_refdef.view.frustum[2].normal[0] = 0 - 0;
4228 r_refdef.view.frustum[2].normal[1] = 0 - 1.0 / r_refdef.view.frustum_y;
4229 r_refdef.view.frustum[2].normal[2] = -1 - 0;
4230 r_refdef.view.frustum[3].normal[0] = 0 + 0;
4231 r_refdef.view.frustum[3].normal[1] = 0 + 1.0 / r_refdef.view.frustum_y;
4232 r_refdef.view.frustum[3].normal[2] = -1 + 0;
4236 zNear = r_refdef.nearclip;
4237 nudge = 1.0 - 1.0 / (1<<23);
4238 r_refdef.view.frustum[4].normal[0] = 0 - 0;
4239 r_refdef.view.frustum[4].normal[1] = 0 - 0;
4240 r_refdef.view.frustum[4].normal[2] = -1 - -nudge;
4241 r_refdef.view.frustum[4].dist = 0 - -2 * zNear * nudge;
4242 r_refdef.view.frustum[5].normal[0] = 0 + 0;
4243 r_refdef.view.frustum[5].normal[1] = 0 + 0;
4244 r_refdef.view.frustum[5].normal[2] = -1 + -nudge;
4245 r_refdef.view.frustum[5].dist = 0 + -2 * zNear * nudge;
4251 r_refdef.view.frustum[0].normal[0] = m[3] - m[0];
4252 r_refdef.view.frustum[0].normal[1] = m[7] - m[4];
4253 r_refdef.view.frustum[0].normal[2] = m[11] - m[8];
4254 r_refdef.view.frustum[0].dist = m[15] - m[12];
4256 r_refdef.view.frustum[1].normal[0] = m[3] + m[0];
4257 r_refdef.view.frustum[1].normal[1] = m[7] + m[4];
4258 r_refdef.view.frustum[1].normal[2] = m[11] + m[8];
4259 r_refdef.view.frustum[1].dist = m[15] + m[12];
4261 r_refdef.view.frustum[2].normal[0] = m[3] - m[1];
4262 r_refdef.view.frustum[2].normal[1] = m[7] - m[5];
4263 r_refdef.view.frustum[2].normal[2] = m[11] - m[9];
4264 r_refdef.view.frustum[2].dist = m[15] - m[13];
4266 r_refdef.view.frustum[3].normal[0] = m[3] + m[1];
4267 r_refdef.view.frustum[3].normal[1] = m[7] + m[5];
4268 r_refdef.view.frustum[3].normal[2] = m[11] + m[9];
4269 r_refdef.view.frustum[3].dist = m[15] + m[13];
4271 r_refdef.view.frustum[4].normal[0] = m[3] - m[2];
4272 r_refdef.view.frustum[4].normal[1] = m[7] - m[6];
4273 r_refdef.view.frustum[4].normal[2] = m[11] - m[10];
4274 r_refdef.view.frustum[4].dist = m[15] - m[14];
4276 r_refdef.view.frustum[5].normal[0] = m[3] + m[2];
4277 r_refdef.view.frustum[5].normal[1] = m[7] + m[6];
4278 r_refdef.view.frustum[5].normal[2] = m[11] + m[10];
4279 r_refdef.view.frustum[5].dist = m[15] + m[14];
4282 if (r_refdef.view.useperspective)
4284 // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
4285 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]);
4286 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]);
4287 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]);
4288 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]);
4290 // then the normals from the corners relative to origin
4291 CrossProduct(r_refdef.view.frustumcorner[2], r_refdef.view.frustumcorner[0], r_refdef.view.frustum[0].normal);
4292 CrossProduct(r_refdef.view.frustumcorner[1], r_refdef.view.frustumcorner[3], r_refdef.view.frustum[1].normal);
4293 CrossProduct(r_refdef.view.frustumcorner[0], r_refdef.view.frustumcorner[1], r_refdef.view.frustum[2].normal);
4294 CrossProduct(r_refdef.view.frustumcorner[3], r_refdef.view.frustumcorner[2], r_refdef.view.frustum[3].normal);
4296 // in a NORMAL view, forward cross left == up
4297 // in a REFLECTED view, forward cross left == down
4298 // so our cross products above need to be adjusted for a left handed coordinate system
4299 CrossProduct(forward, left, v);
4300 if(DotProduct(v, up) < 0)
4302 VectorNegate(r_refdef.view.frustum[0].normal, r_refdef.view.frustum[0].normal);
4303 VectorNegate(r_refdef.view.frustum[1].normal, r_refdef.view.frustum[1].normal);
4304 VectorNegate(r_refdef.view.frustum[2].normal, r_refdef.view.frustum[2].normal);
4305 VectorNegate(r_refdef.view.frustum[3].normal, r_refdef.view.frustum[3].normal);
4308 // Leaving those out was a mistake, those were in the old code, and they
4309 // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
4310 // I couldn't reproduce it after adding those normalizations. --blub
4311 VectorNormalize(r_refdef.view.frustum[0].normal);
4312 VectorNormalize(r_refdef.view.frustum[1].normal);
4313 VectorNormalize(r_refdef.view.frustum[2].normal);
4314 VectorNormalize(r_refdef.view.frustum[3].normal);
4316 // make the corners absolute
4317 VectorAdd(r_refdef.view.frustumcorner[0], r_refdef.view.origin, r_refdef.view.frustumcorner[0]);
4318 VectorAdd(r_refdef.view.frustumcorner[1], r_refdef.view.origin, r_refdef.view.frustumcorner[1]);
4319 VectorAdd(r_refdef.view.frustumcorner[2], r_refdef.view.origin, r_refdef.view.frustumcorner[2]);
4320 VectorAdd(r_refdef.view.frustumcorner[3], r_refdef.view.origin, r_refdef.view.frustumcorner[3]);
4323 VectorCopy(forward, r_refdef.view.frustum[4].normal);
4325 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal);
4326 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal);
4327 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal);
4328 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal);
4329 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) + r_refdef.nearclip;
4333 VectorScale(left, -1.0f, r_refdef.view.frustum[0].normal);
4334 VectorScale(left, 1.0f, r_refdef.view.frustum[1].normal);
4335 VectorScale(up, -1.0f, r_refdef.view.frustum[2].normal);
4336 VectorScale(up, 1.0f, r_refdef.view.frustum[3].normal);
4337 VectorScale(forward, -1.0f, r_refdef.view.frustum[4].normal);
4338 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal) - r_refdef.view.ortho_x;
4339 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal) - r_refdef.view.ortho_x;
4340 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal) - r_refdef.view.ortho_y;
4341 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal) - r_refdef.view.ortho_y;
4342 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) - r_refdef.farclip;
4344 r_refdef.view.numfrustumplanes = 5;
4346 if (r_refdef.view.useclipplane)
4348 r_refdef.view.numfrustumplanes = 6;
4349 r_refdef.view.frustum[5] = r_refdef.view.clipplane;
4352 for (i = 0;i < r_refdef.view.numfrustumplanes;i++)
4353 PlaneClassify(r_refdef.view.frustum + i);
4355 // LadyHavoc: note to all quake engine coders, Quake had a special case
4356 // for 90 degrees which assumed a square view (wrong), so I removed it,
4357 // Quake2 has it disabled as well.
4359 // rotate R_VIEWFORWARD right by FOV_X/2 degrees
4360 //RotatePointAroundVector( r_refdef.view.frustum[0].normal, up, forward, -(90 - r_refdef.fov_x / 2));
4361 //r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, frustum[0].normal);
4362 //PlaneClassify(&frustum[0]);
4364 // rotate R_VIEWFORWARD left by FOV_X/2 degrees
4365 //RotatePointAroundVector( r_refdef.view.frustum[1].normal, up, forward, (90 - r_refdef.fov_x / 2));
4366 //r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, frustum[1].normal);
4367 //PlaneClassify(&frustum[1]);
4369 // rotate R_VIEWFORWARD up by FOV_X/2 degrees
4370 //RotatePointAroundVector( r_refdef.view.frustum[2].normal, left, forward, -(90 - r_refdef.fov_y / 2));
4371 //r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, frustum[2].normal);
4372 //PlaneClassify(&frustum[2]);
4374 // rotate R_VIEWFORWARD down by FOV_X/2 degrees
4375 //RotatePointAroundVector( r_refdef.view.frustum[3].normal, left, forward, (90 - r_refdef.fov_y / 2));
4376 //r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, frustum[3].normal);
4377 //PlaneClassify(&frustum[3]);
4380 //VectorCopy(forward, r_refdef.view.frustum[4].normal);
4381 //r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, frustum[4].normal) + r_nearclip.value;
4382 //PlaneClassify(&frustum[4]);
4385 static void R_View_UpdateWithScissor(const int *myscissor)
4387 R_Main_ResizeViewCache();
4388 R_View_SetFrustum(myscissor);
4389 R_View_WorldVisibility(!r_refdef.view.usevieworiginculling);
4390 R_View_UpdateEntityVisible();
4393 static void R_View_Update(void)
4395 R_Main_ResizeViewCache();
4396 R_View_SetFrustum(NULL);
4397 R_View_WorldVisibility(!r_refdef.view.usevieworiginculling);
4398 R_View_UpdateEntityVisible();
4401 float viewscalefpsadjusted = 1.0f;
4403 void R_SetupView(qbool allowwaterclippingplane, int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4405 const float *customclipplane = NULL;
4407 int /*rtwidth,*/ rtheight;
4408 if (r_refdef.view.useclipplane && allowwaterclippingplane)
4410 // LadyHavoc: couldn't figure out how to make this approach work the same in DPSOFTRAST
4411 vec_t dist = r_refdef.view.clipplane.dist - r_water_clippingplanebias.value;
4412 vec_t viewdist = DotProduct(r_refdef.view.origin, r_refdef.view.clipplane.normal);
4413 if (viewdist < r_refdef.view.clipplane.dist + r_water_clippingplanebias.value)
4414 dist = r_refdef.view.clipplane.dist;
4415 plane[0] = r_refdef.view.clipplane.normal[0];
4416 plane[1] = r_refdef.view.clipplane.normal[1];
4417 plane[2] = r_refdef.view.clipplane.normal[2];
4419 customclipplane = plane;
4422 //rtwidth = viewfbo ? R_TextureWidth(viewdepthtexture ? viewdepthtexture : viewcolortexture) : vid.width;
4423 rtheight = viewfbo ? R_TextureHeight(viewdepthtexture ? viewdepthtexture : viewcolortexture) : vid.height;
4425 if (!r_refdef.view.useperspective)
4426 R_Viewport_InitOrtho3D(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, rtheight - viewheight - viewy, viewwidth, viewheight, r_refdef.view.ortho_x, r_refdef.view.ortho_y, -r_refdef.farclip, r_refdef.farclip, customclipplane);
4427 else if (vid.stencil && r_useinfinitefarclip.integer)
4428 R_Viewport_InitPerspectiveInfinite(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, rtheight - viewheight - viewy, viewwidth, viewheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, customclipplane);
4430 R_Viewport_InitPerspective(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, rtheight - viewheight - viewy, viewwidth, viewheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, r_refdef.farclip, customclipplane);
4431 R_Mesh_SetRenderTargets(viewfbo, viewdepthtexture, viewcolortexture, NULL, NULL, NULL);
4432 R_SetViewport(&r_refdef.view.viewport);
4435 void R_EntityMatrix(const matrix4x4_t *matrix)
4437 if (gl_modelmatrixchanged || memcmp(matrix, &gl_modelmatrix, sizeof(matrix4x4_t)))
4439 gl_modelmatrixchanged = false;
4440 gl_modelmatrix = *matrix;
4441 Matrix4x4_Concat(&gl_modelviewmatrix, &gl_viewmatrix, &gl_modelmatrix);
4442 Matrix4x4_Concat(&gl_modelviewprojectionmatrix, &gl_projectionmatrix, &gl_modelviewmatrix);
4443 Matrix4x4_ToArrayFloatGL(&gl_modelviewmatrix, gl_modelview16f);
4444 Matrix4x4_ToArrayFloatGL(&gl_modelviewprojectionmatrix, gl_modelviewprojection16f);
4446 switch(vid.renderpath)
4448 case RENDERPATH_GL32:
4449 case RENDERPATH_GLES2:
4450 if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
4451 if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
4457 void R_ResetViewRendering2D_Common(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight, float x2, float y2)
4459 r_viewport_t viewport;
4463 // GL is weird because it's bottom to top, r_refdef.view.y is top to bottom
4464 R_Viewport_InitOrtho(&viewport, &identitymatrix, viewx, vid.height - viewheight - viewy, viewwidth, viewheight, 0, 0, x2, y2, -10, 100, NULL);
4465 R_Mesh_SetRenderTargets(viewfbo, viewdepthtexture, viewcolortexture, NULL, NULL, NULL);
4466 R_SetViewport(&viewport);
4467 GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
4468 GL_Color(1, 1, 1, 1);
4469 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4470 GL_BlendFunc(GL_ONE, GL_ZERO);
4471 GL_ScissorTest(false);
4472 GL_DepthMask(false);
4473 GL_DepthRange(0, 1);
4474 GL_DepthTest(false);
4475 GL_DepthFunc(GL_LEQUAL);
4476 R_EntityMatrix(&identitymatrix);
4477 R_Mesh_ResetTextureState();
4478 GL_PolygonOffset(0, 0);
4479 switch(vid.renderpath)
4481 case RENDERPATH_GL32:
4482 case RENDERPATH_GLES2:
4483 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
4486 GL_CullFace(GL_NONE);
4491 void R_ResetViewRendering2D(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4493 R_ResetViewRendering2D_Common(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight, 1.0f, 1.0f);
4496 void R_ResetViewRendering3D(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4498 R_SetupView(true, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
4499 GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
4500 GL_Color(1, 1, 1, 1);
4501 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4502 GL_BlendFunc(GL_ONE, GL_ZERO);
4503 GL_ScissorTest(true);
4505 GL_DepthRange(0, 1);
4507 GL_DepthFunc(GL_LEQUAL);
4508 R_EntityMatrix(&identitymatrix);
4509 R_Mesh_ResetTextureState();
4510 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
4511 switch(vid.renderpath)
4513 case RENDERPATH_GL32:
4514 case RENDERPATH_GLES2:
4515 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
4518 GL_CullFace(r_refdef.view.cullface_back);
4523 R_RenderView_UpdateViewVectors
4526 void R_RenderView_UpdateViewVectors(void)
4528 // break apart the view matrix into vectors for various purposes
4529 // it is important that this occurs outside the RenderScene function because that can be called from reflection renders, where the vectors come out wrong
4530 // however the r_refdef.view.origin IS updated in RenderScene intentionally - otherwise the sky renders at the wrong origin, etc
4531 Matrix4x4_ToVectors(&r_refdef.view.matrix, r_refdef.view.forward, r_refdef.view.left, r_refdef.view.up, r_refdef.view.origin);
4532 VectorNegate(r_refdef.view.left, r_refdef.view.right);
4533 // make an inverted copy of the view matrix for tracking sprites
4534 Matrix4x4_Invert_Full(&r_refdef.view.inverse_matrix, &r_refdef.view.matrix);
4537 void R_RenderTarget_FreeUnused(qbool force)
4539 unsigned int i, j, end;
4540 end = (unsigned int)Mem_ExpandableArray_IndexRange(&r_fb.rendertargets); // checked
4541 for (i = 0; i < end; i++)
4543 r_rendertarget_t *r = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, i);
4544 // free resources for rendertargets that have not been used for a while
4545 // (note: this check is run after the frame render, so any targets used
4546 // this frame will not be affected even at low framerates)
4547 if (r && (host.realtime - r->lastusetime > 0.2 || force))
4550 R_Mesh_DestroyFramebufferObject(r->fbo);
4551 for (j = 0; j < sizeof(r->colortexture) / sizeof(r->colortexture[0]); j++)
4552 if (r->colortexture[j])
4553 R_FreeTexture(r->colortexture[j]);
4554 if (r->depthtexture)
4555 R_FreeTexture(r->depthtexture);
4556 Mem_ExpandableArray_FreeRecord(&r_fb.rendertargets, r);
4561 static void R_CalcTexCoordsForView(float x, float y, float w, float h, float tw, float th, float *texcoord2f)
4563 float iw = 1.0f / tw, ih = 1.0f / th, x1, y1, x2, y2;
4567 y2 = (th - y - h) * ih;
4578 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)
4580 unsigned int i, j, end;
4581 r_rendertarget_t *r = NULL;
4583 // first try to reuse an existing slot if possible
4584 end = (unsigned int)Mem_ExpandableArray_IndexRange(&r_fb.rendertargets); // checked
4585 for (i = 0; i < end; i++)
4587 r = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, i);
4588 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)
4593 // no unused exact match found, so we have to make one in the first unused slot
4594 r = (r_rendertarget_t *)Mem_ExpandableArray_AllocRecord(&r_fb.rendertargets);
4595 r->texturewidth = texturewidth;
4596 r->textureheight = textureheight;
4597 r->colortextype[0] = colortextype0;
4598 r->colortextype[1] = colortextype1;
4599 r->colortextype[2] = colortextype2;
4600 r->colortextype[3] = colortextype3;
4601 r->depthtextype = depthtextype;
4602 r->depthisrenderbuffer = depthisrenderbuffer;
4603 for (j = 0; j < 4; j++)
4604 if (r->colortextype[j])
4605 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);
4606 if (r->depthtextype)
4608 if (r->depthisrenderbuffer)
4609 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);
4611 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);
4613 r->fbo = R_Mesh_CreateFramebufferObject(r->depthtexture, r->colortexture[0], r->colortexture[1], r->colortexture[2], r->colortexture[3]);
4615 r_refdef.stats[r_stat_rendertargets_used]++;
4616 r_refdef.stats[r_stat_rendertargets_pixels] += r->texturewidth * r->textureheight;
4617 r->lastusetime = host.realtime;
4618 R_CalcTexCoordsForView(0, 0, r->texturewidth, r->textureheight, r->texturewidth, r->textureheight, r->texcoord2f);
4622 static void R_Water_StartFrame(int viewwidth, int viewheight)
4624 int waterwidth, waterheight;
4626 if (viewwidth > (int)vid.maxtexturesize_2d || viewheight > (int)vid.maxtexturesize_2d)
4629 // set waterwidth and waterheight to the water resolution that will be
4630 // used (often less than the screen resolution for faster rendering)
4631 waterwidth = (int)bound(16, viewwidth * r_water_resolutionmultiplier.value, viewwidth);
4632 waterheight = (int)bound(16, viewheight * r_water_resolutionmultiplier.value, viewheight);
4634 if (!r_water.integer || r_showsurfaces.integer || r_lockvisibility.integer || r_lockpvs.integer)
4635 waterwidth = waterheight = 0;
4637 // set up variables that will be used in shader setup
4638 r_fb.water.waterwidth = waterwidth;
4639 r_fb.water.waterheight = waterheight;
4640 r_fb.water.texturewidth = waterwidth;
4641 r_fb.water.textureheight = waterheight;
4642 r_fb.water.camerawidth = waterwidth;
4643 r_fb.water.cameraheight = waterheight;
4644 r_fb.water.screenscale[0] = 0.5f;
4645 r_fb.water.screenscale[1] = 0.5f;
4646 r_fb.water.screencenter[0] = 0.5f;
4647 r_fb.water.screencenter[1] = 0.5f;
4648 r_fb.water.enabled = waterwidth != 0;
4650 r_fb.water.maxwaterplanes = MAX_WATERPLANES;
4651 r_fb.water.numwaterplanes = 0;
4654 void R_Water_AddWaterPlane(msurface_t *surface, int entno)
4656 int planeindex, bestplaneindex, vertexindex;
4657 vec3_t mins, maxs, normal, center, v, n;
4658 vec_t planescore, bestplanescore;
4660 r_waterstate_waterplane_t *p;
4661 texture_t *t = R_GetCurrentTexture(surface->texture);
4663 rsurface.texture = t;
4664 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS, 1, ((const msurface_t **)&surface));
4665 // if the model has no normals, it's probably off-screen and they were not generated, so don't add it anyway
4666 if (!rsurface.batchnormal3f || rsurface.batchnumvertices < 1)
4668 // average the vertex normals, find the surface bounds (after deformvertexes)
4669 Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f, v);
4670 Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f, n);
4671 VectorCopy(n, normal);
4672 VectorCopy(v, mins);
4673 VectorCopy(v, maxs);
4674 for (vertexindex = 1;vertexindex < rsurface.batchnumvertices;vertexindex++)
4676 Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f + vertexindex*3, v);
4677 Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f + vertexindex*3, n);
4678 VectorAdd(normal, n, normal);
4679 mins[0] = min(mins[0], v[0]);
4680 mins[1] = min(mins[1], v[1]);
4681 mins[2] = min(mins[2], v[2]);
4682 maxs[0] = max(maxs[0], v[0]);
4683 maxs[1] = max(maxs[1], v[1]);
4684 maxs[2] = max(maxs[2], v[2]);
4686 VectorNormalize(normal);
4687 VectorMAM(0.5f, mins, 0.5f, maxs, center);
4689 VectorCopy(normal, plane.normal);
4690 VectorNormalize(plane.normal);
4691 plane.dist = DotProduct(center, plane.normal);
4692 PlaneClassify(&plane);
4693 if (PlaneDiff(r_refdef.view.origin, &plane) < 0)
4695 // skip backfaces (except if nocullface is set)
4696 // if (!(t->currentmaterialflags & MATERIALFLAG_NOCULLFACE))
4698 VectorNegate(plane.normal, plane.normal);
4700 PlaneClassify(&plane);
4704 // find a matching plane if there is one
4705 bestplaneindex = -1;
4706 bestplanescore = 1048576.0f;
4707 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
4709 if(p->camera_entity == t->camera_entity)
4711 planescore = 1.0f - DotProduct(plane.normal, p->plane.normal) + fabs(plane.dist - p->plane.dist) * 0.001f;
4712 if (bestplaneindex < 0 || bestplanescore > planescore)
4714 bestplaneindex = planeindex;
4715 bestplanescore = planescore;
4719 planeindex = bestplaneindex;
4721 // if this surface does not fit any known plane rendered this frame, add one
4722 if (planeindex < 0 || bestplanescore > 0.001f)
4724 if (r_fb.water.numwaterplanes < r_fb.water.maxwaterplanes)
4726 // store the new plane
4727 planeindex = r_fb.water.numwaterplanes++;
4728 p = r_fb.water.waterplanes + planeindex;
4730 // clear materialflags and pvs
4731 p->materialflags = 0;
4732 p->pvsvalid = false;
4733 p->camera_entity = t->camera_entity;
4734 VectorCopy(mins, p->mins);
4735 VectorCopy(maxs, p->maxs);
4739 // We're totally screwed.
4745 // merge mins/maxs when we're adding this surface to the plane
4746 p = r_fb.water.waterplanes + planeindex;
4747 p->mins[0] = min(p->mins[0], mins[0]);
4748 p->mins[1] = min(p->mins[1], mins[1]);
4749 p->mins[2] = min(p->mins[2], mins[2]);
4750 p->maxs[0] = max(p->maxs[0], maxs[0]);
4751 p->maxs[1] = max(p->maxs[1], maxs[1]);
4752 p->maxs[2] = max(p->maxs[2], maxs[2]);
4754 // merge this surface's materialflags into the waterplane
4755 p->materialflags |= t->currentmaterialflags;
4756 if(!(p->materialflags & MATERIALFLAG_CAMERA))
4758 // merge this surface's PVS into the waterplane
4759 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS
4760 && r_refdef.scene.worldmodel->brush.PointInLeaf && r_refdef.scene.worldmodel->brush.PointInLeaf(r_refdef.scene.worldmodel, center)->clusterindex >= 0)
4762 r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, center, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
4768 extern cvar_t r_drawparticles;
4769 extern cvar_t r_drawdecals;
4771 static void R_Water_ProcessPlanes(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, int viewx, int viewy, int viewwidth, int viewheight)
4774 r_refdef_view_t originalview;
4775 r_refdef_view_t myview;
4776 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;
4777 r_waterstate_waterplane_t *p;
4779 r_rendertarget_t *rt;
4781 originalview = r_refdef.view;
4783 // lowquality hack, temporarily shut down some cvars and restore afterwards
4784 qualityreduction = r_water_lowquality.integer;
4785 if (qualityreduction > 0)
4787 if (qualityreduction >= 1)
4789 old_r_shadows = r_shadows.integer;
4790 old_r_worldrtlight = r_shadow_realtime_world.integer;
4791 old_r_dlight = r_shadow_realtime_dlight.integer;
4792 Cvar_SetValueQuick(&r_shadows, 0);
4793 Cvar_SetValueQuick(&r_shadow_realtime_world, 0);
4794 Cvar_SetValueQuick(&r_shadow_realtime_dlight, 0);
4796 if (qualityreduction >= 2)
4798 old_r_dynamic = r_dynamic.integer;
4799 old_r_particles = r_drawparticles.integer;
4800 old_r_decals = r_drawdecals.integer;
4801 Cvar_SetValueQuick(&r_dynamic, 0);
4802 Cvar_SetValueQuick(&r_drawparticles, 0);
4803 Cvar_SetValueQuick(&r_drawdecals, 0);
4807 for (planeindex = 0, p = r_fb.water.waterplanes; planeindex < r_fb.water.numwaterplanes; planeindex++, p++)
4809 p->rt_reflection = NULL;
4810 p->rt_refraction = NULL;
4811 p->rt_camera = NULL;
4815 r_refdef.view = originalview;
4816 r_refdef.view.showdebug = false;
4817 r_refdef.view.width = r_fb.water.waterwidth;
4818 r_refdef.view.height = r_fb.water.waterheight;
4819 r_refdef.view.useclipplane = true;
4820 myview = r_refdef.view;
4821 r_fb.water.renderingscene = true;
4822 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
4824 if (r_water_cameraentitiesonly.value != 0 && !p->camera_entity)
4827 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
4829 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);
4830 if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4832 r_refdef.view = myview;
4833 Matrix4x4_Reflect(&r_refdef.view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
4834 Matrix4x4_OriginFromMatrix(&r_refdef.view.matrix, r_refdef.view.origin);
4835 if(r_water_scissormode.integer)
4837 R_SetupView(true, rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, r_fb.water.waterwidth, r_fb.water.waterheight);
4838 if (R_ScissorForBBox(p->mins, p->maxs, myscissor))
4840 p->rt_reflection = NULL;
4841 p->rt_refraction = NULL;
4842 p->rt_camera = NULL;
4847 r_refdef.view.clipplane = p->plane;
4848 // reflected view origin may be in solid, so don't cull with it
4849 r_refdef.view.usevieworiginculling = false;
4850 // reverse the cullface settings for this render
4851 r_refdef.view.cullface_front = GL_FRONT;
4852 r_refdef.view.cullface_back = GL_BACK;
4853 // combined pvs (based on what can be seen from each surface center)
4854 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
4856 r_refdef.view.usecustompvs = true;
4858 memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4860 memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4863 r_fb.water.hideplayer = ((r_water_hideplayer.integer >= 2) && !chase_active.integer);
4864 R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4865 GL_ScissorTest(false);
4866 R_ClearScreen(r_refdef.fogenabled);
4867 GL_ScissorTest(true);
4868 if(r_water_scissormode.integer & 2)
4869 R_View_UpdateWithScissor(myscissor);
4872 R_AnimCache_CacheVisibleEntities();
4873 if(r_water_scissormode.integer & 1)
4874 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
4875 R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4877 r_fb.water.hideplayer = false;
4878 p->rt_reflection = rt;
4881 // render the normal view scene and copy into texture
4882 // (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)
4883 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
4885 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);
4886 if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4888 r_refdef.view = myview;
4889 if(r_water_scissormode.integer)
4891 R_SetupView(true, rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, r_fb.water.waterwidth, r_fb.water.waterheight);
4892 if (R_ScissorForBBox(p->mins, p->maxs, myscissor))
4894 p->rt_reflection = NULL;
4895 p->rt_refraction = NULL;
4896 p->rt_camera = NULL;
4901 // combined pvs (based on what can be seen from each surface center)
4902 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
4904 r_refdef.view.usecustompvs = true;
4906 memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4908 memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4911 r_fb.water.hideplayer = ((r_water_hideplayer.integer >= 1) && !chase_active.integer);
4913 r_refdef.view.clipplane = p->plane;
4914 VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
4915 r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
4917 if((p->materialflags & MATERIALFLAG_CAMERA) && p->camera_entity)
4919 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
4920 r_fb.water.hideplayer = false; // we don't want to hide the player model from these ones
4921 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
4922 R_RenderView_UpdateViewVectors();
4923 if(r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
4925 r_refdef.view.usecustompvs = true;
4926 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);
4930 PlaneClassify(&r_refdef.view.clipplane);
4932 R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4933 GL_ScissorTest(false);
4934 R_ClearScreen(r_refdef.fogenabled);
4935 GL_ScissorTest(true);
4936 if(r_water_scissormode.integer & 2)
4937 R_View_UpdateWithScissor(myscissor);
4940 R_AnimCache_CacheVisibleEntities();
4941 if(r_water_scissormode.integer & 1)
4942 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
4943 R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4945 r_fb.water.hideplayer = false;
4946 p->rt_refraction = rt;
4948 else if (p->materialflags & MATERIALFLAG_CAMERA)
4950 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);
4951 if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4953 r_refdef.view = myview;
4955 r_refdef.view.clipplane = p->plane;
4956 VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
4957 r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
4959 r_refdef.view.width = r_fb.water.camerawidth;
4960 r_refdef.view.height = r_fb.water.cameraheight;
4961 r_refdef.view.frustum_x = 1; // tan(45 * M_PI / 180.0);
4962 r_refdef.view.frustum_y = 1; // tan(45 * M_PI / 180.0);
4963 r_refdef.view.ortho_x = 90; // abused as angle by VM_CL_R_SetView
4964 r_refdef.view.ortho_y = 90; // abused as angle by VM_CL_R_SetView
4966 if(p->camera_entity)
4968 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
4969 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
4972 // note: all of the view is used for displaying... so
4973 // there is no use in scissoring
4975 // reverse the cullface settings for this render
4976 r_refdef.view.cullface_front = GL_FRONT;
4977 r_refdef.view.cullface_back = GL_BACK;
4978 // also reverse the view matrix
4979 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
4980 R_RenderView_UpdateViewVectors();
4981 if(p->camera_entity && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
4983 r_refdef.view.usecustompvs = true;
4984 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);
4987 // camera needs no clipplane
4988 r_refdef.view.useclipplane = false;
4989 // TODO: is the camera origin always valid? if so we don't need to clear this
4990 r_refdef.view.usevieworiginculling = false;
4992 PlaneClassify(&r_refdef.view.clipplane);
4994 r_fb.water.hideplayer = false;
4996 R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4997 GL_ScissorTest(false);
4998 R_ClearScreen(r_refdef.fogenabled);
4999 GL_ScissorTest(true);
5001 R_AnimCache_CacheVisibleEntities();
5002 R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
5004 r_fb.water.hideplayer = false;
5009 r_fb.water.renderingscene = false;
5010 r_refdef.view = originalview;
5011 R_ResetViewRendering3D(fbo, depthtexture, colortexture, viewx, viewy, viewwidth, viewheight);
5013 R_AnimCache_CacheVisibleEntities();
5016 r_refdef.view = originalview;
5017 r_fb.water.renderingscene = false;
5018 Cvar_SetValueQuick(&r_water, 0);
5019 Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed! Turned off r_water.\n");
5021 // lowquality hack, restore cvars
5022 if (qualityreduction > 0)
5024 if (qualityreduction >= 1)
5026 Cvar_SetValueQuick(&r_shadows, old_r_shadows);
5027 Cvar_SetValueQuick(&r_shadow_realtime_world, old_r_worldrtlight);
5028 Cvar_SetValueQuick(&r_shadow_realtime_dlight, old_r_dlight);
5030 if (qualityreduction >= 2)
5032 Cvar_SetValueQuick(&r_dynamic, old_r_dynamic);
5033 Cvar_SetValueQuick(&r_drawparticles, old_r_particles);
5034 Cvar_SetValueQuick(&r_drawdecals, old_r_decals);
5039 static void R_Bloom_StartFrame(void)
5041 int screentexturewidth, screentextureheight;
5042 textype_t textype = TEXTYPE_COLORBUFFER;
5045 // clear the pointers to rendertargets from last frame as they're stale
5046 r_fb.rt_screen = NULL;
5047 r_fb.rt_bloom = NULL;
5049 switch (vid.renderpath)
5051 case RENDERPATH_GL32:
5052 r_fb.usedepthtextures = r_usedepthtextures.integer != 0;
5053 if (r_viewfbo.integer == 2) textype = TEXTYPE_COLORBUFFER16F;
5054 if (r_viewfbo.integer == 3) textype = TEXTYPE_COLORBUFFER32F;
5056 case RENDERPATH_GLES2:
5057 r_fb.usedepthtextures = false;
5061 if (r_viewscale_fpsscaling.integer)
5063 double actualframetime;
5064 double targetframetime;
5066 actualframetime = r_refdef.lastdrawscreentime;
5067 targetframetime = (1.0 / r_viewscale_fpsscaling_target.value);
5068 adjust = (targetframetime - actualframetime) * r_viewscale_fpsscaling_multiply.value;
5069 adjust = bound(-r_viewscale_fpsscaling_stepmax.value, adjust, r_viewscale_fpsscaling_stepmax.value);
5070 if (r_viewscale_fpsscaling_stepsize.value > 0)
5073 adjust = floor(adjust / r_viewscale_fpsscaling_stepsize.value) * r_viewscale_fpsscaling_stepsize.value;
5075 adjust = ceil(adjust / r_viewscale_fpsscaling_stepsize.value) * r_viewscale_fpsscaling_stepsize.value;
5077 viewscalefpsadjusted += adjust;
5078 viewscalefpsadjusted = bound(r_viewscale_fpsscaling_min.value, viewscalefpsadjusted, 1.0f);
5081 viewscalefpsadjusted = 1.0f;
5083 scale = r_viewscale.value * sqrt(viewscalefpsadjusted);
5085 scale *= sqrt(vid.samples); // supersampling
5086 scale = bound(0.03125f, scale, 4.0f);
5087 screentexturewidth = (int)ceil(r_refdef.view.width * scale);
5088 screentextureheight = (int)ceil(r_refdef.view.height * scale);
5089 screentexturewidth = bound(1, screentexturewidth, (int)vid.maxtexturesize_2d);
5090 screentextureheight = bound(1, screentextureheight, (int)vid.maxtexturesize_2d);
5092 // set bloomwidth and bloomheight to the bloom resolution that will be
5093 // used (often less than the screen resolution for faster rendering)
5094 r_fb.bloomheight = bound(1, r_bloom_resolution.value * 0.75f, screentextureheight);
5095 r_fb.bloomwidth = r_fb.bloomheight * screentexturewidth / screentextureheight;
5096 r_fb.bloomwidth = bound(1, r_fb.bloomwidth, screentexturewidth);
5097 r_fb.bloomwidth = bound(1, r_fb.bloomwidth, (int)vid.maxtexturesize_2d);
5098 r_fb.bloomheight = bound(1, r_fb.bloomheight, (int)vid.maxtexturesize_2d);
5100 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))
5102 Cvar_SetValueQuick(&r_bloom, 0);
5103 Cvar_SetValueQuick(&r_motionblur, 0);
5104 Cvar_SetValueQuick(&r_damageblur, 0);
5106 if (!r_bloom.integer)
5107 r_fb.bloomwidth = r_fb.bloomheight = 0;
5109 // allocate motionblur ghost texture if needed - this is the only persistent texture and is only useful on the main view
5110 if (r_refdef.view.ismain && (r_fb.screentexturewidth != screentexturewidth || r_fb.screentextureheight != screentextureheight || r_fb.textype != textype))
5112 if (r_fb.ghosttexture)
5113 R_FreeTexture(r_fb.ghosttexture);
5114 r_fb.ghosttexture = NULL;
5116 r_fb.screentexturewidth = screentexturewidth;
5117 r_fb.screentextureheight = screentextureheight;
5118 r_fb.textype = textype;
5120 if (r_fb.screentexturewidth && r_fb.screentextureheight)
5122 if (r_motionblur.value > 0 || r_damageblur.value > 0)
5123 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);
5124 r_fb.ghosttexture_valid = false;
5128 r_fb.rt_screen = R_RenderTarget_Get(screentexturewidth, screentextureheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5130 r_refdef.view.clear = true;
5133 static void R_Bloom_MakeTexture(void)
5136 float xoffset, yoffset, r, brighten;
5137 float colorscale = r_bloom_colorscale.value;
5138 r_viewport_t bloomviewport;
5139 r_rendertarget_t *prev, *cur;
5140 textype_t textype = r_fb.rt_screen->colortextype[0];
5142 r_refdef.stats[r_stat_bloom]++;
5144 R_Viewport_InitOrtho(&bloomviewport, &identitymatrix, 0, 0, r_fb.bloomwidth, r_fb.bloomheight, 0, 0, 1, 1, -10, 100, NULL);
5146 // scale down screen texture to the bloom texture size
5148 prev = r_fb.rt_screen;
5149 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5150 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5151 R_SetViewport(&bloomviewport);
5152 GL_CullFace(GL_NONE);
5153 GL_DepthTest(false);
5154 GL_BlendFunc(GL_ONE, GL_ZERO);
5155 GL_Color(colorscale, colorscale, colorscale, 1);
5156 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, prev->texcoord2f);
5157 // TODO: do boxfilter scale-down in shader?
5158 R_SetupShader_Generic(prev->colortexture[0], false, true, true);
5159 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5160 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5161 // we now have a properly scaled bloom image
5163 // multiply bloom image by itself as many times as desired to darken it
5164 // TODO: if people actually use this it could be done more quickly in the previous shader pass
5165 for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
5168 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5169 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5171 r = bound(0, r_bloom_colorexponent.value / x, 1); // always 0.5 to 1
5173 GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 0);
5174 GL_BlendFunc(GL_SRC_COLOR, GL_ZERO); // square it
5175 GL_Color(1,1,1,1); // no fix factor supported here
5176 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, prev->texcoord2f);
5177 R_SetupShader_Generic(prev->colortexture[0], false, true, false);
5178 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5179 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5183 range = r_bloom_blur.integer * r_fb.bloomwidth / 320;
5184 brighten = r_bloom_brighten.value;
5185 brighten = sqrt(brighten);
5187 brighten *= (3 * range) / (2 * range - 1); // compensate for the "dot particle"
5189 for (dir = 0;dir < 2;dir++)
5192 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5193 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5194 // blend on at multiple vertical offsets to achieve a vertical blur
5195 // TODO: do offset blends using GLSL
5196 // TODO instead of changing the texcoords, change the target positions to prevent artifacts at edges
5198 GL_BlendFunc(GL_ONE, GL_ZERO);
5200 R_SetupShader_Generic(prev->colortexture[0], false, true, false);
5202 for (x = -range;x <= range;x++)
5204 if (!dir){xoffset = 0;yoffset = x;}
5205 else {xoffset = x;yoffset = 0;}
5206 xoffset /= (float)prev->texturewidth;
5207 yoffset /= (float)prev->textureheight;
5208 // compute a texcoord array with the specified x and y offset
5209 r_fb.offsettexcoord2f[0] = xoffset+prev->texcoord2f[0];
5210 r_fb.offsettexcoord2f[1] = yoffset+prev->texcoord2f[1];
5211 r_fb.offsettexcoord2f[2] = xoffset+prev->texcoord2f[2];
5212 r_fb.offsettexcoord2f[3] = yoffset+prev->texcoord2f[3];
5213 r_fb.offsettexcoord2f[4] = xoffset+prev->texcoord2f[4];
5214 r_fb.offsettexcoord2f[5] = yoffset+prev->texcoord2f[5];
5215 r_fb.offsettexcoord2f[6] = xoffset+prev->texcoord2f[6];
5216 r_fb.offsettexcoord2f[7] = yoffset+prev->texcoord2f[7];
5217 // this r value looks like a 'dot' particle, fading sharply to
5218 // black at the edges
5219 // (probably not realistic but looks good enough)
5220 //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
5221 //r = brighten/(range*2+1);
5222 r = brighten / (range * 2 + 1);
5224 r *= (1 - x*x/(float)((range+1)*(range+1)));
5228 GL_Color(r, r, r, 1);
5230 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.offsettexcoord2f);
5232 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5233 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5235 GL_BlendFunc(GL_ONE, GL_ONE);
5240 // now we have the bloom image, so keep track of it
5241 r_fb.rt_bloom = cur;
5244 static void R_BlendView(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5246 uint64_t permutation;
5247 float uservecs[4][4];
5248 rtexture_t *viewtexture;
5249 rtexture_t *bloomtexture;
5251 R_EntityMatrix(&identitymatrix);
5253 if(r_refdef.view.ismain && !R_Stereo_Active() && (r_motionblur.value > 0 || r_damageblur.value > 0) && r_fb.ghosttexture)
5255 // declare variables
5256 float blur_factor, blur_mouseaccel, blur_velocity;
5257 static float blur_average;
5258 static vec3_t blur_oldangles; // used to see how quickly the mouse is moving
5260 // set a goal for the factoring
5261 blur_velocity = bound(0, (VectorLength(cl.movement_velocity) - r_motionblur_velocityfactor_minspeed.value)
5262 / max(1, r_motionblur_velocityfactor_maxspeed.value - r_motionblur_velocityfactor_minspeed.value), 1);
5263 blur_mouseaccel = bound(0, ((fabs(VectorLength(cl.viewangles) - VectorLength(blur_oldangles)) * 10) - r_motionblur_mousefactor_minspeed.value)
5264 / max(1, r_motionblur_mousefactor_maxspeed.value - r_motionblur_mousefactor_minspeed.value), 1);
5265 blur_factor = ((blur_velocity * r_motionblur_velocityfactor.value)
5266 + (blur_mouseaccel * r_motionblur_mousefactor.value));
5268 // from the goal, pick an averaged value between goal and last value
5269 cl.motionbluralpha = bound(0, (cl.time - cl.oldtime) / max(0.001, r_motionblur_averaging.value), 1);
5270 blur_average = blur_average * (1 - cl.motionbluralpha) + blur_factor * cl.motionbluralpha;
5272 // enforce minimum amount of blur
5273 blur_factor = blur_average * (1 - r_motionblur_minblur.value) + r_motionblur_minblur.value;
5275 //Con_Printf("motionblur: direct factor: %f, averaged factor: %f, velocity: %f, mouse accel: %f \n", blur_factor, blur_average, blur_velocity, blur_mouseaccel);
5277 // calculate values into a standard alpha
5278 cl.motionbluralpha = 1 - exp(-
5280 (r_motionblur.value * blur_factor / 80)
5282 (r_damageblur.value * (cl.cshifts[CSHIFT_DAMAGE].percent / 1600))
5285 max(0.0001, cl.time - cl.oldtime) // fps independent
5288 // randomization for the blur value to combat persistent ghosting
5289 cl.motionbluralpha *= lhrandom(1 - r_motionblur_randomize.value, 1 + r_motionblur_randomize.value);
5290 cl.motionbluralpha = bound(0, cl.motionbluralpha, r_motionblur_maxblur.value);
5293 R_ResetViewRendering2D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5294 if (cl.motionbluralpha > 0 && !r_refdef.envmap && r_fb.ghosttexture_valid)
5296 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5297 GL_Color(1, 1, 1, cl.motionbluralpha);
5298 R_CalcTexCoordsForView(0, 0, viewwidth, viewheight, viewwidth, viewheight, r_fb.ghosttexcoord2f);
5299 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.ghosttexcoord2f);
5300 R_SetupShader_Generic(r_fb.ghosttexture, false, true, true);
5301 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5302 r_refdef.stats[r_stat_bloom_drawpixels] += viewwidth * viewheight;
5305 // updates old view angles for next pass
5306 VectorCopy(cl.viewangles, blur_oldangles);
5308 // copy view into the ghost texture
5309 R_Mesh_CopyToTexture(r_fb.ghosttexture, 0, 0, viewx, viewy, viewwidth, viewheight);
5310 r_refdef.stats[r_stat_bloom_copypixels] += viewwidth * viewheight;
5311 r_fb.ghosttexture_valid = true;
5314 if (r_fb.bloomwidth)
5316 // make the bloom texture
5317 R_Bloom_MakeTexture();
5320 #if _MSC_VER >= 1400
5321 #define sscanf sscanf_s
5323 memset(uservecs, 0, sizeof(uservecs));
5324 if (r_glsl_postprocess_uservec1_enable.integer)
5325 sscanf(r_glsl_postprocess_uservec1.string, "%f %f %f %f", &uservecs[0][0], &uservecs[0][1], &uservecs[0][2], &uservecs[0][3]);
5326 if (r_glsl_postprocess_uservec2_enable.integer)
5327 sscanf(r_glsl_postprocess_uservec2.string, "%f %f %f %f", &uservecs[1][0], &uservecs[1][1], &uservecs[1][2], &uservecs[1][3]);
5328 if (r_glsl_postprocess_uservec3_enable.integer)
5329 sscanf(r_glsl_postprocess_uservec3.string, "%f %f %f %f", &uservecs[2][0], &uservecs[2][1], &uservecs[2][2], &uservecs[2][3]);
5330 if (r_glsl_postprocess_uservec4_enable.integer)
5331 sscanf(r_glsl_postprocess_uservec4.string, "%f %f %f %f", &uservecs[3][0], &uservecs[3][1], &uservecs[3][2], &uservecs[3][3]);
5333 // render to the screen fbo
5334 R_ResetViewRendering2D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5335 GL_Color(1, 1, 1, 1);
5336 GL_BlendFunc(GL_ONE, GL_ZERO);
5338 viewtexture = r_fb.rt_screen->colortexture[0];
5339 bloomtexture = r_fb.rt_bloom ? r_fb.rt_bloom->colortexture[0] : NULL;
5341 if (r_rendertarget_debug.integer >= 0)
5343 r_rendertarget_t *rt = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, r_rendertarget_debug.integer);
5344 if (rt && rt->colortexture[0])
5346 viewtexture = rt->colortexture[0];
5347 bloomtexture = NULL;
5351 R_Mesh_PrepareVertices_Mesh_Arrays(4, r_screenvertex3f, NULL, NULL, NULL, NULL, r_fb.rt_screen->texcoord2f, bloomtexture ? r_fb.rt_bloom->texcoord2f : NULL);
5352 switch(vid.renderpath)
5354 case RENDERPATH_GL32:
5355 case RENDERPATH_GLES2:
5357 (r_fb.bloomwidth ? SHADERPERMUTATION_BLOOM : 0)
5358 | (r_refdef.viewblend[3] > 0 ? SHADERPERMUTATION_VIEWTINT : 0)
5359 | (!vid_gammatables_trivial ? SHADERPERMUTATION_GAMMARAMPS : 0)
5360 | (r_glsl_postprocess.integer ? SHADERPERMUTATION_POSTPROCESSING : 0)
5361 | ((!R_Stereo_ColorMasking() && r_glsl_saturation.value != 1) ? SHADERPERMUTATION_SATURATION : 0);
5362 R_SetupShader_SetPermutationGLSL(SHADERMODE_POSTPROCESS, permutation);
5363 if (r_glsl_permutation->tex_Texture_First >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , viewtexture);
5364 if (r_glsl_permutation->tex_Texture_Second >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second , bloomtexture);
5365 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps );
5366 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]);
5367 if (r_glsl_permutation->loc_PixelSize >= 0) qglUniform2f(r_glsl_permutation->loc_PixelSize , 1.0/r_fb.screentexturewidth, 1.0/r_fb.screentextureheight);
5368 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]);
5369 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]);
5370 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]);
5371 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]);
5372 if (r_glsl_permutation->loc_Saturation >= 0) qglUniform1f(r_glsl_permutation->loc_Saturation , r_glsl_saturation.value);
5373 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
5374 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);
5375 if (r_glsl_permutation->loc_ColorFringe >= 0) qglUniform1f(r_glsl_permutation->loc_ColorFringe, r_colorfringe.value );
5378 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5379 r_refdef.stats[r_stat_bloom_drawpixels] += r_refdef.view.width * r_refdef.view.height;
5382 matrix4x4_t r_waterscrollmatrix;
5384 void R_UpdateFog(void)
5387 if (gamemode == GAME_NEHAHRA)
5389 if (gl_fogenable.integer)
5391 r_refdef.oldgl_fogenable = true;
5392 r_refdef.fog_density = gl_fogdensity.value;
5393 r_refdef.fog_red = gl_fogred.value;
5394 r_refdef.fog_green = gl_foggreen.value;
5395 r_refdef.fog_blue = gl_fogblue.value;
5396 r_refdef.fog_alpha = 1;
5397 r_refdef.fog_start = 0;
5398 r_refdef.fog_end = gl_skyclip.value;
5399 r_refdef.fog_height = 1<<30;
5400 r_refdef.fog_fadedepth = 128;
5402 else if (r_refdef.oldgl_fogenable)
5404 r_refdef.oldgl_fogenable = false;
5405 r_refdef.fog_density = 0;
5406 r_refdef.fog_red = 0;
5407 r_refdef.fog_green = 0;
5408 r_refdef.fog_blue = 0;
5409 r_refdef.fog_alpha = 0;
5410 r_refdef.fog_start = 0;
5411 r_refdef.fog_end = 0;
5412 r_refdef.fog_height = 1<<30;
5413 r_refdef.fog_fadedepth = 128;
5418 r_refdef.fog_alpha = bound(0, r_refdef.fog_alpha, 1);
5419 r_refdef.fog_start = max(0, r_refdef.fog_start);
5420 r_refdef.fog_end = max(r_refdef.fog_start + 0.01, r_refdef.fog_end);
5422 if (r_refdef.fog_density && r_drawfog.integer)
5424 r_refdef.fogenabled = true;
5425 // this is the point where the fog reaches 0.9986 alpha, which we
5426 // consider a good enough cutoff point for the texture
5427 // (0.9986 * 256 == 255.6)
5428 if (r_fog_exp2.integer)
5429 r_refdef.fogrange = 32 / (r_refdef.fog_density * r_refdef.fog_density) + r_refdef.fog_start;
5431 r_refdef.fogrange = 2048 / r_refdef.fog_density + r_refdef.fog_start;
5432 r_refdef.fogrange = bound(r_refdef.fog_start, r_refdef.fogrange, r_refdef.fog_end);
5433 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
5434 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
5435 if (strcmp(r_refdef.fogheighttexturename, r_refdef.fog_height_texturename))
5436 R_BuildFogHeightTexture();
5437 // fog color was already set
5438 // update the fog texture
5439 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)
5440 R_BuildFogTexture();
5441 r_refdef.fog_height_texcoordscale = 1.0f / max(0.125f, r_refdef.fog_fadedepth);
5442 r_refdef.fog_height_tablescale = r_refdef.fog_height_tablesize * r_refdef.fog_height_texcoordscale;
5445 r_refdef.fogenabled = false;
5448 if (r_refdef.fog_density)
5450 r_refdef.fogcolor[0] = r_refdef.fog_red;
5451 r_refdef.fogcolor[1] = r_refdef.fog_green;
5452 r_refdef.fogcolor[2] = r_refdef.fog_blue;
5454 Vector4Set(r_refdef.fogplane, 0, 0, 1, -r_refdef.fog_height);
5455 r_refdef.fogplaneviewdist = DotProduct(r_refdef.fogplane, r_refdef.view.origin) + r_refdef.fogplane[3];
5456 r_refdef.fogplaneviewabove = r_refdef.fogplaneviewdist >= 0;
5457 r_refdef.fogheightfade = -0.5f/max(0.125f, r_refdef.fog_fadedepth);
5461 VectorCopy(r_refdef.fogcolor, fogvec);
5462 // color.rgb *= ContrastBoost * SceneBrightness;
5463 VectorScale(fogvec, r_refdef.view.colorscale, fogvec);
5464 r_refdef.fogcolor[0] = bound(0.0f, fogvec[0], 1.0f);
5465 r_refdef.fogcolor[1] = bound(0.0f, fogvec[1], 1.0f);
5466 r_refdef.fogcolor[2] = bound(0.0f, fogvec[2], 1.0f);
5471 void R_UpdateVariables(void)
5475 r_refdef.scene.ambientintensity = r_ambient.value * (1.0f / 64.0f);
5477 r_refdef.farclip = r_farclip_base.value;
5478 if (r_refdef.scene.worldmodel)
5479 r_refdef.farclip += r_refdef.scene.worldmodel->radius * r_farclip_world.value * 2;
5480 r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
5482 if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
5483 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
5484 r_refdef.polygonfactor = 0;
5485 r_refdef.polygonoffset = 0;
5487 r_refdef.scene.rtworld = r_shadow_realtime_world.integer != 0;
5488 r_refdef.scene.rtworldshadows = r_shadow_realtime_world_shadows.integer && vid.stencil;
5489 r_refdef.scene.rtdlight = r_shadow_realtime_dlight.integer != 0 && !gl_flashblend.integer && r_dynamic.integer;
5490 r_refdef.scene.rtdlightshadows = r_refdef.scene.rtdlight && r_shadow_realtime_dlight_shadows.integer && vid.stencil;
5491 r_refdef.scene.lightmapintensity = r_refdef.scene.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
5492 if (r_refdef.scene.worldmodel)
5494 r_refdef.scene.lightmapintensity *= r_refdef.scene.worldmodel->lightmapscale;
5496 if (r_showsurfaces.integer)
5498 r_refdef.scene.rtworld = false;
5499 r_refdef.scene.rtworldshadows = false;
5500 r_refdef.scene.rtdlight = false;
5501 r_refdef.scene.rtdlightshadows = false;
5502 r_refdef.scene.lightmapintensity = 0;
5505 r_gpuskeletal = false;
5506 switch(vid.renderpath)
5508 case RENDERPATH_GL32:
5509 r_gpuskeletal = r_glsl_skeletal.integer && !r_showsurfaces.integer;
5510 case RENDERPATH_GLES2:
5511 if(!vid_gammatables_trivial)
5513 if(!r_texture_gammaramps || vid_gammatables_serial != r_texture_gammaramps_serial)
5515 // build GLSL gamma texture
5516 #define RAMPWIDTH 256
5517 unsigned short ramp[RAMPWIDTH * 3];
5518 unsigned char rampbgr[RAMPWIDTH][4];
5521 r_texture_gammaramps_serial = vid_gammatables_serial;
5523 VID_BuildGammaTables(&ramp[0], RAMPWIDTH);
5524 for(i = 0; i < RAMPWIDTH; ++i)
5526 rampbgr[i][0] = (unsigned char) (ramp[i + 2 * RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
5527 rampbgr[i][1] = (unsigned char) (ramp[i + RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
5528 rampbgr[i][2] = (unsigned char) (ramp[i] * 255.0 / 65535.0 + 0.5);
5531 if (r_texture_gammaramps)
5533 R_UpdateTexture(r_texture_gammaramps, &rampbgr[0][0], 0, 0, 0, RAMPWIDTH, 1, 1, 0);
5537 r_texture_gammaramps = R_LoadTexture2D(r_main_texturepool, "gammaramps", RAMPWIDTH, 1, &rampbgr[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
5543 // remove GLSL gamma texture
5549 static r_refdef_scene_type_t r_currentscenetype = RST_CLIENT;
5550 static r_refdef_scene_t r_scenes_store[ RST_COUNT ];
5556 void R_SelectScene( r_refdef_scene_type_t scenetype ) {
5557 if( scenetype != r_currentscenetype ) {
5558 // store the old scenetype
5559 r_scenes_store[ r_currentscenetype ] = r_refdef.scene;
5560 r_currentscenetype = scenetype;
5561 // move in the new scene
5562 r_refdef.scene = r_scenes_store[ r_currentscenetype ];
5571 r_refdef_scene_t * R_GetScenePointer( r_refdef_scene_type_t scenetype )
5573 // of course, we could also add a qbool that provides a lock state and a ReleaseScenePointer function..
5574 if( scenetype == r_currentscenetype ) {
5575 return &r_refdef.scene;
5577 return &r_scenes_store[ scenetype ];
5581 static int R_SortEntities_Compare(const void *ap, const void *bp)
5583 const entity_render_t *a = *(const entity_render_t **)ap;
5584 const entity_render_t *b = *(const entity_render_t **)bp;
5587 if(a->model < b->model)
5589 if(a->model > b->model)
5593 // TODO possibly calculate the REAL skinnum here first using
5595 if(a->skinnum < b->skinnum)
5597 if(a->skinnum > b->skinnum)
5600 // everything we compared is equal
5603 static void R_SortEntities(void)
5605 // below or equal 2 ents, sorting never gains anything
5606 if(r_refdef.scene.numentities <= 2)
5609 qsort(r_refdef.scene.entities, r_refdef.scene.numentities, sizeof(*r_refdef.scene.entities), R_SortEntities_Compare);
5617 extern cvar_t r_shadow_bouncegrid;
5618 extern cvar_t v_isometric;
5619 extern void V_MakeViewIsometric(void);
5620 void R_RenderView(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, int x, int y, int width, int height)
5622 matrix4x4_t originalmatrix = r_refdef.view.matrix, offsetmatrix;
5624 rtexture_t *viewdepthtexture = NULL;
5625 rtexture_t *viewcolortexture = NULL;
5626 int viewx = r_refdef.view.x, viewy = r_refdef.view.y, viewwidth = r_refdef.view.width, viewheight = r_refdef.view.height;
5628 // finish any 2D rendering that was queued
5631 if (r_timereport_active)
5632 R_TimeReport("start");
5633 r_textureframe++; // used only by R_GetCurrentTexture
5634 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
5636 if(R_CompileShader_CheckStaticParms())
5637 R_GLSL_Restart_f(cmd_local);
5639 if (!r_drawentities.integer)
5640 r_refdef.scene.numentities = 0;
5641 else if (r_sortentities.integer)
5644 R_AnimCache_ClearCache();
5646 /* adjust for stereo display */
5647 if(R_Stereo_Active())
5649 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);
5650 Matrix4x4_Concat(&r_refdef.view.matrix, &originalmatrix, &offsetmatrix);
5653 if (r_refdef.view.isoverlay)
5655 // TODO: FIXME: move this into its own backend function maybe? [2/5/2008 Andreas]
5656 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
5657 GL_Clear(GL_DEPTH_BUFFER_BIT, NULL, 1.0f, 0);
5658 R_TimeReport("depthclear");
5660 r_refdef.view.showdebug = false;
5662 r_fb.water.enabled = false;
5663 r_fb.water.numwaterplanes = 0;
5665 R_RenderScene(0, NULL, NULL, r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
5667 r_refdef.view.matrix = originalmatrix;
5673 if (!r_refdef.scene.entities || r_refdef.view.width * r_refdef.view.height == 0 || !r_renderview.integer || cl_videoplaying/* || !r_refdef.scene.worldmodel*/)
5675 r_refdef.view.matrix = originalmatrix;
5679 r_refdef.view.usevieworiginculling = !r_trippy.value && r_refdef.view.useperspective;
5680 if (v_isometric.integer && r_refdef.view.ismain)
5681 V_MakeViewIsometric();
5683 r_refdef.view.colorscale = r_hdr_scenebrightness.value * r_hdr_irisadaptation_value.value;
5685 if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D)
5686 // in sRGB fallback, behave similar to true sRGB: convert this
5687 // value from linear to sRGB
5688 r_refdef.view.colorscale = Image_sRGBFloatFromLinearFloat(r_refdef.view.colorscale);
5690 R_RenderView_UpdateViewVectors();
5692 R_Shadow_UpdateWorldLightSelection();
5694 // this will set up r_fb.rt_screen
5695 R_Bloom_StartFrame();
5697 // apply bloom brightness offset
5699 r_refdef.view.colorscale *= r_bloom_scenebrightness.value;
5701 // R_Bloom_StartFrame probably set up an fbo for us to render into, it will be rendered to the window later in R_BlendView
5704 viewfbo = r_fb.rt_screen->fbo;
5705 viewdepthtexture = r_fb.rt_screen->depthtexture;
5706 viewcolortexture = r_fb.rt_screen->colortexture[0];
5709 viewwidth = r_fb.rt_screen->texturewidth;
5710 viewheight = r_fb.rt_screen->textureheight;
5713 R_Water_StartFrame(viewwidth, viewheight);
5716 if (r_timereport_active)
5717 R_TimeReport("viewsetup");
5719 R_ResetViewRendering3D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5721 // clear the whole fbo every frame - otherwise the driver will consider
5722 // it to be an inter-frame texture and stall in multi-gpu configurations
5724 GL_ScissorTest(false);
5725 R_ClearScreen(r_refdef.fogenabled);
5726 if (r_timereport_active)
5727 R_TimeReport("viewclear");
5729 r_refdef.view.clear = true;
5731 r_refdef.view.showdebug = true;
5734 if (r_timereport_active)
5735 R_TimeReport("visibility");
5737 R_AnimCache_CacheVisibleEntities();
5738 if (r_timereport_active)
5739 R_TimeReport("animcache");
5741 R_Shadow_UpdateBounceGridTexture();
5742 // R_Shadow_UpdateBounceGridTexture called R_TimeReport a few times internally, so we don't need to do that here.
5744 r_fb.water.numwaterplanes = 0;
5745 if (r_fb.water.enabled)
5746 R_RenderWaterPlanes(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5748 // for the actual view render we use scissoring a fair amount, so scissor
5749 // test needs to be on
5751 GL_ScissorTest(true);
5752 GL_Scissor(viewx, viewy, viewwidth, viewheight);
5753 R_RenderScene(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5754 r_fb.water.numwaterplanes = 0;
5756 // postprocess uses textures that are not aligned with the viewport we're rendering, so no scissoring
5757 GL_ScissorTest(false);
5759 R_BlendView(fbo, depthtexture, colortexture, x, y, width, height);
5760 if (r_timereport_active)
5761 R_TimeReport("blendview");
5763 r_refdef.view.matrix = originalmatrix;
5767 // go back to 2d rendering
5771 void R_RenderWaterPlanes(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5773 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawAddWaterPlanes)
5775 r_refdef.scene.worldmodel->DrawAddWaterPlanes(r_refdef.scene.worldentity);
5776 if (r_timereport_active)
5777 R_TimeReport("waterworld");
5780 // don't let sound skip if going slow
5781 if (r_refdef.scene.extraupdate)
5784 R_DrawModelsAddWaterPlanes();
5785 if (r_timereport_active)
5786 R_TimeReport("watermodels");
5788 if (r_fb.water.numwaterplanes)
5790 R_Water_ProcessPlanes(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5791 if (r_timereport_active)
5792 R_TimeReport("waterscenes");
5796 extern cvar_t cl_locs_show;
5797 static void R_DrawLocs(void);
5798 static void R_DrawEntityBBoxes(prvm_prog_t *prog);
5799 static void R_DrawModelDecals(void);
5800 extern qbool r_shadow_usingdeferredprepass;
5801 extern int r_shadow_shadowmapatlas_modelshadows_size;
5802 void R_RenderScene(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5804 qbool shadowmapping = false;
5806 if (r_timereport_active)
5807 R_TimeReport("beginscene");
5809 r_refdef.stats[r_stat_renders]++;
5813 // don't let sound skip if going slow
5814 if (r_refdef.scene.extraupdate)
5817 R_MeshQueue_BeginScene();
5821 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);
5823 if (r_timereport_active)
5824 R_TimeReport("skystartframe");
5826 if (cl.csqc_vidvars.drawworld)
5828 // don't let sound skip if going slow
5829 if (r_refdef.scene.extraupdate)
5832 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawSky)
5834 r_refdef.scene.worldmodel->DrawSky(r_refdef.scene.worldentity);
5835 if (r_timereport_active)
5836 R_TimeReport("worldsky");
5839 if (R_DrawBrushModelsSky() && r_timereport_active)
5840 R_TimeReport("bmodelsky");
5842 if (skyrendermasked && skyrenderlater)
5844 // we have to force off the water clipping plane while rendering sky
5845 R_SetupView(false, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5847 R_SetupView(true, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5848 if (r_timereport_active)
5849 R_TimeReport("sky");
5853 // save the framebuffer info for R_Shadow_RenderMode_Reset during this view render
5854 r_shadow_viewfbo = viewfbo;
5855 r_shadow_viewdepthtexture = viewdepthtexture;
5856 r_shadow_viewcolortexture = viewcolortexture;
5857 r_shadow_viewx = viewx;
5858 r_shadow_viewy = viewy;
5859 r_shadow_viewwidth = viewwidth;
5860 r_shadow_viewheight = viewheight;
5862 R_Shadow_PrepareModelShadows();
5863 R_Shadow_PrepareLights();
5864 if (r_timereport_active)
5865 R_TimeReport("preparelights");
5867 // render all the shadowmaps that will be used for this view
5868 shadowmapping = R_Shadow_ShadowMappingEnabled();
5869 if (shadowmapping || r_shadow_shadowmapatlas_modelshadows_size)
5871 R_Shadow_DrawShadowMaps();
5872 if (r_timereport_active)
5873 R_TimeReport("shadowmaps");
5876 // render prepass deferred lighting if r_shadow_deferred is on, this produces light buffers that will be sampled in forward pass
5877 if (r_shadow_usingdeferredprepass)
5878 R_Shadow_DrawPrepass();
5880 // now we begin the forward pass of the view render
5881 if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawDepth)
5883 r_refdef.scene.worldmodel->DrawDepth(r_refdef.scene.worldentity);
5884 if (r_timereport_active)
5885 R_TimeReport("worlddepth");
5887 if (r_depthfirst.integer >= 2)
5889 R_DrawModelsDepth();
5890 if (r_timereport_active)
5891 R_TimeReport("modeldepth");
5894 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->Draw)
5896 r_refdef.scene.worldmodel->Draw(r_refdef.scene.worldentity);
5897 if (r_timereport_active)
5898 R_TimeReport("world");
5901 // don't let sound skip if going slow
5902 if (r_refdef.scene.extraupdate)
5906 if (r_timereport_active)
5907 R_TimeReport("models");
5909 // don't let sound skip if going slow
5910 if (r_refdef.scene.extraupdate)
5913 if (!r_shadow_usingdeferredprepass)
5915 R_Shadow_DrawLights();
5916 if (r_timereport_active)
5917 R_TimeReport("rtlights");
5920 // don't let sound skip if going slow
5921 if (r_refdef.scene.extraupdate)
5924 if (cl.csqc_vidvars.drawworld)
5926 R_DrawModelDecals();
5927 if (r_timereport_active)
5928 R_TimeReport("modeldecals");
5931 if (r_timereport_active)
5932 R_TimeReport("particles");
5935 if (r_timereport_active)
5936 R_TimeReport("explosions");
5939 if (r_refdef.view.showdebug)
5941 if (cl_locs_show.integer)
5944 if (r_timereport_active)
5945 R_TimeReport("showlocs");
5948 if (r_drawportals.integer)
5951 if (r_timereport_active)
5952 R_TimeReport("portals");
5955 if (r_showbboxes_client.value > 0)
5957 R_DrawEntityBBoxes(CLVM_prog);
5958 if (r_timereport_active)
5959 R_TimeReport("clbboxes");
5961 if (r_showbboxes.value > 0)
5963 R_DrawEntityBBoxes(SVVM_prog);
5964 if (r_timereport_active)
5965 R_TimeReport("svbboxes");
5969 if (r_transparent.integer)
5971 R_MeshQueue_RenderTransparent();
5972 if (r_timereport_active)
5973 R_TimeReport("drawtrans");
5976 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))
5978 r_refdef.scene.worldmodel->DrawDebug(r_refdef.scene.worldentity);
5979 if (r_timereport_active)
5980 R_TimeReport("worlddebug");
5981 R_DrawModelsDebug();
5982 if (r_timereport_active)
5983 R_TimeReport("modeldebug");
5986 if (cl.csqc_vidvars.drawworld)
5988 R_Shadow_DrawCoronas();
5989 if (r_timereport_active)
5990 R_TimeReport("coronas");
5993 // don't let sound skip if going slow
5994 if (r_refdef.scene.extraupdate)
5998 static const unsigned short bboxelements[36] =
6008 #define BBOXEDGES 13
6009 static const float bboxedges[BBOXEDGES][6] =
6012 { 0, 0, 0, 1, 1, 1 },
6014 { 0, 0, 0, 0, 1, 0 },
6015 { 0, 0, 0, 1, 0, 0 },
6016 { 0, 1, 0, 1, 1, 0 },
6017 { 1, 0, 0, 1, 1, 0 },
6019 { 0, 0, 1, 0, 1, 1 },
6020 { 0, 0, 1, 1, 0, 1 },
6021 { 0, 1, 1, 1, 1, 1 },
6022 { 1, 0, 1, 1, 1, 1 },
6024 { 0, 0, 0, 0, 0, 1 },
6025 { 1, 0, 0, 1, 0, 1 },
6026 { 0, 1, 0, 0, 1, 1 },
6027 { 1, 1, 0, 1, 1, 1 },
6030 static void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
6032 int numvertices = BBOXEDGES * 8;
6033 float vertex3f[BBOXEDGES * 8 * 3], color4f[BBOXEDGES * 8 * 4];
6034 int numtriangles = BBOXEDGES * 12;
6035 unsigned short elements[BBOXEDGES * 36];
6037 float *v, *c, f1, f2, edgemins[3], edgemaxs[3];
6039 RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false);
6041 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6042 GL_DepthMask(false);
6043 GL_DepthRange(0, 1);
6044 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6046 for (edge = 0; edge < BBOXEDGES; edge++)
6048 for (i = 0; i < 3; i++)
6050 edgemins[i] = mins[i] + (maxs[i] - mins[i]) * bboxedges[edge][i] - 0.25f;
6051 edgemaxs[i] = mins[i] + (maxs[i] - mins[i]) * bboxedges[edge][3 + i] + 0.25f;
6053 vertex3f[edge * 24 + 0] = edgemins[0]; vertex3f[edge * 24 + 1] = edgemins[1]; vertex3f[edge * 24 + 2] = edgemins[2];
6054 vertex3f[edge * 24 + 3] = edgemaxs[0]; vertex3f[edge * 24 + 4] = edgemins[1]; vertex3f[edge * 24 + 5] = edgemins[2];
6055 vertex3f[edge * 24 + 6] = edgemins[0]; vertex3f[edge * 24 + 7] = edgemaxs[1]; vertex3f[edge * 24 + 8] = edgemins[2];
6056 vertex3f[edge * 24 + 9] = edgemaxs[0]; vertex3f[edge * 24 + 10] = edgemaxs[1]; vertex3f[edge * 24 + 11] = edgemins[2];
6057 vertex3f[edge * 24 + 12] = edgemins[0]; vertex3f[edge * 24 + 13] = edgemins[1]; vertex3f[edge * 24 + 14] = edgemaxs[2];
6058 vertex3f[edge * 24 + 15] = edgemaxs[0]; vertex3f[edge * 24 + 16] = edgemins[1]; vertex3f[edge * 24 + 17] = edgemaxs[2];
6059 vertex3f[edge * 24 + 18] = edgemins[0]; vertex3f[edge * 24 + 19] = edgemaxs[1]; vertex3f[edge * 24 + 20] = edgemaxs[2];
6060 vertex3f[edge * 24 + 21] = edgemaxs[0]; vertex3f[edge * 24 + 22] = edgemaxs[1]; vertex3f[edge * 24 + 23] = edgemaxs[2];
6061 for (i = 0; i < 36; i++)
6062 elements[edge * 36 + i] = edge * 8 + bboxelements[i];
6064 R_FillColors(color4f, numvertices, cr, cg, cb, ca);
6065 if (r_refdef.fogenabled)
6067 for (i = 0, v = vertex3f, c = color4f; i < numvertices; i++, v += 3, c += 4)
6069 f1 = RSurf_FogVertex(v);
6071 c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
6072 c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
6073 c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
6076 R_Mesh_PrepareVertices_Generic_Arrays(numvertices, vertex3f, color4f, NULL);
6077 R_Mesh_ResetTextureState();
6078 R_SetupShader_Generic_NoTexture(false, false);
6079 R_Mesh_Draw(0, numvertices, 0, numtriangles, NULL, NULL, 0, elements, NULL, 0);
6082 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6084 // hacky overloading of the parameters
6085 prvm_prog_t *prog = (prvm_prog_t *)rtlight;
6088 prvm_edict_t *edict;
6090 GL_CullFace(GL_NONE);
6091 R_SetupShader_Generic_NoTexture(false, false);
6093 for (i = 0;i < numsurfaces;i++)
6095 edict = PRVM_EDICT_NUM(surfacelist[i]);
6096 switch ((int)PRVM_serveredictfloat(edict, solid))
6098 case SOLID_NOT: Vector4Set(color, 1, 1, 1, 0.05);break;
6099 case SOLID_TRIGGER: Vector4Set(color, 1, 0, 1, 0.10);break;
6100 case SOLID_BBOX: Vector4Set(color, 0, 1, 0, 0.10);break;
6101 case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
6102 case SOLID_BSP: Vector4Set(color, 0, 0, 1, 0.05);break;
6103 case SOLID_CORPSE: Vector4Set(color, 1, 0.5, 0, 0.05);break;
6104 default: Vector4Set(color, 0, 0, 0, 0.50);break;
6106 if (prog == CLVM_prog)
6107 color[3] *= r_showbboxes_client.value;
6109 color[3] *= r_showbboxes.value;
6110 color[3] = bound(0, color[3], 1);
6111 GL_DepthTest(!r_showdisabledepthtest.integer);
6112 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
6116 static void R_DrawEntityBBoxes(prvm_prog_t *prog)
6119 prvm_edict_t *edict;
6125 for (i = 0; i < prog->num_edicts; i++)
6127 edict = PRVM_EDICT_NUM(i);
6130 // exclude the following for now, as they don't live in world coordinate space and can't be solid:
6131 if (PRVM_gameedictedict(edict, tag_entity) != 0)
6133 if (prog == SVVM_prog && PRVM_serveredictedict(edict, viewmodelforclient) != 0)
6135 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
6136 R_MeshQueue_AddTransparent(TRANSPARENTSORT_DISTANCE, center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)prog);
6140 static const int nomodelelement3i[24] =
6152 static const unsigned short nomodelelement3s[24] =
6164 static const float nomodelvertex3f[6*3] =
6174 static const float nomodelcolor4f[6*4] =
6176 0.0f, 0.0f, 0.5f, 1.0f,
6177 0.0f, 0.0f, 0.5f, 1.0f,
6178 0.0f, 0.5f, 0.0f, 1.0f,
6179 0.0f, 0.5f, 0.0f, 1.0f,
6180 0.5f, 0.0f, 0.0f, 1.0f,
6181 0.5f, 0.0f, 0.0f, 1.0f
6184 static void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6190 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);
6192 // this is only called once per entity so numsurfaces is always 1, and
6193 // surfacelist is always {0}, so this code does not handle batches
6195 if (rsurface.ent_flags & RENDER_ADDITIVE)
6197 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
6198 GL_DepthMask(false);
6200 else if (ent->alpha < 1)
6202 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6203 GL_DepthMask(false);
6207 GL_BlendFunc(GL_ONE, GL_ZERO);
6210 GL_DepthRange(0, (rsurface.ent_flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
6211 GL_PolygonOffset(rsurface.basepolygonfactor, rsurface.basepolygonoffset);
6212 GL_DepthTest(!(rsurface.ent_flags & RENDER_NODEPTHTEST));
6213 GL_CullFace((rsurface.ent_flags & RENDER_DOUBLESIDED) ? GL_NONE : r_refdef.view.cullface_back);
6214 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
6215 for (i = 0, c = color4f;i < 6;i++, c += 4)
6217 c[0] *= ent->render_fullbright[0] * r_refdef.view.colorscale;
6218 c[1] *= ent->render_fullbright[1] * r_refdef.view.colorscale;
6219 c[2] *= ent->render_fullbright[2] * r_refdef.view.colorscale;
6222 if (r_refdef.fogenabled)
6224 for (i = 0, c = color4f;i < 6;i++, c += 4)
6226 f1 = RSurf_FogVertex(nomodelvertex3f + 3*i);
6228 c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
6229 c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
6230 c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
6233 // R_Mesh_ResetTextureState();
6234 R_SetupShader_Generic_NoTexture(false, false);
6235 R_Mesh_PrepareVertices_Generic_Arrays(6, nomodelvertex3f, color4f, NULL);
6236 R_Mesh_Draw(0, 6, 0, 8, nomodelelement3i, NULL, 0, nomodelelement3s, NULL, 0);
6239 void R_DrawNoModel(entity_render_t *ent)
6242 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
6243 if ((ent->flags & RENDER_ADDITIVE) || (ent->alpha < 1))
6244 R_MeshQueue_AddTransparent((ent->flags & RENDER_NODEPTHTEST) ? TRANSPARENTSORT_HUD : TRANSPARENTSORT_DISTANCE, org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
6246 R_DrawNoModel_TransparentCallback(ent, rsurface.rtlight, 0, NULL);
6249 void R_CalcBeam_Vertex3f (float *vert, const float *org1, const float *org2, float width)
6251 vec3_t right1, right2, diff, normal;
6253 VectorSubtract (org2, org1, normal);
6255 // calculate 'right' vector for start
6256 VectorSubtract (r_refdef.view.origin, org1, diff);
6257 CrossProduct (normal, diff, right1);
6258 VectorNormalize (right1);
6260 // calculate 'right' vector for end
6261 VectorSubtract (r_refdef.view.origin, org2, diff);
6262 CrossProduct (normal, diff, right2);
6263 VectorNormalize (right2);
6265 vert[ 0] = org1[0] + width * right1[0];
6266 vert[ 1] = org1[1] + width * right1[1];
6267 vert[ 2] = org1[2] + width * right1[2];
6268 vert[ 3] = org1[0] - width * right1[0];
6269 vert[ 4] = org1[1] - width * right1[1];
6270 vert[ 5] = org1[2] - width * right1[2];
6271 vert[ 6] = org2[0] - width * right2[0];
6272 vert[ 7] = org2[1] - width * right2[1];
6273 vert[ 8] = org2[2] - width * right2[2];
6274 vert[ 9] = org2[0] + width * right2[0];
6275 vert[10] = org2[1] + width * right2[1];
6276 vert[11] = org2[2] + width * right2[2];
6279 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)
6281 vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
6282 vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
6283 vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
6284 vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
6285 vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
6286 vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
6287 vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
6288 vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
6289 vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
6290 vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
6291 vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
6292 vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
6295 static int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
6300 VectorSet(v, x, y, z);
6301 for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
6302 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
6304 if (i == mesh->numvertices)
6306 if (mesh->numvertices < mesh->maxvertices)
6308 VectorCopy(v, vertex3f);
6309 mesh->numvertices++;
6311 return mesh->numvertices;
6317 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
6321 element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
6322 element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
6323 e = mesh->element3i + mesh->numtriangles * 3;
6324 for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
6326 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
6327 if (mesh->numtriangles < mesh->maxtriangles)
6332 mesh->numtriangles++;
6334 element[1] = element[2];
6338 static void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
6342 element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
6343 element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
6344 e = mesh->element3i + mesh->numtriangles * 3;
6345 for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
6347 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
6348 if (mesh->numtriangles < mesh->maxtriangles)
6353 mesh->numtriangles++;
6355 element[1] = element[2];
6359 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
6360 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
6362 int planenum, planenum2;
6365 mplane_t *plane, *plane2;
6367 double temppoints[2][256*3];
6368 // figure out how large a bounding box we need to properly compute this brush
6370 for (w = 0;w < numplanes;w++)
6371 maxdist = max(maxdist, fabs(planes[w].dist));
6372 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
6373 maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
6374 for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
6378 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
6379 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
6381 if (planenum2 == planenum)
6383 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);
6386 if (tempnumpoints < 3)
6388 // generate elements forming a triangle fan for this polygon
6389 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
6393 static qbool R_TestQ3WaveFunc(q3wavefunc_t func, const float *parms)
6395 if(parms[0] == 0 && parms[1] == 0)
6397 if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
6398 if(rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)] == 0)
6403 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
6406 index = parms[2] + rsurface.shadertime * parms[3];
6407 index -= floor(index);
6408 switch (func & ((1 << Q3WAVEFUNC_USER_SHIFT) - 1))
6411 case Q3WAVEFUNC_NONE:
6412 case Q3WAVEFUNC_NOISE:
6413 case Q3WAVEFUNC_COUNT:
6416 case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
6417 case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
6418 case Q3WAVEFUNC_SAWTOOTH: f = index;break;
6419 case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
6420 case Q3WAVEFUNC_TRIANGLE:
6422 f = index - floor(index);
6435 f = parms[0] + parms[1] * f;
6436 if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
6437 f *= rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)];
6441 static void R_tcMod_ApplyToMatrix(matrix4x4_t *texmatrix, q3shaderinfo_layer_tcmod_t *tcmod, int currentmaterialflags)
6448 matrix4x4_t matrix, temp;
6449 // if shadertime exceeds about 9 hours (32768 seconds), just wrap it,
6450 // it's better to have one huge fixup every 9 hours than gradual
6451 // degradation over time which looks consistently bad after many hours.
6453 // tcmod scroll in particular suffers from this degradation which can't be
6454 // effectively worked around even with floor() tricks because we don't
6455 // know if tcmod scroll is the last tcmod being applied, and for clampmap
6456 // a workaround involving floor() would be incorrect anyway...
6457 shadertime = rsurface.shadertime;
6458 if (shadertime >= 32768.0f)
6459 shadertime -= floor(rsurface.shadertime * (1.0f / 32768.0f)) * 32768.0f;
6460 switch(tcmod->tcmod)
6464 if (currentmaterialflags & MATERIALFLAG_WATERSCROLL)
6465 matrix = r_waterscrollmatrix;
6467 matrix = identitymatrix;
6469 case Q3TCMOD_ENTITYTRANSLATE:
6470 // this is used in Q3 to allow the gamecode to control texcoord
6471 // scrolling on the entity, which is not supported in darkplaces yet.
6472 Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
6474 case Q3TCMOD_ROTATE:
6475 Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
6476 Matrix4x4_ConcatRotate(&matrix, tcmod->parms[0] * rsurface.shadertime, 0, 0, 1);
6477 Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
6480 Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
6482 case Q3TCMOD_SCROLL:
6483 // this particular tcmod is a "bug for bug" compatible one with regards to
6484 // Quake3, the wrapping is unnecessary with our shadetime fix but quake3
6485 // specifically did the wrapping and so we must mimic that...
6486 offsetd[0] = tcmod->parms[0] * rsurface.shadertime;
6487 offsetd[1] = tcmod->parms[1] * rsurface.shadertime;
6488 Matrix4x4_CreateTranslate(&matrix, offsetd[0] - floor(offsetd[0]), offsetd[1] - floor(offsetd[1]), 0);
6490 case Q3TCMOD_PAGE: // poor man's animmap (to store animations into a single file, useful for HTTP downloaded textures)
6491 w = (int) tcmod->parms[0];
6492 h = (int) tcmod->parms[1];
6493 f = rsurface.shadertime / (tcmod->parms[2] * w * h);
6495 idx = (int) floor(f * w * h);
6496 Matrix4x4_CreateTranslate(&matrix, (idx % w) / tcmod->parms[0], (idx / w) / tcmod->parms[1], 0);
6498 case Q3TCMOD_STRETCH:
6499 f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
6500 Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
6502 case Q3TCMOD_TRANSFORM:
6503 VectorSet(tcmat + 0, tcmod->parms[0], tcmod->parms[1], 0);
6504 VectorSet(tcmat + 3, tcmod->parms[2], tcmod->parms[3], 0);
6505 VectorSet(tcmat + 6, 0 , 0 , 1);
6506 VectorSet(tcmat + 9, tcmod->parms[4], tcmod->parms[5], 0);
6507 Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
6509 case Q3TCMOD_TURBULENT:
6510 // this is handled in the RSurf_PrepareVertices function
6511 matrix = identitymatrix;
6515 Matrix4x4_Concat(texmatrix, &matrix, &temp);
6518 static void R_LoadQWSkin(r_qwskincache_t *cache, const char *skinname)
6520 int textureflags = (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_PICMIP;
6521 char name[MAX_QPATH];
6522 skinframe_t *skinframe;
6523 unsigned char pixels[296*194];
6524 strlcpy(cache->name, skinname, sizeof(cache->name));
6525 dpsnprintf(name, sizeof(name), "skins/%s.pcx", cache->name);
6526 if (developer_loading.integer)
6527 Con_Printf("loading %s\n", name);
6528 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
6529 if (!skinframe || !skinframe->base)
6532 fs_offset_t filesize;
6534 f = FS_LoadFile(name, tempmempool, true, &filesize);
6537 if (LoadPCX_QWSkin(f, (int)filesize, pixels, 296, 194))
6538 skinframe = R_SkinFrame_LoadInternalQuake(name, textureflags, true, r_fullbrights.integer, pixels, image_width, image_height);
6542 cache->skinframe = skinframe;
6545 texture_t *R_GetCurrentTexture(texture_t *t)
6548 const entity_render_t *ent = rsurface.entity;
6549 model_t *model = ent->model; // when calling this, ent must not be NULL
6550 q3shaderinfo_layer_tcmod_t *tcmod;
6551 float specularscale = 0.0f;
6553 if (t->update_lastrenderframe == r_textureframe && t->update_lastrenderentity == (void *)ent && !rsurface.forcecurrenttextureupdate)
6554 return t->currentframe;
6555 t->update_lastrenderframe = r_textureframe;
6556 t->update_lastrenderentity = (void *)ent;
6558 if(ent->entitynumber >= MAX_EDICTS && ent->entitynumber < 2 * MAX_EDICTS)
6559 t->camera_entity = ent->entitynumber;
6561 t->camera_entity = 0;
6563 // switch to an alternate material if this is a q1bsp animated material
6565 texture_t *texture = t;
6566 int s = rsurface.ent_skinnum;
6567 if ((unsigned int)s >= (unsigned int)model->numskins)
6569 if (model->skinscenes)
6571 if (model->skinscenes[s].framecount > 1)
6572 s = model->skinscenes[s].firstframe + (unsigned int) (rsurface.shadertime * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
6574 s = model->skinscenes[s].firstframe;
6577 t = t + s * model->num_surfaces;
6580 // use an alternate animation if the entity's frame is not 0,
6581 // and only if the texture has an alternate animation
6582 if (t->animated == 2) // q2bsp
6583 t = t->anim_frames[0][ent->framegroupblend[0].frame % t->anim_total[0]];
6584 else if (rsurface.ent_alttextures && t->anim_total[1])
6585 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[1]) : 0];
6587 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[0]) : 0];
6589 texture->currentframe = t;
6592 // update currentskinframe to be a qw skin or animation frame
6593 if (rsurface.ent_qwskin >= 0)
6595 i = rsurface.ent_qwskin;
6596 if (!r_qwskincache || r_qwskincache_size != cl.maxclients)
6598 r_qwskincache_size = cl.maxclients;
6600 Mem_Free(r_qwskincache);
6601 r_qwskincache = (r_qwskincache_t *)Mem_Alloc(r_main_mempool, sizeof(*r_qwskincache) * r_qwskincache_size);
6603 if (strcmp(r_qwskincache[i].name, cl.scores[i].qw_skin))
6604 R_LoadQWSkin(&r_qwskincache[i], cl.scores[i].qw_skin);
6605 t->currentskinframe = r_qwskincache[i].skinframe;
6606 if (t->materialshaderpass && t->currentskinframe == NULL)
6607 t->currentskinframe = t->materialshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->materialshaderpass->framerate, t->materialshaderpass->numframes)];
6609 else if (t->materialshaderpass && t->materialshaderpass->numframes >= 2)
6610 t->currentskinframe = t->materialshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->materialshaderpass->framerate, t->materialshaderpass->numframes)];
6611 if (t->backgroundshaderpass && t->backgroundshaderpass->numframes >= 2)
6612 t->backgroundcurrentskinframe = t->backgroundshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->backgroundshaderpass->framerate, t->backgroundshaderpass->numframes)];
6614 t->currentmaterialflags = t->basematerialflags;
6615 t->currentalpha = rsurface.entity->alpha * t->basealpha;
6616 if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_water.integer || r_novis.integer || r_trippy.integer))
6617 t->currentalpha *= r_wateralpha.value;
6618 if(t->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay)
6619 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; // we apply wateralpha later
6620 if(!r_fb.water.enabled || r_refdef.view.isoverlay)
6621 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA);
6623 // decide on which type of lighting to use for this surface
6624 if (rsurface.entity->render_modellight_forced)
6625 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
6626 if (rsurface.entity->render_rtlight_disabled)
6627 t->currentmaterialflags |= MATERIALFLAG_NORTLIGHT;
6628 if (rsurface.entity->render_lightgrid)
6629 t->currentmaterialflags |= MATERIALFLAG_LIGHTGRID;
6630 if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND && !(R_BlendFuncFlags(t->customblendfunc[0], t->customblendfunc[1]) & BLENDFUNC_ALLOWS_COLORMOD))
6632 // some CUSTOMBLEND blendfuncs are too weird, we have to ignore colormod and view colorscale
6633 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_NORTLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6634 for (q = 0; q < 3; q++)
6636 t->render_glowmod[q] = rsurface.entity->glowmod[q];
6637 t->render_modellight_lightdir_world[q] = q == 2;
6638 t->render_modellight_lightdir_local[q] = q == 2;
6639 t->render_modellight_ambient[q] = 1;
6640 t->render_modellight_diffuse[q] = 0;
6641 t->render_modellight_specular[q] = 0;
6642 t->render_lightmap_ambient[q] = 0;
6643 t->render_lightmap_diffuse[q] = 0;
6644 t->render_lightmap_specular[q] = 0;
6645 t->render_rtlight_diffuse[q] = 0;
6646 t->render_rtlight_specular[q] = 0;
6649 else if ((t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) || !(rsurface.ent_flags & RENDER_LIGHT))
6651 // fullbright is basically MATERIALFLAG_MODELLIGHT but with ambient locked to 1,1,1 and no shading
6652 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_NORTLIGHT | MATERIALFLAG_MODELLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6653 for (q = 0; q < 3; q++)
6655 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6656 t->render_modellight_ambient[q] = rsurface.entity->render_fullbright[q] * r_refdef.view.colorscale;
6657 t->render_modellight_lightdir_world[q] = q == 2;
6658 t->render_modellight_lightdir_local[q] = q == 2;
6659 t->render_modellight_diffuse[q] = 0;
6660 t->render_modellight_specular[q] = 0;
6661 t->render_lightmap_ambient[q] = 0;
6662 t->render_lightmap_diffuse[q] = 0;
6663 t->render_lightmap_specular[q] = 0;
6664 t->render_rtlight_diffuse[q] = 0;
6665 t->render_rtlight_specular[q] = 0;
6668 else if (t->currentmaterialflags & MATERIALFLAG_LIGHTGRID)
6670 t->currentmaterialflags &= ~MATERIALFLAG_MODELLIGHT;
6671 for (q = 0; q < 3; q++)
6673 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6674 t->render_modellight_lightdir_world[q] = q == 2;
6675 t->render_modellight_lightdir_local[q] = q == 2;
6676 t->render_modellight_ambient[q] = 0;
6677 t->render_modellight_diffuse[q] = 0;
6678 t->render_modellight_specular[q] = 0;
6679 t->render_lightmap_ambient[q] = rsurface.entity->render_lightmap_ambient[q] * r_refdef.view.colorscale;
6680 t->render_lightmap_diffuse[q] = rsurface.entity->render_lightmap_diffuse[q] * 2 * r_refdef.view.colorscale;
6681 t->render_lightmap_specular[q] = rsurface.entity->render_lightmap_specular[q] * 2 * r_refdef.view.colorscale;
6682 t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6683 t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6686 else if ((rsurface.ent_flags & (RENDER_DYNAMICMODELLIGHT | RENDER_CUSTOMIZEDMODELLIGHT)) || rsurface.modeltexcoordlightmap2f == NULL)
6688 // ambient + single direction light (modellight)
6689 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_MODELLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6690 for (q = 0; q < 3; q++)
6692 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6693 t->render_modellight_lightdir_world[q] = rsurface.entity->render_modellight_lightdir_world[q];
6694 t->render_modellight_lightdir_local[q] = rsurface.entity->render_modellight_lightdir_local[q];
6695 t->render_modellight_ambient[q] = rsurface.entity->render_modellight_ambient[q] * r_refdef.view.colorscale;
6696 t->render_modellight_diffuse[q] = rsurface.entity->render_modellight_diffuse[q] * r_refdef.view.colorscale;
6697 t->render_modellight_specular[q] = rsurface.entity->render_modellight_specular[q] * r_refdef.view.colorscale;
6698 t->render_lightmap_ambient[q] = 0;
6699 t->render_lightmap_diffuse[q] = 0;
6700 t->render_lightmap_specular[q] = 0;
6701 t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6702 t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6707 // lightmap - 2x diffuse and specular brightness because bsp files have 0-2 colors as 0-1
6708 for (q = 0; q < 3; q++)
6710 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6711 t->render_modellight_lightdir_world[q] = q == 2;
6712 t->render_modellight_lightdir_local[q] = q == 2;
6713 t->render_modellight_ambient[q] = 0;
6714 t->render_modellight_diffuse[q] = 0;
6715 t->render_modellight_specular[q] = 0;
6716 t->render_lightmap_ambient[q] = rsurface.entity->render_lightmap_ambient[q] * r_refdef.view.colorscale;
6717 t->render_lightmap_diffuse[q] = rsurface.entity->render_lightmap_diffuse[q] * 2 * r_refdef.view.colorscale;
6718 t->render_lightmap_specular[q] = rsurface.entity->render_lightmap_specular[q] * 2 * r_refdef.view.colorscale;
6719 t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6720 t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6724 if (t->currentmaterialflags & MATERIALFLAG_VERTEXCOLOR)
6726 // since MATERIALFLAG_VERTEXCOLOR uses the lightmapcolor4f vertex
6727 // attribute, we punt it to the lightmap path and hope for the best,
6728 // but lighting doesn't work.
6730 // FIXME: this is fine for effects but CSQC polygons should be subject
6732 t->currentmaterialflags &= ~(MATERIALFLAG_MODELLIGHT | MATERIALFLAG_LIGHTGRID);
6733 for (q = 0; q < 3; q++)
6735 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6736 t->render_modellight_lightdir_world[q] = q == 2;
6737 t->render_modellight_lightdir_local[q] = q == 2;
6738 t->render_modellight_ambient[q] = 0;
6739 t->render_modellight_diffuse[q] = 0;
6740 t->render_modellight_specular[q] = 0;
6741 t->render_lightmap_ambient[q] = 0;
6742 t->render_lightmap_diffuse[q] = rsurface.entity->render_fullbright[q] * r_refdef.view.colorscale;
6743 t->render_lightmap_specular[q] = 0;
6744 t->render_rtlight_diffuse[q] = 0;
6745 t->render_rtlight_specular[q] = 0;
6749 for (q = 0; q < 3; q++)
6751 t->render_colormap_pants[q] = rsurface.entity->colormap_pantscolor[q];
6752 t->render_colormap_shirt[q] = rsurface.entity->colormap_shirtcolor[q];
6755 if (rsurface.ent_flags & RENDER_ADDITIVE)
6756 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
6757 else if (t->currentalpha < 1)
6758 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
6759 // LadyHavoc: prevent bugs where code checks add or alpha at higher priority than customblend by clearing these flags
6760 if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
6761 t->currentmaterialflags &= ~(MATERIALFLAG_ADD | MATERIALFLAG_ALPHA);
6762 if (rsurface.ent_flags & RENDER_DOUBLESIDED)
6763 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
6764 if (rsurface.ent_flags & (RENDER_NODEPTHTEST | RENDER_VIEWMODEL))
6765 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
6766 if (t->backgroundshaderpass)
6767 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
6768 if (t->currentmaterialflags & MATERIALFLAG_BLENDED)
6770 if (t->currentmaterialflags & (MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA))
6771 t->currentmaterialflags &= ~MATERIALFLAG_BLENDED;
6774 t->currentmaterialflags &= ~(MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA);
6775 if (vid.allowalphatocoverage && r_transparent_alphatocoverage.integer >= 2 && ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA | MATERIALFLAG_ADD | MATERIALFLAG_CUSTOMBLEND)) == (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)))
6777 // promote alphablend to alphatocoverage (a type of alphatest) if antialiasing is on
6778 t->currentmaterialflags = (t->currentmaterialflags & ~(MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)) | MATERIALFLAG_ALPHATEST;
6780 if ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST)) == MATERIALFLAG_BLENDED && r_transparentdepthmasking.integer && !(t->basematerialflags & MATERIALFLAG_BLENDED))
6781 t->currentmaterialflags |= MATERIALFLAG_TRANSDEPTH;
6783 // there is no tcmod
6784 if (t->currentmaterialflags & MATERIALFLAG_WATERSCROLL)
6786 t->currenttexmatrix = r_waterscrollmatrix;
6787 t->currentbackgroundtexmatrix = r_waterscrollmatrix;
6789 else if (!(t->currentmaterialflags & MATERIALFLAG_CUSTOMSURFACE))
6791 Matrix4x4_CreateIdentity(&t->currenttexmatrix);
6792 Matrix4x4_CreateIdentity(&t->currentbackgroundtexmatrix);
6795 if (t->materialshaderpass)
6796 for (i = 0, tcmod = t->materialshaderpass->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
6797 R_tcMod_ApplyToMatrix(&t->currenttexmatrix, tcmod, t->currentmaterialflags);
6799 t->colormapping = VectorLength2(t->render_colormap_pants) + VectorLength2(t->render_colormap_shirt) >= (1.0f / 1048576.0f);
6800 if (t->currentskinframe->qpixels)
6801 R_SkinFrame_GenerateTexturesFromQPixels(t->currentskinframe, t->colormapping);
6802 t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
6803 if (!t->basetexture)
6804 t->basetexture = r_texture_notexture;
6805 t->pantstexture = t->colormapping ? t->currentskinframe->pants : NULL;
6806 t->shirttexture = t->colormapping ? t->currentskinframe->shirt : NULL;
6807 t->nmaptexture = t->currentskinframe->nmap;
6808 if (!t->nmaptexture)
6809 t->nmaptexture = r_texture_blanknormalmap;
6810 t->glosstexture = r_texture_black;
6811 t->glowtexture = t->currentskinframe->glow;
6812 t->fogtexture = t->currentskinframe->fog;
6813 t->reflectmasktexture = t->currentskinframe->reflect;
6814 if (t->backgroundshaderpass)
6816 for (i = 0, tcmod = t->backgroundshaderpass->tcmods; i < Q3MAXTCMODS && tcmod->tcmod; i++, tcmod++)
6817 R_tcMod_ApplyToMatrix(&t->currentbackgroundtexmatrix, tcmod, t->currentmaterialflags);
6818 t->backgroundbasetexture = (!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base;
6819 t->backgroundnmaptexture = t->backgroundcurrentskinframe->nmap;
6820 t->backgroundglosstexture = r_texture_black;
6821 t->backgroundglowtexture = t->backgroundcurrentskinframe->glow;
6822 if (!t->backgroundnmaptexture)
6823 t->backgroundnmaptexture = r_texture_blanknormalmap;
6824 // make sure that if glow is going to be used, both textures are not NULL
6825 if (!t->backgroundglowtexture && t->glowtexture)
6826 t->backgroundglowtexture = r_texture_black;
6827 if (!t->glowtexture && t->backgroundglowtexture)
6828 t->glowtexture = r_texture_black;
6832 t->backgroundbasetexture = r_texture_white;
6833 t->backgroundnmaptexture = r_texture_blanknormalmap;
6834 t->backgroundglosstexture = r_texture_black;
6835 t->backgroundglowtexture = NULL;
6837 t->specularpower = r_shadow_glossexponent.value;
6838 // TODO: store reference values for these in the texture?
6839 if (r_shadow_gloss.integer > 0)
6841 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
6843 if (r_shadow_glossintensity.value > 0)
6845 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
6846 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
6847 specularscale = r_shadow_glossintensity.value;
6850 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
6852 t->glosstexture = r_texture_white;
6853 t->backgroundglosstexture = r_texture_white;
6854 specularscale = r_shadow_gloss2intensity.value;
6855 t->specularpower = r_shadow_gloss2exponent.value;
6858 specularscale *= t->specularscalemod;
6859 t->specularpower *= t->specularpowermod;
6861 // lightmaps mode looks bad with dlights using actual texturing, so turn
6862 // off the colormap and glossmap, but leave the normalmap on as it still
6863 // accurately represents the shading involved
6864 if (gl_lightmaps.integer && ent != &cl_meshentities[MESH_UI].render)
6866 t->basetexture = r_texture_grey128;
6867 t->pantstexture = r_texture_black;
6868 t->shirttexture = r_texture_black;
6869 if (gl_lightmaps.integer < 2)
6870 t->nmaptexture = r_texture_blanknormalmap;
6871 t->glosstexture = r_texture_black;
6872 t->glowtexture = NULL;
6873 t->fogtexture = NULL;
6874 t->reflectmasktexture = NULL;
6875 t->backgroundbasetexture = NULL;
6876 if (gl_lightmaps.integer < 2)
6877 t->backgroundnmaptexture = r_texture_blanknormalmap;
6878 t->backgroundglosstexture = r_texture_black;
6879 t->backgroundglowtexture = NULL;
6881 t->currentmaterialflags = MATERIALFLAG_WALL | (t->currentmaterialflags & (MATERIALFLAG_NOCULLFACE | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SHORTDEPTHRANGE));
6884 if (specularscale != 1.0f)
6886 for (q = 0; q < 3; q++)
6888 t->render_modellight_specular[q] *= specularscale;
6889 t->render_lightmap_specular[q] *= specularscale;
6890 t->render_rtlight_specular[q] *= specularscale;
6894 t->currentblendfunc[0] = GL_ONE;
6895 t->currentblendfunc[1] = GL_ZERO;
6896 if (t->currentmaterialflags & MATERIALFLAG_ADD)
6898 t->currentblendfunc[0] = GL_SRC_ALPHA;
6899 t->currentblendfunc[1] = GL_ONE;
6901 else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
6903 t->currentblendfunc[0] = GL_SRC_ALPHA;
6904 t->currentblendfunc[1] = GL_ONE_MINUS_SRC_ALPHA;
6906 else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
6908 t->currentblendfunc[0] = t->customblendfunc[0];
6909 t->currentblendfunc[1] = t->customblendfunc[1];
6915 rsurfacestate_t rsurface;
6917 void RSurf_ActiveModelEntity(const entity_render_t *ent, qbool wantnormals, qbool wanttangents, qbool prepass)
6919 model_t *model = ent->model;
6920 //if (rsurface.entity == ent && (!model->surfmesh.isanimated || (!wantnormals && !wanttangents)))
6922 rsurface.entity = (entity_render_t *)ent;
6923 rsurface.skeleton = ent->skeleton;
6924 memcpy(rsurface.userwavefunc_param, ent->userwavefunc_param, sizeof(rsurface.userwavefunc_param));
6925 rsurface.ent_skinnum = ent->skinnum;
6926 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;
6927 rsurface.ent_flags = ent->flags;
6928 if (r_fullbright_directed.integer && (r_fullbright.integer || !model->lit))
6929 rsurface.ent_flags |= RENDER_LIGHT | RENDER_DYNAMICMODELLIGHT;
6930 rsurface.shadertime = r_refdef.scene.time - ent->shadertime;
6931 rsurface.matrix = ent->matrix;
6932 rsurface.inversematrix = ent->inversematrix;
6933 rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
6934 rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
6935 R_EntityMatrix(&rsurface.matrix);
6936 Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
6937 Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
6938 rsurface.fogplaneviewdist = r_refdef.fogplaneviewdist * rsurface.inversematrixscale;
6939 rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
6940 rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
6941 rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
6942 memcpy(rsurface.frameblend, ent->frameblend, sizeof(ent->frameblend));
6943 rsurface.ent_alttextures = ent->framegroupblend[0].frame != 0;
6944 rsurface.basepolygonfactor = r_refdef.polygonfactor;
6945 rsurface.basepolygonoffset = r_refdef.polygonoffset;
6946 if (ent->model->brush.submodel && !prepass)
6948 rsurface.basepolygonfactor += r_polygonoffset_submodel_factor.value;
6949 rsurface.basepolygonoffset += r_polygonoffset_submodel_offset.value;
6951 // if the animcache code decided it should use the shader path, skip the deform step
6952 rsurface.entityskeletaltransform3x4 = ent->animcache_skeletaltransform3x4;
6953 rsurface.entityskeletaltransform3x4buffer = ent->animcache_skeletaltransform3x4buffer;
6954 rsurface.entityskeletaltransform3x4offset = ent->animcache_skeletaltransform3x4offset;
6955 rsurface.entityskeletaltransform3x4size = ent->animcache_skeletaltransform3x4size;
6956 rsurface.entityskeletalnumtransforms = rsurface.entityskeletaltransform3x4 ? model->num_bones : 0;
6957 if (model->surfmesh.isanimated && model->AnimateVertices && !rsurface.entityskeletaltransform3x4)
6959 if (ent->animcache_vertex3f)
6961 r_refdef.stats[r_stat_batch_entitycache_count]++;
6962 r_refdef.stats[r_stat_batch_entitycache_surfaces] += model->num_surfaces;
6963 r_refdef.stats[r_stat_batch_entitycache_vertices] += model->surfmesh.num_vertices;
6964 r_refdef.stats[r_stat_batch_entitycache_triangles] += model->surfmesh.num_triangles;
6965 rsurface.modelvertex3f = ent->animcache_vertex3f;
6966 rsurface.modelvertex3f_vertexbuffer = ent->animcache_vertex3f_vertexbuffer;
6967 rsurface.modelvertex3f_bufferoffset = ent->animcache_vertex3f_bufferoffset;
6968 rsurface.modelsvector3f = wanttangents ? ent->animcache_svector3f : NULL;
6969 rsurface.modelsvector3f_vertexbuffer = wanttangents ? ent->animcache_svector3f_vertexbuffer : NULL;
6970 rsurface.modelsvector3f_bufferoffset = wanttangents ? ent->animcache_svector3f_bufferoffset : 0;
6971 rsurface.modeltvector3f = wanttangents ? ent->animcache_tvector3f : NULL;
6972 rsurface.modeltvector3f_vertexbuffer = wanttangents ? ent->animcache_tvector3f_vertexbuffer : NULL;
6973 rsurface.modeltvector3f_bufferoffset = wanttangents ? ent->animcache_tvector3f_bufferoffset : 0;
6974 rsurface.modelnormal3f = wantnormals ? ent->animcache_normal3f : NULL;
6975 rsurface.modelnormal3f_vertexbuffer = wantnormals ? ent->animcache_normal3f_vertexbuffer : NULL;
6976 rsurface.modelnormal3f_bufferoffset = wantnormals ? ent->animcache_normal3f_bufferoffset : 0;
6978 else if (wanttangents)
6980 r_refdef.stats[r_stat_batch_entityanimate_count]++;
6981 r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
6982 r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
6983 r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
6984 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6985 rsurface.modelsvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6986 rsurface.modeltvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6987 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6988 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, rsurface.modelsvector3f, rsurface.modeltvector3f);
6989 rsurface.modelvertex3f_vertexbuffer = NULL;
6990 rsurface.modelvertex3f_bufferoffset = 0;
6991 rsurface.modelvertex3f_vertexbuffer = 0;
6992 rsurface.modelvertex3f_bufferoffset = 0;
6993 rsurface.modelsvector3f_vertexbuffer = 0;
6994 rsurface.modelsvector3f_bufferoffset = 0;
6995 rsurface.modeltvector3f_vertexbuffer = 0;
6996 rsurface.modeltvector3f_bufferoffset = 0;
6997 rsurface.modelnormal3f_vertexbuffer = 0;
6998 rsurface.modelnormal3f_bufferoffset = 0;
7000 else if (wantnormals)
7002 r_refdef.stats[r_stat_batch_entityanimate_count]++;
7003 r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
7004 r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
7005 r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
7006 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7007 rsurface.modelsvector3f = NULL;
7008 rsurface.modeltvector3f = NULL;
7009 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7010 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, NULL, NULL);
7011 rsurface.modelvertex3f_vertexbuffer = NULL;
7012 rsurface.modelvertex3f_bufferoffset = 0;
7013 rsurface.modelvertex3f_vertexbuffer = 0;
7014 rsurface.modelvertex3f_bufferoffset = 0;
7015 rsurface.modelsvector3f_vertexbuffer = 0;
7016 rsurface.modelsvector3f_bufferoffset = 0;
7017 rsurface.modeltvector3f_vertexbuffer = 0;
7018 rsurface.modeltvector3f_bufferoffset = 0;
7019 rsurface.modelnormal3f_vertexbuffer = 0;
7020 rsurface.modelnormal3f_bufferoffset = 0;
7024 r_refdef.stats[r_stat_batch_entityanimate_count]++;
7025 r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
7026 r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
7027 r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
7028 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7029 rsurface.modelsvector3f = NULL;
7030 rsurface.modeltvector3f = NULL;
7031 rsurface.modelnormal3f = NULL;
7032 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, NULL, NULL, NULL);
7033 rsurface.modelvertex3f_vertexbuffer = NULL;
7034 rsurface.modelvertex3f_bufferoffset = 0;
7035 rsurface.modelvertex3f_vertexbuffer = 0;
7036 rsurface.modelvertex3f_bufferoffset = 0;
7037 rsurface.modelsvector3f_vertexbuffer = 0;
7038 rsurface.modelsvector3f_bufferoffset = 0;
7039 rsurface.modeltvector3f_vertexbuffer = 0;
7040 rsurface.modeltvector3f_bufferoffset = 0;
7041 rsurface.modelnormal3f_vertexbuffer = 0;
7042 rsurface.modelnormal3f_bufferoffset = 0;
7044 rsurface.modelgeneratedvertex = true;
7048 if (rsurface.entityskeletaltransform3x4)
7050 r_refdef.stats[r_stat_batch_entityskeletal_count]++;
7051 r_refdef.stats[r_stat_batch_entityskeletal_surfaces] += model->num_surfaces;
7052 r_refdef.stats[r_stat_batch_entityskeletal_vertices] += model->surfmesh.num_vertices;
7053 r_refdef.stats[r_stat_batch_entityskeletal_triangles] += model->surfmesh.num_triangles;
7057 r_refdef.stats[r_stat_batch_entitystatic_count]++;
7058 r_refdef.stats[r_stat_batch_entitystatic_surfaces] += model->num_surfaces;
7059 r_refdef.stats[r_stat_batch_entitystatic_vertices] += model->surfmesh.num_vertices;
7060 r_refdef.stats[r_stat_batch_entitystatic_triangles] += model->surfmesh.num_triangles;
7062 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
7063 rsurface.modelvertex3f_vertexbuffer = model->surfmesh.data_vertex3f_vertexbuffer;
7064 rsurface.modelvertex3f_bufferoffset = model->surfmesh.data_vertex3f_bufferoffset;
7065 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
7066 rsurface.modelsvector3f_vertexbuffer = model->surfmesh.data_svector3f_vertexbuffer;
7067 rsurface.modelsvector3f_bufferoffset = model->surfmesh.data_svector3f_bufferoffset;
7068 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
7069 rsurface.modeltvector3f_vertexbuffer = model->surfmesh.data_tvector3f_vertexbuffer;
7070 rsurface.modeltvector3f_bufferoffset = model->surfmesh.data_tvector3f_bufferoffset;
7071 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
7072 rsurface.modelnormal3f_vertexbuffer = model->surfmesh.data_normal3f_vertexbuffer;
7073 rsurface.modelnormal3f_bufferoffset = model->surfmesh.data_normal3f_bufferoffset;
7074 rsurface.modelgeneratedvertex = false;
7076 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
7077 rsurface.modellightmapcolor4f_vertexbuffer = model->surfmesh.data_lightmapcolor4f_vertexbuffer;
7078 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.data_lightmapcolor4f_bufferoffset;
7079 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
7080 rsurface.modeltexcoordtexture2f_vertexbuffer = model->surfmesh.data_texcoordtexture2f_vertexbuffer;
7081 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.data_texcoordtexture2f_bufferoffset;
7082 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
7083 rsurface.modeltexcoordlightmap2f_vertexbuffer = model->surfmesh.data_texcoordlightmap2f_vertexbuffer;
7084 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.data_texcoordlightmap2f_bufferoffset;
7085 rsurface.modelskeletalindex4ub = model->surfmesh.data_skeletalindex4ub;
7086 rsurface.modelskeletalindex4ub_vertexbuffer = model->surfmesh.data_skeletalindex4ub_vertexbuffer;
7087 rsurface.modelskeletalindex4ub_bufferoffset = model->surfmesh.data_skeletalindex4ub_bufferoffset;
7088 rsurface.modelskeletalweight4ub = model->surfmesh.data_skeletalweight4ub;
7089 rsurface.modelskeletalweight4ub_vertexbuffer = model->surfmesh.data_skeletalweight4ub_vertexbuffer;
7090 rsurface.modelskeletalweight4ub_bufferoffset = model->surfmesh.data_skeletalweight4ub_bufferoffset;
7091 rsurface.modelelement3i = model->surfmesh.data_element3i;
7092 rsurface.modelelement3i_indexbuffer = model->surfmesh.data_element3i_indexbuffer;
7093 rsurface.modelelement3i_bufferoffset = model->surfmesh.data_element3i_bufferoffset;
7094 rsurface.modelelement3s = model->surfmesh.data_element3s;
7095 rsurface.modelelement3s_indexbuffer = model->surfmesh.data_element3s_indexbuffer;
7096 rsurface.modelelement3s_bufferoffset = model->surfmesh.data_element3s_bufferoffset;
7097 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
7098 rsurface.modelnumvertices = model->surfmesh.num_vertices;
7099 rsurface.modelnumtriangles = model->surfmesh.num_triangles;
7100 rsurface.modelsurfaces = model->data_surfaces;
7101 rsurface.batchgeneratedvertex = false;
7102 rsurface.batchfirstvertex = 0;
7103 rsurface.batchnumvertices = 0;
7104 rsurface.batchfirsttriangle = 0;
7105 rsurface.batchnumtriangles = 0;
7106 rsurface.batchvertex3f = NULL;
7107 rsurface.batchvertex3f_vertexbuffer = NULL;
7108 rsurface.batchvertex3f_bufferoffset = 0;
7109 rsurface.batchsvector3f = NULL;
7110 rsurface.batchsvector3f_vertexbuffer = NULL;
7111 rsurface.batchsvector3f_bufferoffset = 0;
7112 rsurface.batchtvector3f = NULL;
7113 rsurface.batchtvector3f_vertexbuffer = NULL;
7114 rsurface.batchtvector3f_bufferoffset = 0;
7115 rsurface.batchnormal3f = NULL;
7116 rsurface.batchnormal3f_vertexbuffer = NULL;
7117 rsurface.batchnormal3f_bufferoffset = 0;
7118 rsurface.batchlightmapcolor4f = NULL;
7119 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7120 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7121 rsurface.batchtexcoordtexture2f = NULL;
7122 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7123 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7124 rsurface.batchtexcoordlightmap2f = NULL;
7125 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7126 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7127 rsurface.batchskeletalindex4ub = NULL;
7128 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7129 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7130 rsurface.batchskeletalweight4ub = NULL;
7131 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7132 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7133 rsurface.batchelement3i = NULL;
7134 rsurface.batchelement3i_indexbuffer = NULL;
7135 rsurface.batchelement3i_bufferoffset = 0;
7136 rsurface.batchelement3s = NULL;
7137 rsurface.batchelement3s_indexbuffer = NULL;
7138 rsurface.batchelement3s_bufferoffset = 0;
7139 rsurface.forcecurrenttextureupdate = false;
7142 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)
7144 rsurface.entity = r_refdef.scene.worldentity;
7145 if (r != 1.0f || g != 1.0f || b != 1.0f || a != 1.0f) {
7146 // HACK to provide a valid entity with modded colors to R_GetCurrentTexture.
7147 // A better approach could be making this copy only once per frame.
7148 static entity_render_t custom_entity;
7150 custom_entity = *rsurface.entity;
7151 for (q = 0; q < 3; ++q) {
7152 float colormod = q == 0 ? r : q == 1 ? g : b;
7153 custom_entity.render_fullbright[q] *= colormod;
7154 custom_entity.render_modellight_ambient[q] *= colormod;
7155 custom_entity.render_modellight_diffuse[q] *= colormod;
7156 custom_entity.render_lightmap_ambient[q] *= colormod;
7157 custom_entity.render_lightmap_diffuse[q] *= colormod;
7158 custom_entity.render_rtlight_diffuse[q] *= colormod;
7160 custom_entity.alpha *= a;
7161 rsurface.entity = &custom_entity;
7163 rsurface.skeleton = NULL;
7164 rsurface.ent_skinnum = 0;
7165 rsurface.ent_qwskin = -1;
7166 rsurface.ent_flags = entflags;
7167 rsurface.shadertime = r_refdef.scene.time - shadertime;
7168 rsurface.modelnumvertices = numvertices;
7169 rsurface.modelnumtriangles = numtriangles;
7170 rsurface.matrix = *matrix;
7171 rsurface.inversematrix = *inversematrix;
7172 rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
7173 rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
7174 R_EntityMatrix(&rsurface.matrix);
7175 Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
7176 Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
7177 rsurface.fogplaneviewdist *= rsurface.inversematrixscale;
7178 rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
7179 rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
7180 rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
7181 memset(rsurface.frameblend, 0, sizeof(rsurface.frameblend));
7182 rsurface.frameblend[0].lerp = 1;
7183 rsurface.ent_alttextures = false;
7184 rsurface.basepolygonfactor = r_refdef.polygonfactor;
7185 rsurface.basepolygonoffset = r_refdef.polygonoffset;
7186 rsurface.entityskeletaltransform3x4 = NULL;
7187 rsurface.entityskeletaltransform3x4buffer = NULL;
7188 rsurface.entityskeletaltransform3x4offset = 0;
7189 rsurface.entityskeletaltransform3x4size = 0;
7190 rsurface.entityskeletalnumtransforms = 0;
7191 r_refdef.stats[r_stat_batch_entitycustom_count]++;
7192 r_refdef.stats[r_stat_batch_entitycustom_surfaces] += 1;
7193 r_refdef.stats[r_stat_batch_entitycustom_vertices] += rsurface.modelnumvertices;
7194 r_refdef.stats[r_stat_batch_entitycustom_triangles] += rsurface.modelnumtriangles;
7197 rsurface.modelvertex3f = (float *)vertex3f;
7198 rsurface.modelsvector3f = svector3f ? (float *)svector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7199 rsurface.modeltvector3f = tvector3f ? (float *)tvector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7200 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7202 else if (wantnormals)
7204 rsurface.modelvertex3f = (float *)vertex3f;
7205 rsurface.modelsvector3f = NULL;
7206 rsurface.modeltvector3f = NULL;
7207 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7211 rsurface.modelvertex3f = (float *)vertex3f;
7212 rsurface.modelsvector3f = NULL;
7213 rsurface.modeltvector3f = NULL;
7214 rsurface.modelnormal3f = NULL;
7216 rsurface.modelvertex3f_vertexbuffer = 0;
7217 rsurface.modelvertex3f_bufferoffset = 0;
7218 rsurface.modelsvector3f_vertexbuffer = 0;
7219 rsurface.modelsvector3f_bufferoffset = 0;
7220 rsurface.modeltvector3f_vertexbuffer = 0;
7221 rsurface.modeltvector3f_bufferoffset = 0;
7222 rsurface.modelnormal3f_vertexbuffer = 0;
7223 rsurface.modelnormal3f_bufferoffset = 0;
7224 rsurface.modelgeneratedvertex = true;
7225 rsurface.modellightmapcolor4f = (float *)color4f;
7226 rsurface.modellightmapcolor4f_vertexbuffer = 0;
7227 rsurface.modellightmapcolor4f_bufferoffset = 0;
7228 rsurface.modeltexcoordtexture2f = (float *)texcoord2f;
7229 rsurface.modeltexcoordtexture2f_vertexbuffer = 0;
7230 rsurface.modeltexcoordtexture2f_bufferoffset = 0;
7231 rsurface.modeltexcoordlightmap2f = NULL;
7232 rsurface.modeltexcoordlightmap2f_vertexbuffer = 0;
7233 rsurface.modeltexcoordlightmap2f_bufferoffset = 0;
7234 rsurface.modelskeletalindex4ub = NULL;
7235 rsurface.modelskeletalindex4ub_vertexbuffer = NULL;
7236 rsurface.modelskeletalindex4ub_bufferoffset = 0;
7237 rsurface.modelskeletalweight4ub = NULL;
7238 rsurface.modelskeletalweight4ub_vertexbuffer = NULL;
7239 rsurface.modelskeletalweight4ub_bufferoffset = 0;
7240 rsurface.modelelement3i = (int *)element3i;
7241 rsurface.modelelement3i_indexbuffer = NULL;
7242 rsurface.modelelement3i_bufferoffset = 0;
7243 rsurface.modelelement3s = (unsigned short *)element3s;
7244 rsurface.modelelement3s_indexbuffer = NULL;
7245 rsurface.modelelement3s_bufferoffset = 0;
7246 rsurface.modellightmapoffsets = NULL;
7247 rsurface.modelsurfaces = NULL;
7248 rsurface.batchgeneratedvertex = false;
7249 rsurface.batchfirstvertex = 0;
7250 rsurface.batchnumvertices = 0;
7251 rsurface.batchfirsttriangle = 0;
7252 rsurface.batchnumtriangles = 0;
7253 rsurface.batchvertex3f = NULL;
7254 rsurface.batchvertex3f_vertexbuffer = NULL;
7255 rsurface.batchvertex3f_bufferoffset = 0;
7256 rsurface.batchsvector3f = NULL;
7257 rsurface.batchsvector3f_vertexbuffer = NULL;
7258 rsurface.batchsvector3f_bufferoffset = 0;
7259 rsurface.batchtvector3f = NULL;
7260 rsurface.batchtvector3f_vertexbuffer = NULL;
7261 rsurface.batchtvector3f_bufferoffset = 0;
7262 rsurface.batchnormal3f = NULL;
7263 rsurface.batchnormal3f_vertexbuffer = NULL;
7264 rsurface.batchnormal3f_bufferoffset = 0;
7265 rsurface.batchlightmapcolor4f = NULL;
7266 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7267 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7268 rsurface.batchtexcoordtexture2f = NULL;
7269 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7270 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7271 rsurface.batchtexcoordlightmap2f = NULL;
7272 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7273 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7274 rsurface.batchskeletalindex4ub = NULL;
7275 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7276 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7277 rsurface.batchskeletalweight4ub = NULL;
7278 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7279 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7280 rsurface.batchelement3i = NULL;
7281 rsurface.batchelement3i_indexbuffer = NULL;
7282 rsurface.batchelement3i_bufferoffset = 0;
7283 rsurface.batchelement3s = NULL;
7284 rsurface.batchelement3s_indexbuffer = NULL;
7285 rsurface.batchelement3s_bufferoffset = 0;
7286 rsurface.forcecurrenttextureupdate = true;
7288 if (rsurface.modelnumvertices && rsurface.modelelement3i)
7290 if ((wantnormals || wanttangents) && !normal3f)
7292 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7293 Mod_BuildNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.modelnormal3f, r_smoothnormals_areaweighting.integer != 0);
7295 if (wanttangents && !svector3f)
7297 rsurface.modelsvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7298 rsurface.modeltvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7299 Mod_BuildTextureVectorsFromNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modeltexcoordtexture2f, rsurface.modelnormal3f, rsurface.modelelement3i, rsurface.modelsvector3f, rsurface.modeltvector3f, r_smoothnormals_areaweighting.integer != 0);
7304 float RSurf_FogPoint(const float *v)
7306 // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
7307 float FogPlaneViewDist = r_refdef.fogplaneviewdist;
7308 float FogPlaneVertexDist = DotProduct(r_refdef.fogplane, v) + r_refdef.fogplane[3];
7309 float FogHeightFade = r_refdef.fogheightfade;
7311 unsigned int fogmasktableindex;
7312 if (r_refdef.fogplaneviewabove)
7313 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
7315 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
7316 fogmasktableindex = (unsigned int)(VectorDistance(r_refdef.view.origin, v) * fogfrac * r_refdef.fogmasktabledistmultiplier);
7317 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
7320 float RSurf_FogVertex(const float *v)
7322 // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
7323 float FogPlaneViewDist = rsurface.fogplaneviewdist;
7324 float FogPlaneVertexDist = DotProduct(rsurface.fogplane, v) + rsurface.fogplane[3];
7325 float FogHeightFade = rsurface.fogheightfade;
7327 unsigned int fogmasktableindex;
7328 if (r_refdef.fogplaneviewabove)
7329 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
7331 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
7332 fogmasktableindex = (unsigned int)(VectorDistance(rsurface.localvieworigin, v) * fogfrac * rsurface.fogmasktabledistmultiplier);
7333 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
7336 void RSurf_UploadBuffersForBatch(void)
7338 // 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)
7339 // note that if rsurface.batchvertex3f_vertexbuffer is NULL, dynamicvertex is forced as we don't account for the proper base vertex here.
7340 if (rsurface.batchvertex3f && !rsurface.batchvertex3f_vertexbuffer)
7341 rsurface.batchvertex3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f, R_BUFFERDATA_VERTEX, &rsurface.batchvertex3f_bufferoffset);
7342 if (rsurface.batchsvector3f && !rsurface.batchsvector3f_vertexbuffer)
7343 rsurface.batchsvector3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchsvector3f, R_BUFFERDATA_VERTEX, &rsurface.batchsvector3f_bufferoffset);
7344 if (rsurface.batchtvector3f && !rsurface.batchtvector3f_vertexbuffer)
7345 rsurface.batchtvector3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchtvector3f, R_BUFFERDATA_VERTEX, &rsurface.batchtvector3f_bufferoffset);
7346 if (rsurface.batchnormal3f && !rsurface.batchnormal3f_vertexbuffer)
7347 rsurface.batchnormal3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f, R_BUFFERDATA_VERTEX, &rsurface.batchnormal3f_bufferoffset);
7348 if (rsurface.batchlightmapcolor4f && !rsurface.batchlightmapcolor4f_vertexbuffer)
7349 rsurface.batchlightmapcolor4f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[4]), rsurface.batchlightmapcolor4f, R_BUFFERDATA_VERTEX, &rsurface.batchlightmapcolor4f_bufferoffset);
7350 if (rsurface.batchtexcoordtexture2f && !rsurface.batchtexcoordtexture2f_vertexbuffer)
7351 rsurface.batchtexcoordtexture2f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[2]), rsurface.batchtexcoordtexture2f, R_BUFFERDATA_VERTEX, &rsurface.batchtexcoordtexture2f_bufferoffset);
7352 if (rsurface.batchtexcoordlightmap2f && !rsurface.batchtexcoordlightmap2f_vertexbuffer)
7353 rsurface.batchtexcoordlightmap2f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[2]), rsurface.batchtexcoordlightmap2f, R_BUFFERDATA_VERTEX, &rsurface.batchtexcoordlightmap2f_bufferoffset);
7354 if (rsurface.batchskeletalindex4ub && !rsurface.batchskeletalindex4ub_vertexbuffer)
7355 rsurface.batchskeletalindex4ub_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(unsigned char[4]), rsurface.batchskeletalindex4ub, R_BUFFERDATA_VERTEX, &rsurface.batchskeletalindex4ub_bufferoffset);
7356 if (rsurface.batchskeletalweight4ub && !rsurface.batchskeletalweight4ub_vertexbuffer)
7357 rsurface.batchskeletalweight4ub_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(unsigned char[4]), rsurface.batchskeletalweight4ub, R_BUFFERDATA_VERTEX, &rsurface.batchskeletalweight4ub_bufferoffset);
7359 if (rsurface.batchelement3s && !rsurface.batchelement3s_indexbuffer)
7360 rsurface.batchelement3s_indexbuffer = R_BufferData_Store(rsurface.batchnumtriangles * sizeof(short[3]), rsurface.batchelement3s, R_BUFFERDATA_INDEX16, &rsurface.batchelement3s_bufferoffset);
7361 else if (rsurface.batchelement3i && !rsurface.batchelement3i_indexbuffer)
7362 rsurface.batchelement3i_indexbuffer = R_BufferData_Store(rsurface.batchnumtriangles * sizeof(int[3]), rsurface.batchelement3i, R_BUFFERDATA_INDEX32, &rsurface.batchelement3i_bufferoffset);
7364 R_Mesh_VertexPointer( 3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
7365 R_Mesh_ColorPointer( 4, GL_FLOAT, sizeof(float[4]), rsurface.batchlightmapcolor4f, rsurface.batchlightmapcolor4f_vertexbuffer, rsurface.batchlightmapcolor4f_bufferoffset);
7366 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
7367 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchsvector3f, rsurface.batchsvector3f_vertexbuffer, rsurface.batchsvector3f_bufferoffset);
7368 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchtvector3f, rsurface.batchtvector3f_vertexbuffer, rsurface.batchtvector3f_bufferoffset);
7369 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchnormal3f, rsurface.batchnormal3f_vertexbuffer, rsurface.batchnormal3f_bufferoffset);
7370 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordlightmap2f, rsurface.batchtexcoordlightmap2f_vertexbuffer, rsurface.batchtexcoordlightmap2f_bufferoffset);
7371 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
7372 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(unsigned char[4]), rsurface.batchskeletalindex4ub, rsurface.batchskeletalindex4ub_vertexbuffer, rsurface.batchskeletalindex4ub_bufferoffset);
7373 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), rsurface.batchskeletalweight4ub, rsurface.batchskeletalweight4ub_vertexbuffer, rsurface.batchskeletalweight4ub_bufferoffset);
7376 static void RSurf_RenumberElements(const int *inelement3i, int *outelement3i, int numelements, int adjust)
7379 for (i = 0;i < numelements;i++)
7380 outelement3i[i] = inelement3i[i] + adjust;
7383 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
7384 void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const msurface_t **texturesurfacelist)
7392 int surfacefirsttriangle;
7393 int surfacenumtriangles;
7394 int surfacefirstvertex;
7395 int surfaceendvertex;
7396 int surfacenumvertices;
7397 int batchnumsurfaces = texturenumsurfaces;
7398 int batchnumvertices;
7399 int batchnumtriangles;
7402 qbool dynamicvertex;
7405 float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
7408 q3shaderinfo_deform_t *deform;
7409 const msurface_t *surface, *firstsurface;
7410 if (!texturenumsurfaces)
7412 // find vertex range of this surface batch
7414 firstsurface = texturesurfacelist[0];
7415 firsttriangle = firstsurface->num_firsttriangle;
7416 batchnumvertices = 0;
7417 batchnumtriangles = 0;
7418 firstvertex = endvertex = firstsurface->num_firstvertex;
7419 for (i = 0;i < texturenumsurfaces;i++)
7421 surface = texturesurfacelist[i];
7422 if (surface != firstsurface + i)
7424 surfacefirstvertex = surface->num_firstvertex;
7425 surfaceendvertex = surfacefirstvertex + surface->num_vertices;
7426 surfacenumvertices = surface->num_vertices;
7427 surfacenumtriangles = surface->num_triangles;
7428 if (firstvertex > surfacefirstvertex)
7429 firstvertex = surfacefirstvertex;
7430 if (endvertex < surfaceendvertex)
7431 endvertex = surfaceendvertex;
7432 batchnumvertices += surfacenumvertices;
7433 batchnumtriangles += surfacenumtriangles;
7436 r_refdef.stats[r_stat_batch_batches]++;
7438 r_refdef.stats[r_stat_batch_withgaps]++;
7439 r_refdef.stats[r_stat_batch_surfaces] += batchnumsurfaces;
7440 r_refdef.stats[r_stat_batch_vertices] += batchnumvertices;
7441 r_refdef.stats[r_stat_batch_triangles] += batchnumtriangles;
7443 // we now know the vertex range used, and if there are any gaps in it
7444 rsurface.batchfirstvertex = firstvertex;
7445 rsurface.batchnumvertices = endvertex - firstvertex;
7446 rsurface.batchfirsttriangle = firsttriangle;
7447 rsurface.batchnumtriangles = batchnumtriangles;
7449 // check if any dynamic vertex processing must occur
7450 dynamicvertex = false;
7452 // we must use vertexbuffers for rendering, we can upload vertex buffers
7453 // easily enough but if the basevertex is non-zero it becomes more
7454 // difficult, so force dynamicvertex path in that case - it's suboptimal
7455 // but the most optimal case is to have the geometry sources provide their
7457 if (!rsurface.modelvertex3f_vertexbuffer && firstvertex != 0)
7458 dynamicvertex = true;
7460 // a cvar to force the dynamic vertex path to be taken, for debugging
7461 if (r_batch_debugdynamicvertexpath.integer)
7465 r_refdef.stats[r_stat_batch_dynamic_batches_because_cvar] += 1;
7466 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_cvar] += batchnumsurfaces;
7467 r_refdef.stats[r_stat_batch_dynamic_vertices_because_cvar] += batchnumvertices;
7468 r_refdef.stats[r_stat_batch_dynamic_triangles_because_cvar] += batchnumtriangles;
7470 dynamicvertex = true;
7473 // if there is a chance of animated vertex colors, it's a dynamic batch
7474 if ((batchneed & BATCHNEED_ARRAY_VERTEXCOLOR) && texturesurfacelist[0]->lightmapinfo)
7478 r_refdef.stats[r_stat_batch_dynamic_batches_because_lightmapvertex] += 1;
7479 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_lightmapvertex] += batchnumsurfaces;
7480 r_refdef.stats[r_stat_batch_dynamic_vertices_because_lightmapvertex] += batchnumvertices;
7481 r_refdef.stats[r_stat_batch_dynamic_triangles_because_lightmapvertex] += batchnumtriangles;
7483 dynamicvertex = true;
7486 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
7488 switch (deform->deform)
7491 case Q3DEFORM_PROJECTIONSHADOW:
7492 case Q3DEFORM_TEXT0:
7493 case Q3DEFORM_TEXT1:
7494 case Q3DEFORM_TEXT2:
7495 case Q3DEFORM_TEXT3:
7496 case Q3DEFORM_TEXT4:
7497 case Q3DEFORM_TEXT5:
7498 case Q3DEFORM_TEXT6:
7499 case Q3DEFORM_TEXT7:
7502 case Q3DEFORM_AUTOSPRITE:
7505 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_autosprite] += 1;
7506 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_autosprite] += batchnumsurfaces;
7507 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_autosprite] += batchnumvertices;
7508 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_autosprite] += batchnumtriangles;
7510 dynamicvertex = true;
7511 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_TEXCOORD;
7513 case Q3DEFORM_AUTOSPRITE2:
7516 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_autosprite2] += 1;
7517 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_autosprite2] += batchnumsurfaces;
7518 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_autosprite2] += batchnumvertices;
7519 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_autosprite2] += batchnumtriangles;
7521 dynamicvertex = true;
7522 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD;
7524 case Q3DEFORM_NORMAL:
7527 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_normal] += 1;
7528 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_normal] += batchnumsurfaces;
7529 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_normal] += batchnumvertices;
7530 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_normal] += batchnumtriangles;
7532 dynamicvertex = true;
7533 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7536 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
7537 break; // if wavefunc is a nop, ignore this transform
7540 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_wave] += 1;
7541 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_wave] += batchnumsurfaces;
7542 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_wave] += batchnumvertices;
7543 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_wave] += batchnumtriangles;
7545 dynamicvertex = true;
7546 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7548 case Q3DEFORM_BULGE:
7551 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_bulge] += 1;
7552 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_bulge] += batchnumsurfaces;
7553 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_bulge] += batchnumvertices;
7554 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_bulge] += batchnumtriangles;
7556 dynamicvertex = true;
7557 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7560 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
7561 break; // if wavefunc is a nop, ignore this transform
7564 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_move] += 1;
7565 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_move] += batchnumsurfaces;
7566 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_move] += batchnumvertices;
7567 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_move] += batchnumtriangles;
7569 dynamicvertex = true;
7570 batchneed |= BATCHNEED_ARRAY_VERTEX;
7574 if (rsurface.texture->materialshaderpass)
7576 switch (rsurface.texture->materialshaderpass->tcgen.tcgen)
7579 case Q3TCGEN_TEXTURE:
7581 case Q3TCGEN_LIGHTMAP:
7584 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_lightmap] += 1;
7585 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_lightmap] += batchnumsurfaces;
7586 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_lightmap] += batchnumvertices;
7587 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_lightmap] += batchnumtriangles;
7589 dynamicvertex = true;
7590 batchneed |= BATCHNEED_ARRAY_LIGHTMAP;
7592 case Q3TCGEN_VECTOR:
7595 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_vector] += 1;
7596 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_vector] += batchnumsurfaces;
7597 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_vector] += batchnumvertices;
7598 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_vector] += batchnumtriangles;
7600 dynamicvertex = true;
7601 batchneed |= BATCHNEED_ARRAY_VERTEX;
7603 case Q3TCGEN_ENVIRONMENT:
7606 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_environment] += 1;
7607 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_environment] += batchnumsurfaces;
7608 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_environment] += batchnumvertices;
7609 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_environment] += batchnumtriangles;
7611 dynamicvertex = true;
7612 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL;
7615 if (rsurface.texture->materialshaderpass->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
7619 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcmod_turbulent] += 1;
7620 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcmod_turbulent] += batchnumsurfaces;
7621 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcmod_turbulent] += batchnumvertices;
7622 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcmod_turbulent] += batchnumtriangles;
7624 dynamicvertex = true;
7625 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD;
7629 // the caller can specify BATCHNEED_NOGAPS to force a batch with
7630 // firstvertex = 0 and endvertex = numvertices (no gaps, no firstvertex),
7631 // we ensure this by treating the vertex batch as dynamic...
7632 if ((batchneed & BATCHNEED_ALWAYSCOPY) || ((batchneed & BATCHNEED_NOGAPS) && (gaps || firstvertex > 0)))
7636 r_refdef.stats[r_stat_batch_dynamic_batches_because_nogaps] += 1;
7637 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_nogaps] += batchnumsurfaces;
7638 r_refdef.stats[r_stat_batch_dynamic_vertices_because_nogaps] += batchnumvertices;
7639 r_refdef.stats[r_stat_batch_dynamic_triangles_because_nogaps] += batchnumtriangles;
7641 dynamicvertex = true;
7644 // if we're going to have to apply the skeletal transform manually, we need to batch the skeletal data
7645 if (dynamicvertex && rsurface.entityskeletaltransform3x4)
7646 batchneed |= BATCHNEED_ARRAY_SKELETAL;
7648 rsurface.batchvertex3f = rsurface.modelvertex3f;
7649 rsurface.batchvertex3f_vertexbuffer = rsurface.modelvertex3f_vertexbuffer;
7650 rsurface.batchvertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
7651 rsurface.batchsvector3f = rsurface.modelsvector3f;
7652 rsurface.batchsvector3f_vertexbuffer = rsurface.modelsvector3f_vertexbuffer;
7653 rsurface.batchsvector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
7654 rsurface.batchtvector3f = rsurface.modeltvector3f;
7655 rsurface.batchtvector3f_vertexbuffer = rsurface.modeltvector3f_vertexbuffer;
7656 rsurface.batchtvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
7657 rsurface.batchnormal3f = rsurface.modelnormal3f;
7658 rsurface.batchnormal3f_vertexbuffer = rsurface.modelnormal3f_vertexbuffer;
7659 rsurface.batchnormal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
7660 rsurface.batchlightmapcolor4f = rsurface.modellightmapcolor4f;
7661 rsurface.batchlightmapcolor4f_vertexbuffer = rsurface.modellightmapcolor4f_vertexbuffer;
7662 rsurface.batchlightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
7663 rsurface.batchtexcoordtexture2f = rsurface.modeltexcoordtexture2f;
7664 rsurface.batchtexcoordtexture2f_vertexbuffer = rsurface.modeltexcoordtexture2f_vertexbuffer;
7665 rsurface.batchtexcoordtexture2f_bufferoffset = rsurface.modeltexcoordtexture2f_bufferoffset;
7666 rsurface.batchtexcoordlightmap2f = rsurface.modeltexcoordlightmap2f;
7667 rsurface.batchtexcoordlightmap2f_vertexbuffer = rsurface.modeltexcoordlightmap2f_vertexbuffer;
7668 rsurface.batchtexcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
7669 rsurface.batchskeletalindex4ub = rsurface.modelskeletalindex4ub;
7670 rsurface.batchskeletalindex4ub_vertexbuffer = rsurface.modelskeletalindex4ub_vertexbuffer;
7671 rsurface.batchskeletalindex4ub_bufferoffset = rsurface.modelskeletalindex4ub_bufferoffset;
7672 rsurface.batchskeletalweight4ub = rsurface.modelskeletalweight4ub;
7673 rsurface.batchskeletalweight4ub_vertexbuffer = rsurface.modelskeletalweight4ub_vertexbuffer;
7674 rsurface.batchskeletalweight4ub_bufferoffset = rsurface.modelskeletalweight4ub_bufferoffset;
7675 rsurface.batchelement3i = rsurface.modelelement3i;
7676 rsurface.batchelement3i_indexbuffer = rsurface.modelelement3i_indexbuffer;
7677 rsurface.batchelement3i_bufferoffset = rsurface.modelelement3i_bufferoffset;
7678 rsurface.batchelement3s = rsurface.modelelement3s;
7679 rsurface.batchelement3s_indexbuffer = rsurface.modelelement3s_indexbuffer;
7680 rsurface.batchelement3s_bufferoffset = rsurface.modelelement3s_bufferoffset;
7681 rsurface.batchskeletaltransform3x4 = rsurface.entityskeletaltransform3x4;
7682 rsurface.batchskeletaltransform3x4buffer = rsurface.entityskeletaltransform3x4buffer;
7683 rsurface.batchskeletaltransform3x4offset = rsurface.entityskeletaltransform3x4offset;
7684 rsurface.batchskeletaltransform3x4size = rsurface.entityskeletaltransform3x4size;
7685 rsurface.batchskeletalnumtransforms = rsurface.entityskeletalnumtransforms;
7687 // if any dynamic vertex processing has to occur in software, we copy the
7688 // entire surface list together before processing to rebase the vertices
7689 // to start at 0 (otherwise we waste a lot of room in a vertex buffer).
7691 // if any gaps exist and we do not have a static vertex buffer, we have to
7692 // copy the surface list together to avoid wasting upload bandwidth on the
7693 // vertices in the gaps.
7695 // if gaps exist and we have a static vertex buffer, we can choose whether
7696 // to combine the index buffer ranges into one dynamic index buffer or
7697 // simply issue multiple glDrawElements calls (BATCHNEED_ALLOWMULTIDRAW).
7699 // in many cases the batch is reduced to one draw call.
7701 rsurface.batchmultidraw = false;
7702 rsurface.batchmultidrawnumsurfaces = 0;
7703 rsurface.batchmultidrawsurfacelist = NULL;
7707 // static vertex data, just set pointers...
7708 rsurface.batchgeneratedvertex = false;
7709 // if there are gaps, we want to build a combined index buffer,
7710 // otherwise use the original static buffer with an appropriate offset
7713 r_refdef.stats[r_stat_batch_copytriangles_batches] += 1;
7714 r_refdef.stats[r_stat_batch_copytriangles_surfaces] += batchnumsurfaces;
7715 r_refdef.stats[r_stat_batch_copytriangles_vertices] += batchnumvertices;
7716 r_refdef.stats[r_stat_batch_copytriangles_triangles] += batchnumtriangles;
7717 if ((batchneed & BATCHNEED_ALLOWMULTIDRAW) && r_batch_multidraw.integer && batchnumtriangles >= r_batch_multidraw_mintriangles.integer)
7719 rsurface.batchmultidraw = true;
7720 rsurface.batchmultidrawnumsurfaces = texturenumsurfaces;
7721 rsurface.batchmultidrawsurfacelist = texturesurfacelist;
7724 // build a new triangle elements array for this batch
7725 rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
7726 rsurface.batchfirsttriangle = 0;
7728 for (i = 0;i < texturenumsurfaces;i++)
7730 surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
7731 surfacenumtriangles = texturesurfacelist[i]->num_triangles;
7732 memcpy(rsurface.batchelement3i + 3*numtriangles, rsurface.modelelement3i + 3*surfacefirsttriangle, surfacenumtriangles*sizeof(int[3]));
7733 numtriangles += surfacenumtriangles;
7735 rsurface.batchelement3i_indexbuffer = NULL;
7736 rsurface.batchelement3i_bufferoffset = 0;
7737 rsurface.batchelement3s = NULL;
7738 rsurface.batchelement3s_indexbuffer = NULL;
7739 rsurface.batchelement3s_bufferoffset = 0;
7740 if (endvertex <= 65536)
7742 // make a 16bit (unsigned short) index array if possible
7743 rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
7744 for (i = 0;i < numtriangles*3;i++)
7745 rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
7750 r_refdef.stats[r_stat_batch_fast_batches] += 1;
7751 r_refdef.stats[r_stat_batch_fast_surfaces] += batchnumsurfaces;
7752 r_refdef.stats[r_stat_batch_fast_vertices] += batchnumvertices;
7753 r_refdef.stats[r_stat_batch_fast_triangles] += batchnumtriangles;
7758 // something needs software processing, do it for real...
7759 // we only directly handle separate array data in this case and then
7760 // generate interleaved data if needed...
7761 rsurface.batchgeneratedvertex = true;
7762 r_refdef.stats[r_stat_batch_dynamic_batches] += 1;
7763 r_refdef.stats[r_stat_batch_dynamic_surfaces] += batchnumsurfaces;
7764 r_refdef.stats[r_stat_batch_dynamic_vertices] += batchnumvertices;
7765 r_refdef.stats[r_stat_batch_dynamic_triangles] += batchnumtriangles;
7767 // now copy the vertex data into a combined array and make an index array
7768 // (this is what Quake3 does all the time)
7769 // we also apply any skeletal animation here that would have been done in
7770 // the vertex shader, because most of the dynamic vertex animation cases
7771 // need actual vertex positions and normals
7772 //if (dynamicvertex)
7774 rsurface.batchvertex3f = NULL;
7775 rsurface.batchvertex3f_vertexbuffer = NULL;
7776 rsurface.batchvertex3f_bufferoffset = 0;
7777 rsurface.batchsvector3f = NULL;
7778 rsurface.batchsvector3f_vertexbuffer = NULL;
7779 rsurface.batchsvector3f_bufferoffset = 0;
7780 rsurface.batchtvector3f = NULL;
7781 rsurface.batchtvector3f_vertexbuffer = NULL;
7782 rsurface.batchtvector3f_bufferoffset = 0;
7783 rsurface.batchnormal3f = NULL;
7784 rsurface.batchnormal3f_vertexbuffer = NULL;
7785 rsurface.batchnormal3f_bufferoffset = 0;
7786 rsurface.batchlightmapcolor4f = NULL;
7787 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7788 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7789 rsurface.batchtexcoordtexture2f = NULL;
7790 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7791 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7792 rsurface.batchtexcoordlightmap2f = NULL;
7793 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7794 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7795 rsurface.batchskeletalindex4ub = NULL;
7796 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7797 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7798 rsurface.batchskeletalweight4ub = NULL;
7799 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7800 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7801 rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
7802 rsurface.batchelement3i_indexbuffer = NULL;
7803 rsurface.batchelement3i_bufferoffset = 0;
7804 rsurface.batchelement3s = NULL;
7805 rsurface.batchelement3s_indexbuffer = NULL;
7806 rsurface.batchelement3s_bufferoffset = 0;
7807 rsurface.batchskeletaltransform3x4buffer = NULL;
7808 rsurface.batchskeletaltransform3x4offset = 0;
7809 rsurface.batchskeletaltransform3x4size = 0;
7810 // we'll only be setting up certain arrays as needed
7811 if (batchneed & BATCHNEED_ARRAY_VERTEX)
7812 rsurface.batchvertex3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7813 if (batchneed & BATCHNEED_ARRAY_NORMAL)
7814 rsurface.batchnormal3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7815 if (batchneed & BATCHNEED_ARRAY_VECTOR)
7817 rsurface.batchsvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7818 rsurface.batchtvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7820 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
7821 rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
7822 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
7823 rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
7824 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
7825 rsurface.batchtexcoordlightmap2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
7826 if (batchneed & BATCHNEED_ARRAY_SKELETAL)
7828 rsurface.batchskeletalindex4ub = (unsigned char *)R_FrameData_Alloc(batchnumvertices * sizeof(unsigned char[4]));
7829 rsurface.batchskeletalweight4ub = (unsigned char *)R_FrameData_Alloc(batchnumvertices * sizeof(unsigned char[4]));
7833 for (i = 0;i < texturenumsurfaces;i++)
7835 surfacefirstvertex = texturesurfacelist[i]->num_firstvertex;
7836 surfacenumvertices = texturesurfacelist[i]->num_vertices;
7837 surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
7838 surfacenumtriangles = texturesurfacelist[i]->num_triangles;
7839 // copy only the data requested
7840 if (batchneed & (BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_ARRAY_LIGHTMAP))
7842 if (batchneed & BATCHNEED_ARRAY_VERTEX)
7844 if (rsurface.batchvertex3f)
7845 memcpy(rsurface.batchvertex3f + 3*numvertices, rsurface.modelvertex3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7847 memset(rsurface.batchvertex3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7849 if (batchneed & BATCHNEED_ARRAY_NORMAL)
7851 if (rsurface.modelnormal3f)
7852 memcpy(rsurface.batchnormal3f + 3*numvertices, rsurface.modelnormal3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7854 memset(rsurface.batchnormal3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7856 if (batchneed & BATCHNEED_ARRAY_VECTOR)
7858 if (rsurface.modelsvector3f)
7860 memcpy(rsurface.batchsvector3f + 3*numvertices, rsurface.modelsvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7861 memcpy(rsurface.batchtvector3f + 3*numvertices, rsurface.modeltvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7865 memset(rsurface.batchsvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7866 memset(rsurface.batchtvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7869 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
7871 if (rsurface.modellightmapcolor4f)
7872 memcpy(rsurface.batchlightmapcolor4f + 4*numvertices, rsurface.modellightmapcolor4f + 4*surfacefirstvertex, surfacenumvertices * sizeof(float[4]));
7874 memset(rsurface.batchlightmapcolor4f + 4*numvertices, 0, surfacenumvertices * sizeof(float[4]));
7876 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
7878 if (rsurface.modeltexcoordtexture2f)
7879 memcpy(rsurface.batchtexcoordtexture2f + 2*numvertices, rsurface.modeltexcoordtexture2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
7881 memset(rsurface.batchtexcoordtexture2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
7883 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
7885 if (rsurface.modeltexcoordlightmap2f)
7886 memcpy(rsurface.batchtexcoordlightmap2f + 2*numvertices, rsurface.modeltexcoordlightmap2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
7888 memset(rsurface.batchtexcoordlightmap2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
7890 if (batchneed & BATCHNEED_ARRAY_SKELETAL)
7892 if (rsurface.modelskeletalindex4ub)
7894 memcpy(rsurface.batchskeletalindex4ub + 4*numvertices, rsurface.modelskeletalindex4ub + 4*surfacefirstvertex, surfacenumvertices * sizeof(unsigned char[4]));
7895 memcpy(rsurface.batchskeletalweight4ub + 4*numvertices, rsurface.modelskeletalweight4ub + 4*surfacefirstvertex, surfacenumvertices * sizeof(unsigned char[4]));
7899 memset(rsurface.batchskeletalindex4ub + 4*numvertices, 0, surfacenumvertices * sizeof(unsigned char[4]));
7900 memset(rsurface.batchskeletalweight4ub + 4*numvertices, 0, surfacenumvertices * sizeof(unsigned char[4]));
7901 ub = rsurface.batchskeletalweight4ub + 4*numvertices;
7902 for (j = 0;j < surfacenumvertices;j++)
7907 RSurf_RenumberElements(rsurface.modelelement3i + 3*surfacefirsttriangle, rsurface.batchelement3i + 3*numtriangles, 3*surfacenumtriangles, numvertices - surfacefirstvertex);
7908 numvertices += surfacenumvertices;
7909 numtriangles += surfacenumtriangles;
7912 // generate a 16bit index array as well if possible
7913 // (in general, dynamic batches fit)
7914 if (numvertices <= 65536)
7916 rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
7917 for (i = 0;i < numtriangles*3;i++)
7918 rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
7921 // since we've copied everything, the batch now starts at 0
7922 rsurface.batchfirstvertex = 0;
7923 rsurface.batchnumvertices = batchnumvertices;
7924 rsurface.batchfirsttriangle = 0;
7925 rsurface.batchnumtriangles = batchnumtriangles;
7928 // apply skeletal animation that would have been done in the vertex shader
7929 if (rsurface.batchskeletaltransform3x4)
7931 const unsigned char *si;
7932 const unsigned char *sw;
7934 const float *b = rsurface.batchskeletaltransform3x4;
7935 float *vp, *vs, *vt, *vn;
7937 float m[3][4], n[3][4];
7938 float tp[3], ts[3], tt[3], tn[3];
7939 r_refdef.stats[r_stat_batch_dynamicskeletal_batches] += 1;
7940 r_refdef.stats[r_stat_batch_dynamicskeletal_surfaces] += batchnumsurfaces;
7941 r_refdef.stats[r_stat_batch_dynamicskeletal_vertices] += batchnumvertices;
7942 r_refdef.stats[r_stat_batch_dynamicskeletal_triangles] += batchnumtriangles;
7943 si = rsurface.batchskeletalindex4ub;
7944 sw = rsurface.batchskeletalweight4ub;
7945 vp = rsurface.batchvertex3f;
7946 vs = rsurface.batchsvector3f;
7947 vt = rsurface.batchtvector3f;
7948 vn = rsurface.batchnormal3f;
7949 memset(m[0], 0, sizeof(m));
7950 memset(n[0], 0, sizeof(n));
7951 for (i = 0;i < batchnumvertices;i++)
7953 t[0] = b + si[0]*12;
7956 // common case - only one matrix
7970 else if (sw[2] + sw[3])
7973 t[1] = b + si[1]*12;
7974 t[2] = b + si[2]*12;
7975 t[3] = b + si[3]*12;
7976 w[0] = sw[0] * (1.0f / 255.0f);
7977 w[1] = sw[1] * (1.0f / 255.0f);
7978 w[2] = sw[2] * (1.0f / 255.0f);
7979 w[3] = sw[3] * (1.0f / 255.0f);
7980 // blend the matrices
7981 m[0][0] = t[0][ 0] * w[0] + t[1][ 0] * w[1] + t[2][ 0] * w[2] + t[3][ 0] * w[3];
7982 m[0][1] = t[0][ 1] * w[0] + t[1][ 1] * w[1] + t[2][ 1] * w[2] + t[3][ 1] * w[3];
7983 m[0][2] = t[0][ 2] * w[0] + t[1][ 2] * w[1] + t[2][ 2] * w[2] + t[3][ 2] * w[3];
7984 m[0][3] = t[0][ 3] * w[0] + t[1][ 3] * w[1] + t[2][ 3] * w[2] + t[3][ 3] * w[3];
7985 m[1][0] = t[0][ 4] * w[0] + t[1][ 4] * w[1] + t[2][ 4] * w[2] + t[3][ 4] * w[3];
7986 m[1][1] = t[0][ 5] * w[0] + t[1][ 5] * w[1] + t[2][ 5] * w[2] + t[3][ 5] * w[3];
7987 m[1][2] = t[0][ 6] * w[0] + t[1][ 6] * w[1] + t[2][ 6] * w[2] + t[3][ 6] * w[3];
7988 m[1][3] = t[0][ 7] * w[0] + t[1][ 7] * w[1] + t[2][ 7] * w[2] + t[3][ 7] * w[3];
7989 m[2][0] = t[0][ 8] * w[0] + t[1][ 8] * w[1] + t[2][ 8] * w[2] + t[3][ 8] * w[3];
7990 m[2][1] = t[0][ 9] * w[0] + t[1][ 9] * w[1] + t[2][ 9] * w[2] + t[3][ 9] * w[3];
7991 m[2][2] = t[0][10] * w[0] + t[1][10] * w[1] + t[2][10] * w[2] + t[3][10] * w[3];
7992 m[2][3] = t[0][11] * w[0] + t[1][11] * w[1] + t[2][11] * w[2] + t[3][11] * w[3];
7997 t[1] = b + si[1]*12;
7998 w[0] = sw[0] * (1.0f / 255.0f);
7999 w[1] = sw[1] * (1.0f / 255.0f);
8000 // blend the matrices
8001 m[0][0] = t[0][ 0] * w[0] + t[1][ 0] * w[1];
8002 m[0][1] = t[0][ 1] * w[0] + t[1][ 1] * w[1];
8003 m[0][2] = t[0][ 2] * w[0] + t[1][ 2] * w[1];
8004 m[0][3] = t[0][ 3] * w[0] + t[1][ 3] * w[1];
8005 m[1][0] = t[0][ 4] * w[0] + t[1][ 4] * w[1];
8006 m[1][1] = t[0][ 5] * w[0] + t[1][ 5] * w[1];
8007 m[1][2] = t[0][ 6] * w[0] + t[1][ 6] * w[1];
8008 m[1][3] = t[0][ 7] * w[0] + t[1][ 7] * w[1];
8009 m[2][0] = t[0][ 8] * w[0] + t[1][ 8] * w[1];
8010 m[2][1] = t[0][ 9] * w[0] + t[1][ 9] * w[1];
8011 m[2][2] = t[0][10] * w[0] + t[1][10] * w[1];
8012 m[2][3] = t[0][11] * w[0] + t[1][11] * w[1];
8016 // modify the vertex
8018 vp[0] = tp[0] * m[0][0] + tp[1] * m[0][1] + tp[2] * m[0][2] + m[0][3];
8019 vp[1] = tp[0] * m[1][0] + tp[1] * m[1][1] + tp[2] * m[1][2] + m[1][3];
8020 vp[2] = tp[0] * m[2][0] + tp[1] * m[2][1] + tp[2] * m[2][2] + m[2][3];
8024 // the normal transformation matrix is a set of cross products...
8025 CrossProduct(m[1], m[2], n[0]);
8026 CrossProduct(m[2], m[0], n[1]);
8027 CrossProduct(m[0], m[1], n[2]); // is actually transpose(inverse(m)) * det(m)
8029 vn[0] = tn[0] * n[0][0] + tn[1] * n[0][1] + tn[2] * n[0][2];
8030 vn[1] = tn[0] * n[1][0] + tn[1] * n[1][1] + tn[2] * n[1][2];
8031 vn[2] = tn[0] * n[2][0] + tn[1] * n[2][1] + tn[2] * n[2][2];
8032 VectorNormalize(vn);
8037 vs[0] = ts[0] * n[0][0] + ts[1] * n[0][1] + ts[2] * n[0][2];
8038 vs[1] = ts[0] * n[1][0] + ts[1] * n[1][1] + ts[2] * n[1][2];
8039 vs[2] = ts[0] * n[2][0] + ts[1] * n[2][1] + ts[2] * n[2][2];
8040 VectorNormalize(vs);
8043 vt[0] = tt[0] * n[0][0] + tt[1] * n[0][1] + tt[2] * n[0][2];
8044 vt[1] = tt[0] * n[1][0] + tt[1] * n[1][1] + tt[2] * n[1][2];
8045 vt[2] = tt[0] * n[2][0] + tt[1] * n[2][1] + tt[2] * n[2][2];
8046 VectorNormalize(vt);
8051 rsurface.batchskeletaltransform3x4 = NULL;
8052 rsurface.batchskeletalnumtransforms = 0;
8055 // q1bsp surfaces rendered in vertex color mode have to have colors
8056 // calculated based on lightstyles
8057 if ((batchneed & BATCHNEED_ARRAY_VERTEXCOLOR) && texturesurfacelist[0]->lightmapinfo)
8059 // generate color arrays for the surfaces in this list
8064 const unsigned char *lm;
8065 rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
8066 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
8067 rsurface.batchlightmapcolor4f_bufferoffset = 0;
8069 for (i = 0;i < texturenumsurfaces;i++)
8071 surface = texturesurfacelist[i];
8072 offsets = rsurface.modellightmapoffsets + surface->num_firstvertex;
8073 surfacenumvertices = surface->num_vertices;
8074 if (surface->lightmapinfo->samples)
8076 for (j = 0;j < surfacenumvertices;j++)
8078 lm = surface->lightmapinfo->samples + offsets[j];
8079 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[0]];
8080 VectorScale(lm, scale, c);
8081 if (surface->lightmapinfo->styles[1] != 255)
8083 size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
8085 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[1]];
8086 VectorMA(c, scale, lm, c);
8087 if (surface->lightmapinfo->styles[2] != 255)
8090 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[2]];
8091 VectorMA(c, scale, lm, c);
8092 if (surface->lightmapinfo->styles[3] != 255)
8095 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[3]];
8096 VectorMA(c, scale, lm, c);
8103 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);
8109 for (j = 0;j < surfacenumvertices;j++)
8111 Vector4Set(rsurface.batchlightmapcolor4f + 4*numvertices, 0, 0, 0, 1);
8118 // if vertices are deformed (sprite flares and things in maps, possibly
8119 // water waves, bulges and other deformations), modify the copied vertices
8121 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
8124 switch (deform->deform)
8127 case Q3DEFORM_PROJECTIONSHADOW:
8128 case Q3DEFORM_TEXT0:
8129 case Q3DEFORM_TEXT1:
8130 case Q3DEFORM_TEXT2:
8131 case Q3DEFORM_TEXT3:
8132 case Q3DEFORM_TEXT4:
8133 case Q3DEFORM_TEXT5:
8134 case Q3DEFORM_TEXT6:
8135 case Q3DEFORM_TEXT7:
8138 case Q3DEFORM_AUTOSPRITE:
8139 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
8140 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
8141 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
8142 VectorNormalize(newforward);
8143 VectorNormalize(newright);
8144 VectorNormalize(newup);
8145 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8146 // rsurface.batchvertex3f_vertexbuffer = NULL;
8147 // rsurface.batchvertex3f_bufferoffset = 0;
8148 // rsurface.batchsvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchsvector3f);
8149 // rsurface.batchsvector3f_vertexbuffer = NULL;
8150 // rsurface.batchsvector3f_bufferoffset = 0;
8151 // rsurface.batchtvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchtvector3f);
8152 // rsurface.batchtvector3f_vertexbuffer = NULL;
8153 // rsurface.batchtvector3f_bufferoffset = 0;
8154 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8155 // rsurface.batchnormal3f_vertexbuffer = NULL;
8156 // rsurface.batchnormal3f_bufferoffset = 0;
8157 // sometimes we're on a renderpath that does not use vectors (GL11/GL13/GLES1)
8158 if (!VectorLength2(rsurface.batchnormal3f + 3*rsurface.batchfirstvertex))
8159 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8160 if (!VectorLength2(rsurface.batchsvector3f + 3*rsurface.batchfirstvertex))
8161 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);
8162 // a single autosprite surface can contain multiple sprites...
8163 for (j = 0;j < batchnumvertices - 3;j += 4)
8165 VectorClear(center);
8166 for (i = 0;i < 4;i++)
8167 VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
8168 VectorScale(center, 0.25f, center);
8169 VectorCopy(rsurface.batchnormal3f + 3*j, forward);
8170 VectorCopy(rsurface.batchsvector3f + 3*j, right);
8171 VectorCopy(rsurface.batchtvector3f + 3*j, up);
8172 for (i = 0;i < 4;i++)
8174 VectorSubtract(rsurface.batchvertex3f + 3*(j+i), center, v);
8175 VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.batchvertex3f + 3*(j+i));
8178 // if we get here, BATCHNEED_ARRAY_NORMAL and BATCHNEED_ARRAY_VECTOR are in batchneed, so no need to check
8179 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8180 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);
8182 case Q3DEFORM_AUTOSPRITE2:
8183 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
8184 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
8185 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
8186 VectorNormalize(newforward);
8187 VectorNormalize(newright);
8188 VectorNormalize(newup);
8189 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8190 // rsurface.batchvertex3f_vertexbuffer = NULL;
8191 // rsurface.batchvertex3f_bufferoffset = 0;
8193 const float *v1, *v2;
8203 memset(shortest, 0, sizeof(shortest));
8204 // a single autosprite surface can contain multiple sprites...
8205 for (j = 0;j < batchnumvertices - 3;j += 4)
8207 VectorClear(center);
8208 for (i = 0;i < 4;i++)
8209 VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
8210 VectorScale(center, 0.25f, center);
8211 // find the two shortest edges, then use them to define the
8212 // axis vectors for rotating around the central axis
8213 for (i = 0;i < 6;i++)
8215 v1 = rsurface.batchvertex3f + 3*(j+quadedges[i][0]);
8216 v2 = rsurface.batchvertex3f + 3*(j+quadedges[i][1]);
8217 l = VectorDistance2(v1, v2);
8218 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
8220 l += (1.0f / 1024.0f);
8221 if (shortest[0].length2 > l || i == 0)
8223 shortest[1] = shortest[0];
8224 shortest[0].length2 = l;
8225 shortest[0].v1 = v1;
8226 shortest[0].v2 = v2;
8228 else if (shortest[1].length2 > l || i == 1)
8230 shortest[1].length2 = l;
8231 shortest[1].v1 = v1;
8232 shortest[1].v2 = v2;
8235 VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
8236 VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
8237 // this calculates the right vector from the shortest edge
8238 // and the up vector from the edge midpoints
8239 VectorSubtract(shortest[0].v1, shortest[0].v2, right);
8240 VectorNormalize(right);
8241 VectorSubtract(end, start, up);
8242 VectorNormalize(up);
8243 // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
8244 VectorSubtract(rsurface.localvieworigin, center, forward);
8245 //Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, forward);
8246 VectorNegate(forward, forward);
8247 VectorReflect(forward, 0, up, forward);
8248 VectorNormalize(forward);
8249 CrossProduct(up, forward, newright);
8250 VectorNormalize(newright);
8251 // rotate the quad around the up axis vector, this is made
8252 // especially easy by the fact we know the quad is flat,
8253 // so we only have to subtract the center position and
8254 // measure distance along the right vector, and then
8255 // multiply that by the newright vector and add back the
8257 // we also need to subtract the old position to undo the
8258 // displacement from the center, which we do with a
8259 // DotProduct, the subtraction/addition of center is also
8260 // optimized into DotProducts here
8261 l = DotProduct(right, center);
8262 for (i = 0;i < 4;i++)
8264 v1 = rsurface.batchvertex3f + 3*(j+i);
8265 f = DotProduct(right, v1) - l;
8266 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.batchvertex3f + 3*(j+i));
8270 if(batchneed & (BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR)) // otherwise these can stay NULL
8272 // rsurface.batchnormal3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8273 // rsurface.batchnormal3f_vertexbuffer = NULL;
8274 // rsurface.batchnormal3f_bufferoffset = 0;
8275 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8277 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8279 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8280 // rsurface.batchsvector3f_vertexbuffer = NULL;
8281 // rsurface.batchsvector3f_bufferoffset = 0;
8282 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8283 // rsurface.batchtvector3f_vertexbuffer = NULL;
8284 // rsurface.batchtvector3f_bufferoffset = 0;
8285 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);
8288 case Q3DEFORM_NORMAL:
8289 // deform the normals to make reflections wavey
8290 rsurface.batchnormal3f = (float *)R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8291 rsurface.batchnormal3f_vertexbuffer = NULL;
8292 rsurface.batchnormal3f_bufferoffset = 0;
8293 for (j = 0;j < batchnumvertices;j++)
8296 float *normal = rsurface.batchnormal3f + 3*j;
8297 VectorScale(rsurface.batchvertex3f + 3*j, 0.98f, vertex);
8298 normal[0] = rsurface.batchnormal3f[j*3+0] + deform->parms[0] * noise4f( vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8299 normal[1] = rsurface.batchnormal3f[j*3+1] + deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8300 normal[2] = rsurface.batchnormal3f[j*3+2] + deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8301 VectorNormalize(normal);
8303 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8305 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8306 // rsurface.batchsvector3f_vertexbuffer = NULL;
8307 // rsurface.batchsvector3f_bufferoffset = 0;
8308 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8309 // rsurface.batchtvector3f_vertexbuffer = NULL;
8310 // rsurface.batchtvector3f_bufferoffset = 0;
8311 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);
8315 // deform vertex array to make wavey water and flags and such
8316 waveparms[0] = deform->waveparms[0];
8317 waveparms[1] = deform->waveparms[1];
8318 waveparms[2] = deform->waveparms[2];
8319 waveparms[3] = deform->waveparms[3];
8320 if(!R_TestQ3WaveFunc(deform->wavefunc, waveparms))
8321 break; // if wavefunc is a nop, don't make a dynamic vertex array
8322 // this is how a divisor of vertex influence on deformation
8323 animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
8324 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
8325 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8326 // rsurface.batchvertex3f_vertexbuffer = NULL;
8327 // rsurface.batchvertex3f_bufferoffset = 0;
8328 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8329 // rsurface.batchnormal3f_vertexbuffer = NULL;
8330 // rsurface.batchnormal3f_bufferoffset = 0;
8331 for (j = 0;j < batchnumvertices;j++)
8333 // if the wavefunc depends on time, evaluate it per-vertex
8336 waveparms[2] = deform->waveparms[2] + (rsurface.batchvertex3f[j*3+0] + rsurface.batchvertex3f[j*3+1] + rsurface.batchvertex3f[j*3+2]) * animpos;
8337 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
8339 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
8341 // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
8342 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8343 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8345 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8346 // rsurface.batchsvector3f_vertexbuffer = NULL;
8347 // rsurface.batchsvector3f_bufferoffset = 0;
8348 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8349 // rsurface.batchtvector3f_vertexbuffer = NULL;
8350 // rsurface.batchtvector3f_bufferoffset = 0;
8351 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);
8354 case Q3DEFORM_BULGE:
8355 // deform vertex array to make the surface have moving bulges
8356 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8357 // rsurface.batchvertex3f_vertexbuffer = NULL;
8358 // rsurface.batchvertex3f_bufferoffset = 0;
8359 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8360 // rsurface.batchnormal3f_vertexbuffer = NULL;
8361 // rsurface.batchnormal3f_bufferoffset = 0;
8362 for (j = 0;j < batchnumvertices;j++)
8364 scale = sin(rsurface.batchtexcoordtexture2f[j*2+0] * deform->parms[0] + rsurface.shadertime * deform->parms[2]) * deform->parms[1];
8365 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
8367 // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
8368 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8369 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8371 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8372 // rsurface.batchsvector3f_vertexbuffer = NULL;
8373 // rsurface.batchsvector3f_bufferoffset = 0;
8374 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8375 // rsurface.batchtvector3f_vertexbuffer = NULL;
8376 // rsurface.batchtvector3f_bufferoffset = 0;
8377 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);
8381 // deform vertex array
8382 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
8383 break; // if wavefunc is a nop, don't make a dynamic vertex array
8384 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
8385 VectorScale(deform->parms, scale, waveparms);
8386 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8387 // rsurface.batchvertex3f_vertexbuffer = NULL;
8388 // rsurface.batchvertex3f_bufferoffset = 0;
8389 for (j = 0;j < batchnumvertices;j++)
8390 VectorAdd(rsurface.batchvertex3f + 3*j, waveparms, rsurface.batchvertex3f + 3*j);
8395 if (rsurface.batchtexcoordtexture2f && rsurface.texture->materialshaderpass)
8397 // generate texcoords based on the chosen texcoord source
8398 switch(rsurface.texture->materialshaderpass->tcgen.tcgen)
8401 case Q3TCGEN_TEXTURE:
8403 case Q3TCGEN_LIGHTMAP:
8404 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8405 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8406 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8407 if (rsurface.batchtexcoordlightmap2f)
8408 memcpy(rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordlightmap2f, batchnumvertices * sizeof(float[2]));
8410 case Q3TCGEN_VECTOR:
8411 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8412 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8413 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8414 for (j = 0;j < batchnumvertices;j++)
8416 rsurface.batchtexcoordtexture2f[j*2+0] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->materialshaderpass->tcgen.parms);
8417 rsurface.batchtexcoordtexture2f[j*2+1] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->materialshaderpass->tcgen.parms + 3);
8420 case Q3TCGEN_ENVIRONMENT:
8421 // make environment reflections using a spheremap
8422 rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8423 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8424 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8425 for (j = 0;j < batchnumvertices;j++)
8427 // identical to Q3A's method, but executed in worldspace so
8428 // carried models can be shiny too
8430 float viewer[3], d, reflected[3], worldreflected[3];
8432 VectorSubtract(rsurface.localvieworigin, rsurface.batchvertex3f + 3*j, viewer);
8433 // VectorNormalize(viewer);
8435 d = DotProduct(rsurface.batchnormal3f + 3*j, viewer);
8437 reflected[0] = rsurface.batchnormal3f[j*3+0]*2*d - viewer[0];
8438 reflected[1] = rsurface.batchnormal3f[j*3+1]*2*d - viewer[1];
8439 reflected[2] = rsurface.batchnormal3f[j*3+2]*2*d - viewer[2];
8440 // note: this is proportinal to viewer, so we can normalize later
8442 Matrix4x4_Transform3x3(&rsurface.matrix, reflected, worldreflected);
8443 VectorNormalize(worldreflected);
8445 // note: this sphere map only uses world x and z!
8446 // so positive and negative y will LOOK THE SAME.
8447 rsurface.batchtexcoordtexture2f[j*2+0] = 0.5 + 0.5 * worldreflected[1];
8448 rsurface.batchtexcoordtexture2f[j*2+1] = 0.5 - 0.5 * worldreflected[2];
8452 // the only tcmod that needs software vertex processing is turbulent, so
8453 // check for it here and apply the changes if needed
8454 // and we only support that as the first one
8455 // (handling a mixture of turbulent and other tcmods would be problematic
8456 // without punting it entirely to a software path)
8457 if (rsurface.texture->materialshaderpass->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
8459 amplitude = rsurface.texture->materialshaderpass->tcmods[0].parms[1];
8460 animpos = rsurface.texture->materialshaderpass->tcmods[0].parms[2] + rsurface.shadertime * rsurface.texture->materialshaderpass->tcmods[0].parms[3];
8461 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8462 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8463 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8464 for (j = 0;j < batchnumvertices;j++)
8466 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);
8467 rsurface.batchtexcoordtexture2f[j*2+1] += amplitude * sin(((rsurface.batchvertex3f[j*3+1] ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
8473 void RSurf_DrawBatch(void)
8475 // sometimes a zero triangle surface (usually a degenerate patch) makes it
8476 // through the pipeline, killing it earlier in the pipeline would have
8477 // per-surface overhead rather than per-batch overhead, so it's best to
8478 // reject it here, before it hits glDraw.
8479 if (rsurface.batchnumtriangles == 0)
8482 // batch debugging code
8483 if (r_test.integer && rsurface.entity == r_refdef.scene.worldentity && rsurface.batchvertex3f == r_refdef.scene.worldentity->model->surfmesh.data_vertex3f)
8489 e = rsurface.batchelement3i + rsurface.batchfirsttriangle*3;
8490 for (i = 0;i < rsurface.batchnumtriangles*3;i++)
8493 for (j = 0;j < rsurface.entity->model->num_surfaces;j++)
8495 if (c >= rsurface.modelsurfaces[j].num_firstvertex && c < (rsurface.modelsurfaces[j].num_firstvertex + rsurface.modelsurfaces[j].num_vertices))
8497 if (rsurface.modelsurfaces[j].texture != rsurface.texture)
8498 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);
8505 if (rsurface.batchmultidraw)
8507 // issue multiple draws rather than copying index data
8508 int numsurfaces = rsurface.batchmultidrawnumsurfaces;
8509 const msurface_t **surfacelist = rsurface.batchmultidrawsurfacelist;
8510 int i, j, k, firstvertex, endvertex, firsttriangle, endtriangle;
8511 for (i = 0;i < numsurfaces;)
8513 // combine consecutive surfaces as one draw
8514 for (k = i, j = i + 1;j < numsurfaces;k = j, j++)
8515 if (surfacelist[j] != surfacelist[k] + 1)
8517 firstvertex = surfacelist[i]->num_firstvertex;
8518 endvertex = surfacelist[k]->num_firstvertex + surfacelist[k]->num_vertices;
8519 firsttriangle = surfacelist[i]->num_firsttriangle;
8520 endtriangle = surfacelist[k]->num_firsttriangle + surfacelist[k]->num_triangles;
8521 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);
8527 // there is only one consecutive run of index data (may have been combined)
8528 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);
8532 static int RSurf_FindWaterPlaneForSurface(const msurface_t *surface)
8534 // pick the closest matching water plane
8535 int planeindex, vertexindex, bestplaneindex = -1;
8539 r_waterstate_waterplane_t *p;
8540 qbool prepared = false;
8542 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
8544 if(p->camera_entity != rsurface.texture->camera_entity)
8549 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX, 1, &surface);
8551 if(rsurface.batchnumvertices == 0)
8554 for (vertexindex = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3;vertexindex < rsurface.batchnumvertices;vertexindex++, v += 3)
8556 Matrix4x4_Transform(&rsurface.matrix, v, vert);
8557 d += fabs(PlaneDiff(vert, &p->plane));
8559 if (bestd > d || bestplaneindex < 0)
8562 bestplaneindex = planeindex;
8565 return bestplaneindex;
8566 // NOTE: this MAY return a totally unrelated water plane; we can ignore
8567 // this situation though, as it might be better to render single larger
8568 // batches with useless stuff (backface culled for example) than to
8569 // render multiple smaller batches
8572 void RSurf_SetupDepthAndCulling(void)
8574 // submodels are biased to avoid z-fighting with world surfaces that they
8575 // may be exactly overlapping (avoids z-fighting artifacts on certain
8576 // doors and things in Quake maps)
8577 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
8578 GL_PolygonOffset(rsurface.basepolygonfactor + rsurface.texture->biaspolygonfactor, rsurface.basepolygonoffset + rsurface.texture->biaspolygonoffset);
8579 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
8580 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
8583 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8587 float p[3], mins[3], maxs[3];
8589 // transparent sky would be ridiculous
8590 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
8592 R_SetupShader_Generic_NoTexture(false, false);
8593 skyrenderlater = true;
8594 RSurf_SetupDepthAndCulling();
8597 // add the vertices of the surfaces to a world bounding box so we can scissor the sky render later
8598 if (r_sky_scissor.integer)
8600 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
8601 for (j = 0, v = rsurface.batchvertex3f + 3 * rsurface.batchfirstvertex; j < rsurface.batchnumvertices; j++, v += 3)
8603 Matrix4x4_Transform(&rsurface.matrix, v, p);
8606 if (mins[0] > p[0]) mins[0] = p[0];
8607 if (mins[1] > p[1]) mins[1] = p[1];
8608 if (mins[2] > p[2]) mins[2] = p[2];
8609 if (maxs[0] < p[0]) maxs[0] = p[0];
8610 if (maxs[1] < p[1]) maxs[1] = p[1];
8611 if (maxs[2] < p[2]) maxs[2] = p[2];
8615 VectorCopy(p, mins);
8616 VectorCopy(p, maxs);
8619 if (!R_ScissorForBBox(mins, maxs, scissor))
8623 if (skyscissor[0] > scissor[0])
8625 skyscissor[2] += skyscissor[0] - scissor[0];
8626 skyscissor[0] = scissor[0];
8628 if (skyscissor[1] > scissor[1])
8630 skyscissor[3] += skyscissor[1] - scissor[1];
8631 skyscissor[1] = scissor[1];
8633 if (skyscissor[0] + skyscissor[2] < scissor[0] + scissor[2])
8634 skyscissor[2] = scissor[0] + scissor[2] - skyscissor[0];
8635 if (skyscissor[1] + skyscissor[3] < scissor[1] + scissor[3])
8636 skyscissor[3] = scissor[1] + scissor[3] - skyscissor[1];
8639 Vector4Copy(scissor, skyscissor);
8643 // LadyHavoc: HalfLife maps have freaky skypolys so don't use
8644 // skymasking on them, and Quake3 never did sky masking (unlike
8645 // software Quake and software Quake2), so disable the sky masking
8646 // in Quake3 maps as it causes problems with q3map2 sky tricks,
8647 // and skymasking also looks very bad when noclipping outside the
8648 // level, so don't use it then either.
8649 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)
8651 R_Mesh_ResetTextureState();
8652 if (skyrendermasked)
8654 R_SetupShader_DepthOrShadow(false, false, false);
8655 // depth-only (masking)
8656 GL_ColorMask(0, 0, 0, 0);
8657 // just to make sure that braindead drivers don't draw
8658 // anything despite that colormask...
8659 GL_BlendFunc(GL_ZERO, GL_ONE);
8660 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8661 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8665 R_SetupShader_Generic_NoTexture(false, false);
8667 GL_BlendFunc(GL_ONE, GL_ZERO);
8668 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
8669 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1);
8670 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
8673 if (skyrendermasked)
8674 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
8676 R_Mesh_ResetTextureState();
8677 GL_Color(1, 1, 1, 1);
8680 extern rtexture_t *r_shadow_prepasslightingdiffusetexture;
8681 extern rtexture_t *r_shadow_prepasslightingspeculartexture;
8682 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool prepass, qbool ui)
8684 if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA)))
8688 // render screenspace normalmap to texture
8690 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_DEFERREDGEOMETRY, texturenumsurfaces, texturesurfacelist, NULL, false, false);
8695 // bind lightmap texture
8697 // water/refraction/reflection/camera surfaces have to be handled specially
8698 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA | MATERIALFLAG_REFLECTION)))
8700 int start, end, startplaneindex;
8701 for (start = 0;start < texturenumsurfaces;start = end)
8703 startplaneindex = RSurf_FindWaterPlaneForSurface(texturesurfacelist[start]);
8704 if(startplaneindex < 0)
8706 // this happens if the plane e.g. got backface culled and thus didn't get a water plane. We can just ignore this.
8707 // Con_Printf("No matching water plane for surface with material flags 0x%08x - PLEASE DEBUG THIS\n", rsurface.texture->currentmaterialflags);
8711 for (end = start + 1;end < texturenumsurfaces && startplaneindex == RSurf_FindWaterPlaneForSurface(texturesurfacelist[end]);end++)
8713 // now that we have a batch using the same planeindex, render it
8714 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA)))
8716 // render water or distortion background
8718 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BACKGROUND, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false, false);
8720 // blend surface on top
8721 GL_DepthMask(false);
8722 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, end-start, texturesurfacelist + start, NULL, false, false);
8725 else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION))
8727 // render surface with reflection texture as input
8728 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
8729 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false, false);
8736 // render surface batch normally
8737 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
8738 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, texturenumsurfaces, texturesurfacelist, NULL, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) != 0 || ui, ui);
8742 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth)
8746 int texturesurfaceindex;
8748 const msurface_t *surface;
8749 float surfacecolor4f[4];
8751 // R_Mesh_ResetTextureState();
8752 R_SetupShader_Generic_NoTexture(false, false);
8754 GL_BlendFunc(GL_ONE, GL_ZERO);
8755 GL_DepthMask(writedepth);
8757 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_ALWAYSCOPY, texturenumsurfaces, texturesurfacelist);
8759 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
8761 surface = texturesurfacelist[texturesurfaceindex];
8762 k = (int)(((size_t)surface) / sizeof(msurface_t));
8763 Vector4Set(surfacecolor4f, (k & 0xF) * (1.0f / 16.0f), (k & 0xF0) * (1.0f / 256.0f), (k & 0xF00) * (1.0f / 4096.0f), 1);
8764 for (j = 0;j < surface->num_vertices;j++)
8766 Vector4Copy(surfacecolor4f, rsurface.batchlightmapcolor4f + 4 * vi);
8770 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchlightmapcolor4f, rsurface.batchtexcoordtexture2f);
8774 static void R_DrawModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool prepass, qbool ui)
8777 RSurf_SetupDepthAndCulling();
8778 if (r_showsurfaces.integer && r_refdef.view.showdebug)
8780 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist, writedepth);
8783 switch (vid.renderpath)
8785 case RENDERPATH_GL32:
8786 case RENDERPATH_GLES2:
8787 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8793 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
8796 int texturenumsurfaces, endsurface;
8798 const msurface_t *surface;
8799 const msurface_t *texturesurfacelist[MESHQUEUE_TRANSPARENT_BATCHSIZE];
8801 RSurf_ActiveModelEntity(ent, true, true, false);
8803 if (r_transparentdepthmasking.integer)
8805 qbool setup = false;
8806 for (i = 0;i < numsurfaces;i = j)
8809 surface = rsurface.modelsurfaces + surfacelist[i];
8810 texture = surface->texture;
8811 rsurface.texture = R_GetCurrentTexture(texture);
8812 rsurface.lightmaptexture = NULL;
8813 rsurface.deluxemaptexture = NULL;
8814 rsurface.uselightmaptexture = false;
8815 // scan ahead until we find a different texture
8816 endsurface = min(i + 1024, numsurfaces);
8817 texturenumsurfaces = 0;
8818 texturesurfacelist[texturenumsurfaces++] = surface;
8819 for (;j < endsurface;j++)
8821 surface = rsurface.modelsurfaces + surfacelist[j];
8822 if (texture != surface->texture)
8824 texturesurfacelist[texturenumsurfaces++] = surface;
8826 if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_TRANSDEPTH))
8828 // render the range of surfaces as depth
8832 GL_ColorMask(0,0,0,0);
8835 GL_BlendFunc(GL_ONE, GL_ZERO);
8837 // R_Mesh_ResetTextureState();
8839 RSurf_SetupDepthAndCulling();
8840 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8841 R_SetupShader_DepthOrShadow(false, false, !!rsurface.batchskeletaltransform3x4);
8842 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8846 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
8849 for (i = 0;i < numsurfaces;i = j)
8852 surface = rsurface.modelsurfaces + surfacelist[i];
8853 texture = surface->texture;
8854 rsurface.texture = R_GetCurrentTexture(texture);
8855 // scan ahead until we find a different texture
8856 endsurface = min(i + MESHQUEUE_TRANSPARENT_BATCHSIZE, numsurfaces);
8857 texturenumsurfaces = 0;
8858 texturesurfacelist[texturenumsurfaces++] = surface;
8859 rsurface.lightmaptexture = surface->lightmaptexture;
8860 rsurface.deluxemaptexture = surface->deluxemaptexture;
8861 rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
8862 for (;j < endsurface;j++)
8864 surface = rsurface.modelsurfaces + surfacelist[j];
8865 if (texture != surface->texture || rsurface.lightmaptexture != surface->lightmaptexture)
8867 texturesurfacelist[texturenumsurfaces++] = surface;
8869 // render the range of surfaces
8870 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, false, false, false);
8872 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
8875 static void R_ProcessTransparentTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8877 // transparent surfaces get pushed off into the transparent queue
8878 int surfacelistindex;
8879 const msurface_t *surface;
8880 vec3_t tempcenter, center;
8881 for (surfacelistindex = 0;surfacelistindex < texturenumsurfaces;surfacelistindex++)
8883 surface = texturesurfacelist[surfacelistindex];
8884 if (r_transparent_sortsurfacesbynearest.integer)
8886 tempcenter[0] = bound(surface->mins[0], rsurface.localvieworigin[0], surface->maxs[0]);
8887 tempcenter[1] = bound(surface->mins[1], rsurface.localvieworigin[1], surface->maxs[1]);
8888 tempcenter[2] = bound(surface->mins[2], rsurface.localvieworigin[2], surface->maxs[2]);
8892 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
8893 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
8894 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
8896 Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
8897 if (rsurface.entity->transparent_offset) // transparent offset
8899 center[0] += r_refdef.view.forward[0]*rsurface.entity->transparent_offset;
8900 center[1] += r_refdef.view.forward[1]*rsurface.entity->transparent_offset;
8901 center[2] += r_refdef.view.forward[2]*rsurface.entity->transparent_offset;
8903 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);
8907 static void R_DrawTextureSurfaceList_DepthOnly(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8909 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
8911 if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
8913 RSurf_SetupDepthAndCulling();
8914 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8915 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8916 R_SetupShader_DepthOrShadow(false, false, !!rsurface.batchskeletaltransform3x4);
8920 static void R_ProcessModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool depthonly, qbool prepass, qbool ui)
8924 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8926 R_DrawTextureSurfaceList_DepthOnly(texturenumsurfaces, texturesurfacelist);
8929 if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_WALL))
8931 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
8932 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
8934 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8936 else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) && (!r_showsurfaces.integer || r_showsurfaces.integer == 3))
8937 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
8938 else if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_WALL))
8940 else if (((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED) || (r_showsurfaces.integer == 3 && (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST))))
8942 // in the deferred case, transparent surfaces were queued during prepass
8943 if (!r_shadow_usingdeferredprepass)
8944 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
8948 // the alphatest check is to make sure we write depth for anything we skipped on the depth-only pass earlier
8949 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST), prepass, ui);
8954 static void R_QueueModelSurfaceList(entity_render_t *ent, int numsurfaces, const msurface_t **surfacelist, int flagsmask, qbool writedepth, qbool depthonly, qbool prepass, qbool ui)
8958 R_FrameData_SetMark();
8959 // break the surface list down into batches by texture and use of lightmapping
8960 for (i = 0;i < numsurfaces;i = j)
8963 // texture is the base texture pointer, rsurface.texture is the
8964 // current frame/skin the texture is directing us to use (for example
8965 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
8966 // use skin 1 instead)
8967 texture = surfacelist[i]->texture;
8968 rsurface.texture = R_GetCurrentTexture(texture);
8969 if (!(rsurface.texture->currentmaterialflags & flagsmask) || (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW))
8971 // if this texture is not the kind we want, skip ahead to the next one
8972 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
8976 if(depthonly || prepass)
8978 rsurface.lightmaptexture = NULL;
8979 rsurface.deluxemaptexture = NULL;
8980 rsurface.uselightmaptexture = false;
8981 // simply scan ahead until we find a different texture or lightmap state
8982 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
8987 rsurface.lightmaptexture = surfacelist[i]->lightmaptexture;
8988 rsurface.deluxemaptexture = surfacelist[i]->deluxemaptexture;
8989 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
8990 // simply scan ahead until we find a different texture or lightmap state
8991 for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.lightmaptexture == surfacelist[j]->lightmaptexture;j++)
8994 // render the range of surfaces
8995 R_ProcessModelTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly, prepass, ui);
8997 R_FrameData_ReturnToMark();
9000 float locboxvertex3f[6*4*3] =
9002 1,0,1, 1,0,0, 1,1,0, 1,1,1,
9003 0,1,1, 0,1,0, 0,0,0, 0,0,1,
9004 1,1,1, 1,1,0, 0,1,0, 0,1,1,
9005 0,0,1, 0,0,0, 1,0,0, 1,0,1,
9006 0,0,1, 1,0,1, 1,1,1, 0,1,1,
9007 1,0,0, 0,0,0, 0,1,0, 1,1,0
9010 unsigned short locboxelements[6*2*3] =
9020 static void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
9023 cl_locnode_t *loc = (cl_locnode_t *)ent;
9025 float vertex3f[6*4*3];
9027 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9028 GL_DepthMask(false);
9029 GL_DepthRange(0, 1);
9030 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
9032 GL_CullFace(GL_NONE);
9033 R_EntityMatrix(&identitymatrix);
9035 // R_Mesh_ResetTextureState();
9038 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9039 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9040 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9041 surfacelist[0] < 0 ? 0.5f : 0.125f);
9043 if (VectorCompare(loc->mins, loc->maxs))
9045 VectorSet(size, 2, 2, 2);
9046 VectorMA(loc->mins, -0.5f, size, mins);
9050 VectorCopy(loc->mins, mins);
9051 VectorSubtract(loc->maxs, loc->mins, size);
9054 for (i = 0;i < 6*4*3;)
9055 for (j = 0;j < 3;j++, i++)
9056 vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
9058 R_Mesh_PrepareVertices_Generic_Arrays(6*4, vertex3f, NULL, NULL);
9059 R_SetupShader_Generic_NoTexture(false, false);
9060 R_Mesh_Draw(0, 6*4, 0, 6*2, NULL, NULL, 0, locboxelements, NULL, 0);
9063 void R_DrawLocs(void)
9066 cl_locnode_t *loc, *nearestloc;
9068 nearestloc = CL_Locs_FindNearest(cl.movement_origin);
9069 for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
9071 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
9072 R_MeshQueue_AddTransparent(TRANSPARENTSORT_DISTANCE, center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
9076 void R_DecalSystem_Reset(decalsystem_t *decalsystem)
9078 if (decalsystem->decals)
9079 Mem_Free(decalsystem->decals);
9080 memset(decalsystem, 0, sizeof(*decalsystem));
9083 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)
9089 // expand or initialize the system
9090 if (decalsystem->maxdecals <= decalsystem->numdecals)
9092 decalsystem_t old = *decalsystem;
9093 qbool useshortelements;
9094 decalsystem->maxdecals = max(16, decalsystem->maxdecals * 2);
9095 useshortelements = decalsystem->maxdecals * 3 <= 65536;
9096 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)));
9097 decalsystem->color4f = (float *)(decalsystem->decals + decalsystem->maxdecals);
9098 decalsystem->texcoord2f = (float *)(decalsystem->color4f + decalsystem->maxdecals*12);
9099 decalsystem->vertex3f = (float *)(decalsystem->texcoord2f + decalsystem->maxdecals*6);
9100 decalsystem->element3i = (int *)(decalsystem->vertex3f + decalsystem->maxdecals*9);
9101 decalsystem->element3s = (useshortelements ? ((unsigned short *)(decalsystem->element3i + decalsystem->maxdecals*3)) : NULL);
9102 if (decalsystem->numdecals)
9103 memcpy(decalsystem->decals, old.decals, decalsystem->numdecals * sizeof(tridecal_t));
9105 Mem_Free(old.decals);
9106 for (i = 0;i < decalsystem->maxdecals*3;i++)
9107 decalsystem->element3i[i] = i;
9108 if (useshortelements)
9109 for (i = 0;i < decalsystem->maxdecals*3;i++)
9110 decalsystem->element3s[i] = i;
9113 // grab a decal and search for another free slot for the next one
9114 decals = decalsystem->decals;
9115 decal = decalsystem->decals + (i = decalsystem->freedecal++);
9116 for (i = decalsystem->freedecal;i < decalsystem->numdecals && decals[i].color4f[0][3];i++)
9118 decalsystem->freedecal = i;
9119 if (decalsystem->numdecals <= i)
9120 decalsystem->numdecals = i + 1;
9122 // initialize the decal
9124 decal->triangleindex = triangleindex;
9125 decal->surfaceindex = surfaceindex;
9126 decal->decalsequence = decalsequence;
9127 decal->color4f[0][0] = c0[0];
9128 decal->color4f[0][1] = c0[1];
9129 decal->color4f[0][2] = c0[2];
9130 decal->color4f[0][3] = 1;
9131 decal->color4f[1][0] = c1[0];
9132 decal->color4f[1][1] = c1[1];
9133 decal->color4f[1][2] = c1[2];
9134 decal->color4f[1][3] = 1;
9135 decal->color4f[2][0] = c2[0];
9136 decal->color4f[2][1] = c2[1];
9137 decal->color4f[2][2] = c2[2];
9138 decal->color4f[2][3] = 1;
9139 decal->vertex3f[0][0] = v0[0];
9140 decal->vertex3f[0][1] = v0[1];
9141 decal->vertex3f[0][2] = v0[2];
9142 decal->vertex3f[1][0] = v1[0];
9143 decal->vertex3f[1][1] = v1[1];
9144 decal->vertex3f[1][2] = v1[2];
9145 decal->vertex3f[2][0] = v2[0];
9146 decal->vertex3f[2][1] = v2[1];
9147 decal->vertex3f[2][2] = v2[2];
9148 decal->texcoord2f[0][0] = t0[0];
9149 decal->texcoord2f[0][1] = t0[1];
9150 decal->texcoord2f[1][0] = t1[0];
9151 decal->texcoord2f[1][1] = t1[1];
9152 decal->texcoord2f[2][0] = t2[0];
9153 decal->texcoord2f[2][1] = t2[1];
9154 TriangleNormal(v0, v1, v2, decal->plane);
9155 VectorNormalize(decal->plane);
9156 decal->plane[3] = DotProduct(v0, decal->plane);
9159 extern cvar_t cl_decals_bias;
9160 extern cvar_t cl_decals_models;
9161 extern cvar_t cl_decals_newsystem_intensitymultiplier;
9162 // baseparms, parms, temps
9163 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)
9168 const float *vertex3f;
9169 const float *normal3f;
9171 float points[2][9][3];
9178 e = rsurface.modelelement3i + 3*triangleindex;
9180 vertex3f = rsurface.modelvertex3f;
9181 normal3f = rsurface.modelnormal3f;
9185 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9187 index = 3*e[cornerindex];
9188 VectorMA(vertex3f + index, cl_decals_bias.value, normal3f + index, v[cornerindex]);
9193 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9195 index = 3*e[cornerindex];
9196 VectorCopy(vertex3f + index, v[cornerindex]);
9201 //TriangleNormal(v[0], v[1], v[2], normal);
9202 //if (DotProduct(normal, localnormal) < 0.0f)
9204 // clip by each of the box planes formed from the projection matrix
9205 // if anything survives, we emit the decal
9206 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]);
9209 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]);
9212 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]);
9215 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]);
9218 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]);
9221 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]);
9224 // some part of the triangle survived, so we have to accept it...
9227 // dynamic always uses the original triangle
9229 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9231 index = 3*e[cornerindex];
9232 VectorCopy(vertex3f + index, v[cornerindex]);
9235 for (cornerindex = 0;cornerindex < numpoints;cornerindex++)
9237 // convert vertex positions to texcoords
9238 Matrix4x4_Transform(projection, v[cornerindex], temp);
9239 tc[cornerindex][0] = (temp[1]+1.0f)*0.5f * (s2-s1) + s1;
9240 tc[cornerindex][1] = (temp[2]+1.0f)*0.5f * (t2-t1) + t1;
9241 // calculate distance fade from the projection origin
9242 f = a * (1.0f-fabs(temp[0])) * cl_decals_newsystem_intensitymultiplier.value;
9243 f = bound(0.0f, f, 1.0f);
9244 c[cornerindex][0] = r * f;
9245 c[cornerindex][1] = g * f;
9246 c[cornerindex][2] = b * f;
9247 c[cornerindex][3] = 1.0f;
9248 //VectorMA(v[cornerindex], cl_decals_bias.value, localnormal, v[cornerindex]);
9251 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);
9253 for (cornerindex = 0;cornerindex < numpoints-2;cornerindex++)
9254 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);
9256 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)
9258 matrix4x4_t projection;
9259 decalsystem_t *decalsystem;
9262 const msurface_t *surface;
9263 const msurface_t *surfaces;
9264 const texture_t *texture;
9268 float localorigin[3];
9269 float localnormal[3];
9277 int bih_triangles_count;
9278 int bih_triangles[256];
9279 int bih_surfaces[256];
9281 decalsystem = &ent->decalsystem;
9283 if (!model || !ent->allowdecals || ent->alpha < 1 || (ent->flags & (RENDER_ADDITIVE | RENDER_NODEPTHTEST)))
9285 R_DecalSystem_Reset(&ent->decalsystem);
9289 if (!model->brush.data_leafs && !cl_decals_models.integer)
9291 if (decalsystem->model)
9292 R_DecalSystem_Reset(decalsystem);
9296 if (decalsystem->model != model)
9297 R_DecalSystem_Reset(decalsystem);
9298 decalsystem->model = model;
9300 RSurf_ActiveModelEntity(ent, true, false, false);
9302 Matrix4x4_Transform(&rsurface.inversematrix, worldorigin, localorigin);
9303 Matrix4x4_Transform3x3(&rsurface.inversematrix, worldnormal, localnormal);
9304 VectorNormalize(localnormal);
9305 localsize = worldsize*rsurface.inversematrixscale;
9306 localmins[0] = localorigin[0] - localsize;
9307 localmins[1] = localorigin[1] - localsize;
9308 localmins[2] = localorigin[2] - localsize;
9309 localmaxs[0] = localorigin[0] + localsize;
9310 localmaxs[1] = localorigin[1] + localsize;
9311 localmaxs[2] = localorigin[2] + localsize;
9313 //VectorCopy(localnormal, planes[4]);
9314 //VectorVectors(planes[4], planes[2], planes[0]);
9315 AnglesFromVectors(angles, localnormal, NULL, false);
9316 AngleVectors(angles, planes[0], planes[2], planes[4]);
9317 VectorNegate(planes[0], planes[1]);
9318 VectorNegate(planes[2], planes[3]);
9319 VectorNegate(planes[4], planes[5]);
9320 planes[0][3] = DotProduct(planes[0], localorigin) - localsize;
9321 planes[1][3] = DotProduct(planes[1], localorigin) - localsize;
9322 planes[2][3] = DotProduct(planes[2], localorigin) - localsize;
9323 planes[3][3] = DotProduct(planes[3], localorigin) - localsize;
9324 planes[4][3] = DotProduct(planes[4], localorigin) - localsize;
9325 planes[5][3] = DotProduct(planes[5], localorigin) - localsize;
9330 matrix4x4_t forwardprojection;
9331 Matrix4x4_CreateFromQuakeEntity(&forwardprojection, localorigin[0], localorigin[1], localorigin[2], angles[0], angles[1], angles[2], localsize);
9332 Matrix4x4_Invert_Simple(&projection, &forwardprojection);
9337 float projectionvector[4][3];
9338 VectorScale(planes[0], ilocalsize, projectionvector[0]);
9339 VectorScale(planes[2], ilocalsize, projectionvector[1]);
9340 VectorScale(planes[4], ilocalsize, projectionvector[2]);
9341 projectionvector[0][0] = planes[0][0] * ilocalsize;
9342 projectionvector[0][1] = planes[1][0] * ilocalsize;
9343 projectionvector[0][2] = planes[2][0] * ilocalsize;
9344 projectionvector[1][0] = planes[0][1] * ilocalsize;
9345 projectionvector[1][1] = planes[1][1] * ilocalsize;
9346 projectionvector[1][2] = planes[2][1] * ilocalsize;
9347 projectionvector[2][0] = planes[0][2] * ilocalsize;
9348 projectionvector[2][1] = planes[1][2] * ilocalsize;
9349 projectionvector[2][2] = planes[2][2] * ilocalsize;
9350 projectionvector[3][0] = -(localorigin[0]*projectionvector[0][0]+localorigin[1]*projectionvector[1][0]+localorigin[2]*projectionvector[2][0]);
9351 projectionvector[3][1] = -(localorigin[0]*projectionvector[0][1]+localorigin[1]*projectionvector[1][1]+localorigin[2]*projectionvector[2][1]);
9352 projectionvector[3][2] = -(localorigin[0]*projectionvector[0][2]+localorigin[1]*projectionvector[1][2]+localorigin[2]*projectionvector[2][2]);
9353 Matrix4x4_FromVectors(&projection, projectionvector[0], projectionvector[1], projectionvector[2], projectionvector[3]);
9357 dynamic = model->surfmesh.isanimated;
9358 surfaces = model->data_surfaces;
9361 bih_triangles_count = -1;
9364 if(model->render_bih.numleafs)
9365 bih = &model->render_bih;
9366 else if(model->collision_bih.numleafs)
9367 bih = &model->collision_bih;
9370 bih_triangles_count = BIH_GetTriangleListForBox(bih, sizeof(bih_triangles) / sizeof(*bih_triangles), bih_triangles, bih_surfaces, localmins, localmaxs);
9371 if(bih_triangles_count == 0)
9373 if(bih_triangles_count > (int) (sizeof(bih_triangles) / sizeof(*bih_triangles))) // hit too many, likely bad anyway
9375 if(bih_triangles_count > 0)
9377 for (triangleindex = 0; triangleindex < bih_triangles_count; ++triangleindex)
9379 surfaceindex = bih_surfaces[triangleindex];
9380 surface = surfaces + surfaceindex;
9381 texture = surface->texture;
9384 if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
9386 if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
9388 R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, bih_triangles[triangleindex], surfaceindex);
9393 for (surfaceindex = model->submodelsurfaces_start;surfaceindex < model->submodelsurfaces_end;surfaceindex++)
9395 surface = surfaces + surfaceindex;
9396 // check cull box first because it rejects more than any other check
9397 if (!dynamic && !BoxesOverlap(surface->mins, surface->maxs, localmins, localmaxs))
9399 // skip transparent surfaces
9400 texture = surface->texture;
9403 if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
9405 if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
9407 numtriangles = surface->num_triangles;
9408 for (triangleindex = 0; triangleindex < numtriangles; triangleindex++)
9409 R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, triangleindex + surface->num_firsttriangle, surfaceindex);
9414 // do not call this outside of rendering code - use R_DecalSystem_SplatEntities instead
9415 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)
9417 int renderentityindex;
9420 entity_render_t *ent;
9422 worldmins[0] = worldorigin[0] - worldsize;
9423 worldmins[1] = worldorigin[1] - worldsize;
9424 worldmins[2] = worldorigin[2] - worldsize;
9425 worldmaxs[0] = worldorigin[0] + worldsize;
9426 worldmaxs[1] = worldorigin[1] + worldsize;
9427 worldmaxs[2] = worldorigin[2] + worldsize;
9429 R_DecalSystem_SplatEntity(r_refdef.scene.worldentity, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
9431 for (renderentityindex = 0;renderentityindex < r_refdef.scene.numentities;renderentityindex++)
9433 ent = r_refdef.scene.entities[renderentityindex];
9434 if (!BoxesOverlap(ent->mins, ent->maxs, worldmins, worldmaxs))
9437 R_DecalSystem_SplatEntity(ent, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
9441 typedef struct r_decalsystem_splatqueue_s
9448 unsigned int decalsequence;
9450 r_decalsystem_splatqueue_t;
9452 int r_decalsystem_numqueued = 0;
9453 r_decalsystem_splatqueue_t r_decalsystem_queue[MAX_DECALSYSTEM_QUEUE];
9455 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)
9457 r_decalsystem_splatqueue_t *queue;
9459 if (r_decalsystem_numqueued == MAX_DECALSYSTEM_QUEUE)
9462 queue = &r_decalsystem_queue[r_decalsystem_numqueued++];
9463 VectorCopy(worldorigin, queue->worldorigin);
9464 VectorCopy(worldnormal, queue->worldnormal);
9465 Vector4Set(queue->color, r, g, b, a);
9466 Vector4Set(queue->tcrange, s1, t1, s2, t2);
9467 queue->worldsize = worldsize;
9468 queue->decalsequence = cl.decalsequence++;
9471 static void R_DecalSystem_ApplySplatEntitiesQueue(void)
9474 r_decalsystem_splatqueue_t *queue;
9476 for (i = 0, queue = r_decalsystem_queue;i < r_decalsystem_numqueued;i++, queue++)
9477 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);
9478 r_decalsystem_numqueued = 0;
9481 extern cvar_t cl_decals_max;
9482 static void R_DrawModelDecals_FadeEntity(entity_render_t *ent)
9485 decalsystem_t *decalsystem = &ent->decalsystem;
9487 unsigned int killsequence;
9492 if (!decalsystem->numdecals)
9495 if (r_showsurfaces.integer)
9498 if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
9500 R_DecalSystem_Reset(decalsystem);
9504 killsequence = cl.decalsequence - bound(1, (unsigned int) cl_decals_max.integer, cl.decalsequence);
9505 lifetime = cl_decals_time.value + cl_decals_fadetime.value;
9507 if (decalsystem->lastupdatetime)
9508 frametime = (r_refdef.scene.time - decalsystem->lastupdatetime);
9511 decalsystem->lastupdatetime = r_refdef.scene.time;
9512 numdecals = decalsystem->numdecals;
9514 for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
9516 if (decal->color4f[0][3])
9518 decal->lived += frametime;
9519 if (killsequence > decal->decalsequence || decal->lived >= lifetime)
9521 memset(decal, 0, sizeof(*decal));
9522 if (decalsystem->freedecal > i)
9523 decalsystem->freedecal = i;
9527 decal = decalsystem->decals;
9528 while (numdecals > 0 && !decal[numdecals-1].color4f[0][3])
9531 // collapse the array by shuffling the tail decals into the gaps
9534 while (decalsystem->freedecal < numdecals && decal[decalsystem->freedecal].color4f[0][3])
9535 decalsystem->freedecal++;
9536 if (decalsystem->freedecal == numdecals)
9538 decal[decalsystem->freedecal] = decal[--numdecals];
9541 decalsystem->numdecals = numdecals;
9545 // if there are no decals left, reset decalsystem
9546 R_DecalSystem_Reset(decalsystem);
9550 extern skinframe_t *decalskinframe;
9551 static void R_DrawModelDecals_Entity(entity_render_t *ent)
9554 decalsystem_t *decalsystem = &ent->decalsystem;
9563 const unsigned char *surfacevisible = ent == r_refdef.scene.worldentity ? r_refdef.viewcache.world_surfacevisible : NULL;
9566 numdecals = decalsystem->numdecals;
9570 if (r_showsurfaces.integer)
9573 if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
9575 R_DecalSystem_Reset(decalsystem);
9579 // if the model is static it doesn't matter what value we give for
9580 // wantnormals and wanttangents, so this logic uses only rules applicable
9581 // to a model, knowing that they are meaningless otherwise
9582 RSurf_ActiveModelEntity(ent, false, false, false);
9584 decalsystem->lastupdatetime = r_refdef.scene.time;
9586 faderate = 1.0f / max(0.001f, cl_decals_fadetime.value);
9588 // update vertex positions for animated models
9589 v3f = decalsystem->vertex3f;
9590 c4f = decalsystem->color4f;
9591 t2f = decalsystem->texcoord2f;
9592 for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
9594 if (!decal->color4f[0][3])
9597 if (surfacevisible && !surfacevisible[decal->surfaceindex])
9601 if (decal->triangleindex < 0 && DotProduct(r_refdef.view.origin, decal->plane) < decal->plane[3])
9604 // update color values for fading decals
9605 if (decal->lived >= cl_decals_time.value)
9606 alpha = 1 - faderate * (decal->lived - cl_decals_time.value);
9610 c4f[ 0] = decal->color4f[0][0] * alpha;
9611 c4f[ 1] = decal->color4f[0][1] * alpha;
9612 c4f[ 2] = decal->color4f[0][2] * alpha;
9614 c4f[ 4] = decal->color4f[1][0] * alpha;
9615 c4f[ 5] = decal->color4f[1][1] * alpha;
9616 c4f[ 6] = decal->color4f[1][2] * alpha;
9618 c4f[ 8] = decal->color4f[2][0] * alpha;
9619 c4f[ 9] = decal->color4f[2][1] * alpha;
9620 c4f[10] = decal->color4f[2][2] * alpha;
9623 t2f[0] = decal->texcoord2f[0][0];
9624 t2f[1] = decal->texcoord2f[0][1];
9625 t2f[2] = decal->texcoord2f[1][0];
9626 t2f[3] = decal->texcoord2f[1][1];
9627 t2f[4] = decal->texcoord2f[2][0];
9628 t2f[5] = decal->texcoord2f[2][1];
9630 // update vertex positions for animated models
9631 if (decal->triangleindex >= 0 && decal->triangleindex < rsurface.modelnumtriangles)
9633 e = rsurface.modelelement3i + 3*decal->triangleindex;
9634 VectorCopy(rsurface.modelvertex3f + 3*e[0], v3f);
9635 VectorCopy(rsurface.modelvertex3f + 3*e[1], v3f + 3);
9636 VectorCopy(rsurface.modelvertex3f + 3*e[2], v3f + 6);
9640 VectorCopy(decal->vertex3f[0], v3f);
9641 VectorCopy(decal->vertex3f[1], v3f + 3);
9642 VectorCopy(decal->vertex3f[2], v3f + 6);
9645 if (r_refdef.fogenabled)
9647 alpha = RSurf_FogVertex(v3f);
9648 VectorScale(c4f, alpha, c4f);
9649 alpha = RSurf_FogVertex(v3f + 3);
9650 VectorScale(c4f + 4, alpha, c4f + 4);
9651 alpha = RSurf_FogVertex(v3f + 6);
9652 VectorScale(c4f + 8, alpha, c4f + 8);
9663 r_refdef.stats[r_stat_drawndecals] += numtris;
9665 // now render the decals all at once
9666 // (this assumes they all use one particle font texture!)
9667 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);
9668 // R_Mesh_ResetTextureState();
9669 R_Mesh_PrepareVertices_Generic_Arrays(numtris * 3, decalsystem->vertex3f, decalsystem->color4f, decalsystem->texcoord2f);
9670 GL_DepthMask(false);
9671 GL_DepthRange(0, 1);
9672 GL_PolygonOffset(rsurface.basepolygonfactor + r_polygonoffset_decals_factor.value, rsurface.basepolygonoffset + r_polygonoffset_decals_offset.value);
9674 GL_CullFace(GL_NONE);
9675 GL_BlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
9676 R_SetupShader_Generic(decalskinframe->base, false, false, false);
9677 R_Mesh_Draw(0, numtris * 3, 0, numtris, decalsystem->element3i, NULL, 0, decalsystem->element3s, NULL, 0);
9681 static void R_DrawModelDecals(void)
9685 // fade faster when there are too many decals
9686 numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
9687 for (i = 0;i < r_refdef.scene.numentities;i++)
9688 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
9690 R_DrawModelDecals_FadeEntity(r_refdef.scene.worldentity);
9691 for (i = 0;i < r_refdef.scene.numentities;i++)
9692 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
9693 R_DrawModelDecals_FadeEntity(r_refdef.scene.entities[i]);
9695 R_DecalSystem_ApplySplatEntitiesQueue();
9697 numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
9698 for (i = 0;i < r_refdef.scene.numentities;i++)
9699 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
9701 r_refdef.stats[r_stat_totaldecals] += numdecals;
9703 if (r_showsurfaces.integer || !r_drawdecals.integer)
9706 R_DrawModelDecals_Entity(r_refdef.scene.worldentity);
9708 for (i = 0;i < r_refdef.scene.numentities;i++)
9710 if (!r_refdef.viewcache.entityvisible[i])
9712 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
9713 R_DrawModelDecals_Entity(r_refdef.scene.entities[i]);
9717 static void R_DrawDebugModel(void)
9719 entity_render_t *ent = rsurface.entity;
9721 const msurface_t *surface;
9722 model_t *model = ent->model;
9724 if (!sv.active && !cls.demoplayback && ent != r_refdef.scene.worldentity)
9727 if (r_showoverdraw.value > 0)
9729 float c = r_refdef.view.colorscale * r_showoverdraw.value * 0.125f;
9730 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
9731 R_SetupShader_Generic_NoTexture(false, false);
9732 GL_DepthTest(false);
9733 GL_DepthMask(false);
9734 GL_DepthRange(0, 1);
9735 GL_BlendFunc(GL_ONE, GL_ONE);
9736 for (j = model->submodelsurfaces_start;j < model->submodelsurfaces_end;j++)
9738 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9740 surface = model->data_surfaces + j;
9741 rsurface.texture = R_GetCurrentTexture(surface->texture);
9742 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9744 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, 1, &surface);
9745 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
9746 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
9747 GL_Color(c, 0, 0, 1.0f);
9748 else if (ent == r_refdef.scene.worldentity)
9749 GL_Color(c, c, c, 1.0f);
9751 GL_Color(0, c, 0, 1.0f);
9752 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
9756 rsurface.texture = NULL;
9759 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
9761 // R_Mesh_ResetTextureState();
9762 R_SetupShader_Generic_NoTexture(false, false);
9763 GL_DepthRange(0, 1);
9764 GL_DepthTest(!r_showdisabledepthtest.integer);
9765 GL_DepthMask(false);
9766 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9768 if (r_showcollisionbrushes.value > 0 && model->collision_bih.numleafs)
9772 qbool cullbox = false;
9773 const q3mbrush_t *brush;
9774 const bih_t *bih = &model->collision_bih;
9775 const bih_leaf_t *bihleaf;
9776 float vertex3f[3][3];
9777 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
9778 for (bihleafindex = 0, bihleaf = bih->leafs;bihleafindex < bih->numleafs;bihleafindex++, bihleaf++)
9780 if (cullbox && R_CullFrustum(bihleaf->mins, bihleaf->maxs))
9782 switch (bihleaf->type)
9785 brush = model->brush.data_brushes + bihleaf->itemindex;
9786 if (brush->colbrushf && brush->colbrushf->numtriangles)
9788 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);
9789 R_Mesh_PrepareVertices_Generic_Arrays(brush->colbrushf->numpoints, brush->colbrushf->points->v, NULL, NULL);
9790 R_Mesh_Draw(0, brush->colbrushf->numpoints, 0, brush->colbrushf->numtriangles, brush->colbrushf->elements, NULL, 0, NULL, NULL, 0);
9793 case BIH_COLLISIONTRIANGLE:
9794 triangleindex = bihleaf->itemindex;
9795 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+0], vertex3f[0]);
9796 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+1], vertex3f[1]);
9797 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+2], vertex3f[2]);
9798 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);
9799 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
9800 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
9802 case BIH_RENDERTRIANGLE:
9803 triangleindex = bihleaf->itemindex;
9804 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+0], vertex3f[0]);
9805 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+1], vertex3f[1]);
9806 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+2], vertex3f[2]);
9807 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);
9808 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
9809 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
9815 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
9818 if (r_showtris.value > 0 && qglPolygonMode)
9820 if (r_showdisabledepthtest.integer)
9822 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9823 GL_DepthMask(false);
9827 GL_BlendFunc(GL_ONE, GL_ZERO);
9830 qglPolygonMode(GL_FRONT_AND_BACK, GL_LINE);CHECKGLERROR
9831 for (j = model->submodelsurfaces_start; j < model->submodelsurfaces_end; j++)
9833 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9835 surface = model->data_surfaces + j;
9836 rsurface.texture = R_GetCurrentTexture(surface->texture);
9837 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9839 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
9840 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
9841 GL_Color(r_refdef.view.colorscale, 0, 0, r_showtris.value);
9842 else if (ent == r_refdef.scene.worldentity)
9843 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, r_showtris.value);
9845 GL_Color(0, r_refdef.view.colorscale, 0, r_showtris.value);
9846 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
9850 qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);CHECKGLERROR
9851 rsurface.texture = NULL;
9855 // FIXME! implement r_shownormals with just triangles
9856 if (r_shownormals.value != 0 && qglBegin)
9860 if (r_showdisabledepthtest.integer)
9862 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9863 GL_DepthMask(false);
9867 GL_BlendFunc(GL_ONE, GL_ZERO);
9870 for (j = model->submodelsurfaces_start; j < model->submodelsurfaces_end; j++)
9872 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9874 surface = model->data_surfaces + j;
9875 rsurface.texture = R_GetCurrentTexture(surface->texture);
9876 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9878 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
9880 if (r_shownormals.value < 0 && rsurface.batchnormal3f)
9882 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9884 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9885 GL_Color(0, 0, r_refdef.view.colorscale, 1);
9886 qglVertex3f(v[0], v[1], v[2]);
9887 VectorMA(v, -r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
9888 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9889 qglVertex3f(v[0], v[1], v[2]);
9892 if (r_shownormals.value > 0 && rsurface.batchsvector3f)
9894 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9896 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9897 GL_Color(r_refdef.view.colorscale, 0, 0, 1);
9898 qglVertex3f(v[0], v[1], v[2]);
9899 VectorMA(v, r_shownormals.value, rsurface.batchsvector3f + l * 3, v);
9900 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9901 qglVertex3f(v[0], v[1], v[2]);
9904 if (r_shownormals.value > 0 && rsurface.batchtvector3f)
9906 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9908 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9909 GL_Color(0, r_refdef.view.colorscale, 0, 1);
9910 qglVertex3f(v[0], v[1], v[2]);
9911 VectorMA(v, r_shownormals.value, rsurface.batchtvector3f + l * 3, v);
9912 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9913 qglVertex3f(v[0], v[1], v[2]);
9916 if (r_shownormals.value > 0 && rsurface.batchnormal3f)
9918 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9920 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9921 GL_Color(0, 0, r_refdef.view.colorscale, 1);
9922 qglVertex3f(v[0], v[1], v[2]);
9923 VectorMA(v, r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
9924 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9925 qglVertex3f(v[0], v[1], v[2]);
9932 rsurface.texture = NULL;
9938 int r_maxsurfacelist = 0;
9939 const msurface_t **r_surfacelist = NULL;
9940 void R_DrawModelSurfaces(entity_render_t *ent, qbool skysurfaces, qbool writedepth, qbool depthonly, qbool debug, qbool prepass, qbool ui)
9942 int i, j, flagsmask;
9943 model_t *model = ent->model;
9944 msurface_t *surfaces;
9945 unsigned char *update;
9946 int numsurfacelist = 0;
9950 if (r_maxsurfacelist < model->num_surfaces)
9952 r_maxsurfacelist = model->num_surfaces;
9954 Mem_Free((msurface_t **)r_surfacelist);
9955 r_surfacelist = (const msurface_t **) Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
9958 if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
9959 RSurf_ActiveModelEntity(ent, false, false, false);
9961 RSurf_ActiveModelEntity(ent, true, true, true);
9963 RSurf_ActiveModelEntity(ent, model->wantnormals, model->wanttangents, false);
9965 RSurf_ActiveModelEntity(ent, true, true, false);
9967 surfaces = model->data_surfaces;
9968 update = model->brushq1.lightmapupdateflags;
9970 flagsmask = skysurfaces ? MATERIALFLAG_SKY : MATERIALFLAG_WALL;
9975 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
9979 // check if this is an empty model
9980 if (model->submodelsurfaces_start >= model->submodelsurfaces_end)
9983 rsurface.lightmaptexture = NULL;
9984 rsurface.deluxemaptexture = NULL;
9985 rsurface.uselightmaptexture = false;
9986 rsurface.texture = NULL;
9987 rsurface.rtlight = NULL;
9990 // add visible surfaces to draw list
9991 if (ent == r_refdef.scene.worldentity)
9993 // for the world entity, check surfacevisible
9994 for (i = model->submodelsurfaces_start;i < model->submodelsurfaces_end;i++)
9996 j = model->modelsurfaces_sorted[i];
9997 if (r_refdef.viewcache.world_surfacevisible[j])
9998 r_surfacelist[numsurfacelist++] = surfaces + j;
10001 // don't do anything if there were no surfaces added (none of the world entity is visible)
10002 if (!numsurfacelist)
10004 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
10010 // for ui we have to preserve the order of surfaces (not using modelsurfaces_sorted)
10011 for (i = model->submodelsurfaces_start; i < model->submodelsurfaces_end; i++)
10012 r_surfacelist[numsurfacelist++] = surfaces + i;
10016 // add all surfaces
10017 for (i = model->submodelsurfaces_start; i < model->submodelsurfaces_end; i++)
10018 r_surfacelist[numsurfacelist++] = surfaces + model->modelsurfaces_sorted[i];
10022 * Mark lightmaps as dirty if their lightstyle's value changed. We do this by
10023 * using style chains because most styles do not change on most frames, and most
10024 * surfaces do not have styles on them. Mods like Arcane Dimensions (e.g. ad_necrokeep)
10025 * break this rule and animate most surfaces.
10027 if (update && !skysurfaces && !depthonly && !prepass && model->brushq1.num_lightstyles && r_refdef.scene.lightmapintensity > 0 && r_q1bsp_lightmap_updates_enabled.integer)
10029 model_brush_lightstyleinfo_t *style;
10031 // For each lightstyle, check if its value changed and mark the lightmaps as dirty if so
10032 for (i = 0, style = model->brushq1.data_lightstyleinfo; i < model->brushq1.num_lightstyles; i++, style++)
10034 if (style->value != r_refdef.scene.lightstylevalue[style->style])
10036 int* list = style->surfacelist;
10037 style->value = r_refdef.scene.lightstylevalue[style->style];
10038 // Value changed - mark the surfaces belonging to this style chain as dirty
10039 for (j = 0; j < style->numsurfaces; j++)
10040 update[list[j]] = true;
10043 // Now check if update flags are set on any surfaces that are visible
10044 if (r_q1bsp_lightmap_updates_hidden_surfaces.integer)
10047 * We can do less frequent texture uploads (approximately 10hz for animated
10048 * lightstyles) by rebuilding lightmaps on surfaces that are not currently visible.
10049 * For optimal efficiency, this includes the submodels of the worldmodel, so we
10050 * use model->num_surfaces, not nummodelsurfaces.
10052 for (i = 0; i < model->num_surfaces;i++)
10054 R_BuildLightMap(ent, surfaces + i, r_q1bsp_lightmap_updates_combine.integer);
10058 for (i = 0; i < numsurfacelist; i++)
10059 if (update[r_surfacelist[i] - surfaces])
10060 R_BuildLightMap(ent, (msurface_t *)r_surfacelist[i], r_q1bsp_lightmap_updates_combine.integer);
10064 R_QueueModelSurfaceList(ent, numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly, prepass, ui);
10066 // add to stats if desired
10067 if (r_speeds.integer && !skysurfaces && !depthonly)
10069 r_refdef.stats[r_stat_entities_surfaces] += numsurfacelist;
10070 for (j = 0;j < numsurfacelist;j++)
10071 r_refdef.stats[r_stat_entities_triangles] += r_surfacelist[j]->num_triangles;
10074 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
10077 void R_DebugLine(vec3_t start, vec3_t end)
10079 model_t *mod = CL_Mesh_UI();
10081 int e0, e1, e2, e3;
10082 float offsetx, offsety, x1, y1, x2, y2, width = 1.0f;
10083 float r1 = 1.0f, g1 = 0.0f, b1 = 0.0f, alpha1 = 0.25f;
10084 float r2 = 1.0f, g2 = 1.0f, b2 = 0.0f, alpha2 = 0.25f;
10087 // transform to screen coords first
10088 Vector4Set(w[0], start[0], start[1], start[2], 1);
10089 Vector4Set(w[1], end[0], end[1], end[2], 1);
10090 R_Viewport_TransformToScreen(&r_refdef.view.viewport, w[0], s[0]);
10091 R_Viewport_TransformToScreen(&r_refdef.view.viewport, w[1], s[1]);
10092 x1 = s[0][0] * vid_conwidth.value / vid.width;
10093 y1 = (vid.height - s[0][1]) * vid_conheight.value / vid.height;
10094 x2 = s[1][0] * vid_conwidth.value / vid.width;
10095 y2 = (vid.height - s[1][1]) * vid_conheight.value / vid.height;
10096 //Con_DPrintf("R_DebugLine: %.0f,%.0f to %.0f,%.0f\n", x1, y1, x2, y2);
10098 // add the line to the UI mesh for drawing later
10100 // width is measured in real pixels
10101 if (fabs(x2 - x1) > fabs(y2 - y1))
10104 offsety = 0.5f * width * vid_conheight.value / vid.height;
10108 offsetx = 0.5f * width * vid_conwidth.value / vid.width;
10111 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);
10112 e0 = Mod_Mesh_IndexForVertex(mod, surf, x1 - offsetx, y1 - offsety, 10, 0, 0, -1, 0, 0, 0, 0, r1, g1, b1, alpha1);
10113 e1 = Mod_Mesh_IndexForVertex(mod, surf, x2 - offsetx, y2 - offsety, 10, 0, 0, -1, 0, 0, 0, 0, r2, g2, b2, alpha2);
10114 e2 = Mod_Mesh_IndexForVertex(mod, surf, x2 + offsetx, y2 + offsety, 10, 0, 0, -1, 0, 0, 0, 0, r2, g2, b2, alpha2);
10115 e3 = Mod_Mesh_IndexForVertex(mod, surf, x1 + offsetx, y1 + offsety, 10, 0, 0, -1, 0, 0, 0, 0, r1, g1, b1, alpha1);
10116 Mod_Mesh_AddTriangle(mod, surf, e0, e1, e2);
10117 Mod_Mesh_AddTriangle(mod, surf, e0, e2, e3);
10122 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)
10124 static texture_t texture;
10126 // fake enough texture and surface state to render this geometry
10128 texture.update_lastrenderframe = -1; // regenerate this texture
10129 texture.basematerialflags = materialflags | MATERIALFLAG_CUSTOMSURFACE | MATERIALFLAG_WALL;
10130 texture.basealpha = 1.0f;
10131 texture.currentskinframe = skinframe;
10132 texture.currenttexmatrix = *texmatrix; // requires MATERIALFLAG_CUSTOMSURFACE
10133 texture.offsetmapping = OFFSETMAPPING_OFF;
10134 texture.offsetscale = 1;
10135 texture.specularscalemod = 1;
10136 texture.specularpowermod = 1;
10137 texture.transparentsort = TRANSPARENTSORT_DISTANCE;
10139 R_DrawCustomSurface_Texture(&texture, texmatrix, materialflags, firstvertex, numvertices, firsttriangle, numtriangles, writedepth, prepass, ui);
10142 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)
10144 static msurface_t surface;
10145 const msurface_t *surfacelist = &surface;
10147 // fake enough texture and surface state to render this geometry
10148 surface.texture = texture;
10149 surface.num_triangles = numtriangles;
10150 surface.num_firsttriangle = firsttriangle;
10151 surface.num_vertices = numvertices;
10152 surface.num_firstvertex = firstvertex;
10155 rsurface.texture = R_GetCurrentTexture(surface.texture);
10156 rsurface.lightmaptexture = NULL;
10157 rsurface.deluxemaptexture = NULL;
10158 rsurface.uselightmaptexture = false;
10159 R_DrawModelTextureSurfaceList(1, &surfacelist, writedepth, prepass, ui);