/* Affichage d'un quadrillage mimant un ecran */ /* bitmap et de 3 pixels sur cet "ecran" */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Avril 2020 */ #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 taillePixel = 16; static int aff = 0; /* Affichage d'un carre materialisant un pixel */ /* place en coordonnees (x,y). */ /* Le pixel de coordonnees (0,0) est en bas a gauche. */ /* L'axe x est oriente vers la droite. */ /* L'axe y est oriente vers le haut. */ static void pixel(int x,int y) { glBegin(GL_QUADS); glVertex2i(5+x*taillePixel,5+y*taillePixel); glVertex2i(5+x*taillePixel+taillePixel-1,5+y*taillePixel); glVertex2i(5+x*taillePixel+taillePixel-1,5+y*taillePixel+taillePixel-1); glVertex2i(5+x*taillePixel,5+y*taillePixel+taillePixel-1); glEnd(); } /* Affichage d'un quadrillage mimant un ecran bitmap */ /* Les pixels sont carres et ont taillePixel pixels */ /* de cote. */ static void quadrillage(void) { int tx = (glutGet(GLUT_WINDOW_WIDTH)-10)/taillePixel; int ty = (glutGet(GLUT_WINDOW_HEIGHT)-10)/taillePixel; glBegin(GL_LINES); for ( int x = 0 ; x <= tx ; x++ ) { glVertex2i(5+x*taillePixel,5); glVertex2i(5+x*taillePixel,5+ty*taillePixel); } for ( int y = 0 ; y <= ty ; y++ ) { glVertex2i(5,5+y*taillePixel); glVertex2i(5+tx*taillePixel,5+y*taillePixel); } glEnd(); } /* 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(); glColor3fv(bleu); quadrillage(); glColor3fv(jaune); pixel(9,2); glColor3fv(rouge); pixel(4,19); glColor3fv(vert); pixel(17,13); 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 0x0D : aff = (aff+1)%2; 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("Affichage quadrillage"); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return(0); }