Advertisement
Guest User

Untitled

a guest
Apr 10th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* vertex shader */
  2. #shader vertex
  3. #version  330 core
  4.  
  5. layout(location = 0) in vec4 position_modelSpace;
  6. layout(location = 1) in vec2 UV;
  7. layout(location = 2) in vec3 normal_modelSpace;
  8. layout(location = 3) in vec3 tangent_modelSpace;
  9. layout(location = 4) in vec3 bitangent_modelSpace;
  10.  
  11. out struct VSout {
  12.     vec4 position_screenSpace;
  13.     vec2 UV;
  14.     mat3 worldToTangent;
  15. } v;
  16.  
  17. uniform mat4 toWorldSpace;
  18. uniform mat4 toViewSpace;
  19. uniform mat4 toScreenSpace;
  20.  
  21. void main()
  22. {
  23.     mat4 MVP = toScreenSpace * toViewSpace * toWorldSpace;
  24.     mat3 toWorldSpaceMAT3 = mat3(toWorldSpace);
  25.  
  26.     vec3 normal_worldSpace = toWorldSpaceMAT3 * normalize(normal_modelSpace);
  27.     vec3 tangent_worldSpace = toWorldSpaceMAT3 * normalize(tangent_modelSpace);
  28.     vec3 bitangent_worldSpace = toWorldSpaceMAT3 * normalize(bitangent_modelSpace);
  29.  
  30.     v.position_screenSpace = MVP * position_modelSpace;
  31.     v.UV = UV;
  32.     v.worldToTangent = transpose(mat3(
  33.         tangent_worldSpace,
  34.         bitangent_worldSpace,
  35.         normal_worldSpace
  36.     ));
  37.  
  38.     gl_Position = v.position_screenSpace;
  39. }
  40.  
  41. /* fragment shader */
  42. #shader fragment
  43. #version  330 core
  44.  
  45. in struct VSout {
  46.     vec4 position_screenSpace;
  47.     vec2 UV;
  48.     mat3 worldToTangent;
  49. } v;
  50.  
  51. out vec4 color;
  52.  
  53. uniform mat4 toWorldSpace;
  54. uniform mat4 toViewSpace;
  55. uniform mat4 toScreenSpace;
  56.  
  57. uniform sampler2D diffuseTexture;   //slot 0
  58. uniform sampler2D normalMap;        //slot 1
  59.  
  60. vec4 lightDir_worldSpace = -vec4(1.f, 1.f, 1.f, 0.f);
  61.  
  62. void main()
  63. {
  64.     vec4 normalMapRGB = texture(normalMap, v.UV);
  65.     vec3 normal_tangentSpace = normalize(normalMapRGB.xyz * 2.f - 1.f);
  66.     vec3 lightDir_tangentSpace = normalize(lightDir_worldSpace.xyz * v.worldToTangent);
  67.  
  68.     color = texture(diffuseTexture, v.UV);
  69.     color.rgb = color.rgb * clamp( dot(normal_tangentSpace, lightDir_tangentSpace), 0, 1 );
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement