/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2004 */ /* Algorithme de Bresenham */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleMenus.h" #include "ModuleFont.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 int pt = 0 ; static segment s[8] = { { 2,10,27,19 }, { 5, 9,28, 4 }, { 25, 3, 8,16 }, { 28,13, 4, 4 }, { 12, 3,17,19 }, { 13,18,19, 4 }, { 18, 3,15,16 }, { 18,18,14, 4 } } ; static int f1; static int f2; void pixel(int x,int y,float *c) { glColor4fv(c) ; glBegin(GL_QUADS) ; glVertex2f(x-1.0F,y-1.0F) ; glVertex2f(x-1.0F,y) ; glVertex2f(x,y) ; glVertex2f(x,y-1.0F) ; 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 dessineQuadrillage(void) { float i; glColor4fv(couleurGrisMoyen()) ; glBegin(GL_LINES); for ( i = 0 ; i <= 30 ; i++ ) { glVertex3d(i,0,-10.0); glVertex3d(i,20,-10.0); } for ( i = 0 ; i <= 20 ; i++ ) { glVertex3d(0,i,-10.0); glVertex3d(30,i,-10.0); } glEnd() ; } void dessinSegment(segment *s) { pixel(s->pi.x,s->pi.y,couleurRouge()); pixel(s->pf.x,s->pf.y,couleurVert()); line(s->pi.x,s->pi.y,s->pf.x,s->pf.y,couleurBleu(0.3)); } void display1() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); dessineQuadrillage(); switch (aff) { case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : case 7 : dessinSegment(&s[aff]); break ; } glPopMatrix(); glFlush(); glutSwapBuffers() ; } void affichePosition(segment *s) { float pos = 1.0F; glColor4fv(couleurNoir()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"PI :%3d %3d",s->pi.x,s->pi.y) ; pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"PF :%3d %3d",s->pf.x,s->pf.y) ; } void display2() { glClearColor(0.5F,0.5F,0.5F,1.0F); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); switch ( aff) { case 0 : case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : case 7 : affichePosition(&s[aff]); break ; } glPopMatrix(); glutSwapBuffers(); } void special(int key,int x,int y) { switch ( key ) { case GLUT_KEY_RIGHT : if ( pt ) s[aff].pi.x++; else s[aff].pf.x++; glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); break; case GLUT_KEY_LEFT : if ( pt ) s[aff].pi.x--; else s[aff].pf.x--; glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); break; case GLUT_KEY_UP : if ( pt ) s[aff].pi.y++; else s[aff].pf.y++; glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); break; case GLUT_KEY_DOWN : if ( pt ) s[aff].pi.y--; else s[aff].pf.y--; glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); break; } } void key(unsigned char key,int x,int y) { switch ( key ) { case 0x20 : pt = !pt; break; case 0x0D : aff = (aff+1)%8 ; glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); break; case 0x1B : exit(0); break; } } void reshape2(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,w,-h,0,-1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void myinit() { glEnable(GL_ALPHA_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); 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(250,168); glutInitWindowPosition(50,50); f1 = glutCreateWindow("Bresenham"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-0.5,20.5,-0.5,20.5,-50.0,50.0); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(special); glutDisplayFunc(display1); glutInitWindowSize(120,50); glutInitWindowPosition(60,340); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); f2 = glutCreateWindow("Valeurs"); creationMenuBasique(); glutDisplayFunc(display2); glutReshapeFunc(reshape2); glutKeyboardFunc(key); glutSpecialFunc(special); glutMainLoop(); return(0); }