Changes to GLSL files for OpenGL

This commit is contained in:
LuisAntonRebollo 2014-04-13 19:48:51 +02:00
parent 2142d452d4
commit 6aea37b407
98 changed files with 3366 additions and 2686 deletions

View file

@ -20,73 +20,181 @@
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef TORQUE_SHADERGEN
// These are the uniforms used by most lighting shaders.
uniform vec3 inLightPos[4];
uniform vec4 inLightPos[3];
uniform vec4 inLightInvRadiusSq;
uniform vec4 inLightColor[4];
#ifndef TORQUE_BL_NOSPOTLIGHT
uniform vec4 inLightSpotDir[3];
uniform vec4 inLightSpotAngle;
uniform vec4 inLightSpotFalloff;
#endif
uniform vec4 ambient;
uniform float specularPower;
uniform vec4 specularColor;
// This is used to limit the maximum processed
// lights in the compute4Lights down for really
// low end GPUs.
//
// NOTE: If you want to support 10.5.x, this needs to be changed to 2.
#define C4L_MAX_LIGHTS 4
#endif // !TORQUE_SHADERGEN
void compute4Lights( vec3 wsView,
vec3 wsPosition,
vec3 wsNormal,
vec3 wsNormal,
vec4 shadowMask,
#ifdef TORQUE_SHADERGEN
vec4 inLightPos[3],
vec4 inLightInvRadiusSq,
vec4 inLightColor[4],
vec4 inLightSpotDir[3],
vec4 inLightSpotAngle,
vec4 inLightSpotFalloff,
float specularPower,
vec4 specularColor,
#endif // TORQUE_SHADERGEN
out vec4 outDiffuse,
out vec4 outSpecular )
{
#ifdef PHONG_SPECULAR
// (R.V)^c
float reflected = reflect( wsView, wsNormal );
#endif
vec4 nDotL = vec4( 0.0 );
vec4 rDotL = vec4( 0.0 );
vec4 sqDists = vec4( 0.0 );
// NOTE: The light positions and spotlight directions
// are stored in SoA order, so inLightPos[0] is the
// x coord for all 4 lights... inLightPos[1] is y... etc.
//
// This is the key to fully utilizing the vector units and
// saving a huge amount of instructions.
//
// For example this change saved more than 10 instructions
// over a simple for loop for each light.
int i;
for ( i = 0; i < C4L_MAX_LIGHTS; ++i )
{
vec3 lightVector = inLightPos[i] - wsPosition;
vec3 lightDirection = normalize( lightVector );
vec4 lightVectors[3];
for ( i = 0; i < 3; i++ )
lightVectors[i] = wsPosition[i] - inLightPos[i];
nDotL[i] = max( dot( lightDirection, wsNormal ), 0.0 );
vec4 squareDists = vec4(0);
for ( i = 0; i < 3; i++ )
squareDists += lightVectors[i] * lightVectors[i];
#ifdef PHONG_SPECULAR
rDotL[i] = saturate( dot( lightDirection, reflected ) );
#else
// (N.H)^c [Blinn-Phong, TGEA style, default]
rDotL[i] = dot( wsNormal, normalize( lightDirection + wsView ) );
#endif
// Accumulate the dot product between the light
// vector and the normal.
//
// The normal is negated because it faces away from
// the surface and the light faces towards the
// surface... this keeps us from needing to flip
// the light vector direction which complicates
// the spot light calculations.
//
// We normalize the result a little later.
//
vec4 nDotL = vec4(0);
for ( i = 0; i < 3; i++ )
nDotL += lightVectors[i] * -wsNormal[i];
sqDists[i] = dot( lightVector, lightVector );
}
vec4 rDotL = vec4(0);
#ifndef TORQUE_BL_NOSPECULAR
// Attenuation
vec4 atten = vec4( 1.0 ) - ( sqDists * inLightInvRadiusSq );
// We're using the Phong specular reflection model
// here where traditionally Torque has used Blinn-Phong
// which has proven to be more accurate to real materials.
//
// We do so because its cheaper as do not need to
// calculate the half angle for all 4 lights.
//
// Advanced Lighting still uses Blinn-Phong, but the
// specular reconstruction it does looks fairly similar
// to this.
//
vec3 R = reflect( wsView, -wsNormal );
for ( i = 0; i < 3; i++ )
rDotL += lightVectors[i] * R[i];
#endif
// Normalize the dots.
//
// Notice we're using the half type here to get a
// much faster sqrt via the rsq_pp instruction at
// the loss of some precision.
//
// Unless we have some extremely large point lights
// i don't believe the precision loss will matter.
//
half4 correction = half4(inversesqrt( squareDists ));
nDotL = saturate( nDotL * correction );
rDotL = clamp( rDotL * correction, 0.00001, 1.0 );
// First calculate a simple point light linear
// attenuation factor.
//
// If this is a directional light the inverse
// radius should be greater than the distance
// causing the attenuation to have no affect.
//
vec4 atten = saturate( 1.0 - ( squareDists * inLightInvRadiusSq ) );
#ifndef TORQUE_BL_NOSPOTLIGHT
// The spotlight attenuation factor. This is really
// fast for what it does... 6 instructions for 4 spots.
vec4 spotAtten = vec4(0);
for ( i = 0; i < 3; i++ )
spotAtten += lightVectors[i] * inLightSpotDir[i];
vec4 cosAngle = ( spotAtten * correction ) - inLightSpotAngle;
atten *= saturate( cosAngle * inLightSpotFalloff );
#endif
// Finally apply the shadow masking on the attenuation.
atten *= shadowMask;
// Get the final light intensity.
vec4 intensity = nDotL * atten;
// Combine the light colors for output.
vec4 diffuse = clamp( nDotL * atten, vec4( 0.0 ), vec4( 1.0 ) );
outDiffuse = vec4( 0.0 );
for ( i = 0; i < C4L_MAX_LIGHTS; ++i )
outDiffuse += vec4( diffuse[i] ) * inLightColor[i];
outDiffuse = vec4(0);
for ( i = 0; i < 4; i++ )
outDiffuse += intensity[i] * inLightColor[i];
// Output the specular power.
rDotL = max( rDotL, vec4( 0.00001 ) );
outSpecular = pow( rDotL, vec4( specularPower ) );
vec4 specularIntensity = pow( rDotL, vec4(specularPower) ) * atten;
// Apply the per-light specular attenuation.
vec4 specular = vec4(0,0,0,1);
for ( i = 0; i < 4; i++ )
specular += vec4( inLightColor[i].rgb * inLightColor[i].a * specularIntensity[i], 1 );
// Add the final specular intensity values together
// using a single dot product operation then get the
// final specular lighting color.
outSpecular = specularColor * specular;
}
/// The standard specular calculation.
// This value is used in AL as a constant power to raise specular values
// to, before storing them into the light info buffer. The per-material
// specular value is then computer by using the integer identity of
// exponentiation:
//
// (a^m)^n = a^(m*n)
//
// or
//
// (specular^constSpecular)^(matSpecular/constSpecular) = specular^(matSpecular*constSpecular)
//
#define AL_ConstantSpecularPower 12.0f
/// The specular calculation used in Advanced Lighting.
///
/// @param toLight Normalized vector representing direction from the pixel
/// being lit, to the light source, in world space.
@ -96,11 +204,7 @@ void compute4Lights( vec3 wsView,
/// @param toEye The normalized vector representing direction from the pixel
/// being lit to the camera.
///
/// @param specPwr The specular exponent.
///
/// @param specScale A scalar on the specular output used in RGB accumulation.
///
float calcSpecular( vec3 toLight, vec3 normal, vec3 toEye, float specPwr )
float AL_CalcSpecular( vec3 toLight, vec3 normal, vec3 toEye )
{
#ifdef PHONG_SPECULAR
// (R.V)^c
@ -111,5 +215,5 @@ float calcSpecular( vec3 toLight, vec3 normal, vec3 toEye, float specPwr )
#endif
// Return the specular factor.
return pow( max( specVal, 0.00001f ), specPwr );
return pow( max( specVal, 0.00001f ), AL_ConstantSpecularPower );
}