2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "cl_collision.h"
32 // Enable NVIDIA High Performance Graphics while using Integrated Graphics.
36 __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
42 mempool_t *r_main_mempool;
43 rtexturepool_t *r_main_texturepool;
45 int r_textureframe = 0; ///< used only by R_GetCurrentTexture, incremented per view and per UI render
47 static qbool r_loadnormalmap;
48 static qbool r_loadgloss;
50 static qbool r_loaddds;
51 static qbool r_savedds;
52 static qbool r_gpuskeletal;
59 cvar_t r_motionblur = {CF_CLIENT | CF_ARCHIVE, "r_motionblur", "0", "screen motionblur - value represents intensity, somewhere around 0.5 recommended - NOTE: bad performance on multi-gpu!"};
60 cvar_t r_damageblur = {CF_CLIENT | CF_ARCHIVE, "r_damageblur", "0", "screen motionblur based on damage - value represents intensity, somewhere around 0.5 recommended - NOTE: bad performance on multi-gpu!"};
61 cvar_t r_motionblur_averaging = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_averaging", "0.1", "sliding average reaction time for velocity (higher = slower adaption to change)"};
62 cvar_t r_motionblur_randomize = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_randomize", "0.1", "randomizing coefficient to workaround ghosting"};
63 cvar_t r_motionblur_minblur = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_minblur", "0.5", "factor of blur to apply at all times (always have this amount of blur no matter what the other factors are)"};
64 cvar_t r_motionblur_maxblur = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_maxblur", "0.9", "maxmimum amount of blur"};
65 cvar_t r_motionblur_velocityfactor = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_velocityfactor", "1", "factoring in of player velocity to the blur equation - the faster the player moves around the map, the more blur they get"};
66 cvar_t r_motionblur_velocityfactor_minspeed = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_velocityfactor_minspeed", "400", "lower value of velocity when it starts to factor into blur equation"};
67 cvar_t r_motionblur_velocityfactor_maxspeed = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_velocityfactor_maxspeed", "800", "upper value of velocity when it reaches the peak factor into blur equation"};
68 cvar_t r_motionblur_mousefactor = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_mousefactor", "2", "factoring in of mouse acceleration to the blur equation - the faster the player turns their mouse, the more blur they get"};
69 cvar_t r_motionblur_mousefactor_minspeed = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_mousefactor_minspeed", "0", "lower value of mouse acceleration when it starts to factor into blur equation"};
70 cvar_t r_motionblur_mousefactor_maxspeed = {CF_CLIENT | CF_ARCHIVE, "r_motionblur_mousefactor_maxspeed", "50", "upper value of mouse acceleration when it reaches the peak factor into blur equation"};
72 cvar_t r_depthfirst = {CF_CLIENT | CF_ARCHIVE, "r_depthfirst", "0", "renders a depth-only version of the scene before normal rendering begins to eliminate overdraw, values: 0 = off, 1 = world depth, 2 = world and model depth"};
73 cvar_t r_useinfinitefarclip = {CF_CLIENT | CF_ARCHIVE, "r_useinfinitefarclip", "1", "enables use of a special kind of projection matrix that has an extremely large farclip"};
74 cvar_t r_farclip_base = {CF_CLIENT, "r_farclip_base", "65536", "farclip (furthest visible distance) for rendering when r_useinfinitefarclip is 0"};
75 cvar_t r_farclip_world = {CF_CLIENT, "r_farclip_world", "2", "adds map size to farclip multiplied by this value"};
76 cvar_t r_nearclip = {CF_CLIENT, "r_nearclip", "1", "distance from camera of nearclip plane" };
77 cvar_t r_deformvertexes = {CF_CLIENT, "r_deformvertexes", "1", "allows use of deformvertexes in shader files (can be turned off to check performance impact)"};
78 cvar_t r_transparent = {CF_CLIENT, "r_transparent", "1", "allows use of transparent surfaces (can be turned off to check performance impact)"};
79 cvar_t r_transparent_alphatocoverage = {CF_CLIENT, "r_transparent_alphatocoverage", "1", "enables GL_ALPHA_TO_COVERAGE antialiasing technique on alphablend and alphatest surfaces when using vid_samples 2 or higher"};
80 cvar_t r_transparent_sortsurfacesbynearest = {CF_CLIENT, "r_transparent_sortsurfacesbynearest", "1", "sort entity and world surfaces by nearest point on bounding box instead of using the center of the bounding box, usually reduces sorting artifacts"};
81 cvar_t r_transparent_useplanardistance = {CF_CLIENT, "r_transparent_useplanardistance", "0", "sort transparent meshes by distance from view plane rather than spherical distance to the chosen point"};
82 cvar_t r_showoverdraw = {CF_CLIENT, "r_showoverdraw", "0", "shows overlapping geometry"};
83 cvar_t r_showbboxes = {CF_CLIENT, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%, 10 = 100%)"};
84 cvar_t r_showbboxes_client = {CF_CLIENT, "r_showbboxes_client", "0", "shows bounding boxes of clientside qc entities, value controls opacity scaling (1 = 10%, 10 = 100%)"};
85 cvar_t r_showsurfaces = {CF_CLIENT, "r_showsurfaces", "0", "1 shows surfaces as different colors, or a value of 2 shows triangle draw order (for analyzing whether meshes are optimized for vertex cache)"};
86 cvar_t r_showtris = {CF_CLIENT, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
87 cvar_t r_shownormals = {CF_CLIENT, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
88 cvar_t r_showlighting = {CF_CLIENT, "r_showlighting", "0", "shows areas lit by lights, useful for finding out why some areas of a map render slowly (bright orange = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
89 cvar_t r_showcollisionbrushes = {CF_CLIENT, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
90 cvar_t r_showcollisionbrushes_polygonfactor = {CF_CLIENT, "r_showcollisionbrushes_polygonfactor", "-1", "expands outward the brush polygons a little bit, used to make collision brushes appear infront of walls"};
91 cvar_t r_showcollisionbrushes_polygonoffset = {CF_CLIENT, "r_showcollisionbrushes_polygonoffset", "0", "nudges brush polygon depth in hardware depth units, used to make collision brushes appear infront of walls"};
92 cvar_t r_showdisabledepthtest = {CF_CLIENT, "r_showdisabledepthtest", "0", "disables depth testing on r_show* cvars, allowing you to see what hidden geometry the graphics card is processing"};
93 cvar_t r_showspriteedges = {CF_CLIENT, "r_showspriteedges", "0", "renders a debug outline to show the polygon shape of each sprite frame rendered (may be 2 or more in case of interpolated animations), for debugging rendering bugs with specific view types"};
94 cvar_t r_showparticleedges = {CF_CLIENT, "r_showparticleedges", "0", "renders a debug outline to show the polygon shape of each particle, for debugging rendering bugs with specific view types"};
95 cvar_t r_drawportals = {CF_CLIENT, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
96 cvar_t r_drawentities = {CF_CLIENT, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
97 cvar_t r_draw2d = {CF_CLIENT, "r_draw2d","1", "draw 2D stuff (dangerous to turn off)"};
98 cvar_t r_drawworld = {CF_CLIENT, "r_drawworld","1", "draw world (most static stuff)"};
99 cvar_t r_drawviewmodel = {CF_CLIENT, "r_drawviewmodel","1", "draw your weapon model"};
100 cvar_t r_drawexteriormodel = {CF_CLIENT, "r_drawexteriormodel","1", "draw your player model (e.g. in chase cam, reflections)"};
101 cvar_t r_cullentities_trace = {CF_CLIENT, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
102 cvar_t r_cullentities_trace_entityocclusion = {CF_CLIENT, "r_cullentities_trace_entityocclusion", "1", "check for occluding entities such as doors, not just world hull"};
103 cvar_t r_cullentities_trace_samples = {CF_CLIENT, "r_cullentities_trace_samples", "2", "number of samples to test for entity culling (in addition to center sample)"};
104 cvar_t r_cullentities_trace_tempentitysamples = {CF_CLIENT, "r_cullentities_trace_tempentitysamples", "-1", "number of samples to test for entity culling of temp entities (including all CSQC entities), -1 disables trace culling on these entities to prevent flicker (pvs still applies)"};
105 cvar_t r_cullentities_trace_enlarge = {CF_CLIENT, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
106 cvar_t r_cullentities_trace_expand = {CF_CLIENT, "r_cullentities_trace_expand", "0", "box expanded by this many units for entity culling"};
107 cvar_t r_cullentities_trace_pad = {CF_CLIENT, "r_cullentities_trace_pad", "8", "accept traces that hit within this many units of the box"};
108 cvar_t r_cullentities_trace_delay = {CF_CLIENT, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
109 cvar_t r_cullentities_trace_eyejitter = {CF_CLIENT, "r_cullentities_trace_eyejitter", "16", "randomly offset rays from the eye by this much to reduce the odds of flickering"};
110 cvar_t r_sortentities = {CF_CLIENT, "r_sortentities", "0", "sort entities before drawing (might be faster)"};
111 cvar_t r_speeds = {CF_CLIENT, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
112 cvar_t r_fullbright = {CF_CLIENT, "r_fullbright","0", "makes map very bright and renders faster"};
114 cvar_t r_fullbright_directed = {CF_CLIENT, "r_fullbright_directed", "0", "render fullbright things (unlit worldmodel and EF_FULLBRIGHT entities, but not fullbright shaders) using a constant light direction instead to add more depth while keeping uniform brightness"};
115 cvar_t r_fullbright_directed_ambient = {CF_CLIENT, "r_fullbright_directed_ambient", "0.5", "ambient light multiplier for directed fullbright"};
116 cvar_t r_fullbright_directed_diffuse = {CF_CLIENT, "r_fullbright_directed_diffuse", "0.75", "diffuse light multiplier for directed fullbright"};
117 cvar_t r_fullbright_directed_pitch = {CF_CLIENT, "r_fullbright_directed_pitch", "20", "constant pitch direction ('height') of the fake light source to use for fullbright"};
118 cvar_t r_fullbright_directed_pitch_relative = {CF_CLIENT, "r_fullbright_directed_pitch_relative", "0", "whether r_fullbright_directed_pitch is interpreted as absolute (0) or relative (1) pitch"};
120 cvar_t r_wateralpha = {CF_CLIENT | CF_ARCHIVE, "r_wateralpha","1", "opacity of water polygons"};
121 cvar_t r_dynamic = {CF_CLIENT | CF_ARCHIVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
122 cvar_t r_fullbrights = {CF_CLIENT | CF_ARCHIVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
123 cvar_t r_shadows = {CF_CLIENT | CF_ARCHIVE, "r_shadows", "0", "casts fake stencil shadows from models onto the world (rtlights are unaffected by this); when set to 2, always cast the shadows in the direction set by r_shadows_throwdirection, otherwise use the model lighting."};
124 cvar_t r_shadows_darken = {CF_CLIENT | CF_ARCHIVE, "r_shadows_darken", "0.5", "how much shadowed areas will be darkened"};
125 cvar_t r_shadows_throwdistance = {CF_CLIENT | CF_ARCHIVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
126 cvar_t r_shadows_throwdirection = {CF_CLIENT | CF_ARCHIVE, "r_shadows_throwdirection", "0 0 -1", "override throwing direction for r_shadows 2"};
127 cvar_t r_shadows_drawafterrtlighting = {CF_CLIENT | CF_ARCHIVE, "r_shadows_drawafterrtlighting", "0", "draw fake shadows AFTER realtime lightning is drawn. May be useful for simulating fast sunlight on large outdoor maps with only one noshadow rtlight. The price is less realistic appearance of dynamic light shadows."};
128 cvar_t r_shadows_castfrombmodels = {CF_CLIENT | CF_ARCHIVE, "r_shadows_castfrombmodels", "0", "do cast shadows from bmodels"};
129 cvar_t r_shadows_focus = {CF_CLIENT | CF_ARCHIVE, "r_shadows_focus", "0 0 0", "offset the shadowed area focus"};
130 cvar_t r_shadows_shadowmapscale = {CF_CLIENT | CF_ARCHIVE, "r_shadows_shadowmapscale", "0.25", "higher values increase shadowmap quality at a cost of area covered (multiply global shadowmap precision) for fake shadows. Needs shadowmapping ON."};
131 cvar_t r_shadows_shadowmapbias = {CF_CLIENT | CF_ARCHIVE, "r_shadows_shadowmapbias", "-1", "sets shadowmap bias for fake shadows. -1 sets the value of r_shadow_shadowmapping_bias. Needs shadowmapping ON."};
132 cvar_t r_q1bsp_skymasking = {CF_CLIENT, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
133 cvar_t r_polygonoffset_submodel_factor = {CF_CLIENT, "r_polygonoffset_submodel_factor", "0", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
134 cvar_t r_polygonoffset_submodel_offset = {CF_CLIENT, "r_polygonoffset_submodel_offset", "14", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
135 cvar_t r_polygonoffset_decals_factor = {CF_CLIENT, "r_polygonoffset_decals_factor", "0", "biases depth values of decals to prevent z-fighting artifacts"};
136 cvar_t r_polygonoffset_decals_offset = {CF_CLIENT, "r_polygonoffset_decals_offset", "-14", "biases depth values of decals to prevent z-fighting artifacts"};
137 cvar_t r_fog_exp2 = {CF_CLIENT, "r_fog_exp2", "0", "uses GL_EXP2 fog (as in Nehahra) rather than realistic GL_EXP fog"};
138 cvar_t r_fog_clear = {CF_CLIENT, "r_fog_clear", "1", "clears renderbuffer with fog color before render starts"};
139 cvar_t r_drawfog = {CF_CLIENT | CF_ARCHIVE, "r_drawfog", "1", "allows one to disable fog rendering"};
140 cvar_t r_transparentdepthmasking = {CF_CLIENT | CF_ARCHIVE, "r_transparentdepthmasking", "0", "enables depth writes on transparent meshes whose materially is normally opaque, this prevents seeing the inside of a transparent mesh"};
141 cvar_t r_transparent_sortmindist = {CF_CLIENT | CF_ARCHIVE, "r_transparent_sortmindist", "0", "lower distance limit for transparent sorting"};
142 cvar_t r_transparent_sortmaxdist = {CF_CLIENT | CF_ARCHIVE, "r_transparent_sortmaxdist", "32768", "upper distance limit for transparent sorting"};
143 cvar_t r_transparent_sortarraysize = {CF_CLIENT | CF_ARCHIVE, "r_transparent_sortarraysize", "4096", "number of distance-sorting layers"};
144 cvar_t r_celshading = {CF_CLIENT | CF_ARCHIVE, "r_celshading", "0", "cartoon-style light shading (OpenGL 2.x only)"}; // FIXME remove OpenGL 2.x only once implemented for DX9
145 cvar_t r_celoutlines = {CF_CLIENT | CF_ARCHIVE, "r_celoutlines", "0", "cartoon-style outlines (requires r_shadow_deferred)"};
147 cvar_t gl_fogenable = {CF_CLIENT, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
148 cvar_t gl_fogdensity = {CF_CLIENT, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
149 cvar_t gl_fogred = {CF_CLIENT, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
150 cvar_t gl_foggreen = {CF_CLIENT, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
151 cvar_t gl_fogblue = {CF_CLIENT, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
152 cvar_t gl_fogstart = {CF_CLIENT, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
153 cvar_t gl_fogend = {CF_CLIENT, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
154 cvar_t gl_skyclip = {CF_CLIENT, "gl_skyclip", "4608", "nehahra farclip distance - the real fog end (for Nehahra compatibility only)"};
156 cvar_t r_texture_dds_load = {CF_CLIENT | CF_ARCHIVE, "r_texture_dds_load", "0", "load compressed dds/filename.dds texture instead of filename.tga, if the file exists (requires driver support)"};
157 cvar_t r_texture_dds_save = {CF_CLIENT | CF_ARCHIVE, "r_texture_dds_save", "0", "save compressed dds/filename.dds texture when filename.tga is loaded, so that it can be loaded instead next time"};
159 cvar_t r_usedepthtextures = {CF_CLIENT | CF_ARCHIVE, "r_usedepthtextures", "1", "use depth texture instead of depth renderbuffer where possible, uses less video memory but may render slower (or faster) depending on hardware"};
160 cvar_t r_viewfbo = {CF_CLIENT | CF_ARCHIVE, "r_viewfbo", "0", "enables use of an 8bit (1) or 16bit (2) or 32bit (3) per component float framebuffer render, which may be at a different resolution than the video mode"};
161 cvar_t r_rendertarget_debug = {CF_CLIENT, "r_rendertarget_debug", "-1", "replaces the view with the contents of the specified render target (by number - note that these can fluctuate depending on scene)"};
162 cvar_t r_viewscale = {CF_CLIENT | CF_ARCHIVE, "r_viewscale", "1", "scaling factor for resolution of the fbo rendering method, must be > 0, can be above 1 for a costly antialiasing behavior, typical values are 0.5 for 1/4th as many pixels rendered, or 1 for normal rendering"};
163 cvar_t r_viewscale_fpsscaling = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling", "0", "change resolution based on framerate"};
164 cvar_t r_viewscale_fpsscaling_min = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_min", "0.0625", "worst acceptable quality"};
165 cvar_t r_viewscale_fpsscaling_multiply = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_multiply", "5", "adjust quality up or down by the frametime difference from 1.0/target, multiplied by this factor"};
166 cvar_t r_viewscale_fpsscaling_stepsize = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_stepsize", "0.01", "smallest adjustment to hit the target framerate (this value prevents minute oscillations)"};
167 cvar_t r_viewscale_fpsscaling_stepmax = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_stepmax", "1.00", "largest adjustment to hit the target framerate (this value prevents wild overshooting of the estimate)"};
168 cvar_t r_viewscale_fpsscaling_target = {CF_CLIENT | CF_ARCHIVE, "r_viewscale_fpsscaling_target", "70", "desired framerate"};
170 cvar_t r_glsl_skeletal = {CF_CLIENT | CF_ARCHIVE, "r_glsl_skeletal", "1", "render skeletal models faster using a gpu-skinning technique"};
171 cvar_t r_glsl_deluxemapping = {CF_CLIENT | CF_ARCHIVE, "r_glsl_deluxemapping", "1", "use per pixel lighting on deluxemap-compiled q3bsp maps (or a value of 2 forces deluxemap shading even without deluxemaps)"};
172 cvar_t r_glsl_offsetmapping = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
173 cvar_t r_glsl_offsetmapping_steps = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_steps", "2", "offset mapping steps (note: too high values may be not supported by your GPU)"};
174 cvar_t r_glsl_offsetmapping_reliefmapping = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
175 cvar_t r_glsl_offsetmapping_reliefmapping_steps = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_reliefmapping_steps", "10", "relief mapping steps (note: too high values may be not supported by your GPU)"};
176 cvar_t r_glsl_offsetmapping_reliefmapping_refinesteps = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_reliefmapping_refinesteps", "5", "relief mapping refine steps (these are a binary search executed as the last step as given by r_glsl_offsetmapping_reliefmapping_steps)"};
177 cvar_t r_glsl_offsetmapping_scale = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
178 cvar_t r_glsl_offsetmapping_lod = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_lod", "0", "apply distance-based level-of-detail correction to number of offsetmappig steps, effectively making it render faster on large open-area maps"};
179 cvar_t r_glsl_offsetmapping_lod_distance = {CF_CLIENT | CF_ARCHIVE, "r_glsl_offsetmapping_lod_distance", "32", "first LOD level distance, second level (-50% steps) is 2x of this, third (33%) - 3x etc."};
180 cvar_t r_glsl_postprocess = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess", "0", "use a GLSL postprocessing shader"};
181 cvar_t r_glsl_postprocess_uservec1 = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec1", "0 0 0 0", "a 4-component vector to pass as uservec1 to the postprocessing shader (only useful if default.glsl has been customized)"};
182 cvar_t r_glsl_postprocess_uservec2 = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec2", "0 0 0 0", "a 4-component vector to pass as uservec2 to the postprocessing shader (only useful if default.glsl has been customized)"};
183 cvar_t r_glsl_postprocess_uservec3 = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec3", "0 0 0 0", "a 4-component vector to pass as uservec3 to the postprocessing shader (only useful if default.glsl has been customized)"};
184 cvar_t r_glsl_postprocess_uservec4 = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec4", "0 0 0 0", "a 4-component vector to pass as uservec4 to the postprocessing shader (only useful if default.glsl has been customized)"};
185 cvar_t r_glsl_postprocess_uservec1_enable = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec1_enable", "1", "enables postprocessing uservec1 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
186 cvar_t r_glsl_postprocess_uservec2_enable = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec2_enable", "1", "enables postprocessing uservec2 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
187 cvar_t r_glsl_postprocess_uservec3_enable = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec3_enable", "1", "enables postprocessing uservec3 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
188 cvar_t r_glsl_postprocess_uservec4_enable = {CF_CLIENT | CF_ARCHIVE, "r_glsl_postprocess_uservec4_enable", "1", "enables postprocessing uservec4 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
189 cvar_t r_colorfringe = {CF_CLIENT | CF_ARCHIVE, "r_colorfringe", "0", "Chromatic aberration. Values higher than 0.025 will noticeably distort the image"};
190 cvar_t r_fxaa = {CF_CLIENT | CF_ARCHIVE, "r_fxaa", "0", "fast approximate anti aliasing"};
192 cvar_t r_water = {CF_CLIENT | CF_ARCHIVE, "r_water", "0", "whether to use reflections and refraction on water surfaces (note: r_wateralpha must be set below 1)"};
193 cvar_t r_water_cameraentitiesonly = {CF_CLIENT | CF_ARCHIVE, "r_water_cameraentitiesonly", "0", "whether to only show QC-defined reflections/refractions (typically used for camera- or portal-like effects)"};
194 cvar_t r_water_clippingplanebias = {CF_CLIENT | CF_ARCHIVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
195 cvar_t r_water_resolutionmultiplier = {CF_CLIENT | CF_ARCHIVE, "r_water_resolutionmultiplier", "0.5", "multiplier for screen resolution when rendering refracted/reflected scenes, 1 is full quality, lower values are faster"};
196 cvar_t r_water_refractdistort = {CF_CLIENT | CF_ARCHIVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
197 cvar_t r_water_reflectdistort = {CF_CLIENT | CF_ARCHIVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
198 cvar_t r_water_scissormode = {CF_CLIENT, "r_water_scissormode", "3", "scissor (1) or cull (2) or both (3) water renders"};
199 cvar_t r_water_lowquality = {CF_CLIENT, "r_water_lowquality", "0", "special option to accelerate water rendering: 1 disables all dynamic lights, 2 disables particles too"};
200 cvar_t r_water_hideplayer = {CF_CLIENT | CF_ARCHIVE, "r_water_hideplayer", "0", "if set to 1 then player will be hidden in refraction views, if set to 2 then player will also be hidden in reflection views, player is always visible in camera views"};
202 cvar_t r_lerpsprites = {CF_CLIENT | CF_ARCHIVE, "r_lerpsprites", "0", "enables animation smoothing on sprites"};
203 cvar_t r_lerpmodels = {CF_CLIENT | CF_ARCHIVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
204 cvar_t r_nolerp_list = {CF_CLIENT | CF_ARCHIVE, "r_nolerp_list", "progs/v_nail.mdl,progs/v_nail2.mdl,progs/flame.mdl,progs/flame2.mdl,progs/braztall.mdl,progs/brazshrt.mdl,progs/longtrch.mdl,progs/flame_pyre.mdl,progs/v_saw.mdl,progs/v_xfist.mdl,progs/h2stuff/newfire.mdl", "comma separated list of models that will not have their animations smoothed"};
205 cvar_t r_lerplightstyles = {CF_CLIENT | CF_ARCHIVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
206 cvar_t r_waterscroll = {CF_CLIENT | CF_ARCHIVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
208 cvar_t r_bloom = {CF_CLIENT | CF_ARCHIVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
209 cvar_t r_bloom_colorscale = {CF_CLIENT | CF_ARCHIVE, "r_bloom_colorscale", "1", "how bright the glow is"};
211 cvar_t r_bloom_brighten = {CF_CLIENT | CF_ARCHIVE, "r_bloom_brighten", "1", "how bright the glow is, after subtract/power"};
212 cvar_t r_bloom_blur = {CF_CLIENT | CF_ARCHIVE, "r_bloom_blur", "4", "how large the glow is"};
213 cvar_t r_bloom_resolution = {CF_CLIENT | CF_ARCHIVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
214 cvar_t r_bloom_colorexponent = {CF_CLIENT | CF_ARCHIVE, "r_bloom_colorexponent", "1", "how exaggerated the glow is"};
215 cvar_t r_bloom_colorsubtract = {CF_CLIENT | CF_ARCHIVE, "r_bloom_colorsubtract", "0.1", "reduces bloom colors by a certain amount"};
216 cvar_t r_bloom_scenebrightness = {CF_CLIENT | CF_ARCHIVE, "r_bloom_scenebrightness", "1", "global rendering brightness when bloom is enabled"};
218 cvar_t r_hdr_scenebrightness = {CF_CLIENT | CF_ARCHIVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
219 cvar_t r_hdr_glowintensity = {CF_CLIENT | CF_ARCHIVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
220 cvar_t r_hdr_irisadaptation = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation", "0", "adjust scene brightness according to light intensity at player location"};
221 cvar_t r_hdr_irisadaptation_multiplier = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_multiplier", "2", "brightness at which value will be 1.0"};
222 cvar_t r_hdr_irisadaptation_minvalue = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_minvalue", "0.5", "minimum value that can result from multiplier / brightness"};
223 cvar_t r_hdr_irisadaptation_maxvalue = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_maxvalue", "4", "maximum value that can result from multiplier / brightness"};
224 cvar_t r_hdr_irisadaptation_value = {CF_CLIENT, "r_hdr_irisadaptation_value", "1", "current value as scenebrightness multiplier, changes continuously when irisadaptation is active"};
225 cvar_t r_hdr_irisadaptation_fade_up = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_fade_up", "0.1", "fade rate at which value adjusts to darkness"};
226 cvar_t r_hdr_irisadaptation_fade_down = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_fade_down", "0.5", "fade rate at which value adjusts to brightness"};
227 cvar_t r_hdr_irisadaptation_radius = {CF_CLIENT | CF_ARCHIVE, "r_hdr_irisadaptation_radius", "15", "lighting within this many units of the eye is averaged"};
229 cvar_t r_smoothnormals_areaweighting = {CF_CLIENT, "r_smoothnormals_areaweighting", "1", "uses significantly faster (and supposedly higher quality) area-weighted vertex normals and tangent vectors rather than summing normalized triangle normals and tangents"};
231 cvar_t developer_texturelogging = {CF_CLIENT, "developer_texturelogging", "0", "produces a textures.log file containing names of skins and map textures the engine tried to load"};
233 cvar_t gl_lightmaps = {CF_CLIENT, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers), a value of 2 keeps normalmap shading"};
235 cvar_t r_test = {CF_CLIENT, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
237 cvar_t r_batch_multidraw = {CF_CLIENT | CF_ARCHIVE, "r_batch_multidraw", "1", "issue multiple glDrawElements calls when rendering a batch of surfaces with the same texture (otherwise the index data is copied to make it one draw)"};
238 cvar_t r_batch_multidraw_mintriangles = {CF_CLIENT | CF_ARCHIVE, "r_batch_multidraw_mintriangles", "0", "minimum number of triangles to activate multidraw path (copying small groups of triangles may be faster)"};
239 cvar_t r_batch_debugdynamicvertexpath = {CF_CLIENT | CF_ARCHIVE, "r_batch_debugdynamicvertexpath", "0", "force the dynamic batching code path for debugging purposes"};
240 cvar_t r_batch_dynamicbuffer = {CF_CLIENT | CF_ARCHIVE, "r_batch_dynamicbuffer", "0", "use vertex/index buffers for drawing dynamic and copytriangles batches"};
242 cvar_t r_glsl_saturation = {CF_CLIENT | CF_ARCHIVE, "r_glsl_saturation", "1", "saturation multiplier (only working in glsl!)"};
243 cvar_t r_glsl_saturation_redcompensate = {CF_CLIENT | CF_ARCHIVE, "r_glsl_saturation_redcompensate", "0", "a 'vampire sight' addition to desaturation effect, does compensation for red color, r_glsl_restart is required"};
245 cvar_t r_glsl_vertextextureblend_usebothalphas = {CF_CLIENT | CF_ARCHIVE, "r_glsl_vertextextureblend_usebothalphas", "0", "use both alpha layers on vertex blended surfaces, each alpha layer sets amount of 'blend leak' on another layer, requires mod_q3shader_force_terrain_alphaflag on."};
247 // FIXME: This cvar would grow to a ridiculous size after several launches and clean exits when used during surface sorting.
248 cvar_t r_framedatasize = {CF_CLIENT | CF_ARCHIVE, "r_framedatasize", "0.5", "size of renderer data cache used during one frame (for skeletal animation caching, light processing, etc)"};
249 cvar_t r_buffermegs[R_BUFFERDATA_COUNT] =
251 {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_vertex", "4", "vertex buffer size for one frame"},
252 {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_index16", "1", "index buffer size for one frame (16bit indices)"},
253 {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_index32", "1", "index buffer size for one frame (32bit indices)"},
254 {CF_CLIENT | CF_ARCHIVE, "r_buffermegs_uniform", "0.25", "uniform buffer size for one frame"},
257 cvar_t r_q1bsp_lightmap_updates_enabled = {CF_CLIENT | CF_ARCHIVE, "r_q1bsp_lightmap_updates_enabled", "1", "allow lightmaps to be updated on Q1BSP maps (don't turn this off except for debugging)"};
258 cvar_t r_q1bsp_lightmap_updates_combine = {CF_CLIENT | CF_ARCHIVE, "r_q1bsp_lightmap_updates_combine", "2", "combine lightmap texture updates to make fewer glTexSubImage2D calls, modes: 0 = immediately upload lightmaps (may be thousands of small 3x3 updates), 1 = combine to one call, 2 = combine to one full texture update (glTexImage2D) which tells the driver it does not need to lock the resource (faster on most drivers)"};
259 cvar_t r_q1bsp_lightmap_updates_hidden_surfaces = {CF_CLIENT | CF_ARCHIVE, "r_q1bsp_lightmap_updates_hidden_surfaces", "0", "update lightmaps on surfaces that are not visible, so that updates only occur on frames where lightstyles changed value (animation or light switches), only makes sense with combine = 2"};
261 extern cvar_t v_glslgamma_2d;
263 extern qbool v_flipped_state;
265 r_framebufferstate_t r_fb;
267 /// shadow volume bsp struct with automatically growing nodes buffer
270 int r_uniformbufferalignment = 32; // dynamically updated to match GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT
272 rtexture_t *r_texture_blanknormalmap;
273 rtexture_t *r_texture_white;
274 rtexture_t *r_texture_grey128;
275 rtexture_t *r_texture_black;
276 rtexture_t *r_texture_notexture;
277 rtexture_t *r_texture_whitecube;
278 rtexture_t *r_texture_normalizationcube;
279 rtexture_t *r_texture_fogattenuation;
280 rtexture_t *r_texture_fogheighttexture;
281 rtexture_t *r_texture_gammaramps;
282 unsigned int r_texture_gammaramps_serial;
283 //rtexture_t *r_texture_fogintensity;
284 rtexture_t *r_texture_reflectcube;
286 // TODO: hash lookups?
287 typedef struct cubemapinfo_s
294 int r_texture_numcubemaps;
295 cubemapinfo_t *r_texture_cubemaps[MAX_CUBEMAPS];
297 unsigned int r_queries[MAX_OCCLUSION_QUERIES];
298 unsigned int r_numqueries;
299 unsigned int r_maxqueries;
301 typedef struct r_qwskincache_s
303 char name[MAX_QPATH];
304 skinframe_t *skinframe;
308 static r_qwskincache_t *r_qwskincache;
309 static int r_qwskincache_size;
311 /// vertex coordinates for a quad that covers the screen exactly
312 extern const float r_screenvertex3f[12];
313 const float r_screenvertex3f[12] =
321 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
324 for (i = 0;i < verts;i++)
335 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
338 for (i = 0;i < verts;i++)
348 // FIXME: move this to client?
351 if (gamemode == GAME_NEHAHRA)
353 Cvar_Set(&cvars_all, "gl_fogenable", "0");
354 Cvar_Set(&cvars_all, "gl_fogdensity", "0.2");
355 Cvar_Set(&cvars_all, "gl_fogred", "0.3");
356 Cvar_Set(&cvars_all, "gl_foggreen", "0.3");
357 Cvar_Set(&cvars_all, "gl_fogblue", "0.3");
359 r_refdef.fog_density = 0;
360 r_refdef.fog_red = 0;
361 r_refdef.fog_green = 0;
362 r_refdef.fog_blue = 0;
363 r_refdef.fog_alpha = 1;
364 r_refdef.fog_start = 0;
365 r_refdef.fog_end = 16384;
366 r_refdef.fog_height = 1<<30;
367 r_refdef.fog_fadedepth = 128;
368 memset(r_refdef.fog_height_texturename, 0, sizeof(r_refdef.fog_height_texturename));
371 static void R_BuildBlankTextures(void)
373 unsigned char data[4];
374 data[2] = 128; // normal X
375 data[1] = 128; // normal Y
376 data[0] = 255; // normal Z
377 data[3] = 255; // height
378 r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
383 r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
388 r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
393 r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
396 static void R_BuildNoTexture(void)
398 r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, Image_GenerateNoTexture(), TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, -1, NULL);
401 static void R_BuildWhiteCube(void)
403 unsigned char data[6*1*1*4];
404 memset(data, 255, sizeof(data));
405 r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
408 static void R_BuildNormalizationCube(void)
412 vec_t s, t, intensity;
415 data = (unsigned char *)Mem_Alloc(tempmempool, 6*NORMSIZE*NORMSIZE*4);
416 for (side = 0;side < 6;side++)
418 for (y = 0;y < NORMSIZE;y++)
420 for (x = 0;x < NORMSIZE;x++)
422 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
423 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
458 intensity = 127.0f / sqrt(DotProduct(v, v));
459 data[((side*64+y)*64+x)*4+2] = (unsigned char)(128.0f + intensity * v[0]);
460 data[((side*64+y)*64+x)*4+1] = (unsigned char)(128.0f + intensity * v[1]);
461 data[((side*64+y)*64+x)*4+0] = (unsigned char)(128.0f + intensity * v[2]);
462 data[((side*64+y)*64+x)*4+3] = 255;
466 r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
470 static void R_BuildFogTexture(void)
474 unsigned char data1[FOGWIDTH][4];
475 //unsigned char data2[FOGWIDTH][4];
478 r_refdef.fogmasktable_start = r_refdef.fog_start;
479 r_refdef.fogmasktable_alpha = r_refdef.fog_alpha;
480 r_refdef.fogmasktable_range = r_refdef.fogrange;
481 r_refdef.fogmasktable_density = r_refdef.fog_density;
483 r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
484 for (x = 0;x < FOGMASKTABLEWIDTH;x++)
486 d = (x * r - r_refdef.fogmasktable_start);
487 if(developer_extra.integer)
488 Con_DPrintf("%f ", d);
490 if (r_fog_exp2.integer)
491 alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
493 alpha = exp(-r_refdef.fogmasktable_density * 0.004 * d);
494 if(developer_extra.integer)
495 Con_DPrintf(" : %f ", alpha);
496 alpha = 1 - (1 - alpha) * r_refdef.fogmasktable_alpha;
497 if(developer_extra.integer)
498 Con_DPrintf(" = %f\n", alpha);
499 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
502 for (x = 0;x < FOGWIDTH;x++)
504 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
509 //data2[x][0] = 255 - b;
510 //data2[x][1] = 255 - b;
511 //data2[x][2] = 255 - b;
514 if (r_texture_fogattenuation)
516 R_UpdateTexture(r_texture_fogattenuation, &data1[0][0], 0, 0, 0, FOGWIDTH, 1, 1, 0);
517 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, 0, FOGWIDTH, 1, 1, 0);
521 r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
522 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
526 static void R_BuildFogHeightTexture(void)
528 unsigned char *inpixels;
536 strlcpy(r_refdef.fogheighttexturename, r_refdef.fog_height_texturename, sizeof(r_refdef.fogheighttexturename));
537 if (r_refdef.fogheighttexturename[0])
538 inpixels = loadimagepixelsbgra(r_refdef.fogheighttexturename, true, false, false, NULL);
541 r_refdef.fog_height_tablesize = 0;
542 if (r_texture_fogheighttexture)
543 R_FreeTexture(r_texture_fogheighttexture);
544 r_texture_fogheighttexture = NULL;
545 if (r_refdef.fog_height_table2d)
546 Mem_Free(r_refdef.fog_height_table2d);
547 r_refdef.fog_height_table2d = NULL;
548 if (r_refdef.fog_height_table1d)
549 Mem_Free(r_refdef.fog_height_table1d);
550 r_refdef.fog_height_table1d = NULL;
554 r_refdef.fog_height_tablesize = size;
555 r_refdef.fog_height_table1d = (unsigned char *)Mem_Alloc(r_main_mempool, size * 4);
556 r_refdef.fog_height_table2d = (unsigned char *)Mem_Alloc(r_main_mempool, size * size * 4);
557 memcpy(r_refdef.fog_height_table1d, inpixels, size * 4);
559 // LadyHavoc: now the magic - what is that table2d for? it is a cooked
560 // average fog color table accounting for every fog layer between a point
561 // and the camera. (Note: attenuation is handled separately!)
562 for (y = 0;y < size;y++)
564 for (x = 0;x < size;x++)
570 for (j = x;j <= y;j++)
572 Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
578 for (j = x;j >= y;j--)
580 Vector4Add(c, r_refdef.fog_height_table1d + j*4, c);
585 r_refdef.fog_height_table2d[(y*size+x)*4+0] = (unsigned char)(c[0] * f);
586 r_refdef.fog_height_table2d[(y*size+x)*4+1] = (unsigned char)(c[1] * f);
587 r_refdef.fog_height_table2d[(y*size+x)*4+2] = (unsigned char)(c[2] * f);
588 r_refdef.fog_height_table2d[(y*size+x)*4+3] = (unsigned char)(c[3] * f);
591 r_texture_fogheighttexture = R_LoadTexture2D(r_main_texturepool, "fogheighttable", size, size, r_refdef.fog_height_table2d, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_CLAMP, -1, NULL);
594 //=======================================================================================================================================================
596 static const char *builtinshaderstrings[] =
598 #include "shader_glsl.h"
602 //=======================================================================================================================================================
604 typedef struct shaderpermutationinfo_s
609 shaderpermutationinfo_t;
611 typedef struct shadermodeinfo_s
613 const char *sourcebasename;
614 const char *extension;
615 const char **builtinshaderstrings;
624 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
625 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
627 {"#define USEDIFFUSE\n", " diffuse"},
628 {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
629 {"#define USEVIEWTINT\n", " viewtint"},
630 {"#define USECOLORMAPPING\n", " colormapping"},
631 {"#define USESATURATION\n", " saturation"},
632 {"#define USEFOGINSIDE\n", " foginside"},
633 {"#define USEFOGOUTSIDE\n", " fogoutside"},
634 {"#define USEFOGHEIGHTTEXTURE\n", " fogheighttexture"},
635 {"#define USEFOGALPHAHACK\n", " fogalphahack"},
636 {"#define USEGAMMARAMPS\n", " gammaramps"},
637 {"#define USECUBEFILTER\n", " cubefilter"},
638 {"#define USEGLOW\n", " glow"},
639 {"#define USEBLOOM\n", " bloom"},
640 {"#define USESPECULAR\n", " specular"},
641 {"#define USEPOSTPROCESSING\n", " postprocessing"},
642 {"#define USEREFLECTION\n", " reflection"},
643 {"#define USEOFFSETMAPPING\n", " offsetmapping"},
644 {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
645 {"#define USESHADOWMAP2D\n", " shadowmap2d"},
646 {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"}, // TODO make this a static parm
647 {"#define USESHADOWMAPORTHO\n", " shadowmaportho"},
648 {"#define USEDEFERREDLIGHTMAP\n", " deferredlightmap"},
649 {"#define USEALPHAKILL\n", " alphakill"},
650 {"#define USEREFLECTCUBE\n", " reflectcube"},
651 {"#define USENORMALMAPSCROLLBLEND\n", " normalmapscrollblend"},
652 {"#define USEBOUNCEGRID\n", " bouncegrid"},
653 {"#define USEBOUNCEGRIDDIRECTIONAL\n", " bouncegriddirectional"}, // TODO make this a static parm
654 {"#define USETRIPPY\n", " trippy"},
655 {"#define USEDEPTHRGB\n", " depthrgb"},
656 {"#define USEALPHAGENVERTEX\n", " alphagenvertex"},
657 {"#define USESKELETAL\n", " skeletal"},
658 {"#define USEOCCLUDE\n", " occlude"}
661 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
662 shadermodeinfo_t shadermodeinfo[SHADERLANGUAGE_COUNT][SHADERMODE_COUNT] =
664 // SHADERLANGUAGE_GLSL
666 {"combined", "glsl", builtinshaderstrings, "#define MODE_GENERIC\n", " generic"},
667 {"combined", "glsl", builtinshaderstrings, "#define MODE_POSTPROCESS\n", " postprocess"},
668 {"combined", "glsl", builtinshaderstrings, "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
669 {"combined", "glsl", builtinshaderstrings, "#define MODE_FLATCOLOR\n", " flatcolor"},
670 {"combined", "glsl", builtinshaderstrings, "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
671 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTMAP\n", " lightmap"},
672 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
673 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
674 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP\n", " lightdirectionmap_forced_lightmap"},
675 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR\n", " lightdirectionmap_forced_vertexcolor"},
676 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTGRID\n", " lightgrid"},
677 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
678 {"combined", "glsl", builtinshaderstrings, "#define MODE_LIGHTSOURCE\n", " lightsource"},
679 {"combined", "glsl", builtinshaderstrings, "#define MODE_REFRACTION\n", " refraction"},
680 {"combined", "glsl", builtinshaderstrings, "#define MODE_WATER\n", " water"},
681 {"combined", "glsl", builtinshaderstrings, "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
682 {"combined", "glsl", builtinshaderstrings, "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
686 struct r_glsl_permutation_s;
687 typedef struct r_glsl_permutation_s
690 struct r_glsl_permutation_s *hashnext;
692 uint64_t permutation;
694 /// indicates if we have tried compiling this permutation already
696 /// 0 if compilation failed
698 // texture units assigned to each detected uniform
699 int tex_Texture_First;
700 int tex_Texture_Second;
701 int tex_Texture_GammaRamps;
702 int tex_Texture_Normal;
703 int tex_Texture_Color;
704 int tex_Texture_Gloss;
705 int tex_Texture_Glow;
706 int tex_Texture_SecondaryNormal;
707 int tex_Texture_SecondaryColor;
708 int tex_Texture_SecondaryGloss;
709 int tex_Texture_SecondaryGlow;
710 int tex_Texture_Pants;
711 int tex_Texture_Shirt;
712 int tex_Texture_FogHeightTexture;
713 int tex_Texture_FogMask;
714 int tex_Texture_LightGrid;
715 int tex_Texture_Lightmap;
716 int tex_Texture_Deluxemap;
717 int tex_Texture_Attenuation;
718 int tex_Texture_Cube;
719 int tex_Texture_Refraction;
720 int tex_Texture_Reflection;
721 int tex_Texture_ShadowMap2D;
722 int tex_Texture_CubeProjection;
723 int tex_Texture_ScreenNormalMap;
724 int tex_Texture_ScreenDiffuse;
725 int tex_Texture_ScreenSpecular;
726 int tex_Texture_ReflectMask;
727 int tex_Texture_ReflectCube;
728 int tex_Texture_BounceGrid;
729 /// locations of detected uniforms in program object, or -1 if not found
730 int loc_Texture_First;
731 int loc_Texture_Second;
732 int loc_Texture_GammaRamps;
733 int loc_Texture_Normal;
734 int loc_Texture_Color;
735 int loc_Texture_Gloss;
736 int loc_Texture_Glow;
737 int loc_Texture_SecondaryNormal;
738 int loc_Texture_SecondaryColor;
739 int loc_Texture_SecondaryGloss;
740 int loc_Texture_SecondaryGlow;
741 int loc_Texture_Pants;
742 int loc_Texture_Shirt;
743 int loc_Texture_FogHeightTexture;
744 int loc_Texture_FogMask;
745 int loc_Texture_LightGrid;
746 int loc_Texture_Lightmap;
747 int loc_Texture_Deluxemap;
748 int loc_Texture_Attenuation;
749 int loc_Texture_Cube;
750 int loc_Texture_Refraction;
751 int loc_Texture_Reflection;
752 int loc_Texture_ShadowMap2D;
753 int loc_Texture_CubeProjection;
754 int loc_Texture_ScreenNormalMap;
755 int loc_Texture_ScreenDiffuse;
756 int loc_Texture_ScreenSpecular;
757 int loc_Texture_ReflectMask;
758 int loc_Texture_ReflectCube;
759 int loc_Texture_BounceGrid;
761 int loc_BloomBlur_Parameters;
763 int loc_Color_Ambient;
764 int loc_Color_Diffuse;
765 int loc_Color_Specular;
769 int loc_DeferredColor_Ambient;
770 int loc_DeferredColor_Diffuse;
771 int loc_DeferredColor_Specular;
772 int loc_DeferredMod_Diffuse;
773 int loc_DeferredMod_Specular;
774 int loc_DistortScaleRefractReflect;
777 int loc_FogHeightFade;
779 int loc_FogPlaneViewDist;
780 int loc_FogRangeRecip;
783 int loc_LightGridMatrix;
784 int loc_LightGridNormalMatrix;
785 int loc_LightPosition;
786 int loc_OffsetMapping_ScaleSteps;
787 int loc_OffsetMapping_LodDistance;
788 int loc_OffsetMapping_Bias;
790 int loc_ReflectColor;
791 int loc_ReflectFactor;
792 int loc_ReflectOffset;
793 int loc_RefractColor;
795 int loc_ScreenCenterRefractReflect;
796 int loc_ScreenScaleRefractReflect;
797 int loc_ScreenToDepth;
798 int loc_ShadowMap_Parameters;
799 int loc_ShadowMap_TextureScale;
800 int loc_SpecularPower;
801 int loc_Skeletal_Transform12;
807 int loc_ViewTintColor;
809 int loc_ModelToLight;
811 int loc_BackgroundTexMatrix;
812 int loc_ModelViewProjectionMatrix;
813 int loc_ModelViewMatrix;
814 int loc_PixelToScreenTexCoord;
815 int loc_ModelToReflectCube;
816 int loc_ShadowMapMatrix;
817 int loc_BloomColorSubtract;
818 int loc_NormalmapScrollBlend;
819 int loc_BounceGridMatrix;
820 int loc_BounceGridIntensity;
821 /// uniform block bindings
822 int ubibind_Skeletal_Transform12_UniformBlock;
823 /// uniform block indices
824 int ubiloc_Skeletal_Transform12_UniformBlock;
826 r_glsl_permutation_t;
828 #define SHADERPERMUTATION_HASHSIZE 256
831 // non-degradable "lightweight" shader parameters to keep the permutations simpler
832 // these can NOT degrade! only use for simple stuff
835 SHADERSTATICPARM_SATURATION_REDCOMPENSATE = 0, ///< red compensation filter for saturation
836 SHADERSTATICPARM_EXACTSPECULARMATH = 1, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
837 SHADERSTATICPARM_POSTPROCESS_USERVEC1 = 2, ///< postprocess uservec1 is enabled
838 SHADERSTATICPARM_POSTPROCESS_USERVEC2 = 3, ///< postprocess uservec2 is enabled
839 SHADERSTATICPARM_POSTPROCESS_USERVEC3 = 4, ///< postprocess uservec3 is enabled
840 SHADERSTATICPARM_POSTPROCESS_USERVEC4 = 5, ///< postprocess uservec4 is enabled
841 SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS = 6, // use both alpha layers while blending materials, allows more advanced microblending
842 SHADERSTATICPARM_OFFSETMAPPING_USELOD = 7, ///< LOD for offsetmapping
843 SHADERSTATICPARM_SHADOWMAPPCF_1 = 8, ///< PCF 1
844 SHADERSTATICPARM_SHADOWMAPPCF_2 = 9, ///< PCF 2
845 SHADERSTATICPARM_SHADOWSAMPLER = 10, ///< sampler
846 SHADERSTATICPARM_CELSHADING = 11, ///< celshading (alternative diffuse and specular math)
847 SHADERSTATICPARM_CELOUTLINES = 12, ///< celoutline (depth buffer analysis to produce outlines)
848 SHADERSTATICPARM_FXAA = 13, ///< fast approximate anti aliasing
849 SHADERSTATICPARM_COLORFRINGE = 14 ///< colorfringe (chromatic aberration)
851 #define SHADERSTATICPARMS_COUNT 15
853 static const char *shaderstaticparmstrings_list[SHADERSTATICPARMS_COUNT];
854 static int shaderstaticparms_count = 0;
856 static unsigned int r_compileshader_staticparms[(SHADERSTATICPARMS_COUNT + 0x1F) >> 5] = {0};
857 #define R_COMPILESHADER_STATICPARM_ENABLE(p) r_compileshader_staticparms[(p) >> 5] |= (1 << ((p) & 0x1F))
859 extern qbool r_shadow_shadowmapsampler;
860 extern int r_shadow_shadowmappcf;
861 qbool R_CompileShader_CheckStaticParms(void)
863 static int r_compileshader_staticparms_save[(SHADERSTATICPARMS_COUNT + 0x1F) >> 5];
864 memcpy(r_compileshader_staticparms_save, r_compileshader_staticparms, sizeof(r_compileshader_staticparms));
865 memset(r_compileshader_staticparms, 0, sizeof(r_compileshader_staticparms));
868 if (r_glsl_saturation_redcompensate.integer)
869 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SATURATION_REDCOMPENSATE);
870 if (r_glsl_vertextextureblend_usebothalphas.integer)
871 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS);
872 if (r_shadow_glossexact.integer)
873 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_EXACTSPECULARMATH);
874 if (r_glsl_postprocess.integer)
876 if (r_glsl_postprocess_uservec1_enable.integer)
877 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC1);
878 if (r_glsl_postprocess_uservec2_enable.integer)
879 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC2);
880 if (r_glsl_postprocess_uservec3_enable.integer)
881 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC3);
882 if (r_glsl_postprocess_uservec4_enable.integer)
883 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC4);
886 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_FXAA);
887 if (r_glsl_offsetmapping_lod.integer && r_glsl_offsetmapping_lod_distance.integer > 0)
888 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_OFFSETMAPPING_USELOD);
890 if (r_shadow_shadowmapsampler)
891 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWSAMPLER);
892 if (r_shadow_shadowmappcf > 1)
893 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWMAPPCF_2);
894 else if (r_shadow_shadowmappcf)
895 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWMAPPCF_1);
896 if (r_celshading.integer)
897 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_CELSHADING);
898 if (r_celoutlines.integer)
899 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_CELOUTLINES);
900 if (r_colorfringe.value)
901 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_COLORFRINGE);
903 return memcmp(r_compileshader_staticparms, r_compileshader_staticparms_save, sizeof(r_compileshader_staticparms)) != 0;
906 #define R_COMPILESHADER_STATICPARM_EMIT(p, n) \
907 if(r_compileshader_staticparms[(p) >> 5] & (1 << ((p) & 0x1F))) \
908 shaderstaticparmstrings_list[shaderstaticparms_count++] = "#define " n "\n"; \
910 shaderstaticparmstrings_list[shaderstaticparms_count++] = "\n"
911 static void R_CompileShader_AddStaticParms(unsigned int mode, uint64_t permutation)
913 shaderstaticparms_count = 0;
916 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SATURATION_REDCOMPENSATE, "SATURATION_REDCOMPENSATE");
917 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_EXACTSPECULARMATH, "USEEXACTSPECULARMATH");
918 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC1, "USERVEC1");
919 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC2, "USERVEC2");
920 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC3, "USERVEC3");
921 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC4, "USERVEC4");
922 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS, "USEBOTHALPHAS");
923 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_OFFSETMAPPING_USELOD, "USEOFFSETMAPPING_LOD");
924 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWMAPPCF_1, "USESHADOWMAPPCF 1");
925 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWMAPPCF_2, "USESHADOWMAPPCF 2");
926 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWSAMPLER, "USESHADOWSAMPLER");
927 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_CELSHADING, "USECELSHADING");
928 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_CELOUTLINES, "USECELOUTLINES");
929 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_FXAA, "USEFXAA");
930 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_COLORFRINGE, "USECOLORFRINGE");
933 /// information about each possible shader permutation
934 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
935 /// currently selected permutation
936 r_glsl_permutation_t *r_glsl_permutation;
937 /// storage for permutations linked in the hash table
938 memexpandablearray_t r_glsl_permutationarray;
940 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, uint64_t permutation)
942 //unsigned int hashdepth = 0;
943 unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
944 r_glsl_permutation_t *p;
945 for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
947 if (p->mode == mode && p->permutation == permutation)
949 //if (hashdepth > 10)
950 // Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
955 p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
957 p->permutation = permutation;
958 p->hashnext = r_glsl_permutationhash[mode][hashindex];
959 r_glsl_permutationhash[mode][hashindex] = p;
960 //if (hashdepth > 10)
961 // Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
965 static char *R_ShaderStrCat(const char **strings)
968 const char **p = strings;
971 for (p = strings;(t = *p);p++)
974 s = string = (char *)Mem_Alloc(r_main_mempool, len);
976 for (p = strings;(t = *p);p++)
986 static char *R_ShaderStrCat(const char **strings);
987 static void R_InitShaderModeInfo(void)
990 shadermodeinfo_t *modeinfo;
991 // we have a bunch of things to compute that weren't calculated at engine compile time - all filenames should have a crc of the builtin strings to prevent accidental overrides (any customization must be updated to match engine)
992 for (language = 0; language < SHADERLANGUAGE_COUNT; language++)
994 for (i = 0; i < SHADERMODE_COUNT; i++)
996 char filename[MAX_QPATH];
997 modeinfo = &shadermodeinfo[language][i];
998 modeinfo->builtinstring = R_ShaderStrCat(modeinfo->builtinshaderstrings);
999 modeinfo->builtincrc = CRC_Block((const unsigned char *)modeinfo->builtinstring, strlen(modeinfo->builtinstring));
1000 dpsnprintf(filename, sizeof(filename), "%s/%s_crc%i.%s", modeinfo->extension, modeinfo->sourcebasename, modeinfo->builtincrc, modeinfo->extension);
1001 modeinfo->filename = Mem_strdup(r_main_mempool, filename);
1006 static char *ShaderModeInfo_GetShaderText(shadermodeinfo_t *modeinfo, qbool printfromdisknotice, qbool builtinonly)
1009 // if the mode has no filename we have to return the builtin string
1010 if (builtinonly || !modeinfo->filename)
1011 return Mem_strdup(r_main_mempool, modeinfo->builtinstring);
1012 // note that FS_LoadFile appends a 0 byte to make it a valid string
1013 shaderstring = (char *)FS_LoadFile(modeinfo->filename, r_main_mempool, false, NULL);
1016 if (printfromdisknotice)
1017 Con_DPrintf("Loading shaders from file %s...\n", modeinfo->filename);
1018 return shaderstring;
1020 // fall back to builtinstring
1021 return Mem_strdup(r_main_mempool, modeinfo->builtinstring);
1024 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, uint64_t permutation)
1029 shadermodeinfo_t *modeinfo = &shadermodeinfo[SHADERLANGUAGE_GLSL][mode];
1031 char permutationname[256];
1032 int vertstrings_count = 0;
1033 int geomstrings_count = 0;
1034 int fragstrings_count = 0;
1035 const char *vertstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1036 const char *geomstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1037 const char *fragstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1044 permutationname[0] = 0;
1045 sourcestring = ShaderModeInfo_GetShaderText(modeinfo, true, false);
1047 strlcat(permutationname, modeinfo->filename, sizeof(permutationname));
1049 // we need 140 for r_glsl_skeletal (GL_ARB_uniform_buffer_object)
1050 if(vid.support.glshaderversion >= 140)
1052 vertstrings_list[vertstrings_count++] = "#version 140\n";
1053 geomstrings_list[geomstrings_count++] = "#version 140\n";
1054 fragstrings_list[fragstrings_count++] = "#version 140\n";
1055 vertstrings_list[vertstrings_count++] = "#define GLSL140\n";
1056 geomstrings_list[geomstrings_count++] = "#define GLSL140\n";
1057 fragstrings_list[fragstrings_count++] = "#define GLSL140\n";
1059 // if we can do #version 130, we should (this improves quality of offset/reliefmapping thanks to textureGrad)
1060 else if(vid.support.glshaderversion >= 130)
1062 vertstrings_list[vertstrings_count++] = "#version 130\n";
1063 geomstrings_list[geomstrings_count++] = "#version 130\n";
1064 fragstrings_list[fragstrings_count++] = "#version 130\n";
1065 vertstrings_list[vertstrings_count++] = "#define GLSL130\n";
1066 geomstrings_list[geomstrings_count++] = "#define GLSL130\n";
1067 fragstrings_list[fragstrings_count++] = "#define GLSL130\n";
1069 // if we can do #version 120, we should (this adds the invariant keyword)
1070 else if(vid.support.glshaderversion >= 120)
1072 vertstrings_list[vertstrings_count++] = "#version 120\n";
1073 geomstrings_list[geomstrings_count++] = "#version 120\n";
1074 fragstrings_list[fragstrings_count++] = "#version 120\n";
1075 vertstrings_list[vertstrings_count++] = "#define GLSL120\n";
1076 geomstrings_list[geomstrings_count++] = "#define GLSL120\n";
1077 fragstrings_list[fragstrings_count++] = "#define GLSL120\n";
1079 // GLES also adds several things from GLSL120
1080 switch(vid.renderpath)
1082 case RENDERPATH_GLES2:
1083 vertstrings_list[vertstrings_count++] = "#define GLES\n";
1084 geomstrings_list[geomstrings_count++] = "#define GLES\n";
1085 fragstrings_list[fragstrings_count++] = "#define GLES\n";
1091 // the first pretext is which type of shader to compile as
1092 // (later these will all be bound together as a program object)
1093 vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
1094 geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
1095 fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
1097 // the second pretext is the mode (for example a light source)
1098 vertstrings_list[vertstrings_count++] = modeinfo->pretext;
1099 geomstrings_list[geomstrings_count++] = modeinfo->pretext;
1100 fragstrings_list[fragstrings_count++] = modeinfo->pretext;
1101 strlcat(permutationname, modeinfo->name, sizeof(permutationname));
1103 // now add all the permutation pretexts
1104 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1106 if (permutation & (1ll<<i))
1108 vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
1109 geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
1110 fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
1111 strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
1115 // keep line numbers correct
1116 vertstrings_list[vertstrings_count++] = "\n";
1117 geomstrings_list[geomstrings_count++] = "\n";
1118 fragstrings_list[fragstrings_count++] = "\n";
1123 R_CompileShader_AddStaticParms(mode, permutation);
1124 memcpy((char *)(vertstrings_list + vertstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1125 vertstrings_count += shaderstaticparms_count;
1126 memcpy((char *)(geomstrings_list + geomstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1127 geomstrings_count += shaderstaticparms_count;
1128 memcpy((char *)(fragstrings_list + fragstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1129 fragstrings_count += shaderstaticparms_count;
1131 // now append the shader text itself
1132 vertstrings_list[vertstrings_count++] = sourcestring;
1133 geomstrings_list[geomstrings_count++] = sourcestring;
1134 fragstrings_list[fragstrings_count++] = sourcestring;
1136 // we don't currently use geometry shaders for anything, so just empty the list
1137 geomstrings_count = 0;
1139 // compile the shader program
1140 if (vertstrings_count + geomstrings_count + fragstrings_count)
1141 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1145 qglUseProgram(p->program);CHECKGLERROR
1146 // look up all the uniform variable names we care about, so we don't
1147 // have to look them up every time we set them
1152 GLint activeuniformindex = 0;
1153 GLint numactiveuniforms = 0;
1154 char uniformname[128];
1155 GLsizei uniformnamelength = 0;
1156 GLint uniformsize = 0;
1157 GLenum uniformtype = 0;
1158 memset(uniformname, 0, sizeof(uniformname));
1159 qglGetProgramiv(p->program, GL_ACTIVE_UNIFORMS, &numactiveuniforms);
1160 Con_Printf("Shader has %i uniforms\n", numactiveuniforms);
1161 for (activeuniformindex = 0;activeuniformindex < numactiveuniforms;activeuniformindex++)
1163 qglGetActiveUniform(p->program, activeuniformindex, sizeof(uniformname) - 1, &uniformnamelength, &uniformsize, &uniformtype, uniformname);
1164 Con_Printf("Uniform %i name \"%s\" size %i type %i\n", (int)activeuniformindex, uniformname, (int)uniformsize, (int)uniformtype);
1169 p->loc_Texture_First = qglGetUniformLocation(p->program, "Texture_First");
1170 p->loc_Texture_Second = qglGetUniformLocation(p->program, "Texture_Second");
1171 p->loc_Texture_GammaRamps = qglGetUniformLocation(p->program, "Texture_GammaRamps");
1172 p->loc_Texture_Normal = qglGetUniformLocation(p->program, "Texture_Normal");
1173 p->loc_Texture_Color = qglGetUniformLocation(p->program, "Texture_Color");
1174 p->loc_Texture_Gloss = qglGetUniformLocation(p->program, "Texture_Gloss");
1175 p->loc_Texture_Glow = qglGetUniformLocation(p->program, "Texture_Glow");
1176 p->loc_Texture_SecondaryNormal = qglGetUniformLocation(p->program, "Texture_SecondaryNormal");
1177 p->loc_Texture_SecondaryColor = qglGetUniformLocation(p->program, "Texture_SecondaryColor");
1178 p->loc_Texture_SecondaryGloss = qglGetUniformLocation(p->program, "Texture_SecondaryGloss");
1179 p->loc_Texture_SecondaryGlow = qglGetUniformLocation(p->program, "Texture_SecondaryGlow");
1180 p->loc_Texture_Pants = qglGetUniformLocation(p->program, "Texture_Pants");
1181 p->loc_Texture_Shirt = qglGetUniformLocation(p->program, "Texture_Shirt");
1182 p->loc_Texture_FogHeightTexture = qglGetUniformLocation(p->program, "Texture_FogHeightTexture");
1183 p->loc_Texture_FogMask = qglGetUniformLocation(p->program, "Texture_FogMask");
1184 p->loc_Texture_LightGrid = qglGetUniformLocation(p->program, "Texture_LightGrid");
1185 p->loc_Texture_Lightmap = qglGetUniformLocation(p->program, "Texture_Lightmap");
1186 p->loc_Texture_Deluxemap = qglGetUniformLocation(p->program, "Texture_Deluxemap");
1187 p->loc_Texture_Attenuation = qglGetUniformLocation(p->program, "Texture_Attenuation");
1188 p->loc_Texture_Cube = qglGetUniformLocation(p->program, "Texture_Cube");
1189 p->loc_Texture_Refraction = qglGetUniformLocation(p->program, "Texture_Refraction");
1190 p->loc_Texture_Reflection = qglGetUniformLocation(p->program, "Texture_Reflection");
1191 p->loc_Texture_ShadowMap2D = qglGetUniformLocation(p->program, "Texture_ShadowMap2D");
1192 p->loc_Texture_CubeProjection = qglGetUniformLocation(p->program, "Texture_CubeProjection");
1193 p->loc_Texture_ScreenNormalMap = qglGetUniformLocation(p->program, "Texture_ScreenNormalMap");
1194 p->loc_Texture_ScreenDiffuse = qglGetUniformLocation(p->program, "Texture_ScreenDiffuse");
1195 p->loc_Texture_ScreenSpecular = qglGetUniformLocation(p->program, "Texture_ScreenSpecular");
1196 p->loc_Texture_ReflectMask = qglGetUniformLocation(p->program, "Texture_ReflectMask");
1197 p->loc_Texture_ReflectCube = qglGetUniformLocation(p->program, "Texture_ReflectCube");
1198 p->loc_Texture_BounceGrid = qglGetUniformLocation(p->program, "Texture_BounceGrid");
1199 p->loc_Alpha = qglGetUniformLocation(p->program, "Alpha");
1200 p->loc_BloomBlur_Parameters = qglGetUniformLocation(p->program, "BloomBlur_Parameters");
1201 p->loc_ClientTime = qglGetUniformLocation(p->program, "ClientTime");
1202 p->loc_Color_Ambient = qglGetUniformLocation(p->program, "Color_Ambient");
1203 p->loc_Color_Diffuse = qglGetUniformLocation(p->program, "Color_Diffuse");
1204 p->loc_Color_Specular = qglGetUniformLocation(p->program, "Color_Specular");
1205 p->loc_Color_Glow = qglGetUniformLocation(p->program, "Color_Glow");
1206 p->loc_Color_Pants = qglGetUniformLocation(p->program, "Color_Pants");
1207 p->loc_Color_Shirt = qglGetUniformLocation(p->program, "Color_Shirt");
1208 p->loc_DeferredColor_Ambient = qglGetUniformLocation(p->program, "DeferredColor_Ambient");
1209 p->loc_DeferredColor_Diffuse = qglGetUniformLocation(p->program, "DeferredColor_Diffuse");
1210 p->loc_DeferredColor_Specular = qglGetUniformLocation(p->program, "DeferredColor_Specular");
1211 p->loc_DeferredMod_Diffuse = qglGetUniformLocation(p->program, "DeferredMod_Diffuse");
1212 p->loc_DeferredMod_Specular = qglGetUniformLocation(p->program, "DeferredMod_Specular");
1213 p->loc_DistortScaleRefractReflect = qglGetUniformLocation(p->program, "DistortScaleRefractReflect");
1214 p->loc_EyePosition = qglGetUniformLocation(p->program, "EyePosition");
1215 p->loc_FogColor = qglGetUniformLocation(p->program, "FogColor");
1216 p->loc_FogHeightFade = qglGetUniformLocation(p->program, "FogHeightFade");
1217 p->loc_FogPlane = qglGetUniformLocation(p->program, "FogPlane");
1218 p->loc_FogPlaneViewDist = qglGetUniformLocation(p->program, "FogPlaneViewDist");
1219 p->loc_FogRangeRecip = qglGetUniformLocation(p->program, "FogRangeRecip");
1220 p->loc_LightColor = qglGetUniformLocation(p->program, "LightColor");
1221 p->loc_LightGridMatrix = qglGetUniformLocation(p->program, "LightGridMatrix");
1222 p->loc_LightGridNormalMatrix = qglGetUniformLocation(p->program, "LightGridNormalMatrix");
1223 p->loc_LightDir = qglGetUniformLocation(p->program, "LightDir");
1224 p->loc_LightPosition = qglGetUniformLocation(p->program, "LightPosition");
1225 p->loc_OffsetMapping_ScaleSteps = qglGetUniformLocation(p->program, "OffsetMapping_ScaleSteps");
1226 p->loc_OffsetMapping_LodDistance = qglGetUniformLocation(p->program, "OffsetMapping_LodDistance");
1227 p->loc_OffsetMapping_Bias = qglGetUniformLocation(p->program, "OffsetMapping_Bias");
1228 p->loc_PixelSize = qglGetUniformLocation(p->program, "PixelSize");
1229 p->loc_ReflectColor = qglGetUniformLocation(p->program, "ReflectColor");
1230 p->loc_ReflectFactor = qglGetUniformLocation(p->program, "ReflectFactor");
1231 p->loc_ReflectOffset = qglGetUniformLocation(p->program, "ReflectOffset");
1232 p->loc_RefractColor = qglGetUniformLocation(p->program, "RefractColor");
1233 p->loc_Saturation = qglGetUniformLocation(p->program, "Saturation");
1234 p->loc_ScreenCenterRefractReflect = qglGetUniformLocation(p->program, "ScreenCenterRefractReflect");
1235 p->loc_ScreenScaleRefractReflect = qglGetUniformLocation(p->program, "ScreenScaleRefractReflect");
1236 p->loc_ScreenToDepth = qglGetUniformLocation(p->program, "ScreenToDepth");
1237 p->loc_ShadowMap_Parameters = qglGetUniformLocation(p->program, "ShadowMap_Parameters");
1238 p->loc_ShadowMap_TextureScale = qglGetUniformLocation(p->program, "ShadowMap_TextureScale");
1239 p->loc_SpecularPower = qglGetUniformLocation(p->program, "SpecularPower");
1240 p->loc_UserVec1 = qglGetUniformLocation(p->program, "UserVec1");
1241 p->loc_UserVec2 = qglGetUniformLocation(p->program, "UserVec2");
1242 p->loc_UserVec3 = qglGetUniformLocation(p->program, "UserVec3");
1243 p->loc_UserVec4 = qglGetUniformLocation(p->program, "UserVec4");
1244 p->loc_ColorFringe = qglGetUniformLocation(p->program, "ColorFringe");
1245 p->loc_ViewTintColor = qglGetUniformLocation(p->program, "ViewTintColor");
1246 p->loc_ViewToLight = qglGetUniformLocation(p->program, "ViewToLight");
1247 p->loc_ModelToLight = qglGetUniformLocation(p->program, "ModelToLight");
1248 p->loc_TexMatrix = qglGetUniformLocation(p->program, "TexMatrix");
1249 p->loc_BackgroundTexMatrix = qglGetUniformLocation(p->program, "BackgroundTexMatrix");
1250 p->loc_ModelViewMatrix = qglGetUniformLocation(p->program, "ModelViewMatrix");
1251 p->loc_ModelViewProjectionMatrix = qglGetUniformLocation(p->program, "ModelViewProjectionMatrix");
1252 p->loc_PixelToScreenTexCoord = qglGetUniformLocation(p->program, "PixelToScreenTexCoord");
1253 p->loc_ModelToReflectCube = qglGetUniformLocation(p->program, "ModelToReflectCube");
1254 p->loc_ShadowMapMatrix = qglGetUniformLocation(p->program, "ShadowMapMatrix");
1255 p->loc_BloomColorSubtract = qglGetUniformLocation(p->program, "BloomColorSubtract");
1256 p->loc_NormalmapScrollBlend = qglGetUniformLocation(p->program, "NormalmapScrollBlend");
1257 p->loc_BounceGridMatrix = qglGetUniformLocation(p->program, "BounceGridMatrix");
1258 p->loc_BounceGridIntensity = qglGetUniformLocation(p->program, "BounceGridIntensity");
1259 // initialize the samplers to refer to the texture units we use
1260 p->tex_Texture_First = -1;
1261 p->tex_Texture_Second = -1;
1262 p->tex_Texture_GammaRamps = -1;
1263 p->tex_Texture_Normal = -1;
1264 p->tex_Texture_Color = -1;
1265 p->tex_Texture_Gloss = -1;
1266 p->tex_Texture_Glow = -1;
1267 p->tex_Texture_SecondaryNormal = -1;
1268 p->tex_Texture_SecondaryColor = -1;
1269 p->tex_Texture_SecondaryGloss = -1;
1270 p->tex_Texture_SecondaryGlow = -1;
1271 p->tex_Texture_Pants = -1;
1272 p->tex_Texture_Shirt = -1;
1273 p->tex_Texture_FogHeightTexture = -1;
1274 p->tex_Texture_FogMask = -1;
1275 p->tex_Texture_LightGrid = -1;
1276 p->tex_Texture_Lightmap = -1;
1277 p->tex_Texture_Deluxemap = -1;
1278 p->tex_Texture_Attenuation = -1;
1279 p->tex_Texture_Cube = -1;
1280 p->tex_Texture_Refraction = -1;
1281 p->tex_Texture_Reflection = -1;
1282 p->tex_Texture_ShadowMap2D = -1;
1283 p->tex_Texture_CubeProjection = -1;
1284 p->tex_Texture_ScreenNormalMap = -1;
1285 p->tex_Texture_ScreenDiffuse = -1;
1286 p->tex_Texture_ScreenSpecular = -1;
1287 p->tex_Texture_ReflectMask = -1;
1288 p->tex_Texture_ReflectCube = -1;
1289 p->tex_Texture_BounceGrid = -1;
1290 // bind the texture samplers in use
1292 if (p->loc_Texture_First >= 0) {p->tex_Texture_First = sampler;qglUniform1i(p->loc_Texture_First , sampler);sampler++;}
1293 if (p->loc_Texture_Second >= 0) {p->tex_Texture_Second = sampler;qglUniform1i(p->loc_Texture_Second , sampler);sampler++;}
1294 if (p->loc_Texture_GammaRamps >= 0) {p->tex_Texture_GammaRamps = sampler;qglUniform1i(p->loc_Texture_GammaRamps , sampler);sampler++;}
1295 if (p->loc_Texture_Normal >= 0) {p->tex_Texture_Normal = sampler;qglUniform1i(p->loc_Texture_Normal , sampler);sampler++;}
1296 if (p->loc_Texture_Color >= 0) {p->tex_Texture_Color = sampler;qglUniform1i(p->loc_Texture_Color , sampler);sampler++;}
1297 if (p->loc_Texture_Gloss >= 0) {p->tex_Texture_Gloss = sampler;qglUniform1i(p->loc_Texture_Gloss , sampler);sampler++;}
1298 if (p->loc_Texture_Glow >= 0) {p->tex_Texture_Glow = sampler;qglUniform1i(p->loc_Texture_Glow , sampler);sampler++;}
1299 if (p->loc_Texture_SecondaryNormal >= 0) {p->tex_Texture_SecondaryNormal = sampler;qglUniform1i(p->loc_Texture_SecondaryNormal , sampler);sampler++;}
1300 if (p->loc_Texture_SecondaryColor >= 0) {p->tex_Texture_SecondaryColor = sampler;qglUniform1i(p->loc_Texture_SecondaryColor , sampler);sampler++;}
1301 if (p->loc_Texture_SecondaryGloss >= 0) {p->tex_Texture_SecondaryGloss = sampler;qglUniform1i(p->loc_Texture_SecondaryGloss , sampler);sampler++;}
1302 if (p->loc_Texture_SecondaryGlow >= 0) {p->tex_Texture_SecondaryGlow = sampler;qglUniform1i(p->loc_Texture_SecondaryGlow , sampler);sampler++;}
1303 if (p->loc_Texture_Pants >= 0) {p->tex_Texture_Pants = sampler;qglUniform1i(p->loc_Texture_Pants , sampler);sampler++;}
1304 if (p->loc_Texture_Shirt >= 0) {p->tex_Texture_Shirt = sampler;qglUniform1i(p->loc_Texture_Shirt , sampler);sampler++;}
1305 if (p->loc_Texture_FogHeightTexture>= 0) {p->tex_Texture_FogHeightTexture = sampler;qglUniform1i(p->loc_Texture_FogHeightTexture, sampler);sampler++;}
1306 if (p->loc_Texture_FogMask >= 0) {p->tex_Texture_FogMask = sampler;qglUniform1i(p->loc_Texture_FogMask , sampler);sampler++;}
1307 if (p->loc_Texture_LightGrid >= 0) {p->tex_Texture_LightGrid = sampler;qglUniform1i(p->loc_Texture_LightGrid , sampler);sampler++;}
1308 if (p->loc_Texture_Lightmap >= 0) {p->tex_Texture_Lightmap = sampler;qglUniform1i(p->loc_Texture_Lightmap , sampler);sampler++;}
1309 if (p->loc_Texture_Deluxemap >= 0) {p->tex_Texture_Deluxemap = sampler;qglUniform1i(p->loc_Texture_Deluxemap , sampler);sampler++;}
1310 if (p->loc_Texture_Attenuation >= 0) {p->tex_Texture_Attenuation = sampler;qglUniform1i(p->loc_Texture_Attenuation , sampler);sampler++;}
1311 if (p->loc_Texture_Cube >= 0) {p->tex_Texture_Cube = sampler;qglUniform1i(p->loc_Texture_Cube , sampler);sampler++;}
1312 if (p->loc_Texture_Refraction >= 0) {p->tex_Texture_Refraction = sampler;qglUniform1i(p->loc_Texture_Refraction , sampler);sampler++;}
1313 if (p->loc_Texture_Reflection >= 0) {p->tex_Texture_Reflection = sampler;qglUniform1i(p->loc_Texture_Reflection , sampler);sampler++;}
1314 if (p->loc_Texture_ShadowMap2D >= 0) {p->tex_Texture_ShadowMap2D = sampler;qglUniform1i(p->loc_Texture_ShadowMap2D , sampler);sampler++;}
1315 if (p->loc_Texture_CubeProjection >= 0) {p->tex_Texture_CubeProjection = sampler;qglUniform1i(p->loc_Texture_CubeProjection , sampler);sampler++;}
1316 if (p->loc_Texture_ScreenNormalMap >= 0) {p->tex_Texture_ScreenNormalMap = sampler;qglUniform1i(p->loc_Texture_ScreenNormalMap , sampler);sampler++;}
1317 if (p->loc_Texture_ScreenDiffuse >= 0) {p->tex_Texture_ScreenDiffuse = sampler;qglUniform1i(p->loc_Texture_ScreenDiffuse , sampler);sampler++;}
1318 if (p->loc_Texture_ScreenSpecular >= 0) {p->tex_Texture_ScreenSpecular = sampler;qglUniform1i(p->loc_Texture_ScreenSpecular , sampler);sampler++;}
1319 if (p->loc_Texture_ReflectMask >= 0) {p->tex_Texture_ReflectMask = sampler;qglUniform1i(p->loc_Texture_ReflectMask , sampler);sampler++;}
1320 if (p->loc_Texture_ReflectCube >= 0) {p->tex_Texture_ReflectCube = sampler;qglUniform1i(p->loc_Texture_ReflectCube , sampler);sampler++;}
1321 if (p->loc_Texture_BounceGrid >= 0) {p->tex_Texture_BounceGrid = sampler;qglUniform1i(p->loc_Texture_BounceGrid , sampler);sampler++;}
1322 // get the uniform block indices so we can bind them
1323 p->ubiloc_Skeletal_Transform12_UniformBlock = -1;
1324 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1325 p->ubiloc_Skeletal_Transform12_UniformBlock = qglGetUniformBlockIndex(p->program, "Skeletal_Transform12_UniformBlock");
1327 // clear the uniform block bindings
1328 p->ubibind_Skeletal_Transform12_UniformBlock = -1;
1329 // bind the uniform blocks in use
1331 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1332 if (p->ubiloc_Skeletal_Transform12_UniformBlock >= 0) {p->ubibind_Skeletal_Transform12_UniformBlock = ubibind;qglUniformBlockBinding(p->program, p->ubiloc_Skeletal_Transform12_UniformBlock, ubibind);ubibind++;}
1334 // we're done compiling and setting up the shader, at least until it is used
1336 Con_DPrintf("^5GLSL shader %s compiled (%i textures).\n", permutationname, sampler);
1339 Con_Printf("^1GLSL shader %s failed! some features may not work properly.\n", permutationname);
1343 Mem_Free(sourcestring);
1346 static void R_SetupShader_SetPermutationGLSL(unsigned int mode, uint64_t permutation)
1348 r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
1349 if (r_glsl_permutation != perm)
1351 r_glsl_permutation = perm;
1352 if (!r_glsl_permutation->program)
1354 if (!r_glsl_permutation->compiled)
1356 Con_DPrintf("Compiling shader mode %u permutation %" PRIx64 "\n", mode, permutation);
1357 R_GLSL_CompilePermutation(perm, mode, permutation);
1359 if (!r_glsl_permutation->program)
1361 // remove features until we find a valid permutation
1363 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1365 // reduce i more quickly whenever it would not remove any bits
1366 uint64_t j = 1ll<<(SHADERPERMUTATION_COUNT-1-i);
1367 if (!(permutation & j))
1370 r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
1371 if (!r_glsl_permutation->compiled)
1372 R_GLSL_CompilePermutation(perm, mode, permutation);
1373 if (r_glsl_permutation->program)
1376 if (i >= SHADERPERMUTATION_COUNT)
1378 //Con_Printf("Could not find a working OpenGL 2.0 shader for permutation %s %s\n", shadermodeinfo[mode].filename, shadermodeinfo[mode].pretext);
1379 r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
1380 qglUseProgram(0);CHECKGLERROR
1381 return; // no bit left to clear, entire mode is broken
1386 qglUseProgram(r_glsl_permutation->program);CHECKGLERROR
1388 if (r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
1389 if (r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
1390 if (r_glsl_permutation->loc_ClientTime >= 0) qglUniform1f(r_glsl_permutation->loc_ClientTime, cl.time);
1394 void R_GLSL_Restart_f(cmd_state_t *cmd)
1396 unsigned int i, limit;
1397 switch(vid.renderpath)
1399 case RENDERPATH_GL32:
1400 case RENDERPATH_GLES2:
1402 r_glsl_permutation_t *p;
1403 r_glsl_permutation = NULL;
1404 limit = (unsigned int)Mem_ExpandableArray_IndexRange(&r_glsl_permutationarray);
1405 for (i = 0;i < limit;i++)
1407 if ((p = (r_glsl_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_glsl_permutationarray, i)))
1409 GL_Backend_FreeProgram(p->program);
1410 Mem_ExpandableArray_FreeRecord(&r_glsl_permutationarray, (void*)p);
1413 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
1419 static void R_GLSL_DumpShader_f(cmd_state_t *cmd)
1421 int i, language, mode, dupe;
1423 shadermodeinfo_t *modeinfo;
1426 for (language = 0;language < SHADERLANGUAGE_COUNT;language++)
1428 modeinfo = shadermodeinfo[language];
1429 for (mode = 0;mode < SHADERMODE_COUNT;mode++)
1431 // don't dump the same file multiple times (most or all shaders come from the same file)
1432 for (dupe = mode - 1;dupe >= 0;dupe--)
1433 if (!strcmp(modeinfo[mode].filename, modeinfo[dupe].filename))
1437 text = modeinfo[mode].builtinstring;
1440 file = FS_OpenRealFile(modeinfo[mode].filename, "w", false);
1443 FS_Print(file, "/* The engine may define the following macros:\n");
1444 FS_Print(file, "#define VERTEX_SHADER\n#define GEOMETRY_SHADER\n#define FRAGMENT_SHADER\n");
1445 for (i = 0;i < SHADERMODE_COUNT;i++)
1446 FS_Print(file, modeinfo[i].pretext);
1447 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1448 FS_Print(file, shaderpermutationinfo[i].pretext);
1449 FS_Print(file, "*/\n");
1450 FS_Print(file, text);
1452 Con_Printf("%s written\n", modeinfo[mode].filename);
1455 Con_Printf(CON_ERROR "failed to write to %s\n", modeinfo[mode].filename);
1460 void R_SetupShader_Generic(rtexture_t *t, qbool usegamma, qbool notrippy, qbool suppresstexalpha)
1462 uint64_t permutation = 0;
1463 if (r_trippy.integer && !notrippy)
1464 permutation |= SHADERPERMUTATION_TRIPPY;
1465 permutation |= SHADERPERMUTATION_VIEWTINT;
1467 permutation |= SHADERPERMUTATION_DIFFUSE;
1468 if (usegamma && v_glslgamma_2d.integer && !vid.sRGB2D && r_texture_gammaramps && !vid_gammatables_trivial)
1469 permutation |= SHADERPERMUTATION_GAMMARAMPS;
1470 if (suppresstexalpha)
1471 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1472 if (vid.allowalphatocoverage)
1473 GL_AlphaToCoverage(false);
1474 switch (vid.renderpath)
1476 case RENDERPATH_GL32:
1477 case RENDERPATH_GLES2:
1478 R_SetupShader_SetPermutationGLSL(SHADERMODE_GENERIC, permutation);
1479 if (r_glsl_permutation->tex_Texture_First >= 0)
1480 R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First, t);
1481 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0)
1482 R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps);
1487 void R_SetupShader_Generic_NoTexture(qbool usegamma, qbool notrippy)
1489 R_SetupShader_Generic(NULL, usegamma, notrippy, false);
1492 void R_SetupShader_DepthOrShadow(qbool notrippy, qbool depthrgb, qbool skeletal)
1494 uint64_t permutation = 0;
1495 if (r_trippy.integer && !notrippy)
1496 permutation |= SHADERPERMUTATION_TRIPPY;
1498 permutation |= SHADERPERMUTATION_DEPTHRGB;
1500 permutation |= SHADERPERMUTATION_SKELETAL;
1502 if (vid.allowalphatocoverage)
1503 GL_AlphaToCoverage(false);
1504 switch (vid.renderpath)
1506 case RENDERPATH_GL32:
1507 case RENDERPATH_GLES2:
1508 R_SetupShader_SetPermutationGLSL(SHADERMODE_DEPTH_OR_SHADOW, permutation);
1509 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1510 if (r_glsl_permutation->ubiloc_Skeletal_Transform12_UniformBlock >= 0 && rsurface.batchskeletaltransform3x4buffer) qglBindBufferRange(GL_UNIFORM_BUFFER, r_glsl_permutation->ubibind_Skeletal_Transform12_UniformBlock, rsurface.batchskeletaltransform3x4buffer->bufferobject, rsurface.batchskeletaltransform3x4offset, rsurface.batchskeletaltransform3x4size);
1516 #define BLENDFUNC_ALLOWS_COLORMOD 1
1517 #define BLENDFUNC_ALLOWS_FOG 2
1518 #define BLENDFUNC_ALLOWS_FOG_HACK0 4
1519 #define BLENDFUNC_ALLOWS_FOG_HACKALPHA 8
1520 #define BLENDFUNC_ALLOWS_ANYFOG (BLENDFUNC_ALLOWS_FOG | BLENDFUNC_ALLOWS_FOG_HACK0 | BLENDFUNC_ALLOWS_FOG_HACKALPHA)
1521 static int R_BlendFuncFlags(int src, int dst)
1525 // a blendfunc allows colormod if:
1526 // a) it can never keep the destination pixel invariant, or
1527 // b) it can keep the destination pixel invariant, and still can do so if colormodded
1528 // this is to prevent unintended side effects from colormod
1530 // a blendfunc allows fog if:
1531 // blend(fog(src), fog(dst)) == fog(blend(src, dst))
1532 // this is to prevent unintended side effects from fog
1534 // these checks are the output of fogeval.pl
1536 r |= BLENDFUNC_ALLOWS_COLORMOD;
1537 if(src == GL_DST_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1538 if(src == GL_DST_ALPHA && dst == GL_ONE_MINUS_DST_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1539 if(src == GL_DST_COLOR && dst == GL_ONE_MINUS_SRC_ALPHA) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1540 if(src == GL_DST_COLOR && dst == GL_ONE_MINUS_SRC_COLOR) r |= BLENDFUNC_ALLOWS_FOG;
1541 if(src == GL_DST_COLOR && dst == GL_SRC_ALPHA) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1542 if(src == GL_DST_COLOR && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1543 if(src == GL_DST_COLOR && dst == GL_ZERO) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1544 if(src == GL_ONE && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1545 if(src == GL_ONE && dst == GL_ONE_MINUS_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG_HACKALPHA;
1546 if(src == GL_ONE && dst == GL_ZERO) r |= BLENDFUNC_ALLOWS_FOG;
1547 if(src == GL_ONE_MINUS_DST_ALPHA && dst == GL_DST_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1548 if(src == GL_ONE_MINUS_DST_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1549 if(src == GL_ONE_MINUS_DST_COLOR && dst == GL_SRC_COLOR) r |= BLENDFUNC_ALLOWS_FOG;
1550 if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1551 if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1552 if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1553 if(src == GL_ONE_MINUS_SRC_COLOR && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1554 if(src == GL_SRC_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
1555 if(src == GL_SRC_ALPHA && dst == GL_ONE_MINUS_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
1556 if(src == GL_ZERO && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG;
1557 if(src == GL_ZERO && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
1562 void R_SetupShader_Surface(const float rtlightambient[3], const float rtlightdiffuse[3], const float rtlightspecular[3], rsurfacepass_t rsurfacepass, int texturenumsurfaces, const msurface_t **texturesurfacelist, void *surfacewaterplane, qbool notrippy, qbool ui)
1564 // select a permutation of the lighting shader appropriate to this
1565 // combination of texture, entity, light source, and fogging, only use the
1566 // minimum features necessary to avoid wasting rendering time in the
1567 // fragment shader on features that are not being used
1568 uint64_t permutation = 0;
1569 unsigned int mode = 0;
1571 texture_t *t = rsurface.texture;
1573 matrix4x4_t tempmatrix;
1574 r_waterstate_waterplane_t *waterplane = (r_waterstate_waterplane_t *)surfacewaterplane;
1575 if (r_trippy.integer && !notrippy)
1576 permutation |= SHADERPERMUTATION_TRIPPY;
1577 if (t->currentmaterialflags & MATERIALFLAG_ALPHATEST)
1578 permutation |= SHADERPERMUTATION_ALPHAKILL;
1579 if (t->currentmaterialflags & MATERIALFLAG_OCCLUDE)
1580 permutation |= SHADERPERMUTATION_OCCLUDE;
1581 if (t->r_water_waterscroll[0] && t->r_water_waterscroll[1])
1582 permutation |= SHADERPERMUTATION_NORMALMAPSCROLLBLEND; // todo: make generic
1583 if (rsurfacepass == RSURFPASS_BACKGROUND)
1585 // distorted background
1586 if (t->currentmaterialflags & MATERIALFLAG_WATERSHADER)
1588 mode = SHADERMODE_WATER;
1589 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1590 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1591 if((r_wateralpha.value < 1) && (t->currentmaterialflags & MATERIALFLAG_WATERALPHA))
1593 // this is the right thing to do for wateralpha
1594 GL_BlendFunc(GL_ONE, GL_ZERO);
1595 blendfuncflags = R_BlendFuncFlags(GL_ONE, GL_ZERO);
1599 // this is the right thing to do for entity alpha
1600 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1601 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1604 else if (t->currentmaterialflags & MATERIALFLAG_REFRACTION)
1606 mode = SHADERMODE_REFRACTION;
1607 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1608 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1609 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1610 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1614 mode = SHADERMODE_GENERIC;
1615 permutation |= SHADERPERMUTATION_DIFFUSE | SHADERPERMUTATION_ALPHAKILL;
1616 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1617 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1619 if (vid.allowalphatocoverage)
1620 GL_AlphaToCoverage(false);
1622 else if (rsurfacepass == RSURFPASS_DEFERREDGEOMETRY)
1624 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1626 switch(t->offsetmapping)
1628 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1629 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1630 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1631 case OFFSETMAPPING_OFF: break;
1634 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1635 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1636 // normalmap (deferred prepass), may use alpha test on diffuse
1637 mode = SHADERMODE_DEFERREDGEOMETRY;
1638 GL_BlendFunc(GL_ONE, GL_ZERO);
1639 blendfuncflags = R_BlendFuncFlags(GL_ONE, GL_ZERO);
1640 if (vid.allowalphatocoverage)
1641 GL_AlphaToCoverage(false);
1643 else if (rsurfacepass == RSURFPASS_RTLIGHT)
1645 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1647 switch(t->offsetmapping)
1649 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1650 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1651 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1652 case OFFSETMAPPING_OFF: break;
1655 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1656 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1657 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1658 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1660 mode = SHADERMODE_LIGHTSOURCE;
1661 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1662 permutation |= SHADERPERMUTATION_CUBEFILTER;
1663 if (VectorLength2(rtlightdiffuse) > 0)
1664 permutation |= SHADERPERMUTATION_DIFFUSE;
1665 if (VectorLength2(rtlightspecular) > 0)
1666 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1667 if (r_refdef.fogenabled)
1668 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1669 if (t->colormapping)
1670 permutation |= SHADERPERMUTATION_COLORMAPPING;
1671 if (r_shadow_usingshadowmap2d)
1673 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1674 if(r_shadow_shadowmapvsdct)
1675 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
1677 if (r_shadow_shadowmap2ddepthbuffer)
1678 permutation |= SHADERPERMUTATION_DEPTHRGB;
1680 if (t->reflectmasktexture)
1681 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1682 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1683 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE);
1684 if (vid.allowalphatocoverage)
1685 GL_AlphaToCoverage(false);
1687 else if (t->currentmaterialflags & MATERIALFLAG_LIGHTGRID)
1689 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1691 switch(t->offsetmapping)
1693 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1694 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1695 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1696 case OFFSETMAPPING_OFF: break;
1699 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1700 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1701 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1702 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1703 // directional model lighting
1704 mode = SHADERMODE_LIGHTGRID;
1705 if ((t->glowtexture || t->backgroundglowtexture) && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
1706 permutation |= SHADERPERMUTATION_GLOW;
1707 permutation |= SHADERPERMUTATION_DIFFUSE;
1708 if (t->glosstexture || t->backgroundglosstexture)
1709 permutation |= SHADERPERMUTATION_SPECULAR;
1710 if (r_refdef.fogenabled)
1711 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1712 if (t->colormapping)
1713 permutation |= SHADERPERMUTATION_COLORMAPPING;
1714 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
1716 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1717 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1719 if (r_shadow_shadowmap2ddepthbuffer)
1720 permutation |= SHADERPERMUTATION_DEPTHRGB;
1722 if (t->currentmaterialflags & MATERIALFLAG_REFLECTION)
1723 permutation |= SHADERPERMUTATION_REFLECTION;
1724 if (r_shadow_usingdeferredprepass && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
1725 permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
1726 if (t->reflectmasktexture)
1727 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1728 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1730 permutation |= SHADERPERMUTATION_BOUNCEGRID;
1731 if (r_shadow_bouncegrid_state.directional)
1732 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
1734 GL_BlendFunc(t->currentblendfunc[0], t->currentblendfunc[1]);
1735 blendfuncflags = R_BlendFuncFlags(t->currentblendfunc[0], t->currentblendfunc[1]);
1736 // when using alphatocoverage, we don't need alphakill
1737 if (vid.allowalphatocoverage)
1739 if (r_transparent_alphatocoverage.integer)
1741 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1742 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1745 GL_AlphaToCoverage(false);
1748 else if (t->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
1750 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1752 switch(t->offsetmapping)
1754 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1755 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1756 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1757 case OFFSETMAPPING_OFF: break;
1760 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1761 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1762 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1763 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1764 // directional model lighting
1765 mode = SHADERMODE_LIGHTDIRECTION;
1766 if ((t->glowtexture || t->backgroundglowtexture) && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
1767 permutation |= SHADERPERMUTATION_GLOW;
1768 if (VectorLength2(t->render_modellight_diffuse))
1769 permutation |= SHADERPERMUTATION_DIFFUSE;
1770 if (VectorLength2(t->render_modellight_specular) > 0)
1771 permutation |= SHADERPERMUTATION_SPECULAR;
1772 if (r_refdef.fogenabled)
1773 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1774 if (t->colormapping)
1775 permutation |= SHADERPERMUTATION_COLORMAPPING;
1776 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
1778 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1779 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1781 if (r_shadow_shadowmap2ddepthbuffer)
1782 permutation |= SHADERPERMUTATION_DEPTHRGB;
1784 if (t->currentmaterialflags & MATERIALFLAG_REFLECTION)
1785 permutation |= SHADERPERMUTATION_REFLECTION;
1786 if (r_shadow_usingdeferredprepass && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
1787 permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
1788 if (t->reflectmasktexture)
1789 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1790 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1792 permutation |= SHADERPERMUTATION_BOUNCEGRID;
1793 if (r_shadow_bouncegrid_state.directional)
1794 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
1796 GL_BlendFunc(t->currentblendfunc[0], t->currentblendfunc[1]);
1797 blendfuncflags = R_BlendFuncFlags(t->currentblendfunc[0], t->currentblendfunc[1]);
1798 // when using alphatocoverage, we don't need alphakill
1799 if (vid.allowalphatocoverage)
1801 if (r_transparent_alphatocoverage.integer)
1803 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1804 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1807 GL_AlphaToCoverage(false);
1812 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(t->nmaptexture) & TEXF_ALPHA) || t->offsetbias != 0.0f))
1814 switch(t->offsetmapping)
1816 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
1817 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1818 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
1819 case OFFSETMAPPING_OFF: break;
1822 if (t->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
1823 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1824 if (t->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
1825 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
1827 if ((t->glowtexture || t->backgroundglowtexture) && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
1828 permutation |= SHADERPERMUTATION_GLOW;
1829 if (r_refdef.fogenabled && !ui)
1830 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
1831 if (t->colormapping)
1832 permutation |= SHADERPERMUTATION_COLORMAPPING;
1833 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
1835 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
1836 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
1838 if (r_shadow_shadowmap2ddepthbuffer)
1839 permutation |= SHADERPERMUTATION_DEPTHRGB;
1841 if (t->currentmaterialflags & MATERIALFLAG_REFLECTION)
1842 permutation |= SHADERPERMUTATION_REFLECTION;
1843 if (r_shadow_usingdeferredprepass && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
1844 permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
1845 if (t->reflectmasktexture)
1846 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1847 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping)
1849 // deluxemapping (light direction texture)
1850 if (rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping && r_refdef.scene.worldmodel->brushq3.deluxemapping_modelspace)
1851 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
1853 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1854 permutation |= SHADERPERMUTATION_DIFFUSE;
1855 if (VectorLength2(t->render_lightmap_specular) > 0)
1856 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1858 else if (r_glsl_deluxemapping.integer >= 2)
1860 // fake deluxemapping (uniform light direction in tangentspace)
1861 if (rsurface.uselightmaptexture)
1862 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP;
1864 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR;
1865 permutation |= SHADERPERMUTATION_DIFFUSE;
1866 if (VectorLength2(t->render_lightmap_specular) > 0)
1867 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1869 else if (rsurface.uselightmaptexture)
1871 // ordinary lightmapping (q1bsp, q3bsp)
1872 mode = SHADERMODE_LIGHTMAP;
1876 // ordinary vertex coloring (q3bsp)
1877 mode = SHADERMODE_VERTEXCOLOR;
1879 if (r_shadow_bouncegrid_state.texture && cl.csqc_vidvars.drawworld && !notrippy)
1881 permutation |= SHADERPERMUTATION_BOUNCEGRID;
1882 if (r_shadow_bouncegrid_state.directional)
1883 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
1885 GL_BlendFunc(t->currentblendfunc[0], t->currentblendfunc[1]);
1886 blendfuncflags = R_BlendFuncFlags(t->currentblendfunc[0], t->currentblendfunc[1]);
1887 // when using alphatocoverage, we don't need alphakill
1888 if (vid.allowalphatocoverage)
1890 if (r_transparent_alphatocoverage.integer)
1892 GL_AlphaToCoverage((t->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
1893 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
1896 GL_AlphaToCoverage(false);
1899 if(!(blendfuncflags & BLENDFUNC_ALLOWS_ANYFOG))
1900 permutation &= ~(SHADERPERMUTATION_FOGHEIGHTTEXTURE | SHADERPERMUTATION_FOGOUTSIDE | SHADERPERMUTATION_FOGINSIDE);
1901 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACKALPHA && !ui)
1902 permutation |= SHADERPERMUTATION_FOGALPHAHACK;
1903 switch(vid.renderpath)
1905 case RENDERPATH_GL32:
1906 case RENDERPATH_GLES2:
1907 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | (rsurface.modellightmapcolor4f ? BATCHNEED_ARRAY_VERTEXCOLOR : 0) | BATCHNEED_ARRAY_TEXCOORD | (rsurface.uselightmaptexture ? BATCHNEED_ARRAY_LIGHTMAP : 0) | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
1908 RSurf_UploadBuffersForBatch();
1909 // this has to be after RSurf_PrepareVerticesForBatch
1910 if (rsurface.batchskeletaltransform3x4buffer)
1911 permutation |= SHADERPERMUTATION_SKELETAL;
1912 R_SetupShader_SetPermutationGLSL(mode, permutation);
1913 #ifndef USE_GLES2 /* FIXME: GLES3 only */
1914 if (r_glsl_permutation->ubiloc_Skeletal_Transform12_UniformBlock >= 0 && rsurface.batchskeletaltransform3x4buffer) qglBindBufferRange(GL_UNIFORM_BUFFER, r_glsl_permutation->ubibind_Skeletal_Transform12_UniformBlock, rsurface.batchskeletaltransform3x4buffer->bufferobject, rsurface.batchskeletaltransform3x4offset, rsurface.batchskeletaltransform3x4size);
1916 if (r_glsl_permutation->loc_ModelToReflectCube >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.matrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ModelToReflectCube, 1, false, m16f);}
1917 if (mode == SHADERMODE_LIGHTSOURCE)
1919 if (r_glsl_permutation->loc_ModelToLight >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ModelToLight, 1, false, m16f);}
1920 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3f(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
1921 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3f(r_glsl_permutation->loc_LightColor, 1, 1, 1); // DEPRECATED
1922 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, rtlightambient[0], rtlightambient[1], rtlightambient[2]);
1923 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, rtlightdiffuse[0], rtlightdiffuse[1], rtlightdiffuse[2]);
1924 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, rtlightspecular[0], rtlightspecular[1], rtlightspecular[2]);
1926 // additive passes are only darkened by fog, not tinted
1927 if (r_glsl_permutation->loc_FogColor >= 0)
1928 qglUniform3f(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1929 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1f(r_glsl_permutation->loc_SpecularPower, t->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
1933 if (mode == SHADERMODE_FLATCOLOR)
1935 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, t->render_modellight_ambient[0], t->render_modellight_ambient[1], t->render_modellight_ambient[2]);
1937 else if (mode == SHADERMODE_LIGHTGRID)
1939 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, t->render_lightmap_ambient[0], t->render_lightmap_ambient[1], t->render_lightmap_ambient[2]);
1940 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, t->render_lightmap_diffuse[0], t->render_lightmap_diffuse[1], t->render_lightmap_diffuse[2]);
1941 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, t->render_lightmap_specular[0], t->render_lightmap_specular[1], t->render_lightmap_specular[2]);
1942 // other LightGrid uniforms handled below
1944 else if (mode == SHADERMODE_LIGHTDIRECTION)
1946 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, t->render_modellight_ambient[0], t->render_modellight_ambient[1], t->render_modellight_ambient[2]);
1947 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, t->render_modellight_diffuse[0], t->render_modellight_diffuse[1], t->render_modellight_diffuse[2]);
1948 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, t->render_modellight_specular[0], t->render_modellight_specular[1], t->render_modellight_specular[2]);
1949 if (r_glsl_permutation->loc_DeferredMod_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Diffuse, t->render_rtlight_diffuse[0], t->render_rtlight_diffuse[1], t->render_rtlight_diffuse[2]);
1950 if (r_glsl_permutation->loc_DeferredMod_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Specular, t->render_rtlight_specular[0], t->render_rtlight_specular[1], t->render_rtlight_specular[2]);
1951 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3f(r_glsl_permutation->loc_LightColor, 1, 1, 1); // DEPRECATED
1952 if (r_glsl_permutation->loc_LightDir >= 0) qglUniform3f(r_glsl_permutation->loc_LightDir, t->render_modellight_lightdir_local[0], t->render_modellight_lightdir_local[1], t->render_modellight_lightdir_local[2]);
1956 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, t->render_lightmap_ambient[0], t->render_lightmap_ambient[1], t->render_lightmap_ambient[2]);
1957 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, t->render_lightmap_diffuse[0], t->render_lightmap_diffuse[1], t->render_lightmap_diffuse[2]);
1958 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, t->render_lightmap_specular[0], t->render_lightmap_specular[1], t->render_lightmap_specular[2]);
1959 if (r_glsl_permutation->loc_DeferredMod_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Diffuse, t->render_rtlight_diffuse[0], t->render_rtlight_diffuse[1], t->render_rtlight_diffuse[2]);
1960 if (r_glsl_permutation->loc_DeferredMod_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Specular, t->render_rtlight_specular[0], t->render_rtlight_specular[1], t->render_rtlight_specular[2]);
1962 // additive passes are only darkened by fog, not tinted
1963 if (r_glsl_permutation->loc_FogColor >= 0 && !ui)
1965 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACK0)
1966 qglUniform3f(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1968 qglUniform3f(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
1970 if (r_glsl_permutation->loc_DistortScaleRefractReflect >= 0) qglUniform4f(r_glsl_permutation->loc_DistortScaleRefractReflect, r_water_refractdistort.value * t->refractfactor, r_water_refractdistort.value * t->refractfactor, r_water_reflectdistort.value * t->reflectfactor, r_water_reflectdistort.value * t->reflectfactor);
1971 if (r_glsl_permutation->loc_ScreenScaleRefractReflect >= 0) qglUniform4f(r_glsl_permutation->loc_ScreenScaleRefractReflect, r_fb.water.screenscale[0], r_fb.water.screenscale[1], r_fb.water.screenscale[0], r_fb.water.screenscale[1]);
1972 if (r_glsl_permutation->loc_ScreenCenterRefractReflect >= 0) qglUniform4f(r_glsl_permutation->loc_ScreenCenterRefractReflect, r_fb.water.screencenter[0], r_fb.water.screencenter[1], r_fb.water.screencenter[0], r_fb.water.screencenter[1]);
1973 if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4f(r_glsl_permutation->loc_RefractColor, t->refractcolor4f[0], t->refractcolor4f[1], t->refractcolor4f[2], t->refractcolor4f[3] * t->currentalpha);
1974 if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4f(r_glsl_permutation->loc_ReflectColor, t->reflectcolor4f[0], t->reflectcolor4f[1], t->reflectcolor4f[2], t->reflectcolor4f[3] * t->currentalpha);
1975 if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1f(r_glsl_permutation->loc_ReflectFactor, t->reflectmax - t->reflectmin);
1976 if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1f(r_glsl_permutation->loc_ReflectOffset, t->reflectmin);
1977 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1f(r_glsl_permutation->loc_SpecularPower, t->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
1978 if (r_glsl_permutation->loc_NormalmapScrollBlend >= 0) qglUniform2f(r_glsl_permutation->loc_NormalmapScrollBlend, t->r_water_waterscroll[0], t->r_water_waterscroll[1]);
1980 if (r_glsl_permutation->loc_TexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&t->currenttexmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_TexMatrix, 1, false, m16f);}
1981 if (r_glsl_permutation->loc_BackgroundTexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&t->currentbackgroundtexmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_BackgroundTexMatrix, 1, false, m16f);}
1982 if (r_glsl_permutation->loc_ShadowMapMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&r_shadow_shadowmapmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ShadowMapMatrix, 1, false, m16f);}
1983 if (permutation & SHADERPERMUTATION_SHADOWMAPORTHO)
1985 if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform4f(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_modelshadowmap_texturescale[0], r_shadow_modelshadowmap_texturescale[1], r_shadow_modelshadowmap_texturescale[2], r_shadow_modelshadowmap_texturescale[3]);
1986 if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4f(r_glsl_permutation->loc_ShadowMap_Parameters, r_shadow_modelshadowmap_parameters[0], r_shadow_modelshadowmap_parameters[1], r_shadow_modelshadowmap_parameters[2], r_shadow_modelshadowmap_parameters[3]);
1990 if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform4f(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_lightshadowmap_texturescale[0], r_shadow_lightshadowmap_texturescale[1], r_shadow_lightshadowmap_texturescale[2], r_shadow_lightshadowmap_texturescale[3]);
1991 if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4f(r_glsl_permutation->loc_ShadowMap_Parameters, r_shadow_lightshadowmap_parameters[0], r_shadow_lightshadowmap_parameters[1], r_shadow_lightshadowmap_parameters[2], r_shadow_lightshadowmap_parameters[3]);
1994 if (r_glsl_permutation->loc_Color_Glow >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Glow, t->render_glowmod[0], t->render_glowmod[1], t->render_glowmod[2]);
1995 if (r_glsl_permutation->loc_Alpha >= 0) qglUniform1f(r_glsl_permutation->loc_Alpha, t->currentalpha * ((t->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay) ? t->r_water_wateralpha : 1));
1996 if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3f(r_glsl_permutation->loc_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
1997 if (r_glsl_permutation->loc_Color_Pants >= 0)
1999 if (t->pantstexture)
2000 qglUniform3f(r_glsl_permutation->loc_Color_Pants, t->render_colormap_pants[0], t->render_colormap_pants[1], t->render_colormap_pants[2]);
2002 qglUniform3f(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
2004 if (r_glsl_permutation->loc_Color_Shirt >= 0)
2006 if (t->shirttexture)
2007 qglUniform3f(r_glsl_permutation->loc_Color_Shirt, t->render_colormap_shirt[0], t->render_colormap_shirt[1], t->render_colormap_shirt[2]);
2009 qglUniform3f(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
2011 if (r_glsl_permutation->loc_FogPlane >= 0) qglUniform4f(r_glsl_permutation->loc_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]);
2012 if (r_glsl_permutation->loc_FogPlaneViewDist >= 0) qglUniform1f(r_glsl_permutation->loc_FogPlaneViewDist, rsurface.fogplaneviewdist);
2013 if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1f(r_glsl_permutation->loc_FogRangeRecip, rsurface.fograngerecip);
2014 if (r_glsl_permutation->loc_FogHeightFade >= 0) qglUniform1f(r_glsl_permutation->loc_FogHeightFade, rsurface.fogheightfade);
2015 if (r_glsl_permutation->loc_OffsetMapping_ScaleSteps >= 0) qglUniform4f(r_glsl_permutation->loc_OffsetMapping_ScaleSteps,
2016 r_glsl_offsetmapping_scale.value*t->offsetscale,
2017 max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2018 1.0 / max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2019 max(1, r_glsl_offsetmapping_reliefmapping_refinesteps.integer)
2021 if (r_glsl_permutation->loc_OffsetMapping_LodDistance >= 0) qglUniform1f(r_glsl_permutation->loc_OffsetMapping_LodDistance, r_glsl_offsetmapping_lod_distance.integer * r_refdef.view.quality);
2022 if (r_glsl_permutation->loc_OffsetMapping_Bias >= 0) qglUniform1f(r_glsl_permutation->loc_OffsetMapping_Bias, t->offsetbias);
2023 if (r_glsl_permutation->loc_ScreenToDepth >= 0) qglUniform2f(r_glsl_permutation->loc_ScreenToDepth, r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]);
2024 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
2025 if (r_glsl_permutation->loc_BounceGridMatrix >= 0) {Matrix4x4_Concat(&tempmatrix, &r_shadow_bouncegrid_state.matrix, &rsurface.matrix);Matrix4x4_ToArrayFloatGL(&tempmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_BounceGridMatrix, 1, false, m16f);}
2026 if (r_glsl_permutation->loc_BounceGridIntensity >= 0) qglUniform1f(r_glsl_permutation->loc_BounceGridIntensity, r_shadow_bouncegrid_state.intensity*r_refdef.view.colorscale);
2027 if (r_glsl_permutation->loc_LightGridMatrix >= 0 && r_refdef.scene.worldmodel)
2030 Matrix4x4_Concat(&tempmatrix, &r_refdef.scene.worldmodel->brushq3.lightgridworldtotexturematrix, &rsurface.matrix);
2031 Matrix4x4_ToArrayFloatGL(&tempmatrix, m16f);
2032 qglUniformMatrix4fv(r_glsl_permutation->loc_LightGridMatrix, 1, false, m16f);
2033 Matrix4x4_Normalize3(&tempmatrix, &rsurface.matrix);
2034 Matrix4x4_ToArrayFloatGL(&tempmatrix, m16f);
2035 m9f[0] = m16f[0];m9f[1] = m16f[1];m9f[2] = m16f[2];
2036 m9f[3] = m16f[4];m9f[4] = m16f[5];m9f[5] = m16f[6];
2037 m9f[6] = m16f[8];m9f[7] = m16f[9];m9f[8] = m16f[10];
2038 qglUniformMatrix3fv(r_glsl_permutation->loc_LightGridNormalMatrix, 1, false, m9f);
2041 if (r_glsl_permutation->tex_Texture_First >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , r_texture_white );
2042 if (r_glsl_permutation->tex_Texture_Second >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second , r_texture_white );
2043 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps , r_texture_gammaramps );
2044 if (r_glsl_permutation->tex_Texture_Normal >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Normal , t->nmaptexture );
2045 if (r_glsl_permutation->tex_Texture_Color >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Color , t->basetexture );
2046 if (r_glsl_permutation->tex_Texture_Gloss >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Gloss , t->glosstexture );
2047 if (r_glsl_permutation->tex_Texture_Glow >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Glow , t->glowtexture );
2048 if (r_glsl_permutation->tex_Texture_SecondaryNormal >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryNormal , t->backgroundnmaptexture );
2049 if (r_glsl_permutation->tex_Texture_SecondaryColor >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryColor , t->backgroundbasetexture );
2050 if (r_glsl_permutation->tex_Texture_SecondaryGloss >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryGloss , t->backgroundglosstexture );
2051 if (r_glsl_permutation->tex_Texture_SecondaryGlow >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryGlow , t->backgroundglowtexture );
2052 if (r_glsl_permutation->tex_Texture_Pants >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Pants , t->pantstexture );
2053 if (r_glsl_permutation->tex_Texture_Shirt >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Shirt , t->shirttexture );
2054 if (r_glsl_permutation->tex_Texture_ReflectMask >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ReflectMask , t->reflectmasktexture );
2055 if (r_glsl_permutation->tex_Texture_ReflectCube >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ReflectCube , t->reflectcubetexture ? t->reflectcubetexture : r_texture_whitecube);
2056 if (r_glsl_permutation->tex_Texture_FogHeightTexture>= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_FogHeightTexture , r_texture_fogheighttexture );
2057 if (r_glsl_permutation->tex_Texture_FogMask >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_FogMask , r_texture_fogattenuation );
2058 if (r_glsl_permutation->tex_Texture_Lightmap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Lightmap , rsurface.lightmaptexture ? rsurface.lightmaptexture : r_texture_white);
2059 if (r_glsl_permutation->tex_Texture_Deluxemap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Deluxemap , rsurface.deluxemaptexture ? rsurface.deluxemaptexture : r_texture_blanknormalmap);
2060 if (r_glsl_permutation->tex_Texture_Attenuation >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Attenuation , r_shadow_attenuationgradienttexture );
2061 if (rsurfacepass == RSURFPASS_BACKGROUND)
2063 if (r_glsl_permutation->tex_Texture_Refraction >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Refraction , waterplane->rt_refraction ? waterplane->rt_refraction->colortexture[0] : r_texture_black);
2064 if (r_glsl_permutation->tex_Texture_First >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , waterplane->rt_camera ? waterplane->rt_camera->colortexture[0] : r_texture_black);
2065 if (r_glsl_permutation->tex_Texture_Reflection >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Reflection , waterplane->rt_reflection ? waterplane->rt_reflection->colortexture[0] : r_texture_black);
2069 if (r_glsl_permutation->tex_Texture_Reflection >= 0 && waterplane) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Reflection , waterplane->rt_reflection ? waterplane->rt_reflection->colortexture[0] : r_texture_black);
2071 if (r_glsl_permutation->tex_Texture_ScreenNormalMap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenNormalMap , r_shadow_prepassgeometrynormalmaptexture );
2072 if (r_glsl_permutation->tex_Texture_ScreenDiffuse >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenDiffuse , r_shadow_prepasslightingdiffusetexture );
2073 if (r_glsl_permutation->tex_Texture_ScreenSpecular >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenSpecular , r_shadow_prepasslightingspeculartexture );
2074 if (rsurface.rtlight || (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW)))
2076 if (r_glsl_permutation->tex_Texture_ShadowMap2D >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ShadowMap2D, r_shadow_shadowmap2ddepthtexture );
2077 if (rsurface.rtlight)
2079 if (r_glsl_permutation->tex_Texture_Cube >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Cube , rsurface.rtlight->currentcubemap );
2080 if (r_glsl_permutation->tex_Texture_CubeProjection >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_CubeProjection , r_shadow_shadowmapvsdcttexture );
2083 if (r_glsl_permutation->tex_Texture_BounceGrid >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_BounceGrid, r_shadow_bouncegrid_state.texture);
2084 if (r_glsl_permutation->tex_Texture_LightGrid >= 0 && r_refdef.scene.worldmodel) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_LightGrid, r_refdef.scene.worldmodel->brushq3.lightgridtexture);
2090 void R_SetupShader_DeferredLight(const rtlight_t *rtlight)
2092 // select a permutation of the lighting shader appropriate to this
2093 // combination of texture, entity, light source, and fogging, only use the
2094 // minimum features necessary to avoid wasting rendering time in the
2095 // fragment shader on features that are not being used
2096 uint64_t permutation = 0;
2097 unsigned int mode = 0;
2098 const float *lightcolorbase = rtlight->currentcolor;
2099 float ambientscale = rtlight->ambientscale;
2100 float diffusescale = rtlight->diffusescale;
2101 float specularscale = rtlight->specularscale;
2102 // this is the location of the light in view space
2103 vec3_t viewlightorigin;
2104 // this transforms from view space (camera) to light space (cubemap)
2105 matrix4x4_t viewtolight;
2106 matrix4x4_t lighttoview;
2107 float viewtolight16f[16];
2109 mode = SHADERMODE_DEFERREDLIGHTSOURCE;
2110 if (rtlight->currentcubemap != r_texture_whitecube)
2111 permutation |= SHADERPERMUTATION_CUBEFILTER;
2112 if (diffusescale > 0)
2113 permutation |= SHADERPERMUTATION_DIFFUSE;
2114 if (specularscale > 0 && r_shadow_gloss.integer > 0)
2115 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2116 if (r_shadow_usingshadowmap2d)
2118 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2119 if (r_shadow_shadowmapvsdct)
2120 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
2122 if (r_shadow_shadowmap2ddepthbuffer)
2123 permutation |= SHADERPERMUTATION_DEPTHRGB;
2125 if (vid.allowalphatocoverage)
2126 GL_AlphaToCoverage(false);
2127 Matrix4x4_Transform(&r_refdef.view.viewport.viewmatrix, rtlight->shadoworigin, viewlightorigin);
2128 Matrix4x4_Concat(&lighttoview, &r_refdef.view.viewport.viewmatrix, &rtlight->matrix_lighttoworld);
2129 Matrix4x4_Invert_Full(&viewtolight, &lighttoview);
2130 Matrix4x4_ToArrayFloatGL(&viewtolight, viewtolight16f);
2131 switch(vid.renderpath)
2133 case RENDERPATH_GL32:
2134 case RENDERPATH_GLES2:
2135 R_SetupShader_SetPermutationGLSL(mode, permutation);
2136 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3f( r_glsl_permutation->loc_LightPosition , viewlightorigin[0], viewlightorigin[1], viewlightorigin[2]);
2137 if (r_glsl_permutation->loc_ViewToLight >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ViewToLight , 1, false, viewtolight16f);
2138 if (r_glsl_permutation->loc_DeferredColor_Ambient >= 0) qglUniform3f( r_glsl_permutation->loc_DeferredColor_Ambient , lightcolorbase[0] * ambientscale , lightcolorbase[1] * ambientscale , lightcolorbase[2] * ambientscale );
2139 if (r_glsl_permutation->loc_DeferredColor_Diffuse >= 0) qglUniform3f( r_glsl_permutation->loc_DeferredColor_Diffuse , lightcolorbase[0] * diffusescale , lightcolorbase[1] * diffusescale , lightcolorbase[2] * diffusescale );
2140 if (r_glsl_permutation->loc_DeferredColor_Specular >= 0) qglUniform3f( r_glsl_permutation->loc_DeferredColor_Specular , lightcolorbase[0] * specularscale, lightcolorbase[1] * specularscale, lightcolorbase[2] * specularscale);
2141 if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform4f( r_glsl_permutation->loc_ShadowMap_TextureScale , r_shadow_lightshadowmap_texturescale[0], r_shadow_lightshadowmap_texturescale[1], r_shadow_lightshadowmap_texturescale[2], r_shadow_lightshadowmap_texturescale[3]);
2142 if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4f( r_glsl_permutation->loc_ShadowMap_Parameters , r_shadow_lightshadowmap_parameters[0], r_shadow_lightshadowmap_parameters[1], r_shadow_lightshadowmap_parameters[2], r_shadow_lightshadowmap_parameters[3]);
2143 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1f( r_glsl_permutation->loc_SpecularPower , (r_shadow_gloss.integer == 2 ? r_shadow_gloss2exponent.value : r_shadow_glossexponent.value) * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
2144 if (r_glsl_permutation->loc_ScreenToDepth >= 0) qglUniform2f( r_glsl_permutation->loc_ScreenToDepth , r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]);
2145 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f( r_glsl_permutation->loc_PixelToScreenTexCoord , 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
2147 if (r_glsl_permutation->tex_Texture_Attenuation >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Attenuation , r_shadow_attenuationgradienttexture );
2148 if (r_glsl_permutation->tex_Texture_ScreenNormalMap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenNormalMap , r_shadow_prepassgeometrynormalmaptexture );
2149 if (r_glsl_permutation->tex_Texture_Cube >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Cube , rsurface.rtlight->currentcubemap );
2150 if (r_glsl_permutation->tex_Texture_ShadowMap2D >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ShadowMap2D , r_shadow_shadowmap2ddepthtexture );
2151 if (r_glsl_permutation->tex_Texture_CubeProjection >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_CubeProjection , r_shadow_shadowmapvsdcttexture );
2156 #define SKINFRAME_HASH 1024
2160 unsigned int loadsequence; // incremented each level change
2161 memexpandablearray_t array;
2162 skinframe_t *hash[SKINFRAME_HASH];
2165 r_skinframe_t r_skinframe;
2167 void R_SkinFrame_PrepareForPurge(void)
2169 r_skinframe.loadsequence++;
2170 // wrap it without hitting zero
2171 if (r_skinframe.loadsequence >= 200)
2172 r_skinframe.loadsequence = 1;
2175 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
2179 // mark the skinframe as used for the purging code
2180 skinframe->loadsequence = r_skinframe.loadsequence;
2183 void R_SkinFrame_PurgeSkinFrame(skinframe_t *s)
2187 if (s->merged == s->base)
2189 R_PurgeTexture(s->stain); s->stain = NULL;
2190 R_PurgeTexture(s->merged); s->merged = NULL;
2191 R_PurgeTexture(s->base); s->base = NULL;
2192 R_PurgeTexture(s->pants); s->pants = NULL;
2193 R_PurgeTexture(s->shirt); s->shirt = NULL;
2194 R_PurgeTexture(s->nmap); s->nmap = NULL;
2195 R_PurgeTexture(s->gloss); s->gloss = NULL;
2196 R_PurgeTexture(s->glow); s->glow = NULL;
2197 R_PurgeTexture(s->fog); s->fog = NULL;
2198 R_PurgeTexture(s->reflect); s->reflect = NULL;
2199 s->loadsequence = 0;
2202 void R_SkinFrame_Purge(void)
2206 for (i = 0;i < SKINFRAME_HASH;i++)
2208 for (s = r_skinframe.hash[i];s;s = s->next)
2210 if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
2211 R_SkinFrame_PurgeSkinFrame(s);
2216 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
2218 char basename[MAX_QPATH];
2220 Image_StripImageExtension(name, basename, sizeof(basename));
2222 if( last == NULL ) {
2224 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2225 item = r_skinframe.hash[hashindex];
2230 // linearly search through the hash bucket
2231 for( ; item ; item = item->next ) {
2232 if( !strcmp( item->basename, basename ) ) {
2239 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qbool add)
2242 int compareflags = textureflags & TEXF_IMPORTANTBITS;
2244 char basename[MAX_QPATH];
2246 Image_StripImageExtension(name, basename, sizeof(basename));
2248 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2249 for (item = r_skinframe.hash[hashindex];item;item = item->next)
2250 if (!strcmp(item->basename, basename) &&
2251 item->textureflags == compareflags &&
2252 item->comparewidth == comparewidth &&
2253 item->compareheight == compareheight &&
2254 item->comparecrc == comparecrc)
2261 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
2262 memset(item, 0, sizeof(*item));
2263 strlcpy(item->basename, basename, sizeof(item->basename));
2264 item->textureflags = compareflags;
2265 item->comparewidth = comparewidth;
2266 item->compareheight = compareheight;
2267 item->comparecrc = comparecrc;
2268 item->next = r_skinframe.hash[hashindex];
2269 r_skinframe.hash[hashindex] = item;
2271 else if (textureflags & TEXF_FORCE_RELOAD)
2272 R_SkinFrame_PurgeSkinFrame(item);
2274 R_SkinFrame_MarkUsed(item);
2278 #define R_SKINFRAME_LOAD_AVERAGE_COLORS(cnt, getpixel) \
2280 unsigned long long avgcolor[5], wsum; \
2288 for(pix = 0; pix < cnt; ++pix) \
2291 for(comp = 0; comp < 3; ++comp) \
2293 if(w) /* ignore perfectly black pixels because that is better for model skins */ \
2296 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2298 for(comp = 0; comp < 3; ++comp) \
2299 avgcolor[comp] += getpixel * w; \
2302 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2303 avgcolor[4] += getpixel; \
2305 if(avgcolor[3] == 0) /* no pixels seen? even worse */ \
2307 skinframe->avgcolor[0] = avgcolor[2] / (255.0 * avgcolor[3]); \
2308 skinframe->avgcolor[1] = avgcolor[1] / (255.0 * avgcolor[3]); \
2309 skinframe->avgcolor[2] = avgcolor[0] / (255.0 * avgcolor[3]); \
2310 skinframe->avgcolor[3] = avgcolor[4] / (255.0 * cnt); \
2313 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qbool complain, qbool fallbacknotexture)
2315 skinframe_t *skinframe;
2317 if (cls.state == ca_dedicated)
2320 // return an existing skinframe if already loaded
2321 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
2322 if (skinframe && skinframe->base)
2325 // if the skinframe doesn't exist this will create it
2326 return R_SkinFrame_LoadExternal_SkinFrame(skinframe, name, textureflags, complain, fallbacknotexture);
2329 extern cvar_t gl_picmip;
2330 skinframe_t *R_SkinFrame_LoadExternal_SkinFrame(skinframe_t *skinframe, const char *name, int textureflags, qbool complain, qbool fallbacknotexture)
2333 unsigned char *pixels;
2334 unsigned char *bumppixels;
2335 unsigned char *basepixels = NULL;
2336 int basepixels_width = 0;
2337 int basepixels_height = 0;
2338 rtexture_t *ddsbase = NULL;
2339 qbool ddshasalpha = false;
2340 float ddsavgcolor[4];
2341 char basename[MAX_QPATH];
2342 int miplevel = R_PicmipForFlags(textureflags);
2343 int savemiplevel = miplevel;
2347 if (cls.state == ca_dedicated)
2350 Image_StripImageExtension(name, basename, sizeof(basename));
2352 // check for DDS texture file first
2353 if (!r_loaddds || !(ddsbase = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s.dds", basename), vid.sRGB3D, textureflags, &ddshasalpha, ddsavgcolor, miplevel, false)))
2355 basepixels = loadimagepixelsbgra(name, complain, true, false, &miplevel);
2356 if (basepixels == NULL && fallbacknotexture)
2357 basepixels = Image_GenerateNoTexture();
2358 if (basepixels == NULL)
2362 // FIXME handle miplevel
2364 if (developer_loading.integer)
2365 Con_Printf("loading skin \"%s\"\n", name);
2367 // we've got some pixels to store, so really allocate this new texture now
2369 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
2370 textureflags &= ~TEXF_FORCE_RELOAD;
2371 skinframe->stain = NULL;
2372 skinframe->merged = NULL;
2373 skinframe->base = NULL;
2374 skinframe->pants = NULL;
2375 skinframe->shirt = NULL;
2376 skinframe->nmap = NULL;
2377 skinframe->gloss = NULL;
2378 skinframe->glow = NULL;
2379 skinframe->fog = NULL;
2380 skinframe->reflect = NULL;
2381 skinframe->hasalpha = false;
2382 // we could store the q2animname here too
2386 skinframe->base = ddsbase;
2387 skinframe->hasalpha = ddshasalpha;
2388 VectorCopy(ddsavgcolor, skinframe->avgcolor);
2389 if (r_loadfog && skinframe->hasalpha)
2390 skinframe->fog = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_mask.dds", skinframe->basename), false, textureflags | TEXF_ALPHA, NULL, NULL, miplevel, true);
2391 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2395 basepixels_width = image_width;
2396 basepixels_height = image_height;
2397 skinframe->base = R_LoadTexture2D (r_main_texturepool, skinframe->basename, basepixels_width, basepixels_height, basepixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_color.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), miplevel, NULL);
2398 if (textureflags & TEXF_ALPHA)
2400 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
2402 if (basepixels[j] < 255)
2404 skinframe->hasalpha = true;
2408 if (r_loadfog && skinframe->hasalpha)
2410 // has transparent pixels
2411 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2412 for (j = 0;j < image_width * image_height * 4;j += 4)
2417 pixels[j+3] = basepixels[j+3];
2419 skinframe->fog = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_mask", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, textureflags & (gl_texturecompression_color.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), miplevel, NULL);
2423 R_SKINFRAME_LOAD_AVERAGE_COLORS(basepixels_width * basepixels_height, basepixels[4 * pix + comp]);
2425 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2426 if (r_savedds && skinframe->base)
2427 R_SaveTextureDDSFile(skinframe->base, va(vabuf, sizeof(vabuf), "dds/%s.dds", skinframe->basename), r_texture_dds_save.integer < 2, skinframe->hasalpha);
2428 if (r_savedds && skinframe->fog)
2429 R_SaveTextureDDSFile(skinframe->fog, va(vabuf, sizeof(vabuf), "dds/%s_mask.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2435 mymiplevel = savemiplevel;
2436 if (r_loadnormalmap)
2437 skinframe->nmap = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_norm.dds", skinframe->basename), false, (TEXF_ALPHA | textureflags) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP), NULL, NULL, mymiplevel, true);
2438 skinframe->glow = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_glow.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2440 skinframe->gloss = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_gloss.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2441 skinframe->pants = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_pants.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2442 skinframe->shirt = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_shirt.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2443 skinframe->reflect = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_reflect.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel, true);
2446 // _norm is the name used by tenebrae and has been adopted as standard
2447 if (r_loadnormalmap && skinframe->nmap == NULL)
2449 mymiplevel = savemiplevel;
2450 if ((pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_norm", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
2452 skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | textureflags) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP) & (gl_texturecompression_normal.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2456 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_bump", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
2458 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2459 Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
2460 skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | textureflags) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP) & (gl_texturecompression_normal.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2462 Mem_Free(bumppixels);
2464 else if (r_shadow_bumpscale_basetexture.value > 0)
2466 pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
2467 Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
2468 skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), basepixels_width, basepixels_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | textureflags) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP) & (gl_texturecompression_normal.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2472 if (r_savedds && skinframe->nmap)
2473 R_SaveTextureDDSFile(skinframe->nmap, va(vabuf, sizeof(vabuf), "dds/%s_norm.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2477 // _luma is supported only for tenebrae compatibility
2478 // _blend and .blend are supported only for Q3 & QL compatibility, this hack can be removed if better Q3 shader support is implemented
2479 // _glow is the preferred name
2480 mymiplevel = savemiplevel;
2481 if (skinframe->glow == NULL && ((pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_glow", skinframe->basename), false, false, false, &mymiplevel)) || (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s.blend", skinframe->basename), false, false, false, &mymiplevel)) || (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_blend", skinframe->basename), false, false, false, &mymiplevel)) || (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_luma", skinframe->basename), false, false, false, &mymiplevel))))
2483 skinframe->glow = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_glow", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_glow.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2485 if (r_savedds && skinframe->glow)
2486 R_SaveTextureDDSFile(skinframe->glow, va(vabuf, sizeof(vabuf), "dds/%s_glow.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2488 Mem_Free(pixels);pixels = NULL;
2491 mymiplevel = savemiplevel;
2492 if (skinframe->gloss == NULL && r_loadgloss && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_gloss", skinframe->basename), false, false, false, &mymiplevel)))
2494 skinframe->gloss = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_gloss", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, (TEXF_ALPHA | textureflags) & (gl_texturecompression_gloss.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2496 if (r_savedds && skinframe->gloss)
2497 R_SaveTextureDDSFile(skinframe->gloss, va(vabuf, sizeof(vabuf), "dds/%s_gloss.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2503 mymiplevel = savemiplevel;
2504 if (skinframe->pants == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_pants", skinframe->basename), false, false, false, &mymiplevel)))
2506 skinframe->pants = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_pants", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_color.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2508 if (r_savedds && skinframe->pants)
2509 R_SaveTextureDDSFile(skinframe->pants, va(vabuf, sizeof(vabuf), "dds/%s_pants.dds", skinframe->basename), r_texture_dds_save.integer < 2, false);
2515 mymiplevel = savemiplevel;
2516 if (skinframe->shirt == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_shirt", skinframe->basename), false, false, false, &mymiplevel)))
2518 skinframe->shirt = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_shirt", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_color.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2520 if (r_savedds && skinframe->shirt)
2521 R_SaveTextureDDSFile(skinframe->shirt, va(vabuf, sizeof(vabuf), "dds/%s_shirt.dds", skinframe->basename), r_texture_dds_save.integer < 2, false);
2527 mymiplevel = savemiplevel;
2528 if (skinframe->reflect == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_reflect", skinframe->basename), false, false, false, &mymiplevel)))
2530 skinframe->reflect = R_LoadTexture2D (r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_reflect", skinframe->basename), image_width, image_height, pixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags & (gl_texturecompression_reflectmask.integer && gl_texturecompression.integer ? ~0 : ~TEXF_COMPRESS), mymiplevel, NULL);
2532 if (r_savedds && skinframe->reflect)
2533 R_SaveTextureDDSFile(skinframe->reflect, va(vabuf, sizeof(vabuf), "dds/%s_reflect.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
2540 Mem_Free(basepixels);
2545 skinframe_t *R_SkinFrame_LoadInternalBGRA(const char *name, int textureflags, const unsigned char *skindata, int width, int height, int comparewidth, int compareheight, int comparecrc, qbool sRGB)
2548 skinframe_t *skinframe;
2551 if (cls.state == ca_dedicated)
2554 // if already loaded just return it, otherwise make a new skinframe
2555 skinframe = R_SkinFrame_Find(name, textureflags, comparewidth, compareheight, comparecrc, true);
2556 if (skinframe->base)
2558 textureflags &= ~TEXF_FORCE_RELOAD;
2560 skinframe->stain = NULL;
2561 skinframe->merged = NULL;
2562 skinframe->base = NULL;
2563 skinframe->pants = NULL;
2564 skinframe->shirt = NULL;
2565 skinframe->nmap = NULL;
2566 skinframe->gloss = NULL;
2567 skinframe->glow = NULL;
2568 skinframe->fog = NULL;
2569 skinframe->reflect = NULL;
2570 skinframe->hasalpha = false;
2572 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2576 if (developer_loading.integer)
2577 Con_Printf("loading 32bit skin \"%s\"\n", name);
2579 if (r_loadnormalmap && r_shadow_bumpscale_basetexture.value > 0)
2581 unsigned char *a = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2582 unsigned char *b = a + width * height * 4;
2583 Image_HeightmapToNormalmap_BGRA(skindata, b, width, height, false, r_shadow_bumpscale_basetexture.value);
2584 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), width, height, b, TEXTYPE_BGRA, (textureflags | TEXF_ALPHA) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP), -1, NULL);
2587 skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, sRGB ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags, -1, NULL);
2588 if (textureflags & TEXF_ALPHA)
2590 for (i = 3;i < width * height * 4;i += 4)
2592 if (skindata[i] < 255)
2594 skinframe->hasalpha = true;
2598 if (r_loadfog && skinframe->hasalpha)
2600 unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
2601 memcpy(fogpixels, skindata, width * height * 4);
2602 for (i = 0;i < width * height * 4;i += 4)
2603 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
2604 skinframe->fog = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, textureflags, -1, NULL);
2605 Mem_Free(fogpixels);
2609 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, skindata[4 * pix + comp]);
2610 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2615 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
2619 skinframe_t *skinframe;
2621 if (cls.state == ca_dedicated)
2624 // if already loaded just return it, otherwise make a new skinframe
2625 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
2626 if (skinframe->base)
2628 //textureflags &= ~TEXF_FORCE_RELOAD;
2630 skinframe->stain = NULL;
2631 skinframe->merged = NULL;
2632 skinframe->base = NULL;
2633 skinframe->pants = NULL;
2634 skinframe->shirt = NULL;
2635 skinframe->nmap = NULL;
2636 skinframe->gloss = NULL;
2637 skinframe->glow = NULL;
2638 skinframe->fog = NULL;
2639 skinframe->reflect = NULL;
2640 skinframe->hasalpha = false;
2642 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2646 if (developer_loading.integer)
2647 Con_Printf("loading quake skin \"%s\"\n", name);
2649 // we actually don't upload anything until the first use, because mdl skins frequently go unused, and are almost never used in both modes (colormapped and non-colormapped)
2650 skinframe->qpixels = (unsigned char *)Mem_Alloc(r_main_mempool, width*height); // FIXME LEAK
2651 memcpy(skinframe->qpixels, skindata, width*height);
2652 skinframe->qwidth = width;
2653 skinframe->qheight = height;
2656 for (i = 0;i < width * height;i++)
2657 featuresmask |= palette_featureflags[skindata[i]];
2659 skinframe->hasalpha = false;
2662 skinframe->hasalpha = true;
2663 skinframe->qhascolormapping = loadpantsandshirt && (featuresmask & (PALETTEFEATURE_PANTS | PALETTEFEATURE_SHIRT));
2664 skinframe->qgeneratenmap = r_shadow_bumpscale_basetexture.value > 0;
2665 skinframe->qgeneratemerged = true;
2666 skinframe->qgeneratebase = skinframe->qhascolormapping;
2667 skinframe->qgenerateglow = loadglowtexture && (featuresmask & PALETTEFEATURE_GLOW);
2669 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette_bgra_complete)[skindata[pix]*4 + comp]);
2670 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2675 static void R_SkinFrame_GenerateTexturesFromQPixels(skinframe_t *skinframe, qbool colormapped)
2679 unsigned char *skindata;
2682 if (!skinframe->qpixels)
2685 if (!skinframe->qhascolormapping)
2686 colormapped = false;
2690 if (!skinframe->qgeneratebase)
2695 if (!skinframe->qgeneratemerged)
2699 width = skinframe->qwidth;
2700 height = skinframe->qheight;
2701 skindata = skinframe->qpixels;
2703 if (skinframe->qgeneratenmap)
2705 unsigned char *a, *b;
2706 skinframe->qgeneratenmap = false;
2707 a = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2708 b = a + width * height * 4;
2709 // use either a custom palette or the quake palette
2710 Image_Copy8bitBGRA(skindata, a, width * height, palette_bgra_complete);
2711 Image_HeightmapToNormalmap_BGRA(a, b, width, height, false, r_shadow_bumpscale_basetexture.value);
2712 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), width, height, b, TEXTYPE_BGRA, (skinframe->textureflags | TEXF_ALPHA) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP), -1, NULL);
2716 if (skinframe->qgenerateglow)
2718 skinframe->qgenerateglow = false;
2719 if (skinframe->hasalpha) // fence textures
2720 skinframe->glow = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_glow", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags | TEXF_ALPHA, -1, palette_bgra_onlyfullbrights_transparent); // glow
2722 skinframe->glow = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_glow", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, palette_bgra_onlyfullbrights); // glow
2727 skinframe->qgeneratebase = false;
2728 skinframe->base = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nospecial", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, skinframe->glow ? palette_bgra_nocolormapnofullbrights : palette_bgra_nocolormap);
2729 skinframe->pants = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_pants", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, palette_bgra_pantsaswhite);
2730 skinframe->shirt = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_shirt", skinframe->basename), width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, palette_bgra_shirtaswhite);
2734 skinframe->qgeneratemerged = false;
2735 if (skinframe->hasalpha) // fence textures
2736 skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags | TEXF_ALPHA, -1, skinframe->glow ? palette_bgra_nofullbrights_transparent : palette_bgra_transparent);
2738 skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, vid.sRGB3D ? TEXTYPE_SRGB_PALETTE : TEXTYPE_PALETTE, skinframe->textureflags, -1, skinframe->glow ? palette_bgra_nofullbrights : palette_bgra_complete);
2741 if (!skinframe->qgeneratemerged && !skinframe->qgeneratebase)
2743 Mem_Free(skinframe->qpixels);
2744 skinframe->qpixels = NULL;
2748 skinframe_t *R_SkinFrame_LoadInternal8bit(const char *name, int textureflags, const unsigned char *skindata, int width, int height, const unsigned int *palette, const unsigned int *alphapalette)
2751 skinframe_t *skinframe;
2754 if (cls.state == ca_dedicated)
2757 // if already loaded just return it, otherwise make a new skinframe
2758 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
2759 if (skinframe->base)
2761 textureflags &= ~TEXF_FORCE_RELOAD;
2763 skinframe->stain = NULL;
2764 skinframe->merged = NULL;
2765 skinframe->base = NULL;
2766 skinframe->pants = NULL;
2767 skinframe->shirt = NULL;
2768 skinframe->nmap = NULL;
2769 skinframe->gloss = NULL;
2770 skinframe->glow = NULL;
2771 skinframe->fog = NULL;
2772 skinframe->reflect = NULL;
2773 skinframe->hasalpha = false;
2775 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2779 if (developer_loading.integer)
2780 Con_Printf("loading embedded 8bit image \"%s\"\n", name);
2782 skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_PALETTE, textureflags, -1, palette);
2783 if ((textureflags & TEXF_ALPHA) && alphapalette)
2785 for (i = 0;i < width * height;i++)
2787 if (((unsigned char *)palette)[skindata[i]*4+3] < 255)
2789 skinframe->hasalpha = true;
2793 if (r_loadfog && skinframe->hasalpha)
2794 skinframe->fog = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_fog", skinframe->basename), width, height, skindata, TEXTYPE_PALETTE, textureflags, -1, alphapalette);
2797 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette)[skindata[pix]*4 + comp]);
2798 //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2803 skinframe_t *R_SkinFrame_LoadMissing(void)
2805 skinframe_t *skinframe;
2807 if (cls.state == ca_dedicated)
2810 skinframe = R_SkinFrame_Find("missing", TEXF_FORCENEAREST, 0, 0, 0, true);
2811 skinframe->stain = NULL;
2812 skinframe->merged = NULL;
2813 skinframe->base = NULL;
2814 skinframe->pants = NULL;
2815 skinframe->shirt = NULL;
2816 skinframe->nmap = NULL;
2817 skinframe->gloss = NULL;
2818 skinframe->glow = NULL;
2819 skinframe->fog = NULL;
2820 skinframe->reflect = NULL;
2821 skinframe->hasalpha = false;
2823 skinframe->avgcolor[0] = rand() / RAND_MAX;
2824 skinframe->avgcolor[1] = rand() / RAND_MAX;
2825 skinframe->avgcolor[2] = rand() / RAND_MAX;
2826 skinframe->avgcolor[3] = 1;
2831 skinframe_t *R_SkinFrame_LoadNoTexture(void)
2833 if (cls.state == ca_dedicated)
2836 return R_SkinFrame_LoadInternalBGRA("notexture", TEXF_FORCENEAREST, Image_GenerateNoTexture(), 16, 16, 0, 0, 0, false);
2839 skinframe_t *R_SkinFrame_LoadInternalUsingTexture(const char *name, int textureflags, rtexture_t *tex, int width, int height, qbool sRGB)
2841 skinframe_t *skinframe;
2842 if (cls.state == ca_dedicated)
2844 // if already loaded just return it, otherwise make a new skinframe
2845 skinframe = R_SkinFrame_Find(name, textureflags, width, height, 0, true);
2846 if (skinframe->base)
2848 textureflags &= ~TEXF_FORCE_RELOAD;
2849 skinframe->stain = NULL;
2850 skinframe->merged = NULL;
2851 skinframe->base = NULL;
2852 skinframe->pants = NULL;
2853 skinframe->shirt = NULL;
2854 skinframe->nmap = NULL;
2855 skinframe->gloss = NULL;
2856 skinframe->glow = NULL;
2857 skinframe->fog = NULL;
2858 skinframe->reflect = NULL;
2859 skinframe->hasalpha = (textureflags & TEXF_ALPHA) != 0;
2860 // if no data was provided, then clearly the caller wanted to get a blank skinframe
2863 if (developer_loading.integer)
2864 Con_Printf("loading 32bit skin \"%s\"\n", name);
2865 skinframe->base = skinframe->merged = tex;
2866 Vector4Set(skinframe->avgcolor, 1, 1, 1, 1); // bogus placeholder
2870 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
2871 typedef struct suffixinfo_s
2874 qbool flipx, flipy, flipdiagonal;
2877 static suffixinfo_t suffix[3][6] =
2880 {"px", false, false, false},
2881 {"nx", false, false, false},
2882 {"py", false, false, false},
2883 {"ny", false, false, false},
2884 {"pz", false, false, false},
2885 {"nz", false, false, false}
2888 {"posx", false, false, false},
2889 {"negx", false, false, false},
2890 {"posy", false, false, false},
2891 {"negy", false, false, false},
2892 {"posz", false, false, false},
2893 {"negz", false, false, false}
2896 {"rt", true, false, true},
2897 {"lf", false, true, true},
2898 {"ft", true, true, false},
2899 {"bk", false, false, false},
2900 {"up", true, false, true},
2901 {"dn", true, false, true}
2905 static int componentorder[4] = {0, 1, 2, 3};
2907 static rtexture_t *R_LoadCubemap(const char *basename)
2909 int i, j, cubemapsize, forcefilter;
2910 unsigned char *cubemappixels, *image_buffer;
2911 rtexture_t *cubemaptexture;
2914 // HACK: if the cubemap name starts with a !, the cubemap is nearest-filtered
2915 forcefilter = TEXF_FORCELINEAR;
2916 if (basename && basename[0] == '!')
2919 forcefilter = TEXF_FORCENEAREST;
2921 // must start 0 so the first loadimagepixels has no requested width/height
2923 cubemappixels = NULL;
2924 cubemaptexture = NULL;
2925 // keep trying different suffix groups (posx, px, rt) until one loads
2926 for (j = 0;j < 3 && !cubemappixels;j++)
2928 // load the 6 images in the suffix group
2929 for (i = 0;i < 6;i++)
2931 // generate an image name based on the base and and suffix
2932 dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
2934 if ((image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
2936 // an image loaded, make sure width and height are equal
2937 if (image_width == image_height && (!cubemappixels || image_width == cubemapsize))
2939 // if this is the first image to load successfully, allocate the cubemap memory
2940 if (!cubemappixels && image_width >= 1)
2942 cubemapsize = image_width;
2943 // note this clears to black, so unavailable sides are black
2944 cubemappixels = (unsigned char *)Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
2946 // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
2948 Image_CopyMux(cubemappixels+i*cubemapsize*cubemapsize*4, image_buffer, cubemapsize, cubemapsize, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, componentorder);
2951 Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
2953 Mem_Free(image_buffer);
2957 // if a cubemap loaded, upload it
2960 if (developer_loading.integer)
2961 Con_Printf("loading cubemap \"%s\"\n", basename);
2963 cubemaptexture = R_LoadTextureCubeMap(r_main_texturepool, basename, cubemapsize, cubemappixels, vid.sRGB3D ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, (gl_texturecompression_lightcubemaps.integer && gl_texturecompression.integer ? TEXF_COMPRESS : 0) | forcefilter | TEXF_CLAMP, -1, NULL);
2964 Mem_Free(cubemappixels);
2968 Con_DPrintf("failed to load cubemap \"%s\"\n", basename);
2969 if (developer_loading.integer)
2971 Con_Printf("(tried tried images ");
2972 for (j = 0;j < 3;j++)
2973 for (i = 0;i < 6;i++)
2974 Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
2975 Con_Print(" and was unable to find any of them).\n");
2978 return cubemaptexture;
2981 rtexture_t *R_GetCubemap(const char *basename)
2984 for (i = 0;i < r_texture_numcubemaps;i++)
2985 if (r_texture_cubemaps[i] != NULL)
2986 if (!strcasecmp(r_texture_cubemaps[i]->basename, basename))
2987 return r_texture_cubemaps[i]->texture ? r_texture_cubemaps[i]->texture : r_texture_whitecube;
2988 if (i >= MAX_CUBEMAPS || !r_main_mempool)
2989 return r_texture_whitecube;
2990 r_texture_numcubemaps++;
2991 r_texture_cubemaps[i] = (cubemapinfo_t *)Mem_Alloc(r_main_mempool, sizeof(cubemapinfo_t));
2992 strlcpy(r_texture_cubemaps[i]->basename, basename, sizeof(r_texture_cubemaps[i]->basename));
2993 r_texture_cubemaps[i]->texture = R_LoadCubemap(r_texture_cubemaps[i]->basename);
2994 return r_texture_cubemaps[i]->texture;
2997 static void R_Main_FreeViewCache(void)
2999 if (r_refdef.viewcache.entityvisible)
3000 Mem_Free(r_refdef.viewcache.entityvisible);
3001 if (r_refdef.viewcache.world_pvsbits)
3002 Mem_Free(r_refdef.viewcache.world_pvsbits);
3003 if (r_refdef.viewcache.world_leafvisible)
3004 Mem_Free(r_refdef.viewcache.world_leafvisible);
3005 if (r_refdef.viewcache.world_surfacevisible)
3006 Mem_Free(r_refdef.viewcache.world_surfacevisible);
3007 memset(&r_refdef.viewcache, 0, sizeof(r_refdef.viewcache));
3010 static void R_Main_ResizeViewCache(void)
3012 int numentities = r_refdef.scene.numentities;
3013 int numclusters = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusters : 1;
3014 int numclusterbytes = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusterbytes : 1;
3015 int numleafs = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_leafs : 1;
3016 int numsurfaces = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->num_surfaces : 1;
3017 if (r_refdef.viewcache.maxentities < numentities)
3019 r_refdef.viewcache.maxentities = numentities;
3020 if (r_refdef.viewcache.entityvisible)
3021 Mem_Free(r_refdef.viewcache.entityvisible);
3022 r_refdef.viewcache.entityvisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.maxentities);
3024 if (r_refdef.viewcache.world_numclusters != numclusters)
3026 r_refdef.viewcache.world_numclusters = numclusters;
3027 r_refdef.viewcache.world_numclusterbytes = numclusterbytes;
3028 if (r_refdef.viewcache.world_pvsbits)
3029 Mem_Free(r_refdef.viewcache.world_pvsbits);
3030 r_refdef.viewcache.world_pvsbits = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numclusterbytes);
3032 if (r_refdef.viewcache.world_numleafs != numleafs)
3034 r_refdef.viewcache.world_numleafs = numleafs;
3035 if (r_refdef.viewcache.world_leafvisible)
3036 Mem_Free(r_refdef.viewcache.world_leafvisible);
3037 r_refdef.viewcache.world_leafvisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numleafs);
3039 if (r_refdef.viewcache.world_numsurfaces != numsurfaces)
3041 r_refdef.viewcache.world_numsurfaces = numsurfaces;
3042 if (r_refdef.viewcache.world_surfacevisible)
3043 Mem_Free(r_refdef.viewcache.world_surfacevisible);
3044 r_refdef.viewcache.world_surfacevisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numsurfaces);
3048 extern rtexture_t *loadingscreentexture;
3049 static void gl_main_start(void)
3051 loadingscreentexture = NULL;
3052 r_texture_blanknormalmap = NULL;
3053 r_texture_white = NULL;
3054 r_texture_grey128 = NULL;
3055 r_texture_black = NULL;
3056 r_texture_whitecube = NULL;
3057 r_texture_normalizationcube = NULL;
3058 r_texture_fogattenuation = NULL;
3059 r_texture_fogheighttexture = NULL;
3060 r_texture_gammaramps = NULL;
3061 r_texture_numcubemaps = 0;
3062 r_uniformbufferalignment = 32;
3064 r_loaddds = r_texture_dds_load.integer != 0;
3065 r_savedds = vid.support.ext_texture_compression_s3tc && r_texture_dds_save.integer;
3067 switch(vid.renderpath)
3069 case RENDERPATH_GL32:
3070 case RENDERPATH_GLES2:
3071 r_loadnormalmap = true;
3074 #ifdef GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT
3075 qglGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &r_uniformbufferalignment);
3081 R_FrameData_Reset();
3082 R_BufferData_Reset();
3086 memset(r_queries, 0, sizeof(r_queries));
3088 r_qwskincache = NULL;
3089 r_qwskincache_size = 0;
3091 // due to caching of texture_t references, the collision cache must be reset
3092 Collision_Cache_Reset(true);
3094 // set up r_skinframe loading system for textures
3095 memset(&r_skinframe, 0, sizeof(r_skinframe));
3096 r_skinframe.loadsequence = 1;
3097 Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
3099 r_main_texturepool = R_AllocTexturePool();
3100 R_BuildBlankTextures();
3104 R_BuildNormalizationCube();
3106 r_texture_fogattenuation = NULL;
3107 r_texture_fogheighttexture = NULL;
3108 r_texture_gammaramps = NULL;
3109 //r_texture_fogintensity = NULL;
3110 memset(&r_fb, 0, sizeof(r_fb));
3111 Mem_ExpandableArray_NewArray(&r_fb.rendertargets, r_main_mempool, sizeof(r_rendertarget_t), 128);
3112 r_glsl_permutation = NULL;
3113 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
3114 Mem_ExpandableArray_NewArray(&r_glsl_permutationarray, r_main_mempool, sizeof(r_glsl_permutation_t), 256);
3115 memset(&r_svbsp, 0, sizeof (r_svbsp));
3117 memset(r_texture_cubemaps, 0, sizeof(r_texture_cubemaps));
3118 r_texture_numcubemaps = 0;
3120 r_refdef.fogmasktable_density = 0;
3123 // For Steelstorm Android
3124 // FIXME CACHE the program and reload
3125 // FIXME see possible combinations for SS:BR android
3126 Con_DPrintf("Compiling most used shaders for SS:BR android... START\n");
3127 R_SetupShader_SetPermutationGLSL(0, 12);
3128 R_SetupShader_SetPermutationGLSL(0, 13);
3129 R_SetupShader_SetPermutationGLSL(0, 8388621);
3130 R_SetupShader_SetPermutationGLSL(3, 0);
3131 R_SetupShader_SetPermutationGLSL(3, 2048);
3132 R_SetupShader_SetPermutationGLSL(5, 0);
3133 R_SetupShader_SetPermutationGLSL(5, 2);
3134 R_SetupShader_SetPermutationGLSL(5, 2048);
3135 R_SetupShader_SetPermutationGLSL(5, 8388608);
3136 R_SetupShader_SetPermutationGLSL(11, 1);
3137 R_SetupShader_SetPermutationGLSL(11, 2049);
3138 R_SetupShader_SetPermutationGLSL(11, 8193);
3139 R_SetupShader_SetPermutationGLSL(11, 10241);
3140 Con_DPrintf("Compiling most used shaders for SS:BR android... END\n");
3144 extern unsigned int r_shadow_occlusion_buf;
3146 static void gl_main_shutdown(void)
3148 R_RenderTarget_FreeUnused(true);
3149 Mem_ExpandableArray_FreeArray(&r_fb.rendertargets);
3151 R_FrameData_Reset();
3152 R_BufferData_Reset();
3154 R_Main_FreeViewCache();
3156 switch(vid.renderpath)
3158 case RENDERPATH_GL32:
3159 case RENDERPATH_GLES2:
3160 #if defined(GL_SAMPLES_PASSED) && !defined(USE_GLES2)
3162 qglDeleteQueries(r_maxqueries, r_queries);
3166 r_shadow_occlusion_buf = 0;
3169 memset(r_queries, 0, sizeof(r_queries));
3171 r_qwskincache = NULL;
3172 r_qwskincache_size = 0;
3174 // clear out the r_skinframe state
3175 Mem_ExpandableArray_FreeArray(&r_skinframe.array);
3176 memset(&r_skinframe, 0, sizeof(r_skinframe));
3179 Mem_Free(r_svbsp.nodes);
3180 memset(&r_svbsp, 0, sizeof (r_svbsp));
3181 R_FreeTexturePool(&r_main_texturepool);
3182 loadingscreentexture = NULL;
3183 r_texture_blanknormalmap = NULL;
3184 r_texture_white = NULL;
3185 r_texture_grey128 = NULL;
3186 r_texture_black = NULL;
3187 r_texture_whitecube = NULL;
3188 r_texture_normalizationcube = NULL;
3189 r_texture_fogattenuation = NULL;
3190 r_texture_fogheighttexture = NULL;
3191 r_texture_gammaramps = NULL;
3192 r_texture_numcubemaps = 0;
3193 //r_texture_fogintensity = NULL;
3194 memset(&r_fb, 0, sizeof(r_fb));
3195 R_GLSL_Restart_f(cmd_local);
3197 r_glsl_permutation = NULL;
3198 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
3199 Mem_ExpandableArray_FreeArray(&r_glsl_permutationarray);
3202 static void gl_main_newmap(void)
3204 // FIXME: move this code to client
3205 char *entities, entname[MAX_QPATH];
3207 Mem_Free(r_qwskincache);
3208 r_qwskincache = NULL;
3209 r_qwskincache_size = 0;
3212 dpsnprintf(entname, sizeof(entname), "%s.ent", cl.worldnamenoextension);
3213 if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
3215 CL_ParseEntityLump(entities);
3219 if (cl.worldmodel->brush.entities)
3220 CL_ParseEntityLump(cl.worldmodel->brush.entities);
3222 R_Main_FreeViewCache();
3224 R_FrameData_Reset();
3225 R_BufferData_Reset();
3228 void GL_Main_Init(void)
3231 r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
3232 R_InitShaderModeInfo();
3234 Cmd_AddCommand(CF_CLIENT, "r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
3235 Cmd_AddCommand(CF_CLIENT, "r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
3236 // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
3237 if (gamemode == GAME_NEHAHRA)
3239 Cvar_RegisterVariable (&gl_fogenable);
3240 Cvar_RegisterVariable (&gl_fogdensity);
3241 Cvar_RegisterVariable (&gl_fogred);
3242 Cvar_RegisterVariable (&gl_foggreen);
3243 Cvar_RegisterVariable (&gl_fogblue);
3244 Cvar_RegisterVariable (&gl_fogstart);
3245 Cvar_RegisterVariable (&gl_fogend);
3246 Cvar_RegisterVariable (&gl_skyclip);
3248 Cvar_RegisterVariable(&r_motionblur);
3249 Cvar_RegisterVariable(&r_damageblur);
3250 Cvar_RegisterVariable(&r_motionblur_averaging);
3251 Cvar_RegisterVariable(&r_motionblur_randomize);
3252 Cvar_RegisterVariable(&r_motionblur_minblur);
3253 Cvar_RegisterVariable(&r_motionblur_maxblur);
3254 Cvar_RegisterVariable(&r_motionblur_velocityfactor);
3255 Cvar_RegisterVariable(&r_motionblur_velocityfactor_minspeed);
3256 Cvar_RegisterVariable(&r_motionblur_velocityfactor_maxspeed);
3257 Cvar_RegisterVariable(&r_motionblur_mousefactor);
3258 Cvar_RegisterVariable(&r_motionblur_mousefactor_minspeed);
3259 Cvar_RegisterVariable(&r_motionblur_mousefactor_maxspeed);
3260 Cvar_RegisterVariable(&r_depthfirst);
3261 Cvar_RegisterVariable(&r_useinfinitefarclip);
3262 Cvar_RegisterVariable(&r_farclip_base);
3263 Cvar_RegisterVariable(&r_farclip_world);
3264 Cvar_RegisterVariable(&r_nearclip);
3265 Cvar_RegisterVariable(&r_deformvertexes);
3266 Cvar_RegisterVariable(&r_transparent);
3267 Cvar_RegisterVariable(&r_transparent_alphatocoverage);
3268 Cvar_RegisterVariable(&r_transparent_sortsurfacesbynearest);
3269 Cvar_RegisterVariable(&r_transparent_useplanardistance);
3270 Cvar_RegisterVariable(&r_showoverdraw);
3271 Cvar_RegisterVariable(&r_showbboxes);
3272 Cvar_RegisterVariable(&r_showbboxes_client);
3273 Cvar_RegisterVariable(&r_showsurfaces);
3274 Cvar_RegisterVariable(&r_showtris);
3275 Cvar_RegisterVariable(&r_shownormals);
3276 Cvar_RegisterVariable(&r_showlighting);
3277 Cvar_RegisterVariable(&r_showcollisionbrushes);
3278 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
3279 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
3280 Cvar_RegisterVariable(&r_showdisabledepthtest);
3281 Cvar_RegisterVariable(&r_showspriteedges);
3282 Cvar_RegisterVariable(&r_showparticleedges);
3283 Cvar_RegisterVariable(&r_drawportals);
3284 Cvar_RegisterVariable(&r_drawentities);
3285 Cvar_RegisterVariable(&r_draw2d);
3286 Cvar_RegisterVariable(&r_drawworld);
3287 Cvar_RegisterVariable(&r_cullentities_trace);
3288 Cvar_RegisterVariable(&r_cullentities_trace_entityocclusion);
3289 Cvar_RegisterVariable(&r_cullentities_trace_samples);
3290 Cvar_RegisterVariable(&r_cullentities_trace_tempentitysamples);
3291 Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
3292 Cvar_RegisterVariable(&r_cullentities_trace_expand);
3293 Cvar_RegisterVariable(&r_cullentities_trace_pad);
3294 Cvar_RegisterVariable(&r_cullentities_trace_delay);
3295 Cvar_RegisterVariable(&r_cullentities_trace_eyejitter);
3296 Cvar_RegisterVariable(&r_sortentities);
3297 Cvar_RegisterVariable(&r_drawviewmodel);
3298 Cvar_RegisterVariable(&r_drawexteriormodel);
3299 Cvar_RegisterVariable(&r_speeds);
3300 Cvar_RegisterVariable(&r_fullbrights);
3301 Cvar_RegisterVariable(&r_wateralpha);
3302 Cvar_RegisterVariable(&r_dynamic);
3303 Cvar_RegisterVariable(&r_fullbright_directed);
3304 Cvar_RegisterVariable(&r_fullbright_directed_ambient);
3305 Cvar_RegisterVariable(&r_fullbright_directed_diffuse);
3306 Cvar_RegisterVariable(&r_fullbright_directed_pitch);
3307 Cvar_RegisterVariable(&r_fullbright_directed_pitch_relative);
3308 Cvar_RegisterVariable(&r_fullbright);
3309 Cvar_RegisterVariable(&r_shadows);
3310 Cvar_RegisterVariable(&r_shadows_darken);
3311 Cvar_RegisterVariable(&r_shadows_drawafterrtlighting);
3312 Cvar_RegisterVariable(&r_shadows_castfrombmodels);
3313 Cvar_RegisterVariable(&r_shadows_throwdistance);
3314 Cvar_RegisterVariable(&r_shadows_throwdirection);
3315 Cvar_RegisterVariable(&r_shadows_focus);
3316 Cvar_RegisterVariable(&r_shadows_shadowmapscale);
3317 Cvar_RegisterVariable(&r_shadows_shadowmapbias);
3318 Cvar_RegisterVariable(&r_q1bsp_skymasking);
3319 Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
3320 Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
3321 Cvar_RegisterVariable(&r_polygonoffset_decals_factor);
3322 Cvar_RegisterVariable(&r_polygonoffset_decals_offset);
3323 Cvar_RegisterVariable(&r_fog_exp2);
3324 Cvar_RegisterVariable(&r_fog_clear);
3325 Cvar_RegisterVariable(&r_drawfog);
3326 Cvar_RegisterVariable(&r_transparentdepthmasking);
3327 Cvar_RegisterVariable(&r_transparent_sortmindist);
3328 Cvar_RegisterVariable(&r_transparent_sortmaxdist);
3329 Cvar_RegisterVariable(&r_transparent_sortarraysize);
3330 Cvar_RegisterVariable(&r_texture_dds_load);
3331 Cvar_RegisterVariable(&r_texture_dds_save);
3332 Cvar_RegisterVariable(&r_usedepthtextures);
3333 Cvar_RegisterVariable(&r_viewfbo);
3334 Cvar_RegisterVariable(&r_rendertarget_debug);
3335 Cvar_RegisterVariable(&r_viewscale);
3336 Cvar_RegisterVariable(&r_viewscale_fpsscaling);
3337 Cvar_RegisterVariable(&r_viewscale_fpsscaling_min);
3338 Cvar_RegisterVariable(&r_viewscale_fpsscaling_multiply);
3339 Cvar_RegisterVariable(&r_viewscale_fpsscaling_stepsize);
3340 Cvar_RegisterVariable(&r_viewscale_fpsscaling_stepmax);
3341 Cvar_RegisterVariable(&r_viewscale_fpsscaling_target);
3342 Cvar_RegisterVariable(&r_glsl_deluxemapping);
3343 Cvar_RegisterVariable(&r_glsl_offsetmapping);
3344 Cvar_RegisterVariable(&r_glsl_offsetmapping_steps);
3345 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
3346 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping_steps);
3347 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping_refinesteps);
3348 Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
3349 Cvar_RegisterVariable(&r_glsl_offsetmapping_lod);
3350 Cvar_RegisterVariable(&r_glsl_offsetmapping_lod_distance);
3351 Cvar_RegisterVariable(&r_glsl_postprocess);
3352 Cvar_RegisterVariable(&r_glsl_postprocess_uservec1);
3353 Cvar_RegisterVariable(&r_glsl_postprocess_uservec2);
3354 Cvar_RegisterVariable(&r_glsl_postprocess_uservec3);
3355 Cvar_RegisterVariable(&r_glsl_postprocess_uservec4);
3356 Cvar_RegisterVariable(&r_glsl_postprocess_uservec1_enable);
3357 Cvar_RegisterVariable(&r_glsl_postprocess_uservec2_enable);
3358 Cvar_RegisterVariable(&r_glsl_postprocess_uservec3_enable);
3359 Cvar_RegisterVariable(&r_glsl_postprocess_uservec4_enable);
3360 Cvar_RegisterVariable(&r_celshading);
3361 Cvar_RegisterVariable(&r_celoutlines);
3362 Cvar_RegisterVariable(&r_fxaa);
3364 Cvar_RegisterVariable(&r_water);
3365 Cvar_RegisterVariable(&r_water_cameraentitiesonly);
3366 Cvar_RegisterVariable(&r_water_resolutionmultiplier);
3367 Cvar_RegisterVariable(&r_water_clippingplanebias);
3368 Cvar_RegisterVariable(&r_water_refractdistort);
3369 Cvar_RegisterVariable(&r_water_reflectdistort);
3370 Cvar_RegisterVariable(&r_water_scissormode);
3371 Cvar_RegisterVariable(&r_water_lowquality);
3372 Cvar_RegisterVariable(&r_water_hideplayer);
3374 Cvar_RegisterVariable(&r_lerpsprites);
3375 Cvar_RegisterVariable(&r_lerpmodels);
3376 Cvar_RegisterVariable(&r_nolerp_list);
3377 Cvar_RegisterVariable(&r_lerplightstyles);
3378 Cvar_RegisterVariable(&r_waterscroll);
3379 Cvar_RegisterVariable(&r_bloom);
3380 Cvar_RegisterVariable(&r_colorfringe);
3381 Cvar_RegisterVariable(&r_bloom_colorscale);
3382 Cvar_RegisterVariable(&r_bloom_brighten);
3383 Cvar_RegisterVariable(&r_bloom_blur);
3384 Cvar_RegisterVariable(&r_bloom_resolution);
3385 Cvar_RegisterVariable(&r_bloom_colorexponent);
3386 Cvar_RegisterVariable(&r_bloom_colorsubtract);
3387 Cvar_RegisterVariable(&r_bloom_scenebrightness);
3388 Cvar_RegisterVariable(&r_hdr_scenebrightness);
3389 Cvar_RegisterVariable(&r_hdr_glowintensity);
3390 Cvar_RegisterVariable(&r_hdr_irisadaptation);
3391 Cvar_RegisterVariable(&r_hdr_irisadaptation_multiplier);
3392 Cvar_RegisterVariable(&r_hdr_irisadaptation_minvalue);
3393 Cvar_RegisterVariable(&r_hdr_irisadaptation_maxvalue);
3394 Cvar_RegisterVariable(&r_hdr_irisadaptation_value);
3395 Cvar_RegisterVariable(&r_hdr_irisadaptation_fade_up);
3396 Cvar_RegisterVariable(&r_hdr_irisadaptation_fade_down);
3397 Cvar_RegisterVariable(&r_hdr_irisadaptation_radius);
3398 Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
3399 Cvar_RegisterVariable(&developer_texturelogging);
3400 Cvar_RegisterVariable(&gl_lightmaps);
3401 Cvar_RegisterVariable(&r_test);
3402 Cvar_RegisterVariable(&r_batch_multidraw);
3403 Cvar_RegisterVariable(&r_batch_multidraw_mintriangles);
3404 Cvar_RegisterVariable(&r_batch_debugdynamicvertexpath);
3405 Cvar_RegisterVariable(&r_glsl_skeletal);
3406 Cvar_RegisterVariable(&r_glsl_saturation);
3407 Cvar_RegisterVariable(&r_glsl_saturation_redcompensate);
3408 Cvar_RegisterVariable(&r_glsl_vertextextureblend_usebothalphas);
3409 Cvar_RegisterVariable(&r_framedatasize);
3410 for (i = 0;i < R_BUFFERDATA_COUNT;i++)
3411 Cvar_RegisterVariable(&r_buffermegs[i]);
3412 Cvar_RegisterVariable(&r_batch_dynamicbuffer);
3413 Cvar_RegisterVariable(&r_q1bsp_lightmap_updates_enabled);
3414 Cvar_RegisterVariable(&r_q1bsp_lightmap_updates_combine);
3415 Cvar_RegisterVariable(&r_q1bsp_lightmap_updates_hidden_surfaces);
3416 if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
3417 Cvar_SetValue(&cvars_all, "r_fullbrights", 0);
3418 #ifdef DP_MOBILETOUCH
3419 // GLES devices have terrible depth precision in general, so...
3420 Cvar_SetValueQuick(&r_nearclip, 4);
3421 Cvar_SetValueQuick(&r_farclip_base, 4096);
3422 Cvar_SetValueQuick(&r_farclip_world, 0);
3423 Cvar_SetValueQuick(&r_useinfinitefarclip, 0);
3425 R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap, NULL, NULL);
3428 void Render_Init(void)
3441 R_LightningBeams_Init();
3442 CL_MeshEntities_Init();
3446 static void R_GetCornerOfBox(vec3_t out, const vec3_t mins, const vec3_t maxs, int signbits)
3448 out[0] = ((signbits & 1) ? mins : maxs)[0];
3449 out[1] = ((signbits & 2) ? mins : maxs)[1];
3450 out[2] = ((signbits & 4) ? mins : maxs)[2];
3453 static qbool _R_CullBox(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes, int ignore)
3458 if (r_trippy.integer)
3460 for (i = 0;i < numplanes;i++)
3465 R_GetCornerOfBox(corner, mins, maxs, p->signbits);
3466 if (DotProduct(p->normal, corner) < p->dist)
3472 qbool R_CullFrustum(const vec3_t mins, const vec3_t maxs)
3474 // skip nearclip plane, it often culls portals when you are very close, and is almost never useful
3475 return _R_CullBox(mins, maxs, r_refdef.view.numfrustumplanes, r_refdef.view.frustum, 4);
3478 qbool R_CullBox(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
3480 // nothing to ignore
3481 return _R_CullBox(mins, maxs, numplanes, planes, -1);
3484 //==================================================================================
3486 // LadyHavoc: this stores temporary data used within the same frame
3488 typedef struct r_framedata_mem_s
3490 struct r_framedata_mem_s *purge; // older mem block to free on next frame
3491 size_t size; // how much usable space
3492 size_t current; // how much space in use
3493 size_t mark; // last "mark" location, temporary memory can be freed by returning to this
3494 size_t wantedsize; // how much space was allocated
3495 unsigned char *data; // start of real data (16byte aligned)
3499 static r_framedata_mem_t *r_framedata_mem;
3501 void R_FrameData_Reset(void)
3503 while (r_framedata_mem)
3505 r_framedata_mem_t *next = r_framedata_mem->purge;
3506 Mem_Free(r_framedata_mem);
3507 r_framedata_mem = next;
3511 static void R_FrameData_Resize(qbool mustgrow)
3514 wantedsize = (size_t)(r_framedatasize.value * 1024*1024);
3515 wantedsize = bound(65536, wantedsize, 1000*1024*1024);
3516 if (!r_framedata_mem || r_framedata_mem->wantedsize != wantedsize || mustgrow)
3518 r_framedata_mem_t *newmem = (r_framedata_mem_t *)Mem_Alloc(r_main_mempool, wantedsize);
3519 newmem->wantedsize = wantedsize;
3520 newmem->data = (unsigned char *)(((size_t)(newmem+1) + 15) & ~15);
3521 newmem->size = (unsigned char *)newmem + wantedsize - newmem->data;
3522 newmem->current = 0;
3524 newmem->purge = r_framedata_mem;
3525 r_framedata_mem = newmem;
3529 void R_FrameData_NewFrame(void)
3531 R_FrameData_Resize(false);
3532 if (!r_framedata_mem)
3534 // if we ran out of space on the last frame, free the old memory now
3535 while (r_framedata_mem->purge)
3537 // repeatedly remove the second item in the list, leaving only head
3538 r_framedata_mem_t *next = r_framedata_mem->purge->purge;
3539 Mem_Free(r_framedata_mem->purge);
3540 r_framedata_mem->purge = next;
3542 // reset the current mem pointer
3543 r_framedata_mem->current = 0;
3544 r_framedata_mem->mark = 0;
3547 void *R_FrameData_Alloc(size_t size)
3552 // align to 16 byte boundary - the data pointer is already aligned, so we
3553 // only need to ensure the size of every allocation is also aligned
3554 size = (size + 15) & ~15;
3556 while (!r_framedata_mem || r_framedata_mem->current + size > r_framedata_mem->size)
3558 // emergency - we ran out of space, allocate more memory
3559 // note: this has no upper-bound, we'll fail to allocate memory eventually and just die
3560 newvalue = r_framedatasize.value * 2.0f;
3561 // upper bound based on architecture - if we try to allocate more than this we could overflow, better to loop until we error out on allocation failure
3562 if (sizeof(size_t) >= 8)
3563 newvalue = bound(0.25f, newvalue, (float)(1ll << 42));
3565 newvalue = bound(0.25f, newvalue, (float)(1 << 10));
3566 // this might not be a growing it, but we'll allocate another buffer every time
3567 Cvar_SetValueQuick(&r_framedatasize, newvalue);
3568 R_FrameData_Resize(true);
3571 data = r_framedata_mem->data + r_framedata_mem->current;
3572 r_framedata_mem->current += size;
3574 // count the usage for stats
3575 r_refdef.stats[r_stat_framedatacurrent] = max(r_refdef.stats[r_stat_framedatacurrent], (int)r_framedata_mem->current);
3576 r_refdef.stats[r_stat_framedatasize] = max(r_refdef.stats[r_stat_framedatasize], (int)r_framedata_mem->size);
3578 return (void *)data;
3581 void *R_FrameData_Store(size_t size, void *data)
3583 void *d = R_FrameData_Alloc(size);
3585 memcpy(d, data, size);
3589 void R_FrameData_SetMark(void)
3591 if (!r_framedata_mem)
3593 r_framedata_mem->mark = r_framedata_mem->current;
3596 void R_FrameData_ReturnToMark(void)
3598 if (!r_framedata_mem)
3600 r_framedata_mem->current = r_framedata_mem->mark;
3603 //==================================================================================
3605 // avoid reusing the same buffer objects on consecutive frames
3606 #define R_BUFFERDATA_CYCLE 3
3608 typedef struct r_bufferdata_buffer_s
3610 struct r_bufferdata_buffer_s *purge; // older buffer to free on next frame
3611 size_t size; // how much usable space
3612 size_t current; // how much space in use
3613 r_meshbuffer_t *buffer; // the buffer itself
3615 r_bufferdata_buffer_t;
3617 static int r_bufferdata_cycle = 0; // incremented and wrapped each frame
3618 static r_bufferdata_buffer_t *r_bufferdata_buffer[R_BUFFERDATA_CYCLE][R_BUFFERDATA_COUNT];
3620 /// frees all dynamic buffers
3621 void R_BufferData_Reset(void)
3624 r_bufferdata_buffer_t **p, *mem;
3625 for (cycle = 0;cycle < R_BUFFERDATA_CYCLE;cycle++)
3627 for (type = 0;type < R_BUFFERDATA_COUNT;type++)
3630 p = &r_bufferdata_buffer[cycle][type];
3636 R_Mesh_DestroyMeshBuffer(mem->buffer);
3643 // resize buffer as needed (this actually makes a new one, the old one will be recycled next frame)
3644 static void R_BufferData_Resize(r_bufferdata_type_t type, qbool mustgrow, size_t minsize)
3646 r_bufferdata_buffer_t *mem = r_bufferdata_buffer[r_bufferdata_cycle][type];
3648 float newvalue = r_buffermegs[type].value;
3650 // increase the cvar if we have to (but only if we already have a mem)
3651 if (mustgrow && mem)
3653 newvalue = bound(0.25f, newvalue, 256.0f);
3654 while (newvalue * 1024*1024 < minsize)
3657 // clamp the cvar to valid range
3658 newvalue = bound(0.25f, newvalue, 256.0f);
3659 if (r_buffermegs[type].value != newvalue)
3660 Cvar_SetValueQuick(&r_buffermegs[type], newvalue);
3662 // calculate size in bytes
3663 size = (size_t)(newvalue * 1024*1024);
3664 size = bound(131072, size, 256*1024*1024);
3666 // allocate a new buffer if the size is different (purge old one later)
3667 // or if we were told we must grow the buffer
3668 if (!mem || mem->size != size || mustgrow)
3670 mem = (r_bufferdata_buffer_t *)Mem_Alloc(r_main_mempool, sizeof(*mem));
3673 if (type == R_BUFFERDATA_VERTEX)
3674 mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbuffervertex", false, false, true, false);
3675 else if (type == R_BUFFERDATA_INDEX16)
3676 mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbufferindex16", true, false, true, true);
3677 else if (type == R_BUFFERDATA_INDEX32)
3678 mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbufferindex32", true, false, true, false);
3679 else if (type == R_BUFFERDATA_UNIFORM)
3680 mem->buffer = R_Mesh_CreateMeshBuffer(NULL, mem->size, "dynamicbufferuniform", false, true, true, false);
3681 mem->purge = r_bufferdata_buffer[r_bufferdata_cycle][type];
3682 r_bufferdata_buffer[r_bufferdata_cycle][type] = mem;
3686 void R_BufferData_NewFrame(void)
3689 r_bufferdata_buffer_t **p, *mem;
3690 // cycle to the next frame's buffers
3691 r_bufferdata_cycle = (r_bufferdata_cycle + 1) % R_BUFFERDATA_CYCLE;
3692 // if we ran out of space on the last time we used these buffers, free the old memory now
3693 for (type = 0;type < R_BUFFERDATA_COUNT;type++)
3695 if (r_bufferdata_buffer[r_bufferdata_cycle][type])
3697 R_BufferData_Resize((r_bufferdata_type_t)type, false, 131072);
3698 // free all but the head buffer, this is how we recycle obsolete
3699 // buffers after they are no longer in use
3700 p = &r_bufferdata_buffer[r_bufferdata_cycle][type]->purge;
3706 R_Mesh_DestroyMeshBuffer(mem->buffer);
3709 // reset the current offset
3710 r_bufferdata_buffer[r_bufferdata_cycle][type]->current = 0;
3715 r_meshbuffer_t *R_BufferData_Store(size_t datasize, const void *data, r_bufferdata_type_t type, int *returnbufferoffset)
3717 r_bufferdata_buffer_t *mem;
3721 *returnbufferoffset = 0;
3723 // align size to a byte boundary appropriate for the buffer type, this
3724 // makes all allocations have aligned start offsets
3725 if (type == R_BUFFERDATA_UNIFORM)
3726 padsize = (datasize + r_uniformbufferalignment - 1) & ~(r_uniformbufferalignment - 1);
3728 padsize = (datasize + 15) & ~15;
3730 // if we ran out of space in this buffer we must allocate a new one
3731 if (!r_bufferdata_buffer[r_bufferdata_cycle][type] || r_bufferdata_buffer[r_bufferdata_cycle][type]->current + padsize > r_bufferdata_buffer[r_bufferdata_cycle][type]->size)
3732 R_BufferData_Resize(type, true, padsize);
3734 // if the resize did not give us enough memory, fail
3735 if (!r_bufferdata_buffer[r_bufferdata_cycle][type] || r_bufferdata_buffer[r_bufferdata_cycle][type]->current + padsize > r_bufferdata_buffer[r_bufferdata_cycle][type]->size)
3736 Sys_Error("R_BufferData_Store: failed to create a new buffer of sufficient size\n");
3738 mem = r_bufferdata_buffer[r_bufferdata_cycle][type];
3739 offset = (int)mem->current;
3740 mem->current += padsize;
3742 // upload the data to the buffer at the chosen offset
3744 R_Mesh_UpdateMeshBuffer(mem->buffer, NULL, mem->size, false, 0);
3745 R_Mesh_UpdateMeshBuffer(mem->buffer, data, datasize, true, offset);
3747 // count the usage for stats
3748 r_refdef.stats[r_stat_bufferdatacurrent_vertex + type] = max(r_refdef.stats[r_stat_bufferdatacurrent_vertex + type], (int)mem->current);
3749 r_refdef.stats[r_stat_bufferdatasize_vertex + type] = max(r_refdef.stats[r_stat_bufferdatasize_vertex + type], (int)mem->size);
3751 // return the buffer offset
3752 *returnbufferoffset = offset;
3757 //==================================================================================
3759 // LadyHavoc: animcache originally written by Echon, rewritten since then
3762 * Animation cache prevents re-generating mesh data for an animated model
3763 * multiple times in one frame for lighting, shadowing, reflections, etc.
3766 void R_AnimCache_Free(void)
3770 void R_AnimCache_ClearCache(void)
3773 entity_render_t *ent;
3775 for (i = 0;i < r_refdef.scene.numentities;i++)
3777 ent = r_refdef.scene.entities[i];
3778 ent->animcache_vertex3f = NULL;
3779 ent->animcache_vertex3f_vertexbuffer = NULL;
3780 ent->animcache_vertex3f_bufferoffset = 0;
3781 ent->animcache_normal3f = NULL;
3782 ent->animcache_normal3f_vertexbuffer = NULL;
3783 ent->animcache_normal3f_bufferoffset = 0;
3784 ent->animcache_svector3f = NULL;
3785 ent->animcache_svector3f_vertexbuffer = NULL;
3786 ent->animcache_svector3f_bufferoffset = 0;
3787 ent->animcache_tvector3f = NULL;
3788 ent->animcache_tvector3f_vertexbuffer = NULL;
3789 ent->animcache_tvector3f_bufferoffset = 0;
3790 ent->animcache_skeletaltransform3x4 = NULL;
3791 ent->animcache_skeletaltransform3x4buffer = NULL;
3792 ent->animcache_skeletaltransform3x4offset = 0;
3793 ent->animcache_skeletaltransform3x4size = 0;
3797 qbool R_AnimCache_GetEntity(entity_render_t *ent, qbool wantnormals, qbool wanttangents)
3799 model_t *model = ent->model;
3802 // see if this ent is worth caching
3803 if (!model || !model->Draw || !model->AnimateVertices)
3805 // nothing to cache if it contains no animations and has no skeleton
3806 if (!model->surfmesh.isanimated && !(model->num_bones && ent->skeleton && ent->skeleton->relativetransforms))
3808 // see if it is already cached for gpuskeletal
3809 if (ent->animcache_skeletaltransform3x4)
3811 // see if it is already cached as a mesh
3812 if (ent->animcache_vertex3f)
3814 // check if we need to add normals or tangents
3815 if (ent->animcache_normal3f)
3816 wantnormals = false;
3817 if (ent->animcache_svector3f)
3818 wanttangents = false;
3819 if (!wantnormals && !wanttangents)
3823 // check which kind of cache we need to generate
3824 if (r_gpuskeletal && model->num_bones > 0 && model->surfmesh.data_skeletalindex4ub)
3826 // cache the skeleton so the vertex shader can use it
3827 r_refdef.stats[r_stat_animcache_skeletal_count] += 1;
3828 r_refdef.stats[r_stat_animcache_skeletal_bones] += model->num_bones;
3829 r_refdef.stats[r_stat_animcache_skeletal_maxbones] = max(r_refdef.stats[r_stat_animcache_skeletal_maxbones], model->num_bones);
3830 ent->animcache_skeletaltransform3x4 = (float *)R_FrameData_Alloc(sizeof(float[3][4]) * model->num_bones);
3831 Mod_Skeletal_BuildTransforms(model, ent->frameblend, ent->skeleton, NULL, ent->animcache_skeletaltransform3x4);
3832 // note: this can fail if the buffer is at the grow limit
3833 ent->animcache_skeletaltransform3x4size = sizeof(float[3][4]) * model->num_bones;
3834 ent->animcache_skeletaltransform3x4buffer = R_BufferData_Store(ent->animcache_skeletaltransform3x4size, ent->animcache_skeletaltransform3x4, R_BUFFERDATA_UNIFORM, &ent->animcache_skeletaltransform3x4offset);
3836 else if (ent->animcache_vertex3f)
3838 // mesh was already cached but we may need to add normals/tangents
3839 // (this only happens with multiple views, reflections, cameras, etc)
3840 if (wantnormals || wanttangents)
3842 numvertices = model->surfmesh.num_vertices;
3844 ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3847 ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3848 ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3850 model->AnimateVertices(model, ent->frameblend, ent->skeleton, NULL, wantnormals ? ent->animcache_normal3f : NULL, wanttangents ? ent->animcache_svector3f : NULL, wanttangents ? ent->animcache_tvector3f : NULL);
3851 r_refdef.stats[r_stat_animcache_shade_count] += 1;
3852 r_refdef.stats[r_stat_animcache_shade_vertices] += numvertices;
3853 r_refdef.stats[r_stat_animcache_shade_maxvertices] = max(r_refdef.stats[r_stat_animcache_shade_maxvertices], numvertices);
3858 // generate mesh cache
3859 numvertices = model->surfmesh.num_vertices;
3860 ent->animcache_vertex3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3862 ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3865 ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3866 ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
3868 model->AnimateVertices(model, ent->frameblend, ent->skeleton, ent->animcache_vertex3f, ent->animcache_normal3f, ent->animcache_svector3f, ent->animcache_tvector3f);
3869 if (wantnormals || wanttangents)
3871 r_refdef.stats[r_stat_animcache_shade_count] += 1;
3872 r_refdef.stats[r_stat_animcache_shade_vertices] += numvertices;
3873 r_refdef.stats[r_stat_animcache_shade_maxvertices] = max(r_refdef.stats[r_stat_animcache_shade_maxvertices], numvertices);
3875 r_refdef.stats[r_stat_animcache_shape_count] += 1;
3876 r_refdef.stats[r_stat_animcache_shape_vertices] += numvertices;
3877 r_refdef.stats[r_stat_animcache_shape_maxvertices] = max(r_refdef.stats[r_stat_animcache_shape_maxvertices], numvertices);
3882 void R_AnimCache_CacheVisibleEntities(void)
3886 // TODO: thread this
3887 // NOTE: R_PrepareRTLights() also caches entities
3889 for (i = 0;i < r_refdef.scene.numentities;i++)
3890 if (r_refdef.viewcache.entityvisible[i])
3891 R_AnimCache_GetEntity(r_refdef.scene.entities[i], true, true);
3894 //==================================================================================
3896 qbool R_CanSeeBox(int numsamples, vec_t eyejitter, vec_t entboxenlarge, vec_t entboxexpand, vec_t pad, vec3_t eye, vec3_t entboxmins, vec3_t entboxmaxs)
3898 long unsigned int i;
3900 vec3_t eyemins, eyemaxs;
3901 vec3_t boxmins, boxmaxs;
3902 vec3_t padmins, padmaxs;
3905 model_t *model = r_refdef.scene.worldmodel;
3906 static vec3_t positions[] = {
3907 { 0.5f, 0.5f, 0.5f },
3908 { 0.0f, 0.0f, 0.0f },
3909 { 0.0f, 0.0f, 1.0f },
3910 { 0.0f, 1.0f, 0.0f },
3911 { 0.0f, 1.0f, 1.0f },
3912 { 1.0f, 0.0f, 0.0f },
3913 { 1.0f, 0.0f, 1.0f },
3914 { 1.0f, 1.0f, 0.0f },
3915 { 1.0f, 1.0f, 1.0f },
3918 // sample count can be set to -1 to skip this logic, for flicker-prone objects
3922 // view origin is not used for culling in portal/reflection/refraction renders or isometric views
3923 if (!r_refdef.view.usevieworiginculling)
3926 if (!r_cullentities_trace_entityocclusion.integer && (!model || !model->brush.TraceLineOfSight))
3929 // expand the eye box a little
3930 eyemins[0] = eye[0] - eyejitter;
3931 eyemaxs[0] = eye[0] + eyejitter;
3932 eyemins[1] = eye[1] - eyejitter;
3933 eyemaxs[1] = eye[1] + eyejitter;
3934 eyemins[2] = eye[2] - eyejitter;
3935 eyemaxs[2] = eye[2] + eyejitter;
3936 // expand the box a little
3937 boxmins[0] = (entboxenlarge + 1) * entboxmins[0] - entboxenlarge * entboxmaxs[0] - entboxexpand;
3938 boxmaxs[0] = (entboxenlarge + 1) * entboxmaxs[0] - entboxenlarge * entboxmins[0] + entboxexpand;
3939 boxmins[1] = (entboxenlarge + 1) * entboxmins[1] - entboxenlarge * entboxmaxs[1] - entboxexpand;
3940 boxmaxs[1] = (entboxenlarge + 1) * entboxmaxs[1] - entboxenlarge * entboxmins[1] + entboxexpand;
3941 boxmins[2] = (entboxenlarge + 1) * entboxmins[2] - entboxenlarge * entboxmaxs[2] - entboxexpand;
3942 boxmaxs[2] = (entboxenlarge + 1) * entboxmaxs[2] - entboxenlarge * entboxmins[2] + entboxexpand;
3943 // make an even larger box for the acceptable area
3944 padmins[0] = boxmins[0] - pad;
3945 padmaxs[0] = boxmaxs[0] + pad;
3946 padmins[1] = boxmins[1] - pad;
3947 padmaxs[1] = boxmaxs[1] + pad;
3948 padmins[2] = boxmins[2] - pad;
3949 padmaxs[2] = boxmaxs[2] + pad;
3951 // return true if eye overlaps enlarged box
3952 if (BoxesOverlap(boxmins, boxmaxs, eyemins, eyemaxs))
3955 VectorCopy(eye, start);
3956 // try specific positions in the box first - note that these can be cached
3957 if (r_cullentities_trace_entityocclusion.integer)
3959 for (i = 0; i < sizeof(positions) / sizeof(positions[0]); i++)
3962 end[0] = boxmins[0] + (boxmaxs[0] - boxmins[0]) * positions[i][0];
3963 end[1] = boxmins[1] + (boxmaxs[1] - boxmins[1]) * positions[i][1];
3964 end[2] = boxmins[2] + (boxmaxs[2] - boxmins[2]) * positions[i][2];
3965 //trace_t trace = CL_TraceLine(start, end, MOVE_NORMAL, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, MATERIALFLAGMASK_TRANSLUCENT, 0.0f, true, false, NULL, true, true);
3966 trace = CL_Cache_TraceLineSurfaces(start, end, MOVE_NORMAL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT);
3967 // not picky - if the trace ended anywhere in the box we're good
3968 if (BoxesOverlap(trace.endpos, trace.endpos, padmins, padmaxs))
3975 VectorMAM(0.5f, boxmins, 0.5f, boxmaxs, end);
3976 if (model->brush.TraceLineOfSight(model, start, end, padmins, padmaxs))
3980 // try various random positions
3981 for (j = 0; j < numsamples; j++)
3983 VectorSet(start, lhrandom(eyemins[0], eyemaxs[0]), lhrandom(eyemins[1], eyemaxs[1]), lhrandom(eyemins[2], eyemaxs[2]));
3984 VectorSet(end, lhrandom(boxmins[0], boxmaxs[0]), lhrandom(boxmins[1], boxmaxs[1]), lhrandom(boxmins[2], boxmaxs[2]));
3985 if (r_cullentities_trace_entityocclusion.integer)
3987 trace_t trace = CL_TraceLine(start, end, MOVE_NORMAL, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, MATERIALFLAGMASK_TRANSLUCENT, 0.0f, true, false, NULL, true, true);
3988 // not picky - if the trace ended anywhere in the box we're good
3989 if (BoxesOverlap(trace.endpos, trace.endpos, padmins, padmaxs))
3992 else if (model->brush.TraceLineOfSight(model, start, end, padmins, padmaxs))
4000 static void R_View_UpdateEntityVisible (void)
4005 entity_render_t *ent;
4007 if (r_refdef.envmap || r_fb.water.hideplayer)
4008 renderimask = RENDER_EXTERIORMODEL | RENDER_VIEWMODEL;
4009 else if (chase_active.integer || r_fb.water.renderingscene)
4010 renderimask = RENDER_VIEWMODEL;
4012 renderimask = RENDER_EXTERIORMODEL;
4013 if (!r_drawviewmodel.integer)
4014 renderimask |= RENDER_VIEWMODEL;
4015 if (!r_drawexteriormodel.integer)
4016 renderimask |= RENDER_EXTERIORMODEL;
4017 memset(r_refdef.viewcache.entityvisible, 0, r_refdef.scene.numentities);
4018 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs)
4020 // worldmodel can check visibility
4021 for (i = 0;i < r_refdef.scene.numentities;i++)
4023 ent = r_refdef.scene.entities[i];
4024 if (r_refdef.viewcache.world_novis && !(ent->flags & RENDER_VIEWMODEL))
4026 r_refdef.viewcache.entityvisible[i] = false;
4029 if (!(ent->flags & renderimask))
4030 if (!R_CullFrustum(ent->mins, ent->maxs) || (ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)))
4031 if ((ent->flags & (RENDER_NODEPTHTEST | RENDER_WORLDOBJECT | RENDER_VIEWMODEL)) || r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, ent->mins, ent->maxs))
4032 r_refdef.viewcache.entityvisible[i] = true;
4037 // no worldmodel or it can't check visibility
4038 for (i = 0;i < r_refdef.scene.numentities;i++)
4040 ent = r_refdef.scene.entities[i];
4041 if (!(ent->flags & renderimask))
4042 if (!R_CullFrustum(ent->mins, ent->maxs) || (ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)))
4043 r_refdef.viewcache.entityvisible[i] = true;
4046 if (r_cullentities_trace.integer)
4048 for (i = 0;i < r_refdef.scene.numentities;i++)
4050 if (!r_refdef.viewcache.entityvisible[i])
4052 ent = r_refdef.scene.entities[i];
4053 if (!(ent->flags & (RENDER_VIEWMODEL | RENDER_WORLDOBJECT | RENDER_NODEPTHTEST)) && !(ent->model && (ent->model->name[0] == '*')))
4055 samples = ent->last_trace_visibility == 0 ? r_cullentities_trace_tempentitysamples.integer : r_cullentities_trace_samples.integer;
4056 if (R_CanSeeBox(samples, r_cullentities_trace_eyejitter.value, r_cullentities_trace_enlarge.value, r_cullentities_trace_expand.value, r_cullentities_trace_pad.value, r_refdef.view.origin, ent->mins, ent->maxs))
4057 ent->last_trace_visibility = host.realtime;
4058 if (ent->last_trace_visibility < host.realtime - r_cullentities_trace_delay.value)
4059 r_refdef.viewcache.entityvisible[i] = 0;
4065 /// only used if skyrendermasked, and normally returns false
4066 static int R_DrawBrushModelsSky (void)
4069 entity_render_t *ent;
4072 for (i = 0;i < r_refdef.scene.numentities;i++)
4074 if (!r_refdef.viewcache.entityvisible[i])
4076 ent = r_refdef.scene.entities[i];
4077 if (!ent->model || !ent->model->DrawSky)
4079 ent->model->DrawSky(ent);
4085 static void R_DrawNoModel(entity_render_t *ent);
4086 static void R_DrawModels(void)
4089 entity_render_t *ent;
4091 for (i = 0;i < r_refdef.scene.numentities;i++)
4093 if (!r_refdef.viewcache.entityvisible[i])
4095 ent = r_refdef.scene.entities[i];
4096 r_refdef.stats[r_stat_entities]++;
4098 if (ent->model && ent->model->Draw != NULL)
4099 ent->model->Draw(ent);
4105 static void R_DrawModelsDepth(void)
4108 entity_render_t *ent;
4110 for (i = 0;i < r_refdef.scene.numentities;i++)
4112 if (!r_refdef.viewcache.entityvisible[i])
4114 ent = r_refdef.scene.entities[i];
4115 if (ent->model && ent->model->DrawDepth != NULL)
4116 ent->model->DrawDepth(ent);
4120 static void R_DrawModelsDebug(void)
4123 entity_render_t *ent;
4125 for (i = 0;i < r_refdef.scene.numentities;i++)
4127 if (!r_refdef.viewcache.entityvisible[i])
4129 ent = r_refdef.scene.entities[i];
4130 if (ent->model && ent->model->DrawDebug != NULL)
4131 ent->model->DrawDebug(ent);
4135 static void R_DrawModelsAddWaterPlanes(void)
4138 entity_render_t *ent;
4140 for (i = 0;i < r_refdef.scene.numentities;i++)
4142 if (!r_refdef.viewcache.entityvisible[i])
4144 ent = r_refdef.scene.entities[i];
4145 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
4146 ent->model->DrawAddWaterPlanes(ent);
4150 static float irisvecs[7][3] = {{0, 0, 0}, {-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
4152 void R_HDR_UpdateIrisAdaptation(const vec3_t point)
4154 if (r_hdr_irisadaptation.integer)
4159 vec3_t diffusenormal;
4161 vec_t brightness = 0.0f;
4166 VectorCopy(r_refdef.view.forward, forward);
4167 for (c = 0;c < (int)(sizeof(irisvecs)/sizeof(irisvecs[0]));c++)
4169 p[0] = point[0] + irisvecs[c][0] * r_hdr_irisadaptation_radius.value;
4170 p[1] = point[1] + irisvecs[c][1] * r_hdr_irisadaptation_radius.value;
4171 p[2] = point[2] + irisvecs[c][2] * r_hdr_irisadaptation_radius.value;
4172 R_CompleteLightPoint(ambient, diffuse, diffusenormal, p, LP_LIGHTMAP | LP_RTWORLD | LP_DYNLIGHT, r_refdef.scene.lightmapintensity, r_refdef.scene.ambientintensity);
4173 d = DotProduct(forward, diffusenormal);
4174 brightness += VectorLength(ambient);
4176 brightness += d * VectorLength(diffuse);
4178 brightness *= 1.0f / c;
4179 brightness += 0.00001f; // make sure it's never zero
4180 goal = r_hdr_irisadaptation_multiplier.value / brightness;
4181 goal = bound(r_hdr_irisadaptation_minvalue.value, goal, r_hdr_irisadaptation_maxvalue.value);
4182 current = r_hdr_irisadaptation_value.value;
4184 current = min(current + r_hdr_irisadaptation_fade_up.value * cl.realframetime, goal);
4185 else if (current > goal)
4186 current = max(current - r_hdr_irisadaptation_fade_down.value * cl.realframetime, goal);
4187 if (fabs(r_hdr_irisadaptation_value.value - current) > 0.0001f)
4188 Cvar_SetValueQuick(&r_hdr_irisadaptation_value, current);
4190 else if (r_hdr_irisadaptation_value.value != 1.0f)
4191 Cvar_SetValueQuick(&r_hdr_irisadaptation_value, 1.0f);
4194 extern cvar_t r_lockvisibility;
4195 extern cvar_t r_lockpvs;
4197 static void R_View_SetFrustum(const int *scissor)
4200 double fpx = +1, fnx = -1, fpy = +1, fny = -1;
4201 vec3_t forward, left, up, origin, v;
4202 if(r_lockvisibility.integer)
4206 // flipped x coordinates (because x points left here)
4207 fpx = 1.0 - 2.0 * (scissor[0] - r_refdef.view.viewport.x) / (double) (r_refdef.view.viewport.width);
4208 fnx = 1.0 - 2.0 * (scissor[0] + scissor[2] - r_refdef.view.viewport.x) / (double) (r_refdef.view.viewport.width);
4209 // non-flipped y coordinates
4210 fny = -1.0 + 2.0 * (scissor[1] - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
4211 fpy = -1.0 + 2.0 * (scissor[1] + scissor[3] - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
4214 // we can't trust r_refdef.view.forward and friends in reflected scenes
4215 Matrix4x4_ToVectors(&r_refdef.view.matrix, forward, left, up, origin);
4218 r_refdef.view.frustum[0].normal[0] = 0 - 1.0 / r_refdef.view.frustum_x;
4219 r_refdef.view.frustum[0].normal[1] = 0 - 0;
4220 r_refdef.view.frustum[0].normal[2] = -1 - 0;
4221 r_refdef.view.frustum[1].normal[0] = 0 + 1.0 / r_refdef.view.frustum_x;
4222 r_refdef.view.frustum[1].normal[1] = 0 + 0;
4223 r_refdef.view.frustum[1].normal[2] = -1 + 0;
4224 r_refdef.view.frustum[2].normal[0] = 0 - 0;
4225 r_refdef.view.frustum[2].normal[1] = 0 - 1.0 / r_refdef.view.frustum_y;
4226 r_refdef.view.frustum[2].normal[2] = -1 - 0;
4227 r_refdef.view.frustum[3].normal[0] = 0 + 0;
4228 r_refdef.view.frustum[3].normal[1] = 0 + 1.0 / r_refdef.view.frustum_y;
4229 r_refdef.view.frustum[3].normal[2] = -1 + 0;
4233 zNear = r_refdef.nearclip;
4234 nudge = 1.0 - 1.0 / (1<<23);
4235 r_refdef.view.frustum[4].normal[0] = 0 - 0;
4236 r_refdef.view.frustum[4].normal[1] = 0 - 0;
4237 r_refdef.view.frustum[4].normal[2] = -1 - -nudge;
4238 r_refdef.view.frustum[4].dist = 0 - -2 * zNear * nudge;
4239 r_refdef.view.frustum[5].normal[0] = 0 + 0;
4240 r_refdef.view.frustum[5].normal[1] = 0 + 0;
4241 r_refdef.view.frustum[5].normal[2] = -1 + -nudge;
4242 r_refdef.view.frustum[5].dist = 0 + -2 * zNear * nudge;
4248 r_refdef.view.frustum[0].normal[0] = m[3] - m[0];
4249 r_refdef.view.frustum[0].normal[1] = m[7] - m[4];
4250 r_refdef.view.frustum[0].normal[2] = m[11] - m[8];
4251 r_refdef.view.frustum[0].dist = m[15] - m[12];
4253 r_refdef.view.frustum[1].normal[0] = m[3] + m[0];
4254 r_refdef.view.frustum[1].normal[1] = m[7] + m[4];
4255 r_refdef.view.frustum[1].normal[2] = m[11] + m[8];
4256 r_refdef.view.frustum[1].dist = m[15] + m[12];
4258 r_refdef.view.frustum[2].normal[0] = m[3] - m[1];
4259 r_refdef.view.frustum[2].normal[1] = m[7] - m[5];
4260 r_refdef.view.frustum[2].normal[2] = m[11] - m[9];
4261 r_refdef.view.frustum[2].dist = m[15] - m[13];
4263 r_refdef.view.frustum[3].normal[0] = m[3] + m[1];
4264 r_refdef.view.frustum[3].normal[1] = m[7] + m[5];
4265 r_refdef.view.frustum[3].normal[2] = m[11] + m[9];
4266 r_refdef.view.frustum[3].dist = m[15] + m[13];
4268 r_refdef.view.frustum[4].normal[0] = m[3] - m[2];
4269 r_refdef.view.frustum[4].normal[1] = m[7] - m[6];
4270 r_refdef.view.frustum[4].normal[2] = m[11] - m[10];
4271 r_refdef.view.frustum[4].dist = m[15] - m[14];
4273 r_refdef.view.frustum[5].normal[0] = m[3] + m[2];
4274 r_refdef.view.frustum[5].normal[1] = m[7] + m[6];
4275 r_refdef.view.frustum[5].normal[2] = m[11] + m[10];
4276 r_refdef.view.frustum[5].dist = m[15] + m[14];
4279 if (r_refdef.view.useperspective)
4281 // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
4282 VectorMAMAM(1024, forward, fnx * 1024.0 * r_refdef.view.frustum_x, left, fny * 1024.0 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[0]);
4283 VectorMAMAM(1024, forward, fpx * 1024.0 * r_refdef.view.frustum_x, left, fny * 1024.0 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[1]);
4284 VectorMAMAM(1024, forward, fnx * 1024.0 * r_refdef.view.frustum_x, left, fpy * 1024.0 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[2]);
4285 VectorMAMAM(1024, forward, fpx * 1024.0 * r_refdef.view.frustum_x, left, fpy * 1024.0 * r_refdef.view.frustum_y, up, r_refdef.view.frustumcorner[3]);
4287 // then the normals from the corners relative to origin
4288 CrossProduct(r_refdef.view.frustumcorner[2], r_refdef.view.frustumcorner[0], r_refdef.view.frustum[0].normal);
4289 CrossProduct(r_refdef.view.frustumcorner[1], r_refdef.view.frustumcorner[3], r_refdef.view.frustum[1].normal);
4290 CrossProduct(r_refdef.view.frustumcorner[0], r_refdef.view.frustumcorner[1], r_refdef.view.frustum[2].normal);
4291 CrossProduct(r_refdef.view.frustumcorner[3], r_refdef.view.frustumcorner[2], r_refdef.view.frustum[3].normal);
4293 // in a NORMAL view, forward cross left == up
4294 // in a REFLECTED view, forward cross left == down
4295 // so our cross products above need to be adjusted for a left handed coordinate system
4296 CrossProduct(forward, left, v);
4297 if(DotProduct(v, up) < 0)
4299 VectorNegate(r_refdef.view.frustum[0].normal, r_refdef.view.frustum[0].normal);
4300 VectorNegate(r_refdef.view.frustum[1].normal, r_refdef.view.frustum[1].normal);
4301 VectorNegate(r_refdef.view.frustum[2].normal, r_refdef.view.frustum[2].normal);
4302 VectorNegate(r_refdef.view.frustum[3].normal, r_refdef.view.frustum[3].normal);
4305 // Leaving those out was a mistake, those were in the old code, and they
4306 // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
4307 // I couldn't reproduce it after adding those normalizations. --blub
4308 VectorNormalize(r_refdef.view.frustum[0].normal);
4309 VectorNormalize(r_refdef.view.frustum[1].normal);
4310 VectorNormalize(r_refdef.view.frustum[2].normal);
4311 VectorNormalize(r_refdef.view.frustum[3].normal);
4313 // make the corners absolute
4314 VectorAdd(r_refdef.view.frustumcorner[0], r_refdef.view.origin, r_refdef.view.frustumcorner[0]);
4315 VectorAdd(r_refdef.view.frustumcorner[1], r_refdef.view.origin, r_refdef.view.frustumcorner[1]);
4316 VectorAdd(r_refdef.view.frustumcorner[2], r_refdef.view.origin, r_refdef.view.frustumcorner[2]);
4317 VectorAdd(r_refdef.view.frustumcorner[3], r_refdef.view.origin, r_refdef.view.frustumcorner[3]);
4320 VectorCopy(forward, r_refdef.view.frustum[4].normal);
4322 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal);
4323 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal);
4324 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal);
4325 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal);
4326 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) + r_refdef.nearclip;
4330 VectorScale(left, -1.0f, r_refdef.view.frustum[0].normal);
4331 VectorScale(left, 1.0f, r_refdef.view.frustum[1].normal);
4332 VectorScale(up, -1.0f, r_refdef.view.frustum[2].normal);
4333 VectorScale(up, 1.0f, r_refdef.view.frustum[3].normal);
4334 VectorScale(forward, -1.0f, r_refdef.view.frustum[4].normal);
4335 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal) - r_refdef.view.ortho_x;
4336 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal) - r_refdef.view.ortho_x;
4337 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal) - r_refdef.view.ortho_y;
4338 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal) - r_refdef.view.ortho_y;
4339 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) - r_refdef.farclip;
4341 r_refdef.view.numfrustumplanes = 5;
4343 if (r_refdef.view.useclipplane)
4345 r_refdef.view.numfrustumplanes = 6;
4346 r_refdef.view.frustum[5] = r_refdef.view.clipplane;
4349 for (i = 0;i < r_refdef.view.numfrustumplanes;i++)
4350 PlaneClassify(r_refdef.view.frustum + i);
4352 // LadyHavoc: note to all quake engine coders, Quake had a special case
4353 // for 90 degrees which assumed a square view (wrong), so I removed it,
4354 // Quake2 has it disabled as well.
4356 // rotate R_VIEWFORWARD right by FOV_X/2 degrees
4357 //RotatePointAroundVector( r_refdef.view.frustum[0].normal, up, forward, -(90 - r_refdef.fov_x / 2));
4358 //r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, frustum[0].normal);
4359 //PlaneClassify(&frustum[0]);
4361 // rotate R_VIEWFORWARD left by FOV_X/2 degrees
4362 //RotatePointAroundVector( r_refdef.view.frustum[1].normal, up, forward, (90 - r_refdef.fov_x / 2));
4363 //r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, frustum[1].normal);
4364 //PlaneClassify(&frustum[1]);
4366 // rotate R_VIEWFORWARD up by FOV_X/2 degrees
4367 //RotatePointAroundVector( r_refdef.view.frustum[2].normal, left, forward, -(90 - r_refdef.fov_y / 2));
4368 //r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, frustum[2].normal);
4369 //PlaneClassify(&frustum[2]);
4371 // rotate R_VIEWFORWARD down by FOV_X/2 degrees
4372 //RotatePointAroundVector( r_refdef.view.frustum[3].normal, left, forward, (90 - r_refdef.fov_y / 2));
4373 //r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, frustum[3].normal);
4374 //PlaneClassify(&frustum[3]);
4377 //VectorCopy(forward, r_refdef.view.frustum[4].normal);
4378 //r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, frustum[4].normal) + r_nearclip.value;
4379 //PlaneClassify(&frustum[4]);
4382 static void R_View_UpdateWithScissor(const int *myscissor)
4384 R_Main_ResizeViewCache();
4385 R_View_SetFrustum(myscissor);
4386 R_View_WorldVisibility(!r_refdef.view.usevieworiginculling);
4387 R_View_UpdateEntityVisible();
4390 static void R_View_Update(void)
4392 R_Main_ResizeViewCache();
4393 R_View_SetFrustum(NULL);
4394 R_View_WorldVisibility(!r_refdef.view.usevieworiginculling);
4395 R_View_UpdateEntityVisible();
4398 float viewscalefpsadjusted = 1.0f;
4400 void R_SetupView(qbool allowwaterclippingplane, int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4402 const float *customclipplane = NULL;
4404 int /*rtwidth,*/ rtheight;
4405 if (r_refdef.view.useclipplane && allowwaterclippingplane)
4407 // LadyHavoc: couldn't figure out how to make this approach work the same in DPSOFTRAST
4408 vec_t dist = r_refdef.view.clipplane.dist - r_water_clippingplanebias.value;
4409 vec_t viewdist = DotProduct(r_refdef.view.origin, r_refdef.view.clipplane.normal);
4410 if (viewdist < r_refdef.view.clipplane.dist + r_water_clippingplanebias.value)
4411 dist = r_refdef.view.clipplane.dist;
4412 plane[0] = r_refdef.view.clipplane.normal[0];
4413 plane[1] = r_refdef.view.clipplane.normal[1];
4414 plane[2] = r_refdef.view.clipplane.normal[2];
4416 customclipplane = plane;
4419 //rtwidth = viewfbo ? R_TextureWidth(viewdepthtexture ? viewdepthtexture : viewcolortexture) : vid.width;
4420 rtheight = viewfbo ? R_TextureHeight(viewdepthtexture ? viewdepthtexture : viewcolortexture) : vid.height;
4422 if (!r_refdef.view.useperspective)
4423 R_Viewport_InitOrtho3D(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, rtheight - viewheight - viewy, viewwidth, viewheight, r_refdef.view.ortho_x, r_refdef.view.ortho_y, -r_refdef.farclip, r_refdef.farclip, customclipplane);
4424 else if (vid.stencil && r_useinfinitefarclip.integer)
4425 R_Viewport_InitPerspectiveInfinite(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, rtheight - viewheight - viewy, viewwidth, viewheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, customclipplane);
4427 R_Viewport_InitPerspective(&r_refdef.view.viewport, &r_refdef.view.matrix, viewx, rtheight - viewheight - viewy, viewwidth, viewheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, r_refdef.farclip, customclipplane);
4428 R_Mesh_SetRenderTargets(viewfbo, viewdepthtexture, viewcolortexture, NULL, NULL, NULL);
4429 R_SetViewport(&r_refdef.view.viewport);
4432 void R_EntityMatrix(const matrix4x4_t *matrix)
4434 if (gl_modelmatrixchanged || memcmp(matrix, &gl_modelmatrix, sizeof(matrix4x4_t)))
4436 gl_modelmatrixchanged = false;
4437 gl_modelmatrix = *matrix;
4438 Matrix4x4_Concat(&gl_modelviewmatrix, &gl_viewmatrix, &gl_modelmatrix);
4439 Matrix4x4_Concat(&gl_modelviewprojectionmatrix, &gl_projectionmatrix, &gl_modelviewmatrix);
4440 Matrix4x4_ToArrayFloatGL(&gl_modelviewmatrix, gl_modelview16f);
4441 Matrix4x4_ToArrayFloatGL(&gl_modelviewprojectionmatrix, gl_modelviewprojection16f);
4443 switch(vid.renderpath)
4445 case RENDERPATH_GL32:
4446 case RENDERPATH_GLES2:
4447 if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
4448 if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
4454 void R_ResetViewRendering2D_Common(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight, float x2, float y2)
4456 r_viewport_t viewport;
4460 // GL is weird because it's bottom to top, r_refdef.view.y is top to bottom
4461 R_Viewport_InitOrtho(&viewport, &identitymatrix, viewx, vid.height - viewheight - viewy, viewwidth, viewheight, 0, 0, x2, y2, -10, 100, NULL);
4462 R_Mesh_SetRenderTargets(viewfbo, viewdepthtexture, viewcolortexture, NULL, NULL, NULL);
4463 R_SetViewport(&viewport);
4464 GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
4465 GL_Color(1, 1, 1, 1);
4466 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4467 GL_BlendFunc(GL_ONE, GL_ZERO);
4468 GL_ScissorTest(false);
4469 GL_DepthMask(false);
4470 GL_DepthRange(0, 1);
4471 GL_DepthTest(false);
4472 GL_DepthFunc(GL_LEQUAL);
4473 R_EntityMatrix(&identitymatrix);
4474 R_Mesh_ResetTextureState();
4475 GL_PolygonOffset(0, 0);
4476 switch(vid.renderpath)
4478 case RENDERPATH_GL32:
4479 case RENDERPATH_GLES2:
4480 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
4483 GL_CullFace(GL_NONE);
4488 void R_ResetViewRendering2D(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4490 R_ResetViewRendering2D_Common(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight, 1.0f, 1.0f);
4493 void R_ResetViewRendering3D(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
4495 R_SetupView(true, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
4496 GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
4497 GL_Color(1, 1, 1, 1);
4498 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
4499 GL_BlendFunc(GL_ONE, GL_ZERO);
4500 GL_ScissorTest(true);
4502 GL_DepthRange(0, 1);
4504 GL_DepthFunc(GL_LEQUAL);
4505 R_EntityMatrix(&identitymatrix);
4506 R_Mesh_ResetTextureState();
4507 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
4508 switch(vid.renderpath)
4510 case RENDERPATH_GL32:
4511 case RENDERPATH_GLES2:
4512 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
4515 GL_CullFace(r_refdef.view.cullface_back);
4520 R_RenderView_UpdateViewVectors
4523 void R_RenderView_UpdateViewVectors(void)
4525 // break apart the view matrix into vectors for various purposes
4526 // it is important that this occurs outside the RenderScene function because that can be called from reflection renders, where the vectors come out wrong
4527 // however the r_refdef.view.origin IS updated in RenderScene intentionally - otherwise the sky renders at the wrong origin, etc
4528 Matrix4x4_ToVectors(&r_refdef.view.matrix, r_refdef.view.forward, r_refdef.view.left, r_refdef.view.up, r_refdef.view.origin);
4529 VectorNegate(r_refdef.view.left, r_refdef.view.right);
4530 // make an inverted copy of the view matrix for tracking sprites
4531 Matrix4x4_Invert_Full(&r_refdef.view.inverse_matrix, &r_refdef.view.matrix);
4534 void R_RenderTarget_FreeUnused(qbool force)
4536 unsigned int i, j, end;
4537 end = (unsigned int)Mem_ExpandableArray_IndexRange(&r_fb.rendertargets); // checked
4538 for (i = 0; i < end; i++)
4540 r_rendertarget_t *r = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, i);
4541 // free resources for rendertargets that have not been used for a while
4542 // (note: this check is run after the frame render, so any targets used
4543 // this frame will not be affected even at low framerates)
4544 if (r && (host.realtime - r->lastusetime > 0.2 || force))
4547 R_Mesh_DestroyFramebufferObject(r->fbo);
4548 for (j = 0; j < sizeof(r->colortexture) / sizeof(r->colortexture[0]); j++)
4549 if (r->colortexture[j])
4550 R_FreeTexture(r->colortexture[j]);
4551 if (r->depthtexture)
4552 R_FreeTexture(r->depthtexture);
4553 Mem_ExpandableArray_FreeRecord(&r_fb.rendertargets, r);
4558 static void R_CalcTexCoordsForView(float x, float y, float w, float h, float tw, float th, float *texcoord2f)
4560 float iw = 1.0f / tw, ih = 1.0f / th, x1, y1, x2, y2;
4564 y2 = (th - y - h) * ih;
4575 r_rendertarget_t *R_RenderTarget_Get(int texturewidth, int textureheight, textype_t depthtextype, qbool depthisrenderbuffer, textype_t colortextype0, textype_t colortextype1, textype_t colortextype2, textype_t colortextype3)
4577 unsigned int i, j, end;
4578 r_rendertarget_t *r = NULL;
4580 // first try to reuse an existing slot if possible
4581 end = (unsigned int)Mem_ExpandableArray_IndexRange(&r_fb.rendertargets); // checked
4582 for (i = 0; i < end; i++)
4584 r = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, i);
4585 if (r && r->lastusetime != host.realtime && r->texturewidth == texturewidth && r->textureheight == textureheight && r->depthtextype == depthtextype && r->colortextype[0] == colortextype0 && r->colortextype[1] == colortextype1 && r->colortextype[2] == colortextype2 && r->colortextype[3] == colortextype3)
4590 // no unused exact match found, so we have to make one in the first unused slot
4591 r = (r_rendertarget_t *)Mem_ExpandableArray_AllocRecord(&r_fb.rendertargets);
4592 r->texturewidth = texturewidth;
4593 r->textureheight = textureheight;
4594 r->colortextype[0] = colortextype0;
4595 r->colortextype[1] = colortextype1;
4596 r->colortextype[2] = colortextype2;
4597 r->colortextype[3] = colortextype3;
4598 r->depthtextype = depthtextype;
4599 r->depthisrenderbuffer = depthisrenderbuffer;
4600 for (j = 0; j < 4; j++)
4601 if (r->colortextype[j])
4602 r->colortexture[j] = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "rendertarget%i_%i_type%i", i, j, (int)r->colortextype[j]), r->texturewidth, r->textureheight, NULL, r->colortextype[j], TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
4603 if (r->depthtextype)
4605 if (r->depthisrenderbuffer)
4606 r->depthtexture = R_LoadTextureRenderBuffer(r_main_texturepool, va(vabuf, sizeof(vabuf), "renderbuffer%i_depth_type%i", i, (int)r->depthtextype), r->texturewidth, r->textureheight, r->depthtextype);
4608 r->depthtexture = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "rendertarget%i_depth_type%i", i, (int)r->depthtextype), r->texturewidth, r->textureheight, NULL, r->depthtextype, TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
4610 r->fbo = R_Mesh_CreateFramebufferObject(r->depthtexture, r->colortexture[0], r->colortexture[1], r->colortexture[2], r->colortexture[3]);
4612 r_refdef.stats[r_stat_rendertargets_used]++;
4613 r_refdef.stats[r_stat_rendertargets_pixels] += r->texturewidth * r->textureheight;
4614 r->lastusetime = host.realtime;
4615 R_CalcTexCoordsForView(0, 0, r->texturewidth, r->textureheight, r->texturewidth, r->textureheight, r->texcoord2f);
4619 static void R_Water_StartFrame(int viewwidth, int viewheight)
4621 int waterwidth, waterheight;
4623 if (viewwidth > (int)vid.maxtexturesize_2d || viewheight > (int)vid.maxtexturesize_2d)
4626 // set waterwidth and waterheight to the water resolution that will be
4627 // used (often less than the screen resolution for faster rendering)
4628 waterwidth = (int)bound(16, viewwidth * r_water_resolutionmultiplier.value, viewwidth);
4629 waterheight = (int)bound(16, viewheight * r_water_resolutionmultiplier.value, viewheight);
4631 if (!r_water.integer || r_showsurfaces.integer || r_lockvisibility.integer || r_lockpvs.integer)
4632 waterwidth = waterheight = 0;
4634 // set up variables that will be used in shader setup
4635 r_fb.water.waterwidth = waterwidth;
4636 r_fb.water.waterheight = waterheight;
4637 r_fb.water.texturewidth = waterwidth;
4638 r_fb.water.textureheight = waterheight;
4639 r_fb.water.camerawidth = waterwidth;
4640 r_fb.water.cameraheight = waterheight;
4641 r_fb.water.screenscale[0] = 0.5f;
4642 r_fb.water.screenscale[1] = 0.5f;
4643 r_fb.water.screencenter[0] = 0.5f;
4644 r_fb.water.screencenter[1] = 0.5f;
4645 r_fb.water.enabled = waterwidth != 0;
4647 r_fb.water.maxwaterplanes = MAX_WATERPLANES;
4648 r_fb.water.numwaterplanes = 0;
4651 void R_Water_AddWaterPlane(msurface_t *surface, int entno)
4653 int planeindex, bestplaneindex, vertexindex;
4654 vec3_t mins, maxs, normal, center, v, n;
4655 vec_t planescore, bestplanescore;
4657 r_waterstate_waterplane_t *p;
4658 texture_t *t = R_GetCurrentTexture(surface->texture);
4660 rsurface.texture = t;
4661 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS, 1, ((const msurface_t **)&surface));
4662 // if the model has no normals, it's probably off-screen and they were not generated, so don't add it anyway
4663 if (!rsurface.batchnormal3f || rsurface.batchnumvertices < 1)
4665 // average the vertex normals, find the surface bounds (after deformvertexes)
4666 Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f, v);
4667 Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f, n);
4668 VectorCopy(n, normal);
4669 VectorCopy(v, mins);
4670 VectorCopy(v, maxs);
4671 for (vertexindex = 1;vertexindex < rsurface.batchnumvertices;vertexindex++)
4673 Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f + vertexindex*3, v);
4674 Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f + vertexindex*3, n);
4675 VectorAdd(normal, n, normal);
4676 mins[0] = min(mins[0], v[0]);
4677 mins[1] = min(mins[1], v[1]);
4678 mins[2] = min(mins[2], v[2]);
4679 maxs[0] = max(maxs[0], v[0]);
4680 maxs[1] = max(maxs[1], v[1]);
4681 maxs[2] = max(maxs[2], v[2]);
4683 VectorNormalize(normal);
4684 VectorMAM(0.5f, mins, 0.5f, maxs, center);
4686 VectorCopy(normal, plane.normal);
4687 VectorNormalize(plane.normal);
4688 plane.dist = DotProduct(center, plane.normal);
4689 PlaneClassify(&plane);
4690 if (PlaneDiff(r_refdef.view.origin, &plane) < 0)
4692 // skip backfaces (except if nocullface is set)
4693 // if (!(t->currentmaterialflags & MATERIALFLAG_NOCULLFACE))
4695 VectorNegate(plane.normal, plane.normal);
4697 PlaneClassify(&plane);
4701 // find a matching plane if there is one
4702 bestplaneindex = -1;
4703 bestplanescore = 1048576.0f;
4704 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
4706 if(p->camera_entity == t->camera_entity)
4708 planescore = 1.0f - DotProduct(plane.normal, p->plane.normal) + fabs(plane.dist - p->plane.dist) * 0.001f;
4709 if (bestplaneindex < 0 || bestplanescore > planescore)
4711 bestplaneindex = planeindex;
4712 bestplanescore = planescore;
4716 planeindex = bestplaneindex;
4718 // if this surface does not fit any known plane rendered this frame, add one
4719 if (planeindex < 0 || bestplanescore > 0.001f)
4721 if (r_fb.water.numwaterplanes < r_fb.water.maxwaterplanes)
4723 // store the new plane
4724 planeindex = r_fb.water.numwaterplanes++;
4725 p = r_fb.water.waterplanes + planeindex;
4727 // clear materialflags and pvs
4728 p->materialflags = 0;
4729 p->pvsvalid = false;
4730 p->camera_entity = t->camera_entity;
4731 VectorCopy(mins, p->mins);
4732 VectorCopy(maxs, p->maxs);
4736 // We're totally screwed.
4742 // merge mins/maxs when we're adding this surface to the plane
4743 p = r_fb.water.waterplanes + planeindex;
4744 p->mins[0] = min(p->mins[0], mins[0]);
4745 p->mins[1] = min(p->mins[1], mins[1]);
4746 p->mins[2] = min(p->mins[2], mins[2]);
4747 p->maxs[0] = max(p->maxs[0], maxs[0]);
4748 p->maxs[1] = max(p->maxs[1], maxs[1]);
4749 p->maxs[2] = max(p->maxs[2], maxs[2]);
4751 // merge this surface's materialflags into the waterplane
4752 p->materialflags |= t->currentmaterialflags;
4753 if(!(p->materialflags & MATERIALFLAG_CAMERA))
4755 // merge this surface's PVS into the waterplane
4756 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS
4757 && r_refdef.scene.worldmodel->brush.PointInLeaf && r_refdef.scene.worldmodel->brush.PointInLeaf(r_refdef.scene.worldmodel, center)->clusterindex >= 0)
4759 r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, center, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
4765 extern cvar_t r_drawparticles;
4766 extern cvar_t r_drawdecals;
4768 static void R_Water_ProcessPlanes(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, int viewx, int viewy, int viewwidth, int viewheight)
4771 r_refdef_view_t originalview;
4772 r_refdef_view_t myview;
4773 int planeindex, qualityreduction = 0, old_r_dynamic = 0, old_r_shadows = 0, old_r_worldrtlight = 0, old_r_dlight = 0, old_r_particles = 0, old_r_decals = 0;
4774 r_waterstate_waterplane_t *p;
4776 r_rendertarget_t *rt;
4778 originalview = r_refdef.view;
4780 // lowquality hack, temporarily shut down some cvars and restore afterwards
4781 qualityreduction = r_water_lowquality.integer;
4782 if (qualityreduction > 0)
4784 if (qualityreduction >= 1)
4786 old_r_shadows = r_shadows.integer;
4787 old_r_worldrtlight = r_shadow_realtime_world.integer;
4788 old_r_dlight = r_shadow_realtime_dlight.integer;
4789 Cvar_SetValueQuick(&r_shadows, 0);
4790 Cvar_SetValueQuick(&r_shadow_realtime_world, 0);
4791 Cvar_SetValueQuick(&r_shadow_realtime_dlight, 0);
4793 if (qualityreduction >= 2)
4795 old_r_dynamic = r_dynamic.integer;
4796 old_r_particles = r_drawparticles.integer;
4797 old_r_decals = r_drawdecals.integer;
4798 Cvar_SetValueQuick(&r_dynamic, 0);
4799 Cvar_SetValueQuick(&r_drawparticles, 0);
4800 Cvar_SetValueQuick(&r_drawdecals, 0);
4804 for (planeindex = 0, p = r_fb.water.waterplanes; planeindex < r_fb.water.numwaterplanes; planeindex++, p++)
4806 p->rt_reflection = NULL;
4807 p->rt_refraction = NULL;
4808 p->rt_camera = NULL;
4812 r_refdef.view = originalview;
4813 r_refdef.view.showdebug = false;
4814 r_refdef.view.width = r_fb.water.waterwidth;
4815 r_refdef.view.height = r_fb.water.waterheight;
4816 r_refdef.view.useclipplane = true;
4817 myview = r_refdef.view;
4818 r_fb.water.renderingscene = true;
4819 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
4821 if (r_water_cameraentitiesonly.value != 0 && !p->camera_entity)
4824 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
4826 rt = R_RenderTarget_Get(r_fb.water.waterwidth, r_fb.water.waterheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, r_fb.rt_screen->colortextype[0], TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
4827 if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4829 r_refdef.view = myview;
4830 Matrix4x4_Reflect(&r_refdef.view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
4831 Matrix4x4_OriginFromMatrix(&r_refdef.view.matrix, r_refdef.view.origin);
4832 if(r_water_scissormode.integer)
4834 R_SetupView(true, rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, r_fb.water.waterwidth, r_fb.water.waterheight);
4835 if (R_ScissorForBBox(p->mins, p->maxs, myscissor))
4837 p->rt_reflection = NULL;
4838 p->rt_refraction = NULL;
4839 p->rt_camera = NULL;
4844 r_refdef.view.clipplane = p->plane;
4845 // reflected view origin may be in solid, so don't cull with it
4846 r_refdef.view.usevieworiginculling = false;
4847 // reverse the cullface settings for this render
4848 r_refdef.view.cullface_front = GL_FRONT;
4849 r_refdef.view.cullface_back = GL_BACK;
4850 // combined pvs (based on what can be seen from each surface center)
4851 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
4853 r_refdef.view.usecustompvs = true;
4855 memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4857 memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4860 r_fb.water.hideplayer = ((r_water_hideplayer.integer >= 2) && !chase_active.integer);
4861 R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4862 GL_ScissorTest(false);
4863 R_ClearScreen(r_refdef.fogenabled);
4864 GL_ScissorTest(true);
4865 if(r_water_scissormode.integer & 2)
4866 R_View_UpdateWithScissor(myscissor);
4869 R_AnimCache_CacheVisibleEntities();
4870 if(r_water_scissormode.integer & 1)
4871 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
4872 R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4874 r_fb.water.hideplayer = false;
4875 p->rt_reflection = rt;
4878 // render the normal view scene and copy into texture
4879 // (except that a clipping plane should be used to hide everything on one side of the water, and the viewer's weapon model should be omitted)
4880 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
4882 rt = R_RenderTarget_Get(r_fb.water.waterwidth, r_fb.water.waterheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, r_fb.rt_screen->colortextype[0], TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
4883 if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4885 r_refdef.view = myview;
4886 if(r_water_scissormode.integer)
4888 R_SetupView(true, rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, r_fb.water.waterwidth, r_fb.water.waterheight);
4889 if (R_ScissorForBBox(p->mins, p->maxs, myscissor))
4891 p->rt_reflection = NULL;
4892 p->rt_refraction = NULL;
4893 p->rt_camera = NULL;
4898 // combined pvs (based on what can be seen from each surface center)
4899 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
4901 r_refdef.view.usecustompvs = true;
4903 memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4905 memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
4908 r_fb.water.hideplayer = ((r_water_hideplayer.integer >= 1) && !chase_active.integer);
4910 r_refdef.view.clipplane = p->plane;
4911 VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
4912 r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
4914 if((p->materialflags & MATERIALFLAG_CAMERA) && p->camera_entity)
4916 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
4917 r_fb.water.hideplayer = false; // we don't want to hide the player model from these ones
4918 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
4919 R_RenderView_UpdateViewVectors();
4920 if(r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
4922 r_refdef.view.usecustompvs = true;
4923 r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, visorigin, 2, r_refdef.viewcache.world_pvsbits, (r_refdef.viewcache.world_numclusters+7)>>3, false);
4927 PlaneClassify(&r_refdef.view.clipplane);
4929 R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4930 GL_ScissorTest(false);
4931 R_ClearScreen(r_refdef.fogenabled);
4932 GL_ScissorTest(true);
4933 if(r_water_scissormode.integer & 2)
4934 R_View_UpdateWithScissor(myscissor);
4937 R_AnimCache_CacheVisibleEntities();
4938 if(r_water_scissormode.integer & 1)
4939 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
4940 R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4942 r_fb.water.hideplayer = false;
4943 p->rt_refraction = rt;
4945 else if (p->materialflags & MATERIALFLAG_CAMERA)
4947 rt = R_RenderTarget_Get(r_fb.water.waterwidth, r_fb.water.waterheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, r_fb.rt_screen->colortextype[0], TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
4948 if (rt->colortexture[0] == NULL || rt->depthtexture == NULL)
4950 r_refdef.view = myview;
4952 r_refdef.view.clipplane = p->plane;
4953 VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
4954 r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
4956 r_refdef.view.width = r_fb.water.camerawidth;
4957 r_refdef.view.height = r_fb.water.cameraheight;
4958 r_refdef.view.frustum_x = 1; // tan(45 * M_PI / 180.0);
4959 r_refdef.view.frustum_y = 1; // tan(45 * M_PI / 180.0);
4960 r_refdef.view.ortho_x = 90; // abused as angle by VM_CL_R_SetView
4961 r_refdef.view.ortho_y = 90; // abused as angle by VM_CL_R_SetView
4963 if(p->camera_entity)
4965 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
4966 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
4969 // note: all of the view is used for displaying... so
4970 // there is no use in scissoring
4972 // reverse the cullface settings for this render
4973 r_refdef.view.cullface_front = GL_FRONT;
4974 r_refdef.view.cullface_back = GL_BACK;
4975 // also reverse the view matrix
4976 Matrix4x4_ConcatScale3(&r_refdef.view.matrix, 1, 1, -1); // this serves to invert texcoords in the result, as the copied texture is mapped the wrong way round
4977 R_RenderView_UpdateViewVectors();
4978 if(p->camera_entity && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
4980 r_refdef.view.usecustompvs = true;
4981 r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, visorigin, 2, r_refdef.viewcache.world_pvsbits, (r_refdef.viewcache.world_numclusters+7)>>3, false);
4984 // camera needs no clipplane
4985 r_refdef.view.useclipplane = false;
4986 // TODO: is the camera origin always valid? if so we don't need to clear this
4987 r_refdef.view.usevieworiginculling = false;
4989 PlaneClassify(&r_refdef.view.clipplane);
4991 r_fb.water.hideplayer = false;
4993 R_ResetViewRendering3D(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
4994 GL_ScissorTest(false);
4995 R_ClearScreen(r_refdef.fogenabled);
4996 GL_ScissorTest(true);
4998 R_AnimCache_CacheVisibleEntities();
4999 R_RenderScene(rt->fbo, rt->depthtexture, rt->colortexture[0], 0, 0, rt->texturewidth, rt->textureheight);
5001 r_fb.water.hideplayer = false;
5006 r_fb.water.renderingscene = false;
5007 r_refdef.view = originalview;
5008 R_ResetViewRendering3D(fbo, depthtexture, colortexture, viewx, viewy, viewwidth, viewheight);
5010 R_AnimCache_CacheVisibleEntities();
5013 r_refdef.view = originalview;
5014 r_fb.water.renderingscene = false;
5015 Cvar_SetValueQuick(&r_water, 0);
5016 Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed! Turned off r_water.\n");
5018 // lowquality hack, restore cvars
5019 if (qualityreduction > 0)
5021 if (qualityreduction >= 1)
5023 Cvar_SetValueQuick(&r_shadows, old_r_shadows);
5024 Cvar_SetValueQuick(&r_shadow_realtime_world, old_r_worldrtlight);
5025 Cvar_SetValueQuick(&r_shadow_realtime_dlight, old_r_dlight);
5027 if (qualityreduction >= 2)
5029 Cvar_SetValueQuick(&r_dynamic, old_r_dynamic);
5030 Cvar_SetValueQuick(&r_drawparticles, old_r_particles);
5031 Cvar_SetValueQuick(&r_drawdecals, old_r_decals);
5036 static void R_Bloom_StartFrame(void)
5038 int screentexturewidth, screentextureheight;
5039 textype_t textype = TEXTYPE_COLORBUFFER;
5042 // clear the pointers to rendertargets from last frame as they're stale
5043 r_fb.rt_screen = NULL;
5044 r_fb.rt_bloom = NULL;
5046 switch (vid.renderpath)
5048 case RENDERPATH_GL32:
5049 r_fb.usedepthtextures = r_usedepthtextures.integer != 0;
5050 if (r_viewfbo.integer == 2) textype = TEXTYPE_COLORBUFFER16F;
5051 if (r_viewfbo.integer == 3) textype = TEXTYPE_COLORBUFFER32F;
5053 case RENDERPATH_GLES2:
5054 r_fb.usedepthtextures = false;
5058 if (r_viewscale_fpsscaling.integer)
5060 double actualframetime;
5061 double targetframetime;
5063 actualframetime = r_refdef.lastdrawscreentime;
5064 targetframetime = (1.0 / r_viewscale_fpsscaling_target.value);
5065 adjust = (targetframetime - actualframetime) * r_viewscale_fpsscaling_multiply.value;
5066 adjust = bound(-r_viewscale_fpsscaling_stepmax.value, adjust, r_viewscale_fpsscaling_stepmax.value);
5067 if (r_viewscale_fpsscaling_stepsize.value > 0)
5070 adjust = floor(adjust / r_viewscale_fpsscaling_stepsize.value) * r_viewscale_fpsscaling_stepsize.value;
5072 adjust = ceil(adjust / r_viewscale_fpsscaling_stepsize.value) * r_viewscale_fpsscaling_stepsize.value;
5074 viewscalefpsadjusted += adjust;
5075 viewscalefpsadjusted = bound(r_viewscale_fpsscaling_min.value, viewscalefpsadjusted, 1.0f);
5078 viewscalefpsadjusted = 1.0f;
5080 scale = r_viewscale.value * sqrt(viewscalefpsadjusted);
5082 scale *= sqrt(vid.samples); // supersampling
5083 scale = bound(0.03125f, scale, 4.0f);
5084 screentexturewidth = (int)ceil(r_refdef.view.width * scale);
5085 screentextureheight = (int)ceil(r_refdef.view.height * scale);
5086 screentexturewidth = bound(1, screentexturewidth, (int)vid.maxtexturesize_2d);
5087 screentextureheight = bound(1, screentextureheight, (int)vid.maxtexturesize_2d);
5089 // set bloomwidth and bloomheight to the bloom resolution that will be
5090 // used (often less than the screen resolution for faster rendering)
5091 r_fb.bloomheight = bound(1, r_bloom_resolution.value * 0.75f, screentextureheight);
5092 r_fb.bloomwidth = r_fb.bloomheight * screentexturewidth / screentextureheight;
5093 r_fb.bloomwidth = bound(1, r_fb.bloomwidth, screentexturewidth);
5094 r_fb.bloomwidth = bound(1, r_fb.bloomwidth, (int)vid.maxtexturesize_2d);
5095 r_fb.bloomheight = bound(1, r_fb.bloomheight, (int)vid.maxtexturesize_2d);
5097 if ((r_bloom.integer || (!R_Stereo_Active() && (r_motionblur.value > 0 || r_damageblur.value > 0))) && ((r_bloom_resolution.integer < 4 || r_bloom_blur.value < 1 || r_bloom_blur.value >= 512) || r_refdef.view.width > (int)vid.maxtexturesize_2d || r_refdef.view.height > (int)vid.maxtexturesize_2d))
5099 Cvar_SetValueQuick(&r_bloom, 0);
5100 Cvar_SetValueQuick(&r_motionblur, 0);
5101 Cvar_SetValueQuick(&r_damageblur, 0);
5103 if (!r_bloom.integer)
5104 r_fb.bloomwidth = r_fb.bloomheight = 0;
5106 // allocate motionblur ghost texture if needed - this is the only persistent texture and is only useful on the main view
5107 if (r_refdef.view.ismain && (r_fb.screentexturewidth != screentexturewidth || r_fb.screentextureheight != screentextureheight || r_fb.textype != textype))
5109 if (r_fb.ghosttexture)
5110 R_FreeTexture(r_fb.ghosttexture);
5111 r_fb.ghosttexture = NULL;
5113 r_fb.screentexturewidth = screentexturewidth;
5114 r_fb.screentextureheight = screentextureheight;
5115 r_fb.textype = textype;
5117 if (r_fb.screentexturewidth && r_fb.screentextureheight)
5119 if (r_motionblur.value > 0 || r_damageblur.value > 0)
5120 r_fb.ghosttexture = R_LoadTexture2D(r_main_texturepool, "framebuffermotionblur", r_fb.screentexturewidth, r_fb.screentextureheight, NULL, r_fb.textype, TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
5121 r_fb.ghosttexture_valid = false;
5125 r_fb.rt_screen = R_RenderTarget_Get(screentexturewidth, screentextureheight, TEXTYPE_DEPTHBUFFER24STENCIL8, true, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5127 r_refdef.view.clear = true;
5130 static void R_Bloom_MakeTexture(void)
5133 float xoffset, yoffset, r, brighten;
5134 float colorscale = r_bloom_colorscale.value;
5135 r_viewport_t bloomviewport;
5136 r_rendertarget_t *prev, *cur;
5137 textype_t textype = r_fb.rt_screen->colortextype[0];
5139 r_refdef.stats[r_stat_bloom]++;
5141 R_Viewport_InitOrtho(&bloomviewport, &identitymatrix, 0, 0, r_fb.bloomwidth, r_fb.bloomheight, 0, 0, 1, 1, -10, 100, NULL);
5143 // scale down screen texture to the bloom texture size
5145 prev = r_fb.rt_screen;
5146 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5147 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5148 R_SetViewport(&bloomviewport);
5149 GL_CullFace(GL_NONE);
5150 GL_DepthTest(false);
5151 GL_BlendFunc(GL_ONE, GL_ZERO);
5152 GL_Color(colorscale, colorscale, colorscale, 1);
5153 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, prev->texcoord2f);
5154 // TODO: do boxfilter scale-down in shader?
5155 R_SetupShader_Generic(prev->colortexture[0], false, true, true);
5156 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5157 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5158 // we now have a properly scaled bloom image
5160 // multiply bloom image by itself as many times as desired to darken it
5161 // TODO: if people actually use this it could be done more quickly in the previous shader pass
5162 for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
5165 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5166 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5168 r = bound(0, r_bloom_colorexponent.value / x, 1); // always 0.5 to 1
5170 GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 0);
5171 GL_BlendFunc(GL_SRC_COLOR, GL_ZERO); // square it
5172 GL_Color(1,1,1,1); // no fix factor supported here
5173 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, prev->texcoord2f);
5174 R_SetupShader_Generic(prev->colortexture[0], false, true, false);
5175 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5176 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5180 range = r_bloom_blur.integer * r_fb.bloomwidth / 320;
5181 brighten = r_bloom_brighten.value;
5182 brighten = sqrt(brighten);
5184 brighten *= (3 * range) / (2 * range - 1); // compensate for the "dot particle"
5186 for (dir = 0;dir < 2;dir++)
5189 cur = R_RenderTarget_Get(r_fb.bloomwidth, r_fb.bloomheight, TEXTYPE_UNUSED, false, textype, TEXTYPE_UNUSED, TEXTYPE_UNUSED, TEXTYPE_UNUSED);
5190 R_Mesh_SetRenderTargets(cur->fbo, NULL, cur->colortexture[0], NULL, NULL, NULL);
5191 // blend on at multiple vertical offsets to achieve a vertical blur
5192 // TODO: do offset blends using GLSL
5193 // TODO instead of changing the texcoords, change the target positions to prevent artifacts at edges
5195 GL_BlendFunc(GL_ONE, GL_ZERO);
5197 R_SetupShader_Generic(prev->colortexture[0], false, true, false);
5199 for (x = -range;x <= range;x++)
5201 if (!dir){xoffset = 0;yoffset = x;}
5202 else {xoffset = x;yoffset = 0;}
5203 xoffset /= (float)prev->texturewidth;
5204 yoffset /= (float)prev->textureheight;
5205 // compute a texcoord array with the specified x and y offset
5206 r_fb.offsettexcoord2f[0] = xoffset+prev->texcoord2f[0];
5207 r_fb.offsettexcoord2f[1] = yoffset+prev->texcoord2f[1];
5208 r_fb.offsettexcoord2f[2] = xoffset+prev->texcoord2f[2];
5209 r_fb.offsettexcoord2f[3] = yoffset+prev->texcoord2f[3];
5210 r_fb.offsettexcoord2f[4] = xoffset+prev->texcoord2f[4];
5211 r_fb.offsettexcoord2f[5] = yoffset+prev->texcoord2f[5];
5212 r_fb.offsettexcoord2f[6] = xoffset+prev->texcoord2f[6];
5213 r_fb.offsettexcoord2f[7] = yoffset+prev->texcoord2f[7];
5214 // this r value looks like a 'dot' particle, fading sharply to
5215 // black at the edges
5216 // (probably not realistic but looks good enough)
5217 //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
5218 //r = brighten/(range*2+1);
5219 r = brighten / (range * 2 + 1);
5221 r *= (1 - x*x/(float)((range+1)*(range+1)));
5225 GL_Color(r, r, r, 1);
5227 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.offsettexcoord2f);
5229 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5230 r_refdef.stats[r_stat_bloom_drawpixels] += r_fb.bloomwidth * r_fb.bloomheight;
5232 GL_BlendFunc(GL_ONE, GL_ONE);
5237 // now we have the bloom image, so keep track of it
5238 r_fb.rt_bloom = cur;
5241 static void R_BlendView(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5243 uint64_t permutation;
5244 float uservecs[4][4];
5245 rtexture_t *viewtexture;
5246 rtexture_t *bloomtexture;
5248 R_EntityMatrix(&identitymatrix);
5250 if(r_refdef.view.ismain && !R_Stereo_Active() && (r_motionblur.value > 0 || r_damageblur.value > 0) && r_fb.ghosttexture)
5252 // declare variables
5253 float blur_factor, blur_mouseaccel, blur_velocity;
5254 static float blur_average;
5255 static vec3_t blur_oldangles; // used to see how quickly the mouse is moving
5257 // set a goal for the factoring
5258 blur_velocity = bound(0, (VectorLength(cl.movement_velocity) - r_motionblur_velocityfactor_minspeed.value)
5259 / max(1, r_motionblur_velocityfactor_maxspeed.value - r_motionblur_velocityfactor_minspeed.value), 1);
5260 blur_mouseaccel = bound(0, ((fabs(VectorLength(cl.viewangles) - VectorLength(blur_oldangles)) * 10) - r_motionblur_mousefactor_minspeed.value)
5261 / max(1, r_motionblur_mousefactor_maxspeed.value - r_motionblur_mousefactor_minspeed.value), 1);
5262 blur_factor = ((blur_velocity * r_motionblur_velocityfactor.value)
5263 + (blur_mouseaccel * r_motionblur_mousefactor.value));
5265 // from the goal, pick an averaged value between goal and last value
5266 cl.motionbluralpha = bound(0, (cl.time - cl.oldtime) / max(0.001, r_motionblur_averaging.value), 1);
5267 blur_average = blur_average * (1 - cl.motionbluralpha) + blur_factor * cl.motionbluralpha;
5269 // enforce minimum amount of blur
5270 blur_factor = blur_average * (1 - r_motionblur_minblur.value) + r_motionblur_minblur.value;
5272 //Con_Printf("motionblur: direct factor: %f, averaged factor: %f, velocity: %f, mouse accel: %f \n", blur_factor, blur_average, blur_velocity, blur_mouseaccel);
5274 // calculate values into a standard alpha
5275 cl.motionbluralpha = 1 - exp(-
5277 (r_motionblur.value * blur_factor / 80)
5279 (r_damageblur.value * (cl.cshifts[CSHIFT_DAMAGE].percent / 1600))
5282 max(0.0001, cl.time - cl.oldtime) // fps independent
5285 // randomization for the blur value to combat persistent ghosting
5286 cl.motionbluralpha *= lhrandom(1 - r_motionblur_randomize.value, 1 + r_motionblur_randomize.value);
5287 cl.motionbluralpha = bound(0, cl.motionbluralpha, r_motionblur_maxblur.value);
5290 R_ResetViewRendering2D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5291 if (cl.motionbluralpha > 0 && !r_refdef.envmap && r_fb.ghosttexture_valid)
5293 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
5294 GL_Color(1, 1, 1, cl.motionbluralpha);
5295 R_CalcTexCoordsForView(0, 0, viewwidth, viewheight, viewwidth, viewheight, r_fb.ghosttexcoord2f);
5296 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.ghosttexcoord2f);
5297 R_SetupShader_Generic(r_fb.ghosttexture, false, true, true);
5298 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5299 r_refdef.stats[r_stat_bloom_drawpixels] += viewwidth * viewheight;
5302 // updates old view angles for next pass
5303 VectorCopy(cl.viewangles, blur_oldangles);
5305 // copy view into the ghost texture
5306 R_Mesh_CopyToTexture(r_fb.ghosttexture, 0, 0, viewx, viewy, viewwidth, viewheight);
5307 r_refdef.stats[r_stat_bloom_copypixels] += viewwidth * viewheight;
5308 r_fb.ghosttexture_valid = true;
5311 if (r_fb.bloomwidth)
5313 // make the bloom texture
5314 R_Bloom_MakeTexture();
5317 #if _MSC_VER >= 1400
5318 #define sscanf sscanf_s
5320 memset(uservecs, 0, sizeof(uservecs));
5321 if (r_glsl_postprocess_uservec1_enable.integer)
5322 sscanf(r_glsl_postprocess_uservec1.string, "%f %f %f %f", &uservecs[0][0], &uservecs[0][1], &uservecs[0][2], &uservecs[0][3]);
5323 if (r_glsl_postprocess_uservec2_enable.integer)
5324 sscanf(r_glsl_postprocess_uservec2.string, "%f %f %f %f", &uservecs[1][0], &uservecs[1][1], &uservecs[1][2], &uservecs[1][3]);
5325 if (r_glsl_postprocess_uservec3_enable.integer)
5326 sscanf(r_glsl_postprocess_uservec3.string, "%f %f %f %f", &uservecs[2][0], &uservecs[2][1], &uservecs[2][2], &uservecs[2][3]);
5327 if (r_glsl_postprocess_uservec4_enable.integer)
5328 sscanf(r_glsl_postprocess_uservec4.string, "%f %f %f %f", &uservecs[3][0], &uservecs[3][1], &uservecs[3][2], &uservecs[3][3]);
5330 // render to the screen fbo
5331 R_ResetViewRendering2D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5332 GL_Color(1, 1, 1, 1);
5333 GL_BlendFunc(GL_ONE, GL_ZERO);
5335 viewtexture = r_fb.rt_screen->colortexture[0];
5336 bloomtexture = r_fb.rt_bloom ? r_fb.rt_bloom->colortexture[0] : NULL;
5338 if (r_rendertarget_debug.integer >= 0)
5340 r_rendertarget_t *rt = (r_rendertarget_t *)Mem_ExpandableArray_RecordAtIndex(&r_fb.rendertargets, r_rendertarget_debug.integer);
5341 if (rt && rt->colortexture[0])
5343 viewtexture = rt->colortexture[0];
5344 bloomtexture = NULL;
5348 R_Mesh_PrepareVertices_Mesh_Arrays(4, r_screenvertex3f, NULL, NULL, NULL, NULL, r_fb.rt_screen->texcoord2f, bloomtexture ? r_fb.rt_bloom->texcoord2f : NULL);
5349 switch(vid.renderpath)
5351 case RENDERPATH_GL32:
5352 case RENDERPATH_GLES2:
5354 (r_fb.bloomwidth ? SHADERPERMUTATION_BLOOM : 0)
5355 | (r_refdef.viewblend[3] > 0 ? SHADERPERMUTATION_VIEWTINT : 0)
5356 | (!vid_gammatables_trivial ? SHADERPERMUTATION_GAMMARAMPS : 0)
5357 | (r_glsl_postprocess.integer ? SHADERPERMUTATION_POSTPROCESSING : 0)
5358 | ((!R_Stereo_ColorMasking() && r_glsl_saturation.value != 1) ? SHADERPERMUTATION_SATURATION : 0);
5359 R_SetupShader_SetPermutationGLSL(SHADERMODE_POSTPROCESS, permutation);
5360 if (r_glsl_permutation->tex_Texture_First >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , viewtexture);
5361 if (r_glsl_permutation->tex_Texture_Second >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second , bloomtexture);
5362 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps );
5363 if (r_glsl_permutation->loc_ViewTintColor >= 0) qglUniform4f(r_glsl_permutation->loc_ViewTintColor , r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
5364 if (r_glsl_permutation->loc_PixelSize >= 0) qglUniform2f(r_glsl_permutation->loc_PixelSize , 1.0/r_fb.screentexturewidth, 1.0/r_fb.screentextureheight);
5365 if (r_glsl_permutation->loc_UserVec1 >= 0) qglUniform4f(r_glsl_permutation->loc_UserVec1 , uservecs[0][0], uservecs[0][1], uservecs[0][2], uservecs[0][3]);
5366 if (r_glsl_permutation->loc_UserVec2 >= 0) qglUniform4f(r_glsl_permutation->loc_UserVec2 , uservecs[1][0], uservecs[1][1], uservecs[1][2], uservecs[1][3]);
5367 if (r_glsl_permutation->loc_UserVec3 >= 0) qglUniform4f(r_glsl_permutation->loc_UserVec3 , uservecs[2][0], uservecs[2][1], uservecs[2][2], uservecs[2][3]);
5368 if (r_glsl_permutation->loc_UserVec4 >= 0) qglUniform4f(r_glsl_permutation->loc_UserVec4 , uservecs[3][0], uservecs[3][1], uservecs[3][2], uservecs[3][3]);
5369 if (r_glsl_permutation->loc_Saturation >= 0) qglUniform1f(r_glsl_permutation->loc_Saturation , r_glsl_saturation.value);
5370 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/r_fb.screentexturewidth, 1.0f/r_fb.screentextureheight);
5371 if (r_glsl_permutation->loc_BloomColorSubtract >= 0) qglUniform4f(r_glsl_permutation->loc_BloomColorSubtract , r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 0.0f);
5372 if (r_glsl_permutation->loc_ColorFringe >= 0) qglUniform1f(r_glsl_permutation->loc_ColorFringe, r_colorfringe.value );
5375 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
5376 r_refdef.stats[r_stat_bloom_drawpixels] += r_refdef.view.width * r_refdef.view.height;
5379 matrix4x4_t r_waterscrollmatrix;
5381 void R_UpdateFog(void)
5384 if (gamemode == GAME_NEHAHRA)
5386 if (gl_fogenable.integer)
5388 r_refdef.oldgl_fogenable = true;
5389 r_refdef.fog_density = gl_fogdensity.value;
5390 r_refdef.fog_red = gl_fogred.value;
5391 r_refdef.fog_green = gl_foggreen.value;
5392 r_refdef.fog_blue = gl_fogblue.value;
5393 r_refdef.fog_alpha = 1;
5394 r_refdef.fog_start = 0;
5395 r_refdef.fog_end = gl_skyclip.value;
5396 r_refdef.fog_height = 1<<30;
5397 r_refdef.fog_fadedepth = 128;
5399 else if (r_refdef.oldgl_fogenable)
5401 r_refdef.oldgl_fogenable = false;
5402 r_refdef.fog_density = 0;
5403 r_refdef.fog_red = 0;
5404 r_refdef.fog_green = 0;
5405 r_refdef.fog_blue = 0;
5406 r_refdef.fog_alpha = 0;
5407 r_refdef.fog_start = 0;
5408 r_refdef.fog_end = 0;
5409 r_refdef.fog_height = 1<<30;
5410 r_refdef.fog_fadedepth = 128;
5415 r_refdef.fog_alpha = bound(0, r_refdef.fog_alpha, 1);
5416 r_refdef.fog_start = max(0, r_refdef.fog_start);
5417 r_refdef.fog_end = max(r_refdef.fog_start + 0.01, r_refdef.fog_end);
5419 if (r_refdef.fog_density && r_drawfog.integer)
5421 r_refdef.fogenabled = true;
5422 // this is the point where the fog reaches 0.9986 alpha, which we
5423 // consider a good enough cutoff point for the texture
5424 // (0.9986 * 256 == 255.6)
5425 if (r_fog_exp2.integer)
5426 r_refdef.fogrange = 32 / (r_refdef.fog_density * r_refdef.fog_density) + r_refdef.fog_start;
5428 r_refdef.fogrange = 2048 / r_refdef.fog_density + r_refdef.fog_start;
5429 r_refdef.fogrange = bound(r_refdef.fog_start, r_refdef.fogrange, r_refdef.fog_end);
5430 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
5431 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
5432 if (strcmp(r_refdef.fogheighttexturename, r_refdef.fog_height_texturename))
5433 R_BuildFogHeightTexture();
5434 // fog color was already set
5435 // update the fog texture
5436 if (r_refdef.fogmasktable_start != r_refdef.fog_start || r_refdef.fogmasktable_alpha != r_refdef.fog_alpha || r_refdef.fogmasktable_density != r_refdef.fog_density || r_refdef.fogmasktable_range != r_refdef.fogrange)
5437 R_BuildFogTexture();
5438 r_refdef.fog_height_texcoordscale = 1.0f / max(0.125f, r_refdef.fog_fadedepth);
5439 r_refdef.fog_height_tablescale = r_refdef.fog_height_tablesize * r_refdef.fog_height_texcoordscale;
5442 r_refdef.fogenabled = false;
5445 if (r_refdef.fog_density)
5447 r_refdef.fogcolor[0] = r_refdef.fog_red;
5448 r_refdef.fogcolor[1] = r_refdef.fog_green;
5449 r_refdef.fogcolor[2] = r_refdef.fog_blue;
5451 Vector4Set(r_refdef.fogplane, 0, 0, 1, -r_refdef.fog_height);
5452 r_refdef.fogplaneviewdist = DotProduct(r_refdef.fogplane, r_refdef.view.origin) + r_refdef.fogplane[3];
5453 r_refdef.fogplaneviewabove = r_refdef.fogplaneviewdist >= 0;
5454 r_refdef.fogheightfade = -0.5f/max(0.125f, r_refdef.fog_fadedepth);
5458 VectorCopy(r_refdef.fogcolor, fogvec);
5459 // color.rgb *= ContrastBoost * SceneBrightness;
5460 VectorScale(fogvec, r_refdef.view.colorscale, fogvec);
5461 r_refdef.fogcolor[0] = bound(0.0f, fogvec[0], 1.0f);
5462 r_refdef.fogcolor[1] = bound(0.0f, fogvec[1], 1.0f);
5463 r_refdef.fogcolor[2] = bound(0.0f, fogvec[2], 1.0f);
5468 void R_UpdateVariables(void)
5472 r_refdef.scene.ambientintensity = r_ambient.value * (1.0f / 64.0f);
5474 r_refdef.farclip = r_farclip_base.value;
5475 if (r_refdef.scene.worldmodel)
5476 r_refdef.farclip += r_refdef.scene.worldmodel->radius * r_farclip_world.value * 2;
5477 r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
5479 if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
5480 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
5481 r_refdef.polygonfactor = 0;
5482 r_refdef.polygonoffset = 0;
5484 r_refdef.scene.rtworld = r_shadow_realtime_world.integer != 0;
5485 r_refdef.scene.rtworldshadows = r_shadow_realtime_world_shadows.integer && vid.stencil;
5486 r_refdef.scene.rtdlight = r_shadow_realtime_dlight.integer != 0 && !gl_flashblend.integer && r_dynamic.integer;
5487 r_refdef.scene.rtdlightshadows = r_refdef.scene.rtdlight && r_shadow_realtime_dlight_shadows.integer && vid.stencil;
5488 r_refdef.scene.lightmapintensity = r_refdef.scene.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
5489 if (r_refdef.scene.worldmodel)
5491 r_refdef.scene.lightmapintensity *= r_refdef.scene.worldmodel->lightmapscale;
5493 if (r_showsurfaces.integer)
5495 r_refdef.scene.rtworld = false;
5496 r_refdef.scene.rtworldshadows = false;
5497 r_refdef.scene.rtdlight = false;
5498 r_refdef.scene.rtdlightshadows = false;
5499 r_refdef.scene.lightmapintensity = 0;
5502 r_gpuskeletal = false;
5503 switch(vid.renderpath)
5505 case RENDERPATH_GL32:
5506 r_gpuskeletal = r_glsl_skeletal.integer && !r_showsurfaces.integer;
5507 case RENDERPATH_GLES2:
5508 if(!vid_gammatables_trivial)
5510 if(!r_texture_gammaramps || vid_gammatables_serial != r_texture_gammaramps_serial)
5512 // build GLSL gamma texture
5513 #define RAMPWIDTH 256
5514 unsigned short ramp[RAMPWIDTH * 3];
5515 unsigned char rampbgr[RAMPWIDTH][4];
5518 r_texture_gammaramps_serial = vid_gammatables_serial;
5520 VID_BuildGammaTables(&ramp[0], RAMPWIDTH);
5521 for(i = 0; i < RAMPWIDTH; ++i)
5523 rampbgr[i][0] = (unsigned char) (ramp[i + 2 * RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
5524 rampbgr[i][1] = (unsigned char) (ramp[i + RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
5525 rampbgr[i][2] = (unsigned char) (ramp[i] * 255.0 / 65535.0 + 0.5);
5528 if (r_texture_gammaramps)
5530 R_UpdateTexture(r_texture_gammaramps, &rampbgr[0][0], 0, 0, 0, RAMPWIDTH, 1, 1, 0);
5534 r_texture_gammaramps = R_LoadTexture2D(r_main_texturepool, "gammaramps", RAMPWIDTH, 1, &rampbgr[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
5540 // remove GLSL gamma texture
5546 static r_refdef_scene_type_t r_currentscenetype = RST_CLIENT;
5547 static r_refdef_scene_t r_scenes_store[ RST_COUNT ];
5553 void R_SelectScene( r_refdef_scene_type_t scenetype ) {
5554 if( scenetype != r_currentscenetype ) {
5555 // store the old scenetype
5556 r_scenes_store[ r_currentscenetype ] = r_refdef.scene;
5557 r_currentscenetype = scenetype;
5558 // move in the new scene
5559 r_refdef.scene = r_scenes_store[ r_currentscenetype ];
5568 r_refdef_scene_t * R_GetScenePointer( r_refdef_scene_type_t scenetype )
5570 // of course, we could also add a qbool that provides a lock state and a ReleaseScenePointer function..
5571 if( scenetype == r_currentscenetype ) {
5572 return &r_refdef.scene;
5574 return &r_scenes_store[ scenetype ];
5578 static int R_SortEntities_Compare(const void *ap, const void *bp)
5580 const entity_render_t *a = *(const entity_render_t **)ap;
5581 const entity_render_t *b = *(const entity_render_t **)bp;
5584 if(a->model < b->model)
5586 if(a->model > b->model)
5590 // TODO possibly calculate the REAL skinnum here first using
5592 if(a->skinnum < b->skinnum)
5594 if(a->skinnum > b->skinnum)
5597 // everything we compared is equal
5600 static void R_SortEntities(void)
5602 // below or equal 2 ents, sorting never gains anything
5603 if(r_refdef.scene.numentities <= 2)
5606 qsort(r_refdef.scene.entities, r_refdef.scene.numentities, sizeof(*r_refdef.scene.entities), R_SortEntities_Compare);
5614 extern cvar_t r_shadow_bouncegrid;
5615 extern cvar_t v_isometric;
5616 extern void V_MakeViewIsometric(void);
5617 void R_RenderView(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, int x, int y, int width, int height)
5619 matrix4x4_t originalmatrix = r_refdef.view.matrix, offsetmatrix;
5621 rtexture_t *viewdepthtexture = NULL;
5622 rtexture_t *viewcolortexture = NULL;
5623 int viewx = r_refdef.view.x, viewy = r_refdef.view.y, viewwidth = r_refdef.view.width, viewheight = r_refdef.view.height;
5625 // finish any 2D rendering that was queued
5628 if (r_timereport_active)
5629 R_TimeReport("start");
5630 r_textureframe++; // used only by R_GetCurrentTexture
5631 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
5633 if(R_CompileShader_CheckStaticParms())
5634 R_GLSL_Restart_f(cmd_local);
5636 if (!r_drawentities.integer)
5637 r_refdef.scene.numentities = 0;
5638 else if (r_sortentities.integer)
5641 R_AnimCache_ClearCache();
5643 /* adjust for stereo display */
5644 if(R_Stereo_Active())
5646 Matrix4x4_CreateFromQuakeEntity(&offsetmatrix, 0, r_stereo_separation.value * (0.5f - r_stereo_side), 0, 0, r_stereo_angle.value * (0.5f - r_stereo_side), 0, 1);
5647 Matrix4x4_Concat(&r_refdef.view.matrix, &originalmatrix, &offsetmatrix);
5650 if (r_refdef.view.isoverlay)
5652 // TODO: FIXME: move this into its own backend function maybe? [2/5/2008 Andreas]
5653 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
5654 GL_Clear(GL_DEPTH_BUFFER_BIT, NULL, 1.0f, 0);
5655 R_TimeReport("depthclear");
5657 r_refdef.view.showdebug = false;
5659 r_fb.water.enabled = false;
5660 r_fb.water.numwaterplanes = 0;
5662 R_RenderScene(0, NULL, NULL, r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
5664 r_refdef.view.matrix = originalmatrix;
5670 if (!r_refdef.scene.entities || r_refdef.view.width * r_refdef.view.height == 0 || !r_renderview.integer || cl_videoplaying/* || !r_refdef.scene.worldmodel*/)
5672 r_refdef.view.matrix = originalmatrix;
5676 r_refdef.view.usevieworiginculling = !r_trippy.value && r_refdef.view.useperspective;
5677 if (v_isometric.integer && r_refdef.view.ismain)
5678 V_MakeViewIsometric();
5680 r_refdef.view.colorscale = r_hdr_scenebrightness.value * r_hdr_irisadaptation_value.value;
5682 if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D)
5683 // in sRGB fallback, behave similar to true sRGB: convert this
5684 // value from linear to sRGB
5685 r_refdef.view.colorscale = Image_sRGBFloatFromLinearFloat(r_refdef.view.colorscale);
5687 R_RenderView_UpdateViewVectors();
5689 R_Shadow_UpdateWorldLightSelection();
5691 // this will set up r_fb.rt_screen
5692 R_Bloom_StartFrame();
5694 // apply bloom brightness offset
5696 r_refdef.view.colorscale *= r_bloom_scenebrightness.value;
5698 // R_Bloom_StartFrame probably set up an fbo for us to render into, it will be rendered to the window later in R_BlendView
5701 viewfbo = r_fb.rt_screen->fbo;
5702 viewdepthtexture = r_fb.rt_screen->depthtexture;
5703 viewcolortexture = r_fb.rt_screen->colortexture[0];
5706 viewwidth = r_fb.rt_screen->texturewidth;
5707 viewheight = r_fb.rt_screen->textureheight;
5710 R_Water_StartFrame(viewwidth, viewheight);
5713 if (r_timereport_active)
5714 R_TimeReport("viewsetup");
5716 R_ResetViewRendering3D(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5718 // clear the whole fbo every frame - otherwise the driver will consider
5719 // it to be an inter-frame texture and stall in multi-gpu configurations
5721 GL_ScissorTest(false);
5722 R_ClearScreen(r_refdef.fogenabled);
5723 if (r_timereport_active)
5724 R_TimeReport("viewclear");
5726 r_refdef.view.clear = true;
5728 r_refdef.view.showdebug = true;
5731 if (r_timereport_active)
5732 R_TimeReport("visibility");
5734 R_AnimCache_CacheVisibleEntities();
5735 if (r_timereport_active)
5736 R_TimeReport("animcache");
5738 R_Shadow_UpdateBounceGridTexture();
5739 // R_Shadow_UpdateBounceGridTexture called R_TimeReport a few times internally, so we don't need to do that here.
5741 r_fb.water.numwaterplanes = 0;
5742 if (r_fb.water.enabled)
5743 R_RenderWaterPlanes(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5745 // for the actual view render we use scissoring a fair amount, so scissor
5746 // test needs to be on
5748 GL_ScissorTest(true);
5749 GL_Scissor(viewx, viewy, viewwidth, viewheight);
5750 R_RenderScene(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5751 r_fb.water.numwaterplanes = 0;
5753 // postprocess uses textures that are not aligned with the viewport we're rendering, so no scissoring
5754 GL_ScissorTest(false);
5756 R_BlendView(fbo, depthtexture, colortexture, x, y, width, height);
5757 if (r_timereport_active)
5758 R_TimeReport("blendview");
5760 r_refdef.view.matrix = originalmatrix;
5764 // go back to 2d rendering
5768 void R_RenderWaterPlanes(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5770 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawAddWaterPlanes)
5772 r_refdef.scene.worldmodel->DrawAddWaterPlanes(r_refdef.scene.worldentity);
5773 if (r_timereport_active)
5774 R_TimeReport("waterworld");
5777 // don't let sound skip if going slow
5778 if (r_refdef.scene.extraupdate)
5781 R_DrawModelsAddWaterPlanes();
5782 if (r_timereport_active)
5783 R_TimeReport("watermodels");
5785 if (r_fb.water.numwaterplanes)
5787 R_Water_ProcessPlanes(viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5788 if (r_timereport_active)
5789 R_TimeReport("waterscenes");
5793 extern cvar_t cl_locs_show;
5794 static void R_DrawLocs(void);
5795 static void R_DrawEntityBBoxes(prvm_prog_t *prog);
5796 static void R_DrawModelDecals(void);
5797 extern qbool r_shadow_usingdeferredprepass;
5798 extern int r_shadow_shadowmapatlas_modelshadows_size;
5799 void R_RenderScene(int viewfbo, rtexture_t *viewdepthtexture, rtexture_t *viewcolortexture, int viewx, int viewy, int viewwidth, int viewheight)
5801 qbool shadowmapping = false;
5803 if (r_timereport_active)
5804 R_TimeReport("beginscene");
5806 r_refdef.stats[r_stat_renders]++;
5810 // don't let sound skip if going slow
5811 if (r_refdef.scene.extraupdate)
5814 R_MeshQueue_BeginScene();
5818 Matrix4x4_CreateTranslate(&r_waterscrollmatrix, sin(r_refdef.scene.time) * 0.025 * r_waterscroll.value, sin(r_refdef.scene.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
5820 if (r_timereport_active)
5821 R_TimeReport("skystartframe");
5823 if (cl.csqc_vidvars.drawworld)
5825 // don't let sound skip if going slow
5826 if (r_refdef.scene.extraupdate)
5829 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawSky)
5831 r_refdef.scene.worldmodel->DrawSky(r_refdef.scene.worldentity);
5832 if (r_timereport_active)
5833 R_TimeReport("worldsky");
5836 if (R_DrawBrushModelsSky() && r_timereport_active)
5837 R_TimeReport("bmodelsky");
5839 if (skyrendermasked && skyrenderlater)
5841 // we have to force off the water clipping plane while rendering sky
5842 R_SetupView(false, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5844 R_SetupView(true, viewfbo, viewdepthtexture, viewcolortexture, viewx, viewy, viewwidth, viewheight);
5845 if (r_timereport_active)
5846 R_TimeReport("sky");
5850 // save the framebuffer info for R_Shadow_RenderMode_Reset during this view render
5851 r_shadow_viewfbo = viewfbo;
5852 r_shadow_viewdepthtexture = viewdepthtexture;
5853 r_shadow_viewcolortexture = viewcolortexture;
5854 r_shadow_viewx = viewx;
5855 r_shadow_viewy = viewy;
5856 r_shadow_viewwidth = viewwidth;
5857 r_shadow_viewheight = viewheight;
5859 R_Shadow_PrepareModelShadows();
5860 R_Shadow_PrepareLights();
5861 if (r_timereport_active)
5862 R_TimeReport("preparelights");
5864 // render all the shadowmaps that will be used for this view
5865 shadowmapping = R_Shadow_ShadowMappingEnabled();
5866 if (shadowmapping || r_shadow_shadowmapatlas_modelshadows_size)
5868 R_Shadow_DrawShadowMaps();
5869 if (r_timereport_active)
5870 R_TimeReport("shadowmaps");
5873 // render prepass deferred lighting if r_shadow_deferred is on, this produces light buffers that will be sampled in forward pass
5874 if (r_shadow_usingdeferredprepass)
5875 R_Shadow_DrawPrepass();
5877 // now we begin the forward pass of the view render
5878 if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawDepth)
5880 r_refdef.scene.worldmodel->DrawDepth(r_refdef.scene.worldentity);
5881 if (r_timereport_active)
5882 R_TimeReport("worlddepth");
5884 if (r_depthfirst.integer >= 2)
5886 R_DrawModelsDepth();
5887 if (r_timereport_active)
5888 R_TimeReport("modeldepth");
5891 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->Draw)
5893 r_refdef.scene.worldmodel->Draw(r_refdef.scene.worldentity);
5894 if (r_timereport_active)
5895 R_TimeReport("world");
5898 // don't let sound skip if going slow
5899 if (r_refdef.scene.extraupdate)
5903 if (r_timereport_active)
5904 R_TimeReport("models");
5906 // don't let sound skip if going slow
5907 if (r_refdef.scene.extraupdate)
5910 if (!r_shadow_usingdeferredprepass)
5912 R_Shadow_DrawLights();
5913 if (r_timereport_active)
5914 R_TimeReport("rtlights");
5917 // don't let sound skip if going slow
5918 if (r_refdef.scene.extraupdate)
5921 if (cl.csqc_vidvars.drawworld)
5923 R_DrawModelDecals();
5924 if (r_timereport_active)
5925 R_TimeReport("modeldecals");
5928 if (r_timereport_active)
5929 R_TimeReport("particles");
5932 if (r_timereport_active)
5933 R_TimeReport("explosions");
5936 if (r_refdef.view.showdebug)
5938 if (cl_locs_show.integer)
5941 if (r_timereport_active)
5942 R_TimeReport("showlocs");
5945 if (r_drawportals.integer)
5948 if (r_timereport_active)
5949 R_TimeReport("portals");
5952 if (r_showbboxes_client.value > 0)
5954 R_DrawEntityBBoxes(CLVM_prog);
5955 if (r_timereport_active)
5956 R_TimeReport("clbboxes");
5958 if (r_showbboxes.value > 0)
5960 R_DrawEntityBBoxes(SVVM_prog);
5961 if (r_timereport_active)
5962 R_TimeReport("svbboxes");
5966 if (r_transparent.integer)
5968 R_MeshQueue_RenderTransparent();
5969 if (r_timereport_active)
5970 R_TimeReport("drawtrans");
5973 if (r_refdef.view.showdebug && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawDebug && (r_showtris.value > 0 || r_shownormals.value != 0 || r_showcollisionbrushes.value > 0 || r_showoverdraw.value > 0))
5975 r_refdef.scene.worldmodel->DrawDebug(r_refdef.scene.worldentity);
5976 if (r_timereport_active)
5977 R_TimeReport("worlddebug");
5978 R_DrawModelsDebug();
5979 if (r_timereport_active)
5980 R_TimeReport("modeldebug");
5983 if (cl.csqc_vidvars.drawworld)
5985 R_Shadow_DrawCoronas();
5986 if (r_timereport_active)
5987 R_TimeReport("coronas");
5990 // don't let sound skip if going slow
5991 if (r_refdef.scene.extraupdate)
5995 static const unsigned short bboxelements[36] =
6005 #define BBOXEDGES 13
6006 static const float bboxedges[BBOXEDGES][6] =
6009 { 0, 0, 0, 1, 1, 1 },
6011 { 0, 0, 0, 0, 1, 0 },
6012 { 0, 0, 0, 1, 0, 0 },
6013 { 0, 1, 0, 1, 1, 0 },
6014 { 1, 0, 0, 1, 1, 0 },
6016 { 0, 0, 1, 0, 1, 1 },
6017 { 0, 0, 1, 1, 0, 1 },
6018 { 0, 1, 1, 1, 1, 1 },
6019 { 1, 0, 1, 1, 1, 1 },
6021 { 0, 0, 0, 0, 0, 1 },
6022 { 1, 0, 0, 1, 0, 1 },
6023 { 0, 1, 0, 0, 1, 1 },
6024 { 1, 1, 0, 1, 1, 1 },
6027 static void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
6029 int numvertices = BBOXEDGES * 8;
6030 float vertex3f[BBOXEDGES * 8 * 3], color4f[BBOXEDGES * 8 * 4];
6031 int numtriangles = BBOXEDGES * 12;
6032 unsigned short elements[BBOXEDGES * 36];
6034 float *v, *c, f1, f2, edgemins[3], edgemaxs[3];
6036 RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false);
6038 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6039 GL_DepthMask(false);
6040 GL_DepthRange(0, 1);
6041 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6043 for (edge = 0; edge < BBOXEDGES; edge++)
6045 for (i = 0; i < 3; i++)
6047 edgemins[i] = mins[i] + (maxs[i] - mins[i]) * bboxedges[edge][i] - 0.25f;
6048 edgemaxs[i] = mins[i] + (maxs[i] - mins[i]) * bboxedges[edge][3 + i] + 0.25f;
6050 vertex3f[edge * 24 + 0] = edgemins[0]; vertex3f[edge * 24 + 1] = edgemins[1]; vertex3f[edge * 24 + 2] = edgemins[2];
6051 vertex3f[edge * 24 + 3] = edgemaxs[0]; vertex3f[edge * 24 + 4] = edgemins[1]; vertex3f[edge * 24 + 5] = edgemins[2];
6052 vertex3f[edge * 24 + 6] = edgemins[0]; vertex3f[edge * 24 + 7] = edgemaxs[1]; vertex3f[edge * 24 + 8] = edgemins[2];
6053 vertex3f[edge * 24 + 9] = edgemaxs[0]; vertex3f[edge * 24 + 10] = edgemaxs[1]; vertex3f[edge * 24 + 11] = edgemins[2];
6054 vertex3f[edge * 24 + 12] = edgemins[0]; vertex3f[edge * 24 + 13] = edgemins[1]; vertex3f[edge * 24 + 14] = edgemaxs[2];
6055 vertex3f[edge * 24 + 15] = edgemaxs[0]; vertex3f[edge * 24 + 16] = edgemins[1]; vertex3f[edge * 24 + 17] = edgemaxs[2];
6056 vertex3f[edge * 24 + 18] = edgemins[0]; vertex3f[edge * 24 + 19] = edgemaxs[1]; vertex3f[edge * 24 + 20] = edgemaxs[2];
6057 vertex3f[edge * 24 + 21] = edgemaxs[0]; vertex3f[edge * 24 + 22] = edgemaxs[1]; vertex3f[edge * 24 + 23] = edgemaxs[2];
6058 for (i = 0; i < 36; i++)
6059 elements[edge * 36 + i] = edge * 8 + bboxelements[i];
6061 R_FillColors(color4f, numvertices, cr, cg, cb, ca);
6062 if (r_refdef.fogenabled)
6064 for (i = 0, v = vertex3f, c = color4f; i < numvertices; i++, v += 3, c += 4)
6066 f1 = RSurf_FogVertex(v);
6068 c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
6069 c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
6070 c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
6073 R_Mesh_PrepareVertices_Generic_Arrays(numvertices, vertex3f, color4f, NULL);
6074 R_Mesh_ResetTextureState();
6075 R_SetupShader_Generic_NoTexture(false, false);
6076 R_Mesh_Draw(0, numvertices, 0, numtriangles, NULL, NULL, 0, elements, NULL, 0);
6079 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6081 // hacky overloading of the parameters
6082 prvm_prog_t *prog = (prvm_prog_t *)rtlight;
6085 prvm_edict_t *edict;
6087 GL_CullFace(GL_NONE);
6088 R_SetupShader_Generic_NoTexture(false, false);
6090 for (i = 0;i < numsurfaces;i++)
6092 edict = PRVM_EDICT_NUM(surfacelist[i]);
6093 switch ((int)PRVM_serveredictfloat(edict, solid))
6095 case SOLID_NOT: Vector4Set(color, 1, 1, 1, 0.05);break;
6096 case SOLID_TRIGGER: Vector4Set(color, 1, 0, 1, 0.10);break;
6097 case SOLID_BBOX: Vector4Set(color, 0, 1, 0, 0.10);break;
6098 case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
6099 case SOLID_BSP: Vector4Set(color, 0, 0, 1, 0.05);break;
6100 case SOLID_CORPSE: Vector4Set(color, 1, 0.5, 0, 0.05);break;
6101 default: Vector4Set(color, 0, 0, 0, 0.50);break;
6103 if (prog == CLVM_prog)
6104 color[3] *= r_showbboxes_client.value;
6106 color[3] *= r_showbboxes.value;
6107 color[3] = bound(0, color[3], 1);
6108 GL_DepthTest(!r_showdisabledepthtest.integer);
6109 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
6113 static void R_DrawEntityBBoxes(prvm_prog_t *prog)
6116 prvm_edict_t *edict;
6122 for (i = 0; i < prog->num_edicts; i++)
6124 edict = PRVM_EDICT_NUM(i);
6127 // exclude the following for now, as they don't live in world coordinate space and can't be solid:
6128 if (PRVM_gameedictedict(edict, tag_entity) != 0)
6130 if (prog == SVVM_prog && PRVM_serveredictedict(edict, viewmodelforclient) != 0)
6132 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
6133 R_MeshQueue_AddTransparent(TRANSPARENTSORT_DISTANCE, center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)prog);
6137 static const int nomodelelement3i[24] =
6149 static const unsigned short nomodelelement3s[24] =
6161 static const float nomodelvertex3f[6*3] =
6171 static const float nomodelcolor4f[6*4] =
6173 0.0f, 0.0f, 0.5f, 1.0f,
6174 0.0f, 0.0f, 0.5f, 1.0f,
6175 0.0f, 0.5f, 0.0f, 1.0f,
6176 0.0f, 0.5f, 0.0f, 1.0f,
6177 0.5f, 0.0f, 0.0f, 1.0f,
6178 0.5f, 0.0f, 0.0f, 1.0f
6181 static void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6187 RSurf_ActiveCustomEntity(&ent->matrix, &ent->inversematrix, ent->flags, ent->shadertime, ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha, 6, nomodelvertex3f, NULL, NULL, NULL, NULL, nomodelcolor4f, 8, nomodelelement3i, nomodelelement3s, false, false);
6189 // this is only called once per entity so numsurfaces is always 1, and
6190 // surfacelist is always {0}, so this code does not handle batches
6192 if (rsurface.ent_flags & RENDER_ADDITIVE)
6194 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
6195 GL_DepthMask(false);
6197 else if (ent->alpha < 1)
6199 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6200 GL_DepthMask(false);
6204 GL_BlendFunc(GL_ONE, GL_ZERO);
6207 GL_DepthRange(0, (rsurface.ent_flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
6208 GL_PolygonOffset(rsurface.basepolygonfactor, rsurface.basepolygonoffset);
6209 GL_DepthTest(!(rsurface.ent_flags & RENDER_NODEPTHTEST));
6210 GL_CullFace((rsurface.ent_flags & RENDER_DOUBLESIDED) ? GL_NONE : r_refdef.view.cullface_back);
6211 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
6212 for (i = 0, c = color4f;i < 6;i++, c += 4)
6214 c[0] *= ent->render_fullbright[0] * r_refdef.view.colorscale;
6215 c[1] *= ent->render_fullbright[1] * r_refdef.view.colorscale;
6216 c[2] *= ent->render_fullbright[2] * r_refdef.view.colorscale;
6219 if (r_refdef.fogenabled)
6221 for (i = 0, c = color4f;i < 6;i++, c += 4)
6223 f1 = RSurf_FogVertex(nomodelvertex3f + 3*i);
6225 c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
6226 c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
6227 c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
6230 // R_Mesh_ResetTextureState();
6231 R_SetupShader_Generic_NoTexture(false, false);
6232 R_Mesh_PrepareVertices_Generic_Arrays(6, nomodelvertex3f, color4f, NULL);
6233 R_Mesh_Draw(0, 6, 0, 8, nomodelelement3i, NULL, 0, nomodelelement3s, NULL, 0);
6236 void R_DrawNoModel(entity_render_t *ent)
6239 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
6240 if ((ent->flags & RENDER_ADDITIVE) || (ent->alpha < 1))
6241 R_MeshQueue_AddTransparent((ent->flags & RENDER_NODEPTHTEST) ? TRANSPARENTSORT_HUD : TRANSPARENTSORT_DISTANCE, org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
6243 R_DrawNoModel_TransparentCallback(ent, rsurface.rtlight, 0, NULL);
6246 void R_CalcBeam_Vertex3f (float *vert, const float *org1, const float *org2, float width)
6248 vec3_t right1, right2, diff, normal;
6250 VectorSubtract (org2, org1, normal);
6252 // calculate 'right' vector for start
6253 VectorSubtract (r_refdef.view.origin, org1, diff);
6254 CrossProduct (normal, diff, right1);
6255 VectorNormalize (right1);
6257 // calculate 'right' vector for end
6258 VectorSubtract (r_refdef.view.origin, org2, diff);
6259 CrossProduct (normal, diff, right2);
6260 VectorNormalize (right2);
6262 vert[ 0] = org1[0] + width * right1[0];
6263 vert[ 1] = org1[1] + width * right1[1];
6264 vert[ 2] = org1[2] + width * right1[2];
6265 vert[ 3] = org1[0] - width * right1[0];
6266 vert[ 4] = org1[1] - width * right1[1];
6267 vert[ 5] = org1[2] - width * right1[2];
6268 vert[ 6] = org2[0] - width * right2[0];
6269 vert[ 7] = org2[1] - width * right2[1];
6270 vert[ 8] = org2[2] - width * right2[2];
6271 vert[ 9] = org2[0] + width * right2[0];
6272 vert[10] = org2[1] + width * right2[1];
6273 vert[11] = org2[2] + width * right2[2];
6276 void R_CalcSprite_Vertex3f(float *vertex3f, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2)
6278 vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
6279 vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
6280 vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
6281 vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
6282 vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
6283 vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
6284 vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
6285 vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
6286 vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
6287 vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
6288 vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
6289 vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
6292 static int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
6297 VectorSet(v, x, y, z);
6298 for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
6299 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
6301 if (i == mesh->numvertices)
6303 if (mesh->numvertices < mesh->maxvertices)
6305 VectorCopy(v, vertex3f);
6306 mesh->numvertices++;
6308 return mesh->numvertices;
6314 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
6318 element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
6319 element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
6320 e = mesh->element3i + mesh->numtriangles * 3;
6321 for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
6323 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
6324 if (mesh->numtriangles < mesh->maxtriangles)
6329 mesh->numtriangles++;
6331 element[1] = element[2];
6335 static void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
6339 element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
6340 element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
6341 e = mesh->element3i + mesh->numtriangles * 3;
6342 for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
6344 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
6345 if (mesh->numtriangles < mesh->maxtriangles)
6350 mesh->numtriangles++;
6352 element[1] = element[2];
6356 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
6357 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
6359 int planenum, planenum2;
6362 mplane_t *plane, *plane2;
6364 double temppoints[2][256*3];
6365 // figure out how large a bounding box we need to properly compute this brush
6367 for (w = 0;w < numplanes;w++)
6368 maxdist = max(maxdist, fabs(planes[w].dist));
6369 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
6370 maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
6371 for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
6375 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
6376 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
6378 if (planenum2 == planenum)
6380 PolygonD_Divide(tempnumpoints, temppoints[w], plane2->normal[0], plane2->normal[1], plane2->normal[2], plane2->dist, R_MESH_PLANE_DIST_EPSILON, 0, NULL, NULL, 256, temppoints[!w], &tempnumpoints, NULL);
6383 if (tempnumpoints < 3)
6385 // generate elements forming a triangle fan for this polygon
6386 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
6390 static qbool R_TestQ3WaveFunc(q3wavefunc_t func, const float *parms)
6392 if(parms[0] == 0 && parms[1] == 0)
6394 if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
6395 if(rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)] == 0)
6400 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
6403 index = parms[2] + rsurface.shadertime * parms[3];
6404 index -= floor(index);
6405 switch (func & ((1 << Q3WAVEFUNC_USER_SHIFT) - 1))
6408 case Q3WAVEFUNC_NONE:
6409 case Q3WAVEFUNC_NOISE:
6410 case Q3WAVEFUNC_COUNT:
6413 case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
6414 case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
6415 case Q3WAVEFUNC_SAWTOOTH: f = index;break;
6416 case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
6417 case Q3WAVEFUNC_TRIANGLE:
6419 f = index - floor(index);
6432 f = parms[0] + parms[1] * f;
6433 if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
6434 f *= rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)];
6438 static void R_tcMod_ApplyToMatrix(matrix4x4_t *texmatrix, q3shaderinfo_layer_tcmod_t *tcmod, int currentmaterialflags)
6445 matrix4x4_t matrix, temp;
6446 // if shadertime exceeds about 9 hours (32768 seconds), just wrap it,
6447 // it's better to have one huge fixup every 9 hours than gradual
6448 // degradation over time which looks consistently bad after many hours.
6450 // tcmod scroll in particular suffers from this degradation which can't be
6451 // effectively worked around even with floor() tricks because we don't
6452 // know if tcmod scroll is the last tcmod being applied, and for clampmap
6453 // a workaround involving floor() would be incorrect anyway...
6454 shadertime = rsurface.shadertime;
6455 if (shadertime >= 32768.0f)
6456 shadertime -= floor(rsurface.shadertime * (1.0f / 32768.0f)) * 32768.0f;
6457 switch(tcmod->tcmod)
6461 if (currentmaterialflags & MATERIALFLAG_WATERSCROLL)
6462 matrix = r_waterscrollmatrix;
6464 matrix = identitymatrix;
6466 case Q3TCMOD_ENTITYTRANSLATE:
6467 // this is used in Q3 to allow the gamecode to control texcoord
6468 // scrolling on the entity, which is not supported in darkplaces yet.
6469 Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
6471 case Q3TCMOD_ROTATE:
6472 Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
6473 Matrix4x4_ConcatRotate(&matrix, tcmod->parms[0] * rsurface.shadertime, 0, 0, 1);
6474 Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
6477 Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
6479 case Q3TCMOD_SCROLL:
6480 // this particular tcmod is a "bug for bug" compatible one with regards to
6481 // Quake3, the wrapping is unnecessary with our shadetime fix but quake3
6482 // specifically did the wrapping and so we must mimic that...
6483 offsetd[0] = tcmod->parms[0] * rsurface.shadertime;
6484 offsetd[1] = tcmod->parms[1] * rsurface.shadertime;
6485 Matrix4x4_CreateTranslate(&matrix, offsetd[0] - floor(offsetd[0]), offsetd[1] - floor(offsetd[1]), 0);
6487 case Q3TCMOD_PAGE: // poor man's animmap (to store animations into a single file, useful for HTTP downloaded textures)
6488 w = (int) tcmod->parms[0];
6489 h = (int) tcmod->parms[1];
6490 f = rsurface.shadertime / (tcmod->parms[2] * w * h);
6492 idx = (int) floor(f * w * h);
6493 Matrix4x4_CreateTranslate(&matrix, (idx % w) / tcmod->parms[0], (idx / w) / tcmod->parms[1], 0);
6495 case Q3TCMOD_STRETCH:
6496 f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
6497 Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
6499 case Q3TCMOD_TRANSFORM:
6500 VectorSet(tcmat + 0, tcmod->parms[0], tcmod->parms[1], 0);
6501 VectorSet(tcmat + 3, tcmod->parms[2], tcmod->parms[3], 0);
6502 VectorSet(tcmat + 6, 0 , 0 , 1);
6503 VectorSet(tcmat + 9, tcmod->parms[4], tcmod->parms[5], 0);
6504 Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
6506 case Q3TCMOD_TURBULENT:
6507 // this is handled in the RSurf_PrepareVertices function
6508 matrix = identitymatrix;
6512 Matrix4x4_Concat(texmatrix, &matrix, &temp);
6515 static void R_LoadQWSkin(r_qwskincache_t *cache, const char *skinname)
6517 int textureflags = (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_PICMIP;
6518 char name[MAX_QPATH];
6519 skinframe_t *skinframe;
6520 unsigned char pixels[296*194];
6521 strlcpy(cache->name, skinname, sizeof(cache->name));
6522 dpsnprintf(name, sizeof(name), "skins/%s.pcx", cache->name);
6523 if (developer_loading.integer)
6524 Con_Printf("loading %s\n", name);
6525 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
6526 if (!skinframe || !skinframe->base)
6529 fs_offset_t filesize;
6531 f = FS_LoadFile(name, tempmempool, true, &filesize);
6534 if (LoadPCX_QWSkin(f, (int)filesize, pixels, 296, 194))
6535 skinframe = R_SkinFrame_LoadInternalQuake(name, textureflags, true, r_fullbrights.integer, pixels, image_width, image_height);
6539 cache->skinframe = skinframe;
6542 texture_t *R_GetCurrentTexture(texture_t *t)
6545 const entity_render_t *ent = rsurface.entity;
6546 model_t *model = ent->model; // when calling this, ent must not be NULL
6547 q3shaderinfo_layer_tcmod_t *tcmod;
6548 float specularscale = 0.0f;
6550 if (t->update_lastrenderframe == r_textureframe && t->update_lastrenderentity == (void *)ent && !rsurface.forcecurrenttextureupdate)
6551 return t->currentframe;
6552 t->update_lastrenderframe = r_textureframe;
6553 t->update_lastrenderentity = (void *)ent;
6555 if(ent->entitynumber >= MAX_EDICTS && ent->entitynumber < 2 * MAX_EDICTS)
6556 t->camera_entity = ent->entitynumber;
6558 t->camera_entity = 0;
6560 // switch to an alternate material if this is a q1bsp animated material
6562 texture_t *texture = t;
6563 int s = rsurface.ent_skinnum;
6564 if ((unsigned int)s >= (unsigned int)model->numskins)
6566 if (model->skinscenes)
6568 if (model->skinscenes[s].framecount > 1)
6569 s = model->skinscenes[s].firstframe + (unsigned int) (rsurface.shadertime * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
6571 s = model->skinscenes[s].firstframe;
6574 t = t + s * model->num_surfaces;
6577 // use an alternate animation if the entity's frame is not 0,
6578 // and only if the texture has an alternate animation
6579 if (t->animated == 2) // q2bsp
6580 t = t->anim_frames[0][ent->framegroupblend[0].frame % t->anim_total[0]];
6581 else if (rsurface.ent_alttextures && t->anim_total[1])
6582 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[1]) : 0];
6584 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[0]) : 0];
6586 texture->currentframe = t;
6589 // update currentskinframe to be a qw skin or animation frame
6590 if (rsurface.ent_qwskin >= 0)
6592 i = rsurface.ent_qwskin;
6593 if (!r_qwskincache || r_qwskincache_size != cl.maxclients)
6595 r_qwskincache_size = cl.maxclients;
6597 Mem_Free(r_qwskincache);
6598 r_qwskincache = (r_qwskincache_t *)Mem_Alloc(r_main_mempool, sizeof(*r_qwskincache) * r_qwskincache_size);
6600 if (strcmp(r_qwskincache[i].name, cl.scores[i].qw_skin))
6601 R_LoadQWSkin(&r_qwskincache[i], cl.scores[i].qw_skin);
6602 t->currentskinframe = r_qwskincache[i].skinframe;
6603 if (t->materialshaderpass && t->currentskinframe == NULL)
6604 t->currentskinframe = t->materialshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->materialshaderpass->framerate, t->materialshaderpass->numframes)];
6606 else if (t->materialshaderpass && t->materialshaderpass->numframes >= 2)
6607 t->currentskinframe = t->materialshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->materialshaderpass->framerate, t->materialshaderpass->numframes)];
6608 if (t->backgroundshaderpass && t->backgroundshaderpass->numframes >= 2)
6609 t->backgroundcurrentskinframe = t->backgroundshaderpass->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->backgroundshaderpass->framerate, t->backgroundshaderpass->numframes)];
6611 t->currentmaterialflags = t->basematerialflags;
6612 t->currentalpha = rsurface.entity->alpha * t->basealpha;
6613 if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_water.integer || r_novis.integer || r_trippy.integer))
6614 t->currentalpha *= r_wateralpha.value;
6615 if(t->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay)
6616 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; // we apply wateralpha later
6617 if(!r_fb.water.enabled || r_refdef.view.isoverlay)
6618 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA);
6620 // decide on which type of lighting to use for this surface
6621 if (rsurface.entity->render_modellight_forced)
6622 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
6623 if (rsurface.entity->render_rtlight_disabled)
6624 t->currentmaterialflags |= MATERIALFLAG_NORTLIGHT;
6625 if (rsurface.entity->render_lightgrid)
6626 t->currentmaterialflags |= MATERIALFLAG_LIGHTGRID;
6627 if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND && !(R_BlendFuncFlags(t->customblendfunc[0], t->customblendfunc[1]) & BLENDFUNC_ALLOWS_COLORMOD))
6629 // some CUSTOMBLEND blendfuncs are too weird, we have to ignore colormod and view colorscale
6630 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_NORTLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6631 for (q = 0; q < 3; q++)
6633 t->render_glowmod[q] = rsurface.entity->glowmod[q];
6634 t->render_modellight_lightdir_world[q] = q == 2;
6635 t->render_modellight_lightdir_local[q] = q == 2;
6636 t->render_modellight_ambient[q] = 1;
6637 t->render_modellight_diffuse[q] = 0;
6638 t->render_modellight_specular[q] = 0;
6639 t->render_lightmap_ambient[q] = 0;
6640 t->render_lightmap_diffuse[q] = 0;
6641 t->render_lightmap_specular[q] = 0;
6642 t->render_rtlight_diffuse[q] = 0;
6643 t->render_rtlight_specular[q] = 0;
6646 else if ((t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) || !(rsurface.ent_flags & RENDER_LIGHT))
6648 // fullbright is basically MATERIALFLAG_MODELLIGHT but with ambient locked to 1,1,1 and no shading
6649 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_NORTLIGHT | MATERIALFLAG_MODELLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6650 for (q = 0; q < 3; q++)
6652 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6653 t->render_modellight_ambient[q] = rsurface.entity->render_fullbright[q] * r_refdef.view.colorscale;
6654 t->render_modellight_lightdir_world[q] = q == 2;
6655 t->render_modellight_lightdir_local[q] = q == 2;
6656 t->render_modellight_diffuse[q] = 0;
6657 t->render_modellight_specular[q] = 0;
6658 t->render_lightmap_ambient[q] = 0;
6659 t->render_lightmap_diffuse[q] = 0;
6660 t->render_lightmap_specular[q] = 0;
6661 t->render_rtlight_diffuse[q] = 0;
6662 t->render_rtlight_specular[q] = 0;
6665 else if (t->currentmaterialflags & MATERIALFLAG_LIGHTGRID)
6667 t->currentmaterialflags &= ~MATERIALFLAG_MODELLIGHT;
6668 for (q = 0; q < 3; q++)
6670 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6671 t->render_modellight_lightdir_world[q] = q == 2;
6672 t->render_modellight_lightdir_local[q] = q == 2;
6673 t->render_modellight_ambient[q] = 0;
6674 t->render_modellight_diffuse[q] = 0;
6675 t->render_modellight_specular[q] = 0;
6676 t->render_lightmap_ambient[q] = rsurface.entity->render_lightmap_ambient[q] * r_refdef.view.colorscale;
6677 t->render_lightmap_diffuse[q] = rsurface.entity->render_lightmap_diffuse[q] * 2 * r_refdef.view.colorscale;
6678 t->render_lightmap_specular[q] = rsurface.entity->render_lightmap_specular[q] * 2 * r_refdef.view.colorscale;
6679 t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6680 t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6683 else if ((rsurface.ent_flags & (RENDER_DYNAMICMODELLIGHT | RENDER_CUSTOMIZEDMODELLIGHT)) || rsurface.modeltexcoordlightmap2f == NULL)
6685 // ambient + single direction light (modellight)
6686 t->currentmaterialflags = (t->currentmaterialflags | MATERIALFLAG_MODELLIGHT) & ~MATERIALFLAG_LIGHTGRID;
6687 for (q = 0; q < 3; q++)
6689 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6690 t->render_modellight_lightdir_world[q] = rsurface.entity->render_modellight_lightdir_world[q];
6691 t->render_modellight_lightdir_local[q] = rsurface.entity->render_modellight_lightdir_local[q];
6692 t->render_modellight_ambient[q] = rsurface.entity->render_modellight_ambient[q] * r_refdef.view.colorscale;
6693 t->render_modellight_diffuse[q] = rsurface.entity->render_modellight_diffuse[q] * r_refdef.view.colorscale;
6694 t->render_modellight_specular[q] = rsurface.entity->render_modellight_specular[q] * r_refdef.view.colorscale;
6695 t->render_lightmap_ambient[q] = 0;
6696 t->render_lightmap_diffuse[q] = 0;
6697 t->render_lightmap_specular[q] = 0;
6698 t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6699 t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6704 // lightmap - 2x diffuse and specular brightness because bsp files have 0-2 colors as 0-1
6705 for (q = 0; q < 3; q++)
6707 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6708 t->render_modellight_lightdir_world[q] = q == 2;
6709 t->render_modellight_lightdir_local[q] = q == 2;
6710 t->render_modellight_ambient[q] = 0;
6711 t->render_modellight_diffuse[q] = 0;
6712 t->render_modellight_specular[q] = 0;
6713 t->render_lightmap_ambient[q] = rsurface.entity->render_lightmap_ambient[q] * r_refdef.view.colorscale;
6714 t->render_lightmap_diffuse[q] = rsurface.entity->render_lightmap_diffuse[q] * 2 * r_refdef.view.colorscale;
6715 t->render_lightmap_specular[q] = rsurface.entity->render_lightmap_specular[q] * 2 * r_refdef.view.colorscale;
6716 t->render_rtlight_diffuse[q] = rsurface.entity->render_rtlight_diffuse[q] * r_refdef.view.colorscale;
6717 t->render_rtlight_specular[q] = rsurface.entity->render_rtlight_specular[q] * r_refdef.view.colorscale;
6721 if (t->currentmaterialflags & MATERIALFLAG_VERTEXCOLOR)
6723 // since MATERIALFLAG_VERTEXCOLOR uses the lightmapcolor4f vertex
6724 // attribute, we punt it to the lightmap path and hope for the best,
6725 // but lighting doesn't work.
6727 // FIXME: this is fine for effects but CSQC polygons should be subject
6729 t->currentmaterialflags &= ~(MATERIALFLAG_MODELLIGHT | MATERIALFLAG_LIGHTGRID);
6730 for (q = 0; q < 3; q++)
6732 t->render_glowmod[q] = rsurface.entity->render_glowmod[q] * r_refdef.view.colorscale;
6733 t->render_modellight_lightdir_world[q] = q == 2;
6734 t->render_modellight_lightdir_local[q] = q == 2;
6735 t->render_modellight_ambient[q] = 0;
6736 t->render_modellight_diffuse[q] = 0;
6737 t->render_modellight_specular[q] = 0;
6738 t->render_lightmap_ambient[q] = 0;
6739 t->render_lightmap_diffuse[q] = rsurface.entity->render_fullbright[q] * r_refdef.view.colorscale;
6740 t->render_lightmap_specular[q] = 0;
6741 t->render_rtlight_diffuse[q] = 0;
6742 t->render_rtlight_specular[q] = 0;
6746 for (q = 0; q < 3; q++)
6748 t->render_colormap_pants[q] = rsurface.entity->colormap_pantscolor[q];
6749 t->render_colormap_shirt[q] = rsurface.entity->colormap_shirtcolor[q];
6752 if (rsurface.ent_flags & RENDER_ADDITIVE)
6753 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
6754 else if (t->currentalpha < 1)
6755 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
6756 // LadyHavoc: prevent bugs where code checks add or alpha at higher priority than customblend by clearing these flags
6757 if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
6758 t->currentmaterialflags &= ~(MATERIALFLAG_ADD | MATERIALFLAG_ALPHA);
6759 if (rsurface.ent_flags & RENDER_DOUBLESIDED)
6760 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
6761 if (rsurface.ent_flags & (RENDER_NODEPTHTEST | RENDER_VIEWMODEL))
6762 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
6763 if (t->backgroundshaderpass)
6764 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
6765 if (t->currentmaterialflags & MATERIALFLAG_BLENDED)
6767 if (t->currentmaterialflags & (MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA))
6768 t->currentmaterialflags &= ~MATERIALFLAG_BLENDED;
6771 t->currentmaterialflags &= ~(MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA);
6772 if (vid.allowalphatocoverage && r_transparent_alphatocoverage.integer >= 2 && ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA | MATERIALFLAG_ADD | MATERIALFLAG_CUSTOMBLEND)) == (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)))
6774 // promote alphablend to alphatocoverage (a type of alphatest) if antialiasing is on
6775 t->currentmaterialflags = (t->currentmaterialflags & ~(MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)) | MATERIALFLAG_ALPHATEST;
6777 if ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST)) == MATERIALFLAG_BLENDED && r_transparentdepthmasking.integer && !(t->basematerialflags & MATERIALFLAG_BLENDED))
6778 t->currentmaterialflags |= MATERIALFLAG_TRANSDEPTH;
6780 // there is no tcmod
6781 if (t->currentmaterialflags & MATERIALFLAG_WATERSCROLL)
6783 t->currenttexmatrix = r_waterscrollmatrix;
6784 t->currentbackgroundtexmatrix = r_waterscrollmatrix;
6786 else if (!(t->currentmaterialflags & MATERIALFLAG_CUSTOMSURFACE))
6788 Matrix4x4_CreateIdentity(&t->currenttexmatrix);
6789 Matrix4x4_CreateIdentity(&t->currentbackgroundtexmatrix);
6792 if (t->materialshaderpass)
6793 for (i = 0, tcmod = t->materialshaderpass->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
6794 R_tcMod_ApplyToMatrix(&t->currenttexmatrix, tcmod, t->currentmaterialflags);
6796 t->colormapping = VectorLength2(t->render_colormap_pants) + VectorLength2(t->render_colormap_shirt) >= (1.0f / 1048576.0f);
6797 if (t->currentskinframe->qpixels)
6798 R_SkinFrame_GenerateTexturesFromQPixels(t->currentskinframe, t->colormapping);
6799 t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
6800 if (!t->basetexture)
6801 t->basetexture = r_texture_notexture;
6802 t->pantstexture = t->colormapping ? t->currentskinframe->pants : NULL;
6803 t->shirttexture = t->colormapping ? t->currentskinframe->shirt : NULL;
6804 t->nmaptexture = t->currentskinframe->nmap;
6805 if (!t->nmaptexture)
6806 t->nmaptexture = r_texture_blanknormalmap;
6807 t->glosstexture = r_texture_black;
6808 t->glowtexture = t->currentskinframe->glow;
6809 t->fogtexture = t->currentskinframe->fog;
6810 t->reflectmasktexture = t->currentskinframe->reflect;
6811 if (t->backgroundshaderpass)
6813 for (i = 0, tcmod = t->backgroundshaderpass->tcmods; i < Q3MAXTCMODS && tcmod->tcmod; i++, tcmod++)
6814 R_tcMod_ApplyToMatrix(&t->currentbackgroundtexmatrix, tcmod, t->currentmaterialflags);
6815 t->backgroundbasetexture = (!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base;
6816 t->backgroundnmaptexture = t->backgroundcurrentskinframe->nmap;
6817 t->backgroundglosstexture = r_texture_black;
6818 t->backgroundglowtexture = t->backgroundcurrentskinframe->glow;
6819 if (!t->backgroundnmaptexture)
6820 t->backgroundnmaptexture = r_texture_blanknormalmap;
6821 // make sure that if glow is going to be used, both textures are not NULL
6822 if (!t->backgroundglowtexture && t->glowtexture)
6823 t->backgroundglowtexture = r_texture_black;
6824 if (!t->glowtexture && t->backgroundglowtexture)
6825 t->glowtexture = r_texture_black;
6829 t->backgroundbasetexture = r_texture_white;
6830 t->backgroundnmaptexture = r_texture_blanknormalmap;
6831 t->backgroundglosstexture = r_texture_black;
6832 t->backgroundglowtexture = NULL;
6834 t->specularpower = r_shadow_glossexponent.value;
6835 // TODO: store reference values for these in the texture?
6836 if (r_shadow_gloss.integer > 0)
6838 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
6840 if (r_shadow_glossintensity.value > 0)
6842 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
6843 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
6844 specularscale = r_shadow_glossintensity.value;
6847 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
6849 t->glosstexture = r_texture_white;
6850 t->backgroundglosstexture = r_texture_white;
6851 specularscale = r_shadow_gloss2intensity.value;
6852 t->specularpower = r_shadow_gloss2exponent.value;
6855 specularscale *= t->specularscalemod;
6856 t->specularpower *= t->specularpowermod;
6858 // lightmaps mode looks bad with dlights using actual texturing, so turn
6859 // off the colormap and glossmap, but leave the normalmap on as it still
6860 // accurately represents the shading involved
6861 if (gl_lightmaps.integer && ent != &cl_meshentities[MESH_UI].render)
6863 t->basetexture = r_texture_grey128;
6864 t->pantstexture = r_texture_black;
6865 t->shirttexture = r_texture_black;
6866 if (gl_lightmaps.integer < 2)
6867 t->nmaptexture = r_texture_blanknormalmap;
6868 t->glosstexture = r_texture_black;
6869 t->glowtexture = NULL;
6870 t->fogtexture = NULL;
6871 t->reflectmasktexture = NULL;
6872 t->backgroundbasetexture = NULL;
6873 if (gl_lightmaps.integer < 2)
6874 t->backgroundnmaptexture = r_texture_blanknormalmap;
6875 t->backgroundglosstexture = r_texture_black;
6876 t->backgroundglowtexture = NULL;
6878 t->currentmaterialflags = MATERIALFLAG_WALL | (t->currentmaterialflags & (MATERIALFLAG_NOCULLFACE | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SHORTDEPTHRANGE));
6881 if (specularscale != 1.0f)
6883 for (q = 0; q < 3; q++)
6885 t->render_modellight_specular[q] *= specularscale;
6886 t->render_lightmap_specular[q] *= specularscale;
6887 t->render_rtlight_specular[q] *= specularscale;
6891 t->currentblendfunc[0] = GL_ONE;
6892 t->currentblendfunc[1] = GL_ZERO;
6893 if (t->currentmaterialflags & MATERIALFLAG_ADD)
6895 t->currentblendfunc[0] = GL_SRC_ALPHA;
6896 t->currentblendfunc[1] = GL_ONE;
6898 else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
6900 t->currentblendfunc[0] = GL_SRC_ALPHA;
6901 t->currentblendfunc[1] = GL_ONE_MINUS_SRC_ALPHA;
6903 else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
6905 t->currentblendfunc[0] = t->customblendfunc[0];
6906 t->currentblendfunc[1] = t->customblendfunc[1];
6912 rsurfacestate_t rsurface;
6914 void RSurf_ActiveModelEntity(const entity_render_t *ent, qbool wantnormals, qbool wanttangents, qbool prepass)
6916 model_t *model = ent->model;
6917 //if (rsurface.entity == ent && (!model->surfmesh.isanimated || (!wantnormals && !wanttangents)))
6919 rsurface.entity = (entity_render_t *)ent;
6920 rsurface.skeleton = ent->skeleton;
6921 memcpy(rsurface.userwavefunc_param, ent->userwavefunc_param, sizeof(rsurface.userwavefunc_param));
6922 rsurface.ent_skinnum = ent->skinnum;
6923 rsurface.ent_qwskin = (ent->entitynumber <= cl.maxclients && ent->entitynumber >= 1 && cls.protocol == PROTOCOL_QUAKEWORLD && cl.scores[ent->entitynumber - 1].qw_skin[0] && !strcmp(ent->model->name, "progs/player.mdl")) ? (ent->entitynumber - 1) : -1;
6924 rsurface.ent_flags = ent->flags;
6925 if (r_fullbright_directed.integer && (r_fullbright.integer || !model->lit))
6926 rsurface.ent_flags |= RENDER_LIGHT | RENDER_DYNAMICMODELLIGHT;
6927 rsurface.shadertime = r_refdef.scene.time - ent->shadertime;
6928 rsurface.matrix = ent->matrix;
6929 rsurface.inversematrix = ent->inversematrix;
6930 rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
6931 rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
6932 R_EntityMatrix(&rsurface.matrix);
6933 Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
6934 Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
6935 rsurface.fogplaneviewdist = r_refdef.fogplaneviewdist * rsurface.inversematrixscale;
6936 rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
6937 rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
6938 rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
6939 memcpy(rsurface.frameblend, ent->frameblend, sizeof(ent->frameblend));
6940 rsurface.ent_alttextures = ent->framegroupblend[0].frame != 0;
6941 rsurface.basepolygonfactor = r_refdef.polygonfactor;
6942 rsurface.basepolygonoffset = r_refdef.polygonoffset;
6943 if (ent->model->brush.submodel && !prepass)
6945 rsurface.basepolygonfactor += r_polygonoffset_submodel_factor.value;
6946 rsurface.basepolygonoffset += r_polygonoffset_submodel_offset.value;
6948 // if the animcache code decided it should use the shader path, skip the deform step
6949 rsurface.entityskeletaltransform3x4 = ent->animcache_skeletaltransform3x4;
6950 rsurface.entityskeletaltransform3x4buffer = ent->animcache_skeletaltransform3x4buffer;
6951 rsurface.entityskeletaltransform3x4offset = ent->animcache_skeletaltransform3x4offset;
6952 rsurface.entityskeletaltransform3x4size = ent->animcache_skeletaltransform3x4size;
6953 rsurface.entityskeletalnumtransforms = rsurface.entityskeletaltransform3x4 ? model->num_bones : 0;
6954 if (model->surfmesh.isanimated && model->AnimateVertices && !rsurface.entityskeletaltransform3x4)
6956 if (ent->animcache_vertex3f)
6958 r_refdef.stats[r_stat_batch_entitycache_count]++;
6959 r_refdef.stats[r_stat_batch_entitycache_surfaces] += model->num_surfaces;
6960 r_refdef.stats[r_stat_batch_entitycache_vertices] += model->surfmesh.num_vertices;
6961 r_refdef.stats[r_stat_batch_entitycache_triangles] += model->surfmesh.num_triangles;
6962 rsurface.modelvertex3f = ent->animcache_vertex3f;
6963 rsurface.modelvertex3f_vertexbuffer = ent->animcache_vertex3f_vertexbuffer;
6964 rsurface.modelvertex3f_bufferoffset = ent->animcache_vertex3f_bufferoffset;
6965 rsurface.modelsvector3f = wanttangents ? ent->animcache_svector3f : NULL;
6966 rsurface.modelsvector3f_vertexbuffer = wanttangents ? ent->animcache_svector3f_vertexbuffer : NULL;
6967 rsurface.modelsvector3f_bufferoffset = wanttangents ? ent->animcache_svector3f_bufferoffset : 0;
6968 rsurface.modeltvector3f = wanttangents ? ent->animcache_tvector3f : NULL;
6969 rsurface.modeltvector3f_vertexbuffer = wanttangents ? ent->animcache_tvector3f_vertexbuffer : NULL;
6970 rsurface.modeltvector3f_bufferoffset = wanttangents ? ent->animcache_tvector3f_bufferoffset : 0;
6971 rsurface.modelnormal3f = wantnormals ? ent->animcache_normal3f : NULL;
6972 rsurface.modelnormal3f_vertexbuffer = wantnormals ? ent->animcache_normal3f_vertexbuffer : NULL;
6973 rsurface.modelnormal3f_bufferoffset = wantnormals ? ent->animcache_normal3f_bufferoffset : 0;
6975 else if (wanttangents)
6977 r_refdef.stats[r_stat_batch_entityanimate_count]++;
6978 r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
6979 r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
6980 r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
6981 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6982 rsurface.modelsvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6983 rsurface.modeltvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6984 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
6985 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, rsurface.modelsvector3f, rsurface.modeltvector3f);
6986 rsurface.modelvertex3f_vertexbuffer = NULL;
6987 rsurface.modelvertex3f_bufferoffset = 0;
6988 rsurface.modelvertex3f_vertexbuffer = 0;
6989 rsurface.modelvertex3f_bufferoffset = 0;
6990 rsurface.modelsvector3f_vertexbuffer = 0;
6991 rsurface.modelsvector3f_bufferoffset = 0;
6992 rsurface.modeltvector3f_vertexbuffer = 0;
6993 rsurface.modeltvector3f_bufferoffset = 0;
6994 rsurface.modelnormal3f_vertexbuffer = 0;
6995 rsurface.modelnormal3f_bufferoffset = 0;
6997 else if (wantnormals)
6999 r_refdef.stats[r_stat_batch_entityanimate_count]++;
7000 r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
7001 r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
7002 r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
7003 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7004 rsurface.modelsvector3f = NULL;
7005 rsurface.modeltvector3f = NULL;
7006 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7007 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, NULL, NULL);
7008 rsurface.modelvertex3f_vertexbuffer = NULL;
7009 rsurface.modelvertex3f_bufferoffset = 0;
7010 rsurface.modelvertex3f_vertexbuffer = 0;
7011 rsurface.modelvertex3f_bufferoffset = 0;
7012 rsurface.modelsvector3f_vertexbuffer = 0;
7013 rsurface.modelsvector3f_bufferoffset = 0;
7014 rsurface.modeltvector3f_vertexbuffer = 0;
7015 rsurface.modeltvector3f_bufferoffset = 0;
7016 rsurface.modelnormal3f_vertexbuffer = 0;
7017 rsurface.modelnormal3f_bufferoffset = 0;
7021 r_refdef.stats[r_stat_batch_entityanimate_count]++;
7022 r_refdef.stats[r_stat_batch_entityanimate_surfaces] += model->num_surfaces;
7023 r_refdef.stats[r_stat_batch_entityanimate_vertices] += model->surfmesh.num_vertices;
7024 r_refdef.stats[r_stat_batch_entityanimate_triangles] += model->surfmesh.num_triangles;
7025 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
7026 rsurface.modelsvector3f = NULL;
7027 rsurface.modeltvector3f = NULL;
7028 rsurface.modelnormal3f = NULL;
7029 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, NULL, NULL, NULL);
7030 rsurface.modelvertex3f_vertexbuffer = NULL;
7031 rsurface.modelvertex3f_bufferoffset = 0;
7032 rsurface.modelvertex3f_vertexbuffer = 0;
7033 rsurface.modelvertex3f_bufferoffset = 0;
7034 rsurface.modelsvector3f_vertexbuffer = 0;
7035 rsurface.modelsvector3f_bufferoffset = 0;
7036 rsurface.modeltvector3f_vertexbuffer = 0;
7037 rsurface.modeltvector3f_bufferoffset = 0;
7038 rsurface.modelnormal3f_vertexbuffer = 0;
7039 rsurface.modelnormal3f_bufferoffset = 0;
7041 rsurface.modelgeneratedvertex = true;
7045 if (rsurface.entityskeletaltransform3x4)
7047 r_refdef.stats[r_stat_batch_entityskeletal_count]++;
7048 r_refdef.stats[r_stat_batch_entityskeletal_surfaces] += model->num_surfaces;
7049 r_refdef.stats[r_stat_batch_entityskeletal_vertices] += model->surfmesh.num_vertices;
7050 r_refdef.stats[r_stat_batch_entityskeletal_triangles] += model->surfmesh.num_triangles;
7054 r_refdef.stats[r_stat_batch_entitystatic_count]++;
7055 r_refdef.stats[r_stat_batch_entitystatic_surfaces] += model->num_surfaces;
7056 r_refdef.stats[r_stat_batch_entitystatic_vertices] += model->surfmesh.num_vertices;
7057 r_refdef.stats[r_stat_batch_entitystatic_triangles] += model->surfmesh.num_triangles;
7059 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
7060 rsurface.modelvertex3f_vertexbuffer = model->surfmesh.data_vertex3f_vertexbuffer;
7061 rsurface.modelvertex3f_bufferoffset = model->surfmesh.data_vertex3f_bufferoffset;
7062 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
7063 rsurface.modelsvector3f_vertexbuffer = model->surfmesh.data_svector3f_vertexbuffer;
7064 rsurface.modelsvector3f_bufferoffset = model->surfmesh.data_svector3f_bufferoffset;
7065 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
7066 rsurface.modeltvector3f_vertexbuffer = model->surfmesh.data_tvector3f_vertexbuffer;
7067 rsurface.modeltvector3f_bufferoffset = model->surfmesh.data_tvector3f_bufferoffset;
7068 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
7069 rsurface.modelnormal3f_vertexbuffer = model->surfmesh.data_normal3f_vertexbuffer;
7070 rsurface.modelnormal3f_bufferoffset = model->surfmesh.data_normal3f_bufferoffset;
7071 rsurface.modelgeneratedvertex = false;
7073 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
7074 rsurface.modellightmapcolor4f_vertexbuffer = model->surfmesh.data_lightmapcolor4f_vertexbuffer;
7075 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.data_lightmapcolor4f_bufferoffset;
7076 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
7077 rsurface.modeltexcoordtexture2f_vertexbuffer = model->surfmesh.data_texcoordtexture2f_vertexbuffer;
7078 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.data_texcoordtexture2f_bufferoffset;
7079 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
7080 rsurface.modeltexcoordlightmap2f_vertexbuffer = model->surfmesh.data_texcoordlightmap2f_vertexbuffer;
7081 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.data_texcoordlightmap2f_bufferoffset;
7082 rsurface.modelskeletalindex4ub = model->surfmesh.data_skeletalindex4ub;
7083 rsurface.modelskeletalindex4ub_vertexbuffer = model->surfmesh.data_skeletalindex4ub_vertexbuffer;
7084 rsurface.modelskeletalindex4ub_bufferoffset = model->surfmesh.data_skeletalindex4ub_bufferoffset;
7085 rsurface.modelskeletalweight4ub = model->surfmesh.data_skeletalweight4ub;
7086 rsurface.modelskeletalweight4ub_vertexbuffer = model->surfmesh.data_skeletalweight4ub_vertexbuffer;
7087 rsurface.modelskeletalweight4ub_bufferoffset = model->surfmesh.data_skeletalweight4ub_bufferoffset;
7088 rsurface.modelelement3i = model->surfmesh.data_element3i;
7089 rsurface.modelelement3i_indexbuffer = model->surfmesh.data_element3i_indexbuffer;
7090 rsurface.modelelement3i_bufferoffset = model->surfmesh.data_element3i_bufferoffset;
7091 rsurface.modelelement3s = model->surfmesh.data_element3s;
7092 rsurface.modelelement3s_indexbuffer = model->surfmesh.data_element3s_indexbuffer;
7093 rsurface.modelelement3s_bufferoffset = model->surfmesh.data_element3s_bufferoffset;
7094 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
7095 rsurface.modelnumvertices = model->surfmesh.num_vertices;
7096 rsurface.modelnumtriangles = model->surfmesh.num_triangles;
7097 rsurface.modelsurfaces = model->data_surfaces;
7098 rsurface.batchgeneratedvertex = false;
7099 rsurface.batchfirstvertex = 0;
7100 rsurface.batchnumvertices = 0;
7101 rsurface.batchfirsttriangle = 0;
7102 rsurface.batchnumtriangles = 0;
7103 rsurface.batchvertex3f = NULL;
7104 rsurface.batchvertex3f_vertexbuffer = NULL;
7105 rsurface.batchvertex3f_bufferoffset = 0;
7106 rsurface.batchsvector3f = NULL;
7107 rsurface.batchsvector3f_vertexbuffer = NULL;
7108 rsurface.batchsvector3f_bufferoffset = 0;
7109 rsurface.batchtvector3f = NULL;
7110 rsurface.batchtvector3f_vertexbuffer = NULL;
7111 rsurface.batchtvector3f_bufferoffset = 0;
7112 rsurface.batchnormal3f = NULL;
7113 rsurface.batchnormal3f_vertexbuffer = NULL;
7114 rsurface.batchnormal3f_bufferoffset = 0;
7115 rsurface.batchlightmapcolor4f = NULL;
7116 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7117 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7118 rsurface.batchtexcoordtexture2f = NULL;
7119 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7120 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7121 rsurface.batchtexcoordlightmap2f = NULL;
7122 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7123 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7124 rsurface.batchskeletalindex4ub = NULL;
7125 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7126 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7127 rsurface.batchskeletalweight4ub = NULL;
7128 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7129 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7130 rsurface.batchelement3i = NULL;
7131 rsurface.batchelement3i_indexbuffer = NULL;
7132 rsurface.batchelement3i_bufferoffset = 0;
7133 rsurface.batchelement3s = NULL;
7134 rsurface.batchelement3s_indexbuffer = NULL;
7135 rsurface.batchelement3s_bufferoffset = 0;
7136 rsurface.forcecurrenttextureupdate = false;
7139 void RSurf_ActiveCustomEntity(const matrix4x4_t *matrix, const matrix4x4_t *inversematrix, int entflags, double shadertime, float r, float g, float b, float a, int numvertices, const float *vertex3f, const float *texcoord2f, const float *normal3f, const float *svector3f, const float *tvector3f, const float *color4f, int numtriangles, const int *element3i, const unsigned short *element3s, qbool wantnormals, qbool wanttangents)
7141 rsurface.entity = r_refdef.scene.worldentity;
7142 if (r != 1.0f || g != 1.0f || b != 1.0f || a != 1.0f) {
7143 // HACK to provide a valid entity with modded colors to R_GetCurrentTexture.
7144 // A better approach could be making this copy only once per frame.
7145 static entity_render_t custom_entity;
7147 custom_entity = *rsurface.entity;
7148 for (q = 0; q < 3; ++q) {
7149 float colormod = q == 0 ? r : q == 1 ? g : b;
7150 custom_entity.render_fullbright[q] *= colormod;
7151 custom_entity.render_modellight_ambient[q] *= colormod;
7152 custom_entity.render_modellight_diffuse[q] *= colormod;
7153 custom_entity.render_lightmap_ambient[q] *= colormod;
7154 custom_entity.render_lightmap_diffuse[q] *= colormod;
7155 custom_entity.render_rtlight_diffuse[q] *= colormod;
7157 custom_entity.alpha *= a;
7158 rsurface.entity = &custom_entity;
7160 rsurface.skeleton = NULL;
7161 rsurface.ent_skinnum = 0;
7162 rsurface.ent_qwskin = -1;
7163 rsurface.ent_flags = entflags;
7164 rsurface.shadertime = r_refdef.scene.time - shadertime;
7165 rsurface.modelnumvertices = numvertices;
7166 rsurface.modelnumtriangles = numtriangles;
7167 rsurface.matrix = *matrix;
7168 rsurface.inversematrix = *inversematrix;
7169 rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
7170 rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
7171 R_EntityMatrix(&rsurface.matrix);
7172 Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
7173 Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
7174 rsurface.fogplaneviewdist *= rsurface.inversematrixscale;
7175 rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
7176 rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
7177 rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
7178 memset(rsurface.frameblend, 0, sizeof(rsurface.frameblend));
7179 rsurface.frameblend[0].lerp = 1;
7180 rsurface.ent_alttextures = false;
7181 rsurface.basepolygonfactor = r_refdef.polygonfactor;
7182 rsurface.basepolygonoffset = r_refdef.polygonoffset;
7183 rsurface.entityskeletaltransform3x4 = NULL;
7184 rsurface.entityskeletaltransform3x4buffer = NULL;
7185 rsurface.entityskeletaltransform3x4offset = 0;
7186 rsurface.entityskeletaltransform3x4size = 0;
7187 rsurface.entityskeletalnumtransforms = 0;
7188 r_refdef.stats[r_stat_batch_entitycustom_count]++;
7189 r_refdef.stats[r_stat_batch_entitycustom_surfaces] += 1;
7190 r_refdef.stats[r_stat_batch_entitycustom_vertices] += rsurface.modelnumvertices;
7191 r_refdef.stats[r_stat_batch_entitycustom_triangles] += rsurface.modelnumtriangles;
7194 rsurface.modelvertex3f = (float *)vertex3f;
7195 rsurface.modelsvector3f = svector3f ? (float *)svector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7196 rsurface.modeltvector3f = tvector3f ? (float *)tvector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7197 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7199 else if (wantnormals)
7201 rsurface.modelvertex3f = (float *)vertex3f;
7202 rsurface.modelsvector3f = NULL;
7203 rsurface.modeltvector3f = NULL;
7204 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7208 rsurface.modelvertex3f = (float *)vertex3f;
7209 rsurface.modelsvector3f = NULL;
7210 rsurface.modeltvector3f = NULL;
7211 rsurface.modelnormal3f = NULL;
7213 rsurface.modelvertex3f_vertexbuffer = 0;
7214 rsurface.modelvertex3f_bufferoffset = 0;
7215 rsurface.modelsvector3f_vertexbuffer = 0;
7216 rsurface.modelsvector3f_bufferoffset = 0;
7217 rsurface.modeltvector3f_vertexbuffer = 0;
7218 rsurface.modeltvector3f_bufferoffset = 0;
7219 rsurface.modelnormal3f_vertexbuffer = 0;
7220 rsurface.modelnormal3f_bufferoffset = 0;
7221 rsurface.modelgeneratedvertex = true;
7222 rsurface.modellightmapcolor4f = (float *)color4f;
7223 rsurface.modellightmapcolor4f_vertexbuffer = 0;
7224 rsurface.modellightmapcolor4f_bufferoffset = 0;
7225 rsurface.modeltexcoordtexture2f = (float *)texcoord2f;
7226 rsurface.modeltexcoordtexture2f_vertexbuffer = 0;
7227 rsurface.modeltexcoordtexture2f_bufferoffset = 0;
7228 rsurface.modeltexcoordlightmap2f = NULL;
7229 rsurface.modeltexcoordlightmap2f_vertexbuffer = 0;
7230 rsurface.modeltexcoordlightmap2f_bufferoffset = 0;
7231 rsurface.modelskeletalindex4ub = NULL;
7232 rsurface.modelskeletalindex4ub_vertexbuffer = NULL;
7233 rsurface.modelskeletalindex4ub_bufferoffset = 0;
7234 rsurface.modelskeletalweight4ub = NULL;
7235 rsurface.modelskeletalweight4ub_vertexbuffer = NULL;
7236 rsurface.modelskeletalweight4ub_bufferoffset = 0;
7237 rsurface.modelelement3i = (int *)element3i;
7238 rsurface.modelelement3i_indexbuffer = NULL;
7239 rsurface.modelelement3i_bufferoffset = 0;
7240 rsurface.modelelement3s = (unsigned short *)element3s;
7241 rsurface.modelelement3s_indexbuffer = NULL;
7242 rsurface.modelelement3s_bufferoffset = 0;
7243 rsurface.modellightmapoffsets = NULL;
7244 rsurface.modelsurfaces = NULL;
7245 rsurface.batchgeneratedvertex = false;
7246 rsurface.batchfirstvertex = 0;
7247 rsurface.batchnumvertices = 0;
7248 rsurface.batchfirsttriangle = 0;
7249 rsurface.batchnumtriangles = 0;
7250 rsurface.batchvertex3f = NULL;
7251 rsurface.batchvertex3f_vertexbuffer = NULL;
7252 rsurface.batchvertex3f_bufferoffset = 0;
7253 rsurface.batchsvector3f = NULL;
7254 rsurface.batchsvector3f_vertexbuffer = NULL;
7255 rsurface.batchsvector3f_bufferoffset = 0;
7256 rsurface.batchtvector3f = NULL;
7257 rsurface.batchtvector3f_vertexbuffer = NULL;
7258 rsurface.batchtvector3f_bufferoffset = 0;
7259 rsurface.batchnormal3f = NULL;
7260 rsurface.batchnormal3f_vertexbuffer = NULL;
7261 rsurface.batchnormal3f_bufferoffset = 0;
7262 rsurface.batchlightmapcolor4f = NULL;
7263 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7264 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7265 rsurface.batchtexcoordtexture2f = NULL;
7266 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7267 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7268 rsurface.batchtexcoordlightmap2f = NULL;
7269 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7270 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7271 rsurface.batchskeletalindex4ub = NULL;
7272 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7273 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7274 rsurface.batchskeletalweight4ub = NULL;
7275 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7276 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7277 rsurface.batchelement3i = NULL;
7278 rsurface.batchelement3i_indexbuffer = NULL;
7279 rsurface.batchelement3i_bufferoffset = 0;
7280 rsurface.batchelement3s = NULL;
7281 rsurface.batchelement3s_indexbuffer = NULL;
7282 rsurface.batchelement3s_bufferoffset = 0;
7283 rsurface.forcecurrenttextureupdate = true;
7285 if (rsurface.modelnumvertices && rsurface.modelelement3i)
7287 if ((wantnormals || wanttangents) && !normal3f)
7289 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7290 Mod_BuildNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.modelnormal3f, r_smoothnormals_areaweighting.integer != 0);
7292 if (wanttangents && !svector3f)
7294 rsurface.modelsvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7295 rsurface.modeltvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
7296 Mod_BuildTextureVectorsFromNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modeltexcoordtexture2f, rsurface.modelnormal3f, rsurface.modelelement3i, rsurface.modelsvector3f, rsurface.modeltvector3f, r_smoothnormals_areaweighting.integer != 0);
7301 float RSurf_FogPoint(const float *v)
7303 // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
7304 float FogPlaneViewDist = r_refdef.fogplaneviewdist;
7305 float FogPlaneVertexDist = DotProduct(r_refdef.fogplane, v) + r_refdef.fogplane[3];
7306 float FogHeightFade = r_refdef.fogheightfade;
7308 unsigned int fogmasktableindex;
7309 if (r_refdef.fogplaneviewabove)
7310 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
7312 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
7313 fogmasktableindex = (unsigned int)(VectorDistance(r_refdef.view.origin, v) * fogfrac * r_refdef.fogmasktabledistmultiplier);
7314 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
7317 float RSurf_FogVertex(const float *v)
7319 // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
7320 float FogPlaneViewDist = rsurface.fogplaneviewdist;
7321 float FogPlaneVertexDist = DotProduct(rsurface.fogplane, v) + rsurface.fogplane[3];
7322 float FogHeightFade = rsurface.fogheightfade;
7324 unsigned int fogmasktableindex;
7325 if (r_refdef.fogplaneviewabove)
7326 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
7328 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
7329 fogmasktableindex = (unsigned int)(VectorDistance(rsurface.localvieworigin, v) * fogfrac * rsurface.fogmasktabledistmultiplier);
7330 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
7333 void RSurf_UploadBuffersForBatch(void)
7335 // upload buffer data for generated vertex data (dynamicvertex case) or index data (copytriangles case) and models that lack it to begin with (e.g. DrawQ_FlushUI)
7336 // note that if rsurface.batchvertex3f_vertexbuffer is NULL, dynamicvertex is forced as we don't account for the proper base vertex here.
7337 if (rsurface.batchvertex3f && !rsurface.batchvertex3f_vertexbuffer)
7338 rsurface.batchvertex3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f, R_BUFFERDATA_VERTEX, &rsurface.batchvertex3f_bufferoffset);
7339 if (rsurface.batchsvector3f && !rsurface.batchsvector3f_vertexbuffer)
7340 rsurface.batchsvector3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchsvector3f, R_BUFFERDATA_VERTEX, &rsurface.batchsvector3f_bufferoffset);
7341 if (rsurface.batchtvector3f && !rsurface.batchtvector3f_vertexbuffer)
7342 rsurface.batchtvector3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchtvector3f, R_BUFFERDATA_VERTEX, &rsurface.batchtvector3f_bufferoffset);
7343 if (rsurface.batchnormal3f && !rsurface.batchnormal3f_vertexbuffer)
7344 rsurface.batchnormal3f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f, R_BUFFERDATA_VERTEX, &rsurface.batchnormal3f_bufferoffset);
7345 if (rsurface.batchlightmapcolor4f && !rsurface.batchlightmapcolor4f_vertexbuffer)
7346 rsurface.batchlightmapcolor4f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[4]), rsurface.batchlightmapcolor4f, R_BUFFERDATA_VERTEX, &rsurface.batchlightmapcolor4f_bufferoffset);
7347 if (rsurface.batchtexcoordtexture2f && !rsurface.batchtexcoordtexture2f_vertexbuffer)
7348 rsurface.batchtexcoordtexture2f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[2]), rsurface.batchtexcoordtexture2f, R_BUFFERDATA_VERTEX, &rsurface.batchtexcoordtexture2f_bufferoffset);
7349 if (rsurface.batchtexcoordlightmap2f && !rsurface.batchtexcoordlightmap2f_vertexbuffer)
7350 rsurface.batchtexcoordlightmap2f_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(float[2]), rsurface.batchtexcoordlightmap2f, R_BUFFERDATA_VERTEX, &rsurface.batchtexcoordlightmap2f_bufferoffset);
7351 if (rsurface.batchskeletalindex4ub && !rsurface.batchskeletalindex4ub_vertexbuffer)
7352 rsurface.batchskeletalindex4ub_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(unsigned char[4]), rsurface.batchskeletalindex4ub, R_BUFFERDATA_VERTEX, &rsurface.batchskeletalindex4ub_bufferoffset);
7353 if (rsurface.batchskeletalweight4ub && !rsurface.batchskeletalweight4ub_vertexbuffer)
7354 rsurface.batchskeletalweight4ub_vertexbuffer = R_BufferData_Store(rsurface.batchnumvertices * sizeof(unsigned char[4]), rsurface.batchskeletalweight4ub, R_BUFFERDATA_VERTEX, &rsurface.batchskeletalweight4ub_bufferoffset);
7356 if (rsurface.batchelement3s && !rsurface.batchelement3s_indexbuffer)
7357 rsurface.batchelement3s_indexbuffer = R_BufferData_Store(rsurface.batchnumtriangles * sizeof(short[3]), rsurface.batchelement3s, R_BUFFERDATA_INDEX16, &rsurface.batchelement3s_bufferoffset);
7358 else if (rsurface.batchelement3i && !rsurface.batchelement3i_indexbuffer)
7359 rsurface.batchelement3i_indexbuffer = R_BufferData_Store(rsurface.batchnumtriangles * sizeof(int[3]), rsurface.batchelement3i, R_BUFFERDATA_INDEX32, &rsurface.batchelement3i_bufferoffset);
7361 R_Mesh_VertexPointer( 3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
7362 R_Mesh_ColorPointer( 4, GL_FLOAT, sizeof(float[4]), rsurface.batchlightmapcolor4f, rsurface.batchlightmapcolor4f_vertexbuffer, rsurface.batchlightmapcolor4f_bufferoffset);
7363 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
7364 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchsvector3f, rsurface.batchsvector3f_vertexbuffer, rsurface.batchsvector3f_bufferoffset);
7365 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchtvector3f, rsurface.batchtvector3f_vertexbuffer, rsurface.batchtvector3f_bufferoffset);
7366 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchnormal3f, rsurface.batchnormal3f_vertexbuffer, rsurface.batchnormal3f_bufferoffset);
7367 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordlightmap2f, rsurface.batchtexcoordlightmap2f_vertexbuffer, rsurface.batchtexcoordlightmap2f_bufferoffset);
7368 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
7369 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE | 0x80000000, sizeof(unsigned char[4]), rsurface.batchskeletalindex4ub, rsurface.batchskeletalindex4ub_vertexbuffer, rsurface.batchskeletalindex4ub_bufferoffset);
7370 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), rsurface.batchskeletalweight4ub, rsurface.batchskeletalweight4ub_vertexbuffer, rsurface.batchskeletalweight4ub_bufferoffset);
7373 static void RSurf_RenumberElements(const int *inelement3i, int *outelement3i, int numelements, int adjust)
7376 for (i = 0;i < numelements;i++)
7377 outelement3i[i] = inelement3i[i] + adjust;
7380 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
7381 void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const msurface_t **texturesurfacelist)
7389 int surfacefirsttriangle;
7390 int surfacenumtriangles;
7391 int surfacefirstvertex;
7392 int surfaceendvertex;
7393 int surfacenumvertices;
7394 int batchnumsurfaces = texturenumsurfaces;
7395 int batchnumvertices;
7396 int batchnumtriangles;
7399 qbool dynamicvertex;
7402 float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
7405 q3shaderinfo_deform_t *deform;
7406 const msurface_t *surface, *firstsurface;
7407 if (!texturenumsurfaces)
7409 // find vertex range of this surface batch
7411 firstsurface = texturesurfacelist[0];
7412 firsttriangle = firstsurface->num_firsttriangle;
7413 batchnumvertices = 0;
7414 batchnumtriangles = 0;
7415 firstvertex = endvertex = firstsurface->num_firstvertex;
7416 for (i = 0;i < texturenumsurfaces;i++)
7418 surface = texturesurfacelist[i];
7419 if (surface != firstsurface + i)
7421 surfacefirstvertex = surface->num_firstvertex;
7422 surfaceendvertex = surfacefirstvertex + surface->num_vertices;
7423 surfacenumvertices = surface->num_vertices;
7424 surfacenumtriangles = surface->num_triangles;
7425 if (firstvertex > surfacefirstvertex)
7426 firstvertex = surfacefirstvertex;
7427 if (endvertex < surfaceendvertex)
7428 endvertex = surfaceendvertex;
7429 batchnumvertices += surfacenumvertices;
7430 batchnumtriangles += surfacenumtriangles;
7433 r_refdef.stats[r_stat_batch_batches]++;
7435 r_refdef.stats[r_stat_batch_withgaps]++;
7436 r_refdef.stats[r_stat_batch_surfaces] += batchnumsurfaces;
7437 r_refdef.stats[r_stat_batch_vertices] += batchnumvertices;
7438 r_refdef.stats[r_stat_batch_triangles] += batchnumtriangles;
7440 // we now know the vertex range used, and if there are any gaps in it
7441 rsurface.batchfirstvertex = firstvertex;
7442 rsurface.batchnumvertices = endvertex - firstvertex;
7443 rsurface.batchfirsttriangle = firsttriangle;
7444 rsurface.batchnumtriangles = batchnumtriangles;
7446 // check if any dynamic vertex processing must occur
7447 dynamicvertex = false;
7449 // we must use vertexbuffers for rendering, we can upload vertex buffers
7450 // easily enough but if the basevertex is non-zero it becomes more
7451 // difficult, so force dynamicvertex path in that case - it's suboptimal
7452 // but the most optimal case is to have the geometry sources provide their
7454 if (!rsurface.modelvertex3f_vertexbuffer && firstvertex != 0)
7455 dynamicvertex = true;
7457 // a cvar to force the dynamic vertex path to be taken, for debugging
7458 if (r_batch_debugdynamicvertexpath.integer)
7462 r_refdef.stats[r_stat_batch_dynamic_batches_because_cvar] += 1;
7463 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_cvar] += batchnumsurfaces;
7464 r_refdef.stats[r_stat_batch_dynamic_vertices_because_cvar] += batchnumvertices;
7465 r_refdef.stats[r_stat_batch_dynamic_triangles_because_cvar] += batchnumtriangles;
7467 dynamicvertex = true;
7470 // if there is a chance of animated vertex colors, it's a dynamic batch
7471 if ((batchneed & BATCHNEED_ARRAY_VERTEXCOLOR) && texturesurfacelist[0]->lightmapinfo)
7475 r_refdef.stats[r_stat_batch_dynamic_batches_because_lightmapvertex] += 1;
7476 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_lightmapvertex] += batchnumsurfaces;
7477 r_refdef.stats[r_stat_batch_dynamic_vertices_because_lightmapvertex] += batchnumvertices;
7478 r_refdef.stats[r_stat_batch_dynamic_triangles_because_lightmapvertex] += batchnumtriangles;
7480 dynamicvertex = true;
7483 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
7485 switch (deform->deform)
7488 case Q3DEFORM_PROJECTIONSHADOW:
7489 case Q3DEFORM_TEXT0:
7490 case Q3DEFORM_TEXT1:
7491 case Q3DEFORM_TEXT2:
7492 case Q3DEFORM_TEXT3:
7493 case Q3DEFORM_TEXT4:
7494 case Q3DEFORM_TEXT5:
7495 case Q3DEFORM_TEXT6:
7496 case Q3DEFORM_TEXT7:
7499 case Q3DEFORM_AUTOSPRITE:
7502 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_autosprite] += 1;
7503 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_autosprite] += batchnumsurfaces;
7504 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_autosprite] += batchnumvertices;
7505 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_autosprite] += batchnumtriangles;
7507 dynamicvertex = true;
7508 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_TEXCOORD;
7510 case Q3DEFORM_AUTOSPRITE2:
7513 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_autosprite2] += 1;
7514 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_autosprite2] += batchnumsurfaces;
7515 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_autosprite2] += batchnumvertices;
7516 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_autosprite2] += batchnumtriangles;
7518 dynamicvertex = true;
7519 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD;
7521 case Q3DEFORM_NORMAL:
7524 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_normal] += 1;
7525 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_normal] += batchnumsurfaces;
7526 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_normal] += batchnumvertices;
7527 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_normal] += batchnumtriangles;
7529 dynamicvertex = true;
7530 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7533 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
7534 break; // if wavefunc is a nop, ignore this transform
7537 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_wave] += 1;
7538 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_wave] += batchnumsurfaces;
7539 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_wave] += batchnumvertices;
7540 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_wave] += batchnumtriangles;
7542 dynamicvertex = true;
7543 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7545 case Q3DEFORM_BULGE:
7548 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_bulge] += 1;
7549 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_bulge] += batchnumsurfaces;
7550 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_bulge] += batchnumvertices;
7551 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_bulge] += batchnumtriangles;
7553 dynamicvertex = true;
7554 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD;
7557 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
7558 break; // if wavefunc is a nop, ignore this transform
7561 r_refdef.stats[r_stat_batch_dynamic_batches_because_deformvertexes_move] += 1;
7562 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_deformvertexes_move] += batchnumsurfaces;
7563 r_refdef.stats[r_stat_batch_dynamic_vertices_because_deformvertexes_move] += batchnumvertices;
7564 r_refdef.stats[r_stat_batch_dynamic_triangles_because_deformvertexes_move] += batchnumtriangles;
7566 dynamicvertex = true;
7567 batchneed |= BATCHNEED_ARRAY_VERTEX;
7571 if (rsurface.texture->materialshaderpass)
7573 switch (rsurface.texture->materialshaderpass->tcgen.tcgen)
7576 case Q3TCGEN_TEXTURE:
7578 case Q3TCGEN_LIGHTMAP:
7581 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_lightmap] += 1;
7582 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_lightmap] += batchnumsurfaces;
7583 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_lightmap] += batchnumvertices;
7584 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_lightmap] += batchnumtriangles;
7586 dynamicvertex = true;
7587 batchneed |= BATCHNEED_ARRAY_LIGHTMAP;
7589 case Q3TCGEN_VECTOR:
7592 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_vector] += 1;
7593 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_vector] += batchnumsurfaces;
7594 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_vector] += batchnumvertices;
7595 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_vector] += batchnumtriangles;
7597 dynamicvertex = true;
7598 batchneed |= BATCHNEED_ARRAY_VERTEX;
7600 case Q3TCGEN_ENVIRONMENT:
7603 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcgen_environment] += 1;
7604 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcgen_environment] += batchnumsurfaces;
7605 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcgen_environment] += batchnumvertices;
7606 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcgen_environment] += batchnumtriangles;
7608 dynamicvertex = true;
7609 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL;
7612 if (rsurface.texture->materialshaderpass->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
7616 r_refdef.stats[r_stat_batch_dynamic_batches_because_tcmod_turbulent] += 1;
7617 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_tcmod_turbulent] += batchnumsurfaces;
7618 r_refdef.stats[r_stat_batch_dynamic_vertices_because_tcmod_turbulent] += batchnumvertices;
7619 r_refdef.stats[r_stat_batch_dynamic_triangles_because_tcmod_turbulent] += batchnumtriangles;
7621 dynamicvertex = true;
7622 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD;
7626 // the caller can specify BATCHNEED_NOGAPS to force a batch with
7627 // firstvertex = 0 and endvertex = numvertices (no gaps, no firstvertex),
7628 // we ensure this by treating the vertex batch as dynamic...
7629 if ((batchneed & BATCHNEED_ALWAYSCOPY) || ((batchneed & BATCHNEED_NOGAPS) && (gaps || firstvertex > 0)))
7633 r_refdef.stats[r_stat_batch_dynamic_batches_because_nogaps] += 1;
7634 r_refdef.stats[r_stat_batch_dynamic_surfaces_because_nogaps] += batchnumsurfaces;
7635 r_refdef.stats[r_stat_batch_dynamic_vertices_because_nogaps] += batchnumvertices;
7636 r_refdef.stats[r_stat_batch_dynamic_triangles_because_nogaps] += batchnumtriangles;
7638 dynamicvertex = true;
7641 // if we're going to have to apply the skeletal transform manually, we need to batch the skeletal data
7642 if (dynamicvertex && rsurface.entityskeletaltransform3x4)
7643 batchneed |= BATCHNEED_ARRAY_SKELETAL;
7645 rsurface.batchvertex3f = rsurface.modelvertex3f;
7646 rsurface.batchvertex3f_vertexbuffer = rsurface.modelvertex3f_vertexbuffer;
7647 rsurface.batchvertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
7648 rsurface.batchsvector3f = rsurface.modelsvector3f;
7649 rsurface.batchsvector3f_vertexbuffer = rsurface.modelsvector3f_vertexbuffer;
7650 rsurface.batchsvector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
7651 rsurface.batchtvector3f = rsurface.modeltvector3f;
7652 rsurface.batchtvector3f_vertexbuffer = rsurface.modeltvector3f_vertexbuffer;
7653 rsurface.batchtvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
7654 rsurface.batchnormal3f = rsurface.modelnormal3f;
7655 rsurface.batchnormal3f_vertexbuffer = rsurface.modelnormal3f_vertexbuffer;
7656 rsurface.batchnormal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
7657 rsurface.batchlightmapcolor4f = rsurface.modellightmapcolor4f;
7658 rsurface.batchlightmapcolor4f_vertexbuffer = rsurface.modellightmapcolor4f_vertexbuffer;
7659 rsurface.batchlightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
7660 rsurface.batchtexcoordtexture2f = rsurface.modeltexcoordtexture2f;
7661 rsurface.batchtexcoordtexture2f_vertexbuffer = rsurface.modeltexcoordtexture2f_vertexbuffer;
7662 rsurface.batchtexcoordtexture2f_bufferoffset = rsurface.modeltexcoordtexture2f_bufferoffset;
7663 rsurface.batchtexcoordlightmap2f = rsurface.modeltexcoordlightmap2f;
7664 rsurface.batchtexcoordlightmap2f_vertexbuffer = rsurface.modeltexcoordlightmap2f_vertexbuffer;
7665 rsurface.batchtexcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
7666 rsurface.batchskeletalindex4ub = rsurface.modelskeletalindex4ub;
7667 rsurface.batchskeletalindex4ub_vertexbuffer = rsurface.modelskeletalindex4ub_vertexbuffer;
7668 rsurface.batchskeletalindex4ub_bufferoffset = rsurface.modelskeletalindex4ub_bufferoffset;
7669 rsurface.batchskeletalweight4ub = rsurface.modelskeletalweight4ub;
7670 rsurface.batchskeletalweight4ub_vertexbuffer = rsurface.modelskeletalweight4ub_vertexbuffer;
7671 rsurface.batchskeletalweight4ub_bufferoffset = rsurface.modelskeletalweight4ub_bufferoffset;
7672 rsurface.batchelement3i = rsurface.modelelement3i;
7673 rsurface.batchelement3i_indexbuffer = rsurface.modelelement3i_indexbuffer;
7674 rsurface.batchelement3i_bufferoffset = rsurface.modelelement3i_bufferoffset;
7675 rsurface.batchelement3s = rsurface.modelelement3s;
7676 rsurface.batchelement3s_indexbuffer = rsurface.modelelement3s_indexbuffer;
7677 rsurface.batchelement3s_bufferoffset = rsurface.modelelement3s_bufferoffset;
7678 rsurface.batchskeletaltransform3x4 = rsurface.entityskeletaltransform3x4;
7679 rsurface.batchskeletaltransform3x4buffer = rsurface.entityskeletaltransform3x4buffer;
7680 rsurface.batchskeletaltransform3x4offset = rsurface.entityskeletaltransform3x4offset;
7681 rsurface.batchskeletaltransform3x4size = rsurface.entityskeletaltransform3x4size;
7682 rsurface.batchskeletalnumtransforms = rsurface.entityskeletalnumtransforms;
7684 // if any dynamic vertex processing has to occur in software, we copy the
7685 // entire surface list together before processing to rebase the vertices
7686 // to start at 0 (otherwise we waste a lot of room in a vertex buffer).
7688 // if any gaps exist and we do not have a static vertex buffer, we have to
7689 // copy the surface list together to avoid wasting upload bandwidth on the
7690 // vertices in the gaps.
7692 // if gaps exist and we have a static vertex buffer, we can choose whether
7693 // to combine the index buffer ranges into one dynamic index buffer or
7694 // simply issue multiple glDrawElements calls (BATCHNEED_ALLOWMULTIDRAW).
7696 // in many cases the batch is reduced to one draw call.
7698 rsurface.batchmultidraw = false;
7699 rsurface.batchmultidrawnumsurfaces = 0;
7700 rsurface.batchmultidrawsurfacelist = NULL;
7704 // static vertex data, just set pointers...
7705 rsurface.batchgeneratedvertex = false;
7706 // if there are gaps, we want to build a combined index buffer,
7707 // otherwise use the original static buffer with an appropriate offset
7710 r_refdef.stats[r_stat_batch_copytriangles_batches] += 1;
7711 r_refdef.stats[r_stat_batch_copytriangles_surfaces] += batchnumsurfaces;
7712 r_refdef.stats[r_stat_batch_copytriangles_vertices] += batchnumvertices;
7713 r_refdef.stats[r_stat_batch_copytriangles_triangles] += batchnumtriangles;
7714 if ((batchneed & BATCHNEED_ALLOWMULTIDRAW) && r_batch_multidraw.integer && batchnumtriangles >= r_batch_multidraw_mintriangles.integer)
7716 rsurface.batchmultidraw = true;
7717 rsurface.batchmultidrawnumsurfaces = texturenumsurfaces;
7718 rsurface.batchmultidrawsurfacelist = texturesurfacelist;
7721 // build a new triangle elements array for this batch
7722 rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
7723 rsurface.batchfirsttriangle = 0;
7725 for (i = 0;i < texturenumsurfaces;i++)
7727 surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
7728 surfacenumtriangles = texturesurfacelist[i]->num_triangles;
7729 memcpy(rsurface.batchelement3i + 3*numtriangles, rsurface.modelelement3i + 3*surfacefirsttriangle, surfacenumtriangles*sizeof(int[3]));
7730 numtriangles += surfacenumtriangles;
7732 rsurface.batchelement3i_indexbuffer = NULL;
7733 rsurface.batchelement3i_bufferoffset = 0;
7734 rsurface.batchelement3s = NULL;
7735 rsurface.batchelement3s_indexbuffer = NULL;
7736 rsurface.batchelement3s_bufferoffset = 0;
7737 if (endvertex <= 65536)
7739 // make a 16bit (unsigned short) index array if possible
7740 rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
7741 for (i = 0;i < numtriangles*3;i++)
7742 rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
7747 r_refdef.stats[r_stat_batch_fast_batches] += 1;
7748 r_refdef.stats[r_stat_batch_fast_surfaces] += batchnumsurfaces;
7749 r_refdef.stats[r_stat_batch_fast_vertices] += batchnumvertices;
7750 r_refdef.stats[r_stat_batch_fast_triangles] += batchnumtriangles;
7755 // something needs software processing, do it for real...
7756 // we only directly handle separate array data in this case and then
7757 // generate interleaved data if needed...
7758 rsurface.batchgeneratedvertex = true;
7759 r_refdef.stats[r_stat_batch_dynamic_batches] += 1;
7760 r_refdef.stats[r_stat_batch_dynamic_surfaces] += batchnumsurfaces;
7761 r_refdef.stats[r_stat_batch_dynamic_vertices] += batchnumvertices;
7762 r_refdef.stats[r_stat_batch_dynamic_triangles] += batchnumtriangles;
7764 // now copy the vertex data into a combined array and make an index array
7765 // (this is what Quake3 does all the time)
7766 // we also apply any skeletal animation here that would have been done in
7767 // the vertex shader, because most of the dynamic vertex animation cases
7768 // need actual vertex positions and normals
7769 //if (dynamicvertex)
7771 rsurface.batchvertex3f = NULL;
7772 rsurface.batchvertex3f_vertexbuffer = NULL;
7773 rsurface.batchvertex3f_bufferoffset = 0;
7774 rsurface.batchsvector3f = NULL;
7775 rsurface.batchsvector3f_vertexbuffer = NULL;
7776 rsurface.batchsvector3f_bufferoffset = 0;
7777 rsurface.batchtvector3f = NULL;
7778 rsurface.batchtvector3f_vertexbuffer = NULL;
7779 rsurface.batchtvector3f_bufferoffset = 0;
7780 rsurface.batchnormal3f = NULL;
7781 rsurface.batchnormal3f_vertexbuffer = NULL;
7782 rsurface.batchnormal3f_bufferoffset = 0;
7783 rsurface.batchlightmapcolor4f = NULL;
7784 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
7785 rsurface.batchlightmapcolor4f_bufferoffset = 0;
7786 rsurface.batchtexcoordtexture2f = NULL;
7787 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
7788 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
7789 rsurface.batchtexcoordlightmap2f = NULL;
7790 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
7791 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
7792 rsurface.batchskeletalindex4ub = NULL;
7793 rsurface.batchskeletalindex4ub_vertexbuffer = NULL;
7794 rsurface.batchskeletalindex4ub_bufferoffset = 0;
7795 rsurface.batchskeletalweight4ub = NULL;
7796 rsurface.batchskeletalweight4ub_vertexbuffer = NULL;
7797 rsurface.batchskeletalweight4ub_bufferoffset = 0;
7798 rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
7799 rsurface.batchelement3i_indexbuffer = NULL;
7800 rsurface.batchelement3i_bufferoffset = 0;
7801 rsurface.batchelement3s = NULL;
7802 rsurface.batchelement3s_indexbuffer = NULL;
7803 rsurface.batchelement3s_bufferoffset = 0;
7804 rsurface.batchskeletaltransform3x4buffer = NULL;
7805 rsurface.batchskeletaltransform3x4offset = 0;
7806 rsurface.batchskeletaltransform3x4size = 0;
7807 // we'll only be setting up certain arrays as needed
7808 if (batchneed & BATCHNEED_ARRAY_VERTEX)
7809 rsurface.batchvertex3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7810 if (batchneed & BATCHNEED_ARRAY_NORMAL)
7811 rsurface.batchnormal3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7812 if (batchneed & BATCHNEED_ARRAY_VECTOR)
7814 rsurface.batchsvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7815 rsurface.batchtvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
7817 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
7818 rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
7819 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
7820 rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
7821 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
7822 rsurface.batchtexcoordlightmap2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
7823 if (batchneed & BATCHNEED_ARRAY_SKELETAL)
7825 rsurface.batchskeletalindex4ub = (unsigned char *)R_FrameData_Alloc(batchnumvertices * sizeof(unsigned char[4]));
7826 rsurface.batchskeletalweight4ub = (unsigned char *)R_FrameData_Alloc(batchnumvertices * sizeof(unsigned char[4]));
7830 for (i = 0;i < texturenumsurfaces;i++)
7832 surfacefirstvertex = texturesurfacelist[i]->num_firstvertex;
7833 surfacenumvertices = texturesurfacelist[i]->num_vertices;
7834 surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
7835 surfacenumtriangles = texturesurfacelist[i]->num_triangles;
7836 // copy only the data requested
7837 if (batchneed & (BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_ARRAY_LIGHTMAP))
7839 if (batchneed & BATCHNEED_ARRAY_VERTEX)
7841 if (rsurface.batchvertex3f)
7842 memcpy(rsurface.batchvertex3f + 3*numvertices, rsurface.modelvertex3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7844 memset(rsurface.batchvertex3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7846 if (batchneed & BATCHNEED_ARRAY_NORMAL)
7848 if (rsurface.modelnormal3f)
7849 memcpy(rsurface.batchnormal3f + 3*numvertices, rsurface.modelnormal3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7851 memset(rsurface.batchnormal3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7853 if (batchneed & BATCHNEED_ARRAY_VECTOR)
7855 if (rsurface.modelsvector3f)
7857 memcpy(rsurface.batchsvector3f + 3*numvertices, rsurface.modelsvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7858 memcpy(rsurface.batchtvector3f + 3*numvertices, rsurface.modeltvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
7862 memset(rsurface.batchsvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7863 memset(rsurface.batchtvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
7866 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
7868 if (rsurface.modellightmapcolor4f)
7869 memcpy(rsurface.batchlightmapcolor4f + 4*numvertices, rsurface.modellightmapcolor4f + 4*surfacefirstvertex, surfacenumvertices * sizeof(float[4]));
7871 memset(rsurface.batchlightmapcolor4f + 4*numvertices, 0, surfacenumvertices * sizeof(float[4]));
7873 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
7875 if (rsurface.modeltexcoordtexture2f)
7876 memcpy(rsurface.batchtexcoordtexture2f + 2*numvertices, rsurface.modeltexcoordtexture2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
7878 memset(rsurface.batchtexcoordtexture2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
7880 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
7882 if (rsurface.modeltexcoordlightmap2f)
7883 memcpy(rsurface.batchtexcoordlightmap2f + 2*numvertices, rsurface.modeltexcoordlightmap2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
7885 memset(rsurface.batchtexcoordlightmap2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
7887 if (batchneed & BATCHNEED_ARRAY_SKELETAL)
7889 if (rsurface.modelskeletalindex4ub)
7891 memcpy(rsurface.batchskeletalindex4ub + 4*numvertices, rsurface.modelskeletalindex4ub + 4*surfacefirstvertex, surfacenumvertices * sizeof(unsigned char[4]));
7892 memcpy(rsurface.batchskeletalweight4ub + 4*numvertices, rsurface.modelskeletalweight4ub + 4*surfacefirstvertex, surfacenumvertices * sizeof(unsigned char[4]));
7896 memset(rsurface.batchskeletalindex4ub + 4*numvertices, 0, surfacenumvertices * sizeof(unsigned char[4]));
7897 memset(rsurface.batchskeletalweight4ub + 4*numvertices, 0, surfacenumvertices * sizeof(unsigned char[4]));
7898 ub = rsurface.batchskeletalweight4ub + 4*numvertices;
7899 for (j = 0;j < surfacenumvertices;j++)
7904 RSurf_RenumberElements(rsurface.modelelement3i + 3*surfacefirsttriangle, rsurface.batchelement3i + 3*numtriangles, 3*surfacenumtriangles, numvertices - surfacefirstvertex);
7905 numvertices += surfacenumvertices;
7906 numtriangles += surfacenumtriangles;
7909 // generate a 16bit index array as well if possible
7910 // (in general, dynamic batches fit)
7911 if (numvertices <= 65536)
7913 rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
7914 for (i = 0;i < numtriangles*3;i++)
7915 rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
7918 // since we've copied everything, the batch now starts at 0
7919 rsurface.batchfirstvertex = 0;
7920 rsurface.batchnumvertices = batchnumvertices;
7921 rsurface.batchfirsttriangle = 0;
7922 rsurface.batchnumtriangles = batchnumtriangles;
7925 // apply skeletal animation that would have been done in the vertex shader
7926 if (rsurface.batchskeletaltransform3x4)
7928 const unsigned char *si;
7929 const unsigned char *sw;
7931 const float *b = rsurface.batchskeletaltransform3x4;
7932 float *vp, *vs, *vt, *vn;
7934 float m[3][4], n[3][4];
7935 float tp[3], ts[3], tt[3], tn[3];
7936 r_refdef.stats[r_stat_batch_dynamicskeletal_batches] += 1;
7937 r_refdef.stats[r_stat_batch_dynamicskeletal_surfaces] += batchnumsurfaces;
7938 r_refdef.stats[r_stat_batch_dynamicskeletal_vertices] += batchnumvertices;
7939 r_refdef.stats[r_stat_batch_dynamicskeletal_triangles] += batchnumtriangles;
7940 si = rsurface.batchskeletalindex4ub;
7941 sw = rsurface.batchskeletalweight4ub;
7942 vp = rsurface.batchvertex3f;
7943 vs = rsurface.batchsvector3f;
7944 vt = rsurface.batchtvector3f;
7945 vn = rsurface.batchnormal3f;
7946 memset(m[0], 0, sizeof(m));
7947 memset(n[0], 0, sizeof(n));
7948 for (i = 0;i < batchnumvertices;i++)
7950 t[0] = b + si[0]*12;
7953 // common case - only one matrix
7967 else if (sw[2] + sw[3])
7970 t[1] = b + si[1]*12;
7971 t[2] = b + si[2]*12;
7972 t[3] = b + si[3]*12;
7973 w[0] = sw[0] * (1.0f / 255.0f);
7974 w[1] = sw[1] * (1.0f / 255.0f);
7975 w[2] = sw[2] * (1.0f / 255.0f);
7976 w[3] = sw[3] * (1.0f / 255.0f);
7977 // blend the matrices
7978 m[0][0] = t[0][ 0] * w[0] + t[1][ 0] * w[1] + t[2][ 0] * w[2] + t[3][ 0] * w[3];
7979 m[0][1] = t[0][ 1] * w[0] + t[1][ 1] * w[1] + t[2][ 1] * w[2] + t[3][ 1] * w[3];
7980 m[0][2] = t[0][ 2] * w[0] + t[1][ 2] * w[1] + t[2][ 2] * w[2] + t[3][ 2] * w[3];
7981 m[0][3] = t[0][ 3] * w[0] + t[1][ 3] * w[1] + t[2][ 3] * w[2] + t[3][ 3] * w[3];
7982 m[1][0] = t[0][ 4] * w[0] + t[1][ 4] * w[1] + t[2][ 4] * w[2] + t[3][ 4] * w[3];
7983 m[1][1] = t[0][ 5] * w[0] + t[1][ 5] * w[1] + t[2][ 5] * w[2] + t[3][ 5] * w[3];
7984 m[1][2] = t[0][ 6] * w[0] + t[1][ 6] * w[1] + t[2][ 6] * w[2] + t[3][ 6] * w[3];
7985 m[1][3] = t[0][ 7] * w[0] + t[1][ 7] * w[1] + t[2][ 7] * w[2] + t[3][ 7] * w[3];
7986 m[2][0] = t[0][ 8] * w[0] + t[1][ 8] * w[1] + t[2][ 8] * w[2] + t[3][ 8] * w[3];
7987 m[2][1] = t[0][ 9] * w[0] + t[1][ 9] * w[1] + t[2][ 9] * w[2] + t[3][ 9] * w[3];
7988 m[2][2] = t[0][10] * w[0] + t[1][10] * w[1] + t[2][10] * w[2] + t[3][10] * w[3];
7989 m[2][3] = t[0][11] * w[0] + t[1][11] * w[1] + t[2][11] * w[2] + t[3][11] * w[3];
7994 t[1] = b + si[1]*12;
7995 w[0] = sw[0] * (1.0f / 255.0f);
7996 w[1] = sw[1] * (1.0f / 255.0f);
7997 // blend the matrices
7998 m[0][0] = t[0][ 0] * w[0] + t[1][ 0] * w[1];
7999 m[0][1] = t[0][ 1] * w[0] + t[1][ 1] * w[1];
8000 m[0][2] = t[0][ 2] * w[0] + t[1][ 2] * w[1];
8001 m[0][3] = t[0][ 3] * w[0] + t[1][ 3] * w[1];
8002 m[1][0] = t[0][ 4] * w[0] + t[1][ 4] * w[1];
8003 m[1][1] = t[0][ 5] * w[0] + t[1][ 5] * w[1];
8004 m[1][2] = t[0][ 6] * w[0] + t[1][ 6] * w[1];
8005 m[1][3] = t[0][ 7] * w[0] + t[1][ 7] * w[1];
8006 m[2][0] = t[0][ 8] * w[0] + t[1][ 8] * w[1];
8007 m[2][1] = t[0][ 9] * w[0] + t[1][ 9] * w[1];
8008 m[2][2] = t[0][10] * w[0] + t[1][10] * w[1];
8009 m[2][3] = t[0][11] * w[0] + t[1][11] * w[1];
8013 // modify the vertex
8015 vp[0] = tp[0] * m[0][0] + tp[1] * m[0][1] + tp[2] * m[0][2] + m[0][3];
8016 vp[1] = tp[0] * m[1][0] + tp[1] * m[1][1] + tp[2] * m[1][2] + m[1][3];
8017 vp[2] = tp[0] * m[2][0] + tp[1] * m[2][1] + tp[2] * m[2][2] + m[2][3];
8021 // the normal transformation matrix is a set of cross products...
8022 CrossProduct(m[1], m[2], n[0]);
8023 CrossProduct(m[2], m[0], n[1]);
8024 CrossProduct(m[0], m[1], n[2]); // is actually transpose(inverse(m)) * det(m)
8026 vn[0] = tn[0] * n[0][0] + tn[1] * n[0][1] + tn[2] * n[0][2];
8027 vn[1] = tn[0] * n[1][0] + tn[1] * n[1][1] + tn[2] * n[1][2];
8028 vn[2] = tn[0] * n[2][0] + tn[1] * n[2][1] + tn[2] * n[2][2];
8029 VectorNormalize(vn);
8034 vs[0] = ts[0] * n[0][0] + ts[1] * n[0][1] + ts[2] * n[0][2];
8035 vs[1] = ts[0] * n[1][0] + ts[1] * n[1][1] + ts[2] * n[1][2];
8036 vs[2] = ts[0] * n[2][0] + ts[1] * n[2][1] + ts[2] * n[2][2];
8037 VectorNormalize(vs);
8040 vt[0] = tt[0] * n[0][0] + tt[1] * n[0][1] + tt[2] * n[0][2];
8041 vt[1] = tt[0] * n[1][0] + tt[1] * n[1][1] + tt[2] * n[1][2];
8042 vt[2] = tt[0] * n[2][0] + tt[1] * n[2][1] + tt[2] * n[2][2];
8043 VectorNormalize(vt);
8048 rsurface.batchskeletaltransform3x4 = NULL;
8049 rsurface.batchskeletalnumtransforms = 0;
8052 // q1bsp surfaces rendered in vertex color mode have to have colors
8053 // calculated based on lightstyles
8054 if ((batchneed & BATCHNEED_ARRAY_VERTEXCOLOR) && texturesurfacelist[0]->lightmapinfo)
8056 // generate color arrays for the surfaces in this list
8061 const unsigned char *lm;
8062 rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
8063 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
8064 rsurface.batchlightmapcolor4f_bufferoffset = 0;
8066 for (i = 0;i < texturenumsurfaces;i++)
8068 surface = texturesurfacelist[i];
8069 offsets = rsurface.modellightmapoffsets + surface->num_firstvertex;
8070 surfacenumvertices = surface->num_vertices;
8071 if (surface->lightmapinfo->samples)
8073 for (j = 0;j < surfacenumvertices;j++)
8075 lm = surface->lightmapinfo->samples + offsets[j];
8076 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[0]];
8077 VectorScale(lm, scale, c);
8078 if (surface->lightmapinfo->styles[1] != 255)
8080 size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
8082 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[1]];
8083 VectorMA(c, scale, lm, c);
8084 if (surface->lightmapinfo->styles[2] != 255)
8087 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[2]];
8088 VectorMA(c, scale, lm, c);
8089 if (surface->lightmapinfo->styles[3] != 255)
8092 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[3]];
8093 VectorMA(c, scale, lm, c);
8100 Vector4Set(rsurface.batchlightmapcolor4f + 4*numvertices, min(c[0], 255) * (1.0f / 255.0f), min(c[1], 255) * (1.0f / 255.0f), min(c[2], 255) * (1.0f / 255.0f), 1);
8106 for (j = 0;j < surfacenumvertices;j++)
8108 Vector4Set(rsurface.batchlightmapcolor4f + 4*numvertices, 0, 0, 0, 1);
8115 // if vertices are deformed (sprite flares and things in maps, possibly
8116 // water waves, bulges and other deformations), modify the copied vertices
8118 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
8121 switch (deform->deform)
8124 case Q3DEFORM_PROJECTIONSHADOW:
8125 case Q3DEFORM_TEXT0:
8126 case Q3DEFORM_TEXT1:
8127 case Q3DEFORM_TEXT2:
8128 case Q3DEFORM_TEXT3:
8129 case Q3DEFORM_TEXT4:
8130 case Q3DEFORM_TEXT5:
8131 case Q3DEFORM_TEXT6:
8132 case Q3DEFORM_TEXT7:
8135 case Q3DEFORM_AUTOSPRITE:
8136 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
8137 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
8138 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
8139 VectorNormalize(newforward);
8140 VectorNormalize(newright);
8141 VectorNormalize(newup);
8142 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8143 // rsurface.batchvertex3f_vertexbuffer = NULL;
8144 // rsurface.batchvertex3f_bufferoffset = 0;
8145 // rsurface.batchsvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchsvector3f);
8146 // rsurface.batchsvector3f_vertexbuffer = NULL;
8147 // rsurface.batchsvector3f_bufferoffset = 0;
8148 // rsurface.batchtvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchtvector3f);
8149 // rsurface.batchtvector3f_vertexbuffer = NULL;
8150 // rsurface.batchtvector3f_bufferoffset = 0;
8151 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8152 // rsurface.batchnormal3f_vertexbuffer = NULL;
8153 // rsurface.batchnormal3f_bufferoffset = 0;
8154 // sometimes we're on a renderpath that does not use vectors (GL11/GL13/GLES1)
8155 if (!VectorLength2(rsurface.batchnormal3f + 3*rsurface.batchfirstvertex))
8156 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8157 if (!VectorLength2(rsurface.batchsvector3f + 3*rsurface.batchfirstvertex))
8158 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8159 // a single autosprite surface can contain multiple sprites...
8160 for (j = 0;j < batchnumvertices - 3;j += 4)
8162 VectorClear(center);
8163 for (i = 0;i < 4;i++)
8164 VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
8165 VectorScale(center, 0.25f, center);
8166 VectorCopy(rsurface.batchnormal3f + 3*j, forward);
8167 VectorCopy(rsurface.batchsvector3f + 3*j, right);
8168 VectorCopy(rsurface.batchtvector3f + 3*j, up);
8169 for (i = 0;i < 4;i++)
8171 VectorSubtract(rsurface.batchvertex3f + 3*(j+i), center, v);
8172 VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.batchvertex3f + 3*(j+i));
8175 // if we get here, BATCHNEED_ARRAY_NORMAL and BATCHNEED_ARRAY_VECTOR are in batchneed, so no need to check
8176 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8177 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8179 case Q3DEFORM_AUTOSPRITE2:
8180 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
8181 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
8182 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
8183 VectorNormalize(newforward);
8184 VectorNormalize(newright);
8185 VectorNormalize(newup);
8186 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8187 // rsurface.batchvertex3f_vertexbuffer = NULL;
8188 // rsurface.batchvertex3f_bufferoffset = 0;
8190 const float *v1, *v2;
8200 memset(shortest, 0, sizeof(shortest));
8201 // a single autosprite surface can contain multiple sprites...
8202 for (j = 0;j < batchnumvertices - 3;j += 4)
8204 VectorClear(center);
8205 for (i = 0;i < 4;i++)
8206 VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
8207 VectorScale(center, 0.25f, center);
8208 // find the two shortest edges, then use them to define the
8209 // axis vectors for rotating around the central axis
8210 for (i = 0;i < 6;i++)
8212 v1 = rsurface.batchvertex3f + 3*(j+quadedges[i][0]);
8213 v2 = rsurface.batchvertex3f + 3*(j+quadedges[i][1]);
8214 l = VectorDistance2(v1, v2);
8215 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
8217 l += (1.0f / 1024.0f);
8218 if (shortest[0].length2 > l || i == 0)
8220 shortest[1] = shortest[0];
8221 shortest[0].length2 = l;
8222 shortest[0].v1 = v1;
8223 shortest[0].v2 = v2;
8225 else if (shortest[1].length2 > l || i == 1)
8227 shortest[1].length2 = l;
8228 shortest[1].v1 = v1;
8229 shortest[1].v2 = v2;
8232 VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
8233 VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
8234 // this calculates the right vector from the shortest edge
8235 // and the up vector from the edge midpoints
8236 VectorSubtract(shortest[0].v1, shortest[0].v2, right);
8237 VectorNormalize(right);
8238 VectorSubtract(end, start, up);
8239 VectorNormalize(up);
8240 // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
8241 VectorSubtract(rsurface.localvieworigin, center, forward);
8242 //Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, forward);
8243 VectorNegate(forward, forward);
8244 VectorReflect(forward, 0, up, forward);
8245 VectorNormalize(forward);
8246 CrossProduct(up, forward, newright);
8247 VectorNormalize(newright);
8248 // rotate the quad around the up axis vector, this is made
8249 // especially easy by the fact we know the quad is flat,
8250 // so we only have to subtract the center position and
8251 // measure distance along the right vector, and then
8252 // multiply that by the newright vector and add back the
8254 // we also need to subtract the old position to undo the
8255 // displacement from the center, which we do with a
8256 // DotProduct, the subtraction/addition of center is also
8257 // optimized into DotProducts here
8258 l = DotProduct(right, center);
8259 for (i = 0;i < 4;i++)
8261 v1 = rsurface.batchvertex3f + 3*(j+i);
8262 f = DotProduct(right, v1) - l;
8263 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.batchvertex3f + 3*(j+i));
8267 if(batchneed & (BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR)) // otherwise these can stay NULL
8269 // rsurface.batchnormal3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8270 // rsurface.batchnormal3f_vertexbuffer = NULL;
8271 // rsurface.batchnormal3f_bufferoffset = 0;
8272 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8274 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8276 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8277 // rsurface.batchsvector3f_vertexbuffer = NULL;
8278 // rsurface.batchsvector3f_bufferoffset = 0;
8279 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8280 // rsurface.batchtvector3f_vertexbuffer = NULL;
8281 // rsurface.batchtvector3f_bufferoffset = 0;
8282 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8285 case Q3DEFORM_NORMAL:
8286 // deform the normals to make reflections wavey
8287 rsurface.batchnormal3f = (float *)R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8288 rsurface.batchnormal3f_vertexbuffer = NULL;
8289 rsurface.batchnormal3f_bufferoffset = 0;
8290 for (j = 0;j < batchnumvertices;j++)
8293 float *normal = rsurface.batchnormal3f + 3*j;
8294 VectorScale(rsurface.batchvertex3f + 3*j, 0.98f, vertex);
8295 normal[0] = rsurface.batchnormal3f[j*3+0] + deform->parms[0] * noise4f( vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8296 normal[1] = rsurface.batchnormal3f[j*3+1] + deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8297 normal[2] = rsurface.batchnormal3f[j*3+2] + deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
8298 VectorNormalize(normal);
8300 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8302 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8303 // rsurface.batchsvector3f_vertexbuffer = NULL;
8304 // rsurface.batchsvector3f_bufferoffset = 0;
8305 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8306 // rsurface.batchtvector3f_vertexbuffer = NULL;
8307 // rsurface.batchtvector3f_bufferoffset = 0;
8308 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8312 // deform vertex array to make wavey water and flags and such
8313 waveparms[0] = deform->waveparms[0];
8314 waveparms[1] = deform->waveparms[1];
8315 waveparms[2] = deform->waveparms[2];
8316 waveparms[3] = deform->waveparms[3];
8317 if(!R_TestQ3WaveFunc(deform->wavefunc, waveparms))
8318 break; // if wavefunc is a nop, don't make a dynamic vertex array
8319 // this is how a divisor of vertex influence on deformation
8320 animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
8321 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
8322 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8323 // rsurface.batchvertex3f_vertexbuffer = NULL;
8324 // rsurface.batchvertex3f_bufferoffset = 0;
8325 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8326 // rsurface.batchnormal3f_vertexbuffer = NULL;
8327 // rsurface.batchnormal3f_bufferoffset = 0;
8328 for (j = 0;j < batchnumvertices;j++)
8330 // if the wavefunc depends on time, evaluate it per-vertex
8333 waveparms[2] = deform->waveparms[2] + (rsurface.batchvertex3f[j*3+0] + rsurface.batchvertex3f[j*3+1] + rsurface.batchvertex3f[j*3+2]) * animpos;
8334 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
8336 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
8338 // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
8339 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8340 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8342 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8343 // rsurface.batchsvector3f_vertexbuffer = NULL;
8344 // rsurface.batchsvector3f_bufferoffset = 0;
8345 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8346 // rsurface.batchtvector3f_vertexbuffer = NULL;
8347 // rsurface.batchtvector3f_bufferoffset = 0;
8348 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8351 case Q3DEFORM_BULGE:
8352 // deform vertex array to make the surface have moving bulges
8353 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8354 // rsurface.batchvertex3f_vertexbuffer = NULL;
8355 // rsurface.batchvertex3f_bufferoffset = 0;
8356 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
8357 // rsurface.batchnormal3f_vertexbuffer = NULL;
8358 // rsurface.batchnormal3f_bufferoffset = 0;
8359 for (j = 0;j < batchnumvertices;j++)
8361 scale = sin(rsurface.batchtexcoordtexture2f[j*2+0] * deform->parms[0] + rsurface.shadertime * deform->parms[2]) * deform->parms[1];
8362 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
8364 // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
8365 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
8366 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
8368 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8369 // rsurface.batchsvector3f_vertexbuffer = NULL;
8370 // rsurface.batchsvector3f_bufferoffset = 0;
8371 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8372 // rsurface.batchtvector3f_vertexbuffer = NULL;
8373 // rsurface.batchtvector3f_bufferoffset = 0;
8374 Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchsvector3f, rsurface.batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
8378 // deform vertex array
8379 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
8380 break; // if wavefunc is a nop, don't make a dynamic vertex array
8381 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
8382 VectorScale(deform->parms, scale, waveparms);
8383 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8384 // rsurface.batchvertex3f_vertexbuffer = NULL;
8385 // rsurface.batchvertex3f_bufferoffset = 0;
8386 for (j = 0;j < batchnumvertices;j++)
8387 VectorAdd(rsurface.batchvertex3f + 3*j, waveparms, rsurface.batchvertex3f + 3*j);
8392 if (rsurface.batchtexcoordtexture2f && rsurface.texture->materialshaderpass)
8394 // generate texcoords based on the chosen texcoord source
8395 switch(rsurface.texture->materialshaderpass->tcgen.tcgen)
8398 case Q3TCGEN_TEXTURE:
8400 case Q3TCGEN_LIGHTMAP:
8401 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8402 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8403 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8404 if (rsurface.batchtexcoordlightmap2f)
8405 memcpy(rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordlightmap2f, batchnumvertices * sizeof(float[2]));
8407 case Q3TCGEN_VECTOR:
8408 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8409 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8410 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8411 for (j = 0;j < batchnumvertices;j++)
8413 rsurface.batchtexcoordtexture2f[j*2+0] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->materialshaderpass->tcgen.parms);
8414 rsurface.batchtexcoordtexture2f[j*2+1] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->materialshaderpass->tcgen.parms + 3);
8417 case Q3TCGEN_ENVIRONMENT:
8418 // make environment reflections using a spheremap
8419 rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8420 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8421 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8422 for (j = 0;j < batchnumvertices;j++)
8424 // identical to Q3A's method, but executed in worldspace so
8425 // carried models can be shiny too
8427 float viewer[3], d, reflected[3], worldreflected[3];
8429 VectorSubtract(rsurface.localvieworigin, rsurface.batchvertex3f + 3*j, viewer);
8430 // VectorNormalize(viewer);
8432 d = DotProduct(rsurface.batchnormal3f + 3*j, viewer);
8434 reflected[0] = rsurface.batchnormal3f[j*3+0]*2*d - viewer[0];
8435 reflected[1] = rsurface.batchnormal3f[j*3+1]*2*d - viewer[1];
8436 reflected[2] = rsurface.batchnormal3f[j*3+2]*2*d - viewer[2];
8437 // note: this is proportinal to viewer, so we can normalize later
8439 Matrix4x4_Transform3x3(&rsurface.matrix, reflected, worldreflected);
8440 VectorNormalize(worldreflected);
8442 // note: this sphere map only uses world x and z!
8443 // so positive and negative y will LOOK THE SAME.
8444 rsurface.batchtexcoordtexture2f[j*2+0] = 0.5 + 0.5 * worldreflected[1];
8445 rsurface.batchtexcoordtexture2f[j*2+1] = 0.5 - 0.5 * worldreflected[2];
8449 // the only tcmod that needs software vertex processing is turbulent, so
8450 // check for it here and apply the changes if needed
8451 // and we only support that as the first one
8452 // (handling a mixture of turbulent and other tcmods would be problematic
8453 // without punting it entirely to a software path)
8454 if (rsurface.texture->materialshaderpass->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
8456 amplitude = rsurface.texture->materialshaderpass->tcmods[0].parms[1];
8457 animpos = rsurface.texture->materialshaderpass->tcmods[0].parms[2] + rsurface.shadertime * rsurface.texture->materialshaderpass->tcmods[0].parms[3];
8458 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8459 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8460 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8461 for (j = 0;j < batchnumvertices;j++)
8463 rsurface.batchtexcoordtexture2f[j*2+0] += amplitude * sin(((rsurface.batchvertex3f[j*3+0] + rsurface.batchvertex3f[j*3+2]) * 1.0 / 1024.0f + animpos) * M_PI * 2);
8464 rsurface.batchtexcoordtexture2f[j*2+1] += amplitude * sin(((rsurface.batchvertex3f[j*3+1] ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
8470 void RSurf_DrawBatch(void)
8472 // sometimes a zero triangle surface (usually a degenerate patch) makes it
8473 // through the pipeline, killing it earlier in the pipeline would have
8474 // per-surface overhead rather than per-batch overhead, so it's best to
8475 // reject it here, before it hits glDraw.
8476 if (rsurface.batchnumtriangles == 0)
8479 // batch debugging code
8480 if (r_test.integer && rsurface.entity == r_refdef.scene.worldentity && rsurface.batchvertex3f == r_refdef.scene.worldentity->model->surfmesh.data_vertex3f)
8486 e = rsurface.batchelement3i + rsurface.batchfirsttriangle*3;
8487 for (i = 0;i < rsurface.batchnumtriangles*3;i++)
8490 for (j = 0;j < rsurface.entity->model->num_surfaces;j++)
8492 if (c >= rsurface.modelsurfaces[j].num_firstvertex && c < (rsurface.modelsurfaces[j].num_firstvertex + rsurface.modelsurfaces[j].num_vertices))
8494 if (rsurface.modelsurfaces[j].texture != rsurface.texture)
8495 Sys_Error("RSurf_DrawBatch: index %i uses different texture (%s) than surface %i which it belongs to (which uses %s)\n", c, rsurface.texture->name, j, rsurface.modelsurfaces[j].texture->name);
8502 if (rsurface.batchmultidraw)
8504 // issue multiple draws rather than copying index data
8505 int numsurfaces = rsurface.batchmultidrawnumsurfaces;
8506 const msurface_t **surfacelist = rsurface.batchmultidrawsurfacelist;
8507 int i, j, k, firstvertex, endvertex, firsttriangle, endtriangle;
8508 for (i = 0;i < numsurfaces;)
8510 // combine consecutive surfaces as one draw
8511 for (k = i, j = i + 1;j < numsurfaces;k = j, j++)
8512 if (surfacelist[j] != surfacelist[k] + 1)
8514 firstvertex = surfacelist[i]->num_firstvertex;
8515 endvertex = surfacelist[k]->num_firstvertex + surfacelist[k]->num_vertices;
8516 firsttriangle = surfacelist[i]->num_firsttriangle;
8517 endtriangle = surfacelist[k]->num_firsttriangle + surfacelist[k]->num_triangles;
8518 R_Mesh_Draw(firstvertex, endvertex - firstvertex, firsttriangle, endtriangle - firsttriangle, rsurface.batchelement3i, rsurface.batchelement3i_indexbuffer, rsurface.batchelement3i_bufferoffset, rsurface.batchelement3s, rsurface.batchelement3s_indexbuffer, rsurface.batchelement3s_bufferoffset);
8524 // there is only one consecutive run of index data (may have been combined)
8525 R_Mesh_Draw(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchfirsttriangle, rsurface.batchnumtriangles, rsurface.batchelement3i, rsurface.batchelement3i_indexbuffer, rsurface.batchelement3i_bufferoffset, rsurface.batchelement3s, rsurface.batchelement3s_indexbuffer, rsurface.batchelement3s_bufferoffset);
8529 static int RSurf_FindWaterPlaneForSurface(const msurface_t *surface)
8531 // pick the closest matching water plane
8532 int planeindex, vertexindex, bestplaneindex = -1;
8536 r_waterstate_waterplane_t *p;
8537 qbool prepared = false;
8539 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
8541 if(p->camera_entity != rsurface.texture->camera_entity)
8546 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX, 1, &surface);
8548 if(rsurface.batchnumvertices == 0)
8551 for (vertexindex = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3;vertexindex < rsurface.batchnumvertices;vertexindex++, v += 3)
8553 Matrix4x4_Transform(&rsurface.matrix, v, vert);
8554 d += fabs(PlaneDiff(vert, &p->plane));
8556 if (bestd > d || bestplaneindex < 0)
8559 bestplaneindex = planeindex;
8562 return bestplaneindex;
8563 // NOTE: this MAY return a totally unrelated water plane; we can ignore
8564 // this situation though, as it might be better to render single larger
8565 // batches with useless stuff (backface culled for example) than to
8566 // render multiple smaller batches
8569 void RSurf_SetupDepthAndCulling(void)
8571 // submodels are biased to avoid z-fighting with world surfaces that they
8572 // may be exactly overlapping (avoids z-fighting artifacts on certain
8573 // doors and things in Quake maps)
8574 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
8575 GL_PolygonOffset(rsurface.basepolygonfactor + rsurface.texture->biaspolygonfactor, rsurface.basepolygonoffset + rsurface.texture->biaspolygonoffset);
8576 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
8577 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
8580 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8584 float p[3], mins[3], maxs[3];
8586 // transparent sky would be ridiculous
8587 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
8589 R_SetupShader_Generic_NoTexture(false, false);
8590 skyrenderlater = true;
8591 RSurf_SetupDepthAndCulling();
8594 // add the vertices of the surfaces to a world bounding box so we can scissor the sky render later
8595 if (r_sky_scissor.integer)
8597 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
8598 for (j = 0, v = rsurface.batchvertex3f + 3 * rsurface.batchfirstvertex; j < rsurface.batchnumvertices; j++, v += 3)
8600 Matrix4x4_Transform(&rsurface.matrix, v, p);
8603 if (mins[0] > p[0]) mins[0] = p[0];
8604 if (mins[1] > p[1]) mins[1] = p[1];
8605 if (mins[2] > p[2]) mins[2] = p[2];
8606 if (maxs[0] < p[0]) maxs[0] = p[0];
8607 if (maxs[1] < p[1]) maxs[1] = p[1];
8608 if (maxs[2] < p[2]) maxs[2] = p[2];
8612 VectorCopy(p, mins);
8613 VectorCopy(p, maxs);
8616 if (!R_ScissorForBBox(mins, maxs, scissor))
8620 if (skyscissor[0] > scissor[0])
8622 skyscissor[2] += skyscissor[0] - scissor[0];
8623 skyscissor[0] = scissor[0];
8625 if (skyscissor[1] > scissor[1])
8627 skyscissor[3] += skyscissor[1] - scissor[1];
8628 skyscissor[1] = scissor[1];
8630 if (skyscissor[0] + skyscissor[2] < scissor[0] + scissor[2])
8631 skyscissor[2] = scissor[0] + scissor[2] - skyscissor[0];
8632 if (skyscissor[1] + skyscissor[3] < scissor[1] + scissor[3])
8633 skyscissor[3] = scissor[1] + scissor[3] - skyscissor[1];
8636 Vector4Copy(scissor, skyscissor);
8640 // LadyHavoc: HalfLife maps have freaky skypolys so don't use
8641 // skymasking on them, and Quake3 never did sky masking (unlike
8642 // software Quake and software Quake2), so disable the sky masking
8643 // in Quake3 maps as it causes problems with q3map2 sky tricks,
8644 // and skymasking also looks very bad when noclipping outside the
8645 // level, so don't use it then either.
8646 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.skymasking && (r_refdef.scene.worldmodel->brush.isq3bsp ? r_q3bsp_renderskydepth.integer : r_q1bsp_skymasking.integer) && !r_refdef.viewcache.world_novis && !r_trippy.integer)
8648 R_Mesh_ResetTextureState();
8649 if (skyrendermasked)
8651 R_SetupShader_DepthOrShadow(false, false, false);
8652 // depth-only (masking)
8653 GL_ColorMask(0, 0, 0, 0);
8654 // just to make sure that braindead drivers don't draw
8655 // anything despite that colormask...
8656 GL_BlendFunc(GL_ZERO, GL_ONE);
8657 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8658 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8662 R_SetupShader_Generic_NoTexture(false, false);
8664 GL_BlendFunc(GL_ONE, GL_ZERO);
8665 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
8666 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1);
8667 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
8670 if (skyrendermasked)
8671 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
8673 R_Mesh_ResetTextureState();
8674 GL_Color(1, 1, 1, 1);
8677 extern rtexture_t *r_shadow_prepasslightingdiffusetexture;
8678 extern rtexture_t *r_shadow_prepasslightingspeculartexture;
8679 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool prepass, qbool ui)
8681 if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA)))
8685 // render screenspace normalmap to texture
8687 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_DEFERREDGEOMETRY, texturenumsurfaces, texturesurfacelist, NULL, false, false);
8692 // bind lightmap texture
8694 // water/refraction/reflection/camera surfaces have to be handled specially
8695 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA | MATERIALFLAG_REFLECTION)))
8697 int start, end, startplaneindex;
8698 for (start = 0;start < texturenumsurfaces;start = end)
8700 startplaneindex = RSurf_FindWaterPlaneForSurface(texturesurfacelist[start]);
8701 if(startplaneindex < 0)
8703 // this happens if the plane e.g. got backface culled and thus didn't get a water plane. We can just ignore this.
8704 // Con_Printf("No matching water plane for surface with material flags 0x%08x - PLEASE DEBUG THIS\n", rsurface.texture->currentmaterialflags);
8708 for (end = start + 1;end < texturenumsurfaces && startplaneindex == RSurf_FindWaterPlaneForSurface(texturesurfacelist[end]);end++)
8710 // now that we have a batch using the same planeindex, render it
8711 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA)))
8713 // render water or distortion background
8715 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BACKGROUND, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false, false);
8717 // blend surface on top
8718 GL_DepthMask(false);
8719 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, end-start, texturesurfacelist + start, NULL, false, false);
8722 else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION))
8724 // render surface with reflection texture as input
8725 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
8726 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false, false);
8733 // render surface batch normally
8734 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
8735 R_SetupShader_Surface(vec3_origin, vec3_origin, vec3_origin, RSURFPASS_BASE, texturenumsurfaces, texturesurfacelist, NULL, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) != 0 || ui, ui);
8739 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth)
8743 int texturesurfaceindex;
8745 const msurface_t *surface;
8746 float surfacecolor4f[4];
8748 // R_Mesh_ResetTextureState();
8749 R_SetupShader_Generic_NoTexture(false, false);
8751 GL_BlendFunc(GL_ONE, GL_ZERO);
8752 GL_DepthMask(writedepth);
8754 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_ALWAYSCOPY, texturenumsurfaces, texturesurfacelist);
8756 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
8758 surface = texturesurfacelist[texturesurfaceindex];
8759 k = (int)(((size_t)surface) / sizeof(msurface_t));
8760 Vector4Set(surfacecolor4f, (k & 0xF) * (1.0f / 16.0f), (k & 0xF0) * (1.0f / 256.0f), (k & 0xF00) * (1.0f / 4096.0f), 1);
8761 for (j = 0;j < surface->num_vertices;j++)
8763 Vector4Copy(surfacecolor4f, rsurface.batchlightmapcolor4f + 4 * vi);
8767 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchlightmapcolor4f, rsurface.batchtexcoordtexture2f);
8771 static void R_DrawModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool prepass, qbool ui)
8774 RSurf_SetupDepthAndCulling();
8775 if (r_showsurfaces.integer && r_refdef.view.showdebug)
8777 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist, writedepth);
8780 switch (vid.renderpath)
8782 case RENDERPATH_GL32:
8783 case RENDERPATH_GLES2:
8784 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8790 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
8793 int texturenumsurfaces, endsurface;
8795 const msurface_t *surface;
8796 const msurface_t *texturesurfacelist[MESHQUEUE_TRANSPARENT_BATCHSIZE];
8798 RSurf_ActiveModelEntity(ent, true, true, false);
8800 if (r_transparentdepthmasking.integer)
8802 qbool setup = false;
8803 for (i = 0;i < numsurfaces;i = j)
8806 surface = rsurface.modelsurfaces + surfacelist[i];
8807 texture = surface->texture;
8808 rsurface.texture = R_GetCurrentTexture(texture);
8809 rsurface.lightmaptexture = NULL;
8810 rsurface.deluxemaptexture = NULL;
8811 rsurface.uselightmaptexture = false;
8812 // scan ahead until we find a different texture
8813 endsurface = min(i + 1024, numsurfaces);
8814 texturenumsurfaces = 0;
8815 texturesurfacelist[texturenumsurfaces++] = surface;
8816 for (;j < endsurface;j++)
8818 surface = rsurface.modelsurfaces + surfacelist[j];
8819 if (texture != surface->texture)
8821 texturesurfacelist[texturenumsurfaces++] = surface;
8823 if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_TRANSDEPTH))
8825 // render the range of surfaces as depth
8829 GL_ColorMask(0,0,0,0);
8832 GL_BlendFunc(GL_ONE, GL_ZERO);
8834 // R_Mesh_ResetTextureState();
8836 RSurf_SetupDepthAndCulling();
8837 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8838 R_SetupShader_DepthOrShadow(false, false, !!rsurface.batchskeletaltransform3x4);
8839 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8843 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
8846 for (i = 0;i < numsurfaces;i = j)
8849 surface = rsurface.modelsurfaces + surfacelist[i];
8850 texture = surface->texture;
8851 rsurface.texture = R_GetCurrentTexture(texture);
8852 // scan ahead until we find a different texture
8853 endsurface = min(i + MESHQUEUE_TRANSPARENT_BATCHSIZE, numsurfaces);
8854 texturenumsurfaces = 0;
8855 texturesurfacelist[texturenumsurfaces++] = surface;
8856 rsurface.lightmaptexture = surface->lightmaptexture;
8857 rsurface.deluxemaptexture = surface->deluxemaptexture;
8858 rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
8859 for (;j < endsurface;j++)
8861 surface = rsurface.modelsurfaces + surfacelist[j];
8862 if (texture != surface->texture || rsurface.lightmaptexture != surface->lightmaptexture)
8864 texturesurfacelist[texturenumsurfaces++] = surface;
8866 // render the range of surfaces
8867 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, false, false, false);
8869 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
8872 static void R_ProcessTransparentTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8874 // transparent surfaces get pushed off into the transparent queue
8875 int surfacelistindex;
8876 const msurface_t *surface;
8877 vec3_t tempcenter, center;
8878 for (surfacelistindex = 0;surfacelistindex < texturenumsurfaces;surfacelistindex++)
8880 surface = texturesurfacelist[surfacelistindex];
8881 if (r_transparent_sortsurfacesbynearest.integer)
8883 tempcenter[0] = bound(surface->mins[0], rsurface.localvieworigin[0], surface->maxs[0]);
8884 tempcenter[1] = bound(surface->mins[1], rsurface.localvieworigin[1], surface->maxs[1]);
8885 tempcenter[2] = bound(surface->mins[2], rsurface.localvieworigin[2], surface->maxs[2]);
8889 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
8890 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
8891 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
8893 Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
8894 if (rsurface.entity->transparent_offset) // transparent offset
8896 center[0] += r_refdef.view.forward[0]*rsurface.entity->transparent_offset;
8897 center[1] += r_refdef.view.forward[1]*rsurface.entity->transparent_offset;
8898 center[2] += r_refdef.view.forward[2]*rsurface.entity->transparent_offset;
8900 R_MeshQueue_AddTransparent((rsurface.entity->flags & RENDER_WORLDOBJECT) ? TRANSPARENTSORT_SKY : (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) ? TRANSPARENTSORT_HUD : rsurface.texture->transparentsort, center, R_DrawSurface_TransparentCallback, rsurface.entity, surface - rsurface.modelsurfaces, rsurface.rtlight);
8904 static void R_DrawTextureSurfaceList_DepthOnly(int texturenumsurfaces, const msurface_t **texturesurfacelist)
8906 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
8908 if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
8910 RSurf_SetupDepthAndCulling();
8911 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ALLOWMULTIDRAW, texturenumsurfaces, texturesurfacelist);
8912 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
8913 R_SetupShader_DepthOrShadow(false, false, !!rsurface.batchskeletaltransform3x4);
8917 static void R_ProcessModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qbool writedepth, qbool depthonly, qbool prepass, qbool ui)
8921 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8923 R_DrawTextureSurfaceList_DepthOnly(texturenumsurfaces, texturesurfacelist);
8926 if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_WALL))
8928 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
8929 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
8931 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass, ui);
8933 else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) && (!r_showsurfaces.integer || r_showsurfaces.integer == 3))
8934 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
8935 else if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_WALL))
8937 else if (((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED) || (r_showsurfaces.integer == 3 && (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST))))
8939 // in the deferred case, transparent surfaces were queued during prepass
8940 if (!r_shadow_usingdeferredprepass)
8941 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
8945 // the alphatest check is to make sure we write depth for anything we skipped on the depth-only pass earlier
8946 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST), prepass, ui);
8951 static void R_QueueModelSurfaceList(entity_render_t *ent, int numsurfaces, const msurface_t **surfacelist, int flagsmask, qbool writedepth, qbool depthonly, qbool prepass, qbool ui)
8955 R_FrameData_SetMark();
8956 // break the surface list down into batches by texture and use of lightmapping
8957 for (i = 0;i < numsurfaces;i = j)
8960 // texture is the base texture pointer, rsurface.texture is the
8961 // current frame/skin the texture is directing us to use (for example
8962 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
8963 // use skin 1 instead)
8964 texture = surfacelist[i]->texture;
8965 rsurface.texture = R_GetCurrentTexture(texture);
8966 if (!(rsurface.texture->currentmaterialflags & flagsmask) || (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW))
8968 // if this texture is not the kind we want, skip ahead to the next one
8969 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
8973 if(depthonly || prepass)
8975 rsurface.lightmaptexture = NULL;
8976 rsurface.deluxemaptexture = NULL;
8977 rsurface.uselightmaptexture = false;
8978 // simply scan ahead until we find a different texture or lightmap state
8979 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
8984 rsurface.lightmaptexture = surfacelist[i]->lightmaptexture;
8985 rsurface.deluxemaptexture = surfacelist[i]->deluxemaptexture;
8986 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
8987 // simply scan ahead until we find a different texture or lightmap state
8988 for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.lightmaptexture == surfacelist[j]->lightmaptexture;j++)
8991 // render the range of surfaces
8992 R_ProcessModelTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly, prepass, ui);
8994 R_FrameData_ReturnToMark();
8997 float locboxvertex3f[6*4*3] =
8999 1,0,1, 1,0,0, 1,1,0, 1,1,1,
9000 0,1,1, 0,1,0, 0,0,0, 0,0,1,
9001 1,1,1, 1,1,0, 0,1,0, 0,1,1,
9002 0,0,1, 0,0,0, 1,0,0, 1,0,1,
9003 0,0,1, 1,0,1, 1,1,1, 0,1,1,
9004 1,0,0, 0,0,0, 0,1,0, 1,1,0
9007 unsigned short locboxelements[6*2*3] =
9017 static void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
9020 cl_locnode_t *loc = (cl_locnode_t *)ent;
9022 float vertex3f[6*4*3];
9024 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9025 GL_DepthMask(false);
9026 GL_DepthRange(0, 1);
9027 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
9029 GL_CullFace(GL_NONE);
9030 R_EntityMatrix(&identitymatrix);
9032 // R_Mesh_ResetTextureState();
9035 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9036 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9037 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_refdef.view.colorscale,
9038 surfacelist[0] < 0 ? 0.5f : 0.125f);
9040 if (VectorCompare(loc->mins, loc->maxs))
9042 VectorSet(size, 2, 2, 2);
9043 VectorMA(loc->mins, -0.5f, size, mins);
9047 VectorCopy(loc->mins, mins);
9048 VectorSubtract(loc->maxs, loc->mins, size);
9051 for (i = 0;i < 6*4*3;)
9052 for (j = 0;j < 3;j++, i++)
9053 vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
9055 R_Mesh_PrepareVertices_Generic_Arrays(6*4, vertex3f, NULL, NULL);
9056 R_SetupShader_Generic_NoTexture(false, false);
9057 R_Mesh_Draw(0, 6*4, 0, 6*2, NULL, NULL, 0, locboxelements, NULL, 0);
9060 void R_DrawLocs(void)
9063 cl_locnode_t *loc, *nearestloc;
9065 nearestloc = CL_Locs_FindNearest(cl.movement_origin);
9066 for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
9068 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
9069 R_MeshQueue_AddTransparent(TRANSPARENTSORT_DISTANCE, center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
9073 void R_DecalSystem_Reset(decalsystem_t *decalsystem)
9075 if (decalsystem->decals)
9076 Mem_Free(decalsystem->decals);
9077 memset(decalsystem, 0, sizeof(*decalsystem));
9080 static void R_DecalSystem_SpawnTriangle(decalsystem_t *decalsystem, const float *v0, const float *v1, const float *v2, const float *t0, const float *t1, const float *t2, const float *c0, const float *c1, const float *c2, int triangleindex, int surfaceindex, unsigned int decalsequence)
9086 // expand or initialize the system
9087 if (decalsystem->maxdecals <= decalsystem->numdecals)
9089 decalsystem_t old = *decalsystem;
9090 qbool useshortelements;
9091 decalsystem->maxdecals = max(16, decalsystem->maxdecals * 2);
9092 useshortelements = decalsystem->maxdecals * 3 <= 65536;
9093 decalsystem->decals = (tridecal_t *)Mem_Alloc(cls.levelmempool, decalsystem->maxdecals * (sizeof(tridecal_t) + sizeof(float[3][3]) + sizeof(float[3][2]) + sizeof(float[3][4]) + sizeof(int[3]) + (useshortelements ? sizeof(unsigned short[3]) : 0)));
9094 decalsystem->color4f = (float *)(decalsystem->decals + decalsystem->maxdecals);
9095 decalsystem->texcoord2f = (float *)(decalsystem->color4f + decalsystem->maxdecals*12);
9096 decalsystem->vertex3f = (float *)(decalsystem->texcoord2f + decalsystem->maxdecals*6);
9097 decalsystem->element3i = (int *)(decalsystem->vertex3f + decalsystem->maxdecals*9);
9098 decalsystem->element3s = (useshortelements ? ((unsigned short *)(decalsystem->element3i + decalsystem->maxdecals*3)) : NULL);
9099 if (decalsystem->numdecals)
9100 memcpy(decalsystem->decals, old.decals, decalsystem->numdecals * sizeof(tridecal_t));
9102 Mem_Free(old.decals);
9103 for (i = 0;i < decalsystem->maxdecals*3;i++)
9104 decalsystem->element3i[i] = i;
9105 if (useshortelements)
9106 for (i = 0;i < decalsystem->maxdecals*3;i++)
9107 decalsystem->element3s[i] = i;
9110 // grab a decal and search for another free slot for the next one
9111 decals = decalsystem->decals;
9112 decal = decalsystem->decals + (i = decalsystem->freedecal++);
9113 for (i = decalsystem->freedecal;i < decalsystem->numdecals && decals[i].color4f[0][3];i++)
9115 decalsystem->freedecal = i;
9116 if (decalsystem->numdecals <= i)
9117 decalsystem->numdecals = i + 1;
9119 // initialize the decal
9121 decal->triangleindex = triangleindex;
9122 decal->surfaceindex = surfaceindex;
9123 decal->decalsequence = decalsequence;
9124 decal->color4f[0][0] = c0[0];
9125 decal->color4f[0][1] = c0[1];
9126 decal->color4f[0][2] = c0[2];
9127 decal->color4f[0][3] = 1;
9128 decal->color4f[1][0] = c1[0];
9129 decal->color4f[1][1] = c1[1];
9130 decal->color4f[1][2] = c1[2];
9131 decal->color4f[1][3] = 1;
9132 decal->color4f[2][0] = c2[0];
9133 decal->color4f[2][1] = c2[1];
9134 decal->color4f[2][2] = c2[2];
9135 decal->color4f[2][3] = 1;
9136 decal->vertex3f[0][0] = v0[0];
9137 decal->vertex3f[0][1] = v0[1];
9138 decal->vertex3f[0][2] = v0[2];
9139 decal->vertex3f[1][0] = v1[0];
9140 decal->vertex3f[1][1] = v1[1];
9141 decal->vertex3f[1][2] = v1[2];
9142 decal->vertex3f[2][0] = v2[0];
9143 decal->vertex3f[2][1] = v2[1];
9144 decal->vertex3f[2][2] = v2[2];
9145 decal->texcoord2f[0][0] = t0[0];
9146 decal->texcoord2f[0][1] = t0[1];
9147 decal->texcoord2f[1][0] = t1[0];
9148 decal->texcoord2f[1][1] = t1[1];
9149 decal->texcoord2f[2][0] = t2[0];
9150 decal->texcoord2f[2][1] = t2[1];
9151 TriangleNormal(v0, v1, v2, decal->plane);
9152 VectorNormalize(decal->plane);
9153 decal->plane[3] = DotProduct(v0, decal->plane);
9156 extern cvar_t cl_decals_bias;
9157 extern cvar_t cl_decals_models;
9158 extern cvar_t cl_decals_newsystem_intensitymultiplier;
9159 // baseparms, parms, temps
9160 static void R_DecalSystem_SplatTriangle(decalsystem_t *decalsystem, float r, float g, float b, float a, float s1, float t1, float s2, float t2, unsigned int decalsequence, qbool dynamic, float (*planes)[4], matrix4x4_t *projection, int triangleindex, int surfaceindex)
9165 const float *vertex3f;
9166 const float *normal3f;
9168 float points[2][9][3];
9175 e = rsurface.modelelement3i + 3*triangleindex;
9177 vertex3f = rsurface.modelvertex3f;
9178 normal3f = rsurface.modelnormal3f;
9182 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9184 index = 3*e[cornerindex];
9185 VectorMA(vertex3f + index, cl_decals_bias.value, normal3f + index, v[cornerindex]);
9190 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9192 index = 3*e[cornerindex];
9193 VectorCopy(vertex3f + index, v[cornerindex]);
9198 //TriangleNormal(v[0], v[1], v[2], normal);
9199 //if (DotProduct(normal, localnormal) < 0.0f)
9201 // clip by each of the box planes formed from the projection matrix
9202 // if anything survives, we emit the decal
9203 numpoints = PolygonF_Clip(3 , v[0] , planes[0][0], planes[0][1], planes[0][2], planes[0][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[1][0]);
9206 numpoints = PolygonF_Clip(numpoints, points[1][0], planes[1][0], planes[1][1], planes[1][2], planes[1][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[0][0]);
9209 numpoints = PolygonF_Clip(numpoints, points[0][0], planes[2][0], planes[2][1], planes[2][2], planes[2][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[1][0]);
9212 numpoints = PolygonF_Clip(numpoints, points[1][0], planes[3][0], planes[3][1], planes[3][2], planes[3][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[0][0]);
9215 numpoints = PolygonF_Clip(numpoints, points[0][0], planes[4][0], planes[4][1], planes[4][2], planes[4][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), points[1][0]);
9218 numpoints = PolygonF_Clip(numpoints, points[1][0], planes[5][0], planes[5][1], planes[5][2], planes[5][3], 1.0f/64.0f, sizeof(points[0])/sizeof(points[0][0]), v[0]);
9221 // some part of the triangle survived, so we have to accept it...
9224 // dynamic always uses the original triangle
9226 for (cornerindex = 0;cornerindex < 3;cornerindex++)
9228 index = 3*e[cornerindex];
9229 VectorCopy(vertex3f + index, v[cornerindex]);
9232 for (cornerindex = 0;cornerindex < numpoints;cornerindex++)
9234 // convert vertex positions to texcoords
9235 Matrix4x4_Transform(projection, v[cornerindex], temp);
9236 tc[cornerindex][0] = (temp[1]+1.0f)*0.5f * (s2-s1) + s1;
9237 tc[cornerindex][1] = (temp[2]+1.0f)*0.5f * (t2-t1) + t1;
9238 // calculate distance fade from the projection origin
9239 f = a * (1.0f-fabs(temp[0])) * cl_decals_newsystem_intensitymultiplier.value;
9240 f = bound(0.0f, f, 1.0f);
9241 c[cornerindex][0] = r * f;
9242 c[cornerindex][1] = g * f;
9243 c[cornerindex][2] = b * f;
9244 c[cornerindex][3] = 1.0f;
9245 //VectorMA(v[cornerindex], cl_decals_bias.value, localnormal, v[cornerindex]);
9248 R_DecalSystem_SpawnTriangle(decalsystem, v[0], v[1], v[2], tc[0], tc[1], tc[2], c[0], c[1], c[2], triangleindex, surfaceindex, decalsequence);
9250 for (cornerindex = 0;cornerindex < numpoints-2;cornerindex++)
9251 R_DecalSystem_SpawnTriangle(decalsystem, v[0], v[cornerindex+1], v[cornerindex+2], tc[0], tc[cornerindex+1], tc[cornerindex+2], c[0], c[cornerindex+1], c[cornerindex+2], -1, surfaceindex, decalsequence);
9253 static void R_DecalSystem_SplatEntity(entity_render_t *ent, const vec3_t worldorigin, const vec3_t worldnormal, float r, float g, float b, float a, float s1, float t1, float s2, float t2, float worldsize, unsigned int decalsequence)
9255 matrix4x4_t projection;
9256 decalsystem_t *decalsystem;
9259 const msurface_t *surface;
9260 const msurface_t *surfaces;
9261 const texture_t *texture;
9265 float localorigin[3];
9266 float localnormal[3];
9274 int bih_triangles_count;
9275 int bih_triangles[256];
9276 int bih_surfaces[256];
9278 decalsystem = &ent->decalsystem;
9280 if (!model || !ent->allowdecals || ent->alpha < 1 || (ent->flags & (RENDER_ADDITIVE | RENDER_NODEPTHTEST)))
9282 R_DecalSystem_Reset(&ent->decalsystem);
9286 if (!model->brush.data_leafs && !cl_decals_models.integer)
9288 if (decalsystem->model)
9289 R_DecalSystem_Reset(decalsystem);
9293 if (decalsystem->model != model)
9294 R_DecalSystem_Reset(decalsystem);
9295 decalsystem->model = model;
9297 RSurf_ActiveModelEntity(ent, true, false, false);
9299 Matrix4x4_Transform(&rsurface.inversematrix, worldorigin, localorigin);
9300 Matrix4x4_Transform3x3(&rsurface.inversematrix, worldnormal, localnormal);
9301 VectorNormalize(localnormal);
9302 localsize = worldsize*rsurface.inversematrixscale;
9303 localmins[0] = localorigin[0] - localsize;
9304 localmins[1] = localorigin[1] - localsize;
9305 localmins[2] = localorigin[2] - localsize;
9306 localmaxs[0] = localorigin[0] + localsize;
9307 localmaxs[1] = localorigin[1] + localsize;
9308 localmaxs[2] = localorigin[2] + localsize;
9310 //VectorCopy(localnormal, planes[4]);
9311 //VectorVectors(planes[4], planes[2], planes[0]);
9312 AnglesFromVectors(angles, localnormal, NULL, false);
9313 AngleVectors(angles, planes[0], planes[2], planes[4]);
9314 VectorNegate(planes[0], planes[1]);
9315 VectorNegate(planes[2], planes[3]);
9316 VectorNegate(planes[4], planes[5]);
9317 planes[0][3] = DotProduct(planes[0], localorigin) - localsize;
9318 planes[1][3] = DotProduct(planes[1], localorigin) - localsize;
9319 planes[2][3] = DotProduct(planes[2], localorigin) - localsize;
9320 planes[3][3] = DotProduct(planes[3], localorigin) - localsize;
9321 planes[4][3] = DotProduct(planes[4], localorigin) - localsize;
9322 planes[5][3] = DotProduct(planes[5], localorigin) - localsize;
9327 matrix4x4_t forwardprojection;
9328 Matrix4x4_CreateFromQuakeEntity(&forwardprojection, localorigin[0], localorigin[1], localorigin[2], angles[0], angles[1], angles[2], localsize);
9329 Matrix4x4_Invert_Simple(&projection, &forwardprojection);
9334 float projectionvector[4][3];
9335 VectorScale(planes[0], ilocalsize, projectionvector[0]);
9336 VectorScale(planes[2], ilocalsize, projectionvector[1]);
9337 VectorScale(planes[4], ilocalsize, projectionvector[2]);
9338 projectionvector[0][0] = planes[0][0] * ilocalsize;
9339 projectionvector[0][1] = planes[1][0] * ilocalsize;
9340 projectionvector[0][2] = planes[2][0] * ilocalsize;
9341 projectionvector[1][0] = planes[0][1] * ilocalsize;
9342 projectionvector[1][1] = planes[1][1] * ilocalsize;
9343 projectionvector[1][2] = planes[2][1] * ilocalsize;
9344 projectionvector[2][0] = planes[0][2] * ilocalsize;
9345 projectionvector[2][1] = planes[1][2] * ilocalsize;
9346 projectionvector[2][2] = planes[2][2] * ilocalsize;
9347 projectionvector[3][0] = -(localorigin[0]*projectionvector[0][0]+localorigin[1]*projectionvector[1][0]+localorigin[2]*projectionvector[2][0]);
9348 projectionvector[3][1] = -(localorigin[0]*projectionvector[0][1]+localorigin[1]*projectionvector[1][1]+localorigin[2]*projectionvector[2][1]);
9349 projectionvector[3][2] = -(localorigin[0]*projectionvector[0][2]+localorigin[1]*projectionvector[1][2]+localorigin[2]*projectionvector[2][2]);
9350 Matrix4x4_FromVectors(&projection, projectionvector[0], projectionvector[1], projectionvector[2], projectionvector[3]);
9354 dynamic = model->surfmesh.isanimated;
9355 surfaces = model->data_surfaces;
9358 bih_triangles_count = -1;
9361 if(model->render_bih.numleafs)
9362 bih = &model->render_bih;
9363 else if(model->collision_bih.numleafs)
9364 bih = &model->collision_bih;
9367 bih_triangles_count = BIH_GetTriangleListForBox(bih, sizeof(bih_triangles) / sizeof(*bih_triangles), bih_triangles, bih_surfaces, localmins, localmaxs);
9368 if(bih_triangles_count == 0)
9370 if(bih_triangles_count > (int) (sizeof(bih_triangles) / sizeof(*bih_triangles))) // hit too many, likely bad anyway
9372 if(bih_triangles_count > 0)
9374 for (triangleindex = 0; triangleindex < bih_triangles_count; ++triangleindex)
9376 surfaceindex = bih_surfaces[triangleindex];
9377 surface = surfaces + surfaceindex;
9378 texture = surface->texture;
9381 if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
9383 if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
9385 R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, bih_triangles[triangleindex], surfaceindex);
9390 for (surfaceindex = model->submodelsurfaces_start;surfaceindex < model->submodelsurfaces_end;surfaceindex++)
9392 surface = surfaces + surfaceindex;
9393 // check cull box first because it rejects more than any other check
9394 if (!dynamic && !BoxesOverlap(surface->mins, surface->maxs, localmins, localmaxs))
9396 // skip transparent surfaces
9397 texture = surface->texture;
9400 if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
9402 if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
9404 numtriangles = surface->num_triangles;
9405 for (triangleindex = 0; triangleindex < numtriangles; triangleindex++)
9406 R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, triangleindex + surface->num_firsttriangle, surfaceindex);
9411 // do not call this outside of rendering code - use R_DecalSystem_SplatEntities instead
9412 static void R_DecalSystem_ApplySplatEntities(const vec3_t worldorigin, const vec3_t worldnormal, float r, float g, float b, float a, float s1, float t1, float s2, float t2, float worldsize, unsigned int decalsequence)
9414 int renderentityindex;
9417 entity_render_t *ent;
9419 worldmins[0] = worldorigin[0] - worldsize;
9420 worldmins[1] = worldorigin[1] - worldsize;
9421 worldmins[2] = worldorigin[2] - worldsize;
9422 worldmaxs[0] = worldorigin[0] + worldsize;
9423 worldmaxs[1] = worldorigin[1] + worldsize;
9424 worldmaxs[2] = worldorigin[2] + worldsize;
9426 R_DecalSystem_SplatEntity(r_refdef.scene.worldentity, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
9428 for (renderentityindex = 0;renderentityindex < r_refdef.scene.numentities;renderentityindex++)
9430 ent = r_refdef.scene.entities[renderentityindex];
9431 if (!BoxesOverlap(ent->mins, ent->maxs, worldmins, worldmaxs))
9434 R_DecalSystem_SplatEntity(ent, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
9438 typedef struct r_decalsystem_splatqueue_s
9445 unsigned int decalsequence;
9447 r_decalsystem_splatqueue_t;
9449 int r_decalsystem_numqueued = 0;
9450 r_decalsystem_splatqueue_t r_decalsystem_queue[MAX_DECALSYSTEM_QUEUE];
9452 void R_DecalSystem_SplatEntities(const vec3_t worldorigin, const vec3_t worldnormal, float r, float g, float b, float a, float s1, float t1, float s2, float t2, float worldsize)
9454 r_decalsystem_splatqueue_t *queue;
9456 if (r_decalsystem_numqueued == MAX_DECALSYSTEM_QUEUE)
9459 queue = &r_decalsystem_queue[r_decalsystem_numqueued++];
9460 VectorCopy(worldorigin, queue->worldorigin);
9461 VectorCopy(worldnormal, queue->worldnormal);
9462 Vector4Set(queue->color, r, g, b, a);
9463 Vector4Set(queue->tcrange, s1, t1, s2, t2);
9464 queue->worldsize = worldsize;
9465 queue->decalsequence = cl.decalsequence++;
9468 static void R_DecalSystem_ApplySplatEntitiesQueue(void)
9471 r_decalsystem_splatqueue_t *queue;
9473 for (i = 0, queue = r_decalsystem_queue;i < r_decalsystem_numqueued;i++, queue++)
9474 R_DecalSystem_ApplySplatEntities(queue->worldorigin, queue->worldnormal, queue->color[0], queue->color[1], queue->color[2], queue->color[3], queue->tcrange[0], queue->tcrange[1], queue->tcrange[2], queue->tcrange[3], queue->worldsize, queue->decalsequence);
9475 r_decalsystem_numqueued = 0;
9478 extern cvar_t cl_decals_max;
9479 static void R_DrawModelDecals_FadeEntity(entity_render_t *ent)
9482 decalsystem_t *decalsystem = &ent->decalsystem;
9484 unsigned int killsequence;
9489 if (!decalsystem->numdecals)
9492 if (r_showsurfaces.integer)
9495 if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
9497 R_DecalSystem_Reset(decalsystem);
9501 killsequence = cl.decalsequence - bound(1, (unsigned int) cl_decals_max.integer, cl.decalsequence);
9502 lifetime = cl_decals_time.value + cl_decals_fadetime.value;
9504 if (decalsystem->lastupdatetime)
9505 frametime = (r_refdef.scene.time - decalsystem->lastupdatetime);
9508 decalsystem->lastupdatetime = r_refdef.scene.time;
9509 numdecals = decalsystem->numdecals;
9511 for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
9513 if (decal->color4f[0][3])
9515 decal->lived += frametime;
9516 if (killsequence > decal->decalsequence || decal->lived >= lifetime)
9518 memset(decal, 0, sizeof(*decal));
9519 if (decalsystem->freedecal > i)
9520 decalsystem->freedecal = i;
9524 decal = decalsystem->decals;
9525 while (numdecals > 0 && !decal[numdecals-1].color4f[0][3])
9528 // collapse the array by shuffling the tail decals into the gaps
9531 while (decalsystem->freedecal < numdecals && decal[decalsystem->freedecal].color4f[0][3])
9532 decalsystem->freedecal++;
9533 if (decalsystem->freedecal == numdecals)
9535 decal[decalsystem->freedecal] = decal[--numdecals];
9538 decalsystem->numdecals = numdecals;
9542 // if there are no decals left, reset decalsystem
9543 R_DecalSystem_Reset(decalsystem);
9547 extern skinframe_t *decalskinframe;
9548 static void R_DrawModelDecals_Entity(entity_render_t *ent)
9551 decalsystem_t *decalsystem = &ent->decalsystem;
9560 const unsigned char *surfacevisible = ent == r_refdef.scene.worldentity ? r_refdef.viewcache.world_surfacevisible : NULL;
9563 numdecals = decalsystem->numdecals;
9567 if (r_showsurfaces.integer)
9570 if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
9572 R_DecalSystem_Reset(decalsystem);
9576 // if the model is static it doesn't matter what value we give for
9577 // wantnormals and wanttangents, so this logic uses only rules applicable
9578 // to a model, knowing that they are meaningless otherwise
9579 RSurf_ActiveModelEntity(ent, false, false, false);
9581 decalsystem->lastupdatetime = r_refdef.scene.time;
9583 faderate = 1.0f / max(0.001f, cl_decals_fadetime.value);
9585 // update vertex positions for animated models
9586 v3f = decalsystem->vertex3f;
9587 c4f = decalsystem->color4f;
9588 t2f = decalsystem->texcoord2f;
9589 for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
9591 if (!decal->color4f[0][3])
9594 if (surfacevisible && !surfacevisible[decal->surfaceindex])
9598 if (decal->triangleindex < 0 && DotProduct(r_refdef.view.origin, decal->plane) < decal->plane[3])
9601 // update color values for fading decals
9602 if (decal->lived >= cl_decals_time.value)
9603 alpha = 1 - faderate * (decal->lived - cl_decals_time.value);
9607 c4f[ 0] = decal->color4f[0][0] * alpha;
9608 c4f[ 1] = decal->color4f[0][1] * alpha;
9609 c4f[ 2] = decal->color4f[0][2] * alpha;
9611 c4f[ 4] = decal->color4f[1][0] * alpha;
9612 c4f[ 5] = decal->color4f[1][1] * alpha;
9613 c4f[ 6] = decal->color4f[1][2] * alpha;
9615 c4f[ 8] = decal->color4f[2][0] * alpha;
9616 c4f[ 9] = decal->color4f[2][1] * alpha;
9617 c4f[10] = decal->color4f[2][2] * alpha;
9620 t2f[0] = decal->texcoord2f[0][0];
9621 t2f[1] = decal->texcoord2f[0][1];
9622 t2f[2] = decal->texcoord2f[1][0];
9623 t2f[3] = decal->texcoord2f[1][1];
9624 t2f[4] = decal->texcoord2f[2][0];
9625 t2f[5] = decal->texcoord2f[2][1];
9627 // update vertex positions for animated models
9628 if (decal->triangleindex >= 0 && decal->triangleindex < rsurface.modelnumtriangles)
9630 e = rsurface.modelelement3i + 3*decal->triangleindex;
9631 VectorCopy(rsurface.modelvertex3f + 3*e[0], v3f);
9632 VectorCopy(rsurface.modelvertex3f + 3*e[1], v3f + 3);
9633 VectorCopy(rsurface.modelvertex3f + 3*e[2], v3f + 6);
9637 VectorCopy(decal->vertex3f[0], v3f);
9638 VectorCopy(decal->vertex3f[1], v3f + 3);
9639 VectorCopy(decal->vertex3f[2], v3f + 6);
9642 if (r_refdef.fogenabled)
9644 alpha = RSurf_FogVertex(v3f);
9645 VectorScale(c4f, alpha, c4f);
9646 alpha = RSurf_FogVertex(v3f + 3);
9647 VectorScale(c4f + 4, alpha, c4f + 4);
9648 alpha = RSurf_FogVertex(v3f + 6);
9649 VectorScale(c4f + 8, alpha, c4f + 8);
9660 r_refdef.stats[r_stat_drawndecals] += numtris;
9662 // now render the decals all at once
9663 // (this assumes they all use one particle font texture!)
9664 RSurf_ActiveCustomEntity(&rsurface.matrix, &rsurface.inversematrix, rsurface.ent_flags, ent->shadertime, 1, 1, 1, 1, numdecals*3, decalsystem->vertex3f, decalsystem->texcoord2f, NULL, NULL, NULL, decalsystem->color4f, numtris, decalsystem->element3i, decalsystem->element3s, false, false);
9665 // R_Mesh_ResetTextureState();
9666 R_Mesh_PrepareVertices_Generic_Arrays(numtris * 3, decalsystem->vertex3f, decalsystem->color4f, decalsystem->texcoord2f);
9667 GL_DepthMask(false);
9668 GL_DepthRange(0, 1);
9669 GL_PolygonOffset(rsurface.basepolygonfactor + r_polygonoffset_decals_factor.value, rsurface.basepolygonoffset + r_polygonoffset_decals_offset.value);
9671 GL_CullFace(GL_NONE);
9672 GL_BlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
9673 R_SetupShader_Generic(decalskinframe->base, false, false, false);
9674 R_Mesh_Draw(0, numtris * 3, 0, numtris, decalsystem->element3i, NULL, 0, decalsystem->element3s, NULL, 0);
9678 static void R_DrawModelDecals(void)
9682 // fade faster when there are too many decals
9683 numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
9684 for (i = 0;i < r_refdef.scene.numentities;i++)
9685 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
9687 R_DrawModelDecals_FadeEntity(r_refdef.scene.worldentity);
9688 for (i = 0;i < r_refdef.scene.numentities;i++)
9689 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
9690 R_DrawModelDecals_FadeEntity(r_refdef.scene.entities[i]);
9692 R_DecalSystem_ApplySplatEntitiesQueue();
9694 numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
9695 for (i = 0;i < r_refdef.scene.numentities;i++)
9696 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
9698 r_refdef.stats[r_stat_totaldecals] += numdecals;
9700 if (r_showsurfaces.integer || !r_drawdecals.integer)
9703 R_DrawModelDecals_Entity(r_refdef.scene.worldentity);
9705 for (i = 0;i < r_refdef.scene.numentities;i++)
9707 if (!r_refdef.viewcache.entityvisible[i])
9709 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
9710 R_DrawModelDecals_Entity(r_refdef.scene.entities[i]);
9714 static void R_DrawDebugModel(void)
9716 entity_render_t *ent = rsurface.entity;
9718 const msurface_t *surface;
9719 model_t *model = ent->model;
9721 if (!sv.active && !cls.demoplayback && ent != r_refdef.scene.worldentity)
9724 if (r_showoverdraw.value > 0)
9726 float c = r_refdef.view.colorscale * r_showoverdraw.value * 0.125f;
9727 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
9728 R_SetupShader_Generic_NoTexture(false, false);
9729 GL_DepthTest(false);
9730 GL_DepthMask(false);
9731 GL_DepthRange(0, 1);
9732 GL_BlendFunc(GL_ONE, GL_ONE);
9733 for (j = model->submodelsurfaces_start;j < model->submodelsurfaces_end;j++)
9735 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9737 surface = model->data_surfaces + j;
9738 rsurface.texture = R_GetCurrentTexture(surface->texture);
9739 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9741 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, 1, &surface);
9742 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
9743 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
9744 GL_Color(c, 0, 0, 1.0f);
9745 else if (ent == r_refdef.scene.worldentity)
9746 GL_Color(c, c, c, 1.0f);
9748 GL_Color(0, c, 0, 1.0f);
9749 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
9753 rsurface.texture = NULL;
9756 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
9758 // R_Mesh_ResetTextureState();
9759 R_SetupShader_Generic_NoTexture(false, false);
9760 GL_DepthRange(0, 1);
9761 GL_DepthTest(!r_showdisabledepthtest.integer);
9762 GL_DepthMask(false);
9763 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9765 if (r_showcollisionbrushes.value > 0 && model->collision_bih.numleafs)
9769 qbool cullbox = false;
9770 const q3mbrush_t *brush;
9771 const bih_t *bih = &model->collision_bih;
9772 const bih_leaf_t *bihleaf;
9773 float vertex3f[3][3];
9774 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
9775 for (bihleafindex = 0, bihleaf = bih->leafs;bihleafindex < bih->numleafs;bihleafindex++, bihleaf++)
9777 if (cullbox && R_CullFrustum(bihleaf->mins, bihleaf->maxs))
9779 switch (bihleaf->type)
9782 brush = model->brush.data_brushes + bihleaf->itemindex;
9783 if (brush->colbrushf && brush->colbrushf->numtriangles)
9785 GL_Color((bihleafindex & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 5) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 10) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, r_showcollisionbrushes.value);
9786 R_Mesh_PrepareVertices_Generic_Arrays(brush->colbrushf->numpoints, brush->colbrushf->points->v, NULL, NULL);
9787 R_Mesh_Draw(0, brush->colbrushf->numpoints, 0, brush->colbrushf->numtriangles, brush->colbrushf->elements, NULL, 0, NULL, NULL, 0);
9790 case BIH_COLLISIONTRIANGLE:
9791 triangleindex = bihleaf->itemindex;
9792 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+0], vertex3f[0]);
9793 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+1], vertex3f[1]);
9794 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+2], vertex3f[2]);
9795 GL_Color((bihleafindex & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 5) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 10) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, r_showcollisionbrushes.value);
9796 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
9797 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
9799 case BIH_RENDERTRIANGLE:
9800 triangleindex = bihleaf->itemindex;
9801 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+0], vertex3f[0]);
9802 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+1], vertex3f[1]);
9803 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+2], vertex3f[2]);
9804 GL_Color((bihleafindex & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 5) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, ((bihleafindex >> 10) & 31) * (1.0f / 32.0f) * r_refdef.view.colorscale, r_showcollisionbrushes.value);
9805 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
9806 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
9812 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
9815 if (r_showtris.value > 0 && qglPolygonMode)
9817 if (r_showdisabledepthtest.integer)
9819 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9820 GL_DepthMask(false);
9824 GL_BlendFunc(GL_ONE, GL_ZERO);
9827 qglPolygonMode(GL_FRONT_AND_BACK, GL_LINE);CHECKGLERROR
9828 for (j = model->submodelsurfaces_start; j < model->submodelsurfaces_end; j++)
9830 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9832 surface = model->data_surfaces + j;
9833 rsurface.texture = R_GetCurrentTexture(surface->texture);
9834 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9836 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
9837 if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
9838 GL_Color(r_refdef.view.colorscale, 0, 0, r_showtris.value);
9839 else if (ent == r_refdef.scene.worldentity)
9840 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, r_showtris.value);
9842 GL_Color(0, r_refdef.view.colorscale, 0, r_showtris.value);
9843 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
9847 qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);CHECKGLERROR
9848 rsurface.texture = NULL;
9852 // FIXME! implement r_shownormals with just triangles
9853 if (r_shownormals.value != 0 && qglBegin)
9857 if (r_showdisabledepthtest.integer)
9859 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
9860 GL_DepthMask(false);
9864 GL_BlendFunc(GL_ONE, GL_ZERO);
9867 for (j = model->submodelsurfaces_start; j < model->submodelsurfaces_end; j++)
9869 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
9871 surface = model->data_surfaces + j;
9872 rsurface.texture = R_GetCurrentTexture(surface->texture);
9873 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
9875 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
9877 if (r_shownormals.value < 0 && rsurface.batchnormal3f)
9879 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9881 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9882 GL_Color(0, 0, r_refdef.view.colorscale, 1);
9883 qglVertex3f(v[0], v[1], v[2]);
9884 VectorMA(v, -r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
9885 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9886 qglVertex3f(v[0], v[1], v[2]);
9889 if (r_shownormals.value > 0 && rsurface.batchsvector3f)
9891 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9893 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9894 GL_Color(r_refdef.view.colorscale, 0, 0, 1);
9895 qglVertex3f(v[0], v[1], v[2]);
9896 VectorMA(v, r_shownormals.value, rsurface.batchsvector3f + l * 3, v);
9897 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9898 qglVertex3f(v[0], v[1], v[2]);
9901 if (r_shownormals.value > 0 && rsurface.batchtvector3f)
9903 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9905 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9906 GL_Color(0, r_refdef.view.colorscale, 0, 1);
9907 qglVertex3f(v[0], v[1], v[2]);
9908 VectorMA(v, r_shownormals.value, rsurface.batchtvector3f + l * 3, v);
9909 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9910 qglVertex3f(v[0], v[1], v[2]);
9913 if (r_shownormals.value > 0 && rsurface.batchnormal3f)
9915 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
9917 VectorCopy(rsurface.batchvertex3f + l * 3, v);
9918 GL_Color(0, 0, r_refdef.view.colorscale, 1);
9919 qglVertex3f(v[0], v[1], v[2]);
9920 VectorMA(v, r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
9921 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
9922 qglVertex3f(v[0], v[1], v[2]);
9929 rsurface.texture = NULL;
9935 int r_maxsurfacelist = 0;
9936 const msurface_t **r_surfacelist = NULL;
9937 void R_DrawModelSurfaces(entity_render_t *ent, qbool skysurfaces, qbool writedepth, qbool depthonly, qbool debug, qbool prepass, qbool ui)
9939 int i, j, flagsmask;
9940 model_t *model = ent->model;
9941 msurface_t *surfaces;
9942 unsigned char *update;
9943 int numsurfacelist = 0;
9947 if (r_maxsurfacelist < model->num_surfaces)
9949 r_maxsurfacelist = model->num_surfaces;
9951 Mem_Free((msurface_t **)r_surfacelist);
9952 r_surfacelist = (const msurface_t **) Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
9955 if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
9956 RSurf_ActiveModelEntity(ent, false, false, false);
9958 RSurf_ActiveModelEntity(ent, true, true, true);
9960 RSurf_ActiveModelEntity(ent, model->wantnormals, model->wanttangents, false);
9962 RSurf_ActiveModelEntity(ent, true, true, false);
9964 surfaces = model->data_surfaces;
9965 update = model->brushq1.lightmapupdateflags;
9967 flagsmask = skysurfaces ? MATERIALFLAG_SKY : MATERIALFLAG_WALL;
9972 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
9976 // check if this is an empty model
9977 if (model->submodelsurfaces_start >= model->submodelsurfaces_end)
9980 rsurface.lightmaptexture = NULL;
9981 rsurface.deluxemaptexture = NULL;
9982 rsurface.uselightmaptexture = false;
9983 rsurface.texture = NULL;
9984 rsurface.rtlight = NULL;
9987 // add visible surfaces to draw list
9988 if (ent == r_refdef.scene.worldentity)
9990 // for the world entity, check surfacevisible
9991 for (i = model->submodelsurfaces_start;i < model->submodelsurfaces_end;i++)
9993 j = model->modelsurfaces_sorted[i];
9994 if (r_refdef.viewcache.world_surfacevisible[j])
9995 r_surfacelist[numsurfacelist++] = surfaces + j;
9998 // don't do anything if there were no surfaces added (none of the world entity is visible)
9999 if (!numsurfacelist)
10001 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
10007 // for ui we have to preserve the order of surfaces (not using modelsurfaces_sorted)
10008 for (i = model->submodelsurfaces_start; i < model->submodelsurfaces_end; i++)
10009 r_surfacelist[numsurfacelist++] = surfaces + i;
10013 // add all surfaces
10014 for (i = model->submodelsurfaces_start; i < model->submodelsurfaces_end; i++)
10015 r_surfacelist[numsurfacelist++] = surfaces + model->modelsurfaces_sorted[i];
10019 * Mark lightmaps as dirty if their lightstyle's value changed. We do this by
10020 * using style chains because most styles do not change on most frames, and most
10021 * surfaces do not have styles on them. Mods like Arcane Dimensions (e.g. ad_necrokeep)
10022 * break this rule and animate most surfaces.
10024 if (update && !skysurfaces && !depthonly && !prepass && model->brushq1.num_lightstyles && r_refdef.scene.lightmapintensity > 0 && r_q1bsp_lightmap_updates_enabled.integer)
10026 model_brush_lightstyleinfo_t *style;
10028 // For each lightstyle, check if its value changed and mark the lightmaps as dirty if so
10029 for (i = 0, style = model->brushq1.data_lightstyleinfo; i < model->brushq1.num_lightstyles; i++, style++)
10031 if (style->value != r_refdef.scene.lightstylevalue[style->style])
10033 int* list = style->surfacelist;
10034 style->value = r_refdef.scene.lightstylevalue[style->style];
10035 // Value changed - mark the surfaces belonging to this style chain as dirty
10036 for (j = 0; j < style->numsurfaces; j++)
10037 update[list[j]] = true;
10040 // Now check if update flags are set on any surfaces that are visible
10041 if (r_q1bsp_lightmap_updates_hidden_surfaces.integer)
10044 * We can do less frequent texture uploads (approximately 10hz for animated
10045 * lightstyles) by rebuilding lightmaps on surfaces that are not currently visible.
10046 * For optimal efficiency, this includes the submodels of the worldmodel, so we
10047 * use model->num_surfaces, not nummodelsurfaces.
10049 for (i = 0; i < model->num_surfaces;i++)
10051 R_BuildLightMap(ent, surfaces + i, r_q1bsp_lightmap_updates_combine.integer);
10055 for (i = 0; i < numsurfacelist; i++)
10056 if (update[r_surfacelist[i] - surfaces])
10057 R_BuildLightMap(ent, (msurface_t *)r_surfacelist[i], r_q1bsp_lightmap_updates_combine.integer);
10061 R_QueueModelSurfaceList(ent, numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly, prepass, ui);
10063 // add to stats if desired
10064 if (r_speeds.integer && !skysurfaces && !depthonly)
10066 r_refdef.stats[r_stat_entities_surfaces] += numsurfacelist;
10067 for (j = 0;j < numsurfacelist;j++)
10068 r_refdef.stats[r_stat_entities_triangles] += r_surfacelist[j]->num_triangles;
10071 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
10074 void R_DebugLine(vec3_t start, vec3_t end)
10076 model_t *mod = CL_Mesh_UI();
10078 int e0, e1, e2, e3;
10079 float offsetx, offsety, x1, y1, x2, y2, width = 1.0f;
10080 float r1 = 1.0f, g1 = 0.0f, b1 = 0.0f, alpha1 = 0.25f;
10081 float r2 = 1.0f, g2 = 1.0f, b2 = 0.0f, alpha2 = 0.25f;
10084 // transform to screen coords first
10085 Vector4Set(w[0], start[0], start[1], start[2], 1);
10086 Vector4Set(w[1], end[0], end[1], end[2], 1);
10087 R_Viewport_TransformToScreen(&r_refdef.view.viewport, w[0], s[0]);
10088 R_Viewport_TransformToScreen(&r_refdef.view.viewport, w[1], s[1]);
10089 x1 = s[0][0] * vid_conwidth.value / vid.width;
10090 y1 = (vid.height - s[0][1]) * vid_conheight.value / vid.height;
10091 x2 = s[1][0] * vid_conwidth.value / vid.width;
10092 y2 = (vid.height - s[1][1]) * vid_conheight.value / vid.height;
10093 //Con_DPrintf("R_DebugLine: %.0f,%.0f to %.0f,%.0f\n", x1, y1, x2, y2);
10095 // add the line to the UI mesh for drawing later
10097 // width is measured in real pixels
10098 if (fabs(x2 - x1) > fabs(y2 - y1))
10101 offsety = 0.5f * width * vid_conheight.value / vid.height;
10105 offsetx = 0.5f * width * vid_conwidth.value / vid.width;
10108 surf = Mod_Mesh_AddSurface(mod, Mod_Mesh_GetTexture(mod, "white", 0, 0, MATERIALFLAG_WALL | MATERIALFLAG_VERTEXCOLOR | MATERIALFLAG_ALPHAGEN_VERTEX | MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW), true);
10109 e0 = Mod_Mesh_IndexForVertex(mod, surf, x1 - offsetx, y1 - offsety, 10, 0, 0, -1, 0, 0, 0, 0, r1, g1, b1, alpha1);
10110 e1 = Mod_Mesh_IndexForVertex(mod, surf, x2 - offsetx, y2 - offsety, 10, 0, 0, -1, 0, 0, 0, 0, r2, g2, b2, alpha2);
10111 e2 = Mod_Mesh_IndexForVertex(mod, surf, x2 + offsetx, y2 + offsety, 10, 0, 0, -1, 0, 0, 0, 0, r2, g2, b2, alpha2);
10112 e3 = Mod_Mesh_IndexForVertex(mod, surf, x1 + offsetx, y1 + offsety, 10, 0, 0, -1, 0, 0, 0, 0, r1, g1, b1, alpha1);
10113 Mod_Mesh_AddTriangle(mod, surf, e0, e1, e2);
10114 Mod_Mesh_AddTriangle(mod, surf, e0, e2, e3);
10119 void R_DrawCustomSurface(skinframe_t *skinframe, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qbool writedepth, qbool prepass, qbool ui)
10121 static texture_t texture;
10123 // fake enough texture and surface state to render this geometry
10125 texture.update_lastrenderframe = -1; // regenerate this texture
10126 texture.basematerialflags = materialflags | MATERIALFLAG_CUSTOMSURFACE | MATERIALFLAG_WALL;
10127 texture.basealpha = 1.0f;
10128 texture.currentskinframe = skinframe;
10129 texture.currenttexmatrix = *texmatrix; // requires MATERIALFLAG_CUSTOMSURFACE
10130 texture.offsetmapping = OFFSETMAPPING_OFF;
10131 texture.offsetscale = 1;
10132 texture.specularscalemod = 1;
10133 texture.specularpowermod = 1;
10134 texture.transparentsort = TRANSPARENTSORT_DISTANCE;
10136 R_DrawCustomSurface_Texture(&texture, texmatrix, materialflags, firstvertex, numvertices, firsttriangle, numtriangles, writedepth, prepass, ui);
10139 void R_DrawCustomSurface_Texture(texture_t *texture, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qbool writedepth, qbool prepass, qbool ui)
10141 static msurface_t surface;
10142 const msurface_t *surfacelist = &surface;
10144 // fake enough texture and surface state to render this geometry
10145 surface.texture = texture;
10146 surface.num_triangles = numtriangles;
10147 surface.num_firsttriangle = firsttriangle;
10148 surface.num_vertices = numvertices;
10149 surface.num_firstvertex = firstvertex;
10152 rsurface.texture = R_GetCurrentTexture(surface.texture);
10153 rsurface.lightmaptexture = NULL;
10154 rsurface.deluxemaptexture = NULL;
10155 rsurface.uselightmaptexture = false;
10156 R_DrawModelTextureSurfaceList(1, &surfacelist, writedepth, prepass, ui);