+ //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"
+"varying vec2 TexCoord;\n"
+"varying vec2 TexCoordLightmap;\n"
+"\n"
+"varying vec3 CubeVector;\n"
+"varying vec3 LightVector;\n"
+"varying vec3 EyeVector;\n"
+"#ifdef USEFOG\n"
+"varying vec3 EyeVectorModelSpace;\n"
+"#endif\n"
+"\n"
+"varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
+"varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
+"varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"// vertex shader specific:\n"
+"#ifdef VERTEX_SHADER\n"
+"\n"
+"uniform vec3 LightPosition;\n"
+"uniform vec3 EyePosition;\n"
+"uniform vec3 LightDir;\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"
+" gl_FrontColor = gl_Color;\n"
+" // copy the surface texcoord\n"
+" TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);\n"
+"#if !defined(MODE_LIGHTSOURCE) && !defined(MODE_LIGHTDIRECTION)\n"
+" TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
+"#endif\n"
+"\n"
+"#ifdef MODE_LIGHTSOURCE\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"
+"#ifdef MODE_LIGHTDIRECTION\n"
+" LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
+" LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
+" LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
+"#endif\n"
+"\n"
+" // transform unnormalized eye direction into tangent space\n"
+"#ifndef USEFOG\n"
+" vec3 EyeVectorModelSpace;\n"
+"#endif\n"
+" EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
+" EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
+" EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
+" EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
+"\n"
+"#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
+" VectorS = gl_MultiTexCoord1.xyz;\n"
+" VectorT = gl_MultiTexCoord2.xyz;\n"
+" VectorR = 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 // VERTEX_SHADER\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"// fragment shader specific:\n"
+"#ifdef FRAGMENT_SHADER\n"
+"\n"
+"uniform sampler2D Texture_Normal;\n"
+"uniform sampler2D Texture_Color;\n"
+"uniform sampler2D Texture_Gloss;\n"
+"uniform samplerCube Texture_Cube;\n"
+"uniform sampler2D Texture_FogMask;\n"
+"uniform sampler2D Texture_Pants;\n"
+"uniform sampler2D Texture_Shirt;\n"
+"uniform sampler2D Texture_Lightmap;\n"
+"uniform sampler2D Texture_Deluxemap;\n"
+"uniform sampler2D Texture_Glow;\n"
+"\n"
+"uniform vec3 LightColor;\n"
+"uniform vec3 AmbientColor;\n"
+"uniform vec3 DiffuseColor;\n"
+"uniform vec3 SpecularColor;\n"
+"uniform vec3 Color_Pants;\n"
+"uniform vec3 Color_Shirt;\n"
+"uniform vec3 FogColor;\n"
+"\n"
+"uniform float OffsetMapping_Scale;\n"
+"uniform float OffsetMapping_Bias;\n"
+"uniform float FogRangeRecip;\n"
+"\n"
+"uniform float AmbientScale;\n"
+"uniform float DiffuseScale;\n"
+"uniform float SpecularScale;\n"
+"uniform float SpecularPower;\n"
+"\n"
+"void main(void)\n"
+"{\n"
+" // apply offsetmapping\n"
+"#ifdef USEOFFSETMAPPING\n"
+" vec2 TexCoordOffset = TexCoord;\n"
+"#define TexCoord TexCoordOffset\n"
+"\n"
+" vec3 eyedir = vec3(normalize(EyeVector));\n"
+" float depthbias = 1.0 - eyedir.z; // should this be a -?\n"
+" depthbias = 1.0 - depthbias * depthbias;\n"
+"\n"
+"#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
+" // 14 sample relief mapping: linear search and then binary search\n"
+" vec3 OffsetVector = vec3(EyeVector.xy * (1.0 / EyeVector.z) * depthbias * OffsetMapping_Scale * vec2(-0.1, 0.1), -0.1);\n"
+" vec3 RT = vec3(TexCoord - OffsetVector.xy * 10.0, 1.0) + OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+" if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+" TexCoord = RT.xy;\n"
+"#else\n"
+" // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
+" vec2 OffsetVector = vec2((EyeVector.xy * (1.0 / EyeVector.z) * depthbias) * OffsetMapping_Scale * vec2(-0.333, 0.333));\n"
+" //TexCoord += OffsetVector * 3.0;\n"
+" TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
+" TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
+" TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
+"#endif\n"
+"#endif\n"
+"\n"
+" // combine the diffuse textures (base, pants, shirt)\n"
+" vec4 color = vec4(texture2D(Texture_Color, TexCoord));\n"
+"#ifdef USECOLORMAPPING\n"
+" color.rgb += vec3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + vec3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
+"#endif\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"#ifdef MODE_LIGHTSOURCE\n"
+" // light source\n"
+"\n"
+" // get the surface normal and light normal\n"
+" vec3 surfacenormal = normalize(vec3(texture2D(Texture_Normal, TexCoord)) - 0.5);\n"
+" vec3 diffusenormal = vec3(normalize(LightVector));\n"
+"\n"
+" // calculate directional shading\n"
+" color.rgb *= (AmbientScale + DiffuseScale * max(dot(surfacenormal, diffusenormal), 0.0));\n"
+"#ifdef USESPECULAR\n"
+" vec3 specularnormal = vec3(normalize(diffusenormal + vec3(normalize(EyeVector))));\n"
+" color.rgb += vec3(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 *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector));\n"
+" color.rgb *= vec3(textureCube(Texture_Cube, CubeVector));\n"
+"#endif\n"
+"\n"
+" // apply light color\n"
+" 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"
+"#elif defined(MODE_LIGHTDIRECTION)\n"
+" // directional model lighting\n"
+"\n"
+" // get the surface normal and light normal\n"
+" vec3 surfacenormal = normalize(vec3(texture2D(Texture_Normal, TexCoord)) - 0.5);\n"
+" vec3 diffusenormal = vec3(normalize(LightVector));\n"
+"\n"
+" // calculate directional shading\n"
+" color.rgb *= AmbientColor + DiffuseColor * max(dot(surfacenormal, diffusenormal), 0.0);\n"
+"#ifdef USESPECULAR\n"
+" vec3 specularnormal = vec3(normalize(diffusenormal + vec3(normalize(EyeVector))));\n"
+" color.rgb += vec3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(max(dot(surfacenormal, specularnormal), 0.0), SpecularPower);\n"
+"#endif\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"#elif defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE)\n"
+" // deluxemap lightmapping using light vectors in modelspace (evil q3map2)\n"
+"\n"
+" // get the surface normal and light normal\n"
+" vec3 surfacenormal = normalize(vec3(texture2D(Texture_Normal, TexCoord)) - 0.5);\n"
+" vec3 diffusenormal_modelspace = vec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - 0.5;\n"
+" vec3 diffusenormal = normalize(vec3(dot(diffusenormal_modelspace, VectorS), dot(diffusenormal_modelspace, VectorT), dot(diffusenormal_modelspace, VectorR)));\n"
+"\n"
+" // calculate directional shading\n"
+" vec3 tempcolor = color.rgb * (DiffuseScale * max(dot(surfacenormal, diffusenormal), 0.0));\n"
+"#ifdef USESPECULAR\n"
+" vec3 specularnormal = vec3(normalize(diffusenormal + vec3(normalize(EyeVector))));\n"
+" tempcolor += vec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(max(dot(surfacenormal, specularnormal), 0.0), SpecularPower);\n"
+"#endif\n"
+"\n"
+" // apply lightmap color\n"
+" color.rgb = tempcolor * vec3(texture2D(Texture_Lightmap, TexCoordLightmap)) + color.rgb * vec3(AmbientScale);\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"#elif defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
+" // deluxemap lightmapping using light vectors in tangentspace\n"
+"\n"
+" // get the surface normal and light normal\n"
+" vec3 surfacenormal = normalize(vec3(texture2D(Texture_Normal, TexCoord)) - 0.5);\n"
+" vec3 diffusenormal = normalize(vec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - 0.5);\n"
+"\n"
+" // calculate directional shading\n"
+" vec3 tempcolor = color.rgb * (DiffuseScale * max(dot(surfacenormal, diffusenormal), 0.0));\n"
+"#ifdef USESPECULAR\n"
+" vec3 specularnormal = vec3(normalize(diffusenormal + vec3(normalize(EyeVector))));\n"
+" tempcolor += vec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(max(dot(surfacenormal, specularnormal), 0.0), SpecularPower);\n"
+"#endif\n"
+"\n"
+" // apply lightmap color\n"
+" color.rgb = tempcolor * vec3(texture2D(Texture_Lightmap, TexCoordLightmap)) + color.rgb * vec3(AmbientScale);\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"#else // MODE none (lightmap)\n"
+" // apply lightmap color\n"
+" color.rgb *= vec3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + vec3(AmbientScale);\n"
+"#endif // MODE\n"
+"\n"
+" color *= gl_Color;\n"
+"\n"
+"#ifdef USEGLOW\n"
+" color.rgb += vec3(texture2D(Texture_Glow, TexCoord));\n"
+"#endif\n"
+"\n"
+"#ifdef USEFOG\n"
+" // apply fog\n"
+" float fog = texture2D(Texture_FogMask, vec2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)).x;\n"
+" color.rgb = color.rgb * fog + FogColor * (1.0 - fog);\n"
+"#endif\n"
+"\n"
+" gl_FragColor = color;\n"
+"}\n"
+"\n"
+"#endif // FRAGMENT_SHADER\n"
+;
+
+void R_GLSL_CompilePermutation(int permutation)
+{
+ r_glsl_permutation_t *p = r_glsl_permutations + permutation;
+ int vertstrings_count;
+ int fragstrings_count;
+ char *shaderstring;
+ 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_MODELSPACE)
+ {
+ vertstrings_list[vertstrings_count++] = "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n";
+ fragstrings_list[fragstrings_count++] = "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n";
+ strlcat(permutationname, " lightdirectionmap_modelspace", sizeof(permutationname));
+ }
+ if (permutation & SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)
+ {
+ vertstrings_list[vertstrings_count++] = "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n";
+ fragstrings_list[fragstrings_count++] = "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n";
+ strlcat(permutationname, " lightdirectionmap_tangentspace", 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_OFFSETMAPPING_RELIEFMAPPING)
+ {
+ vertstrings_list[vertstrings_count++] = "#define USEOFFSETMAPPING_RELIEFMAPPING\n";
+ fragstrings_list[fragstrings_count++] = "#define USEOFFSETMAPPING_RELIEFMAPPING\n";
+ strlcat(permutationname, " OFFSETMAPPING_RELIEFMAPPING", sizeof(permutationname));
+ }
+ shaderstring = (char *)FS_LoadFile("glsl/default.glsl", r_main_mempool, false, NULL);
+ if (shaderstring)
+ {
+ Con_DPrintf("GLSL shader text loaded from disk\n");
+ 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);CHECKGLERROR
+ 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_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);
+ CHECKGLERROR
+ qglUseProgramObjectARB(0);CHECKGLERROR
+ }
+ else
+ Con_Printf("permutation%s failed for shader %s, some features may not work properly!\n", permutationname, "glsl/default.glsl");
+ if (shaderstring)
+ Mem_Free(shaderstring);
+}
+
+void R_GLSL_Restart_f(void)
+{
+ int i;
+ for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
+ if (r_glsl_permutations[i].program)
+ GL_Backend_FreeProgram(r_glsl_permutations[i].program);
+ memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
+}
+
+int R_SetupSurfaceShader(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 = rsurface_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 (!(rsurface_texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
+ {
+ if (modellighting)
+ permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTION;
+ else
+ {
+ if (r_glsl_deluxemapping.integer >= 1 && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping && rsurface_lightmaptexture)
+ {
+ if (r_refdef.worldmodel->brushq3.deluxemapping_modelspace)
+ permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_MODELSPACE;
+ else
+ permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
+ }
+ else if (r_glsl_deluxemapping.integer >= 2) // fake mode
+ permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
+ }
+ }
+ if (rsurface_texture->skin.glow)
+ permutation |= SHADERPERMUTATION_GLOW;
+ }
+ if (specularscale > 0)
+ permutation |= SHADERPERMUTATION_SPECULAR;
+ if (fogenabled)
+ permutation |= SHADERPERMUTATION_FOG;
+ if (rsurface_texture->colormapping)
+ permutation |= SHADERPERMUTATION_COLORMAPPING;
+ if (r_glsl_offsetmapping.integer)
+ {
+ permutation |= SHADERPERMUTATION_OFFSETMAPPING;
+ if (r_glsl_offsetmapping_reliefmapping.integer)
+ permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
+ }
+ 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 0; // utterly failed
+ }
+ }
+ }
+ r_glsl_permutation = r_glsl_permutations + permutation;
+ CHECKGLERROR
+ qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
+ R_Mesh_TexMatrix(0, &rsurface_texture->currenttexmatrix);
+ if (permutation & SHADERPERMUTATION_MODE_LIGHTSOURCE)
+ {
+ if (r_glsl_permutation->loc_Texture_Cube >= 0 && r_shadow_rtlight) R_Mesh_TexBindCubeMap(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, rsurface_entity->modellight_ambient[0], rsurface_entity->modellight_ambient[1], rsurface_entity->modellight_ambient[2]);
+ if (r_glsl_permutation->loc_DiffuseColor >= 0)
+ qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor, rsurface_entity->modellight_diffuse[0], rsurface_entity->modellight_diffuse[1], rsurface_entity->modellight_diffuse[2]);
+ if (r_glsl_permutation->loc_SpecularColor >= 0)
+ qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface_entity->modellight_diffuse[0] * rsurface_texture->specularscale, rsurface_entity->modellight_diffuse[1] * rsurface_texture->specularscale, rsurface_entity->modellight_diffuse[2] * rsurface_texture->specularscale);
+ if (r_glsl_permutation->loc_LightDir >= 0)
+ qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface_entity->modellight_lightdir[0], rsurface_entity->modellight_lightdir[1], rsurface_entity->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, r_lightmapintensity * specularscale * 2.0f);
+ }
+ if (r_glsl_permutation->loc_Texture_Normal >= 0) R_Mesh_TexBind(0, R_GetTexture(rsurface_texture->skin.nmap));
+ if (r_glsl_permutation->loc_Texture_Color >= 0) R_Mesh_TexBind(1, R_GetTexture(rsurface_texture->basetexture));
+ if (r_glsl_permutation->loc_Texture_Gloss >= 0) R_Mesh_TexBind(2, R_GetTexture(rsurface_texture->glosstexture));
+ //if (r_glsl_permutation->loc_Texture_Cube >= 0 && permutation & SHADERPERMUTATION_MODE_LIGHTSOURCE) R_Mesh_TexBindCubeMap(3, R_GetTexture(r_shadow_rtlight->currentcubemap));
+ 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(rsurface_texture->skin.pants));
+ if (r_glsl_permutation->loc_Texture_Shirt >= 0) R_Mesh_TexBind(6, R_GetTexture(rsurface_texture->skin.shirt));
+ //if (r_glsl_permutation->loc_Texture_Lightmap >= 0) R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
+ //if (r_glsl_permutation->loc_Texture_Deluxemap >= 0) R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
+ if (r_glsl_permutation->loc_Texture_Glow >= 0) R_Mesh_TexBind(9, R_GetTexture(rsurface_texture->skin.glow));
+ if (r_glsl_permutation->loc_FogColor >= 0)
+ {
+ // additive passes are only darkened by fog, not tinted
+ if (r_shadow_rtlight || (rsurface_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, rsurface_modelorg[0], rsurface_modelorg[1], rsurface_modelorg[2]);
+ if (r_glsl_permutation->loc_Color_Pants >= 0)
+ {
+ if (rsurface_texture->skin.pants)
+ qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface_entity->colormap_pantscolor[0], rsurface_entity->colormap_pantscolor[1], rsurface_entity->colormap_pantscolor[2]);
+ else
+ qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
+ }
+ if (r_glsl_permutation->loc_Color_Shirt >= 0)
+ {
+ if (rsurface_texture->skin.shirt)
+ qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface_entity->colormap_shirtcolor[0], rsurface_entity->colormap_shirtcolor[1], rsurface_entity->colormap_shirtcolor[2]);
+ else
+ qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
+ }
+ 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, rsurface_texture->specularpower);
+ if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
+ CHECKGLERROR
+ return permutation;
+}
+
+void R_SwitchSurfaceShader(int permutation)
+{
+ if (r_glsl_permutation != r_glsl_permutations + permutation)
+ {
+ r_glsl_permutation = r_glsl_permutations + permutation;
+ CHECKGLERROR
+ qglUseProgramObjectARB(r_glsl_permutation->program);
+ CHECKGLERROR
+ }