Advertisement
Chronos_Ouroboros

Untitled

Jan 11th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Vertex
  2. #version 330 core
  3.  
  4. precision highp float;
  5.  
  6. in vec3 in_position;
  7. in vec3 in_normal;
  8. in vec2 in_texCoords;
  9.  
  10. out vec3 normal;
  11. out vec3 FragPos;
  12. out vec2 TexCoords;
  13.  
  14. uniform mat4 model;
  15. uniform mat4 view;
  16. uniform mat4 projection;
  17.  
  18. void main()
  19. {
  20.     FragPos = vec3 (view * model * vec4 (in_position, 1.0));
  21.     normal = mat3 (transpose (inverse (view * model))) * in_normal;
  22.     TexCoords = in_texCoords;
  23.  
  24.     gl_Position = projection * vec4 (FragPos, 1.0);
  25. }
  26.  
  27. // Fragment
  28. #version 330 core
  29.  
  30. precision highp float;
  31.  
  32. out vec4 FragColor;
  33.  
  34. in vec3 normal;
  35. in vec3 FragPos;
  36. in vec2 TexCoords;
  37.  
  38. uniform vec3 objectColor;
  39. uniform vec3 viewPos;
  40. uniform mat4 view;
  41.  
  42. uniform sampler2D matDiffuse;
  43. uniform sampler2D matSpecular;
  44. struct Material_t {
  45.     float shininess;
  46. };
  47. struct Light_t {
  48.     vec3 position;
  49.     vec3 direction;
  50.     float cutOff, outerCutOff;
  51.  
  52.     vec3 ambient, diffuse, specular;
  53.  
  54.     float constant, linear, quadratic;
  55. };
  56.  
  57. uniform Material_t material;
  58. uniform Light_t light;
  59.  
  60. void main () {
  61.     // Setup
  62.     vec3 lightPos = vec3 (view * vec4 (light.position, 1.0));
  63.  
  64.     // Ambient
  65.     vec3 ambient = light.ambient * texture (matDiffuse, TexCoords).rgb;
  66.  
  67.     vec3 lightDir = normalize (lightPos - FragPos);
  68.     float theta     = dot (lightDir, normalize (-vec3 (view * vec4 (light.direction, 1.0))));
  69.     float epsilon   = light.cutOff - light.outerCutOff;
  70.     float intensity = clamp ((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
  71.  
  72.     // Diffuse
  73.     vec3 norm = normalize (normal);
  74.     float diff = max (dot (norm, lightDir), 0.0);
  75.     vec3 diffuse = light.diffuse * diff * texture (matDiffuse, TexCoords).rgb;
  76.  
  77.     // Specular
  78.     vec3 viewDir = normalize (-FragPos);
  79.     vec3 reflectDir = reflect (-lightDir, norm);
  80.     float spec = pow (max (dot (viewDir, reflectDir), 0.0), material.shininess);
  81.     vec3 specular = light.specular * spec * texture (matSpecular, TexCoords).rgb;
  82.  
  83.     // Attenuation
  84.     float distance = length (lightPos - FragPos);
  85.     float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  86.     ambient  *= attenuation;
  87.     diffuse  *= attenuation * intensity;
  88.     specular *= attenuation * intensity;
  89.  
  90.     FragColor = vec4 (ambient + diffuse + specular, 1.0);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement