+static void R_BuildFogTexture(void)
+{
+ int x, b;
+ double r, alpha;
+#define FOGWIDTH 64
+ unsigned char data1[FOGWIDTH][4];
+ unsigned char data2[FOGWIDTH][4];
+ r = (-1.0/256.0) * (FOGWIDTH * FOGWIDTH);
+ for (x = 0;x < FOGWIDTH;x++)
+ {
+ alpha = exp(r / ((double)x*(double)x));
+ if (x == FOGWIDTH - 1)
+ alpha = 1;
+ b = (int)(256.0 * alpha);
+ b = bound(0, b, 255);
+ data1[x][0] = 255 - b;
+ data1[x][1] = 255 - b;
+ data1[x][2] = 255 - b;
+ data1[x][3] = 255;
+ data2[x][0] = b;
+ data2[x][1] = b;
+ data2[x][2] = b;
+ data2[x][3] = 255;
+ }
+ r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
+ r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
+}
+
+static const char *builtinshaderstring =
+"// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
+"// written by Forest 'LordHavoc' Hale\n"
+"\n"
+"// common definitions between vertex shader and fragment shader:\n"
+"\n"
+"// use half floats if available for math performance\n"
+"#ifdef GEFORCEFX\n"
+"#define myhalf half\n"
+"#define myhvec2 hvec2\n"
+"#define myhvec3 hvec3\n"
+"#define myhvec4 hvec4\n"
+"#else\n"
+"#define myhalf float\n"
+"#define myhvec2 vec2\n"
+"#define myhvec3 vec3\n"
+"#define myhvec4 vec4\n"
+"#endif\n"
+"\n"
+"varying vec2 TexCoord;\n"
+"\n"
+"#ifdef USELIGHTSOURCE\n"
+"varying myhvec3 CubeVector;\n"
+"varying vec3 LightVector;\n"
+"#endif\n"
+"\n"
+"#if defined(USESPECULAR) || defined(USEFOG) || defined(USEOFFSETMAPPING)\n"
+"varying vec3 EyeVector;\n"
+"#endif\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"// vertex shader specific:\n"
+"#ifdef VERTEX_SHADER\n"
+"\n"
+"#ifdef USELIGHTSOURCE\n"
+"uniform vec3 LightPosition;\n"
+"#endif\n"
+"\n"
+"#if defined(USESPECULAR) || defined(USEFOG) || defined(USEOFFSETMAPPING)\n"
+"uniform vec3 EyePosition;\n"
+"#endif\n"
+"\n"
+"// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3)\n"
+"\n"
+"void main(void)\n"
+"{\n"
+" // copy the surface texcoord\n"
+" TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);\n"
+"\n"
+"#ifdef USELIGHTSOURCE\n"
+" // transform vertex position into light attenuation/cubemap space\n"
+" // (-1 to +1 across the light box)\n"
+" CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex);\n"
+"\n"
+" // transform unnormalized light direction into tangent space\n"
+" // (we use unnormalized to ensure that it interpolates correctly and then\n"
+" // normalize it per pixel)\n"
+" vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
+" LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
+" LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
+" LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
+"#endif\n"
+"\n"
+"#if defined(USESPECULAR) || defined(USEFOG) || defined(USEOFFSETMAPPING)\n"
+" // transform unnormalized eye direction into tangent space\n"
+" vec3 eyeminusvertex = EyePosition - gl_Vertex.xyz;\n"
+" EyeVector.x = dot(eyeminusvertex, gl_MultiTexCoord1.xyz);\n"
+" EyeVector.y = dot(eyeminusvertex, gl_MultiTexCoord2.xyz);\n"
+" EyeVector.z = dot(eyeminusvertex, gl_MultiTexCoord3.xyz);\n"
+"#endif\n"
+"\n"
+" // transform vertex to camera space, using ftransform to match non-VS\n"
+" // rendering\n"
+" gl_Position = ftransform();\n"
+"}\n"
+"\n"
+"#endif\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"// fragment shader specific:\n"
+"#ifdef FRAGMENT_SHADER\n"
+"\n"
+"uniform myhvec3 LightColor;\n"
+"#ifdef USEOFFSETMAPPING\n"
+"uniform myhalf OffsetMapping_Scale;\n"
+"uniform myhalf OffsetMapping_Bias;\n"
+"#endif\n"
+"\n"
+"#if defined(USELIGHTSOURCE) || defined(USEDELUXEMAPPING)\n"
+"uniform sampler2D Texture_Normal;\n"
+"#endif\n"
+"\n"
+"uniform sampler2D Texture_Color;\n"
+"\n"
+"#ifdef USECOLORMAPPING\n"
+"uniform sampler2D Texture_Pants;\n"
+"uniform sampler2D Texture_Shirt;\n"
+"uniform myhvec3 Color_Pants;\n"
+"uniform myhvec3 Color_Shirt;\n"
+"#endif\n"
+"\n"
+"uniform myhalf AmbientScale;\n"
+"uniform myhalf DiffuseScale;\n"
+"#ifdef USESPECULAR\n"
+"uniform myhalf SpecularScale;\n"
+"uniform myhalf SpecularPower;\n"
+"uniform sampler2D Texture_Gloss;\n"
+"#endif\n"
+"\n"
+"#ifdef USECUBEFILTER\n"
+"uniform samplerCube Texture_Cube;\n"
+"#endif\n"
+"\n"
+"#ifdef USEFOG\n"
+"uniform myhalf FogRangeRecip;\n"
+"uniform sampler2D Texture_FogMask;\n"
+"#endif\n"
+"\n"
+"void main(void)\n"
+"{\n"
+" // apply offsetmapping\n"
+"#ifdef USEOFFSETMAPPING\n"
+" // this is 3 sample because of ATI Radeon 9500-9800/X300 limits\n"
+" myhvec2 OffsetVector = normalize(EyeVector).xy * vec2(-0.333, 0.333);\n"
+" myhvec2 TexCoordOffset = TexCoord + OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoord).w);\n"
+" TexCoordOffset += OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoordOffset).w);\n"
+" TexCoordOffset += OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoordOffset).w);\n"
+"#define TexCoord TexCoordOffset\n"
+"#endif\n"
+"\n"
+" // combine the diffuse textures (base, pants, shirt)\n"
+" myhvec4 color = myhvec4(texture2D(Texture_Color, TexCoord));\n"
+"#ifdef USECOLORMAPPING\n"
+" color.rgb += myhvec3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhvec3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
+"#endif\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"#ifdef USELIGHTSOURCE\n"
+" // light source\n"
+"\n"
+" // get the surface normal and light normal\n"
+"#ifdef SURFACENORMALIZE\n"
+" myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - 0.5);\n"
+"#else\n"
+" myhvec3 surfacenormal = -1.0 + 2.0 * myhvec3(texture2D(Texture_Normal, TexCoord));\n"
+"#endif\n"
+" myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
+"\n"
+" // calculate directional shading\n"
+" color.rgb *= (AmbientScale + DiffuseScale * max(dot(surfacenormal, diffusenormal), 0.0));\n"
+"#ifdef USESPECULAR\n"
+" myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
+" color.rgb += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(max(dot(surfacenormal, specularnormal), 0.0), SpecularPower);\n"
+"#endif\n"
+"\n"
+"#ifdef USECUBEFILTER\n"
+" // apply light cubemap filter\n"
+" color.rgb *= myhvec3(textureCube(Texture_Cube, CubeVector));\n"
+"#endif\n"
+"\n"
+" // apply light color\n"
+" color.rgb = color.rgb * LightColor;\n"
+"\n"
+" // apply attenuation\n"
+" //\n"
+" // the attenuation is (1-(x*x+y*y+z*z)) which gives a large bright\n"
+" // center and sharp falloff at the edge, this is about the most efficient\n"
+" // we can get away with as far as providing illumination.\n"
+" //\n"
+" // pow(1-(x*x+y*y+z*z), 4) is far more realistic but needs large lights to\n"
+" // provide significant illumination, large = slow = pain.\n"
+" color.rgb *= max(1.0 - dot(CubeVector, CubeVector), 0.0);\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"#else\n"
+"#ifdef USEDELUXEMAPPING\n"
+" // deluxemap lightmapping\n"
+"\n"
+" // get the surface normal and light normal\n"
+"#ifdef SURFACENORMALIZE\n"
+" myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - 0.5);\n"
+" myhvec3 diffusenormal = normalize(myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)));\n"
+"#else\n"
+" myhvec3 surfacenormal = -1.0 + 2.0 * myhvec3(texture2D(Texture_Normal, TexCoord));\n"
+" myhvec3 diffusenormal = myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap));\n"
+"#endif\n"
+"\n"
+" // calculate directional shading\n"
+" color.rgb *= (AmbientScale + DiffuseScale * max(dot(surfacenormal, diffusenormal), 0.0));\n"
+"#ifdef USESPECULAR\n"
+" myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
+" color.rgb += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(max(dot(surfacenormal, specularnormal), 0.0), SpecularPower);\n"
+"#endif\n"
+"\n"
+" // apply lightmap color\n"
+" color.rgb *= myhvec3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"#else\n"
+" // apply lightmap color\n"
+" color.rgb *= myhvec3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale;\n"
+"#endif\n"
+"#endif\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"#ifdef USEFOG\n"
+" // apply fog\n"
+" myhalf fog = texture2D(Texture_FogMask, myhvec2(length(EyeVector)*FogRangeRecip, 0)).x;\n"
+" color.rgb = color.rgb * (1 - fog) + FogColor * fog;\n"
+"#endif\n"
+"\n"
+" gl_FragColor = color;\n"
+"}\n"
+"\n"
+"#endif\n"
+;
+
+// the loaded GLSL shader file for compiling shader permutations as needed
+static char *shaderstring = NULL;
+
+void R_GLSL_CompilePermutation(int permutation)
+{
+ r_glsl_permutation_t *p = r_glsl_permutations + permutation;
+ int vertstrings_count;
+ int fragstrings_count;
+ const char *vertstrings_list[SHADERPERMUTATION_COUNT+1];
+ const char *fragstrings_list[SHADERPERMUTATION_COUNT+1];
+ char permutationname[256];
+ if (p->compiled)
+ return;
+ p->compiled = true;
+ vertstrings_list[0] = "#define VERTEX_SHADER\n";
+ fragstrings_list[0] = "#define FRAGMENT_SHADER\n";
+ vertstrings_count = 1;
+ fragstrings_count = 1;
+ permutationname[0] = 0;
+ if (permutation & SHADERPERMUTATION_MODE_LIGHTSOURCE)
+ {
+ vertstrings_list[vertstrings_count++] = "#define MODE_LIGHTSOURCE\n";
+ fragstrings_list[fragstrings_count++] = "#define MODE_LIGHTSOURCE\n";
+ strlcat(permutationname, " lightsource", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP)
+ {
+ vertstrings_list[vertstrings_count++] = "#define MODE_LIGHTDIRECTIONMAP\n";
+ fragstrings_list[fragstrings_count++] = "#define MODE_LIGHTDIRECTIONMAP\n";
+ strlcat(permutationname, " lightdirectionmap", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_MODE_LIGHTDIRECTION)
+ {
+ vertstrings_list[vertstrings_count++] = "#define MODE_LIGHTDIRECTION\n";
+ fragstrings_list[fragstrings_count++] = "#define MODE_LIGHTDIRECTION\n";
+ strlcat(permutationname, " lightdirection", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_GLOW)
+ {
+ vertstrings_list[vertstrings_count++] = "#define USEGLOW\n";
+ fragstrings_list[fragstrings_count++] = "#define USEGLOW\n";
+ strlcat(permutationname, " glow", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_COLORMAPPING)
+ {
+ vertstrings_list[vertstrings_count++] = "#define USECOLORMAPPING\n";
+ fragstrings_list[fragstrings_count++] = "#define USECOLORMAPPING\n";
+ strlcat(permutationname, " colormapping", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_SPECULAR)
+ {
+ vertstrings_list[vertstrings_count++] = "#define USESPECULAR\n";
+ fragstrings_list[fragstrings_count++] = "#define USESPECULAR\n";
+ strlcat(permutationname, " specular", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_FOG)
+ {
+ vertstrings_list[vertstrings_count++] = "#define USEFOG\n";
+ fragstrings_list[fragstrings_count++] = "#define USEFOG\n";
+ strlcat(permutationname, " fog", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_CUBEFILTER)
+ {
+ vertstrings_list[vertstrings_count++] = "#define USECUBEFILTER\n";
+ fragstrings_list[fragstrings_count++] = "#define USECUBEFILTER\n";
+ strlcat(permutationname, " cubefilter", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_OFFSETMAPPING)
+ {
+ vertstrings_list[vertstrings_count++] = "#define USEOFFSETMAPPING\n";
+ fragstrings_list[fragstrings_count++] = "#define USEOFFSETMAPPING\n";
+ strlcat(permutationname, " offsetmapping", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_SURFACENORMALIZE)
+ {
+ vertstrings_list[vertstrings_count++] = "#define SURFACENORMALIZE\n";
+ fragstrings_list[fragstrings_count++] = "#define SURFACENORMALIZE\n";
+ strlcat(permutationname, " surfacenormalize", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_GEFORCEFX)
+ {
+ vertstrings_list[vertstrings_count++] = "#define GEFORCEFX\n";
+ fragstrings_list[fragstrings_count++] = "#define GEFORCEFX\n";
+ strlcat(permutationname, " halffloat", sizeof(permutationname));
+ }
+ if (shaderstring)
+ {
+ vertstrings_list[vertstrings_count++] = shaderstring;
+ fragstrings_list[fragstrings_count++] = shaderstring;
+ }
+ else
+ {
+ vertstrings_list[vertstrings_count++] = builtinshaderstring;
+ fragstrings_list[fragstrings_count++] = builtinshaderstring;
+ }
+ p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, fragstrings_count, fragstrings_list);
+ if (p->program)
+ {
+ CHECKGLERROR
+ qglUseProgramObjectARB(p->program);
+ p->loc_Texture_Normal = qglGetUniformLocationARB(p->program, "Texture_Normal");
+ p->loc_Texture_Color = qglGetUniformLocationARB(p->program, "Texture_Color");
+ p->loc_Texture_Gloss = qglGetUniformLocationARB(p->program, "Texture_Gloss");
+ p->loc_Texture_Cube = qglGetUniformLocationARB(p->program, "Texture_Cube");
+ p->loc_Texture_FogMask = qglGetUniformLocationARB(p->program, "Texture_FogMask");
+ p->loc_Texture_Pants = qglGetUniformLocationARB(p->program, "Texture_Pants");
+ p->loc_Texture_Shirt = qglGetUniformLocationARB(p->program, "Texture_Shirt");
+ p->loc_Texture_Lightmap = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
+ p->loc_Texture_Deluxemap = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
+ p->loc_Texture_Glow = qglGetUniformLocationARB(p->program, "Texture_Glow");
+ p->loc_FogColor = qglGetUniformLocationARB(p->program, "FogColor");
+ p->loc_LightPosition = qglGetUniformLocationARB(p->program, "LightPosition");
+ p->loc_EyePosition = qglGetUniformLocationARB(p->program, "EyePosition");
+ p->loc_LightColor = qglGetUniformLocationARB(p->program, "LightColor");
+ p->loc_Color_Pants = qglGetUniformLocationARB(p->program, "Color_Pants");
+ p->loc_Color_Shirt = qglGetUniformLocationARB(p->program, "Color_Shirt");
+ p->loc_FogRangeRecip = qglGetUniformLocationARB(p->program, "FogRangeRecip");
+ p->loc_AmbientScale = qglGetUniformLocationARB(p->program, "AmbientScale");
+ p->loc_DiffuseScale = qglGetUniformLocationARB(p->program, "DiffuseScale");
+ p->loc_SpecularPower = qglGetUniformLocationARB(p->program, "SpecularPower");
+ p->loc_SpecularScale = qglGetUniformLocationARB(p->program, "SpecularScale");
+ p->loc_OffsetMapping_Scale = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
+ p->loc_OffsetMapping_Bias = qglGetUniformLocationARB(p->program, "OffsetMapping_Bias");
+ p->loc_AmbientColor = qglGetUniformLocationARB(p->program, "AmbientColor");
+ p->loc_DiffuseColor = qglGetUniformLocationARB(p->program, "DiffuseColor");
+ p->loc_SpecularColor = qglGetUniformLocationARB(p->program, "SpecularColor");
+ p->loc_LightDir = qglGetUniformLocationARB(p->program, "LightDir");
+ if (p->loc_Texture_Normal >= 0) qglUniform1iARB(p->loc_Texture_Normal, 0);
+ if (p->loc_Texture_Color >= 0) qglUniform1iARB(p->loc_Texture_Color, 1);
+ if (p->loc_Texture_Gloss >= 0) qglUniform1iARB(p->loc_Texture_Gloss, 2);
+ if (p->loc_Texture_Cube >= 0) qglUniform1iARB(p->loc_Texture_Cube, 3);
+ if (p->loc_Texture_FogMask >= 0) qglUniform1iARB(p->loc_Texture_FogMask, 4);
+ if (p->loc_Texture_Pants >= 0) qglUniform1iARB(p->loc_Texture_Pants, 5);
+ if (p->loc_Texture_Shirt >= 0) qglUniform1iARB(p->loc_Texture_Shirt, 6);
+ if (p->loc_Texture_Lightmap >= 0) qglUniform1iARB(p->loc_Texture_Lightmap, 7);
+ if (p->loc_Texture_Deluxemap >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap, 8);
+ if (p->loc_Texture_Glow >= 0) qglUniform1iARB(p->loc_Texture_Glow, 9);
+ qglUseProgramObjectARB(0);
+ CHECKGLERROR
+ }
+ else
+ Con_Printf("permutation%s failed for shader %s, some features may not work properly!\n", permutationname, "glsl/default.glsl");
+}
+
+void R_SetupSurfaceShader(const entity_render_t *ent, const texture_t *texture, const vec3_t modelorg, const vec3_t lightcolorbase, qboolean modellighting)
+{
+ // select a permutation of the lighting shader appropriate to this
+ // combination of texture, entity, light source, and fogging, only use the
+ // minimum features necessary to avoid wasting rendering time in the
+ // fragment shader on features that are not being used
+ int permutation = 0;
+ float specularscale = texture->specularscale;
+ r_glsl_permutation = NULL;
+ if (r_shadow_rtlight)
+ {
+ permutation |= SHADERPERMUTATION_MODE_LIGHTSOURCE;
+ specularscale *= r_shadow_rtlight->specularscale;
+ if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
+ permutation |= SHADERPERMUTATION_CUBEFILTER;
+ }
+ else if (modellighting)
+ {
+ permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTION;
+ if (texture->skin.glow)
+ permutation |= SHADERPERMUTATION_GLOW;
+ }
+ else if (false)
+ {
+ permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP;
+ if (texture->skin.glow)
+ permutation |= SHADERPERMUTATION_GLOW;
+ }
+ else
+ {
+ if (texture->skin.glow)
+ permutation |= SHADERPERMUTATION_GLOW;
+ }
+ if (specularscale > 0)
+ permutation |= SHADERPERMUTATION_SPECULAR;
+ if (fogenabled)
+ permutation |= SHADERPERMUTATION_FOG;
+ if (texture->colormapping)
+ permutation |= SHADERPERMUTATION_COLORMAPPING;
+ if (r_glsl_offsetmapping.integer)
+ permutation |= SHADERPERMUTATION_OFFSETMAPPING;
+ if (r_glsl_surfacenormalize.integer)
+ permutation |= SHADERPERMUTATION_SURFACENORMALIZE;
+ if (r_glsl_usehalffloat.integer)
+ permutation |= SHADERPERMUTATION_GEFORCEFX;
+ if (!r_glsl_permutations[permutation].program)
+ {
+ if (!r_glsl_permutations[permutation].compiled)
+ R_GLSL_CompilePermutation(permutation);
+ if (!r_glsl_permutations[permutation].program)
+ {
+ // remove features until we find a valid permutation
+ int i;
+ for (i = SHADERPERMUTATION_COUNT-1;;i>>=1)
+ {
+ // reduce i more quickly whenever it would not remove any bits
+ if (permutation < i)
+ continue;
+ permutation &= i;
+ if (!r_glsl_permutations[permutation].compiled)
+ R_GLSL_CompilePermutation(permutation);
+ if (r_glsl_permutations[permutation].program)
+ break;
+ if (!i)
+ return; // utterly failed
+ }
+ }
+ }
+ r_glsl_permutation = r_glsl_permutations + permutation;
+ CHECKGLERROR
+ qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
+ R_Mesh_TexMatrix(0, &texture->currenttexmatrix);
+ if (permutation & SHADERPERMUTATION_MODE_LIGHTSOURCE)
+ {
+ R_Mesh_TexMatrix(3, &r_shadow_entitytolight);
+ if (r_glsl_permutation->loc_Texture_Cube >= 0) R_Mesh_TexBind(3, R_GetTexture(r_shadow_rtlight->currentcubemap));
+ if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightPosition, r_shadow_entitylightorigin[0], r_shadow_entitylightorigin[1], r_shadow_entitylightorigin[2]);
+ if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
+ if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, r_shadow_rtlight->ambientscale);
+ if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, r_shadow_rtlight->diffusescale);
+ if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, specularscale);
+ }
+ else if (permutation & SHADERPERMUTATION_MODE_LIGHTDIRECTION)
+ {
+ if (r_glsl_permutation->loc_AmbientColor >= 0)
+ qglUniform3fARB(r_glsl_permutation->loc_AmbientColor, ent->modellight_ambient[0], ent->modellight_ambient[1], ent->modellight_ambient[2]);
+ if (r_glsl_permutation->loc_DiffuseColor >= 0)
+ qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor, ent->modellight_diffuse[0], ent->modellight_diffuse[1], ent->modellight_diffuse[2]);
+ if (r_glsl_permutation->loc_SpecularColor >= 0)
+ qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, ent->modellight_diffuse[0] * texture->specularscale, ent->modellight_diffuse[1] * texture->specularscale, ent->modellight_diffuse[2] * texture->specularscale);
+ if (r_glsl_permutation->loc_LightDir >= 0)
+ qglUniform3fARB(r_glsl_permutation->loc_LightDir, ent->modellight_lightdir[0], ent->modellight_lightdir[1], ent->modellight_lightdir[2]);
+ }
+ else
+ {
+ if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, r_ambient.value * 2.0f / 128.0f);
+ if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, r_lightmapintensity * 2.0f);
+ if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, specularscale * 2.0f);
+ }
+ if (r_glsl_permutation->loc_Texture_Normal >= 0) R_Mesh_TexBind(0, R_GetTexture(texture->skin.nmap));
+ if (r_glsl_permutation->loc_Texture_Color >= 0) R_Mesh_TexBind(1, R_GetTexture(texture->basetexture));
+ if (r_glsl_permutation->loc_Texture_Gloss >= 0) R_Mesh_TexBind(2, R_GetTexture(texture->glosstexture));
+ if (r_glsl_permutation->loc_Texture_FogMask >= 0) R_Mesh_TexBind(4, R_GetTexture(r_texture_fogattenuation));
+ if (r_glsl_permutation->loc_Texture_Pants >= 0) R_Mesh_TexBind(5, R_GetTexture(texture->skin.pants));
+ if (r_glsl_permutation->loc_Texture_Shirt >= 0) R_Mesh_TexBind(6, R_GetTexture(texture->skin.shirt));
+ if (r_glsl_permutation->loc_Texture_Glow >= 0) R_Mesh_TexBind(9, R_GetTexture(texture->skin.glow));
+ if (r_glsl_permutation->loc_FogColor >= 0)
+ {
+ // additive passes are only darkened by fog, not tinted
+ if (r_shadow_rtlight || (texture->currentmaterialflags & MATERIALFLAG_ADD))
+ qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
+ else
+ qglUniform3fARB(r_glsl_permutation->loc_FogColor, fogcolor[0], fogcolor[1], fogcolor[2]);
+ }
+ if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, modelorg[0], modelorg[1], modelorg[2]);
+ if (r_glsl_permutation->loc_Color_Pants >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, ent->colormap_pantscolor[0], ent->colormap_pantscolor[1], ent->colormap_pantscolor[2]);
+ if (r_glsl_permutation->loc_Color_Shirt >= 0) qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, ent->colormap_shirtcolor[0], ent->colormap_shirtcolor[1], ent->colormap_shirtcolor[2]);
+ if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, fograngerecip);
+ if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, texture->specularpower);
+ if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
+ if (r_glsl_permutation->loc_OffsetMapping_Bias >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Bias, r_glsl_offsetmapping_bias.value);
+ CHECKGLERROR
+}
+