Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 150
  2.  
  3. uniform sampler2D tex;
  4. uniform sampler2D etex;
  5. uniform sampler2D stex;
  6. uniform sampler2D ntex;
  7. uniform vec3 lightPos;
  8. uniform vec3 eyePos;
  9. uniform float roughness;
  10.  
  11. in vec2 v_texcoord;
  12. in vec3 v_normal;
  13. in vec3 v_tangent;
  14. in vec3 v_objPos;
  15. in vec3 v_worldPos;
  16. in mat4 v_worldMatrix;
  17.  
  18. out vec4 color;
  19.  
  20. void main(){
  21.     vec3 N = normalize(v_normal);
  22.     vec4 q = texture(ntex, v_texcoord);
  23.     q = (q - (1/2.0)) * 2.0;
  24.     vec3 T = normalize(v_tangent);
  25.     vec3 B = cross(N,T);
  26.     mat3 Q = mat3(T.x, B.x, N.x, T.y, B.y, N.y, T.z, B.z, N.z);
  27.     vec3 newNormal = q.rgb * Q;
  28.     newNormal = normalize(newNormal);
  29.     newNormal = (vec4(newNormal,0) * v_worldMatrix).xyz;
  30.     vec3 L = normalize(lightPos - v_worldPos);
  31.     vec3 V = normalize(eyePos - v_worldPos);
  32.     vec3 R = reflect( -L,newNormal);
  33.     float diffuse = clamp( dot(newNormal,L), 0.0, 1.0 );
  34.  
  35.      float dp = clamp( dot(L,N) , 0.0, 1.0);
  36.  
  37.     //float specular = pow( max( 0.0, dot(V,R)) , 32.0 ); //hardcoded exponent
  38.     //specular *= sign(diffuse);  //no specular if no diffuse
  39.  
  40.     //Cook-Torrance Specular:
  41.     vec3 specmtl = texture(stex, v_texcoord).rgb;
  42.     vec3 specular;
  43.     float r = roughness;
  44.     vec3 one = vec3(1.0);
  45.     vec3 H = normalize(0.5*(L+V));
  46.     vec3 sp = min( specmtl, vec3(0.95));
  47.     vec3 sqrtk = sqrt(sp);
  48.     vec3 n = (-one - sqrtk) / (sqrtk - one);
  49.     vec3 cos_a = vec3(dot(N,V));
  50.     vec3 cos_b = vec3(dot(N,L));
  51.     vec3 cos_c = vec3(dot(V,H));
  52.     vec3 cos_d = vec3(dot(N,H));
  53.     vec3 q2 = sqrt(cos_c*cos_c - one + n*n);
  54.     vec3 f1 = q2 - cos_c;
  55.     vec3 f2 = q2 + cos_c;
  56.     vec3 f3 = (f2 * cos_c)-one;
  57.     vec3 f4 = (f1 * cos_c)+one;
  58.     vec3 Q1 = f1/f2;
  59.     Q1*=Q1;
  60.     vec3 Q2 = f3/f4;
  61.     Q2*=Q2;
  62.     vec3 F=vec3(0.5)*Q1*(vec3(1.0)+Q2);
  63.     float cos2d = cos_d[0]*cos_d[0];
  64.     float t = r*(1.0-1.0/cos2d);
  65.     float M;
  66.    
  67.     float rt = r;
  68.     float rb = rt*0.5;
  69.     //0,0,0 = center of object. Could change (have as
  70.     //a uniform).
  71.     vec3 objPos = vec3(0.0, 0.0, 0.0);
  72.     vec3 v = normalize(v_objPos - vec3(0,0,0));
  73.     float c = dot(v,vec3(0.0,0.0,-1.0));
  74.     float s = sqrt(1.0-c);
  75.     vec3 TT = vec3(c, 0.0 ,sign(v_objPos.z)*s);
  76.     vec3 H_ = normalize( H + -dot(N,H)*N );
  77.     float cos_g = dot(T,H_);
  78.     float cos2g = cos_g * cos_g;
  79.     float tt = (rt*rt*cos2g + rb*rb*(1.0-cos2g))*(1.0 - 1.0/cos2d);
  80.     M = (rt * rb * exp(t)) / (4.0*cos2d*cos2d);
  81.  
  82.     //M = r*exp(t)/(4.0*cos2d*cos2d);
  83.  
  84.     float A = clamp(2.0*cos_d[0]*min(cos_a[0], cos_b[0])/cos_c[0], 0.0, 1.0);
  85.     specular = vec3(M)*F*vec3(A)/(cos_a*cos_b*vec3(3.14159265358979323));
  86.     specular *= sign(dp);
  87.    
  88.     //color.rgb = newNormal;
  89.     color = texture(tex,v_texcoord);
  90.     color.rgb *= diffuse;
  91.     color.rgb += specular * texture(stex,v_texcoord).rgb;
  92.     color.rgb += texture(etex,v_texcoord).rgb;
  93.    
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement