/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Avril 2001 */ /* Illustration du clipping */ /* de Cohen-Sutherland */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleMenus.h" #include "ModuleReshape.h" struct point { int x ; int y ; } ; struct segment { point pi ; point pf ; } ; struct rectangle { point sg ; point id ; } ; static int aff = 0 ; static segment s = { -5,0,5,0 } ; static int p1 = 1 ; void traceSegment(int xi,int yi,int xf,int yf,float *c) { glColor4fv(c) ; glBegin(GL_LINES) ; glVertex2f((float) xi,(float) yi) ; glVertex2f((float) xf,(float) yf) ; glEnd() ; } void pixel(int x,int y,float *c) { glColor4fv(c) ; glBegin(GL_QUADS) ; glVertex2f(x-0.5F,y-0.5F) ; glVertex2f(x-0.5F,y+0.5F) ; glVertex2f(x+0.5F,y+0.5F) ; glVertex2f(x+0.5F,y-0.5F) ; glEnd() ; } void line(int xi,int yi,int xf,int yf,float *c) { int dx,dy,i,xinc,yinc,cumul,x,y ; x = xi ; y = yi ; dx = xf - xi ; dy = yf - yi ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixel(x,y,c) ; if ( dx > dy ) { cumul = dx / 2 ; for ( i = 1 ; i <= dx ; i++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; } pixel(x,y,c) ; } } else { cumul = dy / 2 ; for ( i = 1 ; i <= dy ; i++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } pixel(x,y,c) ; } } } void traceRectangle(rectangle *r,float *c) { line(r->id.x,r->id.y,r->id.x,r->sg.y,c) ; line(r->id.x,r->sg.y,r->sg.x,r->sg.y,c) ; line(r->id.x,r->id.y,r->sg.x,r->id.y,c) ; line(r->sg.x,r->id.y,r->sg.x,r->sg.y,c) ; } int code(point *p,rectangle *r) { int c ; if ( p->x < r->sg.x ) c = 1 ; else c = 0 ; if ( p->x > r->id.x ) c += 2 ; if ( p->y < r->sg.y ) c += 4 ; if ( p->y > r->id.y ) c += 8 ; return(c) ; } int code_nul(int c) { return(c == 0) ; } int pas1commun(int c1,int c2) { return((c1&c2) == 0) ; } int code0(int c) { return(c&1) ; } int code1(int c) { return(c&2) ; } int code2(int c) { return(c&4) ; } int code3(int c) { return(c&8) ; } int intersection(int ai,int bi,int af,int bf,int val) { int res = 0 ; if ( af-ai != 0 ) res =(int) (bi +(double) (val-ai) / (af-ai) * (bf-bi)) ; else res = 32767 ; return(res) ; } void clipperSegment(rectangle *r,segment *s,float *c) { segment se = *s ; int c1,c2 ; point p ; c1 = code(&s->pi,r) ; c2 = code(&s->pf,r) ; while ( ( !code_nul(c1) || !code_nul(c2) ) && ( pas1commun(c1,c2) ) ) { if ( code_nul(c1) ) { p = s->pi ; s->pi = s->pf ; s->pf = p ; c1 = c2 ; } if ( code0(c1) ) { s->pi.y = intersection(s->pi.x,s->pi.y,s->pf.x,s->pf.y,r->sg.x) ; s->pi.x = r->sg.x ; } else if ( code1(c1) ) { s->pi.y = intersection(s->pi.x,s->pi.y,s->pf.x,s->pf.y,r->id.x) ; s->pi.x = r->id.x ; } else if ( code2(c1) ) { s->pi.x = intersection(s->pi.y,s->pi.x,s->pf.y,s->pf.x,r->sg.y) ; s->pi.y = r->sg.y ; } else if ( code3(c1) ) { s->pi.x = intersection(s->pi.y,s->pi.x,s->pf.y,s->pf.x,r->id.y) ; s->pi.y = r->id.y ; } c1 = code(&s->pi,r) ; c2 = code(&s->pf,r) ; } if ( code_nul(c1) && code_nul(c2) ) line(s->pi.x,s->pi.y,s->pf.x,s->pf.y,c) ; *s = se ; } void display() { rectangle r = { -22,-18,21,16 } ; segment s1 = { -20,-23,6,1 } ; segment s2 = { -27,3,13,18 } ; segment s3 = { 24,13,28,-17 } ; segment s4 = { 13,8,-11,3 } ; segment s5 = { 26,-15,14,-27 } ; segment s6 = { 16,-7,16,21 } ; segment s7 = { -6,-18,9,-18 } ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); switch ( aff) { case 0 : traceRectangle(&r,couleurBleu()) ; line(s1.pi.x,s1.pi.y,s1.pf.x,s1.pf.y,couleurVert()) ; line(s2.pi.x,s2.pi.y,s2.pf.x,s2.pf.y,couleurRouge()) ; line(s3.pi.x,s3.pi.y,s3.pf.x,s3.pf.y,couleurJaune()) ; line(s4.pi.x,s4.pi.y,s4.pf.x,s4.pf.y,couleurNoir()) ; line(s5.pi.x,s5.pi.y,s5.pf.x,s5.pf.y,couleurBlanc()) ; line(s6.pi.x,s6.pi.y,s6.pf.x,s6.pf.y,couleurCyan()) ; line(s7.pi.x,s7.pi.y,s7.pf.x,s7.pf.y,couleurMagenta()) ; break ; case 1 : traceRectangle(&r,couleurBleu()) ; clipperSegment(&r,&s1,couleurVert()) ; clipperSegment(&r,&s2,couleurRouge()) ; clipperSegment(&r,&s3,couleurJaune()) ; clipperSegment(&r,&s4,couleurNoir()) ; clipperSegment(&r,&s5,couleurBlanc()) ; clipperSegment(&r,&s6,couleurCyan()) ; clipperSegment(&r,&s7,couleurMagenta()) ; break ; case 2 : traceRectangle(&r,couleurBleu()) ; clipperSegment(&r,&s,couleurRouge()) ; pixel(s.pi.x,s.pi.y,couleurJaune()) ; pixel(s.pf.x,s.pf.y,couleurJaune()) ; break ; } glPopMatrix(); glFlush(); glutSwapBuffers() ; } void special(int k, int x, int y) { switch (k) { case GLUT_KEY_UP : if ( p1 ) { s.pi.y++ ; if ( s.pi.y > 20 ) s.pi.y = 20 ; } else { s.pf.y++ ; if ( s.pf.y > 20 ) s.pf.y = 20 ; } glutPostRedisplay(); break; case GLUT_KEY_DOWN : if ( p1 ) { s.pi.y-- ; if ( s.pi.y < -20 ) s.pi.y = -20 ; } else { s.pf.y-- ; if ( s.pf.y < -20 ) s.pf.y = -20 ; } glutPostRedisplay(); break; case GLUT_KEY_LEFT : if ( p1 ) { s.pi.x-- ; if ( s.pi.x < -30 ) s.pi.x = -30 ; } else { s.pf.x-- ; if ( s.pf.x < -30 ) s.pf.x = -30 ; } glutPostRedisplay(); break; case GLUT_KEY_RIGHT : if ( p1 ) { s.pi.x++ ; if ( s.pi.x > 30 ) s.pi.x = 30 ; } else { s.pf.x++ ; if ( s.pf.x > 30 ) s.pf.x = 30 ; } glutPostRedisplay(); break; } } void key(unsigned char key,int x,int y) { switch ( key ) { case 0x0D : aff++ ; if ( aff == 3 ) aff = 0 ; glutPostRedisplay(); break; case 32 : p1 = !p1 ; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } void myinit() { glShadeModel(GL_SMOOTH); glClearColor(0.8F,0.8F,0.8F,1.0F); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Clipping par Cohen-Sutherland"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-30.0,30.0,-30.0,30.0,-500.0,500.0); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(special); glutDisplayFunc(display); glutMainLoop(); return(0); }