/* Clipping d'un segment de droite */ /* dans un rectangle */ /* par l'algorithme de Cohen-Sutherland */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2010 */ #include #include #include #include #include #include #include "Position2D.h" #include "Segment2D.h" #include "Rectangle2D.h" #include "Trace.h" /* Variables globales */ static int sc = 0; /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection en parallele orthographique */ void reshape(int tx,int ty) { glViewport(0,0,tx,ty); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho((-tx/2)/10.0,(-tx/2+tx)/10.0, (-ty/2)/10.0,(-ty/2+ty)/10.0, -1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(0.6F,0.6F,0.6F,1.0F) ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ; Position2D *ps11 = new Position2D(-15.0,9.0); Position2D *ps12 = new Position2D(10.0,-10.0); Segment2D *s1 = new Segment2D(ps11,ps12); Position2D *ps21 = new Position2D(3.0,2.0); Position2D *ps22 = new Position2D(15.0,6.0); Segment2D *s2 = new Segment2D(ps21,ps22); Position2D *ps31 = new Position2D(-17.0,-5.0); Position2D *ps32 = new Position2D(-7.0,-13.0); Segment2D *s3 = new Segment2D(ps31,ps32); Position2D *pr1 = new Position2D(-12.0,-7.0); Position2D *pr2 = new Position2D(11.0,5.0); Rectangle2D *r = new Rectangle2D(pr1,pr2); glPushMatrix(); glColor3f(0.0F,0.0F,1.0F); r->trace(); if ( ( sc == 0 ) || ( sc == 2 ) ) { glColor3f(1.0F,0.0F,0.0F); s1->trace(); s2->trace(); s3->trace(); } if ( ( sc == 1 ) || ( sc == 2 ) ) { glColor3f(0.0F,1.0F,0.0F); if ( s1->clip(r) ) s1->trace(); if ( s2->clip(r) ) s2->trace(); if ( s3->clip(r) ) s3->trace(); } glPopMatrix(); delete(s1); delete(r); delete(ps11); delete(ps12); delete(ps21); delete(ps22); delete(ps31); delete(ps32); delete(pr1); delete(pr2); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } /* Fonction executee lors de la frappe */ /* d'une touche alphanumerique du clavier */ void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 0x0D : sc = (sc+1)%3; glutPostRedisplay(); break; case 0x1B : exit(0); } } /* Fonction d'initialisation d'OpenGL */ void init(void) { glPointSize(10.0F); } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(400,300); glutInitWindowPosition(50,50); glutCreateWindow("Cohen-Sutherland"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return(0); }