+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"
+"#ifdef __GLSL_CG_DATA_TYPES\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"
+"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 myhvec3 LightColor;\n"
+"uniform myhvec3 AmbientColor;\n"
+"uniform myhvec3 DiffuseColor;\n"
+"uniform myhvec3 SpecularColor;\n"
+"uniform myhvec3 Color_Pants;\n"
+"uniform myhvec3 Color_Shirt;\n"
+"uniform myhvec3 FogColor;\n"
+"\n"
+"uniform myhalf GlowScale;\n"
+"uniform myhalf SceneBrightness;\n"
+"\n"
+"uniform float OffsetMapping_Scale;\n"
+"uniform float OffsetMapping_Bias;\n"
+"uniform float FogRangeRecip;\n"
+"\n"
+"uniform myhalf AmbientScale;\n"
+"uniform myhalf DiffuseScale;\n"
+"uniform myhalf SpecularScale;\n"
+"uniform myhalf 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 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-0.1, 0.1), -0.1);\n"
+" vec3 OffsetVector = vec3(eyedir.xy * 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"
+"#elif 1\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"
+" //vec2 OffsetVector = vec2(normalize(EyeVector.xy)) * OffsetMapping_Scale * vec2(-0.333, 0.333);\n"
+" vec2 OffsetVector = vec2(eyedir.xy) * 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"
+"#elif 0\n"
+" // 10 sample offset mapping\n"
+" //vec2 OffsetVector = vec2(EyeVector.xy * (1.0 / EyeVector.z) * depthbias) * OffsetMapping_Scale * vec2(-0.333, 0.333);\n"
+" //vec2 OffsetVector = vec2(normalize(EyeVector.xy)) * OffsetMapping_Scale * vec2(-0.333, 0.333);\n"
+" vec2 OffsetVector = vec2(eyedir.xy) * OffsetMapping_Scale * vec2(-0.1, 0.1);\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"
+" TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\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"
+" TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
+" TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
+" TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
+"#elif 1\n"
+" // parallax mapping as described in the paper\n"
+" // 'Parallax Mapping with Offset Limiting: A Per-Pixel Approximation of Uneven Surfaces' by Terry Welsh\n"
+" // The paper provides code in the ARB fragment program assembly language\n"
+" // I translated it to GLSL but may have done something wrong - SavageX\n"
+" // LordHavoc: removed bias and simplified to one line\n"
+" // LordHavoc: this is just a single sample offsetmapping...\n"
+" TexCoordOffset += vec2(eyedir.x, -1.0 * eyedir.y) * OffsetMapping_Scale * texture2D(Texture_Normal, TexCoord).a;\n"
+"#else\n"
+" // parallax mapping as described in the paper\n"
+" // 'Parallax Mapping with Offset Limiting: A Per-Pixel Approximation of Uneven Surfaces' by Terry Welsh\n"
+" // The paper provides code in the ARB fragment program assembly language\n"
+" // I translated it to GLSL but may have done something wrong - SavageX\n"
+" float height = texture2D(Texture_Normal, TexCoord).a;\n"
+" height = (height - 0.5) * OffsetMapping_Scale; // bias and scale\n"
+" TexCoordOffset += height * vec2(eyedir.x, -1.0 * eyedir.y);\n"
+"#endif\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 MODE_LIGHTSOURCE\n"
+" // light source\n"
+"\n"
+" // get the surface normal and light normal\n"
+" myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
+" myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
+"\n"
+" // calculate directional shading\n"
+" color.rgb *= AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
+"#ifdef USESPECULAR\n"
+" myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
+" color.rgb += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(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 *= myhvec3(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 *= myhalf(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"
+" myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
+" myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
+"\n"
+" // calculate directional shading\n"
+" color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
+"#ifdef USESPECULAR\n"
+" myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
+" color.rgb += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
+"#endif\n"
+"\n"
+"\n"
+"\n"
+"\n"
+"#elif defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
+" // deluxemap lightmapping using light vectors in modelspace (evil q3map2)\n"
+"\n"
+" // get the surface normal and light normal\n"
+" myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
+"\n"
+"#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
+" myhvec3 diffusenormal_modelspace = myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5);\n"
+" myhvec3 diffusenormal = normalize(myhvec3(dot(diffusenormal_modelspace, myhvec3(VectorS)), dot(diffusenormal_modelspace, myhvec3(VectorT)), dot(diffusenormal_modelspace, myhvec3(VectorR))));\n"
+"#else\n"
+" myhvec3 diffusenormal = normalize(myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5));\n"
+"#endif\n"
+" // calculate directional shading\n"
+" myhvec3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)));\n"
+"#ifdef USESPECULAR\n"
+" myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
+" tempcolor += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
+"#endif\n"
+"\n"
+" // apply lightmap color\n"
+" color.rgb = tempcolor * myhvec3(texture2D(Texture_Lightmap, TexCoordLightmap)) + color.rgb * AmbientScale;\n"
+"\n"
+"\n"
+"#else // MODE none (lightmap)\n"
+" // apply lightmap color\n"
+" color.rgb *= myhvec3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + myhvec3(AmbientScale);\n"
+"#endif // MODE\n"
+"\n"
+" color *= myhvec4(gl_Color);\n"
+"\n"
+"#ifdef USEGLOW\n"
+" color.rgb += myhvec3(texture2D(Texture_Glow, TexCoord)) * GlowScale;\n"
+"#endif\n"
+"\n"
+"#ifdef USEFOG\n"
+" // apply fog\n"
+" myhalf fog = myhalf(texture2D(Texture_FogMask, myhvec2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0)).x);\n"
+" color.rgb = color.rgb * fog + FogColor * (1.0 - fog);\n"
+"#endif\n"
+"\n"
+" color.rgb *= SceneBrightness;\n"
+"\n"
+" gl_FragColor = vec4(color);\n"
+"}\n"
+"\n"
+"#endif // FRAGMENT_SHADER\n"
+;
+
+// NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
+const char *permutationinfo[][2] =
+{
+ {"#define MODE_LIGHTSOURCE\n", " lightsource"},
+ {"#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
+ {"#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
+ {"#define MODE_LIGHTDIRECTION\n", " lightdirection"},
+ {"#define USEGLOW\n", " glow"},
+ {"#define USEFOG\n", " fog"},
+ {"#define USECOLORMAPPING\n", " colormapping"},
+ {"#define USESPECULAR\n", " specular"},
+ {"#define USECUBEFILTER\n", " cubefilter"},
+ {"#define USEOFFSETMAPPING\n", " offsetmapping"},
+ {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
+ {NULL, NULL}
+};
+
+void R_GLSL_CompilePermutation(int permutation)
+{
+ int i;
+ 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;
+ for (i = 0;permutationinfo[i][0];i++)
+ {
+ if (permutation & (1<<i))
+ {
+ vertstrings_list[vertstrings_count++] = permutationinfo[i][0];
+ fragstrings_list[fragstrings_count++] = permutationinfo[i][0];
+ strlcat(permutationname, permutationinfo[i][1], sizeof(permutationname));
+ }
+ else
+ {
+ // keep line numbers correct
+ vertstrings_list[vertstrings_count++] = "\n";
+ fragstrings_list[fragstrings_count++] = "\n";
+ }
+ }
+ 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_GlowScale = qglGetUniformLocationARB(p->program, "GlowScale");
+ p->loc_SceneBrightness = qglGetUniformLocationARB(p->program, "SceneBrightness");
+ 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 && rsurface_lightmaptexture)
+ {
+ if (r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping)
+ {
+ 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 (r_refdef.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_refdef.lightmapintensity * 2.0f);
+ if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, r_refdef.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_GlowScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_GlowScale, r_hdr_glowintensity.value);
+ if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale);
+ 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, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.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, r_refdef.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
+ }
+}
+