Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. #vertex shader
  2. #version 330 core
  3. layout(location = 0) in vec3 layout_vert_position;
  4. layout(location = 1) in vec3 layout_vert_normal;
  5. layout(location = 2) in vec2 layout_vert_text_coords;
  6.  
  7. uniform mat4 u_ModelViewMatrix;
  8. uniform mat4 u_CameraViewMatrix;
  9. uniform mat4 u_CameraProjectionMatrix;
  10. uniform vec3 u_CameraPosition;
  11.  
  12. out vec3 fs_vert_position;
  13. out vec3 fs_camera_position;
  14. out vec3 fs_vert_normal;
  15. out vec2 fs_texture_coord;
  16.  
  17. void main()
  18. {
  19.     vec4 vert_position = u_ModelViewMatrix * vec4(layout_vert_position, 1.0);
  20.     vec3 vert_normal = mat3(transpose(inverse(u_ModelViewMatrix))) * layout_vert_normal;
  21.     vec3 norm = normalize(vert_normal);
  22.     vec4 world_position = u_CameraViewMatrix * vert_position;
  23.     gl_Position = u_CameraProjectionMatrix * world_position;
  24.    
  25.     // send data to fragment shader
  26.     fs_vert_position = vert_position.xyz;
  27.     fs_camera_position = u_CameraPosition;
  28.     fs_vert_normal = norm;
  29.     fs_texture_coord = layout_vert_text_coords;
  30. }
  31.  
  32. #fragment shader
  33. #version 330 core
  34. out vec4 out_color;
  35.  
  36. in vec3 fs_vert_position;
  37. in vec3 fs_camera_position;
  38. in vec3 fs_vert_normal;
  39. in vec2 fs_texture_coord;
  40.  
  41. uniform vec3 u_MaterialKa;
  42. uniform vec3 u_MaterialKd;
  43. uniform vec3 u_MaterialKs;
  44. uniform float u_MaterialNs;
  45. uniform float u_MaterialNi;
  46. uniform float u_MaterialD;
  47.  
  48. uniform sampler2D u_MaterialMapKa;
  49. uniform sampler2D u_MaterialMapKd;
  50. uniform sampler2D u_MaterialMapKs;
  51.  
  52. #define MAX_LIGHTS 4
  53. uniform vec3 u_Gamma;
  54.  
  55. uniform float u_FogDensity;
  56. uniform float u_FogGradient;
  57. uniform vec4 u_FogColor;
  58.  
  59. struct PointLight
  60. {
  61.     vec3 position;
  62.     vec3 ka;
  63.     vec3 kd;
  64.     vec3 ks;
  65.     float constant;
  66.     float linear;
  67.     float quadratic;
  68. };
  69. uniform PointLight u_PointLights[MAX_LIGHTS];
  70. uniform int u_NumPointLights;
  71.  
  72. struct DirLight
  73. {
  74.     vec3 direction;
  75.  
  76.     vec3 ka;
  77.     vec3 kd;
  78.     vec3 ks;
  79. };
  80. uniform DirLight u_DirectionalLight;
  81.  
  82. struct SpotLight
  83. {
  84.     vec3 position;
  85.     vec3 direction;
  86.     float cutOff;
  87.     float outerCutOff;
  88.  
  89.     float constant;
  90.     float linear;
  91.     float quadratic;
  92.  
  93.     vec3 ka;
  94.     vec3 kd;
  95.     vec3 ks;
  96. };
  97. uniform int u_NumSpotLights;
  98. uniform SpotLight u_SpotLights[MAX_LIGHTS];
  99.  
  100. vec3 calc_directional_light(DirLight light, vec3 normal, vec3 view_direction)
  101. {
  102.     vec3 light_direction = normalize(-light.direction);
  103.     // diffuse shading
  104.     float diff = max(dot(normal, light_direction), 0.0);
  105.     // specular shading
  106.     vec3 reflect_direction = reflect(-light_direction, normal);
  107.     float spec = pow(max(dot(view_direction, reflect_direction), 0.0), u_MaterialNs);
  108.     // combine results
  109.     vec3 ambient = light.ka * vec3(texture(u_MaterialMapKa, fs_texture_coord));
  110.     vec3 diffuse = light.kd * diff * vec3(texture(u_MaterialMapKd, fs_texture_coord));
  111.     vec3 specular = light.ks * spec * vec3(texture(u_MaterialMapKs, fs_texture_coord));
  112.     return (ambient + diffuse + specular);
  113. }
  114.  
  115. vec3 calc_point_light(PointLight light, vec3 normal, vec3 vert_position, vec3 view_direction)
  116. {
  117.     vec3 light_direction = normalize(light.position - vert_position);
  118.     // diffuse shading
  119.     float diff = max(dot(normal, light_direction), 0.0);
  120.     // specular shading
  121.     vec3 reflect_direction = reflect(-light_direction, normal);
  122.     float spec = pow(max(dot(view_direction, reflect_direction), 0.0), u_MaterialNs);
  123.     // attenuation
  124.     float distance = length(light.position - vert_position);
  125.     float attenuation = 1.0 / (light.constant + light.linear * distance +
  126.         light.quadratic * (distance * distance));
  127.     // combine results
  128.     vec3 ambient = light.ka * vec3(texture(u_MaterialMapKa, fs_texture_coord));
  129.     vec3 diffuse = light.kd * diff * vec3(texture(u_MaterialMapKd, fs_texture_coord));
  130.     vec3 specular = light.ks * spec * vec3(texture(u_MaterialMapKs, fs_texture_coord));
  131.     ambient *= attenuation;
  132.     diffuse *= attenuation;
  133.     specular *= attenuation;
  134.     return (ambient + diffuse + specular);
  135. }
  136.  
  137. vec3 calc_spot_light(SpotLight light, vec3 normal, vec3 vert_position, vec3 view_direction)
  138. {
  139.     vec3 light_direction = normalize(light.position - vert_position);
  140.     // diffuse shading
  141.     float diff = max(dot(normal, light_direction), 0.0);
  142.     // specular shading
  143.     vec3 reflect_direction = reflect(-light_direction, normal);
  144.     float spec = pow(max(dot(view_direction, reflect_direction), 0.0), u_MaterialNs);
  145.     // attenuation
  146.     float distance = length(light.position - vert_position);
  147.     float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
  148.     // spotlight intensity
  149.     float theta = dot(light_direction, normalize(-light.direction));
  150.     float epsilon = light.cutOff - light.outerCutOff;
  151.     float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
  152.     // combine results
  153.     vec3 ambient = light.ka * vec3(texture(u_MaterialMapKa, fs_texture_coord));
  154.     vec3 diffuse = light.kd * diff * vec3(texture(u_MaterialMapKd, fs_texture_coord));
  155.     vec3 specular = light.ks * spec * vec3(texture(u_MaterialMapKs, fs_texture_coord));
  156.     ambient *= attenuation * intensity;
  157.     diffuse *= attenuation * intensity;
  158.     specular *= attenuation * intensity;
  159.     return (ambient + diffuse + specular);
  160. }
  161.  
  162. void main()
  163. {
  164.     // properties
  165.     vec3 view_direction = normalize(fs_camera_position - fs_vert_position);
  166.     vec3 result = u_MaterialKa + u_MaterialKd + u_MaterialKs;
  167.  
  168.     // phase 1: Directional lighting
  169.     result = calc_directional_light(u_DirectionalLight, fs_vert_normal, view_direction);
  170.     // phase 2: Point lights
  171.     for (int i = 0; i < u_NumPointLights; i++)
  172.     {
  173.         result += calc_point_light(u_PointLights[i], fs_vert_normal, fs_vert_position, view_direction);
  174.     }
  175.     // phase 3: Spot lights
  176.     for (int i = 0; i < u_NumSpotLights; i++)
  177.     {
  178.         result += calc_spot_light(u_SpotLights[i], fs_vert_normal, fs_vert_position, view_direction);
  179.     }
  180.     // TODO : Fog
  181.     out_color = vec4(result, 1.0);
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement