Bob.Killer Site Administrator

Inscrit le: 19 Jan 2008 Messages: 53 Localisation: Lyon
|
Posté le: Dim Jan 20, 2008 17:50 Sujet du message: [3D] Calcul de la matrice TBN Tangent Binormal Normal |
|
|
La matrice TBN sert a calculer l'espace tangent d'un polygone. Celui-ci peut servir à faire du bump mapping à l'aide d'une normal map.
Code: | #include <iostream>
#include "Vector.h"
// Structure d'un vertex et de ses coordonn?es de texture
struct Vecteur3st {
float x,y,z;
float s,t;
};
// Calcul la tangente & la binormal au polygon
void ComputeTangent(Vecteur3st v0, Vecteur3st v1, Vecteur3st v2, Vector *tangent, Vector *binormal)
{
// On calcul les diff?rents vecteurs pour effectuer le calcul
Vector vx0(v0.x, v0.s, v0.t);
Vector vx1(v1.x, v1.s, v1.t);
Vector vx2(v2.x, v2.s, v2.t);
Vector vy0(v0.y, v0.s, v0.t);
Vector vy1(v1.y, v1.s, v1.t);
Vector vy2(v2.y, v2.s, v2.t);
Vector vz0(v0.z, v0.s, v0.t);
Vector vz1(v1.z, v1.s, v1.t);
Vector vz2(v2.z, v2.s, v2.t);
// Calcul des 3 equation de plan
Vector abc0((vx0-vx1) ^ (vx0-vx2));
Vector abc1((vy0-vy1) ^ (vy0-vy2));
Vector abc2((vz0-vz1) ^ (vz0-vz2));
if(tangent)
{
tangent->VecteurNew(-abc0.y/abc0.x,-abc1.y/abc1.x, -abc2.y/abc2.x);
tangent->normalize();
}
if(binormal)
{
binormal->VecteurNew(-abc0.z/abc0.x,-abc1.z/abc1.x, -abc2.z/abc2.x);
binormal->normalize();
}
}
// Fonction principale
int main()
{
// vertex
Vector V0(0.0f, 0.0f, 0.0f);
Vector V1(1.0f, 0.0f, 0.0f);
Vector V2(0.0f, 1.0f, 0.0f);
// vertex et coord texture
Vecteur3st v0={V0.x, V0.y, V0.z, 0.0f, 0.0f };
Vecteur3st v1={V1.x, V1.y, V1.z, 1.0f, 0.0f };
Vecteur3st v2={V2.x, V2.y, V2.z, 0.0f, 1.0f };
// On cr?e les vecteurs
Vector tangent, binormal, normal;
// on les calcul
ComputeTangent(v0, v1, v2, &tangent, &binormal);
normal = ((V1-V0)^(V2-V0));
normal.normalize();
// affichage du r?sultat
printf("Tangente : x : %f, y : %f, z : %f\n",tangent.x, tangent.y, tangent.z);
printf("Binormal : x : %f, y : %f, z : %f\n",binormal.x, binormal.y, binormal.z);
printf("Normal : x : %f, y : %f, z : %f\n",normal.x, normal.y, normal.z);
return 0;
} |
_________________ Site de programmation C/C++, OpenGL, DirectX et Cg, HLSL.
 |
|