Advertisement
Gary_Keen27

Blinn-Phong Shading

Oct 25th, 2016
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////////////////////////////////////
  2. ////////////////////VERTEX SHADER////////////////////
  3. /////////////////////////////////////////////////////
  4. #version 330
  5.  
  6. layout(location = 0) in vec3 inPosition;
  7. layout(location = 1) in vec3 inNormal;
  8. layout(location = 2) in vec2 inTexCoord;
  9. layout(location = 3) in vec3 inTangent;
  10. layout(location = 4) in vec3 inBitangent;
  11.  
  12. out vec2 texCoord;
  13. out vec3 camDir;
  14. out vec3 lightDir;
  15. out float attenuation;
  16.  
  17. uniform mat4 viewMatrix;
  18. uniform mat4 modelMatrix;
  19. uniform mat4 projectionMatrix;
  20. uniform mat3 normalMatrix;
  21. uniform vec3 camPosIn;
  22. uniform vec3 lightPosIn;
  23.  
  24. void main()
  25. {
  26.     vec3 N = normalize(normalMatrix * inNormal);
  27.     vec3 T = normalize(normalMatrix * inTangent);
  28.          T = normalize(T - (dot(N, T) * N));
  29.     vec3 B = normalize(normalMatrix * inBitangent);
  30.  
  31.     if (dot(cross(N, T), B) < 0.0)
  32.         T = T * -1.0;
  33.  
  34.     mat3 TBNMatrix = transpose(mat3(T, B, N));
  35.  
  36.     vec3 position = vec3((viewMatrix * modelMatrix) * vec4(inPosition, 1.0)); //Vector is a position so W = 1.0. If it were a direction w = 0.0.
  37.     camDir = TBNMatrix * (normalize(camPosIn - position));
  38.     lightDir = TBNMatrix * (normalize(lightPosIn - position));
  39.     texCoord = inTexCoord;
  40.  
  41.     float radius = 50.0;
  42.     float dist = length(vec3((viewMatrix * modelMatrix) * vec4(lightPosIn, 1.0)) - position);
  43.     attenuation = 1.0 / (1.0 + ((2.0 / radius) * dist) + ((1.0 / (radius * radius)) * (dist * dist)));
  44.  
  45.     gl_Position = (projectionMatrix * (viewMatrix * modelMatrix)) * vec4(inPosition, 1.0);
  46. }
  47.  
  48. ///////////////////////////////////////////////////////
  49. ////////////////////FRAGMENT SHADER////////////////////
  50. ///////////////////////////////////////////////////////
  51. #version 330
  52. precision mediump float;
  53.  
  54. in vec2 texCoord;
  55. in vec3 camDir;
  56. in vec3 lightDir;
  57. in float attenuation;
  58.  
  59. layout (location = 0) out vec4 FragColour;
  60.  
  61. uniform sampler2D textureMap;
  62. uniform sampler2D normalMap;
  63. uniform sampler2D AOMap;
  64. uniform sampler2D specMap;
  65. uniform sampler2D roughMap;
  66. uniform vec3 lightColour;
  67.  
  68. void main() //Blinn-Phong = (Ka * Ia) + (Kd * (Il * (N.L))) + (Ks * (Il * ((N.H)^Ns)))
  69. {
  70.     vec3 albedo = texture(textureMap, texCoord).rgb;
  71.     vec3 normals = normalize(2.0 * texture(normalMap, texCoord).rgb - 1.0);
  72.     float roughness = texture(roughMap, texCoord).r;
  73.     float spec = texture(specMap, texCoord).r;
  74.     float AO = texture(AOMap, texCoord).r;
  75.  
  76.     vec3 diffuse = vec3(0.0);
  77.     vec3 specular = vec3(0.0);
  78.     vec3 ambientColour = vec3(0.0);
  79.  
  80.     for (int i = 0; i < 1; i++)
  81.     {
  82.         vec3 halfAngle = normalize(camDir + lightDir);
  83.         float NdotL = clamp(dot(normals, lightDir), 0.0, 1.0);
  84.         float NdotH = clamp(dot(normals, halfAngle), 0.0, 1.0);
  85.  
  86.         float specularHighlight = pow(NdotH, 1.0 - roughness);
  87.         vec3 S = spec * (lightColour * specularHighlight);
  88.         vec3 D = (1.0 - roughness) * (lightColour * NdotL);
  89.  
  90.         diffuse += D * attenuation;
  91.         specular += S * attenuation;
  92.         ambientColour += lightColour * attenuation;
  93.     }
  94.     vec3 ambience = (0.01 * ambientColour) * AO;
  95.  
  96.     vec3 BlinnPhong = ambience + diffuse + specular;
  97.     vec3 final = albedo * BlinnPhong;
  98.     final = pow(final, vec3(1.0 / 2.2));
  99.     FragColour = vec4(final, 1.0);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement