2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "cl_dyntexture.h"
30 #include "dpsoftrast.h"
34 extern LPDIRECT3DDEVICE9 vid_d3d9dev;
37 mempool_t *r_main_mempool;
38 rtexturepool_t *r_main_texturepool;
40 static int r_textureframe = 0; ///< used only by R_GetCurrentTexture
42 static qboolean r_loadnormalmap;
43 static qboolean r_loadgloss;
45 static qboolean r_loaddds;
46 static qboolean r_savedds;
53 cvar_t r_motionblur = {CVAR_SAVE, "r_motionblur", "0", "screen motionblur - value represents intensity, somewhere around 0.5 recommended"};
54 cvar_t r_damageblur = {CVAR_SAVE, "r_damageblur", "0", "screen motionblur based on damage - value represents intensity, somewhere around 0.5 recommended"};
55 cvar_t r_motionblur_averaging = {CVAR_SAVE, "r_motionblur_averaging", "0.1", "sliding average reaction time for velocity (higher = slower adaption to change)"};
56 cvar_t r_motionblur_randomize = {CVAR_SAVE, "r_motionblur_randomize", "0.1", "randomizing coefficient to workaround ghosting"};
57 cvar_t r_motionblur_minblur = {CVAR_SAVE, "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)"};
58 cvar_t r_motionblur_maxblur = {CVAR_SAVE, "r_motionblur_maxblur", "0.9", "maxmimum amount of blur"};
59 cvar_t r_motionblur_velocityfactor = {CVAR_SAVE, "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"};
60 cvar_t r_motionblur_velocityfactor_minspeed = {CVAR_SAVE, "r_motionblur_velocityfactor_minspeed", "400", "lower value of velocity when it starts to factor into blur equation"};
61 cvar_t r_motionblur_velocityfactor_maxspeed = {CVAR_SAVE, "r_motionblur_velocityfactor_maxspeed", "800", "upper value of velocity when it reaches the peak factor into blur equation"};
62 cvar_t r_motionblur_mousefactor = {CVAR_SAVE, "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"};
63 cvar_t r_motionblur_mousefactor_minspeed = {CVAR_SAVE, "r_motionblur_mousefactor_minspeed", "0", "lower value of mouse acceleration when it starts to factor into blur equation"};
64 cvar_t r_motionblur_mousefactor_maxspeed = {CVAR_SAVE, "r_motionblur_mousefactor_maxspeed", "50", "upper value of mouse acceleration when it reaches the peak factor into blur equation"};
66 // TODO do we want a r_equalize_entities cvar that works on all ents, or would that be a cheat?
67 cvar_t r_equalize_entities_fullbright = {CVAR_SAVE, "r_equalize_entities_fullbright", "0", "render fullbright entities by equalizing their lightness, not by not rendering light"};
68 cvar_t r_equalize_entities_minambient = {CVAR_SAVE, "r_equalize_entities_minambient", "0.5", "light equalizing: ensure at least this ambient/diffuse ratio"};
69 cvar_t r_equalize_entities_by = {CVAR_SAVE, "r_equalize_entities_by", "0.7", "light equalizing: exponent of dynamics compression (0 = no compression, 1 = full compression)"};
70 cvar_t r_equalize_entities_to = {CVAR_SAVE, "r_equalize_entities_to", "0.8", "light equalizing: target light level"};
72 cvar_t r_depthfirst = {CVAR_SAVE, "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 = {CVAR_SAVE, "r_useinfinitefarclip", "1", "enables use of a special kind of projection matrix that has an extremely large farclip"};
74 cvar_t r_farclip_base = {0, "r_farclip_base", "65536", "farclip (furthest visible distance) for rendering when r_useinfinitefarclip is 0"};
75 cvar_t r_farclip_world = {0, "r_farclip_world", "2", "adds map size to farclip multiplied by this value"};
76 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
77 cvar_t r_deformvertexes = {0, "r_deformvertexes", "1", "allows use of deformvertexes in shader files (can be turned off to check performance impact)"};
78 cvar_t r_transparent = {0, "r_transparent", "1", "allows use of transparent surfaces (can be turned off to check performance impact)"};
79 cvar_t r_transparent_alphatocoverage = {0, "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 = {0, "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 = {0, "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 = {0, "r_showoverdraw", "0", "shows overlapping geometry"};
83 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%, 10 = 100%)"};
84 cvar_t r_showsurfaces = {0, "r_showsurfaces", "0", "1 shows surfaces as different colors, or a value of 2 shows triangle draw order (for analyzing whether meshes are optimized for vertex cache)"};
85 cvar_t r_showtris = {0, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
86 cvar_t r_shownormals = {0, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
87 cvar_t r_showlighting = {0, "r_showlighting", "0", "shows areas lit by lights, useful for finding out why some areas of a map render slowly (bright orange = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
88 cvar_t r_showshadowvolumes = {0, "r_showshadowvolumes", "0", "shows areas shadowed by lights, useful for finding out why some areas of a map render slowly (bright blue = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
89 cvar_t r_showcollisionbrushes = {0, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
90 cvar_t r_showcollisionbrushes_polygonfactor = {0, "r_showcollisionbrushes_polygonfactor", "-1", "expands outward the brush polygons a little bit, used to make collision brushes appear infront of walls"};
91 cvar_t r_showcollisionbrushes_polygonoffset = {0, "r_showcollisionbrushes_polygonoffset", "0", "nudges brush polygon depth in hardware depth units, used to make collision brushes appear infront of walls"};
92 cvar_t r_showdisabledepthtest = {0, "r_showdisabledepthtest", "0", "disables depth testing on r_show* cvars, allowing you to see what hidden geometry the graphics card is processing"};
93 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
94 cvar_t r_drawentities = {0, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
95 cvar_t r_draw2d = {0, "r_draw2d","1", "draw 2D stuff (dangerous to turn off)"};
96 cvar_t r_drawworld = {0, "r_drawworld","1", "draw world (most static stuff)"};
97 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1", "draw your weapon model"};
98 cvar_t r_drawexteriormodel = {0, "r_drawexteriormodel","1", "draw your player model (e.g. in chase cam, reflections)"};
99 cvar_t r_cullentities_trace = {0, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
100 cvar_t r_cullentities_trace_samples = {0, "r_cullentities_trace_samples", "2", "number of samples to test for entity culling (in addition to center sample)"};
101 cvar_t r_cullentities_trace_tempentitysamples = {0, "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)"};
102 cvar_t r_cullentities_trace_enlarge = {0, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
103 cvar_t r_cullentities_trace_delay = {0, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
104 cvar_t r_sortentities = {0, "r_sortentities", "0", "sort entities before drawing (might be faster)"};
105 cvar_t r_speeds = {0, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
106 cvar_t r_fullbright = {0, "r_fullbright","0", "makes map very bright and renders faster"};
108 cvar_t r_fakelight = {0, "r_fakelight","0", "render 'fake' lighting instead of real lightmaps"};
109 cvar_t r_fakelight_intensity = {0, "r_fakelight_intensity","0.75", "fakelight intensity modifier"};
110 #define FAKELIGHT_ENABLED (r_fakelight.integer >= 2 || (r_fakelight.integer && r_refdef.scene.worldmodel && !r_refdef.scene.worldmodel->lit))
112 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1", "opacity of water polygons"};
113 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
114 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
115 cvar_t r_shadows = {CVAR_SAVE, "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."};
116 cvar_t r_shadows_darken = {CVAR_SAVE, "r_shadows_darken", "0.5", "how much shadowed areas will be darkened"};
117 cvar_t r_shadows_throwdistance = {CVAR_SAVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
118 cvar_t r_shadows_throwdirection = {CVAR_SAVE, "r_shadows_throwdirection", "0 0 -1", "override throwing direction for r_shadows 2"};
119 cvar_t r_shadows_drawafterrtlighting = {CVAR_SAVE, "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."};
120 cvar_t r_shadows_castfrombmodels = {CVAR_SAVE, "r_shadows_castfrombmodels", "0", "do cast shadows from bmodels"};
121 cvar_t r_shadows_focus = {CVAR_SAVE, "r_shadows_focus", "0 0 0", "offset the shadowed area focus"};
122 cvar_t r_shadows_shadowmapscale = {CVAR_SAVE, "r_shadows_shadowmapscale", "1", "increases shadowmap quality (multiply global shadowmap precision) for fake shadows. Needs shadowmapping ON."};
123 cvar_t r_q1bsp_skymasking = {0, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
124 cvar_t r_polygonoffset_submodel_factor = {0, "r_polygonoffset_submodel_factor", "0", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
125 cvar_t r_polygonoffset_submodel_offset = {0, "r_polygonoffset_submodel_offset", "14", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
126 cvar_t r_polygonoffset_decals_factor = {0, "r_polygonoffset_decals_factor", "0", "biases depth values of decals to prevent z-fighting artifacts"};
127 cvar_t r_polygonoffset_decals_offset = {0, "r_polygonoffset_decals_offset", "-14", "biases depth values of decals to prevent z-fighting artifacts"};
128 cvar_t r_fog_exp2 = {0, "r_fog_exp2", "0", "uses GL_EXP2 fog (as in Nehahra) rather than realistic GL_EXP fog"};
129 cvar_t r_fog_clear = {0, "r_fog_clear", "1", "clears renderbuffer with fog color before render starts"};
130 cvar_t r_drawfog = {CVAR_SAVE, "r_drawfog", "1", "allows one to disable fog rendering"};
131 cvar_t r_transparentdepthmasking = {CVAR_SAVE, "r_transparentdepthmasking", "0", "enables depth writes on transparent meshes whose materially is normally opaque, this prevents seeing the inside of a transparent mesh"};
132 cvar_t r_transparent_sortmindist = {CVAR_SAVE, "r_transparent_sortmindist", "0", "lower distance limit for transparent sorting"};
133 cvar_t r_transparent_sortmaxdist = {CVAR_SAVE, "r_transparent_sortmaxdist", "32768", "upper distance limit for transparent sorting"};
134 cvar_t r_transparent_sortarraysize = {CVAR_SAVE, "r_transparent_sortarraysize", "4096", "number of distance-sorting layers"};
136 cvar_t gl_fogenable = {0, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
137 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
138 cvar_t gl_fogred = {0, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
139 cvar_t gl_foggreen = {0, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
140 cvar_t gl_fogblue = {0, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
141 cvar_t gl_fogstart = {0, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
142 cvar_t gl_fogend = {0, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
143 cvar_t gl_skyclip = {0, "gl_skyclip", "4608", "nehahra farclip distance - the real fog end (for Nehahra compatibility only)"};
145 cvar_t r_texture_dds_load = {CVAR_SAVE, "r_texture_dds_load", "0", "load compressed dds/filename.dds texture instead of filename.tga, if the file exists (requires driver support)"};
146 cvar_t r_texture_dds_save = {CVAR_SAVE, "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"};
148 cvar_t r_textureunits = {0, "r_textureunits", "32", "number of texture units to use in GL 1.1 and GL 1.3 rendering paths"};
149 static cvar_t gl_combine = {CVAR_READONLY, "gl_combine", "1", "indicates whether the OpenGL 1.3 rendering path is active"};
150 static cvar_t r_glsl = {CVAR_READONLY, "r_glsl", "1", "indicates whether the OpenGL 2.0 rendering path is active"};
152 cvar_t r_usedepthtextures = {CVAR_SAVE, "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"};
153 cvar_t r_viewfbo = {CVAR_SAVE, "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"};
154 cvar_t r_viewscale = {CVAR_SAVE, "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"};
155 cvar_t r_viewscale_fpsscaling = {CVAR_SAVE, "r_viewscale_fpsscaling", "0", "change resolution based on framerate"};
156 cvar_t r_viewscale_fpsscaling_min = {CVAR_SAVE, "r_viewscale_fpsscaling_min", "0.0625", "worst acceptable quality"};
157 cvar_t r_viewscale_fpsscaling_multiply = {CVAR_SAVE, "r_viewscale_fpsscaling_multiply", "5", "adjust quality up or down by the frametime difference from 1.0/target, multiplied by this factor"};
158 cvar_t r_viewscale_fpsscaling_stepsize = {CVAR_SAVE, "r_viewscale_fpsscaling_stepsize", "0.01", "smallest adjustment to hit the target framerate (this value prevents minute oscillations)"};
159 cvar_t r_viewscale_fpsscaling_stepmax = {CVAR_SAVE, "r_viewscale_fpsscaling_stepmax", "1.00", "largest adjustment to hit the target framerate (this value prevents wild overshooting of the estimate)"};
160 cvar_t r_viewscale_fpsscaling_target = {CVAR_SAVE, "r_viewscale_fpsscaling_target", "70", "desired framerate"};
162 cvar_t r_glsl_deluxemapping = {CVAR_SAVE, "r_glsl_deluxemapping", "1", "use per pixel lighting on deluxemap-compiled q3bsp maps (or a value of 2 forces deluxemap shading even without deluxemaps)"};
163 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
164 cvar_t r_glsl_offsetmapping_steps = {CVAR_SAVE, "r_glsl_offsetmapping_steps", "2", "offset mapping steps (note: too high values may be not supported by your GPU)"};
165 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
166 cvar_t r_glsl_offsetmapping_reliefmapping_steps = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping_steps", "10", "relief mapping steps (note: too high values may be not supported by your GPU)"};
167 cvar_t r_glsl_offsetmapping_reliefmapping_refinesteps = {CVAR_SAVE, "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)"};
168 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
169 cvar_t r_glsl_offsetmapping_lod = {CVAR_SAVE, "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"};
170 cvar_t r_glsl_offsetmapping_lod_distance = {CVAR_SAVE, "r_glsl_offsetmapping_lod_distance", "32", "first LOD level distance, second level (-50% steps) is 2x of this, third (33%) - 3x etc."};
171 cvar_t r_glsl_postprocess = {CVAR_SAVE, "r_glsl_postprocess", "0", "use a GLSL postprocessing shader"};
172 cvar_t r_glsl_postprocess_uservec1 = {CVAR_SAVE, "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)"};
173 cvar_t r_glsl_postprocess_uservec2 = {CVAR_SAVE, "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)"};
174 cvar_t r_glsl_postprocess_uservec3 = {CVAR_SAVE, "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)"};
175 cvar_t r_glsl_postprocess_uservec4 = {CVAR_SAVE, "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)"};
176 cvar_t r_glsl_postprocess_uservec1_enable = {CVAR_SAVE, "r_glsl_postprocess_uservec1_enable", "1", "enables postprocessing uservec1 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
177 cvar_t r_glsl_postprocess_uservec2_enable = {CVAR_SAVE, "r_glsl_postprocess_uservec2_enable", "1", "enables postprocessing uservec2 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
178 cvar_t r_glsl_postprocess_uservec3_enable = {CVAR_SAVE, "r_glsl_postprocess_uservec3_enable", "1", "enables postprocessing uservec3 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
179 cvar_t r_glsl_postprocess_uservec4_enable = {CVAR_SAVE, "r_glsl_postprocess_uservec4_enable", "1", "enables postprocessing uservec4 usage, creates USERVEC1 define (only useful if default.glsl has been customized)"};
181 cvar_t r_water = {CVAR_SAVE, "r_water", "0", "whether to use reflections and refraction on water surfaces (note: r_wateralpha must be set below 1)"};
182 cvar_t r_water_clippingplanebias = {CVAR_SAVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
183 cvar_t r_water_resolutionmultiplier = {CVAR_SAVE, "r_water_resolutionmultiplier", "0.5", "multiplier for screen resolution when rendering refracted/reflected scenes, 1 is full quality, lower values are faster"};
184 cvar_t r_water_refractdistort = {CVAR_SAVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
185 cvar_t r_water_reflectdistort = {CVAR_SAVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
186 cvar_t r_water_scissormode = {0, "r_water_scissormode", "3", "scissor (1) or cull (2) or both (3) water renders"};
187 cvar_t r_water_lowquality = {0, "r_water_lowquality", "0", "special option to accelerate water rendering, 1 disables shadows and particles, 2 disables all dynamic lights"};
188 cvar_t r_water_hideplayer = {CVAR_SAVE, "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"};
189 cvar_t r_water_fbo = {CVAR_SAVE, "r_water_fbo", "1", "enables use of render to texture for water effects, otherwise copy to texture is used (slower)"};
191 cvar_t r_lerpsprites = {CVAR_SAVE, "r_lerpsprites", "0", "enables animation smoothing on sprites"};
192 cvar_t r_lerpmodels = {CVAR_SAVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
193 cvar_t r_lerplightstyles = {CVAR_SAVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
194 cvar_t r_waterscroll = {CVAR_SAVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
196 cvar_t r_bloom = {CVAR_SAVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
197 cvar_t r_bloom_colorscale = {CVAR_SAVE, "r_bloom_colorscale", "1", "how bright the glow is"};
199 cvar_t r_bloom_brighten = {CVAR_SAVE, "r_bloom_brighten", "2", "how bright the glow is, after subtract/power"};
200 cvar_t r_bloom_blur = {CVAR_SAVE, "r_bloom_blur", "4", "how large the glow is"};
201 cvar_t r_bloom_resolution = {CVAR_SAVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
202 cvar_t r_bloom_colorexponent = {CVAR_SAVE, "r_bloom_colorexponent", "1", "how exaggerated the glow is"};
203 cvar_t r_bloom_colorsubtract = {CVAR_SAVE, "r_bloom_colorsubtract", "0.125", "reduces bloom colors by a certain amount"};
205 cvar_t r_hdr_scenebrightness = {CVAR_SAVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
206 cvar_t r_hdr_glowintensity = {CVAR_SAVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
207 cvar_t r_hdr_irisadaptation = {CVAR_SAVE, "r_hdr_irisadaptation", "0", "adjust scene brightness according to light intensity at player location"};
208 cvar_t r_hdr_irisadaptation_multiplier = {CVAR_SAVE, "r_hdr_irisadaptation_multiplier", "2", "brightness at which value will be 1.0"};
209 cvar_t r_hdr_irisadaptation_minvalue = {CVAR_SAVE, "r_hdr_irisadaptation_minvalue", "0.5", "minimum value that can result from multiplier / brightness"};
210 cvar_t r_hdr_irisadaptation_maxvalue = {CVAR_SAVE, "r_hdr_irisadaptation_maxvalue", "4", "maximum value that can result from multiplier / brightness"};
211 cvar_t r_hdr_irisadaptation_value = {0, "r_hdr_irisadaptation_value", "1", "current value as scenebrightness multiplier, changes continuously when irisadaptation is active"};
212 cvar_t r_hdr_irisadaptation_fade_up = {CVAR_SAVE, "r_hdr_irisadaptation_fade_up", "0.1", "fade rate at which value adjusts to darkness"};
213 cvar_t r_hdr_irisadaptation_fade_down = {CVAR_SAVE, "r_hdr_irisadaptation_fade_down", "0.5", "fade rate at which value adjusts to brightness"};
214 cvar_t r_hdr_irisadaptation_radius = {CVAR_SAVE, "r_hdr_irisadaptation_radius", "15", "lighting within this many units of the eye is averaged"};
216 cvar_t r_smoothnormals_areaweighting = {0, "r_smoothnormals_areaweighting", "1", "uses significantly faster (and supposedly higher quality) area-weighted vertex normals and tangent vectors rather than summing normalized triangle normals and tangents"};
218 cvar_t developer_texturelogging = {0, "developer_texturelogging", "0", "produces a textures.log file containing names of skins and map textures the engine tried to load"};
220 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers)"};
222 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
224 cvar_t r_glsl_saturation = {CVAR_SAVE, "r_glsl_saturation", "1", "saturation multiplier (only working in glsl!)"};
225 cvar_t r_glsl_saturation_redcompensate = {CVAR_SAVE, "r_glsl_saturation_redcompensate", "0", "a 'vampire sight' addition to desaturation effect, does compensation for red color, r_glsl_restart is required"};
227 cvar_t r_glsl_vertextextureblend_usebothalphas = {CVAR_SAVE, "r_glsl_vertextextureblend_usebothalphas", "0", "use both alpha layers on vertex blended surfaces, each alpha layer sets amount of 'blend leak' on another layer."};
229 cvar_t r_framedatasize = {CVAR_SAVE, "r_framedatasize", "0.5", "size of renderer data cache used during one frame (for skeletal animation caching, light processing, etc)"};
231 extern cvar_t v_glslgamma;
232 extern cvar_t v_glslgamma_2d;
234 extern qboolean v_flipped_state;
236 r_framebufferstate_t r_fb;
238 /// shadow volume bsp struct with automatically growing nodes buffer
241 rtexture_t *r_texture_blanknormalmap;
242 rtexture_t *r_texture_white;
243 rtexture_t *r_texture_grey128;
244 rtexture_t *r_texture_black;
245 rtexture_t *r_texture_notexture;
246 rtexture_t *r_texture_whitecube;
247 rtexture_t *r_texture_normalizationcube;
248 rtexture_t *r_texture_fogattenuation;
249 rtexture_t *r_texture_fogheighttexture;
250 rtexture_t *r_texture_gammaramps;
251 unsigned int r_texture_gammaramps_serial;
252 //rtexture_t *r_texture_fogintensity;
253 rtexture_t *r_texture_reflectcube;
255 // TODO: hash lookups?
256 typedef struct cubemapinfo_s
263 int r_texture_numcubemaps;
264 cubemapinfo_t *r_texture_cubemaps[MAX_CUBEMAPS];
266 unsigned int r_queries[MAX_OCCLUSION_QUERIES];
267 unsigned int r_numqueries;
268 unsigned int r_maxqueries;
270 typedef struct r_qwskincache_s
272 char name[MAX_QPATH];
273 skinframe_t *skinframe;
277 static r_qwskincache_t *r_qwskincache;
278 static int r_qwskincache_size;
280 /// vertex coordinates for a quad that covers the screen exactly
281 extern const float r_screenvertex3f[12];
282 extern const float r_d3dscreenvertex3f[12];
283 const float r_screenvertex3f[12] =
290 const float r_d3dscreenvertex3f[12] =
298 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
301 for (i = 0;i < verts;i++)
312 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
315 for (i = 0;i < verts;i++)
325 // FIXME: move this to client?
328 if (gamemode == GAME_NEHAHRA)
330 Cvar_Set("gl_fogenable", "0");
331 Cvar_Set("gl_fogdensity", "0.2");
332 Cvar_Set("gl_fogred", "0.3");
333 Cvar_Set("gl_foggreen", "0.3");
334 Cvar_Set("gl_fogblue", "0.3");
336 r_refdef.fog_density = 0;
337 r_refdef.fog_red = 0;
338 r_refdef.fog_green = 0;
339 r_refdef.fog_blue = 0;
340 r_refdef.fog_alpha = 1;
341 r_refdef.fog_start = 0;
342 r_refdef.fog_end = 16384;
343 r_refdef.fog_height = 1<<30;
344 r_refdef.fog_fadedepth = 128;
345 memset(r_refdef.fog_height_texturename, 0, sizeof(r_refdef.fog_height_texturename));
348 static void R_BuildBlankTextures(void)
350 unsigned char data[4];
351 data[2] = 128; // normal X
352 data[1] = 128; // normal Y
353 data[0] = 255; // normal Z
354 data[3] = 255; // height
355 r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
360 r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
365 r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
370 r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, -1, NULL);
373 static void R_BuildNoTexture(void)
376 unsigned char pix[16][16][4];
377 // this makes a light grey/dark grey checkerboard texture
378 for (y = 0;y < 16;y++)
380 for (x = 0;x < 16;x++)
382 if ((y < 8) ^ (x < 8))
398 r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], 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);
517 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, 0, FOGWIDTH, 1, 1);
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 // LordHavoc: 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 *builtinshaderstring =
597 #include "shader_glsl.h"
600 const char *builtinhlslshaderstring =
601 #include "shader_hlsl.h"
604 char *glslshaderstring = NULL;
605 char *hlslshaderstring = NULL;
607 //=======================================================================================================================================================
609 typedef struct shaderpermutationinfo_s
614 shaderpermutationinfo_t;
616 typedef struct shadermodeinfo_s
618 const char *vertexfilename;
619 const char *geometryfilename;
620 const char *fragmentfilename;
626 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
627 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
629 {"#define USEDIFFUSE\n", " diffuse"},
630 {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
631 {"#define USEVIEWTINT\n", " viewtint"},
632 {"#define USECOLORMAPPING\n", " colormapping"},
633 {"#define USESATURATION\n", " saturation"},
634 {"#define USEFOGINSIDE\n", " foginside"},
635 {"#define USEFOGOUTSIDE\n", " fogoutside"},
636 {"#define USEFOGHEIGHTTEXTURE\n", " fogheighttexture"},
637 {"#define USEFOGALPHAHACK\n", " fogalphahack"},
638 {"#define USEGAMMARAMPS\n", " gammaramps"},
639 {"#define USECUBEFILTER\n", " cubefilter"},
640 {"#define USEGLOW\n", " glow"},
641 {"#define USEBLOOM\n", " bloom"},
642 {"#define USESPECULAR\n", " specular"},
643 {"#define USEPOSTPROCESSING\n", " postprocessing"},
644 {"#define USEREFLECTION\n", " reflection"},
645 {"#define USEOFFSETMAPPING\n", " offsetmapping"},
646 {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
647 {"#define USESHADOWMAP2D\n", " shadowmap2d"},
648 {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"}, // TODO make this a static parm
649 {"#define USESHADOWMAPORTHO\n", " shadowmaportho"},
650 {"#define USEDEFERREDLIGHTMAP\n", " deferredlightmap"},
651 {"#define USEALPHAKILL\n", " alphakill"},
652 {"#define USEREFLECTCUBE\n", " reflectcube"},
653 {"#define USENORMALMAPSCROLLBLEND\n", " normalmapscrollblend"},
654 {"#define USEBOUNCEGRID\n", " bouncegrid"},
655 {"#define USEBOUNCEGRIDDIRECTIONAL\n", " bouncegriddirectional"}, // TODO make this a static parm
656 {"#define USETRIPPY\n", " trippy"},
657 {"#define USEDEPTHRGB\n", " depthrgb"},
658 {"#define USEALPHAGENVERTEX\n", "alphagenvertex"}
661 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
662 shadermodeinfo_t glslshadermodeinfo[SHADERMODE_COUNT] =
664 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_GENERIC\n", " generic"},
665 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_POSTPROCESS\n", " postprocess"},
666 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
667 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
668 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
669 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTMAP\n", " lightmap"},
670 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FAKELIGHT\n", " fakelight"},
671 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
672 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
673 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP\n", " lightdirectionmap_forced_lightmap"},
674 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR\n", " lightdirectionmap_forced_vertexcolor"},
675 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
676 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
677 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_REFRACTION\n", " refraction"},
678 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_WATER\n", " water"},
679 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
680 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
681 {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
684 shadermodeinfo_t hlslshadermodeinfo[SHADERMODE_COUNT] =
686 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_GENERIC\n", " generic"},
687 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_POSTPROCESS\n", " postprocess"},
688 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
689 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
690 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
691 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTMAP\n", " lightmap"},
692 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_FAKELIGHT\n", " fakelight"},
693 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
694 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
695 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP\n", " lightdirectionmap_forced_lightmap"},
696 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR\n", " lightdirectionmap_forced_vertexcolor"},
697 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
698 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
699 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_REFRACTION\n", " refraction"},
700 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_WATER\n", " water"},
701 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
702 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
703 {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
706 struct r_glsl_permutation_s;
707 typedef struct r_glsl_permutation_s
710 struct r_glsl_permutation_s *hashnext;
712 unsigned int permutation;
714 /// indicates if we have tried compiling this permutation already
716 /// 0 if compilation failed
718 // texture units assigned to each detected uniform
719 int tex_Texture_First;
720 int tex_Texture_Second;
721 int tex_Texture_GammaRamps;
722 int tex_Texture_Normal;
723 int tex_Texture_Color;
724 int tex_Texture_Gloss;
725 int tex_Texture_Glow;
726 int tex_Texture_SecondaryNormal;
727 int tex_Texture_SecondaryColor;
728 int tex_Texture_SecondaryGloss;
729 int tex_Texture_SecondaryGlow;
730 int tex_Texture_Pants;
731 int tex_Texture_Shirt;
732 int tex_Texture_FogHeightTexture;
733 int tex_Texture_FogMask;
734 int tex_Texture_Lightmap;
735 int tex_Texture_Deluxemap;
736 int tex_Texture_Attenuation;
737 int tex_Texture_Cube;
738 int tex_Texture_Refraction;
739 int tex_Texture_Reflection;
740 int tex_Texture_ShadowMap2D;
741 int tex_Texture_CubeProjection;
742 int tex_Texture_ScreenNormalMap;
743 int tex_Texture_ScreenDiffuse;
744 int tex_Texture_ScreenSpecular;
745 int tex_Texture_ReflectMask;
746 int tex_Texture_ReflectCube;
747 int tex_Texture_BounceGrid;
748 /// locations of detected uniforms in program object, or -1 if not found
749 int loc_Texture_First;
750 int loc_Texture_Second;
751 int loc_Texture_GammaRamps;
752 int loc_Texture_Normal;
753 int loc_Texture_Color;
754 int loc_Texture_Gloss;
755 int loc_Texture_Glow;
756 int loc_Texture_SecondaryNormal;
757 int loc_Texture_SecondaryColor;
758 int loc_Texture_SecondaryGloss;
759 int loc_Texture_SecondaryGlow;
760 int loc_Texture_Pants;
761 int loc_Texture_Shirt;
762 int loc_Texture_FogHeightTexture;
763 int loc_Texture_FogMask;
764 int loc_Texture_Lightmap;
765 int loc_Texture_Deluxemap;
766 int loc_Texture_Attenuation;
767 int loc_Texture_Cube;
768 int loc_Texture_Refraction;
769 int loc_Texture_Reflection;
770 int loc_Texture_ShadowMap2D;
771 int loc_Texture_CubeProjection;
772 int loc_Texture_ScreenNormalMap;
773 int loc_Texture_ScreenDiffuse;
774 int loc_Texture_ScreenSpecular;
775 int loc_Texture_ReflectMask;
776 int loc_Texture_ReflectCube;
777 int loc_Texture_BounceGrid;
779 int loc_BloomBlur_Parameters;
781 int loc_Color_Ambient;
782 int loc_Color_Diffuse;
783 int loc_Color_Specular;
787 int loc_DeferredColor_Ambient;
788 int loc_DeferredColor_Diffuse;
789 int loc_DeferredColor_Specular;
790 int loc_DeferredMod_Diffuse;
791 int loc_DeferredMod_Specular;
792 int loc_DistortScaleRefractReflect;
795 int loc_FogHeightFade;
797 int loc_FogPlaneViewDist;
798 int loc_FogRangeRecip;
801 int loc_LightPosition;
802 int loc_OffsetMapping_ScaleSteps;
803 int loc_OffsetMapping_LodDistance;
804 int loc_OffsetMapping_Bias;
806 int loc_ReflectColor;
807 int loc_ReflectFactor;
808 int loc_ReflectOffset;
809 int loc_RefractColor;
811 int loc_ScreenCenterRefractReflect;
812 int loc_ScreenScaleRefractReflect;
813 int loc_ScreenToDepth;
814 int loc_ShadowMap_Parameters;
815 int loc_ShadowMap_TextureScale;
816 int loc_SpecularPower;
821 int loc_ViewTintColor;
823 int loc_ModelToLight;
825 int loc_BackgroundTexMatrix;
826 int loc_ModelViewProjectionMatrix;
827 int loc_ModelViewMatrix;
828 int loc_PixelToScreenTexCoord;
829 int loc_ModelToReflectCube;
830 int loc_ShadowMapMatrix;
831 int loc_BloomColorSubtract;
832 int loc_NormalmapScrollBlend;
833 int loc_BounceGridMatrix;
834 int loc_BounceGridIntensity;
836 r_glsl_permutation_t;
838 #define SHADERPERMUTATION_HASHSIZE 256
841 // non-degradable "lightweight" shader parameters to keep the permutations simpler
842 // these can NOT degrade! only use for simple stuff
845 SHADERSTATICPARM_SATURATION_REDCOMPENSATE = 0, ///< red compensation filter for saturation
846 SHADERSTATICPARM_EXACTSPECULARMATH = 1, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
847 SHADERSTATICPARM_POSTPROCESS_USERVEC1 = 2, ///< postprocess uservec1 is enabled
848 SHADERSTATICPARM_POSTPROCESS_USERVEC2 = 3, ///< postprocess uservec2 is enabled
849 SHADERSTATICPARM_POSTPROCESS_USERVEC3 = 4, ///< postprocess uservec3 is enabled
850 SHADERSTATICPARM_POSTPROCESS_USERVEC4 = 5, ///< postprocess uservec4 is enabled
851 SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS = 6, // use both alpha layers while blending materials, allows more advanced microblending
852 SHADERSTATICPARM_OFFSETMAPPING_USELOD = 7, ///< LOD for offsetmapping
853 SHADERSTATICPARM_SHADOWMAPPCF_1 = 8, ///< PCF 1
854 SHADERSTATICPARM_SHADOWMAPPCF_2 = 9, ///< PCF 2
855 SHADERSTATICPARM_SHADOWSAMPLER = 10, ///< sampler
857 #define SHADERSTATICPARMS_COUNT 11
859 static const char *shaderstaticparmstrings_list[SHADERSTATICPARMS_COUNT];
860 static int shaderstaticparms_count = 0;
862 static unsigned int r_compileshader_staticparms[(SHADERSTATICPARMS_COUNT + 0x1F) >> 5] = {0};
863 #define R_COMPILESHADER_STATICPARM_ENABLE(p) r_compileshader_staticparms[(p) >> 5] |= (1 << ((p) & 0x1F))
865 extern qboolean r_shadow_shadowmapsampler;
866 extern int r_shadow_shadowmappcf;
867 qboolean R_CompileShader_CheckStaticParms(void)
869 static int r_compileshader_staticparms_save[1];
870 memcpy(r_compileshader_staticparms_save, r_compileshader_staticparms, sizeof(r_compileshader_staticparms));
871 memset(r_compileshader_staticparms, 0, sizeof(r_compileshader_staticparms));
874 if (r_glsl_saturation_redcompensate.integer)
875 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SATURATION_REDCOMPENSATE);
876 if (r_glsl_vertextextureblend_usebothalphas.integer)
877 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS);
878 if (r_shadow_glossexact.integer)
879 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_EXACTSPECULARMATH);
880 if (r_glsl_postprocess.integer)
882 if (r_glsl_postprocess_uservec1_enable.integer)
883 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC1);
884 if (r_glsl_postprocess_uservec2_enable.integer)
885 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC2);
886 if (r_glsl_postprocess_uservec3_enable.integer)
887 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC3);
888 if (r_glsl_postprocess_uservec4_enable.integer)
889 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC4);
891 if (r_glsl_offsetmapping_lod.integer && r_glsl_offsetmapping_lod_distance.integer > 0)
892 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_OFFSETMAPPING_USELOD);
894 if (r_shadow_shadowmapsampler)
895 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWSAMPLER);
896 if (r_shadow_shadowmappcf > 1)
897 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWMAPPCF_2);
898 else if (r_shadow_shadowmappcf)
899 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SHADOWMAPPCF_1);
901 return memcmp(r_compileshader_staticparms, r_compileshader_staticparms_save, sizeof(r_compileshader_staticparms)) != 0;
904 #define R_COMPILESHADER_STATICPARM_EMIT(p, n) \
905 if(r_compileshader_staticparms[(p) >> 5] & (1 << ((p) & 0x1F))) \
906 shaderstaticparmstrings_list[shaderstaticparms_count++] = "#define " n "\n"; \
908 shaderstaticparmstrings_list[shaderstaticparms_count++] = "\n"
909 static void R_CompileShader_AddStaticParms(unsigned int mode, unsigned int permutation)
911 shaderstaticparms_count = 0;
914 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SATURATION_REDCOMPENSATE, "SATURATION_REDCOMPENSATE");
915 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_EXACTSPECULARMATH, "USEEXACTSPECULARMATH");
916 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC1, "USERVEC1");
917 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC2, "USERVEC2");
918 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC3, "USERVEC3");
919 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC4, "USERVEC4");
920 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_VERTEXTEXTUREBLEND_USEBOTHALPHAS, "USEBOTHALPHAS");
921 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_OFFSETMAPPING_USELOD, "USEOFFSETMAPPING_LOD");
922 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWMAPPCF_1, "USESHADOWMAPPCF 1");
923 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWMAPPCF_2, "USESHADOWMAPPCF 2");
924 R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SHADOWSAMPLER, "USESHADOWSAMPLER");
927 /// information about each possible shader permutation
928 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
929 /// currently selected permutation
930 r_glsl_permutation_t *r_glsl_permutation;
931 /// storage for permutations linked in the hash table
932 memexpandablearray_t r_glsl_permutationarray;
934 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, unsigned int permutation)
936 //unsigned int hashdepth = 0;
937 unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
938 r_glsl_permutation_t *p;
939 for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
941 if (p->mode == mode && p->permutation == permutation)
943 //if (hashdepth > 10)
944 // Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
949 p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
951 p->permutation = permutation;
952 p->hashnext = r_glsl_permutationhash[mode][hashindex];
953 r_glsl_permutationhash[mode][hashindex] = p;
954 //if (hashdepth > 10)
955 // Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
959 static char *R_GLSL_GetText(const char *filename, qboolean printfromdisknotice)
962 if (!filename || !filename[0])
964 if (!strcmp(filename, "glsl/default.glsl"))
966 if (!glslshaderstring)
968 glslshaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
969 if (glslshaderstring)
970 Con_DPrintf("Loading shaders from file %s...\n", filename);
972 glslshaderstring = (char *)builtinshaderstring;
974 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(glslshaderstring) + 1);
975 memcpy(shaderstring, glslshaderstring, strlen(glslshaderstring) + 1);
978 shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
981 if (printfromdisknotice)
982 Con_DPrintf("from disk %s... ", filename);
988 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, unsigned int permutation)
992 shadermodeinfo_t *modeinfo = glslshadermodeinfo + mode;
993 char *vertexstring, *geometrystring, *fragmentstring;
994 char permutationname[256];
995 int vertstrings_count = 0;
996 int geomstrings_count = 0;
997 int fragstrings_count = 0;
998 const char *vertstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
999 const char *geomstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1000 const char *fragstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1007 permutationname[0] = 0;
1008 vertexstring = R_GLSL_GetText(modeinfo->vertexfilename, true);
1009 geometrystring = R_GLSL_GetText(modeinfo->geometryfilename, false);
1010 fragmentstring = R_GLSL_GetText(modeinfo->fragmentfilename, false);
1012 strlcat(permutationname, modeinfo->vertexfilename, sizeof(permutationname));
1014 // if we can do #version 130, we should (this improves quality of offset/reliefmapping thanks to textureGrad)
1015 if(vid.support.gl20shaders130)
1017 vertstrings_list[vertstrings_count++] = "#version 130\n";
1018 geomstrings_list[geomstrings_count++] = "#version 130\n";
1019 fragstrings_list[fragstrings_count++] = "#version 130\n";
1020 vertstrings_list[vertstrings_count++] = "#define GLSL130\n";
1021 geomstrings_list[geomstrings_count++] = "#define GLSL130\n";
1022 fragstrings_list[fragstrings_count++] = "#define GLSL130\n";
1025 // the first pretext is which type of shader to compile as
1026 // (later these will all be bound together as a program object)
1027 vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
1028 geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
1029 fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
1031 // the second pretext is the mode (for example a light source)
1032 vertstrings_list[vertstrings_count++] = modeinfo->pretext;
1033 geomstrings_list[geomstrings_count++] = modeinfo->pretext;
1034 fragstrings_list[fragstrings_count++] = modeinfo->pretext;
1035 strlcat(permutationname, modeinfo->name, sizeof(permutationname));
1037 // now add all the permutation pretexts
1038 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1040 if (permutation & (1<<i))
1042 vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
1043 geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
1044 fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
1045 strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
1049 // keep line numbers correct
1050 vertstrings_list[vertstrings_count++] = "\n";
1051 geomstrings_list[geomstrings_count++] = "\n";
1052 fragstrings_list[fragstrings_count++] = "\n";
1057 R_CompileShader_AddStaticParms(mode, permutation);
1058 memcpy((char *)(vertstrings_list + vertstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1059 vertstrings_count += shaderstaticparms_count;
1060 memcpy((char *)(geomstrings_list + geomstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1061 geomstrings_count += shaderstaticparms_count;
1062 memcpy((char *)(fragstrings_list + fragstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1063 fragstrings_count += shaderstaticparms_count;
1065 // now append the shader text itself
1066 vertstrings_list[vertstrings_count++] = vertexstring;
1067 geomstrings_list[geomstrings_count++] = geometrystring;
1068 fragstrings_list[fragstrings_count++] = fragmentstring;
1070 // if any sources were NULL, clear the respective list
1072 vertstrings_count = 0;
1073 if (!geometrystring)
1074 geomstrings_count = 0;
1075 if (!fragmentstring)
1076 fragstrings_count = 0;
1078 // compile the shader program
1079 if (vertstrings_count + geomstrings_count + fragstrings_count)
1080 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1084 qglUseProgram(p->program);CHECKGLERROR
1085 // look up all the uniform variable names we care about, so we don't
1086 // have to look them up every time we set them
1088 p->loc_Texture_First = qglGetUniformLocation(p->program, "Texture_First");
1089 p->loc_Texture_Second = qglGetUniformLocation(p->program, "Texture_Second");
1090 p->loc_Texture_GammaRamps = qglGetUniformLocation(p->program, "Texture_GammaRamps");
1091 p->loc_Texture_Normal = qglGetUniformLocation(p->program, "Texture_Normal");
1092 p->loc_Texture_Color = qglGetUniformLocation(p->program, "Texture_Color");
1093 p->loc_Texture_Gloss = qglGetUniformLocation(p->program, "Texture_Gloss");
1094 p->loc_Texture_Glow = qglGetUniformLocation(p->program, "Texture_Glow");
1095 p->loc_Texture_SecondaryNormal = qglGetUniformLocation(p->program, "Texture_SecondaryNormal");
1096 p->loc_Texture_SecondaryColor = qglGetUniformLocation(p->program, "Texture_SecondaryColor");
1097 p->loc_Texture_SecondaryGloss = qglGetUniformLocation(p->program, "Texture_SecondaryGloss");
1098 p->loc_Texture_SecondaryGlow = qglGetUniformLocation(p->program, "Texture_SecondaryGlow");
1099 p->loc_Texture_Pants = qglGetUniformLocation(p->program, "Texture_Pants");
1100 p->loc_Texture_Shirt = qglGetUniformLocation(p->program, "Texture_Shirt");
1101 p->loc_Texture_FogHeightTexture = qglGetUniformLocation(p->program, "Texture_FogHeightTexture");
1102 p->loc_Texture_FogMask = qglGetUniformLocation(p->program, "Texture_FogMask");
1103 p->loc_Texture_Lightmap = qglGetUniformLocation(p->program, "Texture_Lightmap");
1104 p->loc_Texture_Deluxemap = qglGetUniformLocation(p->program, "Texture_Deluxemap");
1105 p->loc_Texture_Attenuation = qglGetUniformLocation(p->program, "Texture_Attenuation");
1106 p->loc_Texture_Cube = qglGetUniformLocation(p->program, "Texture_Cube");
1107 p->loc_Texture_Refraction = qglGetUniformLocation(p->program, "Texture_Refraction");
1108 p->loc_Texture_Reflection = qglGetUniformLocation(p->program, "Texture_Reflection");
1109 p->loc_Texture_ShadowMap2D = qglGetUniformLocation(p->program, "Texture_ShadowMap2D");
1110 p->loc_Texture_CubeProjection = qglGetUniformLocation(p->program, "Texture_CubeProjection");
1111 p->loc_Texture_ScreenNormalMap = qglGetUniformLocation(p->program, "Texture_ScreenNormalMap");
1112 p->loc_Texture_ScreenDiffuse = qglGetUniformLocation(p->program, "Texture_ScreenDiffuse");
1113 p->loc_Texture_ScreenSpecular = qglGetUniformLocation(p->program, "Texture_ScreenSpecular");
1114 p->loc_Texture_ReflectMask = qglGetUniformLocation(p->program, "Texture_ReflectMask");
1115 p->loc_Texture_ReflectCube = qglGetUniformLocation(p->program, "Texture_ReflectCube");
1116 p->loc_Texture_BounceGrid = qglGetUniformLocation(p->program, "Texture_BounceGrid");
1117 p->loc_Alpha = qglGetUniformLocation(p->program, "Alpha");
1118 p->loc_BloomBlur_Parameters = qglGetUniformLocation(p->program, "BloomBlur_Parameters");
1119 p->loc_ClientTime = qglGetUniformLocation(p->program, "ClientTime");
1120 p->loc_Color_Ambient = qglGetUniformLocation(p->program, "Color_Ambient");
1121 p->loc_Color_Diffuse = qglGetUniformLocation(p->program, "Color_Diffuse");
1122 p->loc_Color_Specular = qglGetUniformLocation(p->program, "Color_Specular");
1123 p->loc_Color_Glow = qglGetUniformLocation(p->program, "Color_Glow");
1124 p->loc_Color_Pants = qglGetUniformLocation(p->program, "Color_Pants");
1125 p->loc_Color_Shirt = qglGetUniformLocation(p->program, "Color_Shirt");
1126 p->loc_DeferredColor_Ambient = qglGetUniformLocation(p->program, "DeferredColor_Ambient");
1127 p->loc_DeferredColor_Diffuse = qglGetUniformLocation(p->program, "DeferredColor_Diffuse");
1128 p->loc_DeferredColor_Specular = qglGetUniformLocation(p->program, "DeferredColor_Specular");
1129 p->loc_DeferredMod_Diffuse = qglGetUniformLocation(p->program, "DeferredMod_Diffuse");
1130 p->loc_DeferredMod_Specular = qglGetUniformLocation(p->program, "DeferredMod_Specular");
1131 p->loc_DistortScaleRefractReflect = qglGetUniformLocation(p->program, "DistortScaleRefractReflect");
1132 p->loc_EyePosition = qglGetUniformLocation(p->program, "EyePosition");
1133 p->loc_FogColor = qglGetUniformLocation(p->program, "FogColor");
1134 p->loc_FogHeightFade = qglGetUniformLocation(p->program, "FogHeightFade");
1135 p->loc_FogPlane = qglGetUniformLocation(p->program, "FogPlane");
1136 p->loc_FogPlaneViewDist = qglGetUniformLocation(p->program, "FogPlaneViewDist");
1137 p->loc_FogRangeRecip = qglGetUniformLocation(p->program, "FogRangeRecip");
1138 p->loc_LightColor = qglGetUniformLocation(p->program, "LightColor");
1139 p->loc_LightDir = qglGetUniformLocation(p->program, "LightDir");
1140 p->loc_LightPosition = qglGetUniformLocation(p->program, "LightPosition");
1141 p->loc_OffsetMapping_ScaleSteps = qglGetUniformLocation(p->program, "OffsetMapping_ScaleSteps");
1142 p->loc_OffsetMapping_LodDistance = qglGetUniformLocation(p->program, "OffsetMapping_LodDistance");
1143 p->loc_OffsetMapping_Bias = qglGetUniformLocation(p->program, "OffsetMapping_Bias");
1144 p->loc_PixelSize = qglGetUniformLocation(p->program, "PixelSize");
1145 p->loc_ReflectColor = qglGetUniformLocation(p->program, "ReflectColor");
1146 p->loc_ReflectFactor = qglGetUniformLocation(p->program, "ReflectFactor");
1147 p->loc_ReflectOffset = qglGetUniformLocation(p->program, "ReflectOffset");
1148 p->loc_RefractColor = qglGetUniformLocation(p->program, "RefractColor");
1149 p->loc_Saturation = qglGetUniformLocation(p->program, "Saturation");
1150 p->loc_ScreenCenterRefractReflect = qglGetUniformLocation(p->program, "ScreenCenterRefractReflect");
1151 p->loc_ScreenScaleRefractReflect = qglGetUniformLocation(p->program, "ScreenScaleRefractReflect");
1152 p->loc_ScreenToDepth = qglGetUniformLocation(p->program, "ScreenToDepth");
1153 p->loc_ShadowMap_Parameters = qglGetUniformLocation(p->program, "ShadowMap_Parameters");
1154 p->loc_ShadowMap_TextureScale = qglGetUniformLocation(p->program, "ShadowMap_TextureScale");
1155 p->loc_SpecularPower = qglGetUniformLocation(p->program, "SpecularPower");
1156 p->loc_UserVec1 = qglGetUniformLocation(p->program, "UserVec1");
1157 p->loc_UserVec2 = qglGetUniformLocation(p->program, "UserVec2");
1158 p->loc_UserVec3 = qglGetUniformLocation(p->program, "UserVec3");
1159 p->loc_UserVec4 = qglGetUniformLocation(p->program, "UserVec4");
1160 p->loc_ViewTintColor = qglGetUniformLocation(p->program, "ViewTintColor");
1161 p->loc_ViewToLight = qglGetUniformLocation(p->program, "ViewToLight");
1162 p->loc_ModelToLight = qglGetUniformLocation(p->program, "ModelToLight");
1163 p->loc_TexMatrix = qglGetUniformLocation(p->program, "TexMatrix");
1164 p->loc_BackgroundTexMatrix = qglGetUniformLocation(p->program, "BackgroundTexMatrix");
1165 p->loc_ModelViewMatrix = qglGetUniformLocation(p->program, "ModelViewMatrix");
1166 p->loc_ModelViewProjectionMatrix = qglGetUniformLocation(p->program, "ModelViewProjectionMatrix");
1167 p->loc_PixelToScreenTexCoord = qglGetUniformLocation(p->program, "PixelToScreenTexCoord");
1168 p->loc_ModelToReflectCube = qglGetUniformLocation(p->program, "ModelToReflectCube");
1169 p->loc_ShadowMapMatrix = qglGetUniformLocation(p->program, "ShadowMapMatrix");
1170 p->loc_BloomColorSubtract = qglGetUniformLocation(p->program, "BloomColorSubtract");
1171 p->loc_NormalmapScrollBlend = qglGetUniformLocation(p->program, "NormalmapScrollBlend");
1172 p->loc_BounceGridMatrix = qglGetUniformLocation(p->program, "BounceGridMatrix");
1173 p->loc_BounceGridIntensity = qglGetUniformLocation(p->program, "BounceGridIntensity");
1174 // initialize the samplers to refer to the texture units we use
1175 p->tex_Texture_First = -1;
1176 p->tex_Texture_Second = -1;
1177 p->tex_Texture_GammaRamps = -1;
1178 p->tex_Texture_Normal = -1;
1179 p->tex_Texture_Color = -1;
1180 p->tex_Texture_Gloss = -1;
1181 p->tex_Texture_Glow = -1;
1182 p->tex_Texture_SecondaryNormal = -1;
1183 p->tex_Texture_SecondaryColor = -1;
1184 p->tex_Texture_SecondaryGloss = -1;
1185 p->tex_Texture_SecondaryGlow = -1;
1186 p->tex_Texture_Pants = -1;
1187 p->tex_Texture_Shirt = -1;
1188 p->tex_Texture_FogHeightTexture = -1;
1189 p->tex_Texture_FogMask = -1;
1190 p->tex_Texture_Lightmap = -1;
1191 p->tex_Texture_Deluxemap = -1;
1192 p->tex_Texture_Attenuation = -1;
1193 p->tex_Texture_Cube = -1;
1194 p->tex_Texture_Refraction = -1;
1195 p->tex_Texture_Reflection = -1;
1196 p->tex_Texture_ShadowMap2D = -1;
1197 p->tex_Texture_CubeProjection = -1;
1198 p->tex_Texture_ScreenNormalMap = -1;
1199 p->tex_Texture_ScreenDiffuse = -1;
1200 p->tex_Texture_ScreenSpecular = -1;
1201 p->tex_Texture_ReflectMask = -1;
1202 p->tex_Texture_ReflectCube = -1;
1203 p->tex_Texture_BounceGrid = -1;
1205 if (p->loc_Texture_First >= 0) {p->tex_Texture_First = sampler;qglUniform1i(p->loc_Texture_First , sampler);sampler++;}
1206 if (p->loc_Texture_Second >= 0) {p->tex_Texture_Second = sampler;qglUniform1i(p->loc_Texture_Second , sampler);sampler++;}
1207 if (p->loc_Texture_GammaRamps >= 0) {p->tex_Texture_GammaRamps = sampler;qglUniform1i(p->loc_Texture_GammaRamps , sampler);sampler++;}
1208 if (p->loc_Texture_Normal >= 0) {p->tex_Texture_Normal = sampler;qglUniform1i(p->loc_Texture_Normal , sampler);sampler++;}
1209 if (p->loc_Texture_Color >= 0) {p->tex_Texture_Color = sampler;qglUniform1i(p->loc_Texture_Color , sampler);sampler++;}
1210 if (p->loc_Texture_Gloss >= 0) {p->tex_Texture_Gloss = sampler;qglUniform1i(p->loc_Texture_Gloss , sampler);sampler++;}
1211 if (p->loc_Texture_Glow >= 0) {p->tex_Texture_Glow = sampler;qglUniform1i(p->loc_Texture_Glow , sampler);sampler++;}
1212 if (p->loc_Texture_SecondaryNormal >= 0) {p->tex_Texture_SecondaryNormal = sampler;qglUniform1i(p->loc_Texture_SecondaryNormal , sampler);sampler++;}
1213 if (p->loc_Texture_SecondaryColor >= 0) {p->tex_Texture_SecondaryColor = sampler;qglUniform1i(p->loc_Texture_SecondaryColor , sampler);sampler++;}
1214 if (p->loc_Texture_SecondaryGloss >= 0) {p->tex_Texture_SecondaryGloss = sampler;qglUniform1i(p->loc_Texture_SecondaryGloss , sampler);sampler++;}
1215 if (p->loc_Texture_SecondaryGlow >= 0) {p->tex_Texture_SecondaryGlow = sampler;qglUniform1i(p->loc_Texture_SecondaryGlow , sampler);sampler++;}
1216 if (p->loc_Texture_Pants >= 0) {p->tex_Texture_Pants = sampler;qglUniform1i(p->loc_Texture_Pants , sampler);sampler++;}
1217 if (p->loc_Texture_Shirt >= 0) {p->tex_Texture_Shirt = sampler;qglUniform1i(p->loc_Texture_Shirt , sampler);sampler++;}
1218 if (p->loc_Texture_FogHeightTexture>= 0) {p->tex_Texture_FogHeightTexture = sampler;qglUniform1i(p->loc_Texture_FogHeightTexture, sampler);sampler++;}
1219 if (p->loc_Texture_FogMask >= 0) {p->tex_Texture_FogMask = sampler;qglUniform1i(p->loc_Texture_FogMask , sampler);sampler++;}
1220 if (p->loc_Texture_Lightmap >= 0) {p->tex_Texture_Lightmap = sampler;qglUniform1i(p->loc_Texture_Lightmap , sampler);sampler++;}
1221 if (p->loc_Texture_Deluxemap >= 0) {p->tex_Texture_Deluxemap = sampler;qglUniform1i(p->loc_Texture_Deluxemap , sampler);sampler++;}
1222 if (p->loc_Texture_Attenuation >= 0) {p->tex_Texture_Attenuation = sampler;qglUniform1i(p->loc_Texture_Attenuation , sampler);sampler++;}
1223 if (p->loc_Texture_Cube >= 0) {p->tex_Texture_Cube = sampler;qglUniform1i(p->loc_Texture_Cube , sampler);sampler++;}
1224 if (p->loc_Texture_Refraction >= 0) {p->tex_Texture_Refraction = sampler;qglUniform1i(p->loc_Texture_Refraction , sampler);sampler++;}
1225 if (p->loc_Texture_Reflection >= 0) {p->tex_Texture_Reflection = sampler;qglUniform1i(p->loc_Texture_Reflection , sampler);sampler++;}
1226 if (p->loc_Texture_ShadowMap2D >= 0) {p->tex_Texture_ShadowMap2D = sampler;qglUniform1i(p->loc_Texture_ShadowMap2D , sampler);sampler++;}
1227 if (p->loc_Texture_CubeProjection >= 0) {p->tex_Texture_CubeProjection = sampler;qglUniform1i(p->loc_Texture_CubeProjection , sampler);sampler++;}
1228 if (p->loc_Texture_ScreenNormalMap >= 0) {p->tex_Texture_ScreenNormalMap = sampler;qglUniform1i(p->loc_Texture_ScreenNormalMap , sampler);sampler++;}
1229 if (p->loc_Texture_ScreenDiffuse >= 0) {p->tex_Texture_ScreenDiffuse = sampler;qglUniform1i(p->loc_Texture_ScreenDiffuse , sampler);sampler++;}
1230 if (p->loc_Texture_ScreenSpecular >= 0) {p->tex_Texture_ScreenSpecular = sampler;qglUniform1i(p->loc_Texture_ScreenSpecular , sampler);sampler++;}
1231 if (p->loc_Texture_ReflectMask >= 0) {p->tex_Texture_ReflectMask = sampler;qglUniform1i(p->loc_Texture_ReflectMask , sampler);sampler++;}
1232 if (p->loc_Texture_ReflectCube >= 0) {p->tex_Texture_ReflectCube = sampler;qglUniform1i(p->loc_Texture_ReflectCube , sampler);sampler++;}
1233 if (p->loc_Texture_BounceGrid >= 0) {p->tex_Texture_BounceGrid = sampler;qglUniform1i(p->loc_Texture_BounceGrid , sampler);sampler++;}
1235 Con_DPrintf("^5GLSL shader %s compiled (%i textures).\n", permutationname, sampler);
1238 Con_Printf("^1GLSL shader %s failed! some features may not work properly.\n", permutationname);
1242 Mem_Free(vertexstring);
1244 Mem_Free(geometrystring);
1246 Mem_Free(fragmentstring);
1249 static void R_SetupShader_SetPermutationGLSL(unsigned int mode, unsigned int permutation)
1251 r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
1252 if (r_glsl_permutation != perm)
1254 r_glsl_permutation = perm;
1255 if (!r_glsl_permutation->program)
1257 if (!r_glsl_permutation->compiled)
1258 R_GLSL_CompilePermutation(perm, mode, permutation);
1259 if (!r_glsl_permutation->program)
1261 // remove features until we find a valid permutation
1263 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1265 // reduce i more quickly whenever it would not remove any bits
1266 int j = 1<<(SHADERPERMUTATION_COUNT-1-i);
1267 if (!(permutation & j))
1270 r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
1271 if (!r_glsl_permutation->compiled)
1272 R_GLSL_CompilePermutation(perm, mode, permutation);
1273 if (r_glsl_permutation->program)
1276 if (i >= SHADERPERMUTATION_COUNT)
1278 //Con_Printf("Could not find a working OpenGL 2.0 shader for permutation %s %s\n", shadermodeinfo[mode].vertexfilename, shadermodeinfo[mode].pretext);
1279 r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
1280 qglUseProgram(0);CHECKGLERROR
1281 return; // no bit left to clear, entire mode is broken
1286 qglUseProgram(r_glsl_permutation->program);CHECKGLERROR
1288 if (r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
1289 if (r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
1290 if (r_glsl_permutation->loc_ClientTime >= 0) qglUniform1f(r_glsl_permutation->loc_ClientTime, cl.time);
1297 extern LPDIRECT3DDEVICE9 vid_d3d9dev;
1298 extern D3DCAPS9 vid_d3d9caps;
1301 struct r_hlsl_permutation_s;
1302 typedef struct r_hlsl_permutation_s
1304 /// hash lookup data
1305 struct r_hlsl_permutation_s *hashnext;
1307 unsigned int permutation;
1309 /// indicates if we have tried compiling this permutation already
1311 /// NULL if compilation failed
1312 IDirect3DVertexShader9 *vertexshader;
1313 IDirect3DPixelShader9 *pixelshader;
1315 r_hlsl_permutation_t;
1317 typedef enum D3DVSREGISTER_e
1319 D3DVSREGISTER_TexMatrix = 0, // float4x4
1320 D3DVSREGISTER_BackgroundTexMatrix = 4, // float4x4
1321 D3DVSREGISTER_ModelViewProjectionMatrix = 8, // float4x4
1322 D3DVSREGISTER_ModelViewMatrix = 12, // float4x4
1323 D3DVSREGISTER_ShadowMapMatrix = 16, // float4x4
1324 D3DVSREGISTER_ModelToLight = 20, // float4x4
1325 D3DVSREGISTER_EyePosition = 24,
1326 D3DVSREGISTER_FogPlane = 25,
1327 D3DVSREGISTER_LightDir = 26,
1328 D3DVSREGISTER_LightPosition = 27,
1332 typedef enum D3DPSREGISTER_e
1334 D3DPSREGISTER_Alpha = 0,
1335 D3DPSREGISTER_BloomBlur_Parameters = 1,
1336 D3DPSREGISTER_ClientTime = 2,
1337 D3DPSREGISTER_Color_Ambient = 3,
1338 D3DPSREGISTER_Color_Diffuse = 4,
1339 D3DPSREGISTER_Color_Specular = 5,
1340 D3DPSREGISTER_Color_Glow = 6,
1341 D3DPSREGISTER_Color_Pants = 7,
1342 D3DPSREGISTER_Color_Shirt = 8,
1343 D3DPSREGISTER_DeferredColor_Ambient = 9,
1344 D3DPSREGISTER_DeferredColor_Diffuse = 10,
1345 D3DPSREGISTER_DeferredColor_Specular = 11,
1346 D3DPSREGISTER_DeferredMod_Diffuse = 12,
1347 D3DPSREGISTER_DeferredMod_Specular = 13,
1348 D3DPSREGISTER_DistortScaleRefractReflect = 14,
1349 D3DPSREGISTER_EyePosition = 15, // unused
1350 D3DPSREGISTER_FogColor = 16,
1351 D3DPSREGISTER_FogHeightFade = 17,
1352 D3DPSREGISTER_FogPlane = 18,
1353 D3DPSREGISTER_FogPlaneViewDist = 19,
1354 D3DPSREGISTER_FogRangeRecip = 20,
1355 D3DPSREGISTER_LightColor = 21,
1356 D3DPSREGISTER_LightDir = 22, // unused
1357 D3DPSREGISTER_LightPosition = 23,
1358 D3DPSREGISTER_OffsetMapping_ScaleSteps = 24,
1359 D3DPSREGISTER_PixelSize = 25,
1360 D3DPSREGISTER_ReflectColor = 26,
1361 D3DPSREGISTER_ReflectFactor = 27,
1362 D3DPSREGISTER_ReflectOffset = 28,
1363 D3DPSREGISTER_RefractColor = 29,
1364 D3DPSREGISTER_Saturation = 30,
1365 D3DPSREGISTER_ScreenCenterRefractReflect = 31,
1366 D3DPSREGISTER_ScreenScaleRefractReflect = 32,
1367 D3DPSREGISTER_ScreenToDepth = 33,
1368 D3DPSREGISTER_ShadowMap_Parameters = 34,
1369 D3DPSREGISTER_ShadowMap_TextureScale = 35,
1370 D3DPSREGISTER_SpecularPower = 36,
1371 D3DPSREGISTER_UserVec1 = 37,
1372 D3DPSREGISTER_UserVec2 = 38,
1373 D3DPSREGISTER_UserVec3 = 39,
1374 D3DPSREGISTER_UserVec4 = 40,
1375 D3DPSREGISTER_ViewTintColor = 41,
1376 D3DPSREGISTER_PixelToScreenTexCoord = 42,
1377 D3DPSREGISTER_BloomColorSubtract = 43,
1378 D3DPSREGISTER_ViewToLight = 44, // float4x4
1379 D3DPSREGISTER_ModelToReflectCube = 48, // float4x4
1380 D3DPSREGISTER_NormalmapScrollBlend = 52,
1381 D3DPSREGISTER_OffsetMapping_LodDistance = 53,
1382 D3DPSREGISTER_OffsetMapping_Bias = 54,
1387 /// information about each possible shader permutation
1388 r_hlsl_permutation_t *r_hlsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
1389 /// currently selected permutation
1390 r_hlsl_permutation_t *r_hlsl_permutation;
1391 /// storage for permutations linked in the hash table
1392 memexpandablearray_t r_hlsl_permutationarray;
1394 static r_hlsl_permutation_t *R_HLSL_FindPermutation(unsigned int mode, unsigned int permutation)
1396 //unsigned int hashdepth = 0;
1397 unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
1398 r_hlsl_permutation_t *p;
1399 for (p = r_hlsl_permutationhash[mode][hashindex];p;p = p->hashnext)
1401 if (p->mode == mode && p->permutation == permutation)
1403 //if (hashdepth > 10)
1404 // Con_Printf("R_HLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
1409 p = (r_hlsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_hlsl_permutationarray);
1411 p->permutation = permutation;
1412 p->hashnext = r_hlsl_permutationhash[mode][hashindex];
1413 r_hlsl_permutationhash[mode][hashindex] = p;
1414 //if (hashdepth > 10)
1415 // Con_Printf("R_HLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
1419 static char *R_HLSL_GetText(const char *filename, qboolean printfromdisknotice)
1422 if (!filename || !filename[0])
1424 if (!strcmp(filename, "hlsl/default.hlsl"))
1426 if (!hlslshaderstring)
1428 hlslshaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
1429 if (hlslshaderstring)
1430 Con_DPrintf("Loading shaders from file %s...\n", filename);
1432 hlslshaderstring = (char *)builtinhlslshaderstring;
1434 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(hlslshaderstring) + 1);
1435 memcpy(shaderstring, hlslshaderstring, strlen(hlslshaderstring) + 1);
1436 return shaderstring;
1438 shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
1441 if (printfromdisknotice)
1442 Con_DPrintf("from disk %s... ", filename);
1443 return shaderstring;
1445 return shaderstring;
1449 //#include <d3dx9shader.h>
1450 //#include <d3dx9mesh.h>
1452 static void R_HLSL_CacheShader(r_hlsl_permutation_t *p, const char *cachename, const char *vertstring, const char *fragstring)
1454 DWORD *vsbin = NULL;
1455 DWORD *psbin = NULL;
1456 fs_offset_t vsbinsize;
1457 fs_offset_t psbinsize;
1458 // IDirect3DVertexShader9 *vs = NULL;
1459 // IDirect3DPixelShader9 *ps = NULL;
1460 ID3DXBuffer *vslog = NULL;
1461 ID3DXBuffer *vsbuffer = NULL;
1462 ID3DXConstantTable *vsconstanttable = NULL;
1463 ID3DXBuffer *pslog = NULL;
1464 ID3DXBuffer *psbuffer = NULL;
1465 ID3DXConstantTable *psconstanttable = NULL;
1468 char temp[MAX_INPUTLINE];
1469 const char *vsversion = "vs_3_0", *psversion = "ps_3_0";
1471 qboolean debugshader = gl_paranoid.integer != 0;
1472 if (p->permutation & SHADERPERMUTATION_OFFSETMAPPING) {vsversion = "vs_3_0";psversion = "ps_3_0";}
1473 if (p->permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) {vsversion = "vs_3_0";psversion = "ps_3_0";}
1476 vsbin = (DWORD *)FS_LoadFile(va(vabuf, sizeof(vabuf), "%s.vsbin", cachename), r_main_mempool, true, &vsbinsize);
1477 psbin = (DWORD *)FS_LoadFile(va(vabuf, sizeof(vabuf), "%s.psbin", cachename), r_main_mempool, true, &psbinsize);
1479 if ((!vsbin && vertstring) || (!psbin && fragstring))
1481 const char* dllnames_d3dx9 [] =
1505 dllhandle_t d3dx9_dll = NULL;
1506 HRESULT (WINAPI *qD3DXCompileShaderFromFileA)(LPCSTR pSrcFile, CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable);
1507 HRESULT (WINAPI *qD3DXPreprocessShader)(LPCSTR pSrcData, UINT SrcDataSize, CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPD3DXBUFFER* ppShaderText, LPD3DXBUFFER* ppErrorMsgs);
1508 HRESULT (WINAPI *qD3DXCompileShader)(LPCSTR pSrcData, UINT SrcDataLen, CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable);
1509 dllfunction_t d3dx9_dllfuncs[] =
1511 {"D3DXCompileShaderFromFileA", (void **) &qD3DXCompileShaderFromFileA},
1512 {"D3DXPreprocessShader", (void **) &qD3DXPreprocessShader},
1513 {"D3DXCompileShader", (void **) &qD3DXCompileShader},
1516 if (Sys_LoadLibrary(dllnames_d3dx9, &d3dx9_dll, d3dx9_dllfuncs))
1518 DWORD shaderflags = 0;
1520 shaderflags = D3DXSHADER_DEBUG | D3DXSHADER_SKIPOPTIMIZATION;
1521 vsbin = (DWORD *)Mem_Realloc(tempmempool, vsbin, 0);
1522 psbin = (DWORD *)Mem_Realloc(tempmempool, psbin, 0);
1523 if (vertstring && vertstring[0])
1527 // vsresult = qD3DXPreprocessShader(vertstring, strlen(vertstring), NULL, NULL, &vsbuffer, &vslog);
1528 // FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_vs.fx", cachename), vsbuffer->GetBufferPointer(), vsbuffer->GetBufferSize());
1529 FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_vs.fx", cachename), vertstring, strlen(vertstring));
1530 vsresult = qD3DXCompileShaderFromFileA(va(vabuf, sizeof(vabuf), "%s/%s_vs.fx", fs_gamedir, cachename), NULL, NULL, "main", vsversion, shaderflags, &vsbuffer, &vslog, &vsconstanttable);
1533 vsresult = qD3DXCompileShader(vertstring, strlen(vertstring), NULL, NULL, "main", vsversion, shaderflags, &vsbuffer, &vslog, &vsconstanttable);
1536 vsbinsize = vsbuffer->GetBufferSize();
1537 vsbin = (DWORD *)Mem_Alloc(tempmempool, vsbinsize);
1538 memcpy(vsbin, vsbuffer->GetBufferPointer(), vsbinsize);
1539 vsbuffer->Release();
1543 strlcpy(temp, (const char *)vslog->GetBufferPointer(), min(sizeof(temp), vslog->GetBufferSize()));
1544 Con_DPrintf("HLSL vertex shader compile output for %s follows:\n%s\n", cachename, temp);
1548 if (fragstring && fragstring[0])
1552 // psresult = qD3DXPreprocessShader(fragstring, strlen(fragstring), NULL, NULL, &psbuffer, &pslog);
1553 // FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_ps.fx", cachename), psbuffer->GetBufferPointer(), psbuffer->GetBufferSize());
1554 FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_ps.fx", cachename), fragstring, strlen(fragstring));
1555 psresult = qD3DXCompileShaderFromFileA(va(vabuf, sizeof(vabuf), "%s/%s_ps.fx", fs_gamedir, cachename), NULL, NULL, "main", psversion, shaderflags, &psbuffer, &pslog, &psconstanttable);
1558 psresult = qD3DXCompileShader(fragstring, strlen(fragstring), NULL, NULL, "main", psversion, shaderflags, &psbuffer, &pslog, &psconstanttable);
1561 psbinsize = psbuffer->GetBufferSize();
1562 psbin = (DWORD *)Mem_Alloc(tempmempool, psbinsize);
1563 memcpy(psbin, psbuffer->GetBufferPointer(), psbinsize);
1564 psbuffer->Release();
1568 strlcpy(temp, (const char *)pslog->GetBufferPointer(), min(sizeof(temp), pslog->GetBufferSize()));
1569 Con_DPrintf("HLSL pixel shader compile output for %s follows:\n%s\n", cachename, temp);
1573 Sys_UnloadLibrary(&d3dx9_dll);
1576 Con_DPrintf("Unable to compile shader - D3DXCompileShader function not found\n");
1580 vsresult = IDirect3DDevice9_CreateVertexShader(vid_d3d9dev, vsbin, &p->vertexshader);
1581 if (FAILED(vsresult))
1582 Con_DPrintf("HLSL CreateVertexShader failed for %s (hresult = %8x)\n", cachename, vsresult);
1583 psresult = IDirect3DDevice9_CreatePixelShader(vid_d3d9dev, psbin, &p->pixelshader);
1584 if (FAILED(psresult))
1585 Con_DPrintf("HLSL CreatePixelShader failed for %s (hresult = %8x)\n", cachename, psresult);
1587 // free the shader data
1588 vsbin = (DWORD *)Mem_Realloc(tempmempool, vsbin, 0);
1589 psbin = (DWORD *)Mem_Realloc(tempmempool, psbin, 0);
1592 static void R_HLSL_CompilePermutation(r_hlsl_permutation_t *p, unsigned int mode, unsigned int permutation)
1595 shadermodeinfo_t *modeinfo = hlslshadermodeinfo + mode;
1596 int vertstring_length = 0;
1597 int geomstring_length = 0;
1598 int fragstring_length = 0;
1600 char *vertexstring, *geometrystring, *fragmentstring;
1601 char *vertstring, *geomstring, *fragstring;
1602 char permutationname[256];
1603 char cachename[256];
1604 int vertstrings_count = 0;
1605 int geomstrings_count = 0;
1606 int fragstrings_count = 0;
1607 const char *vertstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1608 const char *geomstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1609 const char *fragstrings_list[32+5+SHADERSTATICPARMS_COUNT+1];
1614 p->vertexshader = NULL;
1615 p->pixelshader = NULL;
1617 permutationname[0] = 0;
1619 vertexstring = R_HLSL_GetText(modeinfo->vertexfilename, true);
1620 geometrystring = R_HLSL_GetText(modeinfo->geometryfilename, false);
1621 fragmentstring = R_HLSL_GetText(modeinfo->fragmentfilename, false);
1623 strlcat(permutationname, modeinfo->vertexfilename, sizeof(permutationname));
1624 strlcat(cachename, "hlsl/", sizeof(cachename));
1626 // define HLSL so that the shader can tell apart the HLSL compiler and the Cg compiler
1627 vertstrings_count = 0;
1628 geomstrings_count = 0;
1629 fragstrings_count = 0;
1630 vertstrings_list[vertstrings_count++] = "#define HLSL\n";
1631 geomstrings_list[geomstrings_count++] = "#define HLSL\n";
1632 fragstrings_list[fragstrings_count++] = "#define HLSL\n";
1634 // the first pretext is which type of shader to compile as
1635 // (later these will all be bound together as a program object)
1636 vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
1637 geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
1638 fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
1640 // the second pretext is the mode (for example a light source)
1641 vertstrings_list[vertstrings_count++] = modeinfo->pretext;
1642 geomstrings_list[geomstrings_count++] = modeinfo->pretext;
1643 fragstrings_list[fragstrings_count++] = modeinfo->pretext;
1644 strlcat(permutationname, modeinfo->name, sizeof(permutationname));
1645 strlcat(cachename, modeinfo->name, sizeof(cachename));
1647 // now add all the permutation pretexts
1648 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1650 if (permutation & (1<<i))
1652 vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
1653 geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
1654 fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
1655 strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
1656 strlcat(cachename, shaderpermutationinfo[i].name, sizeof(cachename));
1660 // keep line numbers correct
1661 vertstrings_list[vertstrings_count++] = "\n";
1662 geomstrings_list[geomstrings_count++] = "\n";
1663 fragstrings_list[fragstrings_count++] = "\n";
1668 R_CompileShader_AddStaticParms(mode, permutation);
1669 memcpy(vertstrings_list + vertstrings_count, shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1670 vertstrings_count += shaderstaticparms_count;
1671 memcpy(geomstrings_list + geomstrings_count, shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1672 geomstrings_count += shaderstaticparms_count;
1673 memcpy(fragstrings_list + fragstrings_count, shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
1674 fragstrings_count += shaderstaticparms_count;
1676 // replace spaces in the cachename with _ characters
1677 for (i = 0;cachename[i];i++)
1678 if (cachename[i] == ' ')
1681 // now append the shader text itself
1682 vertstrings_list[vertstrings_count++] = vertexstring;
1683 geomstrings_list[geomstrings_count++] = geometrystring;
1684 fragstrings_list[fragstrings_count++] = fragmentstring;
1686 // if any sources were NULL, clear the respective list
1688 vertstrings_count = 0;
1689 if (!geometrystring)
1690 geomstrings_count = 0;
1691 if (!fragmentstring)
1692 fragstrings_count = 0;
1694 vertstring_length = 0;
1695 for (i = 0;i < vertstrings_count;i++)
1696 vertstring_length += strlen(vertstrings_list[i]);
1697 vertstring = t = (char *)Mem_Alloc(tempmempool, vertstring_length + 1);
1698 for (i = 0;i < vertstrings_count;t += strlen(vertstrings_list[i]), i++)
1699 memcpy(t, vertstrings_list[i], strlen(vertstrings_list[i]));
1701 geomstring_length = 0;
1702 for (i = 0;i < geomstrings_count;i++)
1703 geomstring_length += strlen(geomstrings_list[i]);
1704 geomstring = t = (char *)Mem_Alloc(tempmempool, geomstring_length + 1);
1705 for (i = 0;i < geomstrings_count;t += strlen(geomstrings_list[i]), i++)
1706 memcpy(t, geomstrings_list[i], strlen(geomstrings_list[i]));
1708 fragstring_length = 0;
1709 for (i = 0;i < fragstrings_count;i++)
1710 fragstring_length += strlen(fragstrings_list[i]);
1711 fragstring = t = (char *)Mem_Alloc(tempmempool, fragstring_length + 1);
1712 for (i = 0;i < fragstrings_count;t += strlen(fragstrings_list[i]), i++)
1713 memcpy(t, fragstrings_list[i], strlen(fragstrings_list[i]));
1715 // try to load the cached shader, or generate one
1716 R_HLSL_CacheShader(p, cachename, vertstring, fragstring);
1718 if ((p->vertexshader || !vertstring[0]) && (p->pixelshader || !fragstring[0]))
1719 Con_DPrintf("^5HLSL shader %s compiled.\n", permutationname);
1721 Con_Printf("^1HLSL shader %s failed! some features may not work properly.\n", permutationname);
1725 Mem_Free(vertstring);
1727 Mem_Free(geomstring);
1729 Mem_Free(fragstring);
1731 Mem_Free(vertexstring);
1733 Mem_Free(geometrystring);
1735 Mem_Free(fragmentstring);
1738 static inline void hlslVSSetParameter16f(D3DVSREGISTER_t r, const float *a) {IDirect3DDevice9_SetVertexShaderConstantF(vid_d3d9dev, r, a, 4);}
1739 static inline void hlslVSSetParameter4fv(D3DVSREGISTER_t r, const float *a) {IDirect3DDevice9_SetVertexShaderConstantF(vid_d3d9dev, r, a, 1);}
1740 static inline void hlslVSSetParameter4f(D3DVSREGISTER_t r, float x, float y, float z, float w) {float temp[4];Vector4Set(temp, x, y, z, w);IDirect3DDevice9_SetVertexShaderConstantF(vid_d3d9dev, r, temp, 1);}
1741 static inline void hlslVSSetParameter3f(D3DVSREGISTER_t r, float x, float y, float z) {float temp[4];Vector4Set(temp, x, y, z, 0);IDirect3DDevice9_SetVertexShaderConstantF(vid_d3d9dev, r, temp, 1);}
1742 static inline void hlslVSSetParameter2f(D3DVSREGISTER_t r, float x, float y) {float temp[4];Vector4Set(temp, x, y, 0, 0);IDirect3DDevice9_SetVertexShaderConstantF(vid_d3d9dev, r, temp, 1);}
1743 static inline void hlslVSSetParameter1f(D3DVSREGISTER_t r, float x) {float temp[4];Vector4Set(temp, x, 0, 0, 0);IDirect3DDevice9_SetVertexShaderConstantF(vid_d3d9dev, r, temp, 1);}
1745 static inline void hlslPSSetParameter16f(D3DPSREGISTER_t r, const float *a) {IDirect3DDevice9_SetPixelShaderConstantF(vid_d3d9dev, r, a, 4);}
1746 static inline void hlslPSSetParameter4fv(D3DPSREGISTER_t r, const float *a) {IDirect3DDevice9_SetPixelShaderConstantF(vid_d3d9dev, r, a, 1);}
1747 static inline void hlslPSSetParameter4f(D3DPSREGISTER_t r, float x, float y, float z, float w) {float temp[4];Vector4Set(temp, x, y, z, w);IDirect3DDevice9_SetPixelShaderConstantF(vid_d3d9dev, r, temp, 1);}
1748 static inline void hlslPSSetParameter3f(D3DPSREGISTER_t r, float x, float y, float z) {float temp[4];Vector4Set(temp, x, y, z, 0);IDirect3DDevice9_SetPixelShaderConstantF(vid_d3d9dev, r, temp, 1);}
1749 static inline void hlslPSSetParameter2f(D3DPSREGISTER_t r, float x, float y) {float temp[4];Vector4Set(temp, x, y, 0, 0);IDirect3DDevice9_SetPixelShaderConstantF(vid_d3d9dev, r, temp, 1);}
1750 static inline void hlslPSSetParameter1f(D3DPSREGISTER_t r, float x) {float temp[4];Vector4Set(temp, x, 0, 0, 0);IDirect3DDevice9_SetPixelShaderConstantF(vid_d3d9dev, r, temp, 1);}
1752 void R_SetupShader_SetPermutationHLSL(unsigned int mode, unsigned int permutation)
1754 r_hlsl_permutation_t *perm = R_HLSL_FindPermutation(mode, permutation);
1755 if (r_hlsl_permutation != perm)
1757 r_hlsl_permutation = perm;
1758 if (!r_hlsl_permutation->vertexshader && !r_hlsl_permutation->pixelshader)
1760 if (!r_hlsl_permutation->compiled)
1761 R_HLSL_CompilePermutation(perm, mode, permutation);
1762 if (!r_hlsl_permutation->vertexshader && !r_hlsl_permutation->pixelshader)
1764 // remove features until we find a valid permutation
1766 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1768 // reduce i more quickly whenever it would not remove any bits
1769 int j = 1<<(SHADERPERMUTATION_COUNT-1-i);
1770 if (!(permutation & j))
1773 r_hlsl_permutation = R_HLSL_FindPermutation(mode, permutation);
1774 if (!r_hlsl_permutation->compiled)
1775 R_HLSL_CompilePermutation(perm, mode, permutation);
1776 if (r_hlsl_permutation->vertexshader || r_hlsl_permutation->pixelshader)
1779 if (i >= SHADERPERMUTATION_COUNT)
1781 //Con_Printf("Could not find a working HLSL shader for permutation %s %s\n", shadermodeinfo[mode].vertexfilename, shadermodeinfo[mode].pretext);
1782 r_hlsl_permutation = R_HLSL_FindPermutation(mode, permutation);
1783 return; // no bit left to clear, entire mode is broken
1787 IDirect3DDevice9_SetVertexShader(vid_d3d9dev, r_hlsl_permutation->vertexshader);
1788 IDirect3DDevice9_SetPixelShader(vid_d3d9dev, r_hlsl_permutation->pixelshader);
1790 hlslVSSetParameter16f(D3DVSREGISTER_ModelViewProjectionMatrix, gl_modelviewprojection16f);
1791 hlslVSSetParameter16f(D3DVSREGISTER_ModelViewMatrix, gl_modelview16f);
1792 hlslPSSetParameter1f(D3DPSREGISTER_ClientTime, cl.time);
1796 static void R_SetupShader_SetPermutationSoft(unsigned int mode, unsigned int permutation)
1798 DPSOFTRAST_SetShader(mode, permutation, r_shadow_glossexact.integer);
1799 DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1, 1, false, gl_modelviewprojection16f);
1800 DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelViewMatrixM1, 1, false, gl_modelview16f);
1801 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_ClientTime, cl.time);
1804 void R_GLSL_Restart_f(void)
1806 unsigned int i, limit;
1807 if (glslshaderstring && glslshaderstring != builtinshaderstring)
1808 Mem_Free(glslshaderstring);
1809 glslshaderstring = NULL;
1810 if (hlslshaderstring && hlslshaderstring != builtinhlslshaderstring)
1811 Mem_Free(hlslshaderstring);
1812 hlslshaderstring = NULL;
1813 switch(vid.renderpath)
1815 case RENDERPATH_D3D9:
1818 r_hlsl_permutation_t *p;
1819 r_hlsl_permutation = NULL;
1820 limit = Mem_ExpandableArray_IndexRange(&r_hlsl_permutationarray);
1821 for (i = 0;i < limit;i++)
1823 if ((p = (r_hlsl_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_hlsl_permutationarray, i)))
1825 if (p->vertexshader)
1826 IDirect3DVertexShader9_Release(p->vertexshader);
1828 IDirect3DPixelShader9_Release(p->pixelshader);
1829 Mem_ExpandableArray_FreeRecord(&r_hlsl_permutationarray, (void*)p);
1832 memset(r_hlsl_permutationhash, 0, sizeof(r_hlsl_permutationhash));
1836 case RENDERPATH_D3D10:
1837 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1839 case RENDERPATH_D3D11:
1840 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1842 case RENDERPATH_GL20:
1843 case RENDERPATH_GLES2:
1845 r_glsl_permutation_t *p;
1846 r_glsl_permutation = NULL;
1847 limit = Mem_ExpandableArray_IndexRange(&r_glsl_permutationarray);
1848 for (i = 0;i < limit;i++)
1850 if ((p = (r_glsl_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_glsl_permutationarray, i)))
1852 GL_Backend_FreeProgram(p->program);
1853 Mem_ExpandableArray_FreeRecord(&r_glsl_permutationarray, (void*)p);
1856 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
1859 case RENDERPATH_GL11:
1860 case RENDERPATH_GL13:
1861 case RENDERPATH_GLES1:
1863 case RENDERPATH_SOFT:
1868 static void R_GLSL_DumpShader_f(void)
1873 file = FS_OpenRealFile("glsl/default.glsl", "w", false);
1876 FS_Print(file, "/* The engine may define the following macros:\n");
1877 FS_Print(file, "#define VERTEX_SHADER\n#define GEOMETRY_SHADER\n#define FRAGMENT_SHADER\n");
1878 for (i = 0;i < SHADERMODE_COUNT;i++)
1879 FS_Print(file, glslshadermodeinfo[i].pretext);
1880 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1881 FS_Print(file, shaderpermutationinfo[i].pretext);
1882 FS_Print(file, "*/\n");
1883 FS_Print(file, builtinshaderstring);
1885 Con_Printf("glsl/default.glsl written\n");
1888 Con_Printf("failed to write to glsl/default.glsl\n");
1890 file = FS_OpenRealFile("hlsl/default.hlsl", "w", false);
1893 FS_Print(file, "/* The engine may define the following macros:\n");
1894 FS_Print(file, "#define VERTEX_SHADER\n#define GEOMETRY_SHADER\n#define FRAGMENT_SHADER\n");
1895 for (i = 0;i < SHADERMODE_COUNT;i++)
1896 FS_Print(file, hlslshadermodeinfo[i].pretext);
1897 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1898 FS_Print(file, shaderpermutationinfo[i].pretext);
1899 FS_Print(file, "*/\n");
1900 FS_Print(file, builtinhlslshaderstring);
1902 Con_Printf("hlsl/default.hlsl written\n");
1905 Con_Printf("failed to write to hlsl/default.hlsl\n");
1908 void R_SetupShader_Generic(rtexture_t *first, rtexture_t *second, int texturemode, int rgbscale, qboolean usegamma, qboolean notrippy, qboolean suppresstexalpha)
1910 unsigned int permutation = 0;
1911 if (r_trippy.integer && !notrippy)
1912 permutation |= SHADERPERMUTATION_TRIPPY;
1913 permutation |= SHADERPERMUTATION_VIEWTINT;
1915 permutation |= SHADERPERMUTATION_DIFFUSE;
1917 permutation |= SHADERPERMUTATION_SPECULAR;
1918 if (texturemode == GL_MODULATE)
1919 permutation |= SHADERPERMUTATION_COLORMAPPING;
1920 else if (texturemode == GL_ADD)
1921 permutation |= SHADERPERMUTATION_GLOW;
1922 else if (texturemode == GL_DECAL)
1923 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
1924 if (usegamma && v_glslgamma.integer && v_glslgamma_2d.integer && !vid.sRGB2D && r_texture_gammaramps && !vid_gammatables_trivial)
1925 permutation |= SHADERPERMUTATION_GAMMARAMPS;
1926 if (suppresstexalpha)
1927 permutation |= SHADERPERMUTATION_REFLECTCUBE;
1929 texturemode = GL_MODULATE;
1930 if (vid.allowalphatocoverage)
1931 GL_AlphaToCoverage(false);
1932 switch (vid.renderpath)
1934 case RENDERPATH_D3D9:
1936 R_SetupShader_SetPermutationHLSL(SHADERMODE_GENERIC, permutation);
1937 R_Mesh_TexBind(GL20TU_FIRST , first );
1938 R_Mesh_TexBind(GL20TU_SECOND, second);
1939 if (permutation & SHADERPERMUTATION_GAMMARAMPS)
1940 R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps);
1943 case RENDERPATH_D3D10:
1944 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1946 case RENDERPATH_D3D11:
1947 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1949 case RENDERPATH_GL20:
1950 case RENDERPATH_GLES2:
1951 R_SetupShader_SetPermutationGLSL(SHADERMODE_GENERIC, permutation);
1952 R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , first );
1953 R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second, second);
1954 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0)
1955 R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps);
1957 case RENDERPATH_GL13:
1958 case RENDERPATH_GLES1:
1959 R_Mesh_TexBind(0, first );
1960 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
1961 R_Mesh_TexBind(1, second);
1963 R_Mesh_TexCombine(1, texturemode, texturemode, rgbscale, 1);
1965 case RENDERPATH_GL11:
1966 R_Mesh_TexBind(0, first );
1968 case RENDERPATH_SOFT:
1969 R_SetupShader_SetPermutationSoft(SHADERMODE_GENERIC, permutation);
1970 R_Mesh_TexBind(GL20TU_FIRST , first );
1971 R_Mesh_TexBind(GL20TU_SECOND, second);
1976 void R_SetupShader_Generic_NoTexture(qboolean usegamma, qboolean notrippy)
1978 R_SetupShader_Generic(NULL, NULL, GL_MODULATE, 1, usegamma, notrippy, false);
1981 void R_SetupShader_DepthOrShadow(qboolean notrippy, qboolean depthrgb)
1983 unsigned int permutation = 0;
1984 if (r_trippy.integer && !notrippy)
1985 permutation |= SHADERPERMUTATION_TRIPPY;
1987 permutation |= SHADERPERMUTATION_DEPTHRGB;
1988 if (vid.allowalphatocoverage)
1989 GL_AlphaToCoverage(false);
1990 switch (vid.renderpath)
1992 case RENDERPATH_D3D9:
1994 R_SetupShader_SetPermutationHLSL(SHADERMODE_DEPTH_OR_SHADOW, permutation);
1997 case RENDERPATH_D3D10:
1998 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2000 case RENDERPATH_D3D11:
2001 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2003 case RENDERPATH_GL20:
2004 case RENDERPATH_GLES2:
2005 R_SetupShader_SetPermutationGLSL(SHADERMODE_DEPTH_OR_SHADOW, permutation);
2007 case RENDERPATH_GL13:
2008 case RENDERPATH_GLES1:
2009 R_Mesh_TexBind(0, 0);
2010 R_Mesh_TexBind(1, 0);
2012 case RENDERPATH_GL11:
2013 R_Mesh_TexBind(0, 0);
2015 case RENDERPATH_SOFT:
2016 R_SetupShader_SetPermutationSoft(SHADERMODE_DEPTH_OR_SHADOW, permutation);
2021 void R_SetupShader_ShowDepth(qboolean notrippy)
2023 int permutation = 0;
2024 if (r_trippy.integer && !notrippy)
2025 permutation |= SHADERPERMUTATION_TRIPPY;
2026 if (vid.allowalphatocoverage)
2027 GL_AlphaToCoverage(false);
2028 switch (vid.renderpath)
2030 case RENDERPATH_D3D9:
2032 R_SetupShader_SetPermutationHLSL(SHADERMODE_SHOWDEPTH, permutation);
2035 case RENDERPATH_D3D10:
2036 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2038 case RENDERPATH_D3D11:
2039 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2041 case RENDERPATH_GL20:
2042 case RENDERPATH_GLES2:
2043 R_SetupShader_SetPermutationGLSL(SHADERMODE_SHOWDEPTH, permutation);
2045 case RENDERPATH_GL13:
2046 case RENDERPATH_GLES1:
2048 case RENDERPATH_GL11:
2050 case RENDERPATH_SOFT:
2051 R_SetupShader_SetPermutationSoft(SHADERMODE_SHOWDEPTH, permutation);
2056 extern qboolean r_shadow_usingdeferredprepass;
2057 extern rtexture_t *r_shadow_attenuationgradienttexture;
2058 extern rtexture_t *r_shadow_attenuation2dtexture;
2059 extern rtexture_t *r_shadow_attenuation3dtexture;
2060 extern qboolean r_shadow_usingshadowmap2d;
2061 extern qboolean r_shadow_usingshadowmaportho;
2062 extern float r_shadow_shadowmap_texturescale[2];
2063 extern float r_shadow_shadowmap_parameters[4];
2064 extern qboolean r_shadow_shadowmapvsdct;
2065 extern rtexture_t *r_shadow_shadowmap2ddepthbuffer;
2066 extern rtexture_t *r_shadow_shadowmap2ddepthtexture;
2067 extern rtexture_t *r_shadow_shadowmapvsdcttexture;
2068 extern matrix4x4_t r_shadow_shadowmapmatrix;
2069 extern int r_shadow_shadowmaplod; // changes for each light based on distance
2070 extern int r_shadow_prepass_width;
2071 extern int r_shadow_prepass_height;
2072 extern rtexture_t *r_shadow_prepassgeometrydepthbuffer;
2073 extern rtexture_t *r_shadow_prepassgeometrynormalmaptexture;
2074 extern rtexture_t *r_shadow_prepasslightingdiffusetexture;
2075 extern rtexture_t *r_shadow_prepasslightingspeculartexture;
2077 #define BLENDFUNC_ALLOWS_COLORMOD 1
2078 #define BLENDFUNC_ALLOWS_FOG 2
2079 #define BLENDFUNC_ALLOWS_FOG_HACK0 4
2080 #define BLENDFUNC_ALLOWS_FOG_HACKALPHA 8
2081 #define BLENDFUNC_ALLOWS_ANYFOG (BLENDFUNC_ALLOWS_FOG | BLENDFUNC_ALLOWS_FOG_HACK0 | BLENDFUNC_ALLOWS_FOG_HACKALPHA)
2082 static int R_BlendFuncFlags(int src, int dst)
2086 // a blendfunc allows colormod if:
2087 // a) it can never keep the destination pixel invariant, or
2088 // b) it can keep the destination pixel invariant, and still can do so if colormodded
2089 // this is to prevent unintended side effects from colormod
2091 // a blendfunc allows fog if:
2092 // blend(fog(src), fog(dst)) == fog(blend(src, dst))
2093 // this is to prevent unintended side effects from fog
2095 // these checks are the output of fogeval.pl
2097 r |= BLENDFUNC_ALLOWS_COLORMOD;
2098 if(src == GL_DST_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
2099 if(src == GL_DST_ALPHA && dst == GL_ONE_MINUS_DST_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
2100 if(src == GL_DST_COLOR && dst == GL_ONE_MINUS_SRC_ALPHA) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
2101 if(src == GL_DST_COLOR && dst == GL_ONE_MINUS_SRC_COLOR) r |= BLENDFUNC_ALLOWS_FOG;
2102 if(src == GL_DST_COLOR && dst == GL_SRC_ALPHA) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
2103 if(src == GL_DST_COLOR && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
2104 if(src == GL_DST_COLOR && dst == GL_ZERO) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
2105 if(src == GL_ONE && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
2106 if(src == GL_ONE && dst == GL_ONE_MINUS_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG_HACKALPHA;
2107 if(src == GL_ONE && dst == GL_ZERO) r |= BLENDFUNC_ALLOWS_FOG;
2108 if(src == GL_ONE_MINUS_DST_ALPHA && dst == GL_DST_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
2109 if(src == GL_ONE_MINUS_DST_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
2110 if(src == GL_ONE_MINUS_DST_COLOR && dst == GL_SRC_COLOR) r |= BLENDFUNC_ALLOWS_FOG;
2111 if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
2112 if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
2113 if(src == GL_ONE_MINUS_SRC_ALPHA && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
2114 if(src == GL_ONE_MINUS_SRC_COLOR && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
2115 if(src == GL_SRC_ALPHA && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG_HACK0;
2116 if(src == GL_SRC_ALPHA && dst == GL_ONE_MINUS_SRC_ALPHA) r |= BLENDFUNC_ALLOWS_FOG;
2117 if(src == GL_ZERO && dst == GL_ONE) r |= BLENDFUNC_ALLOWS_FOG;
2118 if(src == GL_ZERO && dst == GL_SRC_COLOR) r &= ~BLENDFUNC_ALLOWS_COLORMOD;
2123 void R_SetupShader_Surface(const vec3_t lightcolorbase, qboolean modellighting, float ambientscale, float diffusescale, float specularscale, rsurfacepass_t rsurfacepass, int texturenumsurfaces, const msurface_t **texturesurfacelist, void *surfacewaterplane, qboolean notrippy)
2125 // select a permutation of the lighting shader appropriate to this
2126 // combination of texture, entity, light source, and fogging, only use the
2127 // minimum features necessary to avoid wasting rendering time in the
2128 // fragment shader on features that are not being used
2129 unsigned int permutation = 0;
2130 unsigned int mode = 0;
2132 static float dummy_colormod[3] = {1, 1, 1};
2133 float *colormod = rsurface.colormod;
2135 matrix4x4_t tempmatrix;
2136 r_waterstate_waterplane_t *waterplane = (r_waterstate_waterplane_t *)surfacewaterplane;
2137 if (r_trippy.integer && !notrippy)
2138 permutation |= SHADERPERMUTATION_TRIPPY;
2139 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
2140 permutation |= SHADERPERMUTATION_ALPHAKILL;
2141 if (rsurface.texture->r_water_waterscroll[0] && rsurface.texture->r_water_waterscroll[1])
2142 permutation |= SHADERPERMUTATION_NORMALMAPSCROLLBLEND; // todo: make generic
2143 if (rsurfacepass == RSURFPASS_BACKGROUND)
2145 // distorted background
2146 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
2148 mode = SHADERMODE_WATER;
2149 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
2150 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
2151 if((r_wateralpha.value < 1) && (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERALPHA))
2153 // this is the right thing to do for wateralpha
2154 GL_BlendFunc(GL_ONE, GL_ZERO);
2155 blendfuncflags = R_BlendFuncFlags(GL_ONE, GL_ZERO);
2159 // this is the right thing to do for entity alpha
2160 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2161 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2164 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFRACTION)
2166 mode = SHADERMODE_REFRACTION;
2167 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
2168 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
2169 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2170 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2174 mode = SHADERMODE_GENERIC;
2175 permutation |= SHADERPERMUTATION_DIFFUSE;
2176 GL_BlendFunc(GL_ONE, GL_ZERO);
2177 blendfuncflags = R_BlendFuncFlags(GL_ONE, GL_ZERO);
2179 if (vid.allowalphatocoverage)
2180 GL_AlphaToCoverage(false);
2182 else if (rsurfacepass == RSURFPASS_DEFERREDGEOMETRY)
2184 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(rsurface.texture->nmaptexture) & TEXF_ALPHA) || rsurface.texture->offsetbias != 0.0f))
2186 switch(rsurface.texture->offsetmapping)
2188 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
2189 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2190 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2191 case OFFSETMAPPING_OFF: break;
2194 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2195 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2196 // normalmap (deferred prepass), may use alpha test on diffuse
2197 mode = SHADERMODE_DEFERREDGEOMETRY;
2198 GL_BlendFunc(GL_ONE, GL_ZERO);
2199 blendfuncflags = R_BlendFuncFlags(GL_ONE, GL_ZERO);
2200 if (vid.allowalphatocoverage)
2201 GL_AlphaToCoverage(false);
2203 else if (rsurfacepass == RSURFPASS_RTLIGHT)
2205 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(rsurface.texture->nmaptexture) & TEXF_ALPHA) || rsurface.texture->offsetbias != 0.0f))
2207 switch(rsurface.texture->offsetmapping)
2209 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
2210 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2211 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2212 case OFFSETMAPPING_OFF: break;
2215 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2216 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2217 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
2218 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
2220 mode = SHADERMODE_LIGHTSOURCE;
2221 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2222 permutation |= SHADERPERMUTATION_CUBEFILTER;
2223 if (diffusescale > 0)
2224 permutation |= SHADERPERMUTATION_DIFFUSE;
2225 if (specularscale > 0)
2226 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2227 if (r_refdef.fogenabled)
2228 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
2229 if (rsurface.texture->colormapping)
2230 permutation |= SHADERPERMUTATION_COLORMAPPING;
2231 if (r_shadow_usingshadowmap2d)
2233 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2234 if(r_shadow_shadowmapvsdct)
2235 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
2237 if (r_shadow_shadowmap2ddepthbuffer)
2238 permutation |= SHADERPERMUTATION_DEPTHRGB;
2240 if (rsurface.texture->reflectmasktexture)
2241 permutation |= SHADERPERMUTATION_REFLECTCUBE;
2242 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2243 blendfuncflags = R_BlendFuncFlags(GL_SRC_ALPHA, GL_ONE);
2244 if (vid.allowalphatocoverage)
2245 GL_AlphaToCoverage(false);
2247 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
2249 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(rsurface.texture->nmaptexture) & TEXF_ALPHA) || rsurface.texture->offsetbias != 0.0f))
2251 switch(rsurface.texture->offsetmapping)
2253 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
2254 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2255 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2256 case OFFSETMAPPING_OFF: break;
2259 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2260 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2261 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
2262 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
2263 // unshaded geometry (fullbright or ambient model lighting)
2264 mode = SHADERMODE_FLATCOLOR;
2265 ambientscale = diffusescale = specularscale = 0;
2266 if (rsurface.texture->glowtexture && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2267 permutation |= SHADERPERMUTATION_GLOW;
2268 if (r_refdef.fogenabled)
2269 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
2270 if (rsurface.texture->colormapping)
2271 permutation |= SHADERPERMUTATION_COLORMAPPING;
2272 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
2274 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
2275 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2277 if (r_shadow_shadowmap2ddepthbuffer)
2278 permutation |= SHADERPERMUTATION_DEPTHRGB;
2280 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2281 permutation |= SHADERPERMUTATION_REFLECTION;
2282 if (rsurface.texture->reflectmasktexture)
2283 permutation |= SHADERPERMUTATION_REFLECTCUBE;
2284 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
2285 blendfuncflags = R_BlendFuncFlags(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
2286 // when using alphatocoverage, we don't need alphakill
2287 if (vid.allowalphatocoverage)
2289 if (r_transparent_alphatocoverage.integer)
2291 GL_AlphaToCoverage((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
2292 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
2295 GL_AlphaToCoverage(false);
2298 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT_DIRECTIONAL)
2300 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(rsurface.texture->nmaptexture) & TEXF_ALPHA) || rsurface.texture->offsetbias != 0.0f))
2302 switch(rsurface.texture->offsetmapping)
2304 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
2305 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2306 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2307 case OFFSETMAPPING_OFF: break;
2310 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2311 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2312 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
2313 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
2314 // directional model lighting
2315 mode = SHADERMODE_LIGHTDIRECTION;
2316 if (rsurface.texture->glowtexture && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2317 permutation |= SHADERPERMUTATION_GLOW;
2318 permutation |= SHADERPERMUTATION_DIFFUSE;
2319 if (specularscale > 0)
2320 permutation |= SHADERPERMUTATION_SPECULAR;
2321 if (r_refdef.fogenabled)
2322 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
2323 if (rsurface.texture->colormapping)
2324 permutation |= SHADERPERMUTATION_COLORMAPPING;
2325 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
2327 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
2328 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2330 if (r_shadow_shadowmap2ddepthbuffer)
2331 permutation |= SHADERPERMUTATION_DEPTHRGB;
2333 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2334 permutation |= SHADERPERMUTATION_REFLECTION;
2335 if (r_shadow_usingdeferredprepass && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
2336 permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
2337 if (rsurface.texture->reflectmasktexture)
2338 permutation |= SHADERPERMUTATION_REFLECTCUBE;
2339 if (r_shadow_bouncegridtexture && cl.csqc_vidvars.drawworld)
2341 permutation |= SHADERPERMUTATION_BOUNCEGRID;
2342 if (r_shadow_bouncegriddirectional)
2343 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
2345 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
2346 blendfuncflags = R_BlendFuncFlags(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
2347 // when using alphatocoverage, we don't need alphakill
2348 if (vid.allowalphatocoverage)
2350 if (r_transparent_alphatocoverage.integer)
2352 GL_AlphaToCoverage((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
2353 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
2356 GL_AlphaToCoverage(false);
2359 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
2361 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(rsurface.texture->nmaptexture) & TEXF_ALPHA) || rsurface.texture->offsetbias != 0.0f))
2363 switch(rsurface.texture->offsetmapping)
2365 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
2366 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2367 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2368 case OFFSETMAPPING_OFF: break;
2371 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2372 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2373 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
2374 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
2375 // ambient model lighting
2376 mode = SHADERMODE_LIGHTDIRECTION;
2377 if (rsurface.texture->glowtexture && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2378 permutation |= SHADERPERMUTATION_GLOW;
2379 if (r_refdef.fogenabled)
2380 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
2381 if (rsurface.texture->colormapping)
2382 permutation |= SHADERPERMUTATION_COLORMAPPING;
2383 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
2385 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
2386 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2388 if (r_shadow_shadowmap2ddepthbuffer)
2389 permutation |= SHADERPERMUTATION_DEPTHRGB;
2391 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2392 permutation |= SHADERPERMUTATION_REFLECTION;
2393 if (r_shadow_usingdeferredprepass && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
2394 permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
2395 if (rsurface.texture->reflectmasktexture)
2396 permutation |= SHADERPERMUTATION_REFLECTCUBE;
2397 if (r_shadow_bouncegridtexture && cl.csqc_vidvars.drawworld)
2399 permutation |= SHADERPERMUTATION_BOUNCEGRID;
2400 if (r_shadow_bouncegriddirectional)
2401 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
2403 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
2404 blendfuncflags = R_BlendFuncFlags(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
2405 // when using alphatocoverage, we don't need alphakill
2406 if (vid.allowalphatocoverage)
2408 if (r_transparent_alphatocoverage.integer)
2410 GL_AlphaToCoverage((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
2411 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
2414 GL_AlphaToCoverage(false);
2419 if (r_glsl_offsetmapping.integer && ((R_TextureFlags(rsurface.texture->nmaptexture) & TEXF_ALPHA) || rsurface.texture->offsetbias != 0.0f))
2421 switch(rsurface.texture->offsetmapping)
2423 case OFFSETMAPPING_LINEAR: permutation |= SHADERPERMUTATION_OFFSETMAPPING;break;
2424 case OFFSETMAPPING_RELIEF: permutation |= SHADERPERMUTATION_OFFSETMAPPING | SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2425 case OFFSETMAPPING_DEFAULT: permutation |= SHADERPERMUTATION_OFFSETMAPPING;if (r_glsl_offsetmapping_reliefmapping.integer) permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;break;
2426 case OFFSETMAPPING_OFF: break;
2429 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2430 permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2431 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHAGEN_VERTEX)
2432 permutation |= SHADERPERMUTATION_ALPHAGEN_VERTEX;
2434 if (rsurface.texture->glowtexture && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2435 permutation |= SHADERPERMUTATION_GLOW;
2436 if (r_refdef.fogenabled)
2437 permutation |= r_texture_fogheighttexture ? SHADERPERMUTATION_FOGHEIGHTTEXTURE : (r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE);
2438 if (rsurface.texture->colormapping)
2439 permutation |= SHADERPERMUTATION_COLORMAPPING;
2440 if (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW))
2442 permutation |= SHADERPERMUTATION_SHADOWMAPORTHO;
2443 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2445 if (r_shadow_shadowmap2ddepthbuffer)
2446 permutation |= SHADERPERMUTATION_DEPTHRGB;
2448 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2449 permutation |= SHADERPERMUTATION_REFLECTION;
2450 if (r_shadow_usingdeferredprepass && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED))
2451 permutation |= SHADERPERMUTATION_DEFERREDLIGHTMAP;
2452 if (rsurface.texture->reflectmasktexture)
2453 permutation |= SHADERPERMUTATION_REFLECTCUBE;
2454 if (FAKELIGHT_ENABLED)
2456 // fake lightmapping (q1bsp, q3bsp, fullbright map)
2457 mode = SHADERMODE_FAKELIGHT;
2458 permutation |= SHADERPERMUTATION_DIFFUSE;
2459 if (specularscale > 0)
2460 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2462 else if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping)
2464 // deluxemapping (light direction texture)
2465 if (rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping && r_refdef.scene.worldmodel->brushq3.deluxemapping_modelspace)
2466 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
2468 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
2469 permutation |= SHADERPERMUTATION_DIFFUSE;
2470 if (specularscale > 0)
2471 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2473 else if (r_glsl_deluxemapping.integer >= 2)
2475 // fake deluxemapping (uniform light direction in tangentspace)
2476 if (rsurface.uselightmaptexture)
2477 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_LIGHTMAP;
2479 mode = SHADERMODE_LIGHTDIRECTIONMAP_FORCED_VERTEXCOLOR;
2480 permutation |= SHADERPERMUTATION_DIFFUSE;
2481 if (specularscale > 0)
2482 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2484 else if (rsurface.uselightmaptexture)
2486 // ordinary lightmapping (q1bsp, q3bsp)
2487 mode = SHADERMODE_LIGHTMAP;
2491 // ordinary vertex coloring (q3bsp)
2492 mode = SHADERMODE_VERTEXCOLOR;
2494 if (r_shadow_bouncegridtexture && cl.csqc_vidvars.drawworld)
2496 permutation |= SHADERPERMUTATION_BOUNCEGRID;
2497 if (r_shadow_bouncegriddirectional)
2498 permutation |= SHADERPERMUTATION_BOUNCEGRIDDIRECTIONAL;
2500 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
2501 blendfuncflags = R_BlendFuncFlags(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
2502 // when using alphatocoverage, we don't need alphakill
2503 if (vid.allowalphatocoverage)
2505 if (r_transparent_alphatocoverage.integer)
2507 GL_AlphaToCoverage((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
2508 permutation &= ~SHADERPERMUTATION_ALPHAKILL;
2511 GL_AlphaToCoverage(false);
2514 if(!(blendfuncflags & BLENDFUNC_ALLOWS_COLORMOD))
2515 colormod = dummy_colormod;
2516 if(!(blendfuncflags & BLENDFUNC_ALLOWS_ANYFOG))
2517 permutation &= ~(SHADERPERMUTATION_FOGHEIGHTTEXTURE | SHADERPERMUTATION_FOGOUTSIDE | SHADERPERMUTATION_FOGINSIDE);
2518 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACKALPHA)
2519 permutation |= SHADERPERMUTATION_FOGALPHAHACK;
2520 switch(vid.renderpath)
2522 case RENDERPATH_D3D9:
2524 RSurf_PrepareVerticesForBatch(BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR | (rsurface.modellightmapcolor4f ? BATCHNEED_VERTEXMESH_VERTEXCOLOR : 0) | BATCHNEED_VERTEXMESH_TEXCOORD | (rsurface.uselightmaptexture ? BATCHNEED_VERTEXMESH_LIGHTMAP : 0), texturenumsurfaces, texturesurfacelist);
2525 R_Mesh_PrepareVertices_Mesh(rsurface.batchnumvertices, rsurface.batchvertexmesh, rsurface.batchvertexmeshbuffer);
2526 R_SetupShader_SetPermutationHLSL(mode, permutation);
2527 Matrix4x4_ToArrayFloatGL(&rsurface.matrix, m16f);hlslPSSetParameter16f(D3DPSREGISTER_ModelToReflectCube, m16f);
2528 if (mode == SHADERMODE_LIGHTSOURCE)
2530 Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);hlslVSSetParameter16f(D3DVSREGISTER_ModelToLight, m16f);
2531 hlslVSSetParameter3f(D3DVSREGISTER_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
2535 if (mode == SHADERMODE_LIGHTDIRECTION)
2537 hlslVSSetParameter3f(D3DVSREGISTER_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
2540 Matrix4x4_ToArrayFloatGL(&rsurface.texture->currenttexmatrix, m16f);hlslVSSetParameter16f(D3DVSREGISTER_TexMatrix, m16f);
2541 Matrix4x4_ToArrayFloatGL(&rsurface.texture->currentbackgroundtexmatrix, m16f);hlslVSSetParameter16f(D3DVSREGISTER_BackgroundTexMatrix, m16f);
2542 Matrix4x4_ToArrayFloatGL(&r_shadow_shadowmapmatrix, m16f);hlslVSSetParameter16f(D3DVSREGISTER_ShadowMapMatrix, m16f);
2543 hlslVSSetParameter3f(D3DVSREGISTER_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
2544 hlslVSSetParameter4f(D3DVSREGISTER_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]);
2546 if (mode == SHADERMODE_LIGHTSOURCE)
2548 hlslPSSetParameter3f(D3DPSREGISTER_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
2549 hlslPSSetParameter3f(D3DPSREGISTER_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
2550 hlslPSSetParameter3f(D3DPSREGISTER_Color_Ambient, colormod[0] * ambientscale, colormod[1] * ambientscale, colormod[2] * ambientscale);
2551 hlslPSSetParameter3f(D3DPSREGISTER_Color_Diffuse, colormod[0] * diffusescale, colormod[1] * diffusescale, colormod[2] * diffusescale);
2552 hlslPSSetParameter3f(D3DPSREGISTER_Color_Specular, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale);
2554 // additive passes are only darkened by fog, not tinted
2555 hlslPSSetParameter3f(D3DPSREGISTER_FogColor, 0, 0, 0);
2556 hlslPSSetParameter1f(D3DPSREGISTER_SpecularPower, rsurface.texture->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
2560 if (mode == SHADERMODE_FLATCOLOR)
2562 hlslPSSetParameter3f(D3DPSREGISTER_Color_Ambient, colormod[0], colormod[1], colormod[2]);
2564 else if (mode == SHADERMODE_LIGHTDIRECTION)
2566 hlslPSSetParameter3f(D3DPSREGISTER_Color_Ambient, (r_refdef.scene.ambient + rsurface.modellight_ambient[0] * r_refdef.lightmapintensity) * colormod[0], (r_refdef.scene.ambient + rsurface.modellight_ambient[1] * r_refdef.lightmapintensity) * colormod[1], (r_refdef.scene.ambient + rsurface.modellight_ambient[2] * r_refdef.lightmapintensity) * colormod[2]);
2567 hlslPSSetParameter3f(D3DPSREGISTER_Color_Diffuse, r_refdef.lightmapintensity * colormod[0], r_refdef.lightmapintensity * colormod[1], r_refdef.lightmapintensity * colormod[2]);
2568 hlslPSSetParameter3f(D3DPSREGISTER_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale);
2569 hlslPSSetParameter3f(D3DPSREGISTER_DeferredMod_Diffuse, colormod[0], colormod[1], colormod[2]);
2570 hlslPSSetParameter3f(D3DPSREGISTER_DeferredMod_Specular, specularscale, specularscale, specularscale);
2571 hlslPSSetParameter3f(D3DPSREGISTER_LightColor, rsurface.modellight_diffuse[0], rsurface.modellight_diffuse[1], rsurface.modellight_diffuse[2]);
2572 hlslPSSetParameter3f(D3DPSREGISTER_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
2576 hlslPSSetParameter3f(D3DPSREGISTER_Color_Ambient, r_refdef.scene.ambient * colormod[0], r_refdef.scene.ambient * colormod[1], r_refdef.scene.ambient * colormod[2]);
2577 hlslPSSetParameter3f(D3DPSREGISTER_Color_Diffuse, rsurface.texture->lightmapcolor[0], rsurface.texture->lightmapcolor[1], rsurface.texture->lightmapcolor[2]);
2578 hlslPSSetParameter3f(D3DPSREGISTER_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale);
2579 hlslPSSetParameter3f(D3DPSREGISTER_DeferredMod_Diffuse, colormod[0] * diffusescale, colormod[1] * diffusescale, colormod[2] * diffusescale);
2580 hlslPSSetParameter3f(D3DPSREGISTER_DeferredMod_Specular, specularscale, specularscale, specularscale);
2582 // additive passes are only darkened by fog, not tinted
2583 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACK0)
2584 hlslPSSetParameter3f(D3DPSREGISTER_FogColor, 0, 0, 0);
2586 hlslPSSetParameter3f(D3DPSREGISTER_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
2587 hlslPSSetParameter4f(D3DPSREGISTER_DistortScaleRefractReflect, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor);
2588 hlslPSSetParameter4f(D3DPSREGISTER_ScreenScaleRefractReflect, r_fb.water.screenscale[0], r_fb.water.screenscale[1], r_fb.water.screenscale[0], r_fb.water.screenscale[1]);
2589 hlslPSSetParameter4f(D3DPSREGISTER_ScreenCenterRefractReflect, r_fb.water.screencenter[0], r_fb.water.screencenter[1], r_fb.water.screencenter[0], r_fb.water.screencenter[1]);
2590 hlslPSSetParameter4f(D3DPSREGISTER_RefractColor, rsurface.texture->refractcolor4f[0], rsurface.texture->refractcolor4f[1], rsurface.texture->refractcolor4f[2], rsurface.texture->refractcolor4f[3] * rsurface.texture->lightmapcolor[3]);
2591 hlslPSSetParameter4f(D3DPSREGISTER_ReflectColor, rsurface.texture->reflectcolor4f[0], rsurface.texture->reflectcolor4f[1], rsurface.texture->reflectcolor4f[2], rsurface.texture->reflectcolor4f[3] * rsurface.texture->lightmapcolor[3]);
2592 hlslPSSetParameter1f(D3DPSREGISTER_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
2593 hlslPSSetParameter1f(D3DPSREGISTER_ReflectOffset, rsurface.texture->reflectmin);
2594 hlslPSSetParameter1f(D3DPSREGISTER_SpecularPower, (rsurface.texture->specularpower - 1.0f) * (r_shadow_glossexact.integer ? 0.25f : 1.0f));
2595 if (mode == SHADERMODE_WATER)
2596 hlslPSSetParameter2f(D3DPSREGISTER_NormalmapScrollBlend, rsurface.texture->r_water_waterscroll[0], rsurface.texture->r_water_waterscroll[1]);
2598 hlslPSSetParameter2f(D3DPSREGISTER_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
2599 hlslPSSetParameter4f(D3DPSREGISTER_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
2600 hlslPSSetParameter3f(D3DPSREGISTER_Color_Glow, rsurface.glowmod[0], rsurface.glowmod[1], rsurface.glowmod[2]);
2601 hlslPSSetParameter1f(D3DPSREGISTER_Alpha, rsurface.texture->lightmapcolor[3] * ((rsurface.texture->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay) ? rsurface.texture->r_water_wateralpha : 1));
2602 hlslPSSetParameter3f(D3DPSREGISTER_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
2603 if (rsurface.texture->pantstexture)
2604 hlslPSSetParameter3f(D3DPSREGISTER_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]);
2606 hlslPSSetParameter3f(D3DPSREGISTER_Color_Pants, 0, 0, 0);
2607 if (rsurface.texture->shirttexture)
2608 hlslPSSetParameter3f(D3DPSREGISTER_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]);
2610 hlslPSSetParameter3f(D3DPSREGISTER_Color_Shirt, 0, 0, 0);
2611 hlslPSSetParameter4f(D3DPSREGISTER_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]);
2612 hlslPSSetParameter1f(D3DPSREGISTER_FogPlaneViewDist, rsurface.fogplaneviewdist);
2613 hlslPSSetParameter1f(D3DPSREGISTER_FogRangeRecip, rsurface.fograngerecip);
2614 hlslPSSetParameter1f(D3DPSREGISTER_FogHeightFade, rsurface.fogheightfade);
2615 hlslPSSetParameter4f(D3DPSREGISTER_OffsetMapping_ScaleSteps,
2616 r_glsl_offsetmapping_scale.value*rsurface.texture->offsetscale,
2617 max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2618 1.0 / max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2619 max(1, r_glsl_offsetmapping_reliefmapping_refinesteps.integer)
2621 hlslPSSetParameter1f(D3DPSREGISTER_OffsetMapping_LodDistance, r_glsl_offsetmapping_lod_distance.integer * r_refdef.view.quality);
2622 hlslPSSetParameter1f(D3DPSREGISTER_OffsetMapping_Bias, rsurface.texture->offsetbias);
2623 hlslPSSetParameter2f(D3DPSREGISTER_ScreenToDepth, r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]);
2624 hlslPSSetParameter2f(D3DPSREGISTER_PixelToScreenTexCoord, 1.0f/vid.width, 1.0/vid.height);
2626 R_Mesh_TexBind(GL20TU_NORMAL , rsurface.texture->nmaptexture );
2627 R_Mesh_TexBind(GL20TU_COLOR , rsurface.texture->basetexture );
2628 R_Mesh_TexBind(GL20TU_GLOSS , rsurface.texture->glosstexture );
2629 R_Mesh_TexBind(GL20TU_GLOW , rsurface.texture->glowtexture );
2630 if (permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL , rsurface.texture->backgroundnmaptexture );
2631 if (permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) R_Mesh_TexBind(GL20TU_SECONDARY_COLOR , rsurface.texture->backgroundbasetexture );
2632 if (permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS , rsurface.texture->backgroundglosstexture );
2633 if (permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) R_Mesh_TexBind(GL20TU_SECONDARY_GLOW , rsurface.texture->backgroundglowtexture );
2634 if (permutation & SHADERPERMUTATION_COLORMAPPING) R_Mesh_TexBind(GL20TU_PANTS , rsurface.texture->pantstexture );
2635 if (permutation & SHADERPERMUTATION_COLORMAPPING) R_Mesh_TexBind(GL20TU_SHIRT , rsurface.texture->shirttexture );
2636 if (permutation & SHADERPERMUTATION_REFLECTCUBE) R_Mesh_TexBind(GL20TU_REFLECTMASK , rsurface.texture->reflectmasktexture );
2637 if (permutation & SHADERPERMUTATION_REFLECTCUBE) R_Mesh_TexBind(GL20TU_REFLECTCUBE , rsurface.texture->reflectcubetexture ? rsurface.texture->reflectcubetexture : r_texture_whitecube);
2638 if (permutation & SHADERPERMUTATION_FOGHEIGHTTEXTURE) R_Mesh_TexBind(GL20TU_FOGHEIGHTTEXTURE , r_texture_fogheighttexture );
2639 if (permutation & (SHADERPERMUTATION_FOGINSIDE | SHADERPERMUTATION_FOGOUTSIDE)) R_Mesh_TexBind(GL20TU_FOGMASK , r_texture_fogattenuation );
2640 R_Mesh_TexBind(GL20TU_LIGHTMAP , rsurface.lightmaptexture ? rsurface.lightmaptexture : r_texture_white);
2641 R_Mesh_TexBind(GL20TU_DELUXEMAP , rsurface.deluxemaptexture ? rsurface.deluxemaptexture : r_texture_blanknormalmap);
2642 if (rsurface.rtlight ) R_Mesh_TexBind(GL20TU_ATTENUATION , r_shadow_attenuationgradienttexture );
2643 if (rsurfacepass == RSURFPASS_BACKGROUND)
2645 R_Mesh_TexBind(GL20TU_REFRACTION , waterplane->texture_refraction ? waterplane->texture_refraction : r_texture_black);
2646 if(mode == SHADERMODE_GENERIC) R_Mesh_TexBind(GL20TU_FIRST , waterplane->texture_camera ? waterplane->texture_camera : r_texture_black);
2647 R_Mesh_TexBind(GL20TU_REFLECTION , waterplane->texture_reflection ? waterplane->texture_reflection : r_texture_black);
2651 if (permutation & SHADERPERMUTATION_REFLECTION ) R_Mesh_TexBind(GL20TU_REFLECTION , waterplane->texture_reflection ? waterplane->texture_reflection : r_texture_black);
2653 // if (rsurfacepass == RSURFPASS_DEFERREDLIGHT ) R_Mesh_TexBind(GL20TU_SCREENNORMALMAP , r_shadow_prepassgeometrynormalmaptexture );
2654 if (permutation & SHADERPERMUTATION_DEFERREDLIGHTMAP ) R_Mesh_TexBind(GL20TU_SCREENDIFFUSE , r_shadow_prepasslightingdiffusetexture );
2655 if (permutation & SHADERPERMUTATION_DEFERREDLIGHTMAP ) R_Mesh_TexBind(GL20TU_SCREENSPECULAR , r_shadow_prepasslightingspeculartexture );
2656 if (rsurface.rtlight || (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW)))
2658 R_Mesh_TexBind(GL20TU_SHADOWMAP2D, r_shadow_shadowmap2ddepthtexture);
2659 if (rsurface.rtlight)
2661 if (permutation & SHADERPERMUTATION_CUBEFILTER ) R_Mesh_TexBind(GL20TU_CUBE , rsurface.rtlight->currentcubemap );
2662 if (permutation & SHADERPERMUTATION_SHADOWMAPVSDCT ) R_Mesh_TexBind(GL20TU_CUBEPROJECTION , r_shadow_shadowmapvsdcttexture );
2667 case RENDERPATH_D3D10:
2668 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2670 case RENDERPATH_D3D11:
2671 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2673 case RENDERPATH_GL20:
2674 case RENDERPATH_GLES2:
2675 if (!vid.useinterleavedarrays)
2677 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), texturenumsurfaces, texturesurfacelist);
2678 R_Mesh_VertexPointer( 3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
2679 R_Mesh_ColorPointer( 4, GL_FLOAT, sizeof(float[4]), rsurface.batchlightmapcolor4f, rsurface.batchlightmapcolor4f_vertexbuffer, rsurface.batchlightmapcolor4f_bufferoffset);
2680 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
2681 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchsvector3f, rsurface.batchsvector3f_vertexbuffer, rsurface.batchsvector3f_bufferoffset);
2682 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchtvector3f, rsurface.batchtvector3f_vertexbuffer, rsurface.batchtvector3f_bufferoffset);
2683 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), rsurface.batchnormal3f, rsurface.batchnormal3f_vertexbuffer, rsurface.batchnormal3f_bufferoffset);
2684 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordlightmap2f, rsurface.batchtexcoordlightmap2f_vertexbuffer, rsurface.batchtexcoordlightmap2f_bufferoffset);
2688 RSurf_PrepareVerticesForBatch(BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR | (rsurface.modellightmapcolor4f ? BATCHNEED_VERTEXMESH_VERTEXCOLOR : 0) | BATCHNEED_VERTEXMESH_TEXCOORD | (rsurface.uselightmaptexture ? BATCHNEED_VERTEXMESH_LIGHTMAP : 0), texturenumsurfaces, texturesurfacelist);
2689 R_Mesh_PrepareVertices_Mesh(rsurface.batchnumvertices, rsurface.batchvertexmesh, rsurface.batchvertexmeshbuffer);
2691 R_SetupShader_SetPermutationGLSL(mode, permutation);
2692 if (r_glsl_permutation->loc_ModelToReflectCube >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.matrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ModelToReflectCube, 1, false, m16f);}
2693 if (mode == SHADERMODE_LIGHTSOURCE)
2695 if (r_glsl_permutation->loc_ModelToLight >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ModelToLight, 1, false, m16f);}
2696 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3f(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
2697 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3f(r_glsl_permutation->loc_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
2698 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, colormod[0] * ambientscale, colormod[1] * ambientscale, colormod[2] * ambientscale);
2699 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, colormod[0] * diffusescale, colormod[1] * diffusescale, colormod[2] * diffusescale);
2700 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale);
2702 // additive passes are only darkened by fog, not tinted
2703 if (r_glsl_permutation->loc_FogColor >= 0)
2704 qglUniform3f(r_glsl_permutation->loc_FogColor, 0, 0, 0);
2705 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1f(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
2709 if (mode == SHADERMODE_FLATCOLOR)
2711 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, colormod[0], colormod[1], colormod[2]);
2713 else if (mode == SHADERMODE_LIGHTDIRECTION)
2715 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, (r_refdef.scene.ambient + rsurface.modellight_ambient[0] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[0], (r_refdef.scene.ambient + rsurface.modellight_ambient[1] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[1], (r_refdef.scene.ambient + rsurface.modellight_ambient[2] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[2]);
2716 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, r_refdef.lightmapintensity * colormod[0], r_refdef.lightmapintensity * colormod[1], r_refdef.lightmapintensity * colormod[2]);
2717 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale);
2718 if (r_glsl_permutation->loc_DeferredMod_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Diffuse, colormod[0], colormod[1], colormod[2]);
2719 if (r_glsl_permutation->loc_DeferredMod_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Specular, specularscale, specularscale, specularscale);
2720 if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3f(r_glsl_permutation->loc_LightColor, rsurface.modellight_diffuse[0] * r_refdef.scene.rtlightstylevalue[0], rsurface.modellight_diffuse[1] * r_refdef.scene.rtlightstylevalue[0], rsurface.modellight_diffuse[2] * r_refdef.scene.rtlightstylevalue[0]);
2721 if (r_glsl_permutation->loc_LightDir >= 0) qglUniform3f(r_glsl_permutation->loc_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
2725 if (r_glsl_permutation->loc_Color_Ambient >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Ambient, r_refdef.scene.ambient * colormod[0], r_refdef.scene.ambient * colormod[1], r_refdef.scene.ambient * colormod[2]);
2726 if (r_glsl_permutation->loc_Color_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Diffuse, rsurface.texture->lightmapcolor[0], rsurface.texture->lightmapcolor[1], rsurface.texture->lightmapcolor[2]);
2727 if (r_glsl_permutation->loc_Color_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale);
2728 if (r_glsl_permutation->loc_DeferredMod_Diffuse >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Diffuse, colormod[0] * diffusescale, colormod[1] * diffusescale, colormod[2] * diffusescale);
2729 if (r_glsl_permutation->loc_DeferredMod_Specular >= 0) qglUniform3f(r_glsl_permutation->loc_DeferredMod_Specular, specularscale, specularscale, specularscale);
2731 // additive passes are only darkened by fog, not tinted
2732 if (r_glsl_permutation->loc_FogColor >= 0)
2734 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACK0)
2735 qglUniform3f(r_glsl_permutation->loc_FogColor, 0, 0, 0);
2737 qglUniform3f(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
2739 if (r_glsl_permutation->loc_DistortScaleRefractReflect >= 0) qglUniform4f(r_glsl_permutation->loc_DistortScaleRefractReflect, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor);
2740 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]);
2741 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]);
2742 if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4f(r_glsl_permutation->loc_RefractColor, rsurface.texture->refractcolor4f[0], rsurface.texture->refractcolor4f[1], rsurface.texture->refractcolor4f[2], rsurface.texture->refractcolor4f[3] * rsurface.texture->lightmapcolor[3]);
2743 if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4f(r_glsl_permutation->loc_ReflectColor, rsurface.texture->reflectcolor4f[0], rsurface.texture->reflectcolor4f[1], rsurface.texture->reflectcolor4f[2], rsurface.texture->reflectcolor4f[3] * rsurface.texture->lightmapcolor[3]);
2744 if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1f(r_glsl_permutation->loc_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
2745 if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1f(r_glsl_permutation->loc_ReflectOffset, rsurface.texture->reflectmin);
2746 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1f(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
2747 if (r_glsl_permutation->loc_NormalmapScrollBlend >= 0) qglUniform2f(r_glsl_permutation->loc_NormalmapScrollBlend, rsurface.texture->r_water_waterscroll[0], rsurface.texture->r_water_waterscroll[1]);
2749 if (r_glsl_permutation->loc_TexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currenttexmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_TexMatrix, 1, false, m16f);}
2750 if (r_glsl_permutation->loc_BackgroundTexMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currentbackgroundtexmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_BackgroundTexMatrix, 1, false, m16f);}
2751 if (r_glsl_permutation->loc_ShadowMapMatrix >= 0) {Matrix4x4_ToArrayFloatGL(&r_shadow_shadowmapmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_ShadowMapMatrix, 1, false, m16f);}
2752 if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform2f(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
2753 if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4f(r_glsl_permutation->loc_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
2755 if (r_glsl_permutation->loc_Color_Glow >= 0) qglUniform3f(r_glsl_permutation->loc_Color_Glow, rsurface.glowmod[0], rsurface.glowmod[1], rsurface.glowmod[2]);
2756 if (r_glsl_permutation->loc_Alpha >= 0) qglUniform1f(r_glsl_permutation->loc_Alpha, rsurface.texture->lightmapcolor[3] * ((rsurface.texture->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay) ? rsurface.texture->r_water_wateralpha : 1));
2757 if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3f(r_glsl_permutation->loc_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
2758 if (r_glsl_permutation->loc_Color_Pants >= 0)
2760 if (rsurface.texture->pantstexture)
2761 qglUniform3f(r_glsl_permutation->loc_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]);
2763 qglUniform3f(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
2765 if (r_glsl_permutation->loc_Color_Shirt >= 0)
2767 if (rsurface.texture->shirttexture)
2768 qglUniform3f(r_glsl_permutation->loc_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]);
2770 qglUniform3f(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
2772 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]);
2773 if (r_glsl_permutation->loc_FogPlaneViewDist >= 0) qglUniform1f(r_glsl_permutation->loc_FogPlaneViewDist, rsurface.fogplaneviewdist);
2774 if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1f(r_glsl_permutation->loc_FogRangeRecip, rsurface.fograngerecip);
2775 if (r_glsl_permutation->loc_FogHeightFade >= 0) qglUniform1f(r_glsl_permutation->loc_FogHeightFade, rsurface.fogheightfade);
2776 if (r_glsl_permutation->loc_OffsetMapping_ScaleSteps >= 0) qglUniform4f(r_glsl_permutation->loc_OffsetMapping_ScaleSteps,
2777 r_glsl_offsetmapping_scale.value*rsurface.texture->offsetscale,
2778 max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2779 1.0 / max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2780 max(1, r_glsl_offsetmapping_reliefmapping_refinesteps.integer)
2782 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);
2783 if (r_glsl_permutation->loc_OffsetMapping_Bias >= 0) qglUniform1f(r_glsl_permutation->loc_OffsetMapping_Bias, rsurface.texture->offsetbias);
2784 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]);
2785 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height);
2786 if (r_glsl_permutation->loc_BounceGridMatrix >= 0) {Matrix4x4_Concat(&tempmatrix, &r_shadow_bouncegridmatrix, &rsurface.matrix);Matrix4x4_ToArrayFloatGL(&tempmatrix, m16f);qglUniformMatrix4fv(r_glsl_permutation->loc_BounceGridMatrix, 1, false, m16f);}
2787 if (r_glsl_permutation->loc_BounceGridIntensity >= 0) qglUniform1f(r_glsl_permutation->loc_BounceGridIntensity, r_shadow_bouncegridintensity*r_refdef.view.colorscale);
2789 if (r_glsl_permutation->tex_Texture_First >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , r_texture_white );
2790 if (r_glsl_permutation->tex_Texture_Second >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second , r_texture_white );
2791 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps , r_texture_gammaramps );
2792 if (r_glsl_permutation->tex_Texture_Normal >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Normal , rsurface.texture->nmaptexture );
2793 if (r_glsl_permutation->tex_Texture_Color >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Color , rsurface.texture->basetexture );
2794 if (r_glsl_permutation->tex_Texture_Gloss >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Gloss , rsurface.texture->glosstexture );
2795 if (r_glsl_permutation->tex_Texture_Glow >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Glow , rsurface.texture->glowtexture );
2796 if (r_glsl_permutation->tex_Texture_SecondaryNormal >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryNormal , rsurface.texture->backgroundnmaptexture );
2797 if (r_glsl_permutation->tex_Texture_SecondaryColor >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryColor , rsurface.texture->backgroundbasetexture );
2798 if (r_glsl_permutation->tex_Texture_SecondaryGloss >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryGloss , rsurface.texture->backgroundglosstexture );
2799 if (r_glsl_permutation->tex_Texture_SecondaryGlow >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_SecondaryGlow , rsurface.texture->backgroundglowtexture );
2800 if (r_glsl_permutation->tex_Texture_Pants >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Pants , rsurface.texture->pantstexture );
2801 if (r_glsl_permutation->tex_Texture_Shirt >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Shirt , rsurface.texture->shirttexture );
2802 if (r_glsl_permutation->tex_Texture_ReflectMask >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ReflectMask , rsurface.texture->reflectmasktexture );
2803 if (r_glsl_permutation->tex_Texture_ReflectCube >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ReflectCube , rsurface.texture->reflectcubetexture ? rsurface.texture->reflectcubetexture : r_texture_whitecube);
2804 if (r_glsl_permutation->tex_Texture_FogHeightTexture>= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_FogHeightTexture , r_texture_fogheighttexture );
2805 if (r_glsl_permutation->tex_Texture_FogMask >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_FogMask , r_texture_fogattenuation );
2806 if (r_glsl_permutation->tex_Texture_Lightmap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Lightmap , rsurface.lightmaptexture ? rsurface.lightmaptexture : r_texture_white);
2807 if (r_glsl_permutation->tex_Texture_Deluxemap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Deluxemap , rsurface.deluxemaptexture ? rsurface.deluxemaptexture : r_texture_blanknormalmap);
2808 if (r_glsl_permutation->tex_Texture_Attenuation >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Attenuation , r_shadow_attenuationgradienttexture );
2809 if (rsurfacepass == RSURFPASS_BACKGROUND)
2811 if (r_glsl_permutation->tex_Texture_Refraction >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Refraction , waterplane->texture_refraction ? waterplane->texture_refraction : r_texture_black);
2812 if (r_glsl_permutation->tex_Texture_First >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , waterplane->texture_camera ? waterplane->texture_camera : r_texture_black);
2813 if (r_glsl_permutation->tex_Texture_Reflection >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Reflection , waterplane->texture_reflection ? waterplane->texture_reflection : r_texture_black);
2817 if (r_glsl_permutation->tex_Texture_Reflection >= 0 && waterplane) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Reflection , waterplane->texture_reflection ? waterplane->texture_reflection : r_texture_black);
2819 if (r_glsl_permutation->tex_Texture_ScreenNormalMap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenNormalMap , r_shadow_prepassgeometrynormalmaptexture );
2820 if (r_glsl_permutation->tex_Texture_ScreenDiffuse >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenDiffuse , r_shadow_prepasslightingdiffusetexture );
2821 if (r_glsl_permutation->tex_Texture_ScreenSpecular >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenSpecular , r_shadow_prepasslightingspeculartexture );
2822 if (rsurface.rtlight || (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW)))
2824 if (r_glsl_permutation->tex_Texture_ShadowMap2D >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ShadowMap2D, r_shadow_shadowmap2ddepthtexture );
2825 if (rsurface.rtlight)
2827 if (r_glsl_permutation->tex_Texture_Cube >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Cube , rsurface.rtlight->currentcubemap );
2828 if (r_glsl_permutation->tex_Texture_CubeProjection >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_CubeProjection , r_shadow_shadowmapvsdcttexture );
2831 if (r_glsl_permutation->tex_Texture_BounceGrid >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_BounceGrid, r_shadow_bouncegridtexture);
2834 case RENDERPATH_GL11:
2835 case RENDERPATH_GL13:
2836 case RENDERPATH_GLES1:
2838 case RENDERPATH_SOFT:
2839 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), texturenumsurfaces, texturesurfacelist);
2840 R_Mesh_PrepareVertices_Mesh_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchsvector3f, rsurface.batchtvector3f, rsurface.batchnormal3f, rsurface.batchlightmapcolor4f, rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordlightmap2f);
2841 R_SetupShader_SetPermutationSoft(mode, permutation);
2842 {Matrix4x4_ToArrayFloatGL(&rsurface.matrix, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelToReflectCubeM1, 1, false, m16f);}
2843 if (mode == SHADERMODE_LIGHTSOURCE)
2845 {Matrix4x4_ToArrayFloatGL(&rsurface.entitytolight, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelToLightM1, 1, false, m16f);}
2846 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
2847 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
2848 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Ambient, colormod[0] * ambientscale, colormod[1] * ambientscale, colormod[2] * ambientscale);
2849 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Diffuse, colormod[0] * diffusescale, colormod[1] * diffusescale, colormod[2] * diffusescale);
2850 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Specular, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale, r_refdef.view.colorscale * specularscale);
2852 // additive passes are only darkened by fog, not tinted
2853 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_FogColor, 0, 0, 0);
2854 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_SpecularPower, rsurface.texture->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
2858 if (mode == SHADERMODE_FLATCOLOR)
2860 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Ambient, colormod[0], colormod[1], colormod[2]);
2862 else if (mode == SHADERMODE_LIGHTDIRECTION)
2864 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Ambient, (r_refdef.scene.ambient + rsurface.modellight_ambient[0] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[0], (r_refdef.scene.ambient + rsurface.modellight_ambient[1] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[1], (r_refdef.scene.ambient + rsurface.modellight_ambient[2] * r_refdef.lightmapintensity * r_refdef.scene.rtlightstylevalue[0]) * colormod[2]);
2865 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Diffuse, r_refdef.lightmapintensity * colormod[0], r_refdef.lightmapintensity * colormod[1], r_refdef.lightmapintensity * colormod[2]);
2866 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale);
2867 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_DeferredMod_Diffuse, colormod[0], colormod[1], colormod[2]);
2868 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_DeferredMod_Specular, specularscale, specularscale, specularscale);
2869 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_LightColor, rsurface.modellight_diffuse[0] * r_refdef.scene.rtlightstylevalue[0], rsurface.modellight_diffuse[1] * r_refdef.scene.rtlightstylevalue[0], rsurface.modellight_diffuse[2] * r_refdef.scene.rtlightstylevalue[0]);
2870 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
2874 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Ambient, r_refdef.scene.ambient * colormod[0], r_refdef.scene.ambient * colormod[1], r_refdef.scene.ambient * colormod[2]);
2875 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Diffuse, rsurface.texture->lightmapcolor[0], rsurface.texture->lightmapcolor[1], rsurface.texture->lightmapcolor[2]);
2876 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Specular, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale, r_refdef.lightmapintensity * r_refdef.view.colorscale * specularscale);
2877 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_DeferredMod_Diffuse, colormod[0] * diffusescale, colormod[1] * diffusescale, colormod[2] * diffusescale);
2878 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_DeferredMod_Specular, specularscale, specularscale, specularscale);
2880 // additive passes are only darkened by fog, not tinted
2881 if(blendfuncflags & BLENDFUNC_ALLOWS_FOG_HACK0)
2882 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_FogColor, 0, 0, 0);
2884 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
2885 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_DistortScaleRefractReflect, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor);
2886 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ScreenScaleRefractReflect, r_fb.water.screenscale[0], r_fb.water.screenscale[1], r_fb.water.screenscale[0], r_fb.water.screenscale[1]);
2887 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ScreenCenterRefractReflect, r_fb.water.screencenter[0], r_fb.water.screencenter[1], r_fb.water.screencenter[0], r_fb.water.screencenter[1]);
2888 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_RefractColor, rsurface.texture->refractcolor4f[0], rsurface.texture->refractcolor4f[1], rsurface.texture->refractcolor4f[2], rsurface.texture->refractcolor4f[3] * rsurface.texture->lightmapcolor[3]);
2889 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ReflectColor, rsurface.texture->reflectcolor4f[0], rsurface.texture->reflectcolor4f[1], rsurface.texture->reflectcolor4f[2], rsurface.texture->reflectcolor4f[3] * rsurface.texture->lightmapcolor[3]);
2890 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
2891 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_ReflectOffset, rsurface.texture->reflectmin);
2892 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_SpecularPower, rsurface.texture->specularpower * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
2893 DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_NormalmapScrollBlend, rsurface.texture->r_water_waterscroll[0], rsurface.texture->r_water_waterscroll[1]);
2895 {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currenttexmatrix, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_TexMatrixM1, 1, false, m16f);}
2896 {Matrix4x4_ToArrayFloatGL(&rsurface.texture->currentbackgroundtexmatrix, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_BackgroundTexMatrixM1, 1, false, m16f);}
2897 {Matrix4x4_ToArrayFloatGL(&r_shadow_shadowmapmatrix, m16f);DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ShadowMapMatrixM1, 1, false, m16f);}
2898 DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
2899 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
2901 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Glow, rsurface.glowmod[0], rsurface.glowmod[1], rsurface.glowmod[2]);
2902 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_Alpha, rsurface.texture->lightmapcolor[3] * ((rsurface.texture->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay) ? rsurface.texture->r_water_wateralpha : 1));
2903 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
2904 if (DPSOFTRAST_UNIFORM_Color_Pants >= 0)
2906 if (rsurface.texture->pantstexture)
2907 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]);
2909 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Pants, 0, 0, 0);
2911 if (DPSOFTRAST_UNIFORM_Color_Shirt >= 0)
2913 if (rsurface.texture->shirttexture)
2914 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]);
2916 DPSOFTRAST_Uniform3f(DPSOFTRAST_UNIFORM_Color_Shirt, 0, 0, 0);
2918 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]);
2919 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_FogPlaneViewDist, rsurface.fogplaneviewdist);
2920 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_FogRangeRecip, rsurface.fograngerecip);
2921 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_FogHeightFade, rsurface.fogheightfade);
2922 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_OffsetMapping_ScaleSteps,
2923 r_glsl_offsetmapping_scale.value*rsurface.texture->offsetscale,
2924 max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2925 1.0 / max(1, (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING) ? r_glsl_offsetmapping_reliefmapping_steps.integer : r_glsl_offsetmapping_steps.integer),
2926 max(1, r_glsl_offsetmapping_reliefmapping_refinesteps.integer)
2928 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_OffsetMapping_LodDistance, r_glsl_offsetmapping_lod_distance.integer * r_refdef.view.quality);
2929 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_OffsetMapping_Bias, rsurface.texture->offsetbias);
2930 DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_ScreenToDepth, r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]);
2931 DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height);
2933 R_Mesh_TexBind(GL20TU_NORMAL , rsurface.texture->nmaptexture );
2934 R_Mesh_TexBind(GL20TU_COLOR , rsurface.texture->basetexture );
2935 R_Mesh_TexBind(GL20TU_GLOSS , rsurface.texture->glosstexture );
2936 R_Mesh_TexBind(GL20TU_GLOW , rsurface.texture->glowtexture );
2937 if (permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) R_Mesh_TexBind(GL20TU_SECONDARY_NORMAL , rsurface.texture->backgroundnmaptexture );
2938 if (permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) R_Mesh_TexBind(GL20TU_SECONDARY_COLOR , rsurface.texture->backgroundbasetexture );
2939 if (permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS , rsurface.texture->backgroundglosstexture );
2940 if (permutation & SHADERPERMUTATION_VERTEXTEXTUREBLEND) R_Mesh_TexBind(GL20TU_SECONDARY_GLOW , rsurface.texture->backgroundglowtexture );
2941 if (permutation & SHADERPERMUTATION_COLORMAPPING) R_Mesh_TexBind(GL20TU_PANTS , rsurface.texture->pantstexture );
2942 if (permutation & SHADERPERMUTATION_COLORMAPPING) R_Mesh_TexBind(GL20TU_SHIRT , rsurface.texture->shirttexture );
2943 if (permutation & SHADERPERMUTATION_REFLECTCUBE) R_Mesh_TexBind(GL20TU_REFLECTMASK , rsurface.texture->reflectmasktexture );
2944 if (permutation & SHADERPERMUTATION_REFLECTCUBE) R_Mesh_TexBind(GL20TU_REFLECTCUBE , rsurface.texture->reflectcubetexture ? rsurface.texture->reflectcubetexture : r_texture_whitecube);
2945 if (permutation & SHADERPERMUTATION_FOGHEIGHTTEXTURE) R_Mesh_TexBind(GL20TU_FOGHEIGHTTEXTURE , r_texture_fogheighttexture );
2946 if (permutation & (SHADERPERMUTATION_FOGINSIDE | SHADERPERMUTATION_FOGOUTSIDE)) R_Mesh_TexBind(GL20TU_FOGMASK , r_texture_fogattenuation );
2947 R_Mesh_TexBind(GL20TU_LIGHTMAP , rsurface.lightmaptexture ? rsurface.lightmaptexture : r_texture_white);
2948 R_Mesh_TexBind(GL20TU_DELUXEMAP , rsurface.deluxemaptexture ? rsurface.deluxemaptexture : r_texture_blanknormalmap);
2949 if (rsurface.rtlight ) R_Mesh_TexBind(GL20TU_ATTENUATION , r_shadow_attenuationgradienttexture );
2950 if (rsurfacepass == RSURFPASS_BACKGROUND)
2952 R_Mesh_TexBind(GL20TU_REFRACTION , waterplane->texture_refraction ? waterplane->texture_refraction : r_texture_black);
2953 if(mode == SHADERMODE_GENERIC) R_Mesh_TexBind(GL20TU_FIRST , waterplane->texture_camera ? waterplane->texture_camera : r_texture_black);
2954 R_Mesh_TexBind(GL20TU_REFLECTION , waterplane->texture_reflection ? waterplane->texture_reflection : r_texture_black);
2958 if (permutation & SHADERPERMUTATION_REFLECTION ) R_Mesh_TexBind(GL20TU_REFLECTION , waterplane->texture_reflection ? waterplane->texture_reflection : r_texture_black);
2960 // if (rsurfacepass == RSURFPASS_DEFERREDLIGHT ) R_Mesh_TexBind(GL20TU_SCREENNORMALMAP , r_shadow_prepassgeometrynormalmaptexture );
2961 if (permutation & SHADERPERMUTATION_DEFERREDLIGHTMAP ) R_Mesh_TexBind(GL20TU_SCREENDIFFUSE , r_shadow_prepasslightingdiffusetexture );
2962 if (permutation & SHADERPERMUTATION_DEFERREDLIGHTMAP ) R_Mesh_TexBind(GL20TU_SCREENSPECULAR , r_shadow_prepasslightingspeculartexture );
2963 if (rsurface.rtlight || (r_shadow_usingshadowmaportho && !(rsurface.ent_flags & RENDER_NOSELFSHADOW)))
2965 R_Mesh_TexBind(GL20TU_SHADOWMAP2D, r_shadow_shadowmap2ddepthtexture);
2966 if (rsurface.rtlight)
2968 if (permutation & SHADERPERMUTATION_CUBEFILTER ) R_Mesh_TexBind(GL20TU_CUBE , rsurface.rtlight->currentcubemap );
2969 if (permutation & SHADERPERMUTATION_SHADOWMAPVSDCT ) R_Mesh_TexBind(GL20TU_CUBEPROJECTION , r_shadow_shadowmapvsdcttexture );
2976 void R_SetupShader_DeferredLight(const rtlight_t *rtlight)
2978 // select a permutation of the lighting shader appropriate to this
2979 // combination of texture, entity, light source, and fogging, only use the
2980 // minimum features necessary to avoid wasting rendering time in the
2981 // fragment shader on features that are not being used
2982 unsigned int permutation = 0;
2983 unsigned int mode = 0;
2984 const float *lightcolorbase = rtlight->currentcolor;
2985 float ambientscale = rtlight->ambientscale;
2986 float diffusescale = rtlight->diffusescale;
2987 float specularscale = rtlight->specularscale;
2988 // this is the location of the light in view space
2989 vec3_t viewlightorigin;
2990 // this transforms from view space (camera) to light space (cubemap)
2991 matrix4x4_t viewtolight;
2992 matrix4x4_t lighttoview;
2993 float viewtolight16f[16];
2995 mode = SHADERMODE_DEFERREDLIGHTSOURCE;
2996 if (rtlight->currentcubemap != r_texture_whitecube)
2997 permutation |= SHADERPERMUTATION_CUBEFILTER;
2998 if (diffusescale > 0)
2999 permutation |= SHADERPERMUTATION_DIFFUSE;
3000 if (specularscale > 0 && r_shadow_gloss.integer > 0)
3001 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
3002 if (r_shadow_usingshadowmap2d)
3004 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
3005 if (r_shadow_shadowmapvsdct)
3006 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
3008 if (r_shadow_shadowmap2ddepthbuffer)
3009 permutation |= SHADERPERMUTATION_DEPTHRGB;
3011 if (vid.allowalphatocoverage)
3012 GL_AlphaToCoverage(false);
3013 Matrix4x4_Transform(&r_refdef.view.viewport.viewmatrix, rtlight->shadoworigin, viewlightorigin);
3014 Matrix4x4_Concat(&lighttoview, &r_refdef.view.viewport.viewmatrix, &rtlight->matrix_lighttoworld);
3015 Matrix4x4_Invert_Simple(&viewtolight, &lighttoview);
3016 Matrix4x4_ToArrayFloatGL(&viewtolight, viewtolight16f);
3017 switch(vid.renderpath)
3019 case RENDERPATH_D3D9:
3021 R_SetupShader_SetPermutationHLSL(mode, permutation);
3022 hlslPSSetParameter3f(D3DPSREGISTER_LightPosition, viewlightorigin[0], viewlightorigin[1], viewlightorigin[2]);
3023 hlslPSSetParameter16f(D3DPSREGISTER_ViewToLight, viewtolight16f);
3024 hlslPSSetParameter3f(D3DPSREGISTER_DeferredColor_Ambient , lightcolorbase[0] * ambientscale , lightcolorbase[1] * ambientscale , lightcolorbase[2] * ambientscale );
3025 hlslPSSetParameter3f(D3DPSREGISTER_DeferredColor_Diffuse , lightcolorbase[0] * diffusescale , lightcolorbase[1] * diffusescale , lightcolorbase[2] * diffusescale );
3026 hlslPSSetParameter3f(D3DPSREGISTER_DeferredColor_Specular, lightcolorbase[0] * specularscale, lightcolorbase[1] * specularscale, lightcolorbase[2] * specularscale);
3027 hlslPSSetParameter2f(D3DPSREGISTER_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
3028 hlslPSSetParameter4f(D3DPSREGISTER_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
3029 hlslPSSetParameter1f(D3DPSREGISTER_SpecularPower, (r_shadow_gloss.integer == 2 ? r_shadow_gloss2exponent.value : r_shadow_glossexponent.value) * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
3030 hlslPSSetParameter2f(D3DPSREGISTER_ScreenToDepth, r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]);
3031 hlslPSSetParameter2f(D3DPSREGISTER_PixelToScreenTexCoord, 1.0f/vid.width, 1.0/vid.height);
3033 R_Mesh_TexBind(GL20TU_ATTENUATION , r_shadow_attenuationgradienttexture );
3034 R_Mesh_TexBind(GL20TU_SCREENNORMALMAP , r_shadow_prepassgeometrynormalmaptexture );
3035 R_Mesh_TexBind(GL20TU_CUBE , rsurface.rtlight->currentcubemap );
3036 R_Mesh_TexBind(GL20TU_SHADOWMAP2D , r_shadow_shadowmap2ddepthtexture );
3037 R_Mesh_TexBind(GL20TU_CUBEPROJECTION , r_shadow_shadowmapvsdcttexture );
3040 case RENDERPATH_D3D10:
3041 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3043 case RENDERPATH_D3D11:
3044 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3046 case RENDERPATH_GL20:
3047 case RENDERPATH_GLES2:
3048 R_SetupShader_SetPermutationGLSL(mode, permutation);
3049 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3f( r_glsl_permutation->loc_LightPosition , viewlightorigin[0], viewlightorigin[1], viewlightorigin[2]);
3050 if (r_glsl_permutation->loc_ViewToLight >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ViewToLight , 1, false, viewtolight16f);
3051 if (r_glsl_permutation->loc_DeferredColor_Ambient >= 0) qglUniform3f( r_glsl_permutation->loc_DeferredColor_Ambient , lightcolorbase[0] * ambientscale , lightcolorbase[1] * ambientscale , lightcolorbase[2] * ambientscale );
3052 if (r_glsl_permutation->loc_DeferredColor_Diffuse >= 0) qglUniform3f( r_glsl_permutation->loc_DeferredColor_Diffuse , lightcolorbase[0] * diffusescale , lightcolorbase[1] * diffusescale , lightcolorbase[2] * diffusescale );
3053 if (r_glsl_permutation->loc_DeferredColor_Specular >= 0) qglUniform3f( r_glsl_permutation->loc_DeferredColor_Specular , lightcolorbase[0] * specularscale, lightcolorbase[1] * specularscale, lightcolorbase[2] * specularscale);
3054 if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform2f( r_glsl_permutation->loc_ShadowMap_TextureScale , r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
3055 if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4f( r_glsl_permutation->loc_ShadowMap_Parameters , r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
3056 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);
3057 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]);
3058 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f( r_glsl_permutation->loc_PixelToScreenTexCoord , 1.0f/vid.width, 1.0f/vid.height);
3060 if (r_glsl_permutation->tex_Texture_Attenuation >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Attenuation , r_shadow_attenuationgradienttexture );
3061 if (r_glsl_permutation->tex_Texture_ScreenNormalMap >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ScreenNormalMap , r_shadow_prepassgeometrynormalmaptexture );
3062 if (r_glsl_permutation->tex_Texture_Cube >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Cube , rsurface.rtlight->currentcubemap );
3063 if (r_glsl_permutation->tex_Texture_ShadowMap2D >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_ShadowMap2D , r_shadow_shadowmap2ddepthtexture );
3064 if (r_glsl_permutation->tex_Texture_CubeProjection >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_CubeProjection , r_shadow_shadowmapvsdcttexture );
3066 case RENDERPATH_GL11:
3067 case RENDERPATH_GL13:
3068 case RENDERPATH_GLES1:
3070 case RENDERPATH_SOFT:
3071 R_SetupShader_SetPermutationGLSL(mode, permutation);
3072 DPSOFTRAST_Uniform3f( DPSOFTRAST_UNIFORM_LightPosition , viewlightorigin[0], viewlightorigin[1], viewlightorigin[2]);
3073 DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ViewToLightM1 , 1, false, viewtolight16f);
3074 DPSOFTRAST_Uniform3f( DPSOFTRAST_UNIFORM_DeferredColor_Ambient , lightcolorbase[0] * ambientscale , lightcolorbase[1] * ambientscale , lightcolorbase[2] * ambientscale );
3075 DPSOFTRAST_Uniform3f( DPSOFTRAST_UNIFORM_DeferredColor_Diffuse , lightcolorbase[0] * diffusescale , lightcolorbase[1] * diffusescale , lightcolorbase[2] * diffusescale );
3076 DPSOFTRAST_Uniform3f( DPSOFTRAST_UNIFORM_DeferredColor_Specular , lightcolorbase[0] * specularscale, lightcolorbase[1] * specularscale, lightcolorbase[2] * specularscale);
3077 DPSOFTRAST_Uniform2f( DPSOFTRAST_UNIFORM_ShadowMap_TextureScale , r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
3078 DPSOFTRAST_Uniform4f( DPSOFTRAST_UNIFORM_ShadowMap_Parameters , r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
3079 DPSOFTRAST_Uniform1f( DPSOFTRAST_UNIFORM_SpecularPower , (r_shadow_gloss.integer == 2 ? r_shadow_gloss2exponent.value : r_shadow_glossexponent.value) * (r_shadow_glossexact.integer ? 0.25f : 1.0f) - 1.0f);
3080 DPSOFTRAST_Uniform2f( DPSOFTRAST_UNIFORM_ScreenToDepth , r_refdef.view.viewport.screentodepth[0], r_refdef.view.viewport.screentodepth[1]);
3081 DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height);
3083 R_Mesh_TexBind(GL20TU_ATTENUATION , r_shadow_attenuationgradienttexture );
3084 R_Mesh_TexBind(GL20TU_SCREENNORMALMAP , r_shadow_prepassgeometrynormalmaptexture );
3085 R_Mesh_TexBind(GL20TU_CUBE , rsurface.rtlight->currentcubemap );
3086 R_Mesh_TexBind(GL20TU_SHADOWMAP2D , r_shadow_shadowmap2ddepthtexture );
3087 R_Mesh_TexBind(GL20TU_CUBEPROJECTION , r_shadow_shadowmapvsdcttexture );
3092 #define SKINFRAME_HASH 1024
3096 int loadsequence; // incremented each level change
3097 memexpandablearray_t array;
3098 skinframe_t *hash[SKINFRAME_HASH];
3101 r_skinframe_t r_skinframe;
3103 void R_SkinFrame_PrepareForPurge(void)
3105 r_skinframe.loadsequence++;
3106 // wrap it without hitting zero
3107 if (r_skinframe.loadsequence >= 200)
3108 r_skinframe.loadsequence = 1;
3111 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
3115 // mark the skinframe as used for the purging code
3116 skinframe->loadsequence = r_skinframe.loadsequence;
3119 void R_SkinFrame_Purge(void)
3123 for (i = 0;i < SKINFRAME_HASH;i++)
3125 for (s = r_skinframe.hash[i];s;s = s->next)
3127 if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
3129 if (s->merged == s->base)
3131 // FIXME: maybe pass a pointer to the pointer to R_PurgeTexture and reset it to NULL inside? [11/29/2007 Black]
3132 R_PurgeTexture(s->stain );s->stain = NULL;
3133 R_PurgeTexture(s->merged);s->merged = NULL;
3134 R_PurgeTexture(s->base );s->base = NULL;
3135 R_PurgeTexture(s->pants );s->pants = NULL;
3136 R_PurgeTexture(s->shirt );s->shirt = NULL;
3137 R_PurgeTexture(s->nmap );s->nmap = NULL;
3138 R_PurgeTexture(s->gloss );s->gloss = NULL;
3139 R_PurgeTexture(s->glow );s->glow = NULL;
3140 R_PurgeTexture(s->fog );s->fog = NULL;
3141 R_PurgeTexture(s->reflect);s->reflect = NULL;
3142 s->loadsequence = 0;
3148 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
3150 char basename[MAX_QPATH];
3152 Image_StripImageExtension(name, basename, sizeof(basename));
3154 if( last == NULL ) {
3156 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
3157 item = r_skinframe.hash[hashindex];
3162 // linearly search through the hash bucket
3163 for( ; item ; item = item->next ) {
3164 if( !strcmp( item->basename, basename ) ) {
3171 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qboolean add)
3175 char basename[MAX_QPATH];
3177 Image_StripImageExtension(name, basename, sizeof(basename));
3179 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
3180 for (item = r_skinframe.hash[hashindex];item;item = item->next)
3181 if (!strcmp(item->basename, basename) && (comparecrc < 0 || (item->textureflags == textureflags && item->comparewidth == comparewidth && item->compareheight == compareheight && item->comparecrc == comparecrc)))
3185 rtexture_t *dyntexture;
3186 // check whether its a dynamic texture
3187 dyntexture = CL_GetDynTexture( basename );
3188 if (!add && !dyntexture)
3190 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
3191 memset(item, 0, sizeof(*item));
3192 strlcpy(item->basename, basename, sizeof(item->basename));
3193 item->base = dyntexture; // either NULL or dyntexture handle
3194 item->textureflags = textureflags & ~TEXF_FORCE_RELOAD;
3195 item->comparewidth = comparewidth;
3196 item->compareheight = compareheight;
3197 item->comparecrc = comparecrc;
3198 item->next = r_skinframe.hash[hashindex];
3199 r_skinframe.hash[hashindex] = item;
3201 else if (textureflags & TEXF_FORCE_RELOAD)
3203 rtexture_t *dyntexture;
3204 // check whether its a dynamic texture
3205 dyntexture = CL_GetDynTexture( basename );
3206 if (!add && !dyntexture)
3208 if (item->merged == item->base)
3209 item->merged = NULL;
3210 // FIXME: maybe pass a pointer to the pointer to R_PurgeTexture and reset it to NULL inside? [11/29/2007 Black]
3211 R_PurgeTexture(item->stain );item->stain = NULL;
3212 R_PurgeTexture(item->merged);item->merged = NULL;
3213 R_PurgeTexture(item->base );item->base = NULL;
3214 R_PurgeTexture(item->pants );item->pants = NULL;
3215 R_PurgeTexture(item->shirt );item->shirt = NULL;
3216 R_PurgeTexture(item->nmap );item->nmap = NULL;
3217 R_PurgeTexture(item->gloss );item->gloss = NULL;
3218 R_PurgeTexture(item->glow );item->glow = NULL;
3219 R_PurgeTexture(item->fog );item->fog = NULL;
3220 R_PurgeTexture(item->reflect);item->reflect = NULL;
3221 item->loadsequence = 0;
3223 else if( item->base == NULL )
3225 rtexture_t *dyntexture;
3226 // check whether its a dynamic texture
3227 // this only needs to be done because Purge doesnt delete skinframes - only sets the texture pointers to NULL and we need to restore it before returing.. [11/29/2007 Black]
3228 dyntexture = CL_GetDynTexture( basename );
3229 item->base = dyntexture; // either NULL or dyntexture handle
3232 R_SkinFrame_MarkUsed(item);
3236 #define R_SKINFRAME_LOAD_AVERAGE_COLORS(cnt, getpixel) \
3238 unsigned long long avgcolor[5], wsum; \
3246 for(pix = 0; pix < cnt; ++pix) \
3249 for(comp = 0; comp < 3; ++comp) \
3251 if(w) /* ignore perfectly black pixels because that is better for model skins */ \
3254 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
3256 for(comp = 0; comp < 3; ++comp) \
3257 avgcolor[comp] += getpixel * w; \
3260 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
3261 avgcolor[4] += getpixel; \
3263 if(avgcolor[3] == 0) /* no pixels seen? even worse */ \
3265 skinframe->avgcolor[0] = avgcolor[2] / (255.0 * avgcolor[3]); \
3266 skinframe->avgcolor[1] = avgcolor[1] / (255.0 * avgcolor[3]); \
3267 skinframe->avgcolor[2] = avgcolor[0] / (255.0 * avgcolor[3]); \
3268 skinframe->avgcolor[3] = avgcolor[4] / (255.0 * cnt); \
3271 extern cvar_t gl_picmip;
3272 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboolean complain)
3275 unsigned char *pixels;
3276 unsigned char *bumppixels;
3277 unsigned char *basepixels = NULL;
3278 int basepixels_width = 0;
3279 int basepixels_height = 0;
3280 skinframe_t *skinframe;
3281 rtexture_t *ddsbase = NULL;
3282 qboolean ddshasalpha = false;
3283 float ddsavgcolor[4];
3284 char basename[MAX_QPATH];
3285 int miplevel = R_PicmipForFlags(textureflags);
3286 int savemiplevel = miplevel;
3290 if (cls.state == ca_dedicated)
3293 // return an existing skinframe if already loaded
3294 // if loading of the first image fails, don't make a new skinframe as it
3295 // would cause all future lookups of this to be missing
3296 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
3297 if (skinframe && skinframe->base)
3300 Image_StripImageExtension(name, basename, sizeof(basename));
3302 // check for DDS texture file first
3303 if (!r_loaddds || !(ddsbase = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s.dds", basename), vid.sRGB3D, textureflags, &ddshasalpha, ddsavgcolor, miplevel)))
3305 basepixels = loadimagepixelsbgra(name, complain, true, false, &miplevel);
3306 if (basepixels == NULL)
3310 // FIXME handle miplevel
3312 if (developer_loading.integer)
3313 Con_Printf("loading skin \"%s\"\n", name);
3315 // we've got some pixels to store, so really allocate this new texture now
3317 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
3318 textureflags &= ~TEXF_FORCE_RELOAD;
3319 skinframe->stain = NULL;
3320 skinframe->merged = NULL;
3321 skinframe->base = NULL;
3322 skinframe->pants = NULL;
3323 skinframe->shirt = NULL;
3324 skinframe->nmap = NULL;
3325 skinframe->gloss = NULL;
3326 skinframe->glow = NULL;
3327 skinframe->fog = NULL;
3328 skinframe->reflect = NULL;
3329 skinframe->hasalpha = false;
3333 skinframe->base = ddsbase;
3334 skinframe->hasalpha = ddshasalpha;
3335 VectorCopy(ddsavgcolor, skinframe->avgcolor);
3336 if (r_loadfog && skinframe->hasalpha)
3337 skinframe->fog = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_mask.dds", skinframe->basename), false, textureflags | TEXF_ALPHA, NULL, NULL, miplevel);
3338 //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]);
3342 basepixels_width = image_width;
3343 basepixels_height = image_height;
3344 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);
3345 if (textureflags & TEXF_ALPHA)
3347 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
3349 if (basepixels[j] < 255)
3351 skinframe->hasalpha = true;
3355 if (r_loadfog && skinframe->hasalpha)
3357 // has transparent pixels
3358 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
3359 for (j = 0;j < image_width * image_height * 4;j += 4)
3364 pixels[j+3] = basepixels[j+3];
3366 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);
3370 R_SKINFRAME_LOAD_AVERAGE_COLORS(basepixels_width * basepixels_height, basepixels[4 * pix + comp]);
3372 //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]);
3373 if (r_savedds && qglGetCompressedTexImageARB && skinframe->base)
3374 R_SaveTextureDDSFile(skinframe->base, va(vabuf, sizeof(vabuf), "dds/%s.dds", skinframe->basename), r_texture_dds_save.integer < 2, skinframe->hasalpha);
3375 if (r_savedds && qglGetCompressedTexImageARB && skinframe->fog)
3376 R_SaveTextureDDSFile(skinframe->fog, va(vabuf, sizeof(vabuf), "dds/%s_mask.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
3382 mymiplevel = savemiplevel;
3383 if (r_loadnormalmap)
3384 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);
3385 skinframe->glow = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_glow.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel);
3387 skinframe->gloss = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_gloss.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel);
3388 skinframe->pants = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_pants.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel);
3389 skinframe->shirt = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_shirt.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel);
3390 skinframe->reflect = R_LoadTextureDDSFile(r_main_texturepool, va(vabuf, sizeof(vabuf), "dds/%s_reflect.dds", skinframe->basename), vid.sRGB3D, textureflags, NULL, NULL, mymiplevel);
3393 // _norm is the name used by tenebrae and has been adopted as standard
3394 if (r_loadnormalmap && skinframe->nmap == NULL)
3396 mymiplevel = savemiplevel;
3397 if ((pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_norm", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
3399 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);
3403 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_bump", skinframe->basename), false, false, false, &mymiplevel)) != NULL)
3405 pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
3406 Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
3407 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);
3409 Mem_Free(bumppixels);
3411 else if (r_shadow_bumpscale_basetexture.value > 0)
3413 pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
3414 Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
3415 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);
3419 if (r_savedds && qglGetCompressedTexImageARB && skinframe->nmap)
3420 R_SaveTextureDDSFile(skinframe->nmap, va(vabuf, sizeof(vabuf), "dds/%s_norm.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
3424 // _luma is supported only for tenebrae compatibility
3425 // _glow is the preferred name
3426 mymiplevel = savemiplevel;
3427 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_luma", skinframe->basename), false, false, false, &mymiplevel))))
3429 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);
3431 if (r_savedds && qglGetCompressedTexImageARB && skinframe->glow)
3432 R_SaveTextureDDSFile(skinframe->glow, va(vabuf, sizeof(vabuf), "dds/%s_glow.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
3434 Mem_Free(pixels);pixels = NULL;
3437 mymiplevel = savemiplevel;
3438 if (skinframe->gloss == NULL && r_loadgloss && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_gloss", skinframe->basename), false, false, false, &mymiplevel)))
3440 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);
3442 if (r_savedds && qglGetCompressedTexImageARB && skinframe->gloss)
3443 R_SaveTextureDDSFile(skinframe->gloss, va(vabuf, sizeof(vabuf), "dds/%s_gloss.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
3449 mymiplevel = savemiplevel;
3450 if (skinframe->pants == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_pants", skinframe->basename), false, false, false, &mymiplevel)))
3452 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);
3454 if (r_savedds && qglGetCompressedTexImageARB && skinframe->pants)
3455 R_SaveTextureDDSFile(skinframe->pants, va(vabuf, sizeof(vabuf), "dds/%s_pants.dds", skinframe->basename), r_texture_dds_save.integer < 2, false);
3461 mymiplevel = savemiplevel;
3462 if (skinframe->shirt == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_shirt", skinframe->basename), false, false, false, &mymiplevel)))
3464 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);
3466 if (r_savedds && qglGetCompressedTexImageARB && skinframe->shirt)
3467 R_SaveTextureDDSFile(skinframe->shirt, va(vabuf, sizeof(vabuf), "dds/%s_shirt.dds", skinframe->basename), r_texture_dds_save.integer < 2, false);
3473 mymiplevel = savemiplevel;
3474 if (skinframe->reflect == NULL && (pixels = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "%s_reflect", skinframe->basename), false, false, false, &mymiplevel)))
3476 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);
3478 if (r_savedds && qglGetCompressedTexImageARB && skinframe->reflect)
3479 R_SaveTextureDDSFile(skinframe->reflect, va(vabuf, sizeof(vabuf), "dds/%s_reflect.dds", skinframe->basename), r_texture_dds_save.integer < 2, true);
3486 Mem_Free(basepixels);
3491 // this is only used by .spr32 sprites, HL .spr files, HL .bsp files
3492 skinframe_t *R_SkinFrame_LoadInternalBGRA(const char *name, int textureflags, const unsigned char *skindata, int width, int height, qboolean sRGB)
3495 unsigned char *temp1, *temp2;
3496 skinframe_t *skinframe;
3499 if (cls.state == ca_dedicated)
3502 // if already loaded just return it, otherwise make a new skinframe
3503 skinframe = R_SkinFrame_Find(name, textureflags, width, height, (textureflags & TEXF_FORCE_RELOAD) ? -1 : skindata ? CRC_Block(skindata, width*height*4) : 0, true);
3504 if (skinframe && skinframe->base)
3506 textureflags &= ~TEXF_FORCE_RELOAD;
3508 skinframe->stain = NULL;
3509 skinframe->merged = NULL;
3510 skinframe->base = NULL;
3511 skinframe->pants = NULL;
3512 skinframe->shirt = NULL;
3513 skinframe->nmap = NULL;
3514 skinframe->gloss = NULL;
3515 skinframe->glow = NULL;
3516 skinframe->fog = NULL;
3517 skinframe->reflect = NULL;
3518 skinframe->hasalpha = false;
3520 // if no data was provided, then clearly the caller wanted to get a blank skinframe
3524 if (developer_loading.integer)
3525 Con_Printf("loading 32bit skin \"%s\"\n", name);
3527 if (r_loadnormalmap && r_shadow_bumpscale_basetexture.value > 0)
3529 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
3530 temp2 = temp1 + width * height * 4;
3531 Image_HeightmapToNormalmap_BGRA(skindata, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
3532 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, (textureflags | TEXF_ALPHA) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP), -1, NULL);
3535 skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, sRGB ? TEXTYPE_SRGB_BGRA : TEXTYPE_BGRA, textureflags, -1, NULL);
3536 if (textureflags & TEXF_ALPHA)
3538 for (i = 3;i < width * height * 4;i += 4)
3540 if (skindata[i] < 255)
3542 skinframe->hasalpha = true;
3546 if (r_loadfog && skinframe->hasalpha)
3548 unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
3549 memcpy(fogpixels, skindata, width * height * 4);
3550 for (i = 0;i < width * height * 4;i += 4)
3551 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
3552 skinframe->fog = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, textureflags, -1, NULL);
3553 Mem_Free(fogpixels);
3557 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, skindata[4 * pix + comp]);
3558 //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]);
3563 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
3567 skinframe_t *skinframe;
3569 if (cls.state == ca_dedicated)
3572 // if already loaded just return it, otherwise make a new skinframe
3573 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
3574 if (skinframe && skinframe->base)
3576 textureflags &= ~TEXF_FORCE_RELOAD;
3578 skinframe->stain = NULL;
3579 skinframe->merged = NULL;
3580 skinframe->base = NULL;
3581 skinframe->pants = NULL;
3582 skinframe->shirt = NULL;
3583 skinframe->nmap = NULL;
3584 skinframe->gloss = NULL;
3585 skinframe->glow = NULL;
3586 skinframe->fog = NULL;
3587 skinframe->reflect = NULL;
3588 skinframe->hasalpha = false;
3590 // if no data was provided, then clearly the caller wanted to get a blank skinframe
3594 if (developer_loading.integer)
3595 Con_Printf("loading quake skin \"%s\"\n", name);
3597 // 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)
3598 skinframe->qpixels = (unsigned char *)Mem_Alloc(r_main_mempool, width*height); // FIXME LEAK
3599 memcpy(skinframe->qpixels, skindata, width*height);
3600 skinframe->qwidth = width;
3601 skinframe->qheight = height;
3604 for (i = 0;i < width * height;i++)
3605 featuresmask |= palette_featureflags[skindata[i]];
3607 skinframe->hasalpha = false;
3608 skinframe->qhascolormapping = loadpantsandshirt && (featuresmask & (PALETTEFEATURE_PANTS | PALETTEFEATURE_SHIRT));
3609 skinframe->qgeneratenmap = r_shadow_bumpscale_basetexture.value > 0;
3610 skinframe->qgeneratemerged = true;
3611 skinframe->qgeneratebase = skinframe->qhascolormapping;
3612 skinframe->qgenerateglow = loadglowtexture && (featuresmask & PALETTEFEATURE_GLOW);
3614 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette_bgra_complete)[skindata[pix]*4 + comp]);
3615 //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]);
3620 static void R_SkinFrame_GenerateTexturesFromQPixels(skinframe_t *skinframe, qboolean colormapped)
3624 unsigned char *skindata;
3627 if (!skinframe->qpixels)
3630 if (!skinframe->qhascolormapping)
3631 colormapped = false;
3635 if (!skinframe->qgeneratebase)
3640 if (!skinframe->qgeneratemerged)
3644 width = skinframe->qwidth;
3645 height = skinframe->qheight;
3646 skindata = skinframe->qpixels;
3648 if (skinframe->qgeneratenmap)
3650 unsigned char *temp1, *temp2;
3651 skinframe->qgeneratenmap = false;
3652 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
3653 temp2 = temp1 + width * height * 4;
3654 // use either a custom palette or the quake palette
3655 Image_Copy8bitBGRA(skindata, temp1, width * height, palette_bgra_complete);
3656 Image_HeightmapToNormalmap_BGRA(temp1, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
3657 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, (skinframe->textureflags | TEXF_ALPHA) & (r_mipnormalmaps.integer ? ~0 : ~TEXF_MIPMAP), -1, NULL);
3661 if (skinframe->qgenerateglow)
3663 skinframe->qgenerateglow = false;
3664 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
3669 skinframe->qgeneratebase = false;
3670 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);
3671 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);
3672 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);
3676 skinframe->qgeneratemerged = false;
3677 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);
3680 if (!skinframe->qgeneratemerged && !skinframe->qgeneratebase)
3682 Mem_Free(skinframe->qpixels);
3683 skinframe->qpixels = NULL;
3687 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)
3690 skinframe_t *skinframe;
3693 if (cls.state == ca_dedicated)
3696 // if already loaded just return it, otherwise make a new skinframe
3697 skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
3698 if (skinframe && skinframe->base)
3700 textureflags &= ~TEXF_FORCE_RELOAD;
3702 skinframe->stain = NULL;
3703 skinframe->merged = NULL;
3704 skinframe->base = NULL;
3705 skinframe->pants = NULL;
3706 skinframe->shirt = NULL;
3707 skinframe->nmap = NULL;
3708 skinframe->gloss = NULL;
3709 skinframe->glow = NULL;
3710 skinframe->fog = NULL;
3711 skinframe->reflect = NULL;
3712 skinframe->hasalpha = false;
3714 // if no data was provided, then clearly the caller wanted to get a blank skinframe
3718 if (developer_loading.integer)
3719 Con_Printf("loading embedded 8bit image \"%s\"\n", name);
3721 skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_PALETTE, textureflags, -1, palette);
3722 if (textureflags & TEXF_ALPHA)
3724 for (i = 0;i < width * height;i++)
3726 if (((unsigned char *)palette)[skindata[i]*4+3] < 255)
3728 skinframe->hasalpha = true;
3732 if (r_loadfog && skinframe->hasalpha)
3733 skinframe->fog = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "%s_fog", skinframe->basename), width, height, skindata, TEXTYPE_PALETTE, textureflags, -1, alphapalette);
3736 R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette)[skindata[pix]*4 + comp]);
3737 //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]);
3742 skinframe_t *R_SkinFrame_LoadMissing(void)
3744 skinframe_t *skinframe;
3746 if (cls.state == ca_dedicated)
3749 skinframe = R_SkinFrame_Find("missing", TEXF_FORCENEAREST, 0, 0, 0, true);
3750 skinframe->stain = NULL;
3751 skinframe->merged = NULL;
3752 skinframe->base = NULL;
3753 skinframe->pants = NULL;
3754 skinframe->shirt = NULL;
3755 skinframe->nmap = NULL;
3756 skinframe->gloss = NULL;
3757 skinframe->glow = NULL;
3758 skinframe->fog = NULL;
3759 skinframe->reflect = NULL;
3760 skinframe->hasalpha = false;
3762 skinframe->avgcolor[0] = rand() / RAND_MAX;
3763 skinframe->avgcolor[1] = rand() / RAND_MAX;
3764 skinframe->avgcolor[2] = rand() / RAND_MAX;
3765 skinframe->avgcolor[3] = 1;
3770 //static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
3771 typedef struct suffixinfo_s
3774 qboolean flipx, flipy, flipdiagonal;
3777 static suffixinfo_t suffix[3][6] =
3780 {"px", false, false, false},
3781 {"nx", false, false, false},
3782 {"py", false, false, false},
3783 {"ny", false, false, false},
3784 {"pz", false, false, false},
3785 {"nz", false, false, false}
3788 {"posx", false, false, false},
3789 {"negx", false, false, false},
3790 {"posy", false, false, false},
3791 {"negy", false, false, false},
3792 {"posz", false, false, false},
3793 {"negz", false, false, false}
3796 {"rt", true, false, true},
3797 {"lf", false, true, true},
3798 {"ft", true, true, false},
3799 {"bk", false, false, false},
3800 {"up", true, false, true},
3801 {"dn", true, false, true}
3805 static int componentorder[4] = {0, 1, 2, 3};
3807 static rtexture_t *R_LoadCubemap(const char *basename)
3809 int i, j, cubemapsize;
3810 unsigned char *cubemappixels, *image_buffer;
3811 rtexture_t *cubemaptexture;
3813 // must start 0 so the first loadimagepixels has no requested width/height
3815 cubemappixels = NULL;
3816 cubemaptexture = NULL;
3817 // keep trying different suffix groups (posx, px, rt) until one loads
3818 for (j = 0;j < 3 && !cubemappixels;j++)
3820 // load the 6 images in the suffix group
3821 for (i = 0;i < 6;i++)
3823 // generate an image name based on the base and and suffix
3824 dpsnprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
3826 if ((image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
3828 // an image loaded, make sure width and height are equal
3829 if (image_width == image_height && (!cubemappixels || image_width == cubemapsize))
3831 // if this is the first image to load successfully, allocate the cubemap memory
3832 if (!cubemappixels && image_width >= 1)
3834 cubemapsize = image_width;
3835 // note this clears to black, so unavailable sides are black
3836 cubemappixels = (unsigned char *)Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
3838 // copy the image with any flipping needed by the suffix (px and posx types don't need flipping)
3840 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);
3843 Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
3845 Mem_Free(image_buffer);
3849 // if a cubemap loaded, upload it
3852 if (developer_loading.integer)
3853 Con_Printf("loading cubemap \"%s\"\n", basename);
3855 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) | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
3856 Mem_Free(cubemappixels);
3860 Con_DPrintf("failed to load cubemap \"%s\"\n", basename);
3861 if (developer_loading.integer)
3863 Con_Printf("(tried tried images ");
3864 for (j = 0;j < 3;j++)
3865 for (i = 0;i < 6;i++)
3866 Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
3867 Con_Print(" and was unable to find any of them).\n");
3870 return cubemaptexture;
3873 rtexture_t *R_GetCubemap(const char *basename)
3876 for (i = 0;i < r_texture_numcubemaps;i++)
3877 if (r_texture_cubemaps[i] != NULL)
3878 if (!strcasecmp(r_texture_cubemaps[i]->basename, basename))
3879 return r_texture_cubemaps[i]->texture ? r_texture_cubemaps[i]->texture : r_texture_whitecube;
3880 if (i >= MAX_CUBEMAPS || !r_main_mempool)
3881 return r_texture_whitecube;
3882 r_texture_numcubemaps++;
3883 r_texture_cubemaps[i] = (cubemapinfo_t *)Mem_Alloc(r_main_mempool, sizeof(cubemapinfo_t));
3884 strlcpy(r_texture_cubemaps[i]->basename, basename, sizeof(r_texture_cubemaps[i]->basename));
3885 r_texture_cubemaps[i]->texture = R_LoadCubemap(r_texture_cubemaps[i]->basename);
3886 return r_texture_cubemaps[i]->texture;
3889 static void R_Main_FreeViewCache(void)
3891 if (r_refdef.viewcache.entityvisible)
3892 Mem_Free(r_refdef.viewcache.entityvisible);
3893 if (r_refdef.viewcache.world_pvsbits)
3894 Mem_Free(r_refdef.viewcache.world_pvsbits);
3895 if (r_refdef.viewcache.world_leafvisible)
3896 Mem_Free(r_refdef.viewcache.world_leafvisible);
3897 if (r_refdef.viewcache.world_surfacevisible)
3898 Mem_Free(r_refdef.viewcache.world_surfacevisible);
3899 memset(&r_refdef.viewcache, 0, sizeof(r_refdef.viewcache));
3902 static void R_Main_ResizeViewCache(void)
3904 int numentities = r_refdef.scene.numentities;
3905 int numclusters = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusters : 1;
3906 int numclusterbytes = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusterbytes : 1;
3907 int numleafs = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_leafs : 1;
3908 int numsurfaces = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->num_surfaces : 1;
3909 if (r_refdef.viewcache.maxentities < numentities)
3911 r_refdef.viewcache.maxentities = numentities;
3912 if (r_refdef.viewcache.entityvisible)
3913 Mem_Free(r_refdef.viewcache.entityvisible);
3914 r_refdef.viewcache.entityvisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.maxentities);
3916 if (r_refdef.viewcache.world_numclusters != numclusters)
3918 r_refdef.viewcache.world_numclusters = numclusters;
3919 r_refdef.viewcache.world_numclusterbytes = numclusterbytes;
3920 if (r_refdef.viewcache.world_pvsbits)
3921 Mem_Free(r_refdef.viewcache.world_pvsbits);
3922 r_refdef.viewcache.world_pvsbits = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numclusterbytes);
3924 if (r_refdef.viewcache.world_numleafs != numleafs)
3926 r_refdef.viewcache.world_numleafs = numleafs;
3927 if (r_refdef.viewcache.world_leafvisible)
3928 Mem_Free(r_refdef.viewcache.world_leafvisible);
3929 r_refdef.viewcache.world_leafvisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numleafs);
3931 if (r_refdef.viewcache.world_numsurfaces != numsurfaces)
3933 r_refdef.viewcache.world_numsurfaces = numsurfaces;
3934 if (r_refdef.viewcache.world_surfacevisible)
3935 Mem_Free(r_refdef.viewcache.world_surfacevisible);
3936 r_refdef.viewcache.world_surfacevisible = (unsigned char *)Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numsurfaces);
3940 extern rtexture_t *loadingscreentexture;
3941 static void gl_main_start(void)
3943 loadingscreentexture = NULL;
3944 r_texture_blanknormalmap = NULL;
3945 r_texture_white = NULL;
3946 r_texture_grey128 = NULL;
3947 r_texture_black = NULL;
3948 r_texture_whitecube = NULL;
3949 r_texture_normalizationcube = NULL;
3950 r_texture_fogattenuation = NULL;
3951 r_texture_fogheighttexture = NULL;
3952 r_texture_gammaramps = NULL;
3953 r_texture_numcubemaps = 0;
3955 r_loaddds = r_texture_dds_load.integer != 0;
3956 r_savedds = vid.support.arb_texture_compression && vid.support.ext_texture_compression_s3tc && r_texture_dds_save.integer;
3958 switch(vid.renderpath)
3960 case RENDERPATH_GL20:
3961 case RENDERPATH_D3D9:
3962 case RENDERPATH_D3D10:
3963 case RENDERPATH_D3D11:
3964 case RENDERPATH_SOFT:
3965 case RENDERPATH_GLES2:
3966 Cvar_SetValueQuick(&r_textureunits, vid.texunits);
3967 Cvar_SetValueQuick(&gl_combine, 1);
3968 Cvar_SetValueQuick(&r_glsl, 1);
3969 r_loadnormalmap = true;
3973 case RENDERPATH_GL13:
3974 case RENDERPATH_GLES1:
3975 Cvar_SetValueQuick(&r_textureunits, vid.texunits);
3976 Cvar_SetValueQuick(&gl_combine, 1);
3977 Cvar_SetValueQuick(&r_glsl, 0);
3978 r_loadnormalmap = false;
3979 r_loadgloss = false;
3982 case RENDERPATH_GL11:
3983 Cvar_SetValueQuick(&r_textureunits, vid.texunits);
3984 Cvar_SetValueQuick(&gl_combine, 0);
3985 Cvar_SetValueQuick(&r_glsl, 0);
3986 r_loadnormalmap = false;
3987 r_loadgloss = false;
3993 R_FrameData_Reset();
3997 memset(r_queries, 0, sizeof(r_queries));
3999 r_qwskincache = NULL;
4000 r_qwskincache_size = 0;
4002 // due to caching of texture_t references, the collision cache must be reset
4003 Collision_Cache_Reset(true);
4005 // set up r_skinframe loading system for textures
4006 memset(&r_skinframe, 0, sizeof(r_skinframe));
4007 r_skinframe.loadsequence = 1;
4008 Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
4010 r_main_texturepool = R_AllocTexturePool();
4011 R_BuildBlankTextures();
4013 if (vid.support.arb_texture_cube_map)
4016 R_BuildNormalizationCube();
4018 r_texture_fogattenuation = NULL;
4019 r_texture_fogheighttexture = NULL;
4020 r_texture_gammaramps = NULL;
4021 //r_texture_fogintensity = NULL;
4022 memset(&r_fb, 0, sizeof(r_fb));
4023 r_glsl_permutation = NULL;
4024 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
4025 Mem_ExpandableArray_NewArray(&r_glsl_permutationarray, r_main_mempool, sizeof(r_glsl_permutation_t), 256);
4026 glslshaderstring = NULL;
4028 r_hlsl_permutation = NULL;
4029 memset(r_hlsl_permutationhash, 0, sizeof(r_hlsl_permutationhash));
4030 Mem_ExpandableArray_NewArray(&r_hlsl_permutationarray, r_main_mempool, sizeof(r_hlsl_permutation_t), 256);
4032 hlslshaderstring = NULL;
4033 memset(&r_svbsp, 0, sizeof (r_svbsp));
4035 memset(r_texture_cubemaps, 0, sizeof(r_texture_cubemaps));
4036 r_texture_numcubemaps = 0;
4038 r_refdef.fogmasktable_density = 0;
4041 static void gl_main_shutdown(void)
4044 R_FrameData_Reset();
4046 R_Main_FreeViewCache();
4048 switch(vid.renderpath)
4050 case RENDERPATH_GL11:
4051 case RENDERPATH_GL13:
4052 case RENDERPATH_GL20:
4053 case RENDERPATH_GLES1:
4054 case RENDERPATH_GLES2:
4055 #ifdef GL_SAMPLES_PASSED_ARB
4057 qglDeleteQueriesARB(r_maxqueries, r_queries);
4060 case RENDERPATH_D3D9:
4061 //Con_DPrintf("FIXME D3D9 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4063 case RENDERPATH_D3D10:
4064 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4066 case RENDERPATH_D3D11:
4067 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
4069 case RENDERPATH_SOFT:
4075 memset(r_queries, 0, sizeof(r_queries));
4077 r_qwskincache = NULL;
4078 r_qwskincache_size = 0;
4080 // clear out the r_skinframe state
4081 Mem_ExpandableArray_FreeArray(&r_skinframe.array);
4082 memset(&r_skinframe, 0, sizeof(r_skinframe));
4085 Mem_Free(r_svbsp.nodes);
4086 memset(&r_svbsp, 0, sizeof (r_svbsp));
4087 R_FreeTexturePool(&r_main_texturepool);
4088 loadingscreentexture = NULL;
4089 r_texture_blanknormalmap = NULL;
4090 r_texture_white = NULL;
4091 r_texture_grey128 = NULL;
4092 r_texture_black = NULL;
4093 r_texture_whitecube = NULL;
4094 r_texture_normalizationcube = NULL;
4095 r_texture_fogattenuation = NULL;
4096 r_texture_fogheighttexture = NULL;
4097 r_texture_gammaramps = NULL;
4098 r_texture_numcubemaps = 0;
4099 //r_texture_fogintensity = NULL;
4100 memset(&r_fb, 0, sizeof(r_fb));
4103 r_glsl_permutation = NULL;
4104 memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
4105 Mem_ExpandableArray_FreeArray(&r_glsl_permutationarray);
4106 glslshaderstring = NULL;
4108 r_hlsl_permutation = NULL;
4109 memset(r_hlsl_permutationhash, 0, sizeof(r_hlsl_permutationhash));
4110 Mem_ExpandableArray_FreeArray(&r_hlsl_permutationarray);
4112 hlslshaderstring = NULL;
4115 static void gl_main_newmap(void)
4117 // FIXME: move this code to client
4118 char *entities, entname[MAX_QPATH];
4120 Mem_Free(r_qwskincache);
4121 r_qwskincache = NULL;
4122 r_qwskincache_size = 0;
4125 dpsnprintf(entname, sizeof(entname), "%s.ent", cl.worldnamenoextension);
4126 if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
4128 CL_ParseEntityLump(entities);
4132 if (cl.worldmodel->brush.entities)
4133 CL_ParseEntityLump(cl.worldmodel->brush.entities);
4135 R_Main_FreeViewCache();
4137 R_FrameData_Reset();
4140 void GL_Main_Init(void)
4142 r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
4144 Cmd_AddCommand("r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
4145 Cmd_AddCommand("r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
4146 // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
4147 if (gamemode == GAME_NEHAHRA)
4149 Cvar_RegisterVariable (&gl_fogenable);
4150 Cvar_RegisterVariable (&gl_fogdensity);
4151 Cvar_RegisterVariable (&gl_fogred);
4152 Cvar_RegisterVariable (&gl_foggreen);
4153 Cvar_RegisterVariable (&gl_fogblue);
4154 Cvar_RegisterVariable (&gl_fogstart);
4155 Cvar_RegisterVariable (&gl_fogend);
4156 Cvar_RegisterVariable (&gl_skyclip);
4158 Cvar_RegisterVariable(&r_motionblur);
4159 Cvar_RegisterVariable(&r_damageblur);
4160 Cvar_RegisterVariable(&r_motionblur_averaging);
4161 Cvar_RegisterVariable(&r_motionblur_randomize);
4162 Cvar_RegisterVariable(&r_motionblur_minblur);
4163 Cvar_RegisterVariable(&r_motionblur_maxblur);
4164 Cvar_RegisterVariable(&r_motionblur_velocityfactor);
4165 Cvar_RegisterVariable(&r_motionblur_velocityfactor_minspeed);
4166 Cvar_RegisterVariable(&r_motionblur_velocityfactor_maxspeed);
4167 Cvar_RegisterVariable(&r_motionblur_mousefactor);
4168 Cvar_RegisterVariable(&r_motionblur_mousefactor_minspeed);
4169 Cvar_RegisterVariable(&r_motionblur_mousefactor_maxspeed);
4170 Cvar_RegisterVariable(&r_equalize_entities_fullbright);
4171 Cvar_RegisterVariable(&r_equalize_entities_minambient);
4172 Cvar_RegisterVariable(&r_equalize_entities_by);
4173 Cvar_RegisterVariable(&r_equalize_entities_to);
4174 Cvar_RegisterVariable(&r_depthfirst);
4175 Cvar_RegisterVariable(&r_useinfinitefarclip);
4176 Cvar_RegisterVariable(&r_farclip_base);
4177 Cvar_RegisterVariable(&r_farclip_world);
4178 Cvar_RegisterVariable(&r_nearclip);
4179 Cvar_RegisterVariable(&r_deformvertexes);
4180 Cvar_RegisterVariable(&r_transparent);
4181 Cvar_RegisterVariable(&r_transparent_alphatocoverage);
4182 Cvar_RegisterVariable(&r_transparent_sortsurfacesbynearest);
4183 Cvar_RegisterVariable(&r_transparent_useplanardistance);
4184 Cvar_RegisterVariable(&r_showoverdraw);
4185 Cvar_RegisterVariable(&r_showbboxes);
4186 Cvar_RegisterVariable(&r_showsurfaces);
4187 Cvar_RegisterVariable(&r_showtris);
4188 Cvar_RegisterVariable(&r_shownormals);
4189 Cvar_RegisterVariable(&r_showlighting);
4190 Cvar_RegisterVariable(&r_showshadowvolumes);
4191 Cvar_RegisterVariable(&r_showcollisionbrushes);
4192 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
4193 Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
4194 Cvar_RegisterVariable(&r_showdisabledepthtest);
4195 Cvar_RegisterVariable(&r_drawportals);
4196 Cvar_RegisterVariable(&r_drawentities);
4197 Cvar_RegisterVariable(&r_draw2d);
4198 Cvar_RegisterVariable(&r_drawworld);
4199 Cvar_RegisterVariable(&r_cullentities_trace);
4200 Cvar_RegisterVariable(&r_cullentities_trace_samples);
4201 Cvar_RegisterVariable(&r_cullentities_trace_tempentitysamples);
4202 Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
4203 Cvar_RegisterVariable(&r_cullentities_trace_delay);
4204 Cvar_RegisterVariable(&r_sortentities);
4205 Cvar_RegisterVariable(&r_drawviewmodel);
4206 Cvar_RegisterVariable(&r_drawexteriormodel);
4207 Cvar_RegisterVariable(&r_speeds);
4208 Cvar_RegisterVariable(&r_fullbrights);
4209 Cvar_RegisterVariable(&r_wateralpha);
4210 Cvar_RegisterVariable(&r_dynamic);
4211 Cvar_RegisterVariable(&r_fakelight);
4212 Cvar_RegisterVariable(&r_fakelight_intensity);
4213 Cvar_RegisterVariable(&r_fullbright);
4214 Cvar_RegisterVariable(&r_shadows);
4215 Cvar_RegisterVariable(&r_shadows_darken);
4216 Cvar_RegisterVariable(&r_shadows_drawafterrtlighting);
4217 Cvar_RegisterVariable(&r_shadows_castfrombmodels);
4218 Cvar_RegisterVariable(&r_shadows_throwdistance);
4219 Cvar_RegisterVariable(&r_shadows_throwdirection);
4220 Cvar_RegisterVariable(&r_shadows_focus);
4221 Cvar_RegisterVariable(&r_shadows_shadowmapscale);
4222 Cvar_RegisterVariable(&r_q1bsp_skymasking);
4223 Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
4224 Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
4225 Cvar_RegisterVariable(&r_polygonoffset_decals_factor);
4226 Cvar_RegisterVariable(&r_polygonoffset_decals_offset);
4227 Cvar_RegisterVariable(&r_fog_exp2);
4228 Cvar_RegisterVariable(&r_fog_clear);
4229 Cvar_RegisterVariable(&r_drawfog);
4230 Cvar_RegisterVariable(&r_transparentdepthmasking);
4231 Cvar_RegisterVariable(&r_transparent_sortmindist);
4232 Cvar_RegisterVariable(&r_transparent_sortmaxdist);
4233 Cvar_RegisterVariable(&r_transparent_sortarraysize);
4234 Cvar_RegisterVariable(&r_texture_dds_load);
4235 Cvar_RegisterVariable(&r_texture_dds_save);
4236 Cvar_RegisterVariable(&r_textureunits);
4237 Cvar_RegisterVariable(&gl_combine);
4238 Cvar_RegisterVariable(&r_usedepthtextures);
4239 Cvar_RegisterVariable(&r_viewfbo);
4240 Cvar_RegisterVariable(&r_viewscale);
4241 Cvar_RegisterVariable(&r_viewscale_fpsscaling);
4242 Cvar_RegisterVariable(&r_viewscale_fpsscaling_min);
4243 Cvar_RegisterVariable(&r_viewscale_fpsscaling_multiply);
4244 Cvar_RegisterVariable(&r_viewscale_fpsscaling_stepsize);
4245 Cvar_RegisterVariable(&r_viewscale_fpsscaling_stepmax);
4246 Cvar_RegisterVariable(&r_viewscale_fpsscaling_target);
4247 Cvar_RegisterVariable(&r_glsl);
4248 Cvar_RegisterVariable(&r_glsl_deluxemapping);
4249 Cvar_RegisterVariable(&r_glsl_offsetmapping);
4250 Cvar_RegisterVariable(&r_glsl_offsetmapping_steps);
4251 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
4252 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping_steps);
4253 Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping_refinesteps);
4254 Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
4255 Cvar_RegisterVariable(&r_glsl_offsetmapping_lod);
4256 Cvar_RegisterVariable(&r_glsl_offsetmapping_lod_distance);
4257 Cvar_RegisterVariable(&r_glsl_postprocess);
4258 Cvar_RegisterVariable(&r_glsl_postprocess_uservec1);
4259 Cvar_RegisterVariable(&r_glsl_postprocess_uservec2);
4260 Cvar_RegisterVariable(&r_glsl_postprocess_uservec3);
4261 Cvar_RegisterVariable(&r_glsl_postprocess_uservec4);
4262 Cvar_RegisterVariable(&r_glsl_postprocess_uservec1_enable);
4263 Cvar_RegisterVariable(&r_glsl_postprocess_uservec2_enable);
4264 Cvar_RegisterVariable(&r_glsl_postprocess_uservec3_enable);
4265 Cvar_RegisterVariable(&r_glsl_postprocess_uservec4_enable);
4267 Cvar_RegisterVariable(&r_water);
4268 Cvar_RegisterVariable(&r_water_resolutionmultiplier);
4269 Cvar_RegisterVariable(&r_water_clippingplanebias);
4270 Cvar_RegisterVariable(&r_water_refractdistort);
4271 Cvar_RegisterVariable(&r_water_reflectdistort);
4272 Cvar_RegisterVariable(&r_water_scissormode);
4273 Cvar_RegisterVariable(&r_water_lowquality);
4274 Cvar_RegisterVariable(&r_water_hideplayer);
4275 Cvar_RegisterVariable(&r_water_fbo);
4277 Cvar_RegisterVariable(&r_lerpsprites);
4278 Cvar_RegisterVariable(&r_lerpmodels);
4279 Cvar_RegisterVariable(&r_lerplightstyles);
4280 Cvar_RegisterVariable(&r_waterscroll);
4281 Cvar_RegisterVariable(&r_bloom);
4282 Cvar_RegisterVariable(&r_bloom_colorscale);
4283 Cvar_RegisterVariable(&r_bloom_brighten);
4284 Cvar_RegisterVariable(&r_bloom_blur);
4285 Cvar_RegisterVariable(&r_bloom_resolution);
4286 Cvar_RegisterVariable(&r_bloom_colorexponent);
4287 Cvar_RegisterVariable(&r_bloom_colorsubtract);
4288 Cvar_RegisterVariable(&r_hdr_scenebrightness);
4289 Cvar_RegisterVariable(&r_hdr_glowintensity);
4290 Cvar_RegisterVariable(&r_hdr_irisadaptation);
4291 Cvar_RegisterVariable(&r_hdr_irisadaptation_multiplier);
4292 Cvar_RegisterVariable(&r_hdr_irisadaptation_minvalue);
4293 Cvar_RegisterVariable(&r_hdr_irisadaptation_maxvalue);
4294 Cvar_RegisterVariable(&r_hdr_irisadaptation_value);
4295 Cvar_RegisterVariable(&r_hdr_irisadaptation_fade_up);
4296 Cvar_RegisterVariable(&r_hdr_irisadaptation_fade_down);
4297 Cvar_RegisterVariable(&r_hdr_irisadaptation_radius);
4298 Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
4299 Cvar_RegisterVariable(&developer_texturelogging);
4300 Cvar_RegisterVariable(&gl_lightmaps);
4301 Cvar_RegisterVariable(&r_test);
4302 Cvar_RegisterVariable(&r_glsl_saturation);
4303 Cvar_RegisterVariable(&r_glsl_saturation_redcompensate);
4304 Cvar_RegisterVariable(&r_glsl_vertextextureblend_usebothalphas);
4305 Cvar_RegisterVariable(&r_framedatasize);
4306 if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
4307 Cvar_SetValue("r_fullbrights", 0);
4308 R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap, NULL, NULL);
4311 void Render_Init(void)
4324 R_LightningBeams_Init();
4334 extern char *ENGINE_EXTENSIONS;
4337 gl_renderer = (const char *)qglGetString(GL_RENDERER);
4338 gl_vendor = (const char *)qglGetString(GL_VENDOR);
4339 gl_version = (const char *)qglGetString(GL_VERSION);
4340 gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
4344 if (!gl_platformextensions)
4345 gl_platformextensions = "";
4347 Con_Printf("GL_VENDOR: %s\n", gl_vendor);
4348 Con_Printf("GL_RENDERER: %s\n", gl_renderer);
4349 Con_Printf("GL_VERSION: %s\n", gl_version);
4350 Con_DPrintf("GL_EXTENSIONS: %s\n", gl_extensions);
4351 Con_DPrintf("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
4353 VID_CheckExtensions();
4355 // LordHavoc: report supported extensions
4356 Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
4358 // clear to black (loading plaque will be seen over this)
4359 GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 128);
4363 int R_CullBox(const vec3_t mins, const vec3_t maxs)
4367 if (r_trippy.integer)
4369 for (i = 0;i < r_refdef.view.numfrustumplanes;i++)
4371 // skip nearclip plane, it often culls portals when you are very close, and is almost never useful
4374 p = r_refdef.view.frustum + i;
4379 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
4383 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
4387 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
4391 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
4395 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
4399 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
4403 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
4407 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
4415 int R_CullBoxCustomPlanes(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
4419 if (r_trippy.integer)
4421 for (i = 0;i < numplanes;i++)
4428 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
4432 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
4436 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
4440 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
4444 if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
4448 if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
4452 if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
4456 if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
4464 //==================================================================================
4466 // LordHavoc: this stores temporary data used within the same frame
4468 typedef struct r_framedata_mem_s
4470 struct r_framedata_mem_s *purge; // older mem block to free on next frame
4471 size_t size; // how much usable space
4472 size_t current; // how much space in use
4473 size_t mark; // last "mark" location, temporary memory can be freed by returning to this
4474 size_t wantedsize; // how much space was allocated
4475 unsigned char *data; // start of real data (16byte aligned)
4479 static r_framedata_mem_t *r_framedata_mem;
4481 void R_FrameData_Reset(void)
4483 while (r_framedata_mem)
4485 r_framedata_mem_t *next = r_framedata_mem->purge;
4486 Mem_Free(r_framedata_mem);
4487 r_framedata_mem = next;
4491 static void R_FrameData_Resize(void)
4494 wantedsize = (size_t)(r_framedatasize.value * 1024*1024);
4495 wantedsize = bound(65536, wantedsize, 1000*1024*1024);
4496 if (!r_framedata_mem || r_framedata_mem->wantedsize != wantedsize)
4498 r_framedata_mem_t *newmem = (r_framedata_mem_t *)Mem_Alloc(r_main_mempool, wantedsize);
4499 newmem->wantedsize = wantedsize;
4500 newmem->data = (unsigned char *)(((size_t)(newmem+1) + 15) & ~15);
4501 newmem->size = (unsigned char *)newmem + wantedsize - newmem->data;
4502 newmem->current = 0;
4504 newmem->purge = r_framedata_mem;
4505 r_framedata_mem = newmem;
4509 void R_FrameData_NewFrame(void)
4511 R_FrameData_Resize();
4512 if (!r_framedata_mem)
4514 // if we ran out of space on the last frame, free the old memory now
4515 while (r_framedata_mem->purge)
4517 // repeatedly remove the second item in the list, leaving only head
4518 r_framedata_mem_t *next = r_framedata_mem->purge->purge;
4519 Mem_Free(r_framedata_mem->purge);
4520 r_framedata_mem->purge = next;
4522 // reset the current mem pointer
4523 r_framedata_mem->current = 0;
4524 r_framedata_mem->mark = 0;
4527 void *R_FrameData_Alloc(size_t size)
4531 // align to 16 byte boundary - the data pointer is already aligned, so we
4532 // only need to ensure the size of every allocation is also aligned
4533 size = (size + 15) & ~15;
4535 while (!r_framedata_mem || r_framedata_mem->current + size > r_framedata_mem->size)
4537 // emergency - we ran out of space, allocate more memory
4538 Cvar_SetValueQuick(&r_framedatasize, bound(0.25f, r_framedatasize.value * 2.0f, 128.0f));
4539 R_FrameData_Resize();
4542 data = r_framedata_mem->data + r_framedata_mem->current;
4543 r_framedata_mem->current += size;
4545 // count the usage for stats
4546 r_refdef.stats.framedatacurrent = max(r_refdef.stats.framedatacurrent, (int)r_framedata_mem->current);
4547 r_refdef.stats.framedatasize = max(r_refdef.stats.framedatasize, (int)r_framedata_mem->size);
4549 return (void *)data;
4552 void *R_FrameData_Store(size_t size, void *data)
4554 void *d = R_FrameData_Alloc(size);
4556 memcpy(d, data, size);
4560 void R_FrameData_SetMark(void)
4562 if (!r_framedata_mem)
4564 r_framedata_mem->mark = r_framedata_mem->current;
4567 void R_FrameData_ReturnToMark(void)
4569 if (!r_framedata_mem)
4571 r_framedata_mem->current = r_framedata_mem->mark;
4574 //==================================================================================
4576 // LordHavoc: animcache originally written by Echon, rewritten since then
4579 * Animation cache prevents re-generating mesh data for an animated model
4580 * multiple times in one frame for lighting, shadowing, reflections, etc.
4583 void R_AnimCache_Free(void)
4587 void R_AnimCache_ClearCache(void)
4590 entity_render_t *ent;
4592 for (i = 0;i < r_refdef.scene.numentities;i++)
4594 ent = r_refdef.scene.entities[i];
4595 ent->animcache_vertex3f = NULL;
4596 ent->animcache_normal3f = NULL;
4597 ent->animcache_svector3f = NULL;
4598 ent->animcache_tvector3f = NULL;
4599 ent->animcache_vertexmesh = NULL;
4600 ent->animcache_vertex3fbuffer = NULL;
4601 ent->animcache_vertexmeshbuffer = NULL;
4605 static void R_AnimCache_UpdateEntityMeshBuffers(entity_render_t *ent, int numvertices)
4609 // check if we need the meshbuffers
4610 if (!vid.useinterleavedarrays)
4613 if (!ent->animcache_vertexmesh && ent->animcache_normal3f)
4614 ent->animcache_vertexmesh = (r_vertexmesh_t *)R_FrameData_Alloc(sizeof(r_vertexmesh_t)*numvertices);
4615 // TODO: upload vertex3f buffer?
4616 if (ent->animcache_vertexmesh)
4618 memcpy(ent->animcache_vertexmesh, ent->model->surfmesh.vertexmesh, sizeof(r_vertexmesh_t)*numvertices);
4619 for (i = 0;i < numvertices;i++)
4620 memcpy(ent->animcache_vertexmesh[i].vertex3f, ent->animcache_vertex3f + 3*i, sizeof(float[3]));
4621 if (ent->animcache_svector3f)
4622 for (i = 0;i < numvertices;i++)
4623 memcpy(ent->animcache_vertexmesh[i].svector3f, ent->animcache_svector3f + 3*i, sizeof(float[3]));
4624 if (ent->animcache_tvector3f)
4625 for (i = 0;i < numvertices;i++)
4626 memcpy(ent->animcache_vertexmesh[i].tvector3f, ent->animcache_tvector3f + 3*i, sizeof(float[3]));
4627 if (ent->animcache_normal3f)
4628 for (i = 0;i < numvertices;i++)
4629 memcpy(ent->animcache_vertexmesh[i].normal3f, ent->animcache_normal3f + 3*i, sizeof(float[3]));
4630 // TODO: upload vertexmeshbuffer?
4634 qboolean R_AnimCache_GetEntity(entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
4636 dp_model_t *model = ent->model;
4638 // see if it's already cached this frame
4639 if (ent->animcache_vertex3f)
4641 // add normals/tangents if needed (this only happens with multiple views, reflections, cameras, etc)
4642 if (wantnormals || wanttangents)
4644 if (ent->animcache_normal3f)
4645 wantnormals = false;
4646 if (ent->animcache_svector3f)
4647 wanttangents = false;
4648 if (wantnormals || wanttangents)
4650 numvertices = model->surfmesh.num_vertices;
4652 ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
4655 ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
4656 ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
4658 model->AnimateVertices(model, ent->frameblend, ent->skeleton, NULL, wantnormals ? ent->animcache_normal3f : NULL, wanttangents ? ent->animcache_svector3f : NULL, wanttangents ? ent->animcache_tvector3f : NULL);
4659 R_AnimCache_UpdateEntityMeshBuffers(ent, model->surfmesh.num_vertices);
4665 // see if this ent is worth caching
4666 if (!model || !model->Draw || !model->surfmesh.isanimated || !model->AnimateVertices)
4668 // get some memory for this entity and generate mesh data
4669 numvertices = model->surfmesh.num_vertices;
4670 ent->animcache_vertex3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
4672 ent->animcache_normal3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
4675 ent->animcache_svector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
4676 ent->animcache_tvector3f = (float *)R_FrameData_Alloc(sizeof(float[3])*numvertices);
4678 model->AnimateVertices(model, ent->frameblend, ent->skeleton, ent->animcache_vertex3f, ent->animcache_normal3f, ent->animcache_svector3f, ent->animcache_tvector3f);
4679 R_AnimCache_UpdateEntityMeshBuffers(ent, model->surfmesh.num_vertices);
4684 void R_AnimCache_CacheVisibleEntities(void)
4687 qboolean wantnormals = true;
4688 qboolean wanttangents = !r_showsurfaces.integer;
4690 switch(vid.renderpath)
4692 case RENDERPATH_GL20:
4693 case RENDERPATH_D3D9:
4694 case RENDERPATH_D3D10:
4695 case RENDERPATH_D3D11:
4696 case RENDERPATH_GLES2:
4698 case RENDERPATH_GL11:
4699 case RENDERPATH_GL13:
4700 case RENDERPATH_GLES1:
4701 wanttangents = false;
4703 case RENDERPATH_SOFT:
4707 if (r_shownormals.integer)
4708 wanttangents = wantnormals = true;
4710 // TODO: thread this
4711 // NOTE: R_PrepareRTLights() also caches entities
4713 for (i = 0;i < r_refdef.scene.numentities;i++)
4714 if (r_refdef.viewcache.entityvisible[i])
4715 R_AnimCache_GetEntity(r_refdef.scene.entities[i], wantnormals, wanttangents);
4718 //==================================================================================
4720 extern cvar_t r_overheadsprites_pushback;
4722 static void R_View_UpdateEntityLighting (void)
4725 entity_render_t *ent;
4726 vec3_t tempdiffusenormal, avg;
4727 vec_t f, fa, fd, fdd;
4728 qboolean skipunseen = r_shadows.integer != 1; //|| R_Shadow_ShadowMappingEnabled();
4730 for (i = 0;i < r_refdef.scene.numentities;i++)
4732 ent = r_refdef.scene.entities[i];
4734 // skip unseen models and models that updated by CSQC
4735 if ((!r_refdef.viewcache.entityvisible[i] && skipunseen) || ent->flags & RENDER_CUSTOMIZEDMODELLIGHT)
4739 if (ent->model && (ent->model == cl.worldmodel || ent->model->brush.parentmodel == cl.worldmodel))
4741 // TODO: use modellight for r_ambient settings on world?
4742 VectorSet(ent->modellight_ambient, 0, 0, 0);
4743 VectorSet(ent->modellight_diffuse, 0, 0, 0);
4744 VectorSet(ent->modellight_lightdir, 0, 0, 1);
4748 // fetch the lighting from the worldmodel data
4749 VectorClear(ent->modellight_ambient);
4750 VectorClear(ent->modellight_diffuse);
4751 VectorClear(tempdiffusenormal);
4752 if (ent->flags & RENDER_LIGHT)
4755 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
4757 // complete lightning for lit sprites
4758 // todo: make a EF_ field so small ents could be lit purely by modellight and skipping real rtlight pass (like EF_NORTLIGHT)?
4759 if (ent->model->type == mod_sprite && !(ent->model->data_textures[0].basematerialflags & MATERIALFLAG_FULLBRIGHT))
4761 if (ent->model->sprite.sprnum_type == SPR_OVERHEAD) // apply offset for overhead sprites
4762 org[2] = org[2] + r_overheadsprites_pushback.value;
4763 R_LightPoint(ent->modellight_ambient, org, LP_LIGHTMAP | LP_RTWORLD | LP_DYNLIGHT);
4766 R_CompleteLightPoint(ent->modellight_ambient, ent->modellight_diffuse, tempdiffusenormal, org, LP_LIGHTMAP);
4768 if(ent->flags & RENDER_EQUALIZE)
4770 // first fix up ambient lighting...
4771 if(r_equalize_entities_minambient.value > 0)
4773 fd = 0.299f * ent->modellight_diffuse[0] + 0.587f * ent->modellight_diffuse[1] + 0.114f * ent->modellight_diffuse[2];
4776 fa = (0.299f * ent->modellight_ambient[0] + 0.587f * ent->modellight_ambient[1] + 0.114f * ent->modellight_ambient[2]);
4777 if(fa < r_equalize_entities_minambient.value * fd)
4780 // fa'/fd' = minambient
4781 // fa'+0.25*fd' = fa+0.25*fd
4783 // fa' = fd' * minambient
4784 // fd'*(0.25+minambient) = fa+0.25*fd
4786 // fd' = (fa+0.25*fd) * 1 / (0.25+minambient)
4787 // fa' = (fa+0.25*fd) * minambient / (0.25+minambient)
4789 fdd = (fa + 0.25f * fd) / (0.25f + r_equalize_entities_minambient.value);
4790 f = fdd / fd; // f>0 because all this is additive; f<1 because fdd<fd because this follows from fa < r_equalize_entities_minambient.value * fd
4791 VectorMA(ent->modellight_ambient, (1-f)*0.25f, ent->modellight_diffuse, ent->modellight_ambient);
4792 VectorScale(ent->modellight_diffuse, f, ent->modellight_diffuse);
4797 if(r_equalize_entities_to.value > 0 && r_equalize_entities_by.value != 0)
4799 fa = 0.299f * ent->modellight_ambient[0] + 0.587f * ent->modellight_ambient[1] + 0.114f * ent->modellight_ambient[2];
4800 fd = 0.299f * ent->modellight_diffuse[0] + 0.587f * ent->modellight_diffuse[1] + 0.114f * ent->modellight_diffuse[2];
4804 // adjust brightness and saturation to target
4805 avg[0] = avg[1] = avg[2] = fa / f;
4806 VectorLerp(ent->modellight_ambient, r_equalize_entities_by.value, avg, ent->modellight_ambient);
4807 avg[0] = avg[1] = avg[2] = fd / f;
4808 VectorLerp(ent->modellight_diffuse, r_equalize_entities_by.value, avg, ent->modellight_diffuse);
4814 VectorSet(ent->modellight_ambient, 1, 1, 1);
4816 // move the light direction into modelspace coordinates for lighting code
4817 Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, ent->modellight_lightdir);
4818 if(VectorLength2(ent->modellight_lightdir) == 0)
4819 VectorSet(ent->modellight_lightdir, 0, 0, 1); // have to set SOME valid vector here
4820 VectorNormalize(ent->modellight_lightdir);
4824 #define MAX_LINEOFSIGHTTRACES 64
4826 static qboolean R_CanSeeBox(int numsamples, vec_t enlarge, vec3_t eye, vec3_t entboxmins, vec3_t entboxmaxs)
4829 vec3_t boxmins, boxmaxs;
4832 dp_model_t *model = r_refdef.scene.worldmodel;
4834 if (!model || !model->brush.TraceLineOfSight)
4837 // expand the box a little
4838 boxmins[0] = (enlarge+1) * entboxmins[0] - enlarge * entboxmaxs[0];
4839 boxmaxs[0] = (enlarge+1) * entboxmaxs[0] - enlarge * entboxmins[0];
4840 boxmins[1] = (enlarge+1) * entboxmins[1] - enlarge * entboxmaxs[1];
4841 boxmaxs[1] = (enlarge+1) * entboxmaxs[1] - enlarge * entboxmins[1];
4842 boxmins[2] = (enlarge+1) * entboxmins[2] - enlarge * entboxmaxs[2];
4843 boxmaxs[2] = (enlarge+1) * entboxmaxs[2] - enlarge * entboxmins[2];
4845 // return true if eye is inside enlarged box
4846 if (BoxesOverlap(boxmins, boxmaxs, eye, eye))
4850 VectorCopy(eye, start);
4851 VectorMAM(0.5f, boxmins, 0.5f, boxmaxs, end);
4852 if (model->brush.TraceLineOfSight(model, start, end))
4855 // try various random positions
4856 for (i = 0;i < numsamples;i++)
4858 VectorSet(end, lhrandom(boxmins[0], boxmaxs[0]), lhrandom(boxmins[1], boxmaxs[1]), lhrandom(boxmins[2], boxmaxs[2]));
4859 if (model->brush.TraceLineOfSight(model, start, end))
4867 static void R_View_UpdateEntityVisible (void)
4872 entity_render_t *ent;
4874 renderimask = r_refdef.envmap ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL)
4875 : r_fb.water.hideplayer ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL)
4876 : (chase_active.integer || r_fb.water.renderingscene) ? RENDER_VIEWMODEL
4877 : RENDER_EXTERIORMODEL;
4878 if (!r_drawviewmodel.integer)
4879 renderimask |= RENDER_VIEWMODEL;
4880 if (!r_drawexteriormodel.integer)
4881 renderimask |= RENDER_EXTERIORMODEL;
4882 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs)
4884 // worldmodel can check visibility
4885 memset(r_refdef.viewcache.entityvisible, 0, r_refdef.scene.numentities);
4886 for (i = 0;i < r_refdef.scene.numentities;i++)
4888 ent = r_refdef.scene.entities[i];
4889 if (!(ent->flags & renderimask))
4890 if (!R_CullBox(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)))
4891 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))
4892 r_refdef.viewcache.entityvisible[i] = true;
4897 // no worldmodel or it can't check visibility
4898 for (i = 0;i < r_refdef.scene.numentities;i++)
4900 ent = r_refdef.scene.entities[i];
4901 r_refdef.viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs));
4904 if(r_cullentities_trace.integer && r_refdef.scene.worldmodel->brush.TraceLineOfSight && !r_refdef.view.useclipplane && !r_trippy.integer)
4905 // sorry, this check doesn't work for portal/reflection/refraction renders as the view origin is not useful for culling
4907 for (i = 0;i < r_refdef.scene.numentities;i++)
4909 if (!r_refdef.viewcache.entityvisible[i])
4911 ent = r_refdef.scene.entities[i];
4912 if(!(ent->flags & (RENDER_VIEWMODEL | RENDER_WORLDOBJECT | RENDER_NODEPTHTEST)) && !(ent->model && (ent->model->name[0] == '*')))
4914 samples = ent->entitynumber ? r_cullentities_trace_samples.integer : r_cullentities_trace_tempentitysamples.integer;
4916 continue; // temp entities do pvs only
4917 if(R_CanSeeBox(samples, r_cullentities_trace_enlarge.value, r_refdef.view.origin, ent->mins, ent->maxs))
4918 ent->last_trace_visibility = realtime;
4919 if(ent->last_trace_visibility < realtime - r_cullentities_trace_delay.value)
4920 r_refdef.viewcache.entityvisible[i] = 0;
4926 /// only used if skyrendermasked, and normally returns false
4927 static int R_DrawBrushModelsSky (void)
4930 entity_render_t *ent;
4933 for (i = 0;i < r_refdef.scene.numentities;i++)
4935 if (!r_refdef.viewcache.entityvisible[i])
4937 ent = r_refdef.scene.entities[i];
4938 if (!ent->model || !ent->model->DrawSky)
4940 ent->model->DrawSky(ent);
4946 static void R_DrawNoModel(entity_render_t *ent);
4947 static void R_DrawModels(void)
4950 entity_render_t *ent;
4952 for (i = 0;i < r_refdef.scene.numentities;i++)
4954 if (!r_refdef.viewcache.entityvisible[i])
4956 ent = r_refdef.scene.entities[i];
4957 r_refdef.stats.entities++;
4959 if (ent->model && !strncmp(ent->model->name, "models/proto_", 13))
4962 Matrix4x4_ToVectors(&ent->matrix, f, l, u, o);
4963 Con_Printf("R_DrawModels\n");
4964 Con_Printf("model %s O %f %f %f F %f %f %f L %f %f %f U %f %f %f\n", ent->model->name, o[0], o[1], o[2], f[0], f[1], f[2], l[0], l[1], l[2], u[0], u[1], u[2]);
4965 Con_Printf("group: %i %f %i %f %i %f %i %f\n", ent->framegroupblend[0].frame, ent->framegroupblend[0].lerp, ent->framegroupblend[1].frame, ent->framegroupblend[1].lerp, ent->framegroupblend[2].frame, ent->framegroupblend[2].lerp, ent->framegroupblend[3].frame, ent->framegroupblend[3].lerp);
4966 Con_Printf("blend: %i %f %i %f %i %f %i %f %i %f %i %f %i %f %i %f\n", ent->frameblend[0].subframe, ent->frameblend[0].lerp, ent->frameblend[1].subframe, ent->frameblend[1].lerp, ent->frameblend[2].subframe, ent->frameblend[2].lerp, ent->frameblend[3].subframe, ent->frameblend[3].lerp, ent->frameblend[4].subframe, ent->frameblend[4].lerp, ent->frameblend[5].subframe, ent->frameblend[5].lerp, ent->frameblend[6].subframe, ent->frameblend[6].lerp, ent->frameblend[7].subframe, ent->frameblend[7].lerp);
4969 if (ent->model && ent->model->Draw != NULL)
4970 ent->model->Draw(ent);
4976 static void R_DrawModelsDepth(void)
4979 entity_render_t *ent;
4981 for (i = 0;i < r_refdef.scene.numentities;i++)
4983 if (!r_refdef.viewcache.entityvisible[i])
4985 ent = r_refdef.scene.entities[i];
4986 if (ent->model && ent->model->DrawDepth != NULL)
4987 ent->model->DrawDepth(ent);
4991 static void R_DrawModelsDebug(void)
4994 entity_render_t *ent;
4996 for (i = 0;i < r_refdef.scene.numentities;i++)
4998 if (!r_refdef.viewcache.entityvisible[i])
5000 ent = r_refdef.scene.entities[i];
5001 if (ent->model && ent->model->DrawDebug != NULL)
5002 ent->model->DrawDebug(ent);
5006 static void R_DrawModelsAddWaterPlanes(void)
5009 entity_render_t *ent;
5011 for (i = 0;i < r_refdef.scene.numentities;i++)
5013 if (!r_refdef.viewcache.entityvisible[i])
5015 ent = r_refdef.scene.entities[i];
5016 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
5017 ent->model->DrawAddWaterPlanes(ent);
5021 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}};
5023 void R_HDR_UpdateIrisAdaptation(const vec3_t point)
5025 if (r_hdr_irisadaptation.integer)
5030 vec3_t diffusenormal;
5032 vec_t brightness = 0.0f;
5037 VectorCopy(r_refdef.view.forward, forward);
5038 for (c = 0;c < (int)(sizeof(irisvecs)/sizeof(irisvecs[0]));c++)
5040 p[0] = point[0] + irisvecs[c][0] * r_hdr_irisadaptation_radius.value;
5041 p[1] = point[1] + irisvecs[c][1] * r_hdr_irisadaptation_radius.value;
5042 p[2] = point[2] + irisvecs[c][2] * r_hdr_irisadaptation_radius.value;
5043 R_CompleteLightPoint(ambient, diffuse, diffusenormal, p, LP_LIGHTMAP | LP_RTWORLD | LP_DYNLIGHT);
5044 d = DotProduct(forward, diffusenormal);
5045 brightness += VectorLength(ambient);
5047 brightness += d * VectorLength(diffuse);
5049 brightness *= 1.0f / c;
5050 brightness += 0.00001f; // make sure it's never zero
5051 goal = r_hdr_irisadaptation_multiplier.value / brightness;
5052 goal = bound(r_hdr_irisadaptation_minvalue.value, goal, r_hdr_irisadaptation_maxvalue.value);
5053 current = r_hdr_irisadaptation_value.value;
5055 current = min(current + r_hdr_irisadaptation_fade_up.value * cl.realframetime, goal);
5056 else if (current > goal)
5057 current = max(current - r_hdr_irisadaptation_fade_down.value * cl.realframetime, goal);
5058 if (fabs(r_hdr_irisadaptation_value.value - current) > 0.0001f)
5059 Cvar_SetValueQuick(&r_hdr_irisadaptation_value, current);
5061 else if (r_hdr_irisadaptation_value.value != 1.0f)
5062 Cvar_SetValueQuick(&r_hdr_irisadaptation_value, 1.0f);
5065 static void R_View_SetFrustum(const int *scissor)
5068 double fpx = +1, fnx = -1, fpy = +1, fny = -1;
5069 vec3_t forward, left, up, origin, v;
5073 // flipped x coordinates (because x points left here)
5074 fpx = 1.0 - 2.0 * (scissor[0] - r_refdef.view.viewport.x) / (double) (r_refdef.view.viewport.width);
5075 fnx = 1.0 - 2.0 * (scissor[0] + scissor[2] - r_refdef.view.viewport.x) / (double) (r_refdef.view.viewport.width);
5077 // D3D Y coordinate is top to bottom, OpenGL is bottom to top, fix the D3D one
5078 switch(vid.renderpath)
5080 case RENDERPATH_D3D9:
5081 case RENDERPATH_D3D10:
5082 case RENDERPATH_D3D11:
5083 // non-flipped y coordinates
5084 fny = -1.0 + 2.0 * (vid.height - scissor[1] - scissor[3] - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
5085 fpy = -1.0 + 2.0 * (vid.height - scissor[1] - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
5087 case RENDERPATH_SOFT:
5088 case RENDERPATH_GL11:
5089 case RENDERPATH_GL13:
5090 case RENDERPATH_GL20:
5091 case RENDERPATH_GLES1:
5092 case RENDERPATH_GLES2:
5093 // non-flipped y coordinates
5094 fny = -1.0 + 2.0 * (scissor[1] - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
5095 fpy = -1.0 + 2.0 * (scissor[1] + scissor[3] - r_refdef.view.viewport.y) / (double) (r_refdef.view.viewport.height);
5100 // we can't trust r_refdef.view.forward and friends in reflected scenes
5101 Matrix4x4_ToVectors(&r_refdef.view.matrix, forward, left, up, origin);
5104 r_refdef.view.frustum[0].normal[0] = 0 - 1.0 / r_refdef.view.frustum_x;
5105 r_refdef.view.frustum[0].normal[1] = 0 - 0;
5106 r_refdef.view.frustum[0].normal[2] = -1 - 0;
5107 r_refdef.view.frustum[1].normal[0] = 0 + 1.0 / r_refdef.view.frustum_x;
5108 r_refdef.view.frustum[1].normal[1] = 0 + 0;
5109 r_refdef.view.frustum[1].normal[2] = -1 + 0;
5110 r_refdef.view.frustum[2].normal[0] = 0 - 0;
5111 r_refdef.view.frustum[2].normal[1] = 0 - 1.0 / r_refdef.view.frustum_y;
5112 r_refdef.view.frustum[2].normal[2] = -1 - 0;
5113 r_refdef.view.frustum[3].normal[0] = 0 + 0;
5114 r_refdef.view.frustum[3].normal[1] = 0 + 1.0 / r_refdef.view.frustum_y;
5115 r_refdef.view.frustum[3].normal[2] = -1 + 0;
5119 zNear = r_refdef.nearclip;
5120 nudge = 1.0 - 1.0 / (1<<23);
5121 r_refdef.view.frustum[4].normal[0] = 0 - 0;
5122 r_refdef.view.frustum[4].normal[1] = 0 - 0;
5123 r_refdef.view.frustum[4].normal[2] = -1 - -nudge;
5124 r_refdef.view.frustum[4].dist = 0 - -2 * zNear * nudge;
5125 r_refdef.view.frustum[5].normal[0] = 0 + 0;
5126 r_refdef.view.frustum[5].normal[1] = 0 + 0;
5127 r_refdef.view.frustum[5].normal[2] = -1 + -nudge;
5128 r_refdef.view.frustum[5].dist = 0 + -2 * zNear * nudge;
5134 r_refdef.view.frustum[0].normal[0] = m[3] - m[0];
5135 r_refdef.view.frustum[0].normal[1] = m[7] - m[4];
5136 r_refdef.view.frustum[0].normal[2] = m[11] - m[8];
5137 r_refdef.view.frustum[0].dist = m[15] - m[12];
5139 r_refdef.view.frustum[1].normal[0] = m[3] + m[0];
5140 r_refdef.view.frustum[1].normal[1] = m[7] + m[4];
5141 r_refdef.view.frustum[1].normal[2] = m[11] + m[8];
5142 r_refdef.view.frustum[1].dist = m[15] + m[12];
5144 r_refdef.view.frustum[2].normal[0] = m[3] - m[1];
5145 r_refdef.view.frustum[2].normal[1] = m[7] - m[5];
5146 r_refdef.view.frustum[2].normal[2] = m[11] - m[9];
5147 r_refdef.view.frustum[2].dist = m[15] - m[13];
5149 r_refdef.view.frustum[3].normal[0] = m[3] + m[1];
5150 r_refdef.view.frustum[3].normal[1] = m[7] + m[5];
5151 r_refdef.view.frustum[3].normal[2] = m[11] + m[9];
5152 r_refdef.view.frustum[3].dist = m[15] + m[13];
5154 r_refdef.view.frustum[4].normal[0] = m[3] - m[2];
5155 r_refdef.view.frustum[4].normal[1] = m[7] - m[6];
5156 r_refdef.view.frustum[4].normal[2] = m[11] - m[10];
5157 r_refdef.view.frustum[4].dist = m[15] - m[14];
5159 r_refdef.view.frustum[5].normal[0] = m[3] + m[2];
5160 r_refdef.view.frustum[5].normal[1] = m[7] + m[6];
5161 r_refdef.view.frustum[5].normal[2] = m[11] + m[10];
5162 r_refdef.view.frustum[5].dist = m[15] + m[14];
5165 if (r_refdef.view.useperspective)
5167 // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
5168 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]);
5169 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]);
5170 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]);
5171 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]);
5173 // then the normals from the corners relative to origin
5174 CrossProduct(r_refdef.view.frustumcorner[2], r_refdef.view.frustumcorner[0], r_refdef.view.frustum[0].normal);
5175 CrossProduct(r_refdef.view.frustumcorner[1], r_refdef.view.frustumcorner[3], r_refdef.view.frustum[1].normal);
5176 CrossProduct(r_refdef.view.frustumcorner[0], r_refdef.view.frustumcorner[1], r_refdef.view.frustum[2].normal);
5177 CrossProduct(r_refdef.view.frustumcorner[3], r_refdef.view.frustumcorner[2], r_refdef.view.frustum[3].normal);
5179 // in a NORMAL view, forward cross left == up
5180 // in a REFLECTED view, forward cross left == down
5181 // so our cross products above need to be adjusted for a left handed coordinate system
5182 CrossProduct(forward, left, v);
5183 if(DotProduct(v, up) < 0)
5185 VectorNegate(r_refdef.view.frustum[0].normal, r_refdef.view.frustum[0].normal);
5186 VectorNegate(r_refdef.view.frustum[1].normal, r_refdef.view.frustum[1].normal);
5187 VectorNegate(r_refdef.view.frustum[2].normal, r_refdef.view.frustum[2].normal);
5188 VectorNegate(r_refdef.view.frustum[3].normal, r_refdef.view.frustum[3].normal);
5191 // Leaving those out was a mistake, those were in the old code, and they
5192 // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
5193 // I couldn't reproduce it after adding those normalizations. --blub
5194 VectorNormalize(r_refdef.view.frustum[0].normal);
5195 VectorNormalize(r_refdef.view.frustum[1].normal);
5196 VectorNormalize(r_refdef.view.frustum[2].normal);
5197 VectorNormalize(r_refdef.view.frustum[3].normal);
5199 // make the corners absolute
5200 VectorAdd(r_refdef.view.frustumcorner[0], r_refdef.view.origin, r_refdef.view.frustumcorner[0]);
5201 VectorAdd(r_refdef.view.frustumcorner[1], r_refdef.view.origin, r_refdef.view.frustumcorner[1]);
5202 VectorAdd(r_refdef.view.frustumcorner[2], r_refdef.view.origin, r_refdef.view.frustumcorner[2]);
5203 VectorAdd(r_refdef.view.frustumcorner[3], r_refdef.view.origin, r_refdef.view.frustumcorner[3]);
5206 VectorCopy(forward, r_refdef.view.frustum[4].normal);
5208 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal);
5209 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal);
5210 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal);
5211 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal);
5212 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) + r_refdef.nearclip;
5216 VectorScale(left, -r_refdef.view.ortho_x, r_refdef.view.frustum[0].normal);
5217 VectorScale(left, r_refdef.view.ortho_x, r_refdef.view.frustum[1].normal);
5218 VectorScale(up, -r_refdef.view.ortho_y, r_refdef.view.frustum[2].normal);
5219 VectorScale(up, r_refdef.view.ortho_y, r_refdef.view.frustum[3].normal);
5220 VectorCopy(forward, r_refdef.view.frustum[4].normal);
5221 r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[0].normal) + r_refdef.view.ortho_x;
5222 r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[1].normal) + r_refdef.view.ortho_x;
5223 r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[2].normal) + r_refdef.view.ortho_y;
5224 r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[3].normal) + r_refdef.view.ortho_y;
5225 r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, r_refdef.view.frustum[4].normal) + r_refdef.nearclip;
5227 r_refdef.view.numfrustumplanes = 5;
5229 if (r_refdef.view.useclipplane)
5231 r_refdef.view.numfrustumplanes = 6;
5232 r_refdef.view.frustum[5] = r_refdef.view.clipplane;
5235 for (i = 0;i < r_refdef.view.numfrustumplanes;i++)
5236 PlaneClassify(r_refdef.view.frustum + i);
5238 // LordHavoc: note to all quake engine coders, Quake had a special case
5239 // for 90 degrees which assumed a square view (wrong), so I removed it,
5240 // Quake2 has it disabled as well.
5242 // rotate R_VIEWFORWARD right by FOV_X/2 degrees
5243 //RotatePointAroundVector( r_refdef.view.frustum[0].normal, up, forward, -(90 - r_refdef.fov_x / 2));
5244 //r_refdef.view.frustum[0].dist = DotProduct (r_refdef.view.origin, frustum[0].normal);
5245 //PlaneClassify(&frustum[0]);
5247 // rotate R_VIEWFORWARD left by FOV_X/2 degrees
5248 //RotatePointAroundVector( r_refdef.view.frustum[1].normal, up, forward, (90 - r_refdef.fov_x / 2));
5249 //r_refdef.view.frustum[1].dist = DotProduct (r_refdef.view.origin, frustum[1].normal);
5250 //PlaneClassify(&frustum[1]);
5252 // rotate R_VIEWFORWARD up by FOV_X/2 degrees
5253 //RotatePointAroundVector( r_refdef.view.frustum[2].normal, left, forward, -(90 - r_refdef.fov_y / 2));
5254 //r_refdef.view.frustum[2].dist = DotProduct (r_refdef.view.origin, frustum[2].normal);
5255 //PlaneClassify(&frustum[2]);
5257 // rotate R_VIEWFORWARD down by FOV_X/2 degrees
5258 //RotatePointAroundVector( r_refdef.view.frustum[3].normal, left, forward, (90 - r_refdef.fov_y / 2));
5259 //r_refdef.view.frustum[3].dist = DotProduct (r_refdef.view.origin, frustum[3].normal);
5260 //PlaneClassify(&frustum[3]);
5263 //VectorCopy(forward, r_refdef.view.frustum[4].normal);
5264 //r_refdef.view.frustum[4].dist = DotProduct (r_refdef.view.origin, frustum[4].normal) + r_nearclip.value;
5265 //PlaneClassify(&frustum[4]);
5268 static void R_View_UpdateWithScissor(const int *myscissor)
5270 R_Main_ResizeViewCache();
5271 R_View_SetFrustum(myscissor);
5272 R_View_WorldVisibility(r_refdef.view.useclipplane);
5273 R_View_UpdateEntityVisible();
5274 R_View_UpdateEntityLighting();
5275 R_AnimCache_CacheVisibleEntities();
5278 static void R_View_Update(void)
5280 R_Main_ResizeViewCache();
5281 R_View_SetFrustum(NULL);
5282 R_View_WorldVisibility(r_refdef.view.useclipplane);
5283 R_View_UpdateEntityVisible();
5284 R_View_UpdateEntityLighting();
5285 R_AnimCache_CacheVisibleEntities();
5288 float viewscalefpsadjusted = 1.0f;
5290 static void R_GetScaledViewSize(int width, int height, int *outwidth, int *outheight)
5292 float scale = r_viewscale.value * sqrt(viewscalefpsadjusted);
5293 scale = bound(0.03125f, scale, 1.0f);
5294 *outwidth = (int)ceil(width * scale);
5295 *outheight = (int)ceil(height * scale);
5298 void R_SetupView(qboolean allowwaterclippingplane, int fbo, rtexture_t *depthtexture, rtexture_t *colortexture)
5300 const float *customclipplane = NULL;
5302 int /*rtwidth,*/ rtheight, scaledwidth, scaledheight;
5303 if (r_refdef.view.useclipplane && allowwaterclippingplane)
5305 // LordHavoc: couldn't figure out how to make this approach the
5306 vec_t dist = r_refdef.view.clipplane.dist - r_water_clippingplanebias.value;
5307 vec_t viewdist = DotProduct(r_refdef.view.origin, r_refdef.view.clipplane.normal);
5308 if (viewdist < r_refdef.view.clipplane.dist + r_water_clippingplanebias.value)
5309 dist = r_refdef.view.clipplane.dist;
5310 plane[0] = r_refdef.view.clipplane.normal[0];
5311 plane[1] = r_refdef.view.clipplane.normal[1];
5312 plane[2] = r_refdef.view.clipplane.normal[2];
5314 if(vid.renderpath != RENDERPATH_SOFT) customclipplane = plane;
5317 //rtwidth = fbo ? R_TextureWidth(depthtexture ? depthtexture : colortexture) : vid.width;
5318 rtheight = fbo ? R_TextureHeight(depthtexture ? depthtexture : colortexture) : vid.height;
5320 R_GetScaledViewSize(r_refdef.view.width, r_refdef.view.height, &scaledwidth, &scaledheight);
5321 if (!r_refdef.view.useperspective)
5322 R_Viewport_InitOrtho(&r_refdef.view.viewport, &r_refdef.view.matrix, r_refdef.view.x, rtheight - scaledheight - r_refdef.view.y, scaledwidth, scaledheight, -r_refdef.view.ortho_x, -r_refdef.view.ortho_y, r_refdef.view.ortho_x, r_refdef.view.ortho_y, -r_refdef.farclip, r_refdef.farclip, customclipplane);
5323 else if (vid.stencil && r_useinfinitefarclip.integer)
5324 R_Viewport_InitPerspectiveInfinite(&r_refdef.view.viewport, &r_refdef.view.matrix, r_refdef.view.x, rtheight - scaledheight - r_refdef.view.y, scaledwidth, scaledheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, customclipplane);
5326 R_Viewport_InitPerspective(&r_refdef.view.viewport, &r_refdef.view.matrix, r_refdef.view.x, rtheight - scaledheight - r_refdef.view.y, scaledwidth, scaledheight, r_refdef.view.frustum_x, r_refdef.view.frustum_y, r_refdef.nearclip, r_refdef.farclip, customclipplane);
5327 R_Mesh_SetRenderTargets(fbo, depthtexture, colortexture, NULL, NULL, NULL);
5328 R_SetViewport(&r_refdef.view.viewport);
5329 if (r_refdef.view.useclipplane && allowwaterclippingplane && vid.renderpath == RENDERPATH_SOFT)
5331 matrix4x4_t mvpmatrix, invmvpmatrix, invtransmvpmatrix;
5332 float screenplane[4];
5333 Matrix4x4_Concat(&mvpmatrix, &r_refdef.view.viewport.projectmatrix, &r_refdef.view.viewport.viewmatrix);
5334 Matrix4x4_Invert_Full(&invmvpmatrix, &mvpmatrix);
5335 Matrix4x4_Transpose(&invtransmvpmatrix, &invmvpmatrix);
5336 Matrix4x4_Transform4(&invtransmvpmatrix, plane, screenplane);
5337 DPSOFTRAST_ClipPlane(screenplane[0], screenplane[1], screenplane[2], screenplane[3]);
5341 void R_EntityMatrix(const matrix4x4_t *matrix)
5343 if (gl_modelmatrixchanged || memcmp(matrix, &gl_modelmatrix, sizeof(matrix4x4_t)))
5345 gl_modelmatrixchanged = false;
5346 gl_modelmatrix = *matrix;
5347 Matrix4x4_Concat(&gl_modelviewmatrix, &gl_viewmatrix, &gl_modelmatrix);
5348 Matrix4x4_Concat(&gl_modelviewprojectionmatrix, &gl_projectionmatrix, &gl_modelviewmatrix);
5349 Matrix4x4_ToArrayFloatGL(&gl_modelviewmatrix, gl_modelview16f);
5350 Matrix4x4_ToArrayFloatGL(&gl_modelviewprojectionmatrix, gl_modelviewprojection16f);
5352 switch(vid.renderpath)
5354 case RENDERPATH_D3D9:
5356 hlslVSSetParameter16f(D3DVSREGISTER_ModelViewProjectionMatrix, gl_modelviewprojection16f);
5357 hlslVSSetParameter16f(D3DVSREGISTER_ModelViewMatrix, gl_modelview16f);
5360 case RENDERPATH_D3D10:
5361 Con_DPrintf("FIXME D3D10 shader %s:%i\n", __FILE__, __LINE__);
5363 case RENDERPATH_D3D11:
5364 Con_DPrintf("FIXME D3D11 shader %s:%i\n", __FILE__, __LINE__);
5366 case RENDERPATH_GL11:
5367 case RENDERPATH_GL13:
5368 case RENDERPATH_GLES1:
5369 qglLoadMatrixf(gl_modelview16f);CHECKGLERROR
5371 case RENDERPATH_SOFT:
5372 DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelViewProjectionMatrixM1, 1, false, gl_modelviewprojection16f);
5373 DPSOFTRAST_UniformMatrix4fv(DPSOFTRAST_UNIFORM_ModelViewMatrixM1, 1, false, gl_modelview16f);
5375 case RENDERPATH_GL20:
5376 case RENDERPATH_GLES2:
5377 if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
5378 if (r_glsl_permutation && r_glsl_permutation->loc_ModelViewMatrix >= 0) qglUniformMatrix4fv(r_glsl_permutation->loc_ModelViewMatrix, 1, false, gl_modelview16f);
5384 void R_ResetViewRendering2D_Common(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, float x2, float y2)
5386 r_viewport_t viewport;
5390 // GL is weird because it's bottom to top, r_refdef.view.y is top to bottom
5391 R_Viewport_InitOrtho(&viewport, &identitymatrix, r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height, 0, 0, x2, y2, -10, 100, NULL);
5392 R_Mesh_SetRenderTargets(fbo, depthtexture, colortexture, NULL, NULL, NULL);
5393 R_SetViewport(&viewport);
5394 GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
5395 GL_Color(1, 1, 1, 1);
5396 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
5397 GL_BlendFunc(GL_ONE, GL_ZERO);
5398 GL_ScissorTest(false);
5399 GL_DepthMask(false);
5400 GL_DepthRange(0, 1);
5401 GL_DepthTest(false);
5402 GL_DepthFunc(GL_LEQUAL);
5403 R_EntityMatrix(&identitymatrix);
5404 R_Mesh_ResetTextureState();
5405 GL_PolygonOffset(0, 0);
5406 R_SetStencil(false, 255, GL_KEEP, GL_KEEP, GL_KEEP, GL_ALWAYS, 128, 255);
5407 switch(vid.renderpath)
5409 case RENDERPATH_GL11:
5410 case RENDERPATH_GL13:
5411 case RENDERPATH_GL20:
5412 case RENDERPATH_GLES1:
5413 case RENDERPATH_GLES2:
5414 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
5416 case RENDERPATH_D3D9:
5417 case RENDERPATH_D3D10:
5418 case RENDERPATH_D3D11:
5419 case RENDERPATH_SOFT:
5422 GL_CullFace(GL_NONE);
5427 void R_ResetViewRendering2D(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture)
5431 R_ResetViewRendering2D_Common(fbo, depthtexture, colortexture, 1, 1);
5434 void R_ResetViewRendering3D(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture)
5438 R_SetupView(true, fbo, depthtexture, colortexture);
5439 GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
5440 GL_Color(1, 1, 1, 1);
5441 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
5442 GL_BlendFunc(GL_ONE, GL_ZERO);
5443 GL_ScissorTest(true);
5445 GL_DepthRange(0, 1);
5447 GL_DepthFunc(GL_LEQUAL);
5448 R_EntityMatrix(&identitymatrix);
5449 R_Mesh_ResetTextureState();
5450 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
5451 R_SetStencil(false, 255, GL_KEEP, GL_KEEP, GL_KEEP, GL_ALWAYS, 128, 255);
5452 switch(vid.renderpath)
5454 case RENDERPATH_GL11:
5455 case RENDERPATH_GL13:
5456 case RENDERPATH_GL20:
5457 case RENDERPATH_GLES1:
5458 case RENDERPATH_GLES2:
5459 qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
5461 case RENDERPATH_D3D9:
5462 case RENDERPATH_D3D10:
5463 case RENDERPATH_D3D11:
5464 case RENDERPATH_SOFT:
5467 GL_CullFace(r_refdef.view.cullface_back);
5472 R_RenderView_UpdateViewVectors
5475 void R_RenderView_UpdateViewVectors(void)
5477 // break apart the view matrix into vectors for various purposes
5478 // it is important that this occurs outside the RenderScene function because that can be called from reflection renders, where the vectors come out wrong
5479 // however the r_refdef.view.origin IS updated in RenderScene intentionally - otherwise the sky renders at the wrong origin, etc
5480 Matrix4x4_ToVectors(&r_refdef.view.matrix, r_refdef.view.forward, r_refdef.view.left, r_refdef.view.up, r_refdef.view.origin);
5481 VectorNegate(r_refdef.view.left, r_refdef.view.right);
5482 // make an inverted copy of the view matrix for tracking sprites
5483 Matrix4x4_Invert_Simple(&r_refdef.view.inverse_matrix, &r_refdef.view.matrix);
5486 void R_RenderScene(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture);
5487 void R_RenderWaterPlanes(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture);
5489 static void R_Water_StartFrame(void)
5492 int waterwidth, waterheight, texturewidth, textureheight, camerawidth, cameraheight;
5493 r_waterstate_waterplane_t *p;
5494 qboolean usewaterfbo = (r_viewfbo.integer >= 1 || r_water_fbo.integer >= 1) && vid.support.ext_framebuffer_object && vid.samples < 2;
5496 if (vid.width > (int)vid.maxtexturesize_2d || vid.height > (int)vid.maxtexturesize_2d)
5499 switch(vid.renderpath)
5501 case RENDERPATH_GL20:
5502 case RENDERPATH_D3D9:
5503 case RENDERPATH_D3D10:
5504 case RENDERPATH_D3D11:
5505 case RENDERPATH_SOFT:
5506 case RENDERPATH_GLES2:
5508 case RENDERPATH_GL11:
5509 case RENDERPATH_GL13:
5510 case RENDERPATH_GLES1:
5514 // set waterwidth and waterheight to the water resolution that will be
5515 // used (often less than the screen resolution for faster rendering)
5516 R_GetScaledViewSize(bound(1, vid.width * r_water_resolutionmultiplier.value, vid.width), bound(1, vid.height * r_water_resolutionmultiplier.value, vid.height), &waterwidth, &waterheight);
5518 // calculate desired texture sizes
5519 // can't use water if the card does not support the texture size
5520 if (!r_water.integer || r_showsurfaces.integer)
5521 texturewidth = textureheight = waterwidth = waterheight = camerawidth = cameraheight = 0;
5522 else if (vid.support.arb_texture_non_power_of_two)
5524 texturewidth = waterwidth;
5525 textureheight = waterheight;
5526 camerawidth = waterwidth;
5527 cameraheight = waterheight;
5531 for (texturewidth = 1;texturewidth < waterwidth ;texturewidth *= 2);
5532 for (textureheight = 1;textureheight < waterheight;textureheight *= 2);
5533 for (camerawidth = 1;camerawidth <= waterwidth; camerawidth *= 2); camerawidth /= 2;
5534 for (cameraheight = 1;cameraheight <= waterheight;cameraheight *= 2); cameraheight /= 2;
5537 // allocate textures as needed
5538 if (r_fb.water.texturewidth != texturewidth || r_fb.water.textureheight != textureheight || r_fb.water.camerawidth != camerawidth || r_fb.water.cameraheight != cameraheight || (r_fb.depthtexture && !usewaterfbo))
5540 r_fb.water.maxwaterplanes = MAX_WATERPLANES;
5541 for (i = 0, p = r_fb.water.waterplanes;i < r_fb.water.maxwaterplanes;i++, p++)
5543 if (p->texture_refraction)
5544 R_FreeTexture(p->texture_refraction);
5545 p->texture_refraction = NULL;
5546 if (p->fbo_refraction)
5547 R_Mesh_DestroyFramebufferObject(p->fbo_refraction);
5548 p->fbo_refraction = 0;
5549 if (p->texture_reflection)
5550 R_FreeTexture(p->texture_reflection);
5551 p->texture_reflection = NULL;
5552 if (p->fbo_reflection)
5553 R_Mesh_DestroyFramebufferObject(p->fbo_reflection);
5554 p->fbo_reflection = 0;
5555 if (p->texture_camera)
5556 R_FreeTexture(p->texture_camera);
5557 p->texture_camera = NULL;
5559 R_Mesh_DestroyFramebufferObject(p->fbo_camera);
5562 memset(&r_fb.water, 0, sizeof(r_fb.water));
5563 r_fb.water.texturewidth = texturewidth;
5564 r_fb.water.textureheight = textureheight;
5565 r_fb.water.camerawidth = camerawidth;
5566 r_fb.water.cameraheight = cameraheight;
5569 if (r_fb.water.texturewidth)
5571 int scaledwidth, scaledheight;
5573 r_fb.water.enabled = true;
5575 // water resolution is usually reduced
5576 r_fb.water.waterwidth = (int)bound(1, r_refdef.view.width * r_water_resolutionmultiplier.value, r_refdef.view.width);
5577 r_fb.water.waterheight = (int)bound(1, r_refdef.view.height * r_water_resolutionmultiplier.value, r_refdef.view.height);
5578 R_GetScaledViewSize(r_fb.water.waterwidth, r_fb.water.waterheight, &scaledwidth, &scaledheight);
5580 // set up variables that will be used in shader setup
5581 r_fb.water.screenscale[0] = 0.5f * (float)scaledwidth / (float)r_fb.water.texturewidth;
5582 r_fb.water.screenscale[1] = 0.5f * (float)scaledheight / (float)r_fb.water.textureheight;
5583 r_fb.water.screencenter[0] = 0.5f * (float)scaledwidth / (float)r_fb.water.texturewidth;
5584 r_fb.water.screencenter[1] = 0.5f * (float)scaledheight / (float)r_fb.water.textureheight;
5587 r_fb.water.maxwaterplanes = MAX_WATERPLANES;
5588 r_fb.water.numwaterplanes = 0;
5591 void R_Water_AddWaterPlane(msurface_t *surface, int entno)
5593 int planeindex, bestplaneindex, vertexindex;
5594 vec3_t mins, maxs, normal, center, v, n;
5595 vec_t planescore, bestplanescore;
5597 r_waterstate_waterplane_t *p;
5598 texture_t *t = R_GetCurrentTexture(surface->texture);
5600 rsurface.texture = t;
5601 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS, 1, ((const msurface_t **)&surface));
5602 // if the model has no normals, it's probably off-screen and they were not generated, so don't add it anyway
5603 if (!rsurface.batchnormal3f || rsurface.batchnumvertices < 1)
5605 // average the vertex normals, find the surface bounds (after deformvertexes)
5606 Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f, v);
5607 Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f, n);
5608 VectorCopy(n, normal);
5609 VectorCopy(v, mins);
5610 VectorCopy(v, maxs);
5611 for (vertexindex = 1;vertexindex < rsurface.batchnumvertices;vertexindex++)
5613 Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f + vertexindex*3, v);
5614 Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f + vertexindex*3, n);
5615 VectorAdd(normal, n, normal);
5616 mins[0] = min(mins[0], v[0]);
5617 mins[1] = min(mins[1], v[1]);
5618 mins[2] = min(mins[2], v[2]);
5619 maxs[0] = max(maxs[0], v[0]);
5620 maxs[1] = max(maxs[1], v[1]);
5621 maxs[2] = max(maxs[2], v[2]);
5623 VectorNormalize(normal);
5624 VectorMAM(0.5f, mins, 0.5f, maxs, center);
5626 VectorCopy(normal, plane.normal);
5627 VectorNormalize(plane.normal);
5628 plane.dist = DotProduct(center, plane.normal);
5629 PlaneClassify(&plane);
5630 if (PlaneDiff(r_refdef.view.origin, &plane) < 0)
5632 // skip backfaces (except if nocullface is set)
5633 // if (!(t->currentmaterialflags & MATERIALFLAG_NOCULLFACE))
5635 VectorNegate(plane.normal, plane.normal);
5637 PlaneClassify(&plane);
5641 // find a matching plane if there is one
5642 bestplaneindex = -1;
5643 bestplanescore = 1048576.0f;
5644 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
5646 if(p->camera_entity == t->camera_entity)
5648 planescore = 1.0f - DotProduct(plane.normal, p->plane.normal) + fabs(plane.dist - p->plane.dist) * 0.001f;
5649 if (bestplaneindex < 0 || bestplanescore > planescore)
5651 bestplaneindex = planeindex;
5652 bestplanescore = planescore;
5656 planeindex = bestplaneindex;
5657 p = r_fb.water.waterplanes + planeindex;
5659 // if this surface does not fit any known plane rendered this frame, add one
5660 if ((planeindex < 0 || bestplanescore > 0.001f) && r_fb.water.numwaterplanes < r_fb.water.maxwaterplanes)
5662 // store the new plane
5663 planeindex = r_fb.water.numwaterplanes++;
5664 p = r_fb.water.waterplanes + planeindex;
5666 // clear materialflags and pvs
5667 p->materialflags = 0;
5668 p->pvsvalid = false;
5669 p->camera_entity = t->camera_entity;
5670 VectorCopy(mins, p->mins);
5671 VectorCopy(maxs, p->maxs);
5675 // merge mins/maxs when we're adding this surface to the plane
5676 p->mins[0] = min(p->mins[0], mins[0]);
5677 p->mins[1] = min(p->mins[1], mins[1]);
5678 p->mins[2] = min(p->mins[2], mins[2]);
5679 p->maxs[0] = max(p->maxs[0], maxs[0]);
5680 p->maxs[1] = max(p->maxs[1], maxs[1]);
5681 p->maxs[2] = max(p->maxs[2], maxs[2]);
5683 // merge this surface's materialflags into the waterplane
5684 p->materialflags |= t->currentmaterialflags;
5685 if(!(p->materialflags & MATERIALFLAG_CAMERA))
5687 // merge this surface's PVS into the waterplane
5688 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS
5689 && r_refdef.scene.worldmodel->brush.PointInLeaf && r_refdef.scene.worldmodel->brush.PointInLeaf(r_refdef.scene.worldmodel, center)->clusterindex >= 0)
5691 r_refdef.scene.worldmodel->brush.FatPVS(r_refdef.scene.worldmodel, center, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
5697 extern cvar_t r_drawparticles;
5698 extern cvar_t r_drawdecals;
5700 static void R_Water_ProcessPlanes(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture)
5703 r_refdef_view_t originalview;
5704 r_refdef_view_t myview;
5705 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;
5706 r_waterstate_waterplane_t *p;
5708 qboolean usewaterfbo = (r_viewfbo.integer >= 1 || r_water_fbo.integer >= 1) && vid.support.ext_framebuffer_object && vid.samples < 2;
5711 originalview = r_refdef.view;
5713 // lowquality hack, temporarily shut down some cvars and restore afterwards
5714 qualityreduction = r_water_lowquality.integer;
5715 if (qualityreduction > 0)
5717 if (qualityreduction >= 1)
5719 old_r_shadows = r_shadows.integer;
5720 old_r_worldrtlight = r_shadow_realtime_world.integer;
5721 old_r_dlight = r_shadow_realtime_dlight.integer;
5722 Cvar_SetValueQuick(&r_shadows, 0);
5723 Cvar_SetValueQuick(&r_shadow_realtime_world, 0);
5724 Cvar_SetValueQuick(&r_shadow_realtime_dlight, 0);
5726 if (qualityreduction >= 2)
5728 old_r_dynamic = r_dynamic.integer;
5729 old_r_particles = r_drawparticles.integer;
5730 old_r_decals = r_drawdecals.integer;
5731 Cvar_SetValueQuick(&r_dynamic, 0);
5732 Cvar_SetValueQuick(&r_drawparticles, 0);
5733 Cvar_SetValueQuick(&r_drawdecals, 0);
5737 // make sure enough textures are allocated
5738 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
5740 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
5742 if (!p->texture_refraction)
5743 p->texture_refraction = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "waterplane%i_refraction", planeindex), r_fb.water.texturewidth, r_fb.water.textureheight, NULL, r_fb.textype, TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
5744 if (!p->texture_refraction)
5748 if (r_fb.water.depthtexture == NULL)
5749 r_fb.water.depthtexture = R_LoadTextureRenderBuffer(r_main_texturepool, "waterviewdepth", r_fb.water.texturewidth, r_fb.water.textureheight, TEXTYPE_DEPTHBUFFER24STENCIL8);
5750 if (p->fbo_refraction == 0)
5751 p->fbo_refraction = R_Mesh_CreateFramebufferObject(r_fb.water.depthtexture, p->texture_refraction, NULL, NULL, NULL);
5754 else if (p->materialflags & MATERIALFLAG_CAMERA)
5756 if (!p->texture_camera)
5757 p->texture_camera = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "waterplane%i_camera", planeindex), r_fb.water.camerawidth, r_fb.water.cameraheight, NULL, r_fb.textype, TEXF_RENDERTARGET | TEXF_FORCELINEAR, -1, NULL);
5758 if (!p->texture_camera)
5762 if (r_fb.water.depthtexture == NULL)
5763 r_fb.water.depthtexture = R_LoadTextureRenderBuffer(r_main_texturepool, "waterviewdepth", r_fb.water.texturewidth, r_fb.water.textureheight, TEXTYPE_DEPTHBUFFER24STENCIL8);
5764 if (p->fbo_camera == 0)
5765 p->fbo_camera = R_Mesh_CreateFramebufferObject(r_fb.water.depthtexture, p->texture_camera, NULL, NULL, NULL);
5769 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
5771 if (!p->texture_reflection)
5772 p->texture_reflection = R_LoadTexture2D(r_main_texturepool, va(vabuf, sizeof(vabuf), "waterplane%i_reflection", planeindex), r_fb.water.texturewidth, r_fb.water.textureheight, NULL, r_fb.textype, TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
5773 if (!p->texture_reflection)
5777 if (r_fb.water.depthtexture == NULL)
5778 r_fb.water.depthtexture = R_LoadTextureRenderBuffer(r_main_texturepool, "waterviewdepth", r_fb.water.texturewidth, r_fb.water.textureheight, TEXTYPE_DEPTHBUFFER24STENCIL8);
5779 if (p->fbo_reflection == 0)
5780 p->fbo_reflection = R_Mesh_CreateFramebufferObject(r_fb.water.depthtexture, p->texture_reflection, NULL, NULL, NULL);
5786 r_refdef.view = originalview;
5787 r_refdef.view.showdebug = false;
5788 r_refdef.view.width = r_fb.water.waterwidth;
5789 r_refdef.view.height = r_fb.water.waterheight;
5790 r_refdef.view.useclipplane = true;
5791 myview = r_refdef.view;
5792 r_fb.water.renderingscene = true;
5793 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
5795 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
5797 r_refdef.view = myview;
5798 if(r_water_scissormode.integer)
5800 R_SetupView(true, p->fbo_reflection, r_fb.water.depthtexture, p->texture_reflection);
5801 if(R_ScissorForBBox(p->mins, p->maxs, myscissor))
5802 continue; // FIXME the plane then still may get rendered but with broken texture, but it sure won't be visible
5805 // render reflected scene and copy into texture
5806 Matrix4x4_Reflect(&r_refdef.view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
5807 // update the r_refdef.view.origin because otherwise the sky renders at the wrong location (amongst other problems)
5808 Matrix4x4_OriginFromMatrix(&r_refdef.view.matrix, r_refdef.view.origin);
5809 r_refdef.view.clipplane = p->plane;
5810 // reverse the cullface settings for this render
5811 r_refdef.view.cullface_front = GL_FRONT;
5812 r_refdef.view.cullface_back = GL_BACK;
5813 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.num_pvsclusterbytes)
5815 r_refdef.view.usecustompvs = true;
5817 memcpy(r_refdef.viewcache.world_pvsbits, p->pvsbits, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
5819 memset(r_refdef.viewcache.world_pvsbits, 0xFF, r_refdef.scene.worldmodel->brush.num_pvsclusterbytes);
5822 r_fb.water.hideplayer = r_water_hideplayer.integer >= 2;
5823 R_ResetViewRendering3D(p->fbo_reflection, r_fb.water.depthtexture, p->texture_reflection);
5824 R_ClearScreen(r_refdef.fogenabled);
5825 if(r_water_scissormode.integer & 2)
5826 R_View_UpdateWithScissor(myscissor);
5829 if(r_water_scissormode.integer & 1)
5830 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
5831 R_RenderScene(p->fbo_reflection, r_fb.water.depthtexture, p->texture_reflection);
5833 if (!p->fbo_reflection)
5834 R_Mesh_CopyToTexture(p->texture_reflection, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
5835 r_fb.water.hideplayer = false;
5838 // render the normal view scene and copy into texture
5839 // (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)
5840 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
5842 r_refdef.view = myview;
5843 if(r_water_scissormode.integer)
5845 R_SetupView(true, p->fbo_refraction, r_fb.water.depthtexture, p->texture_refraction);
5846 if(R_ScissorForBBox(p->mins, p->maxs, myscissor))
5847 continue; // FIXME the plane then still may get rendered but with broken texture, but it sure won't be visible
5850 r_fb.water.hideplayer = r_water_hideplayer.integer >= 1;
5852 r_refdef.view.clipplane = p->plane;
5853 VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
5854 r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
5856 if((p->materialflags & MATERIALFLAG_CAMERA) && p->camera_entity)
5858 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
5859 r_fb.water.hideplayer = false; // we don't want to hide the player model from these ones
5860 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
5861 R_RenderView_UpdateViewVectors();
5862 if(r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
5864 r_refdef.view.usecustompvs = true;
5865 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);
5869 PlaneClassify(&r_refdef.view.clipplane);
5871 R_ResetViewRendering3D(p->fbo_refraction, r_fb.water.depthtexture, p->texture_refraction);
5872 R_ClearScreen(r_refdef.fogenabled);
5873 if(r_water_scissormode.integer & 2)
5874 R_View_UpdateWithScissor(myscissor);
5877 if(r_water_scissormode.integer & 1)
5878 GL_Scissor(myscissor[0], myscissor[1], myscissor[2], myscissor[3]);
5879 R_RenderScene(p->fbo_refraction, r_fb.water.depthtexture, p->texture_refraction);
5881 if (!p->fbo_refraction)
5882 R_Mesh_CopyToTexture(p->texture_refraction, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
5883 r_fb.water.hideplayer = false;
5885 else if (p->materialflags & MATERIALFLAG_CAMERA)
5887 r_refdef.view = myview;
5889 r_refdef.view.clipplane = p->plane;
5890 VectorNegate(r_refdef.view.clipplane.normal, r_refdef.view.clipplane.normal);
5891 r_refdef.view.clipplane.dist = -r_refdef.view.clipplane.dist;
5893 r_refdef.view.width = r_fb.water.camerawidth;
5894 r_refdef.view.height = r_fb.water.cameraheight;
5895 r_refdef.view.frustum_x = 1; // tan(45 * M_PI / 180.0);
5896 r_refdef.view.frustum_y = 1; // tan(45 * M_PI / 180.0);
5897 r_refdef.view.ortho_x = 90; // abused as angle by VM_CL_R_SetView
5898 r_refdef.view.ortho_y = 90; // abused as angle by VM_CL_R_SetView
5900 if(p->camera_entity)
5902 // we need to perform a matrix transform to render the view... so let's get the transformation matrix
5903 CL_VM_TransformView(p->camera_entity - MAX_EDICTS, &r_refdef.view.matrix, &r_refdef.view.clipplane, visorigin);
5906 // note: all of the view is used for displaying... so
5907 // there is no use in scissoring
5909 // reverse the cullface settings for this render
5910 r_refdef.view.cullface_front = GL_FRONT;
5911 r_refdef.view.cullface_back = GL_BACK;
5912 // also reverse the view matrix
5913 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
5914 R_RenderView_UpdateViewVectors();
5915 if(p->camera_entity && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.FatPVS)
5917 r_refdef.view.usecustompvs = true;
5918 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);
5921 // camera needs no clipplane
5922 r_refdef.view.useclipplane = false;
5924 PlaneClassify(&r_refdef.view.clipplane);
5926 r_fb.water.hideplayer = false;
5928 R_ResetViewRendering3D(p->fbo_camera, r_fb.water.depthtexture, p->texture_camera);
5929 R_ClearScreen(r_refdef.fogenabled);
5931 R_RenderScene(p->fbo_camera, r_fb.water.depthtexture, p->texture_camera);
5934 R_Mesh_CopyToTexture(p->texture_camera, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
5935 r_fb.water.hideplayer = false;
5939 if(vid.renderpath==RENDERPATH_SOFT) DPSOFTRAST_ClipPlane(0, 0, 0, 1);
5940 r_fb.water.renderingscene = false;
5941 r_refdef.view = originalview;
5942 R_ResetViewRendering3D(fbo, depthtexture, colortexture);
5943 if (!r_fb.water.depthtexture)
5944 R_ClearScreen(r_refdef.fogenabled);
5948 r_refdef.view = originalview;
5949 r_fb.water.renderingscene = false;
5950 Cvar_SetValueQuick(&r_water, 0);
5951 Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed! Turned off r_water.\n");
5953 // lowquality hack, restore cvars
5954 if (qualityreduction > 0)
5956 if (qualityreduction >= 1)
5958 Cvar_SetValueQuick(&r_shadows, old_r_shadows);
5959 Cvar_SetValueQuick(&r_shadow_realtime_world, old_r_worldrtlight);
5960 Cvar_SetValueQuick(&r_shadow_realtime_dlight, old_r_dlight);
5962 if (qualityreduction >= 2)
5964 Cvar_SetValueQuick(&r_dynamic, old_r_dynamic);
5965 Cvar_SetValueQuick(&r_drawparticles, old_r_particles);
5966 Cvar_SetValueQuick(&r_drawdecals, old_r_decals);
5971 static void R_Bloom_StartFrame(void)
5974 int bloomtexturewidth, bloomtextureheight, screentexturewidth, screentextureheight;
5975 int viewwidth, viewheight;
5976 qboolean useviewfbo = r_viewfbo.integer >= 1 && vid.support.ext_framebuffer_object && vid.samples < 2;
5977 textype_t textype = TEXTYPE_COLORBUFFER;
5979 switch (vid.renderpath)
5981 case RENDERPATH_GL20:
5982 r_fb.usedepthtextures = r_usedepthtextures.integer != 0;
5983 if (vid.support.ext_framebuffer_object)
5985 if (r_viewfbo.integer == 2) textype = TEXTYPE_COLORBUFFER16F;
5986 if (r_viewfbo.integer == 3) textype = TEXTYPE_COLORBUFFER32F;
5989 case RENDERPATH_GL11:
5990 case RENDERPATH_GL13:
5991 case RENDERPATH_GLES1:
5992 case RENDERPATH_GLES2:
5993 case RENDERPATH_D3D9:
5994 case RENDERPATH_D3D10:
5995 case RENDERPATH_D3D11:
5996 r_fb.usedepthtextures = false;
5998 case RENDERPATH_SOFT:
5999 r_fb.usedepthtextures = true;
6003 if (r_viewscale_fpsscaling.integer)
6005 double actualframetime;
6006 double targetframetime;
6008 actualframetime = r_refdef.lastdrawscreentime;
6009 targetframetime = (1.0 / r_viewscale_fpsscaling_target.value);
6010 adjust = (targetframetime - actualframetime) * r_viewscale_fpsscaling_multiply.value;
6011 adjust = bound(-r_viewscale_fpsscaling_stepmax.value, adjust, r_viewscale_fpsscaling_stepmax.value);
6012 if (r_viewscale_fpsscaling_stepsize.value > 0)
6013 adjust = (int)(adjust / r_viewscale_fpsscaling_stepsize.value) * r_viewscale_fpsscaling_stepsize.value;
6014 viewscalefpsadjusted += adjust;
6015 viewscalefpsadjusted = bound(r_viewscale_fpsscaling_min.value, viewscalefpsadjusted, 1.0f);
6018 viewscalefpsadjusted = 1.0f;
6020 R_GetScaledViewSize(r_refdef.view.width, r_refdef.view.height, &viewwidth, &viewheight);
6022 switch(vid.renderpath)
6024 case RENDERPATH_GL20:
6025 case RENDERPATH_D3D9:
6026 case RENDERPATH_D3D10:
6027 case RENDERPATH_D3D11:
6028 case RENDERPATH_SOFT:
6029 case RENDERPATH_GLES2:
6031 case RENDERPATH_GL11:
6032 case RENDERPATH_GL13:
6033 case RENDERPATH_GLES1:
6037 // set bloomwidth and bloomheight to the bloom resolution that will be
6038 // used (often less than the screen resolution for faster rendering)
6039 r_fb.bloomwidth = bound(1, r_bloom_resolution.integer, vid.height);
6040 r_fb.bloomheight = r_fb.bloomwidth * vid.height / vid.width;
6041 r_fb.bloomheight = bound(1, r_fb.bloomheight, vid.height);
6042 r_fb.bloomwidth = bound(1, r_fb.bloomwidth, (int)vid.maxtexturesize_2d);
6043 r_fb.bloomheight = bound(1, r_fb.bloomheight, (int)vid.maxtexturesize_2d);
6045 // calculate desired texture sizes
6046 if (vid.support.arb_texture_non_power_of_two)
6048 screentexturewidth = vid.width;
6049 screentextureheight = vid.height;
6050 bloomtexturewidth = r_fb.bloomwidth;
6051 bloomtextureheight = r_fb.bloomheight;
6055 for (screentexturewidth = 1;screentexturewidth < vid.width ;screentexturewidth *= 2);
6056 for (screentextureheight = 1;screentextureheight < vid.height ;screentextureheight *= 2);
6057 for (bloomtexturewidth = 1;bloomtexturewidth < r_fb.bloomwidth ;bloomtexturewidth *= 2);
6058 for (bloomtextureheight = 1;bloomtextureheight < r_fb.bloomheight;bloomtextureheight *= 2);
6061 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))
6063 Cvar_SetValueQuick(&r_bloom, 0);
6064 Cvar_SetValueQuick(&r_motionblur, 0);
6065 Cvar_SetValueQuick(&r_damageblur, 0);
6068 if (!(r_glsl_postprocess.integer || (!R_Stereo_ColorMasking() && r_glsl_saturation.value != 1) || (v_glslgamma.integer && !vid_gammatables_trivial))
6070 && (R_Stereo_Active() || (r_motionblur.value <= 0 && r_damageblur.value <= 0))
6072 && r_viewscale.value == 1.0f
6073 && !r_viewscale_fpsscaling.integer)
6074 screentexturewidth = screentextureheight = 0;
6075 if (!r_bloom.integer)
6076 bloomtexturewidth = bloomtextureheight = 0;
6078 // allocate textures as needed
6079 if (r_fb.screentexturewidth != screentexturewidth
6080 || r_fb.screentextureheight != screentextureheight
6081 || r_fb.bloomtexturewidth != bloomtexturewidth
6082 || r_fb.bloomtextureheight != bloomtextureheight
6083 || r_fb.textype != textype
6084 || useviewfbo != (r_fb.fbo != 0))
6086 for (i = 0;i < (int)(sizeof(r_fb.bloomtexture)/sizeof(r_fb.bloomtexture[i]));i++)
6088 if (r_fb.bloomtexture[i])
6089 R_FreeTexture(r_fb.bloomtexture[i]);
6090 r_fb.bloomtexture[i] = NULL;
6092 if (r_fb.bloomfbo[i])
6093 R_Mesh_DestroyFramebufferObject(r_fb.bloomfbo[i]);
6094 r_fb.bloomfbo[i] = 0;
6098 R_Mesh_DestroyFramebufferObject(r_fb.fbo);
6101 if (r_fb.colortexture)
6102 R_FreeTexture(r_fb.colortexture);
6103 r_fb.colortexture = NULL;
6105 if (r_fb.depthtexture)
6106 R_FreeTexture(r_fb.depthtexture);
6107 r_fb.depthtexture = NULL;
6109 if (r_fb.ghosttexture)
6110 R_FreeTexture(r_fb.ghosttexture);
6111 r_fb.ghosttexture = NULL;
6113 r_fb.screentexturewidth = screentexturewidth;
6114 r_fb.screentextureheight = screentextureheight;
6115 r_fb.bloomtexturewidth = bloomtexturewidth;
6116 r_fb.bloomtextureheight = bloomtextureheight;
6117 r_fb.textype = textype;
6119 if (r_fb.screentexturewidth && r_fb.screentextureheight)
6121 if (r_motionblur.value > 0 || r_damageblur.value > 0)
6122 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);
6123 r_fb.ghosttexture_valid = false;
6124 r_fb.colortexture = R_LoadTexture2D(r_main_texturepool, "framebuffercolor", r_fb.screentexturewidth, r_fb.screentextureheight, NULL, r_fb.textype, TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
6127 r_fb.depthtexture = R_LoadTextureRenderBuffer(r_main_texturepool, "framebufferdepth", r_fb.screentexturewidth, r_fb.screentextureheight, TEXTYPE_DEPTHBUFFER24STENCIL8);
6128 r_fb.fbo = R_Mesh_CreateFramebufferObject(r_fb.depthtexture, r_fb.colortexture, NULL, NULL, NULL);
6129 R_Mesh_SetRenderTargets(r_fb.fbo, r_fb.depthtexture, r_fb.colortexture, NULL, NULL, NULL);
6133 if (r_fb.bloomtexturewidth && r_fb.bloomtextureheight)
6135 for (i = 0;i < (int)(sizeof(r_fb.bloomtexture)/sizeof(r_fb.bloomtexture[i]));i++)
6137 r_fb.bloomtexture[i] = R_LoadTexture2D(r_main_texturepool, "framebufferbloom", r_fb.bloomtexturewidth, r_fb.bloomtextureheight, NULL, r_fb.textype, TEXF_RENDERTARGET | TEXF_FORCELINEAR | TEXF_CLAMP, -1, NULL);
6139 r_fb.bloomfbo[i] = R_Mesh_CreateFramebufferObject(NULL, r_fb.bloomtexture[i], NULL, NULL, NULL);
6144 // bloom texture is a different resolution
6145 r_fb.bloomwidth = bound(1, r_bloom_resolution.integer, r_refdef.view.height);
6146 r_fb.bloomheight = r_fb.bloomwidth * r_refdef.view.height / r_refdef.view.width;
6147 r_fb.bloomheight = bound(1, r_fb.bloomheight, r_refdef.view.height);
6148 r_fb.bloomwidth = bound(1, r_fb.bloomwidth, r_fb.bloomtexturewidth);
6149 r_fb.bloomheight = bound(1, r_fb.bloomheight, r_fb.bloomtextureheight);
6151 // set up a texcoord array for the full resolution screen image
6152 // (we have to keep this around to copy back during final render)
6153 r_fb.screentexcoord2f[0] = 0;
6154 r_fb.screentexcoord2f[1] = (float)viewheight / (float)r_fb.screentextureheight;
6155 r_fb.screentexcoord2f[2] = (float)viewwidth / (float)r_fb.screentexturewidth;
6156 r_fb.screentexcoord2f[3] = (float)viewheight / (float)r_fb.screentextureheight;
6157 r_fb.screentexcoord2f[4] = (float)viewwidth / (float)r_fb.screentexturewidth;
6158 r_fb.screentexcoord2f[5] = 0;
6159 r_fb.screentexcoord2f[6] = 0;
6160 r_fb.screentexcoord2f[7] = 0;
6162 // set up a texcoord array for the reduced resolution bloom image
6163 // (which will be additive blended over the screen image)
6164 r_fb.bloomtexcoord2f[0] = 0;
6165 r_fb.bloomtexcoord2f[1] = (float)r_fb.bloomheight / (float)r_fb.bloomtextureheight;
6166 r_fb.bloomtexcoord2f[2] = (float)r_fb.bloomwidth / (float)r_fb.bloomtexturewidth;
6167 r_fb.bloomtexcoord2f[3] = (float)r_fb.bloomheight / (float)r_fb.bloomtextureheight;
6168 r_fb.bloomtexcoord2f[4] = (float)r_fb.bloomwidth / (float)r_fb.bloomtexturewidth;
6169 r_fb.bloomtexcoord2f[5] = 0;
6170 r_fb.bloomtexcoord2f[6] = 0;
6171 r_fb.bloomtexcoord2f[7] = 0;
6173 switch(vid.renderpath)
6175 case RENDERPATH_GL11:
6176 case RENDERPATH_GL13:
6177 case RENDERPATH_GL20:
6178 case RENDERPATH_SOFT:
6179 case RENDERPATH_GLES1:
6180 case RENDERPATH_GLES2:
6182 case RENDERPATH_D3D9:
6183 case RENDERPATH_D3D10:
6184 case RENDERPATH_D3D11:
6187 for (i = 0;i < 4;i++)
6189 r_fb.screentexcoord2f[i*2+0] += 0.5f / (float)r_fb.screentexturewidth;
6190 r_fb.screentexcoord2f[i*2+1] += 0.5f / (float)r_fb.screentextureheight;
6191 r_fb.bloomtexcoord2f[i*2+0] += 0.5f / (float)r_fb.bloomtexturewidth;
6192 r_fb.bloomtexcoord2f[i*2+1] += 0.5f / (float)r_fb.bloomtextureheight;
6198 R_Viewport_InitOrtho(&r_fb.bloomviewport, &identitymatrix, r_refdef.view.x, (r_fb.bloomfbo[0] ? r_fb.bloomtextureheight : vid.height) - r_fb.bloomheight - r_refdef.view.y, r_fb.bloomwidth, r_fb.bloomheight, 0, 0, 1, 1, -10, 100, NULL);
6201 r_refdef.view.clear = true;
6204 static void R_Bloom_MakeTexture(void)
6207 float xoffset, yoffset, r, brighten;
6209 float colorscale = r_bloom_colorscale.value;
6211 r_refdef.stats.bloom++;
6215 R_Mesh_CopyToTexture(r_fb.colortexture, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
6216 r_refdef.stats.bloom_copypixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
6219 // scale down screen texture to the bloom texture size
6221 r_fb.bloomindex = 0;
6222 R_Mesh_SetRenderTargets(r_fb.bloomfbo[r_fb.bloomindex], NULL, r_fb.bloomtexture[r_fb.bloomindex], NULL, NULL, NULL);
6223 R_SetViewport(&r_fb.bloomviewport);
6224 GL_BlendFunc(GL_ONE, GL_ZERO);
6225 GL_Color(colorscale, colorscale, colorscale, 1);
6226 // D3D has upside down Y coords, the easiest way to flip this is to flip the screen vertices rather than the texcoords, so we just use a different array for that...
6227 switch(vid.renderpath)
6229 case RENDERPATH_GL11:
6230 case RENDERPATH_GL13:
6231 case RENDERPATH_GL20:
6232 case RENDERPATH_GLES1:
6233 case RENDERPATH_GLES2:
6234 case RENDERPATH_SOFT:
6235 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.screentexcoord2f);
6237 case RENDERPATH_D3D9:
6238 case RENDERPATH_D3D10:
6239 case RENDERPATH_D3D11:
6240 R_Mesh_PrepareVertices_Generic_Arrays(4, r_d3dscreenvertex3f, NULL, r_fb.screentexcoord2f);
6243 // TODO: do boxfilter scale-down in shader?
6244 R_SetupShader_Generic(r_fb.colortexture, NULL, GL_MODULATE, 1, false, true, true);
6245 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
6246 r_refdef.stats.bloom_drawpixels += r_fb.bloomwidth * r_fb.bloomheight;
6248 // we now have a properly scaled bloom image
6249 if (!r_fb.bloomfbo[r_fb.bloomindex])
6251 // copy it into the bloom texture
6252 R_Mesh_CopyToTexture(r_fb.bloomtexture[r_fb.bloomindex], 0, 0, r_fb.bloomviewport.x, r_fb.bloomviewport.y, r_fb.bloomviewport.width, r_fb.bloomviewport.height);
6253 r_refdef.stats.bloom_copypixels += r_fb.bloomviewport.width * r_fb.bloomviewport.height;
6256 // multiply bloom image by itself as many times as desired
6257 for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
6259 intex = r_fb.bloomtexture[r_fb.bloomindex];
6260 r_fb.bloomindex ^= 1;
6261 R_Mesh_SetRenderTargets(r_fb.bloomfbo[r_fb.bloomindex], NULL, r_fb.bloomtexture[r_fb.bloomindex], NULL, NULL, NULL);
6263 r = bound(0, r_bloom_colorexponent.value / x, 1); // always 0.5 to 1
6264 if (!r_fb.bloomfbo[r_fb.bloomindex])
6266 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR); // square it and multiply by two
6267 GL_Color(r,r,r,1); // apply fix factor
6272 GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 128);
6273 GL_BlendFunc(GL_SRC_COLOR, GL_ZERO); // square it
6274 GL_Color(1,1,1,1); // no fix factor supported here
6276 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.bloomtexcoord2f);
6277 R_SetupShader_Generic(intex, NULL, GL_MODULATE, 1, false, true, false);
6278 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
6279 r_refdef.stats.bloom_drawpixels += r_fb.bloomwidth * r_fb.bloomheight;
6281 if (!r_fb.bloomfbo[r_fb.bloomindex])
6283 // copy the darkened image to a texture
6284 R_Mesh_CopyToTexture(r_fb.bloomtexture[r_fb.bloomindex], 0, 0, r_fb.bloomviewport.x, r_fb.bloomviewport.y, r_fb.bloomviewport.width, r_fb.bloomviewport.height);
6285 r_refdef.stats.bloom_copypixels += r_fb.bloomviewport.width * r_fb.bloomviewport.height;
6289 range = r_bloom_blur.integer * r_fb.bloomwidth / 320;
6290 brighten = r_bloom_brighten.value;
6291 brighten = sqrt(brighten);
6293 brighten *= (3 * range) / (2 * range - 1); // compensate for the "dot particle"
6295 for (dir = 0;dir < 2;dir++)
6297 intex = r_fb.bloomtexture[r_fb.bloomindex];
6298 r_fb.bloomindex ^= 1;
6299 R_Mesh_SetRenderTargets(r_fb.bloomfbo[r_fb.bloomindex], NULL, r_fb.bloomtexture[r_fb.bloomindex], NULL, NULL, NULL);
6300 // blend on at multiple vertical offsets to achieve a vertical blur
6301 // TODO: do offset blends using GLSL
6302 // TODO instead of changing the texcoords, change the target positions to prevent artifacts at edges
6303 GL_BlendFunc(GL_ONE, GL_ZERO);
6304 R_SetupShader_Generic(intex, NULL, GL_MODULATE, 1, false, true, false);
6305 for (x = -range;x <= range;x++)
6307 if (!dir){xoffset = 0;yoffset = x;}
6308 else {xoffset = x;yoffset = 0;}
6309 xoffset /= (float)r_fb.bloomtexturewidth;
6310 yoffset /= (float)r_fb.bloomtextureheight;
6311 // compute a texcoord array with the specified x and y offset
6312 r_fb.offsettexcoord2f[0] = xoffset+0;
6313 r_fb.offsettexcoord2f[1] = yoffset+(float)r_fb.bloomheight / (float)r_fb.bloomtextureheight;
6314 r_fb.offsettexcoord2f[2] = xoffset+(float)r_fb.bloomwidth / (float)r_fb.bloomtexturewidth;
6315 r_fb.offsettexcoord2f[3] = yoffset+(float)r_fb.bloomheight / (float)r_fb.bloomtextureheight;
6316 r_fb.offsettexcoord2f[4] = xoffset+(float)r_fb.bloomwidth / (float)r_fb.bloomtexturewidth;
6317 r_fb.offsettexcoord2f[5] = yoffset+0;
6318 r_fb.offsettexcoord2f[6] = xoffset+0;
6319 r_fb.offsettexcoord2f[7] = yoffset+0;
6320 // this r value looks like a 'dot' particle, fading sharply to
6321 // black at the edges
6322 // (probably not realistic but looks good enough)
6323 //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
6324 //r = brighten/(range*2+1);
6325 r = brighten / (range * 2 + 1);
6327 r *= (1 - x*x/(float)(range*range));
6328 GL_Color(r, r, r, 1);
6329 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.offsettexcoord2f);
6330 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
6331 r_refdef.stats.bloom_drawpixels += r_fb.bloomwidth * r_fb.bloomheight;
6332 GL_BlendFunc(GL_ONE, GL_ONE);
6335 if (!r_fb.bloomfbo[r_fb.bloomindex])
6337 // copy the vertically or horizontally blurred bloom view to a texture
6338 R_Mesh_CopyToTexture(r_fb.bloomtexture[r_fb.bloomindex], 0, 0, r_fb.bloomviewport.x, r_fb.bloomviewport.y, r_fb.bloomviewport.width, r_fb.bloomviewport.height);
6339 r_refdef.stats.bloom_copypixels += r_fb.bloomviewport.width * r_fb.bloomviewport.height;
6344 static void R_BlendView(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture)
6346 unsigned int permutation;
6347 float uservecs[4][4];
6349 switch (vid.renderpath)
6351 case RENDERPATH_GL20:
6352 case RENDERPATH_D3D9:
6353 case RENDERPATH_D3D10:
6354 case RENDERPATH_D3D11:
6355 case RENDERPATH_SOFT:
6356 case RENDERPATH_GLES2:
6358 (r_fb.bloomtexture[r_fb.bloomindex] ? SHADERPERMUTATION_BLOOM : 0)
6359 | (r_refdef.viewblend[3] > 0 ? SHADERPERMUTATION_VIEWTINT : 0)
6360 | ((v_glslgamma.value && !vid_gammatables_trivial) ? SHADERPERMUTATION_GAMMARAMPS : 0)
6361 | (r_glsl_postprocess.integer ? SHADERPERMUTATION_POSTPROCESSING : 0)
6362 | ((!R_Stereo_ColorMasking() && r_glsl_saturation.value != 1) ? SHADERPERMUTATION_SATURATION : 0);
6364 if (r_fb.colortexture)
6368 R_Mesh_CopyToTexture(r_fb.colortexture, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
6369 r_refdef.stats.bloom_copypixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
6372 if(!R_Stereo_Active() && (r_motionblur.value > 0 || r_damageblur.value > 0) && r_fb.ghosttexture)
6374 // declare variables
6375 float blur_factor, blur_mouseaccel, blur_velocity;
6376 static float blur_average;
6377 static vec3_t blur_oldangles; // used to see how quickly the mouse is moving
6379 // set a goal for the factoring
6380 blur_velocity = bound(0, (VectorLength(cl.movement_velocity) - r_motionblur_velocityfactor_minspeed.value)
6381 / max(1, r_motionblur_velocityfactor_maxspeed.value - r_motionblur_velocityfactor_minspeed.value), 1);
6382 blur_mouseaccel = bound(0, ((fabs(VectorLength(cl.viewangles) - VectorLength(blur_oldangles)) * 10) - r_motionblur_mousefactor_minspeed.value)
6383 / max(1, r_motionblur_mousefactor_maxspeed.value - r_motionblur_mousefactor_minspeed.value), 1);
6384 blur_factor = ((blur_velocity * r_motionblur_velocityfactor.value)
6385 + (blur_mouseaccel * r_motionblur_mousefactor.value));
6387 // from the goal, pick an averaged value between goal and last value
6388 cl.motionbluralpha = bound(0, (cl.time - cl.oldtime) / max(0.001, r_motionblur_averaging.value), 1);
6389 blur_average = blur_average * (1 - cl.motionbluralpha) + blur_factor * cl.motionbluralpha;
6391 // enforce minimum amount of blur
6392 blur_factor = blur_average * (1 - r_motionblur_minblur.value) + r_motionblur_minblur.value;
6394 //Con_Printf("motionblur: direct factor: %f, averaged factor: %f, velocity: %f, mouse accel: %f \n", blur_factor, blur_average, blur_velocity, blur_mouseaccel);
6396 // calculate values into a standard alpha
6397 cl.motionbluralpha = 1 - exp(-
6399 (r_motionblur.value * blur_factor / 80)
6401 (r_damageblur.value * (cl.cshifts[CSHIFT_DAMAGE].percent / 1600))
6404 max(0.0001, cl.time - cl.oldtime) // fps independent
6407 // randomization for the blur value to combat persistent ghosting
6408 cl.motionbluralpha *= lhrandom(1 - r_motionblur_randomize.value, 1 + r_motionblur_randomize.value);
6409 cl.motionbluralpha = bound(0, cl.motionbluralpha, r_motionblur_maxblur.value);
6412 R_ResetViewRendering2D(fbo, depthtexture, colortexture);
6413 if (cl.motionbluralpha > 0 && !r_refdef.envmap && r_fb.ghosttexture_valid)
6415 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6416 GL_Color(1, 1, 1, cl.motionbluralpha);
6417 switch(vid.renderpath)
6419 case RENDERPATH_GL11:
6420 case RENDERPATH_GL13:
6421 case RENDERPATH_GL20:
6422 case RENDERPATH_GLES1:
6423 case RENDERPATH_GLES2:
6424 case RENDERPATH_SOFT:
6425 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, r_fb.screentexcoord2f);
6427 case RENDERPATH_D3D9:
6428 case RENDERPATH_D3D10:
6429 case RENDERPATH_D3D11:
6430 R_Mesh_PrepareVertices_Generic_Arrays(4, r_d3dscreenvertex3f, NULL, r_fb.screentexcoord2f);
6433 R_SetupShader_Generic(r_fb.ghosttexture, NULL, GL_MODULATE, 1, false, true, true);
6434 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
6435 r_refdef.stats.bloom_drawpixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
6438 // updates old view angles for next pass
6439 VectorCopy(cl.viewangles, blur_oldangles);
6441 // copy view into the ghost texture
6442 R_Mesh_CopyToTexture(r_fb.ghosttexture, 0, 0, r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
6443 r_refdef.stats.bloom_copypixels += r_refdef.view.viewport.width * r_refdef.view.viewport.height;
6444 r_fb.ghosttexture_valid = true;
6449 // no r_fb.colortexture means we're rendering to the real fb
6450 // we may still have to do view tint...
6451 if (r_refdef.viewblend[3] >= (1.0f / 256.0f))
6453 // apply a color tint to the whole view
6454 R_ResetViewRendering2D(0, NULL, NULL);
6455 GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
6456 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, NULL);
6457 R_SetupShader_Generic_NoTexture(false, true);
6458 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6459 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
6461 break; // no screen processing, no bloom, skip it
6464 if (r_fb.bloomtexture[0])
6466 // make the bloom texture
6467 R_Bloom_MakeTexture();
6470 #if _MSC_VER >= 1400
6471 #define sscanf sscanf_s
6473 memset(uservecs, 0, sizeof(uservecs));
6474 if (r_glsl_postprocess_uservec1_enable.integer)
6475 sscanf(r_glsl_postprocess_uservec1.string, "%f %f %f %f", &uservecs[0][0], &uservecs[0][1], &uservecs[0][2], &uservecs[0][3]);
6476 if (r_glsl_postprocess_uservec2_enable.integer)
6477 sscanf(r_glsl_postprocess_uservec2.string, "%f %f %f %f", &uservecs[1][0], &uservecs[1][1], &uservecs[1][2], &uservecs[1][3]);
6478 if (r_glsl_postprocess_uservec3_enable.integer)
6479 sscanf(r_glsl_postprocess_uservec3.string, "%f %f %f %f", &uservecs[2][0], &uservecs[2][1], &uservecs[2][2], &uservecs[2][3]);
6480 if (r_glsl_postprocess_uservec4_enable.integer)
6481 sscanf(r_glsl_postprocess_uservec4.string, "%f %f %f %f", &uservecs[3][0], &uservecs[3][1], &uservecs[3][2], &uservecs[3][3]);
6483 R_ResetViewRendering2D(0, NULL, NULL); // here we render to the real framebuffer!
6484 GL_Color(1, 1, 1, 1);
6485 GL_BlendFunc(GL_ONE, GL_ZERO);
6487 switch(vid.renderpath)
6489 case RENDERPATH_GL20:
6490 case RENDERPATH_GLES2:
6491 R_Mesh_PrepareVertices_Mesh_Arrays(4, r_screenvertex3f, NULL, NULL, NULL, NULL, r_fb.screentexcoord2f, r_fb.bloomtexcoord2f);
6492 R_SetupShader_SetPermutationGLSL(SHADERMODE_POSTPROCESS, permutation);
6493 if (r_glsl_permutation->tex_Texture_First >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_First , r_fb.colortexture);
6494 if (r_glsl_permutation->tex_Texture_Second >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_Second , r_fb.bloomtexture[r_fb.bloomindex]);
6495 if (r_glsl_permutation->tex_Texture_GammaRamps >= 0) R_Mesh_TexBind(r_glsl_permutation->tex_Texture_GammaRamps, r_texture_gammaramps );
6496 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]);
6497 if (r_glsl_permutation->loc_PixelSize >= 0) qglUniform2f(r_glsl_permutation->loc_PixelSize , 1.0/r_fb.screentexturewidth, 1.0/r_fb.screentextureheight);
6498 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]);
6499 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]);
6500 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]);
6501 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]);
6502 if (r_glsl_permutation->loc_Saturation >= 0) qglUniform1f(r_glsl_permutation->loc_Saturation , r_glsl_saturation.value);
6503 if (r_glsl_permutation->loc_PixelToScreenTexCoord >= 0) qglUniform2f(r_glsl_permutation->loc_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height);
6504 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);
6506 case RENDERPATH_D3D9:
6508 // D3D has upside down Y coords, the easiest way to flip this is to flip the screen vertices rather than the texcoords, so we just use a different array for that...
6509 R_Mesh_PrepareVertices_Mesh_Arrays(4, r_d3dscreenvertex3f, NULL, NULL, NULL, NULL, r_fb.screentexcoord2f, r_fb.bloomtexcoord2f);
6510 R_SetupShader_SetPermutationHLSL(SHADERMODE_POSTPROCESS, permutation);
6511 R_Mesh_TexBind(GL20TU_FIRST , r_fb.colortexture);
6512 R_Mesh_TexBind(GL20TU_SECOND , r_fb.bloomtexture[r_fb.bloomindex]);
6513 R_Mesh_TexBind(GL20TU_GAMMARAMPS, r_texture_gammaramps );
6514 hlslPSSetParameter4f(D3DPSREGISTER_ViewTintColor , r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
6515 hlslPSSetParameter2f(D3DPSREGISTER_PixelSize , 1.0/r_fb.screentexturewidth, 1.0/r_fb.screentextureheight);
6516 hlslPSSetParameter4f(D3DPSREGISTER_UserVec1 , uservecs[0][0], uservecs[0][1], uservecs[0][2], uservecs[0][3]);
6517 hlslPSSetParameter4f(D3DPSREGISTER_UserVec2 , uservecs[1][0], uservecs[1][1], uservecs[1][2], uservecs[1][3]);
6518 hlslPSSetParameter4f(D3DPSREGISTER_UserVec3 , uservecs[2][0], uservecs[2][1], uservecs[2][2], uservecs[2][3]);
6519 hlslPSSetParameter4f(D3DPSREGISTER_UserVec4 , uservecs[3][0], uservecs[3][1], uservecs[3][2], uservecs[3][3]);
6520 hlslPSSetParameter1f(D3DPSREGISTER_Saturation , r_glsl_saturation.value);
6521 hlslPSSetParameter2f(D3DPSREGISTER_PixelToScreenTexCoord, 1.0f/vid.width, 1.0/vid.height);
6522 hlslPSSetParameter4f(D3DPSREGISTER_BloomColorSubtract , r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 0.0f);
6525 case RENDERPATH_D3D10:
6526 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
6528 case RENDERPATH_D3D11:
6529 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
6531 case RENDERPATH_SOFT:
6532 R_Mesh_PrepareVertices_Mesh_Arrays(4, r_screenvertex3f, NULL, NULL, NULL, NULL, r_fb.screentexcoord2f, r_fb.bloomtexcoord2f);
6533 R_SetupShader_SetPermutationSoft(SHADERMODE_POSTPROCESS, permutation);
6534 R_Mesh_TexBind(GL20TU_FIRST , r_fb.colortexture);
6535 R_Mesh_TexBind(GL20TU_SECOND , r_fb.bloomtexture[r_fb.bloomindex]);
6536 R_Mesh_TexBind(GL20TU_GAMMARAMPS, r_texture_gammaramps );
6537 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_ViewTintColor , r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
6538 DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_PixelSize , 1.0/r_fb.screentexturewidth, 1.0/r_fb.screentextureheight);
6539 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_UserVec1 , uservecs[0][0], uservecs[0][1], uservecs[0][2], uservecs[0][3]);
6540 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_UserVec2 , uservecs[1][0], uservecs[1][1], uservecs[1][2], uservecs[1][3]);
6541 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_UserVec3 , uservecs[2][0], uservecs[2][1], uservecs[2][2], uservecs[2][3]);
6542 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_UserVec4 , uservecs[3][0], uservecs[3][1], uservecs[3][2], uservecs[3][3]);
6543 DPSOFTRAST_Uniform1f(DPSOFTRAST_UNIFORM_Saturation , r_glsl_saturation.value);
6544 DPSOFTRAST_Uniform2f(DPSOFTRAST_UNIFORM_PixelToScreenTexCoord, 1.0f/vid.width, 1.0f/vid.height);
6545 DPSOFTRAST_Uniform4f(DPSOFTRAST_UNIFORM_BloomColorSubtract , r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 0.0f);
6550 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
6551 r_refdef.stats.bloom_drawpixels += r_refdef.view.width * r_refdef.view.height;
6553 case RENDERPATH_GL11:
6554 case RENDERPATH_GL13:
6555 case RENDERPATH_GLES1:
6556 if (r_refdef.viewblend[3] >= (1.0f / 256.0f))
6558 // apply a color tint to the whole view
6559 R_ResetViewRendering2D(0, NULL, NULL);
6560 GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
6561 R_Mesh_PrepareVertices_Generic_Arrays(4, r_screenvertex3f, NULL, NULL);
6562 R_SetupShader_Generic_NoTexture(false, true);
6563 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6564 R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
6570 matrix4x4_t r_waterscrollmatrix;
6572 void R_UpdateFog(void)
6575 if (gamemode == GAME_NEHAHRA)
6577 if (gl_fogenable.integer)
6579 r_refdef.oldgl_fogenable = true;
6580 r_refdef.fog_density = gl_fogdensity.value;
6581 r_refdef.fog_red = gl_fogred.value;
6582 r_refdef.fog_green = gl_foggreen.value;
6583 r_refdef.fog_blue = gl_fogblue.value;
6584 r_refdef.fog_alpha = 1;
6585 r_refdef.fog_start = 0;
6586 r_refdef.fog_end = gl_skyclip.value;
6587 r_refdef.fog_height = 1<<30;
6588 r_refdef.fog_fadedepth = 128;
6590 else if (r_refdef.oldgl_fogenable)
6592 r_refdef.oldgl_fogenable = false;
6593 r_refdef.fog_density = 0;
6594 r_refdef.fog_red = 0;
6595 r_refdef.fog_green = 0;
6596 r_refdef.fog_blue = 0;
6597 r_refdef.fog_alpha = 0;
6598 r_refdef.fog_start = 0;
6599 r_refdef.fog_end = 0;
6600 r_refdef.fog_height = 1<<30;
6601 r_refdef.fog_fadedepth = 128;
6606 r_refdef.fog_alpha = bound(0, r_refdef.fog_alpha, 1);
6607 r_refdef.fog_start = max(0, r_refdef.fog_start);
6608 r_refdef.fog_end = max(r_refdef.fog_start + 0.01, r_refdef.fog_end);
6610 if (r_refdef.fog_density && r_drawfog.integer)
6612 r_refdef.fogenabled = true;
6613 // this is the point where the fog reaches 0.9986 alpha, which we
6614 // consider a good enough cutoff point for the texture
6615 // (0.9986 * 256 == 255.6)
6616 if (r_fog_exp2.integer)
6617 r_refdef.fogrange = 32 / (r_refdef.fog_density * r_refdef.fog_density) + r_refdef.fog_start;
6619 r_refdef.fogrange = 2048 / r_refdef.fog_density + r_refdef.fog_start;
6620 r_refdef.fogrange = bound(r_refdef.fog_start, r_refdef.fogrange, r_refdef.fog_end);
6621 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
6622 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
6623 if (strcmp(r_refdef.fogheighttexturename, r_refdef.fog_height_texturename))
6624 R_BuildFogHeightTexture();
6625 // fog color was already set
6626 // update the fog texture
6627 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)
6628 R_BuildFogTexture();
6629 r_refdef.fog_height_texcoordscale = 1.0f / max(0.125f, r_refdef.fog_fadedepth);
6630 r_refdef.fog_height_tablescale = r_refdef.fog_height_tablesize * r_refdef.fog_height_texcoordscale;
6633 r_refdef.fogenabled = false;
6636 if (r_refdef.fog_density)
6638 r_refdef.fogcolor[0] = r_refdef.fog_red;
6639 r_refdef.fogcolor[1] = r_refdef.fog_green;
6640 r_refdef.fogcolor[2] = r_refdef.fog_blue;
6642 Vector4Set(r_refdef.fogplane, 0, 0, 1, -r_refdef.fog_height);
6643 r_refdef.fogplaneviewdist = DotProduct(r_refdef.fogplane, r_refdef.view.origin) + r_refdef.fogplane[3];
6644 r_refdef.fogplaneviewabove = r_refdef.fogplaneviewdist >= 0;
6645 r_refdef.fogheightfade = -0.5f/max(0.125f, r_refdef.fog_fadedepth);
6649 VectorCopy(r_refdef.fogcolor, fogvec);
6650 // color.rgb *= ContrastBoost * SceneBrightness;
6651 VectorScale(fogvec, r_refdef.view.colorscale, fogvec);
6652 r_refdef.fogcolor[0] = bound(0.0f, fogvec[0], 1.0f);
6653 r_refdef.fogcolor[1] = bound(0.0f, fogvec[1], 1.0f);
6654 r_refdef.fogcolor[2] = bound(0.0f, fogvec[2], 1.0f);
6659 void R_UpdateVariables(void)
6663 r_refdef.scene.ambient = r_ambient.value * (1.0f / 64.0f);
6665 r_refdef.farclip = r_farclip_base.value;
6666 if (r_refdef.scene.worldmodel)
6667 r_refdef.farclip += r_refdef.scene.worldmodel->radius * r_farclip_world.value * 2;
6668 r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
6670 if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
6671 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
6672 r_refdef.polygonfactor = 0;
6673 r_refdef.polygonoffset = 0;
6674 r_refdef.shadowpolygonfactor = r_refdef.polygonfactor + r_shadow_polygonfactor.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
6675 r_refdef.shadowpolygonoffset = r_refdef.polygonoffset + r_shadow_polygonoffset.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
6677 r_refdef.scene.rtworld = r_shadow_realtime_world.integer != 0;
6678 r_refdef.scene.rtworldshadows = r_shadow_realtime_world_shadows.integer && vid.stencil;
6679 r_refdef.scene.rtdlight = r_shadow_realtime_dlight.integer != 0 && !gl_flashblend.integer && r_dynamic.integer;
6680 r_refdef.scene.rtdlightshadows = r_refdef.scene.rtdlight && r_shadow_realtime_dlight_shadows.integer && vid.stencil;
6681 r_refdef.lightmapintensity = r_refdef.scene.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
6682 if (FAKELIGHT_ENABLED)
6684 r_refdef.lightmapintensity *= r_fakelight_intensity.value;
6686 else if (r_refdef.scene.worldmodel)
6688 r_refdef.lightmapintensity *= r_refdef.scene.worldmodel->lightmapscale;
6690 if (r_showsurfaces.integer)
6692 r_refdef.scene.rtworld = false;
6693 r_refdef.scene.rtworldshadows = false;
6694 r_refdef.scene.rtdlight = false;
6695 r_refdef.scene.rtdlightshadows = false;
6696 r_refdef.lightmapintensity = 0;
6699 switch(vid.renderpath)
6701 case RENDERPATH_GL20:
6702 case RENDERPATH_D3D9:
6703 case RENDERPATH_D3D10:
6704 case RENDERPATH_D3D11:
6705 case RENDERPATH_SOFT:
6706 case RENDERPATH_GLES2:
6707 if(v_glslgamma.integer && !vid_gammatables_trivial)
6709 if(!r_texture_gammaramps || vid_gammatables_serial != r_texture_gammaramps_serial)
6711 // build GLSL gamma texture
6712 #define RAMPWIDTH 256
6713 unsigned short ramp[RAMPWIDTH * 3];
6714 unsigned char rampbgr[RAMPWIDTH][4];
6717 r_texture_gammaramps_serial = vid_gammatables_serial;
6719 VID_BuildGammaTables(&ramp[0], RAMPWIDTH);
6720 for(i = 0; i < RAMPWIDTH; ++i)
6722 rampbgr[i][0] = (unsigned char) (ramp[i + 2 * RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
6723 rampbgr[i][1] = (unsigned char) (ramp[i + RAMPWIDTH] * 255.0 / 65535.0 + 0.5);
6724 rampbgr[i][2] = (unsigned char) (ramp[i] * 255.0 / 65535.0 + 0.5);
6727 if (r_texture_gammaramps)
6729 R_UpdateTexture(r_texture_gammaramps, &rampbgr[0][0], 0, 0, 0, RAMPWIDTH, 1, 1);
6733 r_texture_gammaramps = R_LoadTexture2D(r_main_texturepool, "gammaramps", RAMPWIDTH, 1, &rampbgr[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, -1, NULL);
6739 // remove GLSL gamma texture
6742 case RENDERPATH_GL11:
6743 case RENDERPATH_GL13:
6744 case RENDERPATH_GLES1:
6749 static r_refdef_scene_type_t r_currentscenetype = RST_CLIENT;
6750 static r_refdef_scene_t r_scenes_store[ RST_COUNT ];
6756 void R_SelectScene( r_refdef_scene_type_t scenetype ) {
6757 if( scenetype != r_currentscenetype ) {
6758 // store the old scenetype
6759 r_scenes_store[ r_currentscenetype ] = r_refdef.scene;
6760 r_currentscenetype = scenetype;
6761 // move in the new scene
6762 r_refdef.scene = r_scenes_store[ r_currentscenetype ];
6771 r_refdef_scene_t * R_GetScenePointer( r_refdef_scene_type_t scenetype )
6773 // of course, we could also add a qboolean that provides a lock state and a ReleaseScenePointer function..
6774 if( scenetype == r_currentscenetype ) {
6775 return &r_refdef.scene;
6777 return &r_scenes_store[ scenetype ];
6781 static int R_SortEntities_Compare(const void *ap, const void *bp)
6783 const entity_render_t *a = *(const entity_render_t **)ap;
6784 const entity_render_t *b = *(const entity_render_t **)bp;
6787 if(a->model < b->model)
6789 if(a->model > b->model)
6793 // TODO possibly calculate the REAL skinnum here first using
6795 if(a->skinnum < b->skinnum)
6797 if(a->skinnum > b->skinnum)
6800 // everything we compared is equal
6803 static void R_SortEntities(void)
6805 // below or equal 2 ents, sorting never gains anything
6806 if(r_refdef.scene.numentities <= 2)
6809 qsort(r_refdef.scene.entities, r_refdef.scene.numentities, sizeof(*r_refdef.scene.entities), R_SortEntities_Compare);
6817 int dpsoftrast_test;
6818 extern cvar_t r_shadow_bouncegrid;
6819 void R_RenderView(void)
6821 matrix4x4_t originalmatrix = r_refdef.view.matrix, offsetmatrix;
6823 rtexture_t *depthtexture;
6824 rtexture_t *colortexture;
6826 dpsoftrast_test = r_test.integer;
6828 if (r_timereport_active)
6829 R_TimeReport("start");
6830 r_textureframe++; // used only by R_GetCurrentTexture
6831 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
6833 if(R_CompileShader_CheckStaticParms())
6836 if (!r_drawentities.integer)
6837 r_refdef.scene.numentities = 0;
6838 else if (r_sortentities.integer)
6841 R_AnimCache_ClearCache();
6842 R_FrameData_NewFrame();
6844 /* adjust for stereo display */
6845 if(R_Stereo_Active())
6847 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);
6848 Matrix4x4_Concat(&r_refdef.view.matrix, &originalmatrix, &offsetmatrix);
6851 if (r_refdef.view.isoverlay)
6853 // TODO: FIXME: move this into its own backend function maybe? [2/5/2008 Andreas]
6854 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
6855 GL_Clear(GL_DEPTH_BUFFER_BIT, NULL, 1.0f, 0);
6856 R_TimeReport("depthclear");
6858 r_refdef.view.showdebug = false;
6860 r_fb.water.enabled = false;
6861 r_fb.water.numwaterplanes = 0;
6863 R_RenderScene(0, NULL, NULL);
6865 r_refdef.view.matrix = originalmatrix;
6871 if (!r_refdef.scene.entities || r_refdef.view.width * r_refdef.view.height == 0 || !r_renderview.integer || cl_videoplaying/* || !r_refdef.scene.worldmodel*/)
6873 r_refdef.view.matrix = originalmatrix;
6877 r_refdef.view.colorscale = r_hdr_scenebrightness.value * r_hdr_irisadaptation_value.value;
6879 if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D)
6880 // in sRGB fallback, behave similar to true sRGB: convert this
6881 // value from linear to sRGB
6882 r_refdef.view.colorscale = Image_sRGBFloatFromLinearFloat(r_refdef.view.colorscale);
6884 R_RenderView_UpdateViewVectors();
6886 R_Shadow_UpdateWorldLightSelection();
6888 R_Bloom_StartFrame();
6889 R_Water_StartFrame();
6891 // now we probably have an fbo to render into
6893 depthtexture = r_fb.depthtexture;
6894 colortexture = r_fb.colortexture;
6897 if (r_timereport_active)
6898 R_TimeReport("viewsetup");
6900 R_ResetViewRendering3D(fbo, depthtexture, colortexture);
6902 if (r_refdef.view.clear || r_refdef.fogenabled || fbo)
6904 R_ClearScreen(r_refdef.fogenabled);
6905 if (r_timereport_active)
6906 R_TimeReport("viewclear");
6908 r_refdef.view.clear = true;
6910 r_refdef.view.showdebug = true;
6913 if (r_timereport_active)
6914 R_TimeReport("visibility");
6916 R_Shadow_UpdateBounceGridTexture();
6917 if (r_timereport_active && r_shadow_bouncegrid.integer)
6918 R_TimeReport("bouncegrid");
6920 r_fb.water.numwaterplanes = 0;
6921 if (r_fb.water.enabled)
6922 R_RenderWaterPlanes(fbo, depthtexture, colortexture);
6924 R_RenderScene(fbo, depthtexture, colortexture);
6925 r_fb.water.numwaterplanes = 0;
6927 R_BlendView(fbo, depthtexture, colortexture);
6928 if (r_timereport_active)
6929 R_TimeReport("blendview");
6931 GL_Scissor(0, 0, vid.width, vid.height);
6932 GL_ScissorTest(false);
6934 r_refdef.view.matrix = originalmatrix;
6939 void R_RenderWaterPlanes(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture)
6941 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawAddWaterPlanes)
6943 r_refdef.scene.worldmodel->DrawAddWaterPlanes(r_refdef.scene.worldentity);
6944 if (r_timereport_active)
6945 R_TimeReport("waterworld");
6948 // don't let sound skip if going slow
6949 if (r_refdef.scene.extraupdate)
6952 R_DrawModelsAddWaterPlanes();
6953 if (r_timereport_active)
6954 R_TimeReport("watermodels");
6956 if (r_fb.water.numwaterplanes)
6958 R_Water_ProcessPlanes(fbo, depthtexture, colortexture);
6959 if (r_timereport_active)
6960 R_TimeReport("waterscenes");
6964 extern cvar_t cl_locs_show;
6965 static void R_DrawLocs(void);
6966 static void R_DrawEntityBBoxes(void);
6967 static void R_DrawModelDecals(void);
6968 extern cvar_t cl_decals_newsystem;
6969 extern qboolean r_shadow_usingdeferredprepass;
6970 void R_RenderScene(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture)
6972 qboolean shadowmapping = false;
6974 if (r_timereport_active)
6975 R_TimeReport("beginscene");
6977 r_refdef.stats.renders++;
6981 // don't let sound skip if going slow
6982 if (r_refdef.scene.extraupdate)
6985 R_MeshQueue_BeginScene();
6989 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);
6991 if (r_timereport_active)
6992 R_TimeReport("skystartframe");
6994 if (cl.csqc_vidvars.drawworld)
6996 // don't let sound skip if going slow
6997 if (r_refdef.scene.extraupdate)
7000 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawSky)
7002 r_refdef.scene.worldmodel->DrawSky(r_refdef.scene.worldentity);
7003 if (r_timereport_active)
7004 R_TimeReport("worldsky");
7007 if (R_DrawBrushModelsSky() && r_timereport_active)
7008 R_TimeReport("bmodelsky");
7010 if (skyrendermasked && skyrenderlater)
7012 // we have to force off the water clipping plane while rendering sky
7013 R_SetupView(false, fbo, depthtexture, colortexture);
7015 R_SetupView(true, fbo, depthtexture, colortexture);
7016 if (r_timereport_active)
7017 R_TimeReport("sky");
7021 R_Shadow_PrepareLights(fbo, depthtexture, colortexture);
7022 if (r_shadows.integer > 0 && r_refdef.lightmapintensity > 0)
7023 R_Shadow_PrepareModelShadows();
7024 if (r_timereport_active)
7025 R_TimeReport("preparelights");
7027 if (R_Shadow_ShadowMappingEnabled())
7028 shadowmapping = true;
7030 if (r_shadow_usingdeferredprepass)
7031 R_Shadow_DrawPrepass();
7033 if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->DrawDepth)
7035 r_refdef.scene.worldmodel->DrawDepth(r_refdef.scene.worldentity);
7036 if (r_timereport_active)
7037 R_TimeReport("worlddepth");
7039 if (r_depthfirst.integer >= 2)
7041 R_DrawModelsDepth();
7042 if (r_timereport_active)
7043 R_TimeReport("modeldepth");
7046 if (r_shadows.integer >= 2 && shadowmapping && r_refdef.lightmapintensity > 0)
7048 R_ResetViewRendering3D(fbo, depthtexture, colortexture);
7049 R_DrawModelShadowMaps(fbo, depthtexture, colortexture);
7050 R_ResetViewRendering3D(fbo, depthtexture, colortexture);
7051 // don't let sound skip if going slow
7052 if (r_refdef.scene.extraupdate)
7056 if (cl.csqc_vidvars.drawworld && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->Draw)
7058 r_refdef.scene.worldmodel->Draw(r_refdef.scene.worldentity);
7059 if (r_timereport_active)
7060 R_TimeReport("world");
7063 // don't let sound skip if going slow
7064 if (r_refdef.scene.extraupdate)
7068 if (r_timereport_active)
7069 R_TimeReport("models");
7071 // don't let sound skip if going slow
7072 if (r_refdef.scene.extraupdate)
7075 if ((r_shadows.integer == 1 || (r_shadows.integer > 0 && !shadowmapping)) && !r_shadows_drawafterrtlighting.integer && r_refdef.lightmapintensity > 0)
7077 R_ResetViewRendering3D(fbo, depthtexture, colortexture);
7078 R_DrawModelShadows(fbo, depthtexture, colortexture);
7079 R_ResetViewRendering3D(fbo, depthtexture, colortexture);
7080 // don't let sound skip if going slow
7081 if (r_refdef.scene.extraupdate)
7085 if (!r_shadow_usingdeferredprepass)
7087 R_Shadow_DrawLights();
7088 if (r_timereport_active)
7089 R_TimeReport("rtlights");
7092 // don't let sound skip if going slow
7093 if (r_refdef.scene.extraupdate)
7096 if ((r_shadows.integer == 1 || (r_shadows.integer > 0 && !shadowmapping)) && r_shadows_drawafterrtlighting.integer && r_refdef.lightmapintensity > 0)
7098 R_ResetViewRendering3D(fbo, depthtexture, colortexture);
7099 R_DrawModelShadows(fbo, depthtexture, colortexture);
7100 R_ResetViewRendering3D(fbo, depthtexture, colortexture);
7101 // don't let sound skip if going slow
7102 if (r_refdef.scene.extraupdate)
7106 if (cl.csqc_vidvars.drawworld)
7108 if (cl_decals_newsystem.integer)
7110 R_DrawModelDecals();
7111 if (r_timereport_active)
7112 R_TimeReport("modeldecals");
7117 if (r_timereport_active)
7118 R_TimeReport("decals");
7122 if (r_timereport_active)
7123 R_TimeReport("particles");
7126 if (r_timereport_active)
7127 R_TimeReport("explosions");
7129 R_DrawLightningBeams();
7130 if (r_timereport_active)
7131 R_TimeReport("lightning");
7135 VM_CL_AddPolygonsToMeshQueue(CLVM_prog);
7137 if (r_refdef.view.showdebug)
7139 if (cl_locs_show.integer)
7142 if (r_timereport_active)
7143 R_TimeReport("showlocs");
7146 if (r_drawportals.integer)
7149 if (r_timereport_active)
7150 R_TimeReport("portals");
7153 if (r_showbboxes.value > 0)
7155 R_DrawEntityBBoxes();
7156 if (r_timereport_active)
7157 R_TimeReport("bboxes");
7161 if (r_transparent.integer)
7163 R_MeshQueue_RenderTransparent();
7164 if (r_timereport_active)
7165 R_TimeReport("drawtrans");
7168 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))
7170 r_refdef.scene.worldmodel->DrawDebug(r_refdef.scene.worldentity);
7171 if (r_timereport_active)
7172 R_TimeReport("worlddebug");
7173 R_DrawModelsDebug();
7174 if (r_timereport_active)
7175 R_TimeReport("modeldebug");
7178 if (cl.csqc_vidvars.drawworld)
7180 R_Shadow_DrawCoronas();
7181 if (r_timereport_active)
7182 R_TimeReport("coronas");
7187 GL_DepthTest(false);
7188 qglPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
7189 GL_Color(1, 1, 1, 1);
7190 qglBegin(GL_POLYGON);
7191 qglVertex3f(r_refdef.view.frustumcorner[0][0], r_refdef.view.frustumcorner[0][1], r_refdef.view.frustumcorner[0][2]);
7192 qglVertex3f(r_refdef.view.frustumcorner[1][0], r_refdef.view.frustumcorner[1][1], r_refdef.view.frustumcorner[1][2]);
7193 qglVertex3f(r_refdef.view.frustumcorner[3][0], r_refdef.view.frustumcorner[3][1], r_refdef.view.frustumcorner[3][2]);
7194 qglVertex3f(r_refdef.view.frustumcorner[2][0], r_refdef.view.frustumcorner[2][1], r_refdef.view.frustumcorner[2][2]);
7196 qglBegin(GL_POLYGON);
7197 qglVertex3f(r_refdef.view.frustumcorner[0][0] + 1000 * r_refdef.view.forward[0], r_refdef.view.frustumcorner[0][1] + 1000 * r_refdef.view.forward[1], r_refdef.view.frustumcorner[0][2] + 1000 * r_refdef.view.forward[2]);
7198 qglVertex3f(r_refdef.view.frustumcorner[1][0] + 1000 * r_refdef.view.forward[0], r_refdef.view.frustumcorner[1][1] + 1000 * r_refdef.view.forward[1], r_refdef.view.frustumcorner[1][2] + 1000 * r_refdef.view.forward[2]);
7199 qglVertex3f(r_refdef.view.frustumcorner[3][0] + 1000 * r_refdef.view.forward[0], r_refdef.view.frustumcorner[3][1] + 1000 * r_refdef.view.forward[1], r_refdef.view.frustumcorner[3][2] + 1000 * r_refdef.view.forward[2]);
7200 qglVertex3f(r_refdef.view.frustumcorner[2][0] + 1000 * r_refdef.view.forward[0], r_refdef.view.frustumcorner[2][1] + 1000 * r_refdef.view.forward[1], r_refdef.view.frustumcorner[2][2] + 1000 * r_refdef.view.forward[2]);
7202 qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
7206 // don't let sound skip if going slow
7207 if (r_refdef.scene.extraupdate)
7211 static const unsigned short bboxelements[36] =
7221 static void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
7224 float *v, *c, f1, f2, vertex3f[8*3], color4f[8*4];
7226 RSurf_ActiveWorldEntity();
7228 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
7229 GL_DepthMask(false);
7230 GL_DepthRange(0, 1);
7231 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
7232 // R_Mesh_ResetTextureState();
7234 vertex3f[ 0] = mins[0];vertex3f[ 1] = mins[1];vertex3f[ 2] = mins[2]; //
7235 vertex3f[ 3] = maxs[0];vertex3f[ 4] = mins[1];vertex3f[ 5] = mins[2];
7236 vertex3f[ 6] = mins[0];vertex3f[ 7] = maxs[1];vertex3f[ 8] = mins[2];
7237 vertex3f[ 9] = maxs[0];vertex3f[10] = maxs[1];vertex3f[11] = mins[2];
7238 vertex3f[12] = mins[0];vertex3f[13] = mins[1];vertex3f[14] = maxs[2];
7239 vertex3f[15] = maxs[0];vertex3f[16] = mins[1];vertex3f[17] = maxs[2];
7240 vertex3f[18] = mins[0];vertex3f[19] = maxs[1];vertex3f[20] = maxs[2];
7241 vertex3f[21] = maxs[0];vertex3f[22] = maxs[1];vertex3f[23] = maxs[2];
7242 R_FillColors(color4f, 8, cr, cg, cb, ca);
7243 if (r_refdef.fogenabled)
7245 for (i = 0, v = vertex3f, c = color4f;i < 8;i++, v += 3, c += 4)
7247 f1 = RSurf_FogVertex(v);
7249 c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
7250 c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
7251 c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
7254 R_Mesh_PrepareVertices_Generic_Arrays(8, vertex3f, color4f, NULL);
7255 R_Mesh_ResetTextureState();
7256 R_SetupShader_Generic_NoTexture(false, false);
7257 R_Mesh_Draw(0, 8, 0, 12, NULL, NULL, 0, bboxelements, NULL, 0);
7260 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
7262 prvm_prog_t *prog = SVVM_prog;
7265 prvm_edict_t *edict;
7267 // this function draws bounding boxes of server entities
7271 GL_CullFace(GL_NONE);
7272 R_SetupShader_Generic_NoTexture(false, false);
7274 for (i = 0;i < numsurfaces;i++)
7276 edict = PRVM_EDICT_NUM(surfacelist[i]);
7277 switch ((int)PRVM_serveredictfloat(edict, solid))
7279 case SOLID_NOT: Vector4Set(color, 1, 1, 1, 0.05);break;
7280 case SOLID_TRIGGER: Vector4Set(color, 1, 0, 1, 0.10);break;
7281 case SOLID_BBOX: Vector4Set(color, 0, 1, 0, 0.10);break;
7282 case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
7283 case SOLID_BSP: Vector4Set(color, 0, 0, 1, 0.05);break;
7284 case SOLID_CORPSE: Vector4Set(color, 1, 0.5, 0, 0.05);break;
7285 default: Vector4Set(color, 0, 0, 0, 0.50);break;
7287 color[3] *= r_showbboxes.value;
7288 color[3] = bound(0, color[3], 1);
7289 GL_DepthTest(!r_showdisabledepthtest.integer);
7290 GL_CullFace(r_refdef.view.cullface_front);
7291 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
7295 static void R_DrawEntityBBoxes(void)
7298 prvm_edict_t *edict;
7300 prvm_prog_t *prog = SVVM_prog;
7302 // this function draws bounding boxes of server entities
7306 for (i = 0;i < prog->num_edicts;i++)
7308 edict = PRVM_EDICT_NUM(i);
7309 if (edict->priv.server->free)
7311 // exclude the following for now, as they don't live in world coordinate space and can't be solid:
7312 if(PRVM_serveredictedict(edict, tag_entity) != 0)
7314 if(PRVM_serveredictedict(edict, viewmodelforclient) != 0)
7316 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
7317 R_MeshQueue_AddTransparent(MESHQUEUE_SORT_DISTANCE, center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)NULL);
7321 static const int nomodelelement3i[24] =
7333 static const unsigned short nomodelelement3s[24] =
7345 static const float nomodelvertex3f[6*3] =
7355 static const float nomodelcolor4f[6*4] =
7357 0.0f, 0.0f, 0.5f, 1.0f,
7358 0.0f, 0.0f, 0.5f, 1.0f,
7359 0.0f, 0.5f, 0.0f, 1.0f,
7360 0.0f, 0.5f, 0.0f, 1.0f,
7361 0.5f, 0.0f, 0.0f, 1.0f,
7362 0.5f, 0.0f, 0.0f, 1.0f
7365 static void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
7371 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);
7373 // this is only called once per entity so numsurfaces is always 1, and
7374 // surfacelist is always {0}, so this code does not handle batches
7376 if (rsurface.ent_flags & RENDER_ADDITIVE)
7378 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
7379 GL_DepthMask(false);
7381 else if (rsurface.colormod[3] < 1)
7383 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
7384 GL_DepthMask(false);
7388 GL_BlendFunc(GL_ONE, GL_ZERO);
7391 GL_DepthRange(0, (rsurface.ent_flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
7392 GL_PolygonOffset(rsurface.basepolygonfactor, rsurface.basepolygonoffset);
7393 GL_DepthTest(!(rsurface.ent_flags & RENDER_NODEPTHTEST));
7394 GL_CullFace((rsurface.ent_flags & RENDER_DOUBLESIDED) ? GL_NONE : r_refdef.view.cullface_back);
7395 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
7396 for (i = 0, c = color4f;i < 6;i++, c += 4)
7398 c[0] *= rsurface.colormod[0];
7399 c[1] *= rsurface.colormod[1];
7400 c[2] *= rsurface.colormod[2];
7401 c[3] *= rsurface.colormod[3];
7403 if (r_refdef.fogenabled)
7405 for (i = 0, c = color4f;i < 6;i++, c += 4)
7407 f1 = RSurf_FogVertex(nomodelvertex3f + 3*i);
7409 c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
7410 c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
7411 c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
7414 // R_Mesh_ResetTextureState();
7415 R_SetupShader_Generic_NoTexture(false, false);
7416 R_Mesh_PrepareVertices_Generic_Arrays(6, nomodelvertex3f, color4f, NULL);
7417 R_Mesh_Draw(0, 6, 0, 8, nomodelelement3i, NULL, 0, nomodelelement3s, NULL, 0);
7420 void R_DrawNoModel(entity_render_t *ent)
7423 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
7424 if ((ent->flags & RENDER_ADDITIVE) || (ent->alpha < 1))
7425 R_MeshQueue_AddTransparent((ent->flags & RENDER_NODEPTHTEST) ? MESHQUEUE_SORT_HUD : MESHQUEUE_SORT_DISTANCE, org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
7427 R_DrawNoModel_TransparentCallback(ent, rsurface.rtlight, 0, NULL);
7430 void R_CalcBeam_Vertex3f (float *vert, const vec3_t org1, const vec3_t org2, float width)
7432 vec3_t right1, right2, diff, normal;
7434 VectorSubtract (org2, org1, normal);
7436 // calculate 'right' vector for start
7437 VectorSubtract (r_refdef.view.origin, org1, diff);
7438 CrossProduct (normal, diff, right1);
7439 VectorNormalize (right1);
7441 // calculate 'right' vector for end
7442 VectorSubtract (r_refdef.view.origin, org2, diff);
7443 CrossProduct (normal, diff, right2);
7444 VectorNormalize (right2);
7446 vert[ 0] = org1[0] + width * right1[0];
7447 vert[ 1] = org1[1] + width * right1[1];
7448 vert[ 2] = org1[2] + width * right1[2];
7449 vert[ 3] = org1[0] - width * right1[0];
7450 vert[ 4] = org1[1] - width * right1[1];
7451 vert[ 5] = org1[2] - width * right1[2];
7452 vert[ 6] = org2[0] - width * right2[0];
7453 vert[ 7] = org2[1] - width * right2[1];
7454 vert[ 8] = org2[2] - width * right2[2];
7455 vert[ 9] = org2[0] + width * right2[0];
7456 vert[10] = org2[1] + width * right2[1];
7457 vert[11] = org2[2] + width * right2[2];
7460 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)
7462 vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
7463 vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
7464 vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
7465 vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
7466 vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
7467 vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
7468 vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
7469 vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
7470 vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
7471 vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
7472 vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
7473 vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
7476 static int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
7481 VectorSet(v, x, y, z);
7482 for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
7483 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
7485 if (i == mesh->numvertices)
7487 if (mesh->numvertices < mesh->maxvertices)
7489 VectorCopy(v, vertex3f);
7490 mesh->numvertices++;
7492 return mesh->numvertices;
7498 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
7502 element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
7503 element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
7504 e = mesh->element3i + mesh->numtriangles * 3;
7505 for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
7507 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
7508 if (mesh->numtriangles < mesh->maxtriangles)
7513 mesh->numtriangles++;
7515 element[1] = element[2];
7519 static void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
7523 element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
7524 element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
7525 e = mesh->element3i + mesh->numtriangles * 3;
7526 for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
7528 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
7529 if (mesh->numtriangles < mesh->maxtriangles)
7534 mesh->numtriangles++;
7536 element[1] = element[2];
7540 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
7541 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
7543 int planenum, planenum2;
7546 mplane_t *plane, *plane2;
7548 double temppoints[2][256*3];
7549 // figure out how large a bounding box we need to properly compute this brush
7551 for (w = 0;w < numplanes;w++)
7552 maxdist = max(maxdist, fabs(planes[w].dist));
7553 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
7554 maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
7555 for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
7559 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
7560 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
7562 if (planenum2 == planenum)
7564 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);
7567 if (tempnumpoints < 3)
7569 // generate elements forming a triangle fan for this polygon
7570 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
7574 static void R_Texture_AddLayer(texture_t *t, qboolean depthmask, int blendfunc1, int blendfunc2, texturelayertype_t type, rtexture_t *texture, const matrix4x4_t *matrix, float r, float g, float b, float a)
7576 texturelayer_t *layer;
7577 layer = t->currentlayers + t->currentnumlayers++;
7579 layer->depthmask = depthmask;
7580 layer->blendfunc1 = blendfunc1;
7581 layer->blendfunc2 = blendfunc2;
7582 layer->texture = texture;
7583 layer->texmatrix = *matrix;
7584 layer->color[0] = r;
7585 layer->color[1] = g;
7586 layer->color[2] = b;
7587 layer->color[3] = a;
7590 static qboolean R_TestQ3WaveFunc(q3wavefunc_t func, const float *parms)
7592 if(parms[0] == 0 && parms[1] == 0)
7594 if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
7595 if(rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)] == 0)
7600 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
7603 index = parms[2] + rsurface.shadertime * parms[3];
7604 index -= floor(index);
7605 switch (func & ((1 << Q3WAVEFUNC_USER_SHIFT) - 1))
7608 case Q3WAVEFUNC_NONE:
7609 case Q3WAVEFUNC_NOISE:
7610 case Q3WAVEFUNC_COUNT:
7613 case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
7614 case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
7615 case Q3WAVEFUNC_SAWTOOTH: f = index;break;
7616 case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
7617 case Q3WAVEFUNC_TRIANGLE:
7619 f = index - floor(index);
7632 f = parms[0] + parms[1] * f;
7633 if(func >> Q3WAVEFUNC_USER_SHIFT) // assumes rsurface to be set!
7634 f *= rsurface.userwavefunc_param[bound(0, (func >> Q3WAVEFUNC_USER_SHIFT) - 1, Q3WAVEFUNC_USER_COUNT - 1)];
7638 static void R_tcMod_ApplyToMatrix(matrix4x4_t *texmatrix, q3shaderinfo_layer_tcmod_t *tcmod, int currentmaterialflags)
7644 matrix4x4_t matrix, temp;
7645 switch(tcmod->tcmod)
7649 if (currentmaterialflags & MATERIALFLAG_WATERSCROLL)
7650 matrix = r_waterscrollmatrix;
7652 matrix = identitymatrix;
7654 case Q3TCMOD_ENTITYTRANSLATE:
7655 // this is used in Q3 to allow the gamecode to control texcoord
7656 // scrolling on the entity, which is not supported in darkplaces yet.
7657 Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
7659 case Q3TCMOD_ROTATE:
7660 f = tcmod->parms[0] * rsurface.shadertime;
7661 Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
7662 Matrix4x4_ConcatRotate(&matrix, (f / 360 - floor(f / 360)) * 360, 0, 0, 1);
7663 Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
7666 Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
7668 case Q3TCMOD_SCROLL:
7669 // extra care is needed because of precision breakdown with large values of time
7670 offsetd[0] = tcmod->parms[0] * rsurface.shadertime;
7671 offsetd[1] = tcmod->parms[1] * rsurface.shadertime;
7672 Matrix4x4_CreateTranslate(&matrix, offsetd[0] - floor(offsetd[0]), offsetd[1] - floor(offsetd[1]), 0);
7674 case Q3TCMOD_PAGE: // poor man's animmap (to store animations into a single file, useful for HTTP downloaded textures)
7675 w = (int) tcmod->parms[0];
7676 h = (int) tcmod->parms[1];
7677 f = rsurface.shadertime / (tcmod->parms[2] * w * h);
7679 idx = (int) floor(f * w * h);
7680 Matrix4x4_CreateTranslate(&matrix, (idx % w) / tcmod->parms[0], (idx / w) / tcmod->parms[1], 0);
7682 case Q3TCMOD_STRETCH:
7683 f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
7684 Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
7686 case Q3TCMOD_TRANSFORM:
7687 VectorSet(tcmat + 0, tcmod->parms[0], tcmod->parms[1], 0);
7688 VectorSet(tcmat + 3, tcmod->parms[2], tcmod->parms[3], 0);
7689 VectorSet(tcmat + 6, 0 , 0 , 1);
7690 VectorSet(tcmat + 9, tcmod->parms[4], tcmod->parms[5], 0);
7691 Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
7693 case Q3TCMOD_TURBULENT:
7694 // this is handled in the RSurf_PrepareVertices function
7695 matrix = identitymatrix;
7699 Matrix4x4_Concat(texmatrix, &matrix, &temp);
7702 static void R_LoadQWSkin(r_qwskincache_t *cache, const char *skinname)
7704 int textureflags = (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_PICMIP;
7705 char name[MAX_QPATH];
7706 skinframe_t *skinframe;
7707 unsigned char pixels[296*194];
7708 strlcpy(cache->name, skinname, sizeof(cache->name));
7709 dpsnprintf(name, sizeof(name), "skins/%s.pcx", cache->name);
7710 if (developer_loading.integer)
7711 Con_Printf("loading %s\n", name);
7712 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
7713 if (!skinframe || !skinframe->base)
7716 fs_offset_t filesize;
7718 f = FS_LoadFile(name, tempmempool, true, &filesize);
7721 if (LoadPCX_QWSkin(f, (int)filesize, pixels, 296, 194))
7722 skinframe = R_SkinFrame_LoadInternalQuake(name, textureflags, true, r_fullbrights.integer, pixels, image_width, image_height);
7726 cache->skinframe = skinframe;
7729 texture_t *R_GetCurrentTexture(texture_t *t)
7732 const entity_render_t *ent = rsurface.entity;
7733 dp_model_t *model = ent->model;
7734 q3shaderinfo_layer_tcmod_t *tcmod;
7736 if (t->update_lastrenderframe == r_textureframe && t->update_lastrenderentity == (void *)ent)
7737 return t->currentframe;
7738 t->update_lastrenderframe = r_textureframe;
7739 t->update_lastrenderentity = (void *)ent;
7741 if(ent && ent->entitynumber >= MAX_EDICTS && ent->entitynumber < 2 * MAX_EDICTS)
7742 t->camera_entity = ent->entitynumber;
7744 t->camera_entity = 0;
7746 // switch to an alternate material if this is a q1bsp animated material
7748 texture_t *texture = t;
7749 int s = rsurface.ent_skinnum;
7750 if ((unsigned int)s >= (unsigned int)model->numskins)
7752 if (model->skinscenes)
7754 if (model->skinscenes[s].framecount > 1)
7755 s = model->skinscenes[s].firstframe + (unsigned int) (rsurface.shadertime * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
7757 s = model->skinscenes[s].firstframe;
7760 t = t + s * model->num_surfaces;
7763 // use an alternate animation if the entity's frame is not 0,
7764 // and only if the texture has an alternate animation
7765 if (rsurface.ent_alttextures && t->anim_total[1])
7766 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[1]) : 0];
7768 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(rsurface.shadertime * 5.0f) % t->anim_total[0]) : 0];
7770 texture->currentframe = t;
7773 // update currentskinframe to be a qw skin or animation frame
7774 if (rsurface.ent_qwskin >= 0)
7776 i = rsurface.ent_qwskin;
7777 if (!r_qwskincache || r_qwskincache_size != cl.maxclients)
7779 r_qwskincache_size = cl.maxclients;
7781 Mem_Free(r_qwskincache);
7782 r_qwskincache = (r_qwskincache_t *)Mem_Alloc(r_main_mempool, sizeof(*r_qwskincache) * r_qwskincache_size);
7784 if (strcmp(r_qwskincache[i].name, cl.scores[i].qw_skin))
7785 R_LoadQWSkin(&r_qwskincache[i], cl.scores[i].qw_skin);
7786 t->currentskinframe = r_qwskincache[i].skinframe;
7787 if (t->currentskinframe == NULL)
7788 t->currentskinframe = t->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->skinframerate, t->numskinframes)];
7790 else if (t->numskinframes >= 2)
7791 t->currentskinframe = t->skinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->skinframerate, t->numskinframes)];
7792 if (t->backgroundnumskinframes >= 2)
7793 t->backgroundcurrentskinframe = t->backgroundskinframes[LoopingFrameNumberFromDouble(rsurface.shadertime * t->backgroundskinframerate, t->backgroundnumskinframes)];
7795 t->currentmaterialflags = t->basematerialflags;
7796 t->currentalpha = rsurface.colormod[3];
7797 if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_novis.integer || r_trippy.integer))
7798 t->currentalpha *= r_wateralpha.value;
7799 if(t->basematerialflags & MATERIALFLAG_WATERSHADER && r_fb.water.enabled && !r_refdef.view.isoverlay)
7800 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW; // we apply wateralpha later
7801 if(!r_fb.water.enabled || r_refdef.view.isoverlay)
7802 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA);
7803 if (!(rsurface.ent_flags & RENDER_LIGHT))
7804 t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
7805 else if (FAKELIGHT_ENABLED)
7807 // no modellight if using fakelight for the map
7809 else if (rsurface.modeltexcoordlightmap2f == NULL && !(t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
7811 // pick a model lighting mode
7812 if (VectorLength2(rsurface.modellight_diffuse) >= (1.0f / 256.0f))
7813 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT | MATERIALFLAG_MODELLIGHT_DIRECTIONAL;
7815 t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
7817 if (rsurface.ent_flags & RENDER_ADDITIVE)
7818 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
7819 else if (t->currentalpha < 1)
7820 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
7821 // LordHavoc: prevent bugs where code checks add or alpha at higher priority than customblend by clearing these flags
7822 if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
7823 t->currentmaterialflags &= ~(MATERIALFLAG_ADD | MATERIALFLAG_ALPHA);
7824 if (rsurface.ent_flags & RENDER_DOUBLESIDED)
7825 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
7826 if (rsurface.ent_flags & (RENDER_NODEPTHTEST | RENDER_VIEWMODEL))
7827 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
7828 if (t->backgroundnumskinframes)
7829 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
7830 if (t->currentmaterialflags & MATERIALFLAG_BLENDED)
7832 if (t->currentmaterialflags & (MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA))
7833 t->currentmaterialflags &= ~MATERIALFLAG_BLENDED;
7836 t->currentmaterialflags &= ~(MATERIALFLAG_REFRACTION | MATERIALFLAG_WATERSHADER | MATERIALFLAG_CAMERA);
7837 if (vid.allowalphatocoverage && r_transparent_alphatocoverage.integer >= 2 && ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA | MATERIALFLAG_ADD | MATERIALFLAG_CUSTOMBLEND)) == (MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)))
7839 // promote alphablend to alphatocoverage (a type of alphatest) if antialiasing is on
7840 t->currentmaterialflags = (t->currentmaterialflags & ~(MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHA)) | MATERIALFLAG_ALPHATEST;
7842 if ((t->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST)) == MATERIALFLAG_BLENDED && r_transparentdepthmasking.integer && !(t->basematerialflags & MATERIALFLAG_BLENDED))
7843 t->currentmaterialflags |= MATERIALFLAG_TRANSDEPTH;
7845 // there is no tcmod
7846 if (t->currentmaterialflags & MATERIALFLAG_WATERSCROLL)
7848 t->currenttexmatrix = r_waterscrollmatrix;
7849 t->currentbackgroundtexmatrix = r_waterscrollmatrix;
7851 else if (!(t->currentmaterialflags & MATERIALFLAG_CUSTOMSURFACE))
7853 Matrix4x4_CreateIdentity(&t->currenttexmatrix);
7854 Matrix4x4_CreateIdentity(&t->currentbackgroundtexmatrix);
7857 for (i = 0, tcmod = t->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
7858 R_tcMod_ApplyToMatrix(&t->currenttexmatrix, tcmod, t->currentmaterialflags);
7859 for (i = 0, tcmod = t->backgroundtcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
7860 R_tcMod_ApplyToMatrix(&t->currentbackgroundtexmatrix, tcmod, t->currentmaterialflags);
7862 t->colormapping = VectorLength2(rsurface.colormap_pantscolor) + VectorLength2(rsurface.colormap_shirtcolor) >= (1.0f / 1048576.0f);
7863 if (t->currentskinframe->qpixels)
7864 R_SkinFrame_GenerateTexturesFromQPixels(t->currentskinframe, t->colormapping);
7865 t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
7866 if (!t->basetexture)
7867 t->basetexture = r_texture_notexture;
7868 t->pantstexture = t->colormapping ? t->currentskinframe->pants : NULL;
7869 t->shirttexture = t->colormapping ? t->currentskinframe->shirt : NULL;
7870 t->nmaptexture = t->currentskinframe->nmap;
7871 if (!t->nmaptexture)
7872 t->nmaptexture = r_texture_blanknormalmap;
7873 t->glosstexture = r_texture_black;
7874 t->glowtexture = t->currentskinframe->glow;
7875 t->fogtexture = t->currentskinframe->fog;
7876 t->reflectmasktexture = t->currentskinframe->reflect;
7877 if (t->backgroundnumskinframes)
7879 t->backgroundbasetexture = (!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base;
7880 t->backgroundnmaptexture = t->backgroundcurrentskinframe->nmap;
7881 t->backgroundglosstexture = r_texture_black;
7882 t->backgroundglowtexture = t->backgroundcurrentskinframe->glow;
7883 if (!t->backgroundnmaptexture)
7884 t->backgroundnmaptexture = r_texture_blanknormalmap;
7888 t->backgroundbasetexture = r_texture_white;
7889 t->backgroundnmaptexture = r_texture_blanknormalmap;
7890 t->backgroundglosstexture = r_texture_black;
7891 t->backgroundglowtexture = NULL;
7893 t->specularpower = r_shadow_glossexponent.value;
7894 // TODO: store reference values for these in the texture?
7895 t->specularscale = 0;
7896 if (r_shadow_gloss.integer > 0)
7898 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
7900 if (r_shadow_glossintensity.value > 0)
7902 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
7903 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
7904 t->specularscale = r_shadow_glossintensity.value;
7907 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
7909 t->glosstexture = r_texture_white;
7910 t->backgroundglosstexture = r_texture_white;
7911 t->specularscale = r_shadow_gloss2intensity.value;
7912 t->specularpower = r_shadow_gloss2exponent.value;
7915 t->specularscale *= t->specularscalemod;
7916 t->specularpower *= t->specularpowermod;
7917 t->rtlightambient = 0;
7919 // lightmaps mode looks bad with dlights using actual texturing, so turn
7920 // off the colormap and glossmap, but leave the normalmap on as it still
7921 // accurately represents the shading involved
7922 if (gl_lightmaps.integer)
7924 t->basetexture = r_texture_grey128;
7925 t->pantstexture = r_texture_black;
7926 t->shirttexture = r_texture_black;
7927 t->nmaptexture = r_texture_blanknormalmap;
7928 t->glosstexture = r_texture_black;
7929 t->glowtexture = NULL;
7930 t->fogtexture = NULL;
7931 t->reflectmasktexture = NULL;
7932 t->backgroundbasetexture = NULL;
7933 t->backgroundnmaptexture = r_texture_blanknormalmap;
7934 t->backgroundglosstexture = r_texture_black;
7935 t->backgroundglowtexture = NULL;
7936 t->specularscale = 0;
7937 t->currentmaterialflags = MATERIALFLAG_WALL | (t->currentmaterialflags & (MATERIALFLAG_NOCULLFACE | MATERIALFLAG_MODELLIGHT | MATERIALFLAG_MODELLIGHT_DIRECTIONAL | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SHORTDEPTHRANGE));
7940 Vector4Set(t->lightmapcolor, rsurface.colormod[0], rsurface.colormod[1], rsurface.colormod[2], t->currentalpha);
7941 VectorClear(t->dlightcolor);
7942 t->currentnumlayers = 0;
7943 if (t->currentmaterialflags & MATERIALFLAG_WALL)
7945 int blendfunc1, blendfunc2;
7947 if (t->currentmaterialflags & MATERIALFLAG_ADD)
7949 blendfunc1 = GL_SRC_ALPHA;
7950 blendfunc2 = GL_ONE;
7952 else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
7954 blendfunc1 = GL_SRC_ALPHA;
7955 blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
7957 else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
7959 blendfunc1 = t->customblendfunc[0];
7960 blendfunc2 = t->customblendfunc[1];
7964 blendfunc1 = GL_ONE;
7965 blendfunc2 = GL_ZERO;
7967 // don't colormod evilblend textures
7968 if(!R_BlendFuncFlags(blendfunc1, blendfunc2) & BLENDFUNC_ALLOWS_COLORMOD)
7969 VectorSet(t->lightmapcolor, 1, 1, 1);
7970 depthmask = !(t->currentmaterialflags & MATERIALFLAG_BLENDED);
7971 if (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
7973 // fullbright is not affected by r_refdef.lightmapintensity
7974 R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_TEXTURE, t->basetexture, &t->currenttexmatrix, t->lightmapcolor[0], t->lightmapcolor[1], t->lightmapcolor[2], t->lightmapcolor[3]);
7975 if (VectorLength2(rsurface.colormap_pantscolor) >= (1.0f / 1048576.0f) && t->pantstexture)
7976 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->pantstexture, &t->currenttexmatrix, rsurface.colormap_pantscolor[0] * t->lightmapcolor[0], rsurface.colormap_pantscolor[1] * t->lightmapcolor[1], rsurface.colormap_pantscolor[2] * t->lightmapcolor[2], t->lightmapcolor[3]);
7977 if (VectorLength2(rsurface.colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->shirttexture)
7978 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->shirttexture, &t->currenttexmatrix, rsurface.colormap_shirtcolor[0] * t->lightmapcolor[0], rsurface.colormap_shirtcolor[1] * t->lightmapcolor[1], rsurface.colormap_shirtcolor[2] * t->lightmapcolor[2], t->lightmapcolor[3]);
7982 vec3_t ambientcolor;
7984 // set the color tint used for lights affecting this surface
7985 VectorSet(t->dlightcolor, t->lightmapcolor[0] * t->lightmapcolor[3], t->lightmapcolor[1] * t->lightmapcolor[3], t->lightmapcolor[2] * t->lightmapcolor[3]);
7987 // q3bsp has no lightmap updates, so the lightstylevalue that
7988 // would normally be baked into the lightmap must be
7989 // applied to the color
7990 // FIXME: r_glsl 1 rendering doesn't support overbright lightstyles with this (the default light style is not overbright)
7991 if (model->type == mod_brushq3)
7992 colorscale *= r_refdef.scene.rtlightstylevalue[0];
7993 colorscale *= r_refdef.lightmapintensity;
7994 VectorScale(t->lightmapcolor, r_refdef.scene.ambient, ambientcolor);
7995 VectorScale(t->lightmapcolor, colorscale, t->lightmapcolor);
7996 // basic lit geometry
7997 R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LITTEXTURE, t->basetexture, &t->currenttexmatrix, t->lightmapcolor[0], t->lightmapcolor[1], t->lightmapcolor[2], t->lightmapcolor[3]);
7998 // add pants/shirt if needed
7999 if (VectorLength2(rsurface.colormap_pantscolor) >= (1.0f / 1048576.0f) && t->pantstexture)
8000 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->pantstexture, &t->currenttexmatrix, rsurface.colormap_pantscolor[0] * t->lightmapcolor[0], rsurface.colormap_pantscolor[1] * t->lightmapcolor[1], rsurface.colormap_pantscolor[2] * t->lightmapcolor[2], t->lightmapcolor[3]);
8001 if (VectorLength2(rsurface.colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->shirttexture)
8002 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->shirttexture, &t->currenttexmatrix, rsurface.colormap_shirtcolor[0] * t->lightmapcolor[0], rsurface.colormap_shirtcolor[1] * t->lightmapcolor[1], rsurface.colormap_shirtcolor[2] * t->lightmapcolor[2], t->lightmapcolor[3]);
8003 // now add ambient passes if needed
8004 if (VectorLength2(ambientcolor) >= (1.0f/1048576.0f))
8006 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->basetexture, &t->currenttexmatrix, ambientcolor[0], ambientcolor[1], ambientcolor[2], t->lightmapcolor[3]);
8007 if (VectorLength2(rsurface.colormap_pantscolor) >= (1.0f / 1048576.0f) && t->pantstexture)
8008 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->pantstexture, &t->currenttexmatrix, rsurface.colormap_pantscolor[0] * ambientcolor[0], rsurface.colormap_pantscolor[1] * ambientcolor[1], rsurface.colormap_pantscolor[2] * ambientcolor[2], t->lightmapcolor[3]);
8009 if (VectorLength2(rsurface.colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->shirttexture)
8010 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->shirttexture, &t->currenttexmatrix, rsurface.colormap_shirtcolor[0] * ambientcolor[0], rsurface.colormap_shirtcolor[1] * ambientcolor[1], rsurface.colormap_shirtcolor[2] * ambientcolor[2], t->lightmapcolor[3]);
8013 if (t->glowtexture != NULL && !gl_lightmaps.integer)
8014 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->glowtexture, &t->currenttexmatrix, rsurface.glowmod[0], rsurface.glowmod[1], rsurface.glowmod[2], t->lightmapcolor[3]);
8015 if (r_refdef.fogenabled && !(t->currentmaterialflags & MATERIALFLAG_ADD))
8017 // if this is opaque use alpha blend which will darken the earlier
8020 // if this is an alpha blended material, all the earlier passes
8021 // were darkened by fog already, so we only need to add the fog
8022 // color ontop through the fog mask texture
8024 // if this is an additive blended material, all the earlier passes
8025 // were darkened by fog already, and we should not add fog color
8026 // (because the background was not darkened, there is no fog color
8027 // that was lost behind it).
8028 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, (t->currentmaterialflags & MATERIALFLAG_BLENDED) ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, TEXTURELAYERTYPE_FOG, t->fogtexture, &t->currenttexmatrix, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], t->lightmapcolor[3]);
8032 return t->currentframe;
8035 rsurfacestate_t rsurface;
8037 void RSurf_ActiveWorldEntity(void)
8039 dp_model_t *model = r_refdef.scene.worldmodel;
8040 //if (rsurface.entity == r_refdef.scene.worldentity)
8042 rsurface.entity = r_refdef.scene.worldentity;
8043 rsurface.skeleton = NULL;
8044 memset(rsurface.userwavefunc_param, 0, sizeof(rsurface.userwavefunc_param));
8045 rsurface.ent_skinnum = 0;
8046 rsurface.ent_qwskin = -1;
8047 rsurface.ent_flags = r_refdef.scene.worldentity->flags;
8048 rsurface.shadertime = r_refdef.scene.time;
8049 rsurface.matrix = identitymatrix;
8050 rsurface.inversematrix = identitymatrix;
8051 rsurface.matrixscale = 1;
8052 rsurface.inversematrixscale = 1;
8053 R_EntityMatrix(&identitymatrix);
8054 VectorCopy(r_refdef.view.origin, rsurface.localvieworigin);
8055 Vector4Copy(r_refdef.fogplane, rsurface.fogplane);
8056 rsurface.fograngerecip = r_refdef.fograngerecip;
8057 rsurface.fogheightfade = r_refdef.fogheightfade;
8058 rsurface.fogplaneviewdist = r_refdef.fogplaneviewdist;
8059 rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
8060 VectorSet(rsurface.modellight_ambient, 0, 0, 0);
8061 VectorSet(rsurface.modellight_diffuse, 0, 0, 0);
8062 VectorSet(rsurface.modellight_lightdir, 0, 0, 1);
8063 VectorSet(rsurface.colormap_pantscolor, 0, 0, 0);
8064 VectorSet(rsurface.colormap_shirtcolor, 0, 0, 0);
8065 VectorSet(rsurface.colormod, r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale);
8066 rsurface.colormod[3] = 1;
8067 VectorSet(rsurface.glowmod, r_refdef.view.colorscale * r_hdr_glowintensity.value, r_refdef.view.colorscale * r_hdr_glowintensity.value, r_refdef.view.colorscale * r_hdr_glowintensity.value);
8068 memset(rsurface.frameblend, 0, sizeof(rsurface.frameblend));
8069 rsurface.frameblend[0].lerp = 1;
8070 rsurface.ent_alttextures = false;
8071 rsurface.basepolygonfactor = r_refdef.polygonfactor;
8072 rsurface.basepolygonoffset = r_refdef.polygonoffset;
8073 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
8074 rsurface.modelvertex3f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8075 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
8076 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
8077 rsurface.modelsvector3f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8078 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
8079 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
8080 rsurface.modeltvector3f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8081 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
8082 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
8083 rsurface.modelnormal3f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8084 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
8085 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
8086 rsurface.modellightmapcolor4f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8087 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
8088 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
8089 rsurface.modeltexcoordtexture2f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8090 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
8091 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
8092 rsurface.modeltexcoordlightmap2f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8093 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
8094 rsurface.modelelement3i = model->surfmesh.data_element3i;
8095 rsurface.modelelement3i_indexbuffer = model->surfmesh.data_element3i_indexbuffer;
8096 rsurface.modelelement3i_bufferoffset = model->surfmesh.data_element3i_bufferoffset;
8097 rsurface.modelelement3s = model->surfmesh.data_element3s;
8098 rsurface.modelelement3s_indexbuffer = model->surfmesh.data_element3s_indexbuffer;
8099 rsurface.modelelement3s_bufferoffset = model->surfmesh.data_element3s_bufferoffset;
8100 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
8101 rsurface.modelnumvertices = model->surfmesh.num_vertices;
8102 rsurface.modelnumtriangles = model->surfmesh.num_triangles;
8103 rsurface.modelsurfaces = model->data_surfaces;
8104 rsurface.modelvertexmesh = model->surfmesh.vertexmesh;
8105 rsurface.modelvertexmeshbuffer = model->surfmesh.vertexmeshbuffer;
8106 rsurface.modelvertex3fbuffer = model->surfmesh.vertex3fbuffer;
8107 rsurface.modelgeneratedvertex = false;
8108 rsurface.batchgeneratedvertex = false;
8109 rsurface.batchfirstvertex = 0;
8110 rsurface.batchnumvertices = 0;
8111 rsurface.batchfirsttriangle = 0;
8112 rsurface.batchnumtriangles = 0;
8113 rsurface.batchvertex3f = NULL;
8114 rsurface.batchvertex3f_vertexbuffer = NULL;
8115 rsurface.batchvertex3f_bufferoffset = 0;
8116 rsurface.batchsvector3f = NULL;
8117 rsurface.batchsvector3f_vertexbuffer = NULL;
8118 rsurface.batchsvector3f_bufferoffset = 0;
8119 rsurface.batchtvector3f = NULL;
8120 rsurface.batchtvector3f_vertexbuffer = NULL;
8121 rsurface.batchtvector3f_bufferoffset = 0;
8122 rsurface.batchnormal3f = NULL;
8123 rsurface.batchnormal3f_vertexbuffer = NULL;
8124 rsurface.batchnormal3f_bufferoffset = 0;
8125 rsurface.batchlightmapcolor4f = NULL;
8126 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
8127 rsurface.batchlightmapcolor4f_bufferoffset = 0;
8128 rsurface.batchtexcoordtexture2f = NULL;
8129 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8130 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8131 rsurface.batchtexcoordlightmap2f = NULL;
8132 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
8133 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
8134 rsurface.batchvertexmesh = NULL;
8135 rsurface.batchvertexmeshbuffer = NULL;
8136 rsurface.batchvertex3fbuffer = NULL;
8137 rsurface.batchelement3i = NULL;
8138 rsurface.batchelement3i_indexbuffer = NULL;
8139 rsurface.batchelement3i_bufferoffset = 0;
8140 rsurface.batchelement3s = NULL;
8141 rsurface.batchelement3s_indexbuffer = NULL;
8142 rsurface.batchelement3s_bufferoffset = 0;
8143 rsurface.passcolor4f = NULL;
8144 rsurface.passcolor4f_vertexbuffer = NULL;
8145 rsurface.passcolor4f_bufferoffset = 0;
8148 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents, qboolean prepass)
8150 dp_model_t *model = ent->model;
8151 //if (rsurface.entity == ent && (!model->surfmesh.isanimated || (!wantnormals && !wanttangents)))
8153 rsurface.entity = (entity_render_t *)ent;
8154 rsurface.skeleton = ent->skeleton;
8155 memcpy(rsurface.userwavefunc_param, ent->userwavefunc_param, sizeof(rsurface.userwavefunc_param));
8156 rsurface.ent_skinnum = ent->skinnum;
8157 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;
8158 rsurface.ent_flags = ent->flags;
8159 rsurface.shadertime = r_refdef.scene.time - ent->shadertime;
8160 rsurface.matrix = ent->matrix;
8161 rsurface.inversematrix = ent->inversematrix;
8162 rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
8163 rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
8164 R_EntityMatrix(&rsurface.matrix);
8165 Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
8166 Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
8167 rsurface.fogplaneviewdist *= rsurface.inversematrixscale;
8168 rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
8169 rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
8170 rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
8171 VectorCopy(ent->modellight_ambient, rsurface.modellight_ambient);
8172 VectorCopy(ent->modellight_diffuse, rsurface.modellight_diffuse);
8173 VectorCopy(ent->modellight_lightdir, rsurface.modellight_lightdir);
8174 VectorCopy(ent->colormap_pantscolor, rsurface.colormap_pantscolor);
8175 VectorCopy(ent->colormap_shirtcolor, rsurface.colormap_shirtcolor);
8176 VectorScale(ent->colormod, r_refdef.view.colorscale, rsurface.colormod);
8177 rsurface.colormod[3] = ent->alpha;
8178 VectorScale(ent->glowmod, r_refdef.view.colorscale * r_hdr_glowintensity.value, rsurface.glowmod);
8179 memcpy(rsurface.frameblend, ent->frameblend, sizeof(ent->frameblend));
8180 rsurface.ent_alttextures = ent->framegroupblend[0].frame != 0;
8181 rsurface.basepolygonfactor = r_refdef.polygonfactor;
8182 rsurface.basepolygonoffset = r_refdef.polygonoffset;
8183 if (ent->model->brush.submodel && !prepass)
8185 rsurface.basepolygonfactor += r_polygonoffset_submodel_factor.value;
8186 rsurface.basepolygonoffset += r_polygonoffset_submodel_offset.value;
8188 if (model->surfmesh.isanimated && model->AnimateVertices)
8190 if (ent->animcache_vertex3f)
8192 rsurface.modelvertex3f = ent->animcache_vertex3f;
8193 rsurface.modelsvector3f = wanttangents ? ent->animcache_svector3f : NULL;
8194 rsurface.modeltvector3f = wanttangents ? ent->animcache_tvector3f : NULL;
8195 rsurface.modelnormal3f = wantnormals ? ent->animcache_normal3f : NULL;
8196 rsurface.modelvertexmesh = ent->animcache_vertexmesh;
8197 rsurface.modelvertexmeshbuffer = ent->animcache_vertexmeshbuffer;
8198 rsurface.modelvertex3fbuffer = ent->animcache_vertex3fbuffer;
8200 else if (wanttangents)
8202 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
8203 rsurface.modelsvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
8204 rsurface.modeltvector3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
8205 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
8206 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, rsurface.modelsvector3f, rsurface.modeltvector3f);
8207 rsurface.modelvertexmesh = NULL;
8208 rsurface.modelvertexmeshbuffer = NULL;
8209 rsurface.modelvertex3fbuffer = NULL;
8211 else if (wantnormals)
8213 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
8214 rsurface.modelsvector3f = NULL;
8215 rsurface.modeltvector3f = NULL;
8216 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
8217 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, rsurface.modelnormal3f, NULL, NULL);
8218 rsurface.modelvertexmesh = NULL;
8219 rsurface.modelvertexmeshbuffer = NULL;
8220 rsurface.modelvertex3fbuffer = NULL;
8224 rsurface.modelvertex3f = (float *)R_FrameData_Alloc(model->surfmesh.num_vertices * sizeof(float[3]));
8225 rsurface.modelsvector3f = NULL;
8226 rsurface.modeltvector3f = NULL;
8227 rsurface.modelnormal3f = NULL;
8228 model->AnimateVertices(model, rsurface.frameblend, rsurface.skeleton, rsurface.modelvertex3f, NULL, NULL, NULL);
8229 rsurface.modelvertexmesh = NULL;
8230 rsurface.modelvertexmeshbuffer = NULL;
8231 rsurface.modelvertex3fbuffer = NULL;
8233 rsurface.modelvertex3f_vertexbuffer = 0;
8234 rsurface.modelvertex3f_bufferoffset = 0;
8235 rsurface.modelsvector3f_vertexbuffer = 0;
8236 rsurface.modelsvector3f_bufferoffset = 0;
8237 rsurface.modeltvector3f_vertexbuffer = 0;
8238 rsurface.modeltvector3f_bufferoffset = 0;
8239 rsurface.modelnormal3f_vertexbuffer = 0;
8240 rsurface.modelnormal3f_bufferoffset = 0;
8241 rsurface.modelgeneratedvertex = true;
8245 rsurface.modelvertex3f = model->surfmesh.data_vertex3f;
8246 rsurface.modelvertex3f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8247 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
8248 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
8249 rsurface.modelsvector3f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8250 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
8251 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
8252 rsurface.modeltvector3f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8253 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
8254 rsurface.modelnormal3f = model->surfmesh.data_normal3f;
8255 rsurface.modelnormal3f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8256 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
8257 rsurface.modelvertexmesh = model->surfmesh.vertexmesh;
8258 rsurface.modelvertexmeshbuffer = model->surfmesh.vertexmeshbuffer;
8259 rsurface.modelvertex3fbuffer = model->surfmesh.vertex3fbuffer;
8260 rsurface.modelgeneratedvertex = false;
8262 rsurface.modellightmapcolor4f = model->surfmesh.data_lightmapcolor4f;
8263 rsurface.modellightmapcolor4f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8264 rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
8265 rsurface.modeltexcoordtexture2f = model->surfmesh.data_texcoordtexture2f;
8266 rsurface.modeltexcoordtexture2f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8267 rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
8268 rsurface.modeltexcoordlightmap2f = model->surfmesh.data_texcoordlightmap2f;
8269 rsurface.modeltexcoordlightmap2f_vertexbuffer = model->surfmesh.vbo_vertexbuffer;
8270 rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
8271 rsurface.modelelement3i = model->surfmesh.data_element3i;
8272 rsurface.modelelement3i_indexbuffer = model->surfmesh.data_element3i_indexbuffer;
8273 rsurface.modelelement3i_bufferoffset = model->surfmesh.data_element3i_bufferoffset;
8274 rsurface.modelelement3s = model->surfmesh.data_element3s;
8275 rsurface.modelelement3s_indexbuffer = model->surfmesh.data_element3s_indexbuffer;
8276 rsurface.modelelement3s_bufferoffset = model->surfmesh.data_element3s_bufferoffset;
8277 rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
8278 rsurface.modelnumvertices = model->surfmesh.num_vertices;
8279 rsurface.modelnumtriangles = model->surfmesh.num_triangles;
8280 rsurface.modelsurfaces = model->data_surfaces;
8281 rsurface.batchgeneratedvertex = false;
8282 rsurface.batchfirstvertex = 0;
8283 rsurface.batchnumvertices = 0;
8284 rsurface.batchfirsttriangle = 0;
8285 rsurface.batchnumtriangles = 0;
8286 rsurface.batchvertex3f = NULL;
8287 rsurface.batchvertex3f_vertexbuffer = NULL;
8288 rsurface.batchvertex3f_bufferoffset = 0;
8289 rsurface.batchsvector3f = NULL;
8290 rsurface.batchsvector3f_vertexbuffer = NULL;
8291 rsurface.batchsvector3f_bufferoffset = 0;
8292 rsurface.batchtvector3f = NULL;
8293 rsurface.batchtvector3f_vertexbuffer = NULL;
8294 rsurface.batchtvector3f_bufferoffset = 0;
8295 rsurface.batchnormal3f = NULL;
8296 rsurface.batchnormal3f_vertexbuffer = NULL;
8297 rsurface.batchnormal3f_bufferoffset = 0;
8298 rsurface.batchlightmapcolor4f = NULL;
8299 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
8300 rsurface.batchlightmapcolor4f_bufferoffset = 0;
8301 rsurface.batchtexcoordtexture2f = NULL;
8302 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8303 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8304 rsurface.batchtexcoordlightmap2f = NULL;
8305 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
8306 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
8307 rsurface.batchvertexmesh = NULL;
8308 rsurface.batchvertexmeshbuffer = NULL;
8309 rsurface.batchvertex3fbuffer = NULL;
8310 rsurface.batchelement3i = NULL;
8311 rsurface.batchelement3i_indexbuffer = NULL;
8312 rsurface.batchelement3i_bufferoffset = 0;
8313 rsurface.batchelement3s = NULL;
8314 rsurface.batchelement3s_indexbuffer = NULL;
8315 rsurface.batchelement3s_bufferoffset = 0;
8316 rsurface.passcolor4f = NULL;
8317 rsurface.passcolor4f_vertexbuffer = NULL;
8318 rsurface.passcolor4f_bufferoffset = 0;
8321 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, qboolean wantnormals, qboolean wanttangents)
8323 rsurface.entity = r_refdef.scene.worldentity;
8324 rsurface.skeleton = NULL;
8325 rsurface.ent_skinnum = 0;
8326 rsurface.ent_qwskin = -1;
8327 rsurface.ent_flags = entflags;
8328 rsurface.shadertime = r_refdef.scene.time - shadertime;
8329 rsurface.modelnumvertices = numvertices;
8330 rsurface.modelnumtriangles = numtriangles;
8331 rsurface.matrix = *matrix;
8332 rsurface.inversematrix = *inversematrix;
8333 rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
8334 rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
8335 R_EntityMatrix(&rsurface.matrix);
8336 Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
8337 Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
8338 rsurface.fogplaneviewdist *= rsurface.inversematrixscale;
8339 rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
8340 rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
8341 rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
8342 VectorSet(rsurface.modellight_ambient, 0, 0, 0);
8343 VectorSet(rsurface.modellight_diffuse, 0, 0, 0);
8344 VectorSet(rsurface.modellight_lightdir, 0, 0, 1);
8345 VectorSet(rsurface.colormap_pantscolor, 0, 0, 0);
8346 VectorSet(rsurface.colormap_shirtcolor, 0, 0, 0);
8347 Vector4Set(rsurface.colormod, r * r_refdef.view.colorscale, g * r_refdef.view.colorscale, b * r_refdef.view.colorscale, a);
8348 VectorSet(rsurface.glowmod, r_refdef.view.colorscale * r_hdr_glowintensity.value, r_refdef.view.colorscale * r_hdr_glowintensity.value, r_refdef.view.colorscale * r_hdr_glowintensity.value);
8349 memset(rsurface.frameblend, 0, sizeof(rsurface.frameblend));
8350 rsurface.frameblend[0].lerp = 1;
8351 rsurface.ent_alttextures = false;
8352 rsurface.basepolygonfactor = r_refdef.polygonfactor;
8353 rsurface.basepolygonoffset = r_refdef.polygonoffset;
8356 rsurface.modelvertex3f = (float *)vertex3f;
8357 rsurface.modelsvector3f = svector3f ? (float *)svector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
8358 rsurface.modeltvector3f = tvector3f ? (float *)tvector3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
8359 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
8361 else if (wantnormals)
8363 rsurface.modelvertex3f = (float *)vertex3f;
8364 rsurface.modelsvector3f = NULL;
8365 rsurface.modeltvector3f = NULL;
8366 rsurface.modelnormal3f = normal3f ? (float *)normal3f : (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
8370 rsurface.modelvertex3f = (float *)vertex3f;
8371 rsurface.modelsvector3f = NULL;
8372 rsurface.modeltvector3f = NULL;
8373 rsurface.modelnormal3f = NULL;
8375 rsurface.modelvertexmesh = NULL;
8376 rsurface.modelvertexmeshbuffer = NULL;
8377 rsurface.modelvertex3fbuffer = NULL;
8378 rsurface.modelvertex3f_vertexbuffer = 0;
8379 rsurface.modelvertex3f_bufferoffset = 0;
8380 rsurface.modelsvector3f_vertexbuffer = 0;
8381 rsurface.modelsvector3f_bufferoffset = 0;
8382 rsurface.modeltvector3f_vertexbuffer = 0;
8383 rsurface.modeltvector3f_bufferoffset = 0;
8384 rsurface.modelnormal3f_vertexbuffer = 0;
8385 rsurface.modelnormal3f_bufferoffset = 0;
8386 rsurface.modelgeneratedvertex = true;
8387 rsurface.modellightmapcolor4f = (float *)color4f;
8388 rsurface.modellightmapcolor4f_vertexbuffer = 0;
8389 rsurface.modellightmapcolor4f_bufferoffset = 0;
8390 rsurface.modeltexcoordtexture2f = (float *)texcoord2f;
8391 rsurface.modeltexcoordtexture2f_vertexbuffer = 0;
8392 rsurface.modeltexcoordtexture2f_bufferoffset = 0;
8393 rsurface.modeltexcoordlightmap2f = NULL;
8394 rsurface.modeltexcoordlightmap2f_vertexbuffer = 0;
8395 rsurface.modeltexcoordlightmap2f_bufferoffset = 0;
8396 rsurface.modelelement3i = (int *)element3i;
8397 rsurface.modelelement3i_indexbuffer = NULL;
8398 rsurface.modelelement3i_bufferoffset = 0;
8399 rsurface.modelelement3s = (unsigned short *)element3s;
8400 rsurface.modelelement3s_indexbuffer = NULL;
8401 rsurface.modelelement3s_bufferoffset = 0;
8402 rsurface.modellightmapoffsets = NULL;
8403 rsurface.modelsurfaces = NULL;
8404 rsurface.batchgeneratedvertex = false;
8405 rsurface.batchfirstvertex = 0;
8406 rsurface.batchnumvertices = 0;
8407 rsurface.batchfirsttriangle = 0;
8408 rsurface.batchnumtriangles = 0;
8409 rsurface.batchvertex3f = NULL;
8410 rsurface.batchvertex3f_vertexbuffer = NULL;
8411 rsurface.batchvertex3f_bufferoffset = 0;
8412 rsurface.batchsvector3f = NULL;
8413 rsurface.batchsvector3f_vertexbuffer = NULL;
8414 rsurface.batchsvector3f_bufferoffset = 0;
8415 rsurface.batchtvector3f = NULL;
8416 rsurface.batchtvector3f_vertexbuffer = NULL;
8417 rsurface.batchtvector3f_bufferoffset = 0;
8418 rsurface.batchnormal3f = NULL;
8419 rsurface.batchnormal3f_vertexbuffer = NULL;
8420 rsurface.batchnormal3f_bufferoffset = 0;
8421 rsurface.batchlightmapcolor4f = NULL;
8422 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
8423 rsurface.batchlightmapcolor4f_bufferoffset = 0;
8424 rsurface.batchtexcoordtexture2f = NULL;
8425 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8426 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8427 rsurface.batchtexcoordlightmap2f = NULL;
8428 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
8429 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
8430 rsurface.batchvertexmesh = NULL;
8431 rsurface.batchvertexmeshbuffer = NULL;
8432 rsurface.batchvertex3fbuffer = NULL;
8433 rsurface.batchelement3i = NULL;
8434 rsurface.batchelement3i_indexbuffer = NULL;
8435 rsurface.batchelement3i_bufferoffset = 0;
8436 rsurface.batchelement3s = NULL;
8437 rsurface.batchelement3s_indexbuffer = NULL;
8438 rsurface.batchelement3s_bufferoffset = 0;
8439 rsurface.passcolor4f = NULL;
8440 rsurface.passcolor4f_vertexbuffer = NULL;
8441 rsurface.passcolor4f_bufferoffset = 0;
8443 if (rsurface.modelnumvertices && rsurface.modelelement3i)
8445 if ((wantnormals || wanttangents) && !normal3f)
8447 rsurface.modelnormal3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
8448 Mod_BuildNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.modelnormal3f, r_smoothnormals_areaweighting.integer != 0);
8450 if (wanttangents && !svector3f)
8452 rsurface.modelsvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
8453 rsurface.modeltvector3f = (float *)R_FrameData_Alloc(rsurface.modelnumvertices * sizeof(float[3]));
8454 Mod_BuildTextureVectorsFromNormals(0, rsurface.modelnumvertices, rsurface.modelnumtriangles, rsurface.modelvertex3f, rsurface.modeltexcoordtexture2f, rsurface.modelnormal3f, rsurface.modelelement3i, rsurface.modelsvector3f, rsurface.modeltvector3f, r_smoothnormals_areaweighting.integer != 0);
8459 float RSurf_FogPoint(const float *v)
8461 // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
8462 float FogPlaneViewDist = r_refdef.fogplaneviewdist;
8463 float FogPlaneVertexDist = DotProduct(r_refdef.fogplane, v) + r_refdef.fogplane[3];
8464 float FogHeightFade = r_refdef.fogheightfade;
8466 unsigned int fogmasktableindex;
8467 if (r_refdef.fogplaneviewabove)
8468 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
8470 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
8471 fogmasktableindex = (unsigned int)(VectorDistance(r_refdef.view.origin, v) * fogfrac * r_refdef.fogmasktabledistmultiplier);
8472 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
8475 float RSurf_FogVertex(const float *v)
8477 // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
8478 float FogPlaneViewDist = rsurface.fogplaneviewdist;
8479 float FogPlaneVertexDist = DotProduct(rsurface.fogplane, v) + rsurface.fogplane[3];
8480 float FogHeightFade = rsurface.fogheightfade;
8482 unsigned int fogmasktableindex;
8483 if (r_refdef.fogplaneviewabove)
8484 fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
8486 fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
8487 fogmasktableindex = (unsigned int)(VectorDistance(rsurface.localvieworigin, v) * fogfrac * rsurface.fogmasktabledistmultiplier);
8488 return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
8491 static void RSurf_RenumberElements(const int *inelement3i, int *outelement3i, int numelements, int adjust)
8494 for (i = 0;i < numelements;i++)
8495 outelement3i[i] = inelement3i[i] + adjust;
8498 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
8499 extern cvar_t gl_vbo;
8500 void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const msurface_t **texturesurfacelist)
8508 int surfacefirsttriangle;
8509 int surfacenumtriangles;
8510 int surfacefirstvertex;
8511 int surfaceendvertex;
8512 int surfacenumvertices;
8513 int batchnumvertices;
8514 int batchnumtriangles;
8518 qboolean dynamicvertex;
8522 float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
8524 q3shaderinfo_deform_t *deform;
8525 const msurface_t *surface, *firstsurface;
8526 r_vertexmesh_t *vertexmesh;
8527 if (!texturenumsurfaces)
8529 // find vertex range of this surface batch
8531 firstsurface = texturesurfacelist[0];
8532 firsttriangle = firstsurface->num_firsttriangle;
8533 batchnumvertices = 0;
8534 batchnumtriangles = 0;
8535 firstvertex = endvertex = firstsurface->num_firstvertex;
8536 for (i = 0;i < texturenumsurfaces;i++)
8538 surface = texturesurfacelist[i];
8539 if (surface != firstsurface + i)
8541 surfacefirstvertex = surface->num_firstvertex;
8542 surfaceendvertex = surfacefirstvertex + surface->num_vertices;
8543 surfacenumvertices = surface->num_vertices;
8544 surfacenumtriangles = surface->num_triangles;
8545 if (firstvertex > surfacefirstvertex)
8546 firstvertex = surfacefirstvertex;
8547 if (endvertex < surfaceendvertex)
8548 endvertex = surfaceendvertex;
8549 batchnumvertices += surfacenumvertices;
8550 batchnumtriangles += surfacenumtriangles;
8553 // we now know the vertex range used, and if there are any gaps in it
8554 rsurface.batchfirstvertex = firstvertex;
8555 rsurface.batchnumvertices = endvertex - firstvertex;
8556 rsurface.batchfirsttriangle = firsttriangle;
8557 rsurface.batchnumtriangles = batchnumtriangles;
8559 // this variable holds flags for which properties have been updated that
8560 // may require regenerating vertexmesh array...
8563 // check if any dynamic vertex processing must occur
8564 dynamicvertex = false;
8566 // if there is a chance of animated vertex colors, it's a dynamic batch
8567 if ((batchneed & (BATCHNEED_VERTEXMESH_VERTEXCOLOR | BATCHNEED_ARRAY_VERTEXCOLOR)) && texturesurfacelist[0]->lightmapinfo)
8569 dynamicvertex = true;
8570 batchneed |= BATCHNEED_NOGAPS;
8571 needsupdate |= BATCHNEED_VERTEXMESH_VERTEXCOLOR;
8574 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
8576 switch (deform->deform)
8579 case Q3DEFORM_PROJECTIONSHADOW:
8580 case Q3DEFORM_TEXT0:
8581 case Q3DEFORM_TEXT1:
8582 case Q3DEFORM_TEXT2:
8583 case Q3DEFORM_TEXT3:
8584 case Q3DEFORM_TEXT4:
8585 case Q3DEFORM_TEXT5:
8586 case Q3DEFORM_TEXT6:
8587 case Q3DEFORM_TEXT7:
8590 case Q3DEFORM_AUTOSPRITE:
8591 dynamicvertex = true;
8592 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS;
8593 needsupdate |= BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR;
8595 case Q3DEFORM_AUTOSPRITE2:
8596 dynamicvertex = true;
8597 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS;
8598 needsupdate |= BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR;
8600 case Q3DEFORM_NORMAL:
8601 dynamicvertex = true;
8602 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS;
8603 needsupdate |= BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR;
8606 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
8607 break; // if wavefunc is a nop, ignore this transform
8608 dynamicvertex = true;
8609 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS;
8610 needsupdate |= BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR;
8612 case Q3DEFORM_BULGE:
8613 dynamicvertex = true;
8614 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS;
8615 needsupdate |= BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR;
8618 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
8619 break; // if wavefunc is a nop, ignore this transform
8620 dynamicvertex = true;
8621 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS;
8622 needsupdate |= BATCHNEED_VERTEXMESH_VERTEX;
8626 switch(rsurface.texture->tcgen.tcgen)
8629 case Q3TCGEN_TEXTURE:
8631 case Q3TCGEN_LIGHTMAP:
8632 dynamicvertex = true;
8633 batchneed |= BATCHNEED_ARRAY_LIGHTMAP | BATCHNEED_NOGAPS;
8634 needsupdate |= BATCHNEED_VERTEXMESH_LIGHTMAP;
8636 case Q3TCGEN_VECTOR:
8637 dynamicvertex = true;
8638 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS;
8639 needsupdate |= BATCHNEED_VERTEXMESH_TEXCOORD;
8641 case Q3TCGEN_ENVIRONMENT:
8642 dynamicvertex = true;
8643 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS;
8644 needsupdate |= BATCHNEED_VERTEXMESH_TEXCOORD;
8647 if (rsurface.texture->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
8649 dynamicvertex = true;
8650 batchneed |= BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS;
8651 needsupdate |= BATCHNEED_VERTEXMESH_TEXCOORD;
8654 if (!rsurface.modelvertexmesh && (batchneed & (BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR | BATCHNEED_VERTEXMESH_VERTEXCOLOR | BATCHNEED_VERTEXMESH_TEXCOORD | BATCHNEED_VERTEXMESH_LIGHTMAP)))
8656 dynamicvertex = true;
8657 batchneed |= BATCHNEED_NOGAPS;
8658 needsupdate |= (batchneed & (BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR | BATCHNEED_VERTEXMESH_VERTEXCOLOR | BATCHNEED_VERTEXMESH_TEXCOORD | BATCHNEED_VERTEXMESH_LIGHTMAP));
8661 if (dynamicvertex || gaps || rsurface.batchfirstvertex)
8663 // when copying, we need to consider the regeneration of vertexmesh, any dependencies it may have must be set...
8664 if (batchneed & BATCHNEED_VERTEXMESH_VERTEX) batchneed |= BATCHNEED_ARRAY_VERTEX;
8665 if (batchneed & BATCHNEED_VERTEXMESH_NORMAL) batchneed |= BATCHNEED_ARRAY_NORMAL;
8666 if (batchneed & BATCHNEED_VERTEXMESH_VECTOR) batchneed |= BATCHNEED_ARRAY_VECTOR;
8667 if (batchneed & BATCHNEED_VERTEXMESH_VERTEXCOLOR) batchneed |= BATCHNEED_ARRAY_VERTEXCOLOR;
8668 if (batchneed & BATCHNEED_VERTEXMESH_TEXCOORD) batchneed |= BATCHNEED_ARRAY_TEXCOORD;
8669 if (batchneed & BATCHNEED_VERTEXMESH_LIGHTMAP) batchneed |= BATCHNEED_ARRAY_LIGHTMAP;
8672 // when the model data has no vertex buffer (dynamic mesh), we need to
8674 if (vid.useinterleavedarrays ? !rsurface.modelvertexmeshbuffer : !rsurface.modelvertex3f_vertexbuffer)
8675 batchneed |= BATCHNEED_NOGAPS;
8677 // if needsupdate, we have to do a dynamic vertex batch for sure
8678 if (needsupdate & batchneed)
8679 dynamicvertex = true;
8681 // see if we need to build vertexmesh from arrays
8682 if (!rsurface.modelvertexmesh && (batchneed & (BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR | BATCHNEED_VERTEXMESH_VERTEXCOLOR | BATCHNEED_VERTEXMESH_TEXCOORD | BATCHNEED_VERTEXMESH_LIGHTMAP)))
8683 dynamicvertex = true;
8685 // if gaps are unacceptable, and there are gaps, it's a dynamic batch...
8686 // also some drivers strongly dislike firstvertex
8687 if ((batchneed & BATCHNEED_NOGAPS) && (gaps || firstvertex))
8688 dynamicvertex = true;
8690 rsurface.batchvertex3f = rsurface.modelvertex3f;
8691 rsurface.batchvertex3f_vertexbuffer = rsurface.modelvertex3f_vertexbuffer;
8692 rsurface.batchvertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
8693 rsurface.batchsvector3f = rsurface.modelsvector3f;
8694 rsurface.batchsvector3f_vertexbuffer = rsurface.modelsvector3f_vertexbuffer;
8695 rsurface.batchsvector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
8696 rsurface.batchtvector3f = rsurface.modeltvector3f;
8697 rsurface.batchtvector3f_vertexbuffer = rsurface.modeltvector3f_vertexbuffer;
8698 rsurface.batchtvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
8699 rsurface.batchnormal3f = rsurface.modelnormal3f;
8700 rsurface.batchnormal3f_vertexbuffer = rsurface.modelnormal3f_vertexbuffer;
8701 rsurface.batchnormal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
8702 rsurface.batchlightmapcolor4f = rsurface.modellightmapcolor4f;
8703 rsurface.batchlightmapcolor4f_vertexbuffer = rsurface.modellightmapcolor4f_vertexbuffer;
8704 rsurface.batchlightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
8705 rsurface.batchtexcoordtexture2f = rsurface.modeltexcoordtexture2f;
8706 rsurface.batchtexcoordtexture2f_vertexbuffer = rsurface.modeltexcoordtexture2f_vertexbuffer;
8707 rsurface.batchtexcoordtexture2f_bufferoffset = rsurface.modeltexcoordtexture2f_bufferoffset;
8708 rsurface.batchtexcoordlightmap2f = rsurface.modeltexcoordlightmap2f;
8709 rsurface.batchtexcoordlightmap2f_vertexbuffer = rsurface.modeltexcoordlightmap2f_vertexbuffer;
8710 rsurface.batchtexcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
8711 rsurface.batchvertex3fbuffer = rsurface.modelvertex3fbuffer;
8712 rsurface.batchvertexmesh = rsurface.modelvertexmesh;
8713 rsurface.batchvertexmeshbuffer = rsurface.modelvertexmeshbuffer;
8714 rsurface.batchelement3i = rsurface.modelelement3i;
8715 rsurface.batchelement3i_indexbuffer = rsurface.modelelement3i_indexbuffer;
8716 rsurface.batchelement3i_bufferoffset = rsurface.modelelement3i_bufferoffset;
8717 rsurface.batchelement3s = rsurface.modelelement3s;
8718 rsurface.batchelement3s_indexbuffer = rsurface.modelelement3s_indexbuffer;
8719 rsurface.batchelement3s_bufferoffset = rsurface.modelelement3s_bufferoffset;
8721 // if any dynamic vertex processing has to occur in software, we copy the
8722 // entire surface list together before processing to rebase the vertices
8723 // to start at 0 (otherwise we waste a lot of room in a vertex buffer).
8725 // if any gaps exist and we do not have a static vertex buffer, we have to
8726 // copy the surface list together to avoid wasting upload bandwidth on the
8727 // vertices in the gaps.
8729 // if gaps exist and we have a static vertex buffer, we still have to
8730 // combine the index buffer ranges into one dynamic index buffer.
8732 // in all cases we end up with data that can be drawn in one call.
8736 // static vertex data, just set pointers...
8737 rsurface.batchgeneratedvertex = false;
8738 // if there are gaps, we want to build a combined index buffer,
8739 // otherwise use the original static buffer with an appropriate offset
8742 // build a new triangle elements array for this batch
8743 rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
8744 rsurface.batchfirsttriangle = 0;
8746 for (i = 0;i < texturenumsurfaces;i++)
8748 surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
8749 surfacenumtriangles = texturesurfacelist[i]->num_triangles;
8750 memcpy(rsurface.batchelement3i + 3*numtriangles, rsurface.modelelement3i + 3*surfacefirsttriangle, surfacenumtriangles*sizeof(int[3]));
8751 numtriangles += surfacenumtriangles;
8753 rsurface.batchelement3i_indexbuffer = NULL;
8754 rsurface.batchelement3i_bufferoffset = 0;
8755 rsurface.batchelement3s = NULL;
8756 rsurface.batchelement3s_indexbuffer = NULL;
8757 rsurface.batchelement3s_bufferoffset = 0;
8758 if (endvertex <= 65536)
8760 // make a 16bit (unsigned short) index array if possible
8761 rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
8762 for (i = 0;i < numtriangles*3;i++)
8763 rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
8769 // something needs software processing, do it for real...
8770 // we only directly handle separate array data in this case and then
8771 // generate interleaved data if needed...
8772 rsurface.batchgeneratedvertex = true;
8774 // now copy the vertex data into a combined array and make an index array
8775 // (this is what Quake3 does all the time)
8776 //if (gaps || rsurface.batchfirstvertex)
8778 rsurface.batchvertex3fbuffer = NULL;
8779 rsurface.batchvertexmesh = NULL;
8780 rsurface.batchvertexmeshbuffer = NULL;
8781 rsurface.batchvertex3f = NULL;
8782 rsurface.batchvertex3f_vertexbuffer = NULL;
8783 rsurface.batchvertex3f_bufferoffset = 0;
8784 rsurface.batchsvector3f = NULL;
8785 rsurface.batchsvector3f_vertexbuffer = NULL;
8786 rsurface.batchsvector3f_bufferoffset = 0;
8787 rsurface.batchtvector3f = NULL;
8788 rsurface.batchtvector3f_vertexbuffer = NULL;
8789 rsurface.batchtvector3f_bufferoffset = 0;
8790 rsurface.batchnormal3f = NULL;
8791 rsurface.batchnormal3f_vertexbuffer = NULL;
8792 rsurface.batchnormal3f_bufferoffset = 0;
8793 rsurface.batchlightmapcolor4f = NULL;
8794 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
8795 rsurface.batchlightmapcolor4f_bufferoffset = 0;
8796 rsurface.batchtexcoordtexture2f = NULL;
8797 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
8798 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
8799 rsurface.batchtexcoordlightmap2f = NULL;
8800 rsurface.batchtexcoordlightmap2f_vertexbuffer = NULL;
8801 rsurface.batchtexcoordlightmap2f_bufferoffset = 0;
8802 rsurface.batchelement3i = (int *)R_FrameData_Alloc(batchnumtriangles * sizeof(int[3]));
8803 rsurface.batchelement3i_indexbuffer = NULL;
8804 rsurface.batchelement3i_bufferoffset = 0;
8805 rsurface.batchelement3s = NULL;
8806 rsurface.batchelement3s_indexbuffer = NULL;
8807 rsurface.batchelement3s_bufferoffset = 0;
8808 // we'll only be setting up certain arrays as needed
8809 if (batchneed & (BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR | BATCHNEED_VERTEXMESH_VERTEXCOLOR | BATCHNEED_VERTEXMESH_TEXCOORD | BATCHNEED_VERTEXMESH_LIGHTMAP))
8810 rsurface.batchvertexmesh = (r_vertexmesh_t *)R_FrameData_Alloc(batchnumvertices * sizeof(r_vertexmesh_t));
8811 if (batchneed & BATCHNEED_ARRAY_VERTEX)
8812 rsurface.batchvertex3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8813 if (batchneed & BATCHNEED_ARRAY_NORMAL)
8814 rsurface.batchnormal3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8815 if (batchneed & BATCHNEED_ARRAY_VECTOR)
8817 rsurface.batchsvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8818 rsurface.batchtvector3f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
8820 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
8821 rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
8822 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
8823 rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8824 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
8825 rsurface.batchtexcoordlightmap2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
8828 for (i = 0;i < texturenumsurfaces;i++)
8830 surfacefirstvertex = texturesurfacelist[i]->num_firstvertex;
8831 surfacenumvertices = texturesurfacelist[i]->num_vertices;
8832 surfacefirsttriangle = texturesurfacelist[i]->num_firsttriangle;
8833 surfacenumtriangles = texturesurfacelist[i]->num_triangles;
8834 // copy only the data requested
8835 if ((batchneed & (BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR | BATCHNEED_VERTEXMESH_VERTEXCOLOR | BATCHNEED_VERTEXMESH_TEXCOORD | BATCHNEED_VERTEXMESH_LIGHTMAP)) && rsurface.modelvertexmesh)
8836 memcpy(rsurface.batchvertexmesh + numvertices, rsurface.modelvertexmesh + surfacefirstvertex, surfacenumvertices * sizeof(rsurface.batchvertexmesh[0]));
8837 if (batchneed & (BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_ARRAY_LIGHTMAP))
8839 if (batchneed & BATCHNEED_ARRAY_VERTEX)
8841 if (rsurface.batchvertex3f)
8842 memcpy(rsurface.batchvertex3f + 3*numvertices, rsurface.modelvertex3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
8844 memset(rsurface.batchvertex3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
8846 if (batchneed & BATCHNEED_ARRAY_NORMAL)
8848 if (rsurface.modelnormal3f)
8849 memcpy(rsurface.batchnormal3f + 3*numvertices, rsurface.modelnormal3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
8851 memset(rsurface.batchnormal3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
8853 if (batchneed & BATCHNEED_ARRAY_VECTOR)
8855 if (rsurface.modelsvector3f)
8857 memcpy(rsurface.batchsvector3f + 3*numvertices, rsurface.modelsvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
8858 memcpy(rsurface.batchtvector3f + 3*numvertices, rsurface.modeltvector3f + 3*surfacefirstvertex, surfacenumvertices * sizeof(float[3]));
8862 memset(rsurface.batchsvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
8863 memset(rsurface.batchtvector3f + 3*numvertices, 0, surfacenumvertices * sizeof(float[3]));
8866 if (batchneed & BATCHNEED_ARRAY_VERTEXCOLOR)
8868 if (rsurface.modellightmapcolor4f)
8869 memcpy(rsurface.batchlightmapcolor4f + 4*numvertices, rsurface.modellightmapcolor4f + 4*surfacefirstvertex, surfacenumvertices * sizeof(float[4]));
8871 memset(rsurface.batchlightmapcolor4f + 4*numvertices, 0, surfacenumvertices * sizeof(float[4]));
8873 if (batchneed & BATCHNEED_ARRAY_TEXCOORD)
8875 if (rsurface.modeltexcoordtexture2f)
8876 memcpy(rsurface.batchtexcoordtexture2f + 2*numvertices, rsurface.modeltexcoordtexture2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
8878 memset(rsurface.batchtexcoordtexture2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
8880 if (batchneed & BATCHNEED_ARRAY_LIGHTMAP)
8882 if (rsurface.modeltexcoordlightmap2f)
8883 memcpy(rsurface.batchtexcoordlightmap2f + 2*numvertices, rsurface.modeltexcoordlightmap2f + 2*surfacefirstvertex, surfacenumvertices * sizeof(float[2]));
8885 memset(rsurface.batchtexcoordlightmap2f + 2*numvertices, 0, surfacenumvertices * sizeof(float[2]));
8888 RSurf_RenumberElements(rsurface.modelelement3i + 3*surfacefirsttriangle, rsurface.batchelement3i + 3*numtriangles, 3*surfacenumtriangles, numvertices - surfacefirstvertex);
8889 numvertices += surfacenumvertices;
8890 numtriangles += surfacenumtriangles;
8893 // generate a 16bit index array as well if possible
8894 // (in general, dynamic batches fit)
8895 if (numvertices <= 65536)
8897 rsurface.batchelement3s = (unsigned short *)R_FrameData_Alloc(batchnumtriangles * sizeof(unsigned short[3]));
8898 for (i = 0;i < numtriangles*3;i++)
8899 rsurface.batchelement3s[i] = rsurface.batchelement3i[i];
8902 // since we've copied everything, the batch now starts at 0
8903 rsurface.batchfirstvertex = 0;
8904 rsurface.batchnumvertices = batchnumvertices;
8905 rsurface.batchfirsttriangle = 0;
8906 rsurface.batchnumtriangles = batchnumtriangles;
8909 // q1bsp surfaces rendered in vertex color mode have to have colors
8910 // calculated based on lightstyles
8911 if ((batchneed & (BATCHNEED_VERTEXMESH_VERTEXCOLOR | BATCHNEED_ARRAY_VERTEXCOLOR)) && texturesurfacelist[0]->lightmapinfo)
8913 // generate color arrays for the surfaces in this list
8918 const unsigned char *lm;
8919 rsurface.batchlightmapcolor4f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[4]));
8920 rsurface.batchlightmapcolor4f_vertexbuffer = NULL;
8921 rsurface.batchlightmapcolor4f_bufferoffset = 0;
8923 for (i = 0;i < texturenumsurfaces;i++)
8925 surface = texturesurfacelist[i];
8926 offsets = rsurface.modellightmapoffsets + surface->num_firstvertex;
8927 surfacenumvertices = surface->num_vertices;
8928 if (surface->lightmapinfo->samples)
8930 for (j = 0;j < surfacenumvertices;j++)
8932 lm = surface->lightmapinfo->samples + offsets[j];
8933 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[0]];
8934 VectorScale(lm, scale, c);
8935 if (surface->lightmapinfo->styles[1] != 255)
8937 size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
8939 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[1]];
8940 VectorMA(c, scale, lm, c);
8941 if (surface->lightmapinfo->styles[2] != 255)
8944 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[2]];
8945 VectorMA(c, scale, lm, c);
8946 if (surface->lightmapinfo->styles[3] != 255)
8949 scale = r_refdef.scene.lightstylevalue[surface->lightmapinfo->styles[3]];
8950 VectorMA(c, scale, lm, c);
8957 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);
8963 for (j = 0;j < surfacenumvertices;j++)
8965 Vector4Set(rsurface.batchlightmapcolor4f + 4*numvertices, 0, 0, 0, 1);
8972 // if vertices are deformed (sprite flares and things in maps, possibly
8973 // water waves, bulges and other deformations), modify the copied vertices
8975 for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform && r_deformvertexes.integer;deformindex++, deform++)
8977 switch (deform->deform)
8980 case Q3DEFORM_PROJECTIONSHADOW:
8981 case Q3DEFORM_TEXT0:
8982 case Q3DEFORM_TEXT1:
8983 case Q3DEFORM_TEXT2:
8984 case Q3DEFORM_TEXT3:
8985 case Q3DEFORM_TEXT4:
8986 case Q3DEFORM_TEXT5:
8987 case Q3DEFORM_TEXT6:
8988 case Q3DEFORM_TEXT7:
8991 case Q3DEFORM_AUTOSPRITE:
8992 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
8993 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
8994 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
8995 VectorNormalize(newforward);
8996 VectorNormalize(newright);
8997 VectorNormalize(newup);
8998 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
8999 // rsurface.batchvertex3f_vertexbuffer = NULL;
9000 // rsurface.batchvertex3f_bufferoffset = 0;
9001 // rsurface.batchsvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchsvector3f);
9002 // rsurface.batchsvector3f_vertexbuffer = NULL;
9003 // rsurface.batchsvector3f_bufferoffset = 0;
9004 // rsurface.batchtvector3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchtvector3f);
9005 // rsurface.batchtvector3f_vertexbuffer = NULL;
9006 // rsurface.batchtvector3f_bufferoffset = 0;
9007 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
9008 // rsurface.batchnormal3f_vertexbuffer = NULL;
9009 // rsurface.batchnormal3f_bufferoffset = 0;
9010 // sometimes we're on a renderpath that does not use vectors (GL11/GL13/GLES1)
9011 if (!VectorLength2(rsurface.batchnormal3f + 3*rsurface.batchfirstvertex))
9012 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
9013 if (!VectorLength2(rsurface.batchsvector3f + 3*rsurface.batchfirstvertex))
9014 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);
9015 // a single autosprite surface can contain multiple sprites...
9016 for (j = 0;j < batchnumvertices - 3;j += 4)
9018 VectorClear(center);
9019 for (i = 0;i < 4;i++)
9020 VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
9021 VectorScale(center, 0.25f, center);
9022 VectorCopy(rsurface.batchnormal3f + 3*j, forward);
9023 VectorCopy(rsurface.batchsvector3f + 3*j, right);
9024 VectorCopy(rsurface.batchtvector3f + 3*j, up);
9025 for (i = 0;i < 4;i++)
9027 VectorSubtract(rsurface.batchvertex3f + 3*(j+i), center, v);
9028 VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.batchvertex3f + 3*(j+i));
9031 // if we get here, BATCHNEED_ARRAY_NORMAL and BATCHNEED_ARRAY_VECTOR are in batchneed, so no need to check
9032 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
9033 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);
9035 case Q3DEFORM_AUTOSPRITE2:
9036 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, newforward);
9037 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.right, newright);
9038 Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.up, newup);
9039 VectorNormalize(newforward);
9040 VectorNormalize(newright);
9041 VectorNormalize(newup);
9042 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
9043 // rsurface.batchvertex3f_vertexbuffer = NULL;
9044 // rsurface.batchvertex3f_bufferoffset = 0;
9046 const float *v1, *v2;
9056 memset(shortest, 0, sizeof(shortest));
9057 // a single autosprite surface can contain multiple sprites...
9058 for (j = 0;j < batchnumvertices - 3;j += 4)
9060 VectorClear(center);
9061 for (i = 0;i < 4;i++)
9062 VectorAdd(center, rsurface.batchvertex3f + 3*(j+i), center);
9063 VectorScale(center, 0.25f, center);
9064 // find the two shortest edges, then use them to define the
9065 // axis vectors for rotating around the central axis
9066 for (i = 0;i < 6;i++)
9068 v1 = rsurface.batchvertex3f + 3*(j+quadedges[i][0]);
9069 v2 = rsurface.batchvertex3f + 3*(j+quadedges[i][1]);
9070 l = VectorDistance2(v1, v2);
9071 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
9073 l += (1.0f / 1024.0f);
9074 if (shortest[0].length2 > l || i == 0)
9076 shortest[1] = shortest[0];
9077 shortest[0].length2 = l;
9078 shortest[0].v1 = v1;
9079 shortest[0].v2 = v2;
9081 else if (shortest[1].length2 > l || i == 1)
9083 shortest[1].length2 = l;
9084 shortest[1].v1 = v1;
9085 shortest[1].v2 = v2;
9088 VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
9089 VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
9090 // this calculates the right vector from the shortest edge
9091 // and the up vector from the edge midpoints
9092 VectorSubtract(shortest[0].v1, shortest[0].v2, right);
9093 VectorNormalize(right);
9094 VectorSubtract(end, start, up);
9095 VectorNormalize(up);
9096 // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
9097 VectorSubtract(rsurface.localvieworigin, center, forward);
9098 //Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.view.forward, forward);
9099 VectorNegate(forward, forward);
9100 VectorReflect(forward, 0, up, forward);
9101 VectorNormalize(forward);
9102 CrossProduct(up, forward, newright);
9103 VectorNormalize(newright);
9104 // rotate the quad around the up axis vector, this is made
9105 // especially easy by the fact we know the quad is flat,
9106 // so we only have to subtract the center position and
9107 // measure distance along the right vector, and then
9108 // multiply that by the newright vector and add back the
9110 // we also need to subtract the old position to undo the
9111 // displacement from the center, which we do with a
9112 // DotProduct, the subtraction/addition of center is also
9113 // optimized into DotProducts here
9114 l = DotProduct(right, center);
9115 for (i = 0;i < 4;i++)
9117 v1 = rsurface.batchvertex3f + 3*(j+i);
9118 f = DotProduct(right, v1) - l;
9119 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.batchvertex3f + 3*(j+i));
9123 if(batchneed & (BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR)) // otherwise these can stay NULL
9125 // rsurface.batchnormal3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
9126 // rsurface.batchnormal3f_vertexbuffer = NULL;
9127 // rsurface.batchnormal3f_bufferoffset = 0;
9128 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
9130 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
9132 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
9133 // rsurface.batchsvector3f_vertexbuffer = NULL;
9134 // rsurface.batchsvector3f_bufferoffset = 0;
9135 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
9136 // rsurface.batchtvector3f_vertexbuffer = NULL;
9137 // rsurface.batchtvector3f_bufferoffset = 0;
9138 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);
9141 case Q3DEFORM_NORMAL:
9142 // deform the normals to make reflections wavey
9143 rsurface.batchnormal3f = (float *)R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
9144 rsurface.batchnormal3f_vertexbuffer = NULL;
9145 rsurface.batchnormal3f_bufferoffset = 0;
9146 for (j = 0;j < batchnumvertices;j++)
9149 float *normal = rsurface.batchnormal3f + 3*j;
9150 VectorScale(rsurface.batchvertex3f + 3*j, 0.98f, vertex);
9151 normal[0] = rsurface.batchnormal3f[j*3+0] + deform->parms[0] * noise4f( vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
9152 normal[1] = rsurface.batchnormal3f[j*3+1] + deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
9153 normal[2] = rsurface.batchnormal3f[j*3+2] + deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], rsurface.shadertime * deform->parms[1]);
9154 VectorNormalize(normal);
9156 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
9158 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
9159 // rsurface.batchsvector3f_vertexbuffer = NULL;
9160 // rsurface.batchsvector3f_bufferoffset = 0;
9161 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
9162 // rsurface.batchtvector3f_vertexbuffer = NULL;
9163 // rsurface.batchtvector3f_bufferoffset = 0;
9164 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);
9168 // deform vertex array to make wavey water and flags and such
9169 waveparms[0] = deform->waveparms[0];
9170 waveparms[1] = deform->waveparms[1];
9171 waveparms[2] = deform->waveparms[2];
9172 waveparms[3] = deform->waveparms[3];
9173 if(!R_TestQ3WaveFunc(deform->wavefunc, waveparms))
9174 break; // if wavefunc is a nop, don't make a dynamic vertex array
9175 // this is how a divisor of vertex influence on deformation
9176 animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
9177 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
9178 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
9179 // rsurface.batchvertex3f_vertexbuffer = NULL;
9180 // rsurface.batchvertex3f_bufferoffset = 0;
9181 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
9182 // rsurface.batchnormal3f_vertexbuffer = NULL;
9183 // rsurface.batchnormal3f_bufferoffset = 0;
9184 for (j = 0;j < batchnumvertices;j++)
9186 // if the wavefunc depends on time, evaluate it per-vertex
9189 waveparms[2] = deform->waveparms[2] + (rsurface.batchvertex3f[j*3+0] + rsurface.batchvertex3f[j*3+1] + rsurface.batchvertex3f[j*3+2]) * animpos;
9190 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
9192 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
9194 // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
9195 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
9196 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
9198 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
9199 // rsurface.batchsvector3f_vertexbuffer = NULL;
9200 // rsurface.batchsvector3f_bufferoffset = 0;
9201 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
9202 // rsurface.batchtvector3f_vertexbuffer = NULL;
9203 // rsurface.batchtvector3f_bufferoffset = 0;
9204 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);
9207 case Q3DEFORM_BULGE:
9208 // deform vertex array to make the surface have moving bulges
9209 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
9210 // rsurface.batchvertex3f_vertexbuffer = NULL;
9211 // rsurface.batchvertex3f_bufferoffset = 0;
9212 // rsurface.batchnormal3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchnormal3f);
9213 // rsurface.batchnormal3f_vertexbuffer = NULL;
9214 // rsurface.batchnormal3f_bufferoffset = 0;
9215 for (j = 0;j < batchnumvertices;j++)
9217 scale = sin(rsurface.batchtexcoordtexture2f[j*2+0] * deform->parms[0] + rsurface.shadertime * deform->parms[2]) * deform->parms[1];
9218 VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.batchvertex3f + 3*j);
9220 // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
9221 Mod_BuildNormals(rsurface.batchfirstvertex, batchnumvertices, batchnumtriangles, rsurface.batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
9222 if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
9224 // rsurface.batchsvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
9225 // rsurface.batchsvector3f_vertexbuffer = NULL;
9226 // rsurface.batchsvector3f_bufferoffset = 0;
9227 // rsurface.batchtvector3f = R_FrameData_Alloc(batchnumvertices * sizeof(float[3]));
9228 // rsurface.batchtvector3f_vertexbuffer = NULL;
9229 // rsurface.batchtvector3f_bufferoffset = 0;
9230 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);
9234 // deform vertex array
9235 if(!R_TestQ3WaveFunc(deform->wavefunc, deform->waveparms))
9236 break; // if wavefunc is a nop, don't make a dynamic vertex array
9237 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
9238 VectorScale(deform->parms, scale, waveparms);
9239 // rsurface.batchvertex3f = R_FrameData_Store(batchnumvertices * sizeof(float[3]), rsurface.batchvertex3f);
9240 // rsurface.batchvertex3f_vertexbuffer = NULL;
9241 // rsurface.batchvertex3f_bufferoffset = 0;
9242 for (j = 0;j < batchnumvertices;j++)
9243 VectorAdd(rsurface.batchvertex3f + 3*j, waveparms, rsurface.batchvertex3f + 3*j);
9248 // generate texcoords based on the chosen texcoord source
9249 switch(rsurface.texture->tcgen.tcgen)
9252 case Q3TCGEN_TEXTURE:
9254 case Q3TCGEN_LIGHTMAP:
9255 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
9256 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
9257 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
9258 if (rsurface.batchtexcoordlightmap2f)
9259 memcpy(rsurface.batchtexcoordlightmap2f, rsurface.batchtexcoordtexture2f, batchnumvertices * sizeof(float[2]));
9261 case Q3TCGEN_VECTOR:
9262 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
9263 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
9264 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
9265 for (j = 0;j < batchnumvertices;j++)
9267 rsurface.batchtexcoordtexture2f[j*2+0] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->tcgen.parms);
9268 rsurface.batchtexcoordtexture2f[j*2+1] = DotProduct(rsurface.batchvertex3f + 3*j, rsurface.texture->tcgen.parms + 3);
9271 case Q3TCGEN_ENVIRONMENT:
9272 // make environment reflections using a spheremap
9273 rsurface.batchtexcoordtexture2f = (float *)R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
9274 rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
9275 rsurface.batchtexcoordtexture2f_bufferoffset = 0;
9276 for (j = 0;j < batchnumvertices;j++)
9278 // identical to Q3A's method, but executed in worldspace so
9279 // carried models can be shiny too
9281 float viewer[3], d, reflected[3], worldreflected[3];
9283 VectorSubtract(rsurface.localvieworigin, rsurface.batchvertex3f + 3*j, viewer);
9284 // VectorNormalize(viewer);
9286 d = DotProduct(rsurface.batchnormal3f + 3*j, viewer);
9288 reflected[0] = rsurface.batchnormal3f[j*3+0]*2*d - viewer[0];
9289 reflected[1] = rsurface.batchnormal3f[j*3+1]*2*d - viewer[1];
9290 reflected[2] = rsurface.batchnormal3f[j*3+2]*2*d - viewer[2];
9291 // note: this is proportinal to viewer, so we can normalize later
9293 Matrix4x4_Transform3x3(&rsurface.matrix, reflected, worldreflected);
9294 VectorNormalize(worldreflected);
9296 // note: this sphere map only uses world x and z!
9297 // so positive and negative y will LOOK THE SAME.
9298 rsurface.batchtexcoordtexture2f[j*2+0] = 0.5 + 0.5 * worldreflected[1];
9299 rsurface.batchtexcoordtexture2f[j*2+1] = 0.5 - 0.5 * worldreflected[2];
9303 // the only tcmod that needs software vertex processing is turbulent, so
9304 // check for it here and apply the changes if needed
9305 // and we only support that as the first one
9306 // (handling a mixture of turbulent and other tcmods would be problematic
9307 // without punting it entirely to a software path)
9308 if (rsurface.texture->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
9310 amplitude = rsurface.texture->tcmods[0].parms[1];
9311 animpos = rsurface.texture->tcmods[0].parms[2] + rsurface.shadertime * rsurface.texture->tcmods[0].parms[3];
9312 // rsurface.batchtexcoordtexture2f = R_FrameData_Alloc(batchnumvertices * sizeof(float[2]));
9313 // rsurface.batchtexcoordtexture2f_vertexbuffer = NULL;
9314 // rsurface.batchtexcoordtexture2f_bufferoffset = 0;
9315 for (j = 0;j < batchnumvertices;j++)
9317 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);
9318 rsurface.batchtexcoordtexture2f[j*2+1] += amplitude * sin(((rsurface.batchvertex3f[j*3+1] ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
9322 if (needsupdate & batchneed & (BATCHNEED_VERTEXMESH_VERTEX | BATCHNEED_VERTEXMESH_NORMAL | BATCHNEED_VERTEXMESH_VECTOR | BATCHNEED_VERTEXMESH_VERTEXCOLOR | BATCHNEED_VERTEXMESH_TEXCOORD | BATCHNEED_VERTEXMESH_LIGHTMAP))
9324 // convert the modified arrays to vertex structs
9325 // rsurface.batchvertexmesh = R_FrameData_Alloc(batchnumvertices * sizeof(r_vertexmesh_t));
9326 // rsurface.batchvertexmeshbuffer = NULL;
9327 if (batchneed & BATCHNEED_VERTEXMESH_VERTEX)
9328 for (j = 0, vertexmesh = rsurface.batchvertexmesh;j < batchnumvertices;j++, vertexmesh++)
9329 VectorCopy(rsurface.batchvertex3f + 3*j, vertexmesh->vertex3f);
9330 if (batchneed & BATCHNEED_VERTEXMESH_NORMAL)
9331 for (j = 0, vertexmesh = rsurface.batchvertexmesh;j < batchnumvertices;j++, vertexmesh++)
9332 VectorCopy(rsurface.batchnormal3f + 3*j, vertexmesh->normal3f);
9333 if (batchneed & BATCHNEED_VERTEXMESH_VECTOR)
9335 for (j = 0, vertexmesh = rsurface.batchvertexmesh;j < batchnumvertices;j++, vertexmesh++)
9337 VectorCopy(rsurface.batchsvector3f + 3*j, vertexmesh->svector3f);
9338 VectorCopy(rsurface.batchtvector3f + 3*j, vertexmesh->tvector3f);
9341 if ((batchneed & BATCHNEED_VERTEXMESH_VERTEXCOLOR) && rsurface.batchlightmapcolor4f)
9342 for (j = 0, vertexmesh = rsurface.batchvertexmesh;j < batchnumvertices;j++, vertexmesh++)
9343 Vector4Copy(rsurface.batchlightmapcolor4f + 4*j, vertexmesh->color4f);
9344 if (batchneed & BATCHNEED_VERTEXMESH_TEXCOORD)
9345 for (j = 0, vertexmesh = rsurface.batchvertexmesh;j < batchnumvertices;j++, vertexmesh++)
9346 Vector2Copy(rsurface.batchtexcoordtexture2f + 2*j, vertexmesh->texcoordtexture2f);
9347 if ((batchneed & BATCHNEED_VERTEXMESH_LIGHTMAP) && rsurface.batchtexcoordlightmap2f)
9348 for (j = 0, vertexmesh = rsurface.batchvertexmesh;j < batchnumvertices;j++, vertexmesh++)
9349 Vector2Copy(rsurface.batchtexcoordlightmap2f + 2*j, vertexmesh->texcoordlightmap2f);
9353 void RSurf_DrawBatch(void)
9355 // sometimes a zero triangle surface (usually a degenerate patch) makes it
9356 // through the pipeline, killing it earlier in the pipeline would have
9357 // per-surface overhead rather than per-batch overhead, so it's best to
9358 // reject it here, before it hits glDraw.
9359 if (rsurface.batchnumtriangles == 0)
9362 // batch debugging code
9363 if (r_test.integer && rsurface.entity == r_refdef.scene.worldentity && rsurface.batchvertex3f == r_refdef.scene.worldentity->model->surfmesh.data_vertex3f)
9369 e = rsurface.batchelement3i + rsurface.batchfirsttriangle*3;
9370 for (i = 0;i < rsurface.batchnumtriangles*3;i++)
9373 for (j = 0;j < rsurface.entity->model->num_surfaces;j++)
9375 if (c >= rsurface.modelsurfaces[j].num_firstvertex && c < (rsurface.modelsurfaces[j].num_firstvertex + rsurface.modelsurfaces[j].num_vertices))
9377 if (rsurface.modelsurfaces[j].texture != rsurface.texture)
9378 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);
9385 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);
9388 static int RSurf_FindWaterPlaneForSurface(const msurface_t *surface)
9390 // pick the closest matching water plane
9391 int planeindex, vertexindex, bestplaneindex = -1;
9395 r_waterstate_waterplane_t *p;
9396 qboolean prepared = false;
9398 for (planeindex = 0, p = r_fb.water.waterplanes;planeindex < r_fb.water.numwaterplanes;planeindex++, p++)
9400 if(p->camera_entity != rsurface.texture->camera_entity)
9405 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, 1, &surface);
9407 if(rsurface.batchnumvertices == 0)
9410 for (vertexindex = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3;vertexindex < rsurface.batchnumvertices;vertexindex++, v += 3)
9412 Matrix4x4_Transform(&rsurface.matrix, v, vert);
9413 d += fabs(PlaneDiff(vert, &p->plane));
9415 if (bestd > d || bestplaneindex < 0)
9418 bestplaneindex = planeindex;
9421 return bestplaneindex;
9422 // NOTE: this MAY return a totally unrelated water plane; we can ignore
9423 // this situation though, as it might be better to render single larger
9424 // batches with useless stuff (backface culled for example) than to
9425 // render multiple smaller batches
9428 static void RSurf_DrawBatch_GL11_MakeFullbrightLightmapColorArray(void)
9431 rsurface.passcolor4f = (float *)R_FrameData_Alloc(rsurface.batchnumvertices * sizeof(float[4]));
9432 rsurface.passcolor4f_vertexbuffer = 0;
9433 rsurface.passcolor4f_bufferoffset = 0;
9434 for (i = 0;i < rsurface.batchnumvertices;i++)
9435 Vector4Set(rsurface.passcolor4f + 4*i, 0.5f, 0.5f, 0.5f, 1.0f);
9438 static void RSurf_DrawBatch_GL11_ApplyFog(void)
9445 if (rsurface.passcolor4f)
9447 // generate color arrays
9448 c = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;
9449 rsurface.passcolor4f = (float *)R_FrameData_Alloc(rsurface.batchnumvertices * sizeof(float[4]));
9450 rsurface.passcolor4f_vertexbuffer = 0;
9451 rsurface.passcolor4f_bufferoffset = 0;
9452 for (i = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3, c = rsurface.passcolor4f + rsurface.batchfirstvertex * 4, c2 = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;i < rsurface.batchnumvertices;i++, v += 3, c += 4, c2 += 4)
9454 f = RSurf_FogVertex(v);
9463 rsurface.passcolor4f = (float *)R_FrameData_Alloc(rsurface.batchnumvertices * sizeof(float[4]));
9464 rsurface.passcolor4f_vertexbuffer = 0;
9465 rsurface.passcolor4f_bufferoffset = 0;
9466 for (i = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3, c2 = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;i < rsurface.batchnumvertices;i++, v += 3, c2 += 4)
9468 f = RSurf_FogVertex(v);
9477 static void RSurf_DrawBatch_GL11_ApplyFogToFinishedVertexColors(void)
9484 if (!rsurface.passcolor4f)
9486 c = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;
9487 rsurface.passcolor4f = (float *)R_FrameData_Alloc(rsurface.batchnumvertices * sizeof(float[4]));
9488 rsurface.passcolor4f_vertexbuffer = 0;
9489 rsurface.passcolor4f_bufferoffset = 0;
9490 for (i = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3, c2 = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;i < rsurface.batchnumvertices;i++, v += 3, c += 4, c2 += 4)
9492 f = RSurf_FogVertex(v);
9493 c2[0] = c[0] * f + r_refdef.fogcolor[0] * (1 - f);
9494 c2[1] = c[1] * f + r_refdef.fogcolor[1] * (1 - f);
9495 c2[2] = c[2] * f + r_refdef.fogcolor[2] * (1 - f);
9500 static void RSurf_DrawBatch_GL11_ApplyColor(float r, float g, float b, float a)
9505 if (!rsurface.passcolor4f)
9507 c = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;
9508 rsurface.passcolor4f = (float *)R_FrameData_Alloc(rsurface.batchnumvertices * sizeof(float[4]));
9509 rsurface.passcolor4f_vertexbuffer = 0;
9510 rsurface.passcolor4f_bufferoffset = 0;
9511 for (i = 0, c2 = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;i < rsurface.batchnumvertices;i++, c += 4, c2 += 4)
9520 static void RSurf_DrawBatch_GL11_ApplyAmbient(void)
9525 if (!rsurface.passcolor4f)
9527 c = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;
9528 rsurface.passcolor4f = (float *)R_FrameData_Alloc(rsurface.batchnumvertices * sizeof(float[4]));
9529 rsurface.passcolor4f_vertexbuffer = 0;
9530 rsurface.passcolor4f_bufferoffset = 0;
9531 for (i = 0, c2 = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;i < rsurface.batchnumvertices;i++, c += 4, c2 += 4)
9533 c2[0] = c[0] + r_refdef.scene.ambient;
9534 c2[1] = c[1] + r_refdef.scene.ambient;
9535 c2[2] = c[2] + r_refdef.scene.ambient;
9540 static void RSurf_DrawBatch_GL11_Lightmap(float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
9543 rsurface.passcolor4f = NULL;
9544 rsurface.passcolor4f_vertexbuffer = 0;
9545 rsurface.passcolor4f_bufferoffset = 0;
9546 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog();
9547 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(r, g, b, a);
9548 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, rsurface.passcolor4f_vertexbuffer, rsurface.passcolor4f_bufferoffset);
9549 GL_Color(r, g, b, a);
9550 R_Mesh_TexBind(0, rsurface.lightmaptexture);
9554 static void RSurf_DrawBatch_GL11_Unlit(float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
9556 // TODO: optimize applyfog && applycolor case
9557 // just apply fog if necessary, and tint the fog color array if necessary
9558 rsurface.passcolor4f = NULL;
9559 rsurface.passcolor4f_vertexbuffer = 0;
9560 rsurface.passcolor4f_bufferoffset = 0;
9561 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog();
9562 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(r, g, b, a);
9563 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, rsurface.passcolor4f_vertexbuffer, rsurface.passcolor4f_bufferoffset);
9564 GL_Color(r, g, b, a);
9568 static void RSurf_DrawBatch_GL11_VertexColor(float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
9571 rsurface.passcolor4f = rsurface.batchlightmapcolor4f;
9572 rsurface.passcolor4f_vertexbuffer = rsurface.batchlightmapcolor4f_vertexbuffer;
9573 rsurface.passcolor4f_bufferoffset = rsurface.batchlightmapcolor4f_bufferoffset;
9574 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog();
9575 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(r, g, b, a);
9576 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, rsurface.passcolor4f_vertexbuffer, rsurface.passcolor4f_bufferoffset);
9577 GL_Color(r, g, b, a);
9581 static void RSurf_DrawBatch_GL11_ClampColor(void)
9586 if (!rsurface.passcolor4f)
9588 for (i = 0, c1 = rsurface.passcolor4f + 4*rsurface.batchfirstvertex, c2 = rsurface.passcolor4f + 4*rsurface.batchfirstvertex;i < rsurface.batchnumvertices;i++, c1 += 4, c2 += 4)
9590 c2[0] = bound(0.0f, c1[0], 1.0f);
9591 c2[1] = bound(0.0f, c1[1], 1.0f);
9592 c2[2] = bound(0.0f, c1[2], 1.0f);
9593 c2[3] = bound(0.0f, c1[3], 1.0f);
9597 static void RSurf_DrawBatch_GL11_ApplyFakeLight(void)
9607 rsurface.passcolor4f = (float *)R_FrameData_Alloc(rsurface.batchnumvertices * sizeof(float[4]));
9608 rsurface.passcolor4f_vertexbuffer = 0;
9609 rsurface.passcolor4f_bufferoffset = 0;
9610 for (i = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3, n = rsurface.batchnormal3f + rsurface.batchfirstvertex * 3, c = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;i < rsurface.batchnumvertices;i++, v += 3, n += 3, c += 4)
9612 f = -DotProduct(r_refdef.view.forward, n);
9614 f = f * 0.85 + 0.15; // work around so stuff won't get black
9615 f *= r_refdef.lightmapintensity;
9616 Vector4Set(c, f, f, f, 1);
9620 static void RSurf_DrawBatch_GL11_FakeLight(float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
9622 RSurf_DrawBatch_GL11_ApplyFakeLight();
9623 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog();
9624 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(r, g, b, a);
9625 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, rsurface.passcolor4f_vertexbuffer, rsurface.passcolor4f_bufferoffset);
9626 GL_Color(r, g, b, a);
9630 static void RSurf_DrawBatch_GL11_ApplyVertexShade(float *r, float *g, float *b, float *a, qboolean *applycolor)
9638 vec3_t ambientcolor;
9639 vec3_t diffusecolor;
9643 VectorCopy(rsurface.modellight_lightdir, lightdir);
9644 f = 0.5f * r_refdef.lightmapintensity;
9645 ambientcolor[0] = rsurface.modellight_ambient[0] * *r * f;
9646 ambientcolor[1] = rsurface.modellight_ambient[1] * *g * f;
9647 ambientcolor[2] = rsurface.modellight_ambient[2] * *b * f;
9648 diffusecolor[0] = rsurface.modellight_diffuse[0] * *r * f;
9649 diffusecolor[1] = rsurface.modellight_diffuse[1] * *g * f;
9650 diffusecolor[2] = rsurface.modellight_diffuse[2] * *b * f;
9652 if (VectorLength2(diffusecolor) > 0)
9654 // q3-style directional shading
9655 rsurface.passcolor4f = (float *)R_FrameData_Alloc(rsurface.batchnumvertices * sizeof(float[4]));
9656 rsurface.passcolor4f_vertexbuffer = 0;
9657 rsurface.passcolor4f_bufferoffset = 0;
9658 for (i = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3, n = rsurface.batchnormal3f + rsurface.batchfirstvertex * 3, c = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;i < rsurface.batchnumvertices;i++, v += 3, n += 3, c += 4)
9660 if ((f = DotProduct(n, lightdir)) > 0)
9661 VectorMA(ambientcolor, f, diffusecolor, c);
9663 VectorCopy(ambientcolor, c);
9670 *applycolor = false;
9674 *r = ambientcolor[0];
9675 *g = ambientcolor[1];
9676 *b = ambientcolor[2];
9677 rsurface.passcolor4f = NULL;
9678 rsurface.passcolor4f_vertexbuffer = 0;
9679 rsurface.passcolor4f_bufferoffset = 0;
9683 static void RSurf_DrawBatch_GL11_VertexShade(float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
9685 RSurf_DrawBatch_GL11_ApplyVertexShade(&r, &g, &b, &a, &applycolor);
9686 if (applyfog) RSurf_DrawBatch_GL11_ApplyFog();
9687 if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(r, g, b, a);
9688 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, rsurface.passcolor4f_vertexbuffer, rsurface.passcolor4f_bufferoffset);
9689 GL_Color(r, g, b, a);
9693 static void RSurf_DrawBatch_GL11_MakeFogColor(float r, float g, float b, float a)
9701 rsurface.passcolor4f = (float *)R_FrameData_Alloc(rsurface.batchnumvertices * sizeof(float[4]));
9702 rsurface.passcolor4f_vertexbuffer = 0;
9703 rsurface.passcolor4f_bufferoffset = 0;
9705 for (i = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3, c = rsurface.passcolor4f + rsurface.batchfirstvertex * 4;i < rsurface.batchnumvertices;i++, v += 3, c += 4)
9707 f = 1 - RSurf_FogVertex(v);
9715 void RSurf_SetupDepthAndCulling(void)
9717 // submodels are biased to avoid z-fighting with world surfaces that they
9718 // may be exactly overlapping (avoids z-fighting artifacts on certain
9719 // doors and things in Quake maps)
9720 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
9721 GL_PolygonOffset(rsurface.basepolygonfactor + rsurface.texture->biaspolygonfactor, rsurface.basepolygonoffset + rsurface.texture->biaspolygonoffset);
9722 GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
9723 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
9726 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, const msurface_t **texturesurfacelist)
9728 // transparent sky would be ridiculous
9729 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
9731 R_SetupShader_Generic_NoTexture(false, false);
9732 skyrenderlater = true;
9733 RSurf_SetupDepthAndCulling();
9735 // LordHavoc: HalfLife maps have freaky skypolys so don't use
9736 // skymasking on them, and Quake3 never did sky masking (unlike
9737 // software Quake and software Quake2), so disable the sky masking
9738 // in Quake3 maps as it causes problems with q3map2 sky tricks,
9739 // and skymasking also looks very bad when noclipping outside the
9740 // level, so don't use it then either.
9741 if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_refdef.viewcache.world_novis && !r_trippy.integer)
9743 R_Mesh_ResetTextureState();
9744 if (skyrendermasked)
9746 R_SetupShader_DepthOrShadow(false, false);
9747 // depth-only (masking)
9748 GL_ColorMask(0,0,0,0);
9749 // just to make sure that braindead drivers don't draw
9750 // anything despite that colormask...
9751 GL_BlendFunc(GL_ZERO, GL_ONE);
9752 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
9753 if (rsurface.batchvertex3fbuffer)
9754 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3fbuffer);
9756 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer);
9760 R_SetupShader_Generic_NoTexture(false, false);
9762 GL_BlendFunc(GL_ONE, GL_ZERO);
9763 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
9764 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1);
9765 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
9768 if (skyrendermasked)
9769 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
9771 R_Mesh_ResetTextureState();
9772 GL_Color(1, 1, 1, 1);
9775 extern rtexture_t *r_shadow_prepasslightingdiffusetexture;
9776 extern rtexture_t *r_shadow_prepasslightingspeculartexture;
9777 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, const msurface_t **texturesurfacelist, qboolean writedepth, qboolean prepass)
9779 if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA)))
9783 // render screenspace normalmap to texture
9785 R_SetupShader_Surface(vec3_origin, (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT) != 0, 1, 1, rsurface.texture->specularscale, RSURFPASS_DEFERREDGEOMETRY, texturenumsurfaces, texturesurfacelist, NULL, false);
9790 // bind lightmap texture
9792 // water/refraction/reflection/camera surfaces have to be handled specially
9793 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA | MATERIALFLAG_REFLECTION)))
9795 int start, end, startplaneindex;
9796 for (start = 0;start < texturenumsurfaces;start = end)
9798 startplaneindex = RSurf_FindWaterPlaneForSurface(texturesurfacelist[start]);
9799 if(startplaneindex < 0)
9801 // this happens if the plane e.g. got backface culled and thus didn't get a water plane. We can just ignore this.
9802 // Con_Printf("No matching water plane for surface with material flags 0x%08x - PLEASE DEBUG THIS\n", rsurface.texture->currentmaterialflags);
9806 for (end = start + 1;end < texturenumsurfaces && startplaneindex == RSurf_FindWaterPlaneForSurface(texturesurfacelist[end]);end++)
9808 // now that we have a batch using the same planeindex, render it
9809 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA)))
9811 // render water or distortion background
9813 R_SetupShader_Surface(vec3_origin, (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT) != 0, 1, 1, rsurface.texture->specularscale, RSURFPASS_BACKGROUND, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false);
9815 // blend surface on top
9816 GL_DepthMask(false);
9817 R_SetupShader_Surface(vec3_origin, (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT) != 0, 1, 1, rsurface.texture->specularscale, RSURFPASS_BASE, end-start, texturesurfacelist + start, NULL, false);
9820 else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION))
9822 // render surface with reflection texture as input
9823 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
9824 R_SetupShader_Surface(vec3_origin, (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT) != 0, 1, 1, rsurface.texture->specularscale, RSURFPASS_BASE, end-start, texturesurfacelist + start, (void *)(r_fb.water.waterplanes + startplaneindex), false);
9831 // render surface batch normally
9832 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
9833 R_SetupShader_Surface(vec3_origin, (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT) != 0, 1, 1, rsurface.texture->specularscale, RSURFPASS_BASE, texturenumsurfaces, texturesurfacelist, NULL, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) != 0);
9837 static void R_DrawTextureSurfaceList_GL13(int texturenumsurfaces, const msurface_t **texturesurfacelist, qboolean writedepth)
9839 // OpenGL 1.3 path - anything not completely ancient
9840 qboolean applycolor;
9843 const texturelayer_t *layer;
9844 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | ((!rsurface.uselightmaptexture && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)) ? BATCHNEED_ARRAY_VERTEXCOLOR : 0) | BATCHNEED_ARRAY_TEXCOORD | (rsurface.modeltexcoordlightmap2f ? BATCHNEED_ARRAY_LIGHTMAP : 0) | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
9845 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
9847 for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
9850 int layertexrgbscale;
9851 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
9853 if (layerindex == 0)
9857 GL_AlphaTest(false);
9858 GL_DepthFunc(GL_EQUAL);
9861 GL_DepthMask(layer->depthmask && writedepth);
9862 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
9863 if (layer->color[0] > 2 || layer->color[1] > 2 || layer->color[2] > 2)
9865 layertexrgbscale = 4;
9866 VectorScale(layer->color, 0.25f, layercolor);
9868 else if (layer->color[0] > 1 || layer->color[1] > 1 || layer->color[2] > 1)
9870 layertexrgbscale = 2;
9871 VectorScale(layer->color, 0.5f, layercolor);
9875 layertexrgbscale = 1;
9876 VectorScale(layer->color, 1.0f, layercolor);
9878 layercolor[3] = layer->color[3];
9879 applycolor = layercolor[0] != 1 || layercolor[1] != 1 || layercolor[2] != 1 || layercolor[3] != 1;
9880 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, 0, 0);
9881 applyfog = r_refdef.fogenabled && (rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED);
9882 switch (layer->type)
9884 case TEXTURELAYERTYPE_LITTEXTURE:
9885 // single-pass lightmapped texture with 2x rgbscale
9886 R_Mesh_TexBind(0, r_texture_white);
9887 R_Mesh_TexMatrix(0, NULL);
9888 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
9889 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordlightmap2f, rsurface.batchtexcoordlightmap2f_vertexbuffer, rsurface.batchtexcoordlightmap2f_bufferoffset);
9890 R_Mesh_TexBind(1, layer->texture);
9891 R_Mesh_TexMatrix(1, &layer->texmatrix);
9892 R_Mesh_TexCombine(1, GL_MODULATE, GL_MODULATE, layertexrgbscale, 1);
9893 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
9894 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
9895 RSurf_DrawBatch_GL11_VertexShade(layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
9896 else if (FAKELIGHT_ENABLED)
9897 RSurf_DrawBatch_GL11_FakeLight(layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
9898 else if (rsurface.uselightmaptexture)
9899 RSurf_DrawBatch_GL11_Lightmap(layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
9901 RSurf_DrawBatch_GL11_VertexColor(layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
9903 case TEXTURELAYERTYPE_TEXTURE:
9904 // singletexture unlit texture with transparency support
9905 R_Mesh_TexBind(0, layer->texture);
9906 R_Mesh_TexMatrix(0, &layer->texmatrix);
9907 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, layertexrgbscale, 1);
9908 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
9909 R_Mesh_TexBind(1, 0);
9910 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, 0, 0);
9911 RSurf_DrawBatch_GL11_Unlit(layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
9913 case TEXTURELAYERTYPE_FOG:
9914 // singletexture fogging
9917 R_Mesh_TexBind(0, layer->texture);
9918 R_Mesh_TexMatrix(0, &layer->texmatrix);
9919 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, layertexrgbscale, 1);
9920 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
9924 R_Mesh_TexBind(0, 0);
9925 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, 0, 0);
9927 R_Mesh_TexBind(1, 0);
9928 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, 0, 0);
9929 // generate a color array for the fog pass
9930 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, 0, 0);
9931 RSurf_DrawBatch_GL11_MakeFogColor(layercolor[0], layercolor[1], layercolor[2], layercolor[3]);
9935 Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
9938 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
9940 GL_DepthFunc(GL_LEQUAL);
9941 GL_AlphaTest(false);
9945 static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, const msurface_t **texturesurfacelist, qboolean writedepth)
9947 // OpenGL 1.1 - crusty old voodoo path
9950 const texturelayer_t *layer;
9951 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | ((!rsurface.uselightmaptexture && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)) ? BATCHNEED_ARRAY_VERTEXCOLOR : 0) | BATCHNEED_ARRAY_TEXCOORD | (rsurface.modeltexcoordlightmap2f ? BATCHNEED_ARRAY_LIGHTMAP : 0) | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
9952 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
9954 for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
9956 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
9958 if (layerindex == 0)
9962 GL_AlphaTest(false);
9963 GL_DepthFunc(GL_EQUAL);
9966 GL_DepthMask(layer->depthmask && writedepth);
9967 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
9968 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, 0, 0);
9969 applyfog = r_refdef.fogenabled && (rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED);
9970 switch (layer->type)
9972 case TEXTURELAYERTYPE_LITTEXTURE:
9973 if (layer->blendfunc1 == GL_ONE && layer->blendfunc2 == GL_ZERO)
9975 // two-pass lit texture with 2x rgbscale
9976 // first the lightmap pass
9977 R_Mesh_TexBind(0, r_texture_white);
9978 R_Mesh_TexMatrix(0, NULL);
9979 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
9980 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordlightmap2f, rsurface.batchtexcoordlightmap2f_vertexbuffer, rsurface.batchtexcoordlightmap2f_bufferoffset);
9981 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
9982 RSurf_DrawBatch_GL11_VertexShade(1, 1, 1, 1, false, false);
9983 else if (FAKELIGHT_ENABLED)
9984 RSurf_DrawBatch_GL11_FakeLight(1, 1, 1, 1, false, false);
9985 else if (rsurface.uselightmaptexture)
9986 RSurf_DrawBatch_GL11_Lightmap(1, 1, 1, 1, false, false);
9988 RSurf_DrawBatch_GL11_VertexColor(1, 1, 1, 1, false, false);
9989 // then apply the texture to it
9990 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
9991 R_Mesh_TexBind(0, layer->texture);
9992 R_Mesh_TexMatrix(0, &layer->texmatrix);
9993 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
9994 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
9995 RSurf_DrawBatch_GL11_Unlit(layer->color[0] * 0.5f, layer->color[1] * 0.5f, layer->color[2] * 0.5f, layer->color[3], layer->color[0] != 2 || layer->color[1] != 2 || layer->color[2] != 2 || layer->color[3] != 1, false);
9999 // single pass vertex-lighting-only texture with 1x rgbscale and transparency support
10000 R_Mesh_TexBind(0, layer->texture);
10001 R_Mesh_TexMatrix(0, &layer->texmatrix);
10002 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
10003 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
10004 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
10005 RSurf_DrawBatch_GL11_VertexShade(layer->color[0], layer->color[1], layer->color[2], layer->color[3], layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
10007 RSurf_DrawBatch_GL11_VertexColor(layer->color[0], layer->color[1], layer->color[2], layer->color[3], layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
10010 case TEXTURELAYERTYPE_TEXTURE:
10011 // singletexture unlit texture with transparency support
10012 R_Mesh_TexBind(0, layer->texture);
10013 R_Mesh_TexMatrix(0, &layer->texmatrix);
10014 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
10015 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
10016 RSurf_DrawBatch_GL11_Unlit(layer->color[0], layer->color[1], layer->color[2], layer->color[3], layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
10018 case TEXTURELAYERTYPE_FOG:
10019 // singletexture fogging
10020 if (layer->texture)
10022 R_Mesh_TexBind(0, layer->texture);
10023 R_Mesh_TexMatrix(0, &layer->texmatrix);
10024 R_Mesh_TexCombine(0, GL_MODULATE, GL_MODULATE, 1, 1);
10025 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), rsurface.batchtexcoordtexture2f, rsurface.batchtexcoordtexture2f_vertexbuffer, rsurface.batchtexcoordtexture2f_bufferoffset);
10029 R_Mesh_TexBind(0, 0);
10030 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, 0, 0);
10032 // generate a color array for the fog pass
10033 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, 0, 0);
10034 RSurf_DrawBatch_GL11_MakeFogColor(layer->color[0], layer->color[1], layer->color[2], layer->color[3]);
10038 Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
10041 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
10043 GL_DepthFunc(GL_LEQUAL);
10044 GL_AlphaTest(false);
10048 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, const msurface_t **texturesurfacelist, qboolean writedepth)
10052 r_vertexgeneric_t *batchvertex;
10055 // R_Mesh_ResetTextureState();
10056 R_SetupShader_Generic_NoTexture(false, false);
10058 if(rsurface.texture && rsurface.texture->currentskinframe)
10060 memcpy(c, rsurface.texture->currentskinframe->avgcolor, sizeof(c));
10061 c[3] *= rsurface.texture->currentalpha;
10071 if (rsurface.texture->pantstexture || rsurface.texture->shirttexture)
10073 c[0] = 0.5 * (rsurface.colormap_pantscolor[0] * 0.3 + rsurface.colormap_shirtcolor[0] * 0.7);
10074 c[1] = 0.5 * (rsurface.colormap_pantscolor[1] * 0.3 + rsurface.colormap_shirtcolor[1] * 0.7);
10075 c[2] = 0.5 * (rsurface.colormap_pantscolor[2] * 0.3 + rsurface.colormap_shirtcolor[2] * 0.7);
10078 // brighten it up (as texture value 127 means "unlit")
10079 c[0] *= 2 * r_refdef.view.colorscale;
10080 c[1] *= 2 * r_refdef.view.colorscale;
10081 c[2] *= 2 * r_refdef.view.colorscale;
10083 if(rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERALPHA)
10084 c[3] *= r_wateralpha.value;
10086 if(rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHA && c[3] != 1)
10088 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
10089 GL_DepthMask(false);
10091 else if(rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD)
10093 GL_BlendFunc(GL_ONE, GL_ONE);
10094 GL_DepthMask(false);
10096 else if(rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
10098 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // can't do alpha test without texture, so let's blend instead
10099 GL_DepthMask(false);
10101 else if(rsurface.texture->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
10103 GL_BlendFunc(rsurface.texture->customblendfunc[0], rsurface.texture->customblendfunc[1]);
10104 GL_DepthMask(false);
10108 GL_BlendFunc(GL_ONE, GL_ZERO);
10109 GL_DepthMask(writedepth);
10112 if (r_showsurfaces.integer == 3)
10114 rsurface.passcolor4f = NULL;
10116 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
10118 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
10120 rsurface.passcolor4f = NULL;
10121 rsurface.passcolor4f_vertexbuffer = 0;
10122 rsurface.passcolor4f_bufferoffset = 0;
10124 else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
10126 qboolean applycolor = true;
10129 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
10131 r_refdef.lightmapintensity = 1;
10132 RSurf_DrawBatch_GL11_ApplyVertexShade(&one, &one, &one, &one, &applycolor);
10133 r_refdef.lightmapintensity = 0; // we're in showsurfaces, after all
10135 else if (FAKELIGHT_ENABLED)
10137 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
10139 r_refdef.lightmapintensity = r_fakelight_intensity.value;
10140 RSurf_DrawBatch_GL11_ApplyFakeLight();
10141 r_refdef.lightmapintensity = 0; // we're in showsurfaces, after all
10145 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_VERTEXCOLOR | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
10147 rsurface.passcolor4f = rsurface.batchlightmapcolor4f;
10148 rsurface.passcolor4f_vertexbuffer = rsurface.batchlightmapcolor4f_vertexbuffer;
10149 rsurface.passcolor4f_bufferoffset = rsurface.batchlightmapcolor4f_bufferoffset;
10152 if(!rsurface.passcolor4f)
10153 RSurf_DrawBatch_GL11_MakeFullbrightLightmapColorArray();
10155 RSurf_DrawBatch_GL11_ApplyAmbient();
10156 RSurf_DrawBatch_GL11_ApplyColor(c[0], c[1], c[2], c[3]);
10157 if(r_refdef.fogenabled)
10158 RSurf_DrawBatch_GL11_ApplyFogToFinishedVertexColors();
10159 RSurf_DrawBatch_GL11_ClampColor();
10161 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.passcolor4f, NULL);
10162 R_SetupShader_Generic_NoTexture(false, false);
10165 else if (!r_refdef.view.showdebug)
10167 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
10168 batchvertex = R_Mesh_PrepareVertices_Generic_Lock(rsurface.batchnumvertices);
10169 for (j = 0, vi = rsurface.batchfirstvertex;j < rsurface.batchnumvertices;j++, vi++)
10171 VectorCopy(rsurface.batchvertex3f + 3*vi, batchvertex[vi].vertex3f);
10172 Vector4Set(batchvertex[vi].color4f, 0, 0, 0, 1);
10174 R_Mesh_PrepareVertices_Generic_Unlock();
10177 else if (r_showsurfaces.integer == 4)
10179 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
10180 batchvertex = R_Mesh_PrepareVertices_Generic_Lock(rsurface.batchnumvertices);
10181 for (j = 0, vi = rsurface.batchfirstvertex;j < rsurface.batchnumvertices;j++, vi++)
10183 unsigned char c = (vi << 3) * (1.0f / 256.0f);
10184 VectorCopy(rsurface.batchvertex3f + 3*vi, batchvertex[vi].vertex3f);
10185 Vector4Set(batchvertex[vi].color4f, c, c, c, 1);
10187 R_Mesh_PrepareVertices_Generic_Unlock();
10190 else if (r_showsurfaces.integer == 2)
10193 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
10194 batchvertex = R_Mesh_PrepareVertices_Generic_Lock(3*rsurface.batchnumtriangles);
10195 for (j = 0, e = rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle;j < rsurface.batchnumtriangles;j++, e += 3)
10197 unsigned char c = ((j + rsurface.batchfirsttriangle) << 3) * (1.0f / 256.0f);
10198 VectorCopy(rsurface.batchvertex3f + 3*e[0], batchvertex[j*3+0].vertex3f);
10199 VectorCopy(rsurface.batchvertex3f + 3*e[1], batchvertex[j*3+1].vertex3f);
10200 VectorCopy(rsurface.batchvertex3f + 3*e[2], batchvertex[j*3+2].vertex3f);
10201 Vector4Set(batchvertex[j*3+0].color4f, c, c, c, 1);
10202 Vector4Set(batchvertex[j*3+1].color4f, c, c, c, 1);
10203 Vector4Set(batchvertex[j*3+2].color4f, c, c, c, 1);
10205 R_Mesh_PrepareVertices_Generic_Unlock();
10206 R_Mesh_Draw(0, rsurface.batchnumtriangles*3, 0, rsurface.batchnumtriangles, NULL, NULL, 0, NULL, NULL, 0);
10210 int texturesurfaceindex;
10212 const msurface_t *surface;
10213 float surfacecolor4f[4];
10214 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
10215 batchvertex = R_Mesh_PrepareVertices_Generic_Lock(rsurface.batchfirstvertex + rsurface.batchnumvertices);
10217 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
10219 surface = texturesurfacelist[texturesurfaceindex];
10220 k = (int)(((size_t)surface) / sizeof(msurface_t));
10221 Vector4Set(surfacecolor4f, (k & 0xF) * (1.0f / 16.0f), (k & 0xF0) * (1.0f / 256.0f), (k & 0xF00) * (1.0f / 4096.0f), 1);
10222 for (j = 0;j < surface->num_vertices;j++)
10224 VectorCopy(rsurface.batchvertex3f + 3*vi, batchvertex[vi].vertex3f);
10225 Vector4Copy(surfacecolor4f, batchvertex[vi].color4f);
10229 R_Mesh_PrepareVertices_Generic_Unlock();
10234 static void R_DrawWorldTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qboolean writedepth, qboolean prepass)
10237 RSurf_SetupDepthAndCulling();
10238 if (r_showsurfaces.integer)
10240 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist, writedepth);
10243 switch (vid.renderpath)
10245 case RENDERPATH_GL20:
10246 case RENDERPATH_D3D9:
10247 case RENDERPATH_D3D10:
10248 case RENDERPATH_D3D11:
10249 case RENDERPATH_SOFT:
10250 case RENDERPATH_GLES2:
10251 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist, writedepth, prepass);
10253 case RENDERPATH_GL13:
10254 case RENDERPATH_GLES1:
10255 R_DrawTextureSurfaceList_GL13(texturenumsurfaces, texturesurfacelist, writedepth);
10257 case RENDERPATH_GL11:
10258 R_DrawTextureSurfaceList_GL11(texturenumsurfaces, texturesurfacelist, writedepth);
10264 static void R_DrawModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qboolean writedepth, qboolean prepass)
10267 RSurf_SetupDepthAndCulling();
10268 if (r_showsurfaces.integer)
10270 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist, writedepth);
10273 switch (vid.renderpath)
10275 case RENDERPATH_GL20:
10276 case RENDERPATH_D3D9:
10277 case RENDERPATH_D3D10:
10278 case RENDERPATH_D3D11:
10279 case RENDERPATH_SOFT:
10280 case RENDERPATH_GLES2:
10281 R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist, writedepth, prepass);
10283 case RENDERPATH_GL13:
10284 case RENDERPATH_GLES1:
10285 R_DrawTextureSurfaceList_GL13(texturenumsurfaces, texturesurfacelist, writedepth);
10287 case RENDERPATH_GL11:
10288 R_DrawTextureSurfaceList_GL11(texturenumsurfaces, texturesurfacelist, writedepth);
10294 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
10297 int texturenumsurfaces, endsurface;
10298 texture_t *texture;
10299 const msurface_t *surface;
10300 const msurface_t *texturesurfacelist[MESHQUEUE_TRANSPARENT_BATCHSIZE];
10302 // if the model is static it doesn't matter what value we give for
10303 // wantnormals and wanttangents, so this logic uses only rules applicable
10304 // to a model, knowing that they are meaningless otherwise
10305 if (ent == r_refdef.scene.worldentity)
10306 RSurf_ActiveWorldEntity();
10307 else if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
10308 RSurf_ActiveModelEntity(ent, false, false, false);
10311 switch (vid.renderpath)
10313 case RENDERPATH_GL20:
10314 case RENDERPATH_D3D9:
10315 case RENDERPATH_D3D10:
10316 case RENDERPATH_D3D11:
10317 case RENDERPATH_SOFT:
10318 case RENDERPATH_GLES2:
10319 RSurf_ActiveModelEntity(ent, true, true, false);
10321 case RENDERPATH_GL11:
10322 case RENDERPATH_GL13:
10323 case RENDERPATH_GLES1:
10324 RSurf_ActiveModelEntity(ent, true, false, false);
10329 if (r_transparentdepthmasking.integer)
10331 qboolean setup = false;
10332 for (i = 0;i < numsurfaces;i = j)
10335 surface = rsurface.modelsurfaces + surfacelist[i];
10336 texture = surface->texture;
10337 rsurface.texture = R_GetCurrentTexture(texture);
10338 rsurface.lightmaptexture = NULL;
10339 rsurface.deluxemaptexture = NULL;
10340 rsurface.uselightmaptexture = false;
10341 // scan ahead until we find a different texture
10342 endsurface = min(i + 1024, numsurfaces);
10343 texturenumsurfaces = 0;
10344 texturesurfacelist[texturenumsurfaces++] = surface;
10345 for (;j < endsurface;j++)
10347 surface = rsurface.modelsurfaces + surfacelist[j];
10348 if (texture != surface->texture)
10350 texturesurfacelist[texturenumsurfaces++] = surface;
10352 if (!(rsurface.texture->currentmaterialflags & MATERIALFLAG_TRANSDEPTH))
10354 // render the range of surfaces as depth
10358 GL_ColorMask(0,0,0,0);
10360 GL_DepthTest(true);
10361 GL_BlendFunc(GL_ONE, GL_ZERO);
10362 GL_DepthMask(true);
10363 // R_Mesh_ResetTextureState();
10364 R_SetupShader_DepthOrShadow(false, false);
10366 RSurf_SetupDepthAndCulling();
10367 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX, texturenumsurfaces, texturesurfacelist);
10368 if (rsurface.batchvertex3fbuffer)
10369 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3fbuffer);
10371 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer);
10375 GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
10378 for (i = 0;i < numsurfaces;i = j)
10381 surface = rsurface.modelsurfaces + surfacelist[i];
10382 texture = surface->texture;
10383 rsurface.texture = R_GetCurrentTexture(texture);
10384 // scan ahead until we find a different texture
10385 endsurface = min(i + MESHQUEUE_TRANSPARENT_BATCHSIZE, numsurfaces);
10386 texturenumsurfaces = 0;
10387 texturesurfacelist[texturenumsurfaces++] = surface;
10388 if(FAKELIGHT_ENABLED)
10390 rsurface.lightmaptexture = NULL;
10391 rsurface.deluxemaptexture = NULL;
10392 rsurface.uselightmaptexture = false;
10393 for (;j < endsurface;j++)
10395 surface = rsurface.modelsurfaces + surfacelist[j];
10396 if (texture != surface->texture)
10398 texturesurfacelist[texturenumsurfaces++] = surface;
10403 rsurface.lightmaptexture = surface->lightmaptexture;
10404 rsurface.deluxemaptexture = surface->deluxemaptexture;
10405 rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
10406 for (;j < endsurface;j++)
10408 surface = rsurface.modelsurfaces + surfacelist[j];
10409 if (texture != surface->texture || rsurface.lightmaptexture != surface->lightmaptexture)
10411 texturesurfacelist[texturenumsurfaces++] = surface;
10414 // render the range of surfaces
10415 if (ent == r_refdef.scene.worldentity)
10416 R_DrawWorldTextureSurfaceList(texturenumsurfaces, texturesurfacelist, false, false);
10418 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, false, false);
10420 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
10423 static void R_ProcessTransparentTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist)
10425 // transparent surfaces get pushed off into the transparent queue
10426 int surfacelistindex;
10427 const msurface_t *surface;
10428 vec3_t tempcenter, center;
10429 for (surfacelistindex = 0;surfacelistindex < texturenumsurfaces;surfacelistindex++)
10431 surface = texturesurfacelist[surfacelistindex];
10432 if (r_transparent_sortsurfacesbynearest.integer)
10434 tempcenter[0] = bound(surface->mins[0], rsurface.localvieworigin[0], surface->maxs[0]);
10435 tempcenter[1] = bound(surface->mins[1], rsurface.localvieworigin[1], surface->maxs[1]);
10436 tempcenter[2] = bound(surface->mins[2], rsurface.localvieworigin[2], surface->maxs[2]);
10440 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
10441 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
10442 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
10444 Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
10445 if (rsurface.entity->transparent_offset) // transparent offset
10447 center[0] += r_refdef.view.forward[0]*rsurface.entity->transparent_offset;
10448 center[1] += r_refdef.view.forward[1]*rsurface.entity->transparent_offset;
10449 center[2] += r_refdef.view.forward[2]*rsurface.entity->transparent_offset;
10451 R_MeshQueue_AddTransparent((rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) ? MESHQUEUE_SORT_HUD : ((rsurface.entity->flags & RENDER_WORLDOBJECT) ? MESHQUEUE_SORT_SKY : MESHQUEUE_SORT_DISTANCE), center, R_DrawSurface_TransparentCallback, rsurface.entity, surface - rsurface.modelsurfaces, rsurface.rtlight);
10455 static void R_DrawTextureSurfaceList_DepthOnly(int texturenumsurfaces, const msurface_t **texturesurfacelist)
10457 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
10459 if (r_fb.water.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
10461 RSurf_SetupDepthAndCulling();
10462 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX, texturenumsurfaces, texturesurfacelist);
10463 if (rsurface.batchvertex3fbuffer)
10464 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3fbuffer);
10466 R_Mesh_PrepareVertices_Vertex3f(rsurface.batchnumvertices, rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer);
10470 static void R_ProcessWorldTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qboolean writedepth, qboolean depthonly, qboolean prepass)
10474 R_DrawTextureSurfaceList_DepthOnly(texturenumsurfaces, texturesurfacelist);
10477 if (!rsurface.texture->currentnumlayers)
10479 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
10480 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
10482 R_DrawWorldTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass);
10484 else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) && (!r_showsurfaces.integer || r_showsurfaces.integer == 3))
10485 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
10486 else if (!rsurface.texture->currentnumlayers)
10488 else if (((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED) || (r_showsurfaces.integer == 3 && (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST))))
10490 // in the deferred case, transparent surfaces were queued during prepass
10491 if (!r_shadow_usingdeferredprepass)
10492 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
10496 // the alphatest check is to make sure we write depth for anything we skipped on the depth-only pass earlier
10497 R_DrawWorldTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST), prepass);
10502 static void R_QueueWorldSurfaceList(int numsurfaces, const msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly, qboolean prepass)
10505 texture_t *texture;
10506 R_FrameData_SetMark();
10507 // break the surface list down into batches by texture and use of lightmapping
10508 for (i = 0;i < numsurfaces;i = j)
10511 // texture is the base texture pointer, rsurface.texture is the
10512 // current frame/skin the texture is directing us to use (for example
10513 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
10514 // use skin 1 instead)
10515 texture = surfacelist[i]->texture;
10516 rsurface.texture = R_GetCurrentTexture(texture);
10517 if (!(rsurface.texture->currentmaterialflags & flagsmask) || (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW))
10519 // if this texture is not the kind we want, skip ahead to the next one
10520 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
10524 if(FAKELIGHT_ENABLED || depthonly || prepass)
10526 rsurface.lightmaptexture = NULL;
10527 rsurface.deluxemaptexture = NULL;
10528 rsurface.uselightmaptexture = false;
10529 // simply scan ahead until we find a different texture or lightmap state
10530 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
10535 rsurface.lightmaptexture = surfacelist[i]->lightmaptexture;
10536 rsurface.deluxemaptexture = surfacelist[i]->deluxemaptexture;
10537 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
10538 // simply scan ahead until we find a different texture or lightmap state
10539 for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.lightmaptexture == surfacelist[j]->lightmaptexture;j++)
10542 // render the range of surfaces
10543 R_ProcessWorldTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly, prepass);
10545 R_FrameData_ReturnToMark();
10548 static void R_ProcessModelTextureSurfaceList(int texturenumsurfaces, const msurface_t **texturesurfacelist, qboolean writedepth, qboolean depthonly, qboolean prepass)
10552 R_DrawTextureSurfaceList_DepthOnly(texturenumsurfaces, texturesurfacelist);
10555 if (!rsurface.texture->currentnumlayers)
10557 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
10558 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
10560 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth, prepass);
10562 else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY) && (!r_showsurfaces.integer || r_showsurfaces.integer == 3))
10563 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
10564 else if (!rsurface.texture->currentnumlayers)
10566 else if (((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED) || (r_showsurfaces.integer == 3 && (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST))))
10568 // in the deferred case, transparent surfaces were queued during prepass
10569 if (!r_shadow_usingdeferredprepass)
10570 R_ProcessTransparentTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
10574 // the alphatest check is to make sure we write depth for anything we skipped on the depth-only pass earlier
10575 R_DrawModelTextureSurfaceList(texturenumsurfaces, texturesurfacelist, writedepth || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST), prepass);
10580 static void R_QueueModelSurfaceList(entity_render_t *ent, int numsurfaces, const msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly, qboolean prepass)
10583 texture_t *texture;
10584 R_FrameData_SetMark();
10585 // break the surface list down into batches by texture and use of lightmapping
10586 for (i = 0;i < numsurfaces;i = j)
10589 // texture is the base texture pointer, rsurface.texture is the
10590 // current frame/skin the texture is directing us to use (for example
10591 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
10592 // use skin 1 instead)
10593 texture = surfacelist[i]->texture;
10594 rsurface.texture = R_GetCurrentTexture(texture);
10595 if (!(rsurface.texture->currentmaterialflags & flagsmask) || (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW))
10597 // if this texture is not the kind we want, skip ahead to the next one
10598 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
10602 if(FAKELIGHT_ENABLED || depthonly || prepass)
10604 rsurface.lightmaptexture = NULL;
10605 rsurface.deluxemaptexture = NULL;
10606 rsurface.uselightmaptexture = false;
10607 // simply scan ahead until we find a different texture or lightmap state
10608 for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
10613 rsurface.lightmaptexture = surfacelist[i]->lightmaptexture;
10614 rsurface.deluxemaptexture = surfacelist[i]->deluxemaptexture;
10615 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
10616 // simply scan ahead until we find a different texture or lightmap state
10617 for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.lightmaptexture == surfacelist[j]->lightmaptexture;j++)
10620 // render the range of surfaces
10621 R_ProcessModelTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly, prepass);
10623 R_FrameData_ReturnToMark();
10626 float locboxvertex3f[6*4*3] =
10628 1,0,1, 1,0,0, 1,1,0, 1,1,1,
10629 0,1,1, 0,1,0, 0,0,0, 0,0,1,
10630 1,1,1, 1,1,0, 0,1,0, 0,1,1,
10631 0,0,1, 0,0,0, 1,0,0, 1,0,1,
10632 0,0,1, 1,0,1, 1,1,1, 0,1,1,
10633 1,0,0, 0,0,0, 0,1,0, 1,1,0
10636 unsigned short locboxelements[6*2*3] =
10641 12,13,14, 12,14,15,
10642 16,17,18, 16,18,19,
10646 static void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
10649 cl_locnode_t *loc = (cl_locnode_t *)ent;
10651 float vertex3f[6*4*3];
10653 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
10654 GL_DepthMask(false);
10655 GL_DepthRange(0, 1);
10656 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
10657 GL_DepthTest(true);
10658 GL_CullFace(GL_NONE);
10659 R_EntityMatrix(&identitymatrix);
10661 // R_Mesh_ResetTextureState();
10663 i = surfacelist[0];
10664 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_refdef.view.colorscale,
10665 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_refdef.view.colorscale,
10666 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_refdef.view.colorscale,
10667 surfacelist[0] < 0 ? 0.5f : 0.125f);
10669 if (VectorCompare(loc->mins, loc->maxs))
10671 VectorSet(size, 2, 2, 2);
10672 VectorMA(loc->mins, -0.5f, size, mins);
10676 VectorCopy(loc->mins, mins);
10677 VectorSubtract(loc->maxs, loc->mins, size);
10680 for (i = 0;i < 6*4*3;)
10681 for (j = 0;j < 3;j++, i++)
10682 vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
10684 R_Mesh_PrepareVertices_Generic_Arrays(6*4, vertex3f, NULL, NULL);
10685 R_SetupShader_Generic_NoTexture(false, false);
10686 R_Mesh_Draw(0, 6*4, 0, 6*2, NULL, NULL, 0, locboxelements, NULL, 0);
10689 void R_DrawLocs(void)
10692 cl_locnode_t *loc, *nearestloc;
10694 nearestloc = CL_Locs_FindNearest(cl.movement_origin);
10695 for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
10697 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
10698 R_MeshQueue_AddTransparent(MESHQUEUE_SORT_DISTANCE, center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
10702 void R_DecalSystem_Reset(decalsystem_t *decalsystem)
10704 if (decalsystem->decals)
10705 Mem_Free(decalsystem->decals);
10706 memset(decalsystem, 0, sizeof(*decalsystem));
10709 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, int decalsequence)
10712 tridecal_t *decals;
10715 // expand or initialize the system
10716 if (decalsystem->maxdecals <= decalsystem->numdecals)
10718 decalsystem_t old = *decalsystem;
10719 qboolean useshortelements;
10720 decalsystem->maxdecals = max(16, decalsystem->maxdecals * 2);
10721 useshortelements = decalsystem->maxdecals * 3 <= 65536;
10722 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)));
10723 decalsystem->color4f = (float *)(decalsystem->decals + decalsystem->maxdecals);
10724 decalsystem->texcoord2f = (float *)(decalsystem->color4f + decalsystem->maxdecals*12);
10725 decalsystem->vertex3f = (float *)(decalsystem->texcoord2f + decalsystem->maxdecals*6);
10726 decalsystem->element3i = (int *)(decalsystem->vertex3f + decalsystem->maxdecals*9);
10727 decalsystem->element3s = (useshortelements ? ((unsigned short *)(decalsystem->element3i + decalsystem->maxdecals*3)) : NULL);
10728 if (decalsystem->numdecals)
10729 memcpy(decalsystem->decals, old.decals, decalsystem->numdecals * sizeof(tridecal_t));
10731 Mem_Free(old.decals);
10732 for (i = 0;i < decalsystem->maxdecals*3;i++)
10733 decalsystem->element3i[i] = i;
10734 if (useshortelements)
10735 for (i = 0;i < decalsystem->maxdecals*3;i++)
10736 decalsystem->element3s[i] = i;
10739 // grab a decal and search for another free slot for the next one
10740 decals = decalsystem->decals;
10741 decal = decalsystem->decals + (i = decalsystem->freedecal++);
10742 for (i = decalsystem->freedecal;i < decalsystem->numdecals && decals[i].color4f[0][3];i++)
10744 decalsystem->freedecal = i;
10745 if (decalsystem->numdecals <= i)
10746 decalsystem->numdecals = i + 1;
10748 // initialize the decal
10750 decal->triangleindex = triangleindex;
10751 decal->surfaceindex = surfaceindex;
10752 decal->decalsequence = decalsequence;
10753 decal->color4f[0][0] = c0[0];
10754 decal->color4f[0][1] = c0[1];
10755 decal->color4f[0][2] = c0[2];
10756 decal->color4f[0][3] = 1;
10757 decal->color4f[1][0] = c1[0];
10758 decal->color4f[1][1] = c1[1];
10759 decal->color4f[1][2] = c1[2];
10760 decal->color4f[1][3] = 1;
10761 decal->color4f[2][0] = c2[0];
10762 decal->color4f[2][1] = c2[1];
10763 decal->color4f[2][2] = c2[2];
10764 decal->color4f[2][3] = 1;
10765 decal->vertex3f[0][0] = v0[0];
10766 decal->vertex3f[0][1] = v0[1];
10767 decal->vertex3f[0][2] = v0[2];
10768 decal->vertex3f[1][0] = v1[0];
10769 decal->vertex3f[1][1] = v1[1];
10770 decal->vertex3f[1][2] = v1[2];
10771 decal->vertex3f[2][0] = v2[0];
10772 decal->vertex3f[2][1] = v2[1];
10773 decal->vertex3f[2][2] = v2[2];
10774 decal->texcoord2f[0][0] = t0[0];
10775 decal->texcoord2f[0][1] = t0[1];
10776 decal->texcoord2f[1][0] = t1[0];
10777 decal->texcoord2f[1][1] = t1[1];
10778 decal->texcoord2f[2][0] = t2[0];
10779 decal->texcoord2f[2][1] = t2[1];
10780 TriangleNormal(v0, v1, v2, decal->plane);
10781 VectorNormalize(decal->plane);
10782 decal->plane[3] = DotProduct(v0, decal->plane);
10785 extern cvar_t cl_decals_bias;
10786 extern cvar_t cl_decals_models;
10787 extern cvar_t cl_decals_newsystem_intensitymultiplier;
10788 // baseparms, parms, temps
10789 static void R_DecalSystem_SplatTriangle(decalsystem_t *decalsystem, float r, float g, float b, float a, float s1, float t1, float s2, float t2, int decalsequence, qboolean dynamic, float (*planes)[4], matrix4x4_t *projection, int triangleindex, int surfaceindex)
10794 const float *vertex3f;
10795 const float *normal3f;
10797 float points[2][9][3];
10804 e = rsurface.modelelement3i + 3*triangleindex;
10806 vertex3f = rsurface.modelvertex3f;
10807 normal3f = rsurface.modelnormal3f;
10811 for (cornerindex = 0;cornerindex < 3;cornerindex++)
10813 index = 3*e[cornerindex];
10814 VectorMA(vertex3f + index, cl_decals_bias.value, normal3f + index, v[cornerindex]);
10819 for (cornerindex = 0;cornerindex < 3;cornerindex++)
10821 index = 3*e[cornerindex];
10822 VectorCopy(vertex3f + index, v[cornerindex]);
10827 //TriangleNormal(v[0], v[1], v[2], normal);
10828 //if (DotProduct(normal, localnormal) < 0.0f)
10830 // clip by each of the box planes formed from the projection matrix
10831 // if anything survives, we emit the decal
10832 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]);
10835 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]);
10838 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]);
10841 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]);
10844 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]);
10847 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]);
10850 // some part of the triangle survived, so we have to accept it...
10853 // dynamic always uses the original triangle
10855 for (cornerindex = 0;cornerindex < 3;cornerindex++)
10857 index = 3*e[cornerindex];
10858 VectorCopy(vertex3f + index, v[cornerindex]);
10861 for (cornerindex = 0;cornerindex < numpoints;cornerindex++)
10863 // convert vertex positions to texcoords
10864 Matrix4x4_Transform(projection, v[cornerindex], temp);
10865 tc[cornerindex][0] = (temp[1]+1.0f)*0.5f * (s2-s1) + s1;
10866 tc[cornerindex][1] = (temp[2]+1.0f)*0.5f * (t2-t1) + t1;
10867 // calculate distance fade from the projection origin
10868 f = a * (1.0f-fabs(temp[0])) * cl_decals_newsystem_intensitymultiplier.value;
10869 f = bound(0.0f, f, 1.0f);
10870 c[cornerindex][0] = r * f;
10871 c[cornerindex][1] = g * f;
10872 c[cornerindex][2] = b * f;
10873 c[cornerindex][3] = 1.0f;
10874 //VectorMA(v[cornerindex], cl_decals_bias.value, localnormal, v[cornerindex]);
10877 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);
10879 for (cornerindex = 0;cornerindex < numpoints-2;cornerindex++)
10880 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);
10882 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, int decalsequence)
10884 matrix4x4_t projection;
10885 decalsystem_t *decalsystem;
10888 const msurface_t *surface;
10889 const msurface_t *surfaces;
10890 const int *surfacelist;
10891 const texture_t *texture;
10893 int numsurfacelist;
10894 int surfacelistindex;
10897 float localorigin[3];
10898 float localnormal[3];
10899 float localmins[3];
10900 float localmaxs[3];
10903 float planes[6][4];
10906 int bih_triangles_count;
10907 int bih_triangles[256];
10908 int bih_surfaces[256];
10910 decalsystem = &ent->decalsystem;
10911 model = ent->model;
10912 if (!model || !ent->allowdecals || ent->alpha < 1 || (ent->flags & (RENDER_ADDITIVE | RENDER_NODEPTHTEST)))
10914 R_DecalSystem_Reset(&ent->decalsystem);
10918 if (!model->brush.data_leafs && !cl_decals_models.integer)
10920 if (decalsystem->model)
10921 R_DecalSystem_Reset(decalsystem);
10925 if (decalsystem->model != model)
10926 R_DecalSystem_Reset(decalsystem);
10927 decalsystem->model = model;
10929 RSurf_ActiveModelEntity(ent, true, false, false);
10931 Matrix4x4_Transform(&rsurface.inversematrix, worldorigin, localorigin);
10932 Matrix4x4_Transform3x3(&rsurface.inversematrix, worldnormal, localnormal);
10933 VectorNormalize(localnormal);
10934 localsize = worldsize*rsurface.inversematrixscale;
10935 localmins[0] = localorigin[0] - localsize;
10936 localmins[1] = localorigin[1] - localsize;
10937 localmins[2] = localorigin[2] - localsize;
10938 localmaxs[0] = localorigin[0] + localsize;
10939 localmaxs[1] = localorigin[1] + localsize;
10940 localmaxs[2] = localorigin[2] + localsize;
10942 //VectorCopy(localnormal, planes[4]);
10943 //VectorVectors(planes[4], planes[2], planes[0]);
10944 AnglesFromVectors(angles, localnormal, NULL, false);
10945 AngleVectors(angles, planes[0], planes[2], planes[4]);
10946 VectorNegate(planes[0], planes[1]);
10947 VectorNegate(planes[2], planes[3]);
10948 VectorNegate(planes[4], planes[5]);
10949 planes[0][3] = DotProduct(planes[0], localorigin) - localsize;
10950 planes[1][3] = DotProduct(planes[1], localorigin) - localsize;
10951 planes[2][3] = DotProduct(planes[2], localorigin) - localsize;
10952 planes[3][3] = DotProduct(planes[3], localorigin) - localsize;
10953 planes[4][3] = DotProduct(planes[4], localorigin) - localsize;
10954 planes[5][3] = DotProduct(planes[5], localorigin) - localsize;
10959 matrix4x4_t forwardprojection;
10960 Matrix4x4_CreateFromQuakeEntity(&forwardprojection, localorigin[0], localorigin[1], localorigin[2], angles[0], angles[1], angles[2], localsize);
10961 Matrix4x4_Invert_Simple(&projection, &forwardprojection);
10966 float projectionvector[4][3];
10967 VectorScale(planes[0], ilocalsize, projectionvector[0]);
10968 VectorScale(planes[2], ilocalsize, projectionvector[1]);
10969 VectorScale(planes[4], ilocalsize, projectionvector[2]);
10970 projectionvector[0][0] = planes[0][0] * ilocalsize;
10971 projectionvector[0][1] = planes[1][0] * ilocalsize;
10972 projectionvector[0][2] = planes[2][0] * ilocalsize;
10973 projectionvector[1][0] = planes[0][1] * ilocalsize;
10974 projectionvector[1][1] = planes[1][1] * ilocalsize;
10975 projectionvector[1][2] = planes[2][1] * ilocalsize;
10976 projectionvector[2][0] = planes[0][2] * ilocalsize;
10977 projectionvector[2][1] = planes[1][2] * ilocalsize;
10978 projectionvector[2][2] = planes[2][2] * ilocalsize;
10979 projectionvector[3][0] = -(localorigin[0]*projectionvector[0][0]+localorigin[1]*projectionvector[1][0]+localorigin[2]*projectionvector[2][0]);
10980 projectionvector[3][1] = -(localorigin[0]*projectionvector[0][1]+localorigin[1]*projectionvector[1][1]+localorigin[2]*projectionvector[2][1]);
10981 projectionvector[3][2] = -(localorigin[0]*projectionvector[0][2]+localorigin[1]*projectionvector[1][2]+localorigin[2]*projectionvector[2][2]);
10982 Matrix4x4_FromVectors(&projection, projectionvector[0], projectionvector[1], projectionvector[2], projectionvector[3]);
10986 dynamic = model->surfmesh.isanimated;
10987 numsurfacelist = model->nummodelsurfaces;
10988 surfacelist = model->sortedmodelsurfaces;
10989 surfaces = model->data_surfaces;
10992 bih_triangles_count = -1;
10995 if(model->render_bih.numleafs)
10996 bih = &model->render_bih;
10997 else if(model->collision_bih.numleafs)
10998 bih = &model->collision_bih;
11001 bih_triangles_count = BIH_GetTriangleListForBox(bih, sizeof(bih_triangles) / sizeof(*bih_triangles), bih_triangles, bih_surfaces, localmins, localmaxs);
11002 if(bih_triangles_count == 0)
11004 if(bih_triangles_count > (int) (sizeof(bih_triangles) / sizeof(*bih_triangles))) // hit too many, likely bad anyway
11006 if(bih_triangles_count > 0)
11008 for (triangleindex = 0; triangleindex < bih_triangles_count; ++triangleindex)
11010 surfaceindex = bih_surfaces[triangleindex];
11011 surface = surfaces + surfaceindex;
11012 texture = surface->texture;
11013 if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
11015 if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
11017 R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, bih_triangles[triangleindex], surfaceindex);
11022 for (surfacelistindex = 0;surfacelistindex < numsurfacelist;surfacelistindex++)
11024 surfaceindex = surfacelist[surfacelistindex];
11025 surface = surfaces + surfaceindex;
11026 // check cull box first because it rejects more than any other check
11027 if (!dynamic && !BoxesOverlap(surface->mins, surface->maxs, localmins, localmaxs))
11029 // skip transparent surfaces
11030 texture = surface->texture;
11031 if (texture->currentmaterialflags & (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_SKY | MATERIALFLAG_SHORTDEPTHRANGE | MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
11033 if (texture->surfaceflags & Q3SURFACEFLAG_NOMARKS)
11035 numtriangles = surface->num_triangles;
11036 for (triangleindex = 0; triangleindex < numtriangles; triangleindex++)
11037 R_DecalSystem_SplatTriangle(decalsystem, r, g, b, a, s1, t1, s2, t2, decalsequence, dynamic, planes, &projection, triangleindex + surface->num_firsttriangle, surfaceindex);
11042 // do not call this outside of rendering code - use R_DecalSystem_SplatEntities instead
11043 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, int decalsequence)
11045 int renderentityindex;
11046 float worldmins[3];
11047 float worldmaxs[3];
11048 entity_render_t *ent;
11050 if (!cl_decals_newsystem.integer)
11053 worldmins[0] = worldorigin[0] - worldsize;
11054 worldmins[1] = worldorigin[1] - worldsize;
11055 worldmins[2] = worldorigin[2] - worldsize;
11056 worldmaxs[0] = worldorigin[0] + worldsize;
11057 worldmaxs[1] = worldorigin[1] + worldsize;
11058 worldmaxs[2] = worldorigin[2] + worldsize;
11060 R_DecalSystem_SplatEntity(r_refdef.scene.worldentity, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
11062 for (renderentityindex = 0;renderentityindex < r_refdef.scene.numentities;renderentityindex++)
11064 ent = r_refdef.scene.entities[renderentityindex];
11065 if (!BoxesOverlap(ent->mins, ent->maxs, worldmins, worldmaxs))
11068 R_DecalSystem_SplatEntity(ent, worldorigin, worldnormal, r, g, b, a, s1, t1, s2, t2, worldsize, decalsequence);
11072 typedef struct r_decalsystem_splatqueue_s
11074 vec3_t worldorigin;
11075 vec3_t worldnormal;
11081 r_decalsystem_splatqueue_t;
11083 int r_decalsystem_numqueued = 0;
11084 r_decalsystem_splatqueue_t r_decalsystem_queue[MAX_DECALSYSTEM_QUEUE];
11086 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)
11088 r_decalsystem_splatqueue_t *queue;
11090 if (!cl_decals_newsystem.integer || r_decalsystem_numqueued == MAX_DECALSYSTEM_QUEUE)
11093 queue = &r_decalsystem_queue[r_decalsystem_numqueued++];
11094 VectorCopy(worldorigin, queue->worldorigin);
11095 VectorCopy(worldnormal, queue->worldnormal);
11096 Vector4Set(queue->color, r, g, b, a);
11097 Vector4Set(queue->tcrange, s1, t1, s2, t2);
11098 queue->worldsize = worldsize;
11099 queue->decalsequence = cl.decalsequence++;
11102 static void R_DecalSystem_ApplySplatEntitiesQueue(void)
11105 r_decalsystem_splatqueue_t *queue;
11107 for (i = 0, queue = r_decalsystem_queue;i < r_decalsystem_numqueued;i++, queue++)
11108 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);
11109 r_decalsystem_numqueued = 0;
11112 extern cvar_t cl_decals_max;
11113 static void R_DrawModelDecals_FadeEntity(entity_render_t *ent)
11116 decalsystem_t *decalsystem = &ent->decalsystem;
11123 if (!decalsystem->numdecals)
11126 if (r_showsurfaces.integer)
11129 if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
11131 R_DecalSystem_Reset(decalsystem);
11135 killsequence = cl.decalsequence - max(1, cl_decals_max.integer);
11136 lifetime = cl_decals_time.value + cl_decals_fadetime.value;
11138 if (decalsystem->lastupdatetime)
11139 frametime = (r_refdef.scene.time - decalsystem->lastupdatetime);
11142 decalsystem->lastupdatetime = r_refdef.scene.time;
11143 decal = decalsystem->decals;
11144 numdecals = decalsystem->numdecals;
11146 for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
11148 if (decal->color4f[0][3])
11150 decal->lived += frametime;
11151 if (killsequence - decal->decalsequence > 0 || decal->lived >= lifetime)
11153 memset(decal, 0, sizeof(*decal));
11154 if (decalsystem->freedecal > i)
11155 decalsystem->freedecal = i;
11159 decal = decalsystem->decals;
11160 while (numdecals > 0 && !decal[numdecals-1].color4f[0][3])
11163 // collapse the array by shuffling the tail decals into the gaps
11166 while (decalsystem->freedecal < numdecals && decal[decalsystem->freedecal].color4f[0][3])
11167 decalsystem->freedecal++;
11168 if (decalsystem->freedecal == numdecals)
11170 decal[decalsystem->freedecal] = decal[--numdecals];
11173 decalsystem->numdecals = numdecals;
11175 if (numdecals <= 0)
11177 // if there are no decals left, reset decalsystem
11178 R_DecalSystem_Reset(decalsystem);
11182 extern skinframe_t *decalskinframe;
11183 static void R_DrawModelDecals_Entity(entity_render_t *ent)
11186 decalsystem_t *decalsystem = &ent->decalsystem;
11195 const unsigned char *surfacevisible = ent == r_refdef.scene.worldentity ? r_refdef.viewcache.world_surfacevisible : NULL;
11198 numdecals = decalsystem->numdecals;
11202 if (r_showsurfaces.integer)
11205 if (ent->model != decalsystem->model || ent->alpha < 1 || (ent->flags & RENDER_ADDITIVE))
11207 R_DecalSystem_Reset(decalsystem);
11211 // if the model is static it doesn't matter what value we give for
11212 // wantnormals and wanttangents, so this logic uses only rules applicable
11213 // to a model, knowing that they are meaningless otherwise
11214 if (ent == r_refdef.scene.worldentity)
11215 RSurf_ActiveWorldEntity();
11217 RSurf_ActiveModelEntity(ent, false, false, false);
11219 decalsystem->lastupdatetime = r_refdef.scene.time;
11220 decal = decalsystem->decals;
11222 faderate = 1.0f / max(0.001f, cl_decals_fadetime.value);
11224 // update vertex positions for animated models
11225 v3f = decalsystem->vertex3f;
11226 c4f = decalsystem->color4f;
11227 t2f = decalsystem->texcoord2f;
11228 for (i = 0, decal = decalsystem->decals;i < numdecals;i++, decal++)
11230 if (!decal->color4f[0][3])
11233 if (surfacevisible && !surfacevisible[decal->surfaceindex])
11237 if (decal->triangleindex < 0 && DotProduct(r_refdef.view.origin, decal->plane) < decal->plane[3])
11240 // update color values for fading decals
11241 if (decal->lived >= cl_decals_time.value)
11242 alpha = 1 - faderate * (decal->lived - cl_decals_time.value);
11246 c4f[ 0] = decal->color4f[0][0] * alpha;
11247 c4f[ 1] = decal->color4f[0][1] * alpha;
11248 c4f[ 2] = decal->color4f[0][2] * alpha;
11250 c4f[ 4] = decal->color4f[1][0] * alpha;
11251 c4f[ 5] = decal->color4f[1][1] * alpha;
11252 c4f[ 6] = decal->color4f[1][2] * alpha;
11254 c4f[ 8] = decal->color4f[2][0] * alpha;
11255 c4f[ 9] = decal->color4f[2][1] * alpha;
11256 c4f[10] = decal->color4f[2][2] * alpha;
11259 t2f[0] = decal->texcoord2f[0][0];
11260 t2f[1] = decal->texcoord2f[0][1];
11261 t2f[2] = decal->texcoord2f[1][0];
11262 t2f[3] = decal->texcoord2f[1][1];
11263 t2f[4] = decal->texcoord2f[2][0];
11264 t2f[5] = decal->texcoord2f[2][1];
11266 // update vertex positions for animated models
11267 if (decal->triangleindex >= 0 && decal->triangleindex < rsurface.modelnumtriangles)
11269 e = rsurface.modelelement3i + 3*decal->triangleindex;
11270 VectorCopy(rsurface.modelvertex3f + 3*e[0], v3f);
11271 VectorCopy(rsurface.modelvertex3f + 3*e[1], v3f + 3);
11272 VectorCopy(rsurface.modelvertex3f + 3*e[2], v3f + 6);
11276 VectorCopy(decal->vertex3f[0], v3f);
11277 VectorCopy(decal->vertex3f[1], v3f + 3);
11278 VectorCopy(decal->vertex3f[2], v3f + 6);
11281 if (r_refdef.fogenabled)
11283 alpha = RSurf_FogVertex(v3f);
11284 VectorScale(c4f, alpha, c4f);
11285 alpha = RSurf_FogVertex(v3f + 3);
11286 VectorScale(c4f + 4, alpha, c4f + 4);
11287 alpha = RSurf_FogVertex(v3f + 6);
11288 VectorScale(c4f + 8, alpha, c4f + 8);
11299 r_refdef.stats.drawndecals += numtris;
11301 // now render the decals all at once
11302 // (this assumes they all use one particle font texture!)
11303 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);
11304 // R_Mesh_ResetTextureState();
11305 R_Mesh_PrepareVertices_Generic_Arrays(numtris * 3, decalsystem->vertex3f, decalsystem->color4f, decalsystem->texcoord2f);
11306 GL_DepthMask(false);
11307 GL_DepthRange(0, 1);
11308 GL_PolygonOffset(rsurface.basepolygonfactor + r_polygonoffset_decals_factor.value, rsurface.basepolygonoffset + r_polygonoffset_decals_offset.value);
11309 GL_DepthTest(true);
11310 GL_CullFace(GL_NONE);
11311 GL_BlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
11312 R_SetupShader_Generic(decalskinframe->base, NULL, GL_MODULATE, 1, false, false, false);
11313 R_Mesh_Draw(0, numtris * 3, 0, numtris, decalsystem->element3i, NULL, 0, decalsystem->element3s, NULL, 0);
11317 static void R_DrawModelDecals(void)
11321 // fade faster when there are too many decals
11322 numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
11323 for (i = 0;i < r_refdef.scene.numentities;i++)
11324 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
11326 R_DrawModelDecals_FadeEntity(r_refdef.scene.worldentity);
11327 for (i = 0;i < r_refdef.scene.numentities;i++)
11328 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
11329 R_DrawModelDecals_FadeEntity(r_refdef.scene.entities[i]);
11331 R_DecalSystem_ApplySplatEntitiesQueue();
11333 numdecals = r_refdef.scene.worldentity->decalsystem.numdecals;
11334 for (i = 0;i < r_refdef.scene.numentities;i++)
11335 numdecals += r_refdef.scene.entities[i]->decalsystem.numdecals;
11337 r_refdef.stats.totaldecals += numdecals;
11339 if (r_showsurfaces.integer)
11342 R_DrawModelDecals_Entity(r_refdef.scene.worldentity);
11344 for (i = 0;i < r_refdef.scene.numentities;i++)
11346 if (!r_refdef.viewcache.entityvisible[i])
11348 if (r_refdef.scene.entities[i]->decalsystem.numdecals)
11349 R_DrawModelDecals_Entity(r_refdef.scene.entities[i]);
11353 extern cvar_t mod_collision_bih;
11354 static void R_DrawDebugModel(void)
11356 entity_render_t *ent = rsurface.entity;
11357 int i, j, k, l, flagsmask;
11358 const msurface_t *surface;
11359 dp_model_t *model = ent->model;
11362 if (!sv.active && !cls.demoplayback && ent != r_refdef.scene.worldentity)
11365 if (r_showoverdraw.value > 0)
11367 float c = r_refdef.view.colorscale * r_showoverdraw.value * 0.125f;
11368 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
11369 R_SetupShader_Generic_NoTexture(false, false);
11370 GL_DepthTest(false);
11371 GL_DepthMask(false);
11372 GL_DepthRange(0, 1);
11373 GL_BlendFunc(GL_ONE, GL_ONE);
11374 for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
11376 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
11378 rsurface.texture = R_GetCurrentTexture(surface->texture);
11379 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
11381 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_NOGAPS, 1, &surface);
11382 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_refdef.view.cullface_back);
11383 if (!rsurface.texture->currentlayers->depthmask)
11384 GL_Color(c, 0, 0, 1.0f);
11385 else if (ent == r_refdef.scene.worldentity)
11386 GL_Color(c, c, c, 1.0f);
11388 GL_Color(0, c, 0, 1.0f);
11389 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
11393 rsurface.texture = NULL;
11396 flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WALL;
11398 // R_Mesh_ResetTextureState();
11399 R_SetupShader_Generic_NoTexture(false, false);
11400 GL_DepthRange(0, 1);
11401 GL_DepthTest(!r_showdisabledepthtest.integer);
11402 GL_DepthMask(false);
11403 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
11405 if (r_showcollisionbrushes.value > 0 && model->collision_bih.numleafs)
11409 qboolean cullbox = ent == r_refdef.scene.worldentity;
11410 const q3mbrush_t *brush;
11411 const bih_t *bih = &model->collision_bih;
11412 const bih_leaf_t *bihleaf;
11413 float vertex3f[3][3];
11414 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
11416 for (bihleafindex = 0, bihleaf = bih->leafs;bihleafindex < bih->numleafs;bihleafindex++, bihleaf++)
11418 if (cullbox && R_CullBox(bihleaf->mins, bihleaf->maxs))
11420 switch (bihleaf->type)
11423 brush = model->brush.data_brushes + bihleaf->itemindex;
11424 if (brush->colbrushf && brush->colbrushf->numtriangles)
11426 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);
11427 R_Mesh_PrepareVertices_Generic_Arrays(brush->colbrushf->numpoints, brush->colbrushf->points->v, NULL, NULL);
11428 R_Mesh_Draw(0, brush->colbrushf->numpoints, 0, brush->colbrushf->numtriangles, brush->colbrushf->elements, NULL, 0, NULL, NULL, 0);
11431 case BIH_COLLISIONTRIANGLE:
11432 triangleindex = bihleaf->itemindex;
11433 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+0], vertex3f[0]);
11434 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+1], vertex3f[1]);
11435 VectorCopy(model->brush.data_collisionvertex3f + 3*model->brush.data_collisionelement3i[triangleindex*3+2], vertex3f[2]);
11436 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);
11437 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
11438 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
11440 case BIH_RENDERTRIANGLE:
11441 triangleindex = bihleaf->itemindex;
11442 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+0], vertex3f[0]);
11443 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+1], vertex3f[1]);
11444 VectorCopy(model->surfmesh.data_vertex3f + 3*model->surfmesh.data_element3i[triangleindex*3+2], vertex3f[2]);
11445 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);
11446 R_Mesh_PrepareVertices_Generic_Arrays(3, vertex3f[0], NULL, NULL);
11447 R_Mesh_Draw(0, 3, 0, 1, polygonelement3i, NULL, 0, polygonelement3s, NULL, 0);
11453 GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
11456 if (r_showtris.integer && qglPolygonMode)
11458 if (r_showdisabledepthtest.integer)
11460 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
11461 GL_DepthMask(false);
11465 GL_BlendFunc(GL_ONE, GL_ZERO);
11466 GL_DepthMask(true);
11468 qglPolygonMode(GL_FRONT_AND_BACK, GL_LINE);CHECKGLERROR
11469 for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
11471 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
11473 rsurface.texture = R_GetCurrentTexture(surface->texture);
11474 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
11476 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
11477 if (!rsurface.texture->currentlayers->depthmask)
11478 GL_Color(r_refdef.view.colorscale, 0, 0, r_showtris.value);
11479 else if (ent == r_refdef.scene.worldentity)
11480 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, r_showtris.value);
11482 GL_Color(0, r_refdef.view.colorscale, 0, r_showtris.value);
11483 R_Mesh_PrepareVertices_Generic_Arrays(rsurface.batchnumvertices, rsurface.batchvertex3f, NULL, NULL);
11487 qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);CHECKGLERROR
11488 rsurface.texture = NULL;
11491 if (r_shownormals.value != 0 && qglBegin)
11493 if (r_showdisabledepthtest.integer)
11495 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
11496 GL_DepthMask(false);
11500 GL_BlendFunc(GL_ONE, GL_ZERO);
11501 GL_DepthMask(true);
11503 for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
11505 if (ent == r_refdef.scene.worldentity && !r_refdef.viewcache.world_surfacevisible[j])
11507 rsurface.texture = R_GetCurrentTexture(surface->texture);
11508 if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
11510 RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR | BATCHNEED_NOGAPS, 1, &surface);
11511 qglBegin(GL_LINES);
11512 if (r_shownormals.value < 0 && rsurface.batchnormal3f)
11514 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
11516 VectorCopy(rsurface.batchvertex3f + l * 3, v);
11517 GL_Color(0, 0, r_refdef.view.colorscale, 1);
11518 qglVertex3f(v[0], v[1], v[2]);
11519 VectorMA(v, -r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
11520 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
11521 qglVertex3f(v[0], v[1], v[2]);
11524 if (r_shownormals.value > 0 && rsurface.batchsvector3f)
11526 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
11528 VectorCopy(rsurface.batchvertex3f + l * 3, v);
11529 GL_Color(r_refdef.view.colorscale, 0, 0, 1);
11530 qglVertex3f(v[0], v[1], v[2]);
11531 VectorMA(v, r_shownormals.value, rsurface.batchsvector3f + l * 3, v);
11532 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
11533 qglVertex3f(v[0], v[1], v[2]);
11536 if (r_shownormals.value > 0 && rsurface.batchtvector3f)
11538 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
11540 VectorCopy(rsurface.batchvertex3f + l * 3, v);
11541 GL_Color(0, r_refdef.view.colorscale, 0, 1);
11542 qglVertex3f(v[0], v[1], v[2]);
11543 VectorMA(v, r_shownormals.value, rsurface.batchtvector3f + l * 3, v);
11544 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
11545 qglVertex3f(v[0], v[1], v[2]);
11548 if (r_shownormals.value > 0 && rsurface.batchnormal3f)
11550 for (k = 0, l = rsurface.batchfirstvertex;k < rsurface.batchnumvertices;k++, l++)
11552 VectorCopy(rsurface.batchvertex3f + l * 3, v);
11553 GL_Color(0, 0, r_refdef.view.colorscale, 1);
11554 qglVertex3f(v[0], v[1], v[2]);
11555 VectorMA(v, r_shownormals.value, rsurface.batchnormal3f + l * 3, v);
11556 GL_Color(r_refdef.view.colorscale, r_refdef.view.colorscale, r_refdef.view.colorscale, 1);
11557 qglVertex3f(v[0], v[1], v[2]);
11564 rsurface.texture = NULL;
11569 int r_maxsurfacelist = 0;
11570 const msurface_t **r_surfacelist = NULL;
11571 void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean debug, qboolean prepass)
11573 int i, j, endj, flagsmask;
11574 dp_model_t *model = r_refdef.scene.worldmodel;
11575 msurface_t *surfaces;
11576 unsigned char *update;
11577 int numsurfacelist = 0;
11581 if (r_maxsurfacelist < model->num_surfaces)
11583 r_maxsurfacelist = model->num_surfaces;
11585 Mem_Free((msurface_t**)r_surfacelist);
11586 r_surfacelist = (const msurface_t **) Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
11589 RSurf_ActiveWorldEntity();
11591 surfaces = model->data_surfaces;
11592 update = model->brushq1.lightmapupdateflags;
11594 // update light styles on this submodel
11595 if (!skysurfaces && !depthonly && !prepass && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
11597 model_brush_lightstyleinfo_t *style;
11598 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
11600 if (style->value != r_refdef.scene.lightstylevalue[style->style])
11602 int *list = style->surfacelist;
11603 style->value = r_refdef.scene.lightstylevalue[style->style];
11604 for (j = 0;j < style->numsurfaces;j++)
11605 update[list[j]] = true;
11610 flagsmask = skysurfaces ? MATERIALFLAG_SKY : MATERIALFLAG_WALL;
11614 R_DrawDebugModel();
11615 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
11619 rsurface.lightmaptexture = NULL;
11620 rsurface.deluxemaptexture = NULL;
11621 rsurface.uselightmaptexture = false;
11622 rsurface.texture = NULL;
11623 rsurface.rtlight = NULL;
11624 numsurfacelist = 0;
11625 // add visible surfaces to draw list
11626 for (i = 0;i < model->nummodelsurfaces;i++)
11628 j = model->sortedmodelsurfaces[i];
11629 if (r_refdef.viewcache.world_surfacevisible[j])
11630 r_surfacelist[numsurfacelist++] = surfaces + j;
11632 // update lightmaps if needed
11633 if (model->brushq1.firstrender)
11635 model->brushq1.firstrender = false;
11636 for (j = model->firstmodelsurface, endj = model->firstmodelsurface + model->nummodelsurfaces;j < endj;j++)
11638 R_BuildLightMap(r_refdef.scene.worldentity, surfaces + j);
11642 for (j = model->firstmodelsurface, endj = model->firstmodelsurface + model->nummodelsurfaces;j < endj;j++)
11643 if (r_refdef.viewcache.world_surfacevisible[j])
11645 R_BuildLightMap(r_refdef.scene.worldentity, surfaces + j);
11647 // don't do anything if there were no surfaces
11648 if (!numsurfacelist)
11650 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
11653 R_QueueWorldSurfaceList(numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly, prepass);
11655 // add to stats if desired
11656 if (r_speeds.integer && !skysurfaces && !depthonly)
11658 r_refdef.stats.world_surfaces += numsurfacelist;
11659 for (j = 0;j < numsurfacelist;j++)
11660 r_refdef.stats.world_triangles += r_surfacelist[j]->num_triangles;
11663 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
11666 void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean debug, qboolean prepass)
11668 int i, j, endj, flagsmask;
11669 dp_model_t *model = ent->model;
11670 msurface_t *surfaces;
11671 unsigned char *update;
11672 int numsurfacelist = 0;
11676 if (r_maxsurfacelist < model->num_surfaces)
11678 r_maxsurfacelist = model->num_surfaces;
11680 Mem_Free((msurface_t **)r_surfacelist);
11681 r_surfacelist = (const msurface_t **) Mem_Alloc(r_main_mempool, r_maxsurfacelist * sizeof(*r_surfacelist));
11684 // if the model is static it doesn't matter what value we give for
11685 // wantnormals and wanttangents, so this logic uses only rules applicable
11686 // to a model, knowing that they are meaningless otherwise
11687 if (ent == r_refdef.scene.worldentity)
11688 RSurf_ActiveWorldEntity();
11689 else if (r_showsurfaces.integer && r_showsurfaces.integer != 3)
11690 RSurf_ActiveModelEntity(ent, false, false, false);
11692 RSurf_ActiveModelEntity(ent, true, true, true);
11693 else if (depthonly)
11695 switch (vid.renderpath)
11697 case RENDERPATH_GL20:
11698 case RENDERPATH_D3D9:
11699 case RENDERPATH_D3D10:
11700 case RENDERPATH_D3D11:
11701 case RENDERPATH_SOFT:
11702 case RENDERPATH_GLES2:
11703 RSurf_ActiveModelEntity(ent, model->wantnormals, model->wanttangents, false);
11705 case RENDERPATH_GL11:
11706 case RENDERPATH_GL13:
11707 case RENDERPATH_GLES1:
11708 RSurf_ActiveModelEntity(ent, model->wantnormals, false, false);
11714 switch (vid.renderpath)
11716 case RENDERPATH_GL20:
11717 case RENDERPATH_D3D9:
11718 case RENDERPATH_D3D10:
11719 case RENDERPATH_D3D11:
11720 case RENDERPATH_SOFT:
11721 case RENDERPATH_GLES2:
11722 RSurf_ActiveModelEntity(ent, true, true, false);
11724 case RENDERPATH_GL11:
11725 case RENDERPATH_GL13:
11726 case RENDERPATH_GLES1:
11727 RSurf_ActiveModelEntity(ent, true, false, false);
11732 surfaces = model->data_surfaces;
11733 update = model->brushq1.lightmapupdateflags;
11735 // update light styles
11736 if (!skysurfaces && !depthonly && !prepass && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
11738 model_brush_lightstyleinfo_t *style;
11739 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
11741 if (style->value != r_refdef.scene.lightstylevalue[style->style])
11743 int *list = style->surfacelist;
11744 style->value = r_refdef.scene.lightstylevalue[style->style];
11745 for (j = 0;j < style->numsurfaces;j++)
11746 update[list[j]] = true;
11751 flagsmask = skysurfaces ? MATERIALFLAG_SKY : MATERIALFLAG_WALL;
11755 R_DrawDebugModel();
11756 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
11760 rsurface.lightmaptexture = NULL;
11761 rsurface.deluxemaptexture = NULL;
11762 rsurface.uselightmaptexture = false;
11763 rsurface.texture = NULL;
11764 rsurface.rtlight = NULL;
11765 numsurfacelist = 0;
11766 // add visible surfaces to draw list
11767 for (i = 0;i < model->nummodelsurfaces;i++)
11768 r_surfacelist[numsurfacelist++] = surfaces + model->sortedmodelsurfaces[i];
11769 // don't do anything if there were no surfaces
11770 if (!numsurfacelist)
11772 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
11775 // update lightmaps if needed
11779 for (j = model->firstmodelsurface, endj = model->firstmodelsurface + model->nummodelsurfaces;j < endj;j++)
11784 R_BuildLightMap(ent, surfaces + j);
11789 for (j = model->firstmodelsurface, endj = model->firstmodelsurface + model->nummodelsurfaces;j < endj;j++)
11791 R_BuildLightMap(ent, surfaces + j);
11792 R_QueueModelSurfaceList(ent, numsurfacelist, r_surfacelist, flagsmask, writedepth, depthonly, prepass);
11794 // add to stats if desired
11795 if (r_speeds.integer && !skysurfaces && !depthonly)
11797 r_refdef.stats.entities_surfaces += numsurfacelist;
11798 for (j = 0;j < numsurfacelist;j++)
11799 r_refdef.stats.entities_triangles += r_surfacelist[j]->num_triangles;
11802 rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
11805 void R_DrawCustomSurface(skinframe_t *skinframe, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qboolean writedepth, qboolean prepass)
11807 static texture_t texture;
11808 static msurface_t surface;
11809 const msurface_t *surfacelist = &surface;
11811 // fake enough texture and surface state to render this geometry
11813 texture.update_lastrenderframe = -1; // regenerate this texture
11814 texture.basematerialflags = materialflags | MATERIALFLAG_CUSTOMSURFACE | MATERIALFLAG_WALL;
11815 texture.currentskinframe = skinframe;
11816 texture.currenttexmatrix = *texmatrix; // requires MATERIALFLAG_CUSTOMSURFACE
11817 texture.offsetmapping = OFFSETMAPPING_OFF;
11818 texture.offsetscale = 1;
11819 texture.specularscalemod = 1;
11820 texture.specularpowermod = 1;
11822 surface.texture = &texture;
11823 surface.num_triangles = numtriangles;
11824 surface.num_firsttriangle = firsttriangle;
11825 surface.num_vertices = numvertices;
11826 surface.num_firstvertex = firstvertex;
11829 rsurface.texture = R_GetCurrentTexture(surface.texture);
11830 rsurface.lightmaptexture = NULL;
11831 rsurface.deluxemaptexture = NULL;
11832 rsurface.uselightmaptexture = false;
11833 R_DrawModelTextureSurfaceList(1, &surfacelist, writedepth, prepass);
11836 void R_DrawCustomSurface_Texture(texture_t *texture, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qboolean writedepth, qboolean prepass)
11838 static msurface_t surface;
11839 const msurface_t *surfacelist = &surface;
11841 // fake enough texture and surface state to render this geometry
11842 surface.texture = texture;
11843 surface.num_triangles = numtriangles;
11844 surface.num_firsttriangle = firsttriangle;
11845 surface.num_vertices = numvertices;
11846 surface.num_firstvertex = firstvertex;
11849 rsurface.texture = R_GetCurrentTexture(surface.texture);
11850 rsurface.lightmaptexture = NULL;
11851 rsurface.deluxemaptexture = NULL;
11852 rsurface.uselightmaptexture = false;
11853 R_DrawModelTextureSurfaceList(1, &surfacelist, writedepth, prepass);