Fitzy
Fitzy

Reputation: 1889

Calculating Per-Vertex Tangents for GLSL

Many answers I've seen online to similar questions provide calculations for a tangent-space matrix, but I would like to know how to calculate per-vertex tangents to send to shaders as a vertex attribute. I understand that the tangent for each vertex must be similar to neighboring vertices to avoid visual lighting artefacts, so arbitrary perpendicular vectors cannot be chosen against a normal.

Extra info- I have data such as normals, vertex positions and texture coordinates that I have obtained through a parser I wrote for .obj (wavefront) model files.

I can imagine this would be a relatively simple problem, but not being a mathematician or even particularly competent in the area, the answer does not jump out at me.

Upvotes: 4

Views: 1721

Answers (1)

Anteru
Anteru

Reputation: 19384

Just take the derivatives of the UV (texture-coordinates) to align your tangent spaces. I.e. one vector of your tangent space is the normal, and you can pick the others two freely. By rotating them such that they are coincident with the UV coordinate derivates, you get a smooth tangent field. However, that's just half of the story, as this is indeed a pretty tricky business. Here's some additional reading:

There are also multiple tangent spaces used in different applications, so you have to think about which one you want as well. For comprehensive coverage, search for the mikktspace library (mikktspace.h and mikktspace.c.)

Upvotes: 1

Related Questions