2012-05-09 15:30:53 +00:00
!!permu BUMP
2013-10-08 14:28:11 +00:00
!!permu FRAMEBLEND
2012-05-09 15:30:53 +00:00
!!permu SKELETAL
2013-03-12 22:35:33 +00:00
!!permu UPPERLOWER
2012-05-09 15:30:53 +00:00
!!permu FOG
2015-05-03 19:57:46 +00:00
!!permu REFLECTCUBEMASK
2012-05-09 15:30:53 +00:00
!!cvarf r_glsl_offsetmapping_scale
2013-10-08 14:28:11 +00:00
!!cvardf r_glsl_pcf
2012-05-09 15:30:53 +00:00
2014-08-27 08:41:31 +00:00
#ifndef USE_ARB_SHADOW
//fall back on regular samplers if we must
#define sampler2DShadow sampler2D
#endif
2012-05-09 15:30:53 +00:00
//this is the main shader responsible for realtime dlights.
//texture units:
//s0=diffuse, s1=normal, s2=specular, s3=shadowmap
//custom modifiers:
//PCF(shadowmap)
2013-10-08 14:28:11 +00:00
//CUBEPROJ(projected cubemap)
2012-08-04 01:35:52 +00:00
//SPOT(projected circle
//CUBESHADOW
2013-10-08 14:28:11 +00:00
#ifndef r_glsl_pcf
2014-08-27 08:41:31 +00:00
#error r_glsl_pcf wasnt defined
2013-10-08 14:28:11 +00:00
#endif
#if r_glsl_pcf < 1
#undef r_glsl_pcf
#define r_glsl_pcf 9
#endif
2012-08-04 01:35:52 +00:00
#if 0 && defined(GL_ARB_texture_gather) && defined(PCF)
#extension GL_ARB_texture_gather : enable
#endif
2012-05-09 15:30:53 +00:00
2013-03-12 22:35:33 +00:00
#ifdef UPPERLOWER
#define UPPER
#define LOWER
#endif
2014-10-05 20:04:11 +00:00
//if there's no vertex normals known, disable some stuff.
//FIXME: this results in dupe permutations.
#ifdef NOBUMP
#undef SPECULAR
#undef BUMP
#undef OFFSETMAPPING
#endif
2012-05-09 15:30:53 +00:00
varying vec2 tcbase;
varying vec3 lightvector;
2015-05-03 19:57:46 +00:00
#if defined(SPECULAR) || defined(OFFSETMAPPING) || defined(REFLECTCUBEMASK)
2012-05-09 15:30:53 +00:00
varying vec3 eyevector;
#endif
2015-05-03 19:57:46 +00:00
#ifdef REFLECTCUBEMASK
varying mat3 invsurface;
uniform mat4 m_model;
#endif
2013-06-26 00:39:13 +00:00
#if defined(PCF) || defined(CUBE) || defined(SPOT)
2012-08-04 01:35:52 +00:00
varying vec4 vtexprojcoord;
#endif
2014-08-27 08:41:31 +00:00
2012-05-09 15:30:53 +00:00
#ifdef VERTEX_SHADER
2014-08-27 08:41:31 +00:00
#if defined(PCF) || defined(CUBE) || defined(SPOT)
uniform mat4 l_cubematrix;
#endif
2012-05-09 15:30:53 +00:00
#include "sys/skeletal.h"
uniform vec3 l_lightposition;
attribute vec2 v_texcoord;
2015-05-03 19:57:46 +00:00
#if defined(SPECULAR) || defined(OFFSETMAPPING) || defined(REFLECTCUBEMASK)
2012-05-09 15:30:53 +00:00
uniform vec3 e_eyepos;
#endif
void main ()
{
vec3 n, s, t, w;
gl_Position = skeletaltransform_wnst(w,n,s,t);
tcbase = v_texcoord; //pass the texture coords straight through
vec3 lightminusvertex = l_lightposition - w.xyz;
2014-10-05 20:04:11 +00:00
#ifdef NOBUMP
//the only important thing is distance
lightvector = lightminusvertex;
#else
//the light direction relative to the surface normal, for bumpmapping.
2014-01-13 02:42:25 +00:00
lightvector.x = dot(lightminusvertex, s.xyz);
2012-05-09 15:30:53 +00:00
lightvector.y = dot(lightminusvertex, t.xyz);
lightvector.z = dot(lightminusvertex, n.xyz);
2014-10-05 20:04:11 +00:00
#endif
2015-05-03 19:57:46 +00:00
#if defined(SPECULAR)||defined(OFFSETMAPPING) || defined(REFLECTCUBEMASK)
2012-05-09 15:30:53 +00:00
vec3 eyeminusvertex = e_eyepos - w.xyz;
2014-01-13 02:42:25 +00:00
eyevector.x = dot(eyeminusvertex, s.xyz);
2012-07-05 13:09:44 +00:00
eyevector.y = dot(eyeminusvertex, t.xyz);
2012-05-09 15:30:53 +00:00
eyevector.z = dot(eyeminusvertex, n.xyz);
#endif
2015-05-03 19:57:46 +00:00
#ifdef REFLECTCUBEMASK
invsurface[0] = v_svector;
invsurface[1] = v_tvector;
invsurface[2] = v_normal;
#endif
2013-06-26 00:39:13 +00:00
#if defined(PCF) || defined(SPOT) || defined(CUBE)
2012-08-04 01:35:52 +00:00
//for texture projections/shadowmapping on dlights
vtexprojcoord = (l_cubematrix*vec4(w.xyz, 1.0));
2012-05-09 15:30:53 +00:00
#endif
}
#endif
2012-08-04 01:35:52 +00:00
2012-05-09 15:30:53 +00:00
#ifdef FRAGMENT_SHADER
#include "sys/fog.h"
2013-03-12 22:35:33 +00:00
uniform sampler2D s_t0; //diffuse
2012-08-04 01:35:52 +00:00
2015-05-03 19:57:46 +00:00
#if defined(BUMP) || defined(SPECULAR) || defined(OFFSETMAPPING) || defined(REFLECTCUBEMASK)
2013-03-12 22:35:33 +00:00
uniform sampler2D s_t1; //normalmap
2012-05-09 15:30:53 +00:00
#endif
#ifdef SPECULAR
2013-03-12 22:35:33 +00:00
uniform sampler2D s_t2; //specular
2012-05-09 15:30:53 +00:00
#endif
2013-06-26 00:39:13 +00:00
#ifdef CUBE
2013-03-12 22:35:33 +00:00
uniform samplerCube s_t3; //projected cubemap
2012-05-09 15:30:53 +00:00
#endif
#ifdef PCF
2012-08-04 01:35:52 +00:00
#ifdef CUBESHADOW
2013-03-12 22:35:33 +00:00
uniform samplerCubeShadow s_t4; //shadowmap
2012-08-04 01:35:52 +00:00
#else
#if 0//def GL_ARB_texture_gather
uniform sampler2D s_t4;
2012-05-09 15:30:53 +00:00
#else
2012-08-04 01:35:52 +00:00
uniform sampler2DShadow s_t4;
#endif
2012-05-09 15:30:53 +00:00
#endif
#endif
2013-03-12 22:35:33 +00:00
#ifdef LOWER
uniform sampler2D s_t5; //pants colours
uniform vec3 e_lowercolour;
#endif
#ifdef UPPER
uniform sampler2D s_t6; //shirt colours
uniform vec3 e_uppercolour;
#endif
2012-08-04 01:35:52 +00:00
2015-05-03 19:57:46 +00:00
#ifdef REFLECTCUBEMASK
uniform sampler2D s_reflectmask;
uniform samplerCube s_reflectcube;
#endif
2012-08-04 01:35:52 +00:00
2012-05-09 15:30:53 +00:00
uniform float l_lightradius;
uniform vec3 l_lightcolour;
uniform vec3 l_lightcolourscale;
2013-03-12 23:13:39 +00:00
#ifdef PCF
2013-10-08 14:28:11 +00:00
uniform vec4 l_shadowmapproj; //light projection matrix info
uniform vec2 l_shadowmapscale; //xy are the texture scale, z is 1, w is the scale.
vec3 ShadowmapCoord(void)
2012-08-04 01:35:52 +00:00
{
#ifdef SPOT
//bias it. don't bother figuring out which side or anything, its not needed
//l_projmatrix contains the light's projection matrix so no other magic needed
2014-10-05 20:04:11 +00:00
return ((vtexprojcoord.xyz-vec3(0.0,0.0,0.015))/vtexprojcoord.w + vec3(1.0, 1.0, 1.0)) * vec3(0.5, 0.5, 0.5);
2013-10-08 14:28:11 +00:00
//#elif defined(CUBESHADOW)
// vec3 shadowcoord = vshadowcoord.xyz / vshadowcoord.w;
// #define dosamp(x,y) shadowCube(s_t4, shadowcoord + vec2(x,y)*texscale.xy).r
2012-08-04 01:35:52 +00:00
#else
//figure out which axis to use
//texture is arranged thusly:
//forward left up
//back right down
2013-10-08 14:28:11 +00:00
vec3 dir = abs(vtexprojcoord.xyz);
2012-08-04 01:35:52 +00:00
//assume z is the major axis (ie: forward from the light)
2013-10-08 14:28:11 +00:00
vec3 t = vtexprojcoord.xyz;
2012-08-04 01:35:52 +00:00
float ma = dir.z;
2013-10-08 14:28:11 +00:00
vec3 axis = vec3(0.5/3.0, 0.5/2.0, 0.5);
2012-08-04 01:35:52 +00:00
if (dir.x > ma)
{
ma = dir.x;
2013-10-08 14:28:11 +00:00
t = vtexprojcoord.zyx;
axis.x = 0.5;
2012-08-04 01:35:52 +00:00
}
if (dir.y > ma)
{
ma = dir.y;
2013-10-08 14:28:11 +00:00
t = vtexprojcoord.xzy;
axis.x = 2.5/3.0;
2012-08-04 01:35:52 +00:00
}
2013-10-08 14:28:11 +00:00
//if the axis is negative, flip it.
2012-08-04 01:35:52 +00:00
if (t.z > 0.0)
{
2013-10-08 14:28:11 +00:00
axis.y = 1.5/2.0;
2012-08-04 01:35:52 +00:00
t.z = -t.z;
}
//we also need to pass the result through the light's projection matrix too
2013-10-08 14:28:11 +00:00
//the 'matrix' we need only contains 5 actual values. and one of them is a -1. So we might as well just use a vec4.
//note: the projection matrix also includes scalers to pinch the image inwards to avoid sampling over borders, as well as to cope with non-square source image
//the resulting z is prescaled to result in a value between -0.5 and 0.5.
//also make sure we're in the right quadrant type thing
return axis + ((l_shadowmapproj.xyz*t.xyz + vec3(0.0, 0.0, l_shadowmapproj.w)) / -t.z);
2012-08-04 01:35:52 +00:00
#endif
2013-10-08 14:28:11 +00:00
}
float ShadowmapFilter(void)
{
vec3 shadowcoord = ShadowmapCoord();
2012-08-04 01:35:52 +00:00
#if 0//def GL_ARB_texture_gather
vec2 ipart, fpart;
#define dosamp(x,y) textureGatherOffset(s_t4, ipart.xy, vec2(x,y)))
vec4 tl = step(shadowcoord.z, dosamp(-1.0, -1.0));
vec4 bl = step(shadowcoord.z, dosamp(-1.0, 1.0));
vec4 tr = step(shadowcoord.z, dosamp(1.0, -1.0));
vec4 br = step(shadowcoord.z, dosamp(1.0, 1.0));
//we now have 4*4 results, woo
//we can just average them for 1/16th precision, but that's still limited graduations
//the middle four pixels are 'full strength', but we interpolate the sides to effectively give 3*3
vec4 col = vec4(tl.ba, tr.ba) + vec4(bl.rg, br.rg) + //middle two rows are full strength
mix(vec4(tl.rg, tr.rg), vec4(bl.ba, br.ba), fpart.y); //top+bottom rows
return dot(mix(col.rgb, col.agb, fpart.x), vec3(1.0/9.0)); //blend r+a, gb are mixed because its pretty much free and gives a nicer dot instruction instead of lots of adds.
#else
2014-08-27 08:41:31 +00:00
#ifdef USE_ARB_SHADOW
//with arb_shadow, we can benefit from hardware acclerated pcf, for smoother shadows
2013-10-08 14:28:11 +00:00
#define dosamp(x,y) shadow2D(s_t4, shadowcoord.xyz + (vec3(x,y,0.0)*l_shadowmapscale.xyx)).r
2014-08-27 08:41:31 +00:00
#else
//this will probably be a bit blocky.
#define dosamp(x,y) float(texture2D(s_t4, shadowcoord.xy + (vec2(x,y)*l_shadowmapscale.xy)).r >= shadowcoord.z)
#endif
2012-08-04 01:35:52 +00:00
float s = 0.0;
2013-10-08 14:28:11 +00:00
#if r_glsl_pcf >= 1 && r_glsl_pcf < 5
s += dosamp(0.0, 0.0);
return s;
#elif r_glsl_pcf >= 5 && r_glsl_pcf < 9
s += dosamp(-1.0, 0.0);
s += dosamp(0.0, -1.0);
s += dosamp(0.0, 0.0);
s += dosamp(0.0, 1.0);
s += dosamp(1.0, 0.0);
return s/5.0;
#else
s += dosamp(-1.0, -1.0);
s += dosamp(-1.0, 0.0);
s += dosamp(-1.0, 1.0);
s += dosamp(0.0, -1.0);
s += dosamp(0.0, 0.0);
s += dosamp(0.0, 1.0);
s += dosamp(1.0, -1.0);
s += dosamp(1.0, 0.0);
s += dosamp(1.0, 1.0);
return s/9.0;
#endif
2012-08-04 01:35:52 +00:00
#endif
}
#endif
2012-05-09 15:30:53 +00:00
#ifdef OFFSETMAPPING
#include "sys/offsetmapping.h"
#endif
2013-10-29 17:38:22 +00:00
2012-05-09 15:30:53 +00:00
void main ()
{
2012-07-05 13:09:44 +00:00
//read raw texture samples (offsetmapping munges the tex coords first)
2012-05-09 15:30:53 +00:00
#ifdef OFFSETMAPPING
vec2 tcoffsetmap = offsetmap(s_t1, tcbase, eyevector);
#define tcbase tcoffsetmap
#endif
vec3 bases = vec3(texture2D(s_t0, tcbase));
2013-03-12 22:35:33 +00:00
#ifdef UPPER
vec4 uc = texture2D(s_t6, tcbase);
bases.rgb += uc.rgb*e_uppercolour*uc.a;
#endif
#ifdef LOWER
vec4 lc = texture2D(s_t5, tcbase);
bases.rgb += lc.rgb*e_lowercolour*lc.a;
#endif
2015-05-03 19:57:46 +00:00
#if defined(BUMP) || defined(SPECULAR) || defined(REFLECTCUBEMASK)
2012-07-05 13:09:44 +00:00
vec3 bumps = normalize(vec3(texture2D(s_t1, tcbase)) - 0.5);
2015-05-03 19:57:46 +00:00
#elif defined(REFLECTCUBEMASK)
vec3 bumps = vec3(0.0,0.0,1.0);
2012-05-09 15:30:53 +00:00
#endif
#ifdef SPECULAR
vec4 specs = texture2D(s_t2, tcbase);
#endif
2012-07-05 13:09:44 +00:00
float colorscale = max(1.0 - (dot(lightvector, lightvector)/(l_lightradius*l_lightradius)), 0.0);
2012-05-09 15:30:53 +00:00
vec3 diff;
2014-10-05 20:04:11 +00:00
#ifdef NOBUMP
//surface can only support ambient lighting, even for lights that try to avoid it.
diff = bases * (l_lightcolourscale.x+l_lightcolourscale.y);
2012-05-09 15:30:53 +00:00
#else
2014-10-05 20:04:11 +00:00
vec3 nl = normalize(lightvector);
#ifdef BUMP
diff = bases * (l_lightcolourscale.x + l_lightcolourscale.y * max(dot(bumps, nl), 0.0));
#else
//we still do bumpmapping even without bumps to ensure colours are always sane. light.exe does it too.
diff = bases * (l_lightcolourscale.x + l_lightcolourscale.y * max(dot(vec3(0.0, 0.0, 1.0), nl), 0.0));
#endif
2012-05-09 15:30:53 +00:00
#endif
2012-07-05 13:09:44 +00:00
2012-05-09 15:30:53 +00:00
#ifdef SPECULAR
2012-07-05 13:09:44 +00:00
vec3 halfdir = normalize(normalize(eyevector) + nl);
float spec = pow(max(dot(halfdir, bumps), 0.0), 32.0 * specs.a);
diff += l_lightcolourscale.z * spec * specs.rgb;
2012-05-09 15:30:53 +00:00
#endif
2012-07-05 13:09:44 +00:00
2015-05-03 19:57:46 +00:00
#ifdef REFLECTCUBEMASK
vec3 rtc = reflect(-eyevector, bumps);
rtc = rtc.x*invsurface[0] + rtc.y*invsurface[1] + rtc.z*invsurface[2];
rtc = (m_model * vec4(rtc.xyz,0.0)).xyz;
diff += texture2D(s_reflectmask, tcbase).rgb * textureCube(s_reflectcube, rtc).rgb;
#endif
2012-07-05 13:09:44 +00:00
2013-06-26 00:39:13 +00:00
#ifdef CUBE
2012-08-04 01:35:52 +00:00
/*filter the colour by the cubemap projection*/
diff *= textureCube(s_t3, vtexprojcoord.xyz).rgb;
2012-05-09 15:30:53 +00:00
#endif
2012-08-04 01:35:52 +00:00
2012-05-09 15:30:53 +00:00
#if defined(SPOT)
2012-08-04 01:35:52 +00:00
/*filter the colour by the spotlight. discard anything behind the light so we don't get a mirror image*/
if (vtexprojcoord.w < 0.0) discard;
vec2 spot = ((vtexprojcoord.st)/vtexprojcoord.w);colorscale*=1.0-(dot(spot,spot));
2012-05-09 15:30:53 +00:00
#endif
2012-08-04 01:35:52 +00:00
#ifdef PCF
/*filter the light by the shadowmap. logically a boolean, but we allow fractions for softer shadows*/
//diff.rgb = (vtexprojcoord.xyz/vtexprojcoord.w) * 0.5 + 0.5;
colorscale *= ShadowmapFilter();
2013-11-10 21:14:28 +00:00
// diff = ShadowmapCoord();
2012-08-04 01:35:52 +00:00
#endif
2012-05-09 15:30:53 +00:00
#if defined(PROJECTION)
2012-08-04 01:35:52 +00:00
/*2d projection, not used*/
// diff *= texture2d(s_t3, shadowcoord);
2012-05-09 15:30:53 +00:00
#endif
2012-08-04 01:35:52 +00:00
2012-05-09 15:30:53 +00:00
gl_FragColor.rgb = fog3additive(diff*colorscale*l_lightcolour);
2015-05-03 19:57:46 +00:00
2012-05-09 15:30:53 +00:00
}
#endif
2014-08-27 08:41:31 +00:00