Hello!
I am trying to make this code work on my terrain, but sadly the result doesn’t look correct.
The bump mapping works, but the general light is odd.
Any ideas what can be wrong?
Vertex:
fvViewDirection = (gl_ModelViewMatrix * gl_Vertex).xyz;
fvNormal = gl_NormalMatrix * gl_Normal;
fvBinormal = gl_NormalMatrix * gl_MultiTexCoord2.xyz;
fvTangent = gl_NormalMatrix * gl_MultiTexCoord1.xyz;
ViewDirection.x = dot( fvTangent, fvViewDirection );
ViewDirection.y = dot( fvBinormal, fvViewDirection );
ViewDirection.z = dot( fvNormal, fvViewDirection );
ViewDirection = normalize(ViewDirection);
Frag:
vec3 fvBumpNormal = normalize( (( texture2D( bumpMap, Texcoord * 5.0).xyz * fBumpSmoothness) + ( texture2D( bumpMap, Texcoord * 0.45).xyz) * 0.5 * fBumpSmoothness) - fBumpHeight * fBumpSmoothness);
float fNDotL ;
vec3 fvReflection ;
float fRDotV ;
vec4 fvSpecColor = vec4(1.0, 1.0, 1.0, 1.0);
vec4 fvNewDiffuse ;
vec4 fvTotalDiffuse = vec4(0, 0, 0, 0);
vec4 fvTotalAmbient = vec4(0, 0, 0, 0);
vec4 fvTotalSpecular = vec4(0, 0, 0, 0);
fvLightDirection = gl_LightSource[0].position.xyz - fvViewDirection;
LightDirection.x = dot( fvTangent, fvLightDirection );
LightDirection.y = dot( fvBinormal, fvLightDirection );
LightDirection.z = dot( fvNormal, fvLightDirection );
LightDirection = normalize(LightDirection);
fNDotL = dot( fvBumpNormal, LightDirection );
fvReflection = normalize( ( (fSpecularSpread * fvBumpNormal ) * fNDotL ) - LightDirection );
fRDotV = max( dot( fvReflection, -ViewDirection ), 0.0 );
fvNewDiffuse = clamp(gl_LightSource[0].diffuse * fNDotL, 0.0, 1.0);
fvTotalDiffuse = min(fvTotalDiffuse + fvNewDiffuse, 1.0);
fvTotalSpecular = min(fvTotalSpecular + clamp((pow(fRDotV, fSpecularPower ) ) * (fvNewDiffuse + 0.2) / 1.2 * (fvSpecColor * gl_LightSource[0].specular), 0.0, 1.0), 1.0);
fvTotalAmbient = fvTotalAmbient + gl_LightSource[0].ambient;
gl_FragColor = fLightPower * (fvBaseColor * ( fvTotalAmbient + fvTotalDiffuse ) + fvTotalSpecular);
The fvBaseColor
is my ground texture.
Thanks in advance!
/Bracer