/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Mars 2002 */ /* Rasterisation d'un segment de droite */ /* sur un ecran bitmap */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleMenus.h" #include "ModuleReshape.h" struct coord2D { double x; double y; } ; struct segment { coord2D pi; coord2D pf; } ; int aff = 0; static segment seg = { { 5,3 },{ 35,16 } }; void pixel(int x, int y) { glBegin(GL_QUADS) ; glVertex2i(x,y) ; glVertex2i(x+1,y) ; glVertex2i(x+1,y+1) ; glVertex2i(x,y+1) ; glEnd() ; } void lignePixels(int xi,int yi,int xf,int yf) { int i,cumul ; int x = xi ; int y = yi ; int dx = xf - xi ; int dy = yf - yi ; int xinc = ( dx > 0 ) ? 1 : -1 ; int yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixel(x,y) ; 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) ; } } else { cumul = dy / 2 ; for ( i = 1 ; i <= dy ; i++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } pixel(x,y) ; } } } void dessineSegment(float *c,segment *s) { glColor4fv(c) ; glLineWidth(3.0) ; glBegin(GL_LINES) ; glVertex2d(s->pi.x+0.5,s->pi.y+0.5); glVertex2d(s->pf.x+0.5,s->pf.y+0.5); glEnd() ; glLineWidth(1.0) ; } void dessineQuadrillage(float *c) { int i; glColor4fv(c) ; glBegin(GL_LINES); for ( i = 1 ; i < 20 ; i++ ) { glVertex2d(1.0,i); glVertex2d(39.0,i); } for ( i = 1 ; i < 40 ; i++ ) { glVertex2d(i,1.0); glVertex2d(i,19.0); } glEnd() ; } void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); switch (aff) { case 0 : dessineSegment(couleurRouge(),&seg) ; break ; case 1 : dessineQuadrillage(couleurBleu()) ; dessineSegment(couleurRouge(),&seg) ; break ; } glPopMatrix(); glFlush(); glutSwapBuffers() ; } void key(unsigned char key,int x,int y) { switch ( key ) { case 0x1B : exit(0); break; case 0x0D : aff = (aff+1)%2; glutPostRedisplay(); break; } } void myinit() { 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(320,160); glutInitWindowPosition(50,50); glutCreateWindow("Rasterisation d'un segment"); myinit(); creationMenuBasique(); setParametresOrthoBasique(0.0,20.0,0.0,20.0,-50.0,50.0); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutDisplayFunc(display); glutMainLoop(); return(0); }