Talking about bump mapping, specular highlight and these kind of things in OpenGL Shading Language (GLSL)
I have:
- An array of vertices (e.g. {0.2,0.5,0.1, 0.2,0.4,0.5, ...})
- An array of normals (e.g. {0.0,0.0,1.0, 0.0,1.0,0.0, ...})
- The position of a point light in world space (e.g. {0.0,1.0,-5.0})
- 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)
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?
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!
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.
---- Update -----
I found this solution:
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);
But I don't know if it is 100% correct.
See Question&Answers more detail:os