# Binormals and tangents

I’m implementing BRDF in our 3d-engine, but I don’t know how to calculate binormals and tangents for a bunch of triangles.
I downloaded a demo from nvidia, but it uses patches, not triangles, no help there…

If it doesn’t work, keep trying Btw it’s a picture of a binding (from a cover)…

Thanks,
Morten Versvik

The binormal and tangent are vectors perpendicular to the normal and to eachother. Given the position and normal of a vertex, you can figure out the plane the vectors lie in, but not the specific directions that they point. For that you need more information.
The vectors should align themselves along and acrossed the “grain” of the material. I’m assuming the Nvidia demo uses the gradients of the patches to define this. If you don’t have patches but do have textures, you could use the gradient to the texture coordinate UV’s. If all you have is positions and normals… try picking some arbitrary but consistent vector, like the local z-axis, and do a cross product with your normal to get the tangent and then cross the tangent and the normal to get the binormal. It won’t correspond to anything specific, but at least it’s a start!
In the end, the tangent/binormal of a vertex is a separate property that realy should be specifically assigned, but I am not aware of any 3D modeling packages that currently support this concept.

-Cory

Originally posted by Morten:
[b]I’m implementing BRDF in our 3d-engine, but I don’t know how to calculate binormals and tangents for a bunch of triangles.
I downloaded a demo from nvidia, but it uses patches, not triangles, no help there…

If it doesn’t work, keep trying Btw it’s a picture of a binding (from a cover)…

Thanks,
Morten Versvik[/b]

do have textures, you could use the gradient to the texture coordinate UV’s.

what are the gradients (language problems… could you use the official language: c++ or pseudocode, plz )

its just i’m not far enought in math yet… i know what the tangent and binormal has to look like, but no change how to get it…

md2shader already has to deal with this problem. You might refer to its source code.

• Matt

sorry, but the md2shader is not my code not so nice to read, i think…

ok looks like ife found it…is it here in?

md2ComputeModelInstant(Md2ModelInstant *instant,
int frameA, int frameB, float frameBlendWeight,
Vector objectSpaceLightPosition[],
Vector objectSpaceEyePosition[])

if so, is it that?

``````/* Compute the partial derivatives of X, Y, and Z with respect to S and T. */
{
GLfloat vec0[4], vec1[4];  /* Five components: X, Y, Z, S, T. */

/* Need difference vectors to find cross product. */
vec0[0] = v[1][0] - v[0][0];
vec0[1] = v[1][1] - v[0][1];
vec0[2] = v[1][2] - v[0][2];
vec0[3] = stc[1][0] - stc[0][0];

vec1[0] = v[2][0] - v[0][0];
vec1[1] = v[2][1] - v[0][1];
vec1[2] = v[2][2] - v[0][2];
vec1[3] = stc[2][0] - stc[0][0];

pxpt = (vec0[3] * vec1[0] - vec0[0] * vec1[3]);
pypt = (vec0[3] * vec1[1] - vec0[1] * vec1[3]);
pzpt = (vec0[3] * vec1[2] - vec0[2] * vec1[3]);
invlen = invSqrt(pxpt*pxpt + pypt*pypt + pzpt*pzpt);
pxpt *= invlen;
pypt *= invlen;
pzpt *= invlen;
}``````

That’s it.

gradient == partial derivative == the amount the UV changed relative to the amount the XYZ changed between the vertices of the face