Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. float f;
  2. vec3 edge1, edge2, tangentbuf;
  3. vec2 deltaUV1, deltaUV2;
  4. //for each face, x=vert,y=uv, z=normal
  5. for (size_t i = 0; i < totalrefs.size(); i += 9)
  6. {
  7. edge1 = verticeref[totalrefs[i + 3] - 1] - verticeref[totalrefs[i] - 1];
  8. edge2 = verticeref[totalrefs[i + 6] - 1] - verticeref[totalrefs[i] - 1];
  9. deltaUV1 = texkoordref[totalrefs[i + 4] - 1] - texkoordref[totalrefs[i + 1] - 1];
  10. deltaUV2 = texkoordref[totalrefs[i + 7] - 1] - texkoordref[totalrefs[i + 1] - 1];
  11. f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
  12.  
  13. tangentbuf = f*vec3(
  14. (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x),
  15. (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y),
  16. (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z));
  17.  
  18. normalize(tangentref[tangentcount++]);
  19. tangentref[totalrefs[i] - 1] += vec4(normalize(tangentbuf), 1);
  20. tangentref[totalrefs[i + 3] - 1] += vec4(normalize(tangentbuf), 1);
  21. tangentref[totalrefs[i + 6] - 1] += vec4(normalize(tangentbuf), 1);
  22. }
  23.  
  24. //for (auto &x : tangentref)x = normalize(x / x.w); //find average
  25. for (auto &x : tangentref)x = normalize(x); //just normalize is enough?
  26.  
  27. #version 400
  28. layout ( location = 0 ) in vec3 vertex_position;
  29. layout ( location = 1 ) in vec2 tex_cord;
  30. layout ( location = 2 ) in vec3 vertex_normal;
  31. layout ( location = 3 ) in vec3 vertex_tangent;
  32.  
  33. uniform mat4 modelviewmatrix; //view*model matrix
  34. uniform mat3 normalmatrix; // transpose/invert(view*model)
  35. uniform mat4 priv_mat; //projection-view-model pre multiplied
  36.  
  37. uniform vec4 LightPosition; //after view*lightpos, aka viewspace
  38.  
  39. out vec2 fUV; //fragment uv
  40. out vec3 lightdir; //tangent lightdir
  41. out vec3 viewdir; //tangent viewdir
  42.  
  43. void main()
  44. {
  45. vec3 norm = normalize(normalmatrix*vertex_normal);
  46. vec3 tang = normalize(normalmatrix*vec3(vertex_tangent));
  47. tang = normalize(tang - dot(tang, norm) * norm); //Gramm-Schmidt
  48. vec3 binormal = normalize(cross(norm,tang));
  49.  
  50. //matrix for transformation to tangent space
  51. mat3 tbn=mat3(tang,binormal,norm);
  52.  
  53. //turn light dir and view dir to tangent space
  54. vec3 pos = vec3(modelviewmatrix*vec4(vertex_position,1.0));
  55. lightdir = normalize(tbn*(LightPosition.xyz-pos));
  56. viewdir =tbn*normalize(-pos); //tangent view
  57.  
  58. fUV = tex_cord; //uv
  59. gl_Position = priv_mat*vec4(vertex_position,1.0); //model
  60. }
  61.  
  62. #version 400
  63.  
  64. in vec2 fUV;
  65. in vec3 lightdir; //tang light
  66. in vec3 viewdir; //tang view
  67.  
  68. uniform sampler2D png_tex;
  69. uniform sampler2D bump_map;
  70.  
  71. uniform vec3 LightIntensity;
  72.  
  73. uniform vec3 Kd; // Diffuse reflectivity
  74. uniform vec3 Ka; // Ambient reflectivity
  75. uniform vec3 Ks; // Specular reflectivity
  76. uniform float Shininess; // Specular shininess factor
  77.  
  78. out vec4 Color;
  79.  
  80. vec3 phongmodel(vec3 norm, vec3 diffr){
  81. vec3 r=reflect(-lightdir,norm);
  82. vec3 ambient = LightIntensity*Ka;
  83. float sdotn = max(dot(lightdir,norm),0.0);
  84. vec3 diffuse = LightIntensity*diffr*sdotn;
  85. vec3 spec = vec3(0.0);
  86. if(sdotn>0.0)
  87. spec = LightIntensity*Ks*pow(max(dot(r,viewdir),0.0),Shininess);
  88. return ambient+diffuse+spec;
  89. }
  90.  
  91. void main()
  92. {
  93. vec4 bump_normal=texture(bump_map, fUV);
  94. vec4 tex_color=texture(png_tex,fUV);
  95. Color = vec4(phongmodel(bump_normal.xyz,tex_color.rgb),1.0);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement