Advertisement
Guest User

Untitled

a guest
Sep 17th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //vert
  2.  
  3. #version 330 core
  4.  
  5. #define VERT_POSITION 0
  6. #define VERT_TEXCOORD 1
  7. #define VERT_NORMAL 2
  8. #define VERT_TANGENT 3
  9.  
  10. layout( location = VERT_POSITION ) in vec3 position;
  11. layout( location = VERT_TEXCOORD ) in vec2 texcoord;
  12. layout( location = VERT_NORMAL ) in vec3 normal;
  13. layout( location = VERT_TANGENT ) in vec3 tangent;
  14.  
  15. uniform struct Transform
  16. {
  17.     mat4 model;
  18.     mat4 viewProjection;
  19.     mat3 normal;
  20.     vec3 viewPosition;
  21. } transform;
  22.  
  23.  
  24. out Vertex
  25. {
  26.     vec2 texcoord;
  27.     vec3 normal;
  28.     vec3 fragPosition;
  29.     vec3 viewDir;
  30. } Vert;
  31.  
  32. out mat3 TBN_matrix;
  33.  
  34. void main(void)
  35. {
  36.     vec4 vertex = transform.model * vec4( position, 1.0 );
  37.     gl_Position = transform.viewProjection * vertex;
  38.    
  39.     vec3 t = normalize( ( transform.model * vec4( tangent, 0.0 ) ).xyz );
  40.     vec3 n = normalize( ( transform.model * vec4( normal, 0.0 ) ).xyz );
  41.     vec3 b = normalize( ( transform.model * vec4( cross( n, t ), 0.0 ) ).xyz );
  42.    
  43.     TBN_matrix = transpose( mat3( t, b, n ) );
  44.    
  45.     Vert.normal = ( transform.normal * normal );
  46.     Vert.fragPosition = ( transform.model * vec4( position, 1.0 ) ).xyz;
  47.     Vert.texcoord = texcoord;
  48.     Vert.viewDir = transform.viewPosition - Vert.fragPosition;
  49. }
  50.  
  51. //frag
  52.  
  53. #version 330 core
  54.  
  55. #define FRAG_OUTPUT0 0
  56.  
  57.  
  58. struct LightProperties
  59. {
  60.     bool isEnabled;
  61.     bool isLocal;
  62.     bool isSpot;
  63.    
  64.     vec4 position;
  65.     vec4 ambient;
  66.     vec4 diffuse;
  67.     vec4 specular;
  68.     vec3 attenuation;
  69.  
  70.     vec3 spotDirection;
  71.     float spotCosCutoff;
  72.     float spotExponent;
  73. };
  74.  
  75. struct Material
  76. {
  77.     vec4 ambient;
  78.     vec4 diffuse; //texture0
  79.     vec4 specular; //texture1
  80.     vec4 emission;
  81.     float shininess;
  82. };
  83.  
  84. uniform LightProperties light;
  85. uniform Material material;
  86. uniform sampler2D texture0; //diffuse
  87. uniform sampler2D texture1; //normal
  88.  
  89. in Vertex
  90. {
  91.     vec2 texcoord;
  92.     vec3 normal;
  93.     vec3 fragPosition;
  94.     vec3 viewDir;
  95. } Vert;
  96.  
  97. in mat3 TBN_matrix;
  98.  
  99. layout( location = FRAG_OUTPUT0 ) out vec4 frag_color;
  100.  
  101. float CalcReflect( vec3 norm, vec3 lightdir, vec3 viewdir, float shine );
  102. vec4 CalcLight( LightProperties light, Material material, vec3 normal, vec3 fragpos, vec3 viewdir );
  103.  
  104. void main(void)
  105. {
  106.     vec3 normal = normalize( ( TBN_matrix * ( 2.0 * texture( texture1, Vert.texcoord ).xyz - 1.0 ) ) );
  107.  
  108.     vec3 viewDir = normalize( Vert.viewDir );
  109.    
  110.     frag_color = vec4( 0.0 );
  111.  
  112.     frag_color = texture( texture0, Vert.texcoord ) * CalcLight( light, material, normal, Vert.fragPosition, viewDir );
  113. }
  114.  
  115. vec4 CalcLight( LightProperties light, Material material, vec3 normal, vec3 fragpos, vec3 viewdir )
  116. {
  117.     vec3 lightdir = ( light.position.xyz - fragpos );
  118.     float distance = length( lightdir );
  119.     lightdir = normalize( lightdir );
  120.    
  121.     vec4 output = vec4( 0.0 );
  122.    
  123.     output = material.emission;
  124.  
  125.     output += material.ambient * light.ambient;
  126.    
  127.     float diffuse = max( 0.0, dot( normal, lightdir ) );
  128.     if( diffuse > 0.0 )
  129.     {
  130.         output += material.diffuse * light.diffuse * diffuse;
  131.     }
  132.  
  133.     float specular = CalcReflect( normal, lightdir, viewdir, material.shininess );
  134.     if( specular > 0 )
  135.     {
  136.         output += material.specular * light.specular * specular;
  137.     }
  138.     return output;
  139. }
  140.  
  141. float CalcReflect( vec3 norm, vec3 lightdir, vec3 viewdir, float shine )
  142. {
  143.     return max( pow( dot( reflect( lightdir, norm ), viewdir ), shine ), 0.0 );
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement