/* Sphere */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Mars 2014 */ #include #include #include #include #include #include "Sphere.h" #include "Position3D.h" #include "Materiel.h" #include "RayonLumineux.h" #include "LumierePonctuelle.h" #include "LumiereDirectionnelle.h" #include "Direction3D.h" #include "Energie.h" #include "Scene.h" Sphere::Sphere(void):Objet() { rayon = 1.0; } Sphere::Sphere(Position3D *p,double r,Materiel *m):Objet(p,m) { rayon = r; } Sphere::Sphere(Sphere *s):Objet(s) { rayon = s->rayon; } Sphere::~Sphere(void) { } void Sphere::print(void) { } 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); } void Sphere::calculNormale(Direction3D *d,Position3D *p) { d->c[0] = (p->c[0] - centre->c[0])/rayon; d->c[1] = (p->c[1] - centre->c[1])/rayon; d->c[2] = (p->c[2] - centre->c[2])/rayon; d->c[3] = 0.0; } void Sphere::modeliseOpenGL(void) { glutSolidSphere(rayon,180,180); } Sphere *Sphere::clone(void) { return(new Sphere(this)); }