/* Clipping de Cohen-Sutherland */ /* d'un segment de droite vis a vis */ /* d'un quadrilatere quelconque */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Decembre 2012 */ #include #include #include #include #include #include #include "Position2D.h" #include "Direction2D.h" #include "Segment2D.h" #include "Quadrilatere2D.h" /* Variables et constantes globales */ static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F }; static const float magenta[] = { 1.0F,0.0F,1.0F,1.0F }; static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; static const float vert[] = { 0.0F,0.7F,0.0F,1.0F }; static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F }; static int zoom = 11; static int aff = 1; static Position2D *q1 = NULL; static Position2D *q2 = NULL; static Position2D *q3 = NULL; static Position2D *q4 = NULL; static Position2D *s1 = NULL; static Position2D *s2 = NULL; static void clean(void) { if ( q1 ) { delete(q1); q1 = NULL; } if ( q2 ) { delete(q2); q2 = NULL; } if ( q3 ) { delete(q3); q3 = NULL; } if ( q4 ) { delete(q4); q4 = NULL; } if ( s1 ) { delete(s1); s1 = NULL; } if ( s2 ) { delete(s2); s2 = NULL; } } /* Scene dessinee */ static void fond(void) { glColor3fv(bleu); int tx = (glutGet(GLUT_WINDOW_WIDTH)+(zoom-1))/zoom; int ty = (glutGet(GLUT_WINDOW_HEIGHT)+(zoom-1))/zoom; for ( int x = 0 ; x <= tx ; x++ ) for ( int y = 0 ; y <= ty ; y++ ) { glBegin(GL_LINE_LOOP); glVertex2i(x*zoom,y*zoom); glVertex2i(x*zoom+(zoom-1),y*zoom); glVertex2i(x*zoom+(zoom-1),y*zoom+(zoom-1)); glVertex2i(x*zoom,y*zoom+(zoom-1)); glEnd(); } } static void scene(void) { glColor4fv(jaune); Quadrilatere2D *q = new Quadrilatere2D(q1,q2,q3,q4); q->draw(zoom); Segment2D *s = new Segment2D(s1,s2); glColor4fv(rouge); s->draw(zoom); if ( q->clip(s) ) { glColor4fv(vert); s->draw(zoom); } delete(s); delete(q); } static void scene2(void) { glColor4fv(jaune); Quadrilatere2D *q = new Quadrilatere2D(q1,q2,q3,q4); q->zoom(zoom); q->drawGL(); Segment2D *s = new Segment2D(s1,s2); s->zoom(zoom); glColor4fv(rouge); s->drawGL(); if ( q->clip(s) ) { glColor4fv(vert); s->drawGL(); } delete(s); delete(q); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ static void display(void) { glClearColor(0.8F,0.8F,0.8F,1.0F); glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); switch(aff) { case 0 : fond(); scene(); break; case 1 : scene2(); break; } glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur OpenGL: %d\n",error); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* -> Ajustement de la camera de visualisation */ static void reshape(int x,int y) { glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,x,0.0,y,-1000.0,1000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction executee lors de la frappe */ /* d'une touche du clavier */ static void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 43 : zoom++; glutPostRedisplay(); break; case 45 : zoom--; if ( zoom == 0 ) zoom = 1; glutPostRedisplay(); break; case 0x20 : s1->c[0] = 2+rand()%40; s1->c[1] = 2+rand()%24; s2->c[0] = 2+rand()%40; s2->c[1] = 2+rand()%24; glutPostRedisplay(); break; case 0x0D : aff = !aff; glutPostRedisplay(); break; case 0x1B : clean(); exit(0); break; } } /* Fonction principale */ int main(int argc,char **argv) { atexit(clean); q1 = new Position2D(31.0,11.0); q2 = new Position2D(32.0,24.0); q3 = new Position2D(11.0,17.0); q4 = new Position2D(14.0,9.0); s1 = new Position2D( 9.0, 3.0); s2 = new Position2D(29.0,26.0); glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); glutInitWindowSize(480,320); glutInitWindowPosition(50,50); glutCreateWindow("Clipping de Cohen-Sutherland"); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }