/* Bresenham */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2012 */ #include #include #include #include #include #include /* 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 xi = 8; static int yi = 18; static int xf = 37; static int yf = 12; static int aff = 0; /* Scene dessinee */ void allumePixel(int x,int y) { glPointSize(9.0F); glBegin(GL_POINTS); glVertex2i(x*11+4,y*11+4); glEnd(); glPointSize(1.0F); } ////////////////////////////////////////////////// void ligne1(int xi,int yi,int xf,int yf) { int dx,dy,i,xinc,yinc,cumul,x,y,x1; 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); allumePixel(xi,yi); if ( dx > dy ) { cumul = dx / 2; for ( i = 1 ; i <= dx ; i++ ) { x += xinc; cumul += dy; if ( cumul >= dx ) { if ( ( y != yi ) && ( y != yf ) ) { allumePixel((x1+x-xinc)/2,y); } cumul -= dx; y += yinc; x1 = x; } } allumePixel(xf,yf); } else { cumul = dy / 2; for ( i = 1 ; i <= dy ; i++ ) { y += yinc; cumul += dx; if ( cumul >= dy ) { cumul -= dy; x += xinc; } allumePixel(x,y); } } } void ligne2(int xi,int yi,int xf,int yf) { 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); allumePixel(x,y); cumul = dy / 2; for ( i = 1 ; i <= dy ; i++ ) { y += yinc; cumul += dx; while ( cumul >= dy ) { cumul -= dy; x += xinc; } allumePixel(x,y); } if ( yi == yf ) allumePixel(xf,yf); } ////////////////////////////////////////////////// void trace(int xi,int yi,int xf,int yf) { 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); allumePixel(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; } allumePixel(x,y); } } else { cumul = dy / 2; for ( i = 1 ; i <= dy ; i++ ) { y += yinc; cumul += dx; if ( cumul >= dy ) { cumul -= dy; x += xinc; } allumePixel(x,y); } } } static void fond(void) { glColor3fv(bleu); int tx = (glutGet(GLUT_WINDOW_WIDTH)+10)/11; int ty = (glutGet(GLUT_WINDOW_HEIGHT)+10)/11; for ( int x = 0 ; x <= tx ; x++ ) for ( int y = 0 ; y <= ty ; y++ ) { glBegin(GL_LINE_LOOP); glVertex2i(x*11,y*11); glVertex2i(x*11+10,y*11); glVertex2i(x*11+10,y*11+10); glVertex2i(x*11,y*11+10); glEnd(); } } static void scene(void) { glColor3fv(jaune); trace(xi,yi,xf,yf); glColor3fv(vert); switch (aff) { case 0 : ligne1(xi,yi,xf,yf); break; case 1 : ligne2(xi,yi,xf,yf); break; } } /* 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(); fond(); scene(); 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 0x20 : aff = !aff; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } /* Fonction executee lors de la frappe */ /* d'une touche speciale du clavier */ static void special(int key,int x,int y) { switch ( key ) { case GLUT_KEY_UP : if ( glutGetModifiers() == GLUT_ACTIVE_SHIFT ) yi++; else yf++; glutPostRedisplay(); break; case GLUT_KEY_DOWN : if ( glutGetModifiers() == GLUT_ACTIVE_SHIFT ) yi--; else yf--; glutPostRedisplay(); break; case GLUT_KEY_RIGHT : if ( glutGetModifiers() == GLUT_ACTIVE_SHIFT ) xi++; else xf++; glutPostRedisplay(); break; case GLUT_KEY_LEFT : if ( glutGetModifiers() == GLUT_ACTIVE_SHIFT ) xi--; else xf--; glutPostRedisplay(); break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); glutInitWindowSize(480,320); glutInitWindowPosition(50,50); glutCreateWindow("Bresenham"); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }