Bob.Killer Site Administrator

Inscrit le: 19 Jan 2008 Messages: 53 Localisation: Lyon
|
Posté le: Dim Jan 20, 2008 17:46 Sujet du message: [3D] Fonction pour calculer les normales |
|
|
Afin de pouvoir calculer l'éclairage d'une forme, nous avons besoin d'avoir ses normales. Voici une fonction qui peut le faire.
Code: | void Vecteur_Unite(float vector[3])
{
float length;
// Calcul de la norme du vecteur
length = (float)sqrt((vector[0]*vector[0]) + (vector[1]*vector[1]) + (vector[2]*vector[2]));
if(length == 0.0f) length = 1.0f; //évite une violente erreur !!!
vector[0] /= length;
vector[1] /= length;
vector[2] /= length;
}
// Points p1, p2, & p3 spécifiés dans le sens trigonométrique
void Normale(float v[3][3], float out[3])
{
float v1[3],v2[3];
static const int x = 0;
static const int y = 1;
static const int z = 2;
// Calcul de 2 vecteurs à partir des 3 points
v1[x] = v[0][x] - v[1][x];
v1[y] = v[0][y] - v[1][y];
v1[z] = v[0][z] - v[1][z];
v2[x] = v[1][x] - v[2][x];
v2[y] = v[1][y] - v[2][y];
v2[z] = v[1][z] - v[2][z];
// calcul du produit vectoriel
out[x] = (v1[y]*v2[z] - v1[z]*v2[y]);
out[y] = (v1[z]*v2[x] - v1[x]*v2[z]);
out[z] = (v1[x]*v2[y] - v1[y]*v2[x]);
// on le réduit à un vecteur unité
Vecteur_Unite(out);
} |
_________________ Site de programmation C/C++, OpenGL, DirectX et Cg, HLSL.
 |
|