/* Remplissage 2D d'une facette triangulaire */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 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 }; ////////////////////////////////////////////////// /* Affichage des coordonnees des pixels */ /* allumes lors du trace d'un segment de droite */ /* par algorithme de Bresenham */ static void affichageSegment(int *px,int *py) { int dx,dy,i,xinc,yinc,cumul,x,y; x = px[0]; y = py[0]; dx = px[1] - px[0]; dy = py[1] - py[0]; xinc = ( dx > 0 ) ? 1 : -1; yinc = ( dy > 0 ) ? 1 : -1; dx = abs(dx); dy = abs(dy); printf("%3d %3d\n",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; } printf("%3d %3d\n",x,y); } } else { cumul = dy / 2; for ( i = 1 ; i <= dy ; i++ ) { y += yinc; cumul += dx; if ( cumul >= dy ) { cumul -= dy; x += xinc; } printf("%3d %3d\n",x,y); } } } static int mx[2] = { -1,-1 }; static int my[2] = { -1,-1 }; static int nb = 0; static int bt = 0; /* Fonction appelee lors d'un clic de souris */ static void mouse(int bouton,int etat,int x,int y) { if ( bouton == GLUT_LEFT_BUTTON ) { mx[nb%2] = x; my[nb%2] = y; bt = 1; if ( etat == GLUT_UP ) { printf("%4d %4d\n",x,y); bt = 0; nb++; } glutPostRedisplay(); } if ( bouton == GLUT_RIGHT_BUTTON ) { if ( etat == GLUT_UP ) { if ( mx[1] != -1 ) affichageSegment(mx,my); } } } /* Fonction appelee lors d'un deplacement */ /* de souris bouton appuye */ static void motion(int x,int y) { if ( bt ) { mx[nb%2] = x; my[nb%2] = y; printf("%4d %4d\n",x,y); glutPostRedisplay(); } } /* Scene dessinee */ static int aff = 1; static void scene(void) { glPushMatrix(); if ( mx[0] != -1 ) { glPushMatrix(); glTranslatef(mx[0],my[0],0.0F); glutSolidSphere(10.0,36,36); glPopMatrix(); } if ( mx[1] != -1 ) { glPushMatrix(); glTranslatef(mx[1],my[1],0.0F); glutSolidSphere(10.0,36,36); glPopMatrix(); if ( aff ) { glBegin(GL_LINES); glVertex2f(mx[0],my[0]); glVertex2f(mx[1],my[1]); glEnd(); } } glPopMatrix(); } ////////////////////////////////////////////////// /* 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(); 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,y,0.0,-100.0,100.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 ' ' : aff = !aff; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); glutInitWindowSize(350,350); glutInitWindowPosition(50,50); glutCreateWindow("Gestion de la souris"); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }