ColSpecEmit.frag 3.7 KB

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