/* Clipping de Cohen-Sutherland */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2011 */ #include #include #include #include #include #include "Rectangle2D.h" #include "Position2D.h" #include "Segment2D.h" /* Variables et constantes globales */ static int scn = 0; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ void init(void) { } /* Scene dessinee */ static void drawRectangle(Rectangle2D *r) { glPushMatrix(); glBegin(GL_LINE_LOOP); glVertex2d(r->ig->c[0],r->ig->c[1]); glVertex2d(r->sd->c[0],r->ig->c[1]); glVertex2d(r->sd->c[0],r->sd->c[1]); glVertex2d(r->ig->c[0],r->sd->c[1]); glEnd(); glPopMatrix(); } static void drawSegment(Segment2D *s) { glPushMatrix(); glBegin(GL_LINES); glVertex2d(s->pi->c[0]+0.4999,s->pi->c[1]+0.4999); glVertex2d(s->pf->c[0]+0.4999,s->pf->c[1]+0.4999); glEnd(); glPopMatrix(); } void sceneAvecClipping(void) { Position2D *p1 = new Position2D(100.0,80.0); Position2D *p2 = new Position2D(300.0,220.0); Rectangle2D *r = new Rectangle2D(p1,p2); delete(p1); delete(p2); glPushMatrix(); glColor3f(1.0F,1.0F,1.0F); drawRectangle(r); glPopMatrix(); for ( int i = 0 ; i < 100 ; i++ ) { Position2D *pi = new Position2D(rand()%400,rand()%300); Position2D *pf = new Position2D(rand()%400,rand()%300); Segment2D *s = new Segment2D(pi,pf); delete(pi); delete(pf); glColor3f(1.0F,0.0F,0.0F); if ( s->clip(r) ) drawSegment(s); delete(s); } delete(r); } void sceneSansClipping(void) { Position2D *p1 = new Position2D(100.0,80.0); Position2D *p2 = new Position2D(300.0,220.0); Rectangle2D *r = new Rectangle2D(p1,p2); delete(p1); delete(p2); glPushMatrix(); glColor3f(1.0F,1.0F,1.0F); drawRectangle(r); glPopMatrix(); for ( int i = 0 ; i < 100 ; i++ ) { Position2D *pi = new Position2D(rand()%400,rand()%300); Position2D *pf = new Position2D(rand()%400,rand()%300); Segment2D *s = new Segment2D(pi,pf); delete(pi); delete(pf); glColor3f(1.0F,0.0F,0.0F); drawSegment(s); delete(s); } delete(r); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ void reshape(int tx,int ty) { glViewport(0,0,tx,ty); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,tx,0,ty,-20.0,20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(0.5F,0.5F,0.5F,0.5F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); srand(1); glPushMatrix(); if ( scn ) sceneAvecClipping(); else sceneSansClipping(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur : %d\n",error); } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier */ void keyboard(unsigned char key,int x,int y) { switch (key) { case 0x0D : scn = (scn+1)%2; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(400,300); glutInitWindowPosition(50,50); glutCreateWindow("Clipping de Cohen-Sutherland"); init(); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }