Advertisement
Guest User

Fragment Shader

a guest
May 3rd, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. #version 420
  2.  
  3.  
  4. uniform vec4 meshColor;
  5.  
  6. struct light {
  7.     vec3 position;
  8.     vec3 diffuse;
  9.     vec3 specular;
  10.     vec3 ambient;
  11.     float attenuation;
  12. };
  13.  
  14. uniform light[5] lights;
  15. uniform mat4 modelViewMatrix;
  16.  
  17. // Textures
  18. uniform sampler2D diffuseTexture;
  19. uniform sampler2D depthTexture;
  20. uniform sampler2D normalTexture;
  21. uniform sampler2D specularTexture;
  22.  
  23. // Colors
  24. uniform vec3 diffuseColor;
  25. uniform bool textured;
  26. uniform float shininess;
  27. uniform float glossiness;
  28. uniform float repeatx;
  29. uniform float repeaty;
  30.  
  31. // Ouput data
  32. out vec4 fragColor;
  33.  
  34. // Input data
  35. in vec4 Position;
  36. in vec4 ShadowCoordinates;
  37. in vec2 UV;
  38. in vec3 Normal;
  39.  
  40. in vec3 LightDir;
  41. in vec3 ViewDir;
  42.  
  43. vec3 ads( int lightIndex, vec3 norm, vec3 Color, vec3 specMap ) {
  44.     vec3 lightDir = normalize(LightDir);
  45.     vec3 viewDir = normalize(ViewDir);
  46.  
  47.     float visibility = 1.0;
  48.     float bias = 0.0005;
  49.  
  50.     if ( texture( depthTexture, ShadowCoordinates.xy ).r  <  ShadowCoordinates.z - bias) {
  51.         //visibility = 0.65;
  52.     }
  53.  
  54.     vec3 r = reflect( -lightDir, norm );
  55.     vec3 ambient = lights[0].ambient * Color;
  56.     float sDotN = max( dot(lightDir, norm), 0.0 );
  57.     vec3 diffuse = lights[0].diffuse * Color * sDotN;
  58.     vec3 spec = vec3(0.0);
  59.     if( sDotN > 0.0 && visibility == 1.0 )
  60.         spec = shininess * (lights[0].specular * specMap * pow( max( dot(r,viewDir), 0.0 ), max(1.0, glossiness) ));
  61.  
  62.     return visibility * (ambient + diffuse + spec);
  63. }
  64.  
  65. void main()
  66. {
  67.  
  68.     vec4 Color = vec4(diffuseColor, 1.0);
  69.     vec3 normalMap = normalize(texture( normalTexture, vec2(UV.x*repeatx, UV.y*repeaty) ).rgb*2.0 - 1.0);  
  70.     vec4 specularMap = texture( specularTexture, vec2(UV.x*repeatx, UV.y*repeaty) );
  71.        
  72.     //if (textured)
  73.         //Color = texture( diffuseTexture, vec2(UV.x*repeatx, UV.y*repeaty) );
  74.    
  75.     vec3 rcol = vec3(0.0);
  76.     for (int i = 0; i < 1; i++)
  77.         rcol += ads (i, normalMap, Color.rgb, specularMap.rbg);
  78.  
  79.     fragColor = vec4(rcol, 1.0);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement