/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2005 */ /* Un programme de deplacement d'une sphere */ /* par drag and drop et de controle clavier */ /* de la taille et de la position */ /* de cette sphere */ #include #include #include #include #include static int lumiere = 0; static int px = 100; static int py = 100; static int dx; static int dy; static int r = 10; static GLfloat couleurBlanc[] = { 1.0F,1.0F,1.0F,1.0F }; static GLfloat couleurRouge[] = { 1.0F,0.0F,0.0F,1.0F }; static GLfloat couleurVert[] = { 0.0F,1.0F,0.0F,1.0F }; static GLfloat couleurBleu[] = { 0.0F,0.0F,1.0F,1.0F }; static GLfloat couleurNoir[] = { 0.0F,0.0F,0.0F,1.0F }; static int dragdrop = 0; void init(void) { GLfloat mat_shininess[] = { 50.0F }; GLfloat light0_position[] = { 1.0F,1.0F,1.0F,0.0F }; GLfloat light1_position[] = { -1.0F,1.0F,1.0F,0.0F }; GLfloat light2_position[] = { 1.0F,-1.0F,1.0F,0.0F }; glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurRouge); glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurVert); glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurBleu); glLightfv(GL_LIGHT0,GL_POSITION,light0_position); glLightfv(GL_LIGHT1,GL_POSITION,light1_position); glLightfv(GL_LIGHT2,GL_POSITION,light2_position); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_LIGHT2); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_AUTO_NORMAL); glClearColor(0.0F,0.0F,0.0F,1.0F); } void scene(void) { glPushMatrix(); glTranslatef(px,py,0.0F); glutSolidSphere(r,60,60); glPopMatrix(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if ( lumiere ) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING); glPushMatrix(); scene(); glPopMatrix(); glFlush(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("\nAttention erreur %d\n",error); glutSwapBuffers(); } void souris(int bouton,int etat,int x,int y) { if ( etat == GLUT_DOWN ) if ( (px-x)*(px-x)+(py-y)*(py-y) < r*r ) { dx = px-x; dy = py-y; dragdrop = 1; } else { px = x; py = y; glutPostRedisplay(); } if ( etat == GLUT_UP ) dragdrop = 0; } void motion(int x,int y) { if ( dragdrop ) { px = x+dx; py = y+dy; glutPostRedisplay(); } } void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 43 : r++; glutPostRedisplay(); break; case 45 : r--; if ( r < 1.0F ) r = 1.0F; glutPostRedisplay(); break; case ' ' : lumiere = !lumiere; glutPostRedisplay(); break; case 0x1B : exit(0); break; } } void special(int key,int x,int y) { switch ( key ) { case GLUT_KEY_RIGHT : px++; glutPostRedisplay(); break; case GLUT_KEY_LEFT : px--; glutPostRedisplay(); break; case GLUT_KEY_UP : py--; glutPostRedisplay(); break; case GLUT_KEY_DOWN : py++; glutPostRedisplay(); break; } } void reshape(int w,int l) { glViewport(0,0,w,l); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,w,l,0,-100.0F,100.0F); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Controles souris et clavier"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutSpecialFunc(special); glutMouseFunc(souris); glutMotionFunc(motion); glutDisplayFunc(display); glutMainLoop(); return(0); }