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