Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. //shader version
  2. #version 150 core
  3.  
  4. uniform mat4 modelViewMatrix;
  5.  
  6. //inverse and transpose matrix for normals
  7. uniform mat4 normalMatrix;
  8.  
  9. //projectionMatrix*modelViewMatrix
  10. uniform mat4 modelViewProjectionMatrix;
  11.  
  12. //input vertex: position, normal, texture coordinates
  13. in vec3 pos;
  14. in vec3 nor;
  15. in vec2 tex;
  16.  
  17. //output vertex to fragment shader
  18. out VertexData
  19. {
  20.     vec3 diffus_color;
  21.     vec3 spec_color;
  22.     vec3 position;
  23.     vec3 normal;
  24.     vec2 texcoord;
  25. } VertexOut;
  26.  
  27. const vec3 light_pos = vec3(0, 5, 8);
  28. uniform mat4 viewMatrix;
  29.  
  30. const vec4 mat_ambient = vec4(0.2, 0.2, 0.2, 1.0);
  31. const vec4 mat_diffuse = vec4(0.7, 0.7, 0.7, 1.0);
  32. const vec4 mat_specular = vec4(0.5, 0.5, 0.5, 1.0);
  33. const float mat_shininess = 5.0;
  34. const float light_power = 7.0;
  35.  
  36. void main()
  37. {
  38.     gl_Position = modelViewProjectionMatrix*vec4(pos.xyz,1);
  39.  
  40.     VertexOut.position = vec3(modelViewMatrix*vec4(pos.xyz,1));
  41.     VertexOut.normal = vec3(normalMatrix*vec4(nor.xyz,1)); 
  42.     VertexOut.texcoord = vec2(tex.xy);
  43.  
  44.     vec3 normalDirection = normalize(vec3(normalMatrix * vec4(nor.xyz, 1)));
  45.     vec3 viewDirection = normalize(-vec3(modelViewMatrix * vec4(nor.xyz, 1)));
  46.  
  47.     vec3 vertexToLightSource = vec3(viewMatrix * vec4(light_pos, 1)) - VertexOut.position;
  48.     float distance = length(vertexToLightSource);
  49.     float attenuation = light_power / distance;
  50.  
  51.     vec3 lightDirection = normalize(vertexToLightSource);
  52.  
  53.     vec3 ambientLighting = vec3(mat_ambient);
  54.     vec3 diffuseReflection = attenuation * vec3(mat_diffuse) * max(0.0, dot(normalDirection, lightDirection));
  55.  
  56.     vec3 specularReflection;
  57.     if (dot(normalDirection, lightDirection) < 0.0) {
  58.        specularReflection = vec3(0.0, 0.0, 0.0);
  59.     } else {
  60.        specularReflection = attenuation * vec3(mat_specular)
  61.           * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)),
  62.           mat_shininess);
  63.     }
  64.  
  65.     VertexOut.diffus_color = ambientLighting + diffuseReflection;
  66.     VertexOut.spec_color = specularReflection;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement