NorColSpecHDR.frag 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. varying vec3 normal;
  2. varying vec3 tangent;
  3. varying vec3 binormal;
  4. varying vec4 pos;
  5. varying vec4 vertexColor;
  6. uniform sampler2D diffuse;
  7. uniform sampler2D normal_map;
  8. uniform sampler2D specular_map;
  9. uniform vec4 diffuse_color;
  10. uniform vec4 specular_color;
  11. uniform vec4 ambient_color;
  12. uniform float shininess;
  13. float calculateAttenuation(in int i, in float dist)
  14. {
  15. return(1.0 / (gl_LightSource[i].constantAttenuation +
  16. gl_LightSource[i].linearAttenuation * dist +
  17. gl_LightSource[i].quadraticAttenuation * dist * dist));
  18. }
  19. void pointLight(in int i, in vec3 bump, in vec3 normal, in vec3 tangent, in vec3 binormal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
  20. vec4 color = diffuse_color;
  21. vec4 matspec = specular_color;
  22. float shininess = shininess;
  23. vec4 lightspec = gl_LightSource[i].specular;
  24. vec4 lpos = gl_LightSource[i].position;
  25. vec3 tmpVec = lpos.xyz - pos.xyz;
  26. lpos.x = dot(tmpVec, tangent);
  27. lpos.y = dot(tmpVec, binormal);
  28. lpos.z = dot(tmpVec, normal);
  29. float distSqr = dot(lpos.xyz, lpos.xyz);
  30. vec3 lVec = lpos.xyz * inversesqrt(distSqr);
  31. tmpVec = -pos.xyz;
  32. vec3 v;
  33. v.x = dot(tmpVec, tangent);
  34. v.y = dot(tmpVec, binormal);
  35. v.z = dot(tmpVec, normal);
  36. v = normalize(v);
  37. float nDotL = dot(lVec, bump);
  38. if(nDotL > 0.0) {
  39. float dist = length(lpos.xyz);
  40. float attenuation = calculateAttenuation(i, dist);
  41. diffuse += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation;
  42. if (shininess != 0.0) {
  43. specular += lightspec * matspec * pow(clamp(dot(reflect(-lVec, bump), v),0.0,1.0), shininess) * attenuation;
  44. }
  45. }
  46. }
  47. void spotLight(in int i, in vec3 bump, in vec3 normal, in vec3 tangent, in vec3 binormal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
  48. vec4 color = diffuse_color;
  49. vec4 matspec = specular_color;
  50. float shininess = shininess;
  51. vec4 lightspec = gl_LightSource[i].specular;
  52. vec4 lpos = gl_LightSource[i].position;
  53. vec3 tmpVec = lpos.xyz - pos.xyz;
  54. lpos.x = dot(tmpVec, tangent);
  55. lpos.y = dot(tmpVec, binormal);
  56. lpos.z = dot(tmpVec, normal);
  57. float distSqr = dot(lpos.xyz, lpos.xyz);
  58. vec3 lVec = lpos.xyz * inversesqrt(distSqr);
  59. tmpVec = -pos.xyz;
  60. vec3 v;
  61. v.x = dot(tmpVec, tangent);
  62. v.y = dot(tmpVec, binormal);
  63. v.z = dot(tmpVec, normal);
  64. v = normalize(v);
  65. tmpVec = gl_LightSource[i].spotDirection.xyz;
  66. vec3 lDir;
  67. lDir.x = dot(tmpVec, tangent);
  68. lDir.y = dot(tmpVec, binormal);
  69. lDir.z = dot(tmpVec, normal);
  70. lDir = normalize(lDir);
  71. float cos_outer_cone_angle = (1.0-gl_LightSource[i].spotExponent) * gl_LightSource[i].spotCosCutoff;
  72. float cos_cur_angle = dot(-lDir, lVec);
  73. float cos_inner_cone_angle = gl_LightSource[i].spotCosCutoff;
  74. float cos_inner_minus_outer_angle = cos_inner_cone_angle - cos_outer_cone_angle;
  75. float spot = clamp((cos_cur_angle - cos_outer_cone_angle) / cos_inner_minus_outer_angle, 0.0, 1.0);
  76. float nDotL = dot(lVec, bump);
  77. if(nDotL > 0.0) {
  78. float dist = length(lpos.xyz);
  79. float attenuation = calculateAttenuation(i, dist);
  80. diffuse += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation * spot;
  81. if (shininess != 0.0) {
  82. specular += lightspec * matspec * pow(clamp(dot(reflect(-lVec, bump), v),0.0,1.0), shininess) * attenuation * spot;
  83. }
  84. }}
  85. void doLights(in int numLights, in vec3 bump, in vec3 normal, in vec3 tangent, in vec3 binormal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
  86. for (int i = 0; i < numLights; i++) {
  87. if (gl_LightSource[i].spotCutoff == 180.0) {
  88. pointLight(i, bump, normal, tangent, binormal, pos, diffuse, specular);
  89. } else {
  90. spotLight(i, bump, normal, tangent, binormal, pos, diffuse, specular);
  91. }
  92. }
  93. }
  94. void main()
  95. {
  96. vec4 diffuse_val = vec4(0.0);
  97. vec4 specular_val = vec4(0.0);
  98. vec3 bump = normalize( texture2D(normal_map, gl_TexCoord[0].st).xyz * 2.0 - 1.0);
  99. doLights(6, bump, normal, tangent, binormal, pos, diffuse_val, specular_val);
  100. specular_val.xyz *= texture2D(specular_map, gl_TexCoord[0].st).xyz * gl_FrontMaterial.specular.a;
  101. diffuse_val.a = 1.0;
  102. specular_val.a = 1.0;
  103. vec4 texColor = texture2D(diffuse, gl_TexCoord[0].st);
  104. vec4 color = (diffuse_val * texColor * vertexColor) +
  105. (specular_val * 1.0)+
  106. (ambient_color * texColor * vertexColor);
  107. // fog
  108. const float LOG2 = 1.442695;
  109. float z = gl_FragCoord.z / gl_FragCoord.w;
  110. float fogFactor = exp2( -gl_Fog.density *
  111. gl_Fog.density *
  112. z *
  113. z *
  114. LOG2 );
  115. fogFactor = clamp(fogFactor, 0.0, 1.0);
  116. color = mix(gl_Fog.color, color, fogFactor );
  117. color.a = diffuse_color.a * texColor.a;
  118. gl_FragColor = color;
  119. }