DefaultShaderVertex.vert 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. uniform float shininess;
  2. uniform vec4 diffuse_color;
  3. uniform vec4 specular_color;
  4. uniform vec4 ambient_color;
  5. float calculateAttenuation(in int i, in float dist)
  6. {
  7. return(1.0 / (gl_LightSource[i].constantAttenuation +
  8. gl_LightSource[i].linearAttenuation * dist +
  9. gl_LightSource[i].quadraticAttenuation * dist * dist));
  10. }
  11. void pointLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
  12. vec4 color = diffuse_color;
  13. vec4 matspec = specular_color;
  14. float shininess = shininess;
  15. vec4 lightspec = gl_LightSource[i].specular;
  16. vec4 lpos = gl_LightSource[i].position;
  17. vec4 s = pos-lpos;
  18. vec4 sn = -normalize(s);
  19. vec3 light = sn.xyz;
  20. vec3 n = normalize(normal);
  21. vec3 r = -reflect(light, n);
  22. r = normalize(r);
  23. vec3 v = -pos.xyz;
  24. v = normalize(v);
  25. float nDotL = dot(n, sn.xyz);
  26. if(nDotL > 0.0) {
  27. float dist = length(s);
  28. float attenuation = calculateAttenuation(i, dist);
  29. diffuse += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation;
  30. if (shininess != 0.0) {
  31. specular += lightspec * matspec * pow(max(0.0,dot(r, v)), shininess) * attenuation;
  32. }
  33. }
  34. }
  35. void spotLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
  36. vec4 color = diffuse_color;
  37. vec4 matspec = specular_color;
  38. float shininess = shininess;
  39. vec4 lightspec = gl_LightSource[i].specular;
  40. vec4 lpos = gl_LightSource[i].position;
  41. vec4 s = pos-lpos;
  42. vec4 sn = -normalize(s);
  43. vec3 light = sn.xyz;
  44. vec3 n = normalize(normal);
  45. vec3 r = -reflect(light, n);
  46. r = normalize(r);
  47. vec3 v = -pos.xyz;
  48. v = normalize(v);
  49. float cos_outer_cone_angle = (1.0-gl_LightSource[i].spotExponent) * gl_LightSource[i].spotCosCutoff;
  50. float cos_cur_angle = dot(-normalize(gl_LightSource[i].spotDirection), sn.xyz);
  51. float cos_inner_cone_angle = gl_LightSource[i].spotCosCutoff;
  52. float cos_inner_minus_outer_angle = cos_inner_cone_angle - cos_outer_cone_angle;
  53. float spot = 0.0;
  54. spot = clamp((cos_cur_angle - cos_outer_cone_angle) / cos_inner_minus_outer_angle, 0.0, 1.0);
  55. float nDotL = dot(n, sn.xyz);
  56. if(nDotL > 0.0) {
  57. float dist = length(s);
  58. float attenuation = calculateAttenuation(i, dist);
  59. diffuse += color * max(0.0, nDotL) * gl_LightSource[i].diffuse * attenuation * spot;
  60. if (shininess != 0.0) {
  61. specular += lightspec * matspec * pow(max(0.0,dot(r, v)), shininess) * attenuation * spot;
  62. }
  63. }
  64. }
  65. void doLights(in int numLights, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
  66. for (int i = 0; i < numLights; i++) {
  67. if (gl_LightSource[i].spotCutoff == 180.0) {
  68. pointLight(i, normal, pos, diffuse, specular);
  69. } else {
  70. spotLight(i, normal, pos, diffuse, specular);
  71. }
  72. }
  73. }
  74. varying vec4 vertexColor;
  75. varying vec4 specularColor;
  76. void main() {
  77. vec3 normal = gl_NormalMatrix * gl_Normal;
  78. gl_Position = ftransform();
  79. vec4 pos = gl_ModelViewMatrix * gl_Vertex;
  80. vec4 rawpos = gl_Vertex;
  81. vertexColor = gl_Color;
  82. gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
  83. vec4 diffuse_val = vec4(0.0);
  84. vec4 specular_val = vec4(0.0);
  85. doLights(6, normal, pos, diffuse_val, specular_val);
  86. vec4 color = diffuse_val +
  87. ambient_color;
  88. color.a = diffuse_color.a;
  89. specularColor = specular_val;
  90. vertexColor = clamp(color*vertexColor, 0.0, 1.0);
  91. }