Images en lancer de rayons (à gauche) et en Z-Buffer sous OpenGL (à droite)
int Sphere::intersection(RayonLumineux *rl) {
double sx = rl->origine->c[0] - centre->c[0] ;
double sy = rl->origine->c[1] - centre->c[1];
double sz = rl->origine->c[2] - centre->c[2];
double a = rl->direction->c[0]*rl->direction->c[0] +
rl->direction->c[1]*rl->direction->c[1] +
rl->direction->c[2]*rl->direction->c[2];
double b = 2.0F*(rl->direction->c[0]*sx +
rl->direction->c[1]*sy +
rl->direction->c[2]*sz);
double c = sx*sx + sy*sy + sz*sz - rayon*rayon;
double delta = b*b-4*a*c;
if ( delta < 0.0F )
return(0);
if ( delta == 0 ) {
double t = -b/(2.0*a);
if ( t >= 0.000001) {
return(1); }
else
return(0); }
{ double t = (-b-sqrt(delta))/(2.0*a);
if ( t >= 0.000001 ) {
return(1); } }
{ double t = (-b+sqrt(delta))/(2.0*a);
if ( t >= 0.000001 ) {
return(1); } }
return(0);
}
int Sphere::intersection(RayonLumineux *rl,double *d) {
double sx = rl->origine->c[0] - centre->c[0] ;
double sy = rl->origine->c[1] - centre->c[1];
double sz = rl->origine->c[2] - centre->c[2];
double a = rl->direction->c[0]*rl->direction->c[0] +
rl->direction->c[1]*rl->direction->c[1] +
rl->direction->c[2]*rl->direction->c[2];
double b = 2.0F*(rl->direction->c[0]*sx +
rl->direction->c[1]*sy +
rl->direction->c[2]*sz);
double c = sx*sx + sy*sy + sz*sz - rayon*rayon;
double delta = b*b-4*a*c;
if ( delta < 0.0 )
return(0);
if ( delta == 0 ) {
double t = -b/(2.0*a);
if ( t >= 0.000001) {
*d = t;
return(1); }
else
return(0); }
{ double t = (-b-sqrt(delta))/(2.0*a);
if ( t >= 0.000001 ) {
*d = t;
return(1); } }
{ double t = (-b+sqrt(delta))/(2.0*a);
if ( t >= 0.000001 ) {
*d = t;
return(1); } }
return(0);
}