Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. // https://tutorialsplay.com/opengl/2014/09/26/lesson-15-water-with-opengl-on-the-gpu-with-glsl/
  2.  
  3. uniform sampler2D color_texture;
  4.  
  5. uniform samplerCube Env;
  6. uniform float alpha;
  7. varying vec3 Normal;
  8. varying vec3 EyeDir;
  9.  
  10. uniform vec4 texmix;
  11. uniform vec2 uvdrag;
  12.  
  13. uniform int lighttype;
  14. varying vec4 diffuse,ambientGlobal,ambient,ecPos;
  15. varying vec3 normal,halfVector;
  16.  
  17. void main() {
  18. vec4 color = ambient;
  19.  
  20. if (lighttype == 1) // directional light
  21. {
  22. vec3 n,halfV,lightDir;
  23. float NdotL,NdotHV;
  24.  
  25. lightDir = vec3(gl_LightSource[0].position);
  26.  
  27. /* The ambient term will always be present */
  28. // color = ambient;
  29.  
  30. /* a fragment shader can't write a varying variable, hence we need
  31. a new variable to store the normalized interpolated normal */
  32. n = normalize(normal);
  33.  
  34. /* compute the dot product between normal and ldir */
  35. NdotL = max(dot(n,lightDir),0.0);
  36.  
  37. if (NdotL > 0.0)
  38. {
  39. color += diffuse * NdotL;
  40. halfV = normalize(halfVector);
  41. NdotHV = max(dot(n,halfV),0.0);
  42. color += gl_FrontMaterial.specular
  43. * gl_LightSource[0].specular
  44. * pow(NdotHV, gl_FrontMaterial.shininess);
  45. }
  46. }
  47.  
  48. if (lighttype == 2) // point light
  49. {
  50. vec3 n,halfV,viewV,lightDir;
  51. float NdotL,NdotHV;
  52. color = ambientGlobal;
  53. float att, dist;
  54.  
  55. /* a fragment shader can't write a verying variable, hence we need
  56. a new variable to store the normalized interpolated normal */
  57. n = normalize(normal);
  58.  
  59. // Compute the ligt direction
  60. lightDir = vec3(gl_LightSource[0].position-ecPos);
  61.  
  62. /* compute the distance to the light source to a varying variable*/
  63. dist = length(lightDir);
  64.  
  65. /* compute the dot product between normal and ldir */
  66. NdotL = max(dot(n,normalize(lightDir)),0.0);
  67.  
  68. if (NdotL > 0.0)
  69. {
  70. att = 1.0 / (gl_LightSource[0].constantAttenuation
  71. + gl_LightSource[0].linearAttenuation * dist
  72. + gl_LightSource[0].quadraticAttenuation * dist * dist);
  73.  
  74. color += att * (diffuse * NdotL + ambient);
  75.  
  76. halfV = normalize(halfVector);
  77. NdotHV = max(dot(n,halfV),0.0);
  78. color += att * gl_FrontMaterial.specular
  79. * gl_LightSource[0].specular
  80. * pow(NdotHV,gl_FrontMaterial.shininess);
  81. }
  82. }
  83.  
  84. if (lighttype == 3) // spot light
  85. {
  86. vec3 n,halfV,viewV,lightDir;
  87. float NdotL,NdotHV;
  88. color = ambientGlobal;
  89. float att, dist,spotEffect;
  90.  
  91. /* a fragment shader can't write a verying variable, hence we need
  92. a new variable to store the normalized interpolated normal */
  93. n = normalize(normal);
  94.  
  95. // Compute the ligt direction
  96. lightDir = vec3(gl_LightSource[0].position-ecPos);
  97.  
  98. /* compute the distance to the light source to a varying variable*/
  99. dist = length(lightDir);
  100.  
  101. /* compute the dot product between normal and ldir */
  102. NdotL = max(dot(n,normalize(lightDir)),0.0);
  103.  
  104. if (NdotL > 0.0)
  105. {
  106. spotEffect = dot(normalize(gl_LightSource[0].spotDirection), normalize(-lightDir));
  107.  
  108. if (spotEffect > gl_LightSource[0].spotCosCutoff)
  109. {
  110. spotEffect = pow(spotEffect, gl_LightSource[0].spotExponent);
  111. att = spotEffect / (gl_LightSource[0].constantAttenuation
  112. + gl_LightSource[0].linearAttenuation * dist
  113. + gl_LightSource[0].quadraticAttenuation * dist * dist);
  114.  
  115. color += att * (diffuse * NdotL + ambient);
  116.  
  117. halfV = normalize(halfVector);
  118. NdotHV = max(dot(n,halfV),0.0);
  119. color += att * gl_FrontMaterial.specular
  120. * gl_LightSource[0].specular
  121. * pow(NdotHV,gl_FrontMaterial.shininess);
  122. }
  123. }
  124. }
  125.  
  126. // Set the output color of our current pixel
  127. vec4 tex = texture2D(color_texture, gl_TexCoord[0].st);
  128.  
  129. // specular cubemap
  130. vec3 reflectDir = reflect(EyeDir, normalize(Normal));
  131. vec4 pixel = textureCube(Env, vec3(-reflectDir.x, -reflectDir.y, reflectDir.z));
  132. gl_FragColor = (texmix.x * vec4(pixel.rgb, alpha)) + (texmix.y * vec4(tex.rgb, alpha)) + (texmix.z * vec4(color.rgb, alpha));
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement