Skip to content

Commit

Permalink
OpenGL2: Some shader cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileTheory committed Sep 17, 2013
1 parent 5985cca commit 42501db
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 43 deletions.
56 changes: 32 additions & 24 deletions code/renderergl2/glsl/lightall_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,30 @@ vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float s
return vec3(0.0);
}


float CalcLightAttenuation(vec3 dir, float sqrRadius)
{
// point light at >0 radius, directional otherwise
float point = float(sqrRadius > 0.0);

// inverse square light
float attenuation = sqrRadius / dot(dir, dir);

// zero light at radius, approximating q3 style
// also don't attenuate directional light
attenuation = (0.5 * attenuation - 1.5) * point + 1.0;

// clamp attenuation
#if defined(NO_LIGHT_CLAMP)
attenuation *= float(attenuation > 0.0);
#else
attenuation = clamp(attenuation, 0.0, 1.0);
#endif

return attenuation;
}


void main()
{
vec3 L, N, E, H;
Expand Down Expand Up @@ -290,30 +314,12 @@ void main()
#endif
vec3 lightColor = lightSample.rgb;
#elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
// inverse square light
float attenuation = u_LightRadius * u_LightRadius / dot(L, L);

// zero light at radius, approximating q3 style
attenuation = 0.5 * attenuation - 0.5;
//attenuation = 0.0697168 * attenuation;
//attenuation *= step(0.294117, attenuation);

// clamp attenuation
#if defined(NO_LIGHT_CLAMP)
attenuation *= step(0.0, attenuation);
#else
attenuation = clamp(attenuation, 0.0, 1.0);
#endif

// don't attenuate directional light
attenuation = (attenuation - 1.0) * var_LightDir.w + 1.0;

vec3 lightColor = u_DirectedLight * attenuation;
vec3 lightColor = u_DirectedLight * CalcLightAttenuation(L, u_LightRadius * u_LightRadius);
vec3 ambientColor = u_AmbientLight;
#elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
vec3 lightColor = var_lightColor;
#endif

vec2 texCoords = var_DiffuseTex;

#if defined(USE_PARALLAXMAP)
Expand Down Expand Up @@ -360,9 +366,9 @@ void main()

// surfaces not facing the light are always shadowed
#if defined(USE_TANGENT_SPACE_LIGHT)
shadowValue *= step(0.0, var_PrimaryLightDir.z);
shadowValue *= float(var_PrimaryLightDir.z > 0.0);
#else
shadowValue *= step(0.0, dot(var_Normal, var_PrimaryLightDir));
shadowValue *= float(dot(var_Normal, var_PrimaryLightDir) > 0.0);
#endif

#if defined(SHADOWMAP_MODULATE)
Expand Down Expand Up @@ -488,11 +494,13 @@ void main()
reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, shininess);

lightColor = u_PrimaryLightColor; // * CalcLightAttenuation(L, u_PrimaryLightRadius * u_PrimaryLightRadius);

#if defined(USE_SHADOWMAP)
reflectance *= shadowValue;
lightColor *= shadowValue;
#endif

gl_FragColor.rgb += u_PrimaryLightColor * reflectance * NL;
gl_FragColor.rgb += lightColor * reflectance * NL;
#endif

#if defined(USE_LINEAR_LIGHT)
Expand Down
42 changes: 24 additions & 18 deletions code/renderergl2/glsl/lightall_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ vec2 ModTexCoords(vec2 st, vec3 position, vec4 texMatrix, vec4 offTurb)
#endif


float CalcLightAttenuation(vec3 dir, float sqrRadius)
{
// point light at >0 radius, directional otherwise
float point = float(sqrRadius > 0.0);

// inverse square light
float attenuation = sqrRadius / dot(dir, dir);

// zero light at radius, approximating q3 style
// also don't attenuate directional light
attenuation = (0.5 * attenuation - 1.5) * point + 1.0;

// clamp attenuation
#if defined(NO_LIGHT_CLAMP)
attenuation *= float(attenuation > 0.0);
#else
attenuation = clamp(attenuation, 0.0, 1.0);
#endif

return attenuation;
}


void main()
{
#if defined(USE_VERTEX_ANIMATION)
Expand Down Expand Up @@ -187,24 +210,7 @@ void main()
#endif

#if defined(USE_LIGHT_VECTOR) && defined(USE_FAST_LIGHT)
// inverse square light
float attenuation = u_LightRadius * u_LightRadius / dot(L, L);

// zero light at radius, approximating q3 style
attenuation = 0.5 * attenuation - 0.5;
//attenuation = 0.0697168 * attenuation;
//attenuation *= step(0.294117, attenuation);

// clamp attenuation
#if defined(NO_LIGHT_CLAMP)
attenuation *= step(0.0, attenuation);
#else
attenuation = clamp(attenuation, 0.0, 1.0);
#endif

// don't attenuate directional light
attenuation = (attenuation - 1.0) * u_LightOrigin.w + 1.0;

float attenuation = CalcLightAttenuation(L, u_LightRadius * u_LightRadius);
float NL = clamp(dot(normal, normalize(L)), 0.0, 1.0);

var_Color.rgb *= u_DirectedLight * attenuation * NL + u_AmbientLight;
Expand Down
2 changes: 1 addition & 1 deletion code/renderergl2/tr_shade.c
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input )
vec[3] = 0.0f;
GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, vec);

GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, 999999.0f);
GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, 0.0f);
}

if (pStage->alphaGen == AGEN_PORTAL)
Expand Down

0 comments on commit 42501db

Please sign in to comment.