title: How to calculate Tangent and Binormal?
body: <p>Talking about bump mapping, specular highlight and these kind of things in OpenGL Shading Language (GLSL)</p>
  
  <p>I have:</p>
  
  <ul>
  <li>An array of vertices (e.g. {0.2,0.5,0.1, 0.2,0.4,0.5, ...})</li>
  <li>An array of normals (e.g. {0.0,0.0,1.0, 0.0,1.0,0.0, ...})</li>
  <li>The position of a point light in world space (e.g. {0.0,1.0,-5.0})</li>
  <li>The position of the viewer in world space (e.g. {0.0,0.0,0.0}) (assume the viewer is in the center of the world)</li>
  </ul>
  
  <p>Now, how can I calculate the Binormal and Tangent for each vertex? I mean, what is the formula to calculate the Binormals, what I have to use based on those informations? And about the tangent?</p>
  
  <p>I'll construct the TBN Matrix anyway, so if you know a formula to construct the matrix directly based on those informations will be nice!</p>
  
  <p>Oh, yeh, I have the texture coordinates too, if needed.
  And as I'm talking about GLSL, would be nice a per-vertex solution, I mean, one which doesn't need to access more than one vertex information at a time.</p>
  
  <p>---- Update -----</p>
  
  <p>I found this solution:</p>
  
  <pre>
  vec3 tangent;
  vec3 binormal;
  
  vec3 c1 = cross(a_normal, vec3(0.0, 0.0, 1.0));
  vec3 c2 = cross(a_normal, vec3(0.0, 1.0, 0.0));
  
  if (length(c1)>length(c2))
  {
      tangent = c1;
  }
  else
  {
      tangent = c2;
  }
  
  tangent = normalize(tangent);
  
  binormal = cross(v_nglNormal, tangent);
  binormal = normalize(binormal);
  </pre>
  
  <p>But I don't know if it is 100% correct.</p>