#include "facet.h" #include #include #include #include using namespace std; static void erreur(char *mesg) { cout << "ERREUR dans facet.cpp :" << mesg << endl; exit(0); } void Facet::calculerNormale() { ItVertex itv=av.begin(); Point3d s1=(**(itv++)).point(); Point3d s2=(**(itv++)).point(); Point3d s3=(**(itv++)).point(); Vecteur3d v1(s1,s2); Vecteur3d v2(s2,s3); n=prodVect(v1,v2); float dist=sqrt(prodScal(n,n)); if (dist<1e-05) { printf("facette anormale : norme nulle\n"); if (itv!=av.end()) { s3=(**(itv++)).point(); Vecteur3d v3(s2,s3); n=prodVect(v1,v3); dist=sqrt(prodScal(n,n)); } } else { n.scale(1.0/dist); } } void Facet::tracer2() { ItVertex itv; glBegin(GL_POLYGON); int k=0; for(itv=av.begin();itv!=av.end();itv++) { Vertex *v=*itv; if (an.size()!=0) { glNormal3fv(an[k]->fv()); k++; } else glNormal3fv(v->normal().fv()); glVertex3fv(v->point().fv()); } glEnd(); } void Facet::tracer() { tracer2(); } void Facet::clean() { } Facet::~Facet() { } void AFacet::tracer(bool grille) { ItFacet itf; if (grille) { glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1,1); } for(itf=begin();itf!=end();itf++) { (*itf)->tracer(); } if (grille) { glPushAttrib(GL_LIGHTING_BIT); glDisable(GL_POLYGON_OFFSET_FILL); glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glDisable(GL_LIGHTING); glColor3f(0.0,0.0,0.0); glLineWidth(2.0); for(itf=begin();itf!=end();itf++) { (*itf)->tracer(); } glLineWidth(1.0); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glPopAttrib(); } } void AFacet::clean() { for(ItFacet i=begin();i!=end();i++) { delete (*i); } } AFacet::~AFacet() { } void Objet::tracer(bool grille) { af.tracer(grille); } void Objet::gonfler(float k) { ItVertex ita; for(ita=av.begin();ita!=av.end();ita++) { (*ita)->setPoint((*ita)->point()+::scale((*ita)->normal(),k)); } } bool minx(Vertex *p1, Vertex *p2) { return p1->point()[0]point()[0]; } bool maxx(Vertex *p1,Vertex *p2) { return p1->point()[0]point()[0]; } float Objet::minBox(int coord) { ItVertex ita; ita=min_element(av.begin(),av.end(),minx); return (*ita)->point()[0]; } float Objet::maxBox(int coord) { ItVertex ita; ita=max_element(av.begin(),av.end(),maxx); return (*ita)->point()[0]; } void Objet::adaptBox(float x1,float x2,float y1,float y2, float z1, float z2) { float min_x=minBox(0); float max_x=maxBox(0); cout << "min_x = " << min_x << endl; cout << "max_x = " << max_x << endl; translate(-(min_x+max_x)/2.0,0,0); float kscale=(x2-x1)/(max_x-min_x); scale(kscale); translate((x1+x2)/2.0,0,0); cout << "scale = " << kscale << endl; cout << "nouveau min_x = " << minBox(0) << endl; cout << "nouveau max_x = " << maxBox(0) << endl; } void Objet::tracerNormale() { ItVertex ita; Point3d pp; for(ita=av.begin();ita!=av.end();ita++) { glBegin(GL_LINES); glVertex3fv((*ita)->point().fv()); pp=(*ita)->point()+::scale((*ita)->normal(),0.1); glVertex3fv(pp.fv()); glEnd(); } } void Objet::scale(float k) { ItVertex itv; for(itv=av.begin();itvscale(k); } } void Objet::translate(float x,float y, float z) { ItVertex itv; for(itv=av.begin();itvtranslate(x,y,z); } } Objet::~Objet() { normale.clean(); af.clean(); } void AFacet::calculerNormale() { ItFacet itf; for(itf=begin();itf!=end();itf++) { (*itf)->calculerNormale(); } } void Objet::calculerNormaleSommet() { ItFacet itf; ItVertex itv; af.calculerNormale(); for(itv=av.begin();itv!=av.end();itv++) { (*itv)->addNormalBegin(); } for(itf=af.begin();itf!=af.end();itf++) { for(itv=(*itf)->av.begin();itv!=(*itf)->av.end();itv++) { (*itv)->addNormal((*itf)->normal()); } } for(itv=av.begin();itv!=av.end();itv++) { // printf("%f,%f,%f\n",(*ita)->n.x,(*ita)->n.y,(*ita)->n.z); (*itv)->addNormalEnd(); } } void initA(AFacet &l) { ItFacet itf; for(itf=l.begin();itf!=l.end();itf++) { (*itf)->A=(*((*itf)->av.begin()))->point(); } } void tracer(Facet &f) { f.tracer2(); }