Hello folks,
I know this was discussed in another post, but I thought it could be better to put it in a new post.
Although doing per-pixel lighting leads to increased calculations, and thus performance hits, when I apply this bump-mapping shader, my performance drops from about 150fps to 15fps… this seems so much!
Here is the code:
VS:
varying vec4 lightDir;
varying vec3 normal, halfVector, spotDir;
void main()
{
vec3 tempLight;
// computing TBN matrix for tangent space ****************************************************
vec3 v_Normal = normalize(gl_NormalMatrix*gl_Normal); // normal to eye space
vec3 v_Tangent = normalize(gl_NormalMatrix*gl_MultiTexCoord2.xyz); // tangent to eye space
vec3 v_Binormal = normalize(gl_NormalMatrix*gl_MultiTexCoord3.xyz); // binormal to eye space
mat3 tangentBasis = mat3( // in column major order
v_Tangent.x, v_Binormal.x, v_Normal.x,
v_Tangent.y, v_Binormal.y, v_Normal.y,
v_Tangent.z, v_Binormal.z, v_Normal.z);
// compute light vector ***********************************************************************
vec4 ecPos, bb;
vec3 aux;
ecPos = gl_ModelViewMatrix * gl_Vertex;
aux = vec3(gl_LightSource[0].position-ecPos);
tempLight = aux;
// compute normal and half vector **************************************************************
normal = normalize(gl_NormalMatrix * gl_Normal);// vertex to eye coordinates
halfVector = normalize(gl_LightSource[0].halfVector.xyz);
// convert coordinates to tangent space ********************************************************
tempLight = tangentBasis * tempLight;
halfVector = tangentBasis * halfVector;
spotDir = tangentBasis * gl_LightSource[0].spotDirection;
// pass texture coords to fragment shader ******************************************************
gl_TexCoord[0] = gl_MultiTexCoord0;
// putting distance in w component of light ****************************************************
lightDir = vec4(tempLight, 0.0);
lightDir.w = length(aux);
// convert vertex position *********************************************************************
gl_Position = ftransform();
}
FS:
varying vec4 lightDir;
varying vec3 normal, halfVector, spotDir;
uniform sampler2D decalMap;
uniform sampler2D normalMap;
void main()
{
vec3 n,l,halfV;
vec4 texel;
float NdotL,NdotHV;
float att;
float spotEffect;
float dist;
// retrieve material parameters ***************************************************************
vec4 color = gl_FrontLightModelProduct.sceneColor;
vec4 ambient = gl_FrontLightProduct[0].ambient;
vec4 diffuse = gl_FrontLightProduct[0].diffuse;
vec4 specular = gl_FrontLightProduct[0].specular;
// compute normals from normal map ************************************************************
vec2 tuv = vec2(gl_TexCoord[0].s, -gl_TexCoord[0].t);
n = 2.0 * (texture2D(normalMap, tuv).rgb - 0.5);
n = normalize(n);
// compute light ******************************************************************************
l = normalize(lightDir.xyz);
dist = lightDir.w;
NdotL = max(dot(n,l),0.0);
if (NdotL > 0.0)
{
spotEffect = dot(normalize(spotDir), normalize(-l));
if (spotEffect > gl_LightSource[0].spotCosCutoff)
{
spotEffect = pow(spotEffect, gl_LightSource[0].spotExponent);
att = spotEffect / (gl_LightSource[0].constantAttenuation +
gl_LightSource[0].linearAttenuation * dist +
gl_LightSource[0].quadraticAttenuation * dist * dist);
color += att * (diffuse * NdotL + ambient);
halfV = normalize(halfVector);
NdotHV = max(dot(n,halfV),0.0);
color += att * specular * pow(NdotHV, gl_FrontMaterial.shininess);
}
}
// apply texture ******************************************************************************
texel = texture2D(decalMap,gl_TexCoord[0].st);
color *= texel;
// set fragment color *************************************************************************
gl_FragColor = color;
}
I know this is not optimal code, but suffering such a great performance hit makes me wonder whether I did miss something…
Another point to mention is that I can’t compute light in my FS because I’m not using a directional light, but a spotlight, which vectors need to be interpolated per-fragment. Anybody got a clue?
Thanks fellows!
HardTop