Advertisement
Gary_Keen27

Gouraud Shading

Oct 23rd, 2016
246
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.  
  10. out vec3 Gouraud;
  11. out vec2 texCoord;
  12.  
  13. uniform mat4 viewMatrix;
  14. uniform mat4 modelMatrix;
  15. uniform mat4 projectionMatrix;
  16. uniform mat3 normalMatrix;
  17. uniform vec3 camPosIn;
  18. uniform vec3 lightPosIn;
  19. uniform vec3 lightColour;
  20.  
  21. void main()
  22. {  
  23.     vec3 position = vec3(0.0);
  24.     vec3 normals = vec3(0.0);
  25.     vec3 diffuse = vec3(0.0);
  26.     vec3 specular = vec3(0.0);
  27.     vec3 ambientColour = vec3(0.0);
  28.  
  29.     position = vec3((viewMatrix * modelMatrix) * vec4(inPosition, 1.0));   
  30.     normals = normalize(normalMatrix * inNormal);
  31.     texCoord = inTexCoord;
  32.  
  33.     vec3 camDir = normalize(camPosIn - position);
  34.  
  35.     for (int i = 0; i < 1; i++)
  36.     {
  37.         float radius = 50.0;
  38.  
  39.         vec3 lightDir = normalize(lightPosIn - position);
  40.         vec3 halfAngle = normalize(camDir + lightDir);
  41.         float NdotL = clamp(dot(normals, lightDir), 0.0, 1.0);
  42.         float NdotH = clamp(dot(normals, halfAngle), 0.0, 1.0);
  43.  
  44.         float specularHighlight = pow(NdotH, 250);
  45.         vec3 S = (lightColour * specularHighlight);
  46.         vec3 D = (lightColour * NdotL);
  47.        
  48.         float dist = length(lightPosIn - position);
  49.         float attenuation = 1.0 / (1.0 + ((2.0 / radius) * dist) + ((1.0 / (radius * radius)) * (dist * dist)));
  50.        
  51.         diffuse += D * attenuation;
  52.         specular += S * attenuation;
  53.         ambientColour += lightColour * attenuation;
  54.     }
  55.     vec3 ambience = 0.01 * ambientColour;
  56.  
  57.     Gouraud = ambience + diffuse + specular;
  58.     gl_Position = (projectionMatrix * (viewMatrix * modelMatrix)) * vec4(inPosition, 1.0);
  59. }
  60. ///////////////////////////////////////////////////////
  61. ////////////////////FRAGMENT SHADER////////////////////
  62. ///////////////////////////////////////////////////////
  63. #version 330
  64. precision mediump float;
  65.  
  66. in vec3 Gouraud;
  67. in vec2 texCoord;
  68.  
  69. layout (location = 0) out vec4 FragColour;
  70.  
  71. uniform sampler2D textureMap;
  72.  
  73. void main()
  74. {
  75.     vec3 albedo = texture(textureMap, texCoord).rgb;
  76.     vec3 final = albedo * Gouraud;
  77.     final = pow(final, vec3(1.0 / 2.2));
  78.     FragColour = vec4(final, 1.0);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement