/* Un programme OpenGL de rasterisation */ /* d'un cercle */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2009 */ #include #include #include #include #include static int scn = 0; static int r = 50; /* Scene dessinee */ void pixelsSymetriques(int x,int y) { glVertex2f(x,y); glVertex2f(-x,y); glVertex2f(x,-y); glVertex2f(-x,-y); glVertex2f(y,x); glVertex2f(-y,x); glVertex2f(y,-x); glVertex2f(-y,-x); } void cercle(int r) { int x,y,d; x = 0; y = r; d = 1 - r; glBegin(GL_POINTS); pixelsSymetriques(x,y); while ( y > x ) { if ( d < 0 ) d += 2 * x + 3; else { d += 2 * (x - y) + 5; y--; } x++; pixelsSymetriques(x,y); } glEnd(); } void scene1(void) { glPushMatrix(); glColor3f(0.0F,0.0F,0.0F); cercle(r); glPopMatrix(); } void disque(int r) { int *tx =(int *) calloc(r+1,sizeof(int)); int x,y,d; x = 0; y = r; d = 1 - r; tx[y] = x; tx[x] = y; while ( y > x ) { if ( d < 0 ) d += 2 * x + 3; else { d += 2 * (x - y) + 5; y--; } x++; tx[y] = x; tx[x] = y; } glBegin(GL_LINES); glVertex2i(tx[0],0); glVertex2i(-tx[0],0); for ( int i = 1 ; i <= r ; i++ ) { glVertex2i(tx[i],i); glVertex2i(-tx[i],i); glVertex2i(tx[i],-i); glVertex2i(-tx[i],-i); } glEnd(); free(tx); } void scene2(void) { glPushMatrix(); glColor3f(0.0F,0.0F,0.0F); disque(r); glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(1.0F,1.0F,1.0F,1.0F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); if ( scn == 0 ) scene1(); else scene2(); glPopMatrix(); glFlush(); glutSwapBuffers(); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ void reshape(int x,int y) { glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION) ; glLoadIdentity() ; glOrtho(-x/2,-x/2+x,-y/2,-y/2+y,-300.0,300.0) ; glMatrixMode(GL_MODELVIEW) ; glLoadIdentity() ; } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier */ void keyboard(unsigned char key,int x,int y) { switch (key) { case 43 : r++; glutPostRedisplay(); break; case 45 : r--; if ( r == 0 ) r = 1; else glutPostRedisplay(); break; case 0x0D : scn = !scn; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(240,240); glutInitWindowPosition(50,50); glutCreateWindow("Rasterisation d'un cercle"); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }