Calculer la normale d'une surface
Bonjour,
Je suis entrain d'écrire un module pour la gestion de la lumière dans une scène OpenGL. Je suis obligé de calculer la normale de chaque surface pour avoir un rendu réaliste. Pour ce faire, j'ai écrit une pétite fonction pour claculer la normale d'une surface. Elle a comme paramètres les coordonnées x, y et z de 3 points de la surface concernée comme sit:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
double[] SurfaceNormal(double point1X,double point1Y,double point1Z,double point2X,double point2Y,double point2Z,double point3X,double point3Y,double point3Z)
{
//The first vector corresponding to P1 and P2
double v1[]= new double[3];
// // The second vector corresponding to P1 and P3
double v2[]= new double[3];
// The result vector
double v[]= new double[3];
// The first vector corresponding to the first and the second points of the surface
v1[0]=point1X-point2X;// X1
v1[1]=point1Y-point2Y;// Y1
v1[2]=point1Z-point2Z;// Z1
// The second vector corresponding to the second and the third points of the surface
v2[0]=point1X-point3X;;// X2
v2[1]=point1Y-point3Y;// Y2
v2[2]=point1Z-point3Z;// Z2
// We calculate the vector V : vector product of v1 and v2
v[0]=v1[1]*v2[2]- v1[2]*v2[1];// Y1*Z2-Z1*Y2
v[1]=v1[2]*v2[0]-v1[0]*v2[2];//Z1*X2-X1*Z2
v[2]=v1[0]*v2[1]-v1[1]*v2[0];//X1*Y2-Y1*X2
// We normalize the vector V
double var=Math.sqrt(Math.pow(v[0], 2)+Math.pow(v[1], 2)+Math.pow(v[2], 2));
v[0]=-v[0]/var;
v[1]=-v[1]/var;
v[2]=-v[2]/var;
return v;
} |
J'obtient un rendu bizare (surfaces qui n'apparaissenet pas, surface blanches, sufaces grises et surfaces noires), donc je me demande si le calcul de la normale que j'effectue est correct ou non? si non quels sont les causes probables de mon problème? Merci d'avance.